scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 1 | /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* ***** BEGIN LICENSE BLOCK ***** |
| 3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 4 | * |
| 5 | * The contents of this file are subject to the Mozilla Public License Version |
| 6 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * http://www.mozilla.org/MPL/ |
| 9 | * |
| 10 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 12 | * for the specific language governing rights and limitations under the |
| 13 | * License. |
| 14 | * |
| 15 | * The Original Code is mozilla.org code. |
| 16 | * |
| 17 | * The Initial Developer of the Original Code is |
| 18 | * Netscape Communications Corporation. |
| 19 | * Portions created by the Initial Developer are Copyright (C) 1998 |
| 20 | * the Initial Developer. All Rights Reserved. |
| 21 | * |
| 22 | * Contributor(s): |
| 23 | * Chris Saari <saari@netscape.com> |
| 24 | * Apple Computer |
| 25 | * |
| 26 | * Alternatively, the contents of this file may be used under the terms of |
| 27 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 28 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 29 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 30 | * of those above. If you wish to allow use of your version of this file only |
| 31 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 32 | * use your version of this file under the terms of the MPL, indicate your |
| 33 | * decision by deleting the provisions above and replace them with the notice |
| 34 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 35 | * the provisions above, a recipient may use your version of this file under |
| 36 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 37 | * |
| 38 | * ***** END LICENSE BLOCK ***** */ |
| 39 | |
| 40 | /* |
| 41 | The Graphics Interchange Format(c) is the copyright property of CompuServe |
| 42 | Incorporated. Only CompuServe Incorporated is authorized to define, redefine, |
| 43 | enhance, alter, modify or change in any way the definition of the format. |
| 44 | |
| 45 | CompuServe Incorporated hereby grants a limited, non-exclusive, royalty-free |
| 46 | license for the use of the Graphics Interchange Format(sm) in computer |
| 47 | software; computer software utilizing GIF(sm) must acknowledge ownership of the |
| 48 | Graphics Interchange Format and its Service Mark by CompuServe Incorporated, in |
| 49 | User and Technical Documentation. Computer software utilizing GIF, which is |
| 50 | distributed or may be distributed without User or Technical Documentation must |
| 51 | display to the screen or printer a message acknowledging ownership of the |
| 52 | Graphics Interchange Format and the Service Mark by CompuServe Incorporated; in |
| 53 | this case, the acknowledgement may be displayed in an opening screen or leading |
| 54 | banner, or a closing screen or trailing banner. A message such as the following |
| 55 | may be used: |
| 56 | |
| 57 | "The Graphics Interchange Format(c) is the Copyright property of |
| 58 | CompuServe Incorporated. GIF(sm) is a Service Mark property of |
| 59 | CompuServe Incorporated." |
| 60 | |
| 61 | For further information, please contact : |
| 62 | |
| 63 | CompuServe Incorporated |
| 64 | Graphics Technology Department |
| 65 | 5000 Arlington Center Boulevard |
| 66 | Columbus, Ohio 43220 |
| 67 | U. S. A. |
| 68 | |
| 69 | CompuServe Incorporated maintains a mailing list with all those individuals and |
| 70 | organizations who wish to receive copies of this document when it is corrected |
| 71 | or revised. This service is offered free of charge; please provide us with your |
| 72 | mailing address. |
| 73 | */ |
| 74 | |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 75 | #include "SkGifImageReader.h" |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 76 | #include "SkColorPriv.h" |
| 77 | #include "SkGifCodec.h" |
| 78 | |
| 79 | #include <algorithm> |
| 80 | #include <string.h> |
| 81 | |
| 82 | |
| 83 | // GETN(n, s) requests at least 'n' bytes available from 'q', at start of state 's'. |
| 84 | // |
| 85 | // Note, the hold will never need to be bigger than 256 bytes to gather up in the hold, |
| 86 | // as each GIF block (except colormaps) can never be bigger than 256 bytes. |
| 87 | // Colormaps are directly copied in the resp. global_colormap or dynamically allocated local_colormap. |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 88 | // So a fixed buffer in SkGifImageReader is good enough. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 89 | // This buffer is only needed to copy left-over data from one GifWrite call to the next |
| 90 | #define GETN(n, s) \ |
| 91 | do { \ |
| 92 | m_bytesToConsume = (n); \ |
| 93 | m_state = (s); \ |
| 94 | } while (0) |
| 95 | |
| 96 | // Get a 16-bit value stored in little-endian format. |
| 97 | #define GETINT16(p) ((p)[1]<<8|(p)[0]) |
| 98 | |
| 99 | // Send the data to the display front-end. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 100 | bool SkGIFLZWContext::outputRow(const unsigned char* rowBegin) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 101 | { |
| 102 | int drowStart = irow; |
| 103 | int drowEnd = irow; |
| 104 | |
| 105 | // Haeberli-inspired hack for interlaced GIFs: Replicate lines while |
| 106 | // displaying to diminish the "venetian-blind" effect as the image is |
| 107 | // loaded. Adjust pixel vertical positions to avoid the appearance of the |
| 108 | // image crawling up the screen as successive passes are drawn. |
| 109 | if (m_frameContext->progressiveDisplay() && m_frameContext->interlaced() && ipass < 4) { |
| 110 | unsigned rowDup = 0; |
| 111 | unsigned rowShift = 0; |
| 112 | |
| 113 | switch (ipass) { |
| 114 | case 1: |
| 115 | rowDup = 7; |
| 116 | rowShift = 3; |
| 117 | break; |
| 118 | case 2: |
| 119 | rowDup = 3; |
| 120 | rowShift = 1; |
| 121 | break; |
| 122 | case 3: |
| 123 | rowDup = 1; |
| 124 | rowShift = 0; |
| 125 | break; |
| 126 | default: |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | drowStart -= rowShift; |
| 131 | drowEnd = drowStart + rowDup; |
| 132 | |
| 133 | // Extend if bottom edge isn't covered because of the shift upward. |
| 134 | if (((m_frameContext->height() - 1) - drowEnd) <= rowShift) |
| 135 | drowEnd = m_frameContext->height() - 1; |
| 136 | |
| 137 | // Clamp first and last rows to upper and lower edge of image. |
| 138 | if (drowStart < 0) |
| 139 | drowStart = 0; |
| 140 | |
| 141 | if ((unsigned)drowEnd >= m_frameContext->height()) |
| 142 | drowEnd = m_frameContext->height() - 1; |
| 143 | } |
| 144 | |
| 145 | // Protect against too much image data. |
| 146 | if ((unsigned)drowStart >= m_frameContext->height()) |
| 147 | return true; |
| 148 | |
| 149 | // CALLBACK: Let the client know we have decoded a row. |
Matt Sarett | 4ef986d | 2016-11-03 14:52:28 -0400 | [diff] [blame] | 150 | const bool writeTransparentPixels = (SkCodec::kNone == m_frameContext->getRequiredFrame()); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 151 | if (!m_client->haveDecodedRow(m_frameContext->frameId(), rowBegin, |
scroggo | 1285f41 | 2016-10-26 13:48:03 -0700 | [diff] [blame] | 152 | drowStart, drowEnd - drowStart + 1, writeTransparentPixels)) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 153 | return false; |
| 154 | |
| 155 | if (!m_frameContext->interlaced()) |
| 156 | irow++; |
| 157 | else { |
| 158 | do { |
| 159 | switch (ipass) { |
| 160 | case 1: |
| 161 | irow += 8; |
| 162 | if (irow >= m_frameContext->height()) { |
| 163 | ipass++; |
| 164 | irow = 4; |
| 165 | } |
| 166 | break; |
| 167 | |
| 168 | case 2: |
| 169 | irow += 8; |
| 170 | if (irow >= m_frameContext->height()) { |
| 171 | ipass++; |
| 172 | irow = 2; |
| 173 | } |
| 174 | break; |
| 175 | |
| 176 | case 3: |
| 177 | irow += 4; |
| 178 | if (irow >= m_frameContext->height()) { |
| 179 | ipass++; |
| 180 | irow = 1; |
| 181 | } |
| 182 | break; |
| 183 | |
| 184 | case 4: |
| 185 | irow += 2; |
| 186 | if (irow >= m_frameContext->height()) { |
| 187 | ipass++; |
| 188 | irow = 0; |
| 189 | } |
| 190 | break; |
| 191 | |
| 192 | default: |
| 193 | break; |
| 194 | } |
| 195 | } while (irow > (m_frameContext->height() - 1)); |
| 196 | } |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | // Perform Lempel-Ziv-Welch decoding. |
| 201 | // Returns true if decoding was successful. In this case the block will have been completely consumed and/or rowsRemaining will be 0. |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 202 | // Otherwise, decoding failed; returns false in this case, which will always cause the SkGifImageReader to set the "decode failed" flag. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 203 | bool SkGIFLZWContext::doLZW(const unsigned char* block, size_t bytesInBlock) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 204 | { |
| 205 | const size_t width = m_frameContext->width(); |
| 206 | |
| 207 | if (rowIter == rowBuffer.end()) |
| 208 | return true; |
| 209 | |
| 210 | for (const unsigned char* ch = block; bytesInBlock-- > 0; ch++) { |
| 211 | // Feed the next byte into the decoder's 32-bit input buffer. |
| 212 | datum += ((int) *ch) << bits; |
| 213 | bits += 8; |
| 214 | |
| 215 | // Check for underflow of decoder's 32-bit input buffer. |
| 216 | while (bits >= codesize) { |
| 217 | // Get the leading variable-length symbol from the data stream. |
| 218 | int code = datum & codemask; |
| 219 | datum >>= codesize; |
| 220 | bits -= codesize; |
| 221 | |
| 222 | // Reset the dictionary to its original state, if requested. |
| 223 | if (code == clearCode) { |
| 224 | codesize = m_frameContext->dataSize() + 1; |
| 225 | codemask = (1 << codesize) - 1; |
| 226 | avail = clearCode + 2; |
| 227 | oldcode = -1; |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | // Check for explicit end-of-stream code. |
| 232 | if (code == (clearCode + 1)) { |
| 233 | // end-of-stream should only appear after all image data. |
| 234 | if (!rowsRemaining) |
| 235 | return true; |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | const int tempCode = code; |
| 240 | unsigned short codeLength = 0; |
| 241 | if (code < avail) { |
| 242 | // This is a pre-existing code, so we already know what it |
| 243 | // encodes. |
| 244 | codeLength = suffixLength[code]; |
| 245 | rowIter += codeLength; |
| 246 | } else if (code == avail && oldcode != -1) { |
| 247 | // This is a new code just being added to the dictionary. |
| 248 | // It must encode the contents of the previous code, plus |
| 249 | // the first character of the previous code again. |
| 250 | codeLength = suffixLength[oldcode] + 1; |
| 251 | rowIter += codeLength; |
| 252 | *--rowIter = firstchar; |
| 253 | code = oldcode; |
| 254 | } else { |
| 255 | // This is an invalid code. The dictionary is just initialized |
| 256 | // and the code is incomplete. We don't know how to handle |
| 257 | // this case. |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | while (code >= clearCode) { |
| 262 | *--rowIter = suffix[code]; |
| 263 | code = prefix[code]; |
| 264 | } |
| 265 | |
| 266 | *--rowIter = firstchar = suffix[code]; |
| 267 | |
| 268 | // Define a new codeword in the dictionary as long as we've read |
| 269 | // more than one value from the stream. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 270 | if (avail < SK_MAX_DICTIONARY_ENTRIES && oldcode != -1) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 271 | prefix[avail] = oldcode; |
| 272 | suffix[avail] = firstchar; |
| 273 | suffixLength[avail] = suffixLength[oldcode] + 1; |
| 274 | ++avail; |
| 275 | |
| 276 | // If we've used up all the codewords of a given length |
| 277 | // increase the length of codewords by one bit, but don't |
| 278 | // exceed the specified maximum codeword size. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 279 | if (!(avail & codemask) && avail < SK_MAX_DICTIONARY_ENTRIES) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 280 | ++codesize; |
| 281 | codemask += avail; |
| 282 | } |
| 283 | } |
| 284 | oldcode = tempCode; |
| 285 | rowIter += codeLength; |
| 286 | |
| 287 | // Output as many rows as possible. |
| 288 | unsigned char* rowBegin = rowBuffer.begin(); |
| 289 | for (; rowBegin + width <= rowIter; rowBegin += width) { |
| 290 | if (!outputRow(rowBegin)) |
| 291 | return false; |
| 292 | rowsRemaining--; |
| 293 | if (!rowsRemaining) |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | if (rowBegin != rowBuffer.begin()) { |
| 298 | // Move the remaining bytes to the beginning of the buffer. |
| 299 | const size_t bytesToCopy = rowIter - rowBegin; |
| 300 | memcpy(&rowBuffer.front(), rowBegin, bytesToCopy); |
| 301 | rowIter = rowBuffer.begin() + bytesToCopy; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | return true; |
| 306 | } |
| 307 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 308 | sk_sp<SkColorTable> SkGIFColorMap::buildTable(SkColorType colorType, size_t transparentPixel) const |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 309 | { |
| 310 | if (!m_isDefined) |
| 311 | return nullptr; |
| 312 | |
| 313 | const PackColorProc proc = choose_pack_color_proc(false, colorType); |
| 314 | if (m_table) { |
| 315 | if (transparentPixel > (unsigned) m_table->count() |
| 316 | || m_table->operator[](transparentPixel) == SK_ColorTRANSPARENT) { |
| 317 | if (proc == m_packColorProc) { |
| 318 | // This SkColorTable has already been built with the same transparent color and |
| 319 | // packing proc. Reuse it. |
| 320 | return m_table; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | m_packColorProc = proc; |
| 325 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 326 | SkASSERT(m_colors <= SK_MAX_COLORS); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 327 | const uint8_t* srcColormap = m_rawData->bytes(); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 328 | SkPMColor colorStorage[SK_MAX_COLORS]; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 329 | for (size_t i = 0; i < m_colors; i++) { |
| 330 | if (i == transparentPixel) { |
| 331 | colorStorage[i] = SK_ColorTRANSPARENT; |
| 332 | } else { |
| 333 | colorStorage[i] = proc(255, srcColormap[0], srcColormap[1], srcColormap[2]); |
| 334 | } |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 335 | srcColormap += SK_BYTES_PER_COLORMAP_ENTRY; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 336 | } |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 337 | for (size_t i = m_colors; i < SK_MAX_COLORS; i++) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 338 | colorStorage[i] = SK_ColorTRANSPARENT; |
| 339 | } |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 340 | m_table = sk_sp<SkColorTable>(new SkColorTable(colorStorage, SK_MAX_COLORS)); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 341 | return m_table; |
| 342 | } |
| 343 | |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 344 | sk_sp<SkColorTable> SkGifImageReader::getColorTable(SkColorType colorType, size_t index) const { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 345 | if (index >= m_frames.size()) { |
| 346 | return nullptr; |
| 347 | } |
| 348 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 349 | const SkGIFFrameContext* frameContext = m_frames[index].get(); |
| 350 | const SkGIFColorMap& localColorMap = frameContext->localColorMap(); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 351 | if (localColorMap.isDefined()) { |
| 352 | return localColorMap.buildTable(colorType, frameContext->transparentPixel()); |
| 353 | } |
| 354 | if (m_globalColorMap.isDefined()) { |
| 355 | return m_globalColorMap.buildTable(colorType, frameContext->transparentPixel()); |
| 356 | } |
| 357 | return nullptr; |
| 358 | } |
| 359 | |
| 360 | // Perform decoding for this frame. frameComplete will be true if the entire frame is decoded. |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 361 | // Returns false if a decoding error occurred. This is a fatal error and causes the SkGifImageReader to set the "decode failed" flag. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 362 | // Otherwise, either not enough data is available to decode further than before, or the new data has been decoded successfully; returns true in this case. |
Leon Scroggins III | 45565b6 | 2016-12-05 14:56:30 -0500 | [diff] [blame^] | 363 | bool SkGIFFrameContext::decode(SkGifCodec* client, bool* frameComplete) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 364 | { |
| 365 | *frameComplete = false; |
| 366 | if (!m_lzwContext) { |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 367 | // Wait for more data to properly initialize SkGIFLZWContext. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 368 | if (!isDataSizeDefined() || !isHeaderDefined()) |
| 369 | return true; |
| 370 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 371 | m_lzwContext.reset(new SkGIFLZWContext(client, this)); |
Leon Scroggins III | 45565b6 | 2016-12-05 14:56:30 -0500 | [diff] [blame^] | 372 | if (!m_lzwContext->prepareToDecode()) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 373 | m_lzwContext.reset(); |
| 374 | return false; |
| 375 | } |
| 376 | |
| 377 | m_currentLzwBlock = 0; |
| 378 | } |
| 379 | |
| 380 | // Some bad GIFs have extra blocks beyond the last row, which we don't want to decode. |
| 381 | while (m_currentLzwBlock < m_lzwBlocks.size() && m_lzwContext->hasRemainingRows()) { |
| 382 | if (!m_lzwContext->doLZW(reinterpret_cast<const unsigned char*>(m_lzwBlocks[m_currentLzwBlock]->data()), |
| 383 | m_lzwBlocks[m_currentLzwBlock]->size())) { |
| 384 | return false; |
| 385 | } |
| 386 | ++m_currentLzwBlock; |
| 387 | } |
| 388 | |
| 389 | // If this frame is data complete then the previous loop must have completely decoded all LZW blocks. |
| 390 | // There will be no more decoding for this frame so it's time to cleanup. |
| 391 | if (isComplete()) { |
| 392 | *frameComplete = true; |
| 393 | m_lzwContext.reset(); |
| 394 | } |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | // Decode a frame. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 399 | // This method uses SkGIFFrameContext:decode() to decode the frame; decoding error is reported to client as a critical failure. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 400 | // Return true if decoding has progressed. Return false if an error has occurred. |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 401 | bool SkGifImageReader::decode(size_t frameIndex, bool* frameComplete) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 402 | { |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 403 | SkGIFFrameContext* currentFrame = m_frames[frameIndex].get(); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 404 | |
Leon Scroggins III | 45565b6 | 2016-12-05 14:56:30 -0500 | [diff] [blame^] | 405 | return currentFrame->decode(m_client, frameComplete); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | // Parse incoming GIF data stream into internal data structures. |
| 409 | // Return true if parsing has progressed or there is not enough data. |
| 410 | // Return false if a fatal error is encountered. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 411 | bool SkGifImageReader::parse(SkGifImageReader::SkGIFParseQuery query) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 412 | { |
| 413 | if (m_parseCompleted) { |
| 414 | return true; |
| 415 | } |
| 416 | |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 417 | if (SkGIFLoopCountQuery == query && m_loopCount != cLoopCountNotSeen) { |
| 418 | // Loop count has already been parsed. |
| 419 | return true; |
| 420 | } |
| 421 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 422 | // SkGIFSizeQuery and SkGIFFrameCountQuery are negative, so this is only meaningful when >= 0. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 423 | const int lastFrameToParse = (int) query; |
| 424 | if (lastFrameToParse >= 0 && (int) m_frames.size() > lastFrameToParse |
| 425 | && m_frames[lastFrameToParse]->isComplete()) { |
| 426 | // We have already parsed this frame. |
| 427 | return true; |
| 428 | } |
| 429 | |
| 430 | while (true) { |
| 431 | const size_t bytesBuffered = m_streamBuffer.buffer(m_bytesToConsume); |
| 432 | if (bytesBuffered < m_bytesToConsume) { |
| 433 | // The stream does not yet have enough data. Mark that we need less next time around, |
| 434 | // and return. |
| 435 | m_bytesToConsume -= bytesBuffered; |
| 436 | return true; |
| 437 | } |
| 438 | |
| 439 | switch (m_state) { |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 440 | case SkGIFLZW: |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 441 | SkASSERT(!m_frames.empty()); |
| 442 | // FIXME: All this copying might be wasteful for e.g. SkMemoryStream |
| 443 | m_frames.back()->addLzwBlock(m_streamBuffer.get(), m_streamBuffer.bytesBuffered()); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 444 | GETN(1, SkGIFSubBlock); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 445 | break; |
| 446 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 447 | case SkGIFLZWStart: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 448 | SkASSERT(!m_frames.empty()); |
| 449 | m_frames.back()->setDataSize(this->getOneByte()); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 450 | GETN(1, SkGIFSubBlock); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 454 | case SkGIFType: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 455 | const char* currentComponent = m_streamBuffer.get(); |
| 456 | |
| 457 | // All GIF files begin with "GIF87a" or "GIF89a". |
| 458 | if (!memcmp(currentComponent, "GIF89a", 6)) |
| 459 | m_version = 89; |
| 460 | else if (!memcmp(currentComponent, "GIF87a", 6)) |
| 461 | m_version = 87; |
| 462 | else { |
| 463 | // This prevents attempting to continue reading this invalid stream. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 464 | GETN(0, SkGIFDone); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 465 | return false; |
| 466 | } |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 467 | GETN(7, SkGIFGlobalHeader); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 468 | break; |
| 469 | } |
| 470 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 471 | case SkGIFGlobalHeader: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 472 | const unsigned char* currentComponent = |
| 473 | reinterpret_cast<const unsigned char*>(m_streamBuffer.get()); |
| 474 | |
| 475 | // This is the height and width of the "screen" or frame into which |
| 476 | // images are rendered. The individual images can be smaller than |
| 477 | // the screen size and located with an origin anywhere within the |
| 478 | // screen. |
| 479 | // Note that we don't inform the client of the size yet, as it might |
| 480 | // change after we read the first frame's image header. |
| 481 | m_screenWidth = GETINT16(currentComponent); |
| 482 | m_screenHeight = GETINT16(currentComponent + 2); |
| 483 | |
| 484 | const size_t globalColorMapColors = 2 << (currentComponent[4] & 0x07); |
| 485 | |
| 486 | if ((currentComponent[4] & 0x80) && globalColorMapColors > 0) { /* global map */ |
| 487 | m_globalColorMap.setNumColors(globalColorMapColors); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 488 | GETN(SK_BYTES_PER_COLORMAP_ENTRY * globalColorMapColors, SkGIFGlobalColormap); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 489 | break; |
| 490 | } |
| 491 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 492 | GETN(1, SkGIFImageStart); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 493 | break; |
| 494 | } |
| 495 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 496 | case SkGIFGlobalColormap: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 497 | m_globalColorMap.setRawData(m_streamBuffer.get(), m_streamBuffer.bytesBuffered()); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 498 | GETN(1, SkGIFImageStart); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 499 | break; |
| 500 | } |
| 501 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 502 | case SkGIFImageStart: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 503 | const char currentComponent = m_streamBuffer.get()[0]; |
| 504 | |
| 505 | if (currentComponent == '!') { // extension. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 506 | GETN(2, SkGIFExtension); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 507 | break; |
| 508 | } |
| 509 | |
| 510 | if (currentComponent == ',') { // image separator. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 511 | GETN(9, SkGIFImageHeader); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 512 | break; |
| 513 | } |
| 514 | |
| 515 | // If we get anything other than ',' (image separator), '!' |
| 516 | // (extension), or ';' (trailer), there is extraneous data |
| 517 | // between blocks. The GIF87a spec tells us to keep reading |
| 518 | // until we find an image separator, but GIF89a says such |
| 519 | // a file is corrupt. We follow Mozilla's implementation and |
| 520 | // proceed as if the file were correctly terminated, so the |
| 521 | // GIF will display. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 522 | GETN(0, SkGIFDone); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 523 | break; |
| 524 | } |
| 525 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 526 | case SkGIFExtension: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 527 | const unsigned char* currentComponent = |
| 528 | reinterpret_cast<const unsigned char*>(m_streamBuffer.get()); |
| 529 | |
| 530 | size_t bytesInBlock = currentComponent[1]; |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 531 | SkGIFState exceptionState = SkGIFSkipBlock; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 532 | |
| 533 | switch (*currentComponent) { |
| 534 | case 0xf9: |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 535 | // The GIF spec mandates that the GIFControlExtension header block length is 4 bytes, |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 536 | exceptionState = SkGIFControlExtension; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 537 | // and the parser for this block reads 4 bytes, so we must enforce that the buffer |
| 538 | // contains at least this many bytes. If the GIF specifies a different length, we |
| 539 | // allow that, so long as it's larger; the additional data will simply be ignored. |
| 540 | bytesInBlock = std::max(bytesInBlock, static_cast<size_t>(4)); |
| 541 | break; |
| 542 | |
| 543 | // The GIF spec also specifies the lengths of the following two extensions' headers |
| 544 | // (as 12 and 11 bytes, respectively). Because we ignore the plain text extension entirely |
| 545 | // and sanity-check the actual length of the application extension header before reading it, |
| 546 | // we allow GIFs to deviate from these values in either direction. This is important for |
| 547 | // real-world compatibility, as GIFs in the wild exist with application extension headers |
| 548 | // that are both shorter and longer than 11 bytes. |
| 549 | case 0x01: |
| 550 | // ignoring plain text extension |
| 551 | break; |
| 552 | |
| 553 | case 0xff: |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 554 | exceptionState = SkGIFApplicationExtension; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 555 | break; |
| 556 | |
| 557 | case 0xfe: |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 558 | exceptionState = SkGIFConsumeComment; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 559 | break; |
| 560 | } |
| 561 | |
| 562 | if (bytesInBlock) |
| 563 | GETN(bytesInBlock, exceptionState); |
| 564 | else |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 565 | GETN(1, SkGIFImageStart); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 566 | break; |
| 567 | } |
| 568 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 569 | case SkGIFConsumeBlock: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 570 | const unsigned char currentComponent = this->getOneByte(); |
| 571 | if (!currentComponent) |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 572 | GETN(1, SkGIFImageStart); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 573 | else |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 574 | GETN(currentComponent, SkGIFSkipBlock); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 575 | break; |
| 576 | } |
| 577 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 578 | case SkGIFSkipBlock: { |
| 579 | GETN(1, SkGIFConsumeBlock); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 580 | break; |
| 581 | } |
| 582 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 583 | case SkGIFControlExtension: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 584 | const unsigned char* currentComponent = |
| 585 | reinterpret_cast<const unsigned char*>(m_streamBuffer.get()); |
| 586 | |
| 587 | addFrameIfNecessary(); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 588 | SkGIFFrameContext* currentFrame = m_frames.back().get(); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 589 | if (*currentComponent & 0x1) |
| 590 | currentFrame->setTransparentPixel(currentComponent[3]); |
| 591 | |
| 592 | // We ignore the "user input" bit. |
| 593 | |
| 594 | // NOTE: This relies on the values in the FrameDisposalMethod enum |
| 595 | // matching those in the GIF spec! |
| 596 | int rawDisposalMethod = ((*currentComponent) >> 2) & 0x7; |
| 597 | switch (rawDisposalMethod) { |
| 598 | case 1: |
| 599 | case 2: |
| 600 | case 3: |
| 601 | currentFrame->setDisposalMethod((SkCodecAnimation::DisposalMethod) rawDisposalMethod); |
| 602 | break; |
| 603 | case 4: |
| 604 | // Some specs say that disposal method 3 is "overwrite previous", others that setting |
| 605 | // the third bit of the field (i.e. method 4) is. We map both to the same value. |
| 606 | currentFrame->setDisposalMethod(SkCodecAnimation::RestorePrevious_DisposalMethod); |
| 607 | break; |
| 608 | default: |
| 609 | // Other values use the default. |
| 610 | currentFrame->setDisposalMethod(SkCodecAnimation::Keep_DisposalMethod); |
| 611 | break; |
| 612 | } |
| 613 | currentFrame->setDelayTime(GETINT16(currentComponent + 1) * 10); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 614 | GETN(1, SkGIFConsumeBlock); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 615 | break; |
| 616 | } |
| 617 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 618 | case SkGIFCommentExtension: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 619 | const unsigned char currentComponent = this->getOneByte(); |
| 620 | if (currentComponent) |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 621 | GETN(currentComponent, SkGIFConsumeComment); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 622 | else |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 623 | GETN(1, SkGIFImageStart); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 624 | break; |
| 625 | } |
| 626 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 627 | case SkGIFConsumeComment: { |
| 628 | GETN(1, SkGIFCommentExtension); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 629 | break; |
| 630 | } |
| 631 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 632 | case SkGIFApplicationExtension: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 633 | // Check for netscape application extension. |
| 634 | if (m_streamBuffer.bytesBuffered() == 11) { |
| 635 | const unsigned char* currentComponent = |
| 636 | reinterpret_cast<const unsigned char*>(m_streamBuffer.get()); |
| 637 | |
| 638 | if (!memcmp(currentComponent, "NETSCAPE2.0", 11) || !memcmp(currentComponent, "ANIMEXTS1.0", 11)) |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 639 | GETN(1, SkGIFNetscapeExtensionBlock); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 640 | } |
| 641 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 642 | if (m_state != SkGIFNetscapeExtensionBlock) |
| 643 | GETN(1, SkGIFConsumeBlock); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 644 | break; |
| 645 | } |
| 646 | |
| 647 | // Netscape-specific GIF extension: animation looping. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 648 | case SkGIFNetscapeExtensionBlock: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 649 | const int currentComponent = this->getOneByte(); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 650 | // SkGIFConsumeNetscapeExtension always reads 3 bytes from the stream; we should at least wait for this amount. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 651 | if (currentComponent) |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 652 | GETN(std::max(3, currentComponent), SkGIFConsumeNetscapeExtension); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 653 | else |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 654 | GETN(1, SkGIFImageStart); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 655 | break; |
| 656 | } |
| 657 | |
| 658 | // Parse netscape-specific application extensions |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 659 | case SkGIFConsumeNetscapeExtension: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 660 | const unsigned char* currentComponent = |
| 661 | reinterpret_cast<const unsigned char*>(m_streamBuffer.get()); |
| 662 | |
| 663 | int netscapeExtension = currentComponent[0] & 7; |
| 664 | |
| 665 | // Loop entire animation specified # of times. Only read the loop count during the first iteration. |
| 666 | if (netscapeExtension == 1) { |
| 667 | m_loopCount = GETINT16(currentComponent + 1); |
| 668 | |
| 669 | // Zero loop count is infinite animation loop request. |
| 670 | if (!m_loopCount) |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 671 | m_loopCount = SkCodec::kRepetitionCountInfinite; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 672 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 673 | GETN(1, SkGIFNetscapeExtensionBlock); |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 674 | |
| 675 | if (SkGIFLoopCountQuery == query) { |
| 676 | m_streamBuffer.flush(); |
| 677 | return true; |
| 678 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 679 | } else if (netscapeExtension == 2) { |
| 680 | // Wait for specified # of bytes to enter buffer. |
| 681 | |
| 682 | // Don't do this, this extension doesn't exist (isn't used at all) |
| 683 | // and doesn't do anything, as our streaming/buffering takes care of it all... |
| 684 | // See: http://semmix.pl/color/exgraf/eeg24.htm |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 685 | GETN(1, SkGIFNetscapeExtensionBlock); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 686 | } else { |
| 687 | // 0,3-7 are yet to be defined netscape extension codes |
| 688 | // This prevents attempting to continue reading this invalid stream. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 689 | GETN(0, SkGIFDone); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 690 | return false; |
| 691 | } |
| 692 | break; |
| 693 | } |
| 694 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 695 | case SkGIFImageHeader: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 696 | unsigned height, width, xOffset, yOffset; |
| 697 | const unsigned char* currentComponent = |
| 698 | reinterpret_cast<const unsigned char*>(m_streamBuffer.get()); |
| 699 | |
| 700 | /* Get image offsets, with respect to the screen origin */ |
| 701 | xOffset = GETINT16(currentComponent); |
| 702 | yOffset = GETINT16(currentComponent + 2); |
| 703 | |
| 704 | /* Get image width and height. */ |
| 705 | width = GETINT16(currentComponent + 4); |
| 706 | height = GETINT16(currentComponent + 6); |
| 707 | |
| 708 | // Some GIF files have frames that don't fit in the specified |
| 709 | // overall image size. For the first frame, we can simply enlarge |
| 710 | // the image size to allow the frame to be visible. We can't do |
| 711 | // this on subsequent frames because the rest of the decoding |
| 712 | // infrastructure assumes the image size won't change as we |
| 713 | // continue decoding, so any subsequent frames that are even |
| 714 | // larger will be cropped. |
| 715 | // Luckily, handling just the first frame is sufficient to deal |
| 716 | // with most cases, e.g. ones where the image size is erroneously |
| 717 | // set to zero, since usually the first frame completely fills |
| 718 | // the image. |
| 719 | if (currentFrameIsFirstFrame()) { |
| 720 | m_screenHeight = std::max(m_screenHeight, yOffset + height); |
| 721 | m_screenWidth = std::max(m_screenWidth, xOffset + width); |
| 722 | } |
| 723 | |
| 724 | // NOTE: Chromium placed this block after setHeaderDefined, down |
| 725 | // below we returned true when asked for the size. So Chromium |
| 726 | // created an image which would fail. Is this the correct behavior? |
| 727 | // We choose to return false early, so we will not create an |
| 728 | // SkCodec. |
| 729 | |
| 730 | // Work around more broken GIF files that have zero image width or |
| 731 | // height. |
| 732 | if (!height || !width) { |
| 733 | height = m_screenHeight; |
| 734 | width = m_screenWidth; |
| 735 | if (!height || !width) { |
| 736 | // This prevents attempting to continue reading this invalid stream. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 737 | GETN(0, SkGIFDone); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 738 | return false; |
| 739 | } |
| 740 | } |
| 741 | |
Jim Van Verth | 3cfdf6c | 2016-10-26 09:45:23 -0400 | [diff] [blame] | 742 | const bool isLocalColormapDefined = SkToBool(currentComponent[8] & 0x80); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 743 | // The three low-order bits of currentComponent[8] specify the bits per pixel. |
| 744 | const size_t numColors = 2 << (currentComponent[8] & 0x7); |
| 745 | if (currentFrameIsFirstFrame()) { |
| 746 | bool hasTransparentPixel; |
| 747 | if (m_frames.size() == 0) { |
| 748 | // We did not see a Graphics Control Extension, so no transparent |
scroggo | 2f7068a | 2016-10-31 04:45:10 -0700 | [diff] [blame] | 749 | // pixel was specified. But if there is no color table, this frame is |
| 750 | // still transparent. |
| 751 | hasTransparentPixel = !isLocalColormapDefined |
| 752 | && m_globalColorMap.numColors() == 0; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 753 | } else { |
| 754 | // This means we did see a Graphics Control Extension, which specifies |
| 755 | // the transparent pixel |
| 756 | const size_t transparentPixel = m_frames[0]->transparentPixel(); |
| 757 | if (isLocalColormapDefined) { |
| 758 | hasTransparentPixel = transparentPixel < numColors; |
| 759 | } else { |
| 760 | const size_t globalColors = m_globalColorMap.numColors(); |
| 761 | if (!globalColors) { |
| 762 | // No color table for this frame, so the frame is empty. |
| 763 | // This is technically different from having a transparent |
| 764 | // pixel, but we'll treat it the same - nothing to draw here. |
| 765 | hasTransparentPixel = true; |
| 766 | } else { |
| 767 | hasTransparentPixel = transparentPixel < globalColors; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | if (hasTransparentPixel) { |
| 773 | m_firstFrameHasAlpha = true; |
| 774 | m_firstFrameSupportsIndex8 = true; |
| 775 | } else { |
| 776 | const bool frameIsSubset = xOffset > 0 || yOffset > 0 |
| 777 | || xOffset + width < m_screenWidth |
| 778 | || yOffset + height < m_screenHeight; |
| 779 | m_firstFrameHasAlpha = frameIsSubset; |
| 780 | m_firstFrameSupportsIndex8 = !frameIsSubset; |
| 781 | } |
| 782 | } |
| 783 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 784 | if (query == SkGIFSizeQuery) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 785 | // The decoder needs to stop, so we return here, before |
| 786 | // flushing the buffer. Next time through, we'll be in the same |
| 787 | // state, requiring the same amount in the buffer. |
| 788 | m_bytesToConsume = 0; |
| 789 | return true; |
| 790 | } |
| 791 | |
| 792 | addFrameIfNecessary(); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 793 | SkGIFFrameContext* currentFrame = m_frames.back().get(); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 794 | |
| 795 | currentFrame->setHeaderDefined(); |
| 796 | |
| 797 | currentFrame->setRect(xOffset, yOffset, width, height); |
Jim Van Verth | 3cfdf6c | 2016-10-26 09:45:23 -0400 | [diff] [blame] | 798 | currentFrame->setInterlaced(SkToBool(currentComponent[8] & 0x40)); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 799 | |
| 800 | // Overlaying interlaced, transparent GIFs over |
| 801 | // existing image data using the Haeberli display hack |
| 802 | // requires saving the underlying image in order to |
| 803 | // avoid jaggies at the transparency edges. We are |
| 804 | // unprepared to deal with that, so don't display such |
| 805 | // images progressively. Which means only the first |
| 806 | // frame can be progressively displayed. |
| 807 | // FIXME: It is possible that a non-transparent frame |
| 808 | // can be interlaced and progressively displayed. |
| 809 | currentFrame->setProgressiveDisplay(currentFrameIsFirstFrame()); |
| 810 | |
| 811 | if (isLocalColormapDefined) { |
| 812 | currentFrame->localColorMap().setNumColors(numColors); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 813 | GETN(SK_BYTES_PER_COLORMAP_ENTRY * numColors, SkGIFImageColormap); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 814 | break; |
| 815 | } |
| 816 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 817 | GETN(1, SkGIFLZWStart); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 818 | break; |
| 819 | } |
| 820 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 821 | case SkGIFImageColormap: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 822 | SkASSERT(!m_frames.empty()); |
| 823 | m_frames.back()->localColorMap().setRawData(m_streamBuffer.get(), m_streamBuffer.bytesBuffered()); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 824 | GETN(1, SkGIFLZWStart); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 825 | break; |
| 826 | } |
| 827 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 828 | case SkGIFSubBlock: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 829 | const size_t bytesInBlock = this->getOneByte(); |
| 830 | if (bytesInBlock) |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 831 | GETN(bytesInBlock, SkGIFLZW); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 832 | else { |
| 833 | // Finished parsing one frame; Process next frame. |
| 834 | SkASSERT(!m_frames.empty()); |
| 835 | // Note that some broken GIF files do not have enough LZW blocks to fully |
| 836 | // decode all rows but we treat it as frame complete. |
| 837 | m_frames.back()->setComplete(); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 838 | GETN(1, SkGIFImageStart); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 839 | if (lastFrameToParse >= 0 && (int) m_frames.size() > lastFrameToParse) { |
| 840 | m_streamBuffer.flush(); |
| 841 | return true; |
| 842 | } |
| 843 | } |
| 844 | break; |
| 845 | } |
| 846 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 847 | case SkGIFDone: { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 848 | m_parseCompleted = true; |
| 849 | return true; |
| 850 | } |
| 851 | |
| 852 | default: |
| 853 | // We shouldn't ever get here. |
| 854 | // This prevents attempting to continue reading this invalid stream. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 855 | GETN(0, SkGIFDone); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 856 | return false; |
| 857 | break; |
| 858 | } // switch |
| 859 | m_streamBuffer.flush(); |
| 860 | } |
| 861 | |
| 862 | return true; |
| 863 | } |
| 864 | |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 865 | void SkGifImageReader::addFrameIfNecessary() |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 866 | { |
| 867 | if (m_frames.empty() || m_frames.back()->isComplete()) { |
| 868 | const size_t i = m_frames.size(); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 869 | std::unique_ptr<SkGIFFrameContext> frame(new SkGIFFrameContext(i)); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 870 | if (0 == i) { |
| 871 | frame->setRequiredFrame(SkCodec::kNone); |
| 872 | } else { |
| 873 | // FIXME: We could correct these after decoding (i.e. some frames may turn out to be |
| 874 | // independent although we did not determine that here). |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 875 | const SkGIFFrameContext* prevFrameContext = m_frames[i - 1].get(); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 876 | switch (prevFrameContext->getDisposalMethod()) { |
| 877 | case SkCodecAnimation::Keep_DisposalMethod: |
| 878 | frame->setRequiredFrame(i - 1); |
| 879 | break; |
| 880 | case SkCodecAnimation::RestorePrevious_DisposalMethod: |
| 881 | frame->setRequiredFrame(prevFrameContext->getRequiredFrame()); |
| 882 | break; |
| 883 | case SkCodecAnimation::RestoreBGColor_DisposalMethod: |
| 884 | // If the prior frame covers the whole image |
| 885 | if (prevFrameContext->frameRect() == SkIRect::MakeWH(m_screenWidth, |
| 886 | m_screenHeight) |
| 887 | // Or the prior frame was independent |
| 888 | || prevFrameContext->getRequiredFrame() == SkCodec::kNone) |
| 889 | { |
| 890 | // This frame is independent, since we clear everything |
| 891 | // prior frame to the BG color |
| 892 | frame->setRequiredFrame(SkCodec::kNone); |
| 893 | } else { |
| 894 | frame->setRequiredFrame(i - 1); |
| 895 | } |
| 896 | break; |
| 897 | } |
| 898 | } |
| 899 | m_frames.push_back(std::move(frame)); |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | // FIXME: Move this method to close to doLZW(). |
Leon Scroggins III | 45565b6 | 2016-12-05 14:56:30 -0500 | [diff] [blame^] | 904 | bool SkGIFLZWContext::prepareToDecode() |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 905 | { |
| 906 | SkASSERT(m_frameContext->isDataSizeDefined() && m_frameContext->isHeaderDefined()); |
| 907 | |
| 908 | // Since we use a codesize of 1 more than the datasize, we need to ensure |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 909 | // that our datasize is strictly less than the SK_MAX_DICTIONARY_ENTRY_BITS. |
| 910 | if (m_frameContext->dataSize() >= SK_MAX_DICTIONARY_ENTRY_BITS) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 911 | return false; |
| 912 | clearCode = 1 << m_frameContext->dataSize(); |
| 913 | avail = clearCode + 2; |
| 914 | oldcode = -1; |
| 915 | codesize = m_frameContext->dataSize() + 1; |
| 916 | codemask = (1 << codesize) - 1; |
| 917 | datum = bits = 0; |
| 918 | ipass = m_frameContext->interlaced() ? 1 : 0; |
| 919 | irow = 0; |
| 920 | |
| 921 | // We want to know the longest sequence encodable by a dictionary with |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 922 | // SK_MAX_DICTIONARY_ENTRIES entries. If we ignore the need to encode the base |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 923 | // values themselves at the beginning of the dictionary, as well as the need |
| 924 | // for a clear code or a termination code, we could use every entry to |
| 925 | // encode a series of multiple values. If the input value stream looked |
| 926 | // like "AAAAA..." (a long string of just one value), the first dictionary |
| 927 | // entry would encode AA, the next AAA, the next AAAA, and so forth. Thus |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 928 | // the longest sequence would be SK_MAX_DICTIONARY_ENTRIES + 1 values. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 929 | // |
| 930 | // However, we have to account for reserved entries. The first |datasize| |
| 931 | // bits are reserved for the base values, and the next two entries are |
| 932 | // reserved for the clear code and termination code. In theory a GIF can |
| 933 | // set the datasize to 0, meaning we have just two reserved entries, making |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 934 | // the longest sequence (SK_MAX_DICTIONARY_ENTIRES + 1) - 2 values long. Since |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 935 | // each value is a byte, this is also the number of bytes in the longest |
| 936 | // encodable sequence. |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 937 | const size_t maxBytes = SK_MAX_DICTIONARY_ENTRIES - 1; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 938 | |
| 939 | // Now allocate the output buffer. We decode directly into this buffer |
| 940 | // until we have at least one row worth of data, then call outputRow(). |
| 941 | // This means worst case we may have (row width - 1) bytes in the buffer |
| 942 | // and then decode a sequence |maxBytes| long to append. |
| 943 | rowBuffer.reset(m_frameContext->width() - 1 + maxBytes); |
| 944 | rowIter = rowBuffer.begin(); |
| 945 | rowsRemaining = m_frameContext->height(); |
| 946 | |
| 947 | // Clearing the whole suffix table lets us be more tolerant of bad data. |
| 948 | for (int i = 0; i < clearCode; ++i) { |
| 949 | suffix[i] = i; |
| 950 | suffixLength[i] = 1; |
| 951 | } |
| 952 | return true; |
| 953 | } |
| 954 | |