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

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

Issue 2386453003: WIP: Implement APNG (Closed)
Patch Set: Create m_reader on construction and remove |m_offset| Created 4 years 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 11 matching lines...) Expand all
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #ifndef PNGImageReader_h 26 #ifndef PNGImageReader_h
27 #define PNGImageReader_h 27 #define PNGImageReader_h
28 28
29 #include "platform/image-decoders/png/PNGImageDecoder.h" 29 #include "platform/image-decoders/png/PNGImageDecoder.h"
30 #include "png.h" 30 #include "png.h"
31 31
32 namespace blink {
33
34 class SegmentReader;
35
32 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR) 36 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR)
33 #error version error: compile against a versioned libpng. 37 #error version error: compile against a versioned libpng.
34 #endif 38 #endif
35 39
36 #if PNG_LIBPNG_VER_MAJOR > 1 || \ 40 #if PNG_LIBPNG_VER_MAJOR > 1 || \
37 (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 4) 41 (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 4)
38 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr) 42 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr)
39 #else 43 #else
40 #define JMPBUF(png_ptr) png_ptr->jmpbuf 44 #define JMPBUF(png_ptr) png_ptr->jmpbuf
41 #endif 45 #endif
42 46
43 namespace blink {
44
45 class SegmentReader;
46
47 class PLATFORM_EXPORT PNGImageReader final { 47 class PLATFORM_EXPORT PNGImageReader final {
48 USING_FAST_MALLOC(PNGImageReader); 48 USING_FAST_MALLOC(PNGImageReader);
49 WTF_MAKE_NONCOPYABLE(PNGImageReader); 49 WTF_MAKE_NONCOPYABLE(PNGImageReader);
50 50
51 public: 51 public:
52 PNGImageReader(PNGImageDecoder*, size_t offset); 52 PNGImageReader(PNGImageDecoder*, size_t initialOffset);
53 ~PNGImageReader(); 53 ~PNGImageReader();
54 54
55 bool decode(const SegmentReader&, bool sizeOnly); 55 struct FrameInfo {
56 // The offset where the frame data of this frame starts.
57 size_t startOffset;
58 // The number of bytes that contain frame data, starting at startOffset.
59 size_t byteLength;
60 size_t duration;
61 IntRect frameRect;
62 uint8_t disposalMethod;
63 uint8_t alphaBlend;
64 };
65
66 // This method tries to parse the data stream in the SegmentReader, up to
67 // the point where the PNGParseQuery can be answered.
68 // @return true when the query is fulfilled.
69 // false when:
70 // A) not enough data provided
71 // B) the data processing by libpng fails. In this case, it also
72 // calls setFailed() on m_decoder.
73 bool parse(SegmentReader&, PNGImageDecoder::PNGParseQuery);
74
75 void decode(SegmentReader&, size_t);
76 const FrameInfo& frameInfo(size_t) const;
77
78 // This will return the number of frames that have been detected in the
79 // stream so far. If an incomplete stream is provided, this number is not
80 // final. However, all frame info for the frames up to this number is
81 // available so it can be used by the client if it wants to.
82 size_t frameCount() const;
83
84 // This is called by the the libpng callback that handles unknown chunks.
85 void parseAnimationChunk(const char* tag,
86 const void* dataChunk,
87 size_t length);
88
89 // |m_parseCompleted| becomes true when
90 // a) the png is determined to be non-animated, if no acTL chunk is found
91 // before the IDAT chunk, or
92 // b) when the IEND chunk is seen for animated pngs.
93 bool parseCompleted() const { return m_parseCompleted; };
94 void setParseCompleted(bool completed) { m_parseCompleted = completed; };
scroggo_chromium 2016/12/08 13:59:09 nit: this is only called with true, and I think it
joostouwerling 2016/12/08 16:28:39 Done.
95
96 // This method indicates for *animated* PNGs whether the first frame is fully
97 // received. That is, when the fcTL chunk of the next frame has been received.
98 bool firstFrameFullyReceived() const;
99
100 // This method is called when a frame with status ImageFrame::FramePartial
101 // gets cleared in PNGImageDecoder.
102 //
103 // For |frameIndex| > 0, no action is necessary, since each decode call will
104 // decode the frame completely from start to end.
105 //
106 // For |frameIndex| == 0, |m_progressiveDecodeOffset| will be set to 0, to
107 // make sure progressive decoding is started from the beginning of the frame.
108 void clearDecodeState(size_t frameIndex);
109
56 png_structp pngPtr() const { return m_png; } 110 png_structp pngPtr() const { return m_png; }
57 png_infop infoPtr() const { return m_info; } 111 png_infop infoPtr() const { return m_info; }
58 112
59 size_t getReadOffset() const { return m_readOffset; }
60 void setReadOffset(size_t offset) { m_readOffset = offset; }
61 size_t currentBufferSize() const { return m_currentBufferSize; }
62 bool decodingSizeOnly() const { return m_decodingSizeOnly; }
63 void setHasAlpha(bool hasAlpha) { m_hasAlpha = hasAlpha; }
64 bool hasAlpha() const { return m_hasAlpha; }
65
66 png_bytep interlaceBuffer() const { return m_interlaceBuffer.get(); } 113 png_bytep interlaceBuffer() const { return m_interlaceBuffer.get(); }
67 void createInterlaceBuffer(int size) { 114 void createInterlaceBuffer(int size) {
68 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]); 115 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]);
69 } 116 }
117 void clearInterlaceBuffer() { m_interlaceBuffer.reset(); }
70 118
71 private: 119 private:
72 png_structp m_png; 120 png_structp m_png;
73 png_infop m_info; 121 png_infop m_info;
74 PNGImageDecoder* m_decoder; 122 PNGImageDecoder* m_decoder;
123 // This indicates at what offset in the stream the PNG image starts.
124 const size_t m_initialOffset;
125 // This is used to keep track of how many bytes we've read during parsing.
75 size_t m_readOffset; 126 size_t m_readOffset;
76 size_t m_currentBufferSize; 127
77 bool m_decodingSizeOnly; 128 // This variable is used to do progressive decoding for the first frame. It
78 bool m_hasAlpha; 129 // keeps track of how many bytes have been processed so far.
130 size_t m_progressiveDecodeOffset;
131 // The offset where IDAT starts. This is used to re-use the complete set of
132 // info/header chunks when mimicking a png image for a frame.
133 size_t m_idatOffset;
134 // This flag is set to true when an fcTL chunk is encountered before the
135 // IDAT chunk(s). In that case, the IDAT data is the first frame. Otherwise,
136 // the IDAT is ignored for an animated image.
137 bool m_idatIsPartOfAnimation;
138 bool m_isAnimated;
139 bool m_parsedSignature;
140 bool m_parseCompleted;
141
79 std::unique_ptr<png_byte[]> m_interlaceBuffer; 142 std::unique_ptr<png_byte[]> m_interlaceBuffer;
143
144 Vector<FrameInfo, 1> m_frameInfo;
145 // This is used to temporarily store frame information, until it is pushed to
146 // |m_frameInfo|, when all frame data has been seen in the stream.
147 FrameInfo m_newFrame;
148
149 size_t processData(SegmentReader&, size_t offset, size_t length);
150 bool parseSize(SegmentReader&);
151 void parseFrameInfo(const png_byte* data);
152 void resetPNGStruct();
153 void startFrameDecoding(SegmentReader&, size_t);
154 // @return true if the complete frame has been decoded
155 // false otherwise
156 bool progressivelyDecodeFirstFrame(SegmentReader&);
157 void decodeFrame(SegmentReader&, size_t);
158 void endFrameDecoding();
159 };
80 }; 160 };
81 161
82 } // namespace blink
83
84 #endif 162 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698