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 WEBP_DECODER_H 17 #define WEBP_DECODER_H 18 19 #include "abs_image_decoder.h" 20 #include "hilog/log.h" 21 #include "input_data_stream.h" 22 #include "log_tags.h" 23 #include "plugin_class_base.h" 24 #include "webp/decode.h" 25 #include "webp/demux.h" 26 27 namespace OHOS { 28 namespace ImagePlugin { 29 enum class WebpDecodingState : int32_t { 30 UNDECIDED = 0, 31 SOURCE_INITED = 1, 32 BASE_INFO_PARSING = 2, 33 BASE_INFO_PARSED = 3, 34 IMAGE_DECODING = 4, 35 IMAGE_ERROR = 5, 36 IMAGE_PARTIAL = 6, 37 IMAGE_DECODED = 7 38 }; 39 40 class WebpDecoder : public AbsImageDecoder, public OHOS::MultimediaPlugin::PluginClassBase { 41 public: 42 WebpDecoder(); 43 ~WebpDecoder() override; 44 void SetSource(InputDataStream &sourceStream) override; 45 void Reset() override; 46 uint32_t SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info) override; 47 uint32_t Decode(uint32_t index, DecodeContext &context) override; 48 uint32_t PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &context) override; 49 uint32_t GetImageSize(uint32_t index, PlSize &size) override; 50 51 private: 52 // private function 53 DISALLOW_COPY_AND_MOVE(WebpDecoder); 54 WEBP_CSP_MODE GetWebpDecodeMode(const PlPixelFormat &pixelFormat, bool premul); 55 uint32_t ReadIncrementalHead(); 56 uint32_t DecodeHeader(); 57 bool AllocHeapBuffer(DecodeContext &context, bool isIncremental); 58 void InitWebpOutput(const DecodeContext &context, WebPDecBuffer &output); 59 bool PreDecodeProc(DecodeContext &context, WebPDecoderConfig &config, bool isIncremental); 60 uint32_t DoCommonDecode(DecodeContext &context); 61 uint32_t DoIncrementalDecode(ProgDecodeContext &context); 62 void FinishOldDecompress(); 63 bool IsDataEnough(); 64 // private members 65 InputDataStream *stream_ = nullptr; 66 DataStreamBuffer dataBuffer_; 67 PlSize webpSize_; 68 size_t incrementSize_ = 0; // current incremental data size 69 size_t lastDecodeSize_ = 0; // last decoded data size 70 int32_t bytesPerPixel_ = 4; // default four bytes for each pixel 71 WEBP_CSP_MODE webpMode_ = MODE_RGBA; 72 WebpDecodingState state_ = WebpDecodingState::UNDECIDED; 73 PixelDecodeOptions opts_; 74 PlPixelFormat outputFormat_ = PlPixelFormat::UNKNOWN; 75 }; 76 } // namespace ImagePlugin 77 } // namespace OHOS 78 79 #endif // WEBP_DECODER_H 80