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