XOffScreenImage.java [plain text]
package gnu.awt.xlib;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.image.ColorModel;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.awt.image.ImageConsumer;
import java.util.Hashtable;
import gnu.awt.j2d.DirectRasterGraphics;
import gnu.awt.j2d.Graphics2DImpl;
import gnu.awt.j2d.IntegerGraphicsState;
import gnu.gcj.xlib.Drawable;
import gnu.gcj.xlib.Pixmap;
import gnu.gcj.xlib.Screen;
import gnu.gcj.xlib.Visual;
import gnu.gcj.xlib.GC;
public class XOffScreenImage extends Image
implements IntegerGraphicsState.ScreenCoupledImage,
ImageConsumer
{
private Pixmap pixmap;
private XGraphicsConfiguration config;
private int width;
private int height;
private Drawable drawable;
private ImageProducer prod;
private GC gc;
private ColorModel pixmapColorModel;
XOffScreenImage (XGraphicsConfiguration config, Drawable drawable, int width, int height, ColorModel cm)
{
this.config = config;
this.width = width;
this.height = height;
this.drawable = drawable;
pixmapColorModel = cm;
pixmap = new Pixmap (drawable, width, height, drawable.getDepth ());
gc = GC.create (pixmap);
}
XOffScreenImage (XGraphicsConfiguration config, Drawable drawable, ImageProducer prod, ColorModel cm)
{
this.config = config;
this.width = 0; this.height = 0;
this.drawable = drawable;
this.prod = prod;
pixmapColorModel = cm;
prod.startProduction (this);
}
public Pixmap getPixmap ()
{
return pixmap;
}
public void flush ()
{
pixmap = null;
}
public Graphics getGraphics ()
{
DirectRasterGraphics gfxDevice = new XGraphics (pixmap, config);
IntegerGraphicsState igState = new IntegerGraphicsState (gfxDevice);
Graphics2DImpl gfx2d = new Graphics2DImpl (config);
gfx2d.setState (igState);
return gfx2d;
}
public int getHeight (ImageObserver observer)
{
return height;
}
public int getHeight ()
{
return height;
}
public ImageProducer getSource ()
{
if (prod == null)
throw new UnsupportedOperationException ("getSource not supported");
else
return prod;
}
public int getWidth (ImageObserver observer)
{
return width;
}
public int getWidth ()
{
return width;
}
public Object getProperty (String name, ImageObserver observer)
{
return null;
}
public GraphicsConfiguration getGraphicsConfiguration ()
{
return config;
}
public void imageComplete (int status)
{
}
public void setColorModel (ColorModel model)
{
}
public void setDimensions (int width, int height)
{
this.width = width;
this.height = height;
pixmap = new Pixmap (drawable, width, height, drawable.getDepth ());
gc = GC.create (pixmap);
}
public void setHints (int flags)
{
}
public void setPixels (int x, int y, int w, int h, ColorModel model, int[] pixels, int offset, int scansize)
{
int idx = 0;
float[] normalizedComponents = new float [4];
int[] unnormalizedComponents = { 0, 0, 0, 0xff };
normalizedComponents[3] = 1;
for (int yp=y; yp < (y + h); yp++)
{
for (int xp=x; xp < (x + w); xp++)
{
int p = (yp - y) * scansize + (xp - x) + offset;
normalizedComponents[0] = (float)model.getRed (pixels[p]) / 255F;
normalizedComponents[1] = (float)model.getGreen (pixels[p]) / 255F;
normalizedComponents[2] = (float)model.getBlue (pixels[p]) / 255F;
pixmapColorModel.getUnnormalizedComponents (normalizedComponents, 0,
unnormalizedComponents, 0);
int pixelColor = pixmapColorModel.getDataElement (unnormalizedComponents, 0);
gc.setForeground (pixelColor);
gc.drawPoint (xp, yp);
}
}
}
public void setPixels (int x, int y, int w, int h, ColorModel model, byte[] pixels, int offset, int scansize)
{
int idx = 0;
float[] normalizedComponents = new float [4];
int[] unnormalizedComponents = { 0, 0, 0, 0xff };
normalizedComponents[3] = 1;
for (int yp=y; yp < (y + h); yp++)
{
for (int xp=x; xp < (x + w); xp++)
{
int p = (yp - y) * scansize + (xp - x) + offset;
normalizedComponents[0] = (float)model.getRed (pixels[p]) / 255F;
normalizedComponents[1] = (float)model.getGreen (pixels[p]) / 255F;
normalizedComponents[2] = (float)model.getBlue (pixels[p]) / 255F;
pixmapColorModel.getUnnormalizedComponents (normalizedComponents, 0,
unnormalizedComponents, 0);
int pixelColor = pixmapColorModel.getDataElement (unnormalizedComponents, 0);
gc.setForeground (pixelColor);
gc.drawPoint (xp, yp);
}
}
}
public void setProperties (Hashtable props)
{
}
}