FileDocCategorySizeDatePackage
BufferedImageFilter.javaAPI DocAndroid 1.5 API11983Wed May 06 22:41:54 BST 2009java.awt.image

BufferedImageFilter

public class BufferedImageFilter extends ImageFilter implements Cloneable
The BufferedImageFilter class provides filtering operations to the BufferedImage objects using operators which implement BufferedImageOp interface.
since
Android 1.0

Fields Summary
private static final org.apache.harmony.awt.gl.AwtImageBackdoorAccessor
accessor
The Constant accessor.
private BufferedImageOp
op
The op.
private WritableRaster
raster
The raster.
private int[]
iData
The i data.
private byte[]
bData
The b data.
private int
width
The width.
private int
height
The height.
private ColorModel
cm
The cm.
private boolean
forcedRGB
The forced rgb.
private int
transferType
The transfer type.
Constructors Summary
public BufferedImageFilter(BufferedImageOp op)
Instantiates a new BufferedImageFilter with the specified BufferedImageOp operator.

param
op the specified BufferedImageOp operator.
throws
NullPointerException if BufferedImageOp is null.


                                                      
       
        if (op == null) {
            throw new NullPointerException(Messages.getString("awt.05")); //$NON-NLS-1$
        }
        this.op = op;
    
Methods Summary
private voidcreateRaster(int dataType)
Creates the raster.

param
dataType the data type.

        boolean createdValidBuffer = false;
        try {
            raster = cm.createCompatibleWritableRaster(width, height);
            int rasterType = raster.getDataBuffer().getDataType();
            if (rasterType == dataType) {
                switch (rasterType) {
                    case DataBuffer.TYPE_INT: {
                        iData = accessor.getDataInt(raster.getDataBuffer());
                        if (iData != null) {
                            createdValidBuffer = true;
                        }
                        break;
                    }
                    case DataBuffer.TYPE_BYTE: {
                        bData = accessor.getDataByte(raster.getDataBuffer());
                        if (bData != null) {
                            createdValidBuffer = true;
                        }
                        break;
                    }
                    default:
                        createdValidBuffer = false;
                }

                if (cm == ColorModel.getRGBdefault()) {
                    forcedRGB = true;
                }
            } else {
                createdValidBuffer = false;
            }
        } catch (Exception e) {
            createdValidBuffer = false;
        }

        if (createdValidBuffer == false) {
            cm = ColorModel.getRGBdefault();
            raster = cm.createCompatibleWritableRaster(width, height);
            iData = accessor.getDataInt(raster.getDataBuffer());
            bData = null;
            forcedRGB = true;
        }
    
private voidforceRGB()
Force rgb.

        if (!forcedRGB) {
            forcedRGB = true;
            int size = width * height;
            int rgbData[] = new int[size];

            if (bData != null) {
                for (int i = 0; i < size; i++) {
                    rgbData[i] = cm.getRGB(bData[i]);
                }
            } else if (iData != null) {
                for (int i = 0; i < size; i++) {
                    rgbData[i] = cm.getRGB(iData[i]);
                }
            }

            cm = ColorModel.getRGBdefault();
            DataBufferInt db = new DataBufferInt(rgbData, size);
            int masks[] = new int[] {
                    0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000
            };
            raster = Raster.createPackedRaster(db, width, height, width, masks, null);
            iData = accessor.getDataInt(db);
            bData = null;
            transferType = DataBuffer.TYPE_INT;
        }
    
public java.awt.image.BufferedImageOpgetBufferedImageOp()
Gets the BufferedImageOp operator associated with this BufferedImageFilter object.

return
the BufferedImageOp associated with this BufferedImageFilter object.

        return op;
    
public voidimageComplete(int status)

        if (status == STATICIMAGEDONE || status == SINGLEFRAMEDONE) {
            BufferedImage bim = new BufferedImage(cm, raster, cm.isAlphaPremultiplied, null);
            bim = op.filter(bim, null);
            DataBuffer dstDb = bim.getRaster().getDataBuffer();
            ColorModel dstCm = bim.getColorModel();
            int dstW = bim.getWidth();
            int dstH = bim.getHeight();

            consumer.setDimensions(dstW, dstH);

            if (dstDb.getDataType() == DataBuffer.TYPE_INT) {
                consumer.setColorModel(dstCm);
                consumer.setPixels(0, 0, dstW, dstH, dstCm, accessor.getDataInt(dstDb), 0, dstW);
            } else if (dstDb.getDataType() == DataBuffer.TYPE_BYTE) {
                consumer.setColorModel(dstCm);
                consumer.setPixels(0, 0, dstW, dstH, dstCm, accessor.getDataByte(dstDb), 0, dstW);
            } else {
                int dstData[] = bim.getRGB(0, 0, dstW, dstH, null, 0, dstW);
                dstCm = ColorModel.getRGBdefault();
                consumer.setColorModel(dstCm);
                consumer.setPixels(0, 0, dstW, dstH, dstCm, dstData, 0, dstW);
            }
        } else if (status == IMAGEERROR || status == IMAGEABORTED) {
            reset();
        }

        consumer.imageComplete(status);
    
