OLD | NEW |
---|---|
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | 1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 /* ***** BEGIN LICENSE BLOCK ***** | 2 /* ***** BEGIN LICENSE BLOCK ***** |
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
4 * | 4 * |
5 * The contents of this file are subject to the Mozilla Public License Version | 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 | 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 | 7 * the License. You may obtain a copy of the License at |
8 * http://www.mozilla.org/MPL/ | 8 * http://www.mozilla.org/MPL/ |
9 * | 9 * |
10 * Software distributed under the License is distributed on an "AS IS" basis, | 10 * Software distributed under the License is distributed on an "AS IS" basis, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
46 #include "SkCodecPriv.h" | 46 #include "SkCodecPriv.h" |
47 #include "SkCodecAnimation.h" | 47 #include "SkCodecAnimation.h" |
48 #include "SkColorTable.h" | 48 #include "SkColorTable.h" |
49 #include "SkData.h" | 49 #include "SkData.h" |
50 #include "SkImageInfo.h" | 50 #include "SkImageInfo.h" |
51 #include "SkStreamBuffer.h" | 51 #include "SkStreamBuffer.h" |
52 #include "../private/SkTArray.h" | 52 #include "../private/SkTArray.h" |
53 #include <memory> | 53 #include <memory> |
54 #include <vector> | 54 #include <vector> |
55 | 55 |
56 typedef SkTArray<unsigned char, true> GIFRow; | 56 typedef SkTArray<unsigned char, true> SkGIFRow; |
57 | 57 |
58 | 58 |
59 #define MAX_DICTIONARY_ENTRY_BITS 12 | 59 #define MAX_DICTIONARY_ENTRY_BITS 12 |
dogben
2016/10/25 18:20:12
Should these should be prefixed also?
scroggo
2016/10/25 18:33:53
Done.
| |
60 #define MAX_DICTIONARY_ENTRIES 4096 // 2^MAX_DICTIONARY_ENTRY_BITS | 60 #define MAX_DICTIONARY_ENTRIES 4096 // 2^MAX_DICTIONARY_ENTRY_BITS |
61 #define MAX_COLORS 256 | 61 #define MAX_COLORS 256 |
62 #define BYTES_PER_COLORMAP_ENTRY 3 | 62 #define BYTES_PER_COLORMAP_ENTRY 3 |
63 | 63 |
64 constexpr int cLoopCountNotSeen = -2; | 64 constexpr int cLoopCountNotSeen = -2; |
dogben
2016/10/25 18:20:13
Should be prefixed.
scroggo
2016/10/25 18:33:53
I moved these into classes, which should take care
| |
65 constexpr size_t kNotFound = static_cast<size_t>(-1); | 65 constexpr size_t kNotFound = static_cast<size_t>(-1); |
66 | 66 |
67 // List of possible parsing states. | 67 // List of possible parsing states. |
68 enum GIFState { | 68 enum SkGIFState { |
69 GIFType, | 69 SkGIFType, |
70 GIFGlobalHeader, | 70 SkGIFGlobalHeader, |
71 GIFGlobalColormap, | 71 SkGIFGlobalColormap, |
72 GIFImageStart, | 72 SkGIFImageStart, |
73 GIFImageHeader, | 73 SkGIFImageHeader, |
74 GIFImageColormap, | 74 SkGIFImageColormap, |
75 GIFImageBody, | 75 SkGIFImageBody, |
76 GIFLZWStart, | 76 SkGIFLZWStart, |
77 GIFLZW, | 77 SkGIFLZW, |
78 GIFSubBlock, | 78 SkGIFSubBlock, |
79 GIFExtension, | 79 SkGIFExtension, |
80 GIFControlExtension, | 80 SkGIFControlExtension, |
81 GIFConsumeBlock, | 81 SkGIFConsumeBlock, |
82 GIFSkipBlock, | 82 SkGIFSkipBlock, |
83 GIFDone, | 83 SkGIFDone, |
84 GIFCommentExtension, | 84 SkGIFCommentExtension, |
85 GIFApplicationExtension, | 85 SkGIFApplicationExtension, |
86 GIFNetscapeExtensionBlock, | 86 SkGIFNetscapeExtensionBlock, |
87 GIFConsumeNetscapeExtension, | 87 SkGIFConsumeNetscapeExtension, |
88 GIFConsumeComment | 88 SkGIFConsumeComment |
89 }; | 89 }; |
90 | 90 |
91 struct GIFFrameContext; | 91 struct SkGIFFrameContext; |
92 | 92 |
93 // LZW decoder state machine. | 93 // LZW decoder state machine. |
94 class GIFLZWContext final : public SkNoncopyable { | 94 class SkGIFLZWContext final : public SkNoncopyable { |
95 public: | 95 public: |
96 GIFLZWContext(SkGifCodec* client, const GIFFrameContext* frameContext) | 96 SkGIFLZWContext(SkGifCodec* client, const SkGIFFrameContext* frameContext) |
97 : codesize(0) | 97 : codesize(0) |
98 , codemask(0) | 98 , codemask(0) |
99 , clearCode(0) | 99 , clearCode(0) |
100 , avail(0) | 100 , avail(0) |
101 , oldcode(0) | 101 , oldcode(0) |
102 , firstchar(0) | 102 , firstchar(0) |
103 , bits(0) | 103 , bits(0) |
104 , datum(0) | 104 , datum(0) |
105 , ipass(0) | 105 , ipass(0) |
106 , irow(0) | 106 , irow(0) |
(...skipping 18 matching lines...) Expand all Loading... | |
125 unsigned char firstchar; | 125 unsigned char firstchar; |
126 int bits; // Number of unread bits in "datum". | 126 int bits; // Number of unread bits in "datum". |
127 int datum; // 32-bit input buffer. | 127 int datum; // 32-bit input buffer. |
128 int ipass; // Interlace pass; Ranges 1-4 if interlaced. | 128 int ipass; // Interlace pass; Ranges 1-4 if interlaced. |
129 size_t irow; // Current output row, starting at zero. | 129 size_t irow; // Current output row, starting at zero. |
130 size_t rowsRemaining; // Rows remaining to be output. | 130 size_t rowsRemaining; // Rows remaining to be output. |
131 | 131 |
132 unsigned short prefix[MAX_DICTIONARY_ENTRIES]; | 132 unsigned short prefix[MAX_DICTIONARY_ENTRIES]; |
133 unsigned char suffix[MAX_DICTIONARY_ENTRIES]; | 133 unsigned char suffix[MAX_DICTIONARY_ENTRIES]; |
134 unsigned short suffixLength[MAX_DICTIONARY_ENTRIES]; | 134 unsigned short suffixLength[MAX_DICTIONARY_ENTRIES]; |
135 GIFRow rowBuffer; // Single scanline temporary buffer. | 135 SkGIFRow rowBuffer; // Single scanline temporary buffer. |
136 unsigned char* rowIter; | 136 unsigned char* rowIter; |
137 | 137 |
138 SkGifCodec* const m_client; | 138 SkGifCodec* const m_client; |
139 const GIFFrameContext* m_frameContext; | 139 const SkGIFFrameContext* m_frameContext; |
140 }; | 140 }; |
141 | 141 |
142 class GIFColorMap final { | 142 class SkGIFColorMap final { |
143 public: | 143 public: |
144 GIFColorMap() | 144 SkGIFColorMap() |
145 : m_isDefined(false) | 145 : m_isDefined(false) |
146 , m_colors(0) | 146 , m_colors(0) |
147 , m_packColorProc(nullptr) | 147 , m_packColorProc(nullptr) |
148 { | 148 { |
149 } | 149 } |
150 | 150 |
151 void setNumColors(size_t colors) { | 151 void setNumColors(size_t colors) { |
152 m_colors = colors; | 152 m_colors = colors; |
153 } | 153 } |
154 | 154 |
(...skipping 13 matching lines...) Expand all Loading... | |
168 | 168 |
169 private: | 169 private: |
170 bool m_isDefined; | 170 bool m_isDefined; |
171 size_t m_colors; | 171 size_t m_colors; |
172 sk_sp<SkData> m_rawData; | 172 sk_sp<SkData> m_rawData; |
173 mutable PackColorProc m_packColorProc; | 173 mutable PackColorProc m_packColorProc; |
174 mutable sk_sp<SkColorTable> m_table; | 174 mutable sk_sp<SkColorTable> m_table; |
175 }; | 175 }; |
176 | 176 |
177 // LocalFrame output state machine. | 177 // LocalFrame output state machine. |
178 struct GIFFrameContext : SkNoncopyable { | 178 struct SkGIFFrameContext : SkNoncopyable { |
179 public: | 179 public: |
180 GIFFrameContext(int id) | 180 SkGIFFrameContext(int id) |
181 : m_frameId(id) | 181 : m_frameId(id) |
182 , m_xOffset(0) | 182 , m_xOffset(0) |
183 , m_yOffset(0) | 183 , m_yOffset(0) |
184 , m_width(0) | 184 , m_width(0) |
185 , m_height(0) | 185 , m_height(0) |
186 , m_transparentPixel(kNotFound) | 186 , m_transparentPixel(kNotFound) |
187 , m_disposalMethod(SkCodecAnimation::Keep_DisposalMethod) | 187 , m_disposalMethod(SkCodecAnimation::Keep_DisposalMethod) |
188 , m_requiredFrame(SkCodec::kNone) | 188 , m_requiredFrame(SkCodec::kNone) |
189 , m_dataSize(0) | 189 , m_dataSize(0) |
190 , m_progressiveDisplay(false) | 190 , m_progressiveDisplay(false) |
191 , m_interlaced(false) | 191 , m_interlaced(false) |
192 , m_delayTime(0) | 192 , m_delayTime(0) |
193 , m_currentLzwBlock(0) | 193 , m_currentLzwBlock(0) |
194 , m_isComplete(false) | 194 , m_isComplete(false) |
195 , m_isHeaderDefined(false) | 195 , m_isHeaderDefined(false) |
196 , m_isDataSizeDefined(false) | 196 , m_isDataSizeDefined(false) |
197 { | 197 { |
198 } | 198 } |
199 | 199 |
200 ~GIFFrameContext() | 200 ~SkGIFFrameContext() |
201 { | 201 { |
202 } | 202 } |
203 | 203 |
204 void addLzwBlock(const void* data, size_t size) | 204 void addLzwBlock(const void* data, size_t size) |
205 { | 205 { |
206 m_lzwBlocks.push_back(SkData::MakeWithCopy(data, size)); | 206 m_lzwBlocks.push_back(SkData::MakeWithCopy(data, size)); |
207 } | 207 } |
208 | 208 |
209 bool decode(SkGifCodec* client, bool* frameDecoded); | 209 bool decode(SkGifCodec* client, bool* frameDecoded); |
210 | 210 |
(...skipping 28 matching lines...) Expand all Loading... | |
239 { | 239 { |
240 m_dataSize = size; | 240 m_dataSize = size; |
241 m_isDataSizeDefined = true; | 241 m_isDataSizeDefined = true; |
242 } | 242 } |
243 bool progressiveDisplay() const { return m_progressiveDisplay; } | 243 bool progressiveDisplay() const { return m_progressiveDisplay; } |
244 void setProgressiveDisplay(bool progressiveDisplay) { m_progressiveDisplay = progressiveDisplay; } | 244 void setProgressiveDisplay(bool progressiveDisplay) { m_progressiveDisplay = progressiveDisplay; } |
245 bool interlaced() const { return m_interlaced; } | 245 bool interlaced() const { return m_interlaced; } |
246 void setInterlaced(bool interlaced) { m_interlaced = interlaced; } | 246 void setInterlaced(bool interlaced) { m_interlaced = interlaced; } |
247 | 247 |
248 void clearDecodeState() { m_lzwContext.reset(); } | 248 void clearDecodeState() { m_lzwContext.reset(); } |
249 const GIFColorMap& localColorMap() const { return m_localColorMap; } | 249 const SkGIFColorMap& localColorMap() const { return m_localColorMap; } |
250 GIFColorMap& localColorMap() { return m_localColorMap; } | 250 SkGIFColorMap& localColorMap() { return m_localColorMap; } |
251 | 251 |
252 private: | 252 private: |
253 int m_frameId; | 253 int m_frameId; |
254 unsigned m_xOffset; | 254 unsigned m_xOffset; |
255 unsigned m_yOffset; // With respect to "screen" origin. | 255 unsigned m_yOffset; // With respect to "screen" origin. |
256 unsigned m_width; | 256 unsigned m_width; |
257 unsigned m_height; | 257 unsigned m_height; |
258 size_t m_transparentPixel; // Index of transparent pixel. Value is kNotFound if there is no transparent pixel. | 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. | 259 SkCodecAnimation::DisposalMethod m_disposalMethod; // Restore to background, leave in place, etc. |
260 size_t m_requiredFrame; | 260 size_t m_requiredFrame; |
261 int m_dataSize; | 261 int m_dataSize; |
262 | 262 |
263 bool m_progressiveDisplay; // If true, do Haeberli interlace hack. | 263 bool m_progressiveDisplay; // If true, do Haeberli interlace hack. |
264 bool m_interlaced; // True, if scanlines arrive interlaced order. | 264 bool m_interlaced; // True, if scanlines arrive interlaced order. |
265 | 265 |
266 unsigned m_delayTime; // Display time, in milliseconds, for this image in a multi-image GIF. | 266 unsigned m_delayTime; // Display time, in milliseconds, for this image in a multi-image GIF. |
267 | 267 |
268 std::unique_ptr<GIFLZWContext> m_lzwContext; | 268 std::unique_ptr<SkGIFLZWContext> m_lzwContext; |
269 std::vector<sk_sp<SkData>> m_lzwBlocks; // LZW blocks for this frame. | 269 std::vector<sk_sp<SkData>> m_lzwBlocks; // LZW blocks for this frame. |
270 GIFColorMap m_localColorMap; | 270 SkGIFColorMap m_localColorMap; |
271 | 271 |
272 size_t m_currentLzwBlock; | 272 size_t m_currentLzwBlock; |
273 bool m_isComplete; | 273 bool m_isComplete; |
274 bool m_isHeaderDefined; | 274 bool m_isHeaderDefined; |
275 bool m_isDataSizeDefined; | 275 bool m_isDataSizeDefined; |
276 }; | 276 }; |
277 | 277 |
278 class SkGifImageReader final : public SkNoncopyable { | 278 class SkGifImageReader final : public SkNoncopyable { |
279 public: | 279 public: |
280 // This takes ownership of stream. | 280 // This takes ownership of stream. |
281 SkGifImageReader(SkStream* stream) | 281 SkGifImageReader(SkStream* stream) |
282 : m_client(nullptr) | 282 : m_client(nullptr) |
283 , m_state(GIFType) | 283 , m_state(SkGIFType) |
284 , m_bytesToConsume(6) // Number of bytes for GIF type, either "GIF87a" o r "GIF89a". | 284 , m_bytesToConsume(6) // Number of bytes for GIF type, either "GIF87a" o r "GIF89a". |
285 , m_version(0) | 285 , m_version(0) |
286 , m_screenWidth(0) | 286 , m_screenWidth(0) |
287 , m_screenHeight(0) | 287 , m_screenHeight(0) |
288 , m_loopCount(cLoopCountNotSeen) | 288 , m_loopCount(cLoopCountNotSeen) |
289 , m_streamBuffer(stream) | 289 , m_streamBuffer(stream) |
290 , m_parseCompleted(false) | 290 , m_parseCompleted(false) |
291 , m_firstFrameHasAlpha(false) | 291 , m_firstFrameHasAlpha(false) |
292 , m_firstFrameSupportsIndex8(false) | 292 , m_firstFrameSupportsIndex8(false) |
293 { | 293 { |
294 } | 294 } |
295 | 295 |
296 ~SkGifImageReader() | 296 ~SkGifImageReader() |
297 { | 297 { |
298 } | 298 } |
299 | 299 |
300 void setClient(SkGifCodec* client) { m_client = client; } | 300 void setClient(SkGifCodec* client) { m_client = client; } |
301 | 301 |
302 unsigned screenWidth() const { return m_screenWidth; } | 302 unsigned screenWidth() const { return m_screenWidth; } |
303 unsigned screenHeight() const { return m_screenHeight; } | 303 unsigned screenHeight() const { return m_screenHeight; } |
304 | 304 |
305 // Option to pass to parse(). All enums are negative, because a non-negative value is used to | 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. | 306 // indicate that the Reader should parse up to and including the frame indic ated. |
307 enum GIFParseQuery { | 307 enum SkGIFParseQuery { |
308 // Parse enough to determine the size. Note that this parses the first f rame's header, | 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. | 309 // since we may decide to expand based on the frame's dimensions. |
310 GIFSizeQuery = -1, | 310 SkGIFSizeQuery = -1, |
311 // Parse to the end, so we know about all frames. | 311 // Parse to the end, so we know about all frames. |
312 GIFFrameCountQuery = -2, | 312 SkGIFFrameCountQuery = -2, |
313 }; | 313 }; |
314 | 314 |
315 // Parse incoming GIF data stream into internal data structures. | 315 // Parse incoming GIF data stream into internal data structures. |
316 // Non-negative values are used to indicate to parse through that frame. | 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. | 317 // Return true if parsing has progressed or there is not enough data. |
318 // Return false if a fatal error is encountered. | 318 // Return false if a fatal error is encountered. |
319 bool parse(GIFParseQuery); | 319 bool parse(SkGIFParseQuery); |
320 | 320 |
321 // Decode the frame indicated by frameIndex. | 321 // Decode the frame indicated by frameIndex. |
322 // frameComplete will be set to true if the frame is completely decoded. | 322 // frameComplete will be set to true if the frame is completely decoded. |
323 // The method returns false if there is an error. | 323 // The method returns false if there is an error. |
324 bool decode(size_t frameIndex, bool* frameComplete); | 324 bool decode(size_t frameIndex, bool* frameComplete); |
325 | 325 |
326 size_t imagesCount() const | 326 size_t imagesCount() const |
327 { | 327 { |
328 if (m_frames.empty()) | 328 if (m_frames.empty()) |
329 return 0; | 329 return 0; |
330 | 330 |
331 // This avoids counting an empty frame when the file is truncated right after | 331 // This avoids counting an empty frame when the file is truncated right after |
332 // GIFControlExtension but before GIFImageHeader. | 332 // SkGIFControlExtension but before SkGIFImageHeader. |
333 // FIXME: This extra complexity is not necessary and we should just repo rt m_frames.size(). | 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; | 334 return m_frames.back()->isHeaderDefined() ? m_frames.size() : m_frames.s ize() - 1; |
335 } | 335 } |
336 int loopCount() const { return m_loopCount; } | 336 int loopCount() const { return m_loopCount; } |
337 | 337 |
338 const GIFColorMap& globalColorMap() const | 338 const SkGIFColorMap& globalColorMap() const |
339 { | 339 { |
340 return m_globalColorMap; | 340 return m_globalColorMap; |
341 } | 341 } |
342 | 342 |
343 const GIFFrameContext* frameContext(size_t index) const | 343 const SkGIFFrameContext* frameContext(size_t index) const |
344 { | 344 { |
345 return index < m_frames.size() ? m_frames[index].get() : 0; | 345 return index < m_frames.size() ? m_frames[index].get() : 0; |
346 } | 346 } |
347 | 347 |
348 void clearDecodeState() { | 348 void clearDecodeState() { |
349 for (size_t index = 0; index < m_frames.size(); index++) { | 349 for (size_t index = 0; index < m_frames.size(); index++) { |
350 m_frames[index]->clearDecodeState(); | 350 m_frames[index]->clearDecodeState(); |
351 } | 351 } |
352 } | 352 } |
353 | 353 |
(...skipping 13 matching lines...) Expand all Loading... | |
367 void addFrameIfNecessary(); | 367 void addFrameIfNecessary(); |
368 bool currentFrameIsFirstFrame() const | 368 bool currentFrameIsFirstFrame() const |
369 { | 369 { |
370 return m_frames.empty() || (m_frames.size() == 1u && !m_frames[0]->isCom plete()); | 370 return m_frames.empty() || (m_frames.size() == 1u && !m_frames[0]->isCom plete()); |
371 } | 371 } |
372 | 372 |
373 // Unowned pointer | 373 // Unowned pointer |
374 SkGifCodec* m_client; | 374 SkGifCodec* m_client; |
375 | 375 |
376 // Parsing state machine. | 376 // Parsing state machine. |
377 GIFState m_state; // Current decoder master state. | 377 SkGIFState m_state; // Current decoder master state. |
378 size_t m_bytesToConsume; // Number of bytes to consume for next stage of par sing. | 378 size_t m_bytesToConsume; // Number of bytes to consume for next stage of par sing. |
379 | 379 |
380 // Global (multi-image) state. | 380 // Global (multi-image) state. |
381 int m_version; // Either 89 for GIF89 or 87 for GIF87. | 381 int m_version; // Either 89 for GIF89 or 87 for GIF87. |
382 unsigned m_screenWidth; // Logical screen width & height. | 382 unsigned m_screenWidth; // Logical screen width & height. |
383 unsigned m_screenHeight; | 383 unsigned m_screenHeight; |
384 GIFColorMap m_globalColorMap; | 384 SkGIFColorMap m_globalColorMap; |
385 int m_loopCount; // Netscape specific extension block to control the number of animation loops a GIF renders. | 385 int m_loopCount; // Netscape specific extension block to control the number of animation loops a GIF renders. |
386 | 386 |
387 std::vector<std::unique_ptr<GIFFrameContext>> m_frames; | 387 std::vector<std::unique_ptr<SkGIFFrameContext>> m_frames; |
388 | 388 |
389 SkStreamBuffer m_streamBuffer; | 389 SkStreamBuffer m_streamBuffer; |
390 bool m_parseCompleted; | 390 bool m_parseCompleted; |
391 | 391 |
392 // These values can be computed before we create a GIFFrameContext, so we | 392 // These values can be computed before we create a SkGIFFrameContext, so we |
393 // store them here instead of on m_frames[0]. | 393 // store them here instead of on m_frames[0]. |
394 bool m_firstFrameHasAlpha; | 394 bool m_firstFrameHasAlpha; |
395 bool m_firstFrameSupportsIndex8; | 395 bool m_firstFrameSupportsIndex8; |
396 }; | 396 }; |
397 | 397 |
398 #endif | 398 #endif |
OLD | NEW |