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

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

Issue 2386453003: WIP: Implement APNG (Closed)
Patch Set: Process feedback on patch 12 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 48
49 #if PNG_LIBPNG_VER_MAJOR > 1 || \ 49 #if PNG_LIBPNG_VER_MAJOR > 1 || \
50 (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 4) 50 (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 4)
51 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr) 51 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr)
52 #else 52 #else
53 #define JMPBUF(png_ptr) png_ptr->jmpbuf 53 #define JMPBUF(png_ptr) png_ptr->jmpbuf
54 #endif 54 #endif
55 55
56 namespace {
57
58 inline blink::PNGImageDecoder* imageDecoder(png_structp png) {
59 return static_cast<blink::PNGImageDecoder*>(png_get_progressive_ptr(png));
60 }
61
62 void PNGAPI pngHeaderAvailable(png_structp png, png_infop) {
63 imageDecoder(png)->headerAvailable();
64 }
65
66 void PNGAPI pngRowAvailable(png_structp png,
67 png_bytep row,
68 png_uint_32 rowIndex,
69 int state) {
70 imageDecoder(png)->rowAvailable(row, rowIndex, state);
71 }
72
73 void PNGAPI pngComplete(png_structp png, png_infop) {
74 imageDecoder(png)->complete();
75 }
76
77 void PNGAPI pngFailed(png_structp png, png_const_charp) {
78 longjmp(JMPBUF(png), 1);
79 }
80
81 } // namespace
82
83 namespace blink { 56 namespace blink {
84 57
85 class PNGImageReader final {
86 USING_FAST_MALLOC(PNGImageReader);
87 WTF_MAKE_NONCOPYABLE(PNGImageReader);
88
89 public:
90 PNGImageReader(PNGImageDecoder* decoder, size_t readOffset)
91 : m_decoder(decoder),
92 m_readOffset(readOffset),
93 m_currentBufferSize(0),
94 m_decodingSizeOnly(false),
95 m_hasAlpha(false) {
96 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0);
97 m_info = png_create_info_struct(m_png);
98 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable,
99 pngRowAvailable, pngComplete);
100 }
101
102 ~PNGImageReader() {
103 png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0);
104 ASSERT(!m_png && !m_info);
105
106 m_readOffset = 0;
107 }
108
109 bool decode(const SegmentReader& data, bool sizeOnly) {
110 m_decodingSizeOnly = sizeOnly;
111
112 // We need to do the setjmp here. Otherwise bad things will happen.
113 if (setjmp(JMPBUF(m_png)))
114 return m_decoder->setFailed();
115
116 const char* segment;
117 while (size_t segmentLength = data.getSomeData(segment, m_readOffset)) {
118 m_readOffset += segmentLength;
119 m_currentBufferSize = m_readOffset;
120 png_process_data(m_png, m_info,
121 reinterpret_cast<png_bytep>(const_cast<char*>(segment)),
122 segmentLength);
123 if (sizeOnly ? m_decoder->isDecodedSizeAvailable()
124 : m_decoder->frameIsCompleteAtIndex(0))
125 return true;
126 }
127
128 return false;
129 }
130
131 png_structp pngPtr() const { return m_png; }
132 png_infop infoPtr() const { return m_info; }
133
134 size_t getReadOffset() const { return m_readOffset; }
135 void setReadOffset(size_t offset) { m_readOffset = offset; }
136 size_t currentBufferSize() const { return m_currentBufferSize; }
137 bool decodingSizeOnly() const { return m_decodingSizeOnly; }
138 void setHasAlpha(bool hasAlpha) { m_hasAlpha = hasAlpha; }
139 bool hasAlpha() const { return m_hasAlpha; }
140
141 png_bytep interlaceBuffer() const { return m_interlaceBuffer.get(); }
142 void createInterlaceBuffer(int size) {
143 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]);
144 }
145
146 private:
147 png_structp m_png;
148 png_infop m_info;
149 PNGImageDecoder* m_decoder;
150 size_t m_readOffset;
151 size_t m_currentBufferSize;
152 bool m_decodingSizeOnly;
153 bool m_hasAlpha;
154 std::unique_ptr<png_byte[]> m_interlaceBuffer;
155 };
156
157 PNGImageDecoder::PNGImageDecoder(AlphaOption alphaOption, 58 PNGImageDecoder::PNGImageDecoder(AlphaOption alphaOption,
158 ColorSpaceOption colorOptions, 59 ColorSpaceOption colorOptions,
159 size_t maxDecodedBytes, 60 size_t maxDecodedBytes,
160 size_t offset) 61 size_t offset)
161 : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes), 62 : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes),
162 m_offset(offset) {} 63 m_offset(offset),
64 m_frameCount(0),
65 m_currentFrame(0),
66 m_repetitionCount(cAnimationLoopOnce) {}
163 67
164 PNGImageDecoder::~PNGImageDecoder() {} 68 PNGImageDecoder::~PNGImageDecoder() {}
165 69
70 size_t PNGImageDecoder::decodeFrameCount() {
71 parse(PNGParseQuery::PNGMetaDataQuery);
72 return m_frameCount;
73 }
74
75 void PNGImageDecoder::decode(size_t index) {
76 parse(PNGParseQuery::PNGMetaDataQuery);
77
78 // @TODO(joostouwerling): show complete frames even if a later frame fails.
79 if (failed())
80 return;
81
82 updateAggressivePurging(index);
83
84 Vector<size_t> framesToDecode;
85 size_t frameToDecode = index;
86
87 // This method is only called by ImageDecoder::frameBufferAtIndex if the frame
88 // status of frame |index| is not ImageFrame::FrameComplete. Therefore, it is
89 // OK that the do-while loop always appends |index| to |m_framesToDecode|,
90 // without checking for its status.
91 //
92 // The requiredPreviousFrameIndex for each frame is set in
93 // PNGImageDecoder::initializeNewFrame().
94 do {
95 framesToDecode.append(frameToDecode);
96 frameToDecode =
97 m_frameBufferCache[frameToDecode].requiredPreviousFrameIndex();
98 } while (frameToDecode != kNotFound &&
99 m_frameBufferCache[frameToDecode].getStatus() !=
100 ImageFrame::FrameComplete);
101
102 for (auto i = framesToDecode.rbegin(); i != framesToDecode.rend(); i++) {
103 m_currentFrame = *i;
104 m_reader->decode(*m_data, *i);
105 if (failed())
106 return;
107
108 // If the frame is not yet complete, we need more data to continue.
109 if (m_frameBufferCache[*i].getStatus() != ImageFrame::FrameComplete)
110 break;
111
112 if (m_purgeAggressively)
113 clearCacheExceptFrame(*i);
114 }
115 }
116
117 // @TODO(joostouwerling) Consolidate this with a proposed change in
118 // ImageDecoder::clearCacheExceptFrame. See
119 // crrev.com/2468233002
120 size_t PNGImageDecoder::clearCacheExceptFrame(size_t clearExceptFrame) {
121 // As per the comments at ImageDecoder::clearCacheExceptFrame
122 if (m_frameBufferCache.size() <= 1)
123 return 0;
124
125 // We expect that after this call, we'll be asked to decode frames after
126 // this one. So we want to avoid clearing frames such that those requests
127 // would force re-decoding from the beginning of the image.
128 //
129 // When |clearExceptFrame| is e.g. DisposeKeep, simply not clearing that
130 // frame is sufficient, as the next frame will be based on it, and in
131 // general future frames can't be based on anything previous.
132 //
133 // However, if this frame is DisposeOverwritePrevious, then subsequent
134 // frames will depend on this frame's required previous frame. In this
135 // case, we need to preserve both this frame and that one.
136 size_t clearExceptFrame2 = kNotFound;
137 if (clearExceptFrame < m_frameBufferCache.size()) {
138 const ImageFrame& frame = m_frameBufferCache[clearExceptFrame];
139 if (frame.getStatus() != ImageFrame::FrameEmpty &&
140 frame.getDisposalMethod() == ImageFrame::DisposeOverwritePrevious) {
141 clearExceptFrame2 = clearExceptFrame;
142 clearExceptFrame = frame.requiredPreviousFrameIndex();
143 }
144 }
145
146 // Now |clearExceptFrame| indicates the frame that future frames will
147 // depend on. But if decoding is skipping forward past intermediate frames,
148 // this frame may be FrameEmpty. So we need to keep traversing back through
149 // the required previous frames until we find the nearest non-empty
150 // ancestor. Preserving that will minimize the amount of future decoding
151 // needed.
152 while (clearExceptFrame < m_frameBufferCache.size() &&
153 m_frameBufferCache[clearExceptFrame].getStatus() ==
154 ImageFrame::FrameEmpty)
155 clearExceptFrame =
156 m_frameBufferCache[clearExceptFrame].requiredPreviousFrameIndex();
157
158 return clearCacheExceptTwoFrames(clearExceptFrame, clearExceptFrame2);
159 }
160
161 size_t PNGImageDecoder::clearCacheExceptTwoFrames(size_t clearExceptFrame1,
162 size_t clearExceptFrame2) {
163 size_t frameBytesCleared = 0;
164 for (size_t i = 0; i < m_frameBufferCache.size(); ++i) {
165 if (m_frameBufferCache[i].getStatus() != ImageFrame::FrameEmpty &&
166 i != clearExceptFrame1 && i != clearExceptFrame2) {
167 frameBytesCleared += frameBytesAtIndex(i);
168 clearFrameBuffer(i);
169 }
170 }
171 return frameBytesCleared;
172 }
173
174 void PNGImageDecoder::clearFrameBuffer(size_t frameIndex) {
175 if (m_frameBufferCache[frameIndex].getStatus() == ImageFrame::FramePartial)
176 m_reader->clearDecodeState(frameIndex);
177
178 m_frameBufferCache[frameIndex].clearPixelData();
179 }
180
181 void PNGImageDecoder::parse(PNGParseQuery query) {
182 if (failed())
183 return;
184
185 if (!m_reader)
186 m_reader = wrapUnique(new PNGImageReader(this, m_offset));
187
188 if (!m_reader->parse(*m_data, query) && isAllDataReceived())
189 setFailed();
190
191 if (query == PNGParseQuery::PNGMetaDataQuery)
192 m_frameCount = m_reader->frameCount();
193 }
194
195 void PNGImageDecoder::setRepetitionCount(size_t repetitionCount) {
196 m_repetitionCount =
197 (repetitionCount == 0) ? cAnimationLoopInfinite : repetitionCount;
198 }
199
200 // This matches the existing behavior to loop once if decoding fails, but this
201 // should be changed to stick with m_repetitionCount to match other browsers.
202 // See crbug.com/267883
203 int PNGImageDecoder::repetitionCount() const {
204 if (m_reader->parseCompleted() && isAllDataReceived() &&
205 m_reader->frameCount() == 1)
206 return cAnimationNone;
207 return failed() ? cAnimationLoopOnce : m_repetitionCount;
208 }
209
210 // These are mapped according to:
211 // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu nk
212 static inline ImageFrame::DisposalMethod getDisposalMethod(
213 uint8_t disposalMethod) {
214 switch (disposalMethod) {
215 case 0:
216 return ImageFrame::DisposalMethod::DisposeKeep;
217 case 1:
218 return ImageFrame::DisposalMethod::DisposeOverwriteBgcolor;
219 case 2:
220 return ImageFrame::DisposalMethod::DisposeOverwritePrevious;
221 default:
222 return ImageFrame::DisposalMethod::DisposeNotSpecified;
223 }
224 }
225
226 // These are mapped according to:
227 // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu nk
228 static inline ImageFrame::AlphaBlendSource getAlphaBlend(uint8_t alphaBlend) {
229 if (alphaBlend == 1)
230 return ImageFrame::AlphaBlendSource::BlendAtopPreviousFrame;
231 return ImageFrame::AlphaBlendSource::BlendAtopBgcolor;
232 }
233
234 void PNGImageDecoder::initializeNewFrame(size_t index) {
235 const PNGImageReader::FrameInfo& frameInfo = m_reader->frameInfo(index);
236 ImageFrame* buffer = &m_frameBufferCache[index];
237
238 IntRect frameRectWithinSize =
239 intersection(frameInfo.frameRect, {IntPoint(), size()});
240 buffer->setOriginalFrameRect(frameRectWithinSize);
241 buffer->setDuration(frameInfo.duration);
242 buffer->setDisposalMethod(getDisposalMethod(frameInfo.disposalMethod));
243 buffer->setAlphaBlendSource(getAlphaBlend(frameInfo.alphaBlend));
244 buffer->setRequiredPreviousFrameIndex(
245 findRequiredPreviousFrame(index, false));
246 }
247
248 // Initialize the frame buffer before decoding. The returned boolean indicates
249 // whether initialisation succeeded when it is true, false otherwise.
250 bool PNGImageDecoder::initFrameBuffer(size_t index) {
251 ImageFrame* const buffer = &m_frameBufferCache[index];
252
253 // Return true if the frame is already initialised.
254 if (buffer->getStatus() != ImageFrame::FrameEmpty)
255 return true;
256
257 if (!buffer->setSizeAndColorSpace(size().width(), size().height(),
258 colorSpace()))
259 return false;
260
261 // Create the image buffer if:
262 // A) The image is encoded with interlacing, or
263 // B) frameShouldBeDecodedIncrementally(m_frameBufferCache) == false. In this
264 // case, the image buffer is used to store the row data, so it can be
265 // written to the frame buffer at once in PNGImageDecoder::complete().
266 // This prevents partially overwriting the content of previous frames.
267 unsigned colorChannels = m_reader->hasAlpha() ? 4 : 3;
268 png_structp png = m_reader->pngPtr();
269 if (PNG_INTERLACE_ADAM7 == png_get_interlace_type(png, m_reader->infoPtr()) ||
270 !frameShouldBeDecodedIncrementally(m_currentFrame)) {
271 m_reader->createImageBuffer(colorChannels * size().width() *
272 size().height());
273 if (!m_reader->imageBuffer())
274 return false;
275 }
276
277 buffer->setHasAlpha(false);
278 size_t requiredPreviousFrameIndex = buffer->requiredPreviousFrameIndex();
279
280 // If frame |index| does not depend on any other frame, ensure the frame is
281 // fully transparent black after initialisation.
282 if (requiredPreviousFrameIndex == kNotFound) {
283 buffer->zeroFillPixelData();
284 } else {
285 ImageFrame* prevBuffer = &m_frameBufferCache[requiredPreviousFrameIndex];
286 ASSERT(prevBuffer->getStatus() == ImageFrame::FrameComplete);
287
288 // We try to reuse |prevBuffer| as starting state to avoid copying.
289 // For DisposeOverwritePrevious, the next frame will also use
290 // |prevBuffer| as its starting state, so we can't take over its image
291 // data using takeBitmapDataIfWritable. Copy the data instead.
292 if ((buffer->getDisposalMethod() == ImageFrame::DisposeOverwritePrevious ||
293 !buffer->takeBitmapDataIfWritable(prevBuffer)) &&
294 !buffer->copyBitmapData(*prevBuffer))
295 return false;
296
297 // We want to clear the previous frame to transparent, without affecting
298 // pixels in the image outside of the frame.
299 if (prevBuffer->getDisposalMethod() ==
300 ImageFrame::DisposeOverwriteBgcolor) {
301 const IntRect& prevRect = prevBuffer->originalFrameRect();
302 ASSERT(!prevRect.contains(IntRect(IntPoint(), size())));
303 buffer->zeroFillFrameRect(prevRect);
304 }
305 }
306
307 buffer->setStatus(ImageFrame::FramePartial);
308 return true;
309 }
310
166 inline float pngFixedToFloat(png_fixed_point x) { 311 inline float pngFixedToFloat(png_fixed_point x) {
167 return ((float)x) * 0.00001f; 312 return ((float)x) * 0.00001f;
168 } 313 }
169 314
170 inline sk_sp<SkColorSpace> readColorSpace(png_structp png, png_infop info) { 315 inline sk_sp<SkColorSpace> readColorSpace(png_structp png, png_infop info) {
171 if (png_get_valid(png, info, PNG_INFO_sRGB)) { 316 if (png_get_valid(png, info, PNG_INFO_sRGB)) {
172 return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); 317 return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
173 } 318 }
174 319
175 png_charp name = nullptr; 320 png_charp name = nullptr;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 361
217 return nullptr; 362 return nullptr;
218 } 363 }
219 364
220 void PNGImageDecoder::headerAvailable() { 365 void PNGImageDecoder::headerAvailable() {
221 png_structp png = m_reader->pngPtr(); 366 png_structp png = m_reader->pngPtr();
222 png_infop info = m_reader->infoPtr(); 367 png_infop info = m_reader->infoPtr();
223 png_uint_32 width = png_get_image_width(png, info); 368 png_uint_32 width = png_get_image_width(png, info);
224 png_uint_32 height = png_get_image_height(png, info); 369 png_uint_32 height = png_get_image_height(png, info);
225 370
226 // Protect against large PNGs. See http://bugzil.la/251381 for more details. 371 // Only set the size of the image once. Since single frames also use this
227 const unsigned long maxPNGSize = 1000000UL; 372 // method, we don't want them to override the size to their frame rect.
228 if (width > maxPNGSize || height > maxPNGSize) { 373 if (!isDecodedSizeAvailable()) {
229 longjmp(JMPBUF(png), 1); 374 // Protect against large PNGs. See http://bugzil.la/251381 for more details.
230 return; 375 const unsigned long maxPNGSize = 1000000UL;
231 } 376 if (width > maxPNGSize || height > maxPNGSize) {
377 longjmp(JMPBUF(png), 1);
378 return;
379 }
232 380
233 // Set the image size now that the image header is available. 381 // Set the image size now that the image header is available.
234 if (!setSize(width, height)) { 382 if (!setSize(width, height)) {
235 longjmp(JMPBUF(png), 1); 383 longjmp(JMPBUF(png), 1);
236 return; 384 return;
385 }
237 } 386 }
238 387
239 int bitDepth, colorType, interlaceType, compressionType, filterType, channels; 388 int bitDepth, colorType, interlaceType, compressionType, filterType, channels;
240 png_get_IHDR(png, info, &width, &height, &bitDepth, &colorType, 389 png_get_IHDR(png, info, &width, &height, &bitDepth, &colorType,
241 &interlaceType, &compressionType, &filterType); 390 &interlaceType, &compressionType, &filterType);
242 391
243 // The options we set here match what Mozilla does. 392 // The options we set here match what Mozilla does.
244 393
245 // Expand to ensure we use 24-bit for RGB and 32-bit for RGBA. 394 // Expand to ensure we use 24-bit for RGB and 32-bit for RGBA.
246 if (colorType == PNG_COLOR_TYPE_PALETTE || 395 if (colorType == PNG_COLOR_TYPE_PALETTE ||
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 if ((gamma <= 0.0) || (gamma > maxGamma)) { 441 if ((gamma <= 0.0) || (gamma > maxGamma)) {
293 gamma = inverseGamma; 442 gamma = inverseGamma;
294 png_set_gAMA(png, info, gamma); 443 png_set_gAMA(png, info, gamma);
295 } 444 }
296 png_set_gamma(png, defaultGamma, gamma); 445 png_set_gamma(png, defaultGamma, gamma);
297 } else { 446 } else {
298 png_set_gamma(png, defaultGamma, inverseGamma); 447 png_set_gamma(png, defaultGamma, inverseGamma);
299 } 448 }
300 } 449 }
301 450
302 // Tell libpng to send us rows for interlaced pngs. 451 // Tell libpng to send us rows for interlaced pngs. Make sure the interlace
scroggo_chromium 2016/11/09 13:42:50 image* buffer
joostouwerling 2016/11/11 20:22:18 In the revisited patch, this is correct.
452 // buffer is cleared before decoding.
453 m_reader->clearImageBuffer();
303 if (interlaceType == PNG_INTERLACE_ADAM7) 454 if (interlaceType == PNG_INTERLACE_ADAM7)
304 png_set_interlace_handling(png); 455 png_set_interlace_handling(png);
305 456
306 // Update our info now. 457 // Update our info now.
307 png_read_update_info(png, info); 458 png_read_update_info(png, info);
308 channels = png_get_channels(png, info); 459 channels = png_get_channels(png, info);
309 ASSERT(channels == 3 || channels == 4); 460 ASSERT(channels == 3 || channels == 4);
310 461
311 m_reader->setHasAlpha(channels == 4); 462 m_reader->setHasAlpha(channels == 4);
312
313 if (m_reader->decodingSizeOnly()) {
314 // If we only needed the size, halt the reader.
315 #if PNG_LIBPNG_VER_MAJOR > 1 || \
316 (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 5)
317 // Passing '0' tells png_process_data_pause() not to cache unprocessed data.
318 m_reader->setReadOffset(m_reader->currentBufferSize() -
319 png_process_data_pause(png, 0));
320 #else
321 m_reader->setReadOffset(m_reader->currentBufferSize() - png->buffer_size);
322 png->buffer_size = 0;
323 #endif
324 }
325 } 463 }
326 464
327 void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, 465 void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer,
328 unsigned rowIndex, 466 unsigned rowIndex,
329 int) { 467 int) {
330 if (m_frameBufferCache.isEmpty()) 468 if (m_frameBufferCache.isEmpty())
331 return; 469 return;
332 470
333 // Initialize the framebuffer if needed. 471 ImageFrame& buffer = m_frameBufferCache[m_currentFrame];
334 ImageFrame& buffer = m_frameBufferCache[0]; 472 if (!initFrameBuffer(m_currentFrame)) {
335 if (buffer.getStatus() == ImageFrame::FrameEmpty) { 473 setFailed();
336 png_structp png = m_reader->pngPtr(); 474 return;
337 if (!buffer.setSizeAndColorSpace(size().width(), size().height(),
338 colorSpace())) {
339 longjmp(JMPBUF(png), 1);
340 return;
341 }
342
343 unsigned colorChannels = m_reader->hasAlpha() ? 4 : 3;
344 if (PNG_INTERLACE_ADAM7 ==
345 png_get_interlace_type(png, m_reader->infoPtr())) {
346 m_reader->createInterlaceBuffer(colorChannels * size().width() *
347 size().height());
348 if (!m_reader->interlaceBuffer()) {
349 longjmp(JMPBUF(png), 1);
350 return;
351 }
352 }
353
354 buffer.setStatus(ImageFrame::FramePartial);
355 buffer.setHasAlpha(false);
356
357 // For PNGs, the frame always fills the entire image.
358 buffer.setOriginalFrameRect(IntRect(IntPoint(), size()));
359 } 475 }
360 476
477 // This frameRect is already clipped, so that it fits within the size of the
478 // image. This is done in initializeNewFrame() after a frameCount() call.
479 const IntRect& frameRect = buffer.originalFrameRect();
480
361 /* libpng comments (here to explain what follows). 481 /* libpng comments (here to explain what follows).
362 * 482 *
363 * this function is called for every row in the image. If the 483 * this function is called for every row in the image. If the
364 * image is interlacing, and you turned on the interlace handler, 484 * image is interlacing, and you turned on the interlace handler,
365 * this function will be called for every row in every pass. 485 * this function will be called for every row in every pass.
366 * Some of these rows will not be changed from the previous pass. 486 * Some of these rows will not be changed from the previous pass.
367 * When the row is not changed, the new_row variable will be NULL. 487 * When the row is not changed, the new_row variable will be NULL.
368 * The rows and passes are called in order, so you don't really 488 * The rows and passes are called in order, so you don't really
369 * need the row_num and pass, but I'm supplying them because it 489 * need the row_num and pass, but I'm supplying them because it
370 * may make your life easier. 490 * may make your life easier.
371 */ 491 */
372 492
373 // Nothing to do if the row is unchanged, or the row is outside 493 // Nothing to do if the row is unchanged, or the row is outside
374 // the image bounds: libpng may send extra rows, ignore them to 494 // the image bounds: libpng may send extra rows, ignore them to
375 // make our lives easier. 495 // make our lives easier.
376 if (!rowBuffer) 496 if (!rowBuffer)
377 return; 497 return;
378 int y = rowIndex; 498 int y = rowIndex + frameRect.y();
379 if (y < 0 || y >= size().height()) 499 ASSERT(y >= 0);
500 if (y >= size().height())
380 return; 501 return;
381 502
382 /* libpng comments (continued). 503 /* libpng comments (continued).
383 * 504 *
384 * For the non-NULL rows of interlaced images, you must call 505 * For the non-NULL rows of interlaced images, you must call
385 * png_progressive_combine_row() passing in the row and the 506 * png_progressive_combine_row() passing in the row and the
386 * old row. You can call this function for NULL rows (it will 507 * old row. You can call this function for NULL rows (it will
387 * just return) and for non-interlaced images (it just does the 508 * just return) and for non-interlaced images (it just does the
388 * memcpy for you) if it will make the code easier. Thus, you 509 * memcpy for you) if it will make the code easier. Thus, you
389 * can just do this for all cases: 510 * can just do this for all cases:
390 * 511 *
391 * png_progressive_combine_row(png_ptr, old_row, new_row); 512 * png_progressive_combine_row(png_ptr, old_row, new_row);
392 * 513 *
393 * where old_row is what was displayed for previous rows. Note 514 * where old_row is what was displayed for previous rows. Note
394 * that the first pass (pass == 0 really) will completely cover 515 * that the first pass (pass == 0 really) will completely cover
395 * the old row, so the rows do not have to be initialized. After 516 * the old row, so the rows do not have to be initialized. After
396 * the first pass (and only for interlaced images), you will have 517 * the first pass (and only for interlaced images), you will have
397 * to pass the current row, and the function will combine the 518 * to pass the current row, and the function will combine the
398 * old row and the new row. 519 * old row and the new row.
399 */ 520 */
400 521
401 bool hasAlpha = m_reader->hasAlpha(); 522 bool hasAlpha = m_reader->hasAlpha();
402 png_bytep row = rowBuffer; 523 png_bytep row = rowBuffer;
403 524
404 if (png_bytep interlaceBuffer = m_reader->interlaceBuffer()) { 525 if (png_bytep imageBuffer = m_reader->imageBuffer()) {
405 unsigned colorChannels = hasAlpha ? 4 : 3; 526 unsigned colorChannels = hasAlpha ? 4 : 3;
406 row = interlaceBuffer + (rowIndex * colorChannels * size().width()); 527 row = imageBuffer + (rowIndex * colorChannels * size().width());
407 png_progressive_combine_row(m_reader->pngPtr(), row, rowBuffer); 528 png_progressive_combine_row(m_reader->pngPtr(), row, rowBuffer);
408 } 529 }
409 530
531 // For non-first frames, don't write rows incrementally to the buffer, since
scroggo_chromium 2016/11/09 13:42:50 This comment is no longer correct. This now applie
joostouwerling 2016/11/11 20:22:18 This is now removed in the revisited patch.
532 // this may result in partial frames being displayed. Instead, write the
533 // rows in the complete() callback.
534 if (!frameShouldBeDecodedIncrementally(m_currentFrame))
535 return;
536
410 // Write the decoded row pixels to the frame buffer. The repetitive 537 // Write the decoded row pixels to the frame buffer. The repetitive
411 // form of the row write loops is for speed. 538 // form of the row write loops is for speed.
412 ImageFrame::PixelData* const dstRow = buffer.getAddr(0, y); 539 ImageFrame::PixelData* const dstRow = buffer.getAddr(frameRect.x(), y);
413 unsigned alphaMask = 255; 540 unsigned alphaMask = 255;
414 int width = size().width(); 541 int width = frameRect.width();
415 542
416 png_bytep srcPtr = row; 543 png_bytep srcPtr = row;
417 if (hasAlpha) { 544 if (hasAlpha) {
418 // Here we apply the color space transformation to the dst space. 545 // Here we apply the color space transformation to the dst space.
419 // It does not really make sense to transform to a gamma-encoded 546 // It does not really make sense to transform to a gamma-encoded
420 // space and then immediately after, perform a linear premultiply. 547 // space and then immediately after, perform a linear premultiply.
421 // Ideally we would pass kPremul_SkAlphaType to xform->apply(), 548 // Ideally we would pass kPremul_SkAlphaType to xform->apply(),
422 // instructing SkColorSpaceXform to perform the linear premultiply 549 // instructing SkColorSpaceXform to perform the linear premultiply
423 // while the pixels are a linear space. 550 // while the pixels are a linear space.
424 // We cannot do this because when we apply the gamma encoding after 551 // We cannot do this because when we apply the gamma encoding after
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 size().width(), kOpaque_SkAlphaType); 588 size().width(), kOpaque_SkAlphaType);
462 } 589 }
463 } 590 }
464 591
465 if (alphaMask != 255 && !buffer.hasAlpha()) 592 if (alphaMask != 255 && !buffer.hasAlpha())
466 buffer.setHasAlpha(true); 593 buffer.setHasAlpha(true);
467 594
468 buffer.setPixelsChanged(true); 595 buffer.setPixelsChanged(true);
469 } 596 }
470 597
598 bool PNGImageDecoder::frameIsCompleteAtIndex(size_t index) const {
599 // @TODO(joostouwerling): show complete frames even if a later frame fails.
600 if (failed())
601 return false;
602 if (index == 0)
603 return ImageDecoder::frameIsCompleteAtIndex(index);
604
605 // For non-first frames, the frame is considered complete if all frame data
606 // has been received. Non-first frames are reported by |m_reader| once it has
607 // parsed all data for that frame, so we can simply return if the index
608 // exists in |m_frameBufferCache| here.
609 return (index < m_frameBufferCache.size());
610 }
611
612 float PNGImageDecoder::frameDurationAtIndex(size_t index) const {
613 return (index < m_frameBufferCache.size()
614 ? m_frameBufferCache[index].duration()
615 : 0);
616 }
617
471 void PNGImageDecoder::complete() { 618 void PNGImageDecoder::complete() {
472 if (m_frameBufferCache.isEmpty()) 619 if (m_frameBufferCache.isEmpty())
473 return; 620 return;
474 621
475 m_frameBufferCache[0].setStatus(ImageFrame::FrameComplete); 622 // @TODO(joostouwerling) if necessary, do a check if all expected data has
476 } 623 // been received. This is because the IEND chunk is sent
624 // artificially. The necessity of this check depends on
625 // how libpng handles in- and overcomplete frame data.
626 ImageFrame* buffer = &m_frameBufferCache[m_currentFrame];
477 627
478 inline bool isComplete(const PNGImageDecoder* decoder) { 628 // The pixels for the first frame or any non-first frame with no required
479 return decoder->frameIsCompleteAtIndex(0); 629 // previous frame are already written to the framebuffer in rowAvailable.
480 } 630 if (frameShouldBeDecodedIncrementally(m_currentFrame)) {
631 m_reader->clearImageBuffer();
632 buffer->setStatus(ImageFrame::FrameComplete);
633 return;
634 }
481 635
482 void PNGImageDecoder::decode(bool onlySize) { 636 // For non-first frames, the frame data was written to the image buffer in
483 if (failed()) 637 // rowAvailable. Write the decoded pixels from the the image buffer to the
484 return; 638 // the frame buffer.
639 png_bytep image = m_reader->imageBuffer();
485 640
486 if (!m_reader) 641 IntRect frameRect = buffer->originalFrameRect();
487 m_reader = wrapUnique(new PNGImageReader(this, m_offset)); 642 bool hasAlpha = m_reader->hasAlpha();
643 unsigned colorChannels = hasAlpha ? 4 : 3;
644 unsigned alphaMask = 255;
488 645
489 // If we couldn't decode the image but have received all the data, decoding 646 // TODO(joostouwerling): for now, this is a straight copy of the code in
490 // has failed. 647 // rowAvailable, except for writing multiple rows. Alpha
491 if (!m_reader->decode(*m_data, onlySize) && isAllDataReceived()) 648 // blending and possible code sharing need to be added.
492 setFailed(); 649 for (int y = frameRect.y(); y < frameRect.maxY();
650 ++y, image += colorChannels * size().width()) {
651 ImageFrame::PixelData* const dstRow = buffer->getAddr(frameRect.x(), y);
652 unsigned alphaMask = 255;
653 int width = frameRect.width();
493 654
494 // If decoding is done or failed, we don't need the PNGImageReader anymore. 655 png_bytep srcPtr = image;
495 if (isComplete(this) || failed()) 656 if (hasAlpha) {
496 m_reader.reset(); 657 // Here we apply the color space transformation to the dst space.
658 // It does not really make sense to transform to a gamma-encoded
659 // space and then immediately after, perform a linear premultiply.
660 // Ideally we would pass kPremul_SkAlphaType to xform->apply(),
661 // instructing SkColorSpaceXform to perform the linear premultiply
662 // while the pixels are a linear space.
663 // We cannot do this because when we apply the gamma encoding after
664 // the premultiply, we will very likely end up with valid pixels
665 // where R, G, and/or B are greater than A. The legacy drawing
666 // pipeline does not know how to handle this.
667 if (SkColorSpaceXform* xform = colorTransform()) {
668 SkColorSpaceXform::ColorFormat colorFormat =
669 SkColorSpaceXform::kRGBA_8888_ColorFormat;
670 xform->apply(colorFormat, dstRow, colorFormat, srcPtr, size().width(),
671 kUnpremul_SkAlphaType);
672 srcPtr = (png_bytep)dstRow;
673 }
674
675 if (buffer->premultiplyAlpha()) {
676 for (auto *dstPixel = dstRow; dstPixel < dstRow + width;
677 dstPixel++, srcPtr += 4) {
678 buffer->setRGBAPremultiply(dstPixel, srcPtr[0], srcPtr[1], srcPtr[2],
679 srcPtr[3]);
680 alphaMask &= srcPtr[3];
681 }
682 } else {
683 for (auto *dstPixel = dstRow; dstPixel < dstRow + width;
684 dstPixel++, srcPtr += 4) {
685 buffer->setRGBARaw(dstPixel, srcPtr[0], srcPtr[1], srcPtr[2],
686 srcPtr[3]);
687 alphaMask &= srcPtr[3];
688 }
689 }
690 } else {
691 for (auto *dstPixel = dstRow; dstPixel < dstRow + width;
692 dstPixel++, srcPtr += 3) {
693 buffer->setRGBARaw(dstPixel, srcPtr[0], srcPtr[1], srcPtr[2], 255);
694 }
695
696 // We'll apply the color space xform to opaque pixels after they have been
697 // written to the ImageFrame, purely because SkColorSpaceXform supports
698 // RGBA (and not RGB).
699 if (SkColorSpaceXform* xform = colorTransform()) {
700 xform->apply(xformColorFormat(), dstRow, xformColorFormat(), dstRow,
701 size().width(), kOpaque_SkAlphaType);
702 }
703 }
704 }
705
706 if (alphaMask != 255 && !buffer->hasAlpha())
707 buffer->setHasAlpha(true);
708
709 m_reader->clearImageBuffer();
710 buffer->setPixelsChanged(true);
711 buffer->setStatus(ImageFrame::FrameComplete);
497 } 712 }
498 713
499 } // namespace blink 714 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698