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() >= |length| |
| 141 // - |length| <= |kBufferSize| |
| 142 // |
| 143 // The reason for the last two precondition is that currently the png signature |
| 144 // plus IHDR chunk (8B + 25B = 33B) is the largest chunk that is read using this |
| 145 // method. If the data is not consecutive, it is stored in |buffer|, which must |
| 146 // have the size of (at least) |length|, but there's no need for it to be larger |
| 147 // than |kBufferSize|. |
| 148 static constexpr size_t kBufferSize = 33; |
| 149 const png_byte* readAsConstPngBytep(const FastSharedBufferReader& reader, |
| 150 size_t readOffset, size_t length, |
| 151 char* buffer) |
| 152 { |
| 153 ASSERT(length <= kBufferSize); |
| 154 return reinterpret_cast<const png_byte*>( |
| 155 reader.getConsecutiveData(readOffset, length, buffer)); |
| 156 } |
| 157 |
| 158 void PNGImageReader::decode(SegmentReader& data, size_t index) |
| 159 { |
| 160 if (index >= m_frameInfo.size()) |
| 161 return; |
| 162 |
| 163 // When decoding by libpng fails in either the non-animated branch or the |
| 164 // animated branch, the decoder needs to be set to the failed state. |
| 165 if (setjmp(JMPBUF(m_png))) { |
| 166 m_decoder->setFailed(); |
| 167 return; |
| 168 } |
| 169 |
| 170 // For non animated PNG's, we don't want to waste CPU time with recreating |
| 171 // the png struct. It suffices to continue parsing where we left off. Since |
| 172 // non animated only need to decode the frame once, we can use the |
| 173 // readOffset field to enable progressive decoding. |
| 174 if (!m_isAnimated) { |
| 175 m_frameInfo[0].readOffset += processData(data, m_frameInfo[0].readOffset
, 0); |
| 176 return; |
| 177 } |
| 178 |
| 179 // Initialize a new png struct for this frame. |
| 180 startFrameDecoding(data, index); |
| 181 |
| 182 // From the frame info that was gathered during parsing, it is known at |
| 183 // what offset the frame data starts and how many bytes are in the stream |
| 184 // before the frame ends. Using this, we process all chunks that fall in |
| 185 // this interval. We catch every fdAT chunk and transform it to an IDAT |
| 186 // chunk, so libpng will decode it like a non-animated PNG image. |
| 187 size_t offset = m_frameInfo[index].readOffset; |
| 188 size_t endOffset = offset + m_frameInfo[index].byteLength; |
| 189 char readBuffer[8]; |
| 190 FastSharedBufferReader reader(&data); |
| 191 |
| 192 while (offset < endOffset) { |
| 193 const png_byte* chunk = readAsConstPngBytep(reader, offset, 8, readBuffe
r); |
| 194 const png_uint_32 length = png_get_uint_32(chunk); |
| 195 |
| 196 if (memcmp(chunk + 4, "fdAT", 4) == 0) { |
| 197 // An fdAT chunk is build up as follows: |
| 198 // - |length| (4B) |
| 199 // - fdAT tag (4B) |
| 200 // - sequence number (4B) |
| 201 // - frame data (|length| - 4B) |
| 202 // - CRC (4B) |
| 203 // Thus, to reformat this into an IDAT chunk, we need to: |
| 204 // - write |length| - 4 as the new length, since the sequence number |
| 205 // must be removed. |
| 206 // - change the tag to IDAT. |
| 207 // - omit the sequence number from the data part of the chunk. |
| 208 png_byte chunkIDAT[] = {0, 0, 0, 0, 'I', 'D', 'A', 'T'}; |
| 209 png_save_uint_32(chunkIDAT, length - 4); |
| 210 png_process_data(m_png, m_info, chunkIDAT, 8); |
| 211 // The frame data and the CRC span |length| bytes, so skip the |
| 212 // sequence number and process |length| bytes to decode the frame. |
| 213 processData(data, offset + 12, length); |
| 214 } else { |
| 215 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); |
| 216 processData(data, offset + 8, length + 4); |
| 217 } |
| 218 |
| 219 offset += 12 + length; |
| 220 } |
| 221 |
| 222 // Finish decoding by sending the IEND chunk. |
| 223 endFrameDecoding(); |
| 224 |
| 225 } |
| 226 |
| 227 void PNGImageReader::startFrameDecoding(SegmentReader& data, size_t index) |
| 228 { |
| 229 // Each frame is processed as if it were a complete, single frame png image. |
| 230 // To accomplish this, destroy the current |m_png| and |m_info| structs and |
| 231 // create new ones. CRC errors are ignored, so fdAT chunks can be processed |
| 232 // as IDATs without recalculating the CRC value. |
| 233 png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0); |
| 234 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0); |
| 235 m_info = png_create_info_struct(m_png); |
| 236 png_set_crc_action(m_png, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE); |
| 237 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, |
| 238 pngRowAvailable, pngComplete); |
| 239 |
| 240 // If the frame is the size of the whole image, we don't need to modify any |
| 241 // data in the IHDR chunk. This means it suffices to re-process all header |
| 242 // data up to the first frame, for mimicking a png image. |
| 243 const IntRect& frameRect = m_frameInfo[index].frameRect; |
| 244 if (frameRect.location() == IntPoint() |
| 245 && frameRect.size() == m_decoder->size()) { |
| 246 processData(data, m_initialOffset, m_bytesInfo); |
| 247 return; |
| 248 } |
| 249 |
| 250 // Process the IHDR chunk, but change the width and height so it reflects |
| 251 // the frame's width and height. Image Decoder will apply the x,y offset. |
| 252 // This step is omitted if the width and height are equal to the image size, |
| 253 // which is done in the block above. |
| 254 FastSharedBufferReader reader(&data); |
| 255 char readBuffer[kBufferSize]; |
| 256 |
| 257 // |headerSize| is equal to |kBufferSize|, but adds more semantic insight. |
| 258 constexpr size_t headerSize = 33; |
| 259 png_byte header[headerSize]; |
| 260 const png_byte* chunk = readAsConstPngBytep(reader, m_initialOffset, |
| 261 headerSize, readBuffer); |
| 262 memcpy(header, chunk, headerSize); |
| 263 |
| 264 // Write the unclipped width and height. Clipping happens in the decoder. |
| 265 png_save_uint_32(header + 16, frameRect.width()); |
| 266 png_save_uint_32(header + 20, frameRect.height()); |
| 267 png_process_data(m_png, m_info, header, headerSize); |
| 268 |
| 269 // Process the rest of the header chunks. Start after the PNG signature and |
| 270 // IHDR chunk, 33B, and process up to the first data chunk. The number of |
| 271 // bytes up to the first data chunk is stored in |m_bytesInfo|. |
| 272 processData(data, m_initialOffset + headerSize, m_bytesInfo - headerSize); |
| 273 } |
| 274 |
| 275 void PNGImageReader::endFrameDecoding() |
| 276 { |
| 277 png_byte IEND[12] = {0, 0, 0, 0, 'I', 'E', 'N', 'D', 174, 66, 96, 130}; |
| 278 png_process_data(m_png, m_info, IEND, 12); |
| 279 } |
| 280 |
| 281 bool PNGImageReader::parse(SegmentReader& data, |
| 282 PNGImageDecoder::PNGParseQuery query) |
| 283 { |
| 284 if (setjmp(JMPBUF(m_png))) |
| 285 return m_decoder->setFailed(); |
| 286 |
| 287 // If the size has not been parsed, do that first, since it's necessary |
| 288 // for both the Size and MetaData query. If parseSize returns false, |
| 289 // it failed because of a lack of data so we can return false at this point. |
| 290 if (!m_decoder->isDecodedSizeAvailable() && !parseSize(data)) |
| 291 return false; |
| 292 |
| 293 if (query == PNGImageDecoder::PNGParseQuery::PNGSizeQuery) |
| 294 return m_decoder->isDecodedSizeAvailable(); |
| 295 |
| 296 // For non animated images (identified by no acTL chunk before the IDAT), |
| 297 // we create one frame. This saves some processing time since we don't need |
| 298 // to go over the stream to find chunks. |
| 299 if (!m_isAnimated) { |
| 300 if (m_frameInfo.size() == 0) { |
| 301 FrameInfo frame; |
| 302 frame.readOffset = m_readOffset + 8; |
| 303 frame.frameRect = IntRect(IntPoint(), m_decoder->size()); |
| 304 frame.duration = 0; |
| 305 frame.alphaBlend = ImageFrame::AlphaBlendSource::BlendAtopBgcolor; |
| 306 frame.disposalMethod = ImageFrame::DisposalMethod::DisposeNotSpecifi
ed; |
| 307 m_frameInfo.append(frame); |
| 308 m_decoder->setMetaDataDecoded(); |
| 309 } |
| 310 return true; |
| 311 } |
| 312 |
| 313 FastSharedBufferReader reader(&data); |
| 314 char readBuffer[kBufferSize]; |
| 315 |
| 316 // At this point, the query is FrameMetaDataQuery. Loop over the data and |
| 317 // register all frames we can find. A frame is registered on the next fcTL |
| 318 // chunk or when the IEND chunk is found. This ensures that only complete |
| 319 // frames are reported, unless there is an error in the stream. |
| 320 while (reader.size() >= m_readOffset + 8) { |
| 321 const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, |
| 322 readBuffer); |
| 323 const size_t length = png_get_uint_32(chunk); |
| 324 const bool isFCTLChunk = memcmp(chunk + 4, "fcTL", 4) == 0; |
| 325 const bool isIENDChunk = memcmp(chunk + 4, "IEND", 4) == 0; |
| 326 |
| 327 // When we find an IDAT chunk (when the IDAT is part of the animation), |
| 328 // or an fdAT chunk, and the readOffset field of the newFrame is 0, |
| 329 // we have found the beginning of a new block of frame data. |
| 330 const bool isFrameData = memcmp(chunk + 4, "fdAT", 4) == 0 |
| 331 || (memcmp(chunk + 4, "IDAT", 4) == 0 && m_idatIsPartOfAnimation); |
| 332 if (m_newFrame.readOffset == 0 && isFrameData) { |
| 333 m_newFrame.readOffset = m_readOffset; |
| 334 |
| 335 // An fcTL or IEND marks the end of the previous frame. Thus, the |
| 336 // FrameInfo data in m_newFrame is submitted to the m_frameInfo vector. |
| 337 // |
| 338 // Furthermore, an fcTL chunk indicates a new frame is coming, |
| 339 // so the m_newFrame variable is prepared accordingly by setting the |
| 340 // readOffset field to 0, which indicates that the frame control info |
| 341 // is available but that we haven't seen any frame data yet. |
| 342 } else if (isFCTLChunk || isIENDChunk) { |
| 343 if (m_newFrame.readOffset != 0) { |
| 344 m_newFrame.byteLength = m_readOffset - m_newFrame.readOffset; |
| 345 m_frameInfo.append(m_newFrame); |
| 346 m_newFrame.readOffset = 0; |
| 347 } |
| 348 |
| 349 if (reader.size() < m_readOffset + 12 + length) |
| 350 return false; |
| 351 |
| 352 if (isIENDChunk) { |
| 353 // Let the decoder know we've parsed all data, so it does not |
| 354 // need to query again. |
| 355 m_decoder->setMetaDataDecoded(); |
| 356 return true; |
| 357 } |
| 358 |
| 359 // At this point, we're dealing with an fcTL chunk, since the above |
| 360 // statement already returns on IEND chunks. |
| 361 |
| 362 // If the fcTL chunk is not 26 bytes long, we can't process it. |
| 363 if (length != 26) |
| 364 return m_decoder->setFailed(); |
| 365 |
| 366 chunk = readAsConstPngBytep(reader, m_readOffset + 8, length, |
| 367 readBuffer); |
| 368 parseFrameInfo(chunk); |
| 369 |
| 370 } |
| 371 m_readOffset += 12 + length; |
| 372 } |
| 373 return false; |
| 374 } |
| 375 |
| 376 // If |length| == 0, read until the stream ends. |
| 377 // @return: number of bytes processed. |
| 378 size_t PNGImageReader::processData(SegmentReader& data, size_t offset, |
| 379 size_t length) |
| 380 { |
| 381 const char* segment; |
| 382 size_t totalProcessedBytes = 0; |
| 383 while (size_t segmentLength = data.getSomeData(segment, offset)) { |
| 384 if (length > 0 && segmentLength + totalProcessedBytes > length) |
| 385 segmentLength = length - totalProcessedBytes; |
| 386 png_process_data(m_png, m_info, |
| 387 reinterpret_cast<png_byte*>(const_cast<char*>(segment))
, |
| 388 segmentLength); |
| 389 offset += segmentLength; |
| 390 totalProcessedBytes += segmentLength; |
| 391 if (totalProcessedBytes == length) |
| 392 return length; |
| 393 } |
| 394 return totalProcessedBytes; |
| 395 } |
| 396 |
| 397 // This methods reads through the stream until it has parsed the image size. |
| 398 // @return true when it succeeds in parsing the size. |
| 399 // false when: |
| 400 // A) not enough data is provided |
| 401 // B) decoding by libpng fails. In the this case, it will also call |
| 402 // setFailed on m_decoder. |
| 403 bool PNGImageReader::parseSize(SegmentReader &data) |
| 404 { |
| 405 FastSharedBufferReader reader(&data); |
| 406 char readBuffer[kBufferSize]; |
| 407 |
| 408 // Process the PNG signature and the IHDR with libpng, such that this code |
| 409 // does not need to be bothered with parsing the contents. This also enables |
| 410 // the reader to use the existing headerAvailable callback in the decoder. |
| 411 // |
| 412 // When we already have decoded the signature, we don't need to do it again. |
| 413 // By setting a flag for this we allow for byte by byte parsing. |
| 414 if (!m_parsedSignature) { |
| 415 if (reader.size() < m_readOffset + 8) |
| 416 return false; |
| 417 const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, |
| 418 readBuffer); |
| 419 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); |
| 420 m_readOffset += 8; |
| 421 m_parsedSignature = true; |
| 422 // Initialize the newFrame by setting the readOffset to 0. |
| 423 m_newFrame.readOffset = 0; |
| 424 } |
| 425 |
| 426 // This loop peeks at the chunk tag until the IDAT chunk is found. When |
| 427 // a different tag is encountered, pass it on to libpng for general parsing. |
| 428 // We can peek at chunks by looking at the first 8 bytes, which contain the |
| 429 // length and the chunk tag. |
| 430 // |
| 431 // When an fcTL (frame control) is encountered before the IDAT, the frame |
| 432 // data in the IDAT chunk is part of the animation. This case is flagged |
| 433 // and the frame info is stored by parsing the fcTL chunk. |
| 434 while (reader.size() >= m_readOffset + 8) { |
| 435 const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, |
| 436 readBuffer); |
| 437 const png_uint_32 length = png_get_uint_32(chunk); |
| 438 |
| 439 // If we encounter the IDAT chunk, we're done with the png header |
| 440 // chunks. Indicate this to libpng by sending the beginning of the IDAT |
| 441 // chunk, which will trigger libpng to call the headerAvailable |
| 442 // callback on m_decoder. This provides the size to the decoder. |
| 443 if (memcmp(chunk + 4, "IDAT", 4) == 0) { |
| 444 if (m_idatIsPartOfAnimation) |
| 445 m_newFrame.readOffset = m_readOffset; |
| 446 m_bytesInfo = m_readOffset; |
| 447 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); |
| 448 return true; |
| 449 } |
| 450 |
| 451 // Consider the PNG image animated if an acTL chunk of the correct |
| 452 // length is present. Parsing the acTL content is done by |
| 453 // parseAnimationControl, called by libpng's png_process_data. |
| 454 if (memcmp(chunk + 4, "acTL", 4) == 0 && length == 8) |
| 455 m_isAnimated = true; |
| 456 |
| 457 // We don't need to check for |length| here, because the decoder will |
| 458 // fail later on for invalid fcTL chunks. |
| 459 if (memcmp(chunk + 4, "fcTL", 4) == 0) |
| 460 m_idatIsPartOfAnimation = true; |
| 461 |
| 462 // 12 is the length, tag and crc part of the chunk, which are all 4B. |
| 463 if (reader.size() < m_readOffset + length + 12) |
| 464 break; |
| 465 |
| 466 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); |
| 467 processData(data, m_readOffset + 8, length + 4); |
| 468 m_readOffset += length + 12; |
| 469 } |
| 470 |
| 471 // If we end up here, not enough data was available for the IDAT chunk |
| 472 // So libpng would not have called headerAvailable yet. |
| 473 return false; |
| 474 } |
| 475 |
| 476 |
| 477 void PNGImageReader::parseAnimationChunk(const char tag[], const void* data_chun
k, size_t length) |
| 478 { |
| 479 const png_byte* data = static_cast<const png_byte*>(data_chunk); |
| 480 |
| 481 // The number of frames as indicated in the animation control chunk (acTL) |
| 482 // is ignored, and the number of frames that are actually present is used. |
| 483 // For now, when the number of indicated frames is different from the |
| 484 // number of supplied frames, the number of supplied frames is what is |
| 485 // provided to the decoder. Therefore, it does not add any benefit of |
| 486 // looking at the value of the indicated framecount. A note here is that |
| 487 // there may be optimisations available, for example, prescaling vectors. |
| 488 if (strcmp(tag, "acTL") == 0 && length == 8) { |
| 489 png_uint_32 repetitionCount = png_get_uint_32(data + 4); |
| 490 m_decoder->setRepetitionCount(repetitionCount); |
| 491 |
| 492 // For fcTL, decoding fails if it does not have the correct length. It is |
| 493 // impossible to make a guess about the frame if not all data is available. |
| 494 // Use longjmp to get back to parse(), which is necessary since this method |
| 495 // is called by a libpng callback. |
| 496 } else if (strcmp(tag, "fcTL") == 0) { |
| 497 if (length != 26) |
| 498 longjmp(JMPBUF(m_png), 1); |
| 499 parseFrameInfo(data); |
| 500 } |
| 501 |
| 502 } |
| 503 |
| 504 size_t PNGImageReader::frameCount() const |
| 505 { |
| 506 return m_frameInfo.size(); |
| 507 } |
| 508 |
| 509 const PNGImageReader::FrameInfo& PNGImageReader::frameInfo(size_t index) const |
| 510 { |
| 511 ASSERT(index < m_frameInfo.size()); |
| 512 return m_frameInfo[index]; |
| 513 } |
| 514 |
| 515 // Extract the frame control info and store it in m_newFrame. The length check |
| 516 // on the data chunk has been done in parseAnimationChunk. |
| 517 // The fcTL specification used can be found at: |
| 518 // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu
nk |
| 519 void PNGImageReader::parseFrameInfo(const png_byte* data) |
| 520 { |
| 521 png_uint_32 width, height, xOffset, yOffset; |
| 522 png_uint_16 delayNumerator, delayDenominator; |
| 523 width = png_get_uint_32(data + 4); |
| 524 height = png_get_uint_32(data + 8); |
| 525 xOffset = png_get_uint_32(data + 12); |
| 526 yOffset = png_get_uint_32(data + 16); |
| 527 delayNumerator = png_get_uint_16(data + 20); |
| 528 delayDenominator = png_get_uint_16(data + 22); |
| 529 |
| 530 m_newFrame.duration = (delayDenominator == 0) ? delayNumerator * 10 |
| 531 : delayNumerator * 1000 / delayDenominator; |
| 532 m_newFrame.frameRect = IntRect(xOffset, yOffset, width, height); |
| 533 m_newFrame.disposalMethod = data[24]; |
| 534 m_newFrame.alphaBlend = data[25]; |
| 535 |
| 536 } |
| 537 |
| 538 }; // namespace blink |
OLD | NEW |