/** * 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."); } }
/** * 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; }