X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fxwt%2Ftranslators%2FGIF.java;h=572c124c5bb92b81b7bfe381217647cb5facb173;hp=4ac420ffb2b63593a7420286fd3512ce426aa370;hb=3591b88b94a6bb378af3d4abe6eb5233ce583104;hpb=42da60bd167403eccc3466575772819007388cfd diff --git a/src/org/xwt/translators/GIF.java b/src/org/xwt/translators/GIF.java index 4ac420f..572c124 100644 --- a/src/org/xwt/translators/GIF.java +++ b/src/org/xwt/translators/GIF.java @@ -43,43 +43,44 @@ */ 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 { // Public Methods ///////////////////////////////////////////////////////// - public GIF() { } + private GIF() { } - public int[] getData() { return data; } - public int getWidth() { return width; } - public int getHeight() { return height; } + private static Queue instances = new Queue(10); - /** 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 Picture fromInputStream(InputStream is, String name) { + public static void load(InputStream is, Picture p) { + GIF g = (GIF)instances.remove(false); + if (g == null) g = new GIF(); try { - if (is instanceof BufferedInputStream) _in = (BufferedInputStream)is; - else _in = new BufferedInputStream(is); - decodeAsBufferedImage(0); - return Platform.createPicture(data, width, height); + 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 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 { 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 { /** 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 { 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 { 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 { // Private Data ////////////////////////////////////////////////////////// + private Picture p; + // State management stuff private int index = -1; private BufferedInputStream _in = null; @@ -415,8 +418,6 @@ public class GIF { // 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;