use of java.awt.image.ConvolveOp in project BroadleafCommerce by BroadleafCommerce.
the class GaussianBlur method filter.
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
if (kernelSize < 1 || kernelSize > NUM_KERNELS) {
return src;
}
if (numOfPasses < 1) {
return src;
}
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();
ColorModel dstCM;
BufferedImage origDst = dst;
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();
}
}
float[] matrix = GAUSSIAN_BLUR_KERNELS[kernelSize - 1];
Kernel gaussianBlur1 = new Kernel(matrix.length, 1, matrix);
Kernel gaussianBlur2 = new Kernel(1, matrix.length, matrix);
ConvolveOp gaussianOp1 = new ConvolveOp(gaussianBlur1, ConvolveOp.EDGE_NO_OP, null);
ConvolveOp gaussianOp2 = new ConvolveOp(gaussianBlur2, ConvolveOp.EDGE_NO_OP, null);
BufferedImage tempImage = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
dst = new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
BufferedImage nextSource = src;
for (int i = 0; i < numOfPasses; i++) {
tempImage = gaussianOp1.filter(nextSource, tempImage);
dst = gaussianOp2.filter(tempImage, dst);
nextSource = dst;
}
if (needToConvert) {
ColorConvertOp ccop = new ColorConvertOp(hints);
ccop.filter(dst, origDst);
} else if (origDst != dst) {
java.awt.Graphics2D g = origDst.createGraphics();
try {
g.drawImage(dst, 0, 0, null);
} finally {
g.dispose();
}
}
return origDst;
}
Aggregations