blob: 3e4cda8a0bac26611ca1760c3da18aeaaf0f973c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkImageDecoder.h"
11#include "SkColor.h"
12#include "SkColorPriv.h"
13#include "SkStream.h"
14#include "SkTemplates.h"
15#include "SkPackBits.h"
16
17#include "gif_lib.h"
18
19class SkGIFImageDecoder : public SkImageDecoder {
20public:
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000021 virtual Format getFormat() const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 return kGIF_Format;
23 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000024
reed@android.com8a1c16f2008-12-17 15:59:43 +000025protected:
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000026 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode mode) SK_OVERRIDE;
27
28private:
29 typedef SkImageDecoder INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000030};
31
32static const uint8_t gStartingIterlaceYValue[] = {
33 0, 4, 2, 1
34};
35static const uint8_t gDeltaIterlaceYValue[] = {
36 8, 8, 4, 2
37};
38
39/* Implement the GIF interlace algorithm in an iterator.
40 1) grab every 8th line beginning at 0
41 2) grab every 8th line beginning at 4
42 3) grab every 4th line beginning at 2
43 4) grab every 2nd line beginning at 1
44*/
45class GifInterlaceIter {
46public:
47 GifInterlaceIter(int height) : fHeight(height) {
48 fStartYPtr = gStartingIterlaceYValue;
49 fDeltaYPtr = gDeltaIterlaceYValue;
50
51 fCurrY = *fStartYPtr++;
52 fDeltaY = *fDeltaYPtr++;
53 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 int currY() const {
56 SkASSERT(fStartYPtr);
57 SkASSERT(fDeltaYPtr);
58 return fCurrY;
59 }
60
61 void next() {
62 SkASSERT(fStartYPtr);
63 SkASSERT(fDeltaYPtr);
64
65 int y = fCurrY + fDeltaY;
66 // We went from an if statement to a while loop so that we iterate
67 // through fStartYPtr until a valid row is found. This is so that images
68 // that are smaller than 5x5 will not trash memory.
69 while (y >= fHeight) {
70 if (gStartingIterlaceYValue +
71 SK_ARRAY_COUNT(gStartingIterlaceYValue) == fStartYPtr) {
72 // we done
73 SkDEBUGCODE(fStartYPtr = NULL;)
74 SkDEBUGCODE(fDeltaYPtr = NULL;)
75 y = 0;
76 } else {
77 y = *fStartYPtr++;
78 fDeltaY = *fDeltaYPtr++;
79 }
80 }
81 fCurrY = y;
82 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000083
reed@android.com8a1c16f2008-12-17 15:59:43 +000084private:
85 const int fHeight;
86 int fCurrY;
87 int fDeltaY;
88 const uint8_t* fStartYPtr;
89 const uint8_t* fDeltaYPtr;
90};
91
92///////////////////////////////////////////////////////////////////////////////
93
94//#define GIF_STAMP "GIF" /* First chars in file - GIF stamp. */
95//#define GIF_STAMP_LEN (sizeof(GIF_STAMP) - 1)
96
97static int DecodeCallBackProc(GifFileType* fileType, GifByteType* out,
98 int size) {
99 SkStream* stream = (SkStream*) fileType->UserData;
100 return (int) stream->read(out, size);
101}
102
103void CheckFreeExtension(SavedImage* Image) {
104 if (Image->ExtensionBlocks) {
reed@google.combb896132013-02-01 19:05:48 +0000105#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 FreeExtension(Image);
reed@google.combb896132013-02-01 19:05:48 +0000107#else
108 GifFreeExtensions(&Image->ExtensionBlockCount, &Image->ExtensionBlocks);
109#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 }
111}
112
113// return NULL on failure
114static const ColorMapObject* find_colormap(const GifFileType* gif) {
115 const ColorMapObject* cmap = gif->Image.ColorMap;
116 if (NULL == cmap) {
117 cmap = gif->SColorMap;
118 }
djsollen@google.com57f49692011-02-23 20:46:31 +0000119
120 if (NULL == cmap) {
121 // no colormap found
122 return NULL;
123 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 // some sanity checks
reed@android.combc7d2fb2010-02-05 15:43:07 +0000125 if (cmap && ((unsigned)cmap->ColorCount > 256 ||
126 cmap->ColorCount != (1 << cmap->BitsPerPixel))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 cmap = NULL;
128 }
129 return cmap;
130}
131
132// return -1 if not found (i.e. we're completely opaque)
133static int find_transpIndex(const SavedImage& image, int colorCount) {
134 int transpIndex = -1;
135 for (int i = 0; i < image.ExtensionBlockCount; ++i) {
136 const ExtensionBlock* eb = image.ExtensionBlocks + i;
137 if (eb->Function == 0xF9 && eb->ByteCount == 4) {
138 if (eb->Bytes[0] & 1) {
139 transpIndex = (unsigned char)eb->Bytes[3];
140 // check for valid transpIndex
141 if (transpIndex >= colorCount) {
142 transpIndex = -1;
143 }
144 break;
145 }
146 }
147 }
148 return transpIndex;
149}
150
151static bool error_return(GifFileType* gif, const SkBitmap& bm,
152 const char msg[]) {
153#if 0
154 SkDebugf("libgif error <%s> bitmap [%d %d] pixels %p colortable %p\n",
155 msg, bm.width(), bm.height(), bm.getPixels(), bm.getColorTable());
156#endif
157 return false;
158}
159
reed@android.com3f1f06a2010-03-03 21:04:12 +0000160bool SkGIFImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* bm, Mode mode) {
reed@google.combb896132013-02-01 19:05:48 +0000161#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc);
reed@google.combb896132013-02-01 19:05:48 +0000163#else
164 GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc, NULL);
165#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000166 if (NULL == gif) {
167 return error_return(gif, *bm, "DGifOpen");
168 }
169
170 SkAutoTCallIProc<GifFileType, DGifCloseFile> acp(gif);
171
172 SavedImage temp_save;
173 temp_save.ExtensionBlocks=NULL;
174 temp_save.ExtensionBlockCount=0;
175 SkAutoTCallVProc<SavedImage, CheckFreeExtension> acp2(&temp_save);
176
177 int width, height;
178 GifRecordType recType;
179 GifByteType *extData;
reed@google.combb896132013-02-01 19:05:48 +0000180#if GIFLIB_MAJOR >= 5
181 int extFunction;
182#endif
reed@android.com9781ca52009-04-14 14:28:22 +0000183 int transpIndex = -1; // -1 means we don't have it (yet)
rmistry@google.comd6176b02012-08-23 18:14:13 +0000184
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185 do {
186 if (DGifGetRecordType(gif, &recType) == GIF_ERROR) {
187 return error_return(gif, *bm, "DGifGetRecordType");
188 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000189
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 switch (recType) {
191 case IMAGE_DESC_RECORD_TYPE: {
192 if (DGifGetImageDesc(gif) == GIF_ERROR) {
193 return error_return(gif, *bm, "IMAGE_DESC_RECORD_TYPE");
194 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000195
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196 if (gif->ImageCount < 1) { // sanity check
197 return error_return(gif, *bm, "ImageCount < 1");
198 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000199
reed@android.com8a1c16f2008-12-17 15:59:43 +0000200 width = gif->SWidth;
201 height = gif->SHeight;
202 if (width <= 0 || height <= 0 ||
203 !this->chooseFromOneChoice(SkBitmap::kIndex8_Config,
204 width, height)) {
205 return error_return(gif, *bm, "chooseFromOneChoice");
206 }
skia.committer@gmail.comc49cabf2013-03-15 07:05:19 +0000207
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000208 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
209 bm->setConfig(SkBitmap::kIndex8_Config, width, height);
210 return true;
211 }
212
213 // No Bitmap reuse supported for this format
214 if (!bm->isNull()) {
215 return false;
216 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000217
reed@android.com8a1c16f2008-12-17 15:59:43 +0000218 bm->setConfig(SkBitmap::kIndex8_Config, width, height);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219 SavedImage* image = &gif->SavedImages[gif->ImageCount-1];
220 const GifImageDesc& desc = image->ImageDesc;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000221
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222 // check for valid descriptor
223 if ( (desc.Top | desc.Left) < 0 ||
224 desc.Left + desc.Width > width ||
225 desc.Top + desc.Height > height) {
226 return error_return(gif, *bm, "TopLeft");
227 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000228
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229 // now we decode the colortable
230 int colorCount = 0;
231 {
232 const ColorMapObject* cmap = find_colormap(gif);
233 if (NULL == cmap) {
234 return error_return(gif, *bm, "null cmap");
235 }
236
237 colorCount = cmap->ColorCount;
238 SkColorTable* ctable = SkNEW_ARGS(SkColorTable, (colorCount));
239 SkPMColor* colorPtr = ctable->lockColors();
240 for (int index = 0; index < colorCount; index++)
241 colorPtr[index] = SkPackARGB32(0xFF,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000242 cmap->Colors[index].Red,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 cmap->Colors[index].Green,
244 cmap->Colors[index].Blue);
245
reed@android.com9781ca52009-04-14 14:28:22 +0000246 transpIndex = find_transpIndex(temp_save, colorCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247 if (transpIndex < 0)
248 ctable->setFlags(ctable->getFlags() | SkColorTable::kColorsAreOpaque_Flag);
249 else
250 colorPtr[transpIndex] = 0; // ram in a transparent SkPMColor
251 ctable->unlockColors(true);
252
253 SkAutoUnref aurts(ctable);
254 if (!this->allocPixelRef(bm, ctable)) {
255 return error_return(gif, *bm, "allocPixelRef");
256 }
257 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000258
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 SkAutoLockPixels alp(*bm);
260
261 // time to decode the scanlines
262 //
263 uint8_t* scanline = bm->getAddr8(0, 0);
264 const int rowBytes = bm->rowBytes();
265 const int innerWidth = desc.Width;
266 const int innerHeight = desc.Height;
267
268 // abort if either inner dimension is <= 0
269 if (innerWidth <= 0 || innerHeight <= 0) {
270 return error_return(gif, *bm, "non-pos inner width/height");
271 }
272
273 // are we only a subset of the total bounds?
274 if ((desc.Top | desc.Left) > 0 ||
275 innerWidth < width || innerHeight < height)
276 {
reed@android.com9781ca52009-04-14 14:28:22 +0000277 int fill;
278 if (transpIndex >= 0) {
279 fill = transpIndex;
280 } else {
281 fill = gif->SBackGroundColor;
282 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 // check for valid fill index/color
reed@android.com9781ca52009-04-14 14:28:22 +0000284 if (static_cast<unsigned>(fill) >=
285 static_cast<unsigned>(colorCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 fill = 0;
287 }
reed@android.com9781ca52009-04-14 14:28:22 +0000288 memset(scanline, fill, bm->getSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000289 // bump our starting address
290 scanline += desc.Top * rowBytes + desc.Left;
291 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000292
reed@android.com8a1c16f2008-12-17 15:59:43 +0000293 // now decode each scanline
294 if (gif->Image.Interlace)
295 {
296 GifInterlaceIter iter(innerHeight);
297 for (int y = 0; y < innerHeight; y++)
298 {
299 uint8_t* row = scanline + iter.currY() * rowBytes;
300 if (DGifGetLine(gif, row, innerWidth) == GIF_ERROR) {
301 return error_return(gif, *bm, "interlace DGifGetLine");
302 }
303 iter.next();
304 }
305 }
306 else
307 {
308 // easy, non-interlace case
309 for (int y = 0; y < innerHeight; y++) {
310 if (DGifGetLine(gif, scanline, innerWidth) == GIF_ERROR) {
311 return error_return(gif, *bm, "DGifGetLine");
312 }
313 scanline += rowBytes;
314 }
315 }
316 goto DONE;
317 } break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000318
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319 case EXTENSION_RECORD_TYPE:
reed@google.combb896132013-02-01 19:05:48 +0000320#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000321 if (DGifGetExtension(gif, &temp_save.Function,
322 &extData) == GIF_ERROR) {
reed@google.combb896132013-02-01 19:05:48 +0000323#else
324 if (DGifGetExtension(gif, &extFunction, &extData) == GIF_ERROR) {
325#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000326 return error_return(gif, *bm, "DGifGetExtension");
327 }
328
329 while (extData != NULL) {
330 /* Create an extension block with our data */
reed@google.combb896132013-02-01 19:05:48 +0000331#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 if (AddExtensionBlock(&temp_save, extData[0],
333 &extData[1]) == GIF_ERROR) {
reed@google.combb896132013-02-01 19:05:48 +0000334#else
335 if (GifAddExtensionBlock(&gif->ExtensionBlockCount,
336 &gif->ExtensionBlocks,
337 extFunction,
338 extData[0],
339 &extData[1]) == GIF_ERROR) {
340#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000341 return error_return(gif, *bm, "AddExtensionBlock");
342 }
343 if (DGifGetExtensionNext(gif, &extData) == GIF_ERROR) {
344 return error_return(gif, *bm, "DGifGetExtensionNext");
345 }
reed@google.combb896132013-02-01 19:05:48 +0000346#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347 temp_save.Function = 0;
reed@google.combb896132013-02-01 19:05:48 +0000348#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349 }
350 break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000351
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352 case TERMINATE_RECORD_TYPE:
353 break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000354
355 default: /* Should be trapped by DGifGetRecordType */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356 break;
357 }
358 } while (recType != TERMINATE_RECORD_TYPE);
359
360DONE:
361 return true;
362}
363
364///////////////////////////////////////////////////////////////////////////////
robertphillips@google.comec51cb82012-03-23 18:13:47 +0000365DEFINE_DECODER_CREATOR(GIFImageDecoder);
366///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367
reed@android.com00bf85a2009-01-22 13:04:56 +0000368#include "SkTRegistry.h"
369
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000370static SkImageDecoder* sk_libgif_dfactory(SkStream* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371 char buf[GIF_STAMP_LEN];
372 if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) {
373 if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 ||
374 memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
375 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) {
376 return SkNEW(SkGIFImageDecoder);
377 }
378 }
379 return NULL;
380}
381
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000382static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libgif_dfactory);