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