blob: 10fdac97f9141c109c10d0adfb0efd20c8eb1139 [file] [log] [blame]
msarett8c8f22a2015-04-01 06:58:48 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkCodec.h"
msarett10522ff2015-09-07 08:54:01 -07009#include "SkColorTable.h"
msarett8c8f22a2015-04-01 06:58:48 -070010#include "SkImageInfo.h"
msarett10522ff2015-09-07 08:54:01 -070011#include "SkSwizzler.h"
msarett8c8f22a2015-04-01 06:58:48 -070012
13#include "gif_lib.h"
14
15/*
16 *
17 * This class implements the decoding for gif images
18 *
19 */
20class SkGifCodec : public SkCodec {
21public:
22
23 /*
24 * Checks the start of the stream to see if the image is a gif
25 */
26 static bool IsGif(SkStream*);
27
28 /*
29 * Assumes IsGif was called and returned true
30 * Creates a gif decoder
31 * Reads enough of the stream to determine the image format
32 */
33 static SkCodec* NewFromStream(SkStream*);
msarett10522ff2015-09-07 08:54:01 -070034
msarett8c8f22a2015-04-01 06:58:48 -070035protected:
36
37 /*
msarett438b2ad2015-04-09 12:43:10 -070038 * Read enough of the stream to initialize the SkGifCodec.
39 * Returns a bool representing success or failure.
40 *
41 * @param codecOut
halcanary96fcdcc2015-08-27 07:41:13 -070042 * If it returned true, and codecOut was not nullptr,
msarett438b2ad2015-04-09 12:43:10 -070043 * codecOut will be set to a new SkGifCodec.
44 *
45 * @param gifOut
halcanary96fcdcc2015-08-27 07:41:13 -070046 * If it returned true, and codecOut was nullptr,
47 * gifOut must be non-nullptr and gifOut will be set to a new
msarett438b2ad2015-04-09 12:43:10 -070048 * GifFileType pointer.
49 *
50 * @param stream
51 * Deleted on failure.
52 * codecOut will take ownership of it in the case where we created a codec.
53 * Ownership is unchanged when we returned a gifOut.
54 *
55 */
msarett10522ff2015-09-07 08:54:01 -070056 static bool ReadHeader(SkStream* stream, SkCodec** codecOut,
57 GifFileType** gifOut);
msarett438b2ad2015-04-09 12:43:10 -070058
59 /*
msarett10522ff2015-09-07 08:54:01 -070060 * Performs the full gif decode
msarett8c8f22a2015-04-01 06:58:48 -070061 */
62 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&,
msarette6dd0042015-10-09 11:07:34 -070063 SkPMColor*, int*, int*) override;
msarett8c8f22a2015-04-01 06:58:48 -070064
65 SkEncodedFormat onGetEncodedFormat() const override {
66 return kGIF_SkEncodedFormat;
67 }
68
scroggob427db12015-08-12 07:24:13 -070069 bool onRewind() override;
70
msarette6dd0042015-10-09 11:07:34 -070071 uint32_t onGetFillValue(SkColorType colorType, SkAlphaType alphaType) const override;
72
73 int onOutputScanline(int inputScanline) const;
74
msarett8c8f22a2015-04-01 06:58:48 -070075private:
76
77 /*
msarett10522ff2015-09-07 08:54:01 -070078 * A gif can contain multiple image frames. We will only decode the first
79 * frame. This function reads up to the first image frame, processing
80 * transparency and/or animation information that comes before the image
81 * data.
82 *
83 * @param gif Pointer to the library type that manages the gif decode
84 * @param transIndex This call will set the transparent index based on the
85 * extension data.
86 */
scroggo46c57472015-09-30 08:57:13 -070087 static Result ReadUpToFirstImage(GifFileType* gif, uint32_t* transIndex);
msarett10522ff2015-09-07 08:54:01 -070088
89 /*
90 * A gif may contain many image frames, all of different sizes.
msarett4aa02d82015-10-06 07:46:02 -070091 * This function checks if the gif dimensions are valid, based on the frame
92 * dimensions, and corrects the gif dimensions if necessary.
msarett10522ff2015-09-07 08:54:01 -070093 *
msarett4aa02d82015-10-06 07:46:02 -070094 * @param gif Pointer to the library type that manages the gif decode
95 * @param size Size of the image that we will decode.
96 * Will be set by this function if the return value is true.
97 * @param frameRect Contains the dimenions and offset of the first image frame.
98 * Will be set by this function if the return value is true.
99 *
100 * @return true on success, false otherwise
msarett10522ff2015-09-07 08:54:01 -0700101 */
msarett4aa02d82015-10-06 07:46:02 -0700102 static bool GetDimensions(GifFileType* gif, SkISize* size, SkIRect* frameRect);
msarett10522ff2015-09-07 08:54:01 -0700103
104 /*
105 * Initializes the color table that we will use for decoding.
106 *
107 * @param dstInfo Contains the requested dst color type.
108 * @param inputColorPtr Copies the encoded color table to the client's
109 * input color table if the client requests kIndex8.
110 * @param inputColorCount If the client requests kIndex8, sets
111 * inputColorCount to 256. Since gifs always
112 * contain 8-bit indices, we need a 256 entry color
113 * table to ensure that indexing is always in
114 * bounds.
115 */
116 void initializeColorTable(const SkImageInfo& dstInfo, SkPMColor* colorPtr,
117 int* inputColorCount);
118
119 /*
scroggo3a7701c2015-09-30 09:15:14 -0700120 * Checks for invalid inputs and calls setFrameDimensions(), and
msarett10522ff2015-09-07 08:54:01 -0700121 * initializeColorTable() in the proper sequence.
122 */
scroggo46c57472015-09-30 08:57:13 -0700123 Result prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
msarett10522ff2015-09-07 08:54:01 -0700124 int* inputColorCount, const Options& opts);
125
126 /*
127 * Initializes the swizzler.
128 *
129 * @param dstInfo Output image information. Dimensions may have been
130 * adjusted if the image frame size does not match the size
131 * indicated in the header.
132 * @param zeroInit Indicates if destination memory is zero initialized.
133 */
scroggo46c57472015-09-30 08:57:13 -0700134 Result initializeSwizzler(const SkImageInfo& dstInfo, ZeroInitialized zeroInit);
msarett10522ff2015-09-07 08:54:01 -0700135
msarette6dd0042015-10-09 11:07:34 -0700136 SkSampler* getSampler(bool createIfNecessary) override {
137 SkASSERT(fSwizzler);
138 return fSwizzler;
139 }
scroggoe7fc14b2015-10-02 13:14:46 -0700140
msarett10522ff2015-09-07 08:54:01 -0700141 /*
msarette6dd0042015-10-09 11:07:34 -0700142 * @return true if the read is successful and false if the read fails.
msarett10522ff2015-09-07 08:54:01 -0700143 */
msarette6dd0042015-10-09 11:07:34 -0700144 bool readRow();
scroggo46c57472015-09-30 08:57:13 -0700145
146 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& opts,
147 SkPMColor inputColorPtr[], int* inputColorCount) override;
148
msarette6dd0042015-10-09 11:07:34 -0700149 int onGetScanlines(void* dst, int count, size_t rowBytes) override;
scroggo46c57472015-09-30 08:57:13 -0700150
151 SkScanlineOrder onGetScanlineOrder() const override;
152
msarett10522ff2015-09-07 08:54:01 -0700153 /*
msarett8c8f22a2015-04-01 06:58:48 -0700154 * This function cleans up the gif object after the decode completes
155 * It is used in a SkAutoTCallIProc template
156 */
msarett438b2ad2015-04-09 12:43:10 -0700157 static void CloseGif(GifFileType* gif);
msarett8c8f22a2015-04-01 06:58:48 -0700158
159 /*
160 * Frees any extension data used in the decode
161 * Used in a SkAutoTCallVProc
162 */
163 static void FreeExtension(SavedImage* image);
164
165 /*
166 * Creates an instance of the decoder
167 * Called only by NewFromStream
168 *
169 * @param srcInfo contains the source width and height
170 * @param stream the stream of image data
171 * @param gif pointer to library type that manages gif decode
172 * takes ownership
msarett10522ff2015-09-07 08:54:01 -0700173 * @param transIndex The transparent index. An invalid value
174 * indicates that there is no transparent index.
msarett8c8f22a2015-04-01 06:58:48 -0700175 */
msarett4aa02d82015-10-06 07:46:02 -0700176 SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, GifFileType* gif, uint32_t transIndex,
177 const SkIRect& frameRect, bool frameIsSubset);
msarett8c8f22a2015-04-01 06:58:48 -0700178
msarett438b2ad2015-04-09 12:43:10 -0700179 SkAutoTCallVProc<GifFileType, CloseGif> fGif; // owned
msarett10522ff2015-09-07 08:54:01 -0700180 SkAutoTDeleteArray<uint8_t> fSrcBuffer;
msarett4aa02d82015-10-06 07:46:02 -0700181 const SkIRect fFrameRect;
msarett10522ff2015-09-07 08:54:01 -0700182 const uint32_t fTransIndex;
183 uint32_t fFillIndex;
msarett4aa02d82015-10-06 07:46:02 -0700184 const bool fFrameIsSubset;
msarett10522ff2015-09-07 08:54:01 -0700185 SkAutoTDelete<SkSwizzler> fSwizzler;
186 SkAutoTUnref<SkColorTable> fColorTable;
187
msarett8c8f22a2015-04-01 06:58:48 -0700188 typedef SkCodec INHERITED;
189};