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

The following examples show how to use java.awt.image.Raster#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: PixelIterator.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an iterator for the given region in the given raster.
 *
 * @param  data     the raster which contains the sample values on which to iterate.
 * @param  subArea  the raster region where to perform the iteration, or {@code null}
 *                  for iterating over all the raster domain.
 * @param  window   size of the window to use in {@link #createWindow(TransferType)} method, or {@code null} if none.
 */
PixelIterator(final Raster data, final Rectangle subArea, final Dimension window) {
    final Rectangle bounds;
    image           = null;
    currentRaster   = data;
    numBands        = data.getNumBands();
    tileWidth       = data.getWidth();
    tileHeight      = data.getHeight();
    tileGridXOffset = data.getMinX();
    tileGridYOffset = data.getMinY();
    tileLowerX      = 0;                    // In this case only one raster: tile index is fixed to 0.
    tileLowerY      = 0;
    tileUpperX      = 1;
    tileUpperY      = 1;
    bounds          = intersection(tileGridXOffset, tileGridYOffset, tileWidth, tileHeight, subArea, window);
    lowerX          = bounds.x;
    lowerY          = bounds.y;
    upperX          = Math.addExact(lowerX, bounds.width);
    upperY          = Math.addExact(lowerY, bounds.height);
    windowWidth     = (window != null) ? window.width  : 0;
    windowHeight    = (window != null) ? window.height : 0;
}
 
Example 2
Source File: PartialImageEdit.java    From Pixelitor with GNU General Public License v3.0 6 votes vote down vote up
private static void debugRaster(String name, Raster raster) {
    if (raster == null) {
        System.err.printf("PartialImageEdit::debugRaster: NULL RASTER, name = '%s'%n", name);
        return;
    }
    Rectangle rasterBounds = raster.getBounds();
    String className = raster.getClass().getSimpleName();
    DataBuffer dataBuffer = raster.getDataBuffer();
    int dataType = dataBuffer.getDataType();
    String typeAsString = Debug.dateBufferTypeAsString(dataType);
    int numBanks = dataBuffer.getNumBanks();
    int numBands = raster.getNumBands();
    int numDataElements = raster.getNumDataElements();

    String msg = format("className = %s, rasterBounds = %s, dataType = %d, " +
            "typeAsString=%s, numBanks = %d, numBands = %d, numDataElements = %d",
        className, rasterBounds, dataType,
        typeAsString, numBanks, numBands, numDataElements);

    System.out.println("PartialImageEdit::debugRaster debugging raster: " + name + ": " + msg);
}
 
Example 3
Source File: AWTImageUtils.java    From dawnsci with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Get datasets from an image
 * @param image
 * @param keepBitWidth if true, then use signed primitives of same bit width for possibly unsigned data
 * @return array of datasets
 */
static public Dataset[] makeDatasets(final BufferedImage image, boolean keepBitWidth) {
	// make raster from buffered image
	final Raster ras = image.getData();
	final SampleModel sm = ras.getSampleModel();
	int[] dtype = getDTypeFromImage(sm, keepBitWidth);

	final int bands = ras.getNumBands();
	Dataset[] data = new Dataset[bands];

	createDatasets(ras, data, dtype[0]);
	if (dtype[1] == 1) {
		for (int i = 0; i < bands; i++) {
			tagIntForShortDataset(data[i]);
		}
	}
	return data;
}
 
Example 4
Source File: ImageUtils.java    From hifive-pitalium with Apache License 2.0 6 votes vote down vote up
/**
 * 元画像の積分画像を生成します。
 *
 * @param source 元画像
 * @return 積分結果の配列
 */
public static double[][] calcIntegralImage(BufferedImage source) {
	double[][] integralImage = new double[source.getHeight()][source.getWidth()];
	Raster raster = source.getRaster();
	int[] pixel = new int[raster.getNumBands()];
	double leftNum;
	double upNum;
	double leftUpNum;
	for (int y = 0; y < source.getHeight(); y++) {
		for (int x = 0; x < source.getWidth(); x++) {
			leftNum = (x == 0) ? 0 : integralImage[y][x - 1];
			upNum = (y == 0) ? 0 : integralImage[y - 1][x];
			leftUpNum = (x == 0 || y == 0) ? 0 : integralImage[y - 1][x - 1];
			try {
				integralImage[y][x] = leftNum + upNum + raster.getPixel(x, y, pixel)[0] - leftUpNum;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	return integralImage;
}
 
Example 5
Source File: Calibration.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
public double getSlope(BufferedImage bi1, BufferedImage bi2, int u, int v,  int s, int n) throws IOException {
   
    Raster r1 = bi1.getRaster().createTranslatedChild(0,0);
    Raster r2 = bi2.getRaster().createTranslatedChild(0,0);
    if (r1.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
    if (r2.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
    SimpleRegression reg = new SimpleRegression(true);
    int minX = u<0?u*-1:0;
    int minY = v<0?v*-1:0;
    int maxX = u>0?bi1.getWidth()-u: bi1.getWidth();
    int maxY = v>0?bi1.getHeight()-v: bi1.getHeight();
    for (int x=minX; x<maxX; x++) {
         for (int y=minY; y<maxY; y++) {
             double d1 = r1.getSampleDouble(x+u,y+v,0);
             if (d1> intensityThreshold) {
                 double d2 = r2.getSampleDouble(x, y, 0);
                 reg.addData(d2, d1);
             }
         }
     }

    double slope = reg.getSlope();
    double intercept = reg.getIntercept();
    logger.info("i,j: "+s+","+n+": "+ "slope: "+slope+" ; intercept: "+intercept);
    return slope;
}
 
Example 6
Source File: Calibration.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
public List<double[]> getPairs(BufferedImage bi1, BufferedImage bi2, int u, int v,  int s, int n) throws IOException {
    List<double[]> pairList = new ArrayList<>(bi1.getWidth()*bi1.getHeight());
    Raster r1 = bi1.getRaster().createTranslatedChild(0,0);
    Raster r2 = bi2.getRaster().createTranslatedChild(0,0);
    if (r1.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
    if (r2.getNumBands()>1) throw new IllegalArgumentException("only 1-banded rasters allowed here");
    SimpleRegression reg = new SimpleRegression(true);
    int minX = u<0?u*-1:0;
    int minY = v<0?v*-1:0;
    int maxX = u>0?bi1.getWidth()-u: bi1.getWidth();
    int maxY = v>0?bi1.getHeight()-v: bi1.getHeight();
    for (int x=minX; x<maxX; x++) {
        for (int y=minY; y<maxY; y++) {
            double d1 = r1.getSampleDouble(x+u,y+v,0);
            if (d1> intensityThreshold) {
                double d2 = r2.getSampleDouble(x, y, 0);
                double[] pair = new double[]{d2,d1};
                pairList.add(pair);
            }
        }
    }

    return pairList;
}
 
Example 7
Source File: OrbitMaskThreshold.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int classNum(int x, int y, final Raster raster) throws Exception {
    int intens = 0;
    for (int b=0; b<raster.getNumBands(); b++) {
        intens += raster.getSample(x,y,b);
    }
    intens /= raster.getNumBands();
    if (intens>=min && intens<=max) return 1;
    else return 0;
}
 
Example 8
Source File: MrGeoRaster.java    From mrgeo with Apache License 2.0 5 votes vote down vote up
public static MrGeoRaster fromRaster(Raster raster) throws IOException
{
  MrGeoRaster mrgeo =
      createEmptyRaster(raster.getWidth(), raster.getHeight(), raster.getNumBands(), raster.getTransferType());

  for (int b = 0; b < raster.getNumBands(); b++)
  {
    for (int y = 0; y < raster.getHeight(); y++)
    {
      for (int x = 0; x < raster.getWidth(); x++)
      {
        switch (mrgeo.datatype())
        {
        case DataBuffer.TYPE_BYTE:
          mrgeo.setPixel(x, y, b, (byte) raster.getSample(x, y, b));
          break;
        case DataBuffer.TYPE_INT:
          mrgeo.setPixel(x, y, b, raster.getSample(x, y, b));
          break;
        case DataBuffer.TYPE_SHORT:
        case DataBuffer.TYPE_USHORT:
          mrgeo.setPixel(x, y, b, (short) raster.getSample(x, y, b));
          break;
        case DataBuffer.TYPE_FLOAT:
          mrgeo.setPixel(x, y, b, raster.getSampleFloat(x, y, b));
          break;
        case DataBuffer.TYPE_DOUBLE:
          mrgeo.setPixel(x, y, b, raster.getSampleDouble(x, y, b));
          break;
        default:
          throw new RasterWritableException("Error trying to read raster.  Bad raster data type");
        }
      }
    }
  }

  return mrgeo;
}
 
Example 9
Source File: RegistrationTrans.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
private float[][] raster2float(Raster r) {
    r = r.createTranslatedChild(0,0);
    float[][] f = new float[r.getWidth()][r.getHeight()];
    for (int x=0; x<r.getWidth(); x++) {
        for (int y=0; y<r.getHeight(); y++) {
            f[x][y] = 0;
            for (int b=0; b<r.getNumBands(); b++) {
                f[x][y] += r.getSample(x,y,b);
            }
        }
    }
    return f;
}
 
Example 10
Source File: mxPngImageEncoder.java    From blog-codes with Apache License 2.0 4 votes vote down vote up
private void writeIDAT() throws IOException
{
	IDATOutputStream ios = new IDATOutputStream(dataOutput, 8192);
	DeflaterOutputStream dos = new DeflaterOutputStream(ios,
			new Deflater(9));

	// Future work - don't convert entire image to a Raster It
	// might seem that you could just call image.getData() but
	// 'BufferedImage.subImage' doesn't appear to set the Width
	// and height properly of the Child Raster, so the Raster
	// you get back here appears larger than it should.
	// This solves that problem by bounding the raster to the
	// image's bounds...
	Raster ras = image.getData(new Rectangle(image.getMinX(), image
			.getMinY(), image.getWidth(), image.getHeight()));
	// log.fine("Image: [" +
	//                    image.getMinY()  + ", " +
	//                    image.getMinX()  + ", " +
	//                    image.getWidth()  + ", " +
	//                    image.getHeight() + "]");
	// log.fine("Ras: [" +
	//                    ras.getMinX()  + ", " +
	//                    ras.getMinY()  + ", " +
	//                    ras.getWidth()  + ", " +
	//                    ras.getHeight() + "]");

	if (skipAlpha)
	{
		int numBands = ras.getNumBands() - 1;
		int[] bandList = new int[numBands];
		for (int i = 0; i < numBands; i++)
		{
			bandList[i] = i;
		}
		ras = ras.createChild(0, 0, ras.getWidth(), ras.getHeight(), 0, 0,
				bandList);
	}

	if (interlace)
	{
		// Interlacing pass 1
		encodePass(dos, ras, 0, 0, 8, 8);
		// Interlacing pass 2
		encodePass(dos, ras, 4, 0, 8, 8);
		// Interlacing pass 3
		encodePass(dos, ras, 0, 4, 4, 8);
		// Interlacing pass 4
		encodePass(dos, ras, 2, 0, 4, 4);
		// Interlacing pass 5
		encodePass(dos, ras, 0, 2, 2, 4);
		// Interlacing pass 6
		encodePass(dos, ras, 1, 0, 2, 2);
		// Interlacing pass 7
		encodePass(dos, ras, 0, 1, 1, 2);
	}
	else
	{
		encodePass(dos, ras, 0, 0, 1, 1);
	}

	dos.finish();
	ios.flush();
}
 
Example 11
Source File: ImageUtils.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static boolean isCMYKType(Raster raster) {
	return raster.getNumBands() == 4; // number of parameters for CMYK color scheme per pixel
}
 
Example 12
Source File: JPXFilter.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public DecodeResult decode(InputStream encoded, OutputStream decoded, COSDictionary
        parameters, int index, DecodeOptions options) throws IOException
{
    DecodeResult result = new DecodeResult(new COSDictionary());
    result.getParameters().addAll(parameters);
    BufferedImage image = readJPX(encoded, options, result);

    Raster raster = image.getRaster();
    switch (raster.getDataBuffer().getDataType())
    {
        case DataBuffer.TYPE_BYTE:
            DataBufferByte byteBuffer = (DataBufferByte) raster.getDataBuffer();
            decoded.write(byteBuffer.getData());
            return result;

        case DataBuffer.TYPE_USHORT:
            DataBufferUShort wordBuffer = (DataBufferUShort) raster.getDataBuffer();
            for (short w : wordBuffer.getData())
            {
                decoded.write(w >> 8);
                decoded.write(w);
            }
            return result;

        case DataBuffer.TYPE_INT:
            // not yet used (as of October 2018) but works as fallback
            // if we decide to convert to BufferedImage.TYPE_INT_RGB
            int[] ar = new int[raster.getNumBands()];
            for (int y = 0; y < image.getHeight(); ++y)
            {
                for (int x = 0; x < image.getWidth(); ++x)
                {
                    raster.getPixel(x, y, ar);
                    for (int i = 0; i < ar.length; ++i)
                    {
                        decoded.write(ar[i]);
                    }
                }
            }
            return result;

        default:
            throw new IOException("Data type " + raster.getDataBuffer().getDataType() + " not implemented");
    }
}
 
Example 13
Source File: Images.java    From basic-tools with MIT License 4 votes vote down vote up
/**
 * 判断黑白照片
 *
 * @param src  文件流
 * @param lv   级别
 * @param clip 是否裁剪
 * @return true/false
 */
public static boolean isBlackWhiteImage(byte[] src, int lv, boolean clip) {
    try {

        BufferedImage srcImage = ImageIO.read(new ByteArrayInputStream(src));
        Raster raster = srcImage.getRaster();

        int no = raster.getNumBands();
        if (no < 3) {
            return true;
        }
        //Set level value.
        lv = 10 * lv;
        int x = 0;
        int y = 0;
        int width = raster.getWidth();
        int height = raster.getHeight();

        if (clip) {
            int clipSize = 300;
            if (width > clipSize && height > clipSize) {
                x = (width - clipSize) / 2;
                y = (height - clipSize) / 2;
                width = clipSize;
                height = clipSize;
            }
        }

        int wh = width * height;
        int threshold = wh / 8;

        int[] pixels = srcImage.getRGB(x, y, width, height, null, 0, width);

        int resRg = 0;
        int resGb = 0;
        int resBr = 0;

        for (int i = 0; i < pixels.length; i++) {
            int r = (pixels[i] & 0xff0000) >> 16;
            int g = (pixels[i] & 0xff00) >> 8;
            int b = (pixels[i] & 0xff);
            if (Math.abs(r - g) > lv) {
                resRg++;
            }
            if (Math.abs(g - b) > lv) {
                resGb++;
            }
            if (Math.abs(b - r) > lv) {
                resBr++;
            }
        }

        int res = resRg + resGb + resBr;
        if (res < threshold) {
            return true;
        }

    } catch (Exception e) {
        log.error("", e);
    }
    return false;
}
 
Example 14
Source File: Index16ColorModel.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public boolean isCompatibleRaster(final Raster raster) {
	return raster.getNumBands() == 1;
}
 
Example 15
Source File: UnsignedIntColorModel.java    From scifio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public boolean isCompatibleRaster(final Raster raster) {
	return raster.getNumBands() == getNumComponents() && raster
		.getTransferType() == getTransferType();
}
 
Example 16
Source File: JPEGCodec.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
  * This is a factory method for creating JPEGEncodeParam objects.
  * It is the users responsiblity to match the colorID with the
  * data contained in the Raster.  Failure to do so may lead to
  * either poor compression or poor image quality.  If you don't
  * understand much about JPEG it is strongly recommended that you
  * stick to the BufferedImage interface.
  * @param ras Raster that is similar to those to be encoded later.
  * @param colorID the COLOR_ID for the encoded data.  This should
  *        match the data in the raster.
  */
public static JPEGEncodeParam getDefaultJPEGEncodeParam(Raster ras,
                                                        int colorID)
{
JPEGParam ret = new JPEGParam(colorID, ras.getNumBands());
ret.setWidth(ras.getWidth());
ret.setHeight(ras.getHeight());

return ret;
}
 
Example 17
Source File: JPEGCodec.java    From oim-fx with MIT License 3 votes vote down vote up
/**
  * This is a factory method for creating JPEGEncodeParam objects.
  * It is the users responsiblity to match the colorID with the
  * data contained in the Raster.  Failure to do so may lead to
  * either poor compression or poor image quality.  If you don't
  * understand much about JPEG it is strongly recommended that you
  * stick to the BufferedImage interface.
  * @param ras Raster that is similar to those to be encoded later.
  * @param colorID the COLOR_ID for the encoded data.  This should
  *        match the data in the raster.
  */
public static JPEGEncodeParam getDefaultJPEGEncodeParam(Raster ras,
                                                       	int colorID)
{
 	JPEGParam ret = new JPEGParam(colorID, ras.getNumBands());
 	ret.setWidth(ras.getWidth());
 	ret.setHeight(ras.getHeight());

 	return ret;
}
 
Example 18
Source File: JPEGCodec.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
  * This is a factory method for creating JPEGEncodeParam objects.
  * It is the users responsiblity to match the colorID with the
  * data contained in the Raster.  Failure to do so may lead to
  * either poor compression or poor image quality.  If you don't
  * understand much about JPEG it is strongly recommended that you
  * stick to the BufferedImage interface.
  * @param ras Raster that is similar to those to be encoded later.
  * @param colorID the COLOR_ID for the encoded data.  This should
  *        match the data in the raster.
  */
public static JPEGEncodeParam getDefaultJPEGEncodeParam(Raster ras,
                                                        int colorID)
{
JPEGParam ret = new JPEGParam(colorID, ras.getNumBands());
ret.setWidth(ras.getWidth());
ret.setHeight(ras.getHeight());

return ret;
}