blob: f6c54c2dc0fa8016fe1444ff48833ce454ca9da6 [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
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000205 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
206 bm->setConfig(SkBitmap::kIndex8_Config, width, height);
207 return true;
208 }
209
210 // No Bitmap reuse supported for this format
211 if (!bm->isNull()) {
212 return false;
213 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000214
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 bm->setConfig(SkBitmap::kIndex8_Config, width, height);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 SavedImage* image = &gif->SavedImages[gif->ImageCount-1];
217 const GifImageDesc& desc = image->ImageDesc;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000218
reed@android.com8a1c16f2008-12-17 15:59:43 +0000219 // check for valid descriptor
220 if ( (desc.Top | desc.Left) < 0 ||
221 desc.Left + desc.Width > width ||
222 desc.Top + desc.Height > height) {
223 return error_return(gif, *bm, "TopLeft");
224 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000225
reed@android.com8a1c16f2008-12-17 15:59:43 +0000226 // now we decode the colortable
227 int colorCount = 0;
228 {
229 const ColorMapObject* cmap = find_colormap(gif);
230 if (NULL == cmap) {
231 return error_return(gif, *bm, "null cmap");
232 }
233
234 colorCount = cmap->ColorCount;
235 SkColorTable* ctable = SkNEW_ARGS(SkColorTable, (colorCount));
236 SkPMColor* colorPtr = ctable->lockColors();
237 for (int index = 0; index < colorCount; index++)
238 colorPtr[index] = SkPackARGB32(0xFF,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000239 cmap->Colors[index].Red,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240 cmap->Colors[index].Green,
241 cmap->Colors[index].Blue);
242
reed@android.com9781ca52009-04-14 14:28:22 +0000243 transpIndex = find_transpIndex(temp_save, colorCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244 if (transpIndex < 0)
245 ctable->setFlags(ctable->getFlags() | SkColorTable::kColorsAreOpaque_Flag);
246 else
247 colorPtr[transpIndex] = 0; // ram in a transparent SkPMColor
248 ctable->unlockColors(true);
249
250 SkAutoUnref aurts(ctable);
251 if (!this->allocPixelRef(bm, ctable)) {
252 return error_return(gif, *bm, "allocPixelRef");
253 }
254 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000255
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 SkAutoLockPixels alp(*bm);
257
258 // time to decode the scanlines
259 //
260 uint8_t* scanline = bm->getAddr8(0, 0);
261 const int rowBytes = bm->rowBytes();
262 const int innerWidth = desc.Width;
263 const int innerHeight = desc.Height;
264
265 // abort if either inner dimension is <= 0
266 if (innerWidth <= 0 || innerHeight <= 0) {
267 return error_return(gif, *bm, "non-pos inner width/height");
268 }
269
270 // are we only a subset of the total bounds?
271 if ((desc.Top | desc.Left) > 0 ||
272 innerWidth < width || innerHeight < height)
273 {
reed@android.com9781ca52009-04-14 14:28:22 +0000274 int fill;
275 if (transpIndex >= 0) {
276 fill = transpIndex;
277 } else {
278 fill = gif->SBackGroundColor;
279 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280 // check for valid fill index/color
reed@android.com9781ca52009-04-14 14:28:22 +0000281 if (static_cast<unsigned>(fill) >=
282 static_cast<unsigned>(colorCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000283 fill = 0;
284 }
reed@android.com9781ca52009-04-14 14:28:22 +0000285 memset(scanline, fill, bm->getSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 // bump our starting address
287 scanline += desc.Top * rowBytes + desc.Left;
288 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000289
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290 // now decode each scanline
291 if (gif->Image.Interlace)
292 {
293 GifInterlaceIter iter(innerHeight);
294 for (int y = 0; y < innerHeight; y++)
295 {
296 uint8_t* row = scanline + iter.currY() * rowBytes;
297 if (DGifGetLine(gif, row, innerWidth) == GIF_ERROR) {
298 return error_return(gif, *bm, "interlace DGifGetLine");
299 }
300 iter.next();
301 }
302 }
303 else
304 {
305 // easy, non-interlace case
306 for (int y = 0; y < innerHeight; y++) {
307 if (DGifGetLine(gif, scanline, innerWidth) == GIF_ERROR) {
308 return error_return(gif, *bm, "DGifGetLine");
309 }
310 scanline += rowBytes;
311 }
312 }
313 goto DONE;
314 } break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000315
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316 case EXTENSION_RECORD_TYPE:
reed@google.combb896132013-02-01 19:05:48 +0000317#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000318 if (DGifGetExtension(gif, &temp_save.Function,
319 &extData) == GIF_ERROR) {
reed@google.combb896132013-02-01 19:05:48 +0000320#else
321 if (DGifGetExtension(gif, &extFunction, &extData) == GIF_ERROR) {
322#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323 return error_return(gif, *bm, "DGifGetExtension");
324 }
325
326 while (extData != NULL) {
327 /* Create an extension block with our data */
reed@google.combb896132013-02-01 19:05:48 +0000328#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000329 if (AddExtensionBlock(&temp_save, extData[0],
330 &extData[1]) == GIF_ERROR) {
reed@google.combb896132013-02-01 19:05:48 +0000331#else
332 if (GifAddExtensionBlock(&gif->ExtensionBlockCount,
333 &gif->ExtensionBlocks,
334 extFunction,
335 extData[0],
336 &extData[1]) == GIF_ERROR) {
337#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000338 return error_return(gif, *bm, "AddExtensionBlock");
339 }
340 if (DGifGetExtensionNext(gif, &extData) == GIF_ERROR) {
341 return error_return(gif, *bm, "DGifGetExtensionNext");
342 }
reed@google.combb896132013-02-01 19:05:48 +0000343#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344 temp_save.Function = 0;
reed@google.combb896132013-02-01 19:05:48 +0000345#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 }
347 break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000348
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349 case TERMINATE_RECORD_TYPE:
350 break;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000351
352 default: /* Should be trapped by DGifGetRecordType */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 break;
354 }
355 } while (recType != TERMINATE_RECORD_TYPE);
356
357DONE:
358 return true;
359}
360
361///////////////////////////////////////////////////////////////////////////////
robertphillips@google.comec51cb82012-03-23 18:13:47 +0000362DEFINE_DECODER_CREATOR(GIFImageDecoder);
363///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000365static bool is_gif(SkStream* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366 char buf[GIF_STAMP_LEN];
367 if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) {
368 if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 ||
369 memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
370 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000371 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000372 }
373 }
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000374 return false;
375}
376
377#include "SkTRegistry.h"
378
379static SkImageDecoder* sk_libgif_dfactory(SkStream* stream) {
380 if (is_gif(stream)) {
381 return SkNEW(SkGIFImageDecoder);
382 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000383 return NULL;
384}
385
robertphillips@google.com8570b5c2012-03-20 17:40:58 +0000386static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libgif_dfactory);
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000387
388static SkImageDecoder::Format get_format_gif(SkStream* stream) {
389 if (is_gif(stream)) {
390 return SkImageDecoder::kGIF_Format;
391 }
392 return SkImageDecoder::kUnknown_Format;
393}
394
395static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_gif);