Java Code Examples for java.awt.image.Kernel#getKernelData()

The following examples show how to use java.awt.image.Kernel#getKernelData() . 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: GaussianFilter.java    From Pixelitor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Blur and transpose a block of ARGB pixels.
 *
 * @param kernel     the blur kernel
 * @param inPixels   the input pixels
 * @param outPixels  the output pixels
 * @param width      the width of the pixel array
 * @param height     the height of the pixel array
 * @param alpha      whether to blur the alpha channel
 * @param edgeAction what to do at the edges
 */
public static void convolveAndTranspose(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha, boolean premultiply, boolean unpremultiply,
                                        int edgeAction, ProgressTracker pt) {
    float[] matrix = kernel.getKernelData(null);
    int cols = kernel.getWidth();
    int cols2 = cols / 2;

    Future<?>[] resultLines = new Future[height];
    for (int y = 0; y < height; y++) {
        int finalY = y;
        Runnable lineTask = () -> convolveAndTransposeLine(inPixels, outPixels, width, height, alpha, premultiply, unpremultiply, edgeAction, matrix, cols2, finalY);
        resultLines[y] = ThreadPool.submit(lineTask);
    }

    ThreadPool.waitFor(resultLines, pt);
}
 
Example 2
Source File: GaussianFilter.java    From pumpernickel with MIT License 4 votes vote down vote up
public static void convolveAndTranspose(Kernel kernel, int[] inPixels,
		int[] outPixels, int width, int height, boolean alpha,
		int edgeAction) {
	float[] matrix = kernel.getKernelData(null);
	int cols = kernel.getWidth();
	int cols2 = cols / 2;

	for (int y = 0; y < height; y++) {
		int index = y;
		int ioffset = y * width;
		for (int x = 0; x < width; x++) {
			float r = 0, g = 0, b = 0, a = 0;
			int moffset = cols2;
			for (int col = -cols2; col <= cols2; col++) {
				float f = matrix[moffset + col];

				if (f != 0) {
					int ix = x + col;
					if (ix < 0) {
						if (edgeAction == CLAMP_EDGES)
							ix = 0;
						else if (edgeAction == WRAP_EDGES)
							ix = (x + width) % width;
					} else if (ix >= width) {
						if (edgeAction == CLAMP_EDGES)
							ix = width - 1;
						else if (edgeAction == WRAP_EDGES)
							ix = (x + width) % width;
					}
					int rgb = inPixels[ioffset + ix];
					a += f * ((rgb >> 24) & 0xff);
					r += f * ((rgb >> 16) & 0xff);
					g += f * ((rgb >> 8) & 0xff);
					b += f * (rgb & 0xff);
				}
			}
			int ia = alpha ? PixelUtils.clamp((int) (a + 0.5)) : 0xff;
			int ir = PixelUtils.clamp((int) (r + 0.5));
			int ig = PixelUtils.clamp((int) (g + 0.5));
			int ib = PixelUtils.clamp((int) (b + 0.5));
			outPixels[index] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
			index += height;
		}
	}
}
 
Example 3
Source File: ConvolveFilter.java    From pumpernickel with MIT License 4 votes vote down vote up
/**
 * Convolve with a 2D kernel
 * 
 * @param kernel
 *            the kernel to apply
 * @param inPixels
 *            the input pixels
 * @param outPixels
 *            the output pixels
 * @param width
 *            the width of the image
 * @param height
 *            the height of the image
 * @param alpha
 *            whether alpha is present
 * @param edgeAction
 *            one of the edge constants
 */
