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/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; includes frame 0 even if partial. |
| 82 size_t frameCount() const { return m_frameInfo.size(); } |
| 83 |
| 84 bool parseCompleted() const { return m_parseCompleted; }; |
| 85 |
| 86 bool frameIsReceivedAtIndex(size_t index) const { |
| 87 if (!index) |
| 88 return firstFrameFullyReceived(); |
| 89 return index < frameCount(); |
| 90 } |
| 91 |
| 92 void clearDecodeState(size_t); |
| 93 |
59 png_structp pngPtr() const { return m_png; } | 94 png_structp pngPtr() const { return m_png; } |
60 png_infop infoPtr() const { return m_info; } | 95 png_infop infoPtr() const { return m_info; } |
61 | 96 |
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(); } | 97 png_bytep interlaceBuffer() const { return m_interlaceBuffer.get(); } |
70 void createInterlaceBuffer(int size) { | 98 void createInterlaceBuffer(int size) { |
71 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]); | 99 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]); |
72 } | 100 } |
| 101 void clearInterlaceBuffer() { m_interlaceBuffer.reset(); } |
73 | 102 |
74 private: | 103 private: |
75 png_structp m_png; | 104 png_structp m_png; |
76 png_infop m_info; | 105 png_infop m_info; |
| 106 png_uint_32 m_width; |
| 107 png_uint_32 m_height; |
| 108 |
77 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. |
78 size_t m_readOffset; | 114 size_t m_readOffset; |
79 size_t m_currentBufferSize; | 115 size_t m_progressiveDecodeOffset; |
80 bool m_decodingSizeOnly; | 116 size_t m_idatOffset; |
81 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 |
82 std::unique_ptr<png_byte[]> m_interlaceBuffer; | 135 std::unique_ptr<png_byte[]> m_interlaceBuffer; |
| 136 |
| 137 // Value used for the byteLength of a FrameInfo struct to indicate that it is |
| 138 // the first frame and its byteLength is not yet known. 1 is a safe value |
| 139 // since the byteLength field of a frame is at least 12. |
| 140 static constexpr size_t kFirstFrameIndicator = 1; |
| 141 |
| 142 // Stores information about a frame until it can be pushed to |m_frameInfo| |
| 143 // once all the frame data has been read from the stream. |
| 144 FrameInfo m_newFrame; |
| 145 Vector<FrameInfo, 1> m_frameInfo; |
| 146 |
| 147 size_t processData(const FastSharedBufferReader&, |
| 148 size_t offset, |
| 149 size_t length); |
| 150 // Returns false on a fatal error. |
| 151 bool parseSize(const FastSharedBufferReader&); |
| 152 // Returns false on an error. |
| 153 bool parseFrameInfo(const png_byte* data); |
| 154 bool shouldDecodeWithNewPNG(size_t) const; |
| 155 void startFrameDecoding(const FastSharedBufferReader&, size_t); |
| 156 // Returns whether the frame was completely decoded. |
| 157 bool progressivelyDecodeFirstFrame(const FastSharedBufferReader&); |
| 158 void decodeFrame(const FastSharedBufferReader&, size_t); |
| 159 void processFdatChunkAsIdat(png_uint_32 fdatLength); |
| 160 // Returns false on a fatal error. |
| 161 bool checkSequenceNumber(const png_byte* position); |
| 162 bool firstFrameFullyReceived() const { |
| 163 return !m_frameInfo.isEmpty() && |
| 164 m_frameInfo[0].byteLength != kFirstFrameIndicator; |
| 165 } |
83 }; | 166 }; |
84 | 167 |
85 } // namespace blink | 168 } // namespace blink |
86 | 169 |
87 #endif | 170 #endif |
OLD | NEW |