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

Side by Side Diff: third_party/gif/GIFImageReader.h

Issue 2045293002: Add support for multiple frames in SkCodec (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix a test - we now draw transparent background for missing color table Created 4 years, 1 month 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
« no previous file with comments | « tests/GifTest.cpp ('k') | third_party/gif/GIFImageReader.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is Mozilla Communicator client code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38 #ifndef GIFImageReader_h
39 #define GIFImageReader_h
40
41 // Define ourselves as the clientPtr. Mozilla just hacked their C++ callback cl ass into this old C decoder,
42 // so we will too.
43 class SkGifCodec;
44
45 #include "SkCodec.h"
46 #include "SkCodecPriv.h"
47 #include "SkCodecAnimation.h"
48 #include "SkColorTable.h"
49 #include "SkData.h"
50 #include "SkImageInfo.h"
51 #include "SkStreamBuffer.h"
52 #include "../private/SkTArray.h"
53 #include <memory>
54 #include <vector>
55
56 typedef SkTArray<unsigned char, true> GIFRow;
57
58
59 #define MAX_DICTIONARY_ENTRY_BITS 12
60 #define MAX_DICTIONARY_ENTRIES 4096 // 2^MAX_DICTIONARY_ENTRY_BITS
61 #define MAX_COLORS 256
62 #define BYTES_PER_COLORMAP_ENTRY 3
63
64 constexpr int cLoopCountNotSeen = -2;
65 constexpr size_t kNotFound = static_cast<size_t>(-1);
66
67 // List of possible parsing states.
68 enum GIFState {
69 GIFType,
70 GIFGlobalHeader,
71 GIFGlobalColormap,
72 GIFImageStart,
73 GIFImageHeader,
74 GIFImageColormap,
75 GIFImageBody,
76 GIFLZWStart,
77 GIFLZW,
78 GIFSubBlock,
79 GIFExtension,
80 GIFControlExtension,
81 GIFConsumeBlock,
82 GIFSkipBlock,
83 GIFDone,
84 GIFCommentExtension,
85 GIFApplicationExtension,
86 GIFNetscapeExtensionBlock,
87 GIFConsumeNetscapeExtension,
88 GIFConsumeComment
89 };
90
91 struct GIFFrameContext;
92
93 // LZW decoder state machine.
94 class GIFLZWContext final : public SkNoncopyable {
95 public:
96 GIFLZWContext(SkGifCodec* client, const GIFFrameContext* frameContext)
97 : codesize(0)
98 , codemask(0)
99 , clearCode(0)
100 , avail(0)
101 , oldcode(0)
102 , firstchar(0)
103 , bits(0)
104 , datum(0)
105 , ipass(0)
106 , irow(0)
107 , rowsRemaining(0)
108 , rowIter(0)
109 , m_client(client)
110 , m_frameContext(frameContext)
111 { }
112
113 bool prepareToDecode();
114 bool outputRow(const unsigned char* rowBegin);
115 bool doLZW(const unsigned char* block, size_t bytesInBlock);
116 bool hasRemainingRows() { return rowsRemaining; }
117
118 private:
119 // LZW decoding states and output states.
120 int codesize;
121 int codemask;
122 int clearCode; // Codeword used to trigger dictionary reset.
123 int avail; // Index of next available slot in dictionary.
124 int oldcode;
125 unsigned char firstchar;
126 int bits; // Number of unread bits in "datum".
127 int datum; // 32-bit input buffer.
128 int ipass; // Interlace pass; Ranges 1-4 if interlaced.
129 size_t irow; // Current output row, starting at zero.
130 size_t rowsRemaining; // Rows remaining to be output.
131
132 unsigned short prefix[MAX_DICTIONARY_ENTRIES];
133 unsigned char suffix[MAX_DICTIONARY_ENTRIES];
134 unsigned short suffixLength[MAX_DICTIONARY_ENTRIES];
135 GIFRow rowBuffer; // Single scanline temporary buffer.
136 unsigned char* rowIter;
137
138 SkGifCodec* const m_client;
139 const GIFFrameContext* m_frameContext;
140 };
141
142 class GIFColorMap final {
143 public:
144 GIFColorMap()
145 : m_isDefined(false)
146 , m_colors(0)
147 , m_packColorProc(nullptr)
148 {
149 }
150
151 void setNumColors(size_t colors) {
152 m_colors = colors;
153 }
154
155 size_t numColors() const { return m_colors; }
156
157 void setRawData(const char* data, size_t size)
158 {
159 // FIXME: Can we avoid this copy?
160 m_rawData = SkData::MakeWithCopy(data, size);
161 SkASSERT(m_colors * BYTES_PER_COLORMAP_ENTRY == size);
162 m_isDefined = true;
163 }
164 bool isDefined() const { return m_isDefined; }
165
166 // Build RGBA table using the data stream.
167 sk_sp<SkColorTable> buildTable(SkColorType dstColorType, size_t transparentP ixel) const;
168
169 private:
170 bool m_isDefined;
171 size_t m_colors;
172 sk_sp<SkData> m_rawData;
173 mutable PackColorProc m_packColorProc;
174 mutable sk_sp<SkColorTable> m_table;
175 };
176
177 // LocalFrame output state machine.
178 struct GIFFrameContext : SkNoncopyable {
179 public:
180 GIFFrameContext(int id)
181 : m_frameId(id)
182 , m_xOffset(0)
183 , m_yOffset(0)
184 , m_width(0)
185 , m_height(0)
186 , m_transparentPixel(kNotFound)
187 , m_disposalMethod(SkCodecAnimation::Keep_DisposalMethod)
188 , m_requiredFrame(SkCodec::kNone)
189 , m_dataSize(0)
190 , m_progressiveDisplay(false)
191 , m_interlaced(false)
192 , m_delayTime(0)
193 , m_currentLzwBlock(0)
194 , m_isComplete(false)
195 , m_isHeaderDefined(false)
196 , m_isDataSizeDefined(false)
197 {
198 }
199
200 ~GIFFrameContext()
201 {
202 }
203
204 void addLzwBlock(const void* data, size_t size)
205 {
206 m_lzwBlocks.push_back(SkData::MakeWithCopy(data, size));
207 }
208
209 bool decode(SkGifCodec* client, bool* frameDecoded);
210
211 int frameId() const { return m_frameId; }
212 void setRect(unsigned x, unsigned y, unsigned width, unsigned height)
213 {
214 m_xOffset = x;
215 m_yOffset = y;
216 m_width = width;
217 m_height = height;
218 }
219 SkIRect frameRect() const { return SkIRect::MakeXYWH(m_xOffset, m_yOffset, m _width, m_height); }
220 unsigned xOffset() const { return m_xOffset; }
221 unsigned yOffset() const { return m_yOffset; }
222 unsigned width() const { return m_width; }
223 unsigned height() const { return m_height; }
224 size_t transparentPixel() const { return m_transparentPixel; }
225 void setTransparentPixel(size_t pixel) { m_transparentPixel = pixel; }
226 SkCodecAnimation::DisposalMethod getDisposalMethod() const { return m_dispos alMethod; }
227 void setDisposalMethod(SkCodecAnimation::DisposalMethod disposalMethod) { m_ disposalMethod = disposalMethod; }
228 size_t getRequiredFrame() const { return m_requiredFrame; }
229 void setRequiredFrame(size_t req) { m_requiredFrame = req; }
230 unsigned delayTime() const { return m_delayTime; }
231 void setDelayTime(unsigned delay) { m_delayTime = delay; }
232 bool isComplete() const { return m_isComplete; }
233 void setComplete() { m_isComplete = true; }
234 bool isHeaderDefined() const { return m_isHeaderDefined; }
235 void setHeaderDefined() { m_isHeaderDefined = true; }
236 bool isDataSizeDefined() const { return m_isDataSizeDefined; }
237 int dataSize() const { return m_dataSize; }
238 void setDataSize(int size)
239 {
240 m_dataSize = size;
241 m_isDataSizeDefined = true;
242 }
243 bool progressiveDisplay() const { return m_progressiveDisplay; }
244 void setProgressiveDisplay(bool progressiveDisplay) { m_progressiveDisplay = progressiveDisplay; }
245 bool interlaced() const { return m_interlaced; }
246 void setInterlaced(bool interlaced) { m_interlaced = interlaced; }
247
248 void clearDecodeState() { m_lzwContext.reset(); }
249 const GIFColorMap& localColorMap() const { return m_localColorMap; }
250 GIFColorMap& localColorMap() { return m_localColorMap; }
251
252 private:
253 int m_frameId;
254 unsigned m_xOffset;
255 unsigned m_yOffset; // With respect to "screen" origin.
256 unsigned m_width;
257 unsigned m_height;
258 size_t m_transparentPixel; // Index of transparent pixel. Value is kNotFound if there is no transparent pixel.
259 SkCodecAnimation::DisposalMethod m_disposalMethod; // Restore to background, leave in place, etc.
260 size_t m_requiredFrame;
261 int m_dataSize;
262
263 bool m_progressiveDisplay; // If true, do Haeberli interlace hack.
264 bool m_interlaced; // True, if scanlines arrive interlaced order.
265
266 unsigned m_delayTime; // Display time, in milliseconds, for this image in a multi-image GIF.
267
268 std::unique_ptr<GIFLZWContext> m_lzwContext;
269 std::vector<sk_sp<SkData>> m_lzwBlocks; // LZW blocks for this frame.
270 GIFColorMap m_localColorMap;
271
272 size_t m_currentLzwBlock;
273 bool m_isComplete;
274 bool m_isHeaderDefined;
275 bool m_isDataSizeDefined;
276 };
277
278 class GIFImageReader final : public SkNoncopyable {
279 public:
280 // This takes ownership of stream.
281 GIFImageReader(SkStream* stream)
282 : m_client(nullptr)
283 , m_state(GIFType)
284 , m_bytesToConsume(6) // Number of bytes for GIF type, either "GIF87a" o r "GIF89a".
285 , m_version(0)
286 , m_screenWidth(0)
287 , m_screenHeight(0)
288 , m_loopCount(cLoopCountNotSeen)
289 , m_streamBuffer(stream)
290 , m_parseCompleted(false)
291 , m_firstFrameHasAlpha(false)
292 , m_firstFrameSupportsIndex8(false)
293 {
294 }
295
296 ~GIFImageReader()
297 {
298 }
299
300 void setClient(SkGifCodec* client) { m_client = client; }
301
302 unsigned screenWidth() const { return m_screenWidth; }
303 unsigned screenHeight() const { return m_screenHeight; }
304
305 // Option to pass to parse(). All enums are negative, because a non-negative value is used to
306 // indicate that the Reader should parse up to and including the frame indic ated.
307 enum GIFParseQuery {
308 // Parse enough to determine the size. Note that this parses the first f rame's header,
309 // since we may decide to expand based on the frame's dimensions.
310 GIFSizeQuery = -1,
311 // Parse to the end, so we know about all frames.
312 GIFFrameCountQuery = -2,
313 };
314
315 // Parse incoming GIF data stream into internal data structures.
316 // Non-negative values are used to indicate to parse through that frame.
317 // Return true if parsing has progressed or there is not enough data.
318 // Return false if a fatal error is encountered.
319 bool parse(GIFParseQuery);
320
321 // Decode the frame indicated by frameIndex.
322 // frameComplete will be set to true if the frame is completely decoded.
323 // The method returns false if there is an error.
324 bool decode(size_t frameIndex, bool* frameComplete);
325
326 size_t imagesCount() const
327 {
328 if (m_frames.empty())
329 return 0;
330
331 // This avoids counting an empty frame when the file is truncated right after
332 // GIFControlExtension but before GIFImageHeader.
333 // FIXME: This extra complexity is not necessary and we should just repo rt m_frames.size().
334 return m_frames.back()->isHeaderDefined() ? m_frames.size() : m_frames.s ize() - 1;
335 }
336 int loopCount() const { return m_loopCount; }
337
338 const GIFColorMap& globalColorMap() const
339 {
340 return m_globalColorMap;
341 }
342
343 const GIFFrameContext* frameContext(size_t index) const
344 {
345 return index < m_frames.size() ? m_frames[index].get() : 0;
346 }
347
348 void clearDecodeState() {
349 for (size_t index = 0; index < m_frames.size(); index++) {
350 m_frames[index]->clearDecodeState();
351 }
352 }
353
354 // Return the color table for frame index (which may be the global color tab le).
355 sk_sp<SkColorTable> getColorTable(SkColorType dstColorType, size_t index) co nst;
356
357 bool firstFrameHasAlpha() const { return m_firstFrameHasAlpha; }
358
359 bool firstFrameSupportsIndex8() const { return m_firstFrameSupportsIndex8; }
360
361 private:
362 // Requires that one byte has been buffered into m_streamBuffer.
363 unsigned char getOneByte() const {
364 return reinterpret_cast<const unsigned char*>(m_streamBuffer.get())[0];
365 }
366
367 void addFrameIfNecessary();
368 bool currentFrameIsFirstFrame() const
369 {
370 return m_frames.empty() || (m_frames.size() == 1u && !m_frames[0]->isCom plete());
371 }
372
373 // Unowned pointer
374 SkGifCodec* m_client;
375
376 // Parsing state machine.
377 GIFState m_state; // Current decoder master state.
378 size_t m_bytesToConsume; // Number of bytes to consume for next stage of par sing.
379
380 // Global (multi-image) state.
381 int m_version; // Either 89 for GIF89 or 87 for GIF87.
382 unsigned m_screenWidth; // Logical screen width & height.
383 unsigned m_screenHeight;
384 GIFColorMap m_globalColorMap;
385 int m_loopCount; // Netscape specific extension block to control the number of animation loops a GIF renders.
386
387 std::vector<std::unique_ptr<GIFFrameContext>> m_frames;
388
389 SkStreamBuffer m_streamBuffer;
390 bool m_parseCompleted;
391
392 // These values can be computed before we create a GIFFrameContext, so we
393 // store them here instead of on m_frames[0].
394 bool m_firstFrameHasAlpha;
395 bool m_firstFrameSupportsIndex8;
396 };
397
398 #endif
OLDNEW
« no previous file with comments | « tests/GifTest.cpp ('k') | third_party/gif/GIFImageReader.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698