Java Code Examples for java.awt.image.WritableRaster#getNumBands()

The following examples show how to use java.awt.image.WritableRaster#getNumBands() . 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: ImageUtils.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
/**
 * scales bands to min = 0 & max = 2^8 or 2^16
 *
 * @param image
 * @param min   int[] containing minima per band
 * @param max
 * @return
 */
public static BufferedImage scaleIntensities(BufferedImage image, int[] min, int[] max) throws IllegalArgumentException {
    WritableRaster raster = image.getRaster();

    if ((min.length != max.length) || min.length != raster.getNumBands())
        throw new IllegalArgumentException("Please ensure consistency of min and max arrays and number of bands in image");

    int width = image.getWidth();
    int height = image.getHeight();

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster.getNumBands(); band++) {
                int pixel = raster.getSample(col, row, band);
                if (pixel < min[band]) pixel = min[band];
                if (pixel > max[band]) pixel = max[band];
                pixel = ((int) (((((double) pixel - min[band]) / (max[band] - min[band])) * ((int) Math.pow(2, image.getColorModel().getPixelSize()) - 1)) + 0.5));
                raster.setSample(col, row, band, pixel);
            }
        }
    }

    return image;
}
 
Example 2
Source File: AWTImageTools.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Extracts pixel data as arrays of doubles, one per channel. */
public static double[][] getDoubles(final WritableRaster r, final int x,
	final int y, final int w, final int h)
{
	if (canUseBankDataDirectly(r, DataBuffer.TYPE_DOUBLE,
		DataBufferDouble.class) && x == 0 && y == 0 && w == r.getWidth() && h == r
			.getHeight())
	{
		return ((DataBufferDouble) r.getDataBuffer()).getBankData();
	}
	// NB: an order of magnitude faster than the naive makeType solution
	final int c = r.getNumBands();
	final double[][] samples = new double[c][w * h];
	for (int i = 0; i < c; i++)
		r.getSamples(x, y, w, h, i, samples[i]);
	return samples;
}
 
Example 3
Source File: AWTImageTools.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Extracts pixel data as arrays of floats, one per channel. */
public static float[][] getFloats(final WritableRaster r, final int x,
	final int y, final int w, final int h)
{
	if (canUseBankDataDirectly(r, DataBuffer.TYPE_FLOAT,
		DataBufferFloat.class) && x == 0 && y == 0 && w == r.getWidth() && h == r
			.getHeight())
	{
		return ((DataBufferFloat) r.getDataBuffer()).getBankData();
	}
	// NB: an order of magnitude faster than the naive makeType solution
	final int c = r.getNumBands();
	final float[][] samples = new float[c][w * h];
	for (int i = 0; i < c; i++)
		r.getSamples(x, y, w, h, i, samples[i]);
	return samples;
}
 
Example 4
Source File: ImageUtils.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Calculates and returns band histograms of a BufferedImage
 *
 * @param image
 * @return
 */
public static int[][] getChannelHistograms(BufferedImage image) {
    WritableRaster raster = image.getRaster();
    int[][] histograms = new int[raster.getNumBands()][(int) (Math.pow(2, image.getColorModel().getPixelSize()) - 1)];

    int width = image.getWidth();
    int height = image.getHeight();

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster.getNumBands(); band++) {
                histograms[band][raster.getSample(col, row, band)]++;
            }
        }
    }

    return histograms;
}
 
Example 5
Source File: ImageUtils.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Calculates and returns band histograms of a BufferedImage w/o a need to extract the raster
 *
 * @param raster
 * @param width
 * @param height
 * @param iNumBins
 * @return
 */
public static int[][] getChannelHistograms(WritableRaster raster, int width, int height, int iNumBins) {

    int[][] histograms = new int[raster.getNumBands()][iNumBins];


    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster.getNumBands(); band++) {
                histograms[band][raster.getSample(col, row, band)]++;
            }
        }
    }

    return histograms;
}
 
Example 6
Source File: AWTImageTools.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Extracts pixel data as arrays of signed integers, one per channel. */
public static int[][] getInts(final WritableRaster r, final int x,
	final int y, final int w, final int h)
{
	if (canUseBankDataDirectly(r, DataBuffer.TYPE_INT, DataBufferInt.class) &&
		x == 0 && y == 0 && w == r.getWidth() && h == r.getHeight())
	{
		return ((DataBufferInt) r.getDataBuffer()).getBankData();
	}
	// NB: an order of magnitude faster than the naive makeType solution
	final int c = r.getNumBands();
	final int[][] samples = new int[c][w * h];
	for (int i = 0; i < c; i++)
		r.getSamples(x, y, w, h, i, samples[i]);
	return samples;
}
 
