Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(807)

Side by Side Diff: src/codec/SkGifCodec.h

Issue 2045293002: Add support for multiple frames in SkCodec (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Various fixes Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "SkCodecAnimation.h"
9 #include "SkColorSpace.h" 10 #include "SkColorSpace.h"
10 #include "SkColorTable.h" 11 #include "SkColorTable.h"
11 #include "SkImageInfo.h" 12 #include "SkImageInfo.h"
12 #include "SkSwizzler.h" 13 #include "SkSwizzler.h"
13 14
14 struct GifFileType; 15 #include "GIFImageReader.h"
15 struct SavedImage;
16 16
17 /* 17 /*
18 * 18 *
19 * This class implements the decoding for gif images 19 * This class implements the decoding for gif images
20 * 20 *
21 */ 21 */
22 class SkGifCodec : public SkCodec { 22 class SkGifCodec : public SkCodec {
23 public: 23 public:
24 static bool IsGif(const void*, size_t); 24 static bool IsGif(const void*, size_t);
25 25
26 /* 26 /*
27 * Assumes IsGif was called and returned true 27 * Assumes IsGif was called and returned true
28 * Creates a gif decoder 28 * Creates a gif decoder
29 * Reads enough of the stream to determine the image format 29 * Reads enough of the stream to determine the image format
30 */ 30 */
31 static SkCodec* NewFromStream(SkStream*); 31 static SkCodec* NewFromStream(SkStream*);
32 32
33 // Callback for GIFImageReader when a row is available.
34 bool haveDecodedRow(size_t frameIndex, const unsigned char* rowBegin,
msarett 2016/10/14 14:58:51 Seems strange to me that this is public... I gues
scroggo_chromium 2016/10/14 19:56:00 Indeed, it is a little awkward, though someone wou
35 size_t rowNumber, unsigned repeatCount, bool writeTransp arentPixels);
33 protected: 36 protected:
34
35 /*
36 * Read enough of the stream to initialize the SkGifCodec.
37 * Returns a bool representing success or failure.
38 *
39 * @param codecOut
40 * If it returned true, and codecOut was not nullptr,
41 * codecOut will be set to a new SkGifCodec.
42 *
43 * @param gifOut
44 * If it returned true, and codecOut was nullptr,
45 * gifOut must be non-nullptr and gifOut will be set to a new
46 * GifFileType pointer.
47 *
48 * @param stream
49 * Deleted on failure.
50 * codecOut will take ownership of it in the case where we created a codec.
51 * Ownership is unchanged when we returned a gifOut.
52 *
53 */
54 static bool ReadHeader(SkStream* stream, SkCodec** codecOut,
55 GifFileType** gifOut);
56
57 /* 37 /*
58 * Performs the full gif decode 38 * Performs the full gif decode
59 */ 39 */
60 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, 40 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&,
61 SkPMColor*, int*, int*) override; 41 SkPMColor*, int*, int*) override;
62 42
63 SkEncodedFormat onGetEncodedFormat() const override { 43 SkEncodedFormat onGetEncodedFormat() const override {
64 return kGIF_SkEncodedFormat; 44 return kGIF_SkEncodedFormat;
65 } 45 }
66 46
67 bool onRewind() override; 47 bool onRewind() override;
68 48
69 uint64_t onGetFillValue(const SkImageInfo&) const override; 49 uint64_t onGetFillValue(const SkImageInfo&) const override;
70 50
71 int onOutputScanline(int inputScanline) const override; 51 std::vector<FrameInfo> onGetFrameInfo() override;
52
53 Result onStartIncrementalDecode(const SkImageInfo& /*dstInfo*/, void*, size_ t,
54 const SkCodec::Options&, SkPMColor*, int*) override;
55
56 Result onIncrementalDecode(int*) override;
72 57
73 private: 58 private:
74 59
75 /* 60 /*
76 * A gif can contain multiple image frames. We will only decode the first
77 * frame. This function reads up to the first image frame, processing
78 * transparency and/or animation information that comes before the image
79 * data.
80 *
81 * @param gif Pointer to the library type that manages the gif decode
82 * @param transIndex This call will set the transparent index based on the
83 * extension data.
84 */
85 static Result ReadUpToFirstImage(GifFileType* gif, uint32_t* transIndex);
86
87 /*
88 * A gif may contain many image frames, all of different sizes.
89 * This function checks if the gif dimensions are valid, based on the frame
90 * dimensions, and corrects the gif dimensions if necessary.
91 *
92 * @param gif Pointer to the library type that manages the gif decode
93 * @param size Size of the image that we will decode.
94 * Will be set by this function if the return value is tru e.
95 * @param frameRect Contains the dimenions and offset of the first image fr ame.
96 * Will be set by this function if the return value is tru e.
97 *
98 * @return true on success, false otherwise
99 */
100 static bool GetDimensions(GifFileType* gif, SkISize* size, SkIRect* frameRe ct);
101
102 /*
103 * Initializes the color table that we will use for decoding. 61 * Initializes the color table that we will use for decoding.
104 * 62 *
105 * @param dstInfo Contains the requested dst color type. 63 * @param dstInfo Contains the requested dst color type.
64 * @param frameIndex Frame whose color table to use.
106 * @param inputColorPtr Copies the encoded color table to the client's 65 * @param inputColorPtr Copies the encoded color table to the client's
107 * input color table if the client requests kIndex8. 66 * input color table if the client requests kIndex8.
108 * @param inputColorCount If the client requests kIndex8, sets 67 * @param inputColorCount If the client requests kIndex8, sets
109 * inputColorCount to 256. Since gifs always 68 * inputColorCount to 256. Since gifs always
110 * contain 8-bit indices, we need a 256 entry color 69 * contain 8-bit indices, we need a 256 entry color
msarett 2016/10/14 14:58:51 Maybe delete this wrong comment? (pretty sure I w
scroggo_chromium 2016/10/14 19:56:00 I updated the comment to better reflect what happe
111 * table to ensure that indexing is always in 70 * table to ensure that indexing is always in
112 * bounds. 71 * bounds. (Exception - set to 1 if the gif's color
72 * table is missing. The only entry will be
73 * transparent.)
113 */ 74 */
114 void initializeColorTable(const SkImageInfo& dstInfo, SkPMColor* colorPtr, 75 void initializeColorTable(const SkImageInfo& dstInfo, size_t frameIndex,
115 int* inputColorCount); 76 SkPMColor* colorPtr, int* inputColorCount);
116 77
117 /* 78 /*
118 * Checks for invalid inputs and calls setFrameDimensions(), and 79 * Does necessary setup, including setting up the color table and swizzler,
119 * initializeColorTable() in the proper sequence. 80 * and reports color info to the client.
120 */ 81 */
121 Result prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr, 82 Result prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
122 int* inputColorCount, const Options& opts); 83 int* inputColorCount, const Options& opts);
123 84
124 /* 85 /*
125 * Initializes the swizzler. 86 * Initializes the swizzler.
126 * 87 *
127 * @param dstInfo Output image information. Dimensions may have been 88 * @param dstInfo Output image information. Dimensions may have been
128 * adjusted if the image frame size does not match the size 89 * adjusted if the image frame size does not match the siz e
129 * indicated in the header. 90 * indicated in the header.
130 * @param options Informs the swizzler if destination memory is zero initia lized. 91 * @param frameIndex Which frame we are decoding. This determines the frameR ect
131 * Contains subset information. 92 * to use.
132 */ 93 */
133 void initializeSwizzler(const SkImageInfo& dstInfo, 94 void initializeSwizzler(const SkImageInfo& dstInfo, size_t frameIndex);
134 const Options& options);
135 95
136 SkSampler* getSampler(bool createIfNecessary) override { 96 SkSampler* getSampler(bool createIfNecessary) override {
137 SkASSERT(fSwizzler); 97 SkASSERT(fSwizzler);
138 return fSwizzler; 98 return fSwizzler.get();
139 } 99 }
140 100
141 /* 101 /*
142 * @return true if the read is successful and false if the read fails. 102 * Recursive function to decode a frame.
103 *
104 * @param firstAttempt Whether this is the first call to decodeFrame since
105 * starting. e.g. true in onGetPixels, and true in the
106 * first call to onIncrementalDecode after calling
107 * onStartIncrementalDecode.
108 * When true, this method may have to initialize the
109 * frame, for example by filling or decoding the prior
110 * frame.
111 * @param opts Options for decoding. May be different from
112 * this->options() for decoding prior frames. Specifies
113 * the frame to decode and whether the prior frame has
114 * already been decoded to fDst. If not, and the frame
115 * is not independent, this method will recursively
116 * decode the frame it depends on.
117 * @param rowsDecoded Out-parameter to report the total number of rows
118 * that have been decoded (or at least written to, if
119 * it had to fill), including rows decoded by prior
120 * calls to onIncrementalDecode.
121 * @return kSuccess if the frame is complete, kIncompleteInput
122 * otherwise.
143 */ 123 */
144 bool readRow(); 124 Result decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded) ;
145
146 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& opts ,
147 SkPMColor inputColorPtr[], int* inputColorCount) override;
148
149 int onGetScanlines(void* dst, int count, size_t rowBytes) override;
150
151 bool onSkipScanlines(int count) override;
152
153 /*
154 * For a scanline decode of "count" lines, this function indicates how
155 * many of the "count" lines should be skipped until we reach the top of
156 * the image frame and how many of the "count" lines are actually inside
157 * the image frame.
158 *
159 * @param count The number of scanlines requested.
160 * @param rowsBeforeFrame Output variable. The number of lines before
161 * we reach the top of the image frame.
162 * @param rowsInFrame Output variable. The number of lines to decode
163 * inside the image frame.
164 */
165 void handleScanlineFrame(int count, int* rowsBeforeFrame, int* rowsInFrame);
166
167 SkScanlineOrder onGetScanlineOrder() const override;
168
169 /*
170 * This function cleans up the gif object after the decode completes
171 * It is used in a SkAutoTCallIProc template
172 */
173 static void CloseGif(GifFileType* gif);
174
175 /*
176 * Frees any extension data used in the decode
177 * Used in a SkAutoTCallVProc
178 */
179 static void FreeExtension(SavedImage* image);
180 125
181 /* 126 /*
182 * Creates an instance of the decoder 127 * Creates an instance of the decoder
183 * Called only by NewFromStream 128 * Called only by NewFromStream
184 * 129 * Takes ownership of the GIFImageReader
185 * @param info contains properties of the encoded data
186 * @param stream the stream of image data
187 * @param gif pointer to library type that manages gif decode
188 * takes ownership
189 * @param transIndex The transparent index. An invalid value
190 * indicates that there is no transparent index.
191 */ 130 */
192 SkGifCodec(int width, int height, const SkEncodedInfo& info, SkStream* strea m, 131 SkGifCodec(GIFImageReader*);
193 GifFileType* gif, uint32_t transIndex, const SkIRect& frameRect, boo l frameIsSubset);
194 132
195 SkAutoTCallVProc<GifFileType, CloseGif> fGif; // owned 133 std::unique_ptr<GIFImageReader> fReader;
196 SkAutoTDeleteArray<uint8_t> fSrcBuffer; 134 std::unique_ptr<uint8_t[]> fTmpBuffer;
197 const SkIRect fFrameRect; 135 std::unique_ptr<SkSwizzler> fSwizzler;
198 const uint32_t fTransIndex; 136 sk_sp<SkColorTable> fCurrColorTable;
199 uint32_t fFillIndex; 137 // We may create a dummy table if there is not a Map in the input data. In
200 const bool fFrameIsSubset; 138 // that case, we set this value to false, and we can skip a lot of decoding
201 SkAutoTDelete<SkSwizzler> fSwizzler; 139 // work (which would not be meaningful anyway). We create a "fake"/"dummy"
202 SkAutoTUnref<SkColorTable> fColorTable; 140 // one in that case, so the client and the swizzler have something to draw.
141 bool fCurrColorTableIsReal;
142 // Whether the background was filled.
143 bool fFilledBackground;
144 // True on the first call to onIncrementalDecode. This value is passed to
145 // decodeFrame.
146 bool fFirstCallToIncrementalDecode;
147
148 void* fDst;
149 size_t fDstRowBytes;
150
151 int fFirstRow;
152 int fLastRow;
153
154 // Updated inside haveDecodedRow when rows are decoded, unless we filled
155 // the background, in which case it is set once and left alone.
156 int fRowsDecoded;
203 157
204 typedef SkCodec INHERITED; 158 typedef SkCodec INHERITED;
205 }; 159 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698