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

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

Issue 2618633004: Add support for Animated PNG (Closed)
Patch Set: Rebase Created 3 years, 9 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 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/PlatformExport.h" 29 #include "platform/PlatformExport.h"
30 #include "platform/geometry/IntRect.h"
31 #include "platform/image-decoders/ImageFrame.h"
30 #include "png.h" 32 #include "png.h"
31 #include "wtf/Allocator.h" 33 #include "wtf/Allocator.h"
32 #include "wtf/PtrUtil.h" 34 #include "wtf/PtrUtil.h"
35 #include "wtf/Vector.h"
33 36
34 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR) 37 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR)
35 #error version error: compile against a versioned libpng. 38 #error version error: compile against a versioned libpng.
36 #endif 39 #endif
37 40
38 #if PNG_LIBPNG_VER_MAJOR > 1 || \ 41 #if PNG_LIBPNG_VER_MAJOR > 1 || \
39 (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 4) 42 (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 4)
40 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr) 43 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr)
41 #else 44 #else
42 #define JMPBUF(png_ptr) png_ptr->jmpbuf 45 #define JMPBUF(png_ptr) png_ptr->jmpbuf
43 #endif 46 #endif
44 47
45 namespace blink { 48 namespace blink {
46 49
50 class FastSharedBufferReader;
47 class PNGImageDecoder; 51 class PNGImageDecoder;
48 class SegmentReader; 52 class SegmentReader;
49 53
50 class PLATFORM_EXPORT PNGImageReader final { 54 class PLATFORM_EXPORT PNGImageReader final {
51 USING_FAST_MALLOC(PNGImageReader); 55 USING_FAST_MALLOC(PNGImageReader);
52 WTF_MAKE_NONCOPYABLE(PNGImageReader); 56 WTF_MAKE_NONCOPYABLE(PNGImageReader);
53 57
54 public: 58 public:
55 PNGImageReader(PNGImageDecoder*, size_t offset); 59 PNGImageReader(PNGImageDecoder*, size_t initialOffset);
56 ~PNGImageReader(); 60 ~PNGImageReader();
57 61
58 bool decode(const SegmentReader&, bool sizeOnly); 62 struct FrameInfo {
63 // The offset where the frame data of this frame starts.
64 size_t startOffset;
65 // The number of bytes that contain frame data, starting at startOffset.
66 size_t byteLength;
67 size_t duration;
68 IntRect frameRect;
69 ImageFrame::DisposalMethod disposalMethod;
70 ImageFrame::AlphaBlendSource alphaBlend;
71 };
72
73 enum class ParseQuery { Size, MetaData };
74
75 bool parse(SegmentReader&, ParseQuery);
76
77 // Returns false on a fatal error.
78 bool decode(SegmentReader&, size_t);
79 const FrameInfo& frameInfo(size_t) const;
80
81 // Number of complete frames parsed so far, included frame zero if it is
82 // partial.
83 size_t frameCount() const;
Noel Gordon 2017/03/06 05:21:48 Minor comment fix, we should just in-line this fun
scroggo_chromium 2017/03/07 20:25:38 Done.
84
85 bool parseCompleted() const { return m_parseCompleted; };
86
87 // This method indicates for *animated* PNGs whether the first frame is fully
88 // received. That is, when the fcTL chunk of the next frame has been received.
89 bool firstFrameFullyReceived() const;
Noel Gordon 2017/03/06 05:21:48 In-line this function? To do that, you could add
scroggo_chromium 2017/03/07 20:25:38 Done.
90
91 void clearDecodeState(size_t frameIndex);
92
59 png_structp pngPtr() const { return m_png; } 93 png_structp pngPtr() const { return m_png; }
60 png_infop infoPtr() const { return m_info; } 94 png_infop infoPtr() const { return m_info; }
61 95
62 size_t getReadOffset() const { return m_readOffset; }
63 void setReadOffset(size_t offset) { m_readOffset = offset; }
64 size_t currentBufferSize() const { return m_currentBufferSize; }
65 bool decodingSizeOnly() const { return m_decodingSizeOnly; }
66 void setHasAlpha(bool hasAlpha) { m_hasAlpha = hasAlpha; }
67 bool hasAlpha() const { return m_hasAlpha; }
68
69 png_bytep interlaceBuffer() const { return m_interlaceBuffer.get(); } 96 png_bytep interlaceBuffer() const { return m_interlaceBuffer.get(); }
70 void createInterlaceBuffer(int size) { 97 void createInterlaceBuffer(int size) {
71 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]); 98 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]);
72 } 99 }
100 void clearInterlaceBuffer() { m_interlaceBuffer.reset(); }
73 101
74 private: 102 private:
75 png_structp m_png; 103 png_structp m_png;
76 png_infop m_info; 104 png_infop m_info;
105 png_uint_32 m_width;
106 png_uint_32 m_height;
107
77 PNGImageDecoder* m_decoder; 108 PNGImageDecoder* m_decoder;
109
110 // The offset in the stream where the PNG image starts.
111 const size_t m_initialOffset;
112 // How many bytes have been read during parsing.
78 size_t m_readOffset; 113 size_t m_readOffset;
79 size_t m_currentBufferSize; 114 size_t m_progressiveDecodeOffset;
80 bool m_decodingSizeOnly; 115 size_t m_idatOffset;
81 bool m_hasAlpha; 116
117 bool m_idatIsPartOfAnimation;
118 // All IDAT chunks must precede the first fdAT chunk, and all fdAT chunks
119 // should be separated from the IDAT chunks by an fcTL chunk. So this is true
120 // until the first fcTL chunk after an IDAT chunk. After that, only fdAT
121 // chunks are expected.
122 bool m_expectIdats;
123 bool m_isAnimated;
124 bool m_parsedSignature;
125 bool m_parsedIHDR;
126 bool m_parseCompleted;
127 uint32_t m_reportedFrameCount;
128 uint32_t m_nextSequenceNumber;
129 // True when an fcTL has been parsed but not its corresponding fdAT or IDAT
130 // chunk. Consecutive fcTLs is an error.
131 bool m_fctlNeedsDatChunk;
132 bool m_ignoreAnimation;
133
82 std::unique_ptr<png_byte[]> m_interlaceBuffer; 134 std::unique_ptr<png_byte[]> m_interlaceBuffer;
135
136 Vector<FrameInfo, 1> m_frameInfo;
137 // This is used to temporarily store frame information, until it is pushed to
138 // |m_frameInfo|, when all frame data has been seen in the stream.
139 FrameInfo m_newFrame;
140
141 size_t processData(const FastSharedBufferReader&,
142 size_t offset,
143 size_t length);
144 // Returns false on a fatal error.
145 bool parseSize(const FastSharedBufferReader&);
146 // Returns false on an error.
147 bool parseFrameInfo(const png_byte* data);
148 void startFrameDecoding(const FastSharedBufferReader&, size_t);
149 // Returns whether the frame was completely decoded.
150 bool progressivelyDecodeFirstFrame(const FastSharedBufferReader&);
151 void decodeFrame(const FastSharedBufferReader&, size_t);
152 void processFdatChunkAsIdat(png_uint_32 fdatLength);
153 // Returns false on a fatal error.
154 bool checkSequenceNumber(const png_byte* position);
83 }; 155 };
84 156
85 } // namespace blink 157 } // namespace blink
86 158
87 #endif 159 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698