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