MemoryImageSource.java [plain text]
package java.awt.image;
import java.util.Hashtable;
import java.util.Vector;
public class MemoryImageSource implements ImageProducer
{
private boolean animated = false;
private boolean fullbuffers = false;
private int[] pixeli;
private int width;
private int height;
private int offset;
private int scansize;
private byte[] pixelb;
private ColorModel cm;
private Hashtable props = new Hashtable();
private Vector consumers = new Vector();
public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off,
int scan)
{
this(w, h, cm, pix, off, scan, null);
}
public MemoryImageSource(int w, int h, ColorModel cm, byte[] pix, int off,
int scan, Hashtable props)
{
width = w;
height = h;
this.cm = cm;
offset = off;
scansize = scan;
this.props = props;
int max = ((scansize > width) ? scansize : width);
pixelb = pix;
}
public MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off,
int scan)
{
this(w, h, cm, pix, off, scan, null);
}
public MemoryImageSource(int w, int h, ColorModel cm, int[] pix, int off,
int scan, Hashtable props)
{
width = w;
height = h;
this.cm = cm;
offset = off;
scansize = scan;
this.props = props;
int max = ((scansize > width) ? scansize : width);
pixeli = pix;
}
public MemoryImageSource(int w, int h, int[] pix, int off, int scan,
Hashtable props)
{
this(w, h, ColorModel.getRGBdefault(), pix, off, scan, props);
}
public MemoryImageSource(int w, int h, int[] pix, int off, int scan)
{
this(w, h, ColorModel.getRGBdefault(), pix, off, scan, null);
}
public synchronized void addConsumer(ImageConsumer ic)
{
if (consumers.contains(ic))
return;
consumers.addElement(ic);
}
public synchronized boolean isConsumer(ImageConsumer ic)
{
if (consumers.contains(ic))
return true;
return false;
}
public synchronized void removeConsumer(ImageConsumer ic)
{
consumers.removeElement(ic);
}
public void startProduction(ImageConsumer ic)
{
if (! (consumers.contains(ic)))
consumers.addElement(ic);
Vector list = (Vector) consumers.clone();
for (int i = 0; i < list.size(); i++)
{
ic = (ImageConsumer) list.elementAt(i);
sendPicture(ic);
if (animated)
ic.imageComplete(ImageConsumer.SINGLEFRAME);
else
ic.imageComplete(ImageConsumer.STATICIMAGEDONE);
}
}
public void requestTopDownLeftRightResend(ImageConsumer ic)
{
startProduction(ic);
}
public synchronized void setAnimated(boolean animated)
{
this.animated = animated;
}
public synchronized void setFullBufferUpdates(boolean fullbuffers)
{
this.fullbuffers = fullbuffers;
}
public void newPixels()
{
if (animated == true)
{
ImageConsumer ic;
Vector list = (Vector) consumers.clone();
for (int i = 0; i < list.size(); i++)
{
ic = (ImageConsumer) list.elementAt(i);
sendPicture(ic);
ic.imageComplete(ImageConsumer.SINGLEFRAME);
}
}
}
private void sendPicture(ImageConsumer ic)
{
ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT);
if (props != null)
ic.setProperties(props);
ic.setDimensions(width, height);
ic.setColorModel(cm);
if (pixeli != null)
ic.setPixels(0, 0, width, height, cm, pixeli, offset, scansize);
else
ic.setPixels(0, 0, width, height, cm, pixelb, offset, scansize);
}
public synchronized void newPixels(int x, int y, int w, int h)
{
if (animated == true)
{
if (fullbuffers)
newPixels();
else
{
ImageConsumer ic;
Vector list = (Vector) consumers.clone();
for (int i = 0; i < list.size(); i++)
{
ic = (ImageConsumer) list.elementAt(i);
ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT);
if (props != null)
ic.setProperties(props);
if (pixeli != null)
{
int[] pixelbuf = new int[w * h];
for (int row = y; row < y + h; row++)
System.arraycopy(pixeli, row * scansize + x + offset,
pixelbuf, 0, w * h);
ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
}
else
{
byte[] pixelbuf = new byte[w * h];
for (int row = y; row < y + h; row++)
System.arraycopy(pixelb, row * scansize + x + offset,
pixelbuf, 0, w * h);
ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
}
ic.imageComplete(ImageConsumer.SINGLEFRAME);
}
}
}
}
public synchronized void newPixels(int x, int y, int w, int h,
boolean framenotify)
{
if (animated == true)
{
if (fullbuffers)
newPixels();
else
{
ImageConsumer ic;
Vector list = (Vector) consumers.clone();
for (int i = 0; i < list.size(); i++)
{
ic = (ImageConsumer) list.elementAt(i);
ic.setHints(ImageConsumer.TOPDOWNLEFTRIGHT);
if (props != null)
ic.setProperties(props);
if (pixeli != null)
{
int[] pixelbuf = new int[w * h];
for (int row = y; row < y + h; row++)
System.arraycopy(pixeli, row * scansize + x + offset,
pixelbuf, 0, w * h);
ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
}
else
{
byte[] pixelbuf = new byte[w * h];
for (int row = y; row < y + h; row++)
System.arraycopy(pixelb, row * scansize + x + offset,
pixelbuf, 0, w * h);
ic.setPixels(x, y, w, h, cm, pixelbuf, 0, w);
}
if (framenotify == true)
ic.imageComplete(ImageConsumer.SINGLEFRAME);
}
}
}
}
public synchronized void newPixels(byte[] newpix, ColorModel newmodel,
int offset, int scansize)
{
pixeli = null;
pixelb = newpix;
cm = newmodel;
this.offset = offset;
this.scansize = scansize;
if (animated == true)
newPixels();
}
public synchronized void newPixels(int[] newpix, ColorModel newmodel,
int offset, int scansize)
{
pixelb = null;
pixeli = newpix;
cm = newmodel;
this.offset = offset;
this.scansize = scansize;
if (animated == true)
newPixels();
}
}