/** * Creates a zeroed destination <CODE>Raster</CODE> with the correct size and number of bands. An * <CODE>IllegalArgumentException</CODE> may be thrown if the number of bands in the source is * incompatible with the matrix. See the class comments for more details. * * @param src The <CODE>Raster</CODE> to be filtered. * @return The zeroed destination <CODE>Raster</CODE>. */ public WritableRaster createCompatibleDestRaster(Raster src) { int nBands = src.getNumBands(); if ((ncols != nBands) && (ncols != (nBands + 1))) { throw new IllegalArgumentException( "Number of columns in the " + "matrix (" + ncols + ") must be equal to the number" + " of bands ([+1]) in src (" + nBands + ")."); } if (src.getNumBands() == nrows) { return src.createCompatibleWritableRaster(); } else { throw new IllegalArgumentException( "Don't know how to create a " + " compatible Raster with " + nrows + " bands."); } }
public final double resample(final Raster raster, final Index index) throws Exception { final int[] x = new int[kernelSize]; final int[] y = new int[kernelSize]; final double[][] samples = new double[kernelSize][kernelSize]; final double[] winX = new double[kernelSize]; final double cx = index.ki[0] + halfKernelSize; double sumX = 0.0; for (int n = 0; n < kernelSize; n++) { x[n] = (int) index.i[n]; y[n] = (int) index.j[n]; winX[n] = sincHanning(cx - n); sumX += winX[n]; } if (!raster.getSamples(x, y, samples)) { if (Double.isNaN(samples[halfKernelSize][halfKernelSize])) { return samples[halfKernelSize][halfKernelSize]; } BiCubicInterpolationResampling.replaceNaNWithMean(samples); } final double cy = index.kj[0] + halfKernelSize; double sumY = 0, winY; double v = 0.0; for (int j = 0; j < kernelSize; j++) { winY = sincHanning(cy - j); sumY += winY; for (int i = 0; i < kernelSize; i++) { v += samples[j][i] * winX[i] * winY; } } v /= sumX * sumY; return v; }
/** * Returns the bounding box of the transformed destination. Since this is not a geometric * operation, the bounding box is the same for the source and destination. An <CODE> * IllegalArgumentException</CODE> may be thrown if the number of bands in the source is * incompatible with the matrix. See the class comments for more details. * * @param src The <CODE>Raster</CODE> to be filtered. * @return The <CODE>Rectangle2D</CODE> representing the destination image's bounding box. * @throws IllegalArgumentException If the number of bands in the source is incompatible with the * matrix. */ public final Rectangle2D getBounds2D(Raster src) { return src.getBounds(); }
/** * Transforms the <CODE>Raster</CODE> using the matrix specified in the constructor. An <CODE> * IllegalArgumentException</CODE> may be thrown if the number of bands in the source or * destination is incompatible with the matrix. See the class comments for more details. * * <p>If the destination is null, it will be created with a number of bands equalling the number * of rows in the matrix. No exception is thrown if the operation causes a data overflow. * * @param src The <CODE>Raster</CODE> to be filtered. * @param dst The <CODE>Raster</CODE> in which to store the results of the filter operation. * @return The filtered <CODE>Raster</CODE>. * @throws IllegalArgumentException If the number of bands in the source or destination is * incompatible with the matrix. */ public WritableRaster filter(Raster src, WritableRaster dst) { int nBands = src.getNumBands(); if (ncols != nBands && ncols != (nBands + 1)) { throw new IllegalArgumentException( "Number of columns in the " + "matrix (" + ncols + ") must be equal to the number" + " of bands ([+1]) in src (" + nBands + ")."); } if (dst == null) { dst = createCompatibleDestRaster(src); } else if (nrows != dst.getNumBands()) { throw new IllegalArgumentException( "Number of rows in the " + "matrix (" + nrows + ") must be equal to the number" + " of bands ([+1]) in dst (" + nBands + ")."); } if (ImagingLib.filter(this, src, dst) != null) { return dst; } int[] pixel = null; int[] dstPixel = new int[dst.getNumBands()]; float accum; int sminX = src.getMinX(); int sY = src.getMinY(); int dminX = dst.getMinX(); int dY = dst.getMinY(); int sX; int dX; if (ncols == nBands) { for (int y = 0; y < src.getHeight(); y++, sY++, dY++) { dX = dminX; sX = sminX; for (int x = 0; x < src.getWidth(); x++, sX++, dX++) { pixel = src.getPixel(sX, sY, pixel); for (int r = 0; r < nrows; r++) { accum = 0.f; for (int c = 0; c < ncols; c++) { accum += matrix[r][c] * pixel[c]; } dstPixel[r] = (int) accum; } dst.setPixel(dX, dY, dstPixel); } } } else { // Need to add constant for (int y = 0; y < src.getHeight(); y++, sY++, dY++) { dX = dminX; sX = sminX; for (int x = 0; x < src.getWidth(); x++, sX++, dX++) { pixel = src.getPixel(sX, sY, pixel); for (int r = 0; r < nrows; r++) { accum = 0.f; for (int c = 0; c < nBands; c++) { accum += matrix[r][c] * pixel[c]; } dstPixel[r] = (int) (accum + matrix[r][nBands]); } dst.setPixel(dX, dY, dstPixel); } } } return dst; }