package NET.worlds.scape; import java.awt.Image; import java.awt.Toolkit; import java.awt.image.ColorModel; import java.awt.image.DirectColorModel; import java.awt.image.ImageConsumer; import java.awt.image.IndexColorModel; import java.util.Hashtable; public class ImageConverter implements ImageConsumer { private String urlName; private String filename; private int width; private int height; private Image image; private boolean done; private boolean ok; private boolean debug = false; private ColorModel model; private int transparentColor = -1; private int hDIB; private int pixelPtr; static { nativeInit(); } public ImageConverter(String urlName, String filename) { this.urlName = urlName; this.filename = filename; } public static native void nativeInit(); public synchronized int convert() { this.image = Toolkit.getDefaultToolkit().getImage(this.filename); this.image.getSource().startProduction(this); while (!this.done) { try { this.wait(); } catch (InterruptedException var2) { } } int textureID = 0; if (this.ok) { textureID = this.convertDIBToTexture(); } this.cleanup(); return textureID; } @Override public void setDimensions(int width, int height) { if (this.debug) { System.out.println("Set dimensions: w " + width + " h " + height); } this.width = width; this.height = height; } @Override public void setProperties(Hashtable props) { if (this.debug) { System.out.println("Set properties"); } } @Override public void setColorModel(ColorModel model) { if (this.debug) { System.out.println("Set color model: " + model); } this.model = model; assert this.width != 0 && this.height != 0; int[] palette = (int[])null; int colors = 0; if (model instanceof IndexColorModel) { IndexColorModel im = (IndexColorModel)model; colors = im.getMapSize(); this.transparentColor = im.getTransparentPixel(); palette = new int[colors * 3]; for (int i = 0; i < colors; i++) { palette[i] = im.getRGB(i); } } else { DirectColorModel dm = (DirectColorModel)model; assert dm.getBlueMask() == 255; assert dm.getGreenMask() == 65280; assert dm.getRedMask() == 16711680; } this.prepareDIB(this.width, this.height, colors, palette); } @Override public void setHints(int hintflags) { if (this.debug) { System.out .println( "Set hints: " + ((hintflags & 1) != 0 ? " RANDOM " : "") + ((hintflags & 2) != 0 ? " TOPDOWNLEFTRIGHT " : "") + ((hintflags & 4) != 0 ? " COMPLETESCANS " : "") + ((hintflags & 8) != 0 ? " SINGLEPASS " : "") + ((hintflags & 16) != 0 ? " SINGLEFRAME " : "") ); } } @Override public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pixels, int off, int scansize) { if (this.debug) { System.out.println("setPixels(byte): x " + x + " y " + y + " w " + w + " h " + h + " model " + model + " off " + off + " scansize " + scansize); } assert model == this.model; this.setDIBPixelBytes(x, y, w, h, pixels, off, scansize); } @Override public void setPixels(int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize) { if (this.debug) { System.out.println("setPixels(int): x " + x + " y " + y + " w " + w + " h " + h + " model " + model + " off " + off + " scansize " + scansize); } assert model == this.model; this.setDIBPixelInts(x, y, w, h, pixels, off, scansize); } @Override public synchronized void imageComplete(int status) { if (this.debug) { String state = null; switch (status) { case 1: state = "ERROR"; break; case 2: state = "SINGLEDONE"; break; case 3: state = "STATICDONE"; break; case 4: state = "ABORTED"; } System.out.println("Image complete: " + state); } this.image.getSource().removeConsumer(this); this.image.flush(); this.ok = status != 1 && status != 4; this.done = true; this.notify(); } private native void prepareDIB(int var1, int var2, int var3, int[] var4); private native void setDIBPixelBytes(int var1, int var2, int var3, int var4, byte[] var5, int var6, int var7); private native void setDIBPixelInts(int var1, int var2, int var3, int var4, int[] var5, int var6, int var7); private native int convertDIBToTexture(); private native void cleanup(); }