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