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