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 |
(...skipping 11 matching lines...) Expand all Loading... | |
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 a callback for libpng, when it encounters an unknown chunk. | |
scroggo_chromium
2016/12/07 13:44:44
nit: technically this is called by the callback, n
joostouwerling
2016/12/07 16:49:10
Done.
| |
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 | |
95 // This method indicates for *animated* PNGs whether the first frame is fully | |
96 // received. That is, when the fcTL chunk of the next frame has been received. | |
97 bool firstFrameFullyReceived() const; | |
98 | |
99 // This method is called when a frame with status ImageFrame::FramePartial | |
100 // gets cleared in PNGImageDecoder. | |
101 // | |
102 // For |frameIndex| > 0, no action is necessary, since each decode call will | |
103 // decode the frame completely from start to end. | |
104 // | |
105 // For |frameIndex| == 0, |m_progressiveDecodeOffset| will be set to 0, to | |
106 // make sure progressive decoding is started from the beginning of the frame. | |
107 void clearDecodeState(size_t frameIndex); | |
108 | |
56 png_structp pngPtr() const { return m_png; } | 109 png_structp pngPtr() const { return m_png; } |
57 png_infop infoPtr() const { return m_info; } | 110 png_infop infoPtr() const { return m_info; } |
58 | 111 |
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(); } | 112 png_bytep interlaceBuffer() const { return m_interlaceBuffer.get(); } |
67 void createInterlaceBuffer(int size) { | 113 void createInterlaceBuffer(int size) { |
68 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]); | 114 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]); |
69 } | 115 } |
116 void clearInterlaceBuffer() { m_interlaceBuffer.reset(); } | |
70 | 117 |
71 private: | 118 private: |
72 png_structp m_png; | 119 png_structp m_png; |
73 png_infop m_info; | 120 png_infop m_info; |
74 PNGImageDecoder* m_decoder; | 121 PNGImageDecoder* m_decoder; |
122 // This indicates at what offset in the stream the PNG image starts. | |
123 const size_t m_initialOffset; | |
124 // This is used to keep track of how many bytes we've read during parsing. | |
75 size_t m_readOffset; | 125 size_t m_readOffset; |
76 size_t m_currentBufferSize; | 126 |
77 bool m_decodingSizeOnly; | 127 // This variable is used to do progressive decoding for the first frame. It |
78 bool m_hasAlpha; | 128 // keeps track of how many bytes have been processed so far. |
129 size_t m_progressiveDecodeOffset; | |
130 // The offset where IDAT starts. This is used to re-use the complete set of | |
131 // info/header chunks when mimicking a png image for a frame. | |
132 size_t m_idatOffset; | |
133 // This flag is set to true when an fcTL chunk is encountered before the | |
134 // IDAT chunk(s). In that case, the IDAT data is the first frame. Otherwise, | |
135 // the IDAT is ignored for an animated image. | |
136 bool m_idatIsPartOfAnimation; | |
137 bool m_isAnimated; | |
138 bool m_parsedSignature; | |
139 bool m_parseCompleted; | |
140 | |
79 std::unique_ptr<png_byte[]> m_interlaceBuffer; | 141 std::unique_ptr<png_byte[]> m_interlaceBuffer; |
142 | |
143 Vector<FrameInfo, 1> m_frameInfo; | |
144 // This is used to temporarily store frame information, until it is pushed to | |
145 // |m_frameInfo|, when all frame data has been seen in the stream. | |
146 FrameInfo m_newFrame; | |
147 | |
148 size_t processData(SegmentReader&, size_t offset, size_t length); | |
149 bool parseSize(SegmentReader&); | |
150 void parseFrameInfo(const png_byte* data); | |
151 void resetPNGStructPreDecode(); | |
scroggo_chromium
2016/12/07 13:44:44
I find this name long and awkward, and I'm not sur
joostouwerling
2016/12/07 16:49:10
Done.
| |
152 void startFrameDecoding(SegmentReader&, size_t); | |
153 // @return true if the complete frame has been decoded | |
154 // false otherwise | |
155 bool progressivelyDecodeFirstFrame(SegmentReader&); | |
156 void decodeFrame(SegmentReader&, size_t); | |
157 void endFrameDecoding(); | |
158 }; | |
80 }; | 159 }; |
81 | 160 |
82 } // namespace blink | |
83 | |
84 #endif | 161 #endif |
OLD | NEW |