public static void convolveHV(Kernel kernel, int[] inPixels,
		int[] outPixels, int width, int height, boolean alpha,
		int edgeAction) {
	int index = 0;
	float[] matrix = kernel.getKernelData(null);
	int rows = kernel.getHeight();
	int cols = kernel.getWidth();
	int rows2 = rows / 2;
	int cols2 = cols / 2;

	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			float r = 0, g = 0, b = 0, a = 0;

			for (int row = -rows2; row <= rows2; row++) {
				int iy = y + row;
				int ioffset;
				if (0 <= iy && iy < height)
					ioffset = iy * width;
				else if (edgeAction == CLAMP_EDGES)
					ioffset = y * width;
				else if (edgeAction == WRAP_EDGES)
					ioffset = ((iy + height) % height) * width;
				else
					continue;
				int moffset = cols * (row + rows2) + cols2;
				for (int col = -cols2; col <= cols2; col++) {
					float f = matrix[moffset + col];

					if (f != 0) {
						int ix = x + col;
						if (!(0 <= ix && ix < width)) {
							if (edgeAction == CLAMP_EDGES)
								ix = x;
							else if (edgeAction == WRAP_EDGES)
								ix = (x + width) % width;
							else
								continue;
						}
						int rgb = inPixels[ioffset + ix];
						a += f * ((rgb >> 24) & 0xff);
						r += f * ((rgb >> 16) & 0xff);
						g += f * ((rgb >> 8) & 0xff);
						b += f * (rgb & 0xff);
					}
				}
			}
			int ia = alpha ? PixelUtils.clamp((int) (a + 0.5)) : 0xff;
			int ir = PixelUtils.clamp((int) (r + 0.5));
			int ig = PixelUtils.clamp((int) (g + 0.5));
			int ib = PixelUtils.clamp((int) (b + 0.5));
			outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
		}
	}
}
 
Example 4
Source File: ConvolveFilter.java    From pumpernickel with MIT License 4 votes vote down vote up
/**
 * Convolve with a kernel consisting of one row
 * 
 * @param kernel
 *            the kernel to apply
 * @param inPixels
 *            the input pixels
 * @param outPixels
 *            the output pixels
 * @param width
 *            the width of the image
 * @param height
 *            the height of the image
 * @param alpha
 *            whether alpha is present
 * @param edgeAction
 *            one of the edge constants
 */
public static void convolveH(Kernel kernel, int[] inPixels,
		int[] outPixels, int width, int height, boolean alpha,
		int edgeAction) {
	int index = 0;
	float[] matrix = kernel.getKernelData(null);
	int cols = kernel.getWidth();
	int cols2 = cols / 2;

	for (int y = 0; y < height; y++) {
		int ioffset = y * width;
		for (int x = 0; x < width; x++) {
			float r = 0, g = 0, b = 0, a = 0;
			int moffset = cols2;
			for (int col = -cols2; col <= cols2; col++) {
				float f = matrix[moffset + col];

				if (f != 0) {
					int ix = x + col;
					if (ix < 0) {
						if (edgeAction == CLAMP_EDGES)
							ix = 0;
						else if (edgeAction == WRAP_EDGES)
							ix = (x + width) % width;
					} else if (ix >= width) {
						if (edgeAction == CLAMP_EDGES)
							ix = width - 1;
						else if (edgeAction == WRAP_EDGES)
							ix = (x + width) % width;
					}
					int rgb = inPixels[ioffset + ix];
					a += f * ((rgb >> 24) & 0xff);
					r += f * ((rgb >> 16) & 0xff);
					g += f * ((rgb >> 8) & 0xff);
					b += f * (rgb & 0xff);
				}
			}
			int ia = alpha ? PixelUtils.clamp((int) (a + 0.5)) : 0xff;
			int ir = PixelUtils.clamp((int) (r + 0.5));
			int ig = PixelUtils.clamp((int) (g + 0.5));
			int ib = PixelUtils.clamp((int) (b + 0.5));
			outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
		}
	}
}
 
Example 5
Source File: ConvolveFilter.java    From pumpernickel with MIT License 4 votes vote down vote up
/**
 * Convolve with a kernel consisting of one column
 * 
 * @param kernel
 *            the kernel to apply
 * @param inPixels
 *            the input pixels
 * @param outPixels
 *            the output pixels
 * @param width
 *            the width of the image
 * @param height
 *            the height of the image
 * @param alpha
 *            whether alpha is present
 * @param edgeAction
 *            one of the edge constants
 */
