blob: 7a451a056078018e4da754d7724d0132db151733 [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 }
24
25protected:
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 }
51
52 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 }
80
81private:
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) {
102 FreeExtension(Image);
103 }
104}
105
106// return NULL on failure
107static const ColorMapObject* find_colormap(const GifFileType* gif) {
108 const ColorMapObject* cmap = gif->Image.ColorMap;
109 if (NULL == cmap) {
110 cmap = gif->SColorMap;
111 }
djsollen@google.com57f49692011-02-23 20:46:31 +0000112
113 if (NULL == cmap) {
114 // no colormap found
115 return NULL;
116 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 // some sanity checks
reed@android.combc7d2fb2010-02-05 15:43:07 +0000118 if (cmap && ((unsigned)cmap->ColorCount > 256 ||
119 cmap->ColorCount != (1 << cmap->BitsPerPixel))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 cmap = NULL;
121 }
122 return cmap;
123}
124
125// return -1 if not found (i.e. we're completely opaque)
126static int find_transpIndex(const SavedImage& image, int colorCount) {
127 int transpIndex = -1;
128 for (int i = 0; i < image.ExtensionBlockCount; ++i) {
129 const ExtensionBlock* eb = image.ExtensionBlocks + i;
130 if (eb->Function == 0xF9 && eb->ByteCount == 4) {
131 if (eb->Bytes[0] & 1) {
132 transpIndex = (unsigned char)eb->Bytes[3];
133 // check for valid transpIndex
134 if (transpIndex >= colorCount) {
135 transpIndex = -1;
136 }
137 break;
138 }
139 }
140 }
141 return transpIndex;
142}
143
144static bool error_return(GifFileType* gif, const SkBitmap& bm,
145 const char msg[]) {
146#if 0
147 SkDebugf("libgif error <%s> bitmap [%d %d] pixels %p colortable %p\n",
148 msg, bm.width(), bm.height(), bm.getPixels(), bm.getColorTable());
149#endif
150 return false;
151}
152
reed@android.com3f1f06a2010-03-03 21:04:12 +0000153bool SkGIFImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* bm, Mode mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc);
155 if (NULL == gif) {
156 return error_return(gif, *bm, "DGifOpen");
157 }
158
159 SkAutoTCallIProc<GifFileType, DGifCloseFile> acp(gif);
160
161 SavedImage temp_save;
162 temp_save.ExtensionBlocks=NULL;
163 temp_save.ExtensionBlockCount=0;
164 SkAutoTCallVProc<SavedImage, CheckFreeExtension> acp2(&temp_save);
165
166 int width, height;
167 GifRecordType recType;
168 GifByteType *extData;
reed@android.com9781ca52009-04-14 14:28:22 +0000169 int transpIndex = -1; // -1 means we don't have it (yet)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170
171 do {
172 if (DGifGetRecordType(gif, &recType) == GIF_ERROR) {
173 return error_return(gif, *bm, "DGifGetRecordType");
174 }
175
176 switch (recType) {
177 case IMAGE_DESC_RECORD_TYPE: {
178 if (DGifGetImageDesc(gif) == GIF_ERROR) {
179 return error_return(gif, *bm, "IMAGE_DESC_RECORD_TYPE");
180 }
181
182 if (gif->ImageCount < 1) { // sanity check
183 return error_return(gif, *bm, "ImageCount < 1");
184 }
185
186 width = gif->SWidth;
187 height = gif->SHeight;
188 if (width <= 0 || height <= 0 ||
189 !this->chooseFromOneChoice(SkBitmap::kIndex8_Config,
190 width, height)) {
191 return error_return(gif, *bm, "chooseFromOneChoice");
192 }
193
194 bm->setConfig(SkBitmap::kIndex8_Config, width, height);
195 if (SkImageDecoder::kDecodeBounds_Mode == mode)
196 return true;
197
198 SavedImage* image = &gif->SavedImages[gif->ImageCount-1];
199 const GifImageDesc& desc = image->ImageDesc;
200
201 // check for valid descriptor
202 if ( (desc.Top | desc.Left) < 0 ||
203 desc.Left + desc.Width > width ||
204 desc.Top + desc.Height > height) {
205 return error_return(gif, *bm, "TopLeft");
206 }
207
208 // now we decode the colortable
209 int colorCount = 0;
210 {
211 const ColorMapObject* cmap = find_colormap(gif);
212 if (NULL == cmap) {
213 return error_return(gif, *bm, "null cmap");
214 }
215
216 colorCount = cmap->ColorCount;
217 SkColorTable* ctable = SkNEW_ARGS(SkColorTable, (colorCount));
218 SkPMColor* colorPtr = ctable->lockColors();
219 for (int index = 0; index < colorCount; index++)
220 colorPtr[index] = SkPackARGB32(0xFF,
221 cmap->Colors[index].Red,
222 cmap->Colors[index].Green,
223 cmap->Colors[index].Blue);
224
reed@android.com9781ca52009-04-14 14:28:22 +0000225 transpIndex = find_transpIndex(temp_save, colorCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000226 if (transpIndex < 0)
227 ctable->setFlags(ctable->getFlags() | SkColorTable::kColorsAreOpaque_Flag);
228 else
229 colorPtr[transpIndex] = 0; // ram in a transparent SkPMColor
230 ctable->unlockColors(true);
231
232 SkAutoUnref aurts(ctable);
233 if (!this->allocPixelRef(bm, ctable)) {
234 return error_return(gif, *bm, "allocPixelRef");
235 }
236 }
237
238 SkAutoLockPixels alp(*bm);
239
240 // time to decode the scanlines
241 //
242 uint8_t* scanline = bm->getAddr8(0, 0);
243 const int rowBytes = bm->rowBytes();
244 const int innerWidth = desc.Width;
245 const int innerHeight = desc.Height;
246
247 // abort if either inner dimension is <= 0
248 if (innerWidth <= 0 || innerHeight <= 0) {
249 return error_return(gif, *bm, "non-pos inner width/height");
250 }
251
252 // are we only a subset of the total bounds?
253 if ((desc.Top | desc.Left) > 0 ||
254 innerWidth < width || innerHeight < height)
255 {
reed@android.com9781ca52009-04-14 14:28:22 +0000256 int fill;
257 if (transpIndex >= 0) {
258 fill = transpIndex;
259 } else {
260 fill = gif->SBackGroundColor;
261 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262 // check for valid fill index/color
reed@android.com9781ca52009-04-14 14:28:22 +0000263 if (static_cast<unsigned>(fill) >=
264 static_cast<unsigned>(colorCount)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 fill = 0;
266 }
reed@android.com9781ca52009-04-14 14:28:22 +0000267 memset(scanline, fill, bm->getSize());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268 // bump our starting address
269 scanline += desc.Top * rowBytes + desc.Left;
270 }
271
272 // now decode each scanline
273 if (gif->Image.Interlace)
274 {
275 GifInterlaceIter iter(innerHeight);
276 for (int y = 0; y < innerHeight; y++)
277 {
278 uint8_t* row = scanline + iter.currY() * rowBytes;
279 if (DGifGetLine(gif, row, innerWidth) == GIF_ERROR) {
280 return error_return(gif, *bm, "interlace DGifGetLine");
281 }
282 iter.next();
283 }
284 }
285 else
286 {
287 // easy, non-interlace case
288 for (int y = 0; y < innerHeight; y++) {
289 if (DGifGetLine(gif, scanline, innerWidth) == GIF_ERROR) {
290 return error_return(gif, *bm, "DGifGetLine");
291 }
292 scanline += rowBytes;
293 }
294 }
295 goto DONE;
296 } break;
297
298 case EXTENSION_RECORD_TYPE:
299 if (DGifGetExtension(gif, &temp_save.Function,
300 &extData) == GIF_ERROR) {
301 return error_return(gif, *bm, "DGifGetExtension");
302 }
303
304 while (extData != NULL) {
305 /* Create an extension block with our data */
306 if (AddExtensionBlock(&temp_save, extData[0],
307 &extData[1]) == GIF_ERROR) {
308 return error_return(gif, *bm, "AddExtensionBlock");
309 }
310 if (DGifGetExtensionNext(gif, &extData) == GIF_ERROR) {
311 return error_return(gif, *bm, "DGifGetExtensionNext");
312 }
313 temp_save.Function = 0;
314 }
315 break;
316
317 case TERMINATE_RECORD_TYPE:
318 break;
319
320 default: /* Should be trapped by DGifGetRecordType */
321 break;
322 }
323 } while (recType != TERMINATE_RECORD_TYPE);
324
325DONE:
326 return true;
327}
328
329///////////////////////////////////////////////////////////////////////////////
330
reed@android.com00bf85a2009-01-22 13:04:56 +0000331#include "SkTRegistry.h"
332
333static SkImageDecoder* Factory(SkStream* stream) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334 char buf[GIF_STAMP_LEN];
335 if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) {
336 if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 ||
337 memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
338 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) {
339 return SkNEW(SkGIFImageDecoder);
340 }
341 }
342 return NULL;
343}
344
reed@android.com00bf85a2009-01-22 13:04:56 +0000345static SkTRegistry<SkImageDecoder*, SkStream*> gReg(Factory);