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

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

Issue 2386453003: WIP: Implement APNG (Closed)
Patch Set: Implement frame meta data decoding, include tests Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 pngFrameHeaderAvailable(png_structp png, png_infop)
68 {
69 imageDecoder(png)->frameHeaderAvailable();
70 }
71
72 void PNGAPI pngHeaderAvailable(png_structp png, png_infop)
73 {
74 imageDecoder(png)->headerAvailable();
75 }
76
77 void PNGAPI pngRowAvailable(png_structp png, png_bytep row, png_uint_32 rowIndex , int state)
78 {
79 imageDecoder(png)->rowAvailable(row, rowIndex, state);
80 }
81
82 void PNGAPI pngComplete(png_structp png, png_infop)
83 {
84 imageDecoder(png)->complete();
85 }
86
87 void PNGAPI pngFailed(png_structp png, png_const_charp err)
88 {
89 SkDebugf("In pngFailed with err %s.", err);
scroggo_chromium 2016/10/11 20:13:10 You'll need to remove this before submitting.
90 longjmp(JMPBUF(png), 1);
91 }
92
93 } // namespace
94
95 namespace blink {
96
97 /*
scroggo_chromium 2016/10/11 20:13:11 I think most of the comments in ImageDecoder use /
98 * This is the callback function for unknown PNG chunks, which is used to
99 * extract the animation chunks.
100 */
101 static int readAnimationChunk(png_structp png_ptr, png_unknown_chunkp chunk)
102 {
103 PNGImageReader* reader = (PNGImageReader*) png_get_user_chunk_ptr(png_ptr);
104 reader->parseAnimationChunk((const char*) chunk->name, chunk->data, chunk->s ize);
105 return 1;
106 }
107
108 PNGImageReader::PNGImageReader(PNGImageDecoder* decoder, size_t readOffset)
109 : m_decoder(decoder)
110 , m_readOffset(readOffset)
111 , m_hasAlpha(false)
112 , m_idatPartOfAnimation(false)
113 , m_parsedSignature(false)
114 #if USE(QCMSLIB)
115 , m_rowBuffer()
116 #endif
117 {
118 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0);
119 m_info = png_create_info_struct(m_png);
120 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, pngRowAvai lable, pngComplete);
121
122 // Keep the chunks which are of interest for APNG.
123 png_byte apngChunks[] = {"acTL\0fcTL\0fdAT\0"};
124 png_set_keep_unknown_chunks(m_png, PNG_HANDLE_CHUNK_NEVER, apngChunks, 3);
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 /*
135 * @TODO(joostouwerling) implement this method
136 * Decode the frame at `index`
scroggo_chromium 2016/10/11 20:13:11 Lots of places in the code would refer to this as
joostouwerling 2016/10/12 20:49:47 The |var| notation confuses me because my mind int
scroggo_chromium 2016/10/13 13:49:32 Yay, math!
137 */
138 void PNGImageReader::decode(SegmentReader& data, size_t index)
139 {
140
141 }
142
143 /*
144 * This method reads from the FastSharedBufferReader, starting at offset,
145 * and returns |length| bytes in the form of a pointer to a png_byte*. This
146 * function is used to make it easy to pass data from the stream to
147 * png_process_data.
148 *
149 * Pre-conditions before using this:
150 * - reader.size() >= offset + length
scroggo_chromium 2016/10/11 20:13:11 nit: readOffset
151 * - |m_readBuffer| = 26 >= length
scroggo_chromium 2016/10/11 20:13:10 Does m_readBuffer need to be a member variable? I
joostouwerling 2016/10/12 20:49:47 The reason I created this function is so I don't n
152 *
153 * The reason for the last precondition is that at this point, the fcTL
154 * chunk (26B) is the largest chunk that is read using this method. If the data
155 * is not consecutive, it is stored in m_readBuffer, which has a size of 26B.
156 */
157 png_byte* PNGImageReader::readAsPngBytep(const FastSharedBufferReader &reader,
scroggo_chromium 2016/10/11 20:13:10 nit: & should go next to the type: FastSharedBuff
158 size_t readOffset, size_t length)
159 {
160 return const_cast<png_byte*>(reinterpret_cast<const png_byte*>(
scroggo_chromium 2016/10/11 20:13:11 Why the const_cast? I suppose the methods you'll c
joostouwerling 2016/10/12 20:49:47 See comments above.
161 reader.getConsecutiveData(readOffset, length, m_readBuffer)));
162 }
163
164 bool PNGImageReader::parse(SegmentReader& data, PNGImageDecoder::PNGParseQuery q uery)
165 {
166 if (setjmp(JMPBUF(m_png)))
167 return m_decoder->setFailed();
168
169 /*
170 * If the size has not been parsed, do that first, since it's necessary
171 * for both the Size and MetaData query. If parseSize returns false,
172 * it failed because of a lack of data so we can return false at this point.
173 */
174 if (!m_decoder->isDecodedSizeAvailable()) {
175 if (!parseSize(data))
scroggo_chromium 2016/10/11 20:13:10 nit: This could be one if statement: if (!m_decod
176 return false;
177 }
178
179 if (query == PNGImageDecoder::PNGParseQuery::PNGSizeQuery)
180 return m_decoder->isDecodedSizeAvailable();
181
182 FastSharedBufferReader reader(&data);
183
184 /*
185 * At this point, the query is FrameMetaDataQuery. Let's go ahead and loop
scroggo_chromium 2016/10/11 20:13:11 nit: "Let's go ahead and" is informal, and probabl
186 * over the data and register all frames we can find. A frame is registered
187 * on the next fcTL chunk or when the IEND chunk is found. This ensures
188 * that all frame data is available, and that the frame data chunks have the
scroggo_chromium 2016/10/11 20:13:11 This is true, but I think it could be more explici
189 * length they said they have.
190 */
191 while (reader.size() >= m_readOffset + 8) {
192 png_byte* chunk = readAsPngBytep(reader, m_readOffset, 8);
193 size_t length = png_get_uint_32(chunk);
scroggo_chromium 2016/10/11 20:13:11 nit: I think this can be const. Same for the bools
194
195 /*
196 * When we find an IDAT chunk (when the IDAT is part of the animation),
197 * or an fdAT chunk, and the readOffset field of the newFrame is 0,
198 * we have found the beginning of a new block of frame data.
199 */
200 bool isFrameData = (memcmp(chunk + 4, "IDAT", 4) == 0 && m_idatPartOfAni mation)
201 || memcmp(chunk + 4, "fdAT", 4) == 0;
202 if (m_newFrame.readOffset == 0 && isFrameData)
203 m_newFrame.readOffset = m_readOffset;
204
205 /*
206 * An fcTL or IEND marks the end of the previous frame. Thus, the
207 * FrameInfo data in m_newFrame is submitted to the m_frameInfo vector.
208 *
209 * Furthermore, an fcTL chunk indicates a new frame is coming,
210 * so the m_newFrame variable is prepared accordingly by setting the
211 * readOffset field to 0, which indicates that the frame control info
212 * is available but that we haven't seen any frame data yet.
213 */
214 bool isFCTL = memcmp(chunk + 4, "fcTL", 4) == 0;
215 bool isIEND = memcmp(chunk + 4, "IEND", 4) == 0;
216 if (isFCTL || isIEND) {
217 if (m_newFrame.readOffset != 0) {
218 m_newFrame.byteLength = (m_readOffset - 1) - m_newFrame.readOffs et;
219 m_frameInfo.append(m_newFrame);
220 m_newFrame.readOffset = 0;
221 }
222
223 if (reader.size() < m_readOffset + 12 + length)
224 return false;
scroggo_chromium 2016/10/11 20:13:11 If you return false here, and then receive more da
joostouwerling 2016/10/12 20:49:47 Afaik it should be fine since isFrameData and (isF
scroggo_chromium 2016/10/13 13:49:33 Ah yes, those are mutually exclusive, since |chunk
225
226 if (isIEND) {
227 // Let the decoder know we've parsed all data, so it does not
228 // need to query again.
229 m_decoder->setMetaDataDecoded();
230 return true;
231 }
232
233 // Prepare the new frame info and read the frame control data.
234 chunk = readAsPngBytep(reader, m_readOffset + 8, length);
235 parseFrameInfo(chunk);
236 }
237 m_readOffset += 12 + length;
238 }
239 return false;
240 }
241
242 bool PNGImageReader::processData(SegmentReader& data, size_t offset, size_t leng th)
243 {
244 const char* segment;
245 size_t decodedLength = 0;
scroggo_chromium 2016/10/11 20:13:10 nit: I think if you put the word "total" in this n
246 while (size_t segmentLength = data.getSomeData(segment, offset)) {
scroggo_chromium 2016/10/11 20:13:11 I think you need to add decodedLength to offset, o
247 if (segmentLength > length)
248 segmentLength = length;
249 png_process_data(m_png, m_info,
scroggo_chromium 2016/10/11 20:13:10 Do you need to call setjmp in this method in case
joostouwerling 2016/10/12 20:49:46 Unwinding to the caller is sufficient, for now. I'
250 reinterpret_cast<png_byte*>(const_cast<char*>(segment)) ,
251 segmentLength);
252 decodedLength += segmentLength;
253 if (decodedLength == length)
254 return true;
255 }
256 return false;
257 }
258
259 /*
260 * This methods reads through the stream until it has parsed the image size.
261 * It returns true when it succeeds. If there is not enough data or when the
scroggo_chromium 2016/10/11 20:13:10 Again, I think some formatting can make this clear
262 * decoding by libpng fails. In the latter case, it will also call setFailed
263 * on m_decoder.
264 */
265 bool PNGImageReader::parseSize(SegmentReader &data)
266 {
267 FastSharedBufferReader reader(&data);
268
269 /*
270 * Process the PNG signature and the IHDR with libpng, such that this code
271 * does not need to be bothered with parsing the contents. This also enables
272 * the reader to use the existing headerAvailable callback in the decoder.
273 *
274 * When we already have decoded the signature, we don't need to do it again.
275 * By setting a flag for this we allow for byte by byte parsing.
276 */
277 png_byte* chunk;
278 if (!m_parsedSignature) {
279 if (reader.size() < m_readOffset + 8)
scroggo_chromium 2016/10/11 20:13:10 I'm assuming m_readOffset will be zero at this poi
joostouwerling 2016/10/12 20:49:46 I (wrongly?) assumed that PNGImageReader was const
scroggo_chromium 2016/10/13 13:49:32 Oops - no, you are correct. In particular, I think
280 return false;
281 chunk = readAsPngBytep(reader, m_readOffset, 8);
282 png_process_data(m_png, m_info, chunk, 8);
283 m_readOffset += 8;
284 m_parsedSignature = true;
285 // Initialize the newFrame by setting the readOffset to 0.
286 m_newFrame.readOffset = 0;
287 }
288
289 /*
290 * This loop peeks at the chunk tag until the IDAT chunk is found. When
291 * a different tag is encountered, pass it on to libpng for general parsing.
292 * We can peek at chunks by looking at the first 8 bytes, which contain the
293 * length and the chunk tag.
294 *
295 * When an fcTL (frame control) is encountered before the IDAT, the frame
296 * data in the IDAT chunk is part of the animation. This case is flagged
297 * and the frame info is stored by parsing the fcTL chunk.
298 */
299 while (reader.size() > m_readOffset + 8) {
300 chunk = readAsPngBytep(reader, m_readOffset, 8);
scroggo_chromium 2016/10/11 20:13:10 I think you should separately declare png_byte* ch
301 png_uint_32 length = png_get_uint_32(chunk);
scroggo_chromium 2016/10/11 20:13:10 nit: I think this can be const.
302
303 /*
304 * If we encounter the IDAT chunk, we're done with the header.
305 * Indicate this to libpng by sending the beginning of the IDAT chunk
scroggo_chromium 2016/10/11 20:13:11 We're telling libpng that we're done with the head
306 * The size is encoded in the header, so it can return true.
307 */
308 if (memcmp(chunk + 4, "IDAT", 4) == 0) {
309 m_newFrame.readOffset = m_readOffset;
310 png_process_data(m_png, m_info, chunk, 8);
311 return true;
312 };
scroggo_chromium 2016/10/11 20:13:11 No need for a ";" here.
313
314 if (memcmp(chunk + 4, "fcTL", 4) == 0)
315 m_idatPartOfAnimation = true;
316
317 // 12 is the length, tag and crc part of the chunk, which are all 4B.
318 if (reader.size() < m_readOffset + length + 12)
319 break;
320
321 png_process_data(m_png, m_info, chunk, 8);
322 processData(data, m_readOffset + 8, length + 4);
323 m_readOffset += length + 12;
324 }
325
326 // If we end up here, not enough data was available for the IDAT chunk
327 // So libpng would not have called headerAvailable yet.
328 return false;
329 }
330
331
332 void PNGImageReader::parseAnimationChunk(const char tag[], const void* data_chun k, size_t length)
333 {
334 const png_byte* data = static_cast<const png_byte*>(data_chunk);
335
336 if (strcmp(tag, "acTL") == 0 && length == 8)
337 parseAnimationControl(data);
338 else if (strcmp(tag, "fcTL") == 0 && length == 26)
339 parseFrameInfo(data);
340
341 }
342
343 size_t PNGImageReader::frameCount() const
344 {
345 return m_frameInfo.size();
346 }
347
348 const PNGImageReader::FrameInfo& PNGImageReader::frameInfo(size_t index) const
349 {
350 ASSERT(index < m_frameInfo.size());
351 return m_frameInfo[index];
352 }
353
354 /*
355 * The number of frames as indicated in the animation control chunk is ignored,
356 * and the number of frames that are actually present is used. For now, when the
357 * number of indicated frames is different from the number of supplied frames,
358 * the number of supplied frames is what is provided to the decoder. Therefore,
359 * it does not add any benefit of looking at the value of the indicated
360 * framecount. A note here is that there may be optimisations available by, for
361 * example, prescaling vectors.
362 */
363 void PNGImageReader::parseAnimationControl(const png_byte* data)
scroggo_chromium 2016/10/11 20:13:10 nit: It seems like you could put this method's con
364 {
365 png_uint_32 repetitionCount = png_get_uint_32(data + 4);
366 m_decoder->setRepetitionCount(repetitionCount);
367 }
368
369 /*
370 * These are mapped according to:
371 * https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu nk
372 */
373 inline ImageFrame::DisposalMethod getDisposalMethod(uint8_t disposalMethod)
374 {
375 switch (disposalMethod) {
376 case 0:
377 return ImageFrame::DisposalMethod::DisposeKeep;
378 case 1:
379 return ImageFrame::DisposalMethod::DisposeOverwriteBgcolor;
380 case 2:
381 return ImageFrame::DisposalMethod::DisposeOverwritePrevious;
382 }
383 return ImageFrame::DisposalMethod::DisposeNotSpecified;
384 }
385
386 /*
387 * These are mapped according to:
388 * https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu nk
389 */
390 inline ImageFrame::AlphaBlendSource getAlphaBlend(uint8_t alphaBlend)
391 {
392 if (alphaBlend == 1)
393 return ImageFrame::AlphaBlendSource::BlendAtopPreviousFrame;
394 return ImageFrame::AlphaBlendSource::BlendAtopBgcolor;
395 }
396
397 /*
398 * Extract the frame control info and store it in m_newFrame. Use the fcTL
399 * specification which can be found at:
400 * https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu nk
401 */
402 void PNGImageReader::parseFrameInfo(const png_byte* data)
403 {
404 png_uint_32 width, height, xOffset, yOffset;
405 png_uint_16 delayNumerator, delayDenominator;
406 png_byte disposalMethod, alphaBlend;
407 width = png_get_uint_32(data + 4);
408 height = png_get_uint_32(data + 8);
409 xOffset = png_get_uint_32(data + 12);
410 yOffset = png_get_uint_32(data + 16);
411 delayNumerator = png_get_uint_16(data + 20);
412 delayDenominator = png_get_uint_16(data + 22);
413 disposalMethod = data[24];
414 alphaBlend = data[25];
415
416 m_newFrame.duration = (delayDenominator == 0) ? delayNumerator * 10
417 : delayNumerator * 1000 / delayDenominator;
418 m_newFrame.offset = IntPoint(xOffset, yOffset);
419 m_newFrame.size = IntSize(width, height);
420 m_newFrame.disposalMethod = getDisposalMethod(disposalMethod);
421 m_newFrame.alphaBlend = getAlphaBlend(alphaBlend);
422
423 }
424
425 }; // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698