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