Example 7
Source File: RasterUtils.java    From geowave with Apache License 2.0 6 votes vote down vote up
public static void fillWithNoDataValues(
    final WritableRaster raster,
    final double[][] noDataValues) {
  if ((noDataValues != null) && (noDataValues.length >= raster.getNumBands())) {
    final double[] noDataFilledArray = new double[raster.getWidth() * raster.getHeight()];
    for (int b = 0; b < raster.getNumBands(); b++) {
      if ((noDataValues[b] != null) && (noDataValues[b].length > 0)) {
        // just fill every sample in this band with the first no
        // data value for that band
        Arrays.fill(noDataFilledArray, noDataValues[b][0]);
        raster.setSamples(
            raster.getMinX(),
            raster.getMinY(),
            raster.getWidth(),
            raster.getHeight(),
            b,
            noDataFilledArray);
      }
    }
  }
}
 
Example 8
Source File: SimpleAbstractMergeStrategy.java    From geowave with Apache License 2.0 6 votes vote down vote up
protected void mergeRasters(
    final RasterTile<T> thisTile,
    final RasterTile<T> nextTile,
    final WritableRaster thisRaster,
    final WritableRaster nextRaster) {
  final int maxX = nextRaster.getMinX() + nextRaster.getWidth();
  final int maxY = nextRaster.getMinY() + nextRaster.getHeight();
  for (int b = 0; b < nextRaster.getNumBands(); b++) {
    for (int x = nextRaster.getMinX(); x < maxX; x++) {
      for (int y = nextRaster.getMinY(); y < maxY; y++) {
        final double thisSample = thisRaster.getSampleDouble(x, y, b);

        final double nextSample = nextRaster.getSampleDouble(x, y, b);
        thisRaster.setSample(x, y, b, getSample(x, y, b, thisSample, nextSample));
      }
    }
  }
}
 
Example 9
Source File: AWTImageTools.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Extracts pixel data as arrays of unsigned shorts, one per channel. */
public static short[][] getShorts(final WritableRaster r, final int x,
	final int y, final int w, final int h)
{
	if (canUseBankDataDirectly(r, DataBuffer.TYPE_USHORT,
		DataBufferUShort.class) && x == 0 && y == 0 && w == r.getWidth() && h == r
			.getHeight())
	{
		return ((DataBufferUShort) r.getDataBuffer()).getBankData();
	}
	final int c = r.getNumBands();
	final short[][] samples = new short[c][w * h];
	final int[] buf = new int[w * h];
	for (int i = 0; i < c; i++) {
		r.getSamples(x, y, w, h, i, buf);
		for (int j = 0; j < buf.length; j++)
			samples[i][j] = (short) buf[j];
	}
	return samples;
}
 
Example 10
Source File: AWTImageTools.java    From scifio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Extracts pixel data as arrays of unsigned bytes, one per channel. */
public static byte[][] getBytes(final WritableRaster r, final int x,
	final int y, final int w, final int h)
{
	if (canUseBankDataDirectly(r, DataBuffer.TYPE_BYTE, DataBufferByte.class) &&
		x == 0 && y == 0 && w == r.getWidth() && h == r.getHeight())
	{
		return ((DataBufferByte) r.getDataBuffer()).getBankData();
	}
	final int c = r.getNumBands();
	final byte[][] samples = new byte[c][w * h];
	final int[] buf = new int[w * h];
	for (int i = 0; i < c; i++) {
		r.getSamples(x, y, w, h, i, buf);
		for (int j = 0; j < buf.length; j++)
			samples[i][j] = (byte) buf[j];
	}
	return samples;
}
 
Example 11
Source File: AWTImageTools.java    From scifio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Whether we can return the data buffer's bank data without performing any
 * copy or conversion operations.
 */
