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