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