Java Image Blur blur(BufferedImage image, Kernel kernel)

Here you can find the source of blur(BufferedImage image, Kernel kernel)

Description

Apply blur filter to the given image.

License

Apache License

Parameter

Parameter Description
image - the image to blur
kernel - the kernel used for bluring

Return

a blured instance of the image

Declaration

public static BufferedImage blur(BufferedImage image, Kernel kernel) 

Method Source Code

//package com.java2s;
/*/*ww w .  j ava2s  .c  o  m*/
 * Copyright 2013 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.image.BufferedImage;
import java.awt.image.Kernel;

public class Main {
    /**
     * Apply blur filter to the given image. The blur intensity is defined by the given kernel.
     *
     * @param image  - the image to blur
     * @param kernel - the kernel used for bluring
     * @return a blured instance of the image
     */
    public static BufferedImage blur(BufferedImage image, Kernel kernel) {
        final int width = image.getWidth();
        final int height = image.getHeight();

        BufferedImage result = new BufferedImage(width, height, image.getType());
        int[] in = new int[width * height];
        int[] out = new int[width * height];
        image.getRGB(0, 0, width, height, in, 0, width);

        convolve(kernel, in, out, width, height);
        convolve(kernel, out, in, height, width);

        result.setRGB(0, 0, width, height, in, 0, width);
        return result;
    }

    private static void convolve(Kernel kernel, int[] in, int[] out, int width, int height) {
        final float[] matrix = kernel.getKernelData(null);
        final int cols = kernel.getWidth();
        final int halfCols = cols / 2;

        for (int y = 0; y < height; y++) {
            int index = y;
            int imageOffset = y * width;
            for (int x = 0; x < width; x++) {
                int matrixOffset = halfCols;
                float r = 0;
                float g = 0;
                float b = 0;
                for (int c = -halfCols; c <= halfCols; c++) {
                    float f = matrix[c + matrixOffset];
                    if (f != 0) {
                        int idx = x + c;
                        if (idx < 0) {
                            idx = (x + width) % width;
                        } else if (idx >= width) {
                            idx = (x + width) % width;
                        }
                        int rgb = in[idx + imageOffset];
                        r += f * ((rgb >> 16) & 0xff);
                        g += f * ((rgb >> 8) & 0xff);
                        b += f * (rgb & 0xff);
                    }
                }
                int ia = 0xff;
                int ir = clamp((int) (r + 0.5));
                int ig = clamp((int) (g + 0.5));
                int ib = clamp((int) (b + 0.5));
                out[index] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
                index += height;
            }
        }
    }

    /**
     * Clamp a value to range 0..255.
     *
     * @param c - value to clamp
     * @return clamped value in the range 0..255
     */
    private static int clamp(int c) {
        if (c < 0) {
            return 0;
        }
        if (c > 255) {
            return 255;
        }
        return c;
    }
}

Related

  1. blur(int[] srcPixels, int[] dstPixels, int width, int height, float[] kernel, int radius)
  2. blurImage(BufferedImage image, float blurValue)
  3. blurImage(BufferedImage img, BufferedImage dest)
  4. blurPass(int[] srcPixels, int[] dstPixels, int width, int height, int radius)