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 || |
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 :( |
272 SavedImage saveExt; | 94 // FIXME: Gifs can actually be encoded with 4-bits per pixel. Using 8 works,
but we could skip |
273 SkAutoTCallVProc<SavedImage, FreeExtension> autoFreeExt(&saveExt); | 95 // expanding to 8 bits and take advantage of the SkSwizzler to work f
rom 4. |
274 saveExt.ExtensionBlocks = nullptr; | 96 // FIXME: We do not yet know whether the image has alpha, so report binary.
More specifically, |
275 saveExt.ExtensionBlockCount = 0; | 97 // we cannot know without parsing the whole stream, since an arbitrar
y frame could |
276 GifByteType* extData; | 98 // require clearing to the background, even though all preceding fram
es were opaque. |
277 int32_t extFunction; | 99 // Is there some way we can report that the first frame is opaque, wi
thout implying that |
278 | 100 // other frames are also opaque? |
279 // We will loop over components of gif images until we find an image. Once | 101 return SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, SkEncodedInfo::kBi
nary_Alpha, 8); |
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 } | 102 } |
359 | 103 |
360 bool SkGifCodec::GetDimensions(GifFileType* gif, SkISize* size, SkIRect* frameRe
ct) { | 104 SkGifCodec::SkGifCodec(GIFImageReader* reader) |
361 // Get the encoded dimension values | 105 : INHERITED(reader->screenWidth(), reader->screenHeight(), make_info(), null
ptr) |
362 SavedImage* image = &gif->SavedImages[gif->ImageCount - 1]; | 106 , fReader(reader) |
363 const GifImageDesc& desc = image->ImageDesc; | 107 , fTmpBuffer(nullptr) |
364 int frameLeft = desc.Left; | 108 , fSwizzler(nullptr) |
365 int frameTop = desc.Top; | 109 , fCurrColorTable(nullptr) |
366 int frameWidth = desc.Width; | 110 , fCurrColorTableIsReal(false) |
367 int frameHeight = desc.Height; | 111 , fFilledBackground(false) |
368 int width = gif->SWidth; | 112 , fFirstCallToIncrementalDecode(false) |
369 int height = gif->SHeight; | 113 , fDst(nullptr) |
| 114 , fDstRowBytes(0) |
| 115 , fRowsDecoded(0) |
| 116 { |
| 117 reader->setClient(this); |
| 118 } |
370 | 119 |
371 // Ensure that the decode dimensions are large enough to contain the frame | 120 std::vector<SkCodec::FrameInfo> SkGifCodec::onGetFrameInfo() { |
372 width = SkTMax(width, frameWidth + frameLeft); | 121 fReader->parse(GIFImageReader::GIFFrameCountQuery); |
373 height = SkTMax(height, frameHeight + frameTop); | 122 const size_t size = fReader->imagesCount(); |
| 123 std::vector<FrameInfo> result(size); |
| 124 for (size_t i = 0; i < size; i++) { |
| 125 const GIFFrameContext* frameContext = fReader->frameContext(i); |
| 126 result[i].fDuration = frameContext->delayTime(); |
| 127 result[i].fRequiredFrame = frameContext->getRequiredFrame(); |
| 128 } |
| 129 return result; |
| 130 } |
374 | 131 |
375 // All of these dimensions should be positive, as they are encoded as unsign
ed 16-bit integers. | 132 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 | 133 SkPMColor* inputColorPtr, int* inputColorCount) { |
377 // in fact positive. | 134 fCurrColorTable = std::move(fReader->getColorTable(dstInfo.colorType(), fram
eIndex)); |
378 if (frameLeft < 0 || frameTop < 0 || frameWidth < 0 || frameHeight < 0 || wi
dth <= 0 || | 135 fCurrColorTableIsReal = fCurrColorTable; |
379 height <= 0) { | 136 if (!fCurrColorTable) { |
380 return false; | 137 // This is possible for an empty frame. Create a dummy with one value (t
ransparent). |
| 138 SkPMColor color = SK_ColorTRANSPARENT; |
| 139 fCurrColorTable.reset(new SkColorTable(&color, 1)); |
381 } | 140 } |
382 | 141 |
383 frameRect->setXYWH(frameLeft, frameTop, frameWidth, frameHeight); | 142 if (inputColorCount) { |
384 size->set(width, height); | 143 *inputColorCount = fCurrColorTable->count(); |
385 return true; | 144 } |
| 145 |
| 146 copy_color_table(dstInfo, fCurrColorTable.get(), inputColorPtr, inputColorCo
unt); |
386 } | 147 } |
387 | 148 |
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 | 149 |
450 SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, SkPMColo
r* inputColorPtr, | 150 SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, SkPMColo
r* inputColorPtr, |
451 int* inputColorCount, const Options& opts) { | 151 int* inputColorCount, const Options& opts) { |
452 // Check for valid input parameters | 152 // Check for valid input parameters |
453 if (!conversion_possible_ignore_color_space(dstInfo, this->getInfo())) { | 153 if (!conversion_possible_ignore_color_space(dstInfo, this->getInfo())) { |
454 return gif_error("Cannot convert input type to output type.\n", kInvalid
Conversion); | 154 return gif_error("Cannot convert input type to output type.\n", kInvalid
Conversion); |
455 } | 155 } |
456 | 156 |
| 157 if (dstInfo.colorType() == kRGBA_F16_SkColorType) { |
| 158 // FIXME: This should be supported. |
| 159 return gif_error("GIF does not yet support F16.\n", kInvalidConversion); |
| 160 } |
| 161 |
| 162 if (opts.fSubset) { |
| 163 return gif_error("Subsets not supported.\n", kUnimplemented); |
| 164 } |
| 165 |
| 166 const size_t frameIndex = opts.fFrameOptions ? opts.fFrameOptions->fIndex :
0; |
| 167 if (frameIndex > 0 && dstInfo.colorType() == kIndex_8_SkColorType) { |
| 168 // FIXME: It is possible that a later frame can be decoded to index8, if
it does one of the |
| 169 // following: |
| 170 // - Covers the entire previous frame |
| 171 // - Shares a color table (and transparent index) with any prior frames
that are showing. |
| 172 // We must support index8 for the first frame to be backwards compatible
on Android, but |
| 173 // we do not (currently) need to support later frames as index8. |
| 174 return gif_error("Cannot decode multiframe gif (except frame 0) as index
8.\n", |
| 175 kInvalidConversion); |
| 176 } |
| 177 |
| 178 fReader->parse((GIFImageReader::GIFParseQuery) frameIndex); |
| 179 |
| 180 if (frameIndex >= fReader->imagesCount()) { |
| 181 return gif_error("frame index out of range!\n", kIncompleteInput); |
| 182 } |
| 183 |
| 184 fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]); |
| 185 |
457 // Initialize color table and copy to the client if necessary | 186 // Initialize color table and copy to the client if necessary |
458 this->initializeColorTable(dstInfo, inputColorPtr, inputColorCount); | 187 this->initializeColorTable(dstInfo, frameIndex, inputColorPtr, inputColorCou
nt); |
459 | 188 this->initializeSwizzler(dstInfo, frameIndex); |
460 this->initializeSwizzler(dstInfo, opts); | |
461 return kSuccess; | 189 return kSuccess; |
462 } | 190 } |
463 | 191 |
464 void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& o
pts) { | 192 void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, size_t frameInde
x) { |
465 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); | 193 const GIFFrameContext* frame = fReader->frameContext(frameIndex); |
466 const SkIRect* frameRect = fFrameIsSubset ? &fFrameRect : nullptr; | 194 // This is only called by prepareToDecode, which ensures frameIndex is in ra
nge. |
467 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), colorPtr,
dstInfo, opts, | 195 SkASSERT(frame); |
468 frameRect)); | |
469 SkASSERT(fSwizzler); | |
470 } | |
471 | 196 |
472 bool SkGifCodec::readRow() { | 197 const int xBegin = frame->xOffset(); |
473 return GIF_ERROR != DGifGetLine(fGif, fSrcBuffer.get(), fFrameRect.width()); | 198 const int xEnd = std::min(static_cast<int>(frame->xOffset() + frame->width()
), |
| 199 static_cast<int>(fReader->screenWidth())); |
| 200 |
| 201 // CreateSwizzler only reads left and right of the frame. We cannot use the
frame's raw |
| 202 // frameRect, since it might extend beyond the edge of the frame. |
| 203 SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0); |
| 204 |
| 205 // The default Options should be fine: |
| 206 // - we'll ignore if the memory is zero initialized - unless we're the first
frame, this won't |
| 207 // matter anyway. |
| 208 // - subsets are not supported for gif |
| 209 // - the swizzler does not need to know about the frame. |
| 210 // We may not be able to use the real Options anyway, since getPixels does n
ot store it (due to |
| 211 // a bug). |
| 212 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), |
| 213 fCurrColorTable->readColors(), dstInfo, Options(), &swizzleR
ect)); |
| 214 SkASSERT(fSwizzler.get()); |
474 } | 215 } |
475 | 216 |
476 /* | 217 /* |
477 * Initiates the gif decode | 218 * Initiates the gif decode |
478 */ | 219 */ |
479 SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo, | 220 SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo, |
480 void* dst, size_t dstRowBytes, | 221 void* pixels, size_t dstRowBytes, |
481 const Options& opts, | 222 const Options& opts, |
482 SkPMColor* inputColorPtr, | 223 SkPMColor* inputColorPtr, |
483 int* inputColorCount, | 224 int* inputColorCount, |
484 int* rowsDecoded) { | 225 int* rowsDecoded) { |
485 Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCoun
t, opts); | 226 Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCoun
t, opts); |
486 if (kSuccess != result) { | 227 if (kSuccess != result) { |
487 return result; | 228 return result; |
488 } | 229 } |
489 | 230 |
490 if (dstInfo.dimensions() != this->getInfo().dimensions()) { | 231 if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
491 return gif_error("Scaling not supported.\n", kInvalidScale); | 232 return gif_error("Scaling not supported.\n", kInvalidScale); |
492 } | 233 } |
493 | 234 |
494 // Initialize the swizzler | 235 fDst = pixels; |
495 if (fFrameIsSubset) { | 236 fDstRowBytes = dstRowBytes; |
496 // Fill the background | 237 |
497 SkSampler::Fill(dstInfo, dst, dstRowBytes, this->getFillValue(dstInfo), | 238 return this->decodeFrame(true, opts, rowsDecoded); |
498 opts.fZeroInitialized); | 239 } |
499 } | 240 |
500 | 241 SkCodec::Result SkGifCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo, |
501 // Iterate over rows of the input | 242 void* pixels, size_t dstRow
Bytes, |
502 for (int y = fFrameRect.top(); y < fFrameRect.bottom(); y++) { | 243 const SkCodec::Options& opt
s, |
503 if (!this->readRow()) { | 244 SkPMColor* inputColorPtr, |
504 *rowsDecoded = y; | 245 int* inputColorCount) { |
505 return gif_error("Could not decode line.\n", kIncompleteInput); | 246 Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCoun
t, opts); |
506 } | 247 if (result != kSuccess) { |
507 void* dstRow = SkTAddOffset<void>(dst, dstRowBytes * this->outputScanlin
e(y)); | 248 return result; |
508 fSwizzler->swizzle(dstRow, fSrcBuffer.get()); | 249 } |
509 } | 250 |
| 251 fDst = pixels; |
| 252 fDstRowBytes = dstRowBytes; |
| 253 |
| 254 fFirstCallToIncrementalDecode = true; |
| 255 |
510 return kSuccess; | 256 return kSuccess; |
511 } | 257 } |
512 | 258 |
513 // FIXME: This is similar to the implementation for bmp and png. Can we share m
ore code or | 259 SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) { |
514 // possibly make this non-virtual? | 260 // It is possible the client has appended more data. Parse, if needed. |
| 261 const auto& options = this->options(); |
| 262 const size_t frameIndex = options.fFrameOptions ? options.fFrameOptions->fIn
dex : 0; |
| 263 fReader->parse((GIFImageReader::GIFParseQuery) frameIndex); |
| 264 |
| 265 const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode; |
| 266 fFirstCallToIncrementalDecode = false; |
| 267 return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded)
; |
| 268 } |
| 269 |
| 270 SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts,
int* rowsDecoded) { |
| 271 const SkImageInfo& dstInfo = this->dstInfo(); |
| 272 const size_t frameIndex = opts.fFrameOptions ? opts.fFrameOptions->fIndex :
0; |
| 273 SkASSERT(frameIndex < fReader->imagesCount()); |
| 274 const GIFFrameContext* frameContext = fReader->frameContext(frameIndex); |
| 275 if (firstAttempt) { |
| 276 // rowsDecoded reports how many rows have been initialized, so a layer a
bove |
| 277 // can fill the rest. In some cases, we fill the background before decod
ing |
| 278 // (or it is already filled for us), so we report rowsDecoded to be the
full |
| 279 // height. |
| 280 bool filledBackground = false; |
| 281 if (frameContext->getRequiredFrame() == kIndependentFrame) { |
| 282 // We may need to clear to transparent for one of the following reas
ons: |
| 283 // - The frameRect does not cover the full bounds. haveDecodedRow wi
ll |
| 284 // only draw inside the frameRect, so we need to clear the rest. |
| 285 // - There is a valid transparent pixel value. (FIXME: I'm assuming |
| 286 // writeTransparentPixels will be false in this case, based on |
| 287 // Chromium's assumption that it would already be zeroed. If we |
| 288 // change that behavior, could we skip Filling here?) |
| 289 // - The frame is interlaced. There is no obvious way to fill |
| 290 // afterwards for an incomplete image. (FIXME: Does the first pass |
| 291 // cover all rows? If so, we do not have to fill here.) |
| 292 if (frameContext->frameRect() != this->getInfo().bounds() |
| 293 || frameContext->transparentPixel() < MAX_COLORS |
| 294 || frameContext->interlaced()) { |
| 295 // fill ignores the width (replaces it with the actual, scaled w
idth). |
| 296 // But we need to scale in Y. |
| 297 const int scaledHeight = get_scaled_dimension(dstInfo.height(), |
| 298 fSwizzler->sampleY
()); |
| 299 auto fillInfo = dstInfo.makeWH(0, scaledHeight); |
| 300 fSwizzler->fill(fillInfo, fDst, fDstRowBytes, this->getFillValue
(dstInfo), |
| 301 opts.fZeroInitialized); |
| 302 filledBackground = true; |
| 303 } |
| 304 } else { |
| 305 // Not independent |
| 306 if (opts.fFrameOptions && !opts.fFrameOptions->fHasPriorFrame) { |
| 307 // Decode that frame into pixels. |
| 308 Options prevFrameOpts(opts); |
| 309 MultiFrameOptions prevFrameMultiOpts; |
| 310 prevFrameMultiOpts.fIndex = frameContext->getRequiredFrame(); |
| 311 prevFrameMultiOpts.fHasPriorFrame = false; |
| 312 prevFrameOpts.fFrameOptions = &prevFrameMultiOpts; |
| 313 const Result prevResult = this->decodeFrame(true, prevFrameOpts,
nullptr); |
| 314 switch (prevResult) { |
| 315 case kSuccess: |
| 316 // Prior frame succeeded. Carry on. |
| 317 break; |
| 318 case kIncompleteInput: |
| 319 // Prior frame was incomplete. So this frame cannot be d
ecoded. |
| 320 return kInvalidInput; |
| 321 default: |
| 322 return prevResult; |
| 323 } |
| 324 } |
| 325 const auto* prevFrame = fReader->frameContext(frameContext->getRequi
redFrame()); |
| 326 if (prevFrame->getDisposalMethod() == SkCodecAnimation::RestoreBGCol
or_DisposalMethod) { |
| 327 const SkIRect prevRect = prevFrame->frameRect(); |
| 328 auto left = get_scaled_dimension(prevRect.fLeft, fSwizzler->samp
leX()); |
| 329 auto top = get_scaled_dimension(prevRect.fTop, fSwizzler->sample
Y()); |
| 330 void* const eraseDst = SkTAddOffset<void>(fDst, top * fDstRowByt
es |
| 331 + left * SkColorTypeBytesPerPixel(dstInfo.colorType())); |
| 332 auto width = get_scaled_dimension(prevRect.width(), fSwizzler->s
ampleX()); |
| 333 auto height = get_scaled_dimension(prevRect.height(), fSwizzler-
>sampleY()); |
| 334 // fSwizzler->fill() would fill to the scaled width of the frame
, but we want to |
| 335 // fill to the scaled with of the width of the PRIOR frame, so w
e do all the scaling |
| 336 // ourselves and call the static version. |
| 337 SkSampler::Fill(dstInfo.makeWH(width, height), eraseDst, |
| 338 fDstRowBytes, this->getFillValue(dstInfo), kNo_Z
eroInitialized); |
| 339 } |
| 340 filledBackground = true; |
| 341 } |
| 342 |
| 343 fFilledBackground = filledBackground; |
| 344 if (filledBackground) { |
| 345 // Report the full (scaled) height, since the client will never need
to fill. |
| 346 fRowsDecoded = get_scaled_dimension(dstInfo.height(), fSwizzler->sam
pleY()); |
| 347 } else { |
| 348 // This will be updated by haveDecodedRow. |
| 349 fRowsDecoded = 0; |
| 350 } |
| 351 } |
| 352 |
| 353 // Note: there is a difference between the following call to GIFImageReader:
:decode |
| 354 // returning false and leaving frameDecoded false: |
| 355 // - If the method returns false, there was an error in the stream. We still
treat this as |
| 356 // incomplete, since we have already decoded some rows. |
| 357 // - If frameDecoded is false, that just means that we do not have enough da
ta. If more data |
| 358 // is supplied, we may be able to continue decoding this frame. We also tr
eat this as |
| 359 // incomplete. |
| 360 // FIXME: Ensure that we do not attempt to continue decoding if the method r
eturns false and |
| 361 // more data is supplied. |
| 362 bool frameDecoded = false; |
| 363 if (!fReader->decode(frameIndex, &frameDecoded) || !frameDecoded) { |
| 364 if (rowsDecoded) { |
| 365 *rowsDecoded = fRowsDecoded; |
| 366 } |
| 367 return kIncompleteInput; |
| 368 } |
| 369 |
| 370 return kSuccess; |
| 371 } |
| 372 |
515 uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const { | 373 uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const { |
516 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); | 374 // Note: Using fCurrColorTable relies on having called initializeColorTable
already. |
517 return get_color_table_fill_value(dstInfo.colorType(), dstInfo.alphaType(),
colorPtr, | 375 // This is (currently) safe because this method is only called when filling,
after |
518 fFillIndex, nullptr); | 376 // initializeColorTable has been called. |
519 } | 377 // FIXME: Is there a way to make this less fragile? |
520 | 378 if (dstInfo.colorType() == kIndex_8_SkColorType && fCurrColorTableIsReal) { |
521 SkCodec::Result SkGifCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, | 379 // We only support index 8 for the first frame, for backwards |
522 const SkCodec::Options& opts, SkPMColor inputColorPtr[], int* inputColor
Count) { | 380 // compatibity on Android, so we are using the color table for the first
frame. |
523 return this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts); | 381 SkASSERT(!this->options().fFrameOptions || this->options().fFrameOptions
->fIndex == 0); |
524 } | 382 // Use the transparent index for the first frame. |
525 | 383 const size_t transPixel = fReader->frameContext(0)->transparentPixel(); |
526 void SkGifCodec::handleScanlineFrame(int count, int* rowsBeforeFrame, int* rowsI
nFrame) { | 384 if (transPixel < (size_t) fCurrColorTable->count()) { |
527 if (fFrameIsSubset) { | 385 return transPixel; |
528 const int currRow = this->currScanline(); | 386 } |
529 | 387 |
530 // The number of rows that remain to be skipped before reaching rows tha
t we | 388 const size_t backgroundIndex = fReader->getBackgroundIndex(); |
531 // actually must decode into. | 389 if (backgroundIndex < (size_t) fCurrColorTable->count()) { |
532 // This must be at least zero. We also make sure that it is less than o
r | 390 // Note that this matches the old behavior in SkGifCodec, and the |
533 // equal to count, since we will skip at most count rows. | 391 // older behavior in SkImageDecoder_libgif. But in the case where |
534 *rowsBeforeFrame = SkTMin(count, SkTMax(0, fFrameRect.top() - currRow)); | 392 // the color table being used is a local color table, this is |
535 | 393 // wrong - the background index is supposed to apply to the global |
536 // Rows left to decode once we reach the start of the frame. | 394 // color table. That said, we cannot guarantee that we can use a |
537 const int rowsLeft = count - *rowsBeforeFrame; | 395 // background color from the global color table if there is a local |
538 | 396 // color table anyway. |
539 // Count the number of that extend beyond the bottom of the frame. We d
o not | 397 return backgroundIndex; |
540 // need to decode into these rows. | 398 } |
541 const int rowsAfterFrame = SkTMax(0, currRow + rowsLeft - fFrameRect.bot
tom()); | 399 |
542 | 400 // Neither index is useful, so we fall through to return |
543 // Set the actual number of source rows that we need to decode. | 401 // SK_ColorTRANSPARENT (i.e. 0). This arbitrary choice matches our old |
544 *rowsInFrame = rowsLeft - rowsAfterFrame; | 402 // behavior. |
| 403 } |
| 404 // Using transparent as the fill value matches the behavior in Chromium, |
| 405 // which ignores the background color. |
| 406 // If the colorType is kIndex_8, and there was no color table (i.e. |
| 407 // fCurrColorTableIsReal is false), this value (zero) corresponds to the |
| 408 // only entry in the dummy color table provided to the client. |
| 409 return SK_ColorTRANSPARENT; |
| 410 } |
| 411 |
| 412 bool SkGifCodec::haveDecodedRow(size_t frameIndex, const unsigned char* rowBegin
, |
| 413 size_t rowNumber, unsigned repeatCount, bool wri
teTransparentPixels) |
| 414 { |
| 415 const GIFFrameContext* frameContext = fReader->frameContext(frameIndex); |
| 416 // The pixel data and coordinates supplied to us are relative to the frame's |
| 417 // origin within the entire image size, i.e. |
| 418 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee |
| 419 // that width == (size().width() - frameContext->xOffset), so |
| 420 // we must ensure we don't run off the end of either the source data or the |
| 421 // row's X-coordinates. |
| 422 const size_t width = frameContext->width(); |
| 423 const int xBegin = frameContext->xOffset(); |
| 424 const int yBegin = frameContext->yOffset() + rowNumber; |
| 425 const int xEnd = std::min(static_cast<int>(frameContext->xOffset() + width), |
| 426 this->getInfo().width()); |
| 427 const int yEnd = std::min(static_cast<int>(frameContext->yOffset() + rowNumb
er + repeatCount), |
| 428 this->getInfo().height()); |
| 429 // FIXME: No need to make the checks on width/xBegin/xEnd for every row. We
could instead do |
| 430 // this once in prepareToDecode. |
| 431 if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= y
Begin)) |
| 432 return true; |
| 433 |
| 434 // yBegin is the first row in the non-sampled image. dstRow will be the row
in the output, |
| 435 // after potentially scaling it. |
| 436 int dstRow = yBegin; |
| 437 |
| 438 const int sampleY = fSwizzler->sampleY(); |
| 439 if (sampleY > 1) { |
| 440 // Check to see whether this row or one that falls in the repeatCount is
needed in the |
| 441 // output. |
| 442 bool foundNecessaryRow = false; |
| 443 for (unsigned i = 0; i < repeatCount; i++) { |
| 444 const int potentialRow = yBegin + i; |
| 445 if (fSwizzler->rowNeeded(potentialRow)) { |
| 446 dstRow = potentialRow / sampleY; |
| 447 const int scaledHeight = get_scaled_dimension(this->dstInfo().he
ight(), sampleY); |
| 448 if (dstRow >= scaledHeight) { |
| 449 return true; |
| 450 } |
| 451 |
| 452 foundNecessaryRow = true; |
| 453 repeatCount -= i; |
| 454 |
| 455 repeatCount = (repeatCount - 1) / sampleY + 1; |
| 456 |
| 457 // Make sure the repeatCount does not take us beyond the end of
the dst |
| 458 if (dstRow + (int) repeatCount > scaledHeight) { |
| 459 repeatCount = scaledHeight - dstRow; |
| 460 SkASSERT(repeatCount >= 1); |
| 461 } |
| 462 break; |
| 463 } |
| 464 } |
| 465 |
| 466 if (!foundNecessaryRow) { |
| 467 return true; |
| 468 } |
| 469 } |
| 470 |
| 471 if (!fFilledBackground) { |
| 472 // At this point, we are definitely going to write the row, so count it
towards the number |
| 473 // of rows decoded. |
| 474 // We do not consider the repeatCount, which only happens for interlaced
, in which case we |
| 475 // have already set fRowsDecoded to the proper value (reflecting that we
have filled the |
| 476 // background). |
| 477 fRowsDecoded++; |
| 478 } |
| 479 |
| 480 if (!fCurrColorTableIsReal) { |
| 481 // No color table, so nothing to draw this frame. |
| 482 // FIXME: We can abort even earlier - no need to decode this frame. |
| 483 return true; |
| 484 } |
| 485 |
| 486 // The swizzler takes care of offsetting into the dst width-wise. |
| 487 void* dstLine = SkTAddOffset<void>(fDst, dstRow * fDstRowBytes); |
| 488 |
| 489 // We may or may not need to write transparent pixels to the buffer. |
| 490 // If we're compositing against a previous image, it's wrong, and if |
| 491 // we're writing atop a cleared, fully transparent buffer, it's |
| 492 // unnecessary; but if we're decoding an interlaced gif and |
| 493 // displaying it "Haeberli"-style, we must write these for passes |
| 494 // beyond the first, or the initial passes will "show through" the |
| 495 // later ones. |
| 496 if (writeTransparentPixels) { |
| 497 fSwizzler->swizzle(dstLine, rowBegin); |
545 } else { | 498 } else { |
546 *rowsBeforeFrame = 0; | 499 // We cannot swizzle directly into the dst, since that will write the tr
ansparent pixels. |
547 *rowsInFrame = count; | 500 // Instead, swizzle into a temporary buffer, and copy that into the dst. |
548 } | 501 const auto dstInfo = this->dstInfo(); |
549 } | 502 // Although onGetFillValue returns a uint64_t, we only use the low eight
bits. The return |
550 | 503 // value is either an 8 bit index (for index8) or SK_ColorTRANSPARENT, w
hich is all zeroes. |
551 int SkGifCodec::onGetScanlines(void* dst, int count, size_t rowBytes) { | 504 memset(fTmpBuffer.get(), (uint8_t) this->onGetFillValue(dstInfo), dstInf
o.minRowBytes()); |
552 int rowsBeforeFrame; | 505 fSwizzler->swizzle(fTmpBuffer.get(), rowBegin); |
553 int rowsInFrame; | 506 |
554 this->handleScanlineFrame(count, &rowsBeforeFrame, &rowsInFrame); | 507 const size_t offsetBytes = fSwizzler->swizzleOffsetBytes(); |
555 | 508 switch (dstInfo.colorType()) { |
556 if (fFrameIsSubset) { | 509 case kBGRA_8888_SkColorType: |
557 // Fill the requested rows | 510 case kRGBA_8888_SkColorType: { |
558 SkImageInfo fillInfo = this->dstInfo().makeWH(this->dstInfo().width(), c
ount); | 511 uint32_t* dstPixel = SkTAddOffset<uint32_t>(dstLine, offsetBytes
); |
559 uint64_t fillValue = this->onGetFillValue(this->dstInfo()); | 512 uint32_t* srcPixel = SkTAddOffset<uint32_t>(fTmpBuffer.get(), of
fsetBytes); |
560 fSwizzler->fill(fillInfo, dst, rowBytes, fillValue, this->options().fZer
oInitialized); | 513 for (int i = 0; i < fSwizzler->swizzleWidth(); i++) { |
561 | 514 // Technically SK_ColorTRANSPARENT is an SkPMColor, and srcP
ixel would have |
562 // Start to write pixels at the start of the image frame | 515 // the opposite swizzle for the non-native swizzle, but TRAN
SPARENT is all |
563 dst = SkTAddOffset<void>(dst, rowBytes * rowsBeforeFrame); | 516 // zeroes, which is the same either way. |
564 } | 517 if (*srcPixel != SK_ColorTRANSPARENT) { |
565 | 518 *dstPixel = *srcPixel; |
566 for (int i = 0; i < rowsInFrame; i++) { | 519 } |
567 if (!this->readRow()) { | 520 dstPixel++; |
568 return i + rowsBeforeFrame; | 521 srcPixel++; |
569 } | 522 } |
570 fSwizzler->swizzle(dst, fSrcBuffer.get()); | 523 break; |
571 dst = SkTAddOffset<void>(dst, rowBytes); | 524 } |
572 } | 525 case kIndex_8_SkColorType: { |
573 | 526 uint8_t* dstPixel = SkTAddOffset<uint8_t>(dstLine, offsetBytes); |
574 return count; | 527 uint8_t* srcPixel = SkTAddOffset<uint8_t>(fTmpBuffer.get(), offs
etBytes); |
575 } | 528 for (int i = 0; i < fSwizzler->swizzleWidth(); i++) { |
576 | 529 if (*srcPixel != frameContext->transparentPixel()) { |
577 bool SkGifCodec::onSkipScanlines(int count) { | 530 *dstPixel = *srcPixel; |
578 int rowsBeforeFrame; | 531 } |
579 int rowsInFrame; | 532 dstPixel++; |
580 this->handleScanlineFrame(count, &rowsBeforeFrame, &rowsInFrame); | 533 srcPixel++; |
581 | 534 } |
582 for (int i = 0; i < rowsInFrame; i++) { | 535 break; |
583 if (!this->readRow()) { | 536 } |
584 return false; | 537 default: |
| 538 SkASSERT(false); |
| 539 break; |
| 540 } |
| 541 } |
| 542 |
| 543 // Tell the frame to copy the row data if need be. |
| 544 if (repeatCount > 1) { |
| 545 const size_t bytesPerPixel = SkColorTypeBytesPerPixel(this->dstInfo().co
lorType()); |
| 546 const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel; |
| 547 void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetB
ytes()); |
| 548 void* dst = copiedLine; |
| 549 for (unsigned i = 1; i < repeatCount; i++) { |
| 550 dst = SkTAddOffset<void>(dst, fDstRowBytes); |
| 551 memcpy(dst, copiedLine, bytesToCopy); |
585 } | 552 } |
586 } | 553 } |
587 | 554 |
588 return true; | 555 return true; |
589 } | 556 } |
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 |