mass rename and rebranding from xwt to ibex - fixed to use ixt files
[org.ibex.core.git] / src / org / xwt / translators / GIF.java
index b3359f5..572c124 100644 (file)
  */
 package org.xwt.translators;
 
-import org.xwt.*;
-import org.xwt.util.*;
+import org.ibex.*;
+import org.ibex.util.*;
 import java.io.BufferedInputStream;
 import java.io.InputStream;
 import java.io.IOException;
-import java.io.PrintWriter;
 
 /** Converts an InputStream carrying a GIF image into an ARGB int[] */
-public class GIF extends ImageDecoder {
+public class GIF {
 
     // Public Methods /////////////////////////////////////////////////////////
 
-    public int[] getData() { return data; }
-    public int getWidth() { return width; }
-    public int getHeight() { return height; }
+    private GIF() { }
 
-    /** Processes an image from InputStream is; returns null if there is an error
-        @param name A string describing the image; used for error reporting.
-     */
-    public static GIF decode(InputStream is, String name) {
+    private static Queue instances = new Queue(10);
+
+    public static void load(InputStream is, Picture p) {
+        GIF g = (GIF)instances.remove(false);
+        if (g == null) g = new GIF();
         try {
-            return new GIF(is, name);
+            g._load(is, p);
         } catch (Exception e) {
-            if (Log.on) Log.log(GIF.class, e);
-            return null;
+            if (Log.on) Log.info(GIF.class, e);
+            return;
         }
+        // FIXME: must reset fields
+        // if (instances.size() < 10) instances.append(g);
     }
 
-    private GIF(InputStream is, String name) throws IOException {
+    private void _load(InputStream is, Picture p) throws IOException {
+        this.p = p;
         if (is instanceof BufferedInputStream) _in = (BufferedInputStream)is;
         else _in = new BufferedInputStream(is);
         decodeAsBufferedImage(0);
+        p.isLoaded = true;
+        p = null;
+        _in = null;
     }
 
-    // Private Methods /////////////////////////////////////////////////////////
-
-    private int[] data = null;
 
     /** Decode a particular frame from the GIF file. Frames must be decoded in order, starting with 0. */
     private void decodeAsBufferedImage(int page) throws IOException, IOException {
@@ -110,7 +111,7 @@ public class GIF extends ImageDecoder {
             if (block_identifier != ',') continue;
 
             if (!readLocalImageDescriptor()) throw new IOException("Unexpected EOF(3)");
-            data = new int[width * height];
+            p.data = new int[p.width * p.height];
             readImage();
 
             // If we did not decode the requested index, need to go on
@@ -131,8 +132,8 @@ public class GIF extends ImageDecoder {
     /** Actually read the image data */
     private void readImage() 
         throws IOException, IOException {
-        int len = width;
-        int rows = height;
+        int len = p.width;
+        int rows = p.height;
         int initialCodeSize;
         int v;
         int xpos = 0, ypos = 0, pass = 0, i;
@@ -249,7 +250,7 @@ public class GIF extends ImageDecoder {
                 if (v < 0) return;
                 
                 // Finally, we can set a pixel!  Joy!
-                data[xpos + ypos * width] = cmap[v];
+                p.data[xpos + ypos * p.width] = cmap[v];
                 xpos++;
             }
             
@@ -334,8 +335,8 @@ public class GIF extends ImageDecoder {
         
         left       = _buf[0] | (_buf[1] << 8);
         top        = _buf[2] | (_buf[3] << 8);
-        width      = _buf[4] | (_buf[5] << 8);
-        height     = _buf[6] | (_buf[7] << 8);
+        p.width      = _buf[4] | (_buf[5] << 8);
+        p.height     = _buf[6] | (_buf[7] << 8);
         packed        = _buf[8];
         hascmap    = (packed & LOCALCOLORMAP) == LOCALCOLORMAP;
         cmapsize   = 2 << (packed & 0x07);
@@ -396,6 +397,8 @@ public class GIF extends ImageDecoder {
 
     // Private Data //////////////////////////////////////////////////////////
 
+    private Picture p;
+
     // State management stuff
     private int index = -1;
     private BufferedInputStream _in = null;
@@ -415,8 +418,6 @@ public class GIF extends ImageDecoder {
     // Local image descriptor contents
     private int left = 0;
     private int top = 0;
-    private int width = 0;
-    private int height = 0;
     private int cmapsize = 0;
     private boolean hascmap = false;
     private boolean interlaced = false;