import java.awt.*; import java.awt.image.*; import java.io.*; /** * Handles frame buffer and Targa TGA image files. * @author: Phil Gage */ class FrameBuffer { final int headersize = 18; private int[] pix; //array for Java Image private byte buf[]; //TGA data private byte header[]= new byte[headersize]; //TGA header int width=0, height=0; /** Can use 0 for null color since alpha normally 1 bits */ private int NULLCOLOR = 0; /** Constuctor when image size unknown before loadtga */ FrameBuffer () { } /** Constuctor when image size known */ FrameBuffer (int width, int height) { this.width = width; this.height = height; buf = new byte[3*width*height+headersize]; pix = new int[width*height]; for (int i=0; i 1.0) c.r = 1.0; if (c.g > 1.0) c.g = 1.0; if (c.b > 1.0) c.b = 1.0; // Pack into TGA alpha, red, green, blue format pix[width*y+x] = 255<<24 | (int)(255*c.r)<<16 | (int)(255*c.g)<<8 | (int)(255*c.b); } /** Get frame buffer pixel (x,y) color */ void getPixel (int x, int y, Color color) { // Unpack from TGA alpha, red, green, blue format color.r = (double)((pix[width*y+x]>>16) & 255) / 255.0; color.g = (double)((pix[width*y+x]>>8) & 255) / 255.0; color.b = (double)((pix[width*y+x]>>0) & 255) / 255.0; } /** Return Image of current buffer for display */ public Image getImage (Frame f) { Image img; img = f.createImage(new MemoryImageSource(width,height,pix,0,width)); return img; } /** Read frame buffer from TGA image file Returns true if file is found and loaded */ public boolean loadtga (String filename) { try { FileInputStream in = new FileInputStream (filename); in.read (header, 0, headersize); // If not initialized, read width, height and alloc if (width == 0) { // Signed bytes are tricky to convert to ints width = header[12] > 0 ? header[12] : (int)header[12] + 256; width += 256*(int)header[13]; height = header[14] > 0 ? header[14] : (int)header[14] + 256; height += 256*(int)header[15]; System.out.println ("Image width=" + width + " height=" + height); // Check size to avoid nasty crash if (width < 10 || width > 1280 || height < 10 || height > 1024) { System.out.println ("Bad Image size"); return false; } buf = new byte[3*width*height]; } in.read (buf, 0, 3*width*height); // Must alloc pix for each frame, saved in image pix = new int[width*height]; for (int i=0; i> 8); header[12] = (byte)(width & 255); header[15] = (byte)(height >> 8); header[14] = (byte)(height & 255); header[16] = 24; //pixelsize header[17] = 32; //descriptor for (int i=0; i>16) & 255); buf[3*i+1] = (byte)((pix[i]>>8) & 255); buf[3*i+0] = (byte)((pix[i]>>0) & 255); } try { FileOutputStream out = new FileOutputStream (filename); out.write (header, 0, headersize); out.write (buf, 0, 3*width*height); } catch (Exception e) { System.out.println ("savetga error: " + e); return false; } return true; } /** Shift image by x,y pixels, could improve speed */ void shift (int x, int y) { int offset = x + y*width; int last = width*height-1; if (offset > 0) for (int i=last; i>=offset; i--) pix[i] = pix[i-offset]; else if (offset < 0) for (int i=0; i<=last+offset; i++) pix[i] = pix[i-offset]; } }