*/
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 {
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
/** 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;
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++;
}
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);
// Private Data //////////////////////////////////////////////////////////
+ private Picture p;
+
// State management stuff
private int index = -1;
private BufferedInputStream _in = null;
// 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;