java.awt.image.Kernel Java Examples

The following examples show how to use java.awt.image.Kernel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BufferedBufImgOps.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void enableConvolveOp(RenderQueue rq,
                                     SurfaceData srcData,
                                     ConvolveOp cop)
{
    // assert rq.lock.isHeldByCurrentThread();
    boolean edgeZero =
        cop.getEdgeCondition() == ConvolveOp.EDGE_ZERO_FILL;
    Kernel kernel = cop.getKernel();
    int kernelWidth = kernel.getWidth();
    int kernelHeight = kernel.getHeight();
    int kernelSize = kernelWidth * kernelHeight;
    int sizeofFloat = 4;
    int totalBytesRequired = 4 + 8 + 12 + (kernelSize * sizeofFloat);

    RenderBuffer buf = rq.getBuffer();
    rq.ensureCapacityAndAlignment(totalBytesRequired, 4);
    buf.putInt(ENABLE_CONVOLVE_OP);
    buf.putLong(srcData.getNativeOps());
    buf.putInt(edgeZero ? 1 : 0);
    buf.putInt(kernelWidth);
    buf.putInt(kernelHeight);
    buf.put(kernel.getKernelData(null));
}
 
Example #2
Source File: BufferedBufImgOps.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void enableConvolveOp(RenderQueue rq,
                                     SurfaceData srcData,
                                     ConvolveOp cop)
{
    // assert rq.lock.isHeldByCurrentThread();
    boolean edgeZero =
        cop.getEdgeCondition() == ConvolveOp.EDGE_ZERO_FILL;
    Kernel kernel = cop.getKernel();
    int kernelWidth = kernel.getWidth();
    int kernelHeight = kernel.getHeight();
    int kernelSize = kernelWidth * kernelHeight;
    int sizeofFloat = 4;
    int totalBytesRequired = 4 + 8 + 12 + (kernelSize * sizeofFloat);

    RenderBuffer buf = rq.getBuffer();
    rq.ensureCapacityAndAlignment(totalBytesRequired, 4);
    buf.putInt(ENABLE_CONVOLVE_OP);
    buf.putLong(srcData.getNativeOps());
    buf.putInt(edgeZero ? 1 : 0);
    buf.putInt(kernelWidth);
    buf.putInt(kernelHeight);
    buf.put(kernel.getKernelData(null));
}
 
Example #3
Source File: ConvolutionFilter.java    From GIFKR with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public final BufferedImage apply(BufferedImage img) {
	
	float[][] matrix = getMatrix();
	float[] data = getKernelData(matrix);
	if(normalize)
		normalize(data); 
	scale(data);
	if(isZero(data))
		return new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
	
	Kernel k = new Kernel(matrix[0].length, matrix.length, data);
	ConvolveOp op = new ConvolveOp(k, ConvolveOp.EDGE_NO_OP, null);
	
	BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
	img2.getGraphics().drawImage(img, 0, 0, null);
	
	return op.filter(img2, null);
}
 
Example #4
Source File: BoxBlurDemo.java    From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static ConvolveOp getBlurFilter(int radius) {
    if (radius < 1) {
        throw new IllegalArgumentException("Radius must be >= 1");
    }
    
    int size = radius * 2 + 1;
    float weight = 1.0f / (size * size);
    float[] data = new float[size * size];
    
    for (int i = 0; i < data.length; i++) {
        data[i] = weight;
    }
    
    Kernel kernel = new Kernel(size, size, data);
    return new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
}
 
Example #5
Source File: ImageComparisonUtil.java    From image-comparison with Apache License 2.0 6 votes vote down vote up
/**
 * Convert image to buffered image.
 *
 * @param img the object of the image to be converted to buffered image.
 * @return the converted buffered image.
 */
public static BufferedImage toBufferedImage(Image img) {
    if (img instanceof BufferedImage) {
        return (BufferedImage) img;
    }

    float softenFactor = 0.05f;
    final Image temp = new ImageIcon(img).getImage();
    final BufferedImage bufferedImage = new BufferedImage(
            temp.getWidth(null),
            temp.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    final Graphics g = bufferedImage.createGraphics();
    g.setColor(Color.white);
    g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
    g.drawImage(temp, 0, 0, null);
    g.dispose();

    final float[] softenArray = {0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0,
            softenFactor, 0};
    final Kernel kernel = new Kernel(3, 3, softenArray);
    final ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);

    return cOp.filter(bufferedImage, null);
}
 
