blob: a46667a375a560f2155e6d78b2829c99718756a3 [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
msarett39b2d5a2016-02-17 08:26:31 -080013struct GifFileType;
14struct SavedImage;
msarett8c8f22a2015-04-01 06:58:48 -070015
16/*
17 *
18 * This class implements the decoding for gif images
19 *
20 */
21class SkGifCodec : public SkCodec {
22public:
scroggodb30be22015-12-08 18:54:13 -080023 static bool IsGif(const void*, size_t);
msarett8c8f22a2015-04-01 06:58:48 -070024
25 /*
26 * Assumes IsGif was called and returned true
27 * Creates a gif decoder
28 * Reads enough of the stream to determine the image format
29 */
30 static SkCodec* NewFromStream(SkStream*);
msarett10522ff2015-09-07 08:54:01 -070031
msarett8c8f22a2015-04-01 06:58:48 -070032protected:
33
34 /*
msarett438b2ad2015-04-09 12:43:10 -070035 * Read enough of the stream to initialize the SkGifCodec.
36 * Returns a bool representing success or failure.
37 *
38 * @param codecOut
halcanary96fcdcc2015-08-27 07:41:13 -070039 * If it returned true, and codecOut was not nullptr,
msarett438b2ad2015-04-09 12:43:10 -070040 * codecOut will be set to a new SkGifCodec.
41 *
42 * @param gifOut
halcanary96fcdcc2015-08-27 07:41:13 -070043 * If it returned true, and codecOut was nullptr,
44 * gifOut must be non-nullptr and gifOut will be set to a new
msarett438b2ad2015-04-09 12:43:10 -070045 * GifFileType pointer.
46 *
47 * @param stream
48 * Deleted on failure.
49 * codecOut will take ownership of it in the case where we created a codec.
50 * Ownership is unchanged when we returned a gifOut.
51 *
52 */
msarett10522ff2015-09-07 08:54:01 -070053 static bool ReadHeader(SkStream* stream, SkCodec** codecOut,
54 GifFileType** gifOut);
msarett438b2ad2015-04-09 12:43:10 -070055
56 /*
msarett10522ff2015-09-07 08:54:01 -070057 * Performs the full gif decode
msarett8c8f22a2015-04-01 06:58:48 -070058 */
59 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&,
msarette6dd0042015-10-09 11:07:34 -070060 SkPMColor*, int*, int*) override;
msarett8c8f22a2015-04-01 06:58:48 -070061
62 SkEncodedFormat onGetEncodedFormat() const override {
63 return kGIF_SkEncodedFormat;
64 }
65
scroggob427db12015-08-12 07:24:13 -070066 bool onRewind() override;
67
scroggoc5560be2016-02-03 09:42:42 -080068 uint32_t onGetFillValue(SkColorType) const override;
msarette6dd0042015-10-09 11:07:34 -070069
mtkleine9759282015-10-15 08:55:33 -070070 int onOutputScanline(int inputScanline) const override;
msarette6dd0042015-10-09 11:07:34 -070071
msarett8c8f22a2015-04-01 06:58:48 -070072private:
73
74 /*
msarett10522ff2015-09-07 08:54:01 -070075 * A gif can contain multiple image frames. We will only decode the first
76 * frame. This function reads up to the first image frame, processing
77 * transparency and/or animation information that comes before the image
78 * data.
79 *
80 * @param gif Pointer to the library type that manages the gif decode
81 * @param transIndex This call will set the transparent index based on the
82 * extension data.
83 */
scroggo46c57472015-09-30 08:57:13 -070084 static Result ReadUpToFirstImage(GifFileType* gif, uint32_t* transIndex);
msarett10522ff2015-09-07 08:54:01 -070085
86 /*
87 * A gif may contain many image frames, all of different sizes.
msarett4aa02d82015-10-06 07:46:02 -070088 * This function checks if the gif dimensions are valid, based on the frame
89 * dimensions, and corrects the gif dimensions if necessary.
msarett10522ff2015-09-07 08:54:01 -070090 *
msarett4aa02d82015-10-06 07:46:02 -070091 * @param gif Pointer to the library type that manages the gif decode
92 * @param size Size of the image that we will decode.
93 * Will be set by this function if the return value is true.
94 * @param frameRect Contains the dimenions and offset of the first image frame.
95 * Will be set by this function if the return value is true.
96 *
97 * @return true on success, false otherwise
msarett10522ff2015-09-07 08:54:01 -070098 */
msarett4aa02d82015-10-06 07:46:02 -070099 static bool GetDimensions(GifFileType* gif, SkISize* size, SkIRect* frameRect);
msarett10522ff2015-09-07 08:54:01 -0700100
101 /*
102 * Initializes the color table that we will use for decoding.
103 *
104 * @param dstInfo Contains the requested dst color type.
105 * @param inputColorPtr Copies the encoded color table to the client's
106 * input color table if the client requests kIndex8.
107 * @param inputColorCount If the client requests kIndex8, sets
108 * inputColorCount to 256. Since gifs always
109 * contain 8-bit indices, we need a 256 entry color
110 * table to ensure that indexing is always in
111 * bounds.
112 */
113 void initializeColorTable(const SkImageInfo& dstInfo, SkPMColor* colorPtr,
114 int* inputColorCount);
115
116 /*
scroggo3a7701c2015-09-30 09:15:14 -0700117 * Checks for invalid inputs and calls setFrameDimensions(), and
msarett10522ff2015-09-07 08:54:01 -0700118 * initializeColorTable() in the proper sequence.
119 */
scroggo46c57472015-09-30 08:57:13 -0700120 Result prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
msarett10522ff2015-09-07 08:54:01 -0700121 int* inputColorCount, const Options& opts);
122
123 /*
124 * Initializes the swizzler.
125 *
126 * @param dstInfo Output image information. Dimensions may have been
127 * adjusted if the image frame size does not match the size
128 * indicated in the header.
msarettfdb47572015-10-13 12:50:14 -0700129 * @param options Informs the swizzler if destination memory is zero initialized.
130 * Contains subset information.
msarett10522ff2015-09-07 08:54:01 -0700131 */
msarettb30d6982016-02-15 10:18:45 -0800132 void initializeSwizzler(const SkImageInfo& dstInfo,
msarettfdb47572015-10-13 12:50:14 -0700133 const Options& options);
msarett10522ff2015-09-07 08:54:01 -0700134
msarette6dd0042015-10-09 11:07:34 -0700135 SkSampler* getSampler(bool createIfNecessary) override {
136 SkASSERT(fSwizzler);
137 return fSwizzler;
138 }
scroggoe7fc14b2015-10-02 13:14:46 -0700139
msarett10522ff2015-09-07 08:54:01 -0700140 /*
msarette6dd0042015-10-09 11:07:34 -0700141 * @return true if the read is successful and false if the read fails.
msarett10522ff2015-09-07 08:54:01 -0700142 */
msarette6dd0042015-10-09 11:07:34 -0700143 bool readRow();
scroggo46c57472015-09-30 08:57:13 -0700144
145 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& opts,
146 SkPMColor inputColorPtr[], int* inputColorCount) override;
147
msarette6dd0042015-10-09 11:07:34 -0700148 int onGetScanlines(void* dst, int count, size_t rowBytes) override;
scroggo46c57472015-09-30 08:57:13 -0700149
msarett72261c02015-11-19 15:29:26 -0800150 bool onSkipScanlines(int count) override;
151
152 /*
153 * For a scanline decode of "count" lines, this function indicates how
154 * many of the "count" lines should be skipped until we reach the top of
155 * the image frame and how many of the "count" lines are actually inside
156 * the image frame.
157 *
158 * @param count The number of scanlines requested.
159 * @param rowsBeforeFrame Output variable. The number of lines before
160 * we reach the top of the image frame.
161 * @param rowsInFrame Output variable. The number of lines to decode
162 * inside the image frame.
163 */
164 void handleScanlineFrame(int count, int* rowsBeforeFrame, int* rowsInFrame);
165
scroggo46c57472015-09-30 08:57:13 -0700166 SkScanlineOrder onGetScanlineOrder() const override;
167
msarett10522ff2015-09-07 08:54:01 -0700168 /*
msarett8c8f22a2015-04-01 06:58:48 -0700169 * This function cleans up the gif object after the decode completes
170 * It is used in a SkAutoTCallIProc template
171 */
msarett438b2ad2015-04-09 12:43:10 -0700172 static void CloseGif(GifFileType* gif);
msarett8c8f22a2015-04-01 06:58:48 -0700173
174 /*
175 * Frees any extension data used in the decode
176 * Used in a SkAutoTCallVProc
177 */
178 static void FreeExtension(SavedImage* image);
179
180 /*
181 * Creates an instance of the decoder
182 * Called only by NewFromStream
183 *
184 * @param srcInfo contains the source width and height
185 * @param stream the stream of image data
186 * @param gif pointer to library type that manages gif decode
187 * takes ownership
msarett10522ff2015-09-07 08:54:01 -0700188 * @param transIndex The transparent index. An invalid value
189 * indicates that there is no transparent index.
msarett8c8f22a2015-04-01 06:58:48 -0700190 */
msarett4aa02d82015-10-06 07:46:02 -0700191 SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, GifFileType* gif, uint32_t transIndex,
192 const SkIRect& frameRect, bool frameIsSubset);
msarett8c8f22a2015-04-01 06:58:48 -0700193
msarett438b2ad2015-04-09 12:43:10 -0700194 SkAutoTCallVProc<GifFileType, CloseGif> fGif; // owned
msarett10522ff2015-09-07 08:54:01 -0700195 SkAutoTDeleteArray<uint8_t> fSrcBuffer;
msarett4aa02d82015-10-06 07:46:02 -0700196 const SkIRect fFrameRect;
msarett10522ff2015-09-07 08:54:01 -0700197 const uint32_t fTransIndex;
198 uint32_t fFillIndex;
msarett4aa02d82015-10-06 07:46:02 -0700199 const bool fFrameIsSubset;
msarett10522ff2015-09-07 08:54:01 -0700200 SkAutoTDelete<SkSwizzler> fSwizzler;
201 SkAutoTUnref<SkColorTable> fColorTable;
202
msarett8c8f22a2015-04-01 06:58:48 -0700203 typedef SkCodec INHERITED;
204};