blob: fb5d18fc0870b1be25caf18aaea110aa5867186a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkColor.h"
9#include "SkColorPriv.h"
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +000010#include "SkColorTable.h"
11#include "SkImageDecoder.h"
halcanary@google.com29d4e632013-10-11 18:21:56 +000012#include "SkRTConf.h"
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +000013#include "SkScaledBitmapSampler.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkStream.h"
15#include "SkTemplates.h"
halcanary@google.com29d4e632013-10-11 18:21:56 +000016#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
18#include "gif_lib.h"
19
20class SkGIFImageDecoder : public SkImageDecoder {
21public:
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000022 virtual Format getFormat() const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +000023 return kGIF_Format;
24 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000025
reed@android.com8a1c16f2008-12-17 15:59:43 +000026protected:
scroggo2a120802014-10-22 12:07:00 -070027 virtual Result onDecode(SkStream* stream, SkBitmap* bm, Mode mode) SK_OVERRIDE;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000028
29private:
30 typedef SkImageDecoder INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000031};
32
33static const uint8_t gStartingIterlaceYValue[] = {
34 0, 4, 2, 1
35};
36static const uint8_t gDeltaIterlaceYValue[] = {
37 8, 8, 4, 2
38};
39
halcanary@google.com29d4e632013-10-11 18:21:56 +000040SK_CONF_DECLARE(bool, c_suppressGIFImageDecoderWarnings,
41 "images.gif.suppressDecoderWarnings", true,
42 "Suppress GIF warnings and errors when calling image decode "
43 "functions.");
44
45
reed@android.com8a1c16f2008-12-17 15:59:43 +000046/* Implement the GIF interlace algorithm in an iterator.
47 1) grab every 8th line beginning at 0
48 2) grab every 8th line beginning at 4
49 3) grab every 4th line beginning at 2
50 4) grab every 2nd line beginning at 1
51*/
52class GifInterlaceIter {
53public:
54 GifInterlaceIter(int height) : fHeight(height) {
55 fStartYPtr = gStartingIterlaceYValue;
56 fDeltaYPtr = gDeltaIterlaceYValue;
57
58 fCurrY = *fStartYPtr++;
59 fDeltaY = *fDeltaYPtr++;
60 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000061
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 int currY() const {
63 SkASSERT(fStartYPtr);
64 SkASSERT(fDeltaYPtr);
65 return fCurrY;
66 }
67
68 void next() {
69 SkASSERT(fStartYPtr);
70 SkASSERT(fDeltaYPtr);
71
72 int y = fCurrY + fDeltaY;
73 // We went from an if statement to a while loop so that we iterate
74 // through fStartYPtr until a valid row is found. This is so that images
75 // that are smaller than 5x5 will not trash memory.
76 while (y >= fHeight) {
77 if (gStartingIterlaceYValue +
78 SK_ARRAY_COUNT(gStartingIterlaceYValue) == fStartYPtr) {
79 // we done
80 SkDEBUGCODE(fStartYPtr = NULL;)
81 SkDEBUGCODE(fDeltaYPtr = NULL;)
82 y = 0;
83 } else {
84 y = *fStartYPtr++;
85 fDeltaY = *fDeltaYPtr++;
86 }
87 }
88 fCurrY = y;
89 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000090
reed@android.com8a1c16f2008-12-17 15:59:43 +000091private:
92 const int fHeight;
93 int fCurrY;
94 int fDeltaY;
95 const uint8_t* fStartYPtr;
96 const uint8_t* fDeltaYPtr;
97};
98
99///////////////////////////////////////////////////////////////////////////////
100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101static int DecodeCallBackProc(GifFileType* fileType, GifByteType* out,
102 int size) {
103 SkStream* stream = (SkStream*) fileType->UserData;
104 return (int) stream->read(out, size);
105}
106
107void CheckFreeExtension(SavedImage* Image) {
108 if (Image->ExtensionBlocks) {
reed@google.combb896132013-02-01 19:05:48 +0000109#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 FreeExtension(Image);
reed@google.combb896132013-02-01 19:05:48 +0000111#else
112 GifFreeExtensions(&Image->ExtensionBlockCount, &Image->ExtensionBlocks);
113#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 }
115}
116
117// return NULL on failure
118static const ColorMapObject* find_colormap(const GifFileType* gif) {
119 const ColorMapObject* cmap = gif->Image.ColorMap;
120 if (NULL == cmap) {
121 cmap = gif->SColorMap;
122 }
djsollen@google.com57f49692011-02-23 20:46:31 +0000123
124 if (NULL == cmap) {
125 // no colormap found
126 return NULL;
127 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 // some sanity checks
reed@android.combc7d2fb2010-02-05 15:43:07 +0000129 if (cmap && ((unsigned)cmap->ColorCount > 256 ||
130 cmap->ColorCount != (1 << cmap->BitsPerPixel))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 cmap = NULL;
132 }
133 return cmap;
134}
135
136// return -1 if not found (i.e. we're completely opaque)
137static int find_transpIndex(const SavedImage& image, int colorCount) {
138 int transpIndex = -1;
139 for (int i = 0; i < image.ExtensionBlockCount; ++i) {
140 const ExtensionBlock* eb = image.ExtensionBlocks + i;
141 if (eb->Function == 0xF9 && eb->ByteCount == 4) {
142 if (eb->Bytes[0] & 1) {
143 transpIndex = (unsigned char)eb->Bytes[3];
144 // check for valid transpIndex
145 if (transpIndex >= colorCount) {
146 transpIndex = -1;
147 }
148 break;
149 }
150 }
151 }
152 return transpIndex;
153}
154
scroggo2a120802014-10-22 12:07:00 -0700155static SkImageDecoder::Result error_return(const SkBitmap& bm, const char msg[]) {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000156 if (!c_suppressGIFImageDecoderWarnings) {
157 SkDebugf("libgif error [%s] bitmap [%d %d] pixels %p colortable %p\n",
158 msg, bm.width(), bm.height(), bm.getPixels(),
159 bm.getColorTable());
160 }
scroggo2a120802014-10-22 12:07:00 -0700161 return SkImageDecoder::kFailure;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162}
scroggo2a120802014-10-22 12:07:00 -0700163
halcanary@google.com29d4e632013-10-11 18:21:56 +0000164static void gif_warning(const SkBitmap& bm, const char msg[]) {
165 if (!c_suppressGIFImageDecoderWarnings) {
166 SkDebugf("libgif warning [%s] bitmap [%d %d] pixels %p colortable %p\n",
167 msg, bm.width(), bm.height(), bm.getPixels(),
168 bm.getColorTable());
169 }
170}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000172/**
173 * Skip rows in the source gif image.
174 * @param gif Source image.
175 * @param dst Scratch output needed by gif library call. Must be >= width bytes.
176 * @param width Bytes per row in the source image.
177 * @param rowsToSkip Number of rows to skip.
178 * @return True on success, false on GIF_ERROR.
179 */
180static bool skip_src_rows(GifFileType* gif, uint8_t* dst, int width, int rowsToSkip) {
181 for (int i = 0; i < rowsToSkip; i++) {
182 if (DGifGetLine(gif, dst, width) == GIF_ERROR) {
183 return false;
184 }
185 }
186 return true;
187}
188
halcanary@google.com3a8ebd92013-12-19 20:57:45 +0000189/**
190 * GIFs with fewer then 256 color entries will sometimes index out of
191 * bounds of the color table (this is malformed, but libgif does not
192 * check sicne it is rare). This function checks for this error and
193 * fixes it. This makes the output image consistantly deterministic.
194 */
195static void sanitize_indexed_bitmap(SkBitmap* bm) {
reed0689d7b2014-06-14 05:30:20 -0700196 if ((kIndex_8_SkColorType == bm->colorType()) && !(bm->empty())) {
halcanary@google.com3a8ebd92013-12-19 20:57:45 +0000197 SkAutoLockPixels alp(*bm);
bsalomon49f085d2014-09-05 13:34:00 -0700198 if (bm->getPixels()) {
halcanary@google.com3a8ebd92013-12-19 20:57:45 +0000199 SkColorTable* ct = bm->getColorTable(); // Index8 must have it.
200 SkASSERT(ct != NULL);
201 uint32_t count = ct->count();
202 SkASSERT(count > 0);
203 SkASSERT(count <= 0x100);
204 if (count != 0x100) { // Full colortables can't go wrong.
205 // Count is a power of 2; asserted elsewhere.
206 uint8_t byteMask = (~(count - 1));
207 bool warning = false;
208 uint8_t* addr = static_cast<uint8_t*>(bm->getPixels());
209 int height = bm->height();
210 int width = bm->width();
211 size_t rowBytes = bm->rowBytes();
212 while (--height >= 0) {
213 uint8_t* ptr = addr;
214 int x = width;
215 while (--x >= 0) {
216 if (0 != ((*ptr) & byteMask)) {
217 warning = true;
218 *ptr = 0;
219 }
220 ++ptr;
221 }
222 addr += rowBytes;
223 }
224 if (warning) {
225 gif_warning(*bm, "Index out of bounds.");
226 }
227 }
228 }
229 }
230}
231
scroggo2a120802014-10-22 12:07:00 -0700232SkImageDecoder::Result SkGIFImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* bm, Mode mode) {
reed@google.combb896132013-02-01 19:05:48 +0000233#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc);
reed@google.combb896132013-02-01 19:05:48 +0000235#else
236 GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc, NULL);
237#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 if (NULL == gif) {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000239 return error_return(*bm, "DGifOpen");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240 }
241
242 SkAutoTCallIProc<GifFileType, DGifCloseFile> acp(gif);
243
244 SavedImage temp_save;
245 temp_save.ExtensionBlocks=NULL;
246 temp_save.ExtensionBlockCount=0;
247 SkAutoTCallVProc<SavedImage, CheckFreeExtension> acp2(&temp_save);
248
249 int width, height;
250 GifRecordType recType;
251 GifByteType *extData;
reed@google.combb896132013-02-01 19:05:48 +0000252#if GIFLIB_MAJOR >= 5
253 int extFunction;
254#endif
reed@android.com9781ca52009-04-14 14:28:22 +0000255 int transpIndex = -1; // -1 means we don't have it (yet)
halcanary@google.com29d4e632013-10-11 18:21:56 +0000256 int fillIndex = gif->SBackGroundColor;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000257
reed@android.com8a1c16f2008-12-17 15:59:43 +0000258 do {
259 if (DGifGetRecordType(gif, &recType) == GIF_ERROR) {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000260 return error_return(*bm, "DGifGetRecordType");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000262
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 switch (recType) {
264 case IMAGE_DESC_RECORD_TYPE: {
265 if (DGifGetImageDesc(gif) == GIF_ERROR) {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000266 return error_return(*bm, "IMAGE_DESC_RECORD_TYPE");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000267 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000268
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269 if (gif->ImageCount < 1) { // sanity check
halcanary@google.com29d4e632013-10-11 18:21:56 +0000270 return error_return(*bm, "ImageCount < 1");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000272
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 width = gif->SWidth;
274 height = gif->SHeight;
halcanary@google.com29d4e632013-10-11 18:21:56 +0000275
276 SavedImage* image = &gif->SavedImages[gif->ImageCount-1];
277 const GifImageDesc& desc = image->ImageDesc;
278
279 int imageLeft = desc.Left;
280 int imageTop = desc.Top;
281 const int innerWidth = desc.Width;
282 const int innerHeight = desc.Height;
283 if (innerWidth <= 0 || innerHeight <= 0) {
284 return error_return(*bm, "invalid dimensions");
285 }
286
287 // check for valid descriptor
288 if (innerWidth > width) {
289 gif_warning(*bm, "image too wide, expanding output to size");
290 width = innerWidth;
291 imageLeft = 0;
292 } else if (imageLeft + innerWidth > width) {
293 gif_warning(*bm, "shifting image left to fit");
294 imageLeft = width - innerWidth;
295 } else if (imageLeft < 0) {
296 gif_warning(*bm, "shifting image right to fit");
297 imageLeft = 0;
298 }
299
300
301 if (innerHeight > height) {
302 gif_warning(*bm, "image too tall, expanding output to size");
303 height = innerHeight;
304 imageTop = 0;
305 } else if (imageTop + innerHeight > height) {
306 gif_warning(*bm, "shifting image up to fit");
307 imageTop = height - innerHeight;
308 } else if (imageTop < 0) {
309 gif_warning(*bm, "shifting image down to fit");
310 imageTop = 0;
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000311 }
312
reed5926b862014-06-11 10:33:13 -0700313#ifdef SK_SUPPORT_LEGACY_IMAGEDECODER_CHOOSER
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000314 // FIXME: We could give the caller a choice of images or configs.
reed6c225732014-06-09 19:52:07 -0700315 if (!this->chooseFromOneChoice(kIndex_8_SkColorType, width, height)) {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000316 return error_return(*bm, "chooseFromOneChoice");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 }
reed5926b862014-06-11 10:33:13 -0700318#endif
skia.committer@gmail.comc49cabf2013-03-15 07:05:19 +0000319
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000320 SkScaledBitmapSampler sampler(width, height, this->getSampleSize());
321
reed6c225732014-06-09 19:52:07 -0700322 bm->setInfo(SkImageInfo::Make(sampler.scaledWidth(), sampler.scaledHeight(),
323 kIndex_8_SkColorType, kPremul_SkAlphaType));
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000324
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000325 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
scroggo2a120802014-10-22 12:07:00 -0700326 return kSuccess;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000327 }
328
rmistry@google.comd6176b02012-08-23 18:14:13 +0000329
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 // now we decode the colortable
331 int colorCount = 0;
332 {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000333 // Declare colorPtr here for scope.
reed@google.com0a6151d2013-10-10 14:44:56 +0000334 SkPMColor colorPtr[256]; // storage for worst-case
halcanary@google.com29d4e632013-10-11 18:21:56 +0000335 const ColorMapObject* cmap = find_colormap(gif);
halcanary@google.com29d4e632013-10-11 18:21:56 +0000336 if (cmap != NULL) {
halcanary@google.com3a8ebd92013-12-19 20:57:45 +0000337 SkASSERT(cmap->ColorCount == (1 << (cmap->BitsPerPixel)));
halcanary@google.com29d4e632013-10-11 18:21:56 +0000338 colorCount = cmap->ColorCount;
339 if (colorCount > 256) {
340 colorCount = 256; // our kIndex8 can't support more
341 }
342 for (int index = 0; index < colorCount; index++) {
343 colorPtr[index] = SkPackARGB32(0xFF,
344 cmap->Colors[index].Red,
345 cmap->Colors[index].Green,
346 cmap->Colors[index].Blue);
347 }
348 } else {
349 // find_colormap() returned NULL. Some (rare, broken)
350 // GIFs don't have a color table, so we force one.
351 gif_warning(*bm, "missing colormap");
352 colorCount = 256;
353 sk_memset32(colorPtr, SK_ColorWHITE, colorCount);
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000354 }
reed@android.com9781ca52009-04-14 14:28:22 +0000355 transpIndex = find_transpIndex(temp_save, colorCount);
halcanary@google.com29d4e632013-10-11 18:21:56 +0000356 if (transpIndex >= 0) {
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000357 colorPtr[transpIndex] = SK_ColorTRANSPARENT; // ram in a transparent SkPMColor
halcanary@google.com29d4e632013-10-11 18:21:56 +0000358 fillIndex = transpIndex;
359 } else if (fillIndex >= colorCount) {
360 // gif->SBackGroundColor should be less than colorCount.
361 fillIndex = 0; // If not, fix it.
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000362 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000363
reedc5e15a12014-09-29 12:10:27 -0700364 SkAutoTUnref<SkColorTable> ctable(SkNEW_ARGS(SkColorTable, (colorPtr, colorCount)));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000365 if (!this->allocPixelRef(bm, ctable)) {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000366 return error_return(*bm, "allocPixelRef");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367 }
368 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000369
reed@android.com8a1c16f2008-12-17 15:59:43 +0000370 // abort if either inner dimension is <= 0
371 if (innerWidth <= 0 || innerHeight <= 0) {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000372 return error_return(*bm, "non-pos inner width/height");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 }
374
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000375 SkAutoLockPixels alp(*bm);
376
377 SkAutoMalloc storage(innerWidth);
378 uint8_t* scanline = (uint8_t*) storage.get();
379
380 // GIF has an option to store the scanlines of an image, plus a larger background,
381 // filled by a fill color. In this case, we will use a subset of the larger bitmap
382 // for sampling.
383 SkBitmap subset;
384 SkBitmap* workingBitmap;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385 // are we only a subset of the total bounds?
halcanary@google.com29d4e632013-10-11 18:21:56 +0000386 if ((imageTop | imageLeft) > 0 ||
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000387 innerWidth < width || innerHeight < height) {
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000388 // Fill the background.
halcanary@google.com29d4e632013-10-11 18:21:56 +0000389 memset(bm->getPixels(), fillIndex, bm->getSize());
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000390
391 // Create a subset of the bitmap.
halcanary@google.com29d4e632013-10-11 18:21:56 +0000392 SkIRect subsetRect(SkIRect::MakeXYWH(imageLeft / sampler.srcDX(),
393 imageTop / sampler.srcDY(),
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000394 innerWidth / sampler.srcDX(),
395 innerHeight / sampler.srcDY()));
396 if (!bm->extractSubset(&subset, subsetRect)) {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000397 return error_return(*bm, "Extract failed.");
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000398 }
399 // Update the sampler. We'll now be only sampling into the subset.
400 sampler = SkScaledBitmapSampler(innerWidth, innerHeight, this->getSampleSize());
401 workingBitmap = &subset;
402 } else {
403 workingBitmap = bm;
404 }
405
406 // bm is already locked, but if we had to take a subset, it must be locked also,
407 // so that getPixels() will point to its pixels.
408 SkAutoLockPixels alpWorking(*workingBitmap);
409
410 if (!sampler.begin(workingBitmap, SkScaledBitmapSampler::kIndex, *this)) {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000411 return error_return(*bm, "Sampler failed to begin.");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000413
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414 // now decode each scanline
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000415 if (gif->Image.Interlace) {
416 // Iterate over the height of the source data. The sampler will
417 // take care of skipping unneeded rows.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418 GifInterlaceIter iter(innerHeight);
halcanary@google.com29d4e632013-10-11 18:21:56 +0000419 for (int y = 0; y < innerHeight; y++) {
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000420 if (DGifGetLine(gif, scanline, innerWidth) == GIF_ERROR) {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000421 gif_warning(*bm, "interlace DGifGetLine");
422 memset(scanline, fillIndex, innerWidth);
423 for (; y < innerHeight; y++) {
424 sampler.sampleInterlaced(scanline, iter.currY());
425 iter.next();
426 }
scroggo2a120802014-10-22 12:07:00 -0700427 return kPartialSuccess;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428 }
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000429 sampler.sampleInterlaced(scanline, iter.currY());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000430 iter.next();
431 }
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000432 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433 // easy, non-interlace case
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000434 const int outHeight = workingBitmap->height();
435 skip_src_rows(gif, scanline, innerWidth, sampler.srcY0());
436 for (int y = 0; y < outHeight; y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000437 if (DGifGetLine(gif, scanline, innerWidth) == GIF_ERROR) {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000438 gif_warning(*bm, "DGifGetLine");
439 memset(scanline, fillIndex, innerWidth);
440 for (; y < outHeight; y++) {
441 sampler.next(scanline);
442 }
scroggo2a120802014-10-22 12:07:00 -0700443 return kPartialSuccess;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444 }
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000445 // scanline now contains the raw data. Sample it.
446 sampler.next(scanline);
447 if (y < outHeight - 1) {
448 skip_src_rows(gif, scanline, innerWidth, sampler.srcDY() - 1);
449 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000450 }
commit-bot@chromium.orgdac4a1d2013-10-08 19:40:18 +0000451 // skip the rest of the rows (if any)
452 int read = (outHeight - 1) * sampler.srcDY() + sampler.srcY0() + 1;
453 SkASSERT(read <= innerHeight);
454 skip_src_rows(gif, scanline, innerWidth, innerHeight - read);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000455 }
halcanary@google.com3a8ebd92013-12-19 20:57:45 +0000456 sanitize_indexed_bitmap(bm);
scroggo2a120802014-10-22 12:07:00 -0700457 return kSuccess;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000458 } break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000459
reed@android.com8a1c16f2008-12-17 15:59:43 +0000460 case EXTENSION_RECORD_TYPE:
reed@google.combb896132013-02-01 19:05:48 +0000461#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000462 if (DGifGetExtension(gif, &temp_save.Function,
463 &extData) == GIF_ERROR) {
reed@google.combb896132013-02-01 19:05:48 +0000464#else
465 if (DGifGetExtension(gif, &extFunction, &extData) == GIF_ERROR) {
466#endif
halcanary@google.com29d4e632013-10-11 18:21:56 +0000467 return error_return(*bm, "DGifGetExtension");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000468 }
469
470 while (extData != NULL) {
471 /* Create an extension block with our data */
reed@google.combb896132013-02-01 19:05:48 +0000472#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000473 if (AddExtensionBlock(&temp_save, extData[0],
474 &extData[1]) == GIF_ERROR) {
reed@google.combb896132013-02-01 19:05:48 +0000475#else
476 if (GifAddExtensionBlock(&gif->ExtensionBlockCount,
477 &gif->ExtensionBlocks,
478 extFunction,
479 extData[0],
480 &extData[1]) == GIF_ERROR) {
481#endif
halcanary@google.com29d4e632013-10-11 18:21:56 +0000482 return error_return(*bm, "AddExtensionBlock");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000483 }
484 if (DGifGetExtensionNext(gif, &extData) == GIF_ERROR) {
halcanary@google.com29d4e632013-10-11 18:21:56 +0000485 return error_return(*bm, "DGifGetExtensionNext");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000486 }
reed@google.combb896132013-02-01 19:05:48 +0000487#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000488 temp_save.Function = 0;
reed@google.combb896132013-02-01 19:05:48 +0000489#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000490 }
491 break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000492
reed@android.com8a1c16f2008-12-17 15:59:43 +0000493 case TERMINATE_RECORD_TYPE:
494 break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000495
496 default: /* Should be trapped by DGifGetRecordType */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000497 break;
498 }
499 } while (recType != TERMINATE_RECORD_TYPE);
500
halcanary@google.com3a8ebd92013-12-19 20:57:45 +0000501 sanitize_indexed_bitmap(bm);
scroggo2a120802014-10-22 12:07:00 -0700502 return kSuccess;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000503}
504
505///////////////////////////////////////////////////////////////////////////////
robertphillips@google.comec51cb82012-03-23 18:13:47 +0000506DEFINE_DECODER_CREATOR(GIFImageDecoder);
507///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000508
scroggo@google.comb5571b32013-09-25 21:34:24 +0000509static bool is_gif(SkStreamRewindable* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000510 char buf[GIF_STAMP_LEN];
511 if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) {
512 if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 ||
513 memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
514 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000515 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000516 }
517 }
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000518 return false;
519}
520
scroggo@google.comb5571b32013-09-25 21:34:24 +0000521static SkImageDecoder* sk_libgif_dfactory(SkStreamRewindable* stream) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000522 if (is_gif(stream)) {
523 return SkNEW(SkGIFImageDecoder);
524 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000525 return NULL;
526}
527
mtklein@google.combd6343b2013-09-04 17:20:18 +0000528static SkImageDecoder_DecodeReg gReg(sk_libgif_dfactory);
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000529
scroggo@google.comb5571b32013-09-25 21:34:24 +0000530static SkImageDecoder::Format get_format_gif(SkStreamRewindable* stream) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000531 if (is_gif(stream)) {
532 return SkImageDecoder::kGIF_Format;
533 }
534 return SkImageDecoder::kUnknown_Format;
535}
536
mtklein@google.combd6343b2013-09-04 17:20:18 +0000537static SkImageDecoder_FormatReg gFormatReg(get_format_gif);