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; GetPluginType()67 std::string GetPluginType() override 68 { 69 return "jpeg"; 70 } 71 72 #ifdef IMAGE_COLORSPACE_FLAG 73 OHOS::ColorManager::ColorSpace GetPixelMapColorSpace() override; 74 bool IsSupportICCProfile() override; 75 #endif 76 77 private: 78 DISALLOW_COPY_AND_MOVE(JpegDecoder); 79 bool ParseExifData(); 80 J_COLOR_SPACE GetDecodeFormat(PixelFormat format, PixelFormat &outputFormat); 81 void CreateHwDecompressor(); 82 uint32_t DoSwDecode(DecodeContext &context); 83 void FinishOldDecompress(); 84 uint32_t DecodeHeader(); 85 uint32_t StartDecompress(const PixelDecodeOptions &opts); 86 uint32_t GetRowBytes(); 87 void CreateDecoder(); 88 bool IsMarker(uint8_t rawPrefix, uint8_t rawMarkderCode, uint8_t markerCode); 89 bool FindMarker(InputDataStream &stream, uint8_t marker); 90 ExifTag getExifTagFromKey(const std::string &key); 91 void FormatTimeStamp(std::string &value, std::string &src); 92 uint32_t GetImagePropertyString(const std::string &key, std::string &value); 93 uint32_t GetImagePropertyStringEx(const std::string &key, std::string &value); 94 uint32_t GetMakerImagePropertyString(const std::string &key, std::string &value); 95 96 static MultimediaPlugin::PluginServer &pluginServer_; 97 jpeg_decompress_struct decodeInfo_; 98 JpegSrcMgr srcMgr_; 99 ErrorMgr jerr_; 100 AbsImageDecompressComponent *hwJpegDecompress_ = nullptr; 101 JpegDecodingState state_ = JpegDecodingState::UNDECIDED; 102 uint32_t streamPosition_ = 0; // may be changed by other decoders, record it and restore if needed. 103 PixelFormat outputFormat_ = PixelFormat::UNKNOWN; 104 PixelDecodeOptions opts_; 105 EXIFInfo exifInfo_; 106 ICCProfileInfo iccProfileInfo_; 107 }; 108 } // namespace ImagePlugin 109 } // namespace OHOS 110 111 #endif // JPEG_DECODER_H 112