public static void convolveV(Kernel kernel, int[] inPixels,
		int[] outPixels, int width, int height, boolean alpha,
		int edgeAction) {
	int index = 0;
	float[] matrix = kernel.getKernelData(null);
	int rows = kernel.getHeight();
	int rows2 = rows / 2;

	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			float r = 0, g = 0, b = 0, a = 0;

			for (int row = -rows2; row <= rows2; row++) {
				int iy = y + row;
				int ioffset;
				if (iy < 0) {
					if (edgeAction == CLAMP_EDGES)
						ioffset = 0;
					else if (edgeAction == WRAP_EDGES)
						ioffset = ((y + height) % height) * width;
					else
						ioffset = iy * width;
				} else if (iy >= height) {
					if (edgeAction == CLAMP_EDGES)
						ioffset = (height - 1) * width;
					else if (edgeAction == WRAP_EDGES)
						ioffset = ((y + height) % height) * width;
					else
						ioffset = iy * width;
				} else
					ioffset = iy * width;

				float f = matrix[row + rows2];

				if (f != 0) {
					int rgb = inPixels[ioffset + x];
					a += f * ((rgb >> 24) & 0xff);
					r += f * ((rgb >> 16) & 0xff);
					g += f * ((rgb >> 8) & 0xff);
					b += f * (rgb & 0xff);
				}
			}
			int ia = alpha ? PixelUtils.clamp((int) (a + 0.5)) : 0xff;
			int ir = PixelUtils.clamp((int) (r + 0.5));
			int ig = PixelUtils.clamp((int) (g + 0.5));
			int ib = PixelUtils.clamp((int) (b + 0.5));
			outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
		}
	}
}
 
Example 6
Source File: ConvolveFilter.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convolve with a 2D kernel.
    * @param kernel the kernel
    * @param inPixels the input pixels
    * @param outPixels the output pixels
    * @param width the width
    * @param height the height
    * @param alpha include alpha channel
    * @param edgeAction what to do at the edges
 */
public static void convolveHV(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha, int edgeAction) {
	int index = 0;
	float[] matrix = kernel.getKernelData( null );
	int rows = kernel.getHeight();
	int cols = kernel.getWidth();
	int rows2 = rows/2;
	int cols2 = cols/2;

	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			float r = 0, g = 0, b = 0, a = 0;

			for (int row = -rows2; row <= rows2; row++) {
				int iy = y+row;
				int ioffset;
				if (0 <= iy && iy < height)
					ioffset = iy*width;
				else if ( edgeAction == CLAMP_EDGES )
					ioffset = y*width;
				else if ( edgeAction == WRAP_EDGES )
					ioffset = ((iy+height) % height) * width;
				else
					continue;
				int moffset = cols*(row+rows2)+cols2;
				for (int col = -cols2; col <= cols2; col++) {
					float f = matrix[moffset+col];

					if (f != 0) {
						int ix = x+col;
						if (!(0 <= ix && ix < width)) {
							if ( edgeAction == CLAMP_EDGES )
								ix = x;
							else if ( edgeAction == WRAP_EDGES )
								ix = (x+width) % width;
							else
								continue;
						}
						int rgb = inPixels[ioffset+ix];
						a += f * ((rgb >> 24) & 0xff);
						r += f * ((rgb >> 16) & 0xff);
						g += f * ((rgb >> 8) & 0xff);
						b += f * (rgb & 0xff);
					}
				}
			}
			int ia = alpha ? PixelUtils.clamp((int)(a+0.5)) : 0xff;
			int ir = PixelUtils.clamp((int)(r+0.5));
			int ig = PixelUtils.clamp((int)(g+0.5));
			int ib = PixelUtils.clamp((int)(b+0.5));
			outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
		}
	}
}
 
Example 7
Source File: ConvolveFilter.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convolve with a kernel consisting of one row.
    * @param kernel the kernel
    * @param inPixels the input pixels
    * @param outPixels the output pixels
    * @param width the width
    * @param height the height
    * @param alpha include alpha channel
    * @param edgeAction what to do at the edges
 */
