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_oom, 73 gif_error, 74 gif_comment_extension, 75 gif_application_extension, 76 gif_netscape_extension_block, 77 gif_consume_netscape_extension, 78 gif_consume_comment 79 } gstate; 80 81 struct GIFFrameReader { 82 /* LZW decoder state machine */ 83 unsigned char *stackp; /* Current stack pointer */ 84 int datasize; 85 int codesize; 86 int codemask; 87 int clear_code; /* Codeword used to trigger dictionary reset */ 88 int avail; /* Index of next available slot in dictionary */ 89 int oldcode; 90 unsigned char firstchar; 91 int bits; /* Number of unread bits in "datum" */ 92 int datum; /* 32-bit input buffer */ 93 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 */ 101 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. */ 109 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 */ 114 115 unsigned delay_time; /* Display time, in milliseconds, 116 for this image in a multi-image GIF */ 117 118 119 unsigned short* prefix; /* LZW decoding tables */ 120 unsigned char* suffix; /* LZW decoding tables */ 121 unsigned char* stack; /* Base of LZW decoder stack */ 122 123 124 GIFFrameReader() { 125 stackp = 0; 126 datasize = codesize = codemask = clear_code = avail = oldcode = 0; 127 firstchar = 0; 128 bits = datum = 0; 129 ipass = 0; 130 rows_remaining = irow = 0; 131 rowbuf = rowend = rowp = 0; 132 133 x_offset = y_offset = width = height = 0; 134 tpixel = 0; 135 disposal_method = WebCore::RGBA32Buffer::DisposeNotSpecified; 136 137 local_colormap = 0; 138 local_colormap_size = 0; 139 is_local_colormap_defined = progressive_display = is_transparent = interlaced = false; 140 141 delay_time = 0; 142 143 prefix = 0; 144 suffix = stack = 0; 145 } 146 147 ~GIFFrameReader() { 148 delete []rowbuf; 149 delete []local_colormap; 150 delete []prefix; 151 delete []suffix; 152 delete []stack; 153 } 154 }; 155 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 */ 164 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 unsigned 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. */ 175 176 // Not really global, but convenient to locate here. 177 int count; /* Remaining # bytes in sub-block */ 178 179 GIFFrameReader* frame_reader; 180 181 GIFImageReader(WebCore::GIFImageDecoder* client = 0) { 182 clientptr = client; 183 state = gif_type; 184 bytes_to_consume = 6; 185 bytes_in_hold = 0; 186 frame_reader = 0; 187 global_colormap = 0; 188 189 screen_bgcolor = version = 0; 190 screen_width = screen_height = 0; 191 global_colormap_size = images_decoded = images_count = 0; 192 loop_count = cLoopCountNotSeen; 193 count = 0; 194 } 195 196 ~GIFImageReader() { 197 close(); 198 } 199 200 void close() { 201 delete []global_colormap; 202 global_colormap = 0; 203 delete frame_reader; 204 frame_reader = 0; 205 } 206 207 bool read(const unsigned char * buf, unsigned int numbytes, 208 WebCore::GIFImageDecoder::GIFQuery query = WebCore::GIFImageDecoder::GIFFullQuery, unsigned haltAtFrame = -1); 209 210 private: 211 bool output_row(); 212 bool do_lzw(const unsigned char *q); 213 }; 214 215 #endif 216