blob: d7dc2b9e1d401242d2c730f805a947b75ca31de0 [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&,
63 SkPMColor*, int32_t*) override;
64
65 SkEncodedFormat onGetEncodedFormat() const override {
66 return kGIF_SkEncodedFormat;
67 }
68
scroggob427db12015-08-12 07:24:13 -070069 bool onRewind() override;
70
msarett8c8f22a2015-04-01 06:58:48 -070071private:
72
73 /*
msarett10522ff2015-09-07 08:54:01 -070074 * A gif can contain multiple image frames. We will only decode the first
75 * frame. This function reads up to the first image frame, processing
76 * transparency and/or animation information that comes before the image
77 * data.
78 *
79 * @param gif Pointer to the library type that manages the gif decode
80 * @param transIndex This call will set the transparent index based on the
81 * extension data.
82 */
scroggo46c57472015-09-30 08:57:13 -070083 static Result ReadUpToFirstImage(GifFileType* gif, uint32_t* transIndex);
msarett10522ff2015-09-07 08:54:01 -070084
85 /*
86 * A gif may contain many image frames, all of different sizes.
87 * This function checks if the frame dimensions are valid and corrects
88 * them if necessary. It then sets fFrameDims to the corrected
89 * dimensions.
90 *
91 * @param desc The image frame descriptor
92 */
93 bool setFrameDimensions(const GifImageDesc& desc);
94
95 /*
96 * Initializes the color table that we will use for decoding.
97 *
98 * @param dstInfo Contains the requested dst color type.
99 * @param inputColorPtr Copies the encoded color table to the client's
100 * input color table if the client requests kIndex8.
101 * @param inputColorCount If the client requests kIndex8, sets
102 * inputColorCount to 256. Since gifs always
103 * contain 8-bit indices, we need a 256 entry color
104 * table to ensure that indexing is always in
105 * bounds.
106 */
107 void initializeColorTable(const SkImageInfo& dstInfo, SkPMColor* colorPtr,
108 int* inputColorCount);
109
110 /*
scroggo3a7701c2015-09-30 09:15:14 -0700111 * Checks for invalid inputs and calls setFrameDimensions(), and
msarett10522ff2015-09-07 08:54:01 -0700112 * initializeColorTable() in the proper sequence.
113 */
scroggo46c57472015-09-30 08:57:13 -0700114 Result prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
msarett10522ff2015-09-07 08:54:01 -0700115 int* inputColorCount, const Options& opts);
116
117 /*
118 * Initializes the swizzler.
119 *
120 * @param dstInfo Output image information. Dimensions may have been
121 * adjusted if the image frame size does not match the size
122 * indicated in the header.
123 * @param zeroInit Indicates if destination memory is zero initialized.
124 */
scroggo46c57472015-09-30 08:57:13 -0700125 Result initializeSwizzler(const SkImageInfo& dstInfo, ZeroInitialized zeroInit);
msarett10522ff2015-09-07 08:54:01 -0700126
127 /*
128 * @return kSuccess if the read is successful and kIncompleteInput if the
129 * read fails.
130 */
scroggo46c57472015-09-30 08:57:13 -0700131 Result readRow();
132
133 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& opts,
134 SkPMColor inputColorPtr[], int* inputColorCount) override;
135
136 Result onGetScanlines(void* dst, int count, size_t rowBytes) override;
137
138 SkScanlineOrder onGetScanlineOrder() const override;
139
140 int onNextScanline() const override;
msarett10522ff2015-09-07 08:54:01 -0700141
142 /*
msarett8c8f22a2015-04-01 06:58:48 -0700143 * This function cleans up the gif object after the decode completes
144 * It is used in a SkAutoTCallIProc template
145 */
msarett438b2ad2015-04-09 12:43:10 -0700146 static void CloseGif(GifFileType* gif);
msarett8c8f22a2015-04-01 06:58:48 -0700147
148 /*
149 * Frees any extension data used in the decode
150 * Used in a SkAutoTCallVProc
151 */
152 static void FreeExtension(SavedImage* image);
153
154 /*
155 * Creates an instance of the decoder
156 * Called only by NewFromStream
157 *
158 * @param srcInfo contains the source width and height
159 * @param stream the stream of image data
160 * @param gif pointer to library type that manages gif decode
161 * takes ownership
msarett10522ff2015-09-07 08:54:01 -0700162 * @param transIndex The transparent index. An invalid value
163 * indicates that there is no transparent index.
msarett8c8f22a2015-04-01 06:58:48 -0700164 */
msarett10522ff2015-09-07 08:54:01 -0700165 SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, GifFileType* gif, uint32_t transIndex);
msarett8c8f22a2015-04-01 06:58:48 -0700166
msarett438b2ad2015-04-09 12:43:10 -0700167 SkAutoTCallVProc<GifFileType, CloseGif> fGif; // owned
msarett10522ff2015-09-07 08:54:01 -0700168 SkAutoTDeleteArray<uint8_t> fSrcBuffer;
169 SkIRect fFrameDims;
170 const uint32_t fTransIndex;
171 uint32_t fFillIndex;
172 bool fFrameIsSubset;
173 SkAutoTDelete<SkSwizzler> fSwizzler;
174 SkAutoTUnref<SkColorTable> fColorTable;
175
msarett8c8f22a2015-04-01 06:58:48 -0700176 typedef SkCodec INHERITED;
177};