private static boolean canUseBankDataDirectly(final WritableRaster r,
	final int transferType, final Class<? extends DataBuffer> dataBufferClass)
{
	final int tt = r.getTransferType();
	if (tt != transferType) return false;
	final DataBuffer buffer = r.getDataBuffer();
	if (!dataBufferClass.isInstance(buffer)) return false;
	final SampleModel model = r.getSampleModel();
	if (!(model instanceof ComponentSampleModel)) return false;
	final ComponentSampleModel csm = (ComponentSampleModel) model;
	final int pixelStride = csm.getPixelStride();
	if (pixelStride != 1) return false;
	final int w = r.getWidth();
	final int scanlineStride = csm.getScanlineStride();
	if (scanlineStride != w) return false;
	final int c = r.getNumBands();
	final int[] bandOffsets = csm.getBandOffsets();
	if (bandOffsets.length != c) return false;
	for (int i = 0; i < bandOffsets.length; i++) {
		if (bandOffsets[i] != 0) return false;
	}
	for (int i = 0; i < bandOffsets.length; i++) {
		if (bandOffsets[i] != i) return false;
	}
	return true;
}
 
Example 12
Source File: GeoWaveBasicRasterIT.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public void merge(
    final RasterTile<MergeCounter> thisTile,
    final RasterTile<MergeCounter> nextTile,
    final SampleModel sampleModel) {
  if (nextTile instanceof ServerMergeableRasterTile) {
    final WritableRaster nextRaster =
        Raster.createWritableRaster(sampleModel, nextTile.getDataBuffer(), null);
    final WritableRaster thisRaster =
        Raster.createWritableRaster(sampleModel, thisTile.getDataBuffer(), null);
    final MergeCounter mergeCounter = thisTile.getMetadata();
    // we're merging, this is the incremented new number of merges
    final int newNumMerges =
        mergeCounter.getNumMerges() + nextTile.getMetadata().getNumMerges() + 1;

    // we've merged 1 more tile than the total number of merges (ie.
    // if we've performed 1 merge, we've seen 2 tiles)
    final int totalTiles = newNumMerges + 1;
    final int maxX = nextRaster.getMinX() + nextRaster.getWidth();
    final int maxY = nextRaster.getMinY() + nextRaster.getHeight();
    for (int x = nextRaster.getMinX(); x < maxX; x++) {
      for (int y = nextRaster.getMinY(); y < maxY; y++) {
        for (int b = 0; (b + 1) < nextRaster.getNumBands(); b += 2) {
          final double thisSample = thisRaster.getSampleDouble(x, y, b);
          final double nextSample = nextRaster.getSampleDouble(x, y, b);

          final double sum = thisSample + nextSample;
          final double average = sum / totalTiles;
          thisRaster.setSample(x, y, b, sum);
          thisRaster.setSample(x, y, b + 1, average);
        }
      }
    }
    thisTile.setMetadata(new MergeCounter(newNumMerges));
  }
}
 
Example 13
Source File: VectorPainter.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
private static void averageRaster(WritableRaster raster, Raster count)
{
  for (int y = 0; y < raster.getHeight(); y++)
  {
    for (int x = 0; x < raster.getWidth(); x++)
    {
      for (int b = 0; b < raster.getNumBands(); b++)
      {
        float v = raster.getSampleFloat(x, y, b);
        float c = count.getSampleFloat(x, y, b);

        if (!Float.isNaN(v))
        {
          if (FloatUtils.isEqual(c, 0.0))
          {
            v = Float.NaN;
          }
          else
          {
            v /= c;
          }

          raster.setSample(x, y, b, v);
        }
      }
    }
  }
}
 
Example 14
Source File: ImageUtils.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
public static int[] getPercentileIntensity(BufferedImage image, double percentile) {
    WritableRaster raster = image.getRaster();
    int[] intensities = new int[raster.getNumBands()];

    DescriptiveStatistics[] ds = new DescriptiveStatistics[raster.getNumBands()];
    for (int i = 0; i < raster.getNumBands(); i++)
        ds[i] = new DescriptiveStatistics();

    int width = image.getWidth();
    int height = image.getHeight();

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster.getNumBands(); band++) {
                ds[band].addValue(raster.getSample(col, row, band));
            }
        }
    }

    for (int i = 0; i < ds.length; i++) {
        if (percentile == 0d) intensities[i] = (int) ds[i].getMin();
        else if (percentile == 100d) intensities[i] = (int) ds[i].getMax();
        else
            intensities[i] = (int) ds[i].getPercentile(percentile);
    }
    return intensities;
}
 
Example 15
Source File: ImageUtils.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns min and max intensities and percentiles 0.01, 0.05, 0.1, 0.9, 0.95, 0.99 of a BufferedImage.
 *
 * @param image
 * @return
 */