public static void convolveH(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha, int edgeAction) {
	int index = 0;
	float[] matrix = kernel.getKernelData( null );
	int cols = kernel.getWidth();
	int cols2 = cols/2;

	for (int y = 0; y < height; y++) {
		int ioffset = y*width;
		for (int x = 0; x < width; x++) {
			float r = 0, g = 0, b = 0, a = 0;
			int moffset = cols2;
			for (int col = -cols2; col <= cols2; col++) {
				float f = matrix[moffset+col];

				if (f != 0) {
					int ix = x+col;
					if ( ix < 0 ) {
						if ( edgeAction == CLAMP_EDGES )
							ix = 0;
						else if ( edgeAction == WRAP_EDGES )
							ix = (x+width) % width;
					} else if ( ix >= width) {
						if ( edgeAction == CLAMP_EDGES )
							ix = width-1;
						else if ( edgeAction == WRAP_EDGES )
							ix = (x+width) % width;
					}
					int rgb = inPixels[ioffset+ix];
					a += f * ((rgb >> 24) & 0xff);
					r += f * ((rgb >> 16) & 0xff);
					g += f * ((rgb >> 8) & 0xff);
					b += f * (rgb & 0xff);
				}
			}
			int ia = alpha ? PixelUtils.clamp((int)(a+0.5)) : 0xff;
			int ir = PixelUtils.clamp((int)(r+0.5));
			int ig = PixelUtils.clamp((int)(g+0.5));
			int ib = PixelUtils.clamp((int)(b+0.5));
			outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
		}
	}
}
 
Example 8
Source File: ConvolveFilter.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convolve with a kernel consisting of one column.
    * @param kernel the kernel
    * @param inPixels the input pixels
    * @param outPixels the output pixels
    * @param width the width
    * @param height the height
    * @param alpha include alpha channel
    * @param edgeAction what to do at the edges
 */
public static void convolveV(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha, int edgeAction) {
	int index = 0;
	float[] matrix = kernel.getKernelData( null );
	int rows = kernel.getHeight();
	int rows2 = rows/2;

	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			float r = 0, g = 0, b = 0, a = 0;

			for (int row = -rows2; row <= rows2; row++) {
				int iy = y+row;
				int ioffset;
				if ( iy < 0 ) {
					if ( edgeAction == CLAMP_EDGES )
						ioffset = 0;
					else if ( edgeAction == WRAP_EDGES )
						ioffset = ((y+height) % height)*width;
					else
						ioffset = iy*width;
				} else if ( iy >= height) {
					if ( edgeAction == CLAMP_EDGES )
						ioffset = (height-1)*width;
					else if ( edgeAction == WRAP_EDGES )
						ioffset = ((y+height) % height)*width;
					else
						ioffset = iy*width;
				} else
					ioffset = iy*width;

				float f = matrix[row+rows2];

				if (f != 0) {
					int rgb = inPixels[ioffset+x];
					a += f * ((rgb >> 24) & 0xff);
					r += f * ((rgb >> 16) & 0xff);
					g += f * ((rgb >> 8) & 0xff);
					b += f * (rgb & 0xff);
				}
			}
			int ia = alpha ? PixelUtils.clamp((int)(a+0.5)) : 0xff;
			int ir = PixelUtils.clamp((int)(r+0.5));
			int ig = PixelUtils.clamp((int)(g+0.5));
			int ib = PixelUtils.clamp((int)(b+0.5));
			outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
		}
	}
}
 
Example 9
Source File: SmartBlurFilter.java    From Pixelitor with GNU General Public License v3.0 4 votes vote down vote up
/**
     * Convolve with a kernel consisting of one row
     */
    private void thresholdBlur(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha, ProgressTracker pt) {
//		int index = 0;
        float[] matrix = kernel.getKernelData(null);
        int cols = kernel.getWidth();
        int cols2 = cols / 2;

        for (int y = 0; y < height; y++) {
            int ioffset = y * width;
            int outIndex = y;
            for (int x = 0; x < width; x++) {
                float r = 0, g = 0, b = 0, a = 0;
                int moffset = cols2;

                int rgb1 = inPixels[ioffset + x];
                int a1 = (rgb1 >> 24) & 0xff;
                int r1 = (rgb1 >> 16) & 0xff;
                int g1 = (rgb1 >> 8) & 0xff;
                int b1 = rgb1 & 0xff;
                float af = 0, rf = 0, gf = 0, bf = 0;
                for (int col = -cols2; col <= cols2; col++) {
                    float f = matrix[moffset + col];

                    if (f != 0) {
                        int ix = x + col;
                        if (!(0 <= ix && ix < width)) {
                            ix = x;
                        }
                        int rgb2 = inPixels[ioffset + ix];
                        int a2 = (rgb2 >> 24) & 0xff;
                        int r2 = (rgb2 >> 16) & 0xff;
                        int g2 = (rgb2 >> 8) & 0xff;
                        int b2 = rgb2 & 0xff;

                        int d;
                        d = a1 - a2;
                        if (d >= -threshold && d <= threshold) {
                            a += f * a2;
                            af += f;
                        }
                        d = r1 - r2;
                        if (d >= -threshold && d <= threshold) {
                            r += f * r2;
                            rf += f;
                        }
                        d = g1 - g2;
                        if (d >= -threshold && d <= threshold) {
                            g += f * g2;
                            gf += f;
                        }
                        d = b1 - b2;
                        if (d >= -threshold && d <= threshold) {
                            b += f * b2;
                            bf += f;
                        }
                    }
                }
                a = af == 0 ? a1 : a / af;
                r = rf == 0 ? r1 : r / rf;
                g = gf == 0 ? g1 : g / gf;
                b = bf == 0 ? b1 : b / bf;
                int ia = alpha ? PixelUtils.clamp((int) (a + 0.5)) : 0xff;
                int ir = PixelUtils.clamp((int) (r + 0.5));
                int ig = PixelUtils.clamp((int) (g + 0.5));
                int ib = PixelUtils.clamp((int) (b + 0.5));
                outPixels[outIndex] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
                outIndex += height;
            }
            pt.unitDone();
        }
    }
 
