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 "SkColorTable.h" | |
10 #include "SkImageInfo.h" | 9 #include "SkImageInfo.h" |
11 #include "SkScanlineDecoder.h" | |
12 #include "SkSwizzler.h" | |
13 | 10 |
14 #include "gif_lib.h" | 11 #include "gif_lib.h" |
15 | 12 |
16 /* | 13 /* |
17 * | 14 * |
18 * This class implements the decoding for gif images | 15 * This class implements the decoding for gif images |
19 * | 16 * |
20 */ | 17 */ |
21 class SkGifCodec : public SkCodec { | 18 class SkGifCodec : public SkCodec { |
22 public: | 19 public: |
23 | 20 |
24 /* | 21 /* |
25 * Checks the start of the stream to see if the image is a gif | 22 * Checks the start of the stream to see if the image is a gif |
26 */ | 23 */ |
27 static bool IsGif(SkStream*); | 24 static bool IsGif(SkStream*); |
28 | 25 |
29 /* | 26 /* |
30 * Assumes IsGif was called and returned true | 27 * Assumes IsGif was called and returned true |
31 * Creates a gif decoder | 28 * Creates a gif decoder |
32 * Reads enough of the stream to determine the image format | 29 * Reads enough of the stream to determine the image format |
33 */ | 30 */ |
34 static SkCodec* NewFromStream(SkStream*); | 31 static SkCodec* NewFromStream(SkStream*); |
35 | 32 |
36 static SkScanlineDecoder* NewSDFromStream(SkStream* stream); | |
37 | 33 |
38 protected: | 34 protected: |
39 | 35 |
40 /* | 36 /* |
41 * Read enough of the stream to initialize the SkGifCodec. | 37 * Read enough of the stream to initialize the SkGifCodec. |
42 * Returns a bool representing success or failure. | 38 * Returns a bool representing success or failure. |
43 * | 39 * |
44 * @param codecOut | 40 * @param codecOut |
45 * If it returned true, and codecOut was not nullptr, | 41 * If it returned true, and codecOut was not nullptr, |
46 * codecOut will be set to a new SkGifCodec. | 42 * codecOut will be set to a new SkGifCodec. |
47 * | 43 * |
48 * @param gifOut | 44 * @param gifOut |
49 * If it returned true, and codecOut was nullptr, | 45 * If it returned true, and codecOut was nullptr, |
50 * gifOut must be non-nullptr and gifOut will be set to a new | 46 * gifOut must be non-nullptr and gifOut will be set to a new |
51 * GifFileType pointer. | 47 * GifFileType pointer. |
52 * | 48 * |
53 * @param stream | 49 * @param stream |
54 * Deleted on failure. | 50 * Deleted on failure. |
55 * codecOut will take ownership of it in the case where we created a codec. | 51 * codecOut will take ownership of it in the case where we created a codec. |
56 * Ownership is unchanged when we returned a gifOut. | 52 * Ownership is unchanged when we returned a gifOut. |
57 * | 53 * |
58 */ | 54 */ |
59 static bool ReadHeader(SkStream* stream, SkCodec** codecOut, | 55 static bool ReadHeader(SkStream* stream, SkCodec** codecOut, GifFileType** g
ifOut); |
60 GifFileType** gifOut); | |
61 | 56 |
62 /* | 57 /* |
63 * Performs the full gif decode | 58 * Initiates the gif decode |
64 */ | 59 */ |
65 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, | 60 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, |
66 SkPMColor*, int32_t*) override; | 61 SkPMColor*, int32_t*) override; |
67 | 62 |
68 SkEncodedFormat onGetEncodedFormat() const override { | 63 SkEncodedFormat onGetEncodedFormat() const override { |
69 return kGIF_SkEncodedFormat; | 64 return kGIF_SkEncodedFormat; |
70 } | 65 } |
71 | 66 |
72 bool onRewind() override; | 67 bool onRewind() override; |
73 | 68 |
74 private: | 69 private: |
75 | 70 |
76 /* | 71 /* |
77 * A gif can contain multiple image frames. We will only decode the first | |
78 * frame. This function reads up to the first image frame, processing | |
79 * transparency and/or animation information that comes before the image | |
80 * data. | |
81 * | |
82 * @param gif Pointer to the library type that manages the gif decode | |
83 * @param transIndex This call will set the transparent index based on the | |
84 * extension data. | |
85 */ | |
86 static SkCodec::Result ReadUpToFirstImage(GifFileType* gif, uint32_t* trans
Index); | |
87 | |
88 /* | |
89 * A gif may contain many image frames, all of different sizes. | |
90 * This function checks if the frame dimensions are valid and corrects | |
91 * them if necessary. It then sets fFrameDims to the corrected | |
92 * dimensions. | |
93 * | |
94 * @param desc The image frame descriptor | |
95 */ | |
96 bool setFrameDimensions(const GifImageDesc& desc); | |
97 | |
98 /* | |
99 * Initializes the color table that we will use for decoding. | |
100 * | |
101 * @param dstInfo Contains the requested dst color type. | |
102 * @param inputColorPtr Copies the encoded color table to the client's | |
103 * input color table if the client requests kIndex8. | |
104 * @param inputColorCount If the client requests kIndex8, sets | |
105 * inputColorCount to 256. Since gifs always | |
106 * contain 8-bit indices, we need a 256 entry color | |
107 * table to ensure that indexing is always in | |
108 * bounds. | |
109 */ | |
110 void initializeColorTable(const SkImageInfo& dstInfo, SkPMColor* colorPtr, | |
111 int* inputColorCount); | |
112 | |
113 /* | |
114 * Checks for invalid inputs and calls rewindIfNeeded(), setFramDimensions(),
and | |
115 * initializeColorTable() in the proper sequence. | |
116 */ | |
117 SkCodec::Result prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* input
ColorPtr, | |
118 int* inputColorCount, const Options& opts); | |
119 | |
120 /* | |
121 * Initializes the swizzler. | |
122 * | |
123 * @param dstInfo Output image information. Dimensions may have been | |
124 * adjusted if the image frame size does not match the size | |
125 * indicated in the header. | |
126 * @param zeroInit Indicates if destination memory is zero initialized. | |
127 */ | |
128 SkCodec::Result initializeSwizzler(const SkImageInfo& dstInfo, | |
129 ZeroInitialized zeroInit); | |
130 | |
131 /* | |
132 * @return kSuccess if the read is successful and kIncompleteInput if the | |
133 * read fails. | |
134 */ | |
135 SkCodec::Result readRow(); | |
136 | |
137 /* | |
138 * This function cleans up the gif object after the decode completes | 72 * This function cleans up the gif object after the decode completes |
139 * It is used in a SkAutoTCallIProc template | 73 * It is used in a SkAutoTCallIProc template |
140 */ | 74 */ |
141 static void CloseGif(GifFileType* gif); | 75 static void CloseGif(GifFileType* gif); |
142 | 76 |
143 /* | 77 /* |
144 * Frees any extension data used in the decode | 78 * Frees any extension data used in the decode |
145 * Used in a SkAutoTCallVProc | 79 * Used in a SkAutoTCallVProc |
146 */ | 80 */ |
147 static void FreeExtension(SavedImage* image); | 81 static void FreeExtension(SavedImage* image); |
148 | 82 |
149 /* | 83 /* |
150 * Creates an instance of the decoder | 84 * Creates an instance of the decoder |
151 * Called only by NewFromStream | 85 * Called only by NewFromStream |
152 * | 86 * |
153 * @param srcInfo contains the source width and height | 87 * @param srcInfo contains the source width and height |
154 * @param stream the stream of image data | 88 * @param stream the stream of image data |
155 * @param gif pointer to library type that manages gif decode | 89 * @param gif pointer to library type that manages gif decode |
156 * takes ownership | 90 * takes ownership |
157 * @param transIndex The transparent index. An invalid value | |
158 * indicates that there is no transparent index. | |
159 */ | 91 */ |
160 SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, GifFileType* gif, u
int32_t transIndex); | 92 SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, GifFileType* gif); |
161 | 93 |
162 SkAutoTCallVProc<GifFileType, CloseGif> fGif; // owned | 94 SkAutoTCallVProc<GifFileType, CloseGif> fGif; // owned |
163 SkAutoTDeleteArray<uint8_t> fSrcBuffer; | |
164 SkIRect fFrameDims; | |
165 const uint32_t fTransIndex; | |
166 uint32_t fFillIndex; | |
167 bool fFrameIsSubset; | |
168 SkAutoTDelete<SkSwizzler> fSwizzler; | |
169 SkAutoTUnref<SkColorTable> fColorTable; | |
170 | |
171 friend class SkGifScanlineDecoder; | |
172 | 95 |
173 typedef SkCodec INHERITED; | 96 typedef SkCodec INHERITED; |
174 }; | 97 }; |
OLD | NEW |