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