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

Unified Diff: third_party/WebKit/Source/platform/image-decoders/png/PNGImageReader.h

Issue 2386453003: WIP: Implement APNG (Closed)
Patch Set: Process feedback on patch 12 Created 4 years, 1 month 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/PNGImageReader.h
diff --git a/third_party/WebKit/Source/platform/image-decoders/png/PNGImageReader.h b/third_party/WebKit/Source/platform/image-decoders/png/PNGImageReader.h
new file mode 100644
index 0000000000000000000000000000000000000000..284ee15ce0904faf2cdead25a826aef2cf7af696
--- /dev/null
+++ b/third_party/WebKit/Source/platform/image-decoders/png/PNGImageReader.h
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+#include "platform/image-decoders/SegmentReader.h"
+#include "platform/image-decoders/png/PNGImageDecoder.h"
+#include "platform/PlatformExport.h"
+#include "png.h"
+#include "wtf/Allocator.h"
+#include "wtf/PtrUtil.h"
+
+#ifndef PNGImageReader_h
+#define PNGImageReader_h
+
+namespace blink {
+
+class FastSharedBufferReader;
+
+class PLATFORM_EXPORT PNGImageReader final {
+ USING_FAST_MALLOC(PNGImageReader);
+ WTF_MAKE_NONCOPYABLE(PNGImageReader);
+
+ public:
+ PNGImageReader(PNGImageDecoder*, size_t initialOffset);
+ ~PNGImageReader();
+
+ struct FrameInfo {
+ // The offset where the frame data of this frame starts.
+ size_t startOffset;
+ // The number of bytes that contain frame data, starting at startOffset.
+ size_t byteLength;
+ size_t duration;
+ IntRect frameRect;
+ uint8_t disposalMethod;
+ uint8_t alphaBlend;
+ };
+
+
+ // This method tries to parse the data stream in the SegmentReader, up to
+ // the point where the PNGParseQuery can be answered.
+ // @return true when the query is fulfilled.
+ // false when:
+ // A) not enough data provided
+ // B) the data processing by libpng fails. In this case, it also
+ // calls setFailed() on m_decoder.
+ bool parse(SegmentReader&, PNGImageDecoder::PNGParseQuery);
+
+ void decode(SegmentReader&, size_t);
+ const FrameInfo& frameInfo(size_t) const;
+
+ // This will return the number of frames that have been detected in the
+ // stream so far. If an incomplete stream is provided, this number is not
+ // final. However, all frame info for the frames up to this number is
+ // available so it can be used by the client if it wants to.
+ size_t frameCount() const;
+
+ // This is a callback for libpng, when it encounters an unknown chunk.
+ void parseAnimationChunk(const char* tag, const void* dataChunk, size_t length);
+
+ // |m_parseCompleted| becomes true when
+ // a) the png is determined to be non-animated, if no acTL chunk is found, or
+ // b) when the IEND chunk is seen for animated pngs.
+ bool parseCompleted() { return m_parseCompleted; };
+
+ // This method is called when a frame with status ImageFrame::FramePartial
+ // gets cleared in PNGImageDecoder.
+ //
+ // For |frameIndex| > 0, no action is necessary, since each decode call will
+ // decode the frame completely from start to end.
+ //
+ // For |frameIndex| == 0, |m_progressiveDecodeOffset| will be set to 0, to
+ // make sure progressive decoding is started from the beginning of the frame.
+ void clearDecodeState(size_t frameIndex);
+
+ png_structp pngPtr() const { return m_png; }
+ png_infop infoPtr() const { return m_info; }
+
+ void setHasAlpha(bool hasAlpha) { m_hasAlpha = hasAlpha; }
+ bool hasAlpha() const { return m_hasAlpha; }
+
+ // This buffer is used for two use cases:
+ // 1) for images encoded with interlacing,
+ // 2) to store the pixel data while decoding non-first frames, so the decoder
scroggo_chromium 2016/11/09 13:42:51 No longer non-first frames - this is frames that d
joostouwerling 2016/11/11 20:22:19 This is removed.
+ // can write all rows at once to the frame buffer, in the frame complete
+ // callback. This prevents partially overwriting the contents of the
+ // previous frame.
+ png_bytep imageBuffer() const { return m_imageBuffer.get(); }
+ void createImageBuffer(int size) { m_imageBuffer = wrapArrayUnique(new png_byte[size]); }
+ void clearImageBuffer() { m_imageBuffer.reset(); }
+
+ private:
+ png_structp m_png;
+ png_infop m_info;
+ PNGImageDecoder* m_decoder;
+ // This indicates at what offset in the stream the PNG image starts.
+ const size_t m_initialOffset;
+ // This is used to keep track of how many bytes we've read during parsing.
+ size_t m_readOffset;
+
+ // This variable is used to do progressive decoding for the first frame. It
+ // keeps track of how many bytes have been processed so far.
+ size_t m_progressiveDecodeOffset;
+ // The offset where IDAT starts. This is used to re-use the complete set of
+ // info/header chunks when mimicking a png image for a frame.
+ size_t m_idatOffset;
+ bool m_hasAlpha;
+ // This flag is set to true when an fcTL chunk is encountered before the
+ // IDAT chunk(s). In that case, the IDAT data is the first frame. Otherwise,
+ // the IDAT is ignored.
+ bool m_idatIsPartOfAnimation;
+ bool m_isAnimated;
+ bool m_parsedSignature;
+ bool m_parseCompleted;
+
+ std::unique_ptr<png_byte[]> m_interlaceBuffer;
scroggo_chromium 2016/11/09 13:42:51 I think you no longer need this one?
joostouwerling 2016/11/11 20:22:19 In the revisited patch, it is :) But yeah, here it
+
+ Vector<FrameInfo, 1> m_frameInfo;
+ // This is used to temporarily store frame information, until it is pushed to
+ // |m_frameInfo|, when all frame data has been seen in the stream.
+ FrameInfo m_newFrame;
+
+ size_t processData(SegmentReader&, size_t offset, size_t length);
+ bool parseSize(SegmentReader&);
+ void parseFrameInfo(const png_byte* data);
+ void startFrameDecoding(SegmentReader&, size_t);
+ // @return true if the complete frame has been decoded
+ // false otherwise
+ bool progressivelyDecodeFirstFrame(SegmentReader&);
+ void decodeFrame(SegmentReader&, size_t);
+ void endFrameDecoding();
+
+ std::unique_ptr<png_byte[]> m_imageBuffer;
+};
+
+};
+
+#endif

Powered by Google App Engine
This is Rietveld 408576698