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..83814fd03828bb8b4a6347f85c242e4943f12cb2 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> |
+ |
cblume
2016/10/15 00:30:50
nit: extra line?
|
#if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR) |
#error version error: compile against a versioned libpng. |
#endif |
@@ -55,123 +56,80 @@ |
#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_metaDataDecoded(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_metaDataDecoded) |
+ parse(PNGParseQuery::PNGMetaDataQuery); |
+ |
+ return m_frameCount; |
} |
-void PNGAPI pngFailed(png_structp png, png_const_charp) |
+inline bool frameComplete(ImageFrame& frame) |
{ |
- longjmp(JMPBUF(png), 1); |
+ return frame.getStatus() == ImageFrame::FrameComplete; |
} |
-} // 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; |
- } |
+void PNGImageDecoder::decode(size_t index) |
+{ |
- 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; |
- } |
+} |
- return false; |
- } |
+void PNGImageDecoder::parse(PNGParseQuery query) |
+{ |
+ if (failed()) |
+ return; |
- png_structp pngPtr() const { return m_png; } |
- png_infop infoPtr() const { return m_info; } |
+ if (!m_reader) |
+ m_reader = wrapUnique(new PNGImageReader(this, m_offset)); |
- 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 (!m_reader->parse(*m_data, query) && isAllDataReceived()) |
+ setFailed(); |
- 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 |
+ if (query == PNGParseQuery::PNGMetaDataQuery) |
+ m_frameCount = m_reader->frameCount(); |
+} |
-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 |
-}; |
+void PNGImageDecoder::setRepetitionCount(size_t repetitionCount) |
+{ |
+ m_repetitionCount = (repetitionCount == 0) ? cAnimationLoopInfinite |
cblume
2016/10/15 00:30:50
Is this part of the spec, 0 means repeat forever i
joostouwerling
2016/10/24 22:26:49
Yes, that is part of the spec. I've reasoned here
|
+ : repetitionCount; |
+} |
-PNGImageDecoder::PNGImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOption colorOptions, size_t maxDecodedBytes, size_t offset) |
- : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes) |
- , m_offset(offset) |
+// This matches the existing behavior to loop once if decoding fails, but this |
+// should be changed to stick with m_repititionCount to match other browsers. |
+// See crbug.com/267883 |
+int PNGImageDecoder::repetitionCount() const |
{ |
+ return failed() ? cAnimationLoopOnce : m_repetitionCount; |
} |
-PNGImageDecoder::~PNGImageDecoder() |
+void PNGImageDecoder::initializeNewFrame(size_t index) |
{ |
+ const PNGImageReader::FrameInfo& frameInfo = m_reader->frameInfo(index); |
+ ImageFrame* buffer = &m_frameBufferCache[index]; |
+ |
+ IntRect frameRectWithinSize = intersection(frameInfo.frameRect, |
+ {IntPoint(), size()}); |
+ buffer->setOriginalFrameRect(frameRectWithinSize); |
+ buffer->setDuration(frameInfo.duration); |
+ buffer->setDisposalMethod(frameInfo.disposalMethod); |
+ buffer->setAlphaBlendSource(frameInfo.alphaBlend); |
} |
void PNGImageDecoder::headerAvailable() |
@@ -270,21 +228,11 @@ void PNGImageDecoder::headerAvailable() |
ASSERT(channels == 3 || channels == 4); |
m_reader->setHasAlpha(channels == 4); |
- |
- if (m_reader->decodingSizeOnly()) { |
- // If we only needed the size, halt the reader. |
-#if PNG_LIBPNG_VER_MAJOR > 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 5) |
- // '0' argument to png_process_data_pause means: Do not cache unprocessed data. |
- m_reader->setReadOffset(m_reader->currentBufferSize() - png_process_data_pause(png, 0)); |
-#else |
- m_reader->setReadOffset(m_reader->currentBufferSize() - png->buffer_size); |
- png->buffer_size = 0; |
-#endif |
- } |
} |
void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, unsigned rowIndex, int) |
{ |
+ |
if (m_frameBufferCache.isEmpty()) |
return; |
@@ -413,31 +361,6 @@ void PNGImageDecoder::complete() |
{ |
if (m_frameBufferCache.isEmpty()) |
return; |
- |
- 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 |