Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1123)

Unified Diff: third_party/WebKit/Source/platform/image-decoders/png/PNGImageDecoder.cpp

Issue 2386453003: WIP: Implement APNG (Closed)
Patch Set: Implement frame meta data decoding, include tests Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..234cce7bd3e4a44df2defd3ab8d1b4bf64a4bdf0 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,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)
+ return m_frameCount;
scroggo_chromium 2016/10/11 20:13:09 If we failed, does this get set to m_frame_bufferC
joostouwerling 2016/10/12 20:49:46 I'm not sure this answers completely what you're g
joostouwerling 2016/10/13 18:50:22 Now that I've been thinking about his, I don't thi
+
+ parse(PNGParseQuery::PNGMetaDataQuery);
+ // 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)
+void PNGImageDecoder::decode(size_t index)
{
- longjmp(JMPBUF(png), 1);
+ m_reader->decode(*m_data, index);
scroggo_chromium 2016/10/11 20:13:09 Are we guaranteed to have an m_reader here?
joostouwerling 2016/10/12 20:49:46 m_reader is not deleted by the decoder anymore. T
scroggo_chromium 2016/10/13 13:49:32 I believe that is correct, but it is fragile. Note
}
-} // 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);
+void PNGImageDecoder::parse(PNGParseQuery query)
+{
+ if (failed())
+ return;
- m_readOffset = 0;
- }
+ if (!m_reader)
+ m_reader = wrapUnique(new PNGImageReader(this, m_offset));
- bool decode(const SegmentReader& data, bool sizeOnly)
+ if (!m_reader->parse(*m_data, query) && isAllDataReceived())
{
scroggo_chromium 2016/10/11 20:13:09 Curly braces go on their own line for methods, but
joostouwerling 2016/10/12 20:49:46 Ack, that was due to debugging.
- 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;
+ SkDebugf("Setting failed in PNGImageDecoder::parse");
scroggo_chromium 2016/10/11 20:13:09 You'll need to remove this before submitting.
+ setFailed();
}
- png_structp pngPtr() const { return m_png; }
- png_infop infoPtr() const { return m_info; }
-
- 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; }
-
- 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
+ : repetitionCount;
+}
-PNGImageDecoder::PNGImageDecoder(AlphaOption alphaOption, GammaAndColorProfileOption colorOptions, size_t maxDecodedBytes, size_t offset)
- : ImageDecoder(alphaOption, colorOptions, maxDecodedBytes)
- , m_offset(offset)
+// FIXME(joostouwerling): crbug.com/267883
scroggo_chromium 2016/10/11 20:13:09 It is good to have the bug as a reference, but it
+int PNGImageDecoder::repetitionCount() const
{
+ return failed() ? cAnimationLoopOnce : m_repetitionCount;
}
-PNGImageDecoder::~PNGImageDecoder()
+void PNGImageDecoder::initializeNewFrame(size_t index)
{
+ const auto& frameInfo = m_reader->frameInfo(index);
scroggo_chromium 2016/10/11 20:13:09 I'm not convinced using "auto" here helps readabil
+ auto buffer = &m_frameBufferCache[index];
scroggo_chromium 2016/10/11 20:13:09 If we use "auto" here, I think it should be "auto*
+
+ IntRect frameRect(frameInfo.offset, frameInfo.size);
+ IntRect frameRectWithinSize = intersection(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

Powered by Google App Engine
This is Rietveld 408576698