blob: 774131f648039c5c40993136be133f199a6f74cd [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
msarett8c8f22a2015-04-01 06:58:48 -07008#include "SkCodecPriv.h"
9#include "SkColorPriv.h"
10#include "SkColorTable.h"
msarett1a464672016-01-07 13:17:19 -080011#include "SkGifCodec.h"
msarett8c8f22a2015-04-01 06:58:48 -070012#include "SkStream.h"
13#include "SkSwizzler.h"
14#include "SkUtils.h"
15
msarett39b2d5a2016-02-17 08:26:31 -080016#include "gif_lib.h"
17
msarett8c8f22a2015-04-01 06:58:48 -070018/*
19 * Checks the start of the stream to see if the image is a gif
20 */
scroggodb30be22015-12-08 18:54:13 -080021bool SkGifCodec::IsGif(const void* buf, size_t bytesRead) {
22 if (bytesRead >= GIF_STAMP_LEN) {
msarett8c8f22a2015-04-01 06:58:48 -070023 if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 ||
bungeman0153dea2015-08-27 16:43:42 -070024 memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
25 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0)
26 {
msarett8c8f22a2015-04-01 06:58:48 -070027 return true;
28 }
29 }
30 return false;
31}
32
33/*
msarett8c8f22a2015-04-01 06:58:48 -070034 * Error function
35 */
bungeman0153dea2015-08-27 16:43:42 -070036static SkCodec::Result gif_error(const char* msg, SkCodec::Result result = SkCodec::kInvalidInput) {
msarett8c8f22a2015-04-01 06:58:48 -070037 SkCodecPrintf("Gif Error: %s\n", msg);
38 return result;
39}
40
41
42/*
43 * Read function that will be passed to gif_lib
44 */
bungeman0153dea2015-08-27 16:43:42 -070045static int32_t read_bytes_callback(GifFileType* fileType, GifByteType* out, int32_t size) {
msarett8c8f22a2015-04-01 06:58:48 -070046 SkStream* stream = (SkStream*) fileType->UserData;
47 return (int32_t) stream->read(out, size);
48}
49
50/*
51 * Open the gif file
52 */
53static GifFileType* open_gif(SkStream* stream) {
msarett4691d992016-02-16 13:16:39 -080054#if GIFLIB_MAJOR < 5
55 return DGifOpen(stream, read_bytes_callback);
56#else
halcanary96fcdcc2015-08-27 07:41:13 -070057 return DGifOpen(stream, read_bytes_callback, nullptr);
msarett4691d992016-02-16 13:16:39 -080058#endif
msarett8c8f22a2015-04-01 06:58:48 -070059}
60
msarett8c8f22a2015-04-01 06:58:48 -070061/*
62 * Check if a there is an index of the color table for a transparent pixel
63 */
64static uint32_t find_trans_index(const SavedImage& image) {
65 // If there is a transparent index specified, it will be contained in an
66 // extension block. We will loop through extension blocks in reverse order
67 // to check the most recent extension blocks first.
68 for (int32_t i = image.ExtensionBlockCount - 1; i >= 0; i--) {
69 // Get an extension block
70 const ExtensionBlock& extBlock = image.ExtensionBlocks[i];
71
72 // Specifically, we need to check for a graphics control extension,
73 // which may contain transparency information. Also, note that a valid
74 // graphics control extension is always four bytes. The fourth byte
75 // is the transparent index (if it exists), so we need at least four
76 // bytes.
bungeman0153dea2015-08-27 16:43:42 -070077 if (GRAPHICS_EXT_FUNC_CODE == extBlock.Function && extBlock.ByteCount >= 4) {
msarett8c8f22a2015-04-01 06:58:48 -070078 // Check the transparent color flag which indicates whether a
79 // transparent index exists. It is the least significant bit of
80 // the first byte of the extension block.
81 if (1 == (extBlock.Bytes[0] & 1)) {
msarett8c8f22a2015-04-01 06:58:48 -070082 // Use uint32_t to prevent sign extending
83 return extBlock.Bytes[3];
84 }
85
86 // There should only be one graphics control extension for the image frame
87 break;
88 }
89 }
90
91 // Use maximum unsigned int (surely an invalid index) to indicate that a valid
92 // index was not found.
93 return SK_MaxU32;
94}
95
msarette6dd0042015-10-09 11:07:34 -070096inline uint32_t ceil_div(uint32_t a, uint32_t b) {
msarettd02b99f2015-08-28 07:36:55 -070097 return (a + b - 1) / b;
98}
99
100/*
101 * Gets the output row corresponding to the encoded row for interlaced gifs
102 */
msarette6dd0042015-10-09 11:07:34 -0700103inline uint32_t get_output_row_interlaced(uint32_t encodedRow, uint32_t height) {
msarettd02b99f2015-08-28 07:36:55 -0700104 SkASSERT(encodedRow < height);
105 // First pass
106 if (encodedRow * 8 < height) {
107 return encodedRow * 8;
108 }
109 // Second pass
110 if (encodedRow * 4 < height) {
111 return 4 + 8 * (encodedRow - ceil_div(height, 8));
112 }
113 // Third pass
114 if (encodedRow * 2 < height) {
115 return 2 + 4 * (encodedRow - ceil_div(height, 4));
116 }
117 // Fourth pass
118 return 1 + 2 * (encodedRow - ceil_div(height, 2));
119}
120
msarett8c8f22a2015-04-01 06:58:48 -0700121/*
msarett10522ff2015-09-07 08:54:01 -0700122 * This function cleans up the gif object after the decode completes
123 * It is used in a SkAutoTCallIProc template
124 */
125void SkGifCodec::CloseGif(GifFileType* gif) {
msarett4691d992016-02-16 13:16:39 -0800126#if GIFLIB_MAJOR < 5 || (GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 0)
127 DGifCloseFile(gif);
128#else
129 DGifCloseFile(gif, nullptr);
130#endif
msarett10522ff2015-09-07 08:54:01 -0700131}
132
133/*
134 * This function free extension data that has been saved to assist the image
135 * decoder
136 */
137void SkGifCodec::FreeExtension(SavedImage* image) {
138 if (NULL != image->ExtensionBlocks) {
msarett4691d992016-02-16 13:16:39 -0800139#if GIFLIB_MAJOR < 5
140 FreeExtension(image);
141#else
msarett10522ff2015-09-07 08:54:01 -0700142 GifFreeExtensions(&image->ExtensionBlockCount, &image->ExtensionBlocks);
msarett4691d992016-02-16 13:16:39 -0800143#endif
msarett10522ff2015-09-07 08:54:01 -0700144 }
145}
146
147/*
msarett438b2ad2015-04-09 12:43:10 -0700148 * Read enough of the stream to initialize the SkGifCodec.
149 * Returns a bool representing success or failure.
150 *
151 * @param codecOut
halcanary96fcdcc2015-08-27 07:41:13 -0700152 * If it returned true, and codecOut was not nullptr,
msarett438b2ad2015-04-09 12:43:10 -0700153 * codecOut will be set to a new SkGifCodec.
154 *
155 * @param gifOut
halcanary96fcdcc2015-08-27 07:41:13 -0700156 * If it returned true, and codecOut was nullptr,
157 * gifOut must be non-nullptr and gifOut will be set to a new
msarett438b2ad2015-04-09 12:43:10 -0700158 * GifFileType pointer.
159 *
160 * @param stream
161 * Deleted on failure.
162 * codecOut will take ownership of it in the case where we created a codec.
163 * Ownership is unchanged when we returned a gifOut.
164 *
165 */
166bool SkGifCodec::ReadHeader(SkStream* stream, SkCodec** codecOut, GifFileType** gifOut) {
167 SkAutoTDelete<SkStream> streamDeleter(stream);
168
169 // Read gif header, logical screen descriptor, and global color table
170 SkAutoTCallVProc<GifFileType, CloseGif> gif(open_gif(stream));
171
halcanary96fcdcc2015-08-27 07:41:13 -0700172 if (nullptr == gif) {
msarett438b2ad2015-04-09 12:43:10 -0700173 gif_error("DGifOpen failed.\n");
174 return false;
175 }
176
msarett10522ff2015-09-07 08:54:01 -0700177 // Read through gif extensions to get to the image data. Set the
178 // transparent index based on the extension data.
179 uint32_t transIndex;
180 SkCodec::Result result = ReadUpToFirstImage(gif, &transIndex);
181 if (kSuccess != result){
182 return false;
183 }
184
185 // Read the image descriptor
186 if (GIF_ERROR == DGifGetImageDesc(gif)) {
187 return false;
188 }
189 // If reading the image descriptor is successful, the image count will be
190 // incremented.
191 SkASSERT(gif->ImageCount >= 1);
192
halcanary96fcdcc2015-08-27 07:41:13 -0700193 if (nullptr != codecOut) {
msarett4aa02d82015-10-06 07:46:02 -0700194 SkISize size;
195 SkIRect frameRect;
196 if (!GetDimensions(gif, &size, &frameRect)) {
197 gif_error("Invalid gif size.\n");
msarettf724b992015-10-15 06:41:06 -0700198 return false;
msarett438b2ad2015-04-09 12:43:10 -0700199 }
msarett4aa02d82015-10-06 07:46:02 -0700200 bool frameIsSubset = (size != frameRect.size());
msarett438b2ad2015-04-09 12:43:10 -0700201
msarettc30c4182016-04-20 11:53:35 -0700202 // Determine the encoded alpha type. The transIndex might be valid if it less
msarett10522ff2015-09-07 08:54:01 -0700203 // than 256. We are not certain that the index is valid until we process the color
204 // table, since some gifs have color tables with less than 256 colors. If
205 // there might be a valid transparent index, we must indicate that the image has
206 // alpha.
msarettc30c4182016-04-20 11:53:35 -0700207 // In the case where we must support alpha, we indicate kBinary, since every
208 // pixel will either be fully opaque or fully transparent.
209 SkEncodedInfo::Alpha alpha = (transIndex < 256) ? SkEncodedInfo::kBinary_Alpha :
210 SkEncodedInfo::kOpaque_Alpha;
msarett10522ff2015-09-07 08:54:01 -0700211
msarett438b2ad2015-04-09 12:43:10 -0700212 // Return the codec
msarettc30c4182016-04-20 11:53:35 -0700213 // Use kPalette since Gifs are encoded with a color table.
214 // Use 8-bits per component, since this is the output we get from giflib.
215 // FIXME: Gifs can actually be encoded with 4-bits per pixel. Can we support this?
216 SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, alpha, 8);
217 *codecOut = new SkGifCodec(size.width(), size.height(), info, streamDeleter.release(),
218 gif.release(), transIndex, frameRect, frameIsSubset);
msarett438b2ad2015-04-09 12:43:10 -0700219 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700220 SkASSERT(nullptr != gifOut);
mtklein18300a32016-03-16 13:53:35 -0700221 streamDeleter.release();
222 *gifOut = gif.release();
msarett438b2ad2015-04-09 12:43:10 -0700223 }
224 return true;
225}
226
227/*
msarett8c8f22a2015-04-01 06:58:48 -0700228 * Assumes IsGif was called and returned true
229 * Creates a gif decoder
230 * Reads enough of the stream to determine the image format
231 */
232SkCodec* SkGifCodec::NewFromStream(SkStream* stream) {
halcanary96fcdcc2015-08-27 07:41:13 -0700233 SkCodec* codec = nullptr;
234 if (ReadHeader(stream, &codec, nullptr)) {
msarett438b2ad2015-04-09 12:43:10 -0700235 return codec;
msarett8c8f22a2015-04-01 06:58:48 -0700236 }
halcanary96fcdcc2015-08-27 07:41:13 -0700237 return nullptr;
msarett8c8f22a2015-04-01 06:58:48 -0700238}
239
msarettc30c4182016-04-20 11:53:35 -0700240SkGifCodec::SkGifCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
241 GifFileType* gif, uint32_t transIndex, const SkIRect& frameRect, bool frameIsSubset)
242 : INHERITED(width, height, info, stream)
msarett8c8f22a2015-04-01 06:58:48 -0700243 , fGif(gif)
msarett10522ff2015-09-07 08:54:01 -0700244 , fSrcBuffer(new uint8_t[this->getInfo().width()])
msarettf724b992015-10-15 06:41:06 -0700245 , fFrameRect(frameRect)
msarett10522ff2015-09-07 08:54:01 -0700246 // If it is valid, fTransIndex will be used to set fFillIndex. We don't know if
247 // fTransIndex is valid until we process the color table, since fTransIndex may
248 // be greater than the size of the color table.
249 , fTransIndex(transIndex)
250 // Default fFillIndex is 0. We will overwrite this if fTransIndex is valid, or if
251 // there is a valid background color.
252 , fFillIndex(0)
msarett4aa02d82015-10-06 07:46:02 -0700253 , fFrameIsSubset(frameIsSubset)
msarett10522ff2015-09-07 08:54:01 -0700254 , fSwizzler(NULL)
msarettf724b992015-10-15 06:41:06 -0700255 , fColorTable(NULL)
msarett8c8f22a2015-04-01 06:58:48 -0700256{}
257
scroggob427db12015-08-12 07:24:13 -0700258bool SkGifCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -0700259 GifFileType* gifOut = nullptr;
260 if (!ReadHeader(this->stream(), nullptr, &gifOut)) {
scroggob427db12015-08-12 07:24:13 -0700261 return false;
262 }
263
halcanary96fcdcc2015-08-27 07:41:13 -0700264 SkASSERT(nullptr != gifOut);
scroggob427db12015-08-12 07:24:13 -0700265 fGif.reset(gifOut);
266 return true;
267}
268
msarett10522ff2015-09-07 08:54:01 -0700269SkCodec::Result SkGifCodec::ReadUpToFirstImage(GifFileType* gif, uint32_t* transIndex) {
msarett8c8f22a2015-04-01 06:58:48 -0700270 // Use this as a container to hold information about any gif extension
271 // blocks. This generally stores transparency and animation instructions.
272 SavedImage saveExt;
273 SkAutoTCallVProc<SavedImage, FreeExtension> autoFreeExt(&saveExt);
halcanary96fcdcc2015-08-27 07:41:13 -0700274 saveExt.ExtensionBlocks = nullptr;
msarett8c8f22a2015-04-01 06:58:48 -0700275 saveExt.ExtensionBlockCount = 0;
276 GifByteType* extData;
msarett8c8f22a2015-04-01 06:58:48 -0700277 int32_t extFunction;
msarett8c8f22a2015-04-01 06:58:48 -0700278
279 // We will loop over components of gif images until we find an image. Once
280 // we find an image, we will decode and return it. While many gif files
281 // contain more than one image, we will simply decode the first image.
msarett8c8f22a2015-04-01 06:58:48 -0700282 GifRecordType recordType;
283 do {
284 // Get the current record type
msarett10522ff2015-09-07 08:54:01 -0700285 if (GIF_ERROR == DGifGetRecordType(gif, &recordType)) {
msarett8c8f22a2015-04-01 06:58:48 -0700286 return gif_error("DGifGetRecordType failed.\n", kInvalidInput);
287 }
msarett8c8f22a2015-04-01 06:58:48 -0700288 switch (recordType) {
289 case IMAGE_DESC_RECORD_TYPE: {
msarett10522ff2015-09-07 08:54:01 -0700290 *transIndex = find_trans_index(saveExt);
msarett4aa02d82015-10-06 07:46:02 -0700291
msarett8c8f22a2015-04-01 06:58:48 -0700292 // FIXME: Gif files may have multiple images stored in a single
293 // file. This is most commonly used to enable
294 // animations. Since we are leaving animated gifs as a
295 // TODO, we will return kSuccess after decoding the
296 // first image in the file. This is the same behavior
297 // as SkImageDecoder_libgif.
298 //
299 // Most times this works pretty well, but sometimes it
300 // doesn't. For example, I have an animated test image
301 // where the first image in the file is 1x1, but the
302 // subsequent images are meaningful. This currently
303 // displays the 1x1 image, which is not ideal. Right
304 // now I am leaving this as an issue that will be
305 // addressed when we implement animated gifs.
306 //
307 // It is also possible (not explicitly disallowed in the
308 // specification) that gif files provide multiple
309 // images in a single file that are all meant to be
310 // displayed in the same frame together. I will
311 // currently leave this unimplemented until I find a
312 // test case that expects this behavior.
313 return kSuccess;
314 }
msarett8c8f22a2015-04-01 06:58:48 -0700315 // Extensions are used to specify special properties of the image
316 // such as transparency or animation.
317 case EXTENSION_RECORD_TYPE:
318 // Read extension data
msarett10522ff2015-09-07 08:54:01 -0700319 if (GIF_ERROR == DGifGetExtension(gif, &extFunction, &extData)) {
bungeman0153dea2015-08-27 16:43:42 -0700320 return gif_error("Could not get extension.\n", kIncompleteInput);
msarett8c8f22a2015-04-01 06:58:48 -0700321 }
322
323 // Create an extension block with our data
halcanary96fcdcc2015-08-27 07:41:13 -0700324 while (nullptr != extData) {
msarett8c8f22a2015-04-01 06:58:48 -0700325 // Add a single block
msarett4691d992016-02-16 13:16:39 -0800326
327#if GIFLIB_MAJOR < 5
328 if (AddExtensionBlock(&saveExt, extData[0],
329 &extData[1]) == GIF_ERROR) {
330#else
bungeman0153dea2015-08-27 16:43:42 -0700331 if (GIF_ERROR == GifAddExtensionBlock(&saveExt.ExtensionBlockCount,
332 &saveExt.ExtensionBlocks,
msarett4691d992016-02-16 13:16:39 -0800333 extFunction, extData[0], &extData[1])) {
334#endif
bungeman0153dea2015-08-27 16:43:42 -0700335 return gif_error("Could not add extension block.\n", kIncompleteInput);
msarett8c8f22a2015-04-01 06:58:48 -0700336 }
337 // Move to the next block
msarett10522ff2015-09-07 08:54:01 -0700338 if (GIF_ERROR == DGifGetExtensionNext(gif, &extData)) {
bungeman0153dea2015-08-27 16:43:42 -0700339 return gif_error("Could not get next extension.\n", kIncompleteInput);
msarett8c8f22a2015-04-01 06:58:48 -0700340 }
msarett8c8f22a2015-04-01 06:58:48 -0700341 }
342 break;
343
344 // Signals the end of the gif file
345 case TERMINATE_RECORD_TYPE:
346 break;
347
348 default:
msarett10522ff2015-09-07 08:54:01 -0700349 // DGifGetRecordType returns an error if the record type does
350 // not match one of the above cases. This should not be
351 // reached.
msarett8c8f22a2015-04-01 06:58:48 -0700352 SkASSERT(false);
353 break;
354 }
355 } while (TERMINATE_RECORD_TYPE != recordType);
356
bungeman0153dea2015-08-27 16:43:42 -0700357 return gif_error("Could not find any images to decode in gif file.\n", kInvalidInput);
msarett8c8f22a2015-04-01 06:58:48 -0700358}
msarett10522ff2015-09-07 08:54:01 -0700359
msarett4aa02d82015-10-06 07:46:02 -0700360bool SkGifCodec::GetDimensions(GifFileType* gif, SkISize* size, SkIRect* frameRect) {
361 // Get the encoded dimension values
362 SavedImage* image = &gif->SavedImages[gif->ImageCount - 1];
363 const GifImageDesc& desc = image->ImageDesc;
364 int frameLeft = desc.Left;
365 int frameTop = desc.Top;
366 int frameWidth = desc.Width;
367 int frameHeight = desc.Height;
368 int width = gif->SWidth;
369 int height = gif->SHeight;
370
371 // Ensure that the decode dimensions are large enough to contain the frame
372 width = SkTMax(width, frameWidth + frameLeft);
373 height = SkTMax(height, frameHeight + frameTop);
374
375 // All of these dimensions should be positive, as they are encoded as unsigned 16-bit integers.
376 // It is unclear why giflib casts them to ints. We will go ahead and check that they are
377 // in fact positive.
378 if (frameLeft < 0 || frameTop < 0 || frameWidth < 0 || frameHeight < 0 || width <= 0 ||
379 height <= 0) {
msarett10522ff2015-09-07 08:54:01 -0700380 return false;
381 }
382
msarett4aa02d82015-10-06 07:46:02 -0700383 frameRect->setXYWH(frameLeft, frameTop, frameWidth, frameHeight);
384 size->set(width, height);
msarett10522ff2015-09-07 08:54:01 -0700385 return true;
386}
387
388void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
389 int* inputColorCount) {
390 // Set up our own color table
391 const uint32_t maxColors = 256;
392 SkPMColor colorPtr[256];
393 if (NULL != inputColorCount) {
394 // We set the number of colors to maxColors in order to ensure
395 // safe memory accesses. Otherwise, an invalid pixel could
396 // access memory outside of our color table array.
397 *inputColorCount = maxColors;
398 }
399
400 // Get local color table
401 ColorMapObject* colorMap = fGif->Image.ColorMap;
402 // If there is no local color table, use the global color table
403 if (NULL == colorMap) {
404 colorMap = fGif->SColorMap;
405 }
406
407 uint32_t colorCount = 0;
408 if (NULL != colorMap) {
409 colorCount = colorMap->ColorCount;
410 // giflib guarantees these properties
411 SkASSERT(colorCount == (unsigned) (1 << (colorMap->BitsPerPixel)));
412 SkASSERT(colorCount <= 256);
413 for (uint32_t i = 0; i < colorCount; i++) {
414 colorPtr[i] = SkPackARGB32(0xFF, colorMap->Colors[i].Red,
415 colorMap->Colors[i].Green, colorMap->Colors[i].Blue);
416 }
417 }
418
msarett10522ff2015-09-07 08:54:01 -0700419 // Fill in the color table for indices greater than color count.
420 // This allows for predictable, safe behavior.
msarett7f7ec202016-03-01 12:12:27 -0800421 if (colorCount > 0) {
422 // Gifs have the option to specify the color at a single index of the color
423 // table as transparent. If the transparent index is greater than the
424 // colorCount, we know that there is no valid transparent color in the color
425 // table. If there is not valid transparent index, we will try to use the
426 // backgroundIndex as the fill index. If the backgroundIndex is also not
427 // valid, we will let fFillIndex default to 0 (it is set to zero in the
428 // constructor). This behavior is not specified but matches
429 // SkImageDecoder_libgif.
430 uint32_t backgroundIndex = fGif->SBackGroundColor;
431 if (fTransIndex < colorCount) {
432 colorPtr[fTransIndex] = SK_ColorTRANSPARENT;
433 fFillIndex = fTransIndex;
434 } else if (backgroundIndex < colorCount) {
435 fFillIndex = backgroundIndex;
436 }
437
438 for (uint32_t i = colorCount; i < maxColors; i++) {
439 colorPtr[i] = colorPtr[fFillIndex];
440 }
441 } else {
442 sk_memset32(colorPtr, 0xFF000000, maxColors);
msarett10522ff2015-09-07 08:54:01 -0700443 }
444
445 fColorTable.reset(new SkColorTable(colorPtr, maxColors));
446 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount);
447}
448
449SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
450 int* inputColorCount, const Options& opts) {
msarett10522ff2015-09-07 08:54:01 -0700451 // Check for valid input parameters
msarett10522ff2015-09-07 08:54:01 -0700452 if (!conversion_possible(dstInfo, this->getInfo())) {
453 return gif_error("Cannot convert input type to output type.\n",
454 kInvalidConversion);
455 }
456
msarett10522ff2015-09-07 08:54:01 -0700457 // Initialize color table and copy to the client if necessary
458 this->initializeColorTable(dstInfo, inputColorPtr, inputColorCount);
msarett5af4e0b2015-11-17 11:18:03 -0800459
msarettb30d6982016-02-15 10:18:45 -0800460 this->initializeSwizzler(dstInfo, opts);
461 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700462}
463
msarettb30d6982016-02-15 10:18:45 -0800464void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) {
msarett10522ff2015-09-07 08:54:01 -0700465 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
msarett5af4e0b2015-11-17 11:18:03 -0800466 const SkIRect* frameRect = fFrameIsSubset ? &fFrameRect : nullptr;
msaretta45a6682016-04-22 13:18:37 -0700467 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), colorPtr, dstInfo, opts,
msarett5af4e0b2015-11-17 11:18:03 -0800468 frameRect));
msarettb30d6982016-02-15 10:18:45 -0800469 SkASSERT(fSwizzler);
msarett10522ff2015-09-07 08:54:01 -0700470}
471
msarette6dd0042015-10-09 11:07:34 -0700472bool SkGifCodec::readRow() {
473 return GIF_ERROR != DGifGetLine(fGif, fSrcBuffer.get(), fFrameRect.width());
msarett10522ff2015-09-07 08:54:01 -0700474}
475
476/*
477 * Initiates the gif decode
478 */
479SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo,
480 void* dst, size_t dstRowBytes,
481 const Options& opts,
482 SkPMColor* inputColorPtr,
msarette6dd0042015-10-09 11:07:34 -0700483 int* inputColorCount,
484 int* rowsDecoded) {
msarett10522ff2015-09-07 08:54:01 -0700485 Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts);
486 if (kSuccess != result) {
487 return result;
488 }
489
490 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
491 return gif_error("Scaling not supported.\n", kInvalidScale);
492 }
493
494 // Initialize the swizzler
495 if (fFrameIsSubset) {
msarett10522ff2015-09-07 08:54:01 -0700496 // Fill the background
scroggoc5560be2016-02-03 09:42:42 -0800497 SkSampler::Fill(dstInfo, dst, dstRowBytes, this->getFillValue(dstInfo.colorType()),
msarette6dd0042015-10-09 11:07:34 -0700498 opts.fZeroInitialized);
msarett10522ff2015-09-07 08:54:01 -0700499 }
500
msarette6dd0042015-10-09 11:07:34 -0700501 // Iterate over rows of the input
msarett5af4e0b2015-11-17 11:18:03 -0800502 for (int y = fFrameRect.top(); y < fFrameRect.bottom(); y++) {
msarette6dd0042015-10-09 11:07:34 -0700503 if (!this->readRow()) {
504 *rowsDecoded = y;
505 return gif_error("Could not decode line.\n", kIncompleteInput);
msarett10522ff2015-09-07 08:54:01 -0700506 }
msarette6dd0042015-10-09 11:07:34 -0700507 void* dstRow = SkTAddOffset<void>(dst, dstRowBytes * this->outputScanline(y));
508 fSwizzler->swizzle(dstRow, fSrcBuffer.get());
msarett10522ff2015-09-07 08:54:01 -0700509 }
510 return kSuccess;
511}
512
msarette6dd0042015-10-09 11:07:34 -0700513// FIXME: This is similar to the implementation for bmp and png. Can we share more code or
514// possibly make this non-virtual?
scroggoc5560be2016-02-03 09:42:42 -0800515uint32_t SkGifCodec::onGetFillValue(SkColorType colorType) const {
msarette6dd0042015-10-09 11:07:34 -0700516 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
517 return get_color_table_fill_value(colorType, colorPtr, fFillIndex);
518}
519
scroggo46c57472015-09-30 08:57:13 -0700520SkCodec::Result SkGifCodec::onStartScanlineDecode(const SkImageInfo& dstInfo,
521 const SkCodec::Options& opts, SkPMColor inputColorPtr[], int* inputColorCount) {
msarett5af4e0b2015-11-17 11:18:03 -0800522 return this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, this->options());
scroggo46c57472015-09-30 08:57:13 -0700523}
msarett10522ff2015-09-07 08:54:01 -0700524
msarett72261c02015-11-19 15:29:26 -0800525void SkGifCodec::handleScanlineFrame(int count, int* rowsBeforeFrame, int* rowsInFrame) {
526 if (fFrameIsSubset) {
msarettcb0d5c92015-12-03 12:23:43 -0800527 const int currRow = this->currScanline();
msarett72261c02015-11-19 15:29:26 -0800528
529 // The number of rows that remain to be skipped before reaching rows that we
530 // actually must decode into.
531 // This must be at least zero. We also make sure that it is less than or
532 // equal to count, since we will skip at most count rows.
533 *rowsBeforeFrame = SkTMin(count, SkTMax(0, fFrameRect.top() - currRow));
534
535 // Rows left to decode once we reach the start of the frame.
536 const int rowsLeft = count - *rowsBeforeFrame;
537
538 // Count the number of that extend beyond the bottom of the frame. We do not
539 // need to decode into these rows.
540 const int rowsAfterFrame = SkTMax(0, currRow + rowsLeft - fFrameRect.bottom());
541
542 // Set the actual number of source rows that we need to decode.
543 *rowsInFrame = rowsLeft - rowsAfterFrame;
544 } else {
545 *rowsBeforeFrame = 0;
546 *rowsInFrame = count;
547 }
548}
549
msarette6dd0042015-10-09 11:07:34 -0700550int SkGifCodec::onGetScanlines(void* dst, int count, size_t rowBytes) {
msarett72261c02015-11-19 15:29:26 -0800551 int rowsBeforeFrame;
552 int rowsInFrame;
553 this->handleScanlineFrame(count, &rowsBeforeFrame, &rowsInFrame);
554
scroggo46c57472015-09-30 08:57:13 -0700555 if (fFrameIsSubset) {
556 // Fill the requested rows
msarette6dd0042015-10-09 11:07:34 -0700557 SkImageInfo fillInfo = this->dstInfo().makeWH(this->dstInfo().width(), count);
scroggoc5560be2016-02-03 09:42:42 -0800558 uint32_t fillValue = this->onGetFillValue(this->dstInfo().colorType());
msarett5af4e0b2015-11-17 11:18:03 -0800559 fSwizzler->fill(fillInfo, dst, rowBytes, fillValue, this->options().fZeroInitialized);
scroggo46c57472015-09-30 08:57:13 -0700560
msarett72261c02015-11-19 15:29:26 -0800561 // Start to write pixels at the start of the image frame
msarett4aa02d82015-10-06 07:46:02 -0700562 dst = SkTAddOffset<void>(dst, rowBytes * rowsBeforeFrame);
msarett10522ff2015-09-07 08:54:01 -0700563 }
564
msarette6dd0042015-10-09 11:07:34 -0700565 for (int i = 0; i < rowsInFrame; i++) {
566 if (!this->readRow()) {
567 return i + rowsBeforeFrame;
msarett10522ff2015-09-07 08:54:01 -0700568 }
scroggo46c57472015-09-30 08:57:13 -0700569 fSwizzler->swizzle(dst, fSrcBuffer.get());
570 dst = SkTAddOffset<void>(dst, rowBytes);
msarett10522ff2015-09-07 08:54:01 -0700571 }
msarette6dd0042015-10-09 11:07:34 -0700572
573 return count;
msarett10522ff2015-09-07 08:54:01 -0700574}
scroggo46c57472015-09-30 08:57:13 -0700575
msarett72261c02015-11-19 15:29:26 -0800576bool SkGifCodec::onSkipScanlines(int count) {
577 int rowsBeforeFrame;
578 int rowsInFrame;
579 this->handleScanlineFrame(count, &rowsBeforeFrame, &rowsInFrame);
580
581 for (int i = 0; i < rowsInFrame; i++) {
582 if (!this->readRow()) {
583 return false;
584 }
585 }
586
587 return true;
588}
589
scroggo46c57472015-09-30 08:57:13 -0700590SkCodec::SkScanlineOrder SkGifCodec::onGetScanlineOrder() const {
591 if (fGif->Image.Interlace) {
592 return kOutOfOrder_SkScanlineOrder;
scroggo46c57472015-09-30 08:57:13 -0700593 }
msarette6dd0042015-10-09 11:07:34 -0700594 return kTopDown_SkScanlineOrder;
scroggo46c57472015-09-30 08:57:13 -0700595}
596
msarette6dd0042015-10-09 11:07:34 -0700597int SkGifCodec::onOutputScanline(int inputScanline) const {
scroggo46c57472015-09-30 08:57:13 -0700598 if (fGif->Image.Interlace) {
msarette6dd0042015-10-09 11:07:34 -0700599 if (inputScanline < fFrameRect.top() || inputScanline >= fFrameRect.bottom()) {
600 return inputScanline;
msarett4aa02d82015-10-06 07:46:02 -0700601 }
msarett5af4e0b2015-11-17 11:18:03 -0800602 return get_output_row_interlaced(inputScanline - fFrameRect.top(), fFrameRect.height()) +
603 fFrameRect.top();
scroggo46c57472015-09-30 08:57:13 -0700604 }
msarette6dd0042015-10-09 11:07:34 -0700605 return inputScanline;
scroggo46c57472015-09-30 08:57:13 -0700606}