OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCodec.h" | 8 #include "SkCodec.h" |
| 9 #include "SkCodecAnimation.h" |
9 #include "SkColorSpace.h" | 10 #include "SkColorSpace.h" |
10 #include "SkColorTable.h" | 11 #include "SkColorTable.h" |
11 #include "SkImageInfo.h" | 12 #include "SkImageInfo.h" |
12 #include "SkSwizzler.h" | 13 #include "SkSwizzler.h" |
13 | 14 |
14 struct GifFileType; | 15 #include "GIFImageReader.h" |
15 struct SavedImage; | |
16 | 16 |
17 /* | 17 /* |
18 * | 18 * |
19 * This class implements the decoding for gif images | 19 * This class implements the decoding for gif images |
20 * | 20 * |
21 */ | 21 */ |
22 class SkGifCodec : public SkCodec { | 22 class SkGifCodec : public SkCodec { |
23 public: | 23 public: |
24 static bool IsGif(const void*, size_t); | 24 static bool IsGif(const void*, size_t); |
25 | 25 |
26 /* | 26 /* |
27 * Assumes IsGif was called and returned true | 27 * Assumes IsGif was called and returned true |
28 * Creates a gif decoder | 28 * Creates a gif decoder |
29 * Reads enough of the stream to determine the image format | 29 * Reads enough of the stream to determine the image format |
30 */ | 30 */ |
31 static SkCodec* NewFromStream(SkStream*); | 31 static SkCodec* NewFromStream(SkStream*); |
32 | 32 |
| 33 // Callback for GIFImageReader when a row is available. |
| 34 bool haveDecodedRow(size_t frameIndex, const unsigned char* rowBegin, |
| 35 size_t rowNumber, unsigned repeatCount, bool writeTransp
arentPixels); |
33 protected: | 36 protected: |
34 | |
35 /* | |
36 * Read enough of the stream to initialize the SkGifCodec. | |
37 * Returns a bool representing success or failure. | |
38 * | |
39 * @param codecOut | |
40 * If it returned true, and codecOut was not nullptr, | |
41 * codecOut will be set to a new SkGifCodec. | |
42 * | |
43 * @param gifOut | |
44 * If it returned true, and codecOut was nullptr, | |
45 * gifOut must be non-nullptr and gifOut will be set to a new | |
46 * GifFileType pointer. | |
47 * | |
48 * @param stream | |
49 * Deleted on failure. | |
50 * codecOut will take ownership of it in the case where we created a codec. | |
51 * Ownership is unchanged when we returned a gifOut. | |
52 * | |
53 */ | |
54 static bool ReadHeader(SkStream* stream, SkCodec** codecOut, | |
55 GifFileType** gifOut); | |
56 | |
57 /* | 37 /* |
58 * Performs the full gif decode | 38 * Performs the full gif decode |
59 */ | 39 */ |
60 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, | 40 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, |
61 SkPMColor*, int*, int*) override; | 41 SkPMColor*, int*, int*) override; |
62 | 42 |
63 SkEncodedFormat onGetEncodedFormat() const override { | 43 SkEncodedFormat onGetEncodedFormat() const override { |
64 return kGIF_SkEncodedFormat; | 44 return kGIF_SkEncodedFormat; |
65 } | 45 } |
66 | 46 |
67 bool onRewind() override; | 47 bool onRewind() override; |
68 | 48 |
69 uint64_t onGetFillValue(const SkImageInfo&) const override; | 49 uint64_t onGetFillValue(const SkImageInfo&) const override; |
70 | 50 |
71 int onOutputScanline(int inputScanline) const override; | 51 std::vector<FrameInfo> onGetFrameInfo() override; |
| 52 |
| 53 Result onStartIncrementalDecode(const SkImageInfo& /*dstInfo*/, void*, size_
t, |
| 54 const SkCodec::Options&, SkPMColor*, int*) override; |
| 55 |
| 56 Result onIncrementalDecode(int*) override; |
72 | 57 |
73 private: | 58 private: |
74 | 59 |
75 /* | 60 /* |
76 * A gif can contain multiple image frames. We will only decode the first | |
77 * frame. This function reads up to the first image frame, processing | |
78 * transparency and/or animation information that comes before the image | |
79 * data. | |
80 * | |
81 * @param gif Pointer to the library type that manages the gif decode | |
82 * @param transIndex This call will set the transparent index based on the | |
83 * extension data. | |
84 */ | |
85 static Result ReadUpToFirstImage(GifFileType* gif, uint32_t* transIndex); | |
86 | |
87 /* | |
88 * A gif may contain many image frames, all of different sizes. | |
89 * This function checks if the gif dimensions are valid, based on the frame | |
90 * dimensions, and corrects the gif dimensions if necessary. | |
91 * | |
92 * @param gif Pointer to the library type that manages the gif decode | |
93 * @param size Size of the image that we will decode. | |
94 * Will be set by this function if the return value is tru
e. | |
95 * @param frameRect Contains the dimenions and offset of the first image fr
ame. | |
96 * Will be set by this function if the return value is tru
e. | |
97 * | |
98 * @return true on success, false otherwise | |
99 */ | |
100 static bool GetDimensions(GifFileType* gif, SkISize* size, SkIRect* frameRe
ct); | |
101 | |
102 /* | |
103 * Initializes the color table that we will use for decoding. | 61 * Initializes the color table that we will use for decoding. |
104 * | 62 * |
105 * @param dstInfo Contains the requested dst color type. | 63 * @param dstInfo Contains the requested dst color type. |
| 64 * @param frameIndex Frame whose color table to use. |
106 * @param inputColorPtr Copies the encoded color table to the client's | 65 * @param inputColorPtr Copies the encoded color table to the client's |
107 * input color table if the client requests kIndex8. | 66 * input color table if the client requests kIndex8. |
108 * @param inputColorCount If the client requests kIndex8, sets | 67 * @param inputColorCount If the client requests kIndex8, this will be set |
109 * inputColorCount to 256. Since gifs always | 68 * to the number of colors in the array that |
110 * contain 8-bit indices, we need a 256 entry color | 69 * inputColorPtr now points to. This will typically |
111 * table to ensure that indexing is always in | 70 * be 256. Since gifs may have up to 8-bit indices, |
112 * bounds. | 71 * using a 256-entry table means a pixel will never |
| 72 * be out of range. This will be set to 1 if there |
| 73 * is no color table, since that will be a |
| 74 * transparent frame. |
113 */ | 75 */ |
114 void initializeColorTable(const SkImageInfo& dstInfo, SkPMColor* colorPtr, | 76 void initializeColorTable(const SkImageInfo& dstInfo, size_t frameIndex, |
115 int* inputColorCount); | 77 SkPMColor* colorPtr, int* inputColorCount); |
116 | 78 |
117 /* | 79 /* |
118 * Checks for invalid inputs and calls setFrameDimensions(), and | 80 * Does necessary setup, including setting up the color table and swizzler, |
119 * initializeColorTable() in the proper sequence. | 81 * and reports color info to the client. |
120 */ | 82 */ |
121 Result prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr, | 83 Result prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr, |
122 int* inputColorCount, const Options& opts); | 84 int* inputColorCount, const Options& opts); |
123 | 85 |
124 /* | 86 /* |
125 * Initializes the swizzler. | 87 * Initializes the swizzler. |
126 * | 88 * |
127 * @param dstInfo Output image information. Dimensions may have been | 89 * @param dstInfo Output image information. Dimensions may have been |
128 * adjusted if the image frame size does not match the size | 90 * adjusted if the image frame size does not match the siz
e |
129 * indicated in the header. | 91 * indicated in the header. |
130 * @param options Informs the swizzler if destination memory is zero initia
lized. | 92 * @param frameIndex Which frame we are decoding. This determines the frameR
ect |
131 * Contains subset information. | 93 * to use. |
132 */ | 94 */ |
133 void initializeSwizzler(const SkImageInfo& dstInfo, | 95 void initializeSwizzler(const SkImageInfo& dstInfo, size_t frameIndex); |
134 const Options& options); | |
135 | 96 |
136 SkSampler* getSampler(bool createIfNecessary) override { | 97 SkSampler* getSampler(bool createIfNecessary) override { |
137 SkASSERT(fSwizzler); | 98 SkASSERT(fSwizzler); |
138 return fSwizzler; | 99 return fSwizzler.get(); |
139 } | 100 } |
140 | 101 |
141 /* | 102 /* |
142 * @return true if the read is successful and false if the read fails. | 103 * Recursive function to decode a frame. |
| 104 * |
| 105 * @param firstAttempt Whether this is the first call to decodeFrame since |
| 106 * starting. e.g. true in onGetPixels, and true in the |
| 107 * first call to onIncrementalDecode after calling |
| 108 * onStartIncrementalDecode. |
| 109 * When true, this method may have to initialize the |
| 110 * frame, for example by filling or decoding the prior |
| 111 * frame. |
| 112 * @param opts Options for decoding. May be different from |
| 113 * this->options() for decoding prior frames. Specifies |
| 114 * the frame to decode and whether the prior frame has |
| 115 * already been decoded to fDst. If not, and the frame |
| 116 * is not independent, this method will recursively |
| 117 * decode the frame it depends on. |
| 118 * @param rowsDecoded Out-parameter to report the total number of rows |
| 119 * that have been decoded (or at least written to, if |
| 120 * it had to fill), including rows decoded by prior |
| 121 * calls to onIncrementalDecode. |
| 122 * @return kSuccess if the frame is complete, kIncompleteInput |
| 123 * otherwise. |
143 */ | 124 */ |
144 bool readRow(); | 125 Result decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded)
; |
145 | |
146 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& opts
, | |
147 SkPMColor inputColorPtr[], int* inputColorCount) override; | |
148 | |
149 int onGetScanlines(void* dst, int count, size_t rowBytes) override; | |
150 | |
151 bool onSkipScanlines(int count) override; | |
152 | |
153 /* | |
154 * For a scanline decode of "count" lines, this function indicates how | |
155 * many of the "count" lines should be skipped until we reach the top of | |
156 * the image frame and how many of the "count" lines are actually inside | |
157 * the image frame. | |
158 * | |
159 * @param count The number of scanlines requested. | |
160 * @param rowsBeforeFrame Output variable. The number of lines before | |
161 * we reach the top of the image frame. | |
162 * @param rowsInFrame Output variable. The number of lines to decode | |
163 * inside the image frame. | |
164 */ | |
165 void handleScanlineFrame(int count, int* rowsBeforeFrame, int* rowsInFrame); | |
166 | |
167 SkScanlineOrder onGetScanlineOrder() const override; | |
168 | |
169 /* | |
170 * This function cleans up the gif object after the decode completes | |
171 * It is used in a SkAutoTCallIProc template | |
172 */ | |
173 static void CloseGif(GifFileType* gif); | |
174 | |
175 /* | |
176 * Frees any extension data used in the decode | |
177 * Used in a SkAutoTCallVProc | |
178 */ | |
179 static void FreeExtension(SavedImage* image); | |
180 | 126 |
181 /* | 127 /* |
182 * Creates an instance of the decoder | 128 * Creates an instance of the decoder |
183 * Called only by NewFromStream | 129 * Called only by NewFromStream |
184 * | 130 * Takes ownership of the GIFImageReader |
185 * @param info contains properties of the encoded data | |
186 * @param stream the stream of image data | |
187 * @param gif pointer to library type that manages gif decode | |
188 * takes ownership | |
189 * @param transIndex The transparent index. An invalid value | |
190 * indicates that there is no transparent index. | |
191 */ | 131 */ |
192 SkGifCodec(int width, int height, const SkEncodedInfo& info, SkStream* strea
m, | 132 SkGifCodec(const SkEncodedInfo&, const SkImageInfo&, GIFImageReader*); |
193 GifFileType* gif, uint32_t transIndex, const SkIRect& frameRect, boo
l frameIsSubset); | |
194 | 133 |
195 SkAutoTCallVProc<GifFileType, CloseGif> fGif; // owned | 134 std::unique_ptr<GIFImageReader> fReader; |
196 SkAutoTDeleteArray<uint8_t> fSrcBuffer; | 135 std::unique_ptr<uint8_t[]> fTmpBuffer; |
197 const SkIRect fFrameRect; | 136 std::unique_ptr<SkSwizzler> fSwizzler; |
198 const uint32_t fTransIndex; | 137 sk_sp<SkColorTable> fCurrColorTable; |
199 uint32_t fFillIndex; | 138 // We may create a dummy table if there is not a Map in the input data. In |
200 const bool fFrameIsSubset; | 139 // that case, we set this value to false, and we can skip a lot of decoding |
201 SkAutoTDelete<SkSwizzler> fSwizzler; | 140 // work (which would not be meaningful anyway). We create a "fake"/"dummy" |
202 SkAutoTUnref<SkColorTable> fColorTable; | 141 // one in that case, so the client and the swizzler have something to draw. |
| 142 bool fCurrColorTableIsReal; |
| 143 // Whether the background was filled. |
| 144 bool fFilledBackground; |
| 145 // True on the first call to onIncrementalDecode. This value is passed to |
| 146 // decodeFrame. |
| 147 bool fFirstCallToIncrementalDecode; |
| 148 |
| 149 void* fDst; |
| 150 size_t fDstRowBytes; |
| 151 |
| 152 // Updated inside haveDecodedRow when rows are decoded, unless we filled |
| 153 // the background, in which case it is set once and left alone. |
| 154 int fRowsDecoded; |
203 | 155 |
204 typedef SkCodec INHERITED; | 156 typedef SkCodec INHERITED; |
205 }; | 157 }; |
OLD | NEW |