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 * |
| 100 * @param inputColorCount Sets inputColorCount to 256. Since gifs always |
| 101 * contain 8-bit indices, we need a 256 entry color |
| 102 * table to ensure that indexing is always in |
| 103 * bounds. |
| 104 * @param transIndex The transparent index. This is set in |
| 105 * readUpToFirstImage(). An invalid value indicates |
| 106 * that there is no transparent index. |
| 107 */ |
| 108 void initializeColorTable(int* inputColorCount, uint32_t transIndex); |
| 109 |
| 110 /* |
| 111 * Initializes the swizzler. |
| 112 * |
| 113 * @param dstInfo Output image information. Dimensions may have been |
| 114 * adjusted if the image frame size does not match the size |
| 115 * indicated in the header. |
| 116 * @param zeroInit Indicates if destination memory is zero initialized. |
| 117 */ |
| 118 SkCodec::Result initializeSwizzler(const SkImageInfo& dstInfo, |
| 119 ZeroInitialized zeroInit); |
| 120 |
| 121 /* |
| 122 * @return kSuccess if the read is successful and kIncompleteInput if the |
| 123 * read fails. |
| 124 */ |
| 125 SkCodec::Result readRow(); |
| 126 |
| 127 /* |
72 * This function cleans up the gif object after the decode completes | 128 * This function cleans up the gif object after the decode completes |
73 * It is used in a SkAutoTCallIProc template | 129 * It is used in a SkAutoTCallIProc template |
74 */ | 130 */ |
75 static void CloseGif(GifFileType* gif); | 131 static void CloseGif(GifFileType* gif); |
76 | 132 |
77 /* | 133 /* |
78 * Frees any extension data used in the decode | 134 * Frees any extension data used in the decode |
79 * Used in a SkAutoTCallVProc | 135 * Used in a SkAutoTCallVProc |
80 */ | 136 */ |
81 static void FreeExtension(SavedImage* image); | 137 static void FreeExtension(SavedImage* image); |
82 | 138 |
83 /* | 139 /* |
84 * Creates an instance of the decoder | 140 * Creates an instance of the decoder |
85 * Called only by NewFromStream | 141 * Called only by NewFromStream |
86 * | 142 * |
87 * @param srcInfo contains the source width and height | 143 * @param srcInfo contains the source width and height |
88 * @param stream the stream of image data | 144 * @param stream the stream of image data |
89 * @param gif pointer to library type that manages gif decode | 145 * @param gif pointer to library type that manages gif decode |
90 * takes ownership | 146 * takes ownership |
91 */ | 147 */ |
92 SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, GifFileType* gif); | 148 SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, GifFileType* gif); |
93 | 149 |
94 SkAutoTCallVProc<GifFileType, CloseGif> fGif; // owned | 150 SkAutoTCallVProc<GifFileType, CloseGif> fGif; // owned |
| 151 SkAutoTDeleteArray<uint8_t> fSrcBuffer; |
| 152 SkIRect fFrameDims; |
| 153 uint32_t fFillIndex; |
| 154 bool fFrameIsSubset; |
| 155 SkAutoTDelete<SkSwizzler> fSwizzler; |
| 156 SkAutoTUnref<SkColorTable> fColorTable; |
| 157 |
| 158 friend class SkGifScanlineDecoder; |
95 | 159 |
96 typedef SkCodec INHERITED; | 160 typedef SkCodec INHERITED; |
97 }; | 161 }; |
OLD | NEW |