1    /*
2     * This file was adapted from D J Hagberg's GifDecoder.java
3     *
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.
7     * 
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
16    * they apply.
17    * 
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.
23    * 
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
29    * MODIFICATIONS.
30    * 
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.
42    *
43    */
44   package org.xwt;
45   
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;
51   
52   /** Converts an InputStream carrying a GIF image into an ARGB int[] */
53   public class GIF extends ImageDecoder {
54   
55       // Public Methods /////////////////////////////////////////////////////////
56   
57       public int[] getData() { return data; }
58       public int getWidth() { return width; }
59       public int getHeight() { return height; }
60   
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.
63        */
64       public static GIF decode(InputStream is, String name) {
65           try {
66               return new GIF(is, name);
67           } catch (Exception e) {
68               if (Log.on) Log.log(GIF.class, e);
69               return null;
70           }
71       }
72   
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);
77       }
78   
79       // Private Methods /////////////////////////////////////////////////////////
80   
81       private int[] data = null;
82   
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 {
85   
86           // If the user requested a page already decoded, we cannot go back.
87           if (page <= index ) return;
88   
89           // If we just started reading this stream, initialize the global info.
90           if (index < 0 ) {
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.");
95           }
96           
97           // Loop through the blocks in the image.
98           int block_identifier;
99           while (true) {
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)");
105                  continue;
106              }
107  
108              // not a valid start character -- ignore it.
109              if (block_identifier != ',') continue;
110  
111              if (!readLocalImageDescriptor()) throw new IOException("Unexpected EOF(3)");
112              data = new int[width * height];
113              readImage();
114  
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).
119              index++;
120              if (index < page) continue;
121              
122              // If we did decode the requested index, we can return.
123              break;
124          }
125          
126          // Return the image thus-far decoded
127          return;
128      }
129  
130      /** Actually read the image data */
131      private void readImage() 
132          throws IOException, IOException {
133          int len = width;
134          int rows = height;
135          int initialCodeSize;
136          int v;
137          intintintint, 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];
141          int top_idx;
142          intintintintintintintint inCode, endCode, oldCode, maxCode, code, firstCode;
143          
144          // Initialize the decoder
145          if (!readIntoBuf(1)) throw new IOException("Unexpected EOF decoding image");
146          initialCodeSize = _buf[0];
147  
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;
152  
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
159           */
160          clearCode   = 1 << initialCodeSize;
161          endCode     = clearCode + 1;
162          codeSize    = initialCodeSize + 1;
163          maxCode     = clearCode + 2;
164          oldCode     = -1;
165          firstCode   = -1;
166          
167          for (i = 0; i < clearCode; i++) append[i] = i;
168          top_idx = 0; // top of stack.
169          
170          bitsInWindow = 0;
171          bytes = 0;
172          window = 0L;
173          done = false;
174          c = -1;
175          
176          /* Read until we finish the image */
177          ypos = 0;
178          for (i = 0; i < rows; i++) {
179              for (xpos = 0; xpos < len;) {
180                  
181                  if (top_idx == 0) {
182                      /* Bummer -- our stack is empty.  Now we have to work! */
183                      code = getCode(codeSize);
184                      if (code < 0) return;
185                      
186                      if (code > maxCode || code == endCode) return;
187                      if (code == clearCode) {
188                          codeSize    = initialCodeSize + 1;
189                          maxCode     = clearCode + 2;
190                          oldCode     = -1;
191                          continue;
192                      }
193                      
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.
200                      if (oldCode == -1) {
201                          stack[top_idx++] = append[code];
202                          oldCode = code;
203                          firstCode = code;
204                          continue;
205                      }
206                      
207                      inCode = code;
208  
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;
215                          code = oldCode;
216                      }
217                      
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];
222                          code = prefix[code];
223                      }
224                      firstCode = append[code];
225                      
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;
229                      
230                      // Push the head of the string onto the stack
231                      stack[top_idx++] = firstCode;
232                      
233                      // Add a new string to the string table
234                      prefix[maxCode] = oldCode;
235                      append[maxCode] = firstCode;
236                      maxCode++;
237                      
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++;
243                      oldCode = inCode;
244                  }
245                  
246                  // Pop the next color index off the stack
247                  v = stack[--top_idx];
248                  if (v < 0) return;
249                  
250                  // Finally, we can set a pixel!  Joy!
251                  data[xpos + ypos * width] = cmap[v];
252                  xpos++;
253              }
254              
255              // If interlacing, the next ypos is not just +1
256              if (interlaced) {
257                  ypos += _interlaceStep[pass];
258                  while (ypos >= rows) {
259                      pass++;
260                      if (pass > 3) return;
261                      ypos = _interlaceStart[pass];
262                  }
263              } else ypos++;
264          }
265          return;
266      }
267  
268      /** Extract the next compression code from the file. */
269      private int getCode(int code_size) throws IOException {
270          int ret;
271          
272          while (bitsInWindow < code_size) {
273              // Not enough bits in our window to cover the request
274              if (done) return -1;
275              
276              if (bytes == 0) {
277                  // Not enough bytes in our buffer to add to the window
278                  bytes = getDataBlock();
279                  c = 0;
280                  if (bytes <= 0) {
281                      done = true;
282                      break;
283                  }
284              }
285              // Tack another byte onto the window, see if that's enough
286              window += (_buf[c]) << bitsInWindow;
287              ++c;
288              bitsInWindow += 8;
289              bytes--;
290          }
291          
292          
293          // The next code will always be the last code_size bits of the window
294          ret = ((int)window) & ((1 << code_size) - 1);
295          
296          // Shift data in the window to put the next code at the end
297          window >>= code_size;
298          bitsInWindow -= code_size;
299          return ret;
300      }
301      
302      /** Read the global image descriptor and optional global color map. Sets global_* variables. */
303      private boolean readGlobalImageDescriptor() throws IOException {
304          int packed;
305          int aspect; // we ignore this.
306          int ofs;
307          
308          if (!readIntoBuf(7) ) return false;
309          global_width     = _buf[0] | (_buf[1] << 8);
310          global_height    = _buf[2] | (_buf[3] << 8);
311          packed       = _buf[4];
312          global_bgcolor   = _buf[5];
313          aspect       = _buf[6];
314          global_cmapsize  = 2 << (packed & 0x07);
315          global_hascmap   = (packed & GLOBALCOLORMAP) == GLOBALCOLORMAP;
316          global_color_map = null;
317          
318          // Read the color map, if we have one.
319          if (global_hascmap) {
320              if (!readColorMap(global_cmapsize,true)) {
321                  return false;
322              }
323          }
324          
325          return true;
326      }
327      
328      /** Read a local image descriptor and optional local color map. */
329      private boolean readLocalImageDescriptor() throws IOException {
330          int packed;
331          
332          if (!readIntoBuf(9) ) return false;
333          
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);
338          packed        = _buf[8];
339          hascmap    = (packed & LOCALCOLORMAP) == LOCALCOLORMAP;
340          cmapsize   = 2 << (packed & 0x07);
341          interlaced = (packed & INTERLACE) == INTERLACE;
342          color_map  = null;
343          
344          // Read the local color table, if there is one.
345          return !(hascmap && !readColorMap(cmapsize,false));
346      }
347  
348      /** Read a color map (global or local). */
349      private boolean readColorMap(int nColors, boolean isGlobal)
350          throws IOException {
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;
355          }
356          if (isGlobal) global_color_map = map;
357          else color_map = map;
358          return true;
359      }
360  
361      /** Read the contents of a GIF89a Graphical Extension Block. */
362      private boolean readExtensionBlock() throws IOException {
363          if (!readIntoBuf(1) ) return false;
364          int label = _buf[0];
365          int count = -1;
366          switch (label) {
367          case 0x01:      // Plain Text Extension
368          case 0xff:      // Application Extension
369          case 0xfe:      // Comment Extension
370              break;
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];
376              else trans_idx = -1;
377          }
378          do { count = getDataBlock(); } while (count > 0);
379          return true;
380      }
381  
382      /** Read a block of data from the GIF file. */
383      private int getDataBlock() throws IOException {
384          if (!readIntoBuf(1) ) return -1;
385          int count = _buf[0];
386          if (count != 0) if (!readIntoBuf(count) ) return -1;
387          return count;
388      }
389      
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;
393          return true;
394      }
395  
396      // Private Data //////////////////////////////////////////////////////////
397  
398      // State management stuff
399      private int index = -1;
400      private BufferedInputStream _in = null;
401      private int[] _buf = new int[BUFSIZE];
402  
403      // Transparency settings
404      private int trans_idx = -1;
405  
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;
413  
414      // Local image descriptor contents
415      private int left = 0;
416      private int top = 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;
423  
424      // Variables used in getCode(...) to track sliding bit-window.
425      private int     bytes = 0;
426      private boolean done;
427      private int     c;
428      private long    window;
429      private int     bitsInWindow = 0;
430  
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 };
440  }
441  
442  
443  
444