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

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

Issue 2386453003: WIP: Implement APNG (Closed)
Patch Set: Fix wrong alpha blend and disposal for static PNG 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
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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/PlatformExport.h"
30 #include "platform/geometry/IntRect.h"
30 #include "png.h" 31 #include "png.h"
32 #include "wtf/Allocator.h"
33 #include "wtf/Noncopyable.h"
34 #include "wtf/PtrUtil.h"
35 #include "wtf/Vector.h"
36
37 namespace blink {
38
39 class SegmentReader;
40 class PNGImageDecoder;
31 41
32 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR) 42 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR)
33 #error version error: compile against a versioned libpng. 43 #error version error: compile against a versioned libpng.
34 #endif 44 #endif
35 45
36 #if PNG_LIBPNG_VER_MAJOR > 1 || \ 46 #if PNG_LIBPNG_VER_MAJOR > 1 || \
37 (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 4) 47 (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 4)
38 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr) 48 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr)
39 #else 49 #else
40 #define JMPBUF(png_ptr) png_ptr->jmpbuf 50 #define JMPBUF(png_ptr) png_ptr->jmpbuf
41 #endif 51 #endif
42 52
43 namespace blink {
44
45 class SegmentReader;
46
47 class PLATFORM_EXPORT PNGImageReader final { 53 class PLATFORM_EXPORT PNGImageReader final {
48 USING_FAST_MALLOC(PNGImageReader); 54 USING_FAST_MALLOC(PNGImageReader);
49 WTF_MAKE_NONCOPYABLE(PNGImageReader); 55 WTF_MAKE_NONCOPYABLE(PNGImageReader);
50 56
51 public: 57 public:
52 PNGImageReader(PNGImageDecoder*, size_t offset); 58 PNGImageReader(PNGImageDecoder*, size_t initialOffset);
53 ~PNGImageReader(); 59 ~PNGImageReader();
54 60
55 bool decode(const SegmentReader&, bool sizeOnly); 61 struct FrameInfo {
62 // The offset where the frame data of this frame starts.
63 size_t startOffset;
64 // The number of bytes that contain frame data, starting at startOffset.
65 size_t byteLength;
66 size_t duration;
67 IntRect frameRect;
68 uint8_t disposalMethod;
69 uint8_t alphaBlend;
70 };
71
72 enum class PNGParseQuery { PNGSizeQuery, PNGMetaDataQuery };
73
74 // This method tries to parse the data stream in the SegmentReader, up to
75 // the point where the PNGParseQuery can be answered.
76 // @return true when the query is fulfilled.
77 // false when:
78 // A) not enough data provided
79 // B) the data processing by libpng fails. In this case, it also
80 // calls setFailed() on m_decoder.
81 bool parse(SegmentReader&, PNGParseQuery);
82
83 void decode(SegmentReader&, size_t);
84 const FrameInfo& frameInfo(size_t) const;
85
86 // This will return the number of frames that have been detected in the
87 // stream so far. If an incomplete stream is provided, this number is not
88 // final. However, all frame info for the frames up to this number is
89 // available so it can be used by the client if it wants to.
90 size_t frameCount() const;
91
92 // This is called by the the libpng callback that handles unknown chunks.
93 void parseAnimationChunk(const char* tag,
94 const void* dataChunk,
95 size_t length);
96
97 // |m_parseCompleted| becomes true when
98 // a) the png is determined to be non-animated, if no acTL chunk is found
99 // before the IDAT chunk, or
100 // b) when the IEND chunk is seen for animated pngs.
101 bool parseCompleted() const { return m_parseCompleted; };
102 void setParseCompleted() { m_parseCompleted = true; };
103
104 // This method indicates for *animated* PNGs whether the first frame is fully
105 // received. That is, when the fcTL chunk of the next frame has been received.
106 bool firstFrameFullyReceived() const;
107
108 // This method is called when a frame with status ImageFrame::FramePartial
109 // gets cleared in PNGImageDecoder.
110 //
111 // For |frameIndex| > 0, no action is necessary, since each decode call will
112 // decode the frame completely from start to end.
113 //
114 // For |frameIndex| == 0, |m_progressiveDecodeOffset| will be set to 0, to
115 // make sure progressive decoding is started from the beginning of the frame.
116 void clearDecodeState(size_t frameIndex);
117
56 png_structp pngPtr() const { return m_png; } 118 png_structp pngPtr() const { return m_png; }
57 png_infop infoPtr() const { return m_info; } 119 png_infop infoPtr() const { return m_info; }
58 120
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(); } 121 png_bytep interlaceBuffer() const { return m_interlaceBuffer.get(); }
67 void createInterlaceBuffer(int size) { 122 void createInterlaceBuffer(int size) {
68 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]); 123 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]);
69 } 124 }
125 void clearInterlaceBuffer() { m_interlaceBuffer.reset(); }
70 126
71 private: 127 private:
72 png_structp m_png; 128 png_structp m_png;
73 png_infop m_info; 129 png_infop m_info;
74 PNGImageDecoder* m_decoder; 130 PNGImageDecoder* m_decoder;
131 // This indicates at what offset in the stream the PNG image starts.
132 const size_t m_initialOffset;
133 // This is used to keep track of how many bytes we've read during parsing.
75 size_t m_readOffset; 134 size_t m_readOffset;
76 size_t m_currentBufferSize; 135
77 bool m_decodingSizeOnly; 136 // This variable is used to do progressive decoding for the first frame. It
78 bool m_hasAlpha; 137 // keeps track of how many bytes have been processed so far.
138 size_t m_progressiveDecodeOffset;
139 // The offset where IDAT starts. This is used to re-use the complete set of
140 // info/header chunks when mimicking a png image for a frame.
141 size_t m_idatOffset;
142 // This flag is set to true when an fcTL chunk is encountered before the
143 // IDAT chunk(s). In that case, the IDAT data is the first frame. Otherwise,
144 // the IDAT is ignored for an animated image.
145 bool m_idatIsPartOfAnimation;
146 bool m_isAnimated;
147 bool m_parsedSignature;
148 bool m_parseCompleted;
149
79 std::unique_ptr<png_byte[]> m_interlaceBuffer; 150 std::unique_ptr<png_byte[]> m_interlaceBuffer;
151
152 Vector<FrameInfo, 1> m_frameInfo;
153 // This is used to temporarily store frame information, until it is pushed to
154 // |m_frameInfo|, when all frame data has been seen in the stream.
155 FrameInfo m_newFrame;
156
157 size_t processData(SegmentReader&, size_t offset, size_t length);
158 bool parseSize(SegmentReader&);
159 void parseFrameInfo(const png_byte* data);
160 void resetPNGStruct();
161 void startFrameDecoding(SegmentReader&, size_t);
162 // @return true if the complete frame has been decoded
163 // false otherwise
164 bool progressivelyDecodeFirstFrame(SegmentReader&);
165 void decodeFrame(SegmentReader&, size_t);
166 void endFrameDecoding();
167 };
80 }; 168 };
81 169
82 } // namespace blink
83
84 #endif 170 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698