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 "codec/SkCodec.h" | |
scroggo_chromium
2016/12/13 16:57:37
I thought these were typically included as
#inclu
cblume
2016/12/14 09:16:56
Done.
| |
30 #include "core/SkStream.h" | |
29 #include "platform/image-decoders/ImageDecoder.h" | 31 #include "platform/image-decoders/ImageDecoder.h" |
30 #include "wtf/Noncopyable.h" | 32 #include "wtf/Noncopyable.h" |
31 #include <memory> | 33 #include <memory> |
32 | 34 |
33 class GIFImageReader; | |
34 | |
35 typedef Vector<unsigned char> GIFRow; | |
36 | |
37 namespace blink { | 35 namespace blink { |
38 | 36 |
39 // This class decodes the GIF image format. | 37 // This class decodes the GIF image format. |
40 class PLATFORM_EXPORT GIFImageDecoder final : public ImageDecoder { | 38 class PLATFORM_EXPORT GIFImageDecoder final : public ImageDecoder { |
41 WTF_MAKE_NONCOPYABLE(GIFImageDecoder); | 39 WTF_MAKE_NONCOPYABLE(GIFImageDecoder); |
42 | 40 |
43 public: | 41 public: |
44 GIFImageDecoder(AlphaOption, | 42 GIFImageDecoder(AlphaOption, |
45 ColorSpaceOption, | 43 ColorSpaceOption, |
46 sk_sp<SkColorSpace>, | 44 sk_sp<SkColorSpace>, |
47 size_t maxDecodedBytes); | 45 size_t maxDecodedBytes); |
48 ~GIFImageDecoder() override; | 46 ~GIFImageDecoder() override; |
49 | 47 |
50 enum GIFParseQuery { GIFSizeQuery, GIFFrameCountQuery }; | |
51 | |
52 // ImageDecoder: | 48 // ImageDecoder: |
53 String filenameExtension() const override { return "gif"; } | 49 String filenameExtension() const override { return "gif"; } |
54 void onSetData(SegmentReader* data) override; | 50 void onSetData(SegmentReader* data) override; |
55 int repetitionCount() const override; | 51 int repetitionCount() const override; |
56 bool frameIsCompleteAtIndex(size_t) const override; | 52 bool frameIsCompleteAtIndex(size_t) const override; |
57 float frameDurationAtIndex(size_t) const override; | 53 float frameDurationAtIndex(size_t) const override; |
58 // CAUTION: setFailed() deletes |m_reader|. Be careful to avoid | |
59 // accessing deleted memory, especially when calling this from inside | |
60 // GIFImageReader! | |
61 bool setFailed() override; | |
62 | |
63 // Callbacks from the GIF reader. | |
64 bool haveDecodedRow(size_t frameIndex, | |
65 GIFRow::const_iterator rowBegin, | |
66 size_t width, | |
67 size_t rowNumber, | |
68 unsigned repeatCount, | |
69 bool writeTransparentPixels); | |
70 bool frameComplete(size_t frameIndex); | |
71 | |
72 // For testing. | |
73 bool parseCompleted() const; | |
74 | 54 |
75 private: | 55 private: |
76 // ImageDecoder: | 56 // ImageDecoder: |
77 void clearFrameBuffer(size_t frameIndex) override; | 57 void decodeSize() override; |
78 virtual void decodeSize() { parse(GIFSizeQuery); } | |
79 size_t decodeFrameCount() override; | 58 size_t decodeFrameCount() override; |
80 void initializeNewFrame(size_t) override; | 59 void initializeNewFrame(size_t) override; |
81 void decode(size_t) override; | 60 void decode(size_t) override; |
82 | 61 |
83 // Parses as much as is needed to answer the query, ignoring bitmap | |
84 // data. If parsing fails, sets the "decode failure" flag. | |
85 void parse(GIFParseQuery); | |
86 | |
87 // Reset the alpha tracker for this frame. Before calling this method, the | |
88 // caller must verify that the frame exists. | |
89 void onInitFrameBuffer(size_t) override; | |
90 | |
91 // When the disposal method of the frame is DisposeOverWritePrevious, the | 62 // When the disposal method of the frame is DisposeOverWritePrevious, the |
92 // next frame will use the previous frame's buffer as its starting state, so | 63 // next frame will use the previous frame's buffer as its starting state, so |
93 // we can't take over the data in that case. Before calling this method, the | 64 // we can't take over the data in that case. Before calling this method, the |
94 // caller must verify that the frame exists. | 65 // caller must verify that the frame exists. |
95 bool canReusePreviousFrameBuffer(size_t) const override; | 66 bool canReusePreviousFrameBuffer(size_t) const override; |
96 | 67 |
97 bool m_currentBufferSawAlpha; | 68 class SegmentStream : public SkStream { |
scroggo_chromium
2016/12/13 16:57:37
Add a constructor to initialize m_isDone and m_pos
cblume
2016/12/14 09:16:57
Done.
| |
98 mutable int m_repetitionCount; | 69 public: |
99 std::unique_ptr<GIFImageReader> m_reader; | 70 void setReader(SegmentReader* reader, bool isDone) { |
71 m_reader.reset(reader); | |
72 m_isDone = isDone; | |
73 } | |
74 size_t read(void* dst, size_t len) { | |
75 if (!dst) { // skipping, not reading | |
76 m_position += len; | |
scroggo_chromium
2016/12/13 16:57:37
I think this should only advance to the end of the
cblume
2016/12/14 09:16:57
I agree. In fact, I should probably clamp len to a
| |
77 return len; | |
78 } | |
79 | |
80 size_t totalBytesRead = 0; | |
81 intptr_t destAsInt = reinterpret_cast<intptr_t>(dst); | |
82 while (len) { | |
83 const char* segment; | |
84 size_t bytesRead = m_reader->getSomeData(segment, len); | |
85 if (!bytesRead) { | |
86 return totalBytesRead; // or break; | |
87 } | |
88 if (bytesRead > len) { | |
89 bytesRead = len; | |
90 } | |
cblume
2016/12/14 09:16:57
I'm not sure we need to check if getSomeData() ret
scroggo_chromium
2016/12/14 17:49:23
getSomeData returning more than we asked for is th
cblume
2016/12/16 02:57:10
Done.
| |
91 memcpy(reinterpret_cast<void*>(destAsInt), segment, bytesRead); | |
92 destAsInt += bytesRead; | |
93 len -= bytesRead; | |
94 totalBytesRead += bytesRead; | |
95 } | |
96 return totalBytesRead; | |
97 } | |
98 | |
99 bool isAtEnd() const override { return m_isDone; } | |
scroggo_chromium
2016/12/13 16:57:37
What does isDone mean? The fact that it is passed
cblume
2016/12/14 09:16:57
Thank you for catching this.
I've fixed it to corr
| |
100 | |
101 private: | |
102 std::unique_ptr<SegmentReader> m_reader; | |
scroggo_chromium
2016/12/13 16:57:37
SegmentReader is a ref counted class. Don't you wa
cblume
2016/12/14 09:16:56
Done.
| |
103 size_t m_position; | |
104 bool m_isDone; | |
105 }; | |
106 | |
107 std::unique_ptr<SkCodec> m_codec; | |
108 std::unique_ptr<SegmentStream> m_segmentStream; | |
100 }; | 109 }; |
101 | 110 |
102 } // namespace blink | 111 } // namespace blink |
103 | 112 |
104 #endif | 113 #endif |
OLD | NEW |