Index: third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoder.cpp |
diff --git a/third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoder.cpp b/third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoder.cpp |
index dff41e392ebff0c2226f28f98a7fb5d92c734c52..14f9ac3533b7baa2524f809facd4a20239888698 100644 |
--- a/third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoder.cpp |
+++ b/third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoder.cpp |
@@ -38,10 +38,11 @@ |
#include "platform/image-decoders/png/PNGImageDecoder.h" |
+#include "platform/image-decoders/png/PNGImageReader.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 |
@@ -55,123 +56,70 @@ |
#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)); |
-} |
+namespace blink { |
-void PNGAPI pngHeaderAvailable(png_structp png, png_infop) |
+PNGImageDecoder::PNGImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOption colorOptions, size_t maxDecodedBytes, size_t offset) |
+ : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes) |
+ , m_offset(offset) |
+ , m_frameCountDecoded(false) |
+ , m_frameCount(0) |
+ , m_repetitionCount(cAnimationNone) |
{ |
- imageDecoder(png)->headerAvailable(); |
} |
-void PNGAPI pngRowAvailable(png_structp png, png_bytep row, png_uint_32 rowIndex, int state) |
+PNGImageDecoder::~PNGImageDecoder() |
{ |
- imageDecoder(png)->rowAvailable(row, rowIndex, state); |
} |
-void PNGAPI pngComplete(png_structp png, png_infop) |
+size_t PNGImageDecoder::decodeFrameCount() |
{ |
- imageDecoder(png)->complete(); |
+ if (m_frameCountDecoded) |
+ return m_frameCount; |
+ |
+ parse(PNGParseQuery::PNGFrameCountQuery); |
+ // Similar reasoning to GIFImageDecodeer. If decoding fails, we don't |
+ // return 0 but we return the existing number of frames. This prevents us |
+ // from reporting zero frames when decoding fails halfway through the image. |
+ return failed() ? m_frameBufferCache.size() : m_frameCount; |
} |
-void PNGAPI pngFailed(png_structp png, png_const_charp) |
+inline bool isComplete(const PNGImageDecoder* decoder) |
{ |
- longjmp(JMPBUF(png), 1); |
+ return decoder->frameIsCompleteAtIndex(0); |
} |
-} // namespace |
- |
-namespace blink { |
- |
-class PNGImageReader final { |
- USING_FAST_MALLOC(PNGImageReader); |
- WTF_MAKE_NONCOPYABLE(PNGImageReader); |
-public: |
- 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); |
- } |
- |
- ~PNGImageReader() |
- { |
- png_destroy_read_struct(m_png ? &m_png : 0, m_info ? &m_info : 0, 0); |
- ASSERT(!m_png && !m_info); |
- |
- m_readOffset = 0; |
- } |
- |
- bool decode(const SegmentReader& data, bool sizeOnly) |
- { |
- m_decodingSizeOnly = sizeOnly; |
- |
- // We need to do the setjmp here. Otherwise bad things will happen. |
- 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 (sizeOnly ? m_decoder->isDecodedSizeAvailable() : m_decoder->frameIsCompleteAtIndex(0)) |
- return true; |
- } |
+void PNGImageDecoder::parse(PNGParseQuery query) |
+{ |
+ if (failed()) |
+ return; |
- return false; |
- } |
+ if (!m_reader) |
+ m_reader = wrapUnique(new PNGImageReader(this, m_offset)); |
- png_structp pngPtr() const { return m_png; } |
- png_infop infoPtr() const { return m_info; } |
+ if (!m_reader->parse(*m_data, query)) |
+ setFailed(); |
- size_t getReadOffset() const { return m_readOffset; } |
- void setReadOffset(size_t offset) { m_readOffset = offset; } |
- size_t currentBufferSize() const { return m_currentBufferSize; } |
- bool decodingSizeOnly() const { return m_decodingSizeOnly; } |
- void setHasAlpha(bool hasAlpha) { m_hasAlpha = hasAlpha; } |
- bool hasAlpha() const { return m_hasAlpha; } |
+ if (isComplete(this) || failed()) |
+ m_reader.reset(); |
- png_bytep interlaceBuffer() const { return m_interlaceBuffer.get(); } |
- void createInterlaceBuffer(int size) { m_interlaceBuffer = wrapArrayUnique(new png_byte[size]); } |
-#if USE(QCMSLIB) |
- png_bytep rowBuffer() const { return m_rowBuffer.get(); } |
- void createRowBuffer(int size) { m_rowBuffer = wrapArrayUnique(new png_byte[size]); } |
-#endif |
+} |
-private: |
- png_structp m_png; |
- png_infop m_info; |
- PNGImageDecoder* m_decoder; |
- size_t m_readOffset; |
- size_t m_currentBufferSize; |
- bool m_decodingSizeOnly; |
- bool m_hasAlpha; |
- std::unique_ptr<png_byte[]> m_interlaceBuffer; |
-#if USE(QCMSLIB) |
- std::unique_ptr<png_byte[]> m_rowBuffer; |
-#endif |
-}; |
+bool PNGImageDecoder::isDecodedFrameCountAvailable() const |
+{ |
+ return !failed() && m_frameCountDecoded; |
+} |
-PNGImageDecoder::PNGImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOption colorOptions, size_t maxDecodedBytes, size_t offset) |
- : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes) |
- , m_offset(offset) |
+void PNGImageDecoder::animationControlAvailable(size_t numFrames, size_t numRepetitions) |
{ |
+ m_frameCount = numFrames; |
+ m_frameCountDecoded = true; |
+ m_repetitionCount = (numRepetitions == 0) ? cAnimationLoopInfinite : numRepetitions; |
} |
-PNGImageDecoder::~PNGImageDecoder() |
+int PNGImageDecoder::repetitionCount() const |
{ |
+ 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.
|
} |
void PNGImageDecoder::headerAvailable() |
@@ -285,6 +233,13 @@ void PNGImageDecoder::headerAvailable() |
void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, unsigned rowIndex, int) |
{ |
+ // If we see a frame, but the frame count has not yet been decoded, we are |
+ // dealing with a non animated PNG. |
+ if (!m_frameCountDecoded) { |
+ m_frameCountDecoded = true; |
+ m_frameCount = 1; |
+ } |
+ |
if (m_frameBufferCache.isEmpty()) |
return; |
@@ -417,27 +372,4 @@ void PNGImageDecoder::complete() |
m_frameBufferCache[0].setStatus(ImageFrame::FrameComplete); |
} |
-inline bool isComplete(const PNGImageDecoder* decoder) |
-{ |
- return decoder->frameIsCompleteAtIndex(0); |
-} |
- |
-void PNGImageDecoder::decode(bool onlySize) |
-{ |
- if (failed()) |
- return; |
- |
- if (!m_reader) |
- m_reader = wrapUnique(new PNGImageReader(this, m_offset)); |
- |
- // If we couldn't decode the image but have received all the data, decoding |
- // has failed. |
- if (!m_reader->decode(*m_data, onlySize) && isAllDataReceived()) |
- setFailed(); |
- |
- // If decoding is done or failed, we don't need the PNGImageReader anymore. |
- if (isComplete(this) || failed()) |
- m_reader.reset(); |
-} |
- |
} // namespace blink |