Chromium Code Reviews| 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/PNGImageReader.h" | 39 #include "platform/image-decoders/png/PNGImageReader.h" |
| 40 | 40 |
| 41 #include "platform/image-decoders/FastSharedBufferReader.h" | |
| 41 #include "platform/image-decoders/SegmentReader.h" | 42 #include "platform/image-decoders/SegmentReader.h" |
| 43 #include "platform/image-decoders/png/PNGImageDecoder.h" | |
| 42 #include "png.h" | 44 #include "png.h" |
| 43 #include "wtf/PtrUtil.h" | 45 #include "wtf/PtrUtil.h" |
| 44 #include <memory> | 46 #include <memory> |
| 45 | 47 |
| 46 namespace { | 48 namespace { |
| 47 | 49 |
| 48 inline blink::PNGImageDecoder* imageDecoder(png_structp png) { | 50 inline blink::PNGImageDecoder* imageDecoder(png_structp png) { |
| 49 return static_cast<blink::PNGImageDecoder*>(png_get_progressive_ptr(png)); | 51 return static_cast<blink::PNGImageDecoder*>(png_get_progressive_ptr(png)); |
| 50 } | 52 } |
| 51 | 53 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 65 } | 67 } |
| 66 | 68 |
| 67 void PNGAPI pngFailed(png_structp png, png_const_charp) { | 69 void PNGAPI pngFailed(png_structp png, png_const_charp) { |
| 68 longjmp(JMPBUF(png), 1); | 70 longjmp(JMPBUF(png), 1); |
| 69 } | 71 } |
| 70 | 72 |
| 71 } // namespace | 73 } // namespace |
| 72 | 74 |
| 73 namespace blink { | 75 namespace blink { |
| 74 | 76 |
| 75 PNGImageReader::PNGImageReader(PNGImageDecoder* decoder, size_t readOffset) | 77 // This is the callback function for unknown PNG chunks, which is used to |
| 78 // extract the animation chunks. | |
| 79 static int readAnimationChunk(png_structp pngPtr, png_unknown_chunkp chunk) { | |
| 80 PNGImageReader* reader = (PNGImageReader*)png_get_user_chunk_ptr(pngPtr); | |
| 81 reader->parseAnimationChunk((const char*)chunk->name, chunk->data, | |
| 82 chunk->size); | |
| 83 return 1; | |
| 84 } | |
| 85 | |
| 86 PNGImageReader::PNGImageReader(PNGImageDecoder* decoder, size_t initialOffset) | |
| 76 : m_decoder(decoder), | 87 : m_decoder(decoder), |
| 77 m_readOffset(readOffset), | 88 m_initialOffset(initialOffset), |
| 78 m_currentBufferSize(0), | 89 m_readOffset(initialOffset), |
| 79 m_decodingSizeOnly(false), | 90 m_progressiveDecodeOffset(0), |
| 80 m_hasAlpha(false) { | 91 m_idatOffset(0), |
| 92 m_idatIsPartOfAnimation(false), | |
| 93 m_isAnimated(false), | |
| 94 m_parsedSignature(false), | |
| 95 m_parseCompleted(false) | |
| 96 #if USE(QCMSLIB) | |
|
scroggo_chromium
2016/12/07 13:44:44
QCMSLIB has been removed.
joostouwerling
2016/12/07 16:49:10
Done.
| |
| 97 , | |
| 98 m_rowBuffer() | |
| 99 #endif | |
| 100 { | |
| 81 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0); | 101 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0); |
| 82 m_info = png_create_info_struct(m_png); | 102 m_info = png_create_info_struct(m_png); |
| 83 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, | 103 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, |
| 84 pngRowAvailable, pngComplete); | 104 pngRowAvailable, pngComplete); |
| 105 | |
| 106 // Keep the chunks which are of interest for APNG. We don't need to keep | |
| 107 // the fdAT chunks, since they are converted to IDAT's by the frame decoder. | |
| 108 png_byte apngChunks[] = {"acTL\0fcTL\0"}; | |
| 109 png_set_keep_unknown_chunks(m_png, PNG_HANDLE_CHUNK_NEVER, apngChunks, 2); | |
| 110 png_set_read_user_chunk_fn(m_png, (png_voidp)this, readAnimationChunk); | |
| 85 } | 111 } |
| 86 | 112 |
| 87 PNGImageReader::~PNGImageReader() { | 113 PNGImageReader::~PNGImageReader() { |
| 88 png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0); | 114 png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0); |
| 89 DCHECK(!m_png && !m_info); | 115 DCHECK(!m_png && !m_info); |
| 90 | 116 } |
| 91 m_readOffset = 0; | 117 |
| 92 } | 118 // This method reads from the FastSharedBufferReader, starting at offset, |
| 93 | 119 // and returns |length| bytes in the form of a pointer to a const png_byte*. |
| 94 bool PNGImageReader::decode(const SegmentReader& data, bool sizeOnly) { | 120 // This function is used to make it easy to access data from the reader in a |
| 95 m_decodingSizeOnly = sizeOnly; | 121 // png friendly way, and pass it to libpng for decoding. |
| 96 | 122 // |
| 97 // We need to do the setjmp here. Otherwise bad things will happen. | 123 // Pre-conditions before using this: |
| 124 // - |reader|.size() >= |readOffset| + |length| | |
| 125 // - |buffer|.size() >= |length| | |
| 126 // - |length| <= |kBufferSize| | |
| 127 // | |
| 128 // The reason for the last two precondition is that currently the png signature | |
| 129 // plus IHDR chunk (8B + 25B = 33B) is the largest chunk that is read using this | |
| 130 // method. If the data is not consecutive, it is stored in |buffer|, which must | |
| 131 // have the size of (at least) |length|, but there's no need for it to be larger | |
| 132 // than |kBufferSize|. | |
| 133 static constexpr size_t kBufferSize = 33; | |
| 134 const png_byte* readAsConstPngBytep(const FastSharedBufferReader& reader, | |
| 135 size_t readOffset, | |
| 136 size_t length, | |
| 137 char* buffer) { | |
| 138 DCHECK(length <= kBufferSize); | |
| 139 return reinterpret_cast<const png_byte*>( | |
| 140 reader.getConsecutiveData(readOffset, length, buffer)); | |
| 141 } | |
| 142 | |
| 143 // This is used as a value for the byteLength of a frameInfo struct to | |
| 144 // indicate that it is the first frame, and we still need to set byteLength | |
| 145 // to the correct value as soon as the parser knows it. 1 is a safe value | |
| 146 // since the byteLength field of a frame is at least 12, in the case of an | |
| 147 // empty fdAT or IDAT chunk. | |
| 148 static constexpr size_t kFirstFrameIndicator = 1; | |
| 149 | |
| 150 void PNGImageReader::decode(SegmentReader& data, size_t index) { | |
| 151 if (index >= m_frameInfo.size()) | |
| 152 return; | |
| 153 | |
| 154 // For non animated PNGs, resume decoding where we left off in parse(), at | |
| 155 // the beginning of the IDAT chunk. Recreating a png struct would either | |
| 156 // result in wasted work, by reprocessing all header bytes, or decoding the | |
| 157 // wrong data. | |
| 158 if (!m_isAnimated) { | |
| 159 if (setjmp(JMPBUF(m_png))) { | |
| 160 m_decoder->setFailed(); | |
| 161 return; | |
| 162 } | |
| 163 m_progressiveDecodeOffset += processData( | |
| 164 data, m_frameInfo[0].startOffset + m_progressiveDecodeOffset, 0); | |
| 165 return; | |
| 166 } | |
| 167 | |
| 168 // When a non-first frame is decoded, and the previous decode call was a | |
| 169 // progressive decode of frame 0 which did not completely finish, set | |
| 170 // |m_progressiveDecodeOffset| to 0. This ensures that when a later call to | |
| 171 // decode frame 0 comes in, it will correctly decode the frame from the | |
| 172 // beginning. It is better to re-decode from the start than to try continuing | |
| 173 // where we left off, because: | |
| 174 // - A row may have been partially decoded, but it is hard to find where | |
| 175 // that row starts in the data. But we need to continue decoding from the | |
| 176 // beginning of the row, otherwise the pixels will be shifted and the final | |
| 177 // row won't be complete. | |
| 178 // - The |m_png| struct will be reset for this decode call, so it needs to | |
| 179 // be recreated when decoding for frame 0 continues. Since the header chunks | |
| 180 // need to be re-processed anyway, the added benefit of continuing | |
| 181 // progressive decoding may be very slim. Especially since this is already | |
| 182 // an edge case. | |
| 183 if (index > 0 && m_progressiveDecodeOffset > 0) | |
| 184 m_progressiveDecodeOffset = 0; | |
| 185 | |
| 186 // Progressive decoding is only done if both of the following are true: | |
| 187 // - It is the first frame, thus |index| == 0, AND | |
| 188 // - The byteLength of the first frame is not yet known, *or* it is known | |
| 189 // but we're only partway in a progressive decode, started earlier. | |
| 190 bool firstFrameLengthKnown = firstFrameFullyReceived(); | |
| 191 bool progressiveDecodingAlreadyStarted = m_progressiveDecodeOffset > 0; | |
| 192 bool progressiveDecode = (index == 0 && (!firstFrameLengthKnown || | |
| 193 progressiveDecodingAlreadyStarted)); | |
| 194 bool decodeAsNewPNG = | |
| 195 !progressiveDecode || !progressiveDecodingAlreadyStarted; | |
| 196 | |
| 197 // Initialize a new png struct for this frame. For a progressive decode of | |
| 198 // the first frame, we only need to do this once. | |
| 199 // @FIXME(joostouwerling) check if the existing png struct can be reused. | |
| 200 if (decodeAsNewPNG) | |
| 201 resetPNGStructPreDecode(); | |
| 202 | |
| 203 // Before processing any PNG bytes, set setjmp with the current |m_png| | |
| 204 // struct. This has to be done after resetPNGStructPreDecode(), which will | |
| 205 // have replaced |m_png|. | |
| 206 if (setjmp(JMPBUF(m_png))) { | |
| 207 m_decoder->setFailed(); | |
| 208 return; | |
| 209 } | |
| 210 | |
| 211 // Process the png header chunks with a modified size, reflecting the size of | |
| 212 // this frame. This only needs to be done once for a progressive decode of | |
| 213 // the first frame. | |
| 214 if (decodeAsNewPNG) | |
| 215 startFrameDecoding(data, index); | |
| 216 | |
| 217 bool decodedFrameCompletely; | |
| 218 if (progressiveDecode) { | |
| 219 decodedFrameCompletely = progressivelyDecodeFirstFrame(data); | |
| 220 // If progressive decoding processed all data for this frame, reset | |
| 221 // |m_progressiveDecodeOffset|, so |progressiveDecodingAlreadyStarted| | |
| 222 // will be false for later calls to decode frame 0. | |
| 223 if (decodedFrameCompletely) | |
| 224 m_progressiveDecodeOffset = 0; | |
| 225 } else { | |
| 226 decodeFrame(data, index); | |
| 227 // For a non-progressive decode, we already have all the data we are | |
| 228 // going to get, so consider the frame complete. | |
| 229 decodedFrameCompletely = true; | |
| 230 } | |
| 231 | |
| 232 // Send the IEND chunk if the frame is completely decoded, so the complete | |
| 233 // callback in |m_decoder| will be called. | |
| 234 if (decodedFrameCompletely) | |
| 235 endFrameDecoding(); | |
| 236 } | |
| 237 | |
| 238 void PNGImageReader::resetPNGStructPreDecode() { | |
| 239 // Each frame is processed as if it were a complete, single frame png image. | |
| 240 // To accomplish this, destroy the current |m_png| and |m_info| structs and | |
| 241 // create new ones. CRC errors are ignored, so fdAT chunks can be processed | |
| 242 // as IDATs without recalculating the CRC value. | |
| 243 png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0); | |
| 244 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0); | |
| 245 m_info = png_create_info_struct(m_png); | |
| 246 png_set_crc_action(m_png, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE); | |
| 247 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, | |
| 248 pngRowAvailable, pngComplete); | |
| 249 } | |
| 250 | |
| 251 void PNGImageReader::startFrameDecoding(SegmentReader& data, size_t index) { | |
| 252 // If the frame is the size of the whole image, we don't need to modify any | |
| 253 // data in the IHDR chunk. This means it suffices to re-process all header | |
| 254 // data up to the first frame, for mimicking a png image. | |
| 255 const IntRect& frameRect = m_frameInfo[index].frameRect; | |
| 256 if (frameRect.location() == IntPoint() && | |
| 257 frameRect.size() == m_decoder->size()) { | |
| 258 processData(data, m_initialOffset, m_idatOffset); | |
| 259 return; | |
| 260 } | |
| 261 | |
| 262 // Process the IHDR chunk, but change the width and height so it reflects | |
| 263 // the frame's width and height. Image Decoder will apply the x,y offset. | |
| 264 // This step is omitted if the width and height are equal to the image size, | |
| 265 // which is done in the block above. | |
| 266 FastSharedBufferReader reader(&data); | |
| 267 char readBuffer[kBufferSize]; | |
| 268 | |
| 269 // |headerSize| is equal to |kBufferSize|, but adds more semantic insight. | |
| 270 constexpr size_t headerSize = 33; | |
| 271 png_byte header[headerSize]; | |
| 272 const png_byte* chunk = | |
| 273 readAsConstPngBytep(reader, m_initialOffset, headerSize, readBuffer); | |
| 274 memcpy(header, chunk, headerSize); | |
| 275 | |
| 276 // Write the unclipped width and height. Clipping happens in the decoder. | |
| 277 png_save_uint_32(header + 16, frameRect.width()); | |
| 278 png_save_uint_32(header + 20, frameRect.height()); | |
| 279 png_process_data(m_png, m_info, header, headerSize); | |
| 280 | |
| 281 // Process the rest of the header chunks. Start after the PNG signature and | |
| 282 // IHDR chunk, 33B, and process up to the first data chunk. The number of | |
| 283 // bytes up to the first data chunk is stored in |m_idatOffset|. | |
| 284 processData(data, m_initialOffset + headerSize, m_idatOffset - headerSize); | |
| 285 } | |
| 286 | |
| 287 // Determine if the bytes 4 to 7 of |chunk| indicate that it is a |tag| chunk. | |
| 288 // - The length of |chunk| must be >= 8 | |
| 289 // - The length of |tag| must be = 4 | |
| 290 static inline bool isChunk(const png_byte* chunk, const char* tag) { | |
| 291 return memcmp(chunk + 4, tag, 4) == 0; | |
| 292 } | |
| 293 | |
| 294 bool PNGImageReader::progressivelyDecodeFirstFrame(SegmentReader& data) { | |
| 295 FastSharedBufferReader reader(&data); | |
| 296 char readBuffer[8]; // large enough to identify a chunk. | |
| 297 size_t offset = m_frameInfo[0].startOffset; | |
| 298 | |
| 299 // Loop while there is enough data to do progressive decoding. | |
| 300 while (data.size() >= offset + 8) { | |
| 301 // At the beginning of each loop, the offset is at the start of a chunk. | |
| 302 const png_byte* chunk = readAsConstPngBytep(reader, offset, 8, readBuffer); | |
| 303 const png_uint_32 length = png_get_uint_32(chunk); | |
| 304 | |
| 305 // When an fcTL or IEND chunk is encountered, the frame data has ended. | |
| 306 // Return true, since all frame data is decoded. | |
| 307 if (isChunk(chunk, "fcTL") || isChunk(chunk, "IEND")) | |
| 308 return true; | |
| 309 | |
| 310 // If this chunk was already decoded, move on to the next. | |
| 311 if (m_progressiveDecodeOffset >= offset + length + 12) { | |
| 312 offset += length + 12; | |
| 313 continue; | |
| 314 } | |
| 315 | |
| 316 // At this point, three scenarios are possible: | |
| 317 // 1) Some bytes of this chunk were already decoded in a previous call, | |
| 318 // so we need to continue from there. | |
| 319 // 2) This is an fdAT chunk, so we need to convert it to an IDAT chunk | |
| 320 // before we can decode it. | |
| 321 // 3) This is any other chunk, most likely an IDAT chunk. | |
| 322 // | |
| 323 // In each scenario, we want to decode as much data as possible. In each | |
| 324 // one, do the scenario specific work and set |offset| to where decoding | |
| 325 // needs to continue. From there, decode until the end of the chunk, if | |
| 326 // possible. If the whole chunk is decoded, continue to the next loop. | |
| 327 // Otherwise, store how far we've come in |m_progressiveDecodeOffset| and | |
| 328 // return false to indicate to the caller that the frame is partially | |
| 329 // decoded. | |
| 330 | |
| 331 size_t endOffsetChunk = offset + length + 12; | |
| 332 | |
| 333 // Scenario 1: |m_progressiveDecodeOffset| is ahead of the chunk tag. | |
| 334 if (m_progressiveDecodeOffset >= offset + 8) { | |
| 335 offset = m_progressiveDecodeOffset; | |
| 336 | |
| 337 // Scenario 2: we need to convert the fdAT to an IDAT chunk. For an | |
| 338 // explanation of the numbers, see the comments in decodeFrame(). | |
| 339 } else if (isChunk(chunk, "fdAT")) { | |
| 340 png_byte chunkIDAT[] = {0, 0, 0, 0, 'I', 'D', 'A', 'T'}; | |
| 341 png_save_uint_32(chunkIDAT, length - 4); | |
| 342 png_process_data(m_png, m_info, chunkIDAT, 8); | |
| 343 // Skip the sequence number | |
| 344 offset += 12; | |
| 345 | |
| 346 // Scenario 3: for any other chunk type, process the first 8 bytes. | |
| 347 } else { | |
| 348 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
| 349 offset += 8; | |
| 350 } | |
| 351 | |
| 352 size_t bytesLeftInChunk = endOffsetChunk - offset; | |
| 353 size_t bytesDecoded = processData(data, offset, bytesLeftInChunk); | |
| 354 m_progressiveDecodeOffset = offset + bytesDecoded; | |
| 355 if (bytesDecoded < bytesLeftInChunk) | |
| 356 return false; | |
| 357 offset += bytesDecoded; | |
| 358 } | |
| 359 | |
| 360 return false; | |
| 361 } | |
| 362 | |
| 363 void PNGImageReader::decodeFrame(SegmentReader& data, size_t index) { | |
| 364 // From the frame info that was gathered during parsing, it is known at | |
| 365 // what offset the frame data starts and how many bytes are in the stream | |
| 366 // before the frame ends. Using this, we process all chunks that fall in | |
| 367 // this interval. We catch every fdAT chunk and transform it to an IDAT | |
| 368 // chunk, so libpng will decode it like a non-animated PNG image. | |
| 369 size_t offset = m_frameInfo[index].startOffset; | |
| 370 size_t endOffset = offset + m_frameInfo[index].byteLength; | |
| 371 char readBuffer[8]; | |
| 372 FastSharedBufferReader reader(&data); | |
| 373 | |
| 374 while (offset < endOffset) { | |
| 375 const png_byte* chunk = readAsConstPngBytep(reader, offset, 8, readBuffer); | |
| 376 const png_uint_32 length = png_get_uint_32(chunk); | |
| 377 if (isChunk(chunk, "fdAT")) { | |
| 378 // An fdAT chunk is build up as follows: | |
| 379 // - |length| (4B) | |
| 380 // - fdAT tag (4B) | |
| 381 // - sequence number (4B) | |
| 382 // - frame data (|length| - 4B) | |
| 383 // - CRC (4B) | |
| 384 // Thus, to reformat this into an IDAT chunk, we need to: | |
| 385 // - write |length| - 4 as the new length, since the sequence number | |
| 386 // must be removed. | |
| 387 // - change the tag to IDAT. | |
| 388 // - omit the sequence number from the data part of the chunk. | |
| 389 png_byte chunkIDAT[] = {0, 0, 0, 0, 'I', 'D', 'A', 'T'}; | |
| 390 png_save_uint_32(chunkIDAT, length - 4); | |
| 391 png_process_data(m_png, m_info, chunkIDAT, 8); | |
| 392 // The frame data and the CRC span |length| bytes, so skip the | |
| 393 // sequence number and process |length| bytes to decode the frame. | |
| 394 processData(data, offset + 12, length); | |
| 395 } else { | |
| 396 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
| 397 processData(data, offset + 8, length + 4); | |
| 398 } | |
| 399 offset += 12 + length; | |
| 400 } | |
| 401 } | |
| 402 | |
| 403 void PNGImageReader::endFrameDecoding() { | |
| 404 png_byte IEND[12] = {0, 0, 0, 0, 'I', 'E', 'N', 'D', 174, 66, 96, 130}; | |
| 405 png_process_data(m_png, m_info, IEND, 12); | |
| 406 } | |
| 407 | |
| 408 bool PNGImageReader::parse(SegmentReader& data, | |
| 409 PNGImageDecoder::PNGParseQuery query) { | |
| 410 if (m_parseCompleted) | |
| 411 return true; | |
| 412 | |
| 98 if (setjmp(JMPBUF(m_png))) | 413 if (setjmp(JMPBUF(m_png))) |
| 99 return m_decoder->setFailed(); | 414 return m_decoder->setFailed(); |
| 100 | 415 |
| 416 // If the size has not been parsed, do that first, since it's necessary | |
| 417 // for both the Size and MetaData query. If parseSize returns false, | |
| 418 // it failed because of a lack of data so we can return false at this point. | |
| 419 if (!m_decoder->isDecodedSizeAvailable() && !parseSize(data)) | |
| 420 return false; | |
| 421 | |
| 422 if (query == PNGImageDecoder::PNGParseQuery::PNGSizeQuery) | |
| 423 return m_decoder->isDecodedSizeAvailable(); | |
| 424 | |
| 425 // For non animated images (identified by no acTL chunk before the IDAT), | |
| 426 // we create one frame. This saves some processing time since we don't need | |
| 427 // to go over the stream to find chunks. | |
| 428 if (!m_isAnimated) { | |
| 429 if (m_frameInfo.isEmpty()) { | |
| 430 FrameInfo frame; | |
| 431 // This needs to be plus 8 since the first 8 bytes of the IDAT chunk | |
| 432 // are already processed in parseSize(). | |
| 433 frame.startOffset = m_readOffset + 8; | |
| 434 frame.frameRect = IntRect(IntPoint(), m_decoder->size()); | |
| 435 frame.duration = 0; | |
| 436 frame.alphaBlend = ImageFrame::AlphaBlendSource::BlendAtopBgcolor; | |
| 437 frame.disposalMethod = ImageFrame::DisposalMethod::DisposeNotSpecified; | |
| 438 m_frameInfo.append(frame); | |
| 439 // When the png is not animated, no extra parsing is necessary. | |
| 440 m_parseCompleted = true; | |
| 441 } | |
| 442 return true; | |
| 443 } | |
| 444 | |
| 445 FastSharedBufferReader reader(&data); | |
| 446 char readBuffer[kBufferSize]; | |
| 447 | |
| 448 // At this point, the query is FrameMetaDataQuery. Loop over the data and | |
| 449 // register all frames we can find. A frame is registered on the next fcTL | |
| 450 // chunk or when the IEND chunk is found. This ensures that only complete | |
| 451 // frames are reported, unless there is an error in the stream. | |
| 452 while (reader.size() >= m_readOffset + 8) { | |
| 453 const png_byte* chunk = | |
| 454 readAsConstPngBytep(reader, m_readOffset, 8, readBuffer); | |
| 455 const size_t length = png_get_uint_32(chunk); | |
| 456 | |
| 457 // When we find an IDAT chunk (when the IDAT is part of the animation), | |
| 458 // or an fdAT chunk, and the readOffset field of the newFrame is 0, | |
| 459 // we have found the beginning of a new block of frame data. | |
| 460 const bool isFrameData = | |
| 461 isChunk(chunk, "fdAT") || | |
| 462 (isChunk(chunk, "IDAT") && m_idatIsPartOfAnimation); | |
| 463 if (m_newFrame.startOffset == 0 && isFrameData) { | |
| 464 m_newFrame.startOffset = m_readOffset; | |
| 465 | |
| 466 // When the |frameInfo| vector is empty, the first frame needs to be | |
| 467 // reported as soon as possible, even before all frame data is in | |
| 468 // |data|, so the first frame can be decoded progressively. | |
| 469 if (m_frameInfo.isEmpty()) { | |
| 470 m_newFrame.byteLength = kFirstFrameIndicator; | |
| 471 m_frameInfo.append(m_newFrame); | |
| 472 } | |
| 473 | |
| 474 // An fcTL or IEND marks the end of the previous frame. Thus, the | |
| 475 // FrameInfo data in m_newFrame is submitted to the m_frameInfo vector. | |
| 476 // | |
| 477 // Furthermore, an fcTL chunk indicates a new frame is coming, | |
| 478 // so the m_newFrame variable is prepared accordingly by setting the | |
| 479 // readOffset field to 0, which indicates that the frame control info | |
| 480 // is available but that we haven't seen any frame data yet. | |
| 481 } else if (isChunk(chunk, "fcTL") || isChunk(chunk, "IEND")) { | |
| 482 if (m_newFrame.startOffset != 0) { | |
| 483 m_newFrame.byteLength = m_readOffset - m_newFrame.startOffset; | |
| 484 if (m_frameInfo[0].byteLength == kFirstFrameIndicator) | |
| 485 m_frameInfo[0].byteLength = m_newFrame.byteLength; | |
| 486 else | |
| 487 m_frameInfo.append(m_newFrame); | |
| 488 | |
| 489 m_newFrame.startOffset = 0; | |
| 490 } | |
| 491 | |
| 492 if (reader.size() < m_readOffset + 12 + length) | |
| 493 return false; | |
| 494 | |
| 495 if (isChunk(chunk, "IEND")) { | |
| 496 // The PNG image ends at the IEND chunk, so all parsing is completed. | |
| 497 m_parseCompleted = true; | |
| 498 return true; | |
| 499 } | |
| 500 | |
| 501 // At this point, we're dealing with an fcTL chunk, since the above | |
| 502 // statement already returns on IEND chunks. | |
| 503 | |
| 504 // If the fcTL chunk is not 26 bytes long, we can't process it. | |
| 505 if (length != 26) | |
| 506 return m_decoder->setFailed(); | |
| 507 | |
| 508 chunk = readAsConstPngBytep(reader, m_readOffset + 8, length, readBuffer); | |
| 509 parseFrameInfo(chunk); | |
| 510 } | |
| 511 m_readOffset += 12 + length; | |
| 512 } | |
| 513 return false; | |
| 514 } | |
| 515 | |
| 516 // If |length| == 0, read until the stream ends. | |
| 517 // @return: number of bytes processed. | |
| 518 size_t PNGImageReader::processData(SegmentReader& data, | |
| 519 size_t offset, | |
| 520 size_t length) { | |
| 101 const char* segment; | 521 const char* segment; |
| 102 while (size_t segmentLength = data.getSomeData(segment, m_readOffset)) { | 522 size_t totalProcessedBytes = 0; |
| 103 m_readOffset += segmentLength; | 523 while (size_t segmentLength = data.getSomeData(segment, offset)) { |
| 104 m_currentBufferSize = m_readOffset; | 524 if (length > 0 && segmentLength + totalProcessedBytes > length) |
| 525 segmentLength = length - totalProcessedBytes; | |
| 526 | |
| 105 png_process_data(m_png, m_info, | 527 png_process_data(m_png, m_info, |
| 106 reinterpret_cast<png_bytep>(const_cast<char*>(segment)), | 528 reinterpret_cast<png_byte*>(const_cast<char*>(segment)), |
| 107 segmentLength); | 529 segmentLength); |
| 108 if (sizeOnly ? m_decoder->isDecodedSizeAvailable() | 530 offset += segmentLength; |
| 109 : m_decoder->frameIsCompleteAtIndex(0)) | 531 totalProcessedBytes += segmentLength; |
| 532 if (totalProcessedBytes == length) | |
| 533 return length; | |
| 534 } | |
| 535 return totalProcessedBytes; | |
| 536 } | |
| 537 | |
| 538 // This methods reads through the stream until it has parsed the image size. | |
| 539 // @return true when it succeeds in parsing the size. | |
| 540 // false when: | |
| 541 // A) not enough data is provided | |
| 542 // B) decoding by libpng fails. In the this case, it will also call | |
| 543 // setFailed on m_decoder. | |
| 544 bool PNGImageReader::parseSize(SegmentReader& data) { | |
| 545 FastSharedBufferReader reader(&data); | |
| 546 char readBuffer[kBufferSize]; | |
| 547 | |
| 548 // Process the PNG signature and the IHDR with libpng, such that this code | |
| 549 // does not need to be bothered with parsing the contents. This also enables | |
|
scroggo_chromium
2016/12/07 13:44:44
I don't think we should speculate about whether th
joostouwerling
2016/12/07 16:49:10
Done.
| |
| 550 // the reader to use the existing headerAvailable callback in the decoder. | |
| 551 // | |
| 552 // When we already have decoded the signature, we don't need to do it again. | |
| 553 // By setting a flag for this we allow for byte by byte parsing. | |
| 554 if (!m_parsedSignature) { | |
| 555 if (reader.size() < m_readOffset + 8) | |
| 556 return false; | |
| 557 | |
| 558 const png_byte* chunk = | |
| 559 readAsConstPngBytep(reader, m_readOffset, 8, readBuffer); | |
| 560 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
| 561 m_readOffset += 8; | |
| 562 m_parsedSignature = true; | |
| 563 // Initialize the newFrame by setting the readOffset to 0. | |
| 564 m_newFrame.startOffset = 0; | |
| 565 } | |
| 566 | |
| 567 // This loop peeks at the chunk tag until the IDAT chunk is found. When | |
| 568 // a different tag is encountered, pass it on to libpng for general parsing. | |
| 569 // We can peek at chunks by looking at the first 8 bytes, which contain the | |
| 570 // length and the chunk tag. | |
| 571 // | |
| 572 // When an fcTL (frame control) is encountered before the IDAT, the frame | |
| 573 // data in the IDAT chunk is part of the animation. This case is flagged | |
| 574 // and the frame info is stored by parsing the fcTL chunk. | |
| 575 while (reader.size() >= m_readOffset + 8) { | |
| 576 const png_byte* chunk = | |
| 577 readAsConstPngBytep(reader, m_readOffset, 8, readBuffer); | |
| 578 const png_uint_32 length = png_get_uint_32(chunk); | |
| 579 | |
| 580 // If we encounter the IDAT chunk, we're done with the png header | |
| 581 // chunks. Indicate this to libpng by sending the beginning of the IDAT | |
| 582 // chunk, which will trigger libpng to call the headerAvailable | |
| 583 // callback on m_decoder. This provides the size to the decoder. | |
| 584 if (isChunk(chunk, "IDAT")) { | |
| 585 m_idatOffset = m_readOffset; | |
| 586 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
| 110 return true; | 587 return true; |
| 111 } | 588 } |
| 112 | 589 |
| 590 // Consider the PNG image animated if an acTL chunk of the correct | |
| 591 // length is present. Parsing the acTL content is done by | |
| 592 // parseAnimationControl, called by libpng's png_process_data. | |
| 593 if (isChunk(chunk, "acTL") && length == 8) | |
| 594 m_isAnimated = true; | |
| 595 | |
| 596 // We don't need to check for |length| here, because the decoder will | |
| 597 // fail later on for invalid fcTL chunks. | |
| 598 if (isChunk(chunk, "fcTL")) | |
|
scroggo_chromium
2016/12/07 13:44:44
I think it cannot be both acTL and fcTL. Make this
joostouwerling
2016/12/07 16:49:10
Done.
| |
| 599 m_idatIsPartOfAnimation = true; | |
| 600 | |
| 601 // 12 is the length, tag and crc part of the chunk, which are all 4B. | |
| 602 if (reader.size() < m_readOffset + length + 12) | |
| 603 break; | |
| 604 | |
| 605 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); | |
| 606 processData(data, m_readOffset + 8, length + 4); | |
| 607 m_readOffset += length + 12; | |
| 608 } | |
| 609 | |
| 610 // If we end up here, not enough data was available for the IDAT chunk | |
| 611 // So libpng would not have called headerAvailable yet. | |
| 113 return false; | 612 return false; |
| 114 } | 613 } |
| 115 | 614 |
| 116 } // namespace blink | 615 void PNGImageReader::parseAnimationChunk(const char tag[], |
| 616 const void* dataChunk, | |
| 617 size_t length) { | |
| 618 const png_byte* data = static_cast<const png_byte*>(dataChunk); | |
| 619 | |
| 620 // The number of frames as indicated in the animation control chunk (acTL) | |
| 621 // is ignored, and the number of frames that are actually present is used. | |
| 622 // For now, when the number of indicated frames is different from the | |
| 623 // number of supplied frames, the number of supplied frames is what is | |
| 624 // provided to the decoder. Therefore, it does not add any benefit of | |
| 625 // looking at the value of the indicated framecount. A note here is that | |
| 626 // there may be optimisations available, for example, prescaling vectors. | |
| 627 if (strcmp(tag, "acTL") == 0 && length == 8) { | |
| 628 png_uint_32 repetitionCount = png_get_uint_32(data + 4); | |
| 629 m_decoder->setRepetitionCount(repetitionCount); | |
| 630 | |
| 631 // For fcTL, decoding fails if it does not have the correct length. It is | |
| 632 // impossible to make a guess about the frame if not all data is available. | |
| 633 // Use longjmp to get back to parse(), which is necessary since this method | |
| 634 // is called by a libpng callback. | |
| 635 } else if (strcmp(tag, "fcTL") == 0) { | |
| 636 if (length != 26) | |
| 637 longjmp(JMPBUF(m_png), 1); | |
| 638 parseFrameInfo(data); | |
| 639 } | |
| 640 } | |
| 641 | |
| 642 bool PNGImageReader::firstFrameFullyReceived() const { | |
| 643 DCHECK_GT(m_frameInfo.size(), 0u); | |
| 644 return m_frameInfo[0].byteLength != kFirstFrameIndicator; | |
| 645 } | |
| 646 | |
| 647 void PNGImageReader::clearDecodeState(size_t frameIndex) { | |
| 648 if (frameIndex == 0) | |
| 649 m_progressiveDecodeOffset = 0; | |
| 650 } | |
| 651 | |
| 652 size_t PNGImageReader::frameCount() const { | |
| 653 return m_frameInfo.size(); | |
| 654 } | |
| 655 | |
| 656 const PNGImageReader::FrameInfo& PNGImageReader::frameInfo(size_t index) const { | |
| 657 DCHECK(index < m_frameInfo.size()); | |
| 658 return m_frameInfo[index]; | |
| 659 } | |
| 660 | |
| 661 // Extract the frame control info and store it in m_newFrame. The length check | |
| 662 // on the data chunk has been done in parseAnimationChunk. | |
| 663 // The fcTL specification used can be found at: | |
| 664 // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu nk | |
| 665 void PNGImageReader::parseFrameInfo(const png_byte* data) { | |
| 666 png_uint_32 width, height, xOffset, yOffset; | |
| 667 png_uint_16 delayNumerator, delayDenominator; | |
| 668 width = png_get_uint_32(data + 4); | |
| 669 height = png_get_uint_32(data + 8); | |
| 670 xOffset = png_get_uint_32(data + 12); | |
| 671 yOffset = png_get_uint_32(data + 16); | |
| 672 delayNumerator = png_get_uint_16(data + 20); | |
| 673 delayDenominator = png_get_uint_16(data + 22); | |
| 674 | |
| 675 m_newFrame.duration = (delayDenominator == 0) | |
| 676 ? delayNumerator * 10 | |
| 677 : delayNumerator * 1000 / delayDenominator; | |
| 678 m_newFrame.frameRect = IntRect(xOffset, yOffset, width, height); | |
| 679 m_newFrame.disposalMethod = data[24]; | |
| 680 m_newFrame.alphaBlend = data[25]; | |
| 681 } | |
| 682 | |
| 683 }; // namespace blink | |
| OLD | NEW |