Chromium Code Reviews| 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..78495406bc43e2a747ee5494559a3f6d6fe0b768 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/platform/image-decoders/png/PNGImageReader.cpp |
| @@ -0,0 +1,185 @@ |
| +/* |
| + * 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 "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 pngFrameHeaderAvailable(png_structp png, png_infop) |
| +{ |
| + imageDecoder(png)->frameHeaderAvailable(); |
| +} |
| + |
| +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) |
| +{ |
| + 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->readChunk((const char*) chunk->name, chunk->data, chunk->size); |
| + return 1; |
| +} |
| + |
| +PNGImageReader::PNGImageReader(PNGImageDecoder* decoder, size_t readOffset) |
| + : m_decoder(decoder) |
| + , m_readOffset(readOffset) |
| + , m_currentBufferSize(0) |
| + , m_decodingSizeOnly(false) |
| + , m_hasAlpha(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, 1, 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); |
| + m_readOffset = 0; |
|
scroggo_chromium
2016/10/04 14:50:17
No reason to set an integer variable in the destru
joostouwerling
2016/10/11 16:31:06
Acknowledged.
|
| +} |
| + |
| +bool PNGImageReader::parse(const SegmentReader& data, PNGImageDecoder::PNGParseQuery query) |
| +{ |
| + |
| + if (PNGImageDecoder::PNGParseQuery::PNGSizeQuery == query) |
| + m_decodingSizeOnly = true; |
| + |
| + if (setjmp(JMPBUF(m_png))) |
| + return m_decoder->setFailed(); |
| + |
| + const char *segment; |
| + while (size_t segmentLength = data.getSomeData(segment, m_readOffset)) { |
| + m_readOffset += segmentLength; |
| + m_currentBufferSize = m_readOffset; |
| + png_process_data(m_png, m_info, |
| + reinterpret_cast<png_bytep>(const_cast<char*>(segment)), |
| + segmentLength); |
| + |
| + if (PNGImageDecoder::PNGParseQuery::PNGSizeQuery == query |
|
scroggo_chromium
2016/10/04 14:50:16
This sure looks like it should be a switch stateme
joostouwerling
2016/10/11 16:31:06
Acknowledged.
|
| + && m_decoder->isSizeAvailable()) |
| + return true; |
| + |
| + if (PNGImageDecoder::PNGParseQuery::PNGFrameCountQuery == query |
| + && m_decoder->isDecodedFrameCountAvailable()) |
| + return true; |
| + |
| + if (PNGImageDecoder::PNGParseQuery::PNGFrameDataQuery == query |
| + && m_decoder->frameIsCompleteAtIndex(0)) |
|
scroggo_chromium
2016/10/04 14:50:16
I don't understand PNGFrameDataQuery. What is it a
joostouwerling
2016/10/04 17:48:31
This is the query I want to use for decoding an ac
|
| + return true; |
| + } |
| + return false; |
| + |
| +} |
| + |
| +bool PNGImageReader::readChunk(const char tag[], const void* data_chunk, size_t length) |
| +{ |
| + const png_byte* data = static_cast<const png_byte*>(data_chunk); |
| + |
| + if (!strcmp(tag, "acTL")) { |
| + ASSERT(length == 8); |
| + return readAnimationControl(data); |
| + } |
| + |
| + return false; |
| +} |
| + |
| +inline bool PNGImageReader::readAnimationControl(const png_byte* data) |
| +{ |
| + png_uint_32 numFrames, numRepetitions; |
| + numFrames = png_get_uint_32(data); |
|
scroggo_chromium
2016/10/04 14:50:17
Nit: I might call these frameCount and repetitionC
joostouwerling
2016/10/11 16:31:06
Acknowledged.
|
| + numRepetitions = png_get_uint_32(data); |
| + m_decoder->animationControlAvailable(numFrames, numRepetitions); |
| + return true; |
| +} |
| + |
| +}; // namespace blink |