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 "input_data_stream.h"
21 #include "plugin_class_base.h"
22 #include "webp/decode.h"
23 #include "webp/demux.h"
24 
25 namespace OHOS {
26 namespace ImagePlugin {
27 using namespace Media;
28 
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, Size &size) override;
50 #ifdef IMAGE_COLORSPACE_FLAG
IsSupportICCProfile()51     bool IsSupportICCProfile() override
52     {
53         return false;
54     }
55 #endif
56 
57 private:
58     // private function
59     DISALLOW_COPY_AND_MOVE(WebpDecoder);
60     WEBP_CSP_MODE GetWebpDecodeMode(const PixelFormat &pixelFormat, bool premul);
61     uint32_t ReadIncrementalHead();
62     uint32_t DecodeHeader();
63     bool AllocOutputBuffer(DecodeContext &context, bool isIncremental);
64     void InitWebpOutput(const DecodeContext &context, WebPDecBuffer &output);
65     bool PreDecodeProc(DecodeContext &context, WebPDecoderConfig &config, bool isIncremental);
66     uint32_t DoCommonDecode(DecodeContext &context);
67     uint32_t DoIncrementalDecode(ProgDecodeContext &context);
68     void FinishOldDecompress();
69     bool IsDataEnough();
70     // private members
71     InputDataStream *stream_ = nullptr;
72     DataStreamBuffer dataBuffer_;
73     Size webpSize_;
74     size_t incrementSize_ = 0;   // current incremental data size
75     size_t lastDecodeSize_ = 0;  // last decoded data size
76     int32_t bytesPerPixel_ = 4;  // default four bytes for each pixel
77     WEBP_CSP_MODE webpMode_ = MODE_RGBA;
78     WebpDecodingState state_ = WebpDecodingState::UNDECIDED;
79     PixelDecodeOptions opts_;
80     PixelFormat outputFormat_ = PixelFormat::UNKNOWN;
81 };
82 } // namespace ImagePlugin
83 } // namespace OHOS
84 
85 #endif // WEBP_DECODER_H
86