Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Side by Side Diff: third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoder.cpp

Issue 2386453003: WIP: Implement APNG (Closed)
Patch Set: Basic frame decoding with tests, no alpha blending and disposal yet Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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"
41 #include "png.h" 42 #include "png.h"
42 #include "wtf/PtrUtil.h"
43 #include <memory> 43 #include <memory>
44 44
45 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR) 45 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR)
46 #error version error: compile against a versioned libpng. 46 #error version error: compile against a versioned libpng.
47 #endif 47 #endif
48 #if USE(QCMSLIB) 48 #if USE(QCMSLIB)
49 #include "qcms.h" 49 #include "qcms.h"
50 #endif 50 #endif
51 51
52 #if PNG_LIBPNG_VER_MAJOR > 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MIN OR >= 4) 52 #if PNG_LIBPNG_VER_MAJOR > 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MIN OR >= 4)
53 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr) 53 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr)
54 #else 54 #else
55 #define JMPBUF(png_ptr) png_ptr->jmpbuf 55 #define JMPBUF(png_ptr) png_ptr->jmpbuf
56 #endif 56 #endif
57 57
58 namespace {
59
60 inline blink::PNGImageDecoder* imageDecoder(png_structp png)
61 {
62 return static_cast<blink::PNGImageDecoder*>(png_get_progressive_ptr(png));
63 }
64
65 void PNGAPI pngHeaderAvailable(png_structp png, png_infop)
66 {
67 imageDecoder(png)->headerAvailable();
68 }
69
70 void PNGAPI pngRowAvailable(png_structp png, png_bytep row, png_uint_32 rowIndex , int state)
71 {
72 imageDecoder(png)->rowAvailable(row, rowIndex, state);
73 }
74
75 void PNGAPI pngComplete(png_structp png, png_infop)
76 {
77 imageDecoder(png)->complete();
78 }
79
80 void PNGAPI pngFailed(png_structp png, png_const_charp)
81 {
82 longjmp(JMPBUF(png), 1);
83 }
84
85 } // namespace
86 58
87 namespace blink { 59 namespace blink {
88 60
89 class PNGImageReader final {
90 USING_FAST_MALLOC(PNGImageReader);
91 WTF_MAKE_NONCOPYABLE(PNGImageReader);
92 public:
93 PNGImageReader(PNGImageDecoder* decoder, size_t readOffset)
94 : m_decoder(decoder)
95 , m_readOffset(readOffset)
96 , m_currentBufferSize(0)
97 , m_decodingSizeOnly(false)
98 , m_hasAlpha(false)
99 #if USE(QCMSLIB)
100 , m_rowBuffer()
101 #endif
102 {
103 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0);
104 m_info = png_create_info_struct(m_png);
105 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, pngRow Available, pngComplete);
106 }
107
108 ~PNGImageReader()
109 {
110 png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0);
111 ASSERT(!m_png && !m_info);
112
113 m_readOffset = 0;
114 }
115
116 bool decode(const SegmentReader& data, bool sizeOnly)
117 {
118 m_decodingSizeOnly = sizeOnly;
119
120 // We need to do the setjmp here. Otherwise bad things will happen.
121 if (setjmp(JMPBUF(m_png)))
122 return m_decoder->setFailed();
123
124 const char* segment;
125 while (size_t segmentLength = data.getSomeData(segment, m_readOffset)) {
126 m_readOffset += segmentLength;
127 m_currentBufferSize = m_readOffset;
128 png_process_data(m_png, m_info, reinterpret_cast<png_bytep>(const_ca st<char*>(segment)), segmentLength);
129 if (sizeOnly ? m_decoder->isDecodedSizeAvailable() : m_decoder->fram eIsCompleteAtIndex(0))
130 return true;
131 }
132
133 return false;
134 }
135
136 png_structp pngPtr() const { return m_png; }
137 png_infop infoPtr() const { return m_info; }
138
139 size_t getReadOffset() const { return m_readOffset; }
140 void setReadOffset(size_t offset) { m_readOffset = offset; }
141 size_t currentBufferSize() const { return m_currentBufferSize; }
142 bool decodingSizeOnly() const { return m_decodingSizeOnly; }
143 void setHasAlpha(bool hasAlpha) { m_hasAlpha = hasAlpha; }
144 bool hasAlpha() const { return m_hasAlpha; }
145
146 png_bytep interlaceBuffer() const { return m_interlaceBuffer.get(); }
147 void createInterlaceBuffer(int size) { m_interlaceBuffer = wrapArrayUnique(n ew png_byte[size]); }
148 #if USE(QCMSLIB)
149 png_bytep rowBuffer() const { return m_rowBuffer.get(); }
150 void createRowBuffer(int size) { m_rowBuffer = wrapArrayUnique(new png_byte[ size]); }
151 #endif
152
153 private:
154 png_structp m_png;
155 png_infop m_info;
156 PNGImageDecoder* m_decoder;
157 size_t m_readOffset;
158 size_t m_currentBufferSize;
159 bool m_decodingSizeOnly;
160 bool m_hasAlpha;
161 std::unique_ptr<png_byte[]> m_interlaceBuffer;
162 #if USE(QCMSLIB)
163 std::unique_ptr<png_byte[]> m_rowBuffer;
164 #endif
165 };
166
167 PNGImageDecoder::PNGImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOp tion colorOptions, size_t maxDecodedBytes, size_t offset) 61 PNGImageDecoder::PNGImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOp tion colorOptions, size_t maxDecodedBytes, size_t offset)
168 : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes) 62 : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes)
169 , m_offset(offset) 63 , m_offset(offset)
64 , m_metaDataDecoded(false)
65 , m_frameCount(0)
66 , m_currentFrame(0)
67 , m_repetitionCount(cAnimationLoopOnce)
68 , m_decodedFrameCount(false)
scroggo_chromium 2016/10/25 14:59:05 This variable is never read.
170 { 69 {
171 } 70 }
172 71
173 PNGImageDecoder::~PNGImageDecoder() 72 PNGImageDecoder::~PNGImageDecoder()
174 { 73 {
175 } 74 }
176 75
76 size_t PNGImageDecoder::decodeFrameCount()
77 {
78 m_decodedFrameCount = true;
79 if (!m_metaDataDecoded)
80 parse(PNGParseQuery::PNGMetaDataQuery);
81 return m_frameCount;
82 }
83
84 inline bool frameComplete(ImageFrame& frame)
85 {
86 return frame.getStatus() == ImageFrame::FrameComplete;
87 }
88
89 void PNGImageDecoder::decode(size_t index)
90 {
91 m_currentFrame = index;
92 m_reader->decode(*m_data, index);
93 }
94
95 void PNGImageDecoder::parse(PNGParseQuery query)
96 {
97 if (failed())
98 return;
99
100 if (!m_reader)
101 m_reader = wrapUnique(new PNGImageReader(this, m_offset));
102
103 if (!m_reader->parse(*m_data, query) && isAllDataReceived())
104 setFailed();
105
106 if (query == PNGParseQuery::PNGMetaDataQuery)
107 m_frameCount = m_reader->frameCount();
108 }
109
110 void PNGImageDecoder::setRepetitionCount(size_t repetitionCount)
111 {
112 m_repetitionCount = (repetitionCount == 0) ? cAnimationLoopInfinite
113 : repetitionCount;
114 }
115
116 // This matches the existing behavior to loop once if decoding fails, but this
117 // should be changed to stick with m_repititionCount to match other browsers.
scroggo_chromium 2016/10/25 14:59:06 nit: m_repetitionCount
118 // See crbug.com/267883
119 int PNGImageDecoder::repetitionCount() const
120 {
121 if (m_metaDataDecoded && isAllDataReceived() && m_reader->frameCount() == 1)
122 return cAnimationNone;
123 return failed() ? cAnimationLoopOnce : m_repetitionCount;
124 }
125
126 // These are mapped according to:
127 // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu nk
128 inline ImageFrame::DisposalMethod getDisposalMethod(uint8_t disposalMethod)
scroggo_chromium 2016/10/25 14:59:06 nit: These inline methods should also be static (o
129 {
130 switch (disposalMethod) {
131 case 0:
scroggo_chromium 2016/10/25 14:59:06 I *think* this should line up with switch - pretty
joostouwerling 2016/10/26 15:44:16 ImageDecoder.cpp and GIFImageDecoder.cpp both inde
132 return ImageFrame::DisposalMethod::DisposeKeep;
133 case 1:
134 return ImageFrame::DisposalMethod::DisposeOverwriteBgcolor;
135 case 2:
136 return ImageFrame::DisposalMethod::DisposeOverwritePrevious;
scroggo_chromium 2016/10/25 14:59:06 There should be a default case here.
137 }
138 return ImageFrame::DisposalMethod::DisposeNotSpecified;
scroggo_chromium 2016/10/25 14:59:06 Personally, I find it odd that we distinguish betw
139 }
140
141
142 // These are mapped according to:
143 // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu nk
144 inline ImageFrame::AlphaBlendSource getAlphaBlend(uint8_t alphaBlend)
145 {
146 if (alphaBlend == 1)
147 return ImageFrame::AlphaBlendSource::BlendAtopPreviousFrame;
148 return ImageFrame::AlphaBlendSource::BlendAtopBgcolor;
149 }
150
151 void PNGImageDecoder::initializeNewFrame(size_t index)
152 {
153 const PNGImageReader::FrameInfo& frameInfo = m_reader->frameInfo(index);
154 ImageFrame* buffer = &m_frameBufferCache[index];
155
156 IntRect frameRectWithinSize = intersection(frameInfo.frameRect,
157 {IntPoint(), size()});
158 buffer->setOriginalFrameRect(frameRectWithinSize);
159 buffer->setDuration(frameInfo.duration);
160 buffer->setDisposalMethod(getDisposalMethod(frameInfo.disposalMethod));
161 buffer->setAlphaBlendSource(getAlphaBlend(frameInfo.alphaBlend));
162 }
163
177 void PNGImageDecoder::headerAvailable() 164 void PNGImageDecoder::headerAvailable()
178 { 165 {
179 png_structp png = m_reader->pngPtr(); 166 png_structp png = m_reader->pngPtr();
180 png_infop info = m_reader->infoPtr(); 167 png_infop info = m_reader->infoPtr();
181 png_uint_32 width = png_get_image_width(png, info); 168 png_uint_32 width = png_get_image_width(png, info);
182 png_uint_32 height = png_get_image_height(png, info); 169 png_uint_32 height = png_get_image_height(png, info);
183 170
184 // Protect against large PNGs. See http://bugzil.la/251381 for more details. 171 // Only set the size of the image once. Since single frames also use this
185 const unsigned long maxPNGSize = 1000000UL; 172 // method, we don't want them to override the size to their frame rect.
186 if (width > maxPNGSize || height > maxPNGSize) { 173 if (!isDecodedSizeAvailable()) {
187 longjmp(JMPBUF(png), 1); 174 // Protect against large PNGs. See http://bugzil.la/251381 for more deta ils.
188 return; 175 const unsigned long maxPNGSize = 1000000UL;
189 } 176 if (width > maxPNGSize || height > maxPNGSize) {
177 longjmp(JMPBUF(png), 1);
178 return;
179 }
190 180
191 // Set the image size now that the image header is available. 181 // Set the image size now that the image header is available.
192 if (!setSize(width, height)) { 182 if (!setSize(width, height)) {
193 longjmp(JMPBUF(png), 1); 183 longjmp(JMPBUF(png), 1);
194 return; 184 return;
185 }
195 } 186 }
196 187
197 int bitDepth, colorType, interlaceType, compressionType, filterType, channel s; 188 int bitDepth, colorType, interlaceType, compressionType, filterType, channel s;
198 png_get_IHDR(png, info, &width, &height, &bitDepth, &colorType, &interlaceTy pe, &compressionType, &filterType); 189 png_get_IHDR(png, info, &width, &height, &bitDepth, &colorType, &interlaceTy pe, &compressionType, &filterType);
199 190
200 // The options we set here match what Mozilla does. 191 // The options we set here match what Mozilla does.
201 192
202 // Expand to ensure we use 24-bit for RGB and 32-bit for RGBA. 193 // Expand to ensure we use 24-bit for RGB and 32-bit for RGBA.
203 if (colorType == PNG_COLOR_TYPE_PALETTE || (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8)) 194 if (colorType == PNG_COLOR_TYPE_PALETTE || (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8))
204 png_set_expand(png); 195 png_set_expand(png);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 // Tell libpng to send us rows for interlaced pngs. 254 // Tell libpng to send us rows for interlaced pngs.
264 if (interlaceType == PNG_INTERLACE_ADAM7) 255 if (interlaceType == PNG_INTERLACE_ADAM7)
265 png_set_interlace_handling(png); 256 png_set_interlace_handling(png);
266 257
267 // Update our info now. 258 // Update our info now.
268 png_read_update_info(png, info); 259 png_read_update_info(png, info);
269 channels = png_get_channels(png, info); 260 channels = png_get_channels(png, info);
270 ASSERT(channels == 3 || channels == 4); 261 ASSERT(channels == 3 || channels == 4);
271 262
272 m_reader->setHasAlpha(channels == 4); 263 m_reader->setHasAlpha(channels == 4);
273
274 if (m_reader->decodingSizeOnly()) {
275 // If we only needed the size, halt the reader.
276 #if PNG_LIBPNG_VER_MAJOR > 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MIN OR >= 5)
277 // '0' argument to png_process_data_pause means: Do not cache unprocesse d data.
278 m_reader->setReadOffset(m_reader->currentBufferSize() - png_process_data _pause(png, 0));
279 #else
280 m_reader->setReadOffset(m_reader->currentBufferSize() - png->buffer_size );
281 png->buffer_size = 0;
282 #endif
283 }
284 } 264 }
285 265
286 void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, unsigned rowIndex, int) 266 void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, unsigned rowIndex, int)
287 { 267 {
268
288 if (m_frameBufferCache.isEmpty()) 269 if (m_frameBufferCache.isEmpty())
289 return; 270 return;
290 271
291 // Initialize the framebuffer if needed. 272 // Initialize the framebuffer if needed.
292 ImageFrame& buffer = m_frameBufferCache[0]; 273 ImageFrame& buffer = m_frameBufferCache[m_currentFrame];
293 if (buffer.getStatus() == ImageFrame::FrameEmpty) { 274 if (buffer.getStatus() == ImageFrame::FrameEmpty) {
294 png_structp png = m_reader->pngPtr(); 275 png_structp png = m_reader->pngPtr();
295 if (!buffer.setSizeAndColorProfile(size().width(), size().height(), colo rProfile())) { 276 if (!buffer.setSizeAndColorProfile(size().width(), size().height(), colo rProfile())) {
296 longjmp(JMPBUF(png), 1); 277 longjmp(JMPBUF(png), 1);
297 return; 278 return;
298 } 279 }
299 280
300 unsigned colorChannels = m_reader->hasAlpha() ? 4 : 3; 281 unsigned colorChannels = m_reader->hasAlpha() ? 4 : 3;
301 if (PNG_INTERLACE_ADAM7 == png_get_interlace_type(png, m_reader->infoPtr ())) { 282 if (PNG_INTERLACE_ADAM7 == png_get_interlace_type(png, m_reader->infoPtr ())) {
302 m_reader->createInterlaceBuffer(colorChannels * size().width() * siz e().height()); 283 m_reader->createInterlaceBuffer(colorChannels * size().width() * siz e().height());
303 if (!m_reader->interlaceBuffer()) { 284 if (!m_reader->interlaceBuffer()) {
304 longjmp(JMPBUF(png), 1); 285 longjmp(JMPBUF(png), 1);
305 return; 286 return;
306 } 287 }
307 } 288 }
308 289
309 #if USE(QCMSLIB) 290 #if USE(QCMSLIB)
310 if (colorTransform()) { 291 if (colorTransform()) {
311 m_reader->createRowBuffer(colorChannels * size().width()); 292 m_reader->createRowBuffer(colorChannels * size().width());
312 if (!m_reader->rowBuffer()) { 293 if (!m_reader->rowBuffer()) {
313 longjmp(JMPBUF(png), 1); 294 longjmp(JMPBUF(png), 1);
314 return; 295 return;
315 } 296 }
316 } 297 }
317 #endif 298 #endif
318 buffer.setStatus(ImageFrame::FramePartial); 299 buffer.setStatus(ImageFrame::FramePartial);
319 buffer.setHasAlpha(false); 300 buffer.setHasAlpha(false);
320 301
321 // For PNGs, the frame always fills the entire image. 302 // For PNGs, the frame always fills the entire image.
scroggo_chromium 2016/10/25 14:59:06 I assume this statement is no longer true?
322 buffer.setOriginalFrameRect(IntRect(IntPoint(), size())); 303 buffer.zeroFillPixelData();
scroggo_chromium 2016/10/25 14:59:06 I assume we used to do this elsewhere? Can we remo
joostouwerling 2016/10/26 15:44:16 This call will be superfluous when blending and di
323 } 304 }
305 const IntRect& rect = buffer.originalFrameRect();
scroggo_chromium 2016/10/25 14:59:06 I suggest calling this frameRect. That makes a lit
324 306
325 /* libpng comments (here to explain what follows). 307 /* libpng comments (here to explain what follows).
326 * 308 *
327 * this function is called for every row in the image. If the 309 * this function is called for every row in the image. If the
328 * image is interlacing, and you turned on the interlace handler, 310 * image is interlacing, and you turned on the interlace handler,
329 * this function will be called for every row in every pass. 311 * this function will be called for every row in every pass.
330 * Some of these rows will not be changed from the previous pass. 312 * Some of these rows will not be changed from the previous pass.
331 * When the row is not changed, the new_row variable will be NULL. 313 * When the row is not changed, the new_row variable will be NULL.
332 * The rows and passes are called in order, so you don't really 314 * The rows and passes are called in order, so you don't really
333 * need the row_num and pass, but I'm supplying them because it 315 * need the row_num and pass, but I'm supplying them because it
334 * may make your life easier. 316 * may make your life easier.
335 */ 317 */
336 318
337 // Nothing to do if the row is unchanged, or the row is outside 319 // Nothing to do if the row is unchanged, or the row is outside
338 // the image bounds: libpng may send extra rows, ignore them to 320 // the image bounds: libpng may send extra rows, ignore them to
339 // make our lives easier. 321 // make our lives easier.
340 if (!rowBuffer) 322 if (!rowBuffer)
341 return; 323 return;
342 int y = rowIndex; 324 int y = rowIndex + rect.y();
scroggo_chromium 2016/10/25 14:59:06 Has the frameRect already been intersected with th
joostouwerling 2016/10/26 15:44:16 Yes, intersection happens in initializeNewFrame(),
scroggo_chromium 2016/10/26 18:25:58 Ok good. No need to check for y < 0. My concern wa
343 if (y < 0 || y >= size().height()) 325 if (y < 0 || y >= size().height())
344 return; 326 return;
345 327
346 /* libpng comments (continued). 328 /* libpng comments (continued).
347 * 329 *
348 * For the non-NULL rows of interlaced images, you must call 330 * For the non-NULL rows of interlaced images, you must call
349 * png_progressive_combine_row() passing in the row and the 331 * png_progressive_combine_row() passing in the row and the
350 * old row. You can call this function for NULL rows (it will 332 * old row. You can call this function for NULL rows (it will
351 * just return) and for non-interlaced images (it just does the 333 * just return) and for non-interlaced images (it just does the
352 * memcpy for you) if it will make the code easier. Thus, you 334 * memcpy for you) if it will make the code easier. Thus, you
(...skipping 20 matching lines...) Expand all
373 355
374 #if USE(QCMSLIB) 356 #if USE(QCMSLIB)
375 if (qcms_transform* transform = colorTransform()) { 357 if (qcms_transform* transform = colorTransform()) {
376 qcms_transform_data(transform, row, m_reader->rowBuffer(), size().width( )); 358 qcms_transform_data(transform, row, m_reader->rowBuffer(), size().width( ));
377 row = m_reader->rowBuffer(); 359 row = m_reader->rowBuffer();
378 } 360 }
379 #endif 361 #endif
380 362
381 // Write the decoded row pixels to the frame buffer. The repetitive 363 // Write the decoded row pixels to the frame buffer. The repetitive
382 // form of the row write loops is for speed. 364 // form of the row write loops is for speed.
383 ImageFrame::PixelData* address = buffer.getAddr(0, y); 365 ImageFrame::PixelData* address = buffer.getAddr(rect.x(), y);
scroggo_chromium 2016/10/25 14:59:06 Again, I could imagine that the frameRect has not
384 unsigned alphaMask = 255; 366 unsigned alphaMask = 255;
385 int width = size().width();
386 367
387 png_bytep pixel = row; 368 png_bytep pixel = row;
388 if (hasAlpha) { 369 if (hasAlpha) {
389 if (buffer.premultiplyAlpha()) { 370 if (buffer.premultiplyAlpha()) {
390 for (int x = 0; x < width; ++x, pixel += 4) { 371 for (int x = rect.x(); x < rect.maxX(); ++x, pixel += 4) {
scroggo_chromium 2016/10/25 14:59:06 Similarly, we need to make sure we do not go beyon
391 buffer.setRGBAPremultiply(address++, pixel[0], pixel[1], pixel[2 ], pixel[3]); 372 buffer.setRGBAPremultiply(address++, pixel[0], pixel[1], pixel[2 ], pixel[3]);
392 alphaMask &= pixel[3]; 373 alphaMask &= pixel[3];
393 } 374 }
394 } else { 375 } else {
395 for (int x = 0; x < width; ++x, pixel += 4) { 376 for (int x = rect.x(); x < rect.maxX(); ++x, pixel += 4) {
396 buffer.setRGBARaw(address++, pixel[0], pixel[1], pixel[2], pixel [3]); 377 buffer.setRGBARaw(address++, pixel[0], pixel[1], pixel[2], pixel [3]);
397 alphaMask &= pixel[3]; 378 alphaMask &= pixel[3];
398 } 379 }
399 } 380 }
400 } else { 381 } else {
401 for (int x = 0; x < width; ++x, pixel += 3) { 382 for (int x = rect.x(); x < rect.maxX(); ++x, pixel += 3) {
402 buffer.setRGBARaw(address++, pixel[0], pixel[1], pixel[2], 255); 383 buffer.setRGBARaw(address++, pixel[0], pixel[1], pixel[2], 255);
403 } 384 }
404 } 385 }
405 386
406 if (alphaMask != 255 && !buffer.hasAlpha()) 387 if (alphaMask != 255 && !buffer.hasAlpha())
407 buffer.setHasAlpha(true); 388 buffer.setHasAlpha(true);
408 389
409 buffer.setPixelsChanged(true); 390 buffer.setPixelsChanged(true);
410 } 391 }
411 392
393 bool PNGImageDecoder::frameIsCompleteAtIndex(size_t index) const
394 {
395 if (failed())
scroggo_chromium 2016/10/25 14:59:06 Maybe add a TODO here, regarding being able to sho
396 return false;
397 if (index >= m_frameBufferCache.size())
398 return false;
399 if (index == 0)
400 return ImageDecoder::frameIsCompleteAtIndex(index);
scroggo_chromium 2016/10/25 14:59:05 This is probably the right thing to do if it's non
joostouwerling 2016/10/26 15:44:16 If GIF is able to get away (that is, since a decod
scroggo_chromium 2016/11/29 16:30:51 Revisiting this because your test (VerifyFrameComp
401 return true;
402 }
403
404 float PNGImageDecoder::frameDurationAtIndex(size_t index) const
405 {
406 return (index < m_frameBufferCache.size() ?
407 m_frameBufferCache[index].duration() : 0);
408 }
409
412 void PNGImageDecoder::complete() 410 void PNGImageDecoder::complete()
413 { 411 {
414 if (m_frameBufferCache.isEmpty()) 412 if (m_frameBufferCache.isEmpty())
415 return; 413 return;
416 414 m_frameBufferCache[m_currentFrame].setStatus(ImageFrame::FrameComplete);
417 m_frameBufferCache[0].setStatus(ImageFrame::FrameComplete);
418 }
419
420 inline bool isComplete(const PNGImageDecoder* decoder)
421 {
422 return decoder->frameIsCompleteAtIndex(0);
423 }
424
425 void PNGImageDecoder::decode(bool onlySize)
426 {
427 if (failed())
428 return;
429
430 if (!m_reader)
431 m_reader = wrapUnique(new PNGImageReader(this, m_offset));
432
433 // If we couldn't decode the image but have received all the data, decoding
434 // has failed.
435 if (!m_reader->decode(*m_data, onlySize) && isAllDataReceived())
436 setFailed();
437
438 // If decoding is done or failed, we don't need the PNGImageReader anymore.
439 if (isComplete(this) || failed())
440 m_reader.reset();
441 } 415 }
442 416
443 } // namespace blink 417 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698