Example 10
Source File: ConvolveFilter.java    From Pixelitor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convolve with a 2D kernel.
 *
 * @param kernel     the kernel
 * @param inPixels   the input pixels
 * @param outPixels  the output pixels
 * @param width      the width
 * @param height     the height
 * @param alpha      include alpha channel
 * @param edgeAction what to do at the edges
 */
public void convolveHV(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha, int edgeAction) {
    int index = 0;
    float[] matrix = kernel.getKernelData(null);
    int rows = kernel.getHeight();
    int cols = kernel.getWidth();
    int rows2 = rows / 2;
    int cols2 = cols / 2;

    pt = createProgressTracker(height);

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            float r = 0, g = 0, b = 0, a = 0;

            for (int row = -rows2; row <= rows2; row++) {
                int iy = y + row;
                int ioffset;
                if (0 <= iy && iy < height) {
                    ioffset = iy * width;
                } else if (edgeAction == CLAMP_EDGES) {
                    ioffset = y * width;
                } else if (edgeAction == WRAP_EDGES) {
                    ioffset = ((iy + height) % height) * width;
                } else {
                    continue;
                }
                int moffset = cols * (row + rows2) + cols2;
                for (int col = -cols2; col <= cols2; col++) {
                    float f = matrix[moffset + col];

                    if (f != 0) {
                        int ix = x + col;
                        if (!(0 <= ix && ix < width)) {
                            if (edgeAction == CLAMP_EDGES) {
                                ix = x;
                            } else if (edgeAction == WRAP_EDGES) {
                                ix = (x + width) % width;
                            } else {
                                continue;
                            }
                        }
                        int rgb = inPixels[ioffset + ix];
                        a += f * ((rgb >> 24) & 0xff);
                        r += f * ((rgb >> 16) & 0xff);
                        g += f * ((rgb >> 8) & 0xff);
                        b += f * (rgb & 0xff);
                    }
                }
            }
            int ia = alpha ? PixelUtils.clamp((int) (a + 0.5)) : 0xff;
            int ir = PixelUtils.clamp((int) (r + 0.5));
            int ig = PixelUtils.clamp((int) (g + 0.5));
            int ib = PixelUtils.clamp((int) (b + 0.5));
            outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
        }
        pt.unitDone();
    }
    finishProgressTracker();
}
 
Example 11
Source File: ConvolveFilter.java    From Pixelitor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convolve with a kernel consisting of one row.
 *
 * @param kernel     the kernel
 * @param inPixels   the input pixels
 * @param outPixels  the output pixels
 * @param width      the width
 * @param height     the height
 * @param alpha      include alpha channel
 * @param edgeAction what to do at the edges
 */
