• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 
9 #include "Movie.h"
10 #include "SkColor.h"
11 #include "SkColorPriv.h"
12 #include "SkStream.h"
13 
14 #include "gif_lib.h"
15 
16 #include <log/log.h>
17 
18 #include <string.h>
19 
20 #if GIFLIB_MAJOR < 5 || (GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 0)
21 #define DGifCloseFile(a, b) DGifCloseFile(a)
22 #endif
23 
24 class GIFMovie : public Movie {
25 public:
26     explicit GIFMovie(SkStream* stream);
27     virtual ~GIFMovie();
28 
29 protected:
30     virtual bool onGetInfo(Info*);
31     virtual bool onSetTime(SkMSec);
32     virtual bool onGetBitmap(SkBitmap*);
33 
34 private:
35     GifFileType* fGIF;
36     int fCurrIndex;
37     int fLastDrawIndex;
38     SkBitmap fBackup;
39     SkColor fPaintingColor;
40 };
41 
Decode(GifFileType * fileType,GifByteType * out,int size)42 static int Decode(GifFileType* fileType, GifByteType* out, int size) {
43     SkStream* stream = (SkStream*) fileType->UserData;
44     return (int) stream->read(out, size);
45 }
46 
GIFMovie(SkStream * stream)47 GIFMovie::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 
~GIFMovie()67 GIFMovie::~GIFMovie()
68 {
69     if (fGIF)
70         DGifCloseFile(fGIF, nullptr);
71 }
72 
savedimage_duration(const SavedImage * image)73 static 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 
onGetInfo(Info * info)87 bool 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 
onSetTime(SkMSec time)103 bool 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 
copyLine(uint32_t * dst,const unsigned char * src,const ColorMapObject * cmap,int transparent,int width)122 static 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++) {
126         if (*src != transparent && *src < cmap->ColorCount) {
127             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
copyInterlaceGroup(SkBitmap * bm,const unsigned char * & src,const ColorMapObject * cmap,int transparent,int copyWidth,int copyHeight,const GifImageDesc & imageDesc,int rowStep,int startRow)134 static 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 
blitInterlace(SkBitmap * bm,const SavedImage * frame,const ColorMapObject * cmap,int transparent)151 static 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 
blitNormal(SkBitmap * bm,const SavedImage * frame,const ColorMapObject * cmap,int transparent)182 static 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 
fillRect(SkBitmap * bm,GifWord left,GifWord top,GifWord width,GifWord height,uint32_t col)206 static 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     size_t bytes = copyWidth * SkColorTypeBytesPerPixel(bm->colorType());
223     for (; copyHeight > 0; copyHeight--) {
224         memset(dst, col, bytes);
225         dst += bmWidth;
226     }
227 }
228 
drawFrame(SkBitmap * bm,const SavedImage * frame,const ColorMapObject * cmap)229 static void drawFrame(SkBitmap* bm, const SavedImage* frame, const ColorMapObject* cmap)
230 {
231     int transparent = -1;
232 
233     for (int i = 0; i < frame->ExtensionBlockCount; ++i) {
234         ExtensionBlock* eb = frame->ExtensionBlocks + i;
235         if (eb->Function == GRAPHICS_EXT_FUNC_CODE &&
236             eb->ByteCount == 4) {
237             bool has_transparency = ((eb->Bytes[0] & 1) == 1);
238             if (has_transparency) {
239                 transparent = (unsigned char)eb->Bytes[3];
240             }
241         }
242     }
243 
244     if (frame->ImageDesc.ColorMap != nullptr) {
245         // use local color table
246         cmap = frame->ImageDesc.ColorMap;
247     }
248 
249     if (cmap == nullptr || cmap->ColorCount != (1 << cmap->BitsPerPixel)) {
250         ALOGD("bad colortable setup");
251         return;
252     }
253 
254 #if GIFLIB_MAJOR < 5
255     // before GIFLIB 5, de-interlacing wasn't done by library at load time
256     if (frame->ImageDesc.Interlace) {
257         blitInterlace(bm, frame, cmap, transparent);
258         return;
259     }
260 #endif
261 
262     blitNormal(bm, frame, cmap, transparent);
263 }
264 
checkIfWillBeCleared(const SavedImage * frame)265 static bool checkIfWillBeCleared(const SavedImage* frame)
266 {
267     for (int i = 0; i < frame->ExtensionBlockCount; ++i) {
268         ExtensionBlock* eb = frame->ExtensionBlocks + i;
269         if (eb->Function == GRAPHICS_EXT_FUNC_CODE &&
270             eb->ByteCount == 4) {
271             // check disposal method
272             int disposal = ((eb->Bytes[0] >> 2) & 7);
273             if (disposal == 2 || disposal == 3) {
274                 return true;
275             }
276         }
277     }
278     return false;
279 }
280 
getTransparencyAndDisposalMethod(const SavedImage * frame,bool * trans,int * disposal)281 static void getTransparencyAndDisposalMethod(const SavedImage* frame, bool* trans, int* disposal)
282 {
283     *trans = false;
284     *disposal = 0;
285     for (int i = 0; i < frame->ExtensionBlockCount; ++i) {
286         ExtensionBlock* eb = frame->ExtensionBlocks + i;
287         if (eb->Function == GRAPHICS_EXT_FUNC_CODE &&
288             eb->ByteCount == 4) {
289             *trans = ((eb->Bytes[0] & 1) == 1);
290             *disposal = ((eb->Bytes[0] >> 2) & 7);
291         }
292     }
293 }
294 
295 // return true if area of 'target' is completely covers area of 'covered'
checkIfCover(const SavedImage * target,const SavedImage * covered)296 static bool checkIfCover(const SavedImage* target, const SavedImage* covered)
297 {
298     if (target->ImageDesc.Left <= covered->ImageDesc.Left
299         && covered->ImageDesc.Left + covered->ImageDesc.Width <=
300                target->ImageDesc.Left + target->ImageDesc.Width
301         && target->ImageDesc.Top <= covered->ImageDesc.Top
302         && covered->ImageDesc.Top + covered->ImageDesc.Height <=
303                target->ImageDesc.Top + target->ImageDesc.Height) {
304         return true;
305     }
306     return false;
307 }
308 
disposeFrameIfNeeded(SkBitmap * bm,const SavedImage * cur,const SavedImage * next,SkBitmap * backup,SkColor color)309 static void disposeFrameIfNeeded(SkBitmap* bm, const SavedImage* cur, const SavedImage* next,
310                                  SkBitmap* backup, SkColor color)
311 {
312     // We can skip disposal process if next frame is not transparent
313     // and completely covers current area
314     bool curTrans;
315     int curDisposal;
316     getTransparencyAndDisposalMethod(cur, &curTrans, &curDisposal);
317     bool nextTrans;
318     int nextDisposal;
319     getTransparencyAndDisposalMethod(next, &nextTrans, &nextDisposal);
320     if ((curDisposal == 2 || curDisposal == 3)
321         && (nextTrans || !checkIfCover(next, cur))) {
322         switch (curDisposal) {
323         // restore to background color
324         // -> 'background' means background under this image.
325         case 2:
326             fillRect(bm, cur->ImageDesc.Left, cur->ImageDesc.Top,
327                      cur->ImageDesc.Width, cur->ImageDesc.Height,
328                      color);
329             break;
330 
331         // restore to previous
332         case 3:
333             bm->swap(*backup);
334             break;
335         }
336     }
337 
338     // Save current image if next frame's disposal method == 3
339     if (nextDisposal == 3) {
340         const uint32_t* src = bm->getAddr32(0, 0);
341         uint32_t* dst = backup->getAddr32(0, 0);
342         int cnt = bm->width() * bm->height();
343         memcpy(dst, src, cnt*sizeof(uint32_t));
344     }
345 }
346 
onGetBitmap(SkBitmap * bm)347 bool GIFMovie::onGetBitmap(SkBitmap* bm)
348 {
349     const GifFileType* gif = fGIF;
350     if (nullptr == gif)
351         return false;
352 
353     if (gif->ImageCount < 1) {
354         return false;
355     }
356 
357     const int width = gif->SWidth;
358     const int height = gif->SHeight;
359     if (width <= 0 || height <= 0) {
360         return false;
361     }
362 
363     // no need to draw
364     if (fLastDrawIndex >= 0 && fLastDrawIndex == fCurrIndex) {
365         return true;
366     }
367 
368     int startIndex = fLastDrawIndex + 1;
369     if (fLastDrawIndex < 0 || !bm->readyToDraw()) {
370         // first time
371 
372         startIndex = 0;
373 
374         // create bitmap
375         if (!bm->tryAllocN32Pixels(width, height)) {
376             return false;
377         }
378         // create bitmap for backup
379         if (!fBackup.tryAllocN32Pixels(width, height)) {
380             return false;
381         }
382     } else if (startIndex > fCurrIndex) {
383         // rewind to 1st frame for repeat
384         startIndex = 0;
385     }
386 
387     int lastIndex = fCurrIndex;
388     if (lastIndex < 0) {
389         // first time
390         lastIndex = 0;
391     } else if (lastIndex > fGIF->ImageCount - 1) {
392         // this block must not be reached.
393         lastIndex = fGIF->ImageCount - 1;
394     }
395 
396     SkColor bgColor = SkPackARGB32(0, 0, 0, 0);
397     if (gif->SColorMap != nullptr && gif->SBackGroundColor < gif->SColorMap->ColorCount) {
398         const GifColorType& col = gif->SColorMap->Colors[gif->SBackGroundColor];
399         bgColor = SkColorSetARGB(0xFF, col.Red, col.Green, col.Blue);
400     }
401 
402     // draw each frames - not intelligent way
403     for (int i = startIndex; i <= lastIndex; i++) {
404         const SavedImage* cur = &fGIF->SavedImages[i];
405         if (i == 0) {
406             bool trans;
407             int disposal;
408             getTransparencyAndDisposalMethod(cur, &trans, &disposal);
409             if (!trans && gif->SColorMap != nullptr) {
410                 fPaintingColor = bgColor;
411             } else {
412                 fPaintingColor = SkColorSetARGB(0, 0, 0, 0);
413             }
414 
415             bm->eraseColor(fPaintingColor);
416             fBackup.eraseColor(fPaintingColor);
417         } else {
418             // Dispose previous frame before move to next frame.
419             const SavedImage* prev = &fGIF->SavedImages[i-1];
420             disposeFrameIfNeeded(bm, prev, cur, &fBackup, fPaintingColor);
421         }
422 
423         // Draw frame
424         // We can skip this process if this index is not last and disposal
425         // method == 2 or method == 3
426         if (i == lastIndex || !checkIfWillBeCleared(cur)) {
427             drawFrame(bm, cur, gif->SColorMap);
428         }
429     }
430 
431     // save index
432     fLastDrawIndex = lastIndex;
433     return true;
434 }
435 
436 ///////////////////////////////////////////////////////////////////////////////
437 
DecodeStream(SkStreamRewindable * stream)438 Movie* Movie::DecodeStream(SkStreamRewindable* stream) {
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 ||
442                 memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
443                 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) {
444             // must rewind here, since our construct wants to re-read the data
445             stream->rewind();
446             return new GIFMovie(stream);
447         }
448     }
449     return nullptr;
450 }
451