Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 /* | |
| 9 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. | |
| 10 * | |
| 11 * Redistribution and use in source and binary forms, with or without | |
| 12 * modification, are permitted provided that the following conditions | |
| 13 * are met: | |
| 14 * 1. Redistributions of source code must retain the above copyright | |
| 15 * notice, this list of conditions and the following disclaimer. | |
| 16 * 2. Redistributions in binary form must reproduce the above copyright | |
| 17 * notice, this list of conditions and the following disclaimer in the | |
| 18 * documentation and/or other materials provided with the distribution. | |
| 19 * | |
| 20 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 28 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 31 */ | |
| 32 | |
| 33 #include "SkCodecAnimation.h" | |
| 8 #include "SkCodecPriv.h" | 34 #include "SkCodecPriv.h" |
| 9 #include "SkColorPriv.h" | 35 #include "SkColorPriv.h" |
| 10 #include "SkColorTable.h" | 36 #include "SkColorTable.h" |
| 11 #include "SkGifCodec.h" | 37 #include "SkGifCodec.h" |
| 12 #include "SkStream.h" | 38 #include "SkStream.h" |
| 13 #include "SkSwizzler.h" | 39 #include "SkSwizzler.h" |
| 14 #include "SkUtils.h" | |
| 15 | 40 |
| 16 #include "gif_lib.h" | 41 #define GIF87_STAMP "GIF87a" |
| 42 #define GIF89_STAMP "GIF89a" | |
| 43 #define GIF_STAMP_LEN 6 | |
| 17 | 44 |
| 18 /* | 45 /* |
| 19 * Checks the start of the stream to see if the image is a gif | 46 * Checks the start of the stream to see if the image is a gif |
| 20 */ | 47 */ |
| 21 bool SkGifCodec::IsGif(const void* buf, size_t bytesRead) { | 48 bool SkGifCodec::IsGif(const void* buf, size_t bytesRead) { |
| 22 if (bytesRead >= GIF_STAMP_LEN) { | 49 if (bytesRead >= GIF_STAMP_LEN) { |
| 23 if (memcmp(GIF_STAMP, buf, GIF_STAMP_LEN) == 0 || | 50 if (memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 || |
|
msarett
2016/10/14 14:58:51
Why this change?
scroggo_chromium
2016/10/14 19:56:00
GIFImageReader does not check for GIF_STAMP (see [
| |
| 24 memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 || | |
| 25 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) | 51 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) |
| 26 { | 52 { |
| 27 return true; | 53 return true; |
| 28 } | 54 } |
| 29 } | 55 } |
| 30 return false; | 56 return false; |
| 31 } | 57 } |
| 32 | 58 |
| 33 /* | 59 /* |
| 34 * Error function | 60 * Error function |
| 35 */ | 61 */ |
| 36 static SkCodec::Result gif_error(const char* msg, SkCodec::Result result = SkCod ec::kInvalidInput) { | 62 static SkCodec::Result gif_error(const char* msg, SkCodec::Result result = SkCod ec::kInvalidInput) { |
| 37 SkCodecPrintf("Gif Error: %s\n", msg); | 63 SkCodecPrintf("Gif Error: %s\n", msg); |
| 38 return result; | 64 return result; |
| 39 } | 65 } |
| 40 | 66 |
| 41 | |
| 42 /* | |
| 43 * Read function that will be passed to gif_lib | |
| 44 */ | |
| 45 static int32_t read_bytes_callback(GifFileType* fileType, GifByteType* out, int3 2_t size) { | |
| 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) { | |
| 54 #if GIFLIB_MAJOR < 5 | |
| 55 return DGifOpen(stream, read_bytes_callback); | |
| 56 #else | |
| 57 return DGifOpen(stream, read_bytes_callback, nullptr); | |
| 58 #endif | |
| 59 } | |
| 60 | |
| 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. | |
| 77 if (GRAPHICS_EXT_FUNC_CODE == extBlock.Function && extBlock.ByteCount >= 4) { | |
| 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)) { | |
| 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 val id | |
| 92 // index was not found. | |
| 93 return SK_MaxU32; | |
| 94 } | |
| 95 | |
| 96 inline uint32_t ceil_div(uint32_t a, uint32_t b) { | |
| 97 return (a + b - 1) / b; | |
| 98 } | |
| 99 | |
| 100 /* | |
| 101 * Gets the output row corresponding to the encoded row for interlaced gifs | |
| 102 */ | |
| 103 inline uint32_t get_output_row_interlaced(uint32_t encodedRow, uint32_t height) { | |
| 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 | |
| 121 /* | |
| 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) { | |
| 126 #if GIFLIB_MAJOR < 5 || (GIFLIB_MAJOR == 5 && GIFLIB_MINOR == 0) | |
| 127 DGifCloseFile(gif); | |
| 128 #else | |
| 129 DGifCloseFile(gif, nullptr); | |
| 130 #endif | |
| 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) { | |
| 139 #if GIFLIB_MAJOR < 5 | |
| 140 FreeExtension(image); | |
| 141 #else | |
| 142 GifFreeExtensions(&image->ExtensionBlockCount, &image->ExtensionBlocks); | |
| 143 #endif | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 /* | |
| 148 * Read enough of the stream to initialize the SkGifCodec. | |
| 149 * Returns a bool representing success or failure. | |
| 150 * | |
| 151 * @param codecOut | |
| 152 * If it returned true, and codecOut was not nullptr, | |
| 153 * codecOut will be set to a new SkGifCodec. | |
| 154 * | |
| 155 * @param gifOut | |
| 156 * If it returned true, and codecOut was nullptr, | |
| 157 * gifOut must be non-nullptr and gifOut will be set to a new | |
| 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 | |
| 172 if (nullptr == gif) { | |
| 173 gif_error("DGifOpen failed.\n"); | |
| 174 return false; | |
| 175 } | |
| 176 | |
| 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 | |
| 193 if (nullptr != codecOut) { | |
| 194 SkISize size; | |
| 195 SkIRect frameRect; | |
| 196 if (!GetDimensions(gif, &size, &frameRect)) { | |
| 197 gif_error("Invalid gif size.\n"); | |
| 198 return false; | |
| 199 } | |
| 200 bool frameIsSubset = (size != frameRect.size()); | |
| 201 | |
| 202 // Determine the encoded alpha type. The transIndex might be valid if i t less | |
| 203 // than 256. We are not certain that the index is valid until we proces s 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 i mage has | |
| 206 // alpha. | |
| 207 // In the case where we must support alpha, we indicate kBinary, since e very | |
| 208 // pixel will either be fully opaque or fully transparent. | |
| 209 SkEncodedInfo::Alpha alpha = (transIndex < 256) ? SkEncodedInfo::kBinary _Alpha : | |
| 210 SkEncodedInfo::kOpaque_Alpha; | |
| 211 | |
| 212 // Return the codec | |
| 213 // Use kPalette since Gifs are encoded with a color table. | |
| 214 // Use 8-bits per component, since this is the output we get from giflib . | |
| 215 // FIXME: Gifs can actually be encoded with 4-bits per pixel. Can we su pport this? | |
| 216 SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, alpha, 8); | |
| 217 *codecOut = new SkGifCodec(size.width(), size.height(), info, streamDele ter.release(), | |
| 218 gif.release(), transIndex, frameRect, frameIsSubset); | |
| 219 } else { | |
| 220 SkASSERT(nullptr != gifOut); | |
| 221 streamDeleter.release(); | |
| 222 *gifOut = gif.release(); | |
| 223 } | |
| 224 return true; | |
| 225 } | |
| 226 | |
| 227 /* | 67 /* |
| 228 * Assumes IsGif was called and returned true | 68 * Assumes IsGif was called and returned true |
| 229 * Creates a gif decoder | 69 * Creates a gif decoder |
| 230 * Reads enough of the stream to determine the image format | 70 * Reads enough of the stream to determine the image format |
| 231 */ | 71 */ |
| 232 SkCodec* SkGifCodec::NewFromStream(SkStream* stream) { | 72 SkCodec* SkGifCodec::NewFromStream(SkStream* stream) { |
| 233 SkCodec* codec = nullptr; | 73 std::unique_ptr<GIFImageReader> reader(new GIFImageReader(stream)); |
| 234 if (ReadHeader(stream, &codec, nullptr)) { | 74 if (!reader->parse(GIFImageReader::GIFSizeQuery)) { |
| 235 return codec; | 75 // Not enough data to determine the size. |
| 76 return nullptr; | |
| 236 } | 77 } |
| 237 return nullptr; | 78 |
| 79 if (0 == reader->screenWidth() || 0 == reader->screenHeight()) { | |
| 80 return nullptr; | |
| 81 } | |
| 82 | |
| 83 return new SkGifCodec(reader.release()); | |
| 238 } | 84 } |
| 239 | 85 |
| 240 SkGifCodec::SkGifCodec(int width, int height, const SkEncodedInfo& info, SkStrea m* stream, | |
| 241 GifFileType* gif, uint32_t transIndex, const SkIRect& frameRect, bool fr ameIsSubset) | |
| 242 : INHERITED(width, height, info, stream) | |
| 243 , fGif(gif) | |
| 244 , fSrcBuffer(new uint8_t[this->getInfo().width()]) | |
| 245 , fFrameRect(frameRect) | |
| 246 // If it is valid, fTransIndex will be used to set fFillIndex. We don't kno w if | |
| 247 // fTransIndex is valid until we process the color table, since fTransIndex may | |
| 248 // be greater than the size of the color table. | |
| 249 , fTransIndex(transIndex) | |
| 250 // Default fFillIndex is 0. We will overwrite this if fTransIndex is valid, or if | |
| 251 // there is a valid background color. | |
| 252 , fFillIndex(0) | |
| 253 , fFrameIsSubset(frameIsSubset) | |
| 254 , fSwizzler(NULL) | |
| 255 , fColorTable(NULL) | |
| 256 {} | |
| 257 | |
| 258 bool SkGifCodec::onRewind() { | 86 bool SkGifCodec::onRewind() { |
| 259 GifFileType* gifOut = nullptr; | 87 fReader->clearDecodeState(); |
| 260 if (!ReadHeader(this->stream(), nullptr, &gifOut)) { | |
| 261 return false; | |
| 262 } | |
| 263 | |
| 264 SkASSERT(nullptr != gifOut); | |
| 265 fGif.reset(gifOut); | |
| 266 return true; | 88 return true; |
| 267 } | 89 } |
| 268 | 90 |
| 269 SkCodec::Result SkGifCodec::ReadUpToFirstImage(GifFileType* gif, uint32_t* trans Index) { | 91 static SkEncodedInfo make_info() { |
| 270 // Use this as a container to hold information about any gif extension | 92 // Use kPalette since Gifs are encoded with a color table. |
| 271 // blocks. This generally stores transparency and animation instructions. | 93 // FIXME: Except that we may not be able to decode to index8 for a frame bey ond the first :( |
|
msarett
2016/10/14 14:58:50
I'm guessing that users of the animated API are "s
scroggo_chromium
2016/10/14 19:56:00
Currently, requests for Index8 for frames beyond t
| |
| 272 SavedImage saveExt; | 94 // FIXME: Gifs can actually be encoded with 4-bits per pixel. Can we suppor t this? |
|
msarett
2016/10/14 14:58:50
I think the swizzler already supports this. So ye
scroggo_chromium
2016/10/14 19:56:00
sgtm. Updated the comment.
| |
| 273 SkAutoTCallVProc<SavedImage, FreeExtension> autoFreeExt(&saveExt); | 95 // FIXME: We do not yet know whether the image has alpha, so report binary. More specifically, |
|
msarett
2016/10/14 14:58:50
This may be fine, but it's starting to feel like S
scroggo_chromium
2016/10/14 19:56:00
I'm not sure how to do that, though. At this point
| |
| 274 saveExt.ExtensionBlocks = nullptr; | 96 // we cannot know without parsing the whole stream, since an arbitrar y frame could |
| 275 saveExt.ExtensionBlockCount = 0; | 97 // require clearing to the background, even though all preceding fram es were opaque. |
| 276 GifByteType* extData; | 98 return SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, SkEncodedInfo::kBi nary_Alpha, 8); |
| 277 int32_t extFunction; | |
| 278 | |
| 279 // We will loop over components of gif images until we find an image. Once | |
| 280 // we find an image, we will decode and return it. While many gif files | |
| 281 // contain more than one image, we will simply decode the first image. | |
| 282 GifRecordType recordType; | |
| 283 do { | |
| 284 // Get the current record type | |
| 285 if (GIF_ERROR == DGifGetRecordType(gif, &recordType)) { | |
| 286 return gif_error("DGifGetRecordType failed.\n", kInvalidInput); | |
| 287 } | |
| 288 switch (recordType) { | |
| 289 case IMAGE_DESC_RECORD_TYPE: { | |
| 290 *transIndex = find_trans_index(saveExt); | |
| 291 | |
| 292 // FIXME: Gif files may have multiple images stored in a single | |
| 293 // file. This is most commonly used to enable | |
| 294 // animations. Since we are leaving animated gifs as a | |
| 295 // TODO, we will return kSuccess after decoding the | |
| 296 // first image in the file. This is the same behavior | |
| 297 // as SkImageDecoder_libgif. | |
| 298 // | |
| 299 // Most times this works pretty well, but sometimes it | |
| 300 // doesn't. For example, I have an animated test image | |
| 301 // where the first image in the file is 1x1, but the | |
| 302 // subsequent images are meaningful. This currently | |
| 303 // displays the 1x1 image, which is not ideal. Right | |
| 304 // now I am leaving this as an issue that will be | |
| 305 // addressed when we implement animated gifs. | |
| 306 // | |
| 307 // It is also possible (not explicitly disallowed in the | |
| 308 // specification) that gif files provide multiple | |
| 309 // images in a single file that are all meant to be | |
| 310 // displayed in the same frame together. I will | |
| 311 // currently leave this unimplemented until I find a | |
| 312 // test case that expects this behavior. | |
| 313 return kSuccess; | |
| 314 } | |
| 315 // Extensions are used to specify special properties of the image | |
| 316 // such as transparency or animation. | |
| 317 case EXTENSION_RECORD_TYPE: | |
| 318 // Read extension data | |
| 319 if (GIF_ERROR == DGifGetExtension(gif, &extFunction, &extData)) { | |
| 320 return gif_error("Could not get extension.\n", kIncompleteIn put); | |
| 321 } | |
| 322 | |
| 323 // Create an extension block with our data | |
| 324 while (nullptr != extData) { | |
| 325 // Add a single block | |
| 326 | |
| 327 #if GIFLIB_MAJOR < 5 | |
| 328 if (AddExtensionBlock(&saveExt, extData[0], | |
| 329 &extData[1]) == GIF_ERROR) { | |
| 330 #else | |
| 331 if (GIF_ERROR == GifAddExtensionBlock(&saveExt.ExtensionBloc kCount, | |
| 332 &saveExt.ExtensionBloc ks, | |
| 333 extFunction, extData[0 ], &extData[1])) { | |
| 334 #endif | |
| 335 return gif_error("Could not add extension block.\n", kIn completeInput); | |
| 336 } | |
| 337 // Move to the next block | |
| 338 if (GIF_ERROR == DGifGetExtensionNext(gif, &extData)) { | |
| 339 return gif_error("Could not get next extension.\n", kInc ompleteInput); | |
| 340 } | |
| 341 } | |
| 342 break; | |
| 343 | |
| 344 // Signals the end of the gif file | |
| 345 case TERMINATE_RECORD_TYPE: | |
| 346 break; | |
| 347 | |
| 348 default: | |
| 349 // DGifGetRecordType returns an error if the record type does | |
| 350 // not match one of the above cases. This should not be | |
| 351 // reached. | |
| 352 SkASSERT(false); | |
| 353 break; | |
| 354 } | |
| 355 } while (TERMINATE_RECORD_TYPE != recordType); | |
| 356 | |
| 357 return gif_error("Could not find any images to decode in gif file.\n", kInva lidInput); | |
| 358 } | 99 } |
| 359 | 100 |
| 360 bool SkGifCodec::GetDimensions(GifFileType* gif, SkISize* size, SkIRect* frameRe ct) { | 101 SkGifCodec::SkGifCodec(GIFImageReader* reader) |
| 361 // Get the encoded dimension values | 102 : INHERITED(reader->screenWidth(), reader->screenHeight(), make_info(), null ptr) |
| 362 SavedImage* image = &gif->SavedImages[gif->ImageCount - 1]; | 103 , fReader(reader) |
| 363 const GifImageDesc& desc = image->ImageDesc; | 104 , fTmpBuffer(nullptr) |
| 364 int frameLeft = desc.Left; | 105 , fSwizzler(nullptr) |
| 365 int frameTop = desc.Top; | 106 , fCurrColorTable(nullptr) |
| 366 int frameWidth = desc.Width; | 107 , fCurrColorTableIsReal(false) |
| 367 int frameHeight = desc.Height; | 108 , fFilledBackground(false) |
| 368 int width = gif->SWidth; | 109 , fFirstCallToIncrementalDecode(false) |
| 369 int height = gif->SHeight; | 110 , fDst(nullptr) |
| 111 , fDstRowBytes(0) | |
| 112 , fFirstRow(0) | |
| 113 , fLastRow(0) | |
| 114 , fRowsDecoded(0) | |
| 115 { | |
| 116 reader->setClient(this); | |
| 117 } | |
| 370 | 118 |
| 371 // Ensure that the decode dimensions are large enough to contain the frame | 119 std::vector<SkCodec::FrameInfo> SkGifCodec::onGetFrameInfo() { |
| 372 width = SkTMax(width, frameWidth + frameLeft); | 120 fReader->parse(GIFImageReader::GIFFrameCountQuery); |
| 373 height = SkTMax(height, frameHeight + frameTop); | 121 const size_t size = fReader->imagesCount(); |
| 122 std::vector<FrameInfo> result(size); | |
| 123 for (size_t i = 0; i < size; i++) { | |
| 124 const GIFFrameContext* frameContext = fReader->frameContext(i); | |
| 125 result[i].fDuration = frameContext->delayTime(); | |
| 126 result[i].fRequiredFrame = frameContext->getRequiredFrame(); | |
| 127 } | |
| 128 return result; | |
| 129 } | |
| 374 | 130 |
| 375 // All of these dimensions should be positive, as they are encoded as unsign ed 16-bit integers. | 131 void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, size_t frameIn dex, |
| 376 // It is unclear why giflib casts them to ints. We will go ahead and check that they are | 132 SkPMColor* inputColorPtr, int* inputColorCount) { |
| 377 // in fact positive. | 133 fCurrColorTable = std::move(fReader->getColorTable(dstInfo.colorType(), fram eIndex)); |
| 378 if (frameLeft < 0 || frameTop < 0 || frameWidth < 0 || frameHeight < 0 || wi dth <= 0 || | 134 fCurrColorTableIsReal = fCurrColorTable; |
| 379 height <= 0) { | 135 if (!fCurrColorTable) { |
| 380 return false; | 136 // This is possible for an empty frame. Create a dummy with one value (t ransparent). |
| 137 SkPMColor color = SK_ColorTRANSPARENT; | |
| 138 fCurrColorTable.reset(new SkColorTable(&color, 1)); | |
| 381 } | 139 } |
| 382 | 140 |
| 383 frameRect->setXYWH(frameLeft, frameTop, frameWidth, frameHeight); | 141 if (inputColorCount) { |
| 384 size->set(width, height); | 142 *inputColorCount = fCurrColorTable->count(); |
| 385 return true; | 143 } |
| 144 | |
| 145 copy_color_table(dstInfo, fCurrColorTable.get(), inputColorPtr, inputColorCo unt); | |
| 386 } | 146 } |
| 387 | 147 |
| 388 void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, SkPMColor* inp utColorPtr, | |
| 389 int* inputColorCount) { | |
| 390 // Set up our own color table | |
| 391 const uint32_t maxColors = 256; | |
| 392 SkPMColor colorPtr[256]; | |
| 393 if (NULL != inputColorCount) { | |
| 394 // We set the number of colors to maxColors in order to ensure | |
| 395 // safe memory accesses. Otherwise, an invalid pixel could | |
| 396 // access memory outside of our color table array. | |
| 397 *inputColorCount = maxColors; | |
| 398 } | |
| 399 | |
| 400 // Get local color table | |
| 401 ColorMapObject* colorMap = fGif->Image.ColorMap; | |
| 402 // If there is no local color table, use the global color table | |
| 403 if (NULL == colorMap) { | |
| 404 colorMap = fGif->SColorMap; | |
| 405 } | |
| 406 | |
| 407 uint32_t colorCount = 0; | |
| 408 if (NULL != colorMap) { | |
| 409 colorCount = colorMap->ColorCount; | |
| 410 // giflib guarantees these properties | |
| 411 SkASSERT(colorCount == (unsigned) (1 << (colorMap->BitsPerPixel))); | |
| 412 SkASSERT(colorCount <= 256); | |
| 413 PackColorProc proc = choose_pack_color_proc(false, dstInfo.colorType()); | |
| 414 for (uint32_t i = 0; i < colorCount; i++) { | |
| 415 colorPtr[i] = proc(0xFF, colorMap->Colors[i].Red, | |
| 416 colorMap->Colors[i].Green, colorMap->Colors[i].Blue); | |
| 417 } | |
| 418 } | |
| 419 | |
| 420 // Fill in the color table for indices greater than color count. | |
| 421 // This allows for predictable, safe behavior. | |
| 422 if (colorCount > 0) { | |
| 423 // Gifs have the option to specify the color at a single index of the co lor | |
| 424 // table as transparent. If the transparent index is greater than the | |
| 425 // colorCount, we know that there is no valid transparent color in the c olor | |
| 426 // table. If there is not valid transparent index, we will try to use t he | |
| 427 // backgroundIndex as the fill index. If the backgroundIndex is also no t | |
| 428 // valid, we will let fFillIndex default to 0 (it is set to zero in the | |
| 429 // constructor). This behavior is not specified but matches | |
| 430 // SkImageDecoder_libgif. | |
| 431 uint32_t backgroundIndex = fGif->SBackGroundColor; | |
| 432 if (fTransIndex < colorCount) { | |
| 433 colorPtr[fTransIndex] = SK_ColorTRANSPARENT; | |
| 434 fFillIndex = fTransIndex; | |
| 435 } else if (backgroundIndex < colorCount) { | |
| 436 fFillIndex = backgroundIndex; | |
| 437 } | |
| 438 | |
| 439 for (uint32_t i = colorCount; i < maxColors; i++) { | |
| 440 colorPtr[i] = colorPtr[fFillIndex]; | |
| 441 } | |
| 442 } else { | |
| 443 sk_memset32(colorPtr, 0xFF000000, maxColors); | |
| 444 } | |
| 445 | |
| 446 fColorTable.reset(new SkColorTable(colorPtr, maxColors)); | |
| 447 copy_color_table(dstInfo, this->fColorTable, inputColorPtr, inputColorCount) ; | |
| 448 } | |
| 449 | 148 |
| 450 SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, SkPMColo r* inputColorPtr, | 149 SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, SkPMColo r* inputColorPtr, |
| 451 int* inputColorCount, const Options& opts) { | 150 int* inputColorCount, const Options& opts) { |
| 452 // Check for valid input parameters | 151 // Check for valid input parameters |
| 453 if (!conversion_possible_ignore_color_space(dstInfo, this->getInfo())) { | 152 if (!conversion_possible_ignore_color_space(dstInfo, this->getInfo())) { |
| 454 return gif_error("Cannot convert input type to output type.\n", kInvalid Conversion); | 153 return gif_error("Cannot convert input type to output type.\n", kInvalid Conversion); |
| 455 } | 154 } |
| 456 | 155 |
| 156 if (dstInfo.colorType() == kRGBA_F16_SkColorType) { | |
| 157 // FIXME: This should be supported. | |
| 158 return gif_error("GIF does not yet support F16.\n", kInvalidConversion); | |
| 159 } | |
| 160 | |
| 161 const size_t frameIndex = opts.fFrameOptions ? opts.fFrameOptions->fIndex : 0; | |
| 162 if (frameIndex > 0 && dstInfo.colorType() == kIndex_8_SkColorType) { | |
| 163 // FIXME: It is possible that a later frame can be decoded to index8, if it does one of the | |
| 164 // following: | |
| 165 // - Covers the entire previous frame | |
| 166 // - Shares a color table (and transparent index) with any prior frames that are showing. | |
| 167 // We must support index8 for the first frame to be backwards compatible on Android, but | |
| 168 // we do not (currently) need to support later frames as index8. | |
| 169 return gif_error("Cannot decode multiframe gif (except frame 0) as index 8.\n", | |
| 170 kInvalidConversion); | |
| 171 } | |
| 172 | |
| 173 fReader->parse((GIFImageReader::GIFParseQuery) frameIndex); | |
| 174 | |
| 175 if (frameIndex >= fReader->imagesCount()) { | |
| 176 return gif_error("frame index out of range!\n", kIncompleteInput); | |
| 177 } | |
| 178 | |
| 179 fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]); | |
| 180 | |
| 457 // Initialize color table and copy to the client if necessary | 181 // Initialize color table and copy to the client if necessary |
| 458 this->initializeColorTable(dstInfo, inputColorPtr, inputColorCount); | 182 this->initializeColorTable(dstInfo, frameIndex, inputColorPtr, inputColorCou nt); |
| 459 | 183 this->initializeSwizzler(dstInfo, frameIndex); |
| 460 this->initializeSwizzler(dstInfo, opts); | |
| 461 return kSuccess; | 184 return kSuccess; |
| 462 } | 185 } |
| 463 | 186 |
| 464 void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& o pts) { | 187 void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, size_t frameInde x) { |
| 465 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); | 188 const GIFFrameContext* frame = fReader->frameContext(frameIndex); |
| 466 const SkIRect* frameRect = fFrameIsSubset ? &fFrameRect : nullptr; | 189 // This is only called by prepareToDecode, which ensures frameIndex is in ra nge. |
| 467 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), colorPtr, dstInfo, opts, | 190 SkASSERT(frame); |
| 468 frameRect)); | |
| 469 SkASSERT(fSwizzler); | |
| 470 } | |
| 471 | 191 |
| 472 bool SkGifCodec::readRow() { | 192 const int xBegin = frame->xOffset(); |
| 473 return GIF_ERROR != DGifGetLine(fGif, fSrcBuffer.get(), fFrameRect.width()); | 193 const int xEnd = std::min(static_cast<int>(frame->xOffset() + frame->width() ), |
| 194 static_cast<int>(fReader->screenWidth())); | |
| 195 | |
| 196 // CreateSwizzler only reads left and right of the frame. We cannot use the frame's raw | |
| 197 // frameRect, since it might extend beyond the edge of the frame. | |
| 198 SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0); | |
| 199 | |
| 200 // The default Options should be fine: | |
| 201 // - we'll ignore if the memory is zero initialized - unless we're the first frame, this won't | |
| 202 // matter anyway. | |
| 203 // - subsets are not supported for gif | |
| 204 // - the swizzler does not need to know about the frame. | |
| 205 // We may not be able to use the real Options anyway, since getPixels does n ot store it (due to | |
| 206 // a bug). | |
| 207 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), | |
| 208 fCurrColorTable->readColors(), dstInfo, Options(), &swizzleR ect)); | |
| 209 SkASSERT(fSwizzler.get()); | |
| 474 } | 210 } |
| 475 | 211 |
| 476 /* | 212 /* |
| 477 * Initiates the gif decode | 213 * Initiates the gif decode |
| 478 */ | 214 */ |
| 479 SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo, | 215 SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 480 void* dst, size_t dstRowBytes, | 216 void* pixels, size_t dstRowBytes, |
| 481 const Options& opts, | 217 const Options& opts, |
| 482 SkPMColor* inputColorPtr, | 218 SkPMColor* inputColorPtr, |
| 483 int* inputColorCount, | 219 int* inputColorCount, |
| 484 int* rowsDecoded) { | 220 int* rowsDecoded) { |
| 485 Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCoun t, opts); | 221 Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCoun t, opts); |
| 486 if (kSuccess != result) { | 222 if (kSuccess != result) { |
| 487 return result; | 223 return result; |
| 488 } | 224 } |
| 489 | 225 |
| 490 if (dstInfo.dimensions() != this->getInfo().dimensions()) { | 226 if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 491 return gif_error("Scaling not supported.\n", kInvalidScale); | 227 return gif_error("Scaling not supported.\n", kInvalidScale); |
| 492 } | 228 } |
| 493 | 229 |
| 494 // Initialize the swizzler | 230 fDst = pixels; |
| 495 if (fFrameIsSubset) { | 231 fDstRowBytes = dstRowBytes; |
| 496 // Fill the background | 232 |
| 497 SkSampler::Fill(dstInfo, dst, dstRowBytes, this->getFillValue(dstInfo), | 233 fFirstRow = 0; |
| 498 opts.fZeroInitialized); | 234 fLastRow = dstInfo.height() - 1; |
| 499 } | 235 |
| 500 | 236 return this->decodeFrame(true, opts, rowsDecoded); |
| 501 // Iterate over rows of the input | 237 } |
| 502 for (int y = fFrameRect.top(); y < fFrameRect.bottom(); y++) { | 238 |
| 503 if (!this->readRow()) { | 239 SkCodec::Result SkGifCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo, |
| 504 *rowsDecoded = y; | 240 void* pixels, size_t dstRow Bytes, |
| 505 return gif_error("Could not decode line.\n", kIncompleteInput); | 241 const SkCodec::Options& opt s, |
| 506 } | 242 SkPMColor* inputColorPtr, |
| 507 void* dstRow = SkTAddOffset<void>(dst, dstRowBytes * this->outputScanlin e(y)); | 243 int* inputColorCount) { |
| 508 fSwizzler->swizzle(dstRow, fSrcBuffer.get()); | 244 Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCoun t, opts); |
| 509 } | 245 if (result != kSuccess) { |
| 246 return result; | |
| 247 } | |
| 248 | |
| 249 fDst = pixels; | |
| 250 fDstRowBytes = dstRowBytes; | |
| 251 | |
| 252 if (opts.fSubset) { | |
| 253 fFirstRow = opts.fSubset->top(); | |
| 254 fLastRow = opts.fSubset->bottom() - 1; | |
| 255 } else { | |
| 256 fFirstRow = 0; | |
| 257 fLastRow = dstInfo.height() - 1; | |
| 258 } | |
| 259 | |
| 260 fFirstCallToIncrementalDecode = true; | |
| 261 | |
| 510 return kSuccess; | 262 return kSuccess; |
| 511 } | 263 } |
| 512 | 264 |
| 513 // FIXME: This is similar to the implementation for bmp and png. Can we share m ore code or | 265 SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) { |
| 514 // possibly make this non-virtual? | 266 // It is possible the client has appended more data. Parse, if needed. |
| 267 const auto& options = this->options(); | |
| 268 const size_t frameIndex = options.fFrameOptions ? options.fFrameOptions->fIn dex : 0; | |
| 269 fReader->parse((GIFImageReader::GIFParseQuery) frameIndex); | |
| 270 | |
| 271 const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode; | |
| 272 fFirstCallToIncrementalDecode = false; | |
| 273 return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded) ; | |
| 274 } | |
| 275 | |
| 276 SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded) { | |
| 277 const SkImageInfo& dstInfo = this->dstInfo(); | |
| 278 const size_t frameIndex = opts.fFrameOptions ? opts.fFrameOptions->fIndex : 0; | |
| 279 SkASSERT(frameIndex < fReader->imagesCount()); | |
| 280 const GIFFrameContext* frameContext = fReader->frameContext(frameIndex); | |
| 281 if (firstAttempt) { | |
| 282 // rowsDecoded reports how many rows have been initialized, so a layer a bove | |
| 283 // can fill the rest. In some cases, we fill the background before decod ing | |
| 284 // (or it is already filled for us), so we report rowsDecoded to be the full | |
| 285 // height. | |
| 286 bool filledBackground = false; | |
| 287 if (frameContext->getRequiredFrame() == kIndependentFrame) { | |
| 288 // We may need to clear to transparent for one of the following reas ons: | |
| 289 // - The frameRect does not cover the full bounds. haveDecodedRow wi ll | |
| 290 // only draw inside the frameRect, so we need to clear the rest. | |
| 291 // - There is a valid transparent pixel value. (FIXME: I'm assuming | |
| 292 // writeTransparentPixels will be false in this case, based on | |
| 293 // Chromium's assumption that it would already be zeroed. If we | |
| 294 // change that behavior, could we skip Filling here?) | |
| 295 // - The frame is interlaced. There is no obvious way to fill | |
| 296 // afterwards for an incomplete image. (FIXME: Does the first pass | |
| 297 // cover all rows? If so, we do not have to fill here.) | |
| 298 if (frameContext->frameRect() != this->getInfo().bounds() | |
| 299 || frameContext->transparentPixel() < MAX_COLORS | |
| 300 || frameContext->interlaced()) { | |
| 301 // fill ignores the width (replaces it with the actual, scaled w idth). | |
| 302 // But we need to scale in Y. | |
| 303 const int scaledHeight = get_scaled_dimension(dstInfo.height(), fSwizzler->sampleY()); | |
| 304 auto fillInfo = dstInfo.makeWH(0, scaledHeight); | |
| 305 fSwizzler->fill(fillInfo, fDst, fDstRowBytes, this->getFillValue (dstInfo), | |
| 306 opts.fZeroInitialized); | |
| 307 filledBackground = true; | |
| 308 } | |
| 309 } else { | |
| 310 // Not independent | |
| 311 if (opts.fFrameOptions && !opts.fFrameOptions->fHasPriorFrame) { | |
| 312 // Decode that frame into pixels. | |
| 313 Options prevFrameOpts(opts); | |
| 314 MultiFrameOptions prevFrameMultiOpts; | |
| 315 prevFrameMultiOpts.fIndex = frameContext->getRequiredFrame(); | |
| 316 prevFrameMultiOpts.fHasPriorFrame = false; | |
| 317 prevFrameOpts.fFrameOptions = &prevFrameMultiOpts; | |
| 318 const Result prevResult = this->decodeFrame(true, prevFrameOpts, nullptr); | |
| 319 switch (prevResult) { | |
| 320 case kSuccess: | |
| 321 // Prior frame succeeded. Carry on. | |
| 322 break; | |
| 323 case kIncompleteInput: | |
| 324 // Prior frame was incomplete. So this frame cannot be d ecoded. | |
| 325 return kInvalidInput; | |
| 326 default: | |
| 327 return prevResult; | |
| 328 } | |
| 329 } | |
| 330 const GIFFrameContext* prevFrame = fReader->frameContext(frameContex t->getRequiredFrame()); | |
| 331 if (prevFrame->getDisposalMethod() == SkCodecAnimation::RestoreBGCol or_DisposalMethod) { | |
| 332 const SkIRect prevRect = prevFrame->frameRect(); | |
| 333 auto left = get_scaled_dimension(prevRect.fLeft, fSwizzler->samp leX()); | |
| 334 auto top = get_scaled_dimension(prevRect.fTop, fSwizzler->sample Y()); | |
| 335 void* const eraseDst = SkTAddOffset<void>(fDst, top * fDstRowByt es | |
| 336 + left * SkColorTypeBytesPerPixel(dstInfo.colorType())); | |
| 337 auto width = get_scaled_dimension(prevRect.width(), fSwizzler->s ampleX()); | |
| 338 auto height = get_scaled_dimension(prevRect.height(), fSwizzler- >sampleY()); | |
| 339 // fSwizzler->fill() would fill to the scaled width of the frame , but we want to fill | |
| 340 // to the scaled with of the width of the PRIOR frame, so we do all the scaling | |
| 341 // ourselves and call the static version. | |
| 342 SkSampler::Fill(dstInfo.makeWH(width, height), eraseDst, | |
| 343 fDstRowBytes, this->getFillValue(dstInfo), kNo_Z eroInitialized); | |
| 344 } | |
| 345 filledBackground = true; | |
| 346 } | |
| 347 | |
| 348 fFilledBackground = filledBackground; | |
| 349 if (filledBackground) { | |
| 350 // Report the full (scaled) height, since the client will never need to fill. | |
| 351 fRowsDecoded = get_scaled_dimension(dstInfo.height(), fSwizzler->sam pleY()); | |
| 352 } else { | |
| 353 // This will be updated by haveDecodedRow. | |
| 354 fRowsDecoded = 0; | |
| 355 } | |
| 356 } | |
| 357 | |
| 358 // Note: there is a difference between the following call to GIFImageReader: :decode | |
| 359 // returning false and leaving frameDecoded false: | |
| 360 // - If the method returns false, there was an error in the stream. We still treat this as | |
| 361 // incomplete, since we have already decoded some rows. | |
| 362 // - If frameDecoded is false, that just means that we do not have enough da ta. If more data | |
| 363 // is supplied, we may be able to continue decoding this frame. We also tr eat this as | |
| 364 // incomplete. | |
| 365 // FIXME: Ensure that we do not attempt to continue decoding if the method r eturns false and | |
| 366 // more data is supplied. | |
| 367 bool frameDecoded = false; | |
| 368 if (!fReader->decode(frameIndex, &frameDecoded) || !frameDecoded) { | |
| 369 if (rowsDecoded) { | |
| 370 *rowsDecoded = fRowsDecoded; | |
| 371 } | |
| 372 return kIncompleteInput; | |
| 373 } | |
| 374 | |
| 375 return kSuccess; | |
| 376 } | |
| 377 | |
| 515 uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const { | 378 uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const { |
| 516 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); | 379 // Note: Using fCurrColorTable relies on having called initializeColorTable already. |
| 517 return get_color_table_fill_value(dstInfo.colorType(), dstInfo.alphaType(), colorPtr, | 380 // This is (currently) safe because this method is only called when filling, after |
| 518 fFillIndex, nullptr); | 381 // initializeColorTable has been called. |
| 519 } | 382 // FIXME: Is there a way to make this less fragile? |
| 520 | 383 if (dstInfo.colorType() == kIndex_8_SkColorType && fCurrColorTableIsReal) { |
| 521 SkCodec::Result SkGifCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, | 384 // We only support index 8 for the first frame, for backwards |
| 522 const SkCodec::Options& opts, SkPMColor inputColorPtr[], int* inputColor Count) { | 385 // compatibity on Android, so we are using the color table for the first frame. |
| 523 return this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts); | 386 SkASSERT(!this->options().fFrameOptions || this->options().fFrameOptions ->fIndex == 0); |
| 524 } | 387 // Use the transparent index for the first frame. |
| 525 | 388 const size_t transPixel = fReader->frameContext(0)->transparentPixel(); |
| 526 void SkGifCodec::handleScanlineFrame(int count, int* rowsBeforeFrame, int* rowsI nFrame) { | 389 if (transPixel < (size_t) fCurrColorTable->count()) { |
| 527 if (fFrameIsSubset) { | 390 return transPixel; |
| 528 const int currRow = this->currScanline(); | 391 } |
| 529 | 392 |
| 530 // The number of rows that remain to be skipped before reaching rows tha t we | 393 const size_t backgroundIndex = fReader->getBackgroundIndex(); |
| 531 // actually must decode into. | 394 if (backgroundIndex < (size_t) fCurrColorTable->count()) { |
| 532 // This must be at least zero. We also make sure that it is less than o r | 395 // Note that this matches the old behavior in SkGifCodec, and the |
| 533 // equal to count, since we will skip at most count rows. | 396 // older behavior in SkImageDecoder_libgif. But in the case where |
| 534 *rowsBeforeFrame = SkTMin(count, SkTMax(0, fFrameRect.top() - currRow)); | 397 // the color table being used is a local color table, this is |
| 535 | 398 // wrong - the background index is supposed to apply to the global |
| 536 // Rows left to decode once we reach the start of the frame. | 399 // color table. That said, we cannot guarantee that we can use a |
| 537 const int rowsLeft = count - *rowsBeforeFrame; | 400 // background color from the global color table if there is a local |
| 538 | 401 // color table anyway. |
| 539 // Count the number of that extend beyond the bottom of the frame. We d o not | 402 return backgroundIndex; |
| 540 // need to decode into these rows. | 403 } |
| 541 const int rowsAfterFrame = SkTMax(0, currRow + rowsLeft - fFrameRect.bot tom()); | 404 |
| 542 | 405 // Neither index is useful, so we fall through to return |
| 543 // Set the actual number of source rows that we need to decode. | 406 // SK_ColorTRANSPARENT (i.e. 0). This arbitrary choice matches our old |
| 544 *rowsInFrame = rowsLeft - rowsAfterFrame; | 407 // behavior. |
| 408 } | |
| 409 // Using transparent as the fill value matches the behavior in Chromium, | |
| 410 // which ignores the background color. | |
| 411 // If the colorType is kIndex_8, and there was no color table (i.e. | |
| 412 // fCurrColorTableIsReal is false), this value (zero) corresponds to the | |
| 413 // only entry in the dummy color table provided to the client. | |
| 414 return SK_ColorTRANSPARENT; | |
| 415 } | |
| 416 | |
| 417 bool SkGifCodec::haveDecodedRow(size_t frameIndex, const unsigned char* rowBegin , | |
| 418 size_t rowNumber, unsigned repeatCount, bool wri teTransparentPixels) | |
| 419 { | |
| 420 const GIFFrameContext* frameContext = fReader->frameContext(frameIndex); | |
| 421 // The pixel data and coordinates supplied to us are relative to the frame's | |
| 422 // origin within the entire image size, i.e. | |
| 423 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee | |
| 424 // that width == (size().width() - frameContext->xOffset), so | |
| 425 // we must ensure we don't run off the end of either the source data or the | |
| 426 // row's X-coordinates. | |
| 427 const size_t width = frameContext->width(); | |
| 428 const int xBegin = frameContext->xOffset(); | |
| 429 const int yBegin = frameContext->yOffset() + rowNumber; | |
| 430 const int xEnd = std::min(static_cast<int>(frameContext->xOffset() + width), | |
| 431 this->getInfo().width()); | |
| 432 const int yEnd = std::min(static_cast<int>(frameContext->yOffset() + rowNumb er + repeatCount), | |
| 433 this->getInfo().height()); | |
| 434 // FIXME: No need to make the checks on width/xBegin/xEnd for every row. We could instead do | |
| 435 // this once in prepareToDecode. | |
| 436 if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= y Begin)) | |
| 437 return true; | |
| 438 | |
| 439 // Due to sampling (or subsetting, if we add that feature to SkGifCodec), th is may be out of | |
| 440 // the range we will draw to the destination. | |
| 441 if (yBegin > fLastRow || yEnd - 1 < fFirstRow) { | |
| 442 // FIXME: Should we exit early for non-interlaced decodes once we reach the last row? | |
| 443 return true; | |
| 444 } | |
| 445 | |
| 446 // yBegin is the first row in the non-sampled image. dstRow will be the row in the output, | |
| 447 // after potentially scaling it. | |
| 448 int dstRow = yBegin; | |
| 449 | |
| 450 if (fSwizzler->sampleY() > 1) { | |
| 451 // Check to see whether this row or one that falls in the repeatCount is needed in the | |
| 452 // output. | |
| 453 bool foundNecessaryRow = false; | |
| 454 for (unsigned i = 0; i < repeatCount; i++) { | |
| 455 const int potentialRow = yBegin - fFirstRow + i; | |
| 456 if (potentialRow < 0 ) { | |
| 457 continue; | |
| 458 } | |
| 459 if (fSwizzler->rowNeeded(potentialRow)) { | |
| 460 foundNecessaryRow = true; | |
| 461 dstRow = potentialRow / fSwizzler->sampleY(); | |
| 462 repeatCount -= i; | |
| 463 repeatCount = (repeatCount - 1) / fSwizzler->sampleY() + 1; | |
| 464 | |
| 465 // Make sure the repeatCount does not take us beyond the end of the dst | |
| 466 const size_t scaledHeight = get_scaled_dimension(this->dstInfo() .height(), | |
| 467 fSwizzler->samp leY()); | |
| 468 if (dstRow + repeatCount > scaledHeight) { | |
| 469 repeatCount = scaledHeight - dstRow; | |
| 470 SkASSERT(repeatCount >= 1); | |
| 471 } | |
| 472 break; | |
| 473 } | |
| 474 } | |
| 475 | |
| 476 if (!foundNecessaryRow) { | |
| 477 return true; | |
| 478 } | |
| 479 } | |
| 480 | |
| 481 if (!fFilledBackground) { | |
| 482 // At this point, we are definitely going to write the row, so count it towards the number | |
| 483 // of rows decoded. | |
| 484 // We do not consider the repeatCount, which only happens for interlaced , in which case we | |
| 485 // have already set fRowsDecoded to the proper value (reflecting that we have filled the | |
| 486 // background). | |
| 487 fRowsDecoded++; | |
| 488 } | |
| 489 | |
| 490 if (!fCurrColorTableIsReal) { | |
| 491 // No color table, so nothing to draw this frame. | |
| 492 // FIXME: We can abort even earlier - no need to decode this frame. | |
| 493 return true; | |
| 494 } | |
| 495 | |
| 496 // The swizzler takes care of offsetting into the dst width-wise. | |
| 497 void* dstLine = SkTAddOffset<void>(fDst, dstRow * fDstRowBytes); | |
| 498 | |
| 499 // We may or may not need to write transparent pixels to the buffer. | |
| 500 // If we're compositing against a previous image, it's wrong, and if | |
| 501 // we're writing atop a cleared, fully transparent buffer, it's | |
| 502 // unnecessary; but if we're decoding an interlaced gif and | |
| 503 // displaying it "Haeberli"-style, we must write these for passes | |
| 504 // beyond the first, or the initial passes will "show through" the | |
| 505 // later ones. | |
| 506 if (writeTransparentPixels) { | |
| 507 fSwizzler->swizzle(dstLine, rowBegin); | |
| 545 } else { | 508 } else { |
| 546 *rowsBeforeFrame = 0; | 509 // We cannot swizzle directly into the dst, since that will write the tr ansparent pixels. |
| 547 *rowsInFrame = count; | 510 // Instead, swizzle into a temporary buffer, and copy that into the dst. |
| 548 } | 511 const auto dstInfo = this->dstInfo(); |
| 549 } | 512 // Although onGetFillValue returns a uint64_t, we only use the low eight bits. |
| 550 | 513 memset(fTmpBuffer.get(), (uint8_t) this->onGetFillValue(dstInfo), dstInf o.minRowBytes()); |
| 551 int SkGifCodec::onGetScanlines(void* dst, int count, size_t rowBytes) { | 514 fSwizzler->swizzle(fTmpBuffer.get(), rowBegin); |
| 552 int rowsBeforeFrame; | 515 |
| 553 int rowsInFrame; | 516 const size_t offsetBytes = fSwizzler->swizzleOffsetBytes(); |
| 554 this->handleScanlineFrame(count, &rowsBeforeFrame, &rowsInFrame); | 517 switch (dstInfo.colorType()) { |
| 555 | 518 case kBGRA_8888_SkColorType: |
| 556 if (fFrameIsSubset) { | 519 case kRGBA_8888_SkColorType: { |
| 557 // Fill the requested rows | 520 uint32_t* dstPixel = SkTAddOffset<uint32_t>(dstLine, offsetBytes ); |
| 558 SkImageInfo fillInfo = this->dstInfo().makeWH(this->dstInfo().width(), c ount); | 521 uint32_t* srcPixel = SkTAddOffset<uint32_t>(fTmpBuffer.get(), of fsetBytes); |
| 559 uint64_t fillValue = this->onGetFillValue(this->dstInfo()); | 522 for (int i = 0; i < fSwizzler->swizzleWidth(); i++) { |
| 560 fSwizzler->fill(fillInfo, dst, rowBytes, fillValue, this->options().fZer oInitialized); | 523 // Technically SK_ColorTRANSPARENT is an SkPMColor, and srcP ixel would have |
| 561 | 524 // the opposite swizzle for the non-native swizzle, but TRAN SPARENT is all |
| 562 // Start to write pixels at the start of the image frame | 525 // zeroes, which is the same either way. |
| 563 dst = SkTAddOffset<void>(dst, rowBytes * rowsBeforeFrame); | 526 if (*srcPixel != SK_ColorTRANSPARENT) { |
| 564 } | 527 *dstPixel = *srcPixel; |
| 565 | 528 } |
| 566 for (int i = 0; i < rowsInFrame; i++) { | 529 dstPixel++; |
| 567 if (!this->readRow()) { | 530 srcPixel++; |
| 568 return i + rowsBeforeFrame; | 531 } |
| 569 } | 532 break; |
| 570 fSwizzler->swizzle(dst, fSrcBuffer.get()); | 533 } |
| 571 dst = SkTAddOffset<void>(dst, rowBytes); | 534 case kIndex_8_SkColorType: { |
| 572 } | 535 uint8_t* dstPixel = SkTAddOffset<uint8_t>(dstLine, offsetBytes); |
| 573 | 536 uint8_t* srcPixel = SkTAddOffset<uint8_t>(fTmpBuffer.get(), offs etBytes); |
| 574 return count; | 537 for (int i = 0; i < fSwizzler->swizzleWidth(); i++) { |
| 575 } | 538 if (*srcPixel != frameContext->transparentPixel()) { |
| 576 | 539 *dstPixel = *srcPixel; |
| 577 bool SkGifCodec::onSkipScanlines(int count) { | 540 } |
| 578 int rowsBeforeFrame; | 541 dstPixel++; |
| 579 int rowsInFrame; | 542 srcPixel++; |
| 580 this->handleScanlineFrame(count, &rowsBeforeFrame, &rowsInFrame); | 543 } |
| 581 | 544 break; |
| 582 for (int i = 0; i < rowsInFrame; i++) { | 545 } |
| 583 if (!this->readRow()) { | 546 default: |
| 584 return false; | 547 SkASSERT(false); |
| 548 break; | |
| 549 } | |
| 550 } | |
| 551 | |
| 552 // Tell the frame to copy the row data if need be. | |
| 553 if (repeatCount > 1) { | |
| 554 const size_t bytesPerPixel = SkColorTypeBytesPerPixel(this->dstInfo().co lorType()); | |
| 555 const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel; | |
| 556 void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetB ytes()); | |
| 557 void* dst = copiedLine; | |
| 558 for (unsigned i = 1; i < repeatCount; i++) { | |
| 559 dst = SkTAddOffset<void>(dst, fDstRowBytes); | |
| 560 memcpy(dst, copiedLine, bytesToCopy); | |
| 585 } | 561 } |
| 586 } | 562 } |
| 587 | 563 |
| 588 return true; | 564 return true; |
| 589 } | 565 } |
| 590 | |
| 591 SkCodec::SkScanlineOrder SkGifCodec::onGetScanlineOrder() const { | |
| 592 if (fGif->Image.Interlace) { | |
| 593 return kOutOfOrder_SkScanlineOrder; | |
| 594 } | |
| 595 return kTopDown_SkScanlineOrder; | |
| 596 } | |
| 597 | |
| 598 int SkGifCodec::onOutputScanline(int inputScanline) const { | |
| 599 if (fGif->Image.Interlace) { | |
| 600 if (inputScanline < fFrameRect.top() || inputScanline >= fFrameRect.bott om()) { | |
| 601 return inputScanline; | |
| 602 } | |
| 603 return get_output_row_interlaced(inputScanline - fFrameRect.top(), fFram eRect.height()) + | |
| 604 fFrameRect.top(); | |
| 605 } | |
| 606 return inputScanline; | |
| 607 } | |
| OLD | NEW |