blob: 4dd1f0b53b123b3ffc2a7e967f770170f07ba659 [file] [log] [blame]
msarett8c8f22a2015-04-01 06:58:48 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Hal Canary03a7f5f2017-02-10 09:06:38 -05007#ifndef SkGifCodec_DEFINED
8#define SkGifCodec_DEFINED
msarett8c8f22a2015-04-01 06:58:48 -07009
10#include "SkCodec.h"
scroggo19b91532016-10-24 09:03:26 -070011#include "SkCodecAnimation.h"
msarettad8bcfe2016-03-07 07:09:03 -080012#include "SkColorSpace.h"
msarett10522ff2015-09-07 08:54:01 -070013#include "SkColorTable.h"
msarett8c8f22a2015-04-01 06:58:48 -070014#include "SkImageInfo.h"
msarett10522ff2015-09-07 08:54:01 -070015#include "SkSwizzler.h"
msarett8c8f22a2015-04-01 06:58:48 -070016
scroggo3d3a65c2016-10-24 12:28:30 -070017#include "SkGifImageReader.h"
msarett8c8f22a2015-04-01 06:58:48 -070018
19/*
20 *
21 * This class implements the decoding for gif images
22 *
23 */
24class SkGifCodec : public SkCodec {
25public:
scroggodb30be22015-12-08 18:54:13 -080026 static bool IsGif(const void*, size_t);
msarett8c8f22a2015-04-01 06:58:48 -070027
28 /*
29 * Assumes IsGif was called and returned true
msarett8c8f22a2015-04-01 06:58:48 -070030 * Reads enough of the stream to determine the image format
31 */
Mike Reedede7bac2017-07-23 15:30:02 -040032 static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*);
msarett10522ff2015-09-07 08:54:01 -070033
scroggo3d3a65c2016-10-24 12:28:30 -070034 // Callback for SkGifImageReader when a row is available.
Leon Scroggins III223ec292017-08-22 14:13:15 -040035 void haveDecodedRow(int frameIndex, const unsigned char* rowBegin,
Leon Scroggins III249b8e32017-04-17 12:46:33 -040036 int rowNumber, int repeatCount, bool writeTransparentPixels);
msarett8c8f22a2015-04-01 06:58:48 -070037protected:
msarett438b2ad2015-04-09 12:43:10 -070038 /*
msarett10522ff2015-09-07 08:54:01 -070039 * Performs the full gif decode
msarett8c8f22a2015-04-01 06:58:48 -070040 */
41 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&,
Leon Scroggins571b30f2017-07-11 17:35:31 +000042 int*) override;
msarett8c8f22a2015-04-01 06:58:48 -070043
Hal Canarydb683012016-11-23 08:55:18 -070044 SkEncodedImageFormat onGetEncodedFormat() const override {
45 return SkEncodedImageFormat::kGIF;
msarett8c8f22a2015-04-01 06:58:48 -070046 }
47
scroggob427db12015-08-12 07:24:13 -070048 bool onRewind() override;
49
Leon Scroggins III249b8e32017-04-17 12:46:33 -040050 int onGetFrameCount() override;
51 bool onGetFrameInfo(int, FrameInfo*) const override;
scroggoe71b1a12016-11-01 08:28:28 -070052 int onGetRepetitionCount() override;
scroggo19b91532016-10-24 09:03:26 -070053
54 Result onStartIncrementalDecode(const SkImageInfo& /*dstInfo*/, void*, size_t,
Leon Scroggins571b30f2017-07-11 17:35:31 +000055 const SkCodec::Options&) override;
scroggo19b91532016-10-24 09:03:26 -070056
57 Result onIncrementalDecode(int*) override;
msarette6dd0042015-10-09 11:07:34 -070058
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -040059 const SkFrameHolder* getFrameHolder() const override {
60 return fReader.get();
61 }
62
msarett8c8f22a2015-04-01 06:58:48 -070063private:
64
65 /*
msarett10522ff2015-09-07 08:54:01 -070066 * Initializes the color table that we will use for decoding.
67 *
68 * @param dstInfo Contains the requested dst color type.
scroggo19b91532016-10-24 09:03:26 -070069 * @param frameIndex Frame whose color table to use.
msarett10522ff2015-09-07 08:54:01 -070070 */
Leon Scroggins III249b8e32017-04-17 12:46:33 -040071 void initializeColorTable(const SkImageInfo& dstInfo, int frameIndex);
msarett10522ff2015-09-07 08:54:01 -070072
73 /*
Leon Scroggins571b30f2017-07-11 17:35:31 +000074 * Does necessary setup, including setting up the color table and swizzler.
msarett10522ff2015-09-07 08:54:01 -070075 */
Leon Scroggins571b30f2017-07-11 17:35:31 +000076 Result prepareToDecode(const SkImageInfo& dstInfo, const Options& opts);
msarett10522ff2015-09-07 08:54:01 -070077
78 /*
79 * Initializes the swizzler.
80 *
scroggo19b91532016-10-24 09:03:26 -070081 * @param dstInfo Output image information. Dimensions may have been
82 * adjusted if the image frame size does not match the size
83 * indicated in the header.
84 * @param frameIndex Which frame we are decoding. This determines the frameRect
85 * to use.
msarett10522ff2015-09-07 08:54:01 -070086 */
Leon Scroggins III249b8e32017-04-17 12:46:33 -040087 void initializeSwizzler(const SkImageInfo& dstInfo, int frameIndex);
msarett10522ff2015-09-07 08:54:01 -070088
msarette6dd0042015-10-09 11:07:34 -070089 SkSampler* getSampler(bool createIfNecessary) override {
90 SkASSERT(fSwizzler);
scroggo19b91532016-10-24 09:03:26 -070091 return fSwizzler.get();
msarette6dd0042015-10-09 11:07:34 -070092 }
scroggoe7fc14b2015-10-02 13:14:46 -070093
msarett10522ff2015-09-07 08:54:01 -070094 /*
scroggo19b91532016-10-24 09:03:26 -070095 * Recursive function to decode a frame.
msarett72261c02015-11-19 15:29:26 -080096 *
scroggo19b91532016-10-24 09:03:26 -070097 * @param firstAttempt Whether this is the first call to decodeFrame since
98 * starting. e.g. true in onGetPixels, and true in the
99 * first call to onIncrementalDecode after calling
100 * onStartIncrementalDecode.
101 * When true, this method may have to initialize the
102 * frame, for example by filling or decoding the prior
103 * frame.
104 * @param opts Options for decoding. May be different from
105 * this->options() for decoding prior frames. Specifies
106 * the frame to decode and whether the prior frame has
107 * already been decoded to fDst. If not, and the frame
108 * is not independent, this method will recursively
109 * decode the frame it depends on.
110 * @param rowsDecoded Out-parameter to report the total number of rows
111 * that have been decoded (or at least written to, if
112 * it had to fill), including rows decoded by prior
113 * calls to onIncrementalDecode.
114 * @return kSuccess if the frame is complete, kIncompleteInput
115 * otherwise.
msarett72261c02015-11-19 15:29:26 -0800116 */
scroggo19b91532016-10-24 09:03:26 -0700117 Result decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded);
msarett8c8f22a2015-04-01 06:58:48 -0700118
119 /*
Matt Sarett61eedeb2016-11-04 13:19:48 -0400120 * Swizzles and color xforms (if necessary) into dst.
121 */
122 void applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const;
123
124 /*
msarett8c8f22a2015-04-01 06:58:48 -0700125 * Creates an instance of the decoder
126 * Called only by NewFromStream
scroggo3d3a65c2016-10-24 12:28:30 -0700127 * Takes ownership of the SkGifImageReader
msarett8c8f22a2015-04-01 06:58:48 -0700128 */
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400129 SkGifCodec(SkEncodedInfo&&, SkGifImageReader*);
msarett8c8f22a2015-04-01 06:58:48 -0700130
scroggo3d3a65c2016-10-24 12:28:30 -0700131 std::unique_ptr<SkGifImageReader> fReader;
132 std::unique_ptr<uint8_t[]> fTmpBuffer;
133 std::unique_ptr<SkSwizzler> fSwizzler;
134 sk_sp<SkColorTable> fCurrColorTable;
scroggo19b91532016-10-24 09:03:26 -0700135 // We may create a dummy table if there is not a Map in the input data. In
136 // that case, we set this value to false, and we can skip a lot of decoding
137 // work (which would not be meaningful anyway). We create a "fake"/"dummy"
138 // one in that case, so the client and the swizzler have something to draw.
scroggo3d3a65c2016-10-24 12:28:30 -0700139 bool fCurrColorTableIsReal;
scroggo19b91532016-10-24 09:03:26 -0700140 // Whether the background was filled.
scroggo3d3a65c2016-10-24 12:28:30 -0700141 bool fFilledBackground;
scroggo19b91532016-10-24 09:03:26 -0700142 // True on the first call to onIncrementalDecode. This value is passed to
143 // decodeFrame.
scroggo3d3a65c2016-10-24 12:28:30 -0700144 bool fFirstCallToIncrementalDecode;
scroggo19b91532016-10-24 09:03:26 -0700145
scroggo3d3a65c2016-10-24 12:28:30 -0700146 void* fDst;
147 size_t fDstRowBytes;
scroggo19b91532016-10-24 09:03:26 -0700148
149 // Updated inside haveDecodedRow when rows are decoded, unless we filled
150 // the background, in which case it is set once and left alone.
scroggo3d3a65c2016-10-24 12:28:30 -0700151 int fRowsDecoded;
Matt Sarett61eedeb2016-11-04 13:19:48 -0400152 std::unique_ptr<uint32_t[]> fXformBuffer;
msarett10522ff2015-09-07 08:54:01 -0700153
msarett8c8f22a2015-04-01 06:58:48 -0700154 typedef SkCodec INHERITED;
155};
Hal Canary03a7f5f2017-02-10 09:06:38 -0500156#endif // SkGifCodec_DEFINED