private voidreset()
Reset.

        width = 0;
        height = 0;
        forcedRGB = false;
        cm = null;
        iData = null;
        bData = null;
        transferType = DataBuffer.TYPE_UNDEFINED;
        raster = null;
    
public voidsetColorModel(java.awt.image.ColorModel model)

        if (this.cm != null && this.cm != model && raster != null) {
            forceRGB();
        } else {
            this.cm = model;
        }
    
public voidsetDimensions(int width, int height)

        this.width = width;
        this.height = height;
        // Stop image consuming if no pixels expected.
        if (width <= 0 || height <= 0) {
            consumer.imageComplete(ImageConsumer.STATICIMAGEDONE);
            reset();
        }
    
public voidsetPixels(int x, int y, int w, int h, java.awt.image.ColorModel model, byte[] pixels, int off, int scansize)

        setPixels(x, y, w, h, model, pixels, off, scansize, true);
    
public voidsetPixels(int x, int y, int w, int h, java.awt.image.ColorModel model, int[] pixels, int off, int scansize)

        setPixels(x, y, w, h, model, pixels, off, scansize, false);
    
private voidsetPixels(int x, int y, int w, int h, java.awt.image.ColorModel model, java.lang.Object pixels, int off, int scansize, boolean isByteData)
Sets the pixels.

param
x the x.
param
y the y.
param
w the w.
param
h the h.
param
model the model.
param
pixels the pixels.
param
off the off.
param
scansize the scansize.
param
isByteData the is byte data.

        // Check bounds
        // Need to copy only the pixels that will fit into the destination area
        if (x < 0) {
            w -= x;
            off += x;
            x = 0;
        }

        if (y < 0) {
            h -= y;
            off += y * scansize;
            y = 0;
        }

        if (x + w > width) {
            w = width - x;
        }

        if (y + h > height) {
            h = height - y;
        }

        if (w <= 0 || h <= 0) {
            return;
        }

        // Check model
        if (this.cm == null) {
            setColorModel(model);
        } else if (model == null) {
            model = this.cm;
        } else if (!model.equals(this.cm)) {
            forceRGB();
        }

        boolean canArraycopy;
        // Process pixels
        switch (transferType) {
            case DataBuffer.TYPE_UNDEFINED: {
                if (isByteData) {
                    transferType = DataBuffer.TYPE_BYTE;
                    createRaster(transferType);
                    // bData = new byte[width*height];
                    canArraycopy = !forcedRGB;
                    break;
                }
                transferType = DataBuffer.TYPE_INT;
                createRaster(transferType);
                // iData = new int[width*height];
                canArraycopy = !forcedRGB || model.equals(ColorModel.getRGBdefault());
                break;
            } // And proceed to copy the pixels
            case DataBuffer.TYPE_INT: {
                if (isByteData) { // There are int data already but the new data
                    // are bytes
                    forceRGB();
                    canArraycopy = false;
                    break;
                } else if (!forcedRGB || model.equals(ColorModel.getRGBdefault())) {
                    canArraycopy = true;
                    break;
                } // Else fallback to the RGB conversion
            }
            case DataBuffer.TYPE_BYTE: {
                if (isByteData && !forcedRGB) {
                    canArraycopy = true;
                    break;
                }

                // RGB conversion
                canArraycopy = false;
                break;
            }
            default: {
                throw new IllegalStateException(Messages.getString("awt.06")); //$NON-NLS-1$
            }
        }

        off += x;
        int maxOffset = off + h * scansize;
        int dstOffset = x + y * width;

        if (canArraycopy) {
            Object dstArray = isByteData ? (Object)bData : (Object)iData;
            for (; off < maxOffset; off += scansize, dstOffset += width) {
                System.arraycopy(pixels, off, dstArray, dstOffset, w);
            }
        } else {
            // RGB conversion
            for (; off < maxOffset; off += scansize, dstOffset += width) {
                int srcPos = off;
                int dstPos = dstOffset;
                int maxDstPos = dstOffset + w;
                for (; dstPos < maxDstPos; dstPos++, srcPos++) {
                    iData[dstPos] = model.getRGB(isByteData ? ((byte[])pixels)[srcPos]
                            : ((int[])pixels)[srcPos]);
                }
            }
        }