import java.awt.*;
import java.awt.image.*;

public class ImagePixel extends ImageFilter {
    public static int raster[];	// RGB raster image
    public int width, height;	// image size
    public Image image;		// raster image

    private static ColorModel defaultRGB = ColorModel.getRGBdefault();
    private static int rasterTarget[];

    public ImagePixel() {
	raster = null;
	width = height = -1;
	image = null;
    }

    public ImagePixel(Image image, Component c) {
	MediaTracker mt;

	width = image.getWidth(c);
	height = image.getHeight(c);

	if(width != -1 && height != -1)
	    rasterTarget = new int[width * height];
	else
	    rasterTarget = null;
	image = c.createImage(new FilteredImageSource(image.getSource(),
						      this));
	mt = new MediaTracker(c);
	mt.addImage(image, 0);    
	try {
	    mt.waitForID(0);

	    if(width == -1 || height == -1) {
		width = image.getWidth(c);
		height = image.getHeight(c);
	    }
	    this.image = image;
	    raster = rasterTarget;
	} catch (InterruptedException e) {
	    System.err.println("Error: " + e);
	    raster = null;
	    width = height = -1;
	    this.image = null;
	    raster = null;
	}
    }

    public ImagePixel(int width, int height, int raster[], Component c) {
	createImage(width, height, raster, c);
    }

    public Image createImage(Component c) {
	this.image = c.createImage(new MemoryImageSource(width, height, raster,
							 0, width));
	return this.image;
    }

    public Image createImage(int width, int height, int raster[],
			     Component c) {
	this.width = width;
	this.height = height;
	this.raster = raster;
	return createImage(c);
    }

    public void setDimensions(int width, int height) {
	if(rasterTarget == null)
	    rasterTarget = new int[width * height];
	raster = rasterTarget;
	consumer.setDimensions(width, height);
    }

    public void setColorModel(ColorModel model) {
        consumer.setColorModel(defaultRGB);
    }

    public void setHints(int hintflags) {
        consumer.setHints(hintflags);
    }

    public void setPixels(int x, int y, int w, int h, ColorModel model,
                          byte pixels[], int off, int scansize) {
	raster = rasterTarget;
        int srcoff = off;
        int dstoff = y * w + x;
        for (int yc = 0; yc < h; yc++)
            for (int xc = 0; xc < w; xc++)
                raster[dstoff++] = model.getRGB(pixels[srcoff++] & 0xff);
	consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
    }

    public void setPixels(int x, int y, int w, int h, ColorModel model,
                          int pixels[], int off, int scansize) {
	raster = rasterTarget;
        int srcoff = off;
        int dstoff = y * w + x;
        if (model == defaultRGB)
            for (int yc = 0; yc < h; yc++) {
                System.arraycopy(pixels, srcoff, raster, dstoff, w);
                srcoff += scansize;
                dstoff += w;
	    }
	else
            for (int yc = 0; yc < h; yc++)
                for (int xc = 0; xc < w; xc++)
                    raster[dstoff++] = model.getRGB(pixels[srcoff++]);
	consumer.setPixels(x, y, w, h, model, pixels, off, scansize);
    }
}
