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 "png.h" |
| 43 #include "wtf/PtrUtil.h" |
| 44 #include <memory> |
| 45 |
| 46 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR) |
| 47 #error version error: compile against a versioned libpng. |
| 48 #endif |
| 49 #if USE(QCMSLIB) |
| 50 #include "qcms.h" |
| 51 #endif |
| 52 |
| 53 #if PNG_LIBPNG_VER_MAJOR > 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MIN
OR >= 4) |
| 54 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr) |
| 55 #else |
| 56 #define JMPBUF(png_ptr) png_ptr->jmpbuf |
| 57 #endif |
| 58 |
| 59 namespace { |
| 60 |
| 61 inline blink::PNGImageDecoder* imageDecoder(png_structp png) |
| 62 { |
| 63 return static_cast<blink::PNGImageDecoder*>(png_get_progressive_ptr(png)); |
| 64 } |
| 65 |
| 66 void PNGAPI pngFrameHeaderAvailable(png_structp png, png_infop) |
| 67 { |
| 68 imageDecoder(png)->frameHeaderAvailable(); |
| 69 } |
| 70 |
| 71 void PNGAPI pngHeaderAvailable(png_structp png, png_infop) |
| 72 { |
| 73 imageDecoder(png)->headerAvailable(); |
| 74 } |
| 75 |
| 76 void PNGAPI pngRowAvailable(png_structp png, png_bytep row, png_uint_32 rowIndex
, int state) |
| 77 { |
| 78 imageDecoder(png)->rowAvailable(row, rowIndex, state); |
| 79 } |
| 80 |
| 81 void PNGAPI pngComplete(png_structp png, png_infop) |
| 82 { |
| 83 imageDecoder(png)->complete(); |
| 84 } |
| 85 |
| 86 void PNGAPI pngFailed(png_structp png, png_const_charp) |
| 87 { |
| 88 longjmp(JMPBUF(png), 1); |
| 89 } |
| 90 |
| 91 } // namespace |
| 92 |
| 93 namespace blink { |
| 94 |
| 95 // This is the callback function for unknown PNG chunks, which is used to |
| 96 // extract the animation chunks. |
| 97 static int readAnimationChunk(png_structp png_ptr, png_unknown_chunkp chunk) |
| 98 { |
| 99 PNGImageReader* reader = (PNGImageReader*) png_get_user_chunk_ptr(png_ptr); |
| 100 reader->readChunk((const char*) chunk->name, chunk->data, chunk->size); |
| 101 return 1; |
| 102 } |
| 103 |
| 104 PNGImageReader::PNGImageReader(PNGImageDecoder* decoder, size_t readOffset) |
| 105 : m_decoder(decoder) |
| 106 , m_readOffset(readOffset) |
| 107 , m_currentBufferSize(0) |
| 108 , m_decodingSizeOnly(false) |
| 109 , m_hasAlpha(false) |
| 110 #if USE(QCMSLIB) |
| 111 , m_rowBuffer() |
| 112 #endif |
| 113 { |
| 114 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0); |
| 115 m_info = png_create_info_struct(m_png); |
| 116 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, pngRowAvai
lable, 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, 1, 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 m_readOffset = 0; |
| 129 } |
| 130 |
| 131 bool PNGImageReader::parse(const SegmentReader& data, PNGImageDecoder::PNGParseQ
uery query) |
| 132 { |
| 133 |
| 134 if (PNGImageDecoder::PNGParseQuery::PNGSizeQuery == query) |
| 135 m_decodingSizeOnly = true; |
| 136 |
| 137 if (setjmp(JMPBUF(m_png))) |
| 138 return m_decoder->setFailed(); |
| 139 |
| 140 const char *segment; |
| 141 while (size_t segmentLength = data.getSomeData(segment, m_readOffset)) { |
| 142 m_readOffset += segmentLength; |
| 143 m_currentBufferSize = m_readOffset; |
| 144 png_process_data(m_png, m_info, |
| 145 reinterpret_cast<png_bytep>(const_cast<char*>(segment))
, |
| 146 segmentLength); |
| 147 |
| 148 if (PNGImageDecoder::PNGParseQuery::PNGSizeQuery == query |
| 149 && m_decoder->isSizeAvailable()) |
| 150 return true; |
| 151 |
| 152 if (PNGImageDecoder::PNGParseQuery::PNGFrameCountQuery == query |
| 153 && m_decoder->isDecodedFrameCountAvailable()) |
| 154 return true; |
| 155 |
| 156 if (PNGImageDecoder::PNGParseQuery::PNGFrameDataQuery == query |
| 157 && m_decoder->frameIsCompleteAtIndex(0)) |
| 158 return true; |
| 159 } |
| 160 return false; |
| 161 |
| 162 } |
| 163 |
| 164 bool PNGImageReader::readChunk(const char tag[], const void* data_chunk, size_t
length) |
| 165 { |
| 166 const png_byte* data = static_cast<const png_byte*>(data_chunk); |
| 167 |
| 168 if (!strcmp(tag, "acTL")) { |
| 169 ASSERT(length == 8); |
| 170 return readAnimationControl(data); |
| 171 } |
| 172 |
| 173 return false; |
| 174 } |
| 175 |
| 176 inline bool PNGImageReader::readAnimationControl(const png_byte* data) |
| 177 { |
| 178 png_uint_32 numFrames, numRepetitions; |
| 179 numFrames = png_get_uint_32(data); |
| 180 numRepetitions = png_get_uint_32(data + 4); |
| 181 m_decoder->animationControlAvailable(numFrames, numRepetitions); |
| 182 return true; |
| 183 } |
| 184 |
| 185 }; // namespace blink |
OLD | NEW |