blob: f81c601093851a9ed77611737572167f4ee98211 [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:
21 virtual Format getFormat() const {
22 return kGIF_Format;
23 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000024
reed@android.com8a1c16f2008-12-17 15:59:43 +000025protected:
reed@android.com3f1f06a2010-03-03 21:04:12 +000026 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +000027};
28
29static const uint8_t gStartingIterlaceYValue[] = {
30 0, 4, 2, 1
31};
32static const uint8_t gDeltaIterlaceYValue[] = {
33 8, 8, 4, 2
34};
35
36/* Implement the GIF interlace algorithm in an iterator.
37 1) grab every 8th line beginning at 0
38 2) grab every 8th line beginning at 4
39 3) grab every 4th line beginning at 2
40 4) grab every 2nd line beginning at 1
41*/
42class GifInterlaceIter {
43public:
44 GifInterlaceIter(int height) : fHeight(height) {
45 fStartYPtr = gStartingIterlaceYValue;
46 fDeltaYPtr = gDeltaIterlaceYValue;
47
48 fCurrY = *fStartYPtr++;
49 fDeltaY = *fDeltaYPtr++;
50 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000051
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 int currY() const {
53 SkASSERT(fStartYPtr);
54 SkASSERT(fDeltaYPtr);
55 return fCurrY;
56 }
57
58 void next() {
59 SkASSERT(fStartYPtr);
60 SkASSERT(fDeltaYPtr);
61
62 int y = fCurrY + fDeltaY;
63 // We went from an if statement to a while loop so that we iterate
64 // through fStartYPtr until a valid row is found. This is so that images
65 // that are smaller than 5x5 will not trash memory.
66 while (y >= fHeight) {
67 if (gStartingIterlaceYValue +
68 SK_ARRAY_COUNT(gStartingIterlaceYValue) == fStartYPtr) {
69 // we done
70 SkDEBUGCODE(fStartYPtr = NULL;)
71 SkDEBUGCODE(fDeltaYPtr = NULL;)
72 y = 0;
73 } else {
74 y = *fStartYPtr++;
75 fDeltaY = *fDeltaYPtr++;
76 }
77 }
78 fCurrY = y;
79 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000080
reed@android.com8a1c16f2008-12-17 15:59:43 +000081private:
82 const int fHeight;
83 int fCurrY;
84 int fDeltaY;
85 const uint8_t* fStartYPtr;
86 const uint8_t* fDeltaYPtr;
87};
88
89///////////////////////////////////////////////////////////////////////////////
90
91//#define GIF_STAMP "GIF" /* First chars in file - GIF stamp. */
92//#define GIF_STAMP_LEN (sizeof(GIF_STAMP) - 1)
93
94static int DecodeCallBackProc(GifFileType* fileType, GifByteType* out,
95 int size) {
96 SkStream* stream = (SkStream*) fileType->UserData;
97 return (int) stream->read(out, size);
98}
99
100void CheckFreeExtension(SavedImage* Image) {
101 if (Image->ExtensionBlocks) {
reed@google.combb896132013-02-01 19:05:48 +0000102#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 FreeExtension(Image);
reed@google.combb896132013-02-01 19:05:48 +0000104#else
105 GifFreeExtensions(&Image->ExtensionBlockCount, &Image->ExtensionBlocks);
106#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 }
108}
109
110// return NULL on failure
111static const ColorMapObject* find_colormap(const GifFileType* gif) {
112 const ColorMapObject* cmap = gif->Image.ColorMap;
113 if (NULL == cmap) {
114 cmap = gif->SColorMap;
115 }
djsollen@google.com57f49692011-02-23 20:46:31 +0000116
117 if (NULL == cmap) {
118 // no colormap found
119 return NULL;
120 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 // some sanity checks
reed@android.combc7d2fb2010-02-05 15:43:07 +0000122 if (cmap && ((unsigned)cmap->ColorCount > 256 ||
123 cmap->ColorCount != (1 << cmap->BitsPerPixel))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 cmap = NULL;
125 }
126 return cmap;
127}
128
129// return -1 if not found (i.e. we're completely opaque)
130static int find_transpIndex(const SavedImage& image, int colorCount) {
131 int transpIndex = -1;
132 for (int i = 0; i < image.ExtensionBlockCount; ++i) {
133 const ExtensionBlock* eb = image.ExtensionBlocks + i;
134 if (eb->Function == 0xF9 && eb->ByteCount == 4) {
135 if (eb->Bytes[0] & 1) {
136 transpIndex = (unsigned char)eb->Bytes[3];
137 // check for valid transpIndex
138 if (transpIndex >= colorCount) {
139 transpIndex = -1;
140 }
141 break;
142 }
143 }
144 }
145 return transpIndex;
146}
147
148static bool error_return(GifFileType* gif, const SkBitmap& bm,
149 const char msg[]) {
150#if 0
151 SkDebugf("libgif error <%s> bitmap [%d %d] pixels %p colortable %p\n",
152 msg, bm.width(), bm.height(), bm.getPixels(), bm.getColorTable());
153#endif
154 return false;
155}
156
reed@android.com3f1f06a2010-03-03 21:04:12 +0000157bool SkGIFImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* bm, Mode mode) {
reed@google.combb896132013-02-01 19:05:48 +0000158#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000159 GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc);
reed@google.combb896132013-02-01 19:05:48 +0000160#else
161 GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc, NULL);
162#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 if (NULL == gif) {
164 return error_return(gif, *bm, "DGifOpen");
165 }
166
167 SkAutoTCallIProc<GifFileType, DGifCloseFile> acp(gif);
168
169 SavedImage temp_save;
170 temp_save.ExtensionBlocks=NULL;
171 temp_save.ExtensionBlockCount=0;
172 SkAutoTCallVProc<SavedImage, CheckFreeExtension> acp2(&temp_save);
173
174 int width, height;
175 GifRecordType recType;
176 GifByteType *extData;
reed@google.combb896132013-02-01 19:05:48 +0000177#if GIFLIB_MAJOR >= 5
178 int extFunction;
179#endif
reed@android.com9781ca52009-04-14 14:28:22 +0000180 int transpIndex = -1; // -1 means we don't have it (yet)
rmistry@google.comd6176b02012-08-23 18:14:13 +0000181
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 do {
183 if (DGifGetRecordType(gif, &recType) == GIF_ERROR) {
184 return error_return(gif, *bm, "DGifGetRecordType");
185 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000186
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 switch (recType) {
188 case IMAGE_DESC_RECORD_TYPE: {
189 if (DGifGetImageDesc(gif) == GIF_ERROR) {
190 return error_return(gif, *bm, "IMAGE_DESC_RECORD_TYPE");
191 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000192
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 if (gif->ImageCount < 1) { // sanity check
194 return error_return(gif, *bm, "ImageCount < 1");
195 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000196
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 width = gif->SWidth;
198 height = gif->SHeight;
199 if (width <= 0 || height <= 0 ||
200 !this->chooseFromOneChoice(SkBitmap::kIndex8_Config,
201 width, height)) {
202 return error_return(gif, *bm, "chooseFromOneChoice");
203 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000204
reed@android.com8a1c16f2008-12-17 15:59:43 +0000205 bm->setConfig(SkBitmap::kIndex8_Config, width, height);
206 if (SkImageDecoder::kDecodeBounds_Mode == mode)
207 return true;
208
209 SavedImage* image = &gif->SavedImages[gif->ImageCount-1];
210 const GifImageDesc& desc = image->ImageDesc;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000211
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212 // check for valid descriptor
213 if ( (desc.Top | desc.Left) < 0 ||
214 desc.Left + desc.Width > width ||
215 desc.Top + desc.Height > height) {
216 return error_return(gif, *bm, "TopLeft");
217 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000218
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219 // now we decode the colortable
220 int colorCount = 0;
221 {
222 const ColorMapObject* cmap = find_colormap(gif);
223 if (NULL == cmap) {
224 return error_return(gif, *bm, "null cmap");
225 }
226
227 colorCount = cmap->ColorCount;
228 SkColorTable* ctable = SkNEW_ARGS(SkColorTable, (colorCount));
229 SkPMColor* colorPtr = ctable->lockColors();
230 for (int index = 0; index < colorCount; index++)
231 colorPtr[index] = SkPackARGB32(0xFF,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000232 cmap->Colors[index].Red,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000233 cmap->Colors[index].Green,
234 cmap->Colors[index].Blue);
235
reed@android.com9781ca52009-04-14 14:28:22 +0000236 transpIndex = find_transpIndex(temp_save, colorCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 if (transpIndex < 0)
238 ctable->setFlags(ctable->getFlags() | SkColorTable::kColorsAreOpaque_Flag);
239 else
240 colorPtr[transpIndex] = 0; // ram in a transparent SkPMColor
241 ctable->unlockColors(true);
242
243 SkAutoUnref aurts(ctable);
244 if (!this->allocPixelRef(bm, ctable)) {
245 return error_return(gif, *bm, "allocPixelRef");
246 }
247 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000248
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249 SkAutoLockPixels alp(*bm);
250
251 // time to decode the scanlines
252 //
253 uint8_t* scanline = bm->getAddr8(0, 0);
254 const int rowBytes = bm->rowBytes();
255 const int innerWidth = desc.Width;
256 const int innerHeight = desc.Height;
257
258 // abort if either inner dimension is <= 0
259 if (innerWidth <= 0 || innerHeight <= 0) {
260 return error_return(gif, *bm, "non-pos inner width/height");
261 }
262
263 // are we only a subset of the total bounds?
264 if ((desc.Top | desc.Left) > 0 ||
265 innerWidth < width || innerHeight < height)
266 {
reed@android.com9781ca52009-04-14 14:28:22 +0000267 int fill;
268 if (transpIndex >= 0) {
269 fill = transpIndex;
270 } else {
271 fill = gif->SBackGroundColor;
272 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 // check for valid fill index/color
reed@android.com9781ca52009-04-14 14:28:22 +0000274 if (static_cast<unsigned>(fill) >=
275 static_cast<unsigned>(colorCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276 fill = 0;
277 }
reed@android.com9781ca52009-04-14 14:28:22 +0000278 memset(scanline, fill, bm->getSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 // bump our starting address
280 scanline += desc.Top * rowBytes + desc.Left;
281 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000282
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 // now decode each scanline
284 if (gif->Image.Interlace)
285 {
286 GifInterlaceIter iter(innerHeight);
287 for (int y = 0; y < innerHeight; y++)
288 {
289 uint8_t* row = scanline + iter.currY() * rowBytes;
290 if (DGifGetLine(gif, row, innerWidth) == GIF_ERROR) {
291 return error_return(gif, *bm, "interlace DGifGetLine");
292 }
293 iter.next();
294 }
295 }
296 else
297 {
298 // easy, non-interlace case
299 for (int y = 0; y < innerHeight; y++) {
300 if (DGifGetLine(gif, scanline, innerWidth) == GIF_ERROR) {
301 return error_return(gif, *bm, "DGifGetLine");
302 }
303 scanline += rowBytes;
304 }
305 }
306 goto DONE;
307 } break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000308
reed@android.com8a1c16f2008-12-17 15:59:43 +0000309 case EXTENSION_RECORD_TYPE:
reed@google.combb896132013-02-01 19:05:48 +0000310#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000311 if (DGifGetExtension(gif, &temp_save.Function,
312 &extData) == GIF_ERROR) {
reed@google.combb896132013-02-01 19:05:48 +0000313#else
314 if (DGifGetExtension(gif, &extFunction, &extData) == GIF_ERROR) {
315#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316 return error_return(gif, *bm, "DGifGetExtension");
317 }
318
319 while (extData != NULL) {
320 /* Create an extension block with our data */
reed@google.combb896132013-02-01 19:05:48 +0000321#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 if (AddExtensionBlock(&temp_save, extData[0],
323 &extData[1]) == GIF_ERROR) {
reed@google.combb896132013-02-01 19:05:48 +0000324#else
325 if (GifAddExtensionBlock(&gif->ExtensionBlockCount,
326 &gif->ExtensionBlocks,
327 extFunction,
328 extData[0],
329 &extData[1]) == GIF_ERROR) {
330#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000331 return error_return(gif, *bm, "AddExtensionBlock");
332 }
333 if (DGifGetExtensionNext(gif, &extData) == GIF_ERROR) {
334 return error_return(gif, *bm, "DGifGetExtensionNext");
335 }
reed@google.combb896132013-02-01 19:05:48 +0000336#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337 temp_save.Function = 0;
reed@google.combb896132013-02-01 19:05:48 +0000338#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339 }
340 break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000341
reed@android.com8a1c16f2008-12-17 15:59:43 +0000342 case TERMINATE_RECORD_TYPE:
343 break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000344
345 default: /* Should be trapped by DGifGetRecordType */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 break;
347 }
348 } while (recType != TERMINATE_RECORD_TYPE);
349
350DONE:
351 return true;
352}
353
354///////////////////////////////////////////////////////////////////////////////
robertphillips@google.comec51cb82012-03-23 18:13:47 +0000355DEFINE_DECODER_CREATOR(GIFImageDecoder);
356///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357
reed@android.com00bf85a2009-01-22 13:04:56 +0000358#include "SkTRegistry.h"
359
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000360static SkImageDecoder* sk_libgif_dfactory(SkStream* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000361 char buf[GIF_STAMP_LEN];
362 if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) {
363 if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 ||
364 memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
365 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) {
366 return SkNEW(SkGIFImageDecoder);
367 }
368 }
369 return NULL;
370}
371
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000372static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libgif_dfactory);