1 /*
2  * Copyright (C) 2021 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 applicable 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 JPEG_DECODER_H
17 #define JPEG_DECODER_H
18 
19 #include <cstdint>
20 #include <string>
21 #include "abs_image_decoder.h"
22 #include "abs_image_decompress_component.h"
23 #ifdef IMAGE_COLORSPACE_FLAG
24 #include "color_space.h"
25 #endif
26 #include "icc_profile_info.h"
27 #include "jpeg_utils.h"
28 #include "jpeglib.h"
29 #include "plugin_class_base.h"
30 #include "plugin_server.h"
31 #include "exif_info.h"
32 
33 namespace OHOS {
34 namespace ImagePlugin {
35 using namespace Media;
36 
37 enum class JpegDecodingState : int32_t {
38     UNDECIDED = 0,
39     SOURCE_INITED = 1,
40     BASE_INFO_PARSING = 2,
41     BASE_INFO_PARSED = 3,
42     IMAGE_DECODING = 4,
43     IMAGE_ERROR = 5,
44     IMAGE_PARTIAL = 6,
45     IMAGE_DECODED = 7
46 };
47 
48 class JpegDecoder : public AbsImageDecoder, public OHOS::MultimediaPlugin::PluginClassBase {
49 public:
50     JpegDecoder();
51     ~JpegDecoder() override;
52     void SetSource(InputDataStream &sourceStream) override;
53     void Reset() override;
54     uint32_t SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info) override;
55     uint32_t Decode(uint32_t index, DecodeContext &context) override;
56     uint32_t GetImageSize(uint32_t index, Size &size) override;
57     uint32_t PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &context) override;
58     uint32_t GetImagePropertyInt(uint32_t index, const std::string &key, int32_t &value) override;
59     uint32_t GetImagePropertyString(uint32_t index, const std::string &key, std::string &value) override;
60     uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value,
61         const std::string &path) override;
62     uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value,
63         const int fd) override;
64     uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value,
65         uint8_t *data, uint32_t size) override;
66     uint32_t GetFilterArea(const int &privacyType, std::vector<std::pair<uint32_t, uint32_t>> &ranges) override;
67 
68 #ifdef IMAGE_COLORSPACE_FLAG
69     OHOS::ColorManager::ColorSpace GetPixelMapColorSpace() override;
70     bool IsSupportICCProfile() override;
71 #endif
72 
73 private:
74     DISALLOW_COPY_AND_MOVE(JpegDecoder);
75     bool ParseExifData();
76     J_COLOR_SPACE GetDecodeFormat(PixelFormat format, PixelFormat &outputFormat);
77     void CreateHwDecompressor();
78     uint32_t DoSwDecode(DecodeContext &context);
79     void FinishOldDecompress();
80     uint32_t DecodeHeader();
81     uint32_t StartDecompress(const PixelDecodeOptions &opts);
82     uint32_t GetRowBytes();
83     void CreateDecoder();
84     bool IsMarker(uint8_t rawPrefix, uint8_t rawMarkderCode, uint8_t markerCode);
85     bool FindMarker(InputDataStream &stream, uint8_t marker);
86     ExifTag getExifTagFromKey(const std::string &key);
87     void FormatTimeStamp(std::string &value, std::string &src);
88     uint32_t GetImagePropertyString(const std::string &key, std::string &value);
89     uint32_t GetImagePropertyStringEx(const std::string &key, std::string &value);
90     uint32_t GetMakerImagePropertyString(const std::string &key, std::string &value);
91 
92     static MultimediaPlugin::PluginServer &pluginServer_;
93     jpeg_decompress_struct decodeInfo_;
94     JpegSrcMgr srcMgr_;
95     ErrorMgr jerr_;
96     AbsImageDecompressComponent *hwJpegDecompress_ = nullptr;
97     JpegDecodingState state_ = JpegDecodingState::UNDECIDED;
98     uint32_t streamPosition_ = 0;  // may be changed by other decoders, record it and restore if needed.
99     PixelFormat outputFormat_ = PixelFormat::UNKNOWN;
100     PixelDecodeOptions opts_;
101     EXIFInfo exifInfo_;
102     ICCProfileInfo iccProfileInfo_;
103 };
104 } // namespace ImagePlugin
105 } // namespace OHOS
106 
107 #endif // JPEG_DECODER_H
108