public static int[][] getMinMaxIntensitiesOfBI(BufferedImage image) {
    WritableRaster raster = image.getRaster();
    // per band: { min, 1%, 5%, 10%, 90%, 95%, 99%, max }
    int[][] intensities = new int[raster.getNumBands()][8];

    DescriptiveStatistics[] ds = new DescriptiveStatistics[raster.getNumBands()];
    for (int i = 0; i < raster.getNumBands(); i++)
        ds[i] = new DescriptiveStatistics();

    int width = image.getWidth();
    int height = image.getHeight();

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster.getNumBands(); band++) {
                ds[band].addValue(raster.getSample(col, row, band));
            }
        }
    }

    for (int i = 0; i < ds.length; i++) {
        intensities[i][0] = (int) ds[i].getMin();
        intensities[i][1] = (int) ds[i].getPercentile(1);
        intensities[i][2] = (int) ds[i].getPercentile(5);
        intensities[i][3] = (int) ds[i].getPercentile(10);
        intensities[i][4] = (int) ds[i].getPercentile(90);
        intensities[i][5] = (int) ds[i].getPercentile(95);
        intensities[i][6] = (int) ds[i].getPercentile(99);
        intensities[i][7] = (int) ds[i].getMax();
    }
    return intensities;
}
 
Example 16
Source File: ImageUtils.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Can be used to verify impact of different encoding algorithms. Compares two BufferedImages and outputs differences between those.
 *
 * @param image
 * @param image2
 * @return
 */
public static int[] calculateBufferedImageDifferencesPxByPx(BufferedImage image, BufferedImage image2) {
    int width = image.getWidth();
    int height = image.getHeight();

    int width2 = image2.getWidth();
    int height2 = image2.getHeight();

    WritableRaster raster1 = image.getRaster();
    WritableRaster raster2 = image2.getRaster();

    if (width != width2 || height != height2)
        throw new IllegalArgumentException("Please insert two identical images that were treated with different image algorithms");

    int[] diff = new int[width * height];

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster1.getNumBands(); band++) {
                int p1 = raster1.getSample(col, row, band);
                int p2 = raster2.getSample(col, row, band);

                diff[((row * width) + col)] = p2 - p1;
            }
        }
    }

    return diff;
}
 
Example 17
Source File: NoDataMergeStrategy.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public void merge(
    final RasterTile<NoDataMetadata> thisTile,
    final RasterTile<NoDataMetadata> nextTile,
    final SampleModel sampleModel) {

  // this strategy aims for latest tile with data values, but where there
  // is no data in the latest and there is data in the earlier tile, it
  // fills the data from the earlier tile

  // if next tile is null or if this tile does not have metadata, just
  // keep this tile as is
  if ((nextTile != null) && (thisTile.getMetadata() != null)) {
    final NoDataMetadata thisTileMetadata = thisTile.getMetadata();
    final NoDataMetadata nextTileMetadata = nextTile.getMetadata();

    final WritableRaster thisRaster =
        Raster.createWritableRaster(sampleModel, thisTile.getDataBuffer(), null);
    final WritableRaster nextRaster =
        Raster.createWritableRaster(sampleModel, nextTile.getDataBuffer(), null);
    final int maxX = thisRaster.getMinX() + thisRaster.getWidth();
    final int maxY = thisRaster.getMinY() + thisRaster.getHeight();
    boolean recalculateMetadata = false;
    for (int b = 0; b < thisRaster.getNumBands(); b++) {
      for (int x = thisRaster.getMinX(); x < maxX; x++) {
        for (int y = thisRaster.getMinY(); y < maxY; y++) {
          if (thisTileMetadata.isNoData(
              new SampleIndex(x, y, b),
              thisRaster.getSampleDouble(x, y, b))) {
            final double sample = nextRaster.getSampleDouble(x, y, b);
            if ((nextTileMetadata == null)
                || !nextTileMetadata.isNoData(new SampleIndex(x, y, b), sample)) {
              // we only need to recalculate metadata if
              // the raster is overwritten,
              // otherwise just use this raster's
              // metadata
              recalculateMetadata = true;
              thisRaster.setSample(x, y, b, sample);
            }
          }
        }
      }
    }
    if (recalculateMetadata) {
      thisTile.setMetadata(
          NoDataMetadataFactory.mergeMetadata(
              thisTileMetadata,
              thisRaster,
              nextTileMetadata,
              nextRaster));
    }
  }
}