msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 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 | #include "SkCodec_libgif.h" |
| 9 | #include "SkCodecPriv.h" |
| 10 | #include "SkColorPriv.h" |
| 11 | #include "SkColorTable.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 12 | #include "SkStream.h" |
| 13 | #include "SkSwizzler.h" |
| 14 | #include "SkUtils.h" |
| 15 | |
| 16 | /* |
| 17 | * Checks the start of the stream to see if the image is a gif |
| 18 | */ |
| 19 | bool SkGifCodec::IsGif(SkStream* stream) { |
| 20 | char buf[GIF_STAMP_LEN]; |
| 21 | if (stream->read(buf, GIF_STAMP_LEN) == GIF_STAMP_LEN) { |
| 22 | if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 || |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 23 | memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 || |
| 24 | memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) |
| 25 | { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 26 | return true; |
| 27 | } |
| 28 | } |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | /* |
| 33 | * Warning reporting function |
| 34 | */ |
| 35 | static void gif_warning(const char* msg) { |
| 36 | SkCodecPrintf("Gif Warning: %s\n", msg); |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * Error function |
| 41 | */ |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 42 | static SkCodec::Result gif_error(const char* msg, SkCodec::Result result = SkCodec::kInvalidInput) { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 43 | SkCodecPrintf("Gif Error: %s\n", msg); |
| 44 | return result; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /* |
| 49 | * Read function that will be passed to gif_lib |
| 50 | */ |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 51 | static int32_t read_bytes_callback(GifFileType* fileType, GifByteType* out, int32_t size) { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 52 | SkStream* stream = (SkStream*) fileType->UserData; |
| 53 | return (int32_t) stream->read(out, size); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Open the gif file |
| 58 | */ |
| 59 | static GifFileType* open_gif(SkStream* stream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 60 | return DGifOpen(stream, read_bytes_callback, nullptr); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 61 | } |
| 62 | |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 63 | /* |
| 64 | * Check if a there is an index of the color table for a transparent pixel |
| 65 | */ |
| 66 | static uint32_t find_trans_index(const SavedImage& image) { |
| 67 | // If there is a transparent index specified, it will be contained in an |
| 68 | // extension block. We will loop through extension blocks in reverse order |
| 69 | // to check the most recent extension blocks first. |
| 70 | for (int32_t i = image.ExtensionBlockCount - 1; i >= 0; i--) { |
| 71 | // Get an extension block |
| 72 | const ExtensionBlock& extBlock = image.ExtensionBlocks[i]; |
| 73 | |
| 74 | // Specifically, we need to check for a graphics control extension, |
| 75 | // which may contain transparency information. Also, note that a valid |
| 76 | // graphics control extension is always four bytes. The fourth byte |
| 77 | // is the transparent index (if it exists), so we need at least four |
| 78 | // bytes. |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 79 | if (GRAPHICS_EXT_FUNC_CODE == extBlock.Function && extBlock.ByteCount >= 4) { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 80 | // Check the transparent color flag which indicates whether a |
| 81 | // transparent index exists. It is the least significant bit of |
| 82 | // the first byte of the extension block. |
| 83 | if (1 == (extBlock.Bytes[0] & 1)) { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 84 | // Use uint32_t to prevent sign extending |
| 85 | return extBlock.Bytes[3]; |
| 86 | } |
| 87 | |
| 88 | // There should only be one graphics control extension for the image frame |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Use maximum unsigned int (surely an invalid index) to indicate that a valid |
| 94 | // index was not found. |
| 95 | return SK_MaxU32; |
| 96 | } |
| 97 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 98 | inline uint32_t ceil_div(uint32_t a, uint32_t b) { |
msarett | d02b99f | 2015-08-28 07:36:55 -0700 | [diff] [blame] | 99 | return (a + b - 1) / b; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Gets the output row corresponding to the encoded row for interlaced gifs |
| 104 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 105 | inline uint32_t get_output_row_interlaced(uint32_t encodedRow, uint32_t height) { |
msarett | d02b99f | 2015-08-28 07:36:55 -0700 | [diff] [blame] | 106 | SkASSERT(encodedRow < height); |
| 107 | // First pass |
| 108 | if (encodedRow * 8 < height) { |
| 109 | return encodedRow * 8; |
| 110 | } |
| 111 | // Second pass |
| 112 | if (encodedRow * 4 < height) { |
| 113 | return 4 + 8 * (encodedRow - ceil_div(height, 8)); |
| 114 | } |
| 115 | // Third pass |
| 116 | if (encodedRow * 2 < height) { |
| 117 | return 2 + 4 * (encodedRow - ceil_div(height, 4)); |
| 118 | } |
| 119 | // Fourth pass |
| 120 | return 1 + 2 * (encodedRow - ceil_div(height, 2)); |
| 121 | } |
| 122 | |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 123 | /* |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 124 | * This function cleans up the gif object after the decode completes |
| 125 | * It is used in a SkAutoTCallIProc template |
| 126 | */ |
| 127 | void SkGifCodec::CloseGif(GifFileType* gif) { |
| 128 | DGifCloseFile(gif, NULL); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * This function free extension data that has been saved to assist the image |
| 133 | * decoder |
| 134 | */ |
| 135 | void SkGifCodec::FreeExtension(SavedImage* image) { |
| 136 | if (NULL != image->ExtensionBlocks) { |
| 137 | GifFreeExtensions(&image->ExtensionBlockCount, &image->ExtensionBlocks); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 142 | * Read enough of the stream to initialize the SkGifCodec. |
| 143 | * Returns a bool representing success or failure. |
| 144 | * |
| 145 | * @param codecOut |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 146 | * If it returned true, and codecOut was not nullptr, |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 147 | * codecOut will be set to a new SkGifCodec. |
| 148 | * |
| 149 | * @param gifOut |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 150 | * If it returned true, and codecOut was nullptr, |
| 151 | * gifOut must be non-nullptr and gifOut will be set to a new |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 152 | * GifFileType pointer. |
| 153 | * |
| 154 | * @param stream |
| 155 | * Deleted on failure. |
| 156 | * codecOut will take ownership of it in the case where we created a codec. |
| 157 | * Ownership is unchanged when we returned a gifOut. |
| 158 | * |
| 159 | */ |
| 160 | bool SkGifCodec::ReadHeader(SkStream* stream, SkCodec** codecOut, GifFileType** gifOut) { |
| 161 | SkAutoTDelete<SkStream> streamDeleter(stream); |
| 162 | |
| 163 | // Read gif header, logical screen descriptor, and global color table |
| 164 | SkAutoTCallVProc<GifFileType, CloseGif> gif(open_gif(stream)); |
| 165 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 166 | if (nullptr == gif) { |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 167 | gif_error("DGifOpen failed.\n"); |
| 168 | return false; |
| 169 | } |
| 170 | |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 171 | // Read through gif extensions to get to the image data. Set the |
| 172 | // transparent index based on the extension data. |
| 173 | uint32_t transIndex; |
| 174 | SkCodec::Result result = ReadUpToFirstImage(gif, &transIndex); |
| 175 | if (kSuccess != result){ |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | // Read the image descriptor |
| 180 | if (GIF_ERROR == DGifGetImageDesc(gif)) { |
| 181 | return false; |
| 182 | } |
| 183 | // If reading the image descriptor is successful, the image count will be |
| 184 | // incremented. |
| 185 | SkASSERT(gif->ImageCount >= 1); |
| 186 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 187 | if (nullptr != codecOut) { |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 188 | SkISize size; |
| 189 | SkIRect frameRect; |
| 190 | if (!GetDimensions(gif, &size, &frameRect)) { |
| 191 | gif_error("Invalid gif size.\n"); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 192 | } |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 193 | bool frameIsSubset = (size != frameRect.size()); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 194 | |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 195 | // Determine the recommended alpha type. The transIndex might be valid if it less |
| 196 | // than 256. We are not certain that the index is valid until we process the color |
| 197 | // table, since some gifs have color tables with less than 256 colors. If |
| 198 | // there might be a valid transparent index, we must indicate that the image has |
| 199 | // alpha. |
| 200 | // In the case where we must support alpha, we have the option to set the |
| 201 | // suggested alpha type to kPremul or kUnpremul. Both are valid since the alpha |
| 202 | // component will always be 0xFF or the entire 32-bit pixel will be set to zero. |
| 203 | // We prefer kPremul because we support kPremul, and it is more efficient to use |
| 204 | // kPremul directly even when kUnpremul is supported. |
| 205 | SkAlphaType alphaType = (transIndex < 256) ? kPremul_SkAlphaType : kOpaque_SkAlphaType; |
| 206 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 207 | // Return the codec |
| 208 | // kIndex is the most natural color type for gifs, so we set this as |
| 209 | // the default. |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 210 | SkImageInfo imageInfo = SkImageInfo::Make(size.width(), size.height(), kIndex_8_SkColorType, |
| 211 | alphaType); |
| 212 | *codecOut = new SkGifCodec(imageInfo, streamDeleter.detach(), gif.detach(), transIndex, |
| 213 | frameRect, frameIsSubset); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 214 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 215 | SkASSERT(nullptr != gifOut); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 216 | streamDeleter.detach(); |
| 217 | *gifOut = gif.detach(); |
| 218 | } |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | /* |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 223 | * Assumes IsGif was called and returned true |
| 224 | * Creates a gif decoder |
| 225 | * Reads enough of the stream to determine the image format |
| 226 | */ |
| 227 | SkCodec* SkGifCodec::NewFromStream(SkStream* stream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 228 | SkCodec* codec = nullptr; |
| 229 | if (ReadHeader(stream, &codec, nullptr)) { |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 230 | return codec; |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 231 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 232 | return nullptr; |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 233 | } |
| 234 | |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 235 | SkGifCodec::SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, GifFileType* gif, |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 236 | uint32_t transIndex, const SkIRect& frameRect, bool frameIsSubset) |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 237 | : INHERITED(srcInfo, stream) |
| 238 | , fGif(gif) |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 239 | , fSrcBuffer(new uint8_t[this->getInfo().width()]) |
| 240 | // If it is valid, fTransIndex will be used to set fFillIndex. We don't know if |
| 241 | // fTransIndex is valid until we process the color table, since fTransIndex may |
| 242 | // be greater than the size of the color table. |
| 243 | , fTransIndex(transIndex) |
| 244 | // Default fFillIndex is 0. We will overwrite this if fTransIndex is valid, or if |
| 245 | // there is a valid background color. |
| 246 | , fFillIndex(0) |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 247 | , fFrameRect(frameRect) |
| 248 | , fFrameIsSubset(frameIsSubset) |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 249 | , fColorTable(NULL) |
| 250 | , fSwizzler(NULL) |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 251 | {} |
| 252 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 253 | bool SkGifCodec::onRewind() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 254 | GifFileType* gifOut = nullptr; |
| 255 | if (!ReadHeader(this->stream(), nullptr, &gifOut)) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 256 | return false; |
| 257 | } |
| 258 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 259 | SkASSERT(nullptr != gifOut); |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 260 | fGif.reset(gifOut); |
| 261 | return true; |
| 262 | } |
| 263 | |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 264 | SkCodec::Result SkGifCodec::ReadUpToFirstImage(GifFileType* gif, uint32_t* transIndex) { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 265 | // Use this as a container to hold information about any gif extension |
| 266 | // blocks. This generally stores transparency and animation instructions. |
| 267 | SavedImage saveExt; |
| 268 | SkAutoTCallVProc<SavedImage, FreeExtension> autoFreeExt(&saveExt); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 269 | saveExt.ExtensionBlocks = nullptr; |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 270 | saveExt.ExtensionBlockCount = 0; |
| 271 | GifByteType* extData; |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 272 | int32_t extFunction; |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 273 | |
| 274 | // We will loop over components of gif images until we find an image. Once |
| 275 | // we find an image, we will decode and return it. While many gif files |
| 276 | // contain more than one image, we will simply decode the first image. |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 277 | GifRecordType recordType; |
| 278 | do { |
| 279 | // Get the current record type |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 280 | if (GIF_ERROR == DGifGetRecordType(gif, &recordType)) { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 281 | return gif_error("DGifGetRecordType failed.\n", kInvalidInput); |
| 282 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 283 | switch (recordType) { |
| 284 | case IMAGE_DESC_RECORD_TYPE: { |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 285 | *transIndex = find_trans_index(saveExt); |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 286 | |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 287 | // FIXME: Gif files may have multiple images stored in a single |
| 288 | // file. This is most commonly used to enable |
| 289 | // animations. Since we are leaving animated gifs as a |
| 290 | // TODO, we will return kSuccess after decoding the |
| 291 | // first image in the file. This is the same behavior |
| 292 | // as SkImageDecoder_libgif. |
| 293 | // |
| 294 | // Most times this works pretty well, but sometimes it |
| 295 | // doesn't. For example, I have an animated test image |
| 296 | // where the first image in the file is 1x1, but the |
| 297 | // subsequent images are meaningful. This currently |
| 298 | // displays the 1x1 image, which is not ideal. Right |
| 299 | // now I am leaving this as an issue that will be |
| 300 | // addressed when we implement animated gifs. |
| 301 | // |
| 302 | // It is also possible (not explicitly disallowed in the |
| 303 | // specification) that gif files provide multiple |
| 304 | // images in a single file that are all meant to be |
| 305 | // displayed in the same frame together. I will |
| 306 | // currently leave this unimplemented until I find a |
| 307 | // test case that expects this behavior. |
| 308 | return kSuccess; |
| 309 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 310 | // Extensions are used to specify special properties of the image |
| 311 | // such as transparency or animation. |
| 312 | case EXTENSION_RECORD_TYPE: |
| 313 | // Read extension data |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 314 | if (GIF_ERROR == DGifGetExtension(gif, &extFunction, &extData)) { |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 315 | return gif_error("Could not get extension.\n", kIncompleteInput); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // Create an extension block with our data |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 319 | while (nullptr != extData) { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 320 | // Add a single block |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 321 | if (GIF_ERROR == GifAddExtensionBlock(&saveExt.ExtensionBlockCount, |
| 322 | &saveExt.ExtensionBlocks, |
| 323 | extFunction, extData[0], &extData[1])) |
| 324 | { |
| 325 | return gif_error("Could not add extension block.\n", kIncompleteInput); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 326 | } |
| 327 | // Move to the next block |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 328 | if (GIF_ERROR == DGifGetExtensionNext(gif, &extData)) { |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 329 | return gif_error("Could not get next extension.\n", kIncompleteInput); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 330 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 331 | } |
| 332 | break; |
| 333 | |
| 334 | // Signals the end of the gif file |
| 335 | case TERMINATE_RECORD_TYPE: |
| 336 | break; |
| 337 | |
| 338 | default: |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 339 | // DGifGetRecordType returns an error if the record type does |
| 340 | // not match one of the above cases. This should not be |
| 341 | // reached. |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 342 | SkASSERT(false); |
| 343 | break; |
| 344 | } |
| 345 | } while (TERMINATE_RECORD_TYPE != recordType); |
| 346 | |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 347 | return gif_error("Could not find any images to decode in gif file.\n", kInvalidInput); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 348 | } |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 349 | |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 350 | bool SkGifCodec::GetDimensions(GifFileType* gif, SkISize* size, SkIRect* frameRect) { |
| 351 | // Get the encoded dimension values |
| 352 | SavedImage* image = &gif->SavedImages[gif->ImageCount - 1]; |
| 353 | const GifImageDesc& desc = image->ImageDesc; |
| 354 | int frameLeft = desc.Left; |
| 355 | int frameTop = desc.Top; |
| 356 | int frameWidth = desc.Width; |
| 357 | int frameHeight = desc.Height; |
| 358 | int width = gif->SWidth; |
| 359 | int height = gif->SHeight; |
| 360 | |
| 361 | // Ensure that the decode dimensions are large enough to contain the frame |
| 362 | width = SkTMax(width, frameWidth + frameLeft); |
| 363 | height = SkTMax(height, frameHeight + frameTop); |
| 364 | |
| 365 | // All of these dimensions should be positive, as they are encoded as unsigned 16-bit integers. |
| 366 | // It is unclear why giflib casts them to ints. We will go ahead and check that they are |
| 367 | // in fact positive. |
| 368 | if (frameLeft < 0 || frameTop < 0 || frameWidth < 0 || frameHeight < 0 || width <= 0 || |
| 369 | height <= 0) { |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 373 | frameRect->setXYWH(frameLeft, frameTop, frameWidth, frameHeight); |
| 374 | size->set(width, height); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 375 | return true; |
| 376 | } |
| 377 | |
| 378 | void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr, |
| 379 | int* inputColorCount) { |
| 380 | // Set up our own color table |
| 381 | const uint32_t maxColors = 256; |
| 382 | SkPMColor colorPtr[256]; |
| 383 | if (NULL != inputColorCount) { |
| 384 | // We set the number of colors to maxColors in order to ensure |
| 385 | // safe memory accesses. Otherwise, an invalid pixel could |
| 386 | // access memory outside of our color table array. |
| 387 | *inputColorCount = maxColors; |
| 388 | } |
| 389 | |
| 390 | // Get local color table |
| 391 | ColorMapObject* colorMap = fGif->Image.ColorMap; |
| 392 | // If there is no local color table, use the global color table |
| 393 | if (NULL == colorMap) { |
| 394 | colorMap = fGif->SColorMap; |
| 395 | } |
| 396 | |
| 397 | uint32_t colorCount = 0; |
| 398 | if (NULL != colorMap) { |
| 399 | colorCount = colorMap->ColorCount; |
| 400 | // giflib guarantees these properties |
| 401 | SkASSERT(colorCount == (unsigned) (1 << (colorMap->BitsPerPixel))); |
| 402 | SkASSERT(colorCount <= 256); |
| 403 | for (uint32_t i = 0; i < colorCount; i++) { |
| 404 | colorPtr[i] = SkPackARGB32(0xFF, colorMap->Colors[i].Red, |
| 405 | colorMap->Colors[i].Green, colorMap->Colors[i].Blue); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | // Gifs have the option to specify the color at a single index of the color |
| 410 | // table as transparent. If the transparent index is greater than the |
| 411 | // colorCount, we know that there is no valid transparent color in the color |
| 412 | // table. If there is not valid transparent index, we will try to use the |
| 413 | // backgroundIndex as the fill index. If the backgroundIndex is also not |
| 414 | // valid, we will let fFillIndex default to 0 (it is set to zero in the |
| 415 | // constructor). This behavior is not specified but matches |
| 416 | // SkImageDecoder_libgif. |
| 417 | uint32_t backgroundIndex = fGif->SBackGroundColor; |
| 418 | if (fTransIndex < colorCount) { |
| 419 | colorPtr[fTransIndex] = SK_ColorTRANSPARENT; |
| 420 | fFillIndex = fTransIndex; |
| 421 | } else if (backgroundIndex < colorCount) { |
| 422 | fFillIndex = backgroundIndex; |
| 423 | } |
| 424 | |
| 425 | // Fill in the color table for indices greater than color count. |
| 426 | // This allows for predictable, safe behavior. |
| 427 | for (uint32_t i = colorCount; i < maxColors; i++) { |
| 428 | colorPtr[i] = colorPtr[fFillIndex]; |
| 429 | } |
| 430 | |
| 431 | fColorTable.reset(new SkColorTable(colorPtr, maxColors)); |
| 432 | copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount); |
| 433 | } |
| 434 | |
| 435 | SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr, |
| 436 | int* inputColorCount, const Options& opts) { |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 437 | // Check for valid input parameters |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 438 | if (!conversion_possible(dstInfo, this->getInfo())) { |
| 439 | return gif_error("Cannot convert input type to output type.\n", |
| 440 | kInvalidConversion); |
| 441 | } |
| 442 | |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 443 | // Initialize color table and copy to the client if necessary |
| 444 | this->initializeColorTable(dstInfo, inputColorPtr, inputColorCount); |
| 445 | return kSuccess; |
| 446 | } |
| 447 | |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame^] | 448 | SkCodec::Result SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& opts) { |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 449 | const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame^] | 450 | fSwizzler.reset(SkSwizzler::CreateSwizzler(SkSwizzler::kIndex, colorPtr, dstInfo, opts)); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 451 | if (nullptr != fSwizzler.get()) { |
| 452 | return kSuccess; |
| 453 | } |
| 454 | return kUnimplemented; |
| 455 | } |
| 456 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 457 | bool SkGifCodec::readRow() { |
| 458 | return GIF_ERROR != DGifGetLine(fGif, fSrcBuffer.get(), fFrameRect.width()); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* |
| 462 | * Initiates the gif decode |
| 463 | */ |
| 464 | SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 465 | void* dst, size_t dstRowBytes, |
| 466 | const Options& opts, |
| 467 | SkPMColor* inputColorPtr, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 468 | int* inputColorCount, |
| 469 | int* rowsDecoded) { |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 470 | Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts); |
| 471 | if (kSuccess != result) { |
| 472 | return result; |
| 473 | } |
| 474 | |
| 475 | if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 476 | return gif_error("Scaling not supported.\n", kInvalidScale); |
| 477 | } |
| 478 | |
| 479 | // Initialize the swizzler |
| 480 | if (fFrameIsSubset) { |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 481 | const SkImageInfo subsetDstInfo = dstInfo.makeWH(fFrameRect.width(), fFrameRect.height()); |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame^] | 482 | if (kSuccess != this->initializeSwizzler(subsetDstInfo, opts)) { |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 483 | return gif_error("Could not initialize swizzler.\n", kUnimplemented); |
| 484 | } |
| 485 | |
| 486 | // Fill the background |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 487 | SkSampler::Fill(dstInfo, dst, dstRowBytes, |
| 488 | this->getFillValue(dstInfo.colorType(), dstInfo.alphaType()), |
| 489 | opts.fZeroInitialized); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 490 | |
| 491 | // Modify the dst pointer |
| 492 | const int32_t dstBytesPerPixel = SkColorTypeBytesPerPixel(dstInfo.colorType()); |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 493 | dst = SkTAddOffset<void*>(dst, dstRowBytes * fFrameRect.top() + |
| 494 | dstBytesPerPixel * fFrameRect.left()); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 495 | } else { |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame^] | 496 | if (kSuccess != this->initializeSwizzler(dstInfo, opts)) { |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 497 | return gif_error("Could not initialize swizzler.\n", kUnimplemented); |
| 498 | } |
| 499 | } |
| 500 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 501 | // Iterate over rows of the input |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 502 | uint32_t height = fFrameRect.height(); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 503 | for (uint32_t y = 0; y < height; y++) { |
| 504 | if (!this->readRow()) { |
| 505 | *rowsDecoded = y; |
| 506 | return gif_error("Could not decode line.\n", kIncompleteInput); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 507 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 508 | void* dstRow = SkTAddOffset<void>(dst, dstRowBytes * this->outputScanline(y)); |
| 509 | fSwizzler->swizzle(dstRow, fSrcBuffer.get()); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 510 | } |
| 511 | return kSuccess; |
| 512 | } |
| 513 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 514 | // FIXME: This is similar to the implementation for bmp and png. Can we share more code or |
| 515 | // possibly make this non-virtual? |
| 516 | uint32_t SkGifCodec::onGetFillValue(SkColorType colorType, SkAlphaType alphaType) const { |
| 517 | const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
| 518 | return get_color_table_fill_value(colorType, colorPtr, fFillIndex); |
| 519 | } |
| 520 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 521 | SkCodec::Result SkGifCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, |
| 522 | const SkCodec::Options& opts, SkPMColor inputColorPtr[], int* inputColorCount) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 523 | |
| 524 | Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, this->options()); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 525 | if (kSuccess != result) { |
| 526 | return result; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 527 | } |
| 528 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 529 | // Initialize the swizzler |
| 530 | if (fFrameIsSubset) { |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 531 | const SkImageInfo subsetDstInfo = dstInfo.makeWH(fFrameRect.width(), fFrameRect.height()); |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame^] | 532 | if (kSuccess != this->initializeSwizzler(subsetDstInfo, opts)) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 533 | return gif_error("Could not initialize swizzler.\n", kUnimplemented); |
| 534 | } |
| 535 | } else { |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame^] | 536 | if (kSuccess != this->initializeSwizzler(dstInfo, opts)) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 537 | return gif_error("Could not initialize swizzler.\n", kUnimplemented); |
| 538 | } |
| 539 | } |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 540 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 541 | return kSuccess; |
| 542 | } |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 543 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 544 | int SkGifCodec::onGetScanlines(void* dst, int count, size_t rowBytes) { |
| 545 | int rowsBeforeFrame = 0; |
| 546 | int rowsAfterFrame = 0; |
| 547 | int rowsInFrame = count; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 548 | if (fFrameIsSubset) { |
| 549 | // Fill the requested rows |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 550 | SkImageInfo fillInfo = this->dstInfo().makeWH(this->dstInfo().width(), count); |
| 551 | uint32_t fillValue = this->onGetFillValue(this->dstInfo().colorType(), |
| 552 | this->dstInfo().alphaType()); |
| 553 | SkSampler::Fill(fillInfo, dst, rowBytes, fillValue, this->options().fZeroInitialized); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 554 | |
| 555 | // Do nothing for rows before the image frame |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 556 | rowsBeforeFrame = SkTMax(0, fFrameRect.top() - this->INHERITED::nextScanline()); |
| 557 | rowsInFrame = SkTMax(0, rowsInFrame - rowsBeforeFrame); |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 558 | dst = SkTAddOffset<void>(dst, rowBytes * rowsBeforeFrame); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 559 | |
| 560 | // Do nothing for rows after the image frame |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 561 | rowsAfterFrame = SkTMax(0, |
| 562 | this->INHERITED::nextScanline() + rowsInFrame - fFrameRect.bottom()); |
| 563 | rowsInFrame = SkTMax(0, rowsInFrame - rowsAfterFrame); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 564 | |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 565 | // Adjust dst pointer for left offset |
| 566 | int offset = SkColorTypeBytesPerPixel(this->dstInfo().colorType()) * fFrameRect.left(); |
| 567 | dst = SkTAddOffset<void>(dst, offset); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 568 | } |
| 569 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 570 | for (int i = 0; i < rowsInFrame; i++) { |
| 571 | if (!this->readRow()) { |
| 572 | return i + rowsBeforeFrame; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 573 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 574 | fSwizzler->swizzle(dst, fSrcBuffer.get()); |
| 575 | dst = SkTAddOffset<void>(dst, rowBytes); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 576 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 577 | |
| 578 | return count; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 579 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 580 | |
| 581 | SkCodec::SkScanlineOrder SkGifCodec::onGetScanlineOrder() const { |
| 582 | if (fGif->Image.Interlace) { |
| 583 | return kOutOfOrder_SkScanlineOrder; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 584 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 585 | return kTopDown_SkScanlineOrder; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 586 | } |
| 587 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 588 | int SkGifCodec::onOutputScanline(int inputScanline) const { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 589 | if (fGif->Image.Interlace) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 590 | if (inputScanline < fFrameRect.top() || inputScanline >= fFrameRect.bottom()) { |
| 591 | return inputScanline; |
msarett | 4aa02d8 | 2015-10-06 07:46:02 -0700 | [diff] [blame] | 592 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 593 | return get_output_row_interlaced(inputScanline - fFrameRect.top(), fFrameRect.height()); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 594 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 595 | return inputScanline; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 596 | } |