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 |
| 93 // This is the callback function for unknown PNG chunks, which is used to |
| 94 // extract the animation chunks. |
| 95 static int readAnimationChunk(png_structp png_ptr, png_unknown_chunkp chunk) |
| 96 { |
| 97 PNGImageReader* reader = (PNGImageReader*) png_get_user_chunk_ptr(png_ptr); |
| 98 reader->parseAnimationChunk((const char*) chunk->name, chunk->data, |
| 99 chunk->size); |
| 100 return 1; |
| 101 } |
| 102 |
| 103 PNGImageReader::PNGImageReader(PNGImageDecoder* decoder, size_t readOffset) |
| 104 : m_decoder(decoder) |
| 105 , m_readOffset(readOffset) |
| 106 , m_hasAlpha(false) |
| 107 , m_idatIsPartOfAnimation(false) |
| 108 , m_parsedSignature(false) |
| 109 #if USE(QCMSLIB) |
| 110 , m_rowBuffer() |
| 111 #endif |
| 112 { |
| 113 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0); |
| 114 m_info = png_create_info_struct(m_png); |
| 115 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, |
| 116 pngRowAvailable, pngComplete); |
| 117 |
| 118 // Keep the chunks which are of interest for APNG. |
| 119 png_byte apngChunks[] = {"acTL\0fcTL\0fdAT\0"}; |
| 120 png_set_keep_unknown_chunks(m_png, PNG_HANDLE_CHUNK_NEVER, apngChunks, 3); |
| 121 png_set_read_user_chunk_fn(m_png, (png_voidp) this, readAnimationChunk); |
| 122 } |
| 123 |
| 124 PNGImageReader::~PNGImageReader() |
| 125 { |
| 126 png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0); |
| 127 ASSERT(!m_png && !m_info); |
| 128 } |
| 129 |
| 130 |
| 131 // @TODO(joostouwerling) implement this method |
| 132 // Decode the frame at |index| |
| 133 bool PNGImageReader::decode(SegmentReader& data, size_t index) |
| 134 { |
| 135 return false; |
| 136 } |
| 137 |
| 138 // This method reads from the FastSharedBufferReader, starting at offset, |
| 139 // and returns |length| bytes in the form of a pointer to a const png_byte*. |
| 140 // This function is used to make it easy to access data from the reader in a |
| 141 // png friendly way, and pass it to libpng for decoding. |
| 142 // |
| 143 // Pre-conditions before using this: |
| 144 // - |reader|.size() >= |readOffset| + |length| |
| 145 // - |buffer|.size() = kBufferSize >= |length| |
| 146 // |
| 147 // The reason for the last precondition is that at this point, the fcTL |
| 148 // chunk (26B) is the largest chunk that is read using this method. If the data |
| 149 // is not consecutive, it is stored in |buffer|, which should have the size of |
| 150 // (at least) kBufferSize. |
| 151 constexpr size_t kBufferSize = 26; |
| 152 const png_byte* readAsConstPngBytep(const FastSharedBufferReader& reader, |
| 153 size_t readOffset, size_t length, |
| 154 char* buffer) |
| 155 { |
| 156 ASSERT(length <= kBufferSize); |
| 157 return reinterpret_cast<const png_byte*>( |
| 158 reader.getConsecutiveData(readOffset, length, buffer)); |
| 159 } |
| 160 |
| 161 bool PNGImageReader::parse(SegmentReader& data, |
| 162 PNGImageDecoder::PNGParseQuery query) |
| 163 { |
| 164 if (setjmp(JMPBUF(m_png))) |
| 165 return m_decoder->setFailed(); |
| 166 |
| 167 // If the size has not been parsed, do that first, since it's necessary |
| 168 // for both the Size and MetaData query. If parseSize returns false, |
| 169 // it failed because of a lack of data so we can return false at this point. |
| 170 if (!m_decoder->isDecodedSizeAvailable() && !parseSize(data)) |
| 171 return false; |
| 172 |
| 173 if (query == PNGImageDecoder::PNGParseQuery::PNGSizeQuery) |
| 174 return m_decoder->isDecodedSizeAvailable(); |
| 175 |
| 176 FastSharedBufferReader reader(&data); |
| 177 char readBuffer[kBufferSize]; |
| 178 |
| 179 // At this point, the query is FrameMetaDataQuery. Loop over the data and |
| 180 // register all frames we can find. A frame is registered on the next fcTL |
| 181 // chunk or when the IEND chunk is found. This ensures that only complete |
| 182 // frames are reported, unless there is an error in the stream. |
| 183 while (reader.size() >= m_readOffset + 8) { |
| 184 const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, |
| 185 readBuffer); |
| 186 const size_t length = png_get_uint_32(chunk); |
| 187 const bool isFCTLChunk = memcmp(chunk + 4, "fcTL", 4) == 0; |
| 188 const bool isIENDChunk = memcmp(chunk + 4, "IEND", 4) == 0; |
| 189 |
| 190 // When we find an IDAT chunk (when the IDAT is part of the animation), |
| 191 // or an fdAT chunk, and the readOffset field of the newFrame is 0, |
| 192 // we have found the beginning of a new block of frame data. |
| 193 const bool isFrameData = memcmp(chunk + 4, "fdAT", 4) == 0 |
| 194 || (memcmp(chunk + 4, "IDAT", 4) == 0 && m_idatIsPartOfAnimation); |
| 195 if (m_newFrame.readOffset == 0 && isFrameData) { |
| 196 m_newFrame.readOffset = m_readOffset; |
| 197 |
| 198 // An fcTL or IEND marks the end of the previous frame. Thus, the |
| 199 // FrameInfo data in m_newFrame is submitted to the m_frameInfo vector. |
| 200 // |
| 201 // Furthermore, an fcTL chunk indicates a new frame is coming, |
| 202 // so the m_newFrame variable is prepared accordingly by setting the |
| 203 // readOffset field to 0, which indicates that the frame control info |
| 204 // is available but that we haven't seen any frame data yet. |
| 205 } else if (isFCTLChunk || isIENDChunk) { |
| 206 if (m_newFrame.readOffset != 0) { |
| 207 m_newFrame.byteLength = (m_readOffset - 1) - m_newFrame.readOffs
et; |
| 208 m_frameInfo.append(m_newFrame); |
| 209 m_newFrame.readOffset = 0; |
| 210 } |
| 211 |
| 212 if (reader.size() < m_readOffset + 12 + length) |
| 213 return false; |
| 214 |
| 215 if (isIENDChunk) { |
| 216 // Let the decoder know we've parsed all data, so it does not |
| 217 // need to query again. |
| 218 m_decoder->setMetaDataDecoded(); |
| 219 return true; |
| 220 } |
| 221 |
| 222 // Prepare the new frame info and read the frame control data. |
| 223 chunk = readAsConstPngBytep(reader, m_readOffset + 8, length, |
| 224 readBuffer); |
| 225 parseFrameInfo(chunk); |
| 226 } |
| 227 m_readOffset += 12 + length; |
| 228 } |
| 229 return false; |
| 230 } |
| 231 |
| 232 bool PNGImageReader::processData(SegmentReader& data, size_t offset, size_t leng
th) |
| 233 { |
| 234 const char* segment; |
| 235 size_t totalProcessedBytes = 0; |
| 236 while (size_t segmentLength = data.getSomeData(segment, offset)) { |
| 237 if (segmentLength > length) |
| 238 segmentLength = length; |
| 239 png_process_data(m_png, m_info, |
| 240 reinterpret_cast<png_byte*>(const_cast<char*>(segment))
, |
| 241 segmentLength); |
| 242 offset += segmentLength; |
| 243 totalProcessedBytes += segmentLength; |
| 244 if (totalProcessedBytes == length) |
| 245 return true; |
| 246 } |
| 247 return false; |
| 248 } |
| 249 |
| 250 // This methods reads through the stream until it has parsed the image size. |
| 251 // @return true when it succeeds in parsing the size. |
| 252 // false when: |
| 253 // A) not enough data is provided |
| 254 // B) decoding by libpng fails. In the this case, it will also call |
| 255 // setFailed on m_decoder. |
| 256 bool PNGImageReader::parseSize(SegmentReader &data) |
| 257 { |
| 258 FastSharedBufferReader reader(&data); |
| 259 char readBuffer[kBufferSize]; |
| 260 |
| 261 // Process the PNG signature and the IHDR with libpng, such that this code |
| 262 // does not need to be bothered with parsing the contents. This also enables |
| 263 // the reader to use the existing headerAvailable callback in the decoder. |
| 264 // |
| 265 // When we already have decoded the signature, we don't need to do it again. |
| 266 // By setting a flag for this we allow for byte by byte parsing. |
| 267 if (!m_parsedSignature) { |
| 268 if (reader.size() < m_readOffset + 8) |
| 269 return false; |
| 270 const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, |
| 271 readBuffer); |
| 272 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); |
| 273 m_readOffset += 8; |
| 274 m_parsedSignature = true; |
| 275 // Initialize the newFrame by setting the readOffset to 0. |
| 276 m_newFrame.readOffset = 0; |
| 277 } |
| 278 |
| 279 // This loop peeks at the chunk tag until the IDAT chunk is found. When |
| 280 // a different tag is encountered, pass it on to libpng for general parsing. |
| 281 // We can peek at chunks by looking at the first 8 bytes, which contain the |
| 282 // length and the chunk tag. |
| 283 // |
| 284 // When an fcTL (frame control) is encountered before the IDAT, the frame |
| 285 // data in the IDAT chunk is part of the animation. This case is flagged |
| 286 // and the frame info is stored by parsing the fcTL chunk. |
| 287 while (reader.size() >= m_readOffset + 8) { |
| 288 const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, |
| 289 readBuffer); |
| 290 const png_uint_32 length = png_get_uint_32(chunk); |
| 291 |
| 292 // If we encounter the IDAT chunk, we're done with the png header |
| 293 // chunks. Indicate this to libpng by sending the beginning of the IDAT |
| 294 // chunk, which will trigger libpng to call the headerAvailable |
| 295 // callback on m_decoder. This provides the size to the decoder. |
| 296 if (memcmp(chunk + 4, "IDAT", 4) == 0) { |
| 297 m_newFrame.readOffset = m_readOffset; |
| 298 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); |
| 299 return true; |
| 300 } |
| 301 |
| 302 if (memcmp(chunk + 4, "fcTL", 4) == 0) |
| 303 m_idatIsPartOfAnimation = true; |
| 304 |
| 305 // 12 is the length, tag and crc part of the chunk, which are all 4B. |
| 306 if (reader.size() < m_readOffset + length + 12) |
| 307 break; |
| 308 |
| 309 png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); |
| 310 processData(data, m_readOffset + 8, length + 4); |
| 311 m_readOffset += length + 12; |
| 312 } |
| 313 |
| 314 // If we end up here, not enough data was available for the IDAT chunk |
| 315 // So libpng would not have called headerAvailable yet. |
| 316 return false; |
| 317 } |
| 318 |
| 319 |
| 320 void PNGImageReader::parseAnimationChunk(const char tag[], const void* data_chun
k, size_t length) |
| 321 { |
| 322 const png_byte* data = static_cast<const png_byte*>(data_chunk); |
| 323 |
| 324 // The number of frames as indicated in the animation control chunk (acTL) |
| 325 // is ignored, and the number of frames that are actually present is used. |
| 326 // For now, when the number of indicated frames is different from the |
| 327 // number of supplied frames, the number of supplied frames is what is |
| 328 // provided to the decoder. Therefore, it does not add any benefit of |
| 329 // looking at the value of the indicated framecount. A note here is that |
| 330 // there may be optimisations available, for example, prescaling vectors. |
| 331 if (strcmp(tag, "acTL") == 0 && length == 8) { |
| 332 png_uint_32 repetitionCount = png_get_uint_32(data + 4); |
| 333 m_decoder->setRepetitionCount(repetitionCount); |
| 334 } else if (strcmp(tag, "fcTL") == 0 && length == 26) { |
| 335 parseFrameInfo(data); |
| 336 } |
| 337 |
| 338 } |
| 339 |
| 340 size_t PNGImageReader::frameCount() const |
| 341 { |
| 342 return m_frameInfo.size(); |
| 343 } |
| 344 |
| 345 const PNGImageReader::FrameInfo& PNGImageReader::frameInfo(size_t index) const |
| 346 { |
| 347 ASSERT(index < m_frameInfo.size()); |
| 348 return m_frameInfo[index]; |
| 349 } |
| 350 |
| 351 // These are mapped according to: |
| 352 // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu
nk |
| 353 inline ImageFrame::DisposalMethod getDisposalMethod(uint8_t disposalMethod) |
| 354 { |
| 355 switch (disposalMethod) { |
| 356 case 0: |
| 357 return ImageFrame::DisposalMethod::DisposeKeep; |
| 358 case 1: |
| 359 return ImageFrame::DisposalMethod::DisposeOverwriteBgcolor; |
| 360 case 2: |
| 361 return ImageFrame::DisposalMethod::DisposeOverwritePrevious; |
| 362 } |
| 363 return ImageFrame::DisposalMethod::DisposeNotSpecified; |
| 364 } |
| 365 |
| 366 |
| 367 // These are mapped according to: |
| 368 // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu
nk |
| 369 inline ImageFrame::AlphaBlendSource getAlphaBlend(uint8_t alphaBlend) |
| 370 { |
| 371 if (alphaBlend == 1) |
| 372 return ImageFrame::AlphaBlendSource::BlendAtopPreviousFrame; |
| 373 return ImageFrame::AlphaBlendSource::BlendAtopBgcolor; |
| 374 } |
| 375 |
| 376 |
| 377 // Extract the frame control info and store it in m_newFrame. The length check |
| 378 // on the data chunk has been done in parseAnimationChunk. |
| 379 // The fcTL specification used can be found at: |
| 380 // https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chu
nk |
| 381 void PNGImageReader::parseFrameInfo(const png_byte* data) |
| 382 { |
| 383 png_uint_32 width, height, xOffset, yOffset; |
| 384 png_uint_16 delayNumerator, delayDenominator; |
| 385 png_byte disposalMethod, alphaBlend; |
| 386 width = png_get_uint_32(data + 4); |
| 387 height = png_get_uint_32(data + 8); |
| 388 xOffset = png_get_uint_32(data + 12); |
| 389 yOffset = png_get_uint_32(data + 16); |
| 390 delayNumerator = png_get_uint_16(data + 20); |
| 391 delayDenominator = png_get_uint_16(data + 22); |
| 392 disposalMethod = data[24]; |
| 393 alphaBlend = data[25]; |
| 394 |
| 395 m_newFrame.duration = (delayDenominator == 0) ? delayNumerator * 10 |
| 396 : delayNumerator * 1000 / delayDenominator; |
| 397 m_newFrame.frameRect = IntRect(xOffset, yOffset, width, height); |
| 398 m_newFrame.disposalMethod = getDisposalMethod(disposalMethod); |
| 399 m_newFrame.alphaBlend = getAlphaBlend(alphaBlend); |
| 400 |
| 401 } |
| 402 |
| 403 }; // namespace blink |
OLD | NEW |