OLD | NEW |
---|---|
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 // These constants map to the frame control parameters defined in fcTL chunks, | |
40 // as specified at https://wiki.mozilla.org/APNG_Specification. | |
41 const uint8_t kAPNGAlphaBlendBgcolor = 0; | |
42 const uint8_t kAPNGAlphaBlendPreviousFrame = 1; | |
43 const uint8_t kAPNGDisposeKeep = 0; | |
scroggo_chromium
2016/12/12 14:52:44
nit: Maybe leave a blank line in between the Blend
| |
44 const uint8_t kAPNGDisposeBgcolor = 1; | |
45 const uint8_t kAPNGDisposePrevious = 2; | |
46 | |
47 class SegmentReader; | |
48 class PNGImageDecoder; | |
31 | 49 |
32 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR) | 50 #if !defined(PNG_LIBPNG_VER_MAJOR) || !defined(PNG_LIBPNG_VER_MINOR) |
33 #error version error: compile against a versioned libpng. | 51 #error version error: compile against a versioned libpng. |
34 #endif | 52 #endif |
35 | 53 |
36 #if PNG_LIBPNG_VER_MAJOR > 1 || \ | 54 #if PNG_LIBPNG_VER_MAJOR > 1 || \ |
37 (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 4) | 55 (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 4) |
38 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr) | 56 #define JMPBUF(png_ptr) png_jmpbuf(png_ptr) |
39 #else | 57 #else |
40 #define JMPBUF(png_ptr) png_ptr->jmpbuf | 58 #define JMPBUF(png_ptr) png_ptr->jmpbuf |
41 #endif | 59 #endif |
42 | 60 |
43 namespace blink { | |
44 | |
45 class SegmentReader; | |
46 | |
47 class PLATFORM_EXPORT PNGImageReader final { | 61 class PLATFORM_EXPORT PNGImageReader final { |
48 USING_FAST_MALLOC(PNGImageReader); | 62 USING_FAST_MALLOC(PNGImageReader); |
49 WTF_MAKE_NONCOPYABLE(PNGImageReader); | 63 WTF_MAKE_NONCOPYABLE(PNGImageReader); |
50 | 64 |
51 public: | 65 public: |
52 PNGImageReader(PNGImageDecoder*, size_t offset); | 66 PNGImageReader(PNGImageDecoder*, size_t initialOffset); |
53 ~PNGImageReader(); | 67 ~PNGImageReader(); |
54 | 68 |
55 bool decode(const SegmentReader&, bool sizeOnly); | 69 struct FrameInfo { |
70 // The offset where the frame data of this frame starts. | |
71 size_t startOffset; | |
72 // The number of bytes that contain frame data, starting at startOffset. | |
73 size_t byteLength; | |
74 size_t duration; | |
75 IntRect frameRect; | |
76 uint8_t disposalMethod; | |
77 uint8_t alphaBlend; | |
78 }; | |
79 | |
80 enum class PNGParseQuery { PNGSizeQuery, PNGMetaDataQuery }; | |
81 | |
82 // This method tries to parse the data stream in the SegmentReader, up to | |
83 // the point where the PNGParseQuery can be answered. | |
84 // @return true when the query is fulfilled. | |
85 // false when: | |
86 // A) not enough data provided | |
87 // B) the data processing by libpng fails. In this case, it also | |
88 // calls setFailed() on m_decoder. | |
89 bool parse(SegmentReader&, PNGParseQuery); | |
90 | |
91 void decode(SegmentReader&, size_t); | |
92 const FrameInfo& frameInfo(size_t) const; | |
93 | |
94 // This will return the number of frames that have been detected in the | |
95 // stream so far. If an incomplete stream is provided, this number is not | |
96 // final. However, all frame info for the frames up to this number is | |
97 // available so it can be used by the client if it wants to. | |
98 size_t frameCount() const; | |
99 | |
100 // This is called by the the libpng callback that handles unknown chunks. | |
101 void parseAnimationChunk(const char* tag, | |
102 const void* dataChunk, | |
103 size_t length); | |
104 | |
105 // |m_parseCompleted| becomes true when | |
106 // a) the png is determined to be non-animated, if no acTL chunk is found | |
107 // before the IDAT chunk, or | |
108 // b) when the IEND chunk is seen for animated pngs. | |
109 bool parseCompleted() const { return m_parseCompleted; }; | |
110 void setParseCompleted() { m_parseCompleted = true; }; | |
111 | |
112 // This method indicates for *animated* PNGs whether the first frame is fully | |
113 // received. That is, when the fcTL chunk of the next frame has been received. | |
114 bool firstFrameFullyReceived() const; | |
115 | |
116 // This method is called when a frame with status ImageFrame::FramePartial | |
117 // gets cleared in PNGImageDecoder. | |
118 // | |
119 // For |frameIndex| > 0, no action is necessary, since each decode call will | |
120 // decode the frame completely from start to end. | |
121 // | |
122 // For |frameIndex| == 0, |m_progressiveDecodeOffset| will be set to 0, to | |
123 // make sure progressive decoding is started from the beginning of the frame. | |
124 void clearDecodeState(size_t frameIndex); | |
125 | |
56 png_structp pngPtr() const { return m_png; } | 126 png_structp pngPtr() const { return m_png; } |
57 png_infop infoPtr() const { return m_info; } | 127 png_infop infoPtr() const { return m_info; } |
58 | 128 |
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(); } | 129 png_bytep interlaceBuffer() const { return m_interlaceBuffer.get(); } |
67 void createInterlaceBuffer(int size) { | 130 void createInterlaceBuffer(int size) { |
68 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]); | 131 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]); |
69 } | 132 } |
133 void clearInterlaceBuffer() { m_interlaceBuffer.reset(); } | |
70 | 134 |
71 private: | 135 private: |
72 png_structp m_png; | 136 png_structp m_png; |
73 png_infop m_info; | 137 png_infop m_info; |
74 PNGImageDecoder* m_decoder; | 138 PNGImageDecoder* m_decoder; |
139 // This indicates at what offset in the stream the PNG image starts. | |
140 const size_t m_initialOffset; | |
141 // This is used to keep track of how many bytes we've read during parsing. | |
75 size_t m_readOffset; | 142 size_t m_readOffset; |
76 size_t m_currentBufferSize; | 143 |
77 bool m_decodingSizeOnly; | 144 // This variable is used to do progressive decoding for the first frame. It |
78 bool m_hasAlpha; | 145 // keeps track of how many bytes have been processed so far. |
146 size_t m_progressiveDecodeOffset; | |
147 // The offset where IDAT starts. This is used to re-use the complete set of | |
148 // info/header chunks when mimicking a png image for a frame. | |
149 size_t m_idatOffset; | |
150 // This flag is set to true when an fcTL chunk is encountered before the | |
151 // IDAT chunk(s). In that case, the IDAT data is the first frame. Otherwise, | |
152 // the IDAT is ignored for an animated image. | |
153 bool m_idatIsPartOfAnimation; | |
154 bool m_isAnimated; | |
155 bool m_parsedSignature; | |
156 bool m_parseCompleted; | |
157 | |
79 std::unique_ptr<png_byte[]> m_interlaceBuffer; | 158 std::unique_ptr<png_byte[]> m_interlaceBuffer; |
159 | |
160 Vector<FrameInfo, 1> m_frameInfo; | |
161 // This is used to temporarily store frame information, until it is pushed to | |
162 // |m_frameInfo|, when all frame data has been seen in the stream. | |
163 FrameInfo m_newFrame; | |
164 | |
165 size_t processData(SegmentReader&, size_t offset, size_t length); | |
166 bool parseSize(SegmentReader&); | |
167 void parseFrameInfo(const png_byte* data); | |
168 void resetPNGStruct(); | |
169 void startFrameDecoding(SegmentReader&, size_t); | |
170 // @return true if the complete frame has been decoded | |
171 // false otherwise | |
172 bool progressivelyDecodeFirstFrame(SegmentReader&); | |
173 void decodeFrame(SegmentReader&, size_t); | |
174 void endFrameDecoding(); | |
175 }; | |
80 }; | 176 }; |
81 | 177 |
82 } // namespace blink | |
83 | |
84 #endif | 178 #endif |
OLD | NEW |