List of usage examples for java.awt.image Raster getWidth
public final int getWidth()
From source file:it.tidalwave.imageio.test.ImageReaderTestSupport.java
/******************************************************************************************************************* * * ******************************************************************************************************************/ public static void dumpRasterAsText(final @Nonnull Raster raster, final @Nonnull PrintWriter pw) { final int width = raster.getWidth(); final int height = raster.getHeight(); final int bandCount = raster.getNumBands(); logger.fine("Dumping raster %d x %d x %d", width, height, bandCount); for (int y = 0; y < height; y++) { for (int b = 0; b < bandCount; b++) { pw.printf("y=%04d b=%1d : ", y, b); for (int x = 0; x < width; x++) { final int sample = raster.getSample(x, y, b) & 0xffff; pw.printf("%04x ", sample); }/* www. jav a 2 s . co m*/ pw.println(); } } }
From source file:it.tidalwave.imageio.test.ImageReaderTestSupport.java
/******************************************************************************************************************* * * ******************************************************************************************************************/ public static MessageDigest md5(final @Nonnull Raster raster) throws NoSuchAlgorithmException { final MessageDigest md5 = MessageDigest.getInstance("MD5"); for (int b = 0; b < raster.getNumBands(); b++) { for (int y = 0; y < raster.getHeight(); y++) { for (int x = 0; x < raster.getWidth(); x++) { final int sample = raster.getSample(x, y, b) & 0xffff; md5.update((byte) ((sample >>> 24) & 0xff)); md5.update((byte) ((sample >>> 16) & 0xff)); md5.update((byte) ((sample >>> 8) & 0xff)); md5.update((byte) ((sample >>> 0) & 0xff)); }//from w ww .j av a2 s. co m } } return md5; }
From source file:com.occamlab.te.parsers.ImageParser.java
private static String checkTransparentNodata(BufferedImage buffImage, Node node) throws Exception { String transparentNodata = "NA"; boolean noData = false; boolean transparent = true; int[] bandIndexes = new int[4]; bandIndexes[0] = 3; // A bandIndexes[1] = 2; // B bandIndexes[2] = 1; // G bandIndexes[3] = 0; // R Raster raster = buffImage.getRaster(); int minx = raster.getMinX(); int maxx = minx + raster.getWidth(); int miny = raster.getMinY(); int maxy = miny + raster.getHeight(); int bands[][] = new int[bandIndexes.length][raster.getWidth()]; for (int y = miny; y < maxy; y++) { for (int i = 0; i < bandIndexes.length; i++) { raster.getSamples(minx, y, maxx, 1, bandIndexes[i], bands[i]); }/*from w ww . ja va 2s . c o m*/ for (int x = minx; x < maxx; x++) { int a = bands[0][x]; int b = bands[1][x]; int g = bands[2][x]; int r = bands[3][x]; if (b == 0 && g == 0 && r == 0) { noData = true; if (a != 0) { transparent = false; } } } } transparentNodata = (noData) ? (transparent) ? "true" : "false" : "NA"; return transparentNodata; }
From source file:GraphicsUtil.java
/** * Creates a new raster that has a <b>copy</b> of the data in * <tt>ras</tt>. This is highly optimized for speed. There is * no provision for changing any aspect of the SampleModel. * However you can specify a new location for the returned raster. * * This method should be used when you need to change the contents * of a Raster that you do not "own" (ie the result of a * <tt>getData</tt> call)./* ww w . jav a 2 s . c om*/ * * @param ras The Raster to copy. * * @param minX The x location for the upper left corner of the * returned WritableRaster. * * @param minY The y location for the upper left corner of the * returned WritableRaster. * * @return A writable copy of <tt>ras</tt> */ public static WritableRaster copyRaster(Raster ras, int minX, int minY) { WritableRaster ret = Raster.createWritableRaster(ras.getSampleModel(), new Point(0, 0)); ret = ret.createWritableChild(ras.getMinX() - ras.getSampleModelTranslateX(), ras.getMinY() - ras.getSampleModelTranslateY(), ras.getWidth(), ras.getHeight(), minX, minY, null); // Use System.arraycopy to copy the data between the two... DataBuffer srcDB = ras.getDataBuffer(); DataBuffer retDB = ret.getDataBuffer(); if (srcDB.getDataType() != retDB.getDataType()) { throw new IllegalArgumentException("New DataBuffer doesn't match original"); } int len = srcDB.getSize(); int banks = srcDB.getNumBanks(); int[] offsets = srcDB.getOffsets(); for (int b = 0; b < banks; b++) { switch (srcDB.getDataType()) { case DataBuffer.TYPE_BYTE: { DataBufferByte srcDBT = (DataBufferByte) srcDB; DataBufferByte retDBT = (DataBufferByte) retDB; System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len); } case DataBuffer.TYPE_INT: { DataBufferInt srcDBT = (DataBufferInt) srcDB; DataBufferInt retDBT = (DataBufferInt) retDB; System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len); } case DataBuffer.TYPE_SHORT: { DataBufferShort srcDBT = (DataBufferShort) srcDB; DataBufferShort retDBT = (DataBufferShort) retDB; System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len); } case DataBuffer.TYPE_USHORT: { DataBufferUShort srcDBT = (DataBufferUShort) srcDB; DataBufferUShort retDBT = (DataBufferUShort) retDB; System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len); } } } return ret; }
From source file:GraphicsUtil.java
/** * Coerces <tt>ras</tt> to be writable. The returned Raster continues to * reference the DataBuffer from ras, so modifications to the returned * WritableRaster will be seen in ras.<p> * * You can specify a new location for the returned WritableRaster, this * is especially useful for constructing BufferedImages which require * the Raster to be at (0,0)./* w w w .j a va 2 s.c om*/ * * This method should only be used if you need a WritableRaster due to * an interface (such as to construct a BufferedImage), but have no * intention of modifying the contents of the returned Raster. If * you have any doubt about other users of the data in <tt>ras</tt>, * use copyRaster (above). * * @param ras The raster to make writable. * * @param minX The x location for the upper left corner of the * returned WritableRaster. * * @param minY The y location for the upper left corner of the * returned WritableRaster. * * @return A Writable version of <tT>ras</tt> with it's upper left * hand coordinate set to minX, minY (shares it's DataBuffer * with <tt>ras</tt>). */ public static WritableRaster makeRasterWritable(Raster ras, int minX, int minY) { WritableRaster ret = Raster.createWritableRaster(ras.getSampleModel(), ras.getDataBuffer(), new Point(0, 0)); ret = ret.createWritableChild(ras.getMinX() - ras.getSampleModelTranslateX(), ras.getMinY() - ras.getSampleModelTranslateY(), ras.getWidth(), ras.getHeight(), minX, minY, null); return ret; }
From source file:GraphicsUtil.java
/** * Creates a new raster that has a <b>copy</b> of the data in * <tt>ras</tt>. This is highly optimized for speed. There is * no provision for changing any aspect of the SampleModel. * However you can specify a new location for the returned raster. * * This method should be used when you need to change the contents * of a Raster that you do not "own" (ie the result of a * <tt>getData</tt> call).// w w w. j av a2 s.c om * * @param ras The Raster to copy. * * @param minX The x location for the upper left corner of the * returned WritableRaster. * * @param minY The y location for the upper left corner of the * returned WritableRaster. * * @return A writable copy of <tt>ras</tt> */ public static WritableRaster copyRaster(Raster ras, int minX, int minY) { WritableRaster ret = Raster.createWritableRaster(ras.getSampleModel(), new Point(0, 0)); ret = ret.createWritableChild(ras.getMinX() - ras.getSampleModelTranslateX(), ras.getMinY() - ras.getSampleModelTranslateY(), ras.getWidth(), ras.getHeight(), minX, minY, null); // Use System.arraycopy to copy the data between the two... DataBuffer srcDB = ras.getDataBuffer(); DataBuffer retDB = ret.getDataBuffer(); if (srcDB.getDataType() != retDB.getDataType()) { throw new IllegalArgumentException("New DataBuffer doesn't match original"); } int len = srcDB.getSize(); int banks = srcDB.getNumBanks(); int[] offsets = srcDB.getOffsets(); for (int b = 0; b < banks; b++) { switch (srcDB.getDataType()) { case DataBuffer.TYPE_BYTE: { DataBufferByte srcDBT = (DataBufferByte) srcDB; DataBufferByte retDBT = (DataBufferByte) retDB; System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len); break; } case DataBuffer.TYPE_INT: { DataBufferInt srcDBT = (DataBufferInt) srcDB; DataBufferInt retDBT = (DataBufferInt) retDB; System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len); break; } case DataBuffer.TYPE_SHORT: { DataBufferShort srcDBT = (DataBufferShort) srcDB; DataBufferShort retDBT = (DataBufferShort) retDB; System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len); break; } case DataBuffer.TYPE_USHORT: { DataBufferUShort srcDBT = (DataBufferUShort) srcDB; DataBufferUShort retDBT = (DataBufferUShort) retDB; System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len); break; } } } return ret; }
From source file:it.geosolutions.imageio.plugins.nitronitf.ImageIOUtils.java
/** * Utility method for creating a BufferedImage from a source raster Currently only Float->Byte and Byte->Byte are supported. Will throw an * {@link UnsupportedOperationException} if the conversion is not supported. * //from www . j a v a 2s . c o m * @param raster * @param imageType * @return */ public static BufferedImage rasterToBufferedImage(Raster raster, ImageTypeSpecifier imageType) { if (imageType == null) { if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) imageType = ImageTypeSpecifier.createGrayscale(8, DataBuffer.TYPE_BYTE, false); else throw new IllegalArgumentException("unable to dynamically determine the imageType"); } // create a new buffered image, for display BufferedImage bufImage = imageType.createBufferedImage(raster.getWidth(), raster.getHeight()); if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_USHORT && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) { // convert short pixels to bytes short[] shortData = ((DataBufferUShort) raster.getDataBuffer()).getData(); byte[] byteData = ((DataBufferByte) bufImage.getWritableTile(0, 0).getDataBuffer()).getData(); ImageIOUtils.shortToByteBuffer(shortData, byteData, 1, raster.getNumBands()); } else if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_FLOAT && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) { // convert float pixels to bytes float[] floatData = ((DataBufferFloat) raster.getDataBuffer()).getData(); byte[] byteData = ((DataBufferByte) bufImage.getWritableTile(0, 0).getDataBuffer()).getData(); ImageIOUtils.floatToByteBuffer(floatData, byteData, 1, raster.getNumBands()); } else if (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_DOUBLE && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) { // convert double pixels to bytes double[] doubleData = ((DataBufferDouble) raster.getDataBuffer()).getData(); byte[] byteData = ((DataBufferByte) bufImage.getWritableTile(0, 0).getDataBuffer()).getData(); ImageIOUtils.doubleToByteBuffer(doubleData, byteData, 1, raster.getNumBands()); } else if ((raster.getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_BYTE) || (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_USHORT && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_USHORT) || (raster.getDataBuffer().getDataType() == DataBuffer.TYPE_SHORT && bufImage.getRaster().getDataBuffer().getDataType() == DataBuffer.TYPE_SHORT)) { bufImage.setData(raster); } else { throw new UnsupportedOperationException( "Unable to convert raster type to bufferedImage type: " + raster.getDataBuffer().getDataType() + " ==> " + bufImage.getRaster().getDataBuffer().getDataType()); } return bufImage; }
From source file:CheckAlignmentOpImage.java
public Raster computeTile(int x, int y) { Raster r1 = source1.getTile(x, y); Raster r2 = source2.getTile(x, y); int xBounds = r1.getWidth(); if (r2.getWidth() < xBounds) xBounds = r2.getWidth();/* w w w . j av a 2s . com*/ int yBounds = r1.getHeight(); if (r2.getHeight() < yBounds) yBounds = r2.getHeight(); WritableRaster wr; wr = r1.createCompatibleWritableRaster(xBounds, yBounds); int tmpi; int tmpj; for (int i = 0; i < wr.getWidth(); i++) for (int j = 0; j < wr.getHeight(); j++) { tmpi = i / samplingPeriod; tmpj = j / samplingPeriod; if ((tmpi % 2 == 0) && (tmpj % 2 == 0)) wr.setDataElements(i, j, r2.getDataElements(i, j, null)); else if ((tmpi % 2 != 0) && (tmpj % 2 != 0)) wr.setDataElements(i, j, r2.getDataElements(i, j, null)); else wr.setDataElements(i, j, r1.getDataElements(i, j, null)); } return wr; }
From source file:fr.gael.drb.cortex.topic.sentinel3.jai.operator.QuicklookSlstrRIF.java
private BufferedImage toGrayScale(Raster in, PixelCorrection c, boolean invertColors, boolean ignoreBadStats) { int width = in.getWidth(); int height = in.getHeight(); // compute stats SummaryStatistics stats = new SummaryStatistics(); for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { int pixel = checkAndApplyCorrection(in.getSample(i, j, 0), c); if (pixel != c.nodata) stats.addValue(pixel);// w ww. j a v a2 s. co m } } double lowerBound = Math.max(stats.getMin(), stats.getMean() - 3 * stats.getStandardDeviation()); double upperBound = Math.min(stats.getMax(), stats.getMean() + 3 * stats.getStandardDeviation()); if (!ignoreBadStats) if (Double.isNaN(stats.getMean()) || Double.isNaN(stats.getStandardDeviation()) || stats.getStandardDeviation() < 1) throw new IllegalStateException("Ugly band stats. Acquired during night?"); return toGrayScale(in, c, invertColors, lowerBound, upperBound); }
From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.ImageUploaderThumbnailerTester_2.java
private Set<String> checkBlackEdges(RenderedOp image) { Raster imageData = image.getData(); int minX = imageData.getMinX(); int minY = imageData.getMinY(); int maxX = minX + imageData.getWidth() - 1; int maxY = minY + imageData.getHeight() - 1; Set<String> blackSides = new HashSet<String>(); if (isBlackEdge(minX, minX, minY, maxY, imageData)) { blackSides.add("left"); }// www . ja va 2s . c o m if (isBlackEdge(minX, maxX, minY, minY, imageData)) { blackSides.add("top"); } if (isBlackEdge(maxX, maxX, minY, maxY, imageData)) { blackSides.add("right"); } if (isBlackEdge(minX, maxX, maxY, maxY, imageData)) { blackSides.add("bottom"); } return blackSides; }