Example #6
Source File: BufferedBufImgOps.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private static void enableConvolveOp(RenderQueue rq,
                                     SurfaceData srcData,
                                     ConvolveOp cop)
{
    // assert rq.lock.isHeldByCurrentThread();
    boolean edgeZero =
        cop.getEdgeCondition() == ConvolveOp.EDGE_ZERO_FILL;
    Kernel kernel = cop.getKernel();
    int kernelWidth = kernel.getWidth();
    int kernelHeight = kernel.getHeight();
    int kernelSize = kernelWidth * kernelHeight;
    int sizeofFloat = 4;
    int totalBytesRequired = 4 + 8 + 12 + (kernelSize * sizeofFloat);

    RenderBuffer buf = rq.getBuffer();
    rq.ensureCapacityAndAlignment(totalBytesRequired, 4);
    buf.putInt(ENABLE_CONVOLVE_OP);
    buf.putLong(srcData.getNativeOps());
    buf.putInt(edgeZero ? 1 : 0);
    buf.putInt(kernelWidth);
    buf.putInt(kernelHeight);
    buf.put(kernel.getKernelData(null));
}
 
Example #7
Source File: BufferedBufImgOps.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void enableConvolveOp(RenderQueue rq,
                                     SurfaceData srcData,
                                     ConvolveOp cop)
{
    // assert rq.lock.isHeldByCurrentThread();
    boolean edgeZero =
        cop.getEdgeCondition() == ConvolveOp.EDGE_ZERO_FILL;
    Kernel kernel = cop.getKernel();
    int kernelWidth = kernel.getWidth();
    int kernelHeight = kernel.getHeight();
    int kernelSize = kernelWidth * kernelHeight;
    int sizeofFloat = 4;
    int totalBytesRequired = 4 + 8 + 12 + (kernelSize * sizeofFloat);

    RenderBuffer buf = rq.getBuffer();
    rq.ensureCapacityAndAlignment(totalBytesRequired, 4);
    buf.putInt(ENABLE_CONVOLVE_OP);
    buf.putLong(srcData.getNativeOps());
    buf.putInt(edgeZero ? 1 : 0);
    buf.putInt(kernelWidth);
    buf.putInt(kernelHeight);
    buf.put(kernel.getKernelData(null));
}
 
Example #8
Source File: BufferedBufImgOps.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private static void enableConvolveOp(RenderQueue rq,
                                     SurfaceData srcData,
                                     ConvolveOp cop)
{
    // assert rq.lock.isHeldByCurrentThread();
    boolean edgeZero =
        cop.getEdgeCondition() == ConvolveOp.EDGE_ZERO_FILL;
    Kernel kernel = cop.getKernel();
    int kernelWidth = kernel.getWidth();
    int kernelHeight = kernel.getHeight();
    int kernelSize = kernelWidth * kernelHeight;
    int sizeofFloat = 4;
    int totalBytesRequired = 4 + 8 + 12 + (kernelSize * sizeofFloat);

    RenderBuffer buf = rq.getBuffer();
    rq.ensureCapacityAndAlignment(totalBytesRequired, 4);
    buf.putInt(ENABLE_CONVOLVE_OP);
    buf.putLong(srcData.getNativeOps());
    buf.putInt(edgeZero ? 1 : 0);
    buf.putInt(kernelWidth);
    buf.putInt(kernelHeight);
    buf.put(kernel.getKernelData(null));
}
 
