| OLD | NEW |
| (Empty) |
| 1 /* libs/graphics/images/SkImageDecoder_libgif.cpp | |
| 2 ** | |
| 3 ** Copyright 2006, The Android Open Source Project | |
| 4 ** | |
| 5 ** Licensed under the Apache License, Version 2.0 (the "License"); | |
| 6 ** you may not use this file except in compliance with the License. | |
| 7 ** You may obtain a copy of the License at | |
| 8 ** | |
| 9 ** http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 ** | |
| 11 ** Unless required by applicable law or agreed to in writing, software | |
| 12 ** distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 ** See the License for the specific language governing permissions and | |
| 15 ** limitations under the License. | |
| 16 */ | |
| 17 | |
| 18 #include "SkMovie.h" | |
| 19 #include "SkColor.h" | |
| 20 #include "SkColorPriv.h" | |
| 21 #include "SkStream.h" | |
| 22 #include "SkTemplates.h" | |
| 23 | |
| 24 #include "gif_lib.h" | |
| 25 | |
| 26 class SkGIFMovie : public SkMovie { | |
| 27 public: | |
| 28 // we do NOT hold onto the stream, so it is OK if it is on the | |
| 29 // stack of the caller | |
| 30 SkGIFMovie(SkStream* stream); | |
| 31 virtual ~SkGIFMovie(); | |
| 32 | |
| 33 protected: | |
| 34 virtual bool onGetInfo(Info*); | |
| 35 virtual bool onSetTime(SkMSec); | |
| 36 virtual bool onGetBitmap(SkBitmap*); | |
| 37 | |
| 38 private: | |
| 39 GifFileType* fGIF; | |
| 40 SavedImage* fCurrSavedImage; | |
| 41 }; | |
| 42 | |
| 43 SkMovie* SkMovie_GIF_StreamFactory(SkStream* stream) { | |
| 44 char buf[GIF_STAMP_LEN]; | |
| 45 if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) { | |
| 46 if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 || | |
| 47 memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 || | |
| 48 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) { | |
| 49 stream->rewind(); | |
| 50 return SkNEW_ARGS(SkGIFMovie, (stream)); | |
| 51 } | |
| 52 } | |
| 53 return NULL; | |
| 54 } | |
| 55 | |
| 56 SkMovie* SkMovie_GIF_MemoryFactory(const void* data, size_t length) { | |
| 57 if (length > GIF_STAMP_LEN && !memcmp(GIF_STAMP, data, GIF_STAMP_LEN)) { | |
| 58 SkMemoryStream stream(data, length); | |
| 59 return SkNEW_ARGS(SkGIFMovie, (&stream)); | |
| 60 } | |
| 61 return NULL; | |
| 62 } | |
| 63 | |
| 64 static int Decode(GifFileType* fileType, GifByteType* out, int size) { | |
| 65 SkStream* stream = (SkStream*) fileType->UserData; | |
| 66 return (int) stream->read(out, size); | |
| 67 } | |
| 68 | |
| 69 SkGIFMovie::SkGIFMovie(SkStream* stream) | |
| 70 { | |
| 71 fGIF = DGifOpen( stream, Decode ); | |
| 72 if (NULL == fGIF) | |
| 73 return; | |
| 74 | |
| 75 if (DGifSlurp(fGIF) != GIF_OK) | |
| 76 { | |
| 77 DGifCloseFile(fGIF); | |
| 78 fGIF = NULL; | |
| 79 } | |
| 80 fCurrSavedImage = NULL; | |
| 81 } | |
| 82 | |
| 83 SkGIFMovie::~SkGIFMovie() | |
| 84 { | |
| 85 if (fGIF) | |
| 86 DGifCloseFile(fGIF); | |
| 87 } | |
| 88 | |
| 89 static SkMSec savedimage_duration(const SavedImage* image) | |
| 90 { | |
| 91 for (int j = 0; j < image->ExtensionBlockCount; j++) | |
| 92 { | |
| 93 if (image->ExtensionBlocks[j].Function == GRAPHICS_EXT_FUNC_CODE) | |
| 94 { | |
| 95 int size = image->ExtensionBlocks[j].ByteCount; | |
| 96 SkASSERT(size >= 4); | |
| 97 const uint8_t* b = (const uint8_t*)image->ExtensionBlocks[j].Bytes; | |
| 98 return ((b[2] << 8) | b[1]) * 10; | |
| 99 } | |
| 100 } | |
| 101 return 0; | |
| 102 } | |
| 103 | |
| 104 bool SkGIFMovie::onGetInfo(Info* info) | |
| 105 { | |
| 106 if (NULL == fGIF) | |
| 107 return false; | |
| 108 | |
| 109 SkMSec dur = 0; | |
| 110 for (int i = 0; i < fGIF->ImageCount; i++) | |
| 111 dur += savedimage_duration(&fGIF->SavedImages[i]); | |
| 112 | |
| 113 info->fDuration = dur; | |
| 114 info->fWidth = fGIF->SWidth; | |
| 115 info->fHeight = fGIF->SHeight; | |
| 116 info->fIsOpaque = false; // how to compute? | |
| 117 return true; | |
| 118 } | |
| 119 | |
| 120 bool SkGIFMovie::onSetTime(SkMSec time) | |
| 121 { | |
| 122 if (NULL == fGIF) | |
| 123 return false; | |
| 124 | |
| 125 SkMSec dur = 0; | |
| 126 for (int i = 0; i < fGIF->ImageCount; i++) | |
| 127 { | |
| 128 dur += savedimage_duration(&fGIF->SavedImages[i]); | |
| 129 if (dur >= time) | |
| 130 { | |
| 131 SavedImage* prev = fCurrSavedImage; | |
| 132 fCurrSavedImage = &fGIF->SavedImages[i]; | |
| 133 return prev != fCurrSavedImage; | |
| 134 } | |
| 135 } | |
| 136 fCurrSavedImage = &fGIF->SavedImages[fGIF->ImageCount - 1]; | |
| 137 return true; | |
| 138 } | |
| 139 | |
| 140 bool SkGIFMovie::onGetBitmap(SkBitmap* bm) | |
| 141 { | |
| 142 GifFileType* gif = fGIF; | |
| 143 if (NULL == gif) | |
| 144 return false; | |
| 145 | |
| 146 // should we check for the Image cmap or the global (SColorMap) first? | |
| 147 ColorMapObject* cmap = gif->SColorMap; | |
| 148 if (cmap == NULL) | |
| 149 cmap = gif->Image.ColorMap; | |
| 150 | |
| 151 if (cmap == NULL || gif->ImageCount < 1 || cmap->ColorCount != (1 << cmap->B
itsPerPixel)) | |
| 152 { | |
| 153 SkASSERT(!"bad colortable setup"); | |
| 154 return false; | |
| 155 } | |
| 156 | |
| 157 const int width = gif->SWidth; | |
| 158 const int height = gif->SHeight; | |
| 159 if (width <= 0 || height <= 0) { | |
| 160 return false; | |
| 161 } | |
| 162 | |
| 163 SavedImage* gif_image = fCurrSavedImage; | |
| 164 SkBitmap::Config config = SkBitmap::kIndex8_Config; | |
| 165 | |
| 166 SkColorTable* colorTable = SkNEW_ARGS(SkColorTable, (cmap->ColorCount)); | |
| 167 SkAutoUnref aur(colorTable); | |
| 168 | |
| 169 bm->setConfig(config, width, height, 0); | |
| 170 if (!bm->allocPixels(colorTable)) { | |
| 171 return false; | |
| 172 } | |
| 173 | |
| 174 int transparent = -1; | |
| 175 for (int i = 0; i < gif_image->ExtensionBlockCount; ++i) { | |
| 176 ExtensionBlock* eb = gif_image->ExtensionBlocks + i; | |
| 177 if (eb->Function == 0xF9 && | |
| 178 eb->ByteCount == 4) { | |
| 179 bool has_transparency = ((eb->Bytes[0] & 1) == 1); | |
| 180 if (has_transparency) { | |
| 181 transparent = (unsigned char)eb->Bytes[3]; | |
| 182 } | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 SkPMColor* colorPtr = colorTable->lockColors(); | |
| 187 | |
| 188 if (transparent >= 0) | |
| 189 memset(colorPtr, 0, cmap->ColorCount * 4); | |
| 190 else | |
| 191 colorTable->setFlags(colorTable->getFlags() | SkColorTable::kColorsAreOp
aque_Flag); | |
| 192 | |
| 193 for (int index = 0; index < cmap->ColorCount; index++) | |
| 194 { | |
| 195 if (transparent != index) | |
| 196 colorPtr[index] = SkPackARGB32(0xFF, cmap->Colors[index].Red, | |
| 197 cmap->Colors[index].Green, cmap->Colors[index].Blue); | |
| 198 } | |
| 199 colorTable->unlockColors(true); | |
| 200 | |
| 201 unsigned char* in = (unsigned char*)gif_image->RasterBits; | |
| 202 unsigned char* out = bm->getAddr8(0, 0); | |
| 203 if (gif->Image.Interlace) { | |
| 204 | |
| 205 // deinterlace | |
| 206 int row; | |
| 207 // group 1 - every 8th row, starting with row 0 | |
| 208 for (row = 0; row < height; row += 8) { | |
| 209 memcpy(out + width * row, in, width); | |
| 210 in += width; | |
| 211 } | |
| 212 | |
| 213 // group 2 - every 8th row, starting with row 4 | |
| 214 for (row = 4; row < height; row += 8) { | |
| 215 memcpy(out + width * row, in, width); | |
| 216 in += width; | |
| 217 } | |
| 218 | |
| 219 // group 3 - every 4th row, starting with row 2 | |
| 220 for (row = 2; row < height; row += 4) { | |
| 221 memcpy(out + width * row, in, width); | |
| 222 in += width; | |
| 223 } | |
| 224 | |
| 225 for (row = 1; row < height; row += 2) { | |
| 226 memcpy(out + width * row, in, width); | |
| 227 in += width; | |
| 228 } | |
| 229 | |
| 230 } else { | |
| 231 memcpy(out, in, width * height); | |
| 232 } | |
| 233 return true; | |
| 234 } | |
| OLD | NEW |