Java Image Blur blurPass(int[] srcPixels, int[] dstPixels, int width, int height, int radius)

Here you can find the source of blurPass(int[] srcPixels, int[] dstPixels, int width, int height, int radius)

Description

Blurs the source pixels into the destination pixels.

License

Open Source License

Parameter

Parameter Description
srcPixels the source pixels
dstPixels the destination pixels
width the width of the source picture
height the height of the source picture
radius the radius of the blur effect

Declaration

public static void blurPass(int[] srcPixels, int[] dstPixels, int width, int height, int radius) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from www .ja  v  a  2s.c  om*/
     * <p>
     * Blurs the source pixels into the destination pixels. The force of the blur is specified by the radius which must be greater than 0.
     * </p>
     * <p>
     * The source and destination pixels arrays are expected to be in the RGBA format.
     * </p>
     * 
     * @param srcPixels
     * the source pixels
     * @param dstPixels
     * the destination pixels
     * @param width
     * the width of the source picture
     * @param height
     * the height of the source picture
     * @param radius
     * the radius of the blur effect
     * @author Romain Guy <romain.guy@mac.com>
     */
    public static void blurPass(int[] srcPixels, int[] dstPixels, int width, int height, int radius) {
        final int windowSize = radius * 2 + 1;
        final int radiusPlusOne = radius + 1;

        int sumRed;
        int sumGreen;
        int sumBlue;
        int sumAlpha;

        int srcIndex = 0;
        int dstIndex;
        int pixel;

        int[] sumLookupTable = new int[256 * windowSize];
        for (int i = 0; i < sumLookupTable.length; i++)
            sumLookupTable[i] = i / windowSize;

        int[] indexLookupTable = new int[radiusPlusOne];
        if (radius < width)
            for (int i = 0; i < indexLookupTable.length; i++)
                indexLookupTable[i] = i;
        else {
            for (int i = 0; i < width; i++)
                indexLookupTable[i] = i;
            for (int i = width; i < indexLookupTable.length; i++)
                indexLookupTable[i] = width - 1;
        }

        for (int y = 0; y < height; y++) {
            sumAlpha = sumRed = sumGreen = sumBlue = 0;
            dstIndex = y;

            pixel = srcPixels[srcIndex];
            sumRed += radiusPlusOne * (pixel >> 24 & 0xFF);
            sumGreen += radiusPlusOne * (pixel >> 16 & 0xFF);
            sumBlue += radiusPlusOne * (pixel >> 8 & 0xFF);
            sumAlpha += radiusPlusOne * (pixel & 0xFF);

            for (int i = 1; i <= radius; i++) {
                pixel = srcPixels[srcIndex + indexLookupTable[i]];
                sumRed += pixel >> 24 & 0xFF;
                sumGreen += pixel >> 16 & 0xFF;
                sumBlue += pixel >> 8 & 0xFF;
                sumAlpha += pixel & 0xFF;
            }

            for (int x = 0; x < width; x++) {
                dstPixels[dstIndex] = sumLookupTable[sumRed] << 24 | sumLookupTable[sumGreen] << 16
                        | sumLookupTable[sumBlue] << 8 | sumLookupTable[sumAlpha];
                dstIndex += height;

                int nextPixelIndex = x + radiusPlusOne;
                if (nextPixelIndex >= width)
                    nextPixelIndex = width - 1;

                int previousPixelIndex = x - radius;
                if (previousPixelIndex < 0)
                    previousPixelIndex = 0;

                int nextPixel = srcPixels[srcIndex + nextPixelIndex];
                int previousPixel = srcPixels[srcIndex + previousPixelIndex];

                sumRed += nextPixel >> 24 & 0xFF;
                sumRed -= previousPixel >> 24 & 0xFF;

                sumGreen += nextPixel >> 16 & 0xFF;
                sumGreen -= previousPixel >> 16 & 0xFF;

                sumBlue += nextPixel >> 8 & 0xFF;
                sumBlue -= previousPixel >> 8 & 0xFF;

                sumAlpha += nextPixel & 0xFF;
                sumAlpha -= previousPixel & 0xFF;
            }

            srcIndex += width;
        }
    }
}

Related

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