Example #9
Source File: SmartBlurFilter.java    From Pixelitor with GNU General Public License v3.0 6 votes vote down vote up
@Override
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    int width = src.getWidth();
    int height = src.getHeight();

    pt = createProgressTracker(width + height);

    if (dst == null) {
        dst = createCompatibleDestImage(src, null);
    }

    int[] inPixels = new int[width * height];
    int[] outPixels = new int[width * height];
    getRGB(src, 0, 0, width, height, inPixels);

    Kernel kernel = GaussianFilter.makeKernel(hRadius);
    thresholdBlur(kernel, inPixels, outPixels, width, height, true, pt);
    thresholdBlur(kernel, outPixels, inPixels, height, width, true, pt);

    setRGB(dst, 0, 0, width, height, inPixels);

    finishProgressTracker();

    return dst;
}
 
Example #10
Source File: BufferedBufImgOps.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static void enableConvolveOp(RenderQueue rq,
                                     SurfaceData srcData,
                                     ConvolveOp cop)
{
    // assert rq.lock.isHeldByCurrentThread();
    boolean edgeZero =
        cop.getEdgeCondition() == ConvolveOp.EDGE_ZERO_FILL;
    Kernel kernel = cop.getKernel();
    int kernelWidth = kernel.getWidth();
    int kernelHeight = kernel.getHeight();
    int kernelSize = kernelWidth * kernelHeight;
    int sizeofFloat = 4;
    int totalBytesRequired = 4 + 8 + 12 + (kernelSize * sizeofFloat);

    RenderBuffer buf = rq.getBuffer();
    rq.ensureCapacityAndAlignment(totalBytesRequired, 4);
    buf.putInt(ENABLE_CONVOLVE_OP);
    buf.putLong(srcData.getNativeOps());
    buf.putInt(edgeZero ? 1 : 0);
    buf.putInt(kernelWidth);
    buf.putInt(kernelHeight);
    buf.put(kernel.getKernelData(null));
}
 
Example #11
Source File: SamePackingTypeTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImageOp createTestOp() {
    final int size = 1;
    final float v = 1f / (size * size);
    final float[] k_data = new float[size * size];
    Arrays.fill(k_data, v);

    Kernel k = new Kernel(size, size, k_data);
    return new ConvolveOp(k);
}
 
Example #12
Source File: BufferedBufImgOps.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**************************** ConvolveOp support ****************************/

    public static boolean isConvolveOpValid(ConvolveOp cop) {
        Kernel kernel = cop.getKernel();
        int kw = kernel.getWidth();
        int kh = kernel.getHeight();
        // REMIND: we currently can only handle 3x3 and 5x5 kernels,
        //         but hopefully this is just a temporary restriction;
        //         see native shader comments for more details
        if (!(kw == 3 && kh == 3) && !(kw == 5 && kh == 5)) {
            return false;
        }
        return true;
    }
 
Example #13
Source File: MlibOpsTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImageOp getConvolveOp() {
    int kw = 3;
    int kh = 3;
    int size = kw * kh;
    float[] kdata = new float[size];
    Arrays.fill(kdata, 1.0f / size);

    Kernel k  = new Kernel(kw, kh, kdata);
    return new ConvolveOp(k);
}
 
Example #14
Source File: GaussianGrayFilter.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Make a Gaussian blur kernel.
 *
 * @param radius desired kernel radius specified in pixels around center
 * @return the Gaussian kernel of desired radius
 */
public static Kernel makeKernel (float radius)
{
    final int r = (int) Math.ceil(radius);
    final int rows = (r * 2) + 1;
    final float[] matrix = new float[rows];
    final float sigma = 1f; //HB: was radius / 3;
    final float sigmaSq2 = 2 * sigma * sigma;
    final float radiusSq = radius * radius;

    float total = 0;
    int index = 0;

    for (int row = -r; row <= r; row++) {
        float distanceSq = row * row;

        if (distanceSq > radiusSq) {
            matrix[index] = 0;
        } else {
            matrix[index] = (float) Math.exp(-distanceSq / sigmaSq2);
        }

        total += matrix[index];
        index++;
    }

    // Normalize all matrix items
    for (int i = 0; i < rows; i++) {
        matrix[i] /= total;
    }

    return new Kernel(rows, 1, matrix);
}
 