public static void convolveH(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha, int edgeAction) {
    int index = 0;
    float[] matrix = kernel.getKernelData(null);
    int cols = kernel.getWidth();
    int cols2 = cols / 2;

    for (int y = 0; y < height; y++) {
        int ioffset = y * width;
        for (int x = 0; x < width; x++) {
            float r = 0, g = 0, b = 0, a = 0;
            int moffset = cols2;
            for (int col = -cols2; col <= cols2; col++) {
                float f = matrix[moffset + col];

                if (f != 0) {
                    int ix = x + col;
                    if (ix < 0) {
                        if (edgeAction == CLAMP_EDGES) {
                            ix = 0;
                        } else if (edgeAction == WRAP_EDGES) {
                            ix = (x + width) % width;
                        }
                    } else if (ix >= width) {
                        if (edgeAction == CLAMP_EDGES) {
                            ix = width - 1;
                        } else if (edgeAction == WRAP_EDGES) {
                            ix = (x + width) % width;
                        }
                    }
                    int rgb = inPixels[ioffset + ix];
                    a += f * ((rgb >> 24) & 0xff);
                    r += f * ((rgb >> 16) & 0xff);
                    g += f * ((rgb >> 8) & 0xff);
                    b += f * (rgb & 0xff);
                }
            }
            int ia = alpha ? PixelUtils.clamp((int) (a + 0.5)) : 0xff;
            int ir = PixelUtils.clamp((int) (r + 0.5));
            int ig = PixelUtils.clamp((int) (g + 0.5));
            int ib = PixelUtils.clamp((int) (b + 0.5));
            outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
        }
    }
}
 
Example 12
Source File: ConvolveFilter.java    From Pixelitor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convolve with a kernel consisting of one column.
 *
 * @param kernel     the kernel
 * @param inPixels   the input pixels
 * @param outPixels  the output pixels
 * @param width      the width
 * @param height     the height
 * @param alpha      include alpha channel
 * @param edgeAction what to do at the edges
 */
public static void convolveV(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha, int edgeAction) {
    int index = 0;
    float[] matrix = kernel.getKernelData(null);
    int rows = kernel.getHeight();
    int rows2 = rows / 2;

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            float r = 0, g = 0, b = 0, a = 0;

            for (int row = -rows2; row <= rows2; row++) {
                int iy = y + row;
                int ioffset;
                if (iy < 0) {
                    if (edgeAction == CLAMP_EDGES) {
                        ioffset = 0;
                    } else if (edgeAction == WRAP_EDGES) {
                        ioffset = ((y + height) % height) * width;
                    } else {
                        ioffset = iy * width;
                    }
                } else if (iy >= height) {
                    if (edgeAction == CLAMP_EDGES) {
                        ioffset = (height - 1) * width;
                    } else if (edgeAction == WRAP_EDGES) {
                        ioffset = ((y + height) % height) * width;
                    } else {
                        ioffset = iy * width;
                    }
                } else {
                    ioffset = iy * width;
                }

                float f = matrix[row + rows2];

                if (f != 0) {
                    int rgb = inPixels[ioffset + x];
                    a += f * ((rgb >> 24) & 0xff);
                    r += f * ((rgb >> 16) & 0xff);
                    g += f * ((rgb >> 8) & 0xff);
                    b += f * (rgb & 0xff);
                }
            }
            int ia = alpha ? PixelUtils.clamp((int) (a + 0.5)) : 0xff;
            int ir = PixelUtils.clamp((int) (r + 0.5));
            int ig = PixelUtils.clamp((int) (g + 0.5));
            int ib = PixelUtils.clamp((int) (b + 0.5));
            outPixels[index++] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
        }
    }
}
 
