blob: 103107961bb9891938cde8aa7d6eb1472ff47ba3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkMovie.h"
11#include "SkColor.h"
12#include "SkColorPriv.h"
13#include "SkStream.h"
14#include "SkTemplates.h"
djsollen@google.com57f49692011-02-23 20:46:31 +000015#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
17#include "gif_lib.h"
18
mtklein59c69472015-01-20 12:38:06 -080019#if GIFLIB_MAJOR < 5 || (GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 0)
20#define DGifCloseFile(a, b) DGifCloseFile(a)
21#endif
22
reed@android.com8a1c16f2008-12-17 15:59:43 +000023class SkGIFMovie : public SkMovie {
24public:
25 SkGIFMovie(SkStream* stream);
26 virtual ~SkGIFMovie();
27
28protected:
29 virtual bool onGetInfo(Info*);
30 virtual bool onSetTime(SkMSec);
31 virtual bool onGetBitmap(SkBitmap*);
rmistry@google.comd6176b02012-08-23 18:14:13 +000032
reed@android.com8a1c16f2008-12-17 15:59:43 +000033private:
34 GifFileType* fGIF;
djsollen@google.com57f49692011-02-23 20:46:31 +000035 int fCurrIndex;
36 int fLastDrawIndex;
37 SkBitmap fBackup;
Takahiro.Aizawab5b50a02015-06-15 02:52:46 -070038 SkColor fPaintingColor;
reed@android.com8a1c16f2008-12-17 15:59:43 +000039};
40
reed@android.com8a1c16f2008-12-17 15:59:43 +000041static int Decode(GifFileType* fileType, GifByteType* out, int size) {
42 SkStream* stream = (SkStream*) fileType->UserData;
43 return (int) stream->read(out, size);
44}
45
46SkGIFMovie::SkGIFMovie(SkStream* stream)
47{
reed@google.combb896132013-02-01 19:05:48 +000048#if GIFLIB_MAJOR < 5
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 fGIF = DGifOpen( stream, Decode );
reed@google.combb896132013-02-01 19:05:48 +000050#else
halcanary96fcdcc2015-08-27 07:41:13 -070051 fGIF = DGifOpen( stream, Decode, nullptr );
reed@google.combb896132013-02-01 19:05:48 +000052#endif
halcanary96fcdcc2015-08-27 07:41:13 -070053 if (nullptr == fGIF)
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 return;
55
56 if (DGifSlurp(fGIF) != GIF_OK)
57 {
halcanary96fcdcc2015-08-27 07:41:13 -070058 DGifCloseFile(fGIF, nullptr);
59 fGIF = nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 }
djsollen@google.com57f49692011-02-23 20:46:31 +000061 fCurrIndex = -1;
62 fLastDrawIndex = -1;
Takahiro.Aizawab5b50a02015-06-15 02:52:46 -070063 fPaintingColor = SkPackARGB32(0, 0, 0, 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +000064}
65
66SkGIFMovie::~SkGIFMovie()
67{
68 if (fGIF)
halcanary96fcdcc2015-08-27 07:41:13 -070069 DGifCloseFile(fGIF, nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000070}
71
72static 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.com253ac102013-03-22 13:58:34 +000078 SkASSERT(image->ExtensionBlocks[j].ByteCount >= 4);
reed@android.com8a1c16f2008-12-17 15:59:43 +000079 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
86bool SkGIFMovie::onGetInfo(Info* info)
87{
halcanary96fcdcc2015-08-27 07:41:13 -070088 if (nullptr == fGIF)
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 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
102bool SkGIFMovie::onSetTime(SkMSec time)
103{
halcanary96fcdcc2015-08-27 07:41:13 -0700104 if (nullptr == fGIF)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 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.com57f49692011-02-23 20:46:31 +0000113 fCurrIndex = i;
114 return fLastDrawIndex != fCurrIndex;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 }
116 }
djsollen@google.com57f49692011-02-23 20:46:31 +0000117 fCurrIndex = fGIF->ImageCount - 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 return true;
119}
120
djsollen@google.com57f49692011-02-23 20:46:31 +0000121static 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.com29bf8622013-07-31 15:48:10 +0000132#if GIFLIB_MAJOR < 5
djsollen@google.com57f49692011-02-23 20:46:31 +0000133static 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
150static 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.com29bf8622013-07-31 15:48:10 +0000179#endif
djsollen@google.com57f49692011-02-23 20:46:31 +0000180
181static 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.com57f49692011-02-23 20:46:31 +0000198 for (; copyHeight > 0; copyHeight--) {
199 copyLine(dst, src, cmap, transparent, copyWidth);
200 src += frame->ImageDesc.Width;
201 dst += width;
202 }
203}
204
205static 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
227static 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
halcanary96fcdcc2015-08-27 07:41:13 -0700242 if (frame->ImageDesc.ColorMap != nullptr) {
djsollen@google.com57f49692011-02-23 20:46:31 +0000243 // use local color table
244 cmap = frame->ImageDesc.ColorMap;
245 }
246
halcanary96fcdcc2015-08-27 07:41:13 -0700247 if (cmap == nullptr || cmap->ColorCount != (1 << cmap->BitsPerPixel)) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000248 SkDEBUGFAIL("bad colortable setup");
djsollen@google.com57f49692011-02-23 20:46:31 +0000249 return;
250 }
251
djsollen@google.com29bf8622013-07-31 15:48:10 +0000252#if GIFLIB_MAJOR < 5
253 // before GIFLIB 5, de-interlacing wasn't done by library at load time
djsollen@google.com57f49692011-02-23 20:46:31 +0000254 if (frame->ImageDesc.Interlace) {
255 blitInterlace(bm, frame, cmap, transparent);
djsollen@google.com29bf8622013-07-31 15:48:10 +0000256 return;
djsollen@google.com57f49692011-02-23 20:46:31 +0000257 }
djsollen@google.com29bf8622013-07-31 15:48:10 +0000258#endif
259
260 blitNormal(bm, frame, cmap, transparent);
djsollen@google.com57f49692011-02-23 20:46:31 +0000261}
262
263static 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
279static 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'
294static 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
307static 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.com8a1c16f2008-12-17 15:59:43 +0000345bool SkGIFMovie::onGetBitmap(SkBitmap* bm)
346{
djsollen@google.com57f49692011-02-23 20:46:31 +0000347 const GifFileType* gif = fGIF;
halcanary96fcdcc2015-08-27 07:41:13 -0700348 if (nullptr == gif)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349 return false;
350
djsollen@google.com57f49692011-02-23 20:46:31 +0000351 if (gif->ImageCount < 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352 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.com57f49692011-02-23 20:46:31 +0000361 // no need to draw
362 if (fLastDrawIndex >= 0 && fLastDrawIndex == fCurrIndex) {
363 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364 }
365
djsollen@google.com57f49692011-02-23 20:46:31 +0000366 int startIndex = fLastDrawIndex + 1;
367 if (fLastDrawIndex < 0 || !bm->readyToDraw()) {
368 // first time
369
370 startIndex = 0;
371
372 // create bitmap
reed84825042014-09-02 12:50:45 -0700373 if (!bm->tryAllocN32Pixels(width, height)) {
djsollen@google.com57f49692011-02-23 20:46:31 +0000374 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000375 }
djsollen@google.com57f49692011-02-23 20:46:31 +0000376 // create bitmap for backup
reed84825042014-09-02 12:50:45 -0700377 if (!fBackup.tryAllocN32Pixels(width, height)) {
djsollen@google.com57f49692011-02-23 20:46:31 +0000378 return false;
379 }
380 } else if (startIndex > fCurrIndex) {
381 // rewind to 1st frame for repeat
382 startIndex = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000383 }
384
djsollen@google.com57f49692011-02-23 20:46:31 +0000385 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.com8a1c16f2008-12-17 15:59:43 +0000392 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393
djsollen@google.com57f49692011-02-23 20:46:31 +0000394 SkColor bgColor = SkPackARGB32(0, 0, 0, 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700395 if (gif->SColorMap != nullptr) {
djsollen@google.com57f49692011-02-23 20:46:31 +0000396 const GifColorType& col = gif->SColorMap->Colors[fGIF->SBackGroundColor];
397 bgColor = SkColorSetARGB(0xFF, col.Red, col.Green, col.Blue);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398 }
djsollen@google.com57f49692011-02-23 20:46:31 +0000399
djsollen@google.com57f49692011-02-23 20:46:31 +0000400 // 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);
halcanary96fcdcc2015-08-27 07:41:13 -0700407 if (!trans && gif->SColorMap != nullptr) {
Takahiro.Aizawab5b50a02015-06-15 02:52:46 -0700408 fPaintingColor = bgColor;
djsollen@google.com57f49692011-02-23 20:46:31 +0000409 } else {
Takahiro.Aizawab5b50a02015-06-15 02:52:46 -0700410 fPaintingColor = SkColorSetARGB(0, 0, 0, 0);
djsollen@google.com57f49692011-02-23 20:46:31 +0000411 }
412
Takahiro.Aizawab5b50a02015-06-15 02:52:46 -0700413 bm->eraseColor(fPaintingColor);
414 fBackup.eraseColor(fPaintingColor);
djsollen@google.com57f49692011-02-23 20:46:31 +0000415 } else {
416 // Dispose previous frame before move to next frame.
417 const SavedImage* prev = &fGIF->SavedImages[i-1];
Takahiro.Aizawab5b50a02015-06-15 02:52:46 -0700418 disposeFrameIfNeeded(bm, prev, cur, &fBackup, fPaintingColor);
djsollen@google.com57f49692011-02-23 20:46:31 +0000419 }
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.com8a1c16f2008-12-17 15:59:43 +0000431 return true;
432}
reed@android.com8540a792009-06-18 17:53:12 +0000433
434///////////////////////////////////////////////////////////////////////////////
435
436#include "SkTRegistry.h"
437
scroggo@google.comb5571b32013-09-25 21:34:24 +0000438SkMovie* Factory(SkStreamRewindable* stream) {
reed@android.com8540a792009-06-18 17:53:12 +0000439 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.com5df93ea2009-06-18 18:52:22 +0000442 memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
443 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) {
reed@android.coma16cb972009-06-18 20:26:58 +0000444 // must rewind here, since our construct wants to re-read the data
445 stream->rewind();
halcanary385fe4d2015-08-26 13:07:48 -0700446 return new SkGIFMovie(stream);
reed@android.com8540a792009-06-18 17:53:12 +0000447 }
448 }
halcanary96fcdcc2015-08-27 07:41:13 -0700449 return nullptr;
reed@android.com8540a792009-06-18 17:53:12 +0000450}
451
scroggo@google.comb5571b32013-09-25 21:34:24 +0000452static SkTRegistry<SkMovie*(*)(SkStreamRewindable*)> gReg(Factory);