mass rename and rebranding from xwt to ibex - fixed to use ixt files
[org.ibex.core.git] / src / org / xwt / translators / GIF.java
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.translators;
45
46 import org.ibex.*;
47 import org.ibex.util.*;
48 import java.io.BufferedInputStream;
49 import java.io.InputStream;
50 import java.io.IOException;
51
52 /** Converts an InputStream carrying a GIF image into an ARGB int[] */
53 public class GIF {
54
55     // Public Methods /////////////////////////////////////////////////////////
56
57     private GIF() { }
58
59     private static Queue instances = new Queue(10);
60
61     public static void load(InputStream is, Picture p) {
62         GIF g = (GIF)instances.remove(false);
63         if (g == null) g = new GIF();
64         try {
65             g._load(is, p);
66         } catch (Exception e) {
67             if (Log.on) Log.info(GIF.class, e);
68             return;
69         }
70         // FIXME: must reset fields
71         // if (instances.size() < 10) instances.append(g);
72     }
73
74     private void _load(InputStream is, Picture p) throws IOException {
75         this.p = p;
76         if (is instanceof BufferedInputStream) _in = (BufferedInputStream)is;
77         else _in = new BufferedInputStream(is);
78         decodeAsBufferedImage(0);
79         p.isLoaded = true;
80         p = null;
81         _in = null;
82     }
83
84
85     /** Decode a particular frame from the GIF file. Frames must be decoded in order, starting with 0. */
86     private void decodeAsBufferedImage(int page) throws IOException, IOException {
87
88         // If the user requested a page already decoded, we cannot go back.
89         if (page <= index ) return;
90
91         // If we just started reading this stream, initialize the global info.
92         if (index < 0 ) {
93             if (!readIntoBuf(6) || _buf[0] != 'G' || _buf[1] != 'I' || _buf[2] != 'F' ||
94                 _buf[3] != '8' || !(_buf[4] == '7' || _buf[4] == '9') || _buf[5] != 'a')
95                 throw new IOException("Not a GIF8Xa file.");
96             if (!readGlobalImageDescriptor()) throw new IOException("Unable to read GIF header.");
97         }
98         
99         // Loop through the blocks in the image.
100         int block_identifier;
101         while (true) {
102             if(!readIntoBuf(1)) throw new IOException("Unexpected EOF(1)");
103             block_identifier = _buf[0];
104             if (block_identifier == ';') throw new IOException("No image data");
105             if (block_identifier == '!') {
106                 if (!readExtensionBlock()) throw new IOException("Unexpected EOF(2)");
107                 continue;
108             }
109
110             // not a valid start character -- ignore it.
111             if (block_identifier != ',') continue;
112
113             if (!readLocalImageDescriptor()) throw new IOException("Unexpected EOF(3)");
114             p.data = new int[p.width * p.height];
115             readImage();
116
117             // If we did not decode the requested index, need to go on
118             // to the next one (future implementations should consider
119             // caching already-decoded images here to allow for random
120             // access to animated GIF pages).
121             index++;
122             if (index < page) continue;
123             
124             // If we did decode the requested index, we can return.
125             break;
126         }
127         
128         // Return the image thus-far decoded
129         return;
130     }
131
132     /** Actually read the image data */
133     private void readImage() 
134         throws IOException, IOException {
135         int len = p.width;
136         int rows = p.height;
137         int initialCodeSize;
138         int v;
139         int xpos = 0, ypos = 0, pass = 0, i;
140         int prefix[] = new int[(1 << MAX_LWZ_BITS)];
141         int append[] = new int[(1 << MAX_LWZ_BITS)];
142         int stack[] = new int[(1 << MAX_LWZ_BITS)*2];
143         int top_idx;
144         int codeSize, clearCode, inCode, endCode, oldCode, maxCode, code, firstCode;
145         
146         // Initialize the decoder
147         if (!readIntoBuf(1)) throw new IOException("Unexpected EOF decoding image");
148         initialCodeSize = _buf[0];
149
150         // Look at the right color map, setting up transparency if called for.
151         int[] cmap = global_color_map;
152         if (hascmap) cmap = color_map;
153         if (trans_idx >= 0) cmap[trans_idx] = 0x00000000;
154
155         /* Initialize the decoder */
156         /* Set values for "special" numbers:
157          * clear code   reset the decoder
158          * end code     stop decoding
159          * code size    size of the next code to retrieve
160          * max code     next available table position
161          */
162         clearCode   = 1 << initialCodeSize;
163         endCode     = clearCode + 1;
164         codeSize    = initialCodeSize + 1;
165         maxCode     = clearCode + 2;
166         oldCode     = -1;
167         firstCode   = -1;
168         
169         for (i = 0; i < clearCode; i++) append[i] = i;
170         top_idx = 0; // top of stack.
171         
172         bitsInWindow = 0;
173         bytes = 0;
174         window = 0L;
175         done = false;
176         c = -1;
177         
178         /* Read until we finish the image */
179         ypos = 0;
180         for (i = 0; i < rows; i++) {
181             for (xpos = 0; xpos < len;) {
182                 
183                 if (top_idx == 0) {
184                     /* Bummer -- our stack is empty.  Now we have to work! */
185                     code = getCode(codeSize);
186                     if (code < 0) return;
187                     
188                     if (code > maxCode || code == endCode) return;
189                     if (code == clearCode) {
190                         codeSize    = initialCodeSize + 1;
191                         maxCode     = clearCode + 2;
192                         oldCode     = -1;
193                         continue;
194                     }
195                     
196                     // Last pass reset the decoder, so the first code we
197                     // see must be a singleton.  Seed the stack with it,
198                     // and set up the old/first code pointers for
199                     // insertion into the string table.  We can't just
200                     // roll this into the clearCode test above, because
201                     // at that point we have not yet read the next code.
202                     if (oldCode == -1) {
203                         stack[top_idx++] = append[code];
204                         oldCode = code;
205                         firstCode = code;
206                         continue;
207                     }
208                     
209                     inCode = code;
210
211                     // maxCode is always one bigger than our
212                     // highest assigned code.  If the code we see
213                     // is equal to maxCode, then we are about to
214                     // add a new string to the table. ???  
215                     if (code == maxCode) {
216                         stack[top_idx++] = firstCode;
217                         code = oldCode;
218                     }
219                     
220                     // Populate the stack by tracing the string in the
221                     // string table from its tail to its head
222                     while (code > clearCode) {
223                         stack[top_idx++] = append[code];
224                         code = prefix[code];
225                     }
226                     firstCode = append[code];
227                     
228                     // If there's no more room in our string table, quit.
229                     // Otherwise, add a new string to the table
230                     if (maxCode >= (1 << MAX_LWZ_BITS)) return;
231                     
232                     // Push the head of the string onto the stack
233                     stack[top_idx++] = firstCode;
234                     
235                     // Add a new string to the string table
236                     prefix[maxCode] = oldCode;
237                     append[maxCode] = firstCode;
238                     maxCode++;
239                     
240                     // maxCode tells us the maximum code value we can accept.
241                     // If we see that we need more bits to represent it than
242                     // we are requesting from the unpacker, we need to increase
243                     // the number we ask for.
244                     if ((maxCode >= (1 << codeSize)) && (maxCode < (1<<MAX_LWZ_BITS))) codeSize++;
245                     oldCode = inCode;
246                 }
247                 
248                 // Pop the next color index off the stack
249                 v = stack[--top_idx];
250                 if (v < 0) return;
251                 
252                 // Finally, we can set a pixel!  Joy!
253                 p.data[xpos + ypos * p.width] = cmap[v];
254                 xpos++;
255             }
256             
257             // If interlacing, the next ypos is not just +1
258             if (interlaced) {
259                 ypos += _interlaceStep[pass];
260                 while (ypos >= rows) {
261                     pass++;
262                     if (pass > 3) return;
263                     ypos = _interlaceStart[pass];
264                 }
265             } else ypos++;
266         }
267         return;
268     }
269
270     /** Extract the next compression code from the file. */
271     private int getCode(int code_size) throws IOException {
272         int ret;
273         
274         while (bitsInWindow < code_size) {
275             // Not enough bits in our window to cover the request
276             if (done) return -1;
277             
278             if (bytes == 0) {
279                 // Not enough bytes in our buffer to add to the window
280                 bytes = getDataBlock();
281                 c = 0;
282                 if (bytes <= 0) {
283                     done = true;
284                     break;
285                 }
286             }
287             // Tack another byte onto the window, see if that's enough
288             window += (_buf[c]) << bitsInWindow;
289             ++c;
290             bitsInWindow += 8;
291             bytes--;
292         }
293         
294         
295         // The next code will always be the last code_size bits of the window
296         ret = ((int)window) & ((1 << code_size) - 1);
297         
298         // Shift data in the window to put the next code at the end
299         window >>= code_size;
300         bitsInWindow -= code_size;
301         return ret;
302     }
303     
304     /** Read the global image descriptor and optional global color map. Sets global_* variables. */
305     private boolean readGlobalImageDescriptor() throws IOException {
306         int packed;
307         int aspect; // we ignore this.
308         int ofs;
309         
310         if (!readIntoBuf(7) ) return false;
311         global_width     = _buf[0] | (_buf[1] << 8);
312         global_height    = _buf[2] | (_buf[3] << 8);
313         packed       = _buf[4];
314         global_bgcolor   = _buf[5];
315         aspect       = _buf[6];
316         global_cmapsize  = 2 << (packed & 0x07);
317         global_hascmap   = (packed & GLOBALCOLORMAP) == GLOBALCOLORMAP;
318         global_color_map = null;
319         
320         // Read the color map, if we have one.
321         if (global_hascmap) {
322             if (!readColorMap(global_cmapsize,true)) {
323                 return false;
324             }
325         }
326         
327         return true;
328     }
329     
330     /** Read a local image descriptor and optional local color map. */
331     private boolean readLocalImageDescriptor() throws IOException {
332         int packed;
333         
334         if (!readIntoBuf(9) ) return false;
335         
336         left       = _buf[0] | (_buf[1] << 8);
337         top        = _buf[2] | (_buf[3] << 8);
338         p.width      = _buf[4] | (_buf[5] << 8);
339         p.height     = _buf[6] | (_buf[7] << 8);
340         packed        = _buf[8];
341         hascmap    = (packed & LOCALCOLORMAP) == LOCALCOLORMAP;
342         cmapsize   = 2 << (packed & 0x07);
343         interlaced = (packed & INTERLACE) == INTERLACE;
344         color_map  = null;
345         
346         // Read the local color table, if there is one.
347         return !(hascmap && !readColorMap(cmapsize,false));
348     }
349
350     /** Read a color map (global or local). */
351     private boolean readColorMap(int nColors, boolean isGlobal)
352         throws IOException {
353         int[] map = new int[nColors];
354         for( int i=0; i < nColors; ++i) {
355             if (!readIntoBuf(3) ) return false;
356             map[i] = (_buf[0] << 16) | (_buf[1] << 8) | _buf[2] | 0xFF000000;
357         }
358         if (isGlobal) global_color_map = map;
359         else color_map = map;
360         return true;
361     }
362
363     /** Read the contents of a GIF89a Graphical Extension Block. */
364     private boolean readExtensionBlock() throws IOException {
365         if (!readIntoBuf(1) ) return false;
366         int label = _buf[0];
367         int count = -1;
368         switch (label) {
369         case 0x01:      // Plain Text Extension
370         case 0xff:      // Application Extension
371         case 0xfe:      // Comment Extension
372             break;
373         case 0xf9:      // Graphic Control Extension
374             count = getDataBlock();
375             if (count < 0) return true;
376             // Check for transparency setting.
377             if ((_buf[0] & HASTRANSPARENCY) != 0) trans_idx = _buf[3];
378             else trans_idx = -1;
379         }
380         do { count = getDataBlock(); } while (count > 0);
381         return true;
382     }
383
384     /** Read a block of data from the GIF file. */
385     private int getDataBlock() throws IOException {
386         if (!readIntoBuf(1) ) return -1;
387         int count = _buf[0];
388         if (count != 0) if (!readIntoBuf(count) ) return -1;
389         return count;
390     }
391     
392     /** Read the indicated number of bytes into _buf, our instance-wide buffer. */
393     private boolean readIntoBuf(int count) throws IOException {
394         for(int i = 0; i < count; i++) if ((_buf[i] = _in.read()) == -1) return false;
395         return true;
396     }
397
398     // Private Data //////////////////////////////////////////////////////////
399
400     private Picture p;
401
402     // State management stuff
403     private int index = -1;
404     private BufferedInputStream _in = null;
405     private int[] _buf = new int[BUFSIZE];
406
407     // Transparency settings
408     private int trans_idx = -1;
409
410     // Global image descriptor contents
411     private int global_width = 0;
412     private int global_height = 0;
413     private int global_bgcolor = 0;
414     private int global_cmapsize = 0;
415     private boolean global_hascmap = false;
416     private int[] global_color_map = null;
417
418     // Local image descriptor contents
419     private int left = 0;
420     private int top = 0;
421     private int cmapsize = 0;
422     private boolean hascmap = false;
423     private boolean interlaced = false;
424     private int[] color_map = null;
425
426     // Variables used in getCode(...) to track sliding bit-window.
427     private int     bytes = 0;
428     private boolean done;
429     private int     c;
430     private long    window;
431     private int     bitsInWindow = 0;
432
433     // Class-wide constants.
434     private static final int INTERLACE      = 0x40;
435     private static final int GLOBALCOLORMAP = 0x80;
436     private static final int LOCALCOLORMAP  = 0x80;
437     private static final int HASTRANSPARENCY    = 0x01;
438     private static final int MAX_LWZ_BITS   = 12;
439     private static final int BUFSIZE        = 280;
440     private static final int[] _interlaceStep   = { 8, 8, 4, 2 };
441     private static final int[] _interlaceStart  = { 0, 4, 2, 1 };
442 }
443
444
445