2 * This file was adapted from D J Hagberg's GifDecoder.java
4 * This software is copyrighted by D. J. Hagberg, Jr., and other parties.
5 * The following terms apply to all files associated with the software
6 * unless explicitly disclaimed in individual files.
8 * The authors hereby grant permission to use, copy, modify, distribute,
9 * and license this software and its documentation for any purpose, provided
10 * that existing copyright notices are retained in all copies and that this
11 * notice is included verbatim in any distributions. No written agreement,
12 * license, or royalty fee is required for any of the authorized uses.
13 * Modifications to this software may be copyrighted by their authors
14 * and need not follow the licensing terms described here, provided that
15 * the new terms are clearly indicated on the first page of each file where
18 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
19 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
20 * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
21 * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
22 * POSSIBILITY OF SUCH DAMAGE.
24 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
27 * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
28 * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
31 * GOVERNMENT USE: If you are acquiring this software on behalf of the
32 * U.S. government, the Government shall have only "Restricted Rights"
33 * in the software and related documentation as defined in the Federal
34 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
35 * are acquiring the software on behalf of the Department of Defense, the
36 * software shall be classified as "Commercial Computer Software" and the
37 * Government shall have only "Restricted Rights" as defined in Clause
38 * 252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the
39 * authors grant the U.S. Government and others acting in its behalf
40 * permission to use and distribute the software in accordance with the
41 * terms specified in this license.
46 import org.xwt.util.*;
47 import java.io.BufferedInputStream;
48 import java.io.InputStream;
49 import java.io.IOException;
50 import java.io.PrintWriter;
52 /** Converts an InputStream carrying a GIF image into an ARGB int[] */
53 public class GIF implements ImageDecoder {
55 // Public Methods /////////////////////////////////////////////////////////
57 public int[] getData() { return data; }
58 public int getWidth() { return width; }
59 public int getHeight() { return height; }
61 /** Processes an image from InputStream is; returns null if there is an error
62 @param name A string describing the image; used for error reporting.
64 public static GIF decode(InputStream is, String name) {
66 return new GIF(is, name);
67 } catch (Exception e) {
68 if (Log.on) Log.log(GIF.class, e);
73 private GIF(InputStream is, String name) throws IOException {
74 if (is instanceof BufferedInputStream) _in = (BufferedInputStream)is;
75 else _in = new BufferedInputStream(is);
76 decodeAsBufferedImage(0);
79 // Private Methods /////////////////////////////////////////////////////////
81 private int[] data = null;
83 /** Decode a particular frame from the GIF file. Frames must be decoded in order, starting with 0. */
84 private void decodeAsBufferedImage(int page) throws IOException, IOException {
86 // If the user requested a page already decoded, we cannot go back.
87 if (page <= index ) return;
89 // If we just started reading this stream, initialize the global info.
91 if (!readIntoBuf(6) || _buf[0] != 'G' || _buf[1] != 'I' || _buf[2] != 'F' ||
92 _buf[3] != '8' || !(_buf[4] == '7' || _buf[4] == '9') || _buf[5] != 'a')
93 throw new IOException("Not a GIF8Xa file.");
94 if (!readGlobalImageDescriptor()) throw new IOException("Unable to read GIF header.");
97 // Loop through the blocks in the image.
100 if(!readIntoBuf(1)) throw new IOException("Unexpected EOF(1)");
101 block_identifier = _buf[0];
102 if (block_identifier == ';') throw new IOException("No image data");
103 if (block_identifier == '!') {
104 if (!readExtensionBlock()) throw new IOException("Unexpected EOF(2)");
108 // not a valid start character -- ignore it.
109 if (block_identifier != ',') continue;
111 if (!readLocalImageDescriptor()) throw new IOException("Unexpected EOF(3)");
112 data = new int[width * height];
115 // If we did not decode the requested index, need to go on
116 // to the next one (future implementations should consider
117 // caching already-decoded images here to allow for random
118 // access to animated GIF pages).
120 if (index < page) continue;
122 // If we did decode the requested index, we can return.
126 // Return the image thus-far decoded
130 /** Actually read the image data */
131 private void readImage()
132 throws IOException, IOException {
137 int xpos = 0, ypos = 0, pass = 0, i;
138 int prefix[] = new int[(1 << MAX_LWZ_BITS)];
139 int append[] = new int[(1 << MAX_LWZ_BITS)];
140 int stack[] = new int[(1 << MAX_LWZ_BITS)*2];
142 int codeSize, clearCode, inCode, endCode, oldCode, maxCode, code, firstCode;
144 // Initialize the decoder
145 if (!readIntoBuf(1)) throw new IOException("Unexpected EOF decoding image");
146 initialCodeSize = _buf[0];
148 // Look at the right color map, setting up transparency if called for.
149 int[] cmap = global_color_map;
150 if (hascmap) cmap = color_map;
151 if (trans_idx >= 0) cmap[trans_idx] = 0x00000000;
153 /* Initialize the decoder */
154 /* Set values for "special" numbers:
155 * clear code reset the decoder
156 * end code stop decoding
157 * code size size of the next code to retrieve
158 * max code next available table position
160 clearCode = 1 << initialCodeSize;
161 endCode = clearCode + 1;
162 codeSize = initialCodeSize + 1;
163 maxCode = clearCode + 2;
167 for (i = 0; i < clearCode; i++) append[i] = i;
168 top_idx = 0; // top of stack.
176 /* Read until we finish the image */
178 for (i = 0; i < rows; i++) {
179 for (xpos = 0; xpos < len;) {
182 /* Bummer -- our stack is empty. Now we have to work! */
183 code = getCode(codeSize);
184 if (code < 0) return;
186 if (code > maxCode || code == endCode) return;
187 if (code == clearCode) {
188 codeSize = initialCodeSize + 1;
189 maxCode = clearCode + 2;
194 // Last pass reset the decoder, so the first code we
195 // see must be a singleton. Seed the stack with it,
196 // and set up the old/first code pointers for
197 // insertion into the string table. We can't just
198 // roll this into the clearCode test above, because
199 // at that point we have not yet read the next code.
201 stack[top_idx++] = append[code];
209 // maxCode is always one bigger than our
210 // highest assigned code. If the code we see
211 // is equal to maxCode, then we are about to
212 // add a new string to the table. ???
213 if (code == maxCode) {
214 stack[top_idx++] = firstCode;
218 // Populate the stack by tracing the string in the
219 // string table from its tail to its head
220 while (code > clearCode) {
221 stack[top_idx++] = append[code];
224 firstCode = append[code];
226 // If there's no more room in our string table, quit.
227 // Otherwise, add a new string to the table
228 if (maxCode >= (1 << MAX_LWZ_BITS)) return;
230 // Push the head of the string onto the stack
231 stack[top_idx++] = firstCode;
233 // Add a new string to the string table
234 prefix[maxCode] = oldCode;
235 append[maxCode] = firstCode;
238 // maxCode tells us the maximum code value we can accept.
239 // If we see that we need more bits to represent it than
240 // we are requesting from the unpacker, we need to increase
241 // the number we ask for.
242 if ((maxCode >= (1 << codeSize)) && (maxCode < (1<<MAX_LWZ_BITS))) codeSize++;
246 // Pop the next color index off the stack
247 v = stack[--top_idx];
250 // Finally, we can set a pixel! Joy!
251 data[xpos + ypos * width] = cmap[v];
255 // If interlacing, the next ypos is not just +1
257 ypos += _interlaceStep[pass];
258 while (ypos >= rows) {
260 if (pass > 3) return;
261 ypos = _interlaceStart[pass];
268 /** Extract the next compression code from the file. */
269 private int getCode(int code_size) throws IOException {
272 while (bitsInWindow < code_size) {
273 // Not enough bits in our window to cover the request
277 // Not enough bytes in our buffer to add to the window
278 bytes = getDataBlock();
285 // Tack another byte onto the window, see if that's enough
286 window += (_buf[c]) << bitsInWindow;
293 // The next code will always be the last code_size bits of the window
294 ret = ((int)window) & ((1 << code_size) - 1);
296 // Shift data in the window to put the next code at the end
297 window >>= code_size;
298 bitsInWindow -= code_size;
302 /** Read the global image descriptor and optional global color map. Sets global_* variables. */
303 private boolean readGlobalImageDescriptor() throws IOException {
305 int aspect; // we ignore this.
308 if (!readIntoBuf(7) ) return false;
309 global_width = _buf[0] | (_buf[1] << 8);
310 global_height = _buf[2] | (_buf[3] << 8);
312 global_bgcolor = _buf[5];
314 global_cmapsize = 2 << (packed & 0x07);
315 global_hascmap = (packed & GLOBALCOLORMAP) == GLOBALCOLORMAP;
316 global_color_map = null;
318 // Read the color map, if we have one.
319 if (global_hascmap) {
320 if (!readColorMap(global_cmapsize,true)) {
328 /** Read a local image descriptor and optional local color map. */
329 private boolean readLocalImageDescriptor() throws IOException {
332 if (!readIntoBuf(9) ) return false;
334 left = _buf[0] | (_buf[1] << 8);
335 top = _buf[2] | (_buf[3] << 8);
336 width = _buf[4] | (_buf[5] << 8);
337 height = _buf[6] | (_buf[7] << 8);
339 hascmap = (packed & LOCALCOLORMAP) == LOCALCOLORMAP;
340 cmapsize = 2 << (packed & 0x07);
341 interlaced = (packed & INTERLACE) == INTERLACE;
344 // Read the local color table, if there is one.
345 return !(hascmap && !readColorMap(cmapsize,false));
348 /** Read a color map (global or local). */
349 private boolean readColorMap(int nColors, boolean isGlobal)
351 int[] map = new int[nColors];
352 for( int i=0; i < nColors; ++i) {
353 if (!readIntoBuf(3) ) return false;
354 map[i] = (_buf[0] << 16) | (_buf[1] << 8) | _buf[2] | 0xFF000000;
356 if (isGlobal) global_color_map = map;
357 else color_map = map;
361 /** Read the contents of a GIF89a Graphical Extension Block. */
362 private boolean readExtensionBlock() throws IOException {
363 if (!readIntoBuf(1) ) return false;
367 case 0x01: // Plain Text Extension
368 case 0xff: // Application Extension
369 case 0xfe: // Comment Extension
371 case 0xf9: // Graphic Control Extension
372 count = getDataBlock();
373 if (count < 0) return true;
374 // Check for transparency setting.
375 if ((_buf[0] & HASTRANSPARENCY) != 0) trans_idx = _buf[3];
378 do { count = getDataBlock(); } while (count > 0);
382 /** Read a block of data from the GIF file. */
383 private int getDataBlock() throws IOException {
384 if (!readIntoBuf(1) ) return -1;
386 if (count != 0) if (!readIntoBuf(count) ) return -1;
390 /** Read the indicated number of bytes into _buf, our instance-wide buffer. */
391 private boolean readIntoBuf(int count) throws IOException {
392 for(int i = 0; i < count; i++) if ((_buf[i] = _in.read()) == -1) return false;
396 // Private Data //////////////////////////////////////////////////////////
398 // State management stuff
399 private int index = -1;
400 private BufferedInputStream _in = null;
401 private int[] _buf = new int[BUFSIZE];
403 // Transparency settings
404 private int trans_idx = -1;
406 // Global image descriptor contents
407 private int global_width = 0;
408 private int global_height = 0;
409 private int global_bgcolor = 0;
410 private int global_cmapsize = 0;
411 private boolean global_hascmap = false;
412 private int[] global_color_map = null;
414 // Local image descriptor contents
415 private int left = 0;
417 private int width = 0;
418 private int height = 0;
419 private int cmapsize = 0;
420 private boolean hascmap = false;
421 private boolean interlaced = false;
422 private int[] color_map = null;
424 // Variables used in getCode(...) to track sliding bit-window.
425 private int bytes = 0;
426 private boolean done;
429 private int bitsInWindow = 0;
431 // Class-wide constants.
432 private static final int INTERLACE = 0x40;
433 private static final int GLOBALCOLORMAP = 0x80;
434 private static final int LOCALCOLORMAP = 0x80;
435 private static final int HASTRANSPARENCY = 0x01;
436 private static final int MAX_LWZ_BITS = 12;
437 private static final int BUFSIZE = 280;
438 private static final int[] _interlaceStep = { 8, 8, 4, 2 };
439 private static final int[] _interlaceStart = { 0, 4, 2, 1 };