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_decodeOffset(0) | |
107 , m_idatOffset(0) | |
108 , m_hasAlpha(false) | |
109 , m_idatIsPartOfAnimation(false) | |
110 , m_isAnimated(false) | |
111 , m_parsedSignature(false) | |
112 #if USE(QCMSLIB) | |
113 , m_rowBuffer() | |
114 #endif | |
115 { | |
116 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0); | |
117 m_info = png_create_info_struct(m_png); | |
118 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, | |
119 pngRowAvailable, pngComplete); | |
120 | |
121 // Keep the chunks which are of interest for APNG. We don't need to keep | |
122 // the fdAT chunks, since they are converted to IDAT's by the frame decoder. | |
123 png_byte apngChunks[] = {"acTL\0fcTL\0"}; | |
124 png_set_keep_unknown_chunks(m_png, PNG_HANDLE_CHUNK_NEVER, apngChunks, 2); | |
125 png_set_read_user_chunk_fn(m_png, (png_voidp) this, readAnimationChunk); | |
126 } | |
127 | |
128 PNGImageReader::~PNGImageReader() | |
129 { | |
130 png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0); | |
131 ASSERT(!m_png && !m_info); | |
132 } | |
133 | |
134 // This method reads from the FastSharedBufferReader, starting at offset, | |
135 // and returns |length| bytes in the form of a pointer to a const png_byte*. | |
136 // This function is used to make it easy to access data from the reader in a | |
137 // png friendly way, and pass it to libpng for decoding. | |
138 // | |
139 // Pre-conditions before using this: | |
140 // - |reader|.size() >= |readOffset| + |length| | |
141 // - |buffer|.size() >= |length| | |
142 // - |length| <= |kBufferSize| | |
143 // | |
144 // The reason for the last two precondition is that currently the png signature | |
145 // plus IHDR chunk (8B + 25B = 33B) is the largest chunk that is read using this | |
146 // method. If the data is not consecutive, it is stored in |buffer|, which must | |
147 // have the size of (at least) |length|, but there's no need for it to be larger | |
148 // than |kBufferSize|. | |
149 static constexpr size_t kBufferSize = 33; | |
150 const png_byte* readAsConstPngBytep(const FastSharedBufferReader& reader, | |
151 size_t readOffset, size_t length, | |
152 char* buffer) | |
153 { | |
154 ASSERT(length <= kBufferSize); | |
155 return reinterpret_cast<const png_byte*>( | |
156 reader.getConsecutiveData(readOffset, length, buffer)); | |
157 } | |
158 | |
159 // This is used as a value for the byteLength of a frameInfo struct to | |
160 // indicate that it is the first frame, and we still need to set byteLength | |
161 // to the correct value as soon as the parser knows it. 1 is a safe value | |
162 // since the byteLength field of a frame is at least 12, in the case of an | |
163 // empty fdAT or IDAT chunk. | |
164 static constexpr size_t kFirstFrameIndicator = 1; | |
165 | |
166 void PNGImageReader::decode(SegmentReader& data, size_t index) | |
167 { | |
168 if (index >= m_frameInfo.size()) | |
169 return; | |
170 | |
171 // When decoding by libpng fails in either the non-animated branch or the | |
172 // animated branch, the decoder needs to be set to the failed state. | |
173 if (setjmp(JMPBUF(m_png))) { | |
174 m_decoder->setFailed(); | |
175 return; | |
176 } | |
177 | |
178 // For non animated PNG's, we don't want to waste CPU time with recreating | |
scroggo_chromium
2016/10/28 14:20:33
More importantly, if we are resuming the first fra
joostouwerling
2016/10/28 18:41:25
Done.
| |
179 // the png struct. It suffices to continue parsing where we left off. | |
180 if (!m_isAnimated) { | |
181 m_decodeOffset += processData( | |
scroggo_chromium
2016/10/28 14:20:33
Why not continue to use m_frameInfo[0].readOffset?
joostouwerling
2016/10/28 18:41:25
I use m_decodeOffset to keep track how far the fir
scroggo_chromium
2016/10/31 13:35:11
I figured out why I'm confused. PNGImageReader::m_
joostouwerling
2016/10/31 18:40:19
How about startOffset? When glancing over the code
scroggo_chromium
2016/10/31 19:34:06
sgtm
| |
182 data, m_frameInfo[0].readOffset + m_decodeOffset, 0); | |
183 return; | |
184 } | |
185 | |
186 // Progressive decoding is only done if: | |
scroggo_chromium
2016/10/28 14:20:33
I find the term "progressive decoding" a little bi
cblume
2016/10/28 17:29:17
If it isn't too much trouble, I would love the abi
joostouwerling
2016/10/28 18:41:26
Right. But I think we still need to use the progre
joostouwerling
2016/10/28 18:41:26
It would not be too difficult to implement progres
scroggo_chromium
2016/10/31 13:35:11
FWIW, I think it will make the landing process (or
scroggo_chromium
2016/10/31 13:35:12
Oh I'm not suggesting that we shouldn't use the pr
joostouwerling
2016/10/31 18:40:19
As agreed upon in person: for now, only the first
scroggo_chromium
2016/10/31 19:34:06
Sure, it's just that allowing progressive decoding
| |
187 // - It is the first frame, thus |index| == 0 | |
scroggo_chromium
2016/10/28 14:20:33
I think you're saying that both of these statement
joostouwerling
2016/10/28 18:41:26
Done.
| |
188 // - The byteLength of the first frame is not yet known, *or* it is known | |
189 // but we're only halfway in a progressive decode, started earlier. | |
scroggo_chromium
2016/10/28 14:20:33
I don't think you mean precisely halfway. partway?
joostouwerling
2016/10/28 18:41:26
Ack. Good question about the word. The antonyms do
| |
190 bool firstFrameIncomplete = m_frameInfo[0].byteLength == kFirstFrameIndicato r; | |
scroggo_chromium
2016/10/28 14:20:33
I find the word "Incomplete" here confusing, since
joostouwerling
2016/10/28 18:41:25
Right, if the client would have supplied more data
scroggo_chromium
2016/10/31 13:35:11
sgtm
| |
191 bool progressiveDecodingAlreadyStarted = m_decodeOffset > 0; | |
192 bool progressiveDecode = (index == 0 | |
193 && (firstFrameIncomplete || progressiveDecodingAlreadyStarted)); | |
194 | |
195 // Initialize a new png struct for this frame. For a progressive decode of | |
196 // the first frame, we only need to do this once. | |
197 if (!progressiveDecode || m_decodeOffset == 0) | |
scroggo_chromium
2016/10/28 14:20:33
I think this can be
if (!progressiveDecode || !pr
joostouwerling
2016/10/28 18:41:26
Done.
| |
198 startFrameDecoding(data, index); | |
scroggo_chromium
2016/10/28 14:20:33
If the first frame fills the image size, can we sk
joostouwerling
2016/10/28 18:41:26
We could, but there needs to be a check that this
scroggo_chromium
2016/10/31 13:35:12
I think the common case is that it *is* the origin
joostouwerling
2016/10/31 18:40:19
Done.
| |
199 | |
200 // By default, a frame will be considered to be decoded completely, unless | |
scroggo_chromium
2016/10/28 14:20:33
Try to keep comments focused on why, rather than w
joostouwerling
2016/10/28 18:41:26
Acknowledged.
scroggo_chromium
2016/10/31 13:35:11
Yes.
joostouwerling
2016/10/31 18:40:19
I added a todo, which depends on the result of the
| |
201 // the progressive decoding of the first frame returns false, which | |
202 // indicates not all data was available yet. | |
203 bool decodedFrameCompletely = true; | |
204 if (progressiveDecode) | |
205 decodedFrameCompletely = progressivelyDecodeFirstFrame(data); | |
206 else | |
207 decodeFrame(data, index); | |
208 | |
209 // Finish decoding by sending the IEND chunk, but only if the frame was | |
scroggo_chromium
2016/10/28 14:20:33
nit: I find this a little wordy. How about:
// Se
joostouwerling
2016/10/28 18:41:26
I'd say it is interesting to know that this will c
| |
210 // completely decoded. | |
211 if (decodedFrameCompletely) | |
212 endFrameDecoding(); | |
213 | |
scroggo_chromium
2016/10/28 14:20:33
nit: No need for blank line
joostouwerling
2016/10/28 18:41:26
Done.
| |
214 } | |
215 | |
216 void PNGImageReader::startFrameDecoding(SegmentReader& data, size_t index) | |
217 { | |
218 // Each frame is processed as if it were a complete, single frame png image. | |
219 // To accomplish this, destroy the current |m_png| and |m_info| structs and | |
220 // create new ones. CRC errors are ignored, so fdAT chunks can be processed | |
221 // as IDATs without recalculating the CRC value. | |
222 png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0); | |
223 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0); | |
224 m_info = png_create_info_struct(m_png); | |
225 png_set_crc_action(m_png, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE); | |
226 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, | |
227 pngRowAvailable, pngComplete); | |
228 | |
229 // If the frame is the size of the whole image, we don't need to modify any | |
230 // data in the IHDR chunk. This means it suffices to re-process all header | |
231 // data up to the first frame, for mimicking a png image. | |
232 const IntRect& frameRect = m_frameInfo[index].frameRect; | |
233 if (frameRect.location() == IntPoint() | |
234 && frameRect.size() == m_decoder->size()) { | |
235 processData(data, m_initialOffset, m_idatOffset); | |
236 return; | |
237 } | |
238 | |
239 // Process the IHDR chunk, but change the width and height so it reflects | |
240 // the frame's width and height. Image Decoder will apply the x,y offset. | |
241 // This step is omitted if the width and height are equal to the image size, | |
242 // which is done in the block above. | |
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; | |
248 png_byte header[headerSize]; | |
249 const png_byte* chunk = readAsConstPngBytep(reader, m_initialOffset, | |
250 headerSize, readBuffer); | |
251 memcpy(header, chunk, headerSize); | |
252 | |
253 // Write the unclipped width and height. Clipping happens in the decoder. | |
254 png_save_uint_32(header + 16, frameRect.width()); | |
255 png_save_uint_32(header + 20, frameRect.height()); | |
256 png_process_data(m_png, m_info, header, headerSize); | |
257 | |
258 // Process the rest of the header chunks. Start after the PNG signature and | |
259 // IHDR chunk, 33B, and process up to the first data chunk. The number of | |
260 // bytes up to the first data chunk is stored in |m_idatOffset|. | |
261 processData(data, m_initialOffset + headerSize, m_idatOffset - headerSize); | |
262 } | |
263 | |
264 bool PNGImageReader::progressivelyDecodeFirstFrame(SegmentReader& data) | |
265 { | |
266 FastSharedBufferReader reader(&data); | |
267 char readBuffer[8]; // large enough to identify a chunk. | |
268 size_t offset = m_frameInfo[0].readOffset; | |
269 | |
270 // Loop while there is enough data to do progressive decoding. | |
271 while (data.size() >= offset + 8) { | |
272 | |
273 // At the beginning of each loop, the offset is at the start of a chunk. | |
274 const png_byte* chunk = readAsConstPngBytep(reader, offset, 8, | |
275 readBuffer); | |
276 const png_uint_32 length = png_get_uint_32(chunk); | |
277 | |
278 // When an fcTL or IEND chunk is encountered, the frame data has ended. | |
279 // Return true, since all frame data is decoded. | |
280 if (memcmp(chunk, "fcTL", 4) == 0 || memcmp(chunk, "IEND", 0)) { | |
281 m_decodeOffset = 0; | |
scroggo_chromium
2016/10/28 14:20:34
Maybe I don't understand this variable. We're done
joostouwerling
2016/10/28 18:41:26
I apparently removed a comment here by accident, w
| |
282 return true; | |
283 } | |
284 | |
285 // If this chunk was already decoded, move on to the next. | |
286 if (m_decodeOffset >= offset + length + 12) { | |
287 offset += length + 12; | |
288 continue; | |
289 } | |
290 | |
291 // At this point, three scenario's are possible: | |
scroggo_chromium
2016/10/28 14:20:34
nit: scenarios*
joostouwerling
2016/10/28 18:41:25
Done.
| |
292 // 1) Some bytes of this chunk are already decoded in a previous call, | |
scroggo_chromium
2016/10/28 14:20:34
nit: are -> were
joostouwerling
2016/10/28 18:41:25
Done.
| |
293 // so we need to continue from there. | |
294 // 2) This is an fdAT chunk, so we need to convert it to an IDAT chunk | |
295 // before we can decode it. | |
296 // 3) This is any other chunk, most likely an IDAT chunk. | |
297 // | |
298 // In each scenario, we want to decode as much data as possible. In each | |
299 // one, do the scenario specific work and set |offset| to where decoding | |
300 // needs to continue. From there, decode until the end of the chunk, if | |
301 // possible. If the whole chunk is decoded, continue to the next loop. | |
302 // Otherwise, store how far we've came in |m_decodeOffset| and return | |
303 // false to indicate to the caller that the frame is partially decoded. | |
304 | |
305 size_t endOffsetChunk = offset + length + 12; | |
306 | |
307 // Scenario 1: |m_decodeOffset| is ahead of the chunk tag. | |
scroggo_chromium
2016/10/28 14:20:34
Why does this happen? I guess we didn't update rea
joostouwerling
2016/10/28 18:41:26
This can happen if a chunk is partially decoded. W
scroggo_chromium
2016/10/31 13:35:12
I have a comment about this elsewhere - I misunder
joostouwerling
2016/10/31 18:40:19
Done.
| |
308 if (m_decodeOffset >= offset + 8) { | |
309 offset = m_decodeOffset; | |
310 | |
311 // Scenario 2: we need to convert the fdAT to an IDAT chunk. For an | |
312 // explanation of the numbers, see the comments in decodeFrame(). | |
313 } else if (memcmp(chunk, "fdAT", 4) == 0) { | |
314 png_byte chunkIDAT[] = {0, 0, 0, 0, 'I', 'D', 'A', 'T'}; | |
315 png_save_uint_32(chunkIDAT, length - 4); | |
316 png_process_data(m_png, m_info, chunkIDAT, 8); | |
317 // Skip the sequence number | |
318 offset += 12; | |
319 | |
320 // Scenario 3: for any other chunk type, process the first 8 bytes. | |
321 } else { | |
322 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
323 offset += 8; | |
324 } | |
325 | |
326 size_t bytesLeftInChunk = endOffsetChunk - offset; | |
327 size_t bytesDecoded = processData(data, offset, bytesLeftInChunk); | |
328 m_decodeOffset = offset + bytesDecoded; | |
329 if (bytesDecoded < bytesLeftInChunk) | |
330 return false; | |
331 offset += bytesDecoded; | |
332 } | |
333 | |
334 return false; | |
335 } | |
336 | |
337 void PNGImageReader::decodeFrame(SegmentReader& data, size_t index) | |
338 { | |
339 // From the frame info that was gathered during parsing, it is known at | |
340 // what offset the frame data starts and how many bytes are in the stream | |
341 // before the frame ends. Using this, we process all chunks that fall in | |
342 // this interval. We catch every fdAT chunk and transform it to an IDAT | |
343 // chunk, so libpng will decode it like a non-animated PNG image. | |
344 size_t offset = m_frameInfo[index].readOffset; | |
345 size_t endOffset = offset + m_frameInfo[index].byteLength; | |
346 char readBuffer[8]; | |
347 FastSharedBufferReader reader(&data); | |
348 | |
349 while (offset < endOffset) { | |
350 const png_byte* chunk = readAsConstPngBytep(reader, offset, 8, readBuffe r); | |
351 const png_uint_32 length = png_get_uint_32(chunk); | |
352 if (memcmp(chunk + 4, "fdAT", 4) == 0) { | |
353 // An fdAT chunk is build up as follows: | |
354 // - |length| (4B) | |
355 // - fdAT tag (4B) | |
356 // - sequence number (4B) | |
357 // - frame data (|length| - 4B) | |
358 // - CRC (4B) | |
359 // Thus, to reformat this into an IDAT chunk, we need to: | |
360 // - write |length| - 4 as the new length, since the sequence number | |
361 // must be removed. | |
362 // - change the tag to IDAT. | |
363 // - omit the sequence number from the data part of the chunk. | |
364 png_byte chunkIDAT[] = {0, 0, 0, 0, 'I', 'D', 'A', 'T'}; | |
365 png_save_uint_32(chunkIDAT, length - 4); | |
366 png_process_data(m_png, m_info, chunkIDAT, 8); | |
367 // The frame data and the CRC span |length| bytes, so skip the | |
368 // sequence number and process |length| bytes to decode the frame. | |
369 processData(data, offset + 12, length); | |
370 } else { | |
371 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
372 processData(data, offset + 8, length + 4); | |
373 } | |
374 offset += 12 + length; | |
375 } | |
376 } | |
377 | |
378 void PNGImageReader::endFrameDecoding() | |
379 { | |
380 png_byte IEND[12] = {0, 0, 0, 0, 'I', 'E', 'N', 'D', 174, 66, 96, 130}; | |
381 png_process_data(m_png, m_info, IEND, 12); | |
382 } | |
383 | |
384 bool PNGImageReader::parse(SegmentReader& data, | |
385 PNGImageDecoder::PNGParseQuery query) | |
386 { | |
387 if (setjmp(JMPBUF(m_png))) | |
388 return m_decoder->setFailed(); | |
389 | |
390 // If the size has not been parsed, do that first, since it's necessary | |
391 // for both the Size and MetaData query. If parseSize returns false, | |
392 // it failed because of a lack of data so we can return false at this point. | |
393 if (!m_decoder->isDecodedSizeAvailable() && !parseSize(data)) | |
394 return false; | |
395 | |
396 if (query == PNGImageDecoder::PNGParseQuery::PNGSizeQuery) | |
397 return m_decoder->isDecodedSizeAvailable(); | |
398 | |
399 // For non animated images (identified by no acTL chunk before the IDAT), | |
400 // we create one frame. This saves some processing time since we don't need | |
401 // to go over the stream to find chunks. | |
402 if (!m_isAnimated) { | |
403 if (m_frameInfo.isEmpty()) { | |
404 FrameInfo frame; | |
405 // This needs to be plus 8 since the first 8 bytes of the IDAT chunk | |
406 // are already processed in parseSize(). | |
407 frame.readOffset = m_readOffset + 8; | |
408 frame.frameRect = IntRect(IntPoint(), m_decoder->size()); | |
409 frame.duration = 0; | |
410 frame.alphaBlend = ImageFrame::AlphaBlendSource::BlendAtopBgcolor; | |
411 frame.disposalMethod = ImageFrame::DisposalMethod::DisposeNotSpecifi ed; | |
412 m_frameInfo.append(frame); | |
413 m_decoder->setMetaDataDecoded(); | |
414 } | |
415 return true; | |
416 } | |
417 | |
418 FastSharedBufferReader reader(&data); | |
419 char readBuffer[kBufferSize]; | |
420 | |
421 // At this point, the query is FrameMetaDataQuery. Loop over the data and | |
422 // register all frames we can find. A frame is registered on the next fcTL | |
423 // chunk or when the IEND chunk is found. This ensures that only complete | |
424 // frames are reported, unless there is an error in the stream. | |
425 while (reader.size() >= m_readOffset + 8) { | |
426 const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, | |
427 readBuffer); | |
428 const size_t length = png_get_uint_32(chunk); | |
429 const bool isFCTLChunk = memcmp(chunk + 4, "fcTL", 4) == 0; | |
430 const bool isIENDChunk = memcmp(chunk + 4, "IEND", 4) == 0; | |
431 | |
432 // When we find an IDAT chunk (when the IDAT is part of the animation), | |
433 // or an fdAT chunk, and the readOffset field of the newFrame is 0, | |
434 // we have found the beginning of a new block of frame data. | |
435 const bool isFrameData = memcmp(chunk + 4, "fdAT", 4) == 0 | |
436 || (memcmp(chunk + 4, "IDAT", 4) == 0 && m_idatIsPartOfAnimation); | |
437 if (m_newFrame.readOffset == 0 && isFrameData) { | |
438 m_newFrame.readOffset = m_readOffset; | |
439 | |
440 // When the |frameInfo| vector is empty, the first frame needs to be | |
441 // reported as soon as possible, even before all frame data is in | |
442 // |data|, so the first frame can be decoded progressively. | |
443 if (m_frameInfo.isEmpty()) { | |
444 m_newFrame.byteLength = kFirstFrameIndicator; | |
445 m_frameInfo.append(m_newFrame); | |
446 } | |
447 | |
448 // An fcTL or IEND marks the end of the previous frame. Thus, the | |
449 // FrameInfo data in m_newFrame is submitted to the m_frameInfo vector. | |
450 // | |
451 // Furthermore, an fcTL chunk indicates a new frame is coming, | |
452 // so the m_newFrame variable is prepared accordingly by setting the | |
453 // readOffset field to 0, which indicates that the frame control info | |
454 // is available but that we haven't seen any frame data yet. | |
455 } else if (isFCTLChunk || isIENDChunk) { | |
456 if (m_newFrame.readOffset != 0) { | |
457 m_newFrame.byteLength = m_readOffset - m_newFrame.readOffset; | |
458 if (m_frameInfo[0].byteLength == kFirstFrameIndicator) | |
scroggo_chromium
2016/10/28 14:20:34
What would happen if we had a broken APNG with the
joostouwerling
2016/10/28 18:41:25
The IEND case is indeed an invalid APNG file, but
| |
459 m_frameInfo[0].byteLength = m_newFrame.byteLength; | |
460 else | |
461 m_frameInfo.append(m_newFrame); | |
462 | |
463 m_newFrame.readOffset = 0; | |
464 } | |
465 | |
466 if (reader.size() < m_readOffset + 12 + length) | |
467 return false; | |
468 | |
469 if (isIENDChunk) { | |
470 // Let the decoder know we've parsed all data, so it does not | |
471 // need to query again. | |
472 m_decoder->setMetaDataDecoded(); | |
473 return true; | |
474 } | |
475 | |
476 // At this point, we're dealing with an fcTL chunk, since the above | |
477 // statement already returns on IEND chunks. | |
478 | |
479 // If the fcTL chunk is not 26 bytes long, we can't process it. | |
480 if (length != 26) | |
481 return m_decoder->setFailed(); | |
482 | |
483 chunk = readAsConstPngBytep(reader, m_readOffset + 8, length, | |
484 readBuffer); | |
485 parseFrameInfo(chunk); | |
486 | |
487 } | |
488 m_readOffset += 12 + length; | |
489 } | |
490 return false; | |
491 } | |
492 | |
493 // If |length| == 0, read until the stream ends. | |
494 // @return: number of bytes processed. | |
495 size_t PNGImageReader::processData(SegmentReader& data, size_t offset, | |
496 size_t length) | |
497 { | |
498 const char* segment; | |
499 size_t totalProcessedBytes = 0; | |
500 while (size_t segmentLength = data.getSomeData(segment, offset)) { | |
501 if (length > 0 && segmentLength + totalProcessedBytes > length) | |
502 segmentLength = length - totalProcessedBytes; | |
503 png_process_data(m_png, m_info, | |
504 reinterpret_cast<png_byte*>(const_cast<char*>(segment)) , | |
505 segmentLength); | |
506 offset += segmentLength; | |
507 totalProcessedBytes += segmentLength; | |
508 if (totalProcessedBytes == length) | |
509 return length; | |
510 } | |
511 return totalProcessedBytes; | |
512 } | |
513 | |
514 // This methods reads through the stream until it has parsed the image size. | |
515 // @return true when it succeeds in parsing the size. | |
516 // false when: | |
517 // A) not enough data is provided | |
518 // B) decoding by libpng fails. In the this case, it will also call | |
519 // setFailed on m_decoder. | |
520 bool PNGImageReader::parseSize(SegmentReader &data) | |
521 { | |
522 FastSharedBufferReader reader(&data); | |
523 char readBuffer[kBufferSize]; | |
524 | |
525 // Process the PNG signature and the IHDR with libpng, such that this code | |
526 // does not need to be bothered with parsing the contents. This also enables | |
527 // the reader to use the existing headerAvailable callback in the decoder. | |
528 // | |
529 // When we already have decoded the signature, we don't need to do it again. | |
530 // By setting a flag for this we allow for byte by byte parsing. | |
531 if (!m_parsedSignature) { | |
532 if (reader.size() < m_readOffset + 8) | |
533 return false; | |
534 const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, | |
535 readBuffer); | |
536 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
537 m_readOffset += 8; | |
538 m_parsedSignature = true; | |
539 // Initialize the newFrame by setting the readOffset to 0. | |
540 m_newFrame.readOffset = 0; | |
541 } | |
542 | |
543 // This loop peeks at the chunk tag until the IDAT chunk is found. When | |
544 // a different tag is encountered, pass it on to libpng for general parsing. | |
545 // We can peek at chunks by looking at the first 8 bytes, which contain the | |
546 // length and the chunk tag. | |
547 // | |
548 // When an fcTL (frame control) is encountered before the IDAT, the frame | |
549 // data in the IDAT chunk is part of the animation. This case is flagged | |
550 // and the frame info is stored by parsing the fcTL chunk. | |
551 while (reader.size() >= m_readOffset + 8) { | |
552 const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, | |
553 readBuffer); | |
554 const png_uint_32 length = png_get_uint_32(chunk); | |
555 | |
556 // If we encounter the IDAT chunk, we're done with the png header | |
557 // chunks. Indicate this to libpng by sending the beginning of the IDAT | |
558 // chunk, which will trigger libpng to call the headerAvailable | |
559 // callback on m_decoder. This provides the size to the decoder. | |
560 if (memcmp(chunk + 4, "IDAT", 4) == 0) { | |
561 m_idatOffset = m_readOffset; | |
562 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
563 return true; | |
564 } | |
565 | |
566 // Consider the PNG image animated if an acTL chunk of the correct | |
567 // length is present. Parsing the acTL content is done by | |
568 // parseAnimationControl, called by libpng's png_process_data. | |
569 if (memcmp(chunk + 4, "acTL", 4) == 0 && length == 8) | |
570 m_isAnimated = true; | |
571 | |
572 // We don't need to check for |length| here, because the decoder will | |
573 // fail later on for invalid fcTL chunks. | |
574 if (memcmp(chunk + 4, "fcTL", 4) == 0) | |
575 m_idatIsPartOfAnimation = true; | |
576 | |
577 // 12 is the length, tag and crc part of the chunk, which are all 4B. | |
578 if (reader.size() < m_readOffset + length + 12) | |
579 break; | |
580 | |
581 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
582 processData(data, m_readOffset + 8, length + 4); | |
583 m_readOffset += length + 12; | |
584 } | |
585 | |
586 // If we end up here, not enough data was available for the IDAT chunk | |
587 // So libpng would not have called headerAvailable yet. | |
588 return false; | |
589 } | |
590 | |
591 | |
592 void PNGImageReader::parseAnimationChunk(const char tag[], const void* data_chun k, size_t length) | |
593 { | |
594 const png_byte* data = static_cast<const png_byte*>(data_chunk); | |
595 | |
596 // The number of frames as indicated in the animation control chunk (acTL) | |
597 // is ignored, and the number of frames that are actually present is used. | |
598 // For now, when the number of indicated frames is different from the | |
599 // number of supplied frames, the number of supplied frames is what is | |
600 // provided to the decoder. Therefore, it does not add any benefit of | |
601 // looking at the value of the indicated framecount. A note here is that | |
602 // there may be optimisations available, for example, prescaling vectors. | |
603 if (strcmp(tag, "acTL") == 0 && length == 8) { | |
604 png_uint_32 repetitionCount = png_get_uint_32(data + 4); | |
605 m_decoder->setRepetitionCount(repetitionCount); | |
606 | |
607 // For fcTL, decoding fails if it does not have the correct length. It is | |
608 // impossible to make a guess about the frame if not all data is available. | |
609 // Use longjmp to get back to parse(), which is necessary since this method | |
610 // is called by a libpng callback. | |
611 } else if (strcmp(tag, "fcTL") == 0) { | |
612 if (length != 26) | |
613 longjmp(JMPBUF(m_png), 1); | |
614 parseFrameInfo(data); | |
615 } | |
616 | |
617 } | |
618 | |
619 size_t PNGImageReader::frameCount() const | |
620 { | |
621 return m_frameInfo.size(); | |
622 } | |
623 | |
624 const PNGImageReader::FrameInfo& PNGImageReader::frameInfo(size_t index) const | |
625 { | |
626 ASSERT(index < m_frameInfo.size()); | |
627 return m_frameInfo[index]; | |
628 } | |
629 | |
630 // Extract the frame control info and store it in m_newFrame. The length check | |
631 // on the data chunk has been done in parseAnimationChunk. | |
632 // The fcTL specification used can be found at: | |
633 // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu nk | |
634 void PNGImageReader::parseFrameInfo(const png_byte* data) | |
635 { | |
636 png_uint_32 width, height, xOffset, yOffset; | |
637 png_uint_16 delayNumerator, delayDenominator; | |
638 width = png_get_uint_32(data + 4); | |
639 height = png_get_uint_32(data + 8); | |
640 xOffset = png_get_uint_32(data + 12); | |
641 yOffset = png_get_uint_32(data + 16); | |
642 delayNumerator = png_get_uint_16(data + 20); | |
643 delayDenominator = png_get_uint_16(data + 22); | |
644 | |
645 m_newFrame.duration = (delayDenominator == 0) ? delayNumerator * 10 | |
646 : delayNumerator * 1000 / delayDenominator; | |
647 m_newFrame.frameRect = IntRect(xOffset, yOffset, width, height); | |
648 m_newFrame.disposalMethod = data[24]; | |
649 m_newFrame.alphaBlend = data[25]; | |
650 | |
651 } | |
652 | |
653 }; // namespace blink | |
OLD | NEW |