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 "SkMovie.h" |
| 11 | #include "SkColor.h" |
| 12 | #include "SkColorPriv.h" |
| 13 | #include "SkStream.h" |
| 14 | #include "SkTemplates.h" |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 15 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | |
| 17 | #include "gif_lib.h" |
| 18 | |
mtklein | 59c6947 | 2015-01-20 12:38:06 -0800 | [diff] [blame] | 19 | #if GIFLIB_MAJOR < 5 || (GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 0) |
| 20 | #define DGifCloseFile(a, b) DGifCloseFile(a) |
| 21 | #endif |
| 22 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | class SkGIFMovie : public SkMovie { |
| 24 | public: |
| 25 | SkGIFMovie(SkStream* stream); |
| 26 | virtual ~SkGIFMovie(); |
| 27 | |
| 28 | protected: |
| 29 | virtual bool onGetInfo(Info*); |
| 30 | virtual bool onSetTime(SkMSec); |
| 31 | virtual bool onGetBitmap(SkBitmap*); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 32 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | private: |
| 34 | GifFileType* fGIF; |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 35 | int fCurrIndex; |
| 36 | int fLastDrawIndex; |
| 37 | SkBitmap fBackup; |
Takahiro.Aizawa | b5b50a0 | 2015-06-15 02:52:46 -0700 | [diff] [blame] | 38 | SkColor fPaintingColor; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | static int Decode(GifFileType* fileType, GifByteType* out, int size) { |
| 42 | SkStream* stream = (SkStream*) fileType->UserData; |
| 43 | return (int) stream->read(out, size); |
| 44 | } |
| 45 | |
| 46 | SkGIFMovie::SkGIFMovie(SkStream* stream) |
| 47 | { |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 48 | #if GIFLIB_MAJOR < 5 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | fGIF = DGifOpen( stream, Decode ); |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 50 | #else |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 51 | fGIF = DGifOpen( stream, Decode, nullptr ); |
reed@google.com | bb89613 | 2013-02-01 19:05:48 +0000 | [diff] [blame] | 52 | #endif |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | if (nullptr == fGIF) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | return; |
| 55 | |
| 56 | if (DGifSlurp(fGIF) != GIF_OK) |
| 57 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 58 | DGifCloseFile(fGIF, nullptr); |
| 59 | fGIF = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | } |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 61 | fCurrIndex = -1; |
| 62 | fLastDrawIndex = -1; |
Takahiro.Aizawa | b5b50a0 | 2015-06-15 02:52:46 -0700 | [diff] [blame] | 63 | fPaintingColor = SkPackARGB32(0, 0, 0, 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | SkGIFMovie::~SkGIFMovie() |
| 67 | { |
| 68 | if (fGIF) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | DGifCloseFile(fGIF, nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static SkMSec savedimage_duration(const SavedImage* image) |
| 73 | { |
| 74 | for (int j = 0; j < image->ExtensionBlockCount; j++) |
| 75 | { |
| 76 | if (image->ExtensionBlocks[j].Function == GRAPHICS_EXT_FUNC_CODE) |
| 77 | { |
reed@google.com | 253ac10 | 2013-03-22 13:58:34 +0000 | [diff] [blame] | 78 | SkASSERT(image->ExtensionBlocks[j].ByteCount >= 4); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | const uint8_t* b = (const uint8_t*)image->ExtensionBlocks[j].Bytes; |
| 80 | return ((b[2] << 8) | b[1]) * 10; |
| 81 | } |
| 82 | } |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | bool SkGIFMovie::onGetInfo(Info* info) |
| 87 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 88 | if (nullptr == fGIF) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | return false; |
| 90 | |
| 91 | SkMSec dur = 0; |
| 92 | for (int i = 0; i < fGIF->ImageCount; i++) |
| 93 | dur += savedimage_duration(&fGIF->SavedImages[i]); |
| 94 | |
| 95 | info->fDuration = dur; |
| 96 | info->fWidth = fGIF->SWidth; |
| 97 | info->fHeight = fGIF->SHeight; |
| 98 | info->fIsOpaque = false; // how to compute? |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | bool SkGIFMovie::onSetTime(SkMSec time) |
| 103 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 104 | if (nullptr == fGIF) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | return false; |
| 106 | |
| 107 | SkMSec dur = 0; |
| 108 | for (int i = 0; i < fGIF->ImageCount; i++) |
| 109 | { |
| 110 | dur += savedimage_duration(&fGIF->SavedImages[i]); |
| 111 | if (dur >= time) |
| 112 | { |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 113 | fCurrIndex = i; |
| 114 | return fLastDrawIndex != fCurrIndex; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 117 | fCurrIndex = fGIF->ImageCount - 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 121 | static void copyLine(uint32_t* dst, const unsigned char* src, const ColorMapObject* cmap, |
| 122 | int transparent, int width) |
| 123 | { |
| 124 | for (; width > 0; width--, src++, dst++) { |
| 125 | if (*src != transparent) { |
| 126 | const GifColorType& col = cmap->Colors[*src]; |
| 127 | *dst = SkPackARGB32(0xFF, col.Red, col.Green, col.Blue); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
djsollen@google.com | 29bf862 | 2013-07-31 15:48:10 +0000 | [diff] [blame] | 132 | #if GIFLIB_MAJOR < 5 |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 133 | static void copyInterlaceGroup(SkBitmap* bm, const unsigned char*& src, |
| 134 | const ColorMapObject* cmap, int transparent, int copyWidth, |
| 135 | int copyHeight, const GifImageDesc& imageDesc, int rowStep, |
| 136 | int startRow) |
| 137 | { |
| 138 | int row; |
| 139 | // every 'rowStep'th row, starting with row 'startRow' |
| 140 | for (row = startRow; row < copyHeight; row += rowStep) { |
| 141 | uint32_t* dst = bm->getAddr32(imageDesc.Left, imageDesc.Top + row); |
| 142 | copyLine(dst, src, cmap, transparent, copyWidth); |
| 143 | src += imageDesc.Width; |
| 144 | } |
| 145 | |
| 146 | // pad for rest height |
| 147 | src += imageDesc.Width * ((imageDesc.Height - row + rowStep - 1) / rowStep); |
| 148 | } |
| 149 | |
| 150 | static void blitInterlace(SkBitmap* bm, const SavedImage* frame, const ColorMapObject* cmap, |
| 151 | int transparent) |
| 152 | { |
| 153 | int width = bm->width(); |
| 154 | int height = bm->height(); |
| 155 | GifWord copyWidth = frame->ImageDesc.Width; |
| 156 | if (frame->ImageDesc.Left + copyWidth > width) { |
| 157 | copyWidth = width - frame->ImageDesc.Left; |
| 158 | } |
| 159 | |
| 160 | GifWord copyHeight = frame->ImageDesc.Height; |
| 161 | if (frame->ImageDesc.Top + copyHeight > height) { |
| 162 | copyHeight = height - frame->ImageDesc.Top; |
| 163 | } |
| 164 | |
| 165 | // deinterlace |
| 166 | const unsigned char* src = (unsigned char*)frame->RasterBits; |
| 167 | |
| 168 | // group 1 - every 8th row, starting with row 0 |
| 169 | copyInterlaceGroup(bm, src, cmap, transparent, copyWidth, copyHeight, frame->ImageDesc, 8, 0); |
| 170 | |
| 171 | // group 2 - every 8th row, starting with row 4 |
| 172 | copyInterlaceGroup(bm, src, cmap, transparent, copyWidth, copyHeight, frame->ImageDesc, 8, 4); |
| 173 | |
| 174 | // group 3 - every 4th row, starting with row 2 |
| 175 | copyInterlaceGroup(bm, src, cmap, transparent, copyWidth, copyHeight, frame->ImageDesc, 4, 2); |
| 176 | |
| 177 | copyInterlaceGroup(bm, src, cmap, transparent, copyWidth, copyHeight, frame->ImageDesc, 2, 1); |
| 178 | } |
djsollen@google.com | 29bf862 | 2013-07-31 15:48:10 +0000 | [diff] [blame] | 179 | #endif |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 180 | |
| 181 | static void blitNormal(SkBitmap* bm, const SavedImage* frame, const ColorMapObject* cmap, |
| 182 | int transparent) |
| 183 | { |
| 184 | int width = bm->width(); |
| 185 | int height = bm->height(); |
| 186 | const unsigned char* src = (unsigned char*)frame->RasterBits; |
| 187 | uint32_t* dst = bm->getAddr32(frame->ImageDesc.Left, frame->ImageDesc.Top); |
| 188 | GifWord copyWidth = frame->ImageDesc.Width; |
| 189 | if (frame->ImageDesc.Left + copyWidth > width) { |
| 190 | copyWidth = width - frame->ImageDesc.Left; |
| 191 | } |
| 192 | |
| 193 | GifWord copyHeight = frame->ImageDesc.Height; |
| 194 | if (frame->ImageDesc.Top + copyHeight > height) { |
| 195 | copyHeight = height - frame->ImageDesc.Top; |
| 196 | } |
| 197 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 198 | for (; copyHeight > 0; copyHeight--) { |
| 199 | copyLine(dst, src, cmap, transparent, copyWidth); |
| 200 | src += frame->ImageDesc.Width; |
| 201 | dst += width; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | static void fillRect(SkBitmap* bm, GifWord left, GifWord top, GifWord width, GifWord height, |
| 206 | uint32_t col) |
| 207 | { |
| 208 | int bmWidth = bm->width(); |
| 209 | int bmHeight = bm->height(); |
| 210 | uint32_t* dst = bm->getAddr32(left, top); |
| 211 | GifWord copyWidth = width; |
| 212 | if (left + copyWidth > bmWidth) { |
| 213 | copyWidth = bmWidth - left; |
| 214 | } |
| 215 | |
| 216 | GifWord copyHeight = height; |
| 217 | if (top + copyHeight > bmHeight) { |
| 218 | copyHeight = bmHeight - top; |
| 219 | } |
| 220 | |
| 221 | for (; copyHeight > 0; copyHeight--) { |
| 222 | sk_memset32(dst, col, copyWidth); |
| 223 | dst += bmWidth; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | static void drawFrame(SkBitmap* bm, const SavedImage* frame, const ColorMapObject* cmap) |
| 228 | { |
| 229 | int transparent = -1; |
| 230 | |
| 231 | for (int i = 0; i < frame->ExtensionBlockCount; ++i) { |
| 232 | ExtensionBlock* eb = frame->ExtensionBlocks + i; |
| 233 | if (eb->Function == GRAPHICS_EXT_FUNC_CODE && |
| 234 | eb->ByteCount == 4) { |
| 235 | bool has_transparency = ((eb->Bytes[0] & 1) == 1); |
| 236 | if (has_transparency) { |
| 237 | transparent = (unsigned char)eb->Bytes[3]; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 242 | if (frame->ImageDesc.ColorMap != nullptr) { |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 243 | // use local color table |
| 244 | cmap = frame->ImageDesc.ColorMap; |
| 245 | } |
| 246 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 247 | if (cmap == nullptr || cmap->ColorCount != (1 << cmap->BitsPerPixel)) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 248 | SkDEBUGFAIL("bad colortable setup"); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 249 | return; |
| 250 | } |
| 251 | |
djsollen@google.com | 29bf862 | 2013-07-31 15:48:10 +0000 | [diff] [blame] | 252 | #if GIFLIB_MAJOR < 5 |
| 253 | // before GIFLIB 5, de-interlacing wasn't done by library at load time |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 254 | if (frame->ImageDesc.Interlace) { |
| 255 | blitInterlace(bm, frame, cmap, transparent); |
djsollen@google.com | 29bf862 | 2013-07-31 15:48:10 +0000 | [diff] [blame] | 256 | return; |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 257 | } |
djsollen@google.com | 29bf862 | 2013-07-31 15:48:10 +0000 | [diff] [blame] | 258 | #endif |
| 259 | |
| 260 | blitNormal(bm, frame, cmap, transparent); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static bool checkIfWillBeCleared(const SavedImage* frame) |
| 264 | { |
| 265 | for (int i = 0; i < frame->ExtensionBlockCount; ++i) { |
| 266 | ExtensionBlock* eb = frame->ExtensionBlocks + i; |
| 267 | if (eb->Function == GRAPHICS_EXT_FUNC_CODE && |
| 268 | eb->ByteCount == 4) { |
| 269 | // check disposal method |
| 270 | int disposal = ((eb->Bytes[0] >> 2) & 7); |
| 271 | if (disposal == 2 || disposal == 3) { |
| 272 | return true; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | static void getTransparencyAndDisposalMethod(const SavedImage* frame, bool* trans, int* disposal) |
| 280 | { |
| 281 | *trans = false; |
| 282 | *disposal = 0; |
| 283 | for (int i = 0; i < frame->ExtensionBlockCount; ++i) { |
| 284 | ExtensionBlock* eb = frame->ExtensionBlocks + i; |
| 285 | if (eb->Function == GRAPHICS_EXT_FUNC_CODE && |
| 286 | eb->ByteCount == 4) { |
| 287 | *trans = ((eb->Bytes[0] & 1) == 1); |
| 288 | *disposal = ((eb->Bytes[0] >> 2) & 7); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // return true if area of 'target' is completely covers area of 'covered' |
| 294 | static bool checkIfCover(const SavedImage* target, const SavedImage* covered) |
| 295 | { |
| 296 | if (target->ImageDesc.Left <= covered->ImageDesc.Left |
| 297 | && covered->ImageDesc.Left + covered->ImageDesc.Width <= |
| 298 | target->ImageDesc.Left + target->ImageDesc.Width |
| 299 | && target->ImageDesc.Top <= covered->ImageDesc.Top |
| 300 | && covered->ImageDesc.Top + covered->ImageDesc.Height <= |
| 301 | target->ImageDesc.Top + target->ImageDesc.Height) { |
| 302 | return true; |
| 303 | } |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | static void disposeFrameIfNeeded(SkBitmap* bm, const SavedImage* cur, const SavedImage* next, |
| 308 | SkBitmap* backup, SkColor color) |
| 309 | { |
| 310 | // We can skip disposal process if next frame is not transparent |
| 311 | // and completely covers current area |
| 312 | bool curTrans; |
| 313 | int curDisposal; |
| 314 | getTransparencyAndDisposalMethod(cur, &curTrans, &curDisposal); |
| 315 | bool nextTrans; |
| 316 | int nextDisposal; |
| 317 | getTransparencyAndDisposalMethod(next, &nextTrans, &nextDisposal); |
| 318 | if ((curDisposal == 2 || curDisposal == 3) |
| 319 | && (nextTrans || !checkIfCover(next, cur))) { |
| 320 | switch (curDisposal) { |
| 321 | // restore to background color |
| 322 | // -> 'background' means background under this image. |
| 323 | case 2: |
| 324 | fillRect(bm, cur->ImageDesc.Left, cur->ImageDesc.Top, |
| 325 | cur->ImageDesc.Width, cur->ImageDesc.Height, |
| 326 | color); |
| 327 | break; |
| 328 | |
| 329 | // restore to previous |
| 330 | case 3: |
| 331 | bm->swap(*backup); |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // Save current image if next frame's disposal method == 3 |
| 337 | if (nextDisposal == 3) { |
| 338 | const uint32_t* src = bm->getAddr32(0, 0); |
| 339 | uint32_t* dst = backup->getAddr32(0, 0); |
| 340 | int cnt = bm->width() * bm->height(); |
| 341 | memcpy(dst, src, cnt*sizeof(uint32_t)); |
| 342 | } |
| 343 | } |
| 344 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 345 | bool SkGIFMovie::onGetBitmap(SkBitmap* bm) |
| 346 | { |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 347 | const GifFileType* gif = fGIF; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 348 | if (nullptr == gif) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 349 | return false; |
| 350 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 351 | if (gif->ImageCount < 1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 352 | return false; |
| 353 | } |
| 354 | |
| 355 | const int width = gif->SWidth; |
| 356 | const int height = gif->SHeight; |
| 357 | if (width <= 0 || height <= 0) { |
| 358 | return false; |
| 359 | } |
| 360 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 361 | // no need to draw |
| 362 | if (fLastDrawIndex >= 0 && fLastDrawIndex == fCurrIndex) { |
| 363 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 364 | } |
| 365 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 366 | int startIndex = fLastDrawIndex + 1; |
| 367 | if (fLastDrawIndex < 0 || !bm->readyToDraw()) { |
| 368 | // first time |
| 369 | |
| 370 | startIndex = 0; |
| 371 | |
| 372 | // create bitmap |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 373 | if (!bm->tryAllocN32Pixels(width, height)) { |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 374 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 375 | } |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 376 | // create bitmap for backup |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 377 | if (!fBackup.tryAllocN32Pixels(width, height)) { |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 378 | return false; |
| 379 | } |
| 380 | } else if (startIndex > fCurrIndex) { |
| 381 | // rewind to 1st frame for repeat |
| 382 | startIndex = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 383 | } |
| 384 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 385 | int lastIndex = fCurrIndex; |
| 386 | if (lastIndex < 0) { |
| 387 | // first time |
| 388 | lastIndex = 0; |
| 389 | } else if (lastIndex > fGIF->ImageCount - 1) { |
| 390 | // this block must not be reached. |
| 391 | lastIndex = fGIF->ImageCount - 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 392 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 393 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 394 | SkColor bgColor = SkPackARGB32(0, 0, 0, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 395 | if (gif->SColorMap != nullptr) { |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 396 | const GifColorType& col = gif->SColorMap->Colors[fGIF->SBackGroundColor]; |
| 397 | bgColor = SkColorSetARGB(0xFF, col.Red, col.Green, col.Blue); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 398 | } |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 399 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 400 | // draw each frames - not intelligent way |
| 401 | for (int i = startIndex; i <= lastIndex; i++) { |
| 402 | const SavedImage* cur = &fGIF->SavedImages[i]; |
| 403 | if (i == 0) { |
| 404 | bool trans; |
| 405 | int disposal; |
| 406 | getTransparencyAndDisposalMethod(cur, &trans, &disposal); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 407 | if (!trans && gif->SColorMap != nullptr) { |
Takahiro.Aizawa | b5b50a0 | 2015-06-15 02:52:46 -0700 | [diff] [blame] | 408 | fPaintingColor = bgColor; |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 409 | } else { |
Takahiro.Aizawa | b5b50a0 | 2015-06-15 02:52:46 -0700 | [diff] [blame] | 410 | fPaintingColor = SkColorSetARGB(0, 0, 0, 0); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Takahiro.Aizawa | b5b50a0 | 2015-06-15 02:52:46 -0700 | [diff] [blame] | 413 | bm->eraseColor(fPaintingColor); |
| 414 | fBackup.eraseColor(fPaintingColor); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 415 | } else { |
| 416 | // Dispose previous frame before move to next frame. |
| 417 | const SavedImage* prev = &fGIF->SavedImages[i-1]; |
Takahiro.Aizawa | b5b50a0 | 2015-06-15 02:52:46 -0700 | [diff] [blame] | 418 | disposeFrameIfNeeded(bm, prev, cur, &fBackup, fPaintingColor); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | // Draw frame |
| 422 | // We can skip this process if this index is not last and disposal |
| 423 | // method == 2 or method == 3 |
| 424 | if (i == lastIndex || !checkIfWillBeCleared(cur)) { |
| 425 | drawFrame(bm, cur, gif->SColorMap); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // save index |
| 430 | fLastDrawIndex = lastIndex; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 431 | return true; |
| 432 | } |
reed@android.com | 8540a79 | 2009-06-18 17:53:12 +0000 | [diff] [blame] | 433 | |
| 434 | /////////////////////////////////////////////////////////////////////////////// |
| 435 | |
| 436 | #include "SkTRegistry.h" |
| 437 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 438 | SkMovie* Factory(SkStreamRewindable* stream) { |
reed@android.com | 8540a79 | 2009-06-18 17:53:12 +0000 | [diff] [blame] | 439 | char buf[GIF_STAMP_LEN]; |
| 440 | if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) { |
| 441 | if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 || |
reed@android.com | 5df93ea | 2009-06-18 18:52:22 +0000 | [diff] [blame] | 442 | memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 || |
| 443 | memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) { |
reed@android.com | a16cb97 | 2009-06-18 20:26:58 +0000 | [diff] [blame] | 444 | // must rewind here, since our construct wants to re-read the data |
| 445 | stream->rewind(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 446 | return new SkGIFMovie(stream); |
reed@android.com | 8540a79 | 2009-06-18 17:53:12 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 449 | return nullptr; |
reed@android.com | 8540a79 | 2009-06-18 17:53:12 +0000 | [diff] [blame] | 450 | } |
| 451 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 452 | static SkTRegistry<SkMovie*(*)(SkStreamRewindable*)> gReg(Factory); |