Here is an example filter (that I know works). I had taken the
ConvolveOp code and rewrote the guts to apply a color matrix to the
image. Look at the filter() method that operates on Rasters.
David
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.color.ICC_Profile;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Point2D;
import java.awt.image.*;
public class ColorMatrixOp implements BufferedImageOp, RasterOp {
Kernel kernel;
int edgeHint;
RenderingHints hints;
/**
* Edge condition constants.
*/
/**
* Pixels at the edge of the destination image are set to zero. This
* is the default.
*/
public static final int EDGE_ZERO_FILL = 0;
/**
* Pixels at the edge of the source image are copied to
* the corresponding pixels in the destination without modification.
*/
public static final int EDGE_NO_OP = 1;
/**
* Constructs a ColorMatrixOp given a Kernel, an edge condition, and a
* RenderingHints object (which may be null).
* @see Kernel
* @see #EDGE_NO_OP
* @see #EDGE_ZERO_FILL
* @see java.awt.RenderingHints
*/
public ColorMatrixOp(Kernel kernel, int edgeCondition,
RenderingHints hints) {
this.kernel = kernel;
this.edgeHint = edgeCondition;
this.hints = hints;
}
/**
* Constructs a ColorMatrixOp given a Kernel. The edge condition
* will be EDGE_ZERO_FILL.
* @see Kernel
* @see #EDGE_ZERO_FILL
*/
public ColorMatrixOp(Kernel kernel) {
this.kernel = kernel;
this.edgeHint = EDGE_ZERO_FILL;
}
/**
* Returns the edge condition.
* @see #EDGE_NO_OP
* @see #EDGE_ZERO_FILL
*/
public int getEdgeCondition() {
return edgeHint;
}
/**
* Returns the Kernel.
*/
public final Kernel getKernel() {
return (Kernel) kernel.clone();
}
/**
* If the color model in the source image is not the same as that
* in the destination image, the pixels will be converted
* in the destination. If the destination image is null,
* a BufferedImage will be created with the source ColorModel.
* The IllegalArgumentException may be thrown if the source is the
* same as the destination.
*/
public final BufferedImage filter (BufferedImage src, BufferedImage
dst) {
if (src == null) {
throw new NullPointerException("src image is null");
}
if (src == dst) {
throw new IllegalArgumentException("src image cannot be the "+
"same as the dst image");
}
boolean needToConvert = false;
ColorModel srcCM = src.getColorModel();
int numBands = srcCM.getNumColorComponents();
ColorModel dstCM;
BufferedImage origDst = dst;
// Can't convolve an IndexColorModel. Need to expand it
if (srcCM instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel) srcCM;
src = icm.convertToIntDiscrete(src.getRaster(), false);
srcCM = src.getColorModel();
}
if (dst == null) {
dst = createCompatibleDestImage(src, null);
dstCM = srcCM;
origDst = dst;
}
else {
dstCM = dst.getColorModel();
if (srcCM.getColorSpace().getType() !=
dstCM.getColorSpace().getType())
{
needToConvert = true;
dst = createCompatibleDestImage(src, null);
dstCM = dst.getColorModel();
}
else if (dstCM instanceof IndexColorModel) {
dst = createCompatibleDestImage(src, null);
dstCM = dst.getColorModel();
}
}
// do the matrix operation src->dst
WritableRaster srcRaster = src.getRaster();
WritableRaster dstRaster = dst.getRaster();
int numComponents = 3;
if (srcCM.hasAlpha()) {
if (numBands-1 == numComponents || numComponents == 1) {
int minx = srcRaster.getMinX();
int miny = srcRaster.getMinY();
int[] bands = new int[numBands-1];
for (int i=0; i < numBands-1; i++) {
bands[i] = i;
}
srcRaster =
srcRaster.createWritableChild(minx, miny,
srcRaster.getWidth(),
srcRaster.getHeight(),
minx, miny,
bands);
}
}
if (dstCM.hasAlpha()) {
int dstNumBands = dstRaster.getNumBands();
if (dstNumBands-1 == numComponents || numComponents == 1) {
int minx = dstRaster.getMinX();
int miny = dstRaster.getMinY();
int[] bands = new int[numBands-1];
for (int i=0; i < numBands-1; i++) {
bands[i] = i;
}
dstRaster =
dstRaster.createWritableChild(minx, miny,
dstRaster.getWidth(),
dstRaster.getHeight(),
minx, miny,
bands);
}
}
filter(srcRaster, dstRaster);
if (needToConvert) {
ColorConvertOp ccop = new ColorConvertOp(hints);
ccop.filter(dst, origDst);
}
else if (origDst != dst) {
java.awt.Graphics2D g = origDst.createGraphics();
g.drawImage(dst, 0, 0, null);
}
return origDst;
}
/**
* The source and destination must have the same number of bands.
* If the destination Raster is null, a new Raster will be created.
* The IllegalArgumentException may be thrown if the source is
* the same as the destination.
*/
public final WritableRaster filter (Raster src, WritableRaster dst) {
int numBands = src.getNumBands();
int height = src.getHeight();
int width = src.getWidth();
int srcPix[] = new int[numBands];
if (dst == null) {
dst = createCompatibleDestRaster(src);
}
else if (src == dst) {
throw new IllegalArgumentException("src image cannot be the "+
"same as the dst image");
}
else if (src.getNumBands() != dst.getNumBands()) {
throw new ImagingOpException("Different number of bands in
src "+
" and dst Rasters");
}
// do the matrix operation src->dst
int sminX = src.getMinX();
int sY = src.getMinY();
int dminX = dst.getMinX();
int dY = dst.getMinY();
float [] kernVals = kernel.getKernelData(null);
for (int y=0; y < height; y++, sY++, dY++) {
int sX = sminX;
int dX = dminX;
for (int x=0; x < width; x++, sX++, dX++) {
// Find data for all bands at this x,y position
src.getPixel(sX, sY, srcPix);
// transform the data for all bands at this x,y position
srcPix[0] = (int)(srcPix[0]*kernVals[0] +
srcPix[1]*kernVals[1] + srcPix[2]*kernVals[2]);
srcPix[0] = (srcPix[0]<0)?0:(srcPix[0]>255)?255:srcPix[0];
srcPix[1] = (int)(srcPix[0]*kernVals[3] +
srcPix[1]*kernVals[4] + srcPix[2]*kernVals[5]);
srcPix[1] = (srcPix[1]<0)?0:(srcPix[1]>255)?255:srcPix[1];
srcPix[2] = (int)(srcPix[0]*kernVals[6] +
srcPix[1]*kernVals[7] + srcPix[2]*kernVals[8]);
srcPix[2] = (srcPix[2]<0)?0:(srcPix[2]>255)?255:srcPix[2];
// Put it back for all bands
dst.setPixel(dX, dY, srcPix);
}
}
return dst;
}
/**
* Creates a zeroed destination image with the correct size and number
* of bands. If destCM is null, an appropriate ColorModel will be used.
* @param src Source image for the filter operation.
* @param destCM ColorModel of the destination. Can be null.
*/
public BufferedImage createCompatibleDestImage(BufferedImage src,
ColorModel destCM) {
BufferedImage image;
if (destCM == null) {
destCM = src.getColorModel();
// Not much support for ICM
if (destCM instanceof IndexColorModel) {
destCM = ColorModel.getRGBdefault();
}
}
int w = src.getWidth();
int h = src.getHeight();
image = new BufferedImage (destCM,
destCM.createCompatibleWritableRaster(w, h),
destCM.isAlphaPremultiplied(), null);
return image;
}
/**
* Creates a zeroed destination Raster with the correct size and number
* of bands, given this source.
*/
public WritableRaster createCompatibleDestRaster(Raster src) {
return src.createCompatibleWritableRaster();
}
/**
* Returns the bounding box of the filtered destination image. Since
* this is not a geometric operation, the bounding box does not
* change.
*/
public final Rectangle2D getBounds2D(BufferedImage src) {
return getBounds2D(src.getRaster());
}
/**
* Returns the bounding box of the filtered destination Raster. Since
* this is not a geometric operation, the bounding box does not
* change.
*/
public final Rectangle2D getBounds2D(Raster src) {
return src.getBounds();
}
/**
* Returns the location of the destination point given a
* point in the source. If dstPt is non-null, it will
* be used to hold the return value. Since this is not a geometric
* operation, the srcPt will equal the dstPt.
*/
public final Point2D getPoint2D(Point2D srcPt, Point2D dstPt) {
if (dstPt == null) {
dstPt = new Point2D.Float();
}
dstPt.setLocation(srcPt.getX(), srcPt.getY());
return dstPt;
}
/**
* Returns the rendering hints for this op.
*/
public final RenderingHints getRenderingHints() {
return hints;
}
}
Ayman El_Gharabawy wrote:
all i want to to do is to make afilter on a specific part of any image
here is my part of code..
class RedBlueSwapFilterD extends RGBImageFilter {
imageVector iv=new imageVector();
public RedBlueSwapFilterD() {
// The filter's operation does not depend on the
// pixel's location, so IndexColorModels can be
// filtered directly.
canFilterIndexColorModel = false;
}
public int filterRGB(int x, int y, int rgb) {
return ((rgb & 0xff00ff00)
| ((rgb & 0xff0000) >> 16)
| ((rgb & 0xff) << 16));
}
}
//-----------------------------------------------//
here is part of my application..
RedBlueSwapFilterD colorfilterdimension = new RedBlueSwapFilterD();
image = getImage(getDocumentBase(),"1532z.jpg");
colorfilterdimension.filterRGBPixels (20,30,30,40,ai,0,378);
img2 = createImage(new FilteredImageSource(image.getSource(),colorfilterdimension));
g.drawImage (img1,100,100,this);
///---------------------------
i have run time error withfilterRGBPixels pls help
thankx
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".