blob: d368eccd92d629537ae7b8cedf68b1dc29d81970 [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
reed@android.com8a1c16f2008-12-17 15:59:43 +000094static 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 }
skia.committer@gmail.comc49cabf2013-03-15 07:05:19 +0000204
scroggo@google.combc69ce92013-07-09 15:45:14 +0000205 bm->setConfig(SkBitmap::kIndex8_Config, width, height);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000206 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000207 return true;
208 }
209
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210 SavedImage* image = &gif->SavedImages[gif->ImageCount-1];
211 const GifImageDesc& desc = image->ImageDesc;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000212
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213 // check for valid descriptor
214 if ( (desc.Top | desc.Left) < 0 ||
215 desc.Left + desc.Width > width ||
216 desc.Top + desc.Height > height) {
217 return error_return(gif, *bm, "TopLeft");
218 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000219
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220 // now we decode the colortable
221 int colorCount = 0;
222 {
223 const ColorMapObject* cmap = find_colormap(gif);
224 if (NULL == cmap) {
225 return error_return(gif, *bm, "null cmap");
226 }
227
228 colorCount = cmap->ColorCount;
229 SkColorTable* ctable = SkNEW_ARGS(SkColorTable, (colorCount));
230 SkPMColor* colorPtr = ctable->lockColors();
231 for (int index = 0; index < colorCount; index++)
232 colorPtr[index] = SkPackARGB32(0xFF,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000233 cmap->Colors[index].Red,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 cmap->Colors[index].Green,
235 cmap->Colors[index].Blue);
236
reed@android.com9781ca52009-04-14 14:28:22 +0000237 transpIndex = find_transpIndex(temp_save, colorCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 if (transpIndex < 0)
239 ctable->setFlags(ctable->getFlags() | SkColorTable::kColorsAreOpaque_Flag);
240 else
241 colorPtr[transpIndex] = 0; // ram in a transparent SkPMColor
242 ctable->unlockColors(true);
243
244 SkAutoUnref aurts(ctable);
245 if (!this->allocPixelRef(bm, ctable)) {
246 return error_return(gif, *bm, "allocPixelRef");
247 }
248 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000249
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250 SkAutoLockPixels alp(*bm);
251
252 // time to decode the scanlines
253 //
254 uint8_t* scanline = bm->getAddr8(0, 0);
255 const int rowBytes = bm->rowBytes();
256 const int innerWidth = desc.Width;
257 const int innerHeight = desc.Height;
258
259 // abort if either inner dimension is <= 0
260 if (innerWidth <= 0 || innerHeight <= 0) {
261 return error_return(gif, *bm, "non-pos inner width/height");
262 }
263
264 // are we only a subset of the total bounds?
265 if ((desc.Top | desc.Left) > 0 ||
266 innerWidth < width || innerHeight < height)
267 {
reed@android.com9781ca52009-04-14 14:28:22 +0000268 int fill;
269 if (transpIndex >= 0) {
270 fill = transpIndex;
271 } else {
272 fill = gif->SBackGroundColor;
273 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000274 // check for valid fill index/color
reed@android.com9781ca52009-04-14 14:28:22 +0000275 if (static_cast<unsigned>(fill) >=
276 static_cast<unsigned>(colorCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 fill = 0;
278 }
reed@android.com9781ca52009-04-14 14:28:22 +0000279 memset(scanline, fill, bm->getSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280 // bump our starting address
281 scanline += desc.Top * rowBytes + desc.Left;
282 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000283
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 // now decode each scanline
285 if (gif->Image.Interlace)
286 {
287 GifInterlaceIter iter(innerHeight);
288 for (int y = 0; y < innerHeight; y++)
289 {
290 uint8_t* row = scanline + iter.currY() * rowBytes;
291 if (DGifGetLine(gif, row, innerWidth) == GIF_ERROR) {
292 return error_return(gif, *bm, "interlace DGifGetLine");
293 }
294 iter.next();
295 }
296 }
297 else
298 {
299 // easy, non-interlace case
300 for (int y = 0; y < innerHeight; y++) {
301 if (DGifGetLine(gif, scanline, innerWidth) == GIF_ERROR) {
302 return error_return(gif, *bm, "DGifGetLine");
303 }
304 scanline += rowBytes;
305 }
306 }
307 goto DONE;
308 } break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000309
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310 case EXTENSION_RECORD_TYPE:
reed@google.combb896132013-02-01 19:05:48 +0000311#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312 if (DGifGetExtension(gif, &temp_save.Function,
313 &extData) == GIF_ERROR) {
reed@google.combb896132013-02-01 19:05:48 +0000314#else
315 if (DGifGetExtension(gif, &extFunction, &extData) == GIF_ERROR) {
316#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317 return error_return(gif, *bm, "DGifGetExtension");
318 }
319
320 while (extData != NULL) {
321 /* Create an extension block with our data */
reed@google.combb896132013-02-01 19:05:48 +0000322#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323 if (AddExtensionBlock(&temp_save, extData[0],
324 &extData[1]) == GIF_ERROR) {
reed@google.combb896132013-02-01 19:05:48 +0000325#else
326 if (GifAddExtensionBlock(&gif->ExtensionBlockCount,
327 &gif->ExtensionBlocks,
328 extFunction,
329 extData[0],
330 &extData[1]) == GIF_ERROR) {
331#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332 return error_return(gif, *bm, "AddExtensionBlock");
333 }
334 if (DGifGetExtensionNext(gif, &extData) == GIF_ERROR) {
335 return error_return(gif, *bm, "DGifGetExtensionNext");
336 }
reed@google.combb896132013-02-01 19:05:48 +0000337#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338 temp_save.Function = 0;
reed@google.combb896132013-02-01 19:05:48 +0000339#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340 }
341 break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000342
reed@android.com8a1c16f2008-12-17 15:59:43 +0000343 case TERMINATE_RECORD_TYPE:
344 break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000345
346 default: /* Should be trapped by DGifGetRecordType */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347 break;
348 }
349 } while (recType != TERMINATE_RECORD_TYPE);
350
351DONE:
352 return true;
353}
354
355///////////////////////////////////////////////////////////////////////////////
robertphillips@google.comec51cb82012-03-23 18:13:47 +0000356DEFINE_DECODER_CREATOR(GIFImageDecoder);
357///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000358
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000359static bool is_gif(SkStream* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 char buf[GIF_STAMP_LEN];
361 if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) {
362 if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 ||
363 memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
364 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000365 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366 }
367 }
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000368 return false;
369}
370
371#include "SkTRegistry.h"
372
373static SkImageDecoder* sk_libgif_dfactory(SkStream* stream) {
374 if (is_gif(stream)) {
375 return SkNEW(SkGIFImageDecoder);
376 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000377 return NULL;
378}
379
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000380static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libgif_dfactory);
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000381
382static SkImageDecoder::Format get_format_gif(SkStream* stream) {
383 if (is_gif(stream)) {
384 return SkImageDecoder::kGIF_Format;
385 }
386 return SkImageDecoder::kUnknown_Format;
387}
388
389static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_gif);