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