blob: 33472d8ac15f2f745229ac500d92969a9aea9da2 [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
30 * Creates a gif decoder
31 * Reads enough of the stream to determine the image format
32 */
33 static SkCodec* NewFromStream(SkStream*);
msarett10522ff2015-09-07 08:54:01 -070034
scroggo3d3a65c2016-10-24 12:28:30 -070035 // Callback for SkGifImageReader when a row is available.
Leon Scroggins III249b8e32017-04-17 12:46:33 -040036 bool haveDecodedRow(int frameIndex, const unsigned char* rowBegin,
37 int rowNumber, int repeatCount, bool writeTransparentPixels);
msarett8c8f22a2015-04-01 06:58:48 -070038protected:
msarett438b2ad2015-04-09 12:43:10 -070039 /*
msarett10522ff2015-09-07 08:54:01 -070040 * Performs the full gif decode
msarett8c8f22a2015-04-01 06:58:48 -070041 */
42 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&,
msarette6dd0042015-10-09 11:07:34 -070043 SkPMColor*, int*, int*) override;
msarett8c8f22a2015-04-01 06:58:48 -070044
Hal Canarydb683012016-11-23 08:55:18 -070045 SkEncodedImageFormat onGetEncodedFormat() const override {
46 return SkEncodedImageFormat::kGIF;
msarett8c8f22a2015-04-01 06:58:48 -070047 }
48
scroggob427db12015-08-12 07:24:13 -070049 bool onRewind() override;
50
msarettf7eb6fc2016-09-13 09:04:11 -070051 uint64_t onGetFillValue(const SkImageInfo&) const override;
msarette6dd0042015-10-09 11:07:34 -070052
Leon Scroggins III249b8e32017-04-17 12:46:33 -040053 int onGetFrameCount() override;
54 bool onGetFrameInfo(int, FrameInfo*) const override;
scroggoe71b1a12016-11-01 08:28:28 -070055 int onGetRepetitionCount() override;
scroggo19b91532016-10-24 09:03:26 -070056
57 Result onStartIncrementalDecode(const SkImageInfo& /*dstInfo*/, void*, size_t,
58 const SkCodec::Options&, SkPMColor*, int*) override;
59
60 Result onIncrementalDecode(int*) override;
msarette6dd0042015-10-09 11:07:34 -070061
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -040062 const SkFrameHolder* getFrameHolder() const override {
63 return fReader.get();
64 }
65
msarett8c8f22a2015-04-01 06:58:48 -070066private:
67
68 /*
msarett10522ff2015-09-07 08:54:01 -070069 * Initializes the color table that we will use for decoding.
70 *
71 * @param dstInfo Contains the requested dst color type.
scroggo19b91532016-10-24 09:03:26 -070072 * @param frameIndex Frame whose color table to use.
msarett10522ff2015-09-07 08:54:01 -070073 */
Leon Scroggins III249b8e32017-04-17 12:46:33 -040074 void initializeColorTable(const SkImageInfo& dstInfo, int frameIndex);
msarett10522ff2015-09-07 08:54:01 -070075
76 /*
scroggo19b91532016-10-24 09:03:26 -070077 * Does necessary setup, including setting up the color table and swizzler,
78 * and reports color info to the client.
msarett10522ff2015-09-07 08:54:01 -070079 */
scroggo46c57472015-09-30 08:57:13 -070080 Result prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
msarett10522ff2015-09-07 08:54:01 -070081 int* inputColorCount, const Options& opts);
82
83 /*
84 * Initializes the swizzler.
85 *
scroggo19b91532016-10-24 09:03:26 -070086 * @param dstInfo Output image information. Dimensions may have been
87 * adjusted if the image frame size does not match the size
88 * indicated in the header.
89 * @param frameIndex Which frame we are decoding. This determines the frameRect
90 * to use.
msarett10522ff2015-09-07 08:54:01 -070091 */
Leon Scroggins III249b8e32017-04-17 12:46:33 -040092 void initializeSwizzler(const SkImageInfo& dstInfo, int frameIndex);
msarett10522ff2015-09-07 08:54:01 -070093
msarette6dd0042015-10-09 11:07:34 -070094 SkSampler* getSampler(bool createIfNecessary) override {
95 SkASSERT(fSwizzler);
scroggo19b91532016-10-24 09:03:26 -070096 return fSwizzler.get();
msarette6dd0042015-10-09 11:07:34 -070097 }
scroggoe7fc14b2015-10-02 13:14:46 -070098
msarett10522ff2015-09-07 08:54:01 -070099 /*
scroggo19b91532016-10-24 09:03:26 -0700100 * Recursive function to decode a frame.
msarett72261c02015-11-19 15:29:26 -0800101 *
scroggo19b91532016-10-24 09:03:26 -0700102 * @param firstAttempt Whether this is the first call to decodeFrame since
103 * starting. e.g. true in onGetPixels, and true in the
104 * first call to onIncrementalDecode after calling
105 * onStartIncrementalDecode.
106 * When true, this method may have to initialize the
107 * frame, for example by filling or decoding the prior
108 * frame.
109 * @param opts Options for decoding. May be different from
110 * this->options() for decoding prior frames. Specifies
111 * the frame to decode and whether the prior frame has
112 * already been decoded to fDst. If not, and the frame
113 * is not independent, this method will recursively
114 * decode the frame it depends on.
115 * @param rowsDecoded Out-parameter to report the total number of rows
116 * that have been decoded (or at least written to, if
117 * it had to fill), including rows decoded by prior
118 * calls to onIncrementalDecode.
119 * @return kSuccess if the frame is complete, kIncompleteInput
120 * otherwise.
msarett72261c02015-11-19 15:29:26 -0800121 */
scroggo19b91532016-10-24 09:03:26 -0700122 Result decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded);
msarett8c8f22a2015-04-01 06:58:48 -0700123
124 /*
Matt Sarett61eedeb2016-11-04 13:19:48 -0400125 * Swizzles and color xforms (if necessary) into dst.
126 */
127 void applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const;
128
129 /*
msarett8c8f22a2015-04-01 06:58:48 -0700130 * Creates an instance of the decoder
131 * Called only by NewFromStream
scroggo3d3a65c2016-10-24 12:28:30 -0700132 * Takes ownership of the SkGifImageReader
msarett8c8f22a2015-04-01 06:58:48 -0700133 */
scroggo3d3a65c2016-10-24 12:28:30 -0700134 SkGifCodec(const SkEncodedInfo&, const SkImageInfo&, SkGifImageReader*);
msarett8c8f22a2015-04-01 06:58:48 -0700135
scroggo3d3a65c2016-10-24 12:28:30 -0700136 std::unique_ptr<SkGifImageReader> fReader;
137 std::unique_ptr<uint8_t[]> fTmpBuffer;
138 std::unique_ptr<SkSwizzler> fSwizzler;
139 sk_sp<SkColorTable> fCurrColorTable;
scroggo19b91532016-10-24 09:03:26 -0700140 // We may create a dummy table if there is not a Map in the input data. In
141 // that case, we set this value to false, and we can skip a lot of decoding
142 // work (which would not be meaningful anyway). We create a "fake"/"dummy"
143 // one in that case, so the client and the swizzler have something to draw.
scroggo3d3a65c2016-10-24 12:28:30 -0700144 bool fCurrColorTableIsReal;
scroggo19b91532016-10-24 09:03:26 -0700145 // Whether the background was filled.
scroggo3d3a65c2016-10-24 12:28:30 -0700146 bool fFilledBackground;
scroggo19b91532016-10-24 09:03:26 -0700147 // True on the first call to onIncrementalDecode. This value is passed to
148 // decodeFrame.
scroggo3d3a65c2016-10-24 12:28:30 -0700149 bool fFirstCallToIncrementalDecode;
scroggo19b91532016-10-24 09:03:26 -0700150
scroggo3d3a65c2016-10-24 12:28:30 -0700151 void* fDst;
152 size_t fDstRowBytes;
scroggo19b91532016-10-24 09:03:26 -0700153
154 // Updated inside haveDecodedRow when rows are decoded, unless we filled
155 // the background, in which case it is set once and left alone.
scroggo3d3a65c2016-10-24 12:28:30 -0700156 int fRowsDecoded;
Matt Sarett61eedeb2016-11-04 13:19:48 -0400157 std::unique_ptr<uint32_t[]> fXformBuffer;
msarett10522ff2015-09-07 08:54:01 -0700158
msarett8c8f22a2015-04-01 06:58:48 -0700159 typedef SkCodec INHERITED;
160};
Hal Canary03a7f5f2017-02-10 09:06:38 -0500161#endif // SkGifCodec_DEFINED