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