GDALTile { public BufferedImage getAsImage() throws TilerException { int width = parameters.size.width; int height = parameters.size.height; int pixels = width * height; int bandCount = bufferBandCount; DataBuffer imgBuffer = null; int imageType = 0, dataType = 0; if (bufferType == gdalconstConstants.GDT_Byte) { byte[] bytes = new byte[pixels * bandCount]; buffer.get(bytes); imgBuffer = new DataBufferByte(bytes, bytes.length); dataType = DataBuffer.TYPE_BYTE; imageType = indexed ? BufferedImage.TYPE_BYTE_INDEXED : BufferedImage.TYPE_BYTE_GRAY; } else if (bufferType == gdalconstConstants.GDT_Int16 || bufferType == gdalconstConstants.GDT_UInt16 || bufferType == gdalconstConstants.GDT_CInt16) { short[] shorts = new short[pixels * bandCount]; buffer.asShortBuffer().get(shorts); imgBuffer = new DataBufferShort(shorts, shorts.length); dataType = bufferType == gdalconstConstants.GDT_UInt16 ? DataBuffer.TYPE_USHORT : DataBuffer.TYPE_SHORT; imageType = BufferedImage.TYPE_USHORT_GRAY; } else if (bufferType == gdalconstConstants.GDT_Int32 || bufferType == gdalconstConstants.GDT_UInt32 || bufferType == gdalconstConstants.GDT_CInt32) { int[] ints = new int[pixels * bandCount]; buffer.asIntBuffer().get(ints); imgBuffer = new DataBufferInt(ints, ints.length); dataType = DataBuffer.TYPE_INT; imageType = BufferedImage.TYPE_CUSTOM; } else if (bufferType == gdalconstConstants.GDT_Float32 && bandCount == 1) { GDALTile byteTile = this.convertToType(gdalconstConstants.GDT_Byte); bandCount = 4; byte[] bytes = new byte[pixels * bandCount]; ByteBuffer buf = byteTile.getBuffer(); for (int i = 0; i < bandCount; i++) { buf.rewind(); buf.get(bytes, pixels * i, pixels); } imgBuffer = new DataBufferByte(bytes, bytes.length); dataType = DataBuffer.TYPE_BYTE; } else { throw new TilerException("Unsupported image data type"); } int[] offsets = new int[bandCount]; for (int b = 0; b < bandCount; b++) { offsets[b] = b * pixels; } SampleModel sampleModel = new ComponentSampleModel(dataType, width, height, 1, width, offsets); WritableRaster raster = Raster.createWritableRaster(sampleModel, imgBuffer, null); BufferedImage img = null; if (bandCount == 4) { imageType = BufferedImage.TYPE_INT_ARGB_PRE; } else if (bandCount == 3) { imageType = BufferedImage.TYPE_INT_RGB; } else if (indexed && bandCount == 1) { } else { } img = (indexColorModel == null) ? new BufferedImage(width, height, imageType) : new BufferedImage(width, height, imageType, indexColorModel); img.setData(raster); return img; } GDALTile(GDALTileParameters parameters); protected  GDALTile(GDALTile tile, ByteBuffer buffer, int bufferType, int bufferTypeSize, boolean floatingPoint); static boolean isTypeFloatingPoint(int bufferType); BufferedImage getAsImage(); void updateMinMax(NumberArray minmax, NullableNumberArray outsideValues); boolean isBlank(); ByteBuffer getBuffer(); Rectangle getDataRectangle(); int getBufferType(); int getBufferTypeSize(); boolean isFloatingPoint(); int getBandCount(); GDALTile convertToType(int newBufferType);  }
@Test public void testGetAsImage() throws Exception { Dataset dataset = openTestDataset(); Dimension size = new Dimension(512, 512); Sector sector = new Sector(-27, 141, -25, 144); GDALTileParameters params = new GDALTileParameters(dataset, size, sector); GDALTile tile = new GDALTile(params); BufferedImage image = tile.getAsImage(); assertNotNull(image); assertEquals(512, image.getWidth()); assertEquals(512, image.getHeight()); assertEquals(BufferedImage.TYPE_USHORT_GRAY, image.getType()); }