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