epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 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 | |
| 19 | class SkGIFImageDecoder : public SkImageDecoder { |
| 20 | public: |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 21 | virtual Format getFormat() const SK_OVERRIDE { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | return kGIF_Format; |
| 23 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 24 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | protected: |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 26 | virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode mode) SK_OVERRIDE; |
| 27 | |
| 28 | private: |
| 29 | typedef SkImageDecoder INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | static const uint8_t gStartingIterlaceYValue[] = { |
| 33 | 0, 4, 2, 1 |
| 34 | }; |
| 35 | static 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 | */ |
| 45 | class GifInterlaceIter { |
| 46 | public: |
| 47 | GifInterlaceIter(int height) : fHeight(height) { |
| 48 | fStartYPtr = gStartingIterlaceYValue; |
| 49 | fDeltaYPtr = gDeltaIterlaceYValue; |
| 50 | |
| 51 | fCurrY = *fStartYPtr++; |
| 52 | fDeltaY = *fDeltaYPtr++; |
| 53 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 54 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 83 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | private: |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | static int DecodeCallBackProc(GifFileType* fileType, GifByteType* out, |
| 95 | int size) { |
| 96 | SkStream* stream = (SkStream*) fileType->UserData; |
| 97 | return (int) stream->read(out, size); |
| 98 | } |
| 99 | |
| 100 | void CheckFreeExtension(SavedImage* Image) { |
| 101 | if (Image->ExtensionBlocks) { |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 102 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | FreeExtension(Image); |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 104 | #else |
| 105 | GifFreeExtensions(&Image->ExtensionBlockCount, &Image->ExtensionBlocks); |
| 106 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | // return NULL on failure |
| 111 | static 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.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 116 | |
| 117 | if (NULL == cmap) { |
| 118 | // no colormap found |
| 119 | return NULL; |
| 120 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | // some sanity checks |
reed@android.com | bc7d2fb | 2010-02-05 15:43:07 +0000 | [diff] [blame] | 122 | if (cmap && ((unsigned)cmap->ColorCount > 256 || |
| 123 | cmap->ColorCount != (1 << cmap->BitsPerPixel))) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | cmap = NULL; |
| 125 | } |
| 126 | return cmap; |
| 127 | } |
| 128 | |
| 129 | // return -1 if not found (i.e. we're completely opaque) |
| 130 | static 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 | |
| 148 | static 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.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 157 | bool SkGIFImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* bm, Mode mode) { |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 158 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc); |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 160 | #else |
| 161 | GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc, NULL); |
| 162 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 163 | 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.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 177 | #if GIFLIB_MAJOR >= 5 |
| 178 | int extFunction; |
| 179 | #endif |
reed@android.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame] | 180 | int transpIndex = -1; // -1 means we don't have it (yet) |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 181 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | do { |
| 183 | if (DGifGetRecordType(gif, &recType) == GIF_ERROR) { |
| 184 | return error_return(gif, *bm, "DGifGetRecordType"); |
| 185 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 186 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 192 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | if (gif->ImageCount < 1) { // sanity check |
| 194 | return error_return(gif, *bm, "ImageCount < 1"); |
| 195 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 196 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | 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.com | c49cabf | 2013-03-15 07:05:19 +0000 | [diff] [blame] | 204 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 205 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 214 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 215 | bm->setConfig(SkBitmap::kIndex8_Config, width, height); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | SavedImage* image = &gif->SavedImages[gif->ImageCount-1]; |
| 217 | const GifImageDesc& desc = image->ImageDesc; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 218 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | // 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 225 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | // 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 239 | cmap->Colors[index].Red, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 240 | cmap->Colors[index].Green, |
| 241 | cmap->Colors[index].Blue); |
| 242 | |
reed@android.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame] | 243 | transpIndex = find_transpIndex(temp_save, colorCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 244 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 255 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 256 | 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.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame] | 274 | int fill; |
| 275 | if (transpIndex >= 0) { |
| 276 | fill = transpIndex; |
| 277 | } else { |
| 278 | fill = gif->SBackGroundColor; |
| 279 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 280 | // check for valid fill index/color |
reed@android.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame] | 281 | if (static_cast<unsigned>(fill) >= |
| 282 | static_cast<unsigned>(colorCount)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 283 | fill = 0; |
| 284 | } |
reed@android.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame] | 285 | memset(scanline, fill, bm->getSize()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 286 | // bump our starting address |
| 287 | scanline += desc.Top * rowBytes + desc.Left; |
| 288 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 289 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 290 | // 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 315 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 316 | case EXTENSION_RECORD_TYPE: |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 317 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 318 | if (DGifGetExtension(gif, &temp_save.Function, |
| 319 | &extData) == GIF_ERROR) { |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 320 | #else |
| 321 | if (DGifGetExtension(gif, &extFunction, &extData) == GIF_ERROR) { |
| 322 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 323 | return error_return(gif, *bm, "DGifGetExtension"); |
| 324 | } |
| 325 | |
| 326 | while (extData != NULL) { |
| 327 | /* Create an extension block with our data */ |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 328 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 329 | if (AddExtensionBlock(&temp_save, extData[0], |
| 330 | &extData[1]) == GIF_ERROR) { |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 331 | #else |
| 332 | if (GifAddExtensionBlock(&gif->ExtensionBlockCount, |
| 333 | &gif->ExtensionBlocks, |
| 334 | extFunction, |
| 335 | extData[0], |
| 336 | &extData[1]) == GIF_ERROR) { |
| 337 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 338 | return error_return(gif, *bm, "AddExtensionBlock"); |
| 339 | } |
| 340 | if (DGifGetExtensionNext(gif, &extData) == GIF_ERROR) { |
| 341 | return error_return(gif, *bm, "DGifGetExtensionNext"); |
| 342 | } |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 343 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 344 | temp_save.Function = 0; |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 345 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 346 | } |
| 347 | break; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 348 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 349 | case TERMINATE_RECORD_TYPE: |
| 350 | break; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 351 | |
| 352 | default: /* Should be trapped by DGifGetRecordType */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 353 | break; |
| 354 | } |
| 355 | } while (recType != TERMINATE_RECORD_TYPE); |
| 356 | |
| 357 | DONE: |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | ec51cb8 | 2012-03-23 18:13:47 +0000 | [diff] [blame] | 362 | DEFINE_DECODER_CREATOR(GIFImageDecoder); |
| 363 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 364 | |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame^] | 365 | static bool is_gif(SkStream* stream) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 366 | 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.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame^] | 371 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame^] | 374 | return false; |
| 375 | } |
| 376 | |
| 377 | #include "SkTRegistry.h" |
| 378 | |
| 379 | static SkImageDecoder* sk_libgif_dfactory(SkStream* stream) { |
| 380 | if (is_gif(stream)) { |
| 381 | return SkNEW(SkGIFImageDecoder); |
| 382 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 383 | return NULL; |
| 384 | } |
| 385 | |
robertphillips@google.com | 8570b5c | 2012-03-20 17:40:58 +0000 | [diff] [blame] | 386 | static SkTRegistry<SkImageDecoder*, SkStream*> gReg(sk_libgif_dfactory); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame^] | 387 | |
| 388 | static 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 | |
| 395 | static SkTRegistry<SkImageDecoder::Format, SkStream*> gFormatReg(get_format_gif); |