epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkColor.h" |
| 10 | #include "SkColorPriv.h" |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 11 | #include "SkColorTable.h" |
| 12 | #include "SkImageDecoder.h" |
| 13 | #include "SkScaledBitmapSampler.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkStream.h" |
| 15 | #include "SkTemplates.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 157 | /** |
| 158 | * Skip rows in the source gif image. |
| 159 | * @param gif Source image. |
| 160 | * @param dst Scratch output needed by gif library call. Must be >= width bytes. |
| 161 | * @param width Bytes per row in the source image. |
| 162 | * @param rowsToSkip Number of rows to skip. |
| 163 | * @return True on success, false on GIF_ERROR. |
| 164 | */ |
| 165 | static bool skip_src_rows(GifFileType* gif, uint8_t* dst, int width, int rowsToSkip) { |
| 166 | for (int i = 0; i < rowsToSkip; i++) { |
| 167 | if (DGifGetLine(gif, dst, width) == GIF_ERROR) { |
| 168 | return false; |
| 169 | } |
| 170 | } |
| 171 | return true; |
| 172 | } |
| 173 | |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 174 | bool SkGIFImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* bm, Mode mode) { |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 175 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc); |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 177 | #else |
| 178 | GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc, NULL); |
| 179 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 180 | if (NULL == gif) { |
| 181 | return error_return(gif, *bm, "DGifOpen"); |
| 182 | } |
| 183 | |
| 184 | SkAutoTCallIProc<GifFileType, DGifCloseFile> acp(gif); |
| 185 | |
| 186 | SavedImage temp_save; |
| 187 | temp_save.ExtensionBlocks=NULL; |
| 188 | temp_save.ExtensionBlockCount=0; |
| 189 | SkAutoTCallVProc<SavedImage, CheckFreeExtension> acp2(&temp_save); |
| 190 | |
| 191 | int width, height; |
| 192 | GifRecordType recType; |
| 193 | GifByteType *extData; |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 194 | #if GIFLIB_MAJOR >= 5 |
| 195 | int extFunction; |
| 196 | #endif |
reed@android.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame] | 197 | int transpIndex = -1; // -1 means we don't have it (yet) |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 198 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 199 | do { |
| 200 | if (DGifGetRecordType(gif, &recType) == GIF_ERROR) { |
| 201 | return error_return(gif, *bm, "DGifGetRecordType"); |
| 202 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 203 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | switch (recType) { |
| 205 | case IMAGE_DESC_RECORD_TYPE: { |
| 206 | if (DGifGetImageDesc(gif) == GIF_ERROR) { |
| 207 | return error_return(gif, *bm, "IMAGE_DESC_RECORD_TYPE"); |
| 208 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 209 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 210 | if (gif->ImageCount < 1) { // sanity check |
| 211 | return error_return(gif, *bm, "ImageCount < 1"); |
| 212 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 213 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | width = gif->SWidth; |
| 215 | height = gif->SHeight; |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 216 | if (width <= 0 || height <= 0) { |
| 217 | return error_return(gif, *bm, "invalid dimensions"); |
| 218 | } |
| 219 | |
| 220 | // FIXME: We could give the caller a choice of images or configs. |
| 221 | if (!this->chooseFromOneChoice(SkBitmap::kIndex8_Config, width, height)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 222 | return error_return(gif, *bm, "chooseFromOneChoice"); |
| 223 | } |
skia.committer@gmail.com | c49cabf | 2013-03-15 07:05:19 +0000 | [diff] [blame] | 224 | |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 225 | SkScaledBitmapSampler sampler(width, height, this->getSampleSize()); |
| 226 | |
| 227 | bm->setConfig(SkBitmap::kIndex8_Config, sampler.scaledWidth(), |
| 228 | sampler.scaledHeight()); |
| 229 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 230 | if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 231 | return true; |
| 232 | } |
| 233 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | SavedImage* image = &gif->SavedImages[gif->ImageCount-1]; |
| 235 | const GifImageDesc& desc = image->ImageDesc; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 236 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | // check for valid descriptor |
| 238 | if ( (desc.Top | desc.Left) < 0 || |
| 239 | desc.Left + desc.Width > width || |
| 240 | desc.Top + desc.Height > height) { |
| 241 | return error_return(gif, *bm, "TopLeft"); |
| 242 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 243 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 244 | // now we decode the colortable |
| 245 | int colorCount = 0; |
| 246 | { |
| 247 | const ColorMapObject* cmap = find_colormap(gif); |
| 248 | if (NULL == cmap) { |
| 249 | return error_return(gif, *bm, "null cmap"); |
| 250 | } |
reed@google.com | 48d9ff5 | 2013-10-09 16:49:45 +0000 | [diff] [blame] | 251 | colorCount = cmap->ColorCount; |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame^] | 252 | if (colorCount > 256) { |
| 253 | colorCount = 256; // our kIndex8 can't support more |
| 254 | } |
| 255 | |
| 256 | SkPMColor colorPtr[256]; // storage for worst-case |
| 257 | SkAlphaType alphaType = kOpaque_SkAlphaType; |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 258 | for (int index = 0; index < colorCount; index++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | colorPtr[index] = SkPackARGB32(0xFF, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 260 | cmap->Colors[index].Red, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 261 | cmap->Colors[index].Green, |
| 262 | cmap->Colors[index].Blue); |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 263 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 264 | |
reed@android.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame] | 265 | transpIndex = find_transpIndex(temp_save, colorCount); |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 266 | bool reallyHasAlpha = transpIndex >= 0; |
| 267 | if (reallyHasAlpha) { |
| 268 | colorPtr[transpIndex] = SK_ColorTRANSPARENT; // ram in a transparent SkPMColor |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame^] | 269 | alphaType = kPremul_SkAlphaType; |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 270 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 271 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame^] | 272 | SkAutoTUnref<SkColorTable> ctable(SkNEW_ARGS(SkColorTable, |
| 273 | (colorPtr, colorCount, |
| 274 | alphaType))); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 275 | if (!this->allocPixelRef(bm, ctable)) { |
| 276 | return error_return(gif, *bm, "allocPixelRef"); |
| 277 | } |
| 278 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 279 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 280 | const int innerWidth = desc.Width; |
| 281 | const int innerHeight = desc.Height; |
| 282 | |
| 283 | // abort if either inner dimension is <= 0 |
| 284 | if (innerWidth <= 0 || innerHeight <= 0) { |
| 285 | return error_return(gif, *bm, "non-pos inner width/height"); |
| 286 | } |
| 287 | |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 288 | SkAutoLockPixels alp(*bm); |
| 289 | |
| 290 | SkAutoMalloc storage(innerWidth); |
| 291 | uint8_t* scanline = (uint8_t*) storage.get(); |
| 292 | |
| 293 | // GIF has an option to store the scanlines of an image, plus a larger background, |
| 294 | // filled by a fill color. In this case, we will use a subset of the larger bitmap |
| 295 | // for sampling. |
| 296 | SkBitmap subset; |
| 297 | SkBitmap* workingBitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 298 | // are we only a subset of the total bounds? |
| 299 | if ((desc.Top | desc.Left) > 0 || |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 300 | innerWidth < width || innerHeight < height) { |
reed@android.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame] | 301 | int fill; |
| 302 | if (transpIndex >= 0) { |
| 303 | fill = transpIndex; |
| 304 | } else { |
| 305 | fill = gif->SBackGroundColor; |
| 306 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 307 | // check for valid fill index/color |
reed@android.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame] | 308 | if (static_cast<unsigned>(fill) >= |
| 309 | static_cast<unsigned>(colorCount)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 310 | fill = 0; |
| 311 | } |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 312 | // Fill the background. |
| 313 | memset(bm->getPixels(), fill, bm->getSize()); |
| 314 | |
| 315 | // Create a subset of the bitmap. |
| 316 | SkIRect subsetRect(SkIRect::MakeXYWH(desc.Left / sampler.srcDX(), |
| 317 | desc.Top / sampler.srcDY(), |
| 318 | innerWidth / sampler.srcDX(), |
| 319 | innerHeight / sampler.srcDY())); |
| 320 | if (!bm->extractSubset(&subset, subsetRect)) { |
| 321 | return error_return(gif, *bm, "Extract failed."); |
| 322 | } |
| 323 | // Update the sampler. We'll now be only sampling into the subset. |
| 324 | sampler = SkScaledBitmapSampler(innerWidth, innerHeight, this->getSampleSize()); |
| 325 | workingBitmap = ⊂ |
| 326 | } else { |
| 327 | workingBitmap = bm; |
| 328 | } |
| 329 | |
| 330 | // bm is already locked, but if we had to take a subset, it must be locked also, |
| 331 | // so that getPixels() will point to its pixels. |
| 332 | SkAutoLockPixels alpWorking(*workingBitmap); |
| 333 | |
| 334 | if (!sampler.begin(workingBitmap, SkScaledBitmapSampler::kIndex, *this)) { |
| 335 | return error_return(gif, *bm, "Sampler failed to begin."); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 336 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 337 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 338 | // now decode each scanline |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 339 | if (gif->Image.Interlace) { |
| 340 | // Iterate over the height of the source data. The sampler will |
| 341 | // take care of skipping unneeded rows. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 342 | GifInterlaceIter iter(innerHeight); |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 343 | for (int y = 0; y < innerHeight; y++){ |
| 344 | if (DGifGetLine(gif, scanline, innerWidth) == GIF_ERROR) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 345 | return error_return(gif, *bm, "interlace DGifGetLine"); |
| 346 | } |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 347 | sampler.sampleInterlaced(scanline, iter.currY()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 348 | iter.next(); |
| 349 | } |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 350 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 351 | // easy, non-interlace case |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 352 | const int outHeight = workingBitmap->height(); |
| 353 | skip_src_rows(gif, scanline, innerWidth, sampler.srcY0()); |
| 354 | for (int y = 0; y < outHeight; y++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 355 | if (DGifGetLine(gif, scanline, innerWidth) == GIF_ERROR) { |
| 356 | return error_return(gif, *bm, "DGifGetLine"); |
| 357 | } |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 358 | // scanline now contains the raw data. Sample it. |
| 359 | sampler.next(scanline); |
| 360 | if (y < outHeight - 1) { |
| 361 | skip_src_rows(gif, scanline, innerWidth, sampler.srcDY() - 1); |
| 362 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 363 | } |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 364 | // skip the rest of the rows (if any) |
| 365 | int read = (outHeight - 1) * sampler.srcDY() + sampler.srcY0() + 1; |
| 366 | SkASSERT(read <= innerHeight); |
| 367 | skip_src_rows(gif, scanline, innerWidth, innerHeight - read); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 368 | } |
| 369 | goto DONE; |
| 370 | } break; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 371 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 372 | case EXTENSION_RECORD_TYPE: |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 373 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 374 | if (DGifGetExtension(gif, &temp_save.Function, |
| 375 | &extData) == GIF_ERROR) { |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 376 | #else |
| 377 | if (DGifGetExtension(gif, &extFunction, &extData) == GIF_ERROR) { |
| 378 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 379 | return error_return(gif, *bm, "DGifGetExtension"); |
| 380 | } |
| 381 | |
| 382 | while (extData != NULL) { |
| 383 | /* Create an extension block with our data */ |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 384 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 385 | if (AddExtensionBlock(&temp_save, extData[0], |
| 386 | &extData[1]) == GIF_ERROR) { |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 387 | #else |
| 388 | if (GifAddExtensionBlock(&gif->ExtensionBlockCount, |
| 389 | &gif->ExtensionBlocks, |
| 390 | extFunction, |
| 391 | extData[0], |
| 392 | &extData[1]) == GIF_ERROR) { |
| 393 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 394 | return error_return(gif, *bm, "AddExtensionBlock"); |
| 395 | } |
| 396 | if (DGifGetExtensionNext(gif, &extData) == GIF_ERROR) { |
| 397 | return error_return(gif, *bm, "DGifGetExtensionNext"); |
| 398 | } |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 399 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 400 | temp_save.Function = 0; |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 401 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 402 | } |
| 403 | break; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 404 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 405 | case TERMINATE_RECORD_TYPE: |
| 406 | break; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 407 | |
| 408 | default: /* Should be trapped by DGifGetRecordType */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 409 | break; |
| 410 | } |
| 411 | } while (recType != TERMINATE_RECORD_TYPE); |
| 412 | |
| 413 | DONE: |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | ec51cb8 | 2012-03-23 18:13:47 +0000 | [diff] [blame] | 418 | DEFINE_DECODER_CREATOR(GIFImageDecoder); |
| 419 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 420 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 421 | static bool is_gif(SkStreamRewindable* stream) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 422 | char buf[GIF_STAMP_LEN]; |
| 423 | if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) { |
| 424 | if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 || |
| 425 | memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 || |
| 426 | memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 427 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 430 | return false; |
| 431 | } |
| 432 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 433 | static SkImageDecoder* sk_libgif_dfactory(SkStreamRewindable* stream) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 434 | if (is_gif(stream)) { |
| 435 | return SkNEW(SkGIFImageDecoder); |
| 436 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 437 | return NULL; |
| 438 | } |
| 439 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 440 | static SkImageDecoder_DecodeReg gReg(sk_libgif_dfactory); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 441 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 442 | static SkImageDecoder::Format get_format_gif(SkStreamRewindable* stream) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 443 | if (is_gif(stream)) { |
| 444 | return SkImageDecoder::kGIF_Format; |
| 445 | } |
| 446 | return SkImageDecoder::kUnknown_Format; |
| 447 | } |
| 448 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 449 | static SkImageDecoder_FormatReg gFormatReg(get_format_gif); |