Example #15
Source File: BufferedBufImgOps.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**************************** ConvolveOp support ****************************/

    public static boolean isConvolveOpValid(ConvolveOp cop) {
        Kernel kernel = cop.getKernel();
        int kw = kernel.getWidth();
        int kh = kernel.getHeight();
        // REMIND: we currently can only handle 3x3 and 5x5 kernels,
        //         but hopefully this is just a temporary restriction;
        //         see native shader comments for more details
        if (!(kw == 3 && kh == 3) && !(kw == 5 && kh == 5)) {
            return false;
        }
        return true;
    }
 
Example #16
Source File: BufferedBufImgOps.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**************************** ConvolveOp support ****************************/

    public static boolean isConvolveOpValid(ConvolveOp cop) {
        Kernel kernel = cop.getKernel();
        int kw = kernel.getWidth();
        int kh = kernel.getHeight();
        // REMIND: we currently can only handle 3x3 and 5x5 kernels,
        //         but hopefully this is just a temporary restriction;
        //         see native shader comments for more details
        if (!(kw == 3 && kh == 3) && !(kw == 5 && kh == 5)) {
            return false;
        }
        return true;
    }
 
Example #17
Source File: GaussianFilter.java    From Pixelitor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Make a Gaussian blur kernel.
 *
 * @param radius the blur radius
 * @return the kernel
 */
public static Kernel makeKernel(float radius) {
    int r = (int) Math.ceil(radius);
    int rows = r * 2 + 1;
    float[] matrix = new float[rows];
    float sigma = radius / 3;
    float sigma22 = 2 * sigma * sigma;
    float sigmaPi2 = 2 * ImageMath.PI * sigma;
    float sqrtSigmaPi2 = (float) Math.sqrt(sigmaPi2);
    float radius2 = radius * radius;
    float total = 0;
    int index = 0;
    for (int row = -r; row <= r; row++) {
        float distance = row * row;
        if (distance > radius2) {
            matrix[index] = 0;
        } else {
            matrix[index] = (float) Math.exp(-distance / sigma22) / sqrtSigmaPi2;
        }
        total += matrix[index];
        index++;
    }
    for (int i = 0; i < rows; i++) {
        matrix[i] /= total;
    }

    return new Kernel(rows, 1, matrix);
}
 
Example #18
Source File: ConvolveFilter.java    From pumpernickel with MIT License 5 votes vote down vote up
public static void convolve(Kernel kernel, int[] inPixels, int[] outPixels,
		int width, int height, boolean alpha, int edgeAction) {
	if (kernel.getHeight() == 1)
		convolveH(kernel, inPixels, outPixels, width, height, alpha,
				edgeAction);
	else if (kernel.getWidth() == 1)
		convolveV(kernel, inPixels, outPixels, width, height, alpha,
				edgeAction);
	else
		convolveHV(kernel, inPixels, outPixels, width, height, alpha,
				edgeAction);
}
 
Example #19
Source File: GaussianFilter.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Make a Gaussian blur kernel.
 */
public static Kernel makeKernel(float radius) {
	int r = (int) Math.ceil(radius);
	int rows = r * 2 + 1;
	float[] matrix = new float[rows];
	float sigma = radius / 3;
	float sigma22 = 2 * sigma * sigma;
	float sigmaPi2 = 2 * ImageMath.PI * sigma;
	float sqrtSigmaPi2 = (float) Math.sqrt(sigmaPi2);
	float radius2 = radius * radius;
	float total = 0;
	int index = 0;
	for (int row = -r; row <= r; row++) {
		float distance = row * row;
		if (distance > radius2)
			matrix[index] = 0;
		else
			matrix[index] = (float) Math.exp(-(distance) / sigma22)
					/ sqrtSigmaPi2;
		total += matrix[index];
		index++;
	}
	for (int i = 0; i < rows; i++)
		matrix[i] /= total;

	return new Kernel(rows, 1, matrix);
}
 
Example #20
Source File: SamePackingTypeTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImageOp createTestOp() {
    final int size = 1;
    final float v = 1f / (size * size);
    final float[] k_data = new float[size * size];
    Arrays.fill(k_data, v);

    Kernel k = new Kernel(size, size, k_data);
    return new ConvolveOp(k);
}
 
