OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2006 Apple Computer, Inc. | 2 * Copyright (C) 2006 Apple Computer, Inc. |
3 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. | 3 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. |
4 * | 4 * |
5 * Portions are Copyright (C) 2001 mozilla.org | 5 * Portions are Copyright (C) 2001 mozilla.org |
6 * | 6 * |
7 * Other contributors: | 7 * Other contributors: |
8 * Stuart Parmenter <stuart@mozilla.com> | 8 * Stuart Parmenter <stuart@mozilla.com> |
9 * | 9 * |
10 * This library is free software; you can redistribute it and/or | 10 * This library is free software; you can redistribute it and/or |
(...skipping 20 matching lines...) Expand all Loading... | |
31 * licenses (the MPL or the GPL) and not to allow others to use your | 31 * licenses (the MPL or the GPL) and not to allow others to use your |
32 * version of this file under the LGPL, indicate your decision by | 32 * version of this file under the LGPL, indicate your decision by |
33 * deletingthe provisions above and replace them with the notice and | 33 * deletingthe provisions above and replace them with the notice and |
34 * other provisions required by the MPL or the GPL, as the case may be. | 34 * other provisions required by the MPL or the GPL, as the case may be. |
35 * If you do not delete the provisions above, a recipient may use your | 35 * If you do not delete the provisions above, a recipient may use your |
36 * version of this file under any of the LGPL, the MPL or the GPL. | 36 * version of this file under any of the LGPL, the MPL or the GPL. |
37 */ | 37 */ |
38 | 38 |
39 #include "platform/image-decoders/png/PNGImageDecoder.h" | 39 #include "platform/image-decoders/png/PNGImageDecoder.h" |
40 | 40 |
41 #include "platform/image-decoders/png/PNGImageReader.h" | |
42 #include "png.h" | 41 #include "png.h" |
43 #include "wtf/PtrUtil.h" | |
44 #include <memory> | 42 #include <memory> |
45 | 43 |
46 namespace blink { | 44 namespace blink { |
47 | 45 |
48 PNGImageDecoder::PNGImageDecoder(AlphaOption alphaOption, | 46 PNGImageDecoder::PNGImageDecoder(AlphaOption alphaOption, |
49 const ColorBehavior& colorBehavior, | 47 const ColorBehavior& colorBehavior, |
50 size_t maxDecodedBytes, | 48 size_t maxDecodedBytes, |
51 size_t offset) | 49 size_t offset) |
52 : ImageDecoder(alphaOption, colorBehavior, maxDecodedBytes), | 50 : ImageDecoder(alphaOption, colorBehavior, maxDecodedBytes), |
53 m_offset(offset) {} | 51 m_reader(this, offset), |
52 m_frameCount(0), | |
53 m_currentFrame(0), | |
54 m_repetitionCount(cAnimationLoopOnce), | |
55 m_hasAlphaChannel(false), | |
56 m_currentBufferSawAlpha(false), | |
57 m_isParsing(false) {} | |
54 | 58 |
55 PNGImageDecoder::~PNGImageDecoder() {} | 59 PNGImageDecoder::~PNGImageDecoder() {} |
56 | 60 |
61 bool PNGImageDecoder::setFailed() { | |
62 // Update the frame count to make sure it reflects the most up to date number | |
63 // of frames that were parsed. We don't want to set the decoder to the failed | |
64 // state if the reader was able to successfully parse some frames. | |
65 if (m_isParsing) | |
66 m_frameCount = m_reader.frameCount(); | |
67 | |
68 // There are three cases in which the decoder is invalidated: | |
69 // 1) No frames are received so far. | |
70 // 2) The image is not animated. | |
71 // 3) Decoding for the first frame fails. | |
72 if (m_frameCount == 0 || (m_reader.parseCompleted() && m_frameCount == 1) || | |
73 (!m_isParsing && m_currentFrame == 0)) | |
74 return ImageDecoder::setFailed(); | |
75 | |
76 // If decoding fails for later frames, we still want to show earlier frames, | |
77 // but the frame count needs to be adjusted. We do not want to shrink | |
78 // |m_frameBufferCache|, since clients may store pointers to later frames. | |
79 if (!m_isParsing) { | |
80 m_frameBufferCache[m_currentFrame].clearPixelData(); | |
81 m_frameBufferCache[m_currentFrame].setStatus(ImageFrame::FrameEmpty); | |
82 m_frameCount = m_currentFrame; | |
83 } | |
84 | |
85 // Set parseCompleted to true, so parse() returns early and thus won't update | |
86 // |m_frameCount| to the pre-failure value when it is called again. | |
87 m_reader.setParseCompleted(); | |
88 return false; | |
89 } | |
90 | |
91 size_t PNGImageDecoder::decodeFrameCount() { | |
92 parse(PNGImageReader::PNGParseQuery::PNGMetaDataQuery); | |
93 return m_frameCount; | |
94 } | |
95 | |
96 void PNGImageDecoder::decode(size_t index) { | |
97 if (failed()) | |
98 return; | |
99 | |
100 updateAggressivePurging(index); | |
101 | |
102 Vector<size_t> framesToDecode = findFramesToDecode(index); | |
103 for (auto i = framesToDecode.rbegin(); i != framesToDecode.rend(); i++) { | |
104 m_currentFrame = *i; | |
105 m_reader.decode(*m_data, *i); | |
106 if (failed() || !postDecodeProcessing(*i)) | |
107 return; | |
108 } | |
109 } | |
110 | |
111 void PNGImageDecoder::clearFrameBuffer(size_t frameIndex) { | |
112 m_reader.clearDecodeState(frameIndex); | |
113 ImageDecoder::clearFrameBuffer(frameIndex); | |
114 } | |
115 | |
116 void PNGImageDecoder::onInitFrameBuffer(size_t index) { | |
117 unsigned colorChannels = m_hasAlphaChannel ? 4 : 3; | |
118 if (PNG_INTERLACE_ADAM7 == | |
119 png_get_interlace_type(m_reader.pngPtr(), m_reader.infoPtr())) { | |
120 m_reader.createInterlaceBuffer(colorChannels * size().width() * | |
121 size().height()); | |
122 if (!m_reader.interlaceBuffer()) | |
123 setFailed(); | |
124 } | |
125 } | |
126 | |
127 bool PNGImageDecoder::canReusePreviousFrameBuffer(size_t frameIndex) const { | |
128 DCHECK(frameIndex < m_frameBufferCache.size()); | |
129 return m_frameBufferCache[frameIndex].getDisposalMethod() != | |
130 ImageFrame::DisposeOverwritePrevious; | |
131 } | |
132 | |
133 void PNGImageDecoder::parse(PNGImageReader::PNGParseQuery query) { | |
134 if (failed() || m_reader.parseCompleted()) | |
135 return; | |
136 | |
137 m_isParsing = true; | |
138 if (!m_reader.parse(*m_data, query) && isAllDataReceived()) | |
139 setFailed(); | |
140 else if (query == PNGImageReader::PNGParseQuery::PNGMetaDataQuery) | |
141 m_frameCount = m_reader.frameCount(); | |
142 | |
143 m_isParsing = false; | |
144 } | |
145 | |
146 void PNGImageDecoder::setRepetitionCount(size_t repetitionCount) { | |
147 m_repetitionCount = | |
148 (repetitionCount == 0) ? cAnimationLoopInfinite : repetitionCount; | |
149 } | |
150 | |
151 // This matches the existing behavior to loop once if decoding fails, but this | |
152 // should be changed to stick with m_repetitionCount to match other browsers. | |
153 // See crbug.com/267883 | |
154 int PNGImageDecoder::repetitionCount() const { | |
155 if (m_reader.parseCompleted() && m_reader.frameCount() == 1) | |
156 return cAnimationNone; | |
157 return failed() ? cAnimationLoopOnce : m_repetitionCount; | |
158 } | |
159 | |
160 // These are mapped according to: | |
161 // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu nk | |
162 static inline ImageFrame::DisposalMethod getDisposalMethod( | |
163 uint8_t disposalMethod) { | |
164 switch (disposalMethod) { | |
165 case 0: | |
cblume
2016/12/11 22:08:26
Nit: I like that there is a link there describing
joostouwerling
2016/12/12 14:48:50
Thanks! I agree that it is better. There was also
| |
166 return ImageFrame::DisposalMethod::DisposeKeep; | |
167 case 1: | |
168 return ImageFrame::DisposalMethod::DisposeOverwriteBgcolor; | |
169 case 2: | |
170 return ImageFrame::DisposalMethod::DisposeOverwritePrevious; | |
171 default: | |
172 return ImageFrame::DisposalMethod::DisposeNotSpecified; | |
173 } | |
174 } | |
175 | |
176 // These are mapped according to: | |
177 // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu nk | |
178 static inline ImageFrame::AlphaBlendSource getAlphaBlend(uint8_t alphaBlend) { | |
179 if (alphaBlend == 1) | |
180 return ImageFrame::AlphaBlendSource::BlendAtopPreviousFrame; | |
181 return ImageFrame::AlphaBlendSource::BlendAtopBgcolor; | |
182 } | |
183 | |
184 void PNGImageDecoder::initializeNewFrame(size_t index) { | |
185 const PNGImageReader::FrameInfo& frameInfo = m_reader.frameInfo(index); | |
186 ImageFrame* buffer = &m_frameBufferCache[index]; | |
187 | |
188 IntRect frameRectWithinSize = | |
189 intersection(frameInfo.frameRect, {IntPoint(), size()}); | |
190 buffer->setOriginalFrameRect(frameRectWithinSize); | |
191 buffer->setDuration(frameInfo.duration); | |
192 buffer->setDisposalMethod(getDisposalMethod(frameInfo.disposalMethod)); | |
193 buffer->setAlphaBlendSource(getAlphaBlend(frameInfo.alphaBlend)); | |
194 buffer->setRequiredPreviousFrameIndex( | |
195 findRequiredPreviousFrame(index, false)); | |
196 } | |
197 | |
57 inline float pngFixedToFloat(png_fixed_point x) { | 198 inline float pngFixedToFloat(png_fixed_point x) { |
58 return ((float)x) * 0.00001f; | 199 return ((float)x) * 0.00001f; |
59 } | 200 } |
60 | 201 |
61 inline sk_sp<SkColorSpace> readColorSpace(png_structp png, png_infop info) { | 202 inline sk_sp<SkColorSpace> readColorSpace(png_structp png, png_infop info) { |
62 if (png_get_valid(png, info, PNG_INFO_sRGB)) { | 203 if (png_get_valid(png, info, PNG_INFO_sRGB)) { |
63 return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); | 204 return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
64 } | 205 } |
65 | 206 |
66 png_charp name = nullptr; | 207 png_charp name = nullptr; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 // function. It's possible that we should guess sRGB transfer | 243 // function. It's possible that we should guess sRGB transfer |
103 // function, given that unmarked PNGs should be treated as sRGB. | 244 // function, given that unmarked PNGs should be treated as sRGB. |
104 // However, the current behavior matches Safari and Firefox. | 245 // However, the current behavior matches Safari and Firefox. |
105 } | 246 } |
106 } | 247 } |
107 | 248 |
108 return nullptr; | 249 return nullptr; |
109 } | 250 } |
110 | 251 |
111 void PNGImageDecoder::headerAvailable() { | 252 void PNGImageDecoder::headerAvailable() { |
112 png_structp png = m_reader->pngPtr(); | 253 png_structp png = m_reader.pngPtr(); |
113 png_infop info = m_reader->infoPtr(); | 254 png_infop info = m_reader.infoPtr(); |
114 png_uint_32 width = png_get_image_width(png, info); | |
115 png_uint_32 height = png_get_image_height(png, info); | |
116 | 255 |
117 // Protect against large PNGs. See http://bugzil.la/251381 for more details. | 256 png_uint_32 width, height; |
118 const unsigned long maxPNGSize = 1000000UL; | |
119 if (width > maxPNGSize || height > maxPNGSize) { | |
120 longjmp(JMPBUF(png), 1); | |
121 return; | |
122 } | |
123 | |
124 // Set the image size now that the image header is available. | |
125 if (!setSize(width, height)) { | |
126 longjmp(JMPBUF(png), 1); | |
127 return; | |
128 } | |
129 | |
130 int bitDepth, colorType, interlaceType, compressionType, filterType, channels; | 257 int bitDepth, colorType, interlaceType, compressionType, filterType, channels; |
131 png_get_IHDR(png, info, &width, &height, &bitDepth, &colorType, | 258 png_get_IHDR(png, info, &width, &height, &bitDepth, &colorType, |
132 &interlaceType, &compressionType, &filterType); | 259 &interlaceType, &compressionType, &filterType); |
133 | 260 |
134 // The options we set here match what Mozilla does. | 261 // The options we set here match what Mozilla does. |
135 | 262 |
136 // Expand to ensure we use 24-bit for RGB and 32-bit for RGBA. | 263 // Expand to ensure we use 24-bit for RGB and 32-bit for RGBA. |
137 if (colorType == PNG_COLOR_TYPE_PALETTE || | 264 if (colorType == PNG_COLOR_TYPE_PALETTE || |
138 (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8)) | 265 (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8)) |
139 png_set_expand(png); | 266 png_set_expand(png); |
140 | 267 |
141 png_bytep trns = 0; | 268 png_bytep trns = 0; |
142 int trnsCount = 0; | 269 int trnsCount = 0; |
143 if (png_get_valid(png, info, PNG_INFO_tRNS)) { | 270 if (png_get_valid(png, info, PNG_INFO_tRNS)) { |
144 png_get_tRNS(png, info, &trns, &trnsCount, 0); | 271 png_get_tRNS(png, info, &trns, &trnsCount, 0); |
145 png_set_expand(png); | 272 png_set_expand(png); |
146 } | 273 } |
147 | 274 |
148 if (bitDepth == 16) | 275 if (bitDepth == 16) |
149 png_set_strip_16(png); | 276 png_set_strip_16(png); |
150 | 277 |
151 if (colorType == PNG_COLOR_TYPE_GRAY || | 278 if (colorType == PNG_COLOR_TYPE_GRAY || |
152 colorType == PNG_COLOR_TYPE_GRAY_ALPHA) | 279 colorType == PNG_COLOR_TYPE_GRAY_ALPHA) |
153 png_set_gray_to_rgb(png); | 280 png_set_gray_to_rgb(png); |
154 | 281 |
155 if ((colorType & PNG_COLOR_MASK_COLOR) && !ignoresColorSpace()) { | 282 // Only set the size and the color space of the image once. Since non-first |
156 // We only support color profiles for color PALETTE and RGB[A] PNG. | 283 // frames also use this method, we don't want them to override the size of |
157 // Supporting color profiles for gray-scale images is slightly tricky, at | 284 // the image to the size of their frame rect. Frames also don't specify their |
158 // least using the CoreGraphics ICC library, because we expand gray-scale | 285 // own color space, so we need to set it only once. |
159 // images to RGB but we do not similarly transform the color profile. We'd | 286 if (!isDecodedSizeAvailable()) { |
160 // either need to transform the color profile or we'd need to decode into a | 287 // Protect against large PNGs. See http://bugzil.la/251381 for more details. |
161 // gray-scale image buffer and hand that to CoreGraphics. | 288 const unsigned long maxPNGSize = 1000000UL; |
162 sk_sp<SkColorSpace> colorSpace = readColorSpace(png, info); | 289 if (width > maxPNGSize || height > maxPNGSize) { |
163 if (colorSpace) { | 290 longjmp(JMPBUF(png), 1); |
164 setEmbeddedColorSpace(colorSpace); | 291 return; |
292 } | |
293 | |
294 // Set the image size now that the image header is available. | |
295 if (!setSize(width, height)) { | |
296 longjmp(JMPBUF(png), 1); | |
297 return; | |
298 } | |
299 | |
300 if ((colorType & PNG_COLOR_MASK_COLOR) && !ignoresColorSpace()) { | |
301 // We only support color profiles for color PALETTE and RGB[A] PNG. | |
302 // Supporting color profiles for gray-scale images is slightly tricky, at | |
303 // least using the CoreGraphics ICC library, because we expand gray-scale | |
304 // images to RGB but we do not similarly transform the color profile. We'd | |
305 // either need to transform the color profile or we'd need to decode into | |
306 // a | |
307 // gray-scale image buffer and hand that to CoreGraphics. | |
308 sk_sp<SkColorSpace> colorSpace = readColorSpace(png, info); | |
309 if (colorSpace) { | |
310 setEmbeddedColorSpace(colorSpace); | |
311 } | |
165 } | 312 } |
166 } | 313 } |
167 | 314 |
168 if (!hasEmbeddedColorSpace()) { | 315 if (!hasEmbeddedColorSpace()) { |
169 // TODO (msarett): | 316 // TODO (msarett): |
170 // Applying the transfer function (gamma) should be handled by | 317 // Applying the transfer function (gamma) should be handled by |
171 // SkColorSpaceXform. Here we always convert to a transfer function that | 318 // SkColorSpaceXform. Here we always convert to a transfer function that |
172 // is a 2.2 exponential. This is a little strange given that the dst | 319 // is a 2.2 exponential. This is a little strange given that the dst |
173 // transfer function is not necessarily a 2.2 exponential. | 320 // transfer function is not necessarily a 2.2 exponential. |
174 // TODO (msarett): | 321 // TODO (msarett): |
(...skipping 17 matching lines...) Expand all Loading... | |
192 | 339 |
193 // Tell libpng to send us rows for interlaced pngs. | 340 // Tell libpng to send us rows for interlaced pngs. |
194 if (interlaceType == PNG_INTERLACE_ADAM7) | 341 if (interlaceType == PNG_INTERLACE_ADAM7) |
195 png_set_interlace_handling(png); | 342 png_set_interlace_handling(png); |
196 | 343 |
197 // Update our info now. | 344 // Update our info now. |
198 png_read_update_info(png, info); | 345 png_read_update_info(png, info); |
199 channels = png_get_channels(png, info); | 346 channels = png_get_channels(png, info); |
200 ASSERT(channels == 3 || channels == 4); | 347 ASSERT(channels == 3 || channels == 4); |
201 | 348 |
202 m_reader->setHasAlpha(channels == 4); | 349 m_hasAlphaChannel = (channels == 4); |
203 | 350 m_currentBufferSawAlpha = false; |
204 if (m_reader->decodingSizeOnly()) { | |
205 // If we only needed the size, halt the reader. | |
206 #if PNG_LIBPNG_VER_MAJOR > 1 || \ | |
207 (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 5) | |
208 // Passing '0' tells png_process_data_pause() not to cache unprocessed data. | |
209 m_reader->setReadOffset(m_reader->currentBufferSize() - | |
210 png_process_data_pause(png, 0)); | |
211 #else | |
212 m_reader->setReadOffset(m_reader->currentBufferSize() - png->buffer_size); | |
213 png->buffer_size = 0; | |
214 #endif | |
215 } | |
216 } | 351 } |
217 | 352 |
218 void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, | 353 void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, |
219 unsigned rowIndex, | 354 unsigned rowIndex, |
220 int) { | 355 int) { |
221 if (m_frameBufferCache.isEmpty()) | 356 if (m_frameBufferCache.isEmpty()) |
222 return; | 357 return; |
223 | 358 |
224 // Initialize the framebuffer if needed. | 359 // When a client calls ImageDecoder::setMemoryAllocator *before* decoding the |
225 ImageFrame& buffer = m_frameBufferCache[0]; | 360 // frame count, the first frame won't get initialized correctly. The call will |
226 if (buffer.getStatus() == ImageFrame::FrameEmpty) { | 361 // resize |m_frameBufferCache| to 1, and therefore ImageDecoder::frameCount |
227 png_structp png = m_reader->pngPtr(); | 362 // will *not* call initializeNewFrame for the first frame, whether it is a |
228 if (!buffer.setSizeAndColorSpace(size().width(), size().height(), | 363 // static image or the first frame of an animated image. Amongst others, the |
229 colorSpaceForSkImages())) { | 364 // frame rect will not be set. If this is the case, initialize the frame here. |
230 longjmp(JMPBUF(png), 1); | 365 if (m_frameBufferCache[0].originalFrameRect().size() == IntSize(0, 0)) |
231 return; | 366 initializeNewFrame(0); |
232 } | |
233 | 367 |
234 unsigned colorChannels = m_reader->hasAlpha() ? 4 : 3; | 368 if (!initFrameBuffer(m_currentFrame)) { |
235 if (PNG_INTERLACE_ADAM7 == | 369 setFailed(); |
236 png_get_interlace_type(png, m_reader->infoPtr())) { | 370 return; |
237 m_reader->createInterlaceBuffer(colorChannels * size().width() * | |
238 size().height()); | |
239 if (!m_reader->interlaceBuffer()) { | |
240 longjmp(JMPBUF(png), 1); | |
241 return; | |
242 } | |
243 } | |
244 | |
245 buffer.setStatus(ImageFrame::FramePartial); | |
246 buffer.setHasAlpha(false); | |
247 | |
248 // For PNGs, the frame always fills the entire image. | |
249 buffer.setOriginalFrameRect(IntRect(IntPoint(), size())); | |
250 } | 371 } |
251 | 372 |
373 // This frameRect is already clipped, so that it fits within the size of the | |
374 // image. This is done in initializeNewFrame() after a frameCount() call. | |
375 ImageFrame& buffer = m_frameBufferCache[m_currentFrame]; | |
376 const IntRect& frameRect = buffer.originalFrameRect(); | |
377 | |
252 /* libpng comments (here to explain what follows). | 378 /* libpng comments (here to explain what follows). |
253 * | 379 * |
254 * this function is called for every row in the image. If the | 380 * this function is called for every row in the image. If the |
255 * image is interlacing, and you turned on the interlace handler, | 381 * image is interlacing, and you turned on the interlace handler, |
256 * this function will be called for every row in every pass. | 382 * this function will be called for every row in every pass. |
257 * Some of these rows will not be changed from the previous pass. | 383 * Some of these rows will not be changed from the previous pass. |
258 * When the row is not changed, the new_row variable will be NULL. | 384 * When the row is not changed, the new_row variable will be NULL. |
259 * The rows and passes are called in order, so you don't really | 385 * The rows and passes are called in order, so you don't really |
260 * need the row_num and pass, but I'm supplying them because it | 386 * need the row_num and pass, but I'm supplying them because it |
261 * may make your life easier. | 387 * may make your life easier. |
262 */ | 388 */ |
263 | 389 |
264 // Nothing to do if the row is unchanged, or the row is outside | 390 // Nothing to do if the row is unchanged, or the row is outside the image |
265 // the image bounds: libpng may send extra rows, ignore them to | 391 // bounds. In the case that a frame presents more data than the indicated |
266 // make our lives easier. | 392 // frame size, ignore the extra rows and thus use the frame size as the |
393 // source of truth. libpng may also send extra rows. Ignore those as well. | |
394 // This prevents us from trying to write outside of the image bounds. | |
267 if (!rowBuffer) | 395 if (!rowBuffer) |
268 return; | 396 return; |
269 int y = rowIndex; | 397 int y = rowIndex + frameRect.y(); |
270 if (y < 0 || y >= size().height()) | 398 DCHECK_GE(y, 0); |
399 if (y >= size().height()) | |
271 return; | 400 return; |
272 | 401 |
273 /* libpng comments (continued). | 402 /* libpng comments (continued). |
274 * | 403 * |
275 * For the non-NULL rows of interlaced images, you must call | 404 * For the non-NULL rows of interlaced images, you must call |
276 * png_progressive_combine_row() passing in the row and the | 405 * png_progressive_combine_row() passing in the row and the |
277 * old row. You can call this function for NULL rows (it will | 406 * old row. You can call this function for NULL rows (it will |
278 * just return) and for non-interlaced images (it just does the | 407 * just return) and for non-interlaced images (it just does the |
279 * memcpy for you) if it will make the code easier. Thus, you | 408 * memcpy for you) if it will make the code easier. Thus, you |
280 * can just do this for all cases: | 409 * can just do this for all cases: |
281 * | 410 * |
282 * png_progressive_combine_row(png_ptr, old_row, new_row); | 411 * png_progressive_combine_row(png_ptr, old_row, new_row); |
283 * | 412 * |
284 * where old_row is what was displayed for previous rows. Note | 413 * where old_row is what was displayed for previous rows. Note |
285 * that the first pass (pass == 0 really) will completely cover | 414 * that the first pass (pass == 0 really) will completely cover |
286 * the old row, so the rows do not have to be initialized. After | 415 * the old row, so the rows do not have to be initialized. After |
287 * the first pass (and only for interlaced images), you will have | 416 * the first pass (and only for interlaced images), you will have |
288 * to pass the current row, and the function will combine the | 417 * to pass the current row, and the function will combine the |
289 * old row and the new row. | 418 * old row and the new row. |
290 */ | 419 */ |
291 | 420 |
292 bool hasAlpha = m_reader->hasAlpha(); | 421 bool hasAlpha = m_hasAlphaChannel; |
293 png_bytep row = rowBuffer; | 422 png_bytep row = rowBuffer; |
294 | 423 |
295 if (png_bytep interlaceBuffer = m_reader->interlaceBuffer()) { | 424 if (png_bytep interlaceBuffer = m_reader.interlaceBuffer()) { |
296 unsigned colorChannels = hasAlpha ? 4 : 3; | 425 unsigned colorChannels = hasAlpha ? 4 : 3; |
297 row = interlaceBuffer + (rowIndex * colorChannels * size().width()); | 426 row = interlaceBuffer + (rowIndex * colorChannels * size().width()); |
298 png_progressive_combine_row(m_reader->pngPtr(), row, rowBuffer); | 427 png_progressive_combine_row(m_reader.pngPtr(), row, rowBuffer); |
299 } | 428 } |
300 | 429 |
301 // Write the decoded row pixels to the frame buffer. The repetitive | 430 // Write the decoded row pixels to the frame buffer. The repetitive |
302 // form of the row write loops is for speed. | 431 // form of the row write loops is for speed. |
303 ImageFrame::PixelData* const dstRow = buffer.getAddr(0, y); | 432 ImageFrame::PixelData* const dstRow = buffer.getAddr(frameRect.x(), y); |
304 unsigned alphaMask = 255; | 433 unsigned alphaMask = 255; |
305 int width = size().width(); | 434 int width = frameRect.width(); |
306 | 435 |
307 png_bytep srcPtr = row; | 436 png_bytep srcPtr = row; |
308 if (hasAlpha) { | 437 if (hasAlpha) { |
309 // Here we apply the color space transformation to the dst space. | 438 // Here we apply the color space transformation to the dst space. |
310 // It does not really make sense to transform to a gamma-encoded | 439 // It does not really make sense to transform to a gamma-encoded |
311 // space and then immediately after, perform a linear premultiply. | 440 // space and then immediately after, perform a linear premultiply. |
312 // Ideally we would pass kPremul_SkAlphaType to xform->apply(), | 441 // Ideally we would pass kPremul_SkAlphaType to xform->apply(), |
313 // instructing SkColorSpaceXform to perform the linear premultiply | 442 // instructing SkColorSpaceXform to perform the linear premultiply |
314 // while the pixels are a linear space. | 443 // while the pixels are a linear space. |
315 // We cannot do this because when we apply the gamma encoding after | 444 // We cannot do this because when we apply the gamma encoding after |
316 // the premultiply, we will very likely end up with valid pixels | 445 // the premultiply, we will very likely end up with valid pixels |
317 // where R, G, and/or B are greater than A. The legacy drawing | 446 // where R, G, and/or B are greater than A. The legacy drawing |
318 // pipeline does not know how to handle this. | 447 // pipeline does not know how to handle this. |
319 if (SkColorSpaceXform* xform = colorTransform()) { | 448 if (SkColorSpaceXform* xform = colorTransform()) { |
320 SkColorSpaceXform::ColorFormat colorFormat = | 449 SkColorSpaceXform::ColorFormat colorFormat = |
321 SkColorSpaceXform::kRGBA_8888_ColorFormat; | 450 SkColorSpaceXform::kRGBA_8888_ColorFormat; |
322 xform->apply(colorFormat, dstRow, colorFormat, srcPtr, size().width(), | 451 xform->apply(colorFormat, dstRow, colorFormat, srcPtr, size().width(), |
323 kUnpremul_SkAlphaType); | 452 kUnpremul_SkAlphaType); |
324 srcPtr = (png_bytep)dstRow; | 453 srcPtr = (png_bytep)dstRow; |
325 } | 454 } |
326 | 455 |
327 if (buffer.premultiplyAlpha()) { | 456 if (m_frameBufferCache[m_currentFrame].getAlphaBlendSource() == |
328 for (auto *dstPixel = dstRow; dstPixel < dstRow + width; | 457 ImageFrame::BlendAtopBgcolor) { |
329 dstPixel++, srcPtr += 4) { | 458 if (buffer.premultiplyAlpha()) { |
330 buffer.setRGBAPremultiply(dstPixel, srcPtr[0], srcPtr[1], srcPtr[2], | 459 for (auto *dstPixel = dstRow; dstPixel < dstRow + width; |
331 srcPtr[3]); | 460 dstPixel++, srcPtr += 4) { |
332 alphaMask &= srcPtr[3]; | 461 buffer.setRGBAPremultiply(dstPixel, srcPtr[0], srcPtr[1], srcPtr[2], |
462 srcPtr[3]); | |
463 alphaMask &= srcPtr[3]; | |
464 } | |
465 } else { | |
466 for (auto *dstPixel = dstRow; dstPixel < dstRow + width; | |
467 dstPixel++, srcPtr += 4) { | |
468 buffer.setRGBARaw(dstPixel, srcPtr[0], srcPtr[1], srcPtr[2], | |
469 srcPtr[3]); | |
470 alphaMask &= srcPtr[3]; | |
471 } | |
333 } | 472 } |
334 } else { | 473 } else { |
335 for (auto *dstPixel = dstRow; dstPixel < dstRow + width; | 474 // Now, the blend method is ImageFrame::BlendAtopPreviousFrame. Since the |
336 dstPixel++, srcPtr += 4) { | 475 // frame data of the previous frame is copied at initFrameBuffer, we can |
337 buffer.setRGBARaw(dstPixel, srcPtr[0], srcPtr[1], srcPtr[2], srcPtr[3]); | 476 // blend the pixel of this frame, stored in |srcPtr|, over the previous |
338 alphaMask &= srcPtr[3]; | 477 // pixel stored in |dstPixel|. |
478 if (buffer.premultiplyAlpha()) { | |
479 for (auto *dstPixel = dstRow; dstPixel < dstRow + width; | |
480 dstPixel++, srcPtr += 4) { | |
481 buffer.blendRGBAPremultiplied(dstPixel, srcPtr[0], srcPtr[1], | |
482 srcPtr[2], srcPtr[3]); | |
483 alphaMask &= srcPtr[3]; | |
484 } | |
485 } else { | |
486 for (auto *dstPixel = dstRow; dstPixel < dstRow + width; | |
487 dstPixel++, srcPtr += 4) { | |
488 buffer.blendRGBARaw(dstPixel, srcPtr[0], srcPtr[1], srcPtr[2], | |
489 srcPtr[3]); | |
490 alphaMask &= srcPtr[3]; | |
491 } | |
339 } | 492 } |
340 } | 493 } |
494 | |
495 if (alphaMask != 255 && !m_currentBufferSawAlpha) | |
496 m_currentBufferSawAlpha = true; | |
497 | |
341 } else { | 498 } else { |
342 for (auto *dstPixel = dstRow; dstPixel < dstRow + width; | 499 for (auto *dstPixel = dstRow; dstPixel < dstRow + width; |
343 dstPixel++, srcPtr += 3) { | 500 dstPixel++, srcPtr += 3) { |
344 buffer.setRGBARaw(dstPixel, srcPtr[0], srcPtr[1], srcPtr[2], 255); | 501 buffer.setRGBARaw(dstPixel, srcPtr[0], srcPtr[1], srcPtr[2], 255); |
345 } | 502 } |
346 | 503 |
347 // We'll apply the color space xform to opaque pixels after they have been | 504 // We'll apply the color space xform to opaque pixels after they have been |
348 // written to the ImageFrame, purely because SkColorSpaceXform supports | 505 // written to the ImageFrame, purely because SkColorSpaceXform supports |
349 // RGBA (and not RGB). | 506 // RGBA (and not RGB). |
350 if (SkColorSpaceXform* xform = colorTransform()) { | 507 if (SkColorSpaceXform* xform = colorTransform()) { |
351 xform->apply(xformColorFormat(), dstRow, xformColorFormat(), dstRow, | 508 xform->apply(xformColorFormat(), dstRow, xformColorFormat(), dstRow, |
352 size().width(), kOpaque_SkAlphaType); | 509 size().width(), kOpaque_SkAlphaType); |
353 } | 510 } |
354 } | 511 } |
355 | 512 |
356 if (alphaMask != 255 && !buffer.hasAlpha()) | 513 buffer.setPixelsChanged(true); |
357 buffer.setHasAlpha(true); | 514 } |
358 | 515 |
359 buffer.setPixelsChanged(true); | 516 bool PNGImageDecoder::frameIsCompleteAtIndex(size_t index) const { |
517 // For non-animated images, return whether the status of the frame is | |
518 // ImageFrame::FrameComplete through ImageDecoder::frameIsCompleteAtIndex. | |
519 // We only know that the frame is fully received when it is successfully | |
520 // decoded, since there is no explicit check whether the IEND chunk has been | |
521 // received. | |
522 if (index == 0 && m_reader.parseCompleted() && m_frameCount == 1) | |
523 return ImageDecoder::frameIsCompleteAtIndex(index); | |
524 | |
525 // For first frames of animated images, PNGImageReader exposes whether it is | |
526 // fully received through firstFrameFullyReceived(). Non-first frames are | |
527 // reported by |m_reader| once it has parsed all data for that frame, so we | |
528 // can simply return if the index is below the reported frame count. We have | |
529 // to use |m_frameCount| here, and not |m_frameBufferCache|.size(), since | |
530 // |m_frameCount| gets adjusted when decoding or parsing fails, but | |
531 // |m_frameBufferCache| does not, as explained above at | |
532 // PNGImageDecoder::setFailed(). | |
533 if (index == 0) | |
534 return m_reader.firstFrameFullyReceived(); | |
535 return (index < m_frameCount); | |
536 } | |
537 | |
538 float PNGImageDecoder::frameDurationAtIndex(size_t index) const { | |
539 return (index < m_frameCount ? m_frameBufferCache[index].duration() : 0); | |
360 } | 540 } |
361 | 541 |
362 void PNGImageDecoder::complete() { | 542 void PNGImageDecoder::complete() { |
363 if (m_frameBufferCache.isEmpty()) | 543 if (m_frameBufferCache.isEmpty()) |
364 return; | 544 return; |
365 | 545 |
366 m_frameBufferCache[0].setStatus(ImageFrame::FrameComplete); | 546 // We don't know for sure at this point if we've received enough pixels to |
367 } | 547 // completely fill the frame. This is not taken into account. The error could |
548 // be in the indicated frame size, or in the frame data chunks. | |
549 // | |
550 // If the frame size is wrong, we could correct for that, but later frames | |
551 // may depend on this frame's size when they're initialized in | |
552 // initFrameBuffer(), in the case the disposal method of this frame is | |
553 // DisposeOverwriteBgColor. There is no informed decision that can be made | |
554 // about what the author intended, so we stay with the indicated frame size. | |
555 // | |
556 // In the case the data chunk is too small, there's not much we can do. This | |
557 // method only gets called when the frame end has been received, so the | |
558 // encoder indicated that all frame data is received. It could be an encoding | |
559 // error, or it may be an intentional file size reduction. | |
560 // | |
561 // Therefore, the frame size and pixels that aren't decoded are left as-is. | |
368 | 562 |
369 inline bool isComplete(const PNGImageDecoder* decoder) { | 563 if (m_reader.interlaceBuffer()) |
370 return decoder->frameIsCompleteAtIndex(0); | 564 m_reader.clearInterlaceBuffer(); |
371 } | |
372 | 565 |
373 void PNGImageDecoder::decode(bool onlySize) { | 566 ImageFrame& buffer = m_frameBufferCache[m_currentFrame]; |
374 if (failed()) | 567 buffer.setStatus(ImageFrame::FrameComplete); |
375 return; | |
376 | 568 |
377 if (!m_reader) | 569 if (!m_currentBufferSawAlpha) |
378 m_reader = WTF::makeUnique<PNGImageReader>(this, m_offset); | 570 correctAlphaWhenFrameBufferSawNoAlpha(m_currentFrame); |
379 | |
380 // If we couldn't decode the image but have received all the data, decoding | |
381 // has failed. | |
382 if (!m_reader->decode(*m_data, onlySize) && isAllDataReceived()) | |
383 setFailed(); | |
384 | |
385 // If decoding is done or failed, we don't need the PNGImageReader anymore. | |
386 if (isComplete(this) || failed()) | |
387 m_reader.reset(); | |
388 } | 571 } |
389 | 572 |
390 } // namespace blink | 573 } // namespace blink |
OLD | NEW |