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 GIF_DECODER_H
17 #define GIF_DECODER_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <string>
22 #include "abs_image_decoder.h"
23 #include "gif_lib.h"
24 #include "media_errors.h"
25 #include "nocopyable.h"
26 #include "plugin_class_base.h"
27 #ifndef _WIN32
28 #include "securec.h"
29 #else
30 #include "memory.h"
31 #endif
32 
33 namespace OHOS {
34 namespace ImagePlugin {
35 static constexpr uint8_t PIXEL_FORMAT_BYTE_SIZE = 4;
36 
37 class GifDecoder : public AbsImageDecoder, public OHOS::MultimediaPlugin::PluginClassBase {
38 public:
39     GifDecoder();
40     ~GifDecoder() override;
41     void SetSource(InputDataStream &sourceStream) override;
42     void Reset() override;
43     uint32_t SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info) override;
44     uint32_t Decode(uint32_t index, DecodeContext &context) override;
45     uint32_t PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &context) override;
46     uint32_t GetTopLevelImageNum(uint32_t &num) override;
47     uint32_t GetImageSize(uint32_t index, Size &size) override;
48     uint32_t GetImagePropertyInt(uint32_t index, const std::string &key, int32_t &value) override;
49     uint32_t GetImagePropertyString(uint32_t index, const std::string &key, std::string &value) override;
50 #ifdef IMAGE_COLORSPACE_FLAG
IsSupportICCProfile()51     bool IsSupportICCProfile() override
52     {
53         return false;
54     }
55 #endif
56 
57 private:
58     static int32_t InputStreamReader(GifFileType *gif, GifByteType *bytes, int32_t size);
59     DISALLOW_COPY_AND_MOVE(GifDecoder);
60     uint32_t CheckIndex(uint32_t index);
61     uint32_t OverlapFrame(uint32_t startIndex, uint32_t endIndex);
62     uint32_t RedirectOutputBuffer(DecodeContext &context);
63     void GetTransparentAndDisposal(uint32_t index, int32_t &transparentColor, int32_t &disposalMode);
64     GraphicsControlBlock GetGraphicsControlBlock(uint32_t index);
65     uint32_t PaddingBgColor(const SavedImage *savedImage);
66     bool IsFramePreviousCoveredCurrent(const SavedImage *preSavedImage, const SavedImage *curSavedImage);
67     uint32_t PaddingData(const SavedImage *savedImage, int32_t transparentColor);
68     void CopyLine(const GifByteType *srcFrame, uint32_t *dstFrame, int32_t frameWidth, int32_t transparentColor,
69                   const ColorMapObject *colorMap);
70     uint32_t GetPixelColor(uint32_t red, uint32_t green, uint32_t blue, uint32_t alpha);
71     void ParseBgColor();
72     uint32_t UpdateGifFileType(int32_t updateFrameIndex);
73     uint32_t CreateGifFileTypeIfNotExist();
74     uint32_t ParseFrameDetail();
75     uint32_t SetSavedImageRasterBits(SavedImage *saveImagePtr, int32_t frameIndex, uint64_t imageSize,
76                                      int32_t imageWidth, int32_t imageHeight);
77     uint32_t ParseFrameExtension();
78     uint32_t AllocateLocalPixelMapBuffer();
79     void FreeLocalPixelMapBuffer();
80     uint32_t DisposeBackground(uint32_t frameIndex, const SavedImage *curSavedImage);
81     uint32_t GetImageDelayTime(uint32_t index, int32_t &value);
82     uint32_t GetImageLoopCount(uint32_t index, int32_t &value);
83 
84     InputDataStream *inputStreamPtr_ = nullptr;
85     GifFileType *gifPtr_ = nullptr;
86     uint32_t *localPixelMapBuffer_ = nullptr;
87     uint32_t bgColor_ = 0;
88     int32_t lastPixelMapIndex_ = -1;
89     bool isLoadAllFrame_ = false;
90     int32_t savedFrameIndex_ = -1;
91 };
92 } // namespace ImagePlugin
93 } // namespace OHOS
94 
95 #endif // GIF_DECODER_H
96