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
51 The interface for the GIF87/89a decoder.
53 // List of possible parsing states
66 gif_control_extension,
72 gif_comment_extension,
73 gif_application_extension,
74 gif_netscape_extension_block,
75 gif_consume_netscape_extension,
79 /* "Disposal" method indicates how the image should be handled in the
80 framebuffer before the subsequent image is displayed. */
83 DISPOSE_NOT_SPECIFIED = 0,
84 DISPOSE_KEEP = 1, /* Leave it in the framebuffer */
85 DISPOSE_OVERWRITE_BGCOLOR = 2, /* Overwrite with background color */
86 DISPOSE_OVERWRITE_PREVIOUS = 3 /* Save-under */
89 struct GIFFrameReader {
90 /* LZW decoder state machine */
91 unsigned char *stackp; /* Current stack pointer */
95 int clear_code; /* Codeword used to trigger dictionary reset */
96 int avail; /* Index of next available slot in dictionary */
98 unsigned char firstchar;
99 int bits; /* Number of unread bits in "datum" */
100 int datum; /* 32-bit input buffer */
102 /* Output state machine */
103 int ipass; /* Interlace pass; Ranges 1-4 if interlaced. */
104 unsigned int rows_remaining; /* Rows remaining to be output */
105 unsigned int irow; /* Current output row, starting at zero */
106 unsigned char *rowbuf; /* Single scanline temporary buffer */
107 unsigned char *rowend; /* Pointer to end of rowbuf */
108 unsigned char *rowp; /* Current output pointer */
110 /* Parameters for image frame currently being decoded */
111 unsigned int x_offset, y_offset; /* With respect to "screen" origin */
112 unsigned int height, width;
113 int tpixel; /* Index of transparent pixel */
114 gdispose disposal_method; /* Restore to background, leave in place, etc.*/
115 unsigned char *local_colormap; /* Per-image colormap */
116 int local_colormap_size; /* Size of local colormap array. */
118 bool is_local_colormap_defined : 1;
119 bool progressive_display : 1; /* If TRUE, do Haeberli interlace hack */
120 bool interlaced : 1; /* TRUE, if scanlines arrive interlaced order */
121 bool is_transparent : 1; /* TRUE, if tpixel is valid */
123 unsigned delay_time; /* Display time, in milliseconds,
124 for this image in a multi-image GIF */
127 unsigned short* prefix; /* LZW decoding tables */
128 unsigned char* suffix; /* LZW decoding tables */
129 unsigned char* stack; /* Base of LZW decoder stack */
134 datasize = codesize = codemask = clear_code = avail = oldcode = 0;
138 rows_remaining = irow = 0;
139 rowbuf = rowend = rowp = 0;
141 x_offset = y_offset = width = height = 0;
143 disposal_method = DISPOSE_NOT_SPECIFIED;
146 local_colormap_size = 0;
147 is_local_colormap_defined = progressive_display = is_transparent = interlaced = false;
157 delete []local_colormap;
164 struct GIFImageReader {
165 WebCore::GIFImageDecoder* clientptr;
166 /* Parsing state machine */
167 gstate state; /* Current decoder master state */
168 unsigned bytes_to_consume; /* Number of bytes to accumulate */
169 unsigned bytes_in_hold; /* bytes accumulated so far*/
170 unsigned char hold[MAX_HOLD_SIZE]; /* Accumulation buffer */
171 unsigned char* global_colormap; /* (3* MAX_COLORS in size) Default colormap if local not supplied, 3 bytes for each color */
173 /* Global (multi-image) state */
174 int screen_bgcolor; /* Logical screen background color */
175 int version; /* Either 89 for GIF89 or 87 for GIF87 */
176 unsigned screen_width; /* Logical screen width & height */
177 unsigned screen_height;
178 int global_colormap_size; /* Size of global colormap array. */
179 int images_decoded; /* Counts completed frames for animated GIFs */
180 int images_count; /* Counted all frames seen so far (including incomplete frames) */
181 int loop_count; /* Netscape specific extension block to control
182 the number of animation loops a GIF renders. */
184 // Not really global, but convenient to locate here.
185 int count; /* Remaining # bytes in sub-block */
187 GIFFrameReader* frame_reader;
189 GIFImageReader(WebCore::GIFImageDecoder* client = 0) {
192 bytes_to_consume = 6;
197 screen_bgcolor = version = 0;
198 screen_width = screen_height = 0;
199 global_colormap_size = images_decoded = images_count = 0;
209 delete []global_colormap;
215 bool read(const unsigned char * buf, unsigned int numbytes,
216 WebCore::GIFImageDecoder::GIFQuery query = WebCore::GIFImageDecoder::GIFFullQuery, unsigned haltAtFrame = -1);
220 int do_lzw(const unsigned char *q);