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 ***** */
38 #ifndef GIFImageReader_h
39 #define GIFImageReader_h
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,
72 gif_comment_extension,
73 gif_application_extension,
74 gif_netscape_extension_block,
75 gif_consume_netscape_extension,
79 struct GIFFrameReader {
80 /* LZW decoder state machine */
81 unsigned char *stackp; /* Current stack pointer */
85 int clear_code; /* Codeword used to trigger dictionary reset */
86 int avail; /* Index of next available slot in dictionary */
88 unsigned char firstchar;
89 int bits; /* Number of unread bits in "datum" */
90 int datum; /* 32-bit input buffer */
92 /* Output state machine */
93 int ipass; /* Interlace pass; Ranges 1-4 if interlaced. */
94 unsigned int rows_remaining; /* Rows remaining to be output */
95 unsigned int irow; /* Current output row, starting at zero */
96 unsigned char *rowbuf; /* Single scanline temporary buffer */
97 unsigned char *rowend; /* Pointer to end of rowbuf */
98 unsigned char *rowp; /* Current output pointer */
100 /* Parameters for image frame currently being decoded */
101 unsigned int x_offset, y_offset; /* With respect to "screen" origin */
102 unsigned int height, width;
103 int tpixel; /* Index of transparent pixel */
104 WebCore::ImageFrame::FrameDisposalMethod disposal_method; /* Restore to background, leave in place, etc.*/
105 unsigned char *local_colormap; /* Per-image colormap */
106 int local_colormap_size; /* Size of local colormap array. */
108 bool is_local_colormap_defined : 1;
109 bool progressive_display : 1; /* If TRUE, do Haeberli interlace hack */
110 bool interlaced : 1; /* TRUE, if scanlines arrive interlaced order */
111 bool is_transparent : 1; /* TRUE, if tpixel is valid */
113 unsigned delay_time; /* Display time, in milliseconds,
114 for this image in a multi-image GIF */
117 unsigned short* prefix; /* LZW decoding tables */
118 unsigned char* suffix; /* LZW decoding tables */
119 unsigned char* stack; /* Base of LZW decoder stack */
124 datasize = codesize = codemask = clear_code = avail = oldcode = 0;
128 rows_remaining = irow = 0;
129 rowbuf = rowend = rowp = 0;
131 x_offset = y_offset = width = height = 0;
133 disposal_method = WebCore::ImageFrame::DisposeNotSpecified;
136 local_colormap_size = 0;
137 is_local_colormap_defined = progressive_display = is_transparent = interlaced = false;
147 delete []local_colormap;
154 struct GIFImageReader {
155 WebCore::GIFImageDecoder* clientptr;
156 /* Parsing state machine */
157 gstate state; /* Current decoder master state */
158 unsigned bytes_to_consume; /* Number of bytes to accumulate */
159 unsigned bytes_in_hold; /* bytes accumulated so far*/
160 unsigned char hold[MAX_HOLD_SIZE]; /* Accumulation buffer */
161 unsigned char* global_colormap; /* (3* MAX_COLORS in size) Default colormap if local not supplied, 3 bytes for each color */
163 /* Global (multi-image) state */
164 int screen_bgcolor; /* Logical screen background color */
165 int version; /* Either 89 for GIF89 or 87 for GIF87 */
166 unsigned screen_width; /* Logical screen width & height */
167 unsigned screen_height;
168 int global_colormap_size; /* Size of global colormap array. */
169 unsigned images_decoded; /* Counts completed frames for animated GIFs */
170 int images_count; /* Counted all frames seen so far (including incomplete frames) */
171 int loop_count; /* Netscape specific extension block to control
172 the number of animation loops a GIF renders. */
174 // Not really global, but convenient to locate here.
175 int count; /* Remaining # bytes in sub-block */
177 GIFFrameReader* frame_reader;
179 GIFImageReader(WebCore::GIFImageDecoder* client = 0) {
182 bytes_to_consume = 6;
187 screen_bgcolor = version = 0;
188 screen_width = screen_height = 0;
189 global_colormap_size = images_decoded = images_count = 0;
190 loop_count = cLoopCountNotSeen;
195 delete []global_colormap;
201 bool read(const unsigned char * buf, unsigned int numbytes,
202 WebCore::GIFImageDecoder::GIFQuery query = WebCore::GIFImageDecoder::GIFFullQuery, unsigned haltAtFrame = -1);
206 bool do_lzw(const unsigned char *q);