1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef JPEGDECODER_H
17 #define JPEGDECODER_H
18 
19 #include <fstream>
20 #include <mutex>
21 #include <map>
22 #include "codec_jpeg_helper.h"
23 #include "command_parse.h"
24 #include "v1_0/display_composer_type.h"
25 #include "v1_0/display_buffer_type.h"
26 #include "v1_0/include/idisplay_buffer.h"
27 #include "v2_0/icodec_image.h"
28 
29 class JpegDecoder {
30 public:
31     JpegDecoder();
32 
33     ~JpegDecoder();
34 
35     int32_t OnEvent(int32_t error);
36 
37     int32_t Init();
38 
39     int32_t DeInit();
40 
41     int32_t PrepareData(std::string fileInput, std::string fileOutput);
42 
43     int32_t AllocBuffer(uint32_t width, uint32_t height);
44 
45     int32_t Decode(CommandOpt opt);
46 
47 private:
AlignUp(uint32_t width)48     uint32_t inline AlignUp(uint32_t width)
49     {
50         return (((width) + alignment_ - 1) & (~(alignment_ - 1)));
51     }
52 
53 private:
54     OHOS::sptr<OHOS::HDI::Codec::Image::V2_0::ICodecImage> hdiJpeg_;
55     OHOS::HDI::Display::Buffer::V1_0::IDisplayBuffer *hdiBuffer_;
56     OHOS::HDI::Codec::Image::V2_0::CodecJpegHelper *helper_ = nullptr;
57     OHOS::HDI::Codec::Image::V2_0::CodecImageBuffer inBuffer_;
58     OHOS::HDI::Codec::Image::V2_0::CodecImageBuffer outBuffer_;
59     std::unique_ptr<char[]> jpegBuffer_;
60     std::ifstream ioIn_;
61     std::ofstream ioOut_;
62     uint32_t dataStart_;
63     uint32_t bufferLen_;
64     uint32_t compDataLen_;
65     OHOS::HDI::Codec::Image::V2_0::CodecJpegDecInfo decInfo_;
66     std::unique_ptr<int8_t[]> compressBuffer_;
67     uint32_t alignment_ = 16;
68 };
69 #endif // JPEGDECODER_H
70