private static void dumpKernelData(String name, KernelJAI kernel) { float[] kernelData1 = kernel.getKernelData(); int m = kernel.getWidth(); float max = 0; for (int i = 0; i < kernelData1.length; i++) { max = max(max, kernelData1[i]); } System.out.println(name + ":"); for (int j = 0; j < m; j++) { for (int i = 0; i < m; i++) { System.out.print((int) (9.999f * kernelData1[j * m + i] / max)); } System.out.println(); } }
private static BufferedImage convolveICOL( RenderedOp source, KernelJAI gaussKernel, KernelJAI distKernel) { final int m = gaussKernel.getWidth(); final int n = m / 2 + 1; float[] weights = new float[n]; float wSum = 0; for (int k = 0; k < n; k++) { weights[k] = gaussKernel.getElement(distKernel.getXOrigin() + k, distKernel.getYOrigin()); wSum += weights[k]; } for (int k = 0; k < n; k++) { weights[k] /= wSum; } dumpArray("Weights", weights); int width = source.getWidth(); int height = source.getHeight(); Raster aRaster = source.getData(new Rectangle(0, 0, width, height)); BufferedImage cImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); byte[] aData = ((DataBufferByte) aRaster.getDataBuffer()).getData(); byte[] cData = ((DataBufferByte) cImage.getRaster().getDataBuffer()).getData(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { float[] sums = new float[n]; int[] counts = new int[n]; for (int j = 0; j < m; j++) { for (int i = 0; i < m; i++) { int k = (int) distKernel.getElement(i, j); if (k > 0) { int xx = x + i - distKernel.getXOrigin(); int yy = y + j - distKernel.getYOrigin(); if (xx >= 0 && xx < width && yy >= 0 && yy < height) { int a = (aData[yy * width + xx] & 0xff); if (a > 0) { // = if (not no-data) sums[k] += a; counts[k]++; } } } } } int a = aData[y * width + x] & 0xff; float c = weights[0] * a; for (int k = 1; k < n; k++) { if (counts[k] > 0) { c += (weights[k] * sums[k]) / counts[k]; } } if (a == 0) { cData[y * width + x] = (byte) c; } else { cData[y * width + x] = aData[y * width + x]; } } } return cImage; }