OLD | NEW |
---|---|
(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); | |
joostouwerling
2016/10/14 15:02:44
I guess this is to be compatible with the WTF's kN
scroggo_chromium
2016/10/14 19:56:00
It's for the same reason that WTF made their decis
| |
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 void setRawData(const char* data, size_t size) | |
156 { | |
157 // FIXME: Can we avoid this copy? | |
158 m_rawData = SkData::MakeWithCopy(data, size); | |
159 SkASSERT(m_colors * BYTES_PER_COLORMAP_ENTRY == size); | |
160 m_isDefined = true; | |
161 } | |
162 bool isDefined() const { return m_isDefined; } | |
163 | |
164 // Build RGBA table using the data stream. | |
165 sk_sp<SkColorTable> buildTable(SkColorType dstColorType, size_t transparentP ixel) const; | |
166 | |
167 private: | |
168 bool m_isDefined; | |
169 size_t m_colors; | |
170 sk_sp<SkData> m_rawData; | |
171 mutable PackColorProc m_packColorProc; | |
172 mutable sk_sp<SkColorTable> m_table; | |
173 }; | |
174 | |
175 // LocalFrame output state machine. | |
176 struct GIFFrameContext : SkNoncopyable { | |
177 public: | |
178 GIFFrameContext(int id) | |
179 : m_frameId(id) | |
180 , m_xOffset(0) | |
181 , m_yOffset(0) | |
182 , m_width(0) | |
183 , m_height(0) | |
184 , m_transparentPixel(kNotFound) | |
185 , m_disposalMethod(SkCodecAnimation::Keep_DisposalMethod) | |
186 , m_requiredFrame(SkCodec::kIndependentFrame) | |
187 , m_dataSize(0) | |
188 , m_progressiveDisplay(false) | |
189 , m_interlaced(false) | |
190 , m_delayTime(0) | |
191 , m_currentLzwBlock(0) | |
192 , m_isComplete(false) | |
193 , m_isHeaderDefined(false) | |
194 , m_isDataSizeDefined(false) | |
195 { | |
196 } | |
197 | |
198 ~GIFFrameContext() | |
199 { | |
200 } | |
201 | |
202 void addLzwBlock(const void* data, size_t size) | |
203 { | |
204 m_lzwBlocks.push_back(SkData::MakeWithCopy(data, size)); | |
205 } | |
206 | |
207 bool decode(SkGifCodec* client, bool* frameDecoded); | |
208 | |
209 int frameId() const { return m_frameId; } | |
210 void setRect(unsigned x, unsigned y, unsigned width, unsigned height) | |
211 { | |
212 m_xOffset = x; | |
213 m_yOffset = y; | |
214 m_width = width; | |
215 m_height = height; | |
216 } | |
217 SkIRect frameRect() const { return SkIRect::MakeXYWH(m_xOffset, m_yOffset, m _width, m_height); } | |
218 unsigned xOffset() const { return m_xOffset; } | |
219 unsigned yOffset() const { return m_yOffset; } | |
220 unsigned width() const { return m_width; } | |
221 unsigned height() const { return m_height; } | |
222 size_t transparentPixel() const { return m_transparentPixel; } | |
223 void setTransparentPixel(size_t pixel) { m_transparentPixel = pixel; } | |
224 SkCodecAnimation::DisposalMethod getDisposalMethod() const { return m_dispos alMethod; } | |
225 void setDisposalMethod(SkCodecAnimation::DisposalMethod disposalMethod) { m_ disposalMethod = disposalMethod; } | |
226 size_t getRequiredFrame() const { return m_requiredFrame; } | |
227 void setRequiredFrame(size_t req) { m_requiredFrame = req; } | |
228 unsigned delayTime() const { return m_delayTime; } | |
229 void setDelayTime(unsigned delay) { m_delayTime = delay; } | |
230 bool isComplete() const { return m_isComplete; } | |
231 void setComplete() { m_isComplete = true; } | |
232 bool isHeaderDefined() const { return m_isHeaderDefined; } | |
233 void setHeaderDefined() { m_isHeaderDefined = true; } | |
234 bool isDataSizeDefined() const { return m_isDataSizeDefined; } | |
235 int dataSize() const { return m_dataSize; } | |
236 void setDataSize(int size) | |
237 { | |
238 m_dataSize = size; | |
239 m_isDataSizeDefined = true; | |
240 } | |
241 bool progressiveDisplay() const { return m_progressiveDisplay; } | |
242 void setProgressiveDisplay(bool progressiveDisplay) { m_progressiveDisplay = progressiveDisplay; } | |
243 bool interlaced() const { return m_interlaced; } | |
244 void setInterlaced(bool interlaced) { m_interlaced = interlaced; } | |
245 | |
246 void clearDecodeState() { m_lzwContext.reset(); } | |
247 const GIFColorMap& localColorMap() const { return m_localColorMap; } | |
248 GIFColorMap& localColorMap() { return m_localColorMap; } | |
249 | |
250 private: | |
251 int m_frameId; | |
252 unsigned m_xOffset; | |
253 unsigned m_yOffset; // With respect to "screen" origin. | |
254 unsigned m_width; | |
255 unsigned m_height; | |
256 size_t m_transparentPixel; // Index of transparent pixel. Value is kNotFound if there is no transparent pixel. | |
257 SkCodecAnimation::DisposalMethod m_disposalMethod; // Restore to background, leave in place, etc. | |
258 size_t m_requiredFrame; | |
259 int m_dataSize; | |
260 | |
261 bool m_progressiveDisplay; // If true, do Haeberli interlace hack. | |
262 bool m_interlaced; // True, if scanlines arrive interlaced order. | |
263 | |
264 unsigned m_delayTime; // Display time, in milliseconds, for this image in a multi-image GIF. | |
265 | |
266 std::unique_ptr<GIFLZWContext> m_lzwContext; | |
267 std::vector<sk_sp<SkData>> m_lzwBlocks; // LZW blocks for this frame. | |
268 GIFColorMap m_localColorMap; | |
269 | |
270 size_t m_currentLzwBlock; | |
271 bool m_isComplete; | |
272 bool m_isHeaderDefined; | |
273 bool m_isDataSizeDefined; | |
274 }; | |
275 | |
276 class GIFImageReader final : public SkNoncopyable { | |
277 public: | |
278 // This takes ownership of stream. | |
279 GIFImageReader(SkStream* stream) | |
280 : m_client(nullptr) | |
281 , m_state(GIFType) | |
282 , m_bytesToConsume(6) // Number of bytes for GIF type, either "GIF87a" o r "GIF89a". | |
283 , m_version(0) | |
284 , m_screenWidth(0) | |
285 , m_screenHeight(0) | |
286 , m_backgroundIndex(kNotFound) | |
287 , m_loopCount(cLoopCountNotSeen) | |
288 , m_streamBuffer(stream) | |
289 , m_parseCompleted(false) | |
290 { | |
291 } | |
292 | |
293 ~GIFImageReader() | |
294 { | |
295 } | |
296 | |
297 void setClient(SkGifCodec* client) { m_client = client; } | |
298 | |
299 unsigned screenWidth() const { return m_screenWidth; } | |
300 unsigned screenHeight() const { return m_screenHeight; } | |
301 | |
302 enum GIFParseQuery { | |
303 // Parse enough to determine the size. Note that this parses the first f rame's header, | |
304 // since we may decide to expand based on the frame's dimensions. | |
305 GIFSizeQuery = -1, | |
306 // Parse to the end, so we know about all frames. | |
307 GIFFrameCountQuery = -2, | |
308 }; | |
309 | |
310 // Parse incoming GIF data stream into internal data structures. | |
311 // Return true if parsing has progressed or there is not enough data. | |
312 // Return false if a fatal error is encountered. | |
313 bool parse(GIFParseQuery); | |
314 | |
315 bool decode(size_t frameIndex, bool* frameDecoded); | |
joostouwerling
2016/10/14 15:02:44
nit: Why did you decide to put the documentation f
scroggo_chromium
2016/10/14 19:56:00
The documentation just happened to already be in t
| |
316 | |
317 size_t imagesCount() const | |
318 { | |
319 if (m_frames.empty()) | |
320 return 0; | |
321 | |
322 // This avoids counting an empty frame when the file is truncated right after | |
323 // GIFControlExtension but before GIFImageHeader. | |
324 // FIXME: This extra complexity is not necessary and we should just repo rt m_frames.size(). | |
325 return m_frames.back()->isHeaderDefined() ? m_frames.size() : m_frames.s ize() - 1; | |
326 } | |
327 int loopCount() const { return m_loopCount; } | |
328 | |
329 const GIFColorMap& globalColorMap() const | |
330 { | |
331 return m_globalColorMap; | |
332 } | |
333 | |
334 const GIFFrameContext* frameContext(size_t index) const | |
335 { | |
336 return index < m_frames.size() ? m_frames[index].get() : 0; | |
337 } | |
338 | |
339 void clearDecodeState() { | |
340 for (size_t index = 0; index < m_frames.size(); index++) { | |
341 m_frames[index]->clearDecodeState(); | |
342 } | |
343 } | |
344 | |
345 // Return the color table for frame index (which may be the global color tab le). | |
346 sk_sp<SkColorTable> getColorTable(SkColorType dstColorType, size_t index) co nst; | |
347 | |
348 size_t getBackgroundIndex() const { return m_backgroundIndex; } | |
349 | |
350 private: | |
351 // Requires that one byte has been buffered into m_streamBuffer. | |
352 unsigned char getOneByte() const { | |
353 return reinterpret_cast<const unsigned char*>(m_streamBuffer.get())[0]; | |
354 } | |
355 | |
356 void addFrameIfNecessary(); | |
357 bool currentFrameIsFirstFrame() const | |
358 { | |
359 return m_frames.empty() || (m_frames.size() == 1u && !m_frames[0]->isCom plete()); | |
360 } | |
361 | |
362 // Unowned pointer | |
363 SkGifCodec* m_client; | |
364 | |
365 // Parsing state machine. | |
366 GIFState m_state; // Current decoder master state. | |
367 size_t m_bytesToConsume; // Number of bytes to consume for next stage of par sing. | |
368 | |
369 // Global (multi-image) state. | |
370 int m_version; // Either 89 for GIF89 or 87 for GIF87. | |
371 unsigned m_screenWidth; // Logical screen width & height. | |
372 unsigned m_screenHeight; | |
373 GIFColorMap m_globalColorMap; | |
374 size_t m_backgroundIndex; | |
375 int m_loopCount; // Netscape specific extension block to control the number of animation loops a GIF renders. | |
376 | |
377 std::vector<std::unique_ptr<GIFFrameContext>> m_frames; | |
378 | |
379 SkStreamBuffer m_streamBuffer; | |
380 bool m_parseCompleted; | |
381 }; | |
382 | |
383 #endif | |
OLD | NEW |