OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions |
| 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. |
| 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 24 */ |
| 25 |
| 26 |
| 27 #include "platform/image-decoders/SegmentReader.h" |
| 28 #include "platform/image-decoders/png/PNGImageDecoder.h" |
| 29 #include "platform/PlatformExport.h" |
| 30 #include "png.h" |
| 31 #include "wtf/Allocator.h" |
| 32 #include "wtf/PtrUtil.h" |
| 33 |
| 34 #ifndef PNGImageReader_h |
| 35 #define PNGImageReader_h |
| 36 |
| 37 namespace blink { |
| 38 |
| 39 class FastSharedBufferReader; |
| 40 |
| 41 class PLATFORM_EXPORT PNGImageReader final { |
| 42 USING_FAST_MALLOC(PNGImageReader); |
| 43 WTF_MAKE_NONCOPYABLE(PNGImageReader); |
| 44 |
| 45 public: |
| 46 PNGImageReader(PNGImageDecoder*, size_t initialOffset); |
| 47 ~PNGImageReader(); |
| 48 |
| 49 struct FrameInfo { |
| 50 // The offset where the frame data of this frame starts. |
| 51 size_t startOffset; |
| 52 // The number of bytes that contain frame data, starting at startOffset. |
| 53 size_t byteLength; |
| 54 size_t duration; |
| 55 IntRect frameRect; |
| 56 uint8_t disposalMethod; |
| 57 uint8_t alphaBlend; |
| 58 }; |
| 59 |
| 60 |
| 61 // This method tries to parse the data stream in the SegmentReader, up to |
| 62 // the point where the PNGParseQuery can be answered. |
| 63 // @return true when the query is fulfilled. |
| 64 // false when: |
| 65 // A) not enough data provided |
| 66 // B) the data processing by libpng fails. In this case, it also |
| 67 // calls setFailed() on m_decoder. |
| 68 bool parse(SegmentReader&, PNGImageDecoder::PNGParseQuery); |
| 69 |
| 70 void decode(SegmentReader&, size_t); |
| 71 const FrameInfo& frameInfo(size_t) const; |
| 72 |
| 73 // This will return the number of frames that have been detected in the |
| 74 // stream so far. If an incomplete stream is provided, this number is not |
| 75 // final. However, all frame info for the frames up to this number is |
| 76 // available so it can be used by the client if it wants to. |
| 77 size_t frameCount() const; |
| 78 |
| 79 // This is a callback for libpng, when it encounters an unknown chunk. |
| 80 void parseAnimationChunk(const char* tag, const void* dataChunk, size_t length
); |
| 81 |
| 82 // |m_parseCompleted| becomes true when |
| 83 // a) the png is determined to be non-animated, if no acTL chunk is found |
| 84 // before the IDAT chunk, or |
| 85 // b) when the IEND chunk is seen for animated pngs. |
| 86 bool parseCompleted() const { return m_parseCompleted; }; |
| 87 |
| 88 // This method is called when a frame with status ImageFrame::FramePartial |
| 89 // gets cleared in PNGImageDecoder. |
| 90 // |
| 91 // For |frameIndex| > 0, no action is necessary, since each decode call will |
| 92 // decode the frame completely from start to end. |
| 93 // |
| 94 // For |frameIndex| == 0, |m_progressiveDecodeOffset| will be set to 0, to |
| 95 // make sure progressive decoding is started from the beginning of the frame. |
| 96 void clearDecodeState(size_t frameIndex); |
| 97 |
| 98 png_structp pngPtr() const { return m_png; } |
| 99 png_infop infoPtr() const { return m_info; } |
| 100 |
| 101 png_bytep interlaceBuffer() const { return m_interlaceBuffer.get(); } |
| 102 void createInterlaceBuffer(int size) { |
| 103 m_interlaceBuffer = wrapArrayUnique(new png_byte[size]); |
| 104 } |
| 105 void clearInterlaceBuffer() { m_interlaceBuffer.reset(); } |
| 106 |
| 107 private: |
| 108 png_structp m_png; |
| 109 png_infop m_info; |
| 110 PNGImageDecoder* m_decoder; |
| 111 // This indicates at what offset in the stream the PNG image starts. |
| 112 const size_t m_initialOffset; |
| 113 // This is used to keep track of how many bytes we've read during parsing. |
| 114 size_t m_readOffset; |
| 115 |
| 116 // This variable is used to do progressive decoding for the first frame. It |
| 117 // keeps track of how many bytes have been processed so far. |
| 118 size_t m_progressiveDecodeOffset; |
| 119 // The offset where IDAT starts. This is used to re-use the complete set of |
| 120 // info/header chunks when mimicking a png image for a frame. |
| 121 size_t m_idatOffset; |
| 122 // This flag is set to true when an fcTL chunk is encountered before the |
| 123 // IDAT chunk(s). In that case, the IDAT data is the first frame. Otherwise, |
| 124 // the IDAT is ignored for an animated image. |
| 125 bool m_idatIsPartOfAnimation; |
| 126 bool m_isAnimated; |
| 127 bool m_parsedSignature; |
| 128 bool m_parseCompleted; |
| 129 |
| 130 std::unique_ptr<png_byte[]> m_interlaceBuffer; |
| 131 |
| 132 Vector<FrameInfo, 1> m_frameInfo; |
| 133 // This is used to temporarily store frame information, until it is pushed to |
| 134 // |m_frameInfo|, when all frame data has been seen in the stream. |
| 135 FrameInfo m_newFrame; |
| 136 |
| 137 size_t processData(SegmentReader&, size_t offset, size_t length); |
| 138 bool parseSize(SegmentReader&); |
| 139 void parseFrameInfo(const png_byte* data); |
| 140 void startFrameDecoding(SegmentReader&, size_t); |
| 141 // @return true if the complete frame has been decoded |
| 142 // false otherwise |
| 143 bool progressivelyDecodeFirstFrame(SegmentReader&); |
| 144 void decodeFrame(SegmentReader&, size_t); |
| 145 void endFrameDecoding(); |
| 146 |
| 147 }; |
| 148 |
| 149 }; |
| 150 |
| 151 #endif |
OLD | NEW |