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 | |
42 // class into this old C decoder, so we will too. | |
43 #include "platform/image-decoders/FastSharedBufferReader.h" | |
44 #include "platform/image-decoders/gif/GIFImageDecoder.h" | |
45 #include "wtf/Allocator.h" | |
46 #include "wtf/Noncopyable.h" | |
47 #include "wtf/Vector.h" | |
48 #include <memory> | |
49 | |
50 #define MAX_DICTIONARY_ENTRY_BITS 12 | |
51 #define MAX_DICTIONARY_ENTRIES 4096 // 2^MAX_DICTIONARY_ENTRY_BITS | |
52 #define MAX_COLORS 256 | |
53 #define BYTES_PER_COLORMAP_ENTRY 3 | |
54 | |
55 const int cLoopCountNotSeen = -2; | |
56 | |
57 // List of possible parsing states. | |
58 enum GIFState { | |
59 GIFType, | |
60 GIFGlobalHeader, | |
61 GIFGlobalColormap, | |
62 GIFImageStart, | |
63 GIFImageHeader, | |
64 GIFImageColormap, | |
65 GIFImageBody, | |
66 GIFLZWStart, | |
67 GIFLZW, | |
68 GIFSubBlock, | |
69 GIFExtension, | |
70 GIFControlExtension, | |
71 GIFConsumeBlock, | |
72 GIFSkipBlock, | |
73 GIFDone, | |
74 GIFCommentExtension, | |
75 GIFApplicationExtension, | |
76 GIFNetscapeExtensionBlock, | |
77 GIFConsumeNetscapeExtension, | |
78 GIFConsumeComment | |
79 }; | |
80 | |
81 struct GIFFrameContext; | |
82 | |
83 // LZW decoder state machine. | |
84 class GIFLZWContext final { | |
85 USING_FAST_MALLOC(GIFLZWContext); | |
86 WTF_MAKE_NONCOPYABLE(GIFLZWContext); | |
87 | |
88 public: | |
89 GIFLZWContext(blink::GIFImageDecoder* client, | |
90 const GIFFrameContext* frameContext) | |
91 : codesize(0), | |
92 codemask(0), | |
93 clearCode(0), | |
94 avail(0), | |
95 oldcode(0), | |
96 firstchar(0), | |
97 bits(0), | |
98 datum(0), | |
99 ipass(0), | |
100 irow(0), | |
101 rowsRemaining(0), | |
102 rowIter(0), | |
103 m_client(client), | |
104 m_frameContext(frameContext) {} | |
105 | |
106 bool prepareToDecode(); | |
107 bool outputRow(GIFRow::const_iterator rowBegin); | |
108 bool doLZW(const unsigned char* block, size_t bytesInBlock); | |
109 bool hasRemainingRows() { return rowsRemaining; } | |
110 | |
111 private: | |
112 // LZW decoding states and output states. | |
113 int codesize; | |
114 int codemask; | |
115 int clearCode; // Codeword used to trigger dictionary reset. | |
116 int avail; // Index of next available slot in dictionary. | |
117 int oldcode; | |
118 unsigned char firstchar; | |
119 int bits; // Number of unread bits in "datum". | |
120 int datum; // 32-bit input buffer. | |
121 int ipass; // Interlace pass; Ranges 1-4 if interlaced. | |
122 size_t irow; // Current output row, starting at zero. | |
123 size_t rowsRemaining; // Rows remaining to be output. | |
124 | |
125 unsigned short prefix[MAX_DICTIONARY_ENTRIES]; | |
126 unsigned char suffix[MAX_DICTIONARY_ENTRIES]; | |
127 unsigned short suffixLength[MAX_DICTIONARY_ENTRIES]; | |
128 GIFRow rowBuffer; // Single scanline temporary buffer. | |
129 GIFRow::iterator rowIter; | |
130 | |
131 // Initialized during construction and read-only. | |
132 blink::GIFImageDecoder* m_client; | |
133 const GIFFrameContext* m_frameContext; | |
134 }; | |
135 | |
136 // Data structure for one LZW block. | |
137 struct GIFLZWBlock { | |
138 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); | |
139 | |
140 public: | |
141 GIFLZWBlock(size_t position, size_t size) | |
142 : blockPosition(position), blockSize(size) {} | |
143 | |
144 size_t blockPosition; | |
145 size_t blockSize; | |
146 }; | |
147 | |
148 class GIFColorMap final { | |
149 DISALLOW_NEW(); | |
150 | |
151 public: | |
152 typedef Vector<blink::ImageFrame::PixelData> Table; | |
153 | |
154 GIFColorMap() : m_isDefined(false), m_position(0), m_colors(0) {} | |
155 | |
156 // Set position and number of colors for the RGB table in the data stream. | |
157 void setTablePositionAndSize(size_t position, size_t colors) { | |
158 m_position = position; | |
159 m_colors = colors; | |
160 } | |
161 void setDefined() { m_isDefined = true; } | |
162 bool isDefined() const { return m_isDefined; } | |
163 | |
164 // Build RGBA table using the data stream. | |
165 void buildTable(blink::FastSharedBufferReader*); | |
166 const Table& getTable() const { return m_table; } | |
167 | |
168 private: | |
169 bool m_isDefined; | |
170 size_t m_position; | |
171 size_t m_colors; | |
172 Table m_table; | |
173 }; | |
174 | |
175 // LocalFrame output state machine. | |
176 struct GIFFrameContext { | |
177 USING_FAST_MALLOC(GIFFrameContext); | |
178 WTF_MAKE_NONCOPYABLE(GIFFrameContext); | |
179 | |
180 public: | |
181 GIFFrameContext(int id) | |
182 : m_frameId(id), | |
183 m_xOffset(0), | |
184 m_yOffset(0), | |
185 m_width(0), | |
186 m_height(0), | |
187 m_transparentPixel(kNotFound), | |
188 m_disposalMethod(blink::ImageFrame::DisposeNotSpecified), | |
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 ~GIFFrameContext() {} | |
199 | |
200 void addLzwBlock(size_t position, size_t size) { | |
201 m_lzwBlocks.push_back(GIFLZWBlock(position, size)); | |
202 } | |
203 | |
204 bool decode(blink::FastSharedBufferReader*, | |
205 blink::GIFImageDecoder* client, | |
206 bool* frameDecoded); | |
207 | |
208 int frameId() const { return m_frameId; } | |
209 void setRect(unsigned x, unsigned y, unsigned width, unsigned height) { | |
210 m_xOffset = x; | |
211 m_yOffset = y; | |
212 m_width = width; | |
213 m_height = height; | |
214 } | |
215 blink::IntRect frameRect() const { | |
216 return blink::IntRect(m_xOffset, m_yOffset, m_width, m_height); | |
217 } | |
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 blink::ImageFrame::DisposalMethod getDisposalMethod() const { | |
225 return m_disposalMethod; | |
226 } | |
227 void setDisposalMethod(blink::ImageFrame::DisposalMethod disposalMethod) { | |
228 m_disposalMethod = disposalMethod; | |
229 } | |
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 m_dataSize = size; | |
240 m_isDataSizeDefined = true; | |
241 } | |
242 bool progressiveDisplay() const { return m_progressiveDisplay; } | |
243 void setProgressiveDisplay(bool progressiveDisplay) { | |
244 m_progressiveDisplay = progressiveDisplay; | |
245 } | |
246 bool interlaced() const { return m_interlaced; } | |
247 void setInterlaced(bool interlaced) { m_interlaced = interlaced; } | |
248 | |
249 void clearDecodeState() { m_lzwContext.reset(); } | |
250 const GIFColorMap& localColorMap() const { return m_localColorMap; } | |
251 GIFColorMap& localColorMap() { return m_localColorMap; } | |
252 | |
253 private: | |
254 int m_frameId; | |
255 unsigned m_xOffset; | |
256 unsigned m_yOffset; // With respect to "screen" origin. | |
257 unsigned m_width; | |
258 unsigned m_height; | |
259 size_t m_transparentPixel; // Index of transparent pixel. Value is kNotFound | |
260 // if there is no transparent pixel. | |
261 blink::ImageFrame::DisposalMethod | |
262 m_disposalMethod; // Restore to background, leave in place, etc. | |
263 int m_dataSize; | |
264 | |
265 bool m_progressiveDisplay; // If true, do Haeberli interlace hack. | |
266 bool m_interlaced; // True, if scanlines arrive interlaced order. | |
267 | |
268 unsigned m_delayTime; // Display time, in milliseconds, for this image in a | |
269 // multi-image GIF. | |
270 | |
271 std::unique_ptr<GIFLZWContext> m_lzwContext; | |
272 Vector<GIFLZWBlock> m_lzwBlocks; // LZW blocks for this frame. | |
273 GIFColorMap m_localColorMap; | |
274 | |
275 size_t m_currentLzwBlock; | |
276 bool m_isComplete; | |
277 bool m_isHeaderDefined; | |
278 bool m_isDataSizeDefined; | |
279 }; | |
280 | |
281 class PLATFORM_EXPORT GIFImageReader final { | |
282 USING_FAST_MALLOC(GIFImageReader); | |
283 WTF_MAKE_NONCOPYABLE(GIFImageReader); | |
284 | |
285 public: | |
286 GIFImageReader(blink::GIFImageDecoder* client = 0) | |
287 : m_client(client), | |
288 m_state(GIFType), | |
289 // Number of bytes for GIF type, either "GIF87a" or "GIF89a". | |
290 m_bytesToConsume(6), | |
291 m_bytesRead(0), | |
292 m_version(0), | |
293 m_screenWidth(0), | |
294 m_screenHeight(0), | |
295 m_sentSizeToClient(false), | |
296 m_loopCount(cLoopCountNotSeen), | |
297 m_parseCompleted(false) {} | |
298 | |
299 ~GIFImageReader() {} | |
300 | |
301 void setData(PassRefPtr<blink::SegmentReader> data) { m_data = data; } | |
302 bool parse(blink::GIFImageDecoder::GIFParseQuery); | |
303 bool decode(size_t frameIndex); | |
304 | |
305 size_t imagesCount() const { | |
306 if (m_frames.isEmpty()) | |
307 return 0; | |
308 | |
309 // This avoids counting an empty frame when the file is truncated right | |
310 // after GIFControlExtension but before GIFImageHeader. | |
311 // FIXME: This extra complexity is not necessary and we should just report | |
312 // m_frames.size(). | |
313 return m_frames.back()->isHeaderDefined() ? m_frames.size() | |
314 : m_frames.size() - 1; | |
315 } | |
316 int loopCount() const { return m_loopCount; } | |
317 | |
318 const GIFColorMap& globalColorMap() const { return m_globalColorMap; } | |
319 | |
320 const GIFFrameContext* frameContext(size_t index) const { | |
321 return index < m_frames.size() ? m_frames[index].get() : 0; | |
322 } | |
323 | |
324 bool parseCompleted() const { return m_parseCompleted; } | |
325 | |
326 void clearDecodeState(size_t index) { m_frames[index]->clearDecodeState(); } | |
327 | |
328 private: | |
329 bool parseData(size_t dataPosition, | |
330 size_t len, | |
331 blink::GIFImageDecoder::GIFParseQuery); | |
332 void setRemainingBytes(size_t); | |
333 | |
334 void addFrameIfNecessary(); | |
335 bool currentFrameIsFirstFrame() const { | |
336 return m_frames.isEmpty() || | |
337 (m_frames.size() == 1u && !m_frames[0]->isComplete()); | |
338 } | |
339 | |
340 blink::GIFImageDecoder* m_client; | |
341 | |
342 // Parsing state machine. | |
343 GIFState m_state; // Current decoder master state. | |
344 size_t m_bytesToConsume; // Number of bytes to consume for next stage of | |
345 // parsing. | |
346 size_t m_bytesRead; // Number of bytes processed. | |
347 | |
348 // Global (multi-image) state. | |
349 int m_version; // Either 89 for GIF89 or 87 for GIF87. | |
350 unsigned m_screenWidth; // Logical screen width & height. | |
351 unsigned m_screenHeight; | |
352 bool m_sentSizeToClient; | |
353 GIFColorMap m_globalColorMap; | |
354 int m_loopCount; // Netscape specific extension block to control the number | |
355 // of animation loops a GIF renders. | |
356 | |
357 Vector<std::unique_ptr<GIFFrameContext>> m_frames; | |
358 | |
359 RefPtr<blink::SegmentReader> m_data; | |
360 bool m_parseCompleted; | |
361 }; | |
362 | |
363 #endif | |
OLD | NEW |