OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (C) 2006 Apple Computer, Inc. | |
3 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. | |
4 * | |
5 * Portions are Copyright (C) 2001 mozilla.org | |
6 * | |
7 * Other contributors: | |
8 * Stuart Parmenter <stuart@mozilla.com> | |
9 * | |
10 * This library is free software; you can redistribute it and/or | |
11 * modify it under the terms of the GNU Lesser General Public | |
12 * License as published by the Free Software Foundation; either | |
13 * version 2.1 of the License, or (at your option) any later version. | |
14 * | |
15 * This library is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 * Lesser General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU Lesser General Public | |
21 * License along with this library; if not, write to the Free Software | |
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 US A | |
23 * | |
24 * Alternatively, the contents of this file may be used under the terms | |
25 * of either the Mozilla Public License Version 1.1, found at | |
26 * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public | |
27 * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html | |
28 * (the "GPL"), in which case the provisions of the MPL or the GPL are | |
29 * applicable instead of those above. If you wish to allow use of your | |
30 * version of this file only under the terms of one of those two | |
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 | |
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. | |
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. | |
37 */ | |
38 | |
39 #include "platform/image-decoders/png/PNGImageReader.h" | |
40 | |
41 #include "platform/image-decoders/png/PNGImageDecoder.h" | |
42 #include "platform/image-decoders/FastSharedBufferReader.h" | |
43 #include "png.h" | |
44 #include "wtf/PtrUtil.h" | |
45 #include <memory> | |
46 | |
47 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR) | |
48 #error version error: compile against a versioned libpng. | |
49 #endif | |
50 #if USE(QCMSLIB) | |
51 #include "qcms.h" | |
52 #endif | |
53 | |
54 #if PNG_LIBPNG_VER_MAJOR > 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MIN OR >= 4) | |
55 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr) | |
56 #else | |
57 #define JMPBUF(png_ptr) png_ptr->jmpbuf | |
58 #endif | |
59 | |
60 namespace { | |
61 | |
62 inline blink::PNGImageDecoder* imageDecoder(png_structp png) | |
63 { | |
64 return static_cast<blink::PNGImageDecoder*>(png_get_progressive_ptr(png)); | |
65 } | |
66 | |
67 void PNGAPI pngHeaderAvailable(png_structp png, png_infop) | |
68 { | |
69 imageDecoder(png)->headerAvailable(); | |
70 } | |
71 | |
72 void PNGAPI pngRowAvailable(png_structp png, png_bytep row, | |
73 png_uint_32 rowIndex, int state) | |
74 { | |
75 imageDecoder(png)->rowAvailable(row, rowIndex, state); | |
76 } | |
77 | |
78 void PNGAPI pngComplete(png_structp png, png_infop) | |
79 { | |
80 imageDecoder(png)->complete(); | |
81 } | |
82 | |
83 void PNGAPI pngFailed(png_structp png, png_const_charp err) | |
84 { | |
85 longjmp(JMPBUF(png), 1); | |
86 } | |
87 | |
88 } // namespace | |
89 | |
90 namespace blink { | |
91 | |
92 // This is the callback function for unknown PNG chunks, which is used to | |
93 // extract the animation chunks. | |
94 static int readAnimationChunk(png_structp png_ptr, png_unknown_chunkp chunk) | |
95 { | |
96 PNGImageReader* reader = (PNGImageReader*) png_get_user_chunk_ptr(png_ptr); | |
97 reader->parseAnimationChunk((const char*) chunk->name, chunk->data, | |
98 chunk->size); | |
99 return 1; | |
100 } | |
101 | |
102 PNGImageReader::PNGImageReader(PNGImageDecoder* decoder, size_t initialOffset) | |
103 : m_decoder(decoder) | |
104 , m_initialOffset(initialOffset) | |
105 , m_readOffset(initialOffset) | |
106 , m_bytesInfo(0) | |
107 , m_hasAlpha(false) | |
108 , m_idatIsPartOfAnimation(false) | |
109 , m_isAnimated(false) | |
110 , m_parsedSignature(false) | |
111 #if USE(QCMSLIB) | |
112 , m_rowBuffer() | |
113 #endif | |
114 { | |
115 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0); | |
116 m_info = png_create_info_struct(m_png); | |
117 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, | |
118 pngRowAvailable, pngComplete); | |
119 | |
120 // Keep the chunks which are of interest for APNG. We don't need to keep | |
121 // the fdAT chunks, since they are converted to IDAT's by the frame decoder. | |
122 png_byte apngChunks[] = {"acTL\0fcTL\0"}; | |
123 png_set_keep_unknown_chunks(m_png, PNG_HANDLE_CHUNK_NEVER, apngChunks, 2); | |
124 png_set_read_user_chunk_fn(m_png, (png_voidp) this, readAnimationChunk); | |
125 } | |
126 | |
127 PNGImageReader::~PNGImageReader() | |
128 { | |
129 png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0); | |
130 ASSERT(!m_png && !m_info); | |
131 } | |
132 | |
133 // This method reads from the FastSharedBufferReader, starting at offset, | |
134 // and returns |length| bytes in the form of a pointer to a const png_byte*. | |
135 // This function is used to make it easy to access data from the reader in a | |
136 // png friendly way, and pass it to libpng for decoding. | |
137 // | |
138 // Pre-conditions before using this: | |
139 // - |reader|.size() >= |readOffset| + |length| | |
140 // - |buffer|.size() = kBufferSize >= |length| | |
141 // | |
142 // The reason for the last precondition is that at this point, the png signature | |
143 // plus IHDR chunk (8B + 25B) is the largest chunk that is read using this | |
144 // method. If the data is not consecutive, it is stored in |buffer|, which | |
145 // should have the size of (at least) kBufferSize. | |
scroggo_chromium
2016/10/25 14:59:07
should -> must
| |
146 constexpr size_t kBufferSize = 33; | |
scroggo_chromium
2016/10/25 14:59:06
nit: I think this should be static.
| |
147 const png_byte* readAsConstPngBytep(const FastSharedBufferReader& reader, | |
148 size_t readOffset, size_t length, | |
149 char* buffer) | |
150 { | |
151 ASSERT(length <= kBufferSize); | |
152 return reinterpret_cast<const png_byte*>( | |
153 reader.getConsecutiveData(readOffset, length, buffer)); | |
154 } | |
155 | |
156 void PNGImageReader::decode(SegmentReader& data, size_t index) | |
157 { | |
158 if (index >= m_frameInfo.size()) | |
159 return; | |
160 | |
161 // For non animated PNG's, we don't want to waste CPU time with recreating | |
162 // the png struct. It suffices to continue parsing where we left off. Since | |
163 // non animated only need to decode the frame once, we can use the | |
164 // readOffset field to enable progressive decoding. | |
165 if (!m_isAnimated) { | |
166 if (setjmp(JMPBUF(m_png))) { | |
167 m_decoder->setFailed(); | |
168 return; | |
169 } | |
170 m_frameInfo[0].readOffset += processData(data, m_frameInfo[0].readOffset , 0); | |
171 return; | |
172 } | |
173 | |
174 // Initialize a new png struct for this frame. | |
175 startFrameDecoding(data, index); | |
176 | |
177 // From the frame info that was gathered during parsing, it is known at | |
178 // what offset the frame data starts and how many bytes are in the stream | |
179 // before the frame ends. Using this, we process all chunks that fall in | |
180 // this interval. We catch every fdAT chunk and transform it to an IDAT | |
181 // chunk, so libpng will decode it like a non-animated PNG image. | |
182 size_t offset = m_frameInfo[index].readOffset; | |
183 size_t endOffset = offset + m_frameInfo[index].byteLength; | |
184 char readBuffer[8]; | |
185 FastSharedBufferReader reader(&data); | |
186 | |
187 while (offset < endOffset) { | |
188 const png_byte* chunk = readAsConstPngBytep(reader, offset, 8, readBuffe r); | |
scroggo_chromium
2016/10/25 14:59:07
Doesn't your comments up above tell us that readBu
joostouwerling
2016/10/26 15:44:17
I modified the comment to make this more clear.
| |
189 const png_uint_32 length = png_get_uint_32(chunk); | |
190 | |
191 if (memcmp(chunk + 4, "fdAT", 4) == 0) { | |
192 png_byte chunkIDAT[] = {0, 0, 0, 0, 'I', 'D', 'A', 'T'}; | |
scroggo_chromium
2016/10/25 14:59:07
Can these various chunks be constexpr?
joostouwerling
2016/10/26 15:44:17
This one should semantically not be constexpr, sin
| |
193 png_save_uint_32(chunkIDAT, length - 4); | |
scroggo_chromium
2016/10/25 14:59:07
Why do we need to subtract 4 here?
| |
194 png_process_data(m_png, m_info, chunkIDAT, 8); | |
195 // Skip the first 4 bytes of the fdAT chunk, which contain the | |
196 // sequence number. By doing this, parsing |length| bytes will | |
197 // include the CRC at the end of the chunk as well. | |
scroggo_chromium
2016/10/25 14:59:07
Why? Sorry, I do not follow.
joostouwerling
2016/10/26 15:44:17
An fdAT chunk is build up like:
|length| (4B) ->
| |
198 processData(data, offset + 12, length); | |
199 } else { | |
200 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
201 processData(data, offset + 8, length + 4); | |
202 } | |
203 | |
204 offset += 12 + length; | |
205 } | |
206 | |
207 // Finish decoding by sending the IEND chunk. | |
208 endFrameDecoding(); | |
209 | |
210 } | |
211 | |
212 void PNGImageReader::startFrameDecoding(SegmentReader& data, size_t index) | |
213 { | |
214 // Each frame is processed as if it were a complete, single frame png image. | |
215 // To accomplish this, destroy the current |m_png| and |m_info| structs and | |
216 // create new ones. CRC errors are ignored, so fdAT chunks can be processed | |
scroggo_chromium
2016/10/25 14:59:07
Is there a risk to ignoring CRC errors?
joostouwerling
2016/10/26 15:44:17
They are meant as a checksum to see if the data re
scroggo_chromium
2016/10/26 18:25:58
The main concern I would have is that libpng would
joostouwerling
2016/10/27 20:29:55
This is a good question and I don't know exactly w
| |
217 // as IDAT's without recalculating the CRC value. | |
scroggo_chromium
2016/10/25 14:59:07
nit: No need for the ' here.
| |
218 png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0); | |
scroggo_chromium
2016/10/25 14:59:07
A couple of comments:
- Will we ever have m_info b
joostouwerling
2016/10/26 15:44:17
Does that happen for frames with |index| > 0, sinc
scroggo_chromium
2016/10/26 18:25:58
Good question. Just because all the data is availa
joostouwerling
2016/10/27 20:29:55
I don't think that this is the right place to take
scroggo_chromium
2016/10/28 14:20:32
Just to make sure I understand: because you've pro
joostouwerling
2016/10/28 18:41:25
Right.
| |
219 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0); | |
220 m_info = png_create_info_struct(m_png); | |
221 png_set_crc_action(m_png, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE); | |
222 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, | |
223 pngRowAvailable, pngComplete); | |
224 | |
225 if (setjmp(JMPBUF(m_png))) { | |
226 m_decoder->setFailed(); | |
227 return; | |
228 } | |
229 | |
230 // If the frame is the size of the whole image, we don't need to modify any | |
231 // data in the IHDR chunk. This means it suffices to re-process all header | |
232 // data up to the first frame, for mimicking a png image. | |
233 IntRect& frameRect = m_frameInfo[index].frameRect; | |
scroggo_chromium
2016/10/25 14:59:07
I think this can be const
| |
234 if (frameRect.location() == IntPoint() | |
235 && frameRect.size() == m_decoder->size()) { | |
236 processData(data, m_initialOffset, m_bytesInfo); | |
237 return; | |
238 } | |
239 | |
240 // Process the IHDR chunk, but change the width and height so it reflects | |
241 // the frame's width and height. Image Decoder will apply the x,y offset. | |
242 // This step may be omitted if width and height are equal to the image size. | |
scroggo_chromium
2016/10/25 14:59:07
Instead of "may be", I'd say that it is omitted -
| |
243 FastSharedBufferReader reader(&data); | |
244 char readBuffer[kBufferSize]; | |
245 | |
246 // |headerSize| is equal to |kBufferSize|, but adds more semantic insight. | |
247 constexpr size_t headerSize = 33; | |
scroggo_chromium
2016/10/25 14:59:07
nit: kHeaderSize
| |
248 png_byte header[headerSize]; | |
249 const png_byte* chunk = readAsConstPngBytep(reader, m_initialOffset, | |
250 headerSize, readBuffer); | |
251 memcpy(header, chunk, headerSize); | |
scroggo_chromium
2016/10/25 14:59:07
Alternatively, you could always use readBuffer:
i
joostouwerling
2016/10/26 15:44:17
Your option saves 33B of memory but I think it's m
scroggo_chromium
2016/10/26 18:25:58
I don't think (a constant, stack-allocated, briefl
joostouwerling
2016/10/28 18:41:25
Acknowledged.
| |
252 png_save_uint_32(header + 16, frameRect.width()); | |
scroggo_chromium
2016/10/25 14:59:07
Here I'm guessing you do *not* want the clipped wi
joostouwerling
2016/10/26 15:44:17
The width and height that are written here are non
scroggo_chromium
2016/10/26 18:25:58
Okay, I see that now. Maybe add a comment where yo
joostouwerling
2016/10/28 18:41:24
Done
| |
253 png_save_uint_32(header + 20, frameRect.height()); | |
254 png_process_data(m_png, m_info, header, headerSize); | |
255 | |
256 // Process the rest of the header chunks. Start after the PNG signature and | |
257 // IHDR chunk, 33B, and process up to the first data chunk. | |
258 processData(data, m_initialOffset + headerSize, m_bytesInfo - headerSize); | |
259 } | |
260 | |
261 void PNGImageReader::endFrameDecoding() | |
262 { | |
263 png_byte IEND[12] = {0, 0, 0, 0, 'I', 'E', 'N', 'D', 174, 66, 96, 130}; | |
264 png_process_data(m_png, m_info, IEND, 12); | |
scroggo_chromium
2016/10/25 14:59:07
We need to be careful about our setjmps. This is c
joostouwerling
2016/10/26 15:44:17
I improved this by setting the setjmp only in deco
scroggo_chromium
2016/10/26 18:25:58
sgtm
| |
265 } | |
266 | |
267 bool PNGImageReader::parse(SegmentReader& data, | |
268 PNGImageDecoder::PNGParseQuery query) | |
269 { | |
270 if (setjmp(JMPBUF(m_png))) | |
271 return m_decoder->setFailed(); | |
272 | |
273 // If the size has not been parsed, do that first, since it's necessary | |
274 // for both the Size and MetaData query. If parseSize returns false, | |
275 // it failed because of a lack of data so we can return false at this point. | |
276 if (!m_decoder->isDecodedSizeAvailable() && !parseSize(data)) | |
277 return false; | |
278 | |
279 if (query == PNGImageDecoder::PNGParseQuery::PNGSizeQuery) | |
280 return m_decoder->isDecodedSizeAvailable(); | |
281 | |
282 // For non animated images (identified by no acTL chunk before the IDAT), | |
283 // we create one frame. This saves some processing time since we don't need | |
284 // to go over the stream to find chunks. | |
285 if (!m_isAnimated) { | |
286 if (m_frameInfo.size() == 0) { | |
287 FrameInfo frame; | |
288 frame.readOffset = m_readOffset + 8; | |
289 frame.frameRect = IntRect(IntPoint(), m_decoder->size()); | |
290 frame.duration = 0; | |
291 frame.alphaBlend = ImageFrame::AlphaBlendSource::BlendAtopBgcolor; | |
292 frame.disposalMethod = ImageFrame::DisposalMethod::DisposeNotSpecifi ed; | |
293 m_frameInfo.append(frame); | |
294 m_decoder->setMetaDataDecoded(); | |
295 } | |
296 return true; | |
297 } | |
298 | |
299 FastSharedBufferReader reader(&data); | |
300 char readBuffer[kBufferSize]; | |
301 | |
302 // At this point, the query is FrameMetaDataQuery. Loop over the data and | |
303 // register all frames we can find. A frame is registered on the next fcTL | |
304 // chunk or when the IEND chunk is found. This ensures that only complete | |
305 // frames are reported, unless there is an error in the stream. | |
306 while (reader.size() >= m_readOffset + 8) { | |
307 const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, | |
308 readBuffer); | |
309 const size_t length = png_get_uint_32(chunk); | |
310 const bool isFCTLChunk = memcmp(chunk + 4, "fcTL", 4) == 0; | |
311 const bool isIENDChunk = memcmp(chunk + 4, "IEND", 4) == 0; | |
312 | |
313 // When we find an IDAT chunk (when the IDAT is part of the animation), | |
314 // or an fdAT chunk, and the readOffset field of the newFrame is 0, | |
315 // we have found the beginning of a new block of frame data. | |
316 const bool isFrameData = memcmp(chunk + 4, "fdAT", 4) == 0 | |
317 || (memcmp(chunk + 4, "IDAT", 4) == 0 && m_idatIsPartOfAnimation); | |
318 if (m_newFrame.readOffset == 0 && isFrameData) { | |
319 m_newFrame.readOffset = m_readOffset; | |
320 | |
321 // An fcTL or IEND marks the end of the previous frame. Thus, the | |
322 // FrameInfo data in m_newFrame is submitted to the m_frameInfo vector. | |
323 // | |
324 // Furthermore, an fcTL chunk indicates a new frame is coming, | |
325 // so the m_newFrame variable is prepared accordingly by setting the | |
326 // readOffset field to 0, which indicates that the frame control info | |
327 // is available but that we haven't seen any frame data yet. | |
328 } else if (isFCTLChunk || isIENDChunk) { | |
329 if (m_newFrame.readOffset != 0) { | |
330 m_newFrame.byteLength = m_readOffset - m_newFrame.readOffset; | |
331 m_frameInfo.append(m_newFrame); | |
332 m_newFrame.readOffset = 0; | |
333 } | |
334 | |
335 if (reader.size() < m_readOffset + 12 + length) | |
336 return false; | |
337 | |
338 if (isIENDChunk) { | |
339 // Let the decoder know we've parsed all data, so it does not | |
340 // need to query again. | |
341 m_decoder->setMetaDataDecoded(); | |
342 return true; | |
343 } | |
344 | |
345 // At this point, we're dealing with an fcTL chunk, since the above | |
346 // statement already returns on IEND chunks. | |
347 | |
348 // If the fcTL chunk is not 26 bytes long, we can't process it. | |
349 if (length != 26) | |
350 longjmp(JMPBUF(m_png), 1); | |
scroggo_chromium
2016/10/25 14:59:07
Why did you longjmp instead of returning setFailed
| |
351 | |
352 chunk = readAsConstPngBytep(reader, m_readOffset + 8, length, | |
353 readBuffer); | |
354 parseFrameInfo(chunk); | |
355 | |
356 } | |
357 m_readOffset += 12 + length; | |
358 } | |
359 return false; | |
360 } | |
361 | |
362 // If |length| == 0, read until the stream ends. | |
363 // @return: number of bytes processed. | |
364 size_t PNGImageReader::processData(SegmentReader& data, size_t offset, | |
365 size_t length) | |
366 { | |
367 const char* segment; | |
368 size_t totalProcessedBytes = 0; | |
369 while (size_t segmentLength = data.getSomeData(segment, offset)) { | |
370 if (length > 0 && segmentLength + totalProcessedBytes > length) | |
371 segmentLength = length - totalProcessedBytes; | |
372 png_process_data(m_png, m_info, | |
373 reinterpret_cast<png_byte*>(const_cast<char*>(segment)) , | |
374 segmentLength); | |
375 offset += segmentLength; | |
376 totalProcessedBytes += segmentLength; | |
377 if (totalProcessedBytes == length) | |
378 return length; | |
379 } | |
380 return totalProcessedBytes; | |
381 } | |
382 | |
383 // This methods reads through the stream until it has parsed the image size. | |
384 // @return true when it succeeds in parsing the size. | |
385 // false when: | |
386 // A) not enough data is provided | |
387 // B) decoding by libpng fails. In the this case, it will also call | |
388 // setFailed on m_decoder. | |
389 bool PNGImageReader::parseSize(SegmentReader &data) | |
390 { | |
391 FastSharedBufferReader reader(&data); | |
392 char readBuffer[kBufferSize]; | |
393 | |
394 // Process the PNG signature and the IHDR with libpng, such that this code | |
395 // does not need to be bothered with parsing the contents. This also enables | |
396 // the reader to use the existing headerAvailable callback in the decoder. | |
397 // | |
398 // When we already have decoded the signature, we don't need to do it again. | |
399 // By setting a flag for this we allow for byte by byte parsing. | |
400 if (!m_parsedSignature) { | |
401 if (reader.size() < m_readOffset + 8) | |
402 return false; | |
403 const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, | |
404 readBuffer); | |
405 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
406 m_readOffset += 8; | |
407 m_parsedSignature = true; | |
408 // Initialize the newFrame by setting the readOffset to 0. | |
409 m_newFrame.readOffset = 0; | |
410 } | |
411 | |
412 // This loop peeks at the chunk tag until the IDAT chunk is found. When | |
413 // a different tag is encountered, pass it on to libpng for general parsing. | |
414 // We can peek at chunks by looking at the first 8 bytes, which contain the | |
415 // length and the chunk tag. | |
416 // | |
417 // When an fcTL (frame control) is encountered before the IDAT, the frame | |
418 // data in the IDAT chunk is part of the animation. This case is flagged | |
419 // and the frame info is stored by parsing the fcTL chunk. | |
420 while (reader.size() >= m_readOffset + 8) { | |
421 const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, | |
422 readBuffer); | |
423 const png_uint_32 length = png_get_uint_32(chunk); | |
424 | |
425 // If we encounter the IDAT chunk, we're done with the png header | |
426 // chunks. Indicate this to libpng by sending the beginning of the IDAT | |
427 // chunk, which will trigger libpng to call the headerAvailable | |
428 // callback on m_decoder. This provides the size to the decoder. | |
429 if (memcmp(chunk + 4, "IDAT", 4) == 0) { | |
430 if (m_idatIsPartOfAnimation) | |
431 m_newFrame.readOffset = m_readOffset; | |
432 m_bytesInfo = m_readOffset; | |
433 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
434 return true; | |
435 } | |
436 | |
437 // Consider the PNG image animated if an acTL chunk of the correct | |
438 // length is present. Parsing the acTL content is done by | |
439 // parseAnimationControl, called by libpng's png_process_data. | |
440 if (memcmp(chunk + 4, "acTL", 4) == 0 && length == 8) | |
441 m_isAnimated = true; | |
442 | |
443 // We don't need to check for |length| here, because the decoder will | |
444 // fail later on for invalid fcTL chunks. | |
445 if (memcmp(chunk + 4, "fcTL", 4) == 0) | |
446 m_idatIsPartOfAnimation = true; | |
447 | |
448 // 12 is the length, tag and crc part of the chunk, which are all 4B. | |
449 if (reader.size() < m_readOffset + length + 12) | |
450 break; | |
451 | |
452 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
453 processData(data, m_readOffset + 8, length + 4); | |
454 m_readOffset += length + 12; | |
455 } | |
456 | |
457 // If we end up here, not enough data was available for the IDAT chunk | |
458 // So libpng would not have called headerAvailable yet. | |
459 return false; | |
460 } | |
461 | |
462 | |
463 void PNGImageReader::parseAnimationChunk(const char tag[], const void* data_chun k, size_t length) | |
464 { | |
465 const png_byte* data = static_cast<const png_byte*>(data_chunk); | |
466 | |
467 // The number of frames as indicated in the animation control chunk (acTL) | |
468 // is ignored, and the number of frames that are actually present is used. | |
469 // For now, when the number of indicated frames is different from the | |
470 // number of supplied frames, the number of supplied frames is what is | |
471 // provided to the decoder. Therefore, it does not add any benefit of | |
472 // looking at the value of the indicated framecount. A note here is that | |
473 // there may be optimisations available, for example, prescaling vectors. | |
474 if (strcmp(tag, "acTL") == 0 && length == 8) { | |
475 png_uint_32 repetitionCount = png_get_uint_32(data + 4); | |
476 m_decoder->setRepetitionCount(repetitionCount); | |
477 | |
478 // For fcTL, decoding fails if it does not have the correct length. It is | |
479 // impossible to make a guess about the frame if not all data is available. | |
480 } else if (strcmp(tag, "fcTL") == 0) { | |
481 if (length != 26) | |
482 longjmp(JMPBUF(m_png), 1); | |
scroggo_chromium
2016/10/25 14:59:06
Again, why did you longjmp instead of calling setF
joostouwerling
2016/10/26 15:44:17
The previous comment can be handled with setFailed
scroggo_chromium
2016/10/26 18:25:58
Ah yes, of course.
| |
483 parseFrameInfo(data); | |
484 } | |
485 | |
486 } | |
487 | |
488 size_t PNGImageReader::frameCount() const | |
489 { | |
490 return m_frameInfo.size(); | |
491 } | |
492 | |
493 const PNGImageReader::FrameInfo& PNGImageReader::frameInfo(size_t index) const | |
494 { | |
495 ASSERT(index < m_frameInfo.size()); | |
496 return m_frameInfo[index]; | |
497 } | |
498 | |
499 // Extract the frame control info and store it in m_newFrame. The length check | |
500 // on the data chunk has been done in parseAnimationChunk. | |
501 // The fcTL specification used can be found at: | |
502 // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu nk | |
503 void PNGImageReader::parseFrameInfo(const png_byte* data) | |
504 { | |
505 png_uint_32 width, height, xOffset, yOffset; | |
506 png_uint_16 delayNumerator, delayDenominator; | |
507 width = png_get_uint_32(data + 4); | |
508 height = png_get_uint_32(data + 8); | |
509 xOffset = png_get_uint_32(data + 12); | |
510 yOffset = png_get_uint_32(data + 16); | |
511 delayNumerator = png_get_uint_16(data + 20); | |
512 delayDenominator = png_get_uint_16(data + 22); | |
513 | |
514 m_newFrame.duration = (delayDenominator == 0) ? delayNumerator * 10 | |
515 : delayNumerator * 1000 / delayDenominator; | |
516 m_newFrame.frameRect = IntRect(xOffset, yOffset, width, height); | |
517 m_newFrame.disposalMethod = data[24]; | |
518 m_newFrame.alphaBlend = data[25]; | |
519 | |
520 } | |
521 | |
522 }; // namespace blink | |
OLD | NEW |