Example 13
Source File: GaussianFilter.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
public static void convolveAndTranspose ( final Kernel kernel, final int[] inPixels, final int[] outPixels, final int width,
                                          final int height, final boolean alpha, final int edgeAction )
{
    final float[] matrix = kernel.getKernelData ( null );
    final int cols = kernel.getWidth ();
    final int cols2 = cols / 2;

    for ( int y = 0; y < height; y++ )
    {
        int index = y;
        final int iOffset = y * width;
        for ( int x = 0; x < width; x++ )
        {
            float r = 0, g = 0, b = 0, a = 0;
            for ( int col = -cols2; col <= cols2; col++ )
            {
                final float f = matrix[ cols2 + col ];

                if ( f != 0 )
                {
                    int ix = x + col;
                    if ( ix < 0 )
                    {
                        if ( edgeAction == CLAMP_EDGES )
                        {
                            ix = 0;
                        }
                        else if ( edgeAction == WRAP_EDGES )
                        {
                            ix = ( x + width ) % width;
                        }
                    }
                    else if ( ix >= width )
                    {
                        if ( edgeAction == CLAMP_EDGES )
                        {
                            ix = width - 1;
                        }
                        else if ( edgeAction == WRAP_EDGES )
                        {
                            ix = ( x + width ) % width;
                        }
                    }
                    final int rgb = inPixels[ iOffset + ix ];
                    a += f * ( ( rgb >> 24 ) & 0xff );
                    r += f * ( ( rgb >> 16 ) & 0xff );
                    g += f * ( ( rgb >> 8 ) & 0xff );
                    b += f * ( rgb & 0xff );
                }
            }
            final int ia = alpha ? PixelUtils.clamp ( ( int ) ( a + 0.5 ) ) : 0xff;
            final int ir = PixelUtils.clamp ( ( int ) ( r + 0.5 ) );
            final int ig = PixelUtils.clamp ( ( int ) ( g + 0.5 ) );
            final int ib = PixelUtils.clamp ( ( int ) ( b + 0.5 ) );
            outPixels[ index ] = ( ia << 24 ) | ( ir << 16 ) | ( ig << 8 ) | ib;
            index += height;
        }
    }
}
 
Example 14
Source File: ConvolveFilter.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convolve with a 2D kernel
 */
