| 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 GIFImageDecoder_h | 26 #ifndef GIFImageDecoder_h |
| 27 #define GIFImageDecoder_h | 27 #define GIFImageDecoder_h |
| 28 | 28 |
| 29 #include "platform/image-decoders/ImageDecoder.h" | 29 #include "platform/image-decoders/ImageDecoder.h" |
| 30 #include "third_party/skia/include/codec/SkCodec.h" |
| 31 #include "third_party/skia/include/core/SkStream.h" |
| 30 #include "wtf/Noncopyable.h" | 32 #include "wtf/Noncopyable.h" |
| 33 #include "wtf/RefPtr.h" |
| 34 #include <algorithm> |
| 31 #include <memory> | 35 #include <memory> |
| 32 | 36 |
| 33 class GIFImageReader; | |
| 34 | |
| 35 typedef Vector<unsigned char> GIFRow; | |
| 36 | |
| 37 namespace blink { | 37 namespace blink { |
| 38 | 38 |
| 39 // This class decodes the GIF image format. | 39 // This class decodes the GIF image format. |
| 40 class PLATFORM_EXPORT GIFImageDecoder final : public ImageDecoder { | 40 class PLATFORM_EXPORT GIFImageDecoder final : public ImageDecoder { |
| 41 WTF_MAKE_NONCOPYABLE(GIFImageDecoder); | 41 WTF_MAKE_NONCOPYABLE(GIFImageDecoder); |
| 42 | 42 |
| 43 public: | 43 public: |
| 44 GIFImageDecoder(AlphaOption, const ColorBehavior&, size_t maxDecodedBytes); | 44 GIFImageDecoder(AlphaOption, const ColorBehavior&, size_t maxDecodedBytes); |
| 45 ~GIFImageDecoder() override; | 45 ~GIFImageDecoder() override; |
| 46 | 46 |
| 47 enum GIFParseQuery { GIFSizeQuery, GIFFrameCountQuery }; | |
| 48 | |
| 49 // ImageDecoder: | 47 // ImageDecoder: |
| 50 String filenameExtension() const override { return "gif"; } | 48 String filenameExtension() const override { return "gif"; } |
| 51 void onSetData(SegmentReader* data) override; | 49 void onSetData(SegmentReader* data) override; |
| 52 int repetitionCount() const override; | 50 int repetitionCount() const override; |
| 53 bool frameIsCompleteAtIndex(size_t) const override; | 51 bool frameIsCompleteAtIndex(size_t) const override; |
| 54 float frameDurationAtIndex(size_t) const override; | 52 float frameDurationAtIndex(size_t) const override; |
| 55 // CAUTION: setFailed() deletes |m_reader|. Be careful to avoid | |
| 56 // accessing deleted memory, especially when calling this from inside | |
| 57 // GIFImageReader! | |
| 58 bool setFailed() override; | |
| 59 | |
| 60 // Callbacks from the GIF reader. | |
| 61 bool haveDecodedRow(size_t frameIndex, | |
| 62 GIFRow::const_iterator rowBegin, | |
| 63 size_t width, | |
| 64 size_t rowNumber, | |
| 65 unsigned repeatCount, | |
| 66 bool writeTransparentPixels); | |
| 67 bool frameComplete(size_t frameIndex); | |
| 68 | |
| 69 // For testing. | |
| 70 bool parseCompleted() const; | |
| 71 | 53 |
| 72 private: | 54 private: |
| 73 // ImageDecoder: | 55 // ImageDecoder: |
| 74 void clearFrameBuffer(size_t frameIndex) override; | 56 void decodeSize() override {} |
| 75 virtual void decodeSize() { parse(GIFSizeQuery); } | |
| 76 size_t decodeFrameCount() override; | 57 size_t decodeFrameCount() override; |
| 77 void initializeNewFrame(size_t) override; | 58 void initializeNewFrame(size_t) override; |
| 78 void decode(size_t) override; | 59 void decode(size_t) override; |
| 79 | 60 bool frameStatusSufficientForSuccessors(size_t index) { |
| 80 // Parses as much as is needed to answer the query, ignoring bitmap | 61 DCHECK(index < m_frameBufferCache.size()); |
| 81 // data. If parsing fails, sets the "decode failure" flag. | 62 return m_frameBufferCache[index].getStatus() == ImageFrame::FrameComplete; |
| 82 void parse(GIFParseQuery); | 63 } |
| 83 | |
| 84 // Reset the alpha tracker for this frame. Before calling this method, the | |
| 85 // caller must verify that the frame exists. | |
| 86 void onInitFrameBuffer(size_t) override; | |
| 87 | |
| 88 // When the disposal method of the frame is DisposeOverWritePrevious, the | 64 // When the disposal method of the frame is DisposeOverWritePrevious, the |
| 89 // next frame will use the previous frame's buffer as its starting state, so | 65 // next frame will use the previous frame's buffer as its starting state, so |
| 90 // we can't take over the data in that case. Before calling this method, the | 66 // we can't take over the data in that case. Before calling this method, the |
| 91 // caller must verify that the frame exists. | 67 // caller must verify that the frame exists. |
| 92 bool canReusePreviousFrameBuffer(size_t) const override; | 68 bool canReusePreviousFrameBuffer(size_t) const override; |
| 93 | 69 |
| 94 bool m_currentBufferSawAlpha; | 70 class SegmentStream : public SkStream { |
| 95 mutable int m_repetitionCount; | 71 public: |
| 96 std::unique_ptr<GIFImageReader> m_reader; | 72 SegmentStream() : m_reader(), m_position(0), m_hasReadAllContents(true) {} |
| 73 |
| 74 void setReader(SegmentReader* reader, bool allContentsReceived) { |
| 75 m_reader = reader; |
| 76 m_hasReadAllContents = reader->size() == m_position; |
| 77 } |
| 78 |
| 79 size_t read(void* dst, size_t len) { |
| 80 len = std::min(len, m_reader->size()); |
| 81 |
| 82 size_t bytesAdvanced = 0; |
| 83 if (!dst) { // skipping, not reading |
| 84 bytesAdvanced = len; |
| 85 } else { |
| 86 bytesAdvanced = peek(dst, len); |
| 87 } |
| 88 |
| 89 m_position += bytesAdvanced; |
| 90 m_hasReadAllContents = m_position == m_reader->size(); |
| 91 |
| 92 return bytesAdvanced; |
| 93 } |
| 94 |
| 95 size_t peek(void* buffer, size_t size) const override { |
| 96 size = std::min(size, m_reader->size()); |
| 97 |
| 98 size_t peekPosition = m_position; |
| 99 size_t totalBytesPeeked = 0; |
| 100 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer); |
| 101 while (size) { |
| 102 const char* segment = nullptr; |
| 103 size_t bytesPeeked = m_reader->getSomeData(segment, peekPosition); |
| 104 if (!bytesPeeked) { |
| 105 break; |
| 106 } |
| 107 if (bytesPeeked > size) { |
| 108 bytesPeeked = size; |
| 109 } |
| 110 |
| 111 memcpy(bufferAsCharPtr, segment, bytesPeeked); |
| 112 bufferAsCharPtr += bytesPeeked; |
| 113 size -= bytesPeeked; |
| 114 totalBytesPeeked += bytesPeeked; |
| 115 peekPosition += bytesPeeked; |
| 116 } |
| 117 |
| 118 return totalBytesPeeked; |
| 119 } |
| 120 |
| 121 bool isAtEnd() const override { return m_hasReadAllContents; } |
| 122 |
| 123 bool rewind() override { |
| 124 m_position = 0; |
| 125 return true; |
| 126 } |
| 127 |
| 128 bool hasPosition() const override { return true; } |
| 129 size_t getPosition() const override { return m_position; } |
| 130 |
| 131 bool seek(size_t position) override { |
| 132 position = std::min(position, m_reader->size()); |
| 133 m_position = position; |
| 134 |
| 135 return true; |
| 136 } |
| 137 |
| 138 bool move(long offset) override { |
| 139 long absolutePosition = m_position + offset; |
| 140 |
| 141 // clamp inside the bounds of the buffer size |
| 142 absolutePosition = std::max(absolutePosition, 0l); |
| 143 absolutePosition = |
| 144 std::min(static_cast<size_t>(absolutePosition), m_reader->size()); |
| 145 |
| 146 m_position = absolutePosition; |
| 147 |
| 148 return true; |
| 149 } |
| 150 |
| 151 bool hasLength() const override { return true; } |
| 152 size_t getLength() const override { return m_reader->size(); } |
| 153 |
| 154 private: |
| 155 WTF::RefPtr<SegmentReader> m_reader; |
| 156 size_t m_position; |
| 157 bool m_hasReadAllContents; |
| 158 }; |
| 159 |
| 160 std::unique_ptr<SkCodec> m_codec; |
| 161 SegmentStream* m_segmentStream; |
| 162 // m_segmentStream is a raw pointer because it passes ownership to |
| 163 // m_codec when m_codec is created. However, we still need the |
| 164 // reference so we can append more data as it arrives. |
| 97 }; | 165 }; |
| 98 | 166 |
| 99 } // namespace blink | 167 } // namespace blink |
| 100 | 168 |
| 101 #endif | 169 #endif |
| OLD | NEW |