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 "hilog/log.h" 27 #include "icc_profile_info.h" 28 #include "jpeg_utils.h" 29 #include "jpeglib.h" 30 #include "log_tags.h" 31 #include "plugin_class_base.h" 32 #include "plugin_server.h" 33 #include "exif_info.h" 34 35 namespace OHOS { 36 namespace ImagePlugin { 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, PlSize &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 getGrColorSpace(); 70 bool IsSupportICCProfile(); 71 #endif 72 73 private: 74 DISALLOW_COPY_AND_MOVE(JpegDecoder); 75 bool ParseExifData(); 76 J_COLOR_SPACE GetDecodeFormat(PlPixelFormat format, PlPixelFormat &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 89 static MultimediaPlugin::PluginServer &pluginServer_; 90 jpeg_decompress_struct decodeInfo_; 91 JpegSrcMgr srcMgr_; 92 ErrorMgr jerr_; 93 AbsImageDecompressComponent *hwJpegDecompress_ = nullptr; 94 JpegDecodingState state_ = JpegDecodingState::UNDECIDED; 95 uint32_t streamPosition_ = 0; // may be changed by other decoders, record it and restore if needed. 96 PlPixelFormat outputFormat_ = PlPixelFormat::UNKNOWN; 97 PixelDecodeOptions opts_; 98 EXIFInfo exifInfo_; 99 ICCProfileInfo iccProfileInfo_; 100 }; 101 } // namespace ImagePlugin 102 } // namespace OHOS 103 104 #endif // JPEG_DECODER_H 105