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

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

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

Powered by Google App Engine
This is Rietveld 408576698