List of usage examples for java.awt.image DataBuffer TYPE_INT
int TYPE_INT
To view the source code for java.awt.image DataBuffer TYPE_INT.
Click Source Link
From source file:org.apache.xmlgraphics.image.codec.png.PNGImageDecoder.java
/** * A convenience method to create an instance of * <code>ComponentColorModel</code> suitable for use with the given * <code>SampleModel</code>. The <code>SampleModel</code> should have a data * type of <code>DataBuffer.TYPE_BYTE</code>, <code>TYPE_USHORT</code>, or * <code>TYPE_INT</code> and between 1 and 4 bands. Depending on the number * of bands of the <code>SampleModel</code>, either a gray, gray+alpha, rgb, * or rgb+alpha <code>ColorModel</code> is returned. *//*from www . ja v a 2 s .c om*/ public static ColorModel createComponentColorModel(final SampleModel sm) { final int type = sm.getDataType(); final int bands = sm.getNumBands(); ComponentColorModel cm = null; if (type == DataBuffer.TYPE_BYTE) { switch (bands) { case 1: cm = colorModelGray8; break; case 2: cm = colorModelGrayAlpha8; break; case 3: cm = colorModelRGB8; break; case 4: cm = colorModelRGBA8; break; } } else if (type == DataBuffer.TYPE_USHORT) { switch (bands) { case 1: cm = colorModelGray16; break; case 2: cm = colorModelGrayAlpha16; break; case 3: cm = colorModelRGB16; break; case 4: cm = colorModelRGBA16; break; } } else if (type == DataBuffer.TYPE_INT) { switch (bands) { case 1: cm = colorModelGray32; break; case 2: cm = colorModelGrayAlpha32; break; case 3: cm = colorModelRGB32; break; case 4: cm = colorModelRGBA32; break; } } return cm; }
From source file:org.apache.xmlgraphics.ps.PSImageUtils.java
/** * Extracts a packed RGB integer array of a RenderedImage. * @param img the image/*from w w w . j a va2 s. co m*/ * @param startX the starting X coordinate * @param startY the starting Y coordinate * @param w the width of the cropped image * @param h the height of the cropped image * @param rgbArray the prepared integer array to write to * @param offset offset in the target array * @param scansize width of a row in the target array * @return the populated integer array previously passed in as rgbArray parameter */ public static int[] getRGB(RenderedImage img, int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) { Raster raster = img.getData(); int yoff = offset; int off; Object data; int nbands = raster.getNumBands(); int dataType = raster.getDataBuffer().getDataType(); switch (dataType) { case DataBuffer.TYPE_BYTE: data = new byte[nbands]; break; case DataBuffer.TYPE_USHORT: data = new short[nbands]; break; case DataBuffer.TYPE_INT: data = new int[nbands]; break; case DataBuffer.TYPE_FLOAT: data = new float[nbands]; break; case DataBuffer.TYPE_DOUBLE: data = new double[nbands]; break; default: throw new IllegalArgumentException("Unknown data buffer type: " + dataType); } if (rgbArray == null) { rgbArray = new int[offset + h * scansize]; } ColorModel colorModel = img.getColorModel(); for (int y = startY; y < startY + h; y++, yoff += scansize) { off = yoff; for (int x = startX; x < startX + w; x++) { rgbArray[off++] = colorModel.getRGB(raster.getDataElements(x, y, data)); } } return rgbArray; }
From source file:org.geoserver.jai.ConcurrentTileFactory.java
/** * Builds a new tile, eventually recycling the data array backing it *//*w ww. j a v a2s. com*/ public WritableRaster createTile(SampleModel sampleModel, Point location) { // sanity checks if (sampleModel == null) { throw new NullPointerException("sampleModel cannot be null"); } if (location == null) { location = new Point(0, 0); } DataBuffer db = null; // get the three elements making the key into the recycled array map int type = sampleModel.getTransferType(); long numBanks = 0; long size = 0; if (sampleModel instanceof ComponentSampleModel) { ComponentSampleModel csm = (ComponentSampleModel) sampleModel; numBanks = getNumBanksCSM(csm); size = getBufferSizeCSM(csm); } else if (sampleModel instanceof MultiPixelPackedSampleModel) { MultiPixelPackedSampleModel mppsm = (MultiPixelPackedSampleModel) sampleModel; numBanks = 1; int dataTypeSize = DataBuffer.getDataTypeSize(type); size = mppsm.getScanlineStride() * mppsm.getHeight() + (mppsm.getDataBitOffset() + dataTypeSize - 1) / dataTypeSize; } else if (sampleModel instanceof SinglePixelPackedSampleModel) { SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sampleModel; numBanks = 1; size = sppsm.getScanlineStride() * (sppsm.getHeight() - 1) + sppsm.getWidth(); } if (size > 0) { // try to build a new data buffer starting from Object array = recycledArrays.getRecycledArray(type, numBanks, size); if (array != null) { switch (type) { case DataBuffer.TYPE_BYTE: { byte[][] bankData = (byte[][]) array; for (int i = 0; i < numBanks; i++) { Arrays.fill(bankData[i], (byte) 0); } db = new DataBufferByte(bankData, (int) size); } break; case DataBuffer.TYPE_USHORT: { short[][] bankData = (short[][]) array; for (int i = 0; i < numBanks; i++) { Arrays.fill(bankData[i], (short) 0); } db = new DataBufferUShort(bankData, (int) size); } break; case DataBuffer.TYPE_SHORT: { short[][] bankData = (short[][]) array; for (int i = 0; i < numBanks; i++) { Arrays.fill(bankData[i], (short) 0); } db = new DataBufferShort(bankData, (int) size); } break; case DataBuffer.TYPE_INT: { int[][] bankData = (int[][]) array; for (int i = 0; i < numBanks; i++) { Arrays.fill(bankData[i], 0); } db = new DataBufferInt(bankData, (int) size); } break; case DataBuffer.TYPE_FLOAT: { float[][] bankData = (float[][]) array; for (int i = 0; i < numBanks; i++) { Arrays.fill(bankData[i], 0.0F); } db = DataBufferUtils.createDataBufferFloat(bankData, (int) size); } break; case DataBuffer.TYPE_DOUBLE: { double[][] bankData = (double[][]) array; for (int i = 0; i < numBanks; i++) { Arrays.fill(bankData[i], 0.0); } db = DataBufferUtils.createDataBufferDouble(bankData, (int) size); } break; default: throw new IllegalArgumentException("Unknown array type"); } } } if (db == null) { db = sampleModel.createDataBuffer(); } return Raster.createWritableRaster(sampleModel, db, location); }
From source file:org.geotools.imageio.netcdf.NetCDFImageReader.java
/** * @see javax.imageio.ImageReader#read(int, javax.imageio.ImageReadParam) */// w w w. j a v a2 s . c o m @Override public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException { clearAbortRequest(); final Slice2DIndex slice2DIndex = getSlice2DIndex(imageIndex); final String variableName = slice2DIndex.getVariableName(); final VariableAdapter wrapper = getCoverageDescriptor(new NameImpl(variableName)); /* * Fetches the parameters that are not already processed by utility * methods like 'getDestination' or 'computeRegions' (invoked below). */ final int strideX, strideY; // final int[] srcBands; final int[] dstBands; if (param != null) { strideX = param.getSourceXSubsampling(); strideY = param.getSourceYSubsampling(); // srcBands = param.getSourceBands(); dstBands = param.getDestinationBands(); } else { strideX = 1; strideY = 1; // srcBands = null; dstBands = null; } /* * Gets the destination image of appropriate size. We create it now * since it is a convenient way to get the number of destination bands. */ final int width = wrapper.getWidth(); final int height = wrapper.getHeight(); /* * Computes the source region (in the NetCDF file) and the destination * region (in the buffered image). Copies those informations into UCAR * Range structure. */ final Rectangle srcRegion = new Rectangle(); final Rectangle destRegion = new Rectangle(); computeRegions(param, width, height, null, srcRegion, destRegion); // Flipping is needed only when the input latitude coordinate is ordered // from min to max if (needsFlipping) { flipVertically(param, height, srcRegion); } int destWidth = destRegion.x + destRegion.width; int destHeight = destRegion.y + destRegion.height; /* * build the ranges that need to be read from each * dimension based on the source region */ final List<Range> ranges = new LinkedList<Range>(); try { // add the ranges the COARDS way: T, Z, Y, X // T int first = slice2DIndex.getTIndex(); int length = 1; int stride = 1; if (first != -1) { ranges.add(new Range(first, first + length - 1, stride)); } // Z first = slice2DIndex.getZIndex(); if (first != -1) { ranges.add(new Range(first, first + length - 1, stride)); } // Y first = srcRegion.y; length = srcRegion.height; stride = strideY; ranges.add(new Range(first, first + length - 1, stride)); // X first = srcRegion.x; length = srcRegion.width; stride = strideX; ranges.add(new Range(first, first + length - 1, stride)); } catch (InvalidRangeException e) { throw netcdfFailure(e); } /* * create the section of multidimensional array indices * that defines the exact data that need to be read * for this image index and parameters */ final Section section = new Section(ranges); /* * Setting SampleModel and ColorModel. */ final SampleModel sampleModel = wrapper.getSampleModel().createCompatibleSampleModel(destWidth, destHeight); final ColorModel colorModel = ImageIOUtilities.createColorModel(sampleModel); final WritableRaster raster = Raster.createWritableRaster(sampleModel, new Point(0, 0)); final BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null); CoordinateAxis axis = wrapper.variableDS.getCoordinateSystems().get(0).getLatAxis(); boolean flipYAxis = false; try { Array yAxisStart = axis.read(new Section().appendRange(2)); float y1 = yAxisStart.getFloat(0); float y2 = yAxisStart.getFloat(1); if (y2 > y1) { flipYAxis = true; } } catch (InvalidRangeException e) { throw new RuntimeException(e); } /* * Reads the requested sub-region only. */ processImageStarted(imageIndex); final int numDstBands = 1; final float toPercent = 100f / numDstBands; final int type = raster.getSampleModel().getDataType(); final int xmin = destRegion.x; final int ymin = destRegion.y; final int xmax = destRegion.width + xmin; final int ymax = destRegion.height + ymin; for (int zi = 0; zi < numDstBands; zi++) { // final int srcBand = (srcBands == null) ? zi : srcBands[zi]; final int dstBand = (dstBands == null) ? zi : dstBands[zi]; final Array array; try { // TODO leak through array = wrapper.variableDS.read(section); } catch (InvalidRangeException e) { throw netcdfFailure(e); } if (flipYAxis) { final IndexIterator it = array.getIndexIterator(); for (int y = ymax; --y >= ymin;) { for (int x = xmin; x < xmax; x++) { switch (type) { case DataBuffer.TYPE_DOUBLE: { raster.setSample(x, y, dstBand, it.getDoubleNext()); break; } case DataBuffer.TYPE_FLOAT: { raster.setSample(x, y, dstBand, it.getFloatNext()); break; } case DataBuffer.TYPE_BYTE: { byte b = it.getByteNext(); // int myByte = (0x000000FF & ((int) b)); // short anUnsignedByte = (short) myByte; // raster.setSample(x, y, dstBand, anUnsignedByte); raster.setSample(x, y, dstBand, b); break; } default: { raster.setSample(x, y, dstBand, it.getIntNext()); break; } } } } } else { switch (type) { case DataBuffer.TYPE_DOUBLE: { DoubleBuffer doubleBuffer = array.getDataAsByteBuffer().asDoubleBuffer(); double[] samples = new double[destRegion.width * destRegion.height]; doubleBuffer.get(samples); raster.setSamples(xmin, ymin, destRegion.width, destRegion.height, dstBand, samples); break; } case DataBuffer.TYPE_FLOAT: float[] samples = new float[destRegion.width * destRegion.height]; FloatBuffer floatBuffer = array.getDataAsByteBuffer().asFloatBuffer(); floatBuffer.get(samples); raster.setSamples(xmin, ymin, destRegion.width, destRegion.height, dstBand, samples); break; case DataBuffer.TYPE_BYTE: //THIS ONLY WORKS FOR ONE BAND!! raster.setDataElements(xmin, ymin, destRegion.width, destRegion.height, array.getDataAsByteBuffer().array()); break; case DataBuffer.TYPE_INT: IntBuffer intBuffer = array.getDataAsByteBuffer().asIntBuffer(); int[] intSamples = new int[destRegion.width * destRegion.height]; intBuffer.get(intSamples); raster.setSamples(xmin, ymin, destRegion.width, destRegion.height, dstBand, intSamples); break; default: { final IndexIterator it = array.getIndexIterator(); for (int y = ymin; y < ymax; y++) { for (int x = xmin; x < xmax; x++) { raster.setSample(x, y, dstBand, it.getIntNext()); } } break; } } } /* * Checks for abort requests after reading. It would be a waste of a * potentially good image (maybe the abort request occurred after we * just finished the reading) if we didn't implemented the * 'isCancel()' method. But because of the later, which is checked * by the NetCDF library, we can't assume that the image is * complete. */ if (abortRequested()) { processReadAborted(); return image; } /* * Reports progress here, not in the deeper loop, because the costly * part is the call to 'variable.read(...)' which can't report * progress. The loop that copy pixel values is fast, so reporting * progress there would be pointless. */ processImageProgress(zi * toPercent); } processImageComplete(); return image; }
From source file:org.geotools.imageio.unidata.UnidataImageReader.java
/** * @see javax.imageio.ImageReader#read(int, javax.imageio.ImageReadParam) *///www. j a va 2s . c o m @Override public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException { clearAbortRequest(); final UnidataSlice2DIndex slice2DIndex = getSlice2DIndex(imageIndex); final String variableName = slice2DIndex.getVariableName(); final UnidataVariableAdapter wrapper = getCoverageDescriptor(new NameImpl(variableName)); /* * Fetches the parameters that are not already processed by utility * methods like 'getDestination' or 'computeRegions' (invoked below). */ final int strideX, strideY; // final int[] srcBands; final int[] dstBands; if (param != null) { strideX = param.getSourceXSubsampling(); strideY = param.getSourceYSubsampling(); // srcBands = param.getSourceBands(); dstBands = param.getDestinationBands(); } else { strideX = 1; strideY = 1; // srcBands = null; dstBands = null; } /* * Gets the destination image of appropriate size. We create it now * since it is a convenient way to get the number of destination bands. */ final int width = wrapper.getWidth(); final int height = wrapper.getHeight(); /* * Computes the source region (in the NetCDF file) and the destination * region (in the buffered image). Copies those informations into UCAR * Range structure. */ final Rectangle srcRegion = new Rectangle(); final Rectangle destRegion = new Rectangle(); computeRegions(param, width, height, null, srcRegion, destRegion); flipVertically(param, height, srcRegion); int destWidth = destRegion.x + destRegion.width; int destHeight = destRegion.y + destRegion.height; /* * build the ranges that need to be read from each * dimension based on the source region */ final List<Range> ranges = new LinkedList<Range>(); try { // add the ranges the COARDS way: T, Z, Y, X // T int first = slice2DIndex.getTIndex(); int length = 1; int stride = 1; if (first != -1) { ranges.add(new Range(first, first + length - 1, stride)); } // Z first = slice2DIndex.getZIndex(); if (first != -1) { ranges.add(new Range(first, first + length - 1, stride)); } // Y first = srcRegion.y; length = srcRegion.height; stride = strideY; ranges.add(new Range(first, first + length - 1, stride)); // X first = srcRegion.x; length = srcRegion.width; stride = strideX; ranges.add(new Range(first, first + length - 1, stride)); } catch (InvalidRangeException e) { throw netcdfFailure(e); } /* * create the section of multidimensional array indices * that defines the exact data that need to be read * for this image index and parameters */ final Section section = new Section(ranges); /* * Setting SampleModel and ColorModel. */ final SampleModel sampleModel = wrapper.getSampleModel().createCompatibleSampleModel(destWidth, destHeight); final ColorModel colorModel = ImageIOUtilities.createColorModel(sampleModel); final WritableRaster raster = Raster.createWritableRaster(sampleModel, new Point(0, 0)); final BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null); CoordinateAxis axis = wrapper.variableDS.getCoordinateSystems().get(0).getLatAxis(); boolean flipYAxis = false; try { Array yAxisStart = axis.read(new Section().appendRange(2)); float y1 = yAxisStart.getFloat(0); float y2 = yAxisStart.getFloat(1); if (y2 > y1) { flipYAxis = true; } } catch (InvalidRangeException e) { throw new RuntimeException(e); } /* * Reads the requested sub-region only. */ processImageStarted(imageIndex); final int numDstBands = 1; final float toPercent = 100f / numDstBands; final int type = raster.getSampleModel().getDataType(); final int xmin = destRegion.x; final int ymin = destRegion.y; final int xmax = destRegion.width + xmin; final int ymax = destRegion.height + ymin; for (int zi = 0; zi < numDstBands; zi++) { // final int srcBand = (srcBands == null) ? zi : srcBands[zi]; final int dstBand = (dstBands == null) ? zi : dstBands[zi]; final Array array; try { // TODO leak through array = wrapper.variableDS.read(section); } catch (InvalidRangeException e) { throw netcdfFailure(e); } if (flipYAxis) { final IndexIterator it = array.getIndexIterator(); for (int y = ymax; --y >= ymin;) { for (int x = xmin; x < xmax; x++) { switch (type) { case DataBuffer.TYPE_DOUBLE: { raster.setSample(x, y, dstBand, it.getDoubleNext()); break; } case DataBuffer.TYPE_FLOAT: { raster.setSample(x, y, dstBand, it.getFloatNext()); break; } case DataBuffer.TYPE_BYTE: { byte b = it.getByteNext(); // int myByte = (0x000000FF & ((int) b)); // short anUnsignedByte = (short) myByte; // raster.setSample(x, y, dstBand, anUnsignedByte); raster.setSample(x, y, dstBand, b); break; } default: { raster.setSample(x, y, dstBand, it.getIntNext()); break; } } } } } else { switch (type) { case DataBuffer.TYPE_DOUBLE: { DoubleBuffer doubleBuffer = array.getDataAsByteBuffer().asDoubleBuffer(); double[] samples = new double[destRegion.width * destRegion.height]; doubleBuffer.get(samples); raster.setSamples(xmin, ymin, destRegion.width, destRegion.height, dstBand, samples); break; } case DataBuffer.TYPE_FLOAT: float[] samples = new float[destRegion.width * destRegion.height]; FloatBuffer floatBuffer = array.getDataAsByteBuffer().asFloatBuffer(); floatBuffer.get(samples); raster.setSamples(xmin, ymin, destRegion.width, destRegion.height, dstBand, samples); break; case DataBuffer.TYPE_BYTE: //THIS ONLY WORKS FOR ONE BAND!! raster.setDataElements(xmin, ymin, destRegion.width, destRegion.height, array.getDataAsByteBuffer().array()); break; case DataBuffer.TYPE_INT: IntBuffer intBuffer = array.getDataAsByteBuffer().asIntBuffer(); int[] intSamples = new int[destRegion.width * destRegion.height]; intBuffer.get(intSamples); raster.setSamples(xmin, ymin, destRegion.width, destRegion.height, dstBand, intSamples); break; default: { final IndexIterator it = array.getIndexIterator(); for (int y = ymin; y < ymax; y++) { for (int x = xmin; x < xmax; x++) { raster.setSample(x, y, dstBand, it.getIntNext()); } } break; } } } /* * Checks for abort requests after reading. It would be a waste of a * potentially good image (maybe the abort request occurred after we * just finished the reading) if we didn't implemented the * 'isCancel()' method. But because of the later, which is checked * by the NetCDF library, we can't assume that the image is * complete. */ if (abortRequested()) { processReadAborted(); return image; } /* * Reports progress here, not in the deeper loop, because the costly * part is the call to 'variable.read(...)' which can't report * progress. The loop that copy pixel values is fast, so reporting * progress there would be pointless. */ processImageProgress(zi * toPercent); } processImageComplete(); return image; }
From source file:org.mrgeo.cmd.mrsimageinfo.MrsImageInfo.java
private static void printNodata(final MrsPyramidMetadata metadata) { System.out.print("NoData: "); for (int band = 0; band < metadata.getBands(); band++) { if (band > 0) { System.out.print(", "); }//from w ww . j a v a2 s.c om switch (metadata.getTileType()) { case DataBuffer.TYPE_BYTE: System.out.print(metadata.getDefaultValueByte(band)); break; case DataBuffer.TYPE_FLOAT: System.out.print(metadata.getDefaultValueFloat(band)); break; case DataBuffer.TYPE_DOUBLE: System.out.print(metadata.getDefaultValueDouble(band)); break; case DataBuffer.TYPE_INT: System.out.print(metadata.getDefaultValueInt(band)); break; case DataBuffer.TYPE_SHORT: case DataBuffer.TYPE_USHORT: System.out.print(metadata.getDefaultValueShort(band)); break; default: break; } } System.out.println(""); }
From source file:org.mrgeo.cmd.mrsimageinfo.MrsImageInfo.java
private static void printTileType(final MrsPyramidMetadata metadata) { System.out.print("Type: "); switch (metadata.getTileType()) { case DataBuffer.TYPE_BYTE: System.out.println("byte"); break;//from w ww . j av a 2 s. c om case DataBuffer.TYPE_FLOAT: System.out.println("float"); break; case DataBuffer.TYPE_DOUBLE: System.out.println("double"); break; case DataBuffer.TYPE_INT: System.out.println("int"); break; case DataBuffer.TYPE_SHORT: System.out.println("short"); break; case DataBuffer.TYPE_USHORT: System.out.println("unsigned short"); break; default: break; } }
From source file:org.mrgeo.data.raster.RasterWritable.java
private static byte[] rasterToBytes(final Raster raster) { final int datatype = raster.getTransferType(); byte[] pixels; final Object elements = raster.getDataElements(raster.getMinX(), raster.getMinY(), raster.getWidth(), raster.getHeight(), null);// w ww . j a v a 2 s . co m switch (datatype) { case DataBuffer.TYPE_BYTE: { pixels = (byte[]) elements; break; } case DataBuffer.TYPE_FLOAT: { final float[] floatElements = (float[]) elements; pixels = new byte[floatElements.length * RasterUtils.FLOAT_BYTES]; final ByteBuffer bytebuff = ByteBuffer.wrap(pixels); final FloatBuffer floatbuff = bytebuff.asFloatBuffer(); floatbuff.put(floatElements); break; } case DataBuffer.TYPE_DOUBLE: { final double[] doubleElements = (double[]) elements; pixels = new byte[doubleElements.length * RasterUtils.DOUBLE_BYTES]; final ByteBuffer bytebuff = ByteBuffer.wrap(pixels); final DoubleBuffer doubleBuff = bytebuff.asDoubleBuffer(); doubleBuff.put(doubleElements); break; } case DataBuffer.TYPE_INT: { final int[] intElements = (int[]) elements; pixels = new byte[intElements.length * RasterUtils.INT_BYTES]; final ByteBuffer bytebuff = ByteBuffer.wrap(pixels); final IntBuffer intBuff = bytebuff.asIntBuffer(); intBuff.put(intElements); break; } case DataBuffer.TYPE_SHORT: case DataBuffer.TYPE_USHORT: { final short[] shortElements = (short[]) elements; pixels = new byte[shortElements.length * RasterUtils.SHORT_BYTES]; final ByteBuffer bytebuff = ByteBuffer.wrap(pixels); final ShortBuffer shortbuff = bytebuff.asShortBuffer(); shortbuff.put(shortElements); break; } default: throw new RasterWritableException("Error trying to append raster. Bad raster data type"); } return pixels; }
From source file:org.mrgeo.data.raster.RasterWritable.java
private static Raster read(final byte[] rasterBytes, Writable payload) throws IOException { WritableRaster raster;// w w w.j a v a 2 s. c o m final ByteBuffer rasterBuffer = ByteBuffer.wrap(rasterBytes); @SuppressWarnings("unused") final int headersize = rasterBuffer.getInt(); // this isn't really used anymore... final int height = rasterBuffer.getInt(); final int width = rasterBuffer.getInt(); final int bands = rasterBuffer.getInt(); final int datatype = rasterBuffer.getInt(); final SampleModelType sampleModelType = SampleModelType.values()[rasterBuffer.getInt()]; SampleModel model; switch (sampleModelType) { case BANDED: model = new BandedSampleModel(datatype, width, height, bands); break; case MULTIPIXELPACKED: throw new NotImplementedException("MultiPixelPackedSampleModel not implemented yet"); // model = new MultiPixelPackedSampleModel(dataType, w, h, numberOfBits) case PIXELINTERLEAVED: { final int pixelStride = rasterBuffer.getInt(); final int scanlineStride = rasterBuffer.getInt(); final int bandcnt = rasterBuffer.getInt(); final int[] bandOffsets = new int[bandcnt]; for (int i = 0; i < bandcnt; i++) { bandOffsets[i] = rasterBuffer.getInt(); } model = new PixelInterleavedSampleModel(datatype, width, height, pixelStride, scanlineStride, bandOffsets); break; } case SINGLEPIXELPACKED: throw new NotImplementedException("SinglePixelPackedSampleModel not implemented yet"); // model = new SinglePixelPackedSampleModel(dataType, w, h, bitMasks); case COMPONENT: { final int pixelStride = rasterBuffer.getInt(); final int scanlineStride = rasterBuffer.getInt(); final int bandcnt = rasterBuffer.getInt(); final int[] bandOffsets = new int[bandcnt]; for (int i = 0; i < bandcnt; i++) { bandOffsets[i] = rasterBuffer.getInt(); } model = new ComponentSampleModel(datatype, width, height, pixelStride, scanlineStride, bandOffsets); break; } default: throw new RasterWritableException("Unknown RasterSampleModel type"); } // include the header size param in the count int startdata = rasterBuffer.position(); // calculate the data size int[] samplesize = model.getSampleSize(); int samplebytes = 0; for (int ss : samplesize) { // bits to bytes samplebytes += (ss / 8); } int databytes = model.getHeight() * model.getWidth() * samplebytes; // final ByteBuffer rasterBuffer = ByteBuffer.wrap(rasterBytes, headerbytes, databytes); // the corner of the raster is always 0,0 raster = Raster.createWritableRaster(model, null); switch (datatype) { case DataBuffer.TYPE_BYTE: { // we can't use the byte buffer explicitly because the header info is // still in it... final byte[] bytedata = new byte[databytes]; rasterBuffer.get(bytedata); raster.setDataElements(0, 0, width, height, bytedata); break; } case DataBuffer.TYPE_FLOAT: { final FloatBuffer floatbuff = rasterBuffer.asFloatBuffer(); final float[] floatdata = new float[databytes / RasterUtils.FLOAT_BYTES]; floatbuff.get(floatdata); raster.setDataElements(0, 0, width, height, floatdata); break; } case DataBuffer.TYPE_DOUBLE: { final DoubleBuffer doublebuff = rasterBuffer.asDoubleBuffer(); final double[] doubledata = new double[databytes / RasterUtils.DOUBLE_BYTES]; doublebuff.get(doubledata); raster.setDataElements(0, 0, width, height, doubledata); break; } case DataBuffer.TYPE_INT: { final IntBuffer intbuff = rasterBuffer.asIntBuffer(); final int[] intdata = new int[databytes / RasterUtils.INT_BYTES]; intbuff.get(intdata); raster.setDataElements(0, 0, width, height, intdata); break; } case DataBuffer.TYPE_SHORT: case DataBuffer.TYPE_USHORT: { final ShortBuffer shortbuff = rasterBuffer.asShortBuffer(); final short[] shortdata = new short[databytes / RasterUtils.SHORT_BYTES]; shortbuff.get(shortdata); raster.setDataElements(0, 0, width, height, shortdata); break; } default: throw new RasterWritableException("Error trying to read raster. Bad raster data type"); } // should we even try to extract the payload? if (payload != null) { // test to see if this is a raster with a possible payload final int payloadStart = startdata + databytes; if (rasterBytes.length > payloadStart) { // extract the payload final ByteArrayInputStream bais = new ByteArrayInputStream(rasterBytes, payloadStart, rasterBytes.length - payloadStart); final DataInputStream dis = new DataInputStream(bais); payload.readFields(dis); } } return raster; }
From source file:org.mrgeo.image.ImageStats.java
/** * Computes pixel value statistics: min, max, sum, count, & mean for a Raster and returns an array * of ImageStats objects, one for each band in the image. * //from w w w .j a va 2 s .c o m * @param raster * the raster to compute stats for * @param nodata * the value to ignore * @return an array of ImageStats objects */ static public void computeAndUpdateStats(final ImageStats[] tileStats, final Raster raster, final double[] nodata) throws RasterWritableException { final int type = raster.getTransferType(); Number sample; for (int y = 0; y < raster.getHeight(); y++) { for (int x = 0; x < raster.getWidth(); x++) { for (int b = 0; b < raster.getNumBands(); b++) { switch (type) { case DataBuffer.TYPE_BYTE: case DataBuffer.TYPE_INT: case DataBuffer.TYPE_SHORT: case DataBuffer.TYPE_USHORT: sample = raster.getSample(x, y, b); break; case DataBuffer.TYPE_FLOAT: sample = raster.getSampleFloat(x, y, b); break; case DataBuffer.TYPE_DOUBLE: sample = raster.getSampleDouble(x, y, b); break; default: throw new RasterWritableException( "Error computing tile statistics. Unsupported raster data type"); } updateStats(tileStats[b], sample, nodata[b]); } } } }