Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006 Apple Computer, Inc. | 2 * Copyright (C) 2006 Apple Computer, Inc. |
| 3 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. | 3 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. |
| 4 * | 4 * |
| 5 * Portions are Copyright (C) 2001 mozilla.org | 5 * Portions are Copyright (C) 2001 mozilla.org |
| 6 * | 6 * |
| 7 * Other contributors: | 7 * Other contributors: |
| 8 * Stuart Parmenter <stuart@mozilla.com> | 8 * Stuart Parmenter <stuart@mozilla.com> |
| 9 * | 9 * |
| 10 * This library is free software; you can redistribute it and/or | 10 * This library is free software; you can redistribute it and/or |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 * licenses (the MPL or the GPL) and not to allow others to use your | 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 | 32 * version of this file under the LGPL, indicate your decision by |
| 33 * deletingthe provisions above and replace them with the notice and | 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. | 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 | 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. | 36 * version of this file under any of the LGPL, the MPL or the GPL. |
| 37 */ | 37 */ |
| 38 | 38 |
| 39 #include "platform/image-decoders/png/PNGImageDecoder.h" | 39 #include "platform/image-decoders/png/PNGImageDecoder.h" |
| 40 | 40 |
| 41 #include "platform/image-decoders/png/PNGImageReader.h" | |
| 41 #include "png.h" | 42 #include "png.h" |
| 42 #include "wtf/PtrUtil.h" | |
| 43 #include <memory> | 43 #include <memory> |
| 44 | 44 |
| 45 | |
| 45 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR) | 46 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR) |
| 46 #error version error: compile against a versioned libpng. | 47 #error version error: compile against a versioned libpng. |
| 47 #endif | 48 #endif |
| 48 #if USE(QCMSLIB) | 49 #if USE(QCMSLIB) |
| 49 #include "qcms.h" | 50 #include "qcms.h" |
| 50 #endif | 51 #endif |
| 51 | 52 |
| 52 #if PNG_LIBPNG_VER_MAJOR > 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MIN OR >= 4) | 53 #if PNG_LIBPNG_VER_MAJOR > 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MIN OR >= 4) |
| 53 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr) | 54 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr) |
| 54 #else | 55 #else |
| 55 #define JMPBUF(png_ptr) png_ptr->jmpbuf | 56 #define JMPBUF(png_ptr) png_ptr->jmpbuf |
| 56 #endif | 57 #endif |
| 57 | 58 |
| 58 namespace { | |
| 59 | |
| 60 inline blink::PNGImageDecoder* imageDecoder(png_structp png) | |
| 61 { | |
| 62 return static_cast<blink::PNGImageDecoder*>(png_get_progressive_ptr(png)); | |
| 63 } | |
| 64 | |
| 65 void PNGAPI pngHeaderAvailable(png_structp png, png_infop) | |
| 66 { | |
| 67 imageDecoder(png)->headerAvailable(); | |
| 68 } | |
| 69 | |
| 70 void PNGAPI pngRowAvailable(png_structp png, png_bytep row, png_uint_32 rowIndex , int state) | |
| 71 { | |
| 72 imageDecoder(png)->rowAvailable(row, rowIndex, state); | |
| 73 } | |
| 74 | |
| 75 void PNGAPI pngComplete(png_structp png, png_infop) | |
| 76 { | |
| 77 imageDecoder(png)->complete(); | |
| 78 } | |
| 79 | |
| 80 void PNGAPI pngFailed(png_structp png, png_const_charp) | |
| 81 { | |
| 82 longjmp(JMPBUF(png), 1); | |
| 83 } | |
| 84 | |
| 85 } // namespace | |
| 86 | 59 |
| 87 namespace blink { | 60 namespace blink { |
| 88 | 61 |
| 89 class PNGImageReader final { | |
| 90 USING_FAST_MALLOC(PNGImageReader); | |
| 91 WTF_MAKE_NONCOPYABLE(PNGImageReader); | |
| 92 public: | |
| 93 PNGImageReader(PNGImageDecoder* decoder, size_t readOffset) | |
| 94 : m_decoder(decoder) | |
| 95 , m_readOffset(readOffset) | |
| 96 , m_currentBufferSize(0) | |
| 97 , m_decodingSizeOnly(false) | |
| 98 , m_hasAlpha(false) | |
| 99 #if USE(QCMSLIB) | |
| 100 , m_rowBuffer() | |
| 101 #endif | |
| 102 { | |
| 103 m_png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, pngFailed, 0); | |
| 104 m_info = png_create_info_struct(m_png); | |
| 105 png_set_progressive_read_fn(m_png, m_decoder, pngHeaderAvailable, pngRow Available, pngComplete); | |
| 106 } | |
| 107 | |
| 108 ~PNGImageReader() | |
| 109 { | |
| 110 png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0); | |
| 111 ASSERT(!m_png && !m_info); | |
| 112 | |
| 113 m_readOffset = 0; | |
| 114 } | |
| 115 | |
| 116 bool decode(const SegmentReader& data, bool sizeOnly) | |
| 117 { | |
| 118 m_decodingSizeOnly = sizeOnly; | |
| 119 | |
| 120 // We need to do the setjmp here. Otherwise bad things will happen. | |
| 121 if (setjmp(JMPBUF(m_png))) | |
| 122 return m_decoder->setFailed(); | |
| 123 | |
| 124 const char* segment; | |
| 125 while (size_t segmentLength = data.getSomeData(segment, m_readOffset)) { | |
| 126 m_readOffset += segmentLength; | |
| 127 m_currentBufferSize = m_readOffset; | |
| 128 png_process_data(m_png, m_info, reinterpret_cast<png_bytep>(const_ca st<char*>(segment)), segmentLength); | |
| 129 if (sizeOnly ? m_decoder->isDecodedSizeAvailable() : m_decoder->fram eIsCompleteAtIndex(0)) | |
| 130 return true; | |
| 131 } | |
| 132 | |
| 133 return false; | |
| 134 } | |
| 135 | |
| 136 png_structp pngPtr() const { return m_png; } | |
| 137 png_infop infoPtr() const { return m_info; } | |
| 138 | |
| 139 size_t getReadOffset() const { return m_readOffset; } | |
| 140 void setReadOffset(size_t offset) { m_readOffset = offset; } | |
| 141 size_t currentBufferSize() const { return m_currentBufferSize; } | |
| 142 bool decodingSizeOnly() const { return m_decodingSizeOnly; } | |
| 143 void setHasAlpha(bool hasAlpha) { m_hasAlpha = hasAlpha; } | |
| 144 bool hasAlpha() const { return m_hasAlpha; } | |
| 145 | |
| 146 png_bytep interlaceBuffer() const { return m_interlaceBuffer.get(); } | |
| 147 void createInterlaceBuffer(int size) { m_interlaceBuffer = wrapArrayUnique(n ew png_byte[size]); } | |
| 148 #if USE(QCMSLIB) | |
| 149 png_bytep rowBuffer() const { return m_rowBuffer.get(); } | |
| 150 void createRowBuffer(int size) { m_rowBuffer = wrapArrayUnique(new png_byte[ size]); } | |
| 151 #endif | |
| 152 | |
| 153 private: | |
| 154 png_structp m_png; | |
| 155 png_infop m_info; | |
| 156 PNGImageDecoder* m_decoder; | |
| 157 size_t m_readOffset; | |
| 158 size_t m_currentBufferSize; | |
| 159 bool m_decodingSizeOnly; | |
| 160 bool m_hasAlpha; | |
| 161 std::unique_ptr<png_byte[]> m_interlaceBuffer; | |
| 162 #if USE(QCMSLIB) | |
| 163 std::unique_ptr<png_byte[]> m_rowBuffer; | |
| 164 #endif | |
| 165 }; | |
| 166 | |
| 167 PNGImageDecoder::PNGImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOp tion colorOptions, size_t maxDecodedBytes, size_t offset) | 62 PNGImageDecoder::PNGImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOp tion colorOptions, size_t maxDecodedBytes, size_t offset) |
| 168 : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes) | 63 : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes) |
| 169 , m_offset(offset) | 64 , m_offset(offset) |
| 65 , m_frameCountDecoded(false) | |
| 66 , m_frameCount(0) | |
| 67 , m_repetitionCount(cAnimationNone) | |
| 170 { | 68 { |
| 171 } | 69 } |
| 172 | 70 |
| 173 PNGImageDecoder::~PNGImageDecoder() | 71 PNGImageDecoder::~PNGImageDecoder() |
| 174 { | 72 { |
| 175 } | 73 } |
| 176 | 74 |
| 75 size_t PNGImageDecoder::decodeFrameCount() | |
| 76 { | |
| 77 if (m_frameCountDecoded) | |
| 78 return m_frameCount; | |
| 79 | |
| 80 parse(PNGParseQuery::PNGFrameCountQuery); | |
| 81 // Similar reasoning to GIFImageDecodeer. If decoding fails, we don't | |
| 82 // return 0 but we return the existing number of frames. This prevents us | |
| 83 // from reporting zero frames when decoding fails halfway through the image. | |
| 84 return failed() ? m_frameBufferCache.size() : m_frameCount; | |
| 85 } | |
| 86 | |
| 87 inline bool isComplete(const PNGImageDecoder* decoder) | |
| 88 { | |
| 89 return decoder->frameIsCompleteAtIndex(0); | |
| 90 } | |
| 91 | |
| 92 void PNGImageDecoder::parse(PNGParseQuery query) | |
| 93 { | |
| 94 if (failed()) | |
| 95 return; | |
| 96 | |
| 97 if (!m_reader) | |
| 98 m_reader = wrapUnique(new PNGImageReader(this, m_offset)); | |
| 99 | |
| 100 if (!m_reader->parse(*m_data, query)) | |
| 101 setFailed(); | |
| 102 | |
| 103 if (isComplete(this) || failed()) | |
| 104 m_reader.reset(); | |
| 105 | |
| 106 } | |
| 107 | |
| 108 bool PNGImageDecoder::isDecodedFrameCountAvailable() const | |
| 109 { | |
| 110 return !failed() && m_frameCountDecoded; | |
| 111 } | |
| 112 | |
| 113 void PNGImageDecoder::animationControlAvailable(size_t numFrames, size_t numRepe titions) | |
| 114 { | |
| 115 m_frameCount = numFrames; | |
| 116 m_frameCountDecoded = true; | |
| 117 m_repetitionCount = (numRepetitions == 0) ? cAnimationLoopInfinite : numRepe titions; | |
| 118 } | |
| 119 | |
| 120 int PNGImageDecoder::repetitionCount() const | |
| 121 { | |
| 122 return failed() ? cAnimationLoopOnce : m_repetitionCount; | |
|
scroggo_chromium
2016/10/04 14:50:17
This should perhaps be a FIXME regarding crbug.com
joostouwerling
2016/10/11 16:31:06
Acknowledged.
| |
| 123 } | |
| 124 | |
| 177 void PNGImageDecoder::headerAvailable() | 125 void PNGImageDecoder::headerAvailable() |
| 178 { | 126 { |
| 179 png_structp png = m_reader->pngPtr(); | 127 png_structp png = m_reader->pngPtr(); |
| 180 png_infop info = m_reader->infoPtr(); | 128 png_infop info = m_reader->infoPtr(); |
| 181 png_uint_32 width = png_get_image_width(png, info); | 129 png_uint_32 width = png_get_image_width(png, info); |
| 182 png_uint_32 height = png_get_image_height(png, info); | 130 png_uint_32 height = png_get_image_height(png, info); |
| 183 | 131 |
| 184 // Protect against large PNGs. See http://bugzil.la/251381 for more details. | 132 // Protect against large PNGs. See http://bugzil.la/251381 for more details. |
| 185 const unsigned long maxPNGSize = 1000000UL; | 133 const unsigned long maxPNGSize = 1000000UL; |
| 186 if (width > maxPNGSize || height > maxPNGSize) { | 134 if (width > maxPNGSize || height > maxPNGSize) { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 m_reader->setReadOffset(m_reader->currentBufferSize() - png_process_data _pause(png, 0)); | 226 m_reader->setReadOffset(m_reader->currentBufferSize() - png_process_data _pause(png, 0)); |
| 279 #else | 227 #else |
| 280 m_reader->setReadOffset(m_reader->currentBufferSize() - png->buffer_size ); | 228 m_reader->setReadOffset(m_reader->currentBufferSize() - png->buffer_size ); |
| 281 png->buffer_size = 0; | 229 png->buffer_size = 0; |
| 282 #endif | 230 #endif |
| 283 } | 231 } |
| 284 } | 232 } |
| 285 | 233 |
| 286 void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, unsigned rowIndex, int) | 234 void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, unsigned rowIndex, int) |
| 287 { | 235 { |
| 236 // If we see a frame, but the frame count has not yet been decoded, we are | |
| 237 // dealing with a non animated PNG. | |
| 238 if (!m_frameCountDecoded) { | |
| 239 m_frameCountDecoded = true; | |
| 240 m_frameCount = 1; | |
| 241 } | |
| 242 | |
| 288 if (m_frameBufferCache.isEmpty()) | 243 if (m_frameBufferCache.isEmpty()) |
| 289 return; | 244 return; |
| 290 | 245 |
| 291 // Initialize the framebuffer if needed. | 246 // Initialize the framebuffer if needed. |
| 292 ImageFrame& buffer = m_frameBufferCache[0]; | 247 ImageFrame& buffer = m_frameBufferCache[0]; |
| 293 if (buffer.getStatus() == ImageFrame::FrameEmpty) { | 248 if (buffer.getStatus() == ImageFrame::FrameEmpty) { |
| 294 png_structp png = m_reader->pngPtr(); | 249 png_structp png = m_reader->pngPtr(); |
| 295 if (!buffer.setSizeAndColorProfile(size().width(), size().height(), colo rProfile())) { | 250 if (!buffer.setSizeAndColorProfile(size().width(), size().height(), colo rProfile())) { |
| 296 longjmp(JMPBUF(png), 1); | 251 longjmp(JMPBUF(png), 1); |
| 297 return; | 252 return; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 410 } | 365 } |
| 411 | 366 |
| 412 void PNGImageDecoder::complete() | 367 void PNGImageDecoder::complete() |
| 413 { | 368 { |
| 414 if (m_frameBufferCache.isEmpty()) | 369 if (m_frameBufferCache.isEmpty()) |
| 415 return; | 370 return; |
| 416 | 371 |
| 417 m_frameBufferCache[0].setStatus(ImageFrame::FrameComplete); | 372 m_frameBufferCache[0].setStatus(ImageFrame::FrameComplete); |
| 418 } | 373 } |
| 419 | 374 |
| 420 inline bool isComplete(const PNGImageDecoder* decoder) | |
| 421 { | |
| 422 return decoder->frameIsCompleteAtIndex(0); | |
| 423 } | |
| 424 | |
| 425 void PNGImageDecoder::decode(bool onlySize) | |
| 426 { | |
| 427 if (failed()) | |
| 428 return; | |
| 429 | |
| 430 if (!m_reader) | |
| 431 m_reader = wrapUnique(new PNGImageReader(this, m_offset)); | |
| 432 | |
| 433 // If we couldn't decode the image but have received all the data, decoding | |
| 434 // has failed. | |
| 435 if (!m_reader->decode(*m_data, onlySize) && isAllDataReceived()) | |
| 436 setFailed(); | |
| 437 | |
| 438 // If decoding is done or failed, we don't need the PNGImageReader anymore. | |
| 439 if (isComplete(this) || failed()) | |
| 440 m_reader.reset(); | |
| 441 } | |
| 442 | |
| 443 } // namespace blink | 375 } // namespace blink |
| OLD | NEW |