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
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/
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
15 * The Original Code is Mozilla Communicator client code.
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.
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.
36 * ***** END LICENSE BLOCK ***** */
41 // Define ourselves as the clientPtr. Mozilla just hacked their C++ callback class into this old C decoder,
43 #include "GIFImageDecoder.h"
45 #define MAX_LZW_BITS 12
46 #define MAX_BITS 4097 /* 2^MAX_LZW_BITS+1 */
47 #define MAX_COLORS 256
48 #define MAX_HOLD_SIZE 256
50 const int cLoopCountNotSeen = -2;
53 The interface for the GIF87/89a decoder.
55 // List of possible parsing states
68 gif_control_extension,
74 gif_comment_extension,
75 gif_application_extension,
76 gif_netscape_extension_block,
77 gif_consume_netscape_extension,
81 struct GIFFrameReader {
82 /* LZW decoder state machine */
83 unsigned char *stackp; /* Current stack pointer */
87 int clear_code; /* Codeword used to trigger dictionary reset */
88 int avail; /* Index of next available slot in dictionary */
90 unsigned char firstchar;
91 int bits; /* Number of unread bits in "datum" */
92 int datum; /* 32-bit input buffer */
94 /* Output state machine */
95 int ipass; /* Interlace pass; Ranges 1-4 if interlaced. */
96 unsigned int rows_remaining; /* Rows remaining to be output */
97 unsigned int irow; /* Current output row, starting at zero */
98 unsigned char *rowbuf; /* Single scanline temporary buffer */
99 unsigned char *rowend; /* Pointer to end of rowbuf */
100 unsigned char *rowp; /* Current output pointer */
102 /* Parameters for image frame currently being decoded */
103 unsigned int x_offset, y_offset; /* With respect to "screen" origin */
104 unsigned int height, width;
105 int tpixel; /* Index of transparent pixel */
106 WebCore::RGBA32Buffer::FrameDisposalMethod disposal_method; /* Restore to background, leave in place, etc.*/
107 unsigned char *local_colormap; /* Per-image colormap */
108 int local_colormap_size; /* Size of local colormap array. */
110 bool is_local_colormap_defined : 1;
111 bool progressive_display : 1; /* If TRUE, do Haeberli interlace hack */
112 bool interlaced : 1; /* TRUE, if scanlines arrive interlaced order */
113 bool is_transparent : 1; /* TRUE, if tpixel is valid */
115 unsigned delay_time; /* Display time, in milliseconds,
116 for this image in a multi-image GIF */
119 unsigned short* prefix; /* LZW decoding tables */
120 unsigned char* suffix; /* LZW decoding tables */
121 unsigned char* stack; /* Base of LZW decoder stack */
126 datasize = codesize = codemask = clear_code = avail = oldcode = 0;
130 rows_remaining = irow = 0;
131 rowbuf = rowend = rowp = 0;
133 x_offset = y_offset = width = height = 0;
135 disposal_method = WebCore::RGBA32Buffer::DisposeNotSpecified;
138 local_colormap_size = 0;
139 is_local_colormap_defined = progressive_display = is_transparent = interlaced = false;
149 delete []local_colormap;
156 struct GIFImageReader {
157 WebCore::GIFImageDecoder* clientptr;
158 /* Parsing state machine */
159 gstate state; /* Current decoder master state */
160 unsigned bytes_to_consume; /* Number of bytes to accumulate */
161 unsigned bytes_in_hold; /* bytes accumulated so far*/
162 unsigned char hold[MAX_HOLD_SIZE]; /* Accumulation buffer */
163 unsigned char* global_colormap; /* (3* MAX_COLORS in size) Default colormap if local not supplied, 3 bytes for each color */
165 /* Global (multi-image) state */
166 int screen_bgcolor; /* Logical screen background color */
167 int version; /* Either 89 for GIF89 or 87 for GIF87 */
168 unsigned screen_width; /* Logical screen width & height */
169 unsigned screen_height;
170 int global_colormap_size; /* Size of global colormap array. */
171 int images_decoded; /* Counts completed frames for animated GIFs */
172 int images_count; /* Counted all frames seen so far (including incomplete frames) */
173 int loop_count; /* Netscape specific extension block to control
174 the number of animation loops a GIF renders. */
176 // Not really global, but convenient to locate here.
177 int count; /* Remaining # bytes in sub-block */
179 GIFFrameReader* frame_reader;
181 GIFImageReader(WebCore::GIFImageDecoder* client = 0) {
184 bytes_to_consume = 6;
189 screen_bgcolor = version = 0;
190 screen_width = screen_height = 0;
191 global_colormap_size = images_decoded = images_count = 0;
192 loop_count = cLoopCountNotSeen;
201 delete []global_colormap;
207 bool read(const unsigned char * buf, unsigned int numbytes,
208 WebCore::GIFImageDecoder::GIFQuery query = WebCore::GIFImageDecoder::GIFFullQuery, unsigned haltAtFrame = -1);
212 int do_lzw(const unsigned char *q);