initial import
[vuplus_webkit] / Source / WebCore / platform / image-decoders / gif / GIFImageReader.h
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 class into this old C decoder,
42 // so we will too.
43 #include "GIFImageDecoder.h"
44
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
49
50 const int cLoopCountNotSeen = -2;
51
52 /* gif2.h  
53    The interface for the GIF87/89a decoder. 
54 */
55 // List of possible parsing states
56 typedef enum {
57     gif_type,
58     gif_global_header,
59     gif_global_colormap,
60     gif_image_start,            
61     gif_image_header,
62     gif_image_colormap,
63     gif_image_body,
64     gif_lzw_start,
65     gif_lzw,
66     gif_sub_block,
67     gif_extension,
68     gif_control_extension,
69     gif_consume_block,
70     gif_skip_block,
71     gif_done,
72     gif_comment_extension,
73     gif_application_extension,
74     gif_netscape_extension_block,
75     gif_consume_netscape_extension,
76     gif_consume_comment
77 } gstate;
78
79 struct GIFFrameReader {
80     /* LZW decoder state machine */
81     unsigned char *stackp;              /* Current stack pointer */
82     int datasize;
83     int codesize;
84     int codemask;
85     int clear_code;             /* Codeword used to trigger dictionary reset */
86     int avail;                  /* Index of next available slot in dictionary */
87     int oldcode;
88     unsigned char firstchar;
89     int bits;                   /* Number of unread bits in "datum" */
90     int datum;                /* 32-bit input buffer */
91
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 */
99
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. */
107     
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 */
112
113     unsigned delay_time;        /* Display time, in milliseconds,
114                                    for this image in a multi-image GIF */
115
116
117     unsigned short*  prefix;          /* LZW decoding tables */
118     unsigned char*   suffix;          /* LZW decoding tables */
119     unsigned char*   stack;           /* Base of LZW decoder stack */
120
121
122     GIFFrameReader() {
123         stackp = 0;
124         datasize = codesize = codemask = clear_code = avail = oldcode = 0;
125         firstchar = 0;
126         bits = datum = 0;
127         ipass = 0;
128         rows_remaining = irow = 0;
129         rowbuf = rowend = rowp = 0;
130
131         x_offset = y_offset = width = height = 0;
132         tpixel = 0;
133         disposal_method = WebCore::ImageFrame::DisposeNotSpecified;
134
135         local_colormap = 0;
136         local_colormap_size = 0;
137         is_local_colormap_defined = progressive_display = is_transparent = interlaced = false;
138
139         delay_time = 0;
140
141         prefix = 0;
142         suffix = stack = 0;
143     }
144     
145     ~GIFFrameReader() {
146         delete []rowbuf;
147         delete []local_colormap;
148         delete []prefix;
149         delete []suffix;
150         delete []stack;
151     }
152 };
153
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  */
162     
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. */
173     
174     // Not really global, but convenient to locate here.
175     int count;                  /* Remaining # bytes in sub-block */
176     
177     GIFFrameReader* frame_reader;
178
179     GIFImageReader(WebCore::GIFImageDecoder* client = 0) {
180         clientptr = client;
181         state = gif_type;
182         bytes_to_consume = 6;
183         bytes_in_hold = 0;
184         frame_reader = 0;
185         global_colormap = 0;
186
187         screen_bgcolor = version = 0;
188         screen_width = screen_height = 0;
189         global_colormap_size = images_decoded = images_count = 0;
190         loop_count = cLoopCountNotSeen;
191         count = 0;
192     }
193
194     ~GIFImageReader() {
195         delete []global_colormap;
196         global_colormap = 0;
197         delete frame_reader;
198         frame_reader = 0;
199     }
200
201     bool read(const unsigned char * buf, unsigned int numbytes, 
202               WebCore::GIFImageDecoder::GIFQuery query = WebCore::GIFImageDecoder::GIFFullQuery, unsigned haltAtFrame = -1);
203
204 private:
205     bool output_row();
206     bool do_lzw(const unsigned char *q);
207 };
208
209 #endif