blob: ba48989cbbf16687f3aab030803ea6f94177e389 [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 */
7
8#include "SkCodec.h"
msarett10522ff2015-09-07 08:54:01 -07009#include "SkColorTable.h"
msarett8c8f22a2015-04-01 06:58:48 -070010#include "SkImageInfo.h"
msarett10522ff2015-09-07 08:54:01 -070011#include "SkSwizzler.h"
msarett8c8f22a2015-04-01 06:58:48 -070012
13#include "gif_lib.h"
14
15/*
16 *
17 * This class implements the decoding for gif images
18 *
19 */
20class SkGifCodec : public SkCodec {
21public:
scroggodb30be22015-12-08 18:54:13 -080022 static bool IsGif(const void*, size_t);
msarett8c8f22a2015-04-01 06:58:48 -070023
24 /*
25 * Assumes IsGif was called and returned true
26 * Creates a gif decoder
27 * Reads enough of the stream to determine the image format
28 */
29 static SkCodec* NewFromStream(SkStream*);
msarett10522ff2015-09-07 08:54:01 -070030
msarett8c8f22a2015-04-01 06:58:48 -070031protected:
32
33 /*
msarett438b2ad2015-04-09 12:43:10 -070034 * Read enough of the stream to initialize the SkGifCodec.
35 * Returns a bool representing success or failure.
36 *
37 * @param codecOut
halcanary96fcdcc2015-08-27 07:41:13 -070038 * If it returned true, and codecOut was not nullptr,
msarett438b2ad2015-04-09 12:43:10 -070039 * codecOut will be set to a new SkGifCodec.
40 *
41 * @param gifOut
halcanary96fcdcc2015-08-27 07:41:13 -070042 * If it returned true, and codecOut was nullptr,
43 * gifOut must be non-nullptr and gifOut will be set to a new
msarett438b2ad2015-04-09 12:43:10 -070044 * GifFileType pointer.
45 *
46 * @param stream
47 * Deleted on failure.
48 * codecOut will take ownership of it in the case where we created a codec.
49 * Ownership is unchanged when we returned a gifOut.
50 *
51 */
msarett10522ff2015-09-07 08:54:01 -070052 static bool ReadHeader(SkStream* stream, SkCodec** codecOut,
53 GifFileType** gifOut);
msarett438b2ad2015-04-09 12:43:10 -070054
55 /*
msarett10522ff2015-09-07 08:54:01 -070056 * Performs the full gif decode
msarett8c8f22a2015-04-01 06:58:48 -070057 */
58 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&,
msarette6dd0042015-10-09 11:07:34 -070059 SkPMColor*, int*, int*) override;
msarett8c8f22a2015-04-01 06:58:48 -070060
61 SkEncodedFormat onGetEncodedFormat() const override {
62 return kGIF_SkEncodedFormat;
63 }
64
scroggob427db12015-08-12 07:24:13 -070065 bool onRewind() override;
66
msarette6dd0042015-10-09 11:07:34 -070067 uint32_t onGetFillValue(SkColorType colorType, SkAlphaType alphaType) const override;
68
mtkleine9759282015-10-15 08:55:33 -070069 int onOutputScanline(int inputScanline) const override;
msarette6dd0042015-10-09 11:07:34 -070070
msarett8c8f22a2015-04-01 06:58:48 -070071private:
72
73 /*
msarett10522ff2015-09-07 08:54:01 -070074 * A gif can contain multiple image frames. We will only decode the first
75 * frame. This function reads up to the first image frame, processing
76 * transparency and/or animation information that comes before the image
77 * data.
78 *
79 * @param gif Pointer to the library type that manages the gif decode
80 * @param transIndex This call will set the transparent index based on the
81 * extension data.
82 */
scroggo46c57472015-09-30 08:57:13 -070083 static Result ReadUpToFirstImage(GifFileType* gif, uint32_t* transIndex);
msarett10522ff2015-09-07 08:54:01 -070084
85 /*
86 * A gif may contain many image frames, all of different sizes.
msarett4aa02d82015-10-06 07:46:02 -070087 * This function checks if the gif dimensions are valid, based on the frame
88 * dimensions, and corrects the gif dimensions if necessary.
msarett10522ff2015-09-07 08:54:01 -070089 *
msarett4aa02d82015-10-06 07:46:02 -070090 * @param gif Pointer to the library type that manages the gif decode
91 * @param size Size of the image that we will decode.
92 * Will be set by this function if the return value is true.
93 * @param frameRect Contains the dimenions and offset of the first image frame.
94 * Will be set by this function if the return value is true.
95 *
96 * @return true on success, false otherwise
msarett10522ff2015-09-07 08:54:01 -070097 */
msarett4aa02d82015-10-06 07:46:02 -070098 static bool GetDimensions(GifFileType* gif, SkISize* size, SkIRect* frameRect);
msarett10522ff2015-09-07 08:54:01 -070099
100 /*
101 * Initializes the color table that we will use for decoding.
102 *
103 * @param dstInfo Contains the requested dst color type.
104 * @param inputColorPtr Copies the encoded color table to the client's
105 * input color table if the client requests kIndex8.
106 * @param inputColorCount If the client requests kIndex8, sets
107 * inputColorCount to 256. Since gifs always
108 * contain 8-bit indices, we need a 256 entry color
109 * table to ensure that indexing is always in
110 * bounds.
111 */
112 void initializeColorTable(const SkImageInfo& dstInfo, SkPMColor* colorPtr,
113 int* inputColorCount);
114
115 /*
scroggo3a7701c2015-09-30 09:15:14 -0700116 * Checks for invalid inputs and calls setFrameDimensions(), and
msarett10522ff2015-09-07 08:54:01 -0700117 * initializeColorTable() in the proper sequence.
118 */
scroggo46c57472015-09-30 08:57:13 -0700119 Result prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
msarett10522ff2015-09-07 08:54:01 -0700120 int* inputColorCount, const Options& opts);
121
122 /*
123 * Initializes the swizzler.
124 *
125 * @param dstInfo Output image information. Dimensions may have been
126 * adjusted if the image frame size does not match the size
127 * indicated in the header.
msarettfdb47572015-10-13 12:50:14 -0700128 * @param options Informs the swizzler if destination memory is zero initialized.
129 * Contains subset information.
msarett10522ff2015-09-07 08:54:01 -0700130 */
msarettfdb47572015-10-13 12:50:14 -0700131 Result initializeSwizzler(const SkImageInfo& dstInfo,
132 const Options& options);
msarett10522ff2015-09-07 08:54:01 -0700133
msarette6dd0042015-10-09 11:07:34 -0700134 SkSampler* getSampler(bool createIfNecessary) override {
135 SkASSERT(fSwizzler);
136 return fSwizzler;
137 }
scroggoe7fc14b2015-10-02 13:14:46 -0700138
msarett10522ff2015-09-07 08:54:01 -0700139 /*
msarette6dd0042015-10-09 11:07:34 -0700140 * @return true if the read is successful and false if the read fails.
msarett10522ff2015-09-07 08:54:01 -0700141 */
msarette6dd0042015-10-09 11:07:34 -0700142 bool readRow();
scroggo46c57472015-09-30 08:57:13 -0700143
144 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& opts,
145 SkPMColor inputColorPtr[], int* inputColorCount) override;
146
msarette6dd0042015-10-09 11:07:34 -0700147 int onGetScanlines(void* dst, int count, size_t rowBytes) override;
scroggo46c57472015-09-30 08:57:13 -0700148
msarett72261c02015-11-19 15:29:26 -0800149 bool onSkipScanlines(int count) override;
150
151 /*
152 * For a scanline decode of "count" lines, this function indicates how
153 * many of the "count" lines should be skipped until we reach the top of
154 * the image frame and how many of the "count" lines are actually inside
155 * the image frame.
156 *
157 * @param count The number of scanlines requested.
158 * @param rowsBeforeFrame Output variable. The number of lines before
159 * we reach the top of the image frame.
160 * @param rowsInFrame Output variable. The number of lines to decode
161 * inside the image frame.
162 */
163 void handleScanlineFrame(int count, int* rowsBeforeFrame, int* rowsInFrame);
164
scroggo46c57472015-09-30 08:57:13 -0700165 SkScanlineOrder onGetScanlineOrder() const override;
166
msarett10522ff2015-09-07 08:54:01 -0700167 /*
msarett8c8f22a2015-04-01 06:58:48 -0700168 * This function cleans up the gif object after the decode completes
169 * It is used in a SkAutoTCallIProc template
170 */
msarett438b2ad2015-04-09 12:43:10 -0700171 static void CloseGif(GifFileType* gif);
msarett8c8f22a2015-04-01 06:58:48 -0700172
173 /*
174 * Frees any extension data used in the decode
175 * Used in a SkAutoTCallVProc
176 */
177 static void FreeExtension(SavedImage* image);
178
179 /*
180 * Creates an instance of the decoder
181 * Called only by NewFromStream
182 *
183 * @param srcInfo contains the source width and height
184 * @param stream the stream of image data
185 * @param gif pointer to library type that manages gif decode
186 * takes ownership
msarett10522ff2015-09-07 08:54:01 -0700187 * @param transIndex The transparent index. An invalid value
188 * indicates that there is no transparent index.
msarett8c8f22a2015-04-01 06:58:48 -0700189 */
msarett4aa02d82015-10-06 07:46:02 -0700190 SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, GifFileType* gif, uint32_t transIndex,
191 const SkIRect& frameRect, bool frameIsSubset);
msarett8c8f22a2015-04-01 06:58:48 -0700192
msarett438b2ad2015-04-09 12:43:10 -0700193 SkAutoTCallVProc<GifFileType, CloseGif> fGif; // owned
msarett10522ff2015-09-07 08:54:01 -0700194 SkAutoTDeleteArray<uint8_t> fSrcBuffer;
msarett4aa02d82015-10-06 07:46:02 -0700195 const SkIRect fFrameRect;
msarett10522ff2015-09-07 08:54:01 -0700196 const uint32_t fTransIndex;
197 uint32_t fFillIndex;
msarett4aa02d82015-10-06 07:46:02 -0700198 const bool fFrameIsSubset;
msarett10522ff2015-09-07 08:54:01 -0700199 SkAutoTDelete<SkSwizzler> fSwizzler;
200 SkAutoTUnref<SkColorTable> fColorTable;
201
msarett8c8f22a2015-04-01 06:58:48 -0700202 typedef SkCodec INHERITED;
203};