public static void convolveHV ( final Kernel kernel, final int[] inPixels, final int[] outPixels, final int width, final int height, final boolean alpha, final int edgeAction )
{
    int index = 0;
    final float[] matrix = kernel.getKernelData ( null );
    final int rows = kernel.getHeight ();
    final int cols = kernel.getWidth ();
    final int rows2 = rows / 2;
    final int cols2 = cols / 2;

    for ( int y = 0; y < height; y++ )
    {
        for ( int x = 0; x < width; x++ )
        {
            float r = 0, g = 0, b = 0, a = 0;

            for ( int row = -rows2; row <= rows2; row++ )
            {
                final int iy = y + row;
                final int iOffset;
                if ( 0 <= iy && iy < height )
                {
                    iOffset = iy * width;
                }
                else if ( edgeAction == CLAMP_EDGES )
                {
                    iOffset = y * width;
                }
                else if ( edgeAction == WRAP_EDGES )
                {
                    iOffset = ( ( iy + height ) % height ) * width;
                }
                else
                {
                    continue;
                }
                final int mOffset = cols * ( row + rows2 ) + cols2;
                for ( int col = -cols2; col <= cols2; col++ )
                {
                    final float f = matrix[ mOffset + col ];

                    if ( f != 0 )
                    {
                        int ix = x + col;
                        if ( !( 0 <= ix && ix < width ) )
                        {
                            if ( edgeAction == CLAMP_EDGES )
                            {
                                ix = x;
                            }
                            else if ( edgeAction == WRAP_EDGES )
                            {
                                ix = ( x + width ) % width;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        final int rgb = inPixels[ iOffset + ix ];
                        a += f * ( ( rgb >> 24 ) & 0xff );
                        r += f * ( ( rgb >> 16 ) & 0xff );
                        g += f * ( ( rgb >> 8 ) & 0xff );
                        b += f * ( rgb & 0xff );
                    }
                }
            }
            final int ia = alpha ? PixelUtils.clamp ( ( int ) ( a + 0.5 ) ) : 0xff;
            final int ir = PixelUtils.clamp ( ( int ) ( r + 0.5 ) );
            final int ig = PixelUtils.clamp ( ( int ) ( g + 0.5 ) );
            final int ib = PixelUtils.clamp ( ( int ) ( b + 0.5 ) );
            outPixels[ index++ ] = ( ia << 24 ) | ( ir << 16 ) | ( ig << 8 ) | ib;
        }
    }
}
 
Example 15
Source File: ConvolveFilter.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convolve with a kernel consisting of one row
 */
public static void convolveH ( final Kernel kernel, final int[] inPixels, final int[] outPixels, final int width, final int height, final boolean alpha, final int edgeAction )
{
    int index = 0;
    final float[] matrix = kernel.getKernelData ( null );
    final int cols = kernel.getWidth ();
    final int cols2 = cols / 2;

    for ( int y = 0; y < height; y++ )
    {
        final int iOffset = y * width;
        for ( int x = 0; x < width; x++ )
        {
            float r = 0, g = 0, b = 0, a = 0;
            for ( int col = -cols2; col <= cols2; col++ )
            {
                final float f = matrix[ cols2 + col ];

                if ( f != 0 )
                {
                    int ix = x + col;
                    if ( ix < 0 )
                    {
                        if ( edgeAction == CLAMP_EDGES )
                        {
                            ix = 0;
                        }
                        else if ( edgeAction == WRAP_EDGES )
                        {
                            ix = ( x + width ) % width;
                        }
                    }
                    else if ( ix >= width )
                    {
                        if ( edgeAction == CLAMP_EDGES )
                        {
                            ix = width - 1;
                        }
                        else if ( edgeAction == WRAP_EDGES )
                        {
                            ix = ( x + width ) % width;
                        }
                    }
                    final int rgb = inPixels[ iOffset + ix ];
                    a += f * ( ( rgb >> 24 ) & 0xff );
                    r += f * ( ( rgb >> 16 ) & 0xff );
                    g += f * ( ( rgb >> 8 ) & 0xff );
                    b += f * ( rgb & 0xff );
                }
            }
            final int ia = alpha ? PixelUtils.clamp ( ( int ) ( a + 0.5 ) ) : 0xff;
            final int ir = PixelUtils.clamp ( ( int ) ( r + 0.5 ) );
            final int ig = PixelUtils.clamp ( ( int ) ( g + 0.5 ) );
            final int ib = PixelUtils.clamp ( ( int ) ( b + 0.5 ) );
            outPixels[ index++ ] = ( ia << 24 ) | ( ir << 16 ) | ( ig << 8 ) | ib;
        }
    }
}
 
Example 16
Source File: ConvolveFilter.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convolve with a kernel consisting of one column
 */
public static void convolveV ( final Kernel kernel, final int[] inPixels, final int[] outPixels, final int width, final int height, final boolean alpha, final int edgeAction )
{
    int index = 0;
    final float[] matrix = kernel.getKernelData ( null );
    final int rows = kernel.getHeight ();
    final int rows2 = rows / 2;

    for ( int y = 0; y < height; y++ )
    {
        for ( int x = 0; x < width; x++ )
        {
            float r = 0, g = 0, b = 0, a = 0;

            for ( int row = -rows2; row <= rows2; row++ )
            {
                final int iy = y + row;
                final int iOffset;
                if ( iy < 0 )
                {
                    if ( edgeAction == CLAMP_EDGES )
                    {
                        iOffset = 0;
                    }
                    else if ( edgeAction == WRAP_EDGES )
                    {
                        iOffset = ( ( y + height ) % height ) * width;
                    }
                    else
                    {
                        iOffset = iy * width;
                    }
                }
                else if ( iy >= height )
                {
                    if ( edgeAction == CLAMP_EDGES )
                    {
                        iOffset = ( height - 1 ) * width;
                    }
                    else if ( edgeAction == WRAP_EDGES )
                    {
                        iOffset = ( ( y + height ) % height ) * width;
                    }
                    else
                    {
                        iOffset = iy * width;
                    }
                }
                else
                {
                    iOffset = iy * width;
                }

                final float f = matrix[ row + rows2 ];

                if ( f != 0 )
                {
                    final int rgb = inPixels[ iOffset + x ];
                    a += f * ( ( rgb >> 24 ) & 0xff );
                    r += f * ( ( rgb >> 16 ) & 0xff );
                    g += f * ( ( rgb >> 8 ) & 0xff );
                    b += f * ( rgb & 0xff );
                }
            }
            final int ia = alpha ? PixelUtils.clamp ( ( int ) ( a + 0.5 ) ) : 0xff;
            final int ir = PixelUtils.clamp ( ( int ) ( r + 0.5 ) );
            final int ig = PixelUtils.clamp ( ( int ) ( g + 0.5 ) );
            final int ib = PixelUtils.clamp ( ( int ) ( b + 0.5 ) );
            outPixels[ index++ ] = ( ia << 24 ) | ( ir << 16 ) | ( ig << 8 ) | ib;
        }
    }
}