Search in sources :

Example 26 with ConvolveOp

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;
}
Also used : BufferedImage(java.awt.image.BufferedImage) ColorConvertOp(java.awt.image.ColorConvertOp) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) ConvolveOp(java.awt.image.ConvolveOp) Kernel(java.awt.image.Kernel) IndexColorModel(java.awt.image.IndexColorModel)

Aggregations

ConvolveOp (java.awt.image.ConvolveOp)26 Kernel (java.awt.image.Kernel)21 BufferedImage (java.awt.image.BufferedImage)15 Color (java.awt.Color)4 GradientPaint (java.awt.GradientPaint)4 Graphics2D (java.awt.Graphics2D)4 LookupOp (java.awt.image.LookupOp)4 Font (java.awt.Font)3 FontMetrics (java.awt.FontMetrics)3 Rectangle2D (java.awt.geom.Rectangle2D)3 RoundRectangle2D (java.awt.geom.RoundRectangle2D)3 Point (java.awt.Point)2 AffineTransformOp (java.awt.image.AffineTransformOp)2 ByteLookupTable (java.awt.image.ByteLookupTable)2 LookupTable (java.awt.image.LookupTable)2 RescaleOp (java.awt.image.RescaleOp)2 WritableRaster (java.awt.image.WritableRaster)2 SurfaceData (sun.java2d.SurfaceData)2 JPEGEncodeParam (com.sun.image.codec.jpeg.JPEGEncodeParam)1 JPEGImageEncoder (com.sun.image.codec.jpeg.JPEGImageEncoder)1