Example #21
Source File: OpCompatibleImageTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public OpCompatibleImageTest() {
    final Kernel kernel = new Kernel(3, 3,
            new float[] {
        1f/9f, 1f/9f, 1f/9f,
        1f/9f, 1f/9f, 1f/9f,
        1f/9f, 1f/9f, 1f/9f});
    op = new ConvolveOp(kernel);
}
 
Example #22
Source File: BufferedBufImgOps.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**************************** ConvolveOp support ****************************/

    public static boolean isConvolveOpValid(ConvolveOp cop) {
        Kernel kernel = cop.getKernel();
        int kw = kernel.getWidth();
        int kh = kernel.getHeight();
        // REMIND: we currently can only handle 3x3 and 5x5 kernels,
        //         but hopefully this is just a temporary restriction;
        //         see native shader comments for more details
        if (!(kw == 3 && kh == 3) && !(kw == 5 && kh == 5)) {
            return false;
        }
        return true;
    }
 
Example #23
Source File: ConvolveWidget.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a convolve widget with a specified ColvolveOp.
 * @param scene the scene
 * @param convolveOp the convolve operation
 */
public ConvolveWidget (Scene scene, ConvolveOp convolveOp) {
    super (scene);
    this.convolveOp = convolveOp;
    Kernel kernel = convolveOp.getKernel ();
    setBorder (BorderFactory.createEmptyBorder (kernel.getWidth (), kernel.getHeight ()));
}
 
Example #24
Source File: GraphicUtils.java    From xyTalk-pc with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
    * @param size
    * @return
    */
   private static ConvolveOp getBlurOp(int size) {
float[] data = new float[size * size];
float value = 1 / (float) (size * size);
for (int i = 0; i < data.length; i++) {
    data[i] = value;
}
return new ConvolveOp(new Kernel(size, size, data));
   }
 
Example #25
Source File: SamePackingTypeTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImageOp createTestOp() {
    final int size = 1;
    final float v = 1f / (size * size);
    final float[] k_data = new float[size * size];
    Arrays.fill(k_data, v);

    Kernel k = new Kernel(size, size, k_data);
    return new ConvolveOp(k);
}
 
Example #26
Source File: MlibOpsTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImageOp getConvolveOp() {
    int kw = 3;
    int kh = 3;
    int size = kw * kh;
    float[] kdata = new float[size];
    Arrays.fill(kdata, 1.0f / size);

    Kernel k  = new Kernel(kw, kh, kdata);
    return new ConvolveOp(k);
}
 
Example #27
Source File: OpCompatibleImageTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public OpCompatibleImageTest() {
    final Kernel kernel = new Kernel(3, 3,
            new float[] {
        1f/9f, 1f/9f, 1f/9f,
        1f/9f, 1f/9f, 1f/9f,
        1f/9f, 1f/9f, 1f/9f});
    op = new ConvolveOp(kernel);
}
 
Example #28
Source File: SamePackingTypeTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static BufferedImageOp createTestOp() {
    final int size = 1;
    final float v = 1f / (size * size);
    final float[] k_data = new float[size * size];
    Arrays.fill(k_data, v);

    Kernel k = new Kernel(size, size, k_data);
    return new ConvolveOp(k);
}
 
Example #29
Source File: BoxBlurDemo.java    From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static ConvolveOp getBlurFilter(int horizontalRadius,
        int verticalRadius) {
    int width = horizontalRadius * 2 + 1;
    int height = verticalRadius * 2 + 1;

    float weight = 1.0f / (width * height);
    float[] data = new float[width * height];
    
    for (int i = 0; i < data.length; i++) {
        data[i] = weight;
    }
    
    Kernel kernel = new Kernel(width, height, data);
    return new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
}
 
Example #30
Source File: ImageProducer.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * @param bimage
 */
private static BufferedImage blurImage(final BufferedImage bimage) {
    float[] fs = new float[] { 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f,
            1f / 9f, 1f / 9f, 1f / 9f, 1f / 9f };
    final Kernel kernel = new Kernel(3, 3, fs);
    final BufferedImageOp op = new ConvolveOp(kernel);
    return op.filter(bimage, null);
}