Index: third_party/WebKit/Source/platform/image-decoders/png/PNGImageReader.cpp |
diff --git a/third_party/WebKit/Source/platform/image-decoders/png/PNGImageReader.cpp b/third_party/WebKit/Source/platform/image-decoders/png/PNGImageReader.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..53eb430aaf09bd8ea8d20a2f28dbf7da0d3b5811 |
--- /dev/null |
+++ b/third_party/WebKit/Source/platform/image-decoders/png/PNGImageReader.cpp |
@@ -0,0 +1,403 @@ |
+/* |
+ * Copyright (C) 2006 Apple Computer, Inc. |
+ * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. |
+ * |
+ * Portions are Copyright (C) 2001 mozilla.org |
+ * |
+ * Other contributors: |
+ * Stuart Parmenter <stuart@mozilla.com> |
+ * |
+ * This library is free software; you can redistribute it and/or |
+ * modify it under the terms of the GNU Lesser General Public |
+ * License as published by the Free Software Foundation; either |
+ * version 2.1 of the License, or (at your option) any later version. |
+ * |
+ * This library is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
+ * Lesser General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU Lesser General Public |
+ * License along with this library; if not, write to the Free Software |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
+ * |
+ * Alternatively, the contents of this file may be used under the terms |
+ * of either the Mozilla Public License Version 1.1, found at |
+ * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public |
+ * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html |
+ * (the "GPL"), in which case the provisions of the MPL or the GPL are |
+ * applicable instead of those above. If you wish to allow use of your |
+ * version of this file only under the terms of one of those two |
+ * licenses (the MPL or the GPL) and not to allow others to use your |
+ * version of this file under the LGPL, indicate your decision by |
+ * deletingthe provisions above and replace them with the notice and |
+ * other provisions required by the MPL or the GPL, as the case may be. |
+ * If you do not delete the provisions above, a recipient may use your |
+ * version of this file under any of the LGPL, the MPL or the GPL. |
+ */ |
+ |
+#include "platform/image-decoders/png/PNGImageReader.h" |
+ |
+#include "platform/image-decoders/png/PNGImageDecoder.h" |
+#include "platform/image-decoders/FastSharedBufferReader.h" |
+#include "png.h" |
+#include "wtf/PtrUtil.h" |
+#include <memory> |
+ |
+#if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR) |
+#error version error: compile against a versioned libpng. |
+#endif |
+#if USE(QCMSLIB) |
+#include "qcms.h" |
+#endif |
+ |
+#if PNG_LIBPNG_VER_MAJOR > 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 4) |
+#define JMPBUF(png_ptr) png_jmpbuf(png_ptr) |
+#else |
+#define JMPBUF(png_ptr) png_ptr->jmpbuf |
+#endif |
+ |
+namespace { |
+ |
+inline blink::PNGImageDecoder* imageDecoder(png_structp png) |
+{ |
+ return static_cast<blink::PNGImageDecoder*>(png_get_progressive_ptr(png)); |
+} |
+ |
+void PNGAPI pngHeaderAvailable(png_structp png, png_infop) |
+{ |
+ imageDecoder(png)->headerAvailable(); |
+} |
+ |
+void PNGAPI pngRowAvailable(png_structp png, png_bytep row, |
+ png_uint_32 rowIndex, int state) |
+{ |
+ imageDecoder(png)->rowAvailable(row, rowIndex, state); |
+} |
+ |
+void PNGAPI pngComplete(png_structp png, png_infop) |
+{ |
+ imageDecoder(png)->complete(); |
+} |
+ |
+void PNGAPI pngFailed(png_structp png, png_const_charp err) |
+{ |
+ longjmp(JMPBUF(png), 1); |
+} |
+ |
+} // namespace |
+ |
+namespace blink { |
+ |
+ |
+// This is the callback function for unknown PNG chunks, which is used to |
+// extract the animation chunks. |
+static int readAnimationChunk(png_structp png_ptr, png_unknown_chunkp chunk) |
+{ |
+ PNGImageReader* reader = (PNGImageReader*) png_get_user_chunk_ptr(png_ptr); |
+ reader->parseAnimationChunk((const char*) chunk->name, chunk->data, |
+ chunk->size); |
+ return 1; |
+} |
+ |
+PNGImageReader::PNGImageReader(PNGImageDecoder* decoder, size_t readOffset) |
+ : m_decoder(decoder) |
+ , m_readOffset(readOffset) |
+ , m_hasAlpha(false) |
+ , m_idatIsPartOfAnimation(false) |
+ , m_parsedSignature(false) |
+#if USE(QCMSLIB) |
+ , m_rowBuffer() |
+#endif |
+{ |
+ m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0); |
+ m_info = png_create_info_struct(m_png); |
+ png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, |
+ pngRowAvailable, pngComplete); |
+ |
+ // Keep the chunks which are of interest for APNG. |
+ png_byte apngChunks[] = {"acTL\0fcTL\0fdAT\0"}; |
+ png_set_keep_unknown_chunks(m_png, PNG_HANDLE_CHUNK_NEVER, apngChunks, 3); |
+ png_set_read_user_chunk_fn(m_png, (png_voidp) this, readAnimationChunk); |
+} |
+ |
+PNGImageReader::~PNGImageReader() |
+{ |
+ png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0); |
+ ASSERT(!m_png && !m_info); |
+} |
+ |
+ |
+// @TODO(joostouwerling) implement this method |
+// Decode the frame at |index| |
+bool PNGImageReader::decode(SegmentReader& data, size_t index) |
+{ |
+ return false; |
+} |
+ |
+// This method reads from the FastSharedBufferReader, starting at offset, |
+// and returns |length| bytes in the form of a pointer to a const png_byte*. |
+// This function is used to make it easy to access data from the reader in a |
+// png friendly way, and pass it to libpng for decoding. |
+// |
+// Pre-conditions before using this: |
+// - |reader|.size() >= |readOffset| + |length| |
+// - |buffer|.size() = kBufferSize >= |length| |
+// |
+// The reason for the last precondition is that at this point, the fcTL |
+// chunk (26B) is the largest chunk that is read using this method. If the data |
+// is not consecutive, it is stored in |buffer|, which should have the size of |
+// (at least) kBufferSize. |
+constexpr size_t kBufferSize = 26; |
+const png_byte* readAsConstPngBytep(const FastSharedBufferReader& reader, |
+ size_t readOffset, size_t length, |
+ char* buffer) |
+{ |
+ ASSERT(length <= kBufferSize); |
+ return reinterpret_cast<const png_byte*>( |
+ reader.getConsecutiveData(readOffset, length, buffer)); |
+} |
+ |
+bool PNGImageReader::parse(SegmentReader& data, |
+ PNGImageDecoder::PNGParseQuery query) |
+{ |
+ if (setjmp(JMPBUF(m_png))) |
+ return m_decoder->setFailed(); |
+ |
+ // If the size has not been parsed, do that first, since it's necessary |
+ // for both the Size and MetaData query. If parseSize returns false, |
+ // it failed because of a lack of data so we can return false at this point. |
+ if (!m_decoder->isDecodedSizeAvailable() && !parseSize(data)) |
+ return false; |
+ |
+ if (query == PNGImageDecoder::PNGParseQuery::PNGSizeQuery) |
+ return m_decoder->isDecodedSizeAvailable(); |
+ |
+ FastSharedBufferReader reader(&data); |
+ char readBuffer[kBufferSize]; |
+ |
+ // At this point, the query is FrameMetaDataQuery. Loop over the data and |
+ // register all frames we can find. A frame is registered on the next fcTL |
+ // chunk or when the IEND chunk is found. This ensures that only complete |
+ // frames are reported, unless there is an error in the stream. |
+ while (reader.size() >= m_readOffset + 8) { |
+ const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, |
+ readBuffer); |
+ const size_t length = png_get_uint_32(chunk); |
+ const bool isFCTLChunk = memcmp(chunk + 4, "fcTL", 4) == 0; |
+ const bool isIENDChunk = memcmp(chunk + 4, "IEND", 4) == 0; |
+ |
+ // When we find an IDAT chunk (when the IDAT is part of the animation), |
+ // or an fdAT chunk, and the readOffset field of the newFrame is 0, |
+ // we have found the beginning of a new block of frame data. |
+ const bool isFrameData = memcmp(chunk + 4, "fdAT", 4) == 0 |
+ || (memcmp(chunk + 4, "IDAT", 4) == 0 && m_idatIsPartOfAnimation); |
+ if (m_newFrame.readOffset == 0 && isFrameData) { |
+ m_newFrame.readOffset = m_readOffset; |
+ |
+ // An fcTL or IEND marks the end of the previous frame. Thus, the |
+ // FrameInfo data in m_newFrame is submitted to the m_frameInfo vector. |
+ // |
+ // Furthermore, an fcTL chunk indicates a new frame is coming, |
+ // so the m_newFrame variable is prepared accordingly by setting the |
+ // readOffset field to 0, which indicates that the frame control info |
+ // is available but that we haven't seen any frame data yet. |
+ } else if (isFCTLChunk || isIENDChunk) { |
+ if (m_newFrame.readOffset != 0) { |
+ m_newFrame.byteLength = (m_readOffset - 1) - m_newFrame.readOffset; |
+ m_frameInfo.append(m_newFrame); |
+ m_newFrame.readOffset = 0; |
+ } |
+ |
+ if (reader.size() < m_readOffset + 12 + length) |
+ return false; |
+ |
+ if (isIENDChunk) { |
+ // Let the decoder know we've parsed all data, so it does not |
+ // need to query again. |
+ m_decoder->setMetaDataDecoded(); |
+ return true; |
+ } |
+ |
+ // Prepare the new frame info and read the frame control data. |
+ chunk = readAsConstPngBytep(reader, m_readOffset + 8, length, |
+ readBuffer); |
+ parseFrameInfo(chunk); |
+ } |
+ m_readOffset += 12 + length; |
+ } |
+ return false; |
+} |
+ |
+bool PNGImageReader::processData(SegmentReader& data, size_t offset, size_t length) |
+{ |
+ const char* segment; |
+ size_t totalProcessedBytes = 0; |
+ while (size_t segmentLength = data.getSomeData(segment, offset)) { |
+ if (segmentLength > length) |
+ segmentLength = length; |
+ png_process_data(m_png, m_info, |
+ reinterpret_cast<png_byte*>(const_cast<char*>(segment)), |
+ segmentLength); |
+ offset += segmentLength; |
+ totalProcessedBytes += segmentLength; |
+ if (totalProcessedBytes == length) |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+// This methods reads through the stream until it has parsed the image size. |
+// @return true when it succeeds in parsing the size. |
+// false when: |
+// A) not enough data is provided |
+// B) decoding by libpng fails. In the this case, it will also call |
+// setFailed on m_decoder. |
+bool PNGImageReader::parseSize(SegmentReader &data) |
+{ |
+ FastSharedBufferReader reader(&data); |
+ char readBuffer[kBufferSize]; |
+ |
+ // Process the PNG signature and the IHDR with libpng, such that this code |
+ // does not need to be bothered with parsing the contents. This also enables |
+ // the reader to use the existing headerAvailable callback in the decoder. |
+ // |
+ // When we already have decoded the signature, we don't need to do it again. |
+ // By setting a flag for this we allow for byte by byte parsing. |
+ if (!m_parsedSignature) { |
+ if (reader.size() < m_readOffset + 8) |
+ return false; |
+ const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, |
+ readBuffer); |
+ png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); |
+ m_readOffset += 8; |
+ m_parsedSignature = true; |
+ // Initialize the newFrame by setting the readOffset to 0. |
+ m_newFrame.readOffset = 0; |
+ } |
+ |
+ // This loop peeks at the chunk tag until the IDAT chunk is found. When |
+ // a different tag is encountered, pass it on to libpng for general parsing. |
+ // We can peek at chunks by looking at the first 8 bytes, which contain the |
+ // length and the chunk tag. |
+ // |
+ // When an fcTL (frame control) is encountered before the IDAT, the frame |
+ // data in the IDAT chunk is part of the animation. This case is flagged |
+ // and the frame info is stored by parsing the fcTL chunk. |
+ while (reader.size() >= m_readOffset + 8) { |
+ const png_byte* chunk = readAsConstPngBytep(reader, m_readOffset, 8, |
+ readBuffer); |
+ const png_uint_32 length = png_get_uint_32(chunk); |
+ |
+ // If we encounter the IDAT chunk, we're done with the png header |
+ // chunks. Indicate this to libpng by sending the beginning of the IDAT |
+ // chunk, which will trigger libpng to call the headerAvailable |
+ // callback on m_decoder. This provides the size to the decoder. |
+ if (memcmp(chunk + 4, "IDAT", 4) == 0) { |
+ m_newFrame.readOffset = m_readOffset; |
+ png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); |
+ return true; |
+ } |
+ |
+ if (memcmp(chunk + 4, "fcTL", 4) == 0) |
+ m_idatIsPartOfAnimation = true; |
+ |
+ // 12 is the length, tag and crc part of the chunk, which are all 4B. |
+ if (reader.size() < m_readOffset + length + 12) |
+ break; |
+ |
+ png_process_data(m_png, m_info, const_cast<png_byte*>(chunk), 8); |
+ processData(data, m_readOffset + 8, length + 4); |
+ m_readOffset += length + 12; |
+ } |
+ |
+ // If we end up here, not enough data was available for the IDAT chunk |
+ // So libpng would not have called headerAvailable yet. |
+ return false; |
+} |
+ |
+ |
+void PNGImageReader::parseAnimationChunk(const char tag[], const void* data_chunk, size_t length) |
+{ |
+ const png_byte* data = static_cast<const png_byte*>(data_chunk); |
+ |
+ // The number of frames as indicated in the animation control chunk (acTL) |
+ // is ignored, and the number of frames that are actually present is used. |
+ // For now, when the number of indicated frames is different from the |
+ // number of supplied frames, the number of supplied frames is what is |
+ // provided to the decoder. Therefore, it does not add any benefit of |
+ // looking at the value of the indicated framecount. A note here is that |
+ // there may be optimisations available, for example, prescaling vectors. |
+ if (strcmp(tag, "acTL") == 0 && length == 8) { |
+ png_uint_32 repetitionCount = png_get_uint_32(data + 4); |
+ m_decoder->setRepetitionCount(repetitionCount); |
+ } else if (strcmp(tag, "fcTL") == 0 && length == 26) { |
+ parseFrameInfo(data); |
+ } |
+ |
+} |
+ |
+size_t PNGImageReader::frameCount() const |
+{ |
+ return m_frameInfo.size(); |
+} |
+ |
+const PNGImageReader::FrameInfo& PNGImageReader::frameInfo(size_t index) const |
+{ |
+ ASSERT(index < m_frameInfo.size()); |
+ return m_frameInfo[index]; |
+} |
+ |
+// These are mapped according to: |
+// https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk |
+inline ImageFrame::DisposalMethod getDisposalMethod(uint8_t disposalMethod) |
+{ |
+ switch (disposalMethod) { |
+ case 0: |
+ return ImageFrame::DisposalMethod::DisposeKeep; |
+ case 1: |
+ return ImageFrame::DisposalMethod::DisposeOverwriteBgcolor; |
+ case 2: |
+ return ImageFrame::DisposalMethod::DisposeOverwritePrevious; |
+ } |
+ return ImageFrame::DisposalMethod::DisposeNotSpecified; |
+} |
+ |
+ |
+// These are mapped according to: |
+// https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk |
+inline ImageFrame::AlphaBlendSource getAlphaBlend(uint8_t alphaBlend) |
+{ |
+ if (alphaBlend == 1) |
+ return ImageFrame::AlphaBlendSource::BlendAtopPreviousFrame; |
+ return ImageFrame::AlphaBlendSource::BlendAtopBgcolor; |
+} |
+ |
+ |
+// Extract the frame control info and store it in m_newFrame. The length check |
+// on the data chunk has been done in parseAnimationChunk. |
+// The fcTL specification used can be found at: |
+// https://wiki.mozilla.org/APNG_Specification#.60fcTL.60:_The_Frame_Control_Chunk |
+void PNGImageReader::parseFrameInfo(const png_byte* data) |
+{ |
+ png_uint_32 width, height, xOffset, yOffset; |
+ png_uint_16 delayNumerator, delayDenominator; |
+ png_byte disposalMethod, alphaBlend; |
+ width = png_get_uint_32(data + 4); |
+ height = png_get_uint_32(data + 8); |
+ xOffset = png_get_uint_32(data + 12); |
+ yOffset = png_get_uint_32(data + 16); |
+ delayNumerator = png_get_uint_16(data + 20); |
+ delayDenominator = png_get_uint_16(data + 22); |
+ disposalMethod = data[24]; |
+ alphaBlend = data[25]; |
+ |
+ m_newFrame.duration = (delayDenominator == 0) ? delayNumerator * 10 |
+ : delayNumerator * 1000 / delayDenominator; |
+ m_newFrame.frameRect = IntRect(xOffset, yOffset, width, height); |
+ m_newFrame.disposalMethod = getDisposalMethod(disposalMethod); |
+ m_newFrame.alphaBlend = getAlphaBlend(alphaBlend); |
+ |
+} |
+ |
+}; // namespace blink |