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 | #include "SkColor.h" |
| 9 | #include "SkColorPriv.h" |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 10 | #include "SkColorTable.h" |
| 11 | #include "SkImageDecoder.h" |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 12 | #include "SkRTConf.h" |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 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" |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 16 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
| 18 | #include "gif_lib.h" |
| 19 | |
| 20 | class SkGIFImageDecoder : public SkImageDecoder { |
| 21 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 22 | Format getFormat() const override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | return kGIF_Format; |
| 24 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 25 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | Result onDecode(SkStream* stream, SkBitmap* bm, Mode mode) override; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 28 | |
| 29 | private: |
| 30 | typedef SkImageDecoder INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static const uint8_t gStartingIterlaceYValue[] = { |
| 34 | 0, 4, 2, 1 |
| 35 | }; |
| 36 | static const uint8_t gDeltaIterlaceYValue[] = { |
| 37 | 8, 8, 4, 2 |
| 38 | }; |
| 39 | |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 40 | SK_CONF_DECLARE(bool, c_suppressGIFImageDecoderWarnings, |
| 41 | "images.gif.suppressDecoderWarnings", true, |
| 42 | "Suppress GIF warnings and errors when calling image decode " |
| 43 | "functions."); |
| 44 | |
| 45 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | /* Implement the GIF interlace algorithm in an iterator. |
| 47 | 1) grab every 8th line beginning at 0 |
| 48 | 2) grab every 8th line beginning at 4 |
| 49 | 3) grab every 4th line beginning at 2 |
| 50 | 4) grab every 2nd line beginning at 1 |
| 51 | */ |
| 52 | class GifInterlaceIter { |
| 53 | public: |
| 54 | GifInterlaceIter(int height) : fHeight(height) { |
| 55 | fStartYPtr = gStartingIterlaceYValue; |
| 56 | fDeltaYPtr = gDeltaIterlaceYValue; |
| 57 | |
| 58 | fCurrY = *fStartYPtr++; |
| 59 | fDeltaY = *fDeltaYPtr++; |
| 60 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 61 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | int currY() const { |
| 63 | SkASSERT(fStartYPtr); |
| 64 | SkASSERT(fDeltaYPtr); |
| 65 | return fCurrY; |
| 66 | } |
| 67 | |
| 68 | void next() { |
| 69 | SkASSERT(fStartYPtr); |
| 70 | SkASSERT(fDeltaYPtr); |
| 71 | |
| 72 | int y = fCurrY + fDeltaY; |
| 73 | // We went from an if statement to a while loop so that we iterate |
| 74 | // through fStartYPtr until a valid row is found. This is so that images |
| 75 | // that are smaller than 5x5 will not trash memory. |
| 76 | while (y >= fHeight) { |
| 77 | if (gStartingIterlaceYValue + |
| 78 | SK_ARRAY_COUNT(gStartingIterlaceYValue) == fStartYPtr) { |
| 79 | // we done |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | SkDEBUGCODE(fStartYPtr = nullptr;) |
| 81 | SkDEBUGCODE(fDeltaYPtr = nullptr;) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | y = 0; |
| 83 | } else { |
| 84 | y = *fStartYPtr++; |
| 85 | fDeltaY = *fDeltaYPtr++; |
| 86 | } |
| 87 | } |
| 88 | fCurrY = y; |
| 89 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 90 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 91 | private: |
| 92 | const int fHeight; |
| 93 | int fCurrY; |
| 94 | int fDeltaY; |
| 95 | const uint8_t* fStartYPtr; |
| 96 | const uint8_t* fDeltaYPtr; |
| 97 | }; |
| 98 | |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | static int DecodeCallBackProc(GifFileType* fileType, GifByteType* out, |
| 102 | int size) { |
| 103 | SkStream* stream = (SkStream*) fileType->UserData; |
| 104 | return (int) stream->read(out, size); |
| 105 | } |
| 106 | |
| 107 | void CheckFreeExtension(SavedImage* Image) { |
| 108 | if (Image->ExtensionBlocks) { |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 109 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | FreeExtension(Image); |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 111 | #else |
| 112 | GifFreeExtensions(&Image->ExtensionBlockCount, &Image->ExtensionBlocks); |
| 113 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 117 | // return nullptr on failure |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | static const ColorMapObject* find_colormap(const GifFileType* gif) { |
| 119 | const ColorMapObject* cmap = gif->Image.ColorMap; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 120 | if (nullptr == cmap) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | cmap = gif->SColorMap; |
| 122 | } |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 123 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 124 | if (nullptr == cmap) { |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 125 | // no colormap found |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 126 | return nullptr; |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 127 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | // some sanity checks |
reed@android.com | bc7d2fb | 2010-02-05 15:43:07 +0000 | [diff] [blame] | 129 | if (cmap && ((unsigned)cmap->ColorCount > 256 || |
| 130 | cmap->ColorCount != (1 << cmap->BitsPerPixel))) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 131 | cmap = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | } |
| 133 | return cmap; |
| 134 | } |
| 135 | |
| 136 | // return -1 if not found (i.e. we're completely opaque) |
| 137 | static int find_transpIndex(const SavedImage& image, int colorCount) { |
| 138 | int transpIndex = -1; |
| 139 | for (int i = 0; i < image.ExtensionBlockCount; ++i) { |
| 140 | const ExtensionBlock* eb = image.ExtensionBlocks + i; |
| 141 | if (eb->Function == 0xF9 && eb->ByteCount == 4) { |
| 142 | if (eb->Bytes[0] & 1) { |
| 143 | transpIndex = (unsigned char)eb->Bytes[3]; |
| 144 | // check for valid transpIndex |
| 145 | if (transpIndex >= colorCount) { |
| 146 | transpIndex = -1; |
| 147 | } |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | return transpIndex; |
| 153 | } |
| 154 | |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 155 | static SkImageDecoder::Result error_return(const SkBitmap& bm, const char msg[]) { |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 156 | if (!c_suppressGIFImageDecoderWarnings) { |
| 157 | SkDebugf("libgif error [%s] bitmap [%d %d] pixels %p colortable %p\n", |
| 158 | msg, bm.width(), bm.height(), bm.getPixels(), |
| 159 | bm.getColorTable()); |
| 160 | } |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 161 | return SkImageDecoder::kFailure; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | } |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 163 | |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 164 | static void gif_warning(const SkBitmap& bm, const char msg[]) { |
| 165 | if (!c_suppressGIFImageDecoderWarnings) { |
| 166 | SkDebugf("libgif warning [%s] bitmap [%d %d] pixels %p colortable %p\n", |
| 167 | msg, bm.width(), bm.height(), bm.getPixels(), |
| 168 | bm.getColorTable()); |
| 169 | } |
| 170 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 172 | /** |
| 173 | * Skip rows in the source gif image. |
| 174 | * @param gif Source image. |
| 175 | * @param dst Scratch output needed by gif library call. Must be >= width bytes. |
| 176 | * @param width Bytes per row in the source image. |
| 177 | * @param rowsToSkip Number of rows to skip. |
| 178 | * @return True on success, false on GIF_ERROR. |
| 179 | */ |
| 180 | static bool skip_src_rows(GifFileType* gif, uint8_t* dst, int width, int rowsToSkip) { |
| 181 | for (int i = 0; i < rowsToSkip; i++) { |
| 182 | if (DGifGetLine(gif, dst, width) == GIF_ERROR) { |
| 183 | return false; |
| 184 | } |
| 185 | } |
| 186 | return true; |
| 187 | } |
| 188 | |
halcanary@google.com | 3a8ebd9 | 2013-12-19 20:57:45 +0000 | [diff] [blame] | 189 | /** |
| 190 | * GIFs with fewer then 256 color entries will sometimes index out of |
| 191 | * bounds of the color table (this is malformed, but libgif does not |
| 192 | * check sicne it is rare). This function checks for this error and |
| 193 | * fixes it. This makes the output image consistantly deterministic. |
| 194 | */ |
| 195 | static void sanitize_indexed_bitmap(SkBitmap* bm) { |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 196 | if ((kIndex_8_SkColorType == bm->colorType()) && !(bm->empty())) { |
halcanary@google.com | 3a8ebd9 | 2013-12-19 20:57:45 +0000 | [diff] [blame] | 197 | SkAutoLockPixels alp(*bm); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 198 | if (bm->getPixels()) { |
halcanary@google.com | 3a8ebd9 | 2013-12-19 20:57:45 +0000 | [diff] [blame] | 199 | SkColorTable* ct = bm->getColorTable(); // Index8 must have it. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 200 | SkASSERT(ct != nullptr); |
halcanary@google.com | 3a8ebd9 | 2013-12-19 20:57:45 +0000 | [diff] [blame] | 201 | uint32_t count = ct->count(); |
| 202 | SkASSERT(count > 0); |
| 203 | SkASSERT(count <= 0x100); |
| 204 | if (count != 0x100) { // Full colortables can't go wrong. |
| 205 | // Count is a power of 2; asserted elsewhere. |
| 206 | uint8_t byteMask = (~(count - 1)); |
| 207 | bool warning = false; |
| 208 | uint8_t* addr = static_cast<uint8_t*>(bm->getPixels()); |
| 209 | int height = bm->height(); |
| 210 | int width = bm->width(); |
| 211 | size_t rowBytes = bm->rowBytes(); |
| 212 | while (--height >= 0) { |
| 213 | uint8_t* ptr = addr; |
| 214 | int x = width; |
| 215 | while (--x >= 0) { |
| 216 | if (0 != ((*ptr) & byteMask)) { |
| 217 | warning = true; |
| 218 | *ptr = 0; |
| 219 | } |
| 220 | ++ptr; |
| 221 | } |
| 222 | addr += rowBytes; |
| 223 | } |
| 224 | if (warning) { |
| 225 | gif_warning(*bm, "Index out of bounds."); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
mtklein | 75de4f8 | 2015-01-22 10:32:25 -0800 | [diff] [blame] | 232 | namespace { |
| 233 | // This function is a template argument, so can't be static. |
mtklein | 59c6947 | 2015-01-20 12:38:06 -0800 | [diff] [blame] | 234 | int close_gif(GifFileType* gif) { |
| 235 | #if GIFLIB_MAJOR < 5 || (GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 0) |
| 236 | return DGifCloseFile(gif); |
| 237 | #else |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 238 | return DGifCloseFile(gif, nullptr); |
mtklein | 59c6947 | 2015-01-20 12:38:06 -0800 | [diff] [blame] | 239 | #endif |
| 240 | } |
mtklein | 75de4f8 | 2015-01-22 10:32:25 -0800 | [diff] [blame] | 241 | }//namespace |
mtklein | 59c6947 | 2015-01-20 12:38:06 -0800 | [diff] [blame] | 242 | |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 243 | SkImageDecoder::Result SkGIFImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* bm, Mode mode) { |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 244 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc); |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 246 | #else |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 247 | GifFileType* gif = DGifOpen(sk_stream, DecodeCallBackProc, nullptr); |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 248 | #endif |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 249 | if (nullptr == gif) { |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 250 | return error_return(*bm, "DGifOpen"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | } |
| 252 | |
mtklein | 59c6947 | 2015-01-20 12:38:06 -0800 | [diff] [blame] | 253 | SkAutoTCallIProc<GifFileType, close_gif> acp(gif); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 254 | |
| 255 | SavedImage temp_save; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 256 | temp_save.ExtensionBlocks=nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 257 | temp_save.ExtensionBlockCount=0; |
| 258 | SkAutoTCallVProc<SavedImage, CheckFreeExtension> acp2(&temp_save); |
| 259 | |
| 260 | int width, height; |
| 261 | GifRecordType recType; |
| 262 | GifByteType *extData; |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 263 | #if GIFLIB_MAJOR >= 5 |
| 264 | int extFunction; |
| 265 | #endif |
reed@android.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame] | 266 | int transpIndex = -1; // -1 means we don't have it (yet) |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 267 | int fillIndex = gif->SBackGroundColor; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 268 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 269 | do { |
| 270 | if (DGifGetRecordType(gif, &recType) == GIF_ERROR) { |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 271 | return error_return(*bm, "DGifGetRecordType"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 272 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 273 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 274 | switch (recType) { |
| 275 | case IMAGE_DESC_RECORD_TYPE: { |
| 276 | if (DGifGetImageDesc(gif) == GIF_ERROR) { |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 277 | return error_return(*bm, "IMAGE_DESC_RECORD_TYPE"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | if (gif->ImageCount < 1) { // sanity check |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 281 | return error_return(*bm, "ImageCount < 1"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 282 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 283 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 284 | width = gif->SWidth; |
| 285 | height = gif->SHeight; |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 286 | |
| 287 | SavedImage* image = &gif->SavedImages[gif->ImageCount-1]; |
| 288 | const GifImageDesc& desc = image->ImageDesc; |
| 289 | |
| 290 | int imageLeft = desc.Left; |
| 291 | int imageTop = desc.Top; |
| 292 | const int innerWidth = desc.Width; |
| 293 | const int innerHeight = desc.Height; |
| 294 | if (innerWidth <= 0 || innerHeight <= 0) { |
| 295 | return error_return(*bm, "invalid dimensions"); |
| 296 | } |
| 297 | |
| 298 | // check for valid descriptor |
| 299 | if (innerWidth > width) { |
| 300 | gif_warning(*bm, "image too wide, expanding output to size"); |
| 301 | width = innerWidth; |
| 302 | imageLeft = 0; |
| 303 | } else if (imageLeft + innerWidth > width) { |
| 304 | gif_warning(*bm, "shifting image left to fit"); |
| 305 | imageLeft = width - innerWidth; |
| 306 | } else if (imageLeft < 0) { |
| 307 | gif_warning(*bm, "shifting image right to fit"); |
| 308 | imageLeft = 0; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | if (innerHeight > height) { |
| 313 | gif_warning(*bm, "image too tall, expanding output to size"); |
| 314 | height = innerHeight; |
| 315 | imageTop = 0; |
| 316 | } else if (imageTop + innerHeight > height) { |
| 317 | gif_warning(*bm, "shifting image up to fit"); |
| 318 | imageTop = height - innerHeight; |
| 319 | } else if (imageTop < 0) { |
| 320 | gif_warning(*bm, "shifting image down to fit"); |
| 321 | imageTop = 0; |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 322 | } |
| 323 | |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 324 | SkScaledBitmapSampler sampler(width, height, this->getSampleSize()); |
| 325 | |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 326 | bm->setInfo(SkImageInfo::Make(sampler.scaledWidth(), sampler.scaledHeight(), |
| 327 | kIndex_8_SkColorType, kPremul_SkAlphaType)); |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 328 | |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 329 | if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 330 | return kSuccess; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 331 | } |
| 332 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 333 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 334 | // now we decode the colortable |
| 335 | int colorCount = 0; |
| 336 | { |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 337 | // Declare colorPtr here for scope. |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 338 | SkPMColor colorPtr[256]; // storage for worst-case |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 339 | const ColorMapObject* cmap = find_colormap(gif); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 340 | if (cmap != nullptr) { |
halcanary@google.com | 3a8ebd9 | 2013-12-19 20:57:45 +0000 | [diff] [blame] | 341 | SkASSERT(cmap->ColorCount == (1 << (cmap->BitsPerPixel))); |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 342 | colorCount = cmap->ColorCount; |
| 343 | if (colorCount > 256) { |
| 344 | colorCount = 256; // our kIndex8 can't support more |
| 345 | } |
| 346 | for (int index = 0; index < colorCount; index++) { |
| 347 | colorPtr[index] = SkPackARGB32(0xFF, |
| 348 | cmap->Colors[index].Red, |
| 349 | cmap->Colors[index].Green, |
| 350 | cmap->Colors[index].Blue); |
| 351 | } |
| 352 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 353 | // find_colormap() returned nullptr. Some (rare, broken) |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 354 | // GIFs don't have a color table, so we force one. |
| 355 | gif_warning(*bm, "missing colormap"); |
| 356 | colorCount = 256; |
| 357 | sk_memset32(colorPtr, SK_ColorWHITE, colorCount); |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 358 | } |
reed@android.com | 9781ca5 | 2009-04-14 14:28:22 +0000 | [diff] [blame] | 359 | transpIndex = find_transpIndex(temp_save, colorCount); |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 360 | if (transpIndex >= 0) { |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 361 | colorPtr[transpIndex] = SK_ColorTRANSPARENT; // ram in a transparent SkPMColor |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 362 | fillIndex = transpIndex; |
| 363 | } else if (fillIndex >= colorCount) { |
| 364 | // gif->SBackGroundColor should be less than colorCount. |
| 365 | fillIndex = 0; // If not, fix it. |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 366 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 367 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 368 | SkAutoTUnref<SkColorTable> ctable(new SkColorTable(colorPtr, colorCount)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 369 | if (!this->allocPixelRef(bm, ctable)) { |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 370 | return error_return(*bm, "allocPixelRef"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 373 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 374 | // abort if either inner dimension is <= 0 |
| 375 | if (innerWidth <= 0 || innerHeight <= 0) { |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 376 | return error_return(*bm, "non-pos inner width/height"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 377 | } |
| 378 | |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 379 | SkAutoLockPixels alp(*bm); |
| 380 | |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 381 | SkAutoTMalloc<uint8_t> storage(innerWidth); |
| 382 | uint8_t* scanline = storage.get(); |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 383 | |
| 384 | // GIF has an option to store the scanlines of an image, plus a larger background, |
| 385 | // filled by a fill color. In this case, we will use a subset of the larger bitmap |
| 386 | // for sampling. |
| 387 | SkBitmap subset; |
| 388 | SkBitmap* workingBitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 389 | // are we only a subset of the total bounds? |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 390 | if ((imageTop | imageLeft) > 0 || |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 391 | innerWidth < width || innerHeight < height) { |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 392 | // Fill the background. |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 393 | memset(bm->getPixels(), fillIndex, bm->getSize()); |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 394 | |
| 395 | // Create a subset of the bitmap. |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 396 | SkIRect subsetRect(SkIRect::MakeXYWH(imageLeft / sampler.srcDX(), |
| 397 | imageTop / sampler.srcDY(), |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 398 | innerWidth / sampler.srcDX(), |
| 399 | innerHeight / sampler.srcDY())); |
| 400 | if (!bm->extractSubset(&subset, subsetRect)) { |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 401 | return error_return(*bm, "Extract failed."); |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 402 | } |
| 403 | // Update the sampler. We'll now be only sampling into the subset. |
| 404 | sampler = SkScaledBitmapSampler(innerWidth, innerHeight, this->getSampleSize()); |
| 405 | workingBitmap = ⊂ |
| 406 | } else { |
| 407 | workingBitmap = bm; |
| 408 | } |
| 409 | |
| 410 | // bm is already locked, but if we had to take a subset, it must be locked also, |
| 411 | // so that getPixels() will point to its pixels. |
| 412 | SkAutoLockPixels alpWorking(*workingBitmap); |
| 413 | |
| 414 | if (!sampler.begin(workingBitmap, SkScaledBitmapSampler::kIndex, *this)) { |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 415 | return error_return(*bm, "Sampler failed to begin."); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 416 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 417 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 418 | // now decode each scanline |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 419 | if (gif->Image.Interlace) { |
| 420 | // Iterate over the height of the source data. The sampler will |
| 421 | // take care of skipping unneeded rows. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 422 | GifInterlaceIter iter(innerHeight); |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 423 | for (int y = 0; y < innerHeight; y++) { |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 424 | if (DGifGetLine(gif, scanline, innerWidth) == GIF_ERROR) { |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 425 | gif_warning(*bm, "interlace DGifGetLine"); |
| 426 | memset(scanline, fillIndex, innerWidth); |
| 427 | for (; y < innerHeight; y++) { |
| 428 | sampler.sampleInterlaced(scanline, iter.currY()); |
| 429 | iter.next(); |
| 430 | } |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 431 | return kPartialSuccess; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 432 | } |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 433 | sampler.sampleInterlaced(scanline, iter.currY()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 434 | iter.next(); |
| 435 | } |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 436 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 437 | // easy, non-interlace case |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 438 | const int outHeight = workingBitmap->height(); |
| 439 | skip_src_rows(gif, scanline, innerWidth, sampler.srcY0()); |
| 440 | for (int y = 0; y < outHeight; y++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 441 | if (DGifGetLine(gif, scanline, innerWidth) == GIF_ERROR) { |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 442 | gif_warning(*bm, "DGifGetLine"); |
| 443 | memset(scanline, fillIndex, innerWidth); |
| 444 | for (; y < outHeight; y++) { |
| 445 | sampler.next(scanline); |
| 446 | } |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 447 | return kPartialSuccess; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 448 | } |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 449 | // scanline now contains the raw data. Sample it. |
| 450 | sampler.next(scanline); |
| 451 | if (y < outHeight - 1) { |
| 452 | skip_src_rows(gif, scanline, innerWidth, sampler.srcDY() - 1); |
| 453 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 454 | } |
commit-bot@chromium.org | dac4a1d | 2013-10-08 19:40:18 +0000 | [diff] [blame] | 455 | // skip the rest of the rows (if any) |
| 456 | int read = (outHeight - 1) * sampler.srcDY() + sampler.srcY0() + 1; |
| 457 | SkASSERT(read <= innerHeight); |
| 458 | skip_src_rows(gif, scanline, innerWidth, innerHeight - read); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 459 | } |
halcanary@google.com | 3a8ebd9 | 2013-12-19 20:57:45 +0000 | [diff] [blame] | 460 | sanitize_indexed_bitmap(bm); |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 461 | return kSuccess; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 462 | } break; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 463 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 464 | case EXTENSION_RECORD_TYPE: |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 465 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 466 | if (DGifGetExtension(gif, &temp_save.Function, |
| 467 | &extData) == GIF_ERROR) { |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 468 | #else |
| 469 | if (DGifGetExtension(gif, &extFunction, &extData) == GIF_ERROR) { |
| 470 | #endif |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 471 | return error_return(*bm, "DGifGetExtension"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 472 | } |
| 473 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 474 | while (extData != nullptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 475 | /* Create an extension block with our data */ |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 476 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 477 | if (AddExtensionBlock(&temp_save, extData[0], |
| 478 | &extData[1]) == GIF_ERROR) { |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 479 | #else |
jei.mayol | 63af144 | 2015-02-04 21:31:23 -0800 | [diff] [blame] | 480 | if (GifAddExtensionBlock(&temp_save.ExtensionBlockCount, |
| 481 | &temp_save.ExtensionBlocks, |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 482 | extFunction, |
| 483 | extData[0], |
| 484 | &extData[1]) == GIF_ERROR) { |
| 485 | #endif |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 486 | return error_return(*bm, "AddExtensionBlock"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 487 | } |
| 488 | if (DGifGetExtensionNext(gif, &extData) == GIF_ERROR) { |
halcanary@google.com | 29d4e63 | 2013-10-11 18:21:56 +0000 | [diff] [blame] | 489 | return error_return(*bm, "DGifGetExtensionNext"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 490 | } |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 491 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 492 | temp_save.Function = 0; |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 493 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 494 | } |
| 495 | break; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 496 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 497 | case TERMINATE_RECORD_TYPE: |
| 498 | break; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 499 | |
| 500 | default: /* Should be trapped by DGifGetRecordType */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 501 | break; |
| 502 | } |
| 503 | } while (recType != TERMINATE_RECORD_TYPE); |
| 504 | |
halcanary@google.com | 3a8ebd9 | 2013-12-19 20:57:45 +0000 | [diff] [blame] | 505 | sanitize_indexed_bitmap(bm); |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 506 | return kSuccess; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | ec51cb8 | 2012-03-23 18:13:47 +0000 | [diff] [blame] | 510 | DEFINE_DECODER_CREATOR(GIFImageDecoder); |
| 511 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 512 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 513 | static bool is_gif(SkStreamRewindable* stream) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 514 | char buf[GIF_STAMP_LEN]; |
| 515 | if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) { |
| 516 | if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 || |
| 517 | memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 || |
| 518 | memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 519 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 522 | return false; |
| 523 | } |
| 524 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 525 | static SkImageDecoder* sk_libgif_dfactory(SkStreamRewindable* stream) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 526 | if (is_gif(stream)) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 527 | return new SkGIFImageDecoder; |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 528 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 529 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 530 | } |
| 531 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 532 | static SkImageDecoder_DecodeReg gReg(sk_libgif_dfactory); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 533 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 534 | static SkImageDecoder::Format get_format_gif(SkStreamRewindable* stream) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 535 | if (is_gif(stream)) { |
| 536 | return SkImageDecoder::kGIF_Format; |
| 537 | } |
| 538 | return SkImageDecoder::kUnknown_Format; |
| 539 | } |
| 540 | |
mtklein@google.com | bd6343b | 2013-09-04 17:20:18 +0000 | [diff] [blame] | 541 | static SkImageDecoder_FormatReg gFormatReg(get_format_gif); |