throws IOException { DataBuffer dataBuffer = tileRowRaster.getDataBuffer(); int bands = dataBuffer.getNumBanks(); boolean banded = bands > 1; switch (tileRowRaster.getTransferType()) { case DataBuffer.TYPE_BYTE: int bank = banded ? ((BandedSampleModel) tileRowRaster.getSampleModel()).getBankIndices()[band] : band; byte[] rowDataByte = ((DataBufferByte) dataBuffer).getData(bank); WritableRaster destChannel = banded ? raster.createWritableChild(raster.getMinX(), raster.getMinY(), raster.getWidth(), raster.getHeight(), 0, 0, new int[] {band}) : raster; Raster srcChannel = banded case DataBuffer.TYPE_SHORT: short[] rowDataShort = dataBuffer.getDataType() == DataBuffer.TYPE_USHORT ? ((DataBufferUShort) dataBuffer).getData(band) : ((DataBufferShort) dataBuffer).getData(band); case DataBuffer.TYPE_INT: int[] rowDataInt = ((DataBufferInt) dataBuffer).getData(band);
strideElementsAligned = strideBytesAligned / 2; cm = new DirectColorModel(16, 0x7C00, 0x3E0, 0x1F); buffer = new DataBufferUShort(strideElementsAligned * height); raster = Raster.createPackedRaster(buffer, bih.biWidth, height, strideElementsAligned, ((DirectColorModel) cm).getMasks(), null); break; case 32: strideElementsAligned = strideBytesAligned / 4; cm = new DirectColorModel(32, 0xFF0000, 0xFF00, 0xFF); buffer = new DataBufferInt(strideElementsAligned * height); raster = Raster.createPackedRaster(buffer, bih.biWidth, height, strideElementsAligned, ((DirectColorModel) cm).getMasks(), null); break; default: switch (buffer.getDataType()) int[] pixels = ((DataBufferInt) buffer).getData(); ok = GDI.GetDIBits(blitDC, outputBitmap, 0, raster.getHeight(), pixels, bi, 0); short[] pixels = ((DataBufferUShort) buffer).getData(); ok = GDI.GetDIBits(blitDC, outputBitmap, 0, raster.getHeight(), pixels, bi, 0); throw new AssertionError("Unexpected buffer element type: " + buffer.getDataType());
/** * Returns a <code>SoftReference</code> to the internal bank data of the <code>DataBuffer</code> * . */ private static Object getBankReference(DataBuffer db) { Object array = null; switch (db.getDataType()) { case DataBuffer.TYPE_BYTE: array = ((DataBufferByte) db).getBankData(); break; case DataBuffer.TYPE_USHORT: array = ((DataBufferUShort) db).getBankData(); break; case DataBuffer.TYPE_SHORT: array = ((DataBufferShort) db).getBankData(); break; case DataBuffer.TYPE_INT: array = ((DataBufferInt) db).getBankData(); break; case DataBuffer.TYPE_FLOAT: array = DataBufferUtils.getBankDataFloat(db); break; case DataBuffer.TYPE_DOUBLE: array = DataBufferUtils.getBankDataDouble(db); break; default: throw new UnsupportedOperationException(""); } return array; }
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); 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(); DataBufferByte srcDBT = (DataBufferByte) srcDB; DataBufferByte retDBT = (DataBufferByte) retDB; System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len); break; DataBufferInt srcDBT = (DataBufferInt) srcDB; DataBufferInt retDBT = (DataBufferInt) retDB; System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len); break; DataBufferUShort srcDBT = (DataBufferUShort) srcDB; DataBufferUShort retDBT = (DataBufferUShort) retDB; System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len); break;
public static void extractFromPixeldata(Raster raster, int mask, byte[] ovlyData, int off, int length) { ComponentSampleModel sm = (ComponentSampleModel) raster.getSampleModel(); int columns = raster.getWidth(); int stride = sm.getScanlineStride(); DataBuffer db = raster.getDataBuffer(); switch (db.getDataType()) { case DataBuffer.TYPE_BYTE: extractFromPixeldata(((DataBufferByte) db).getData(), columns, stride, mask, ovlyData, off, length); break; case DataBuffer.TYPE_USHORT: extractFromPixeldata(((DataBufferUShort) db).getData(), columns, stride, mask, ovlyData, off, length); break; case DataBuffer.TYPE_SHORT: extractFromPixeldata(((DataBufferShort) db).getData(), columns, stride, mask, ovlyData, off, length); break; default: throw new UnsupportedOperationException("Unsupported DataBuffer type: " + db.getDataType()); //$NON-NLS-1$ } }
public BufferedImage convertToIntDiscrete(Raster raster) { if (!isCompatibleRaster(raster)) throw new IllegalArgumentException( "This raster is not compatible with this PaletteColorModel."); ColorModel cm = new DirectColorModel(getColorSpace(), 24, 0xff0000, 0x00ff00, 0x0000ff, 0, false, DataBuffer.TYPE_INT); int w = raster.getWidth(); int h = raster.getHeight(); WritableRaster discreteRaster = cm.createCompatibleWritableRaster(w, h); int[] discretData = ((DataBufferInt) discreteRaster.getDataBuffer()).getData(); DataBuffer data = raster.getDataBuffer(); if (data instanceof DataBufferByte) { byte[] pixels = ((DataBufferByte) data).getData(); for (int i = 0; i < pixels.length; i++) discretData[i] = getRGB(pixels[i]); } else { short[] pixels = ((DataBufferUShort) data).getData(); for (int i = 0; i < pixels.length; i++) discretData[i] = getRGB(pixels[i]); } return new BufferedImage(cm, discreteRaster, false, null); }
private Buffer wrapImageDataBuffer(final BufferedImage image) { // // Note: Grabbing the DataBuffer will defeat Java2D's image // management mechanism (as of JDK 5/6, at least). This shouldn't // be a problem for most JOGL apps, but those that try to upload // the image into an OpenGL texture and then use the same image in // Java2D rendering might find the 2D rendering is not as fast as // it could be. // final DataBuffer data = image.getRaster().getDataBuffer(); if (data instanceof DataBufferByte) { return ByteBuffer.wrap(((DataBufferByte) data).getData()); } else if (data instanceof DataBufferDouble) { throw new RuntimeException("DataBufferDouble rasters not supported by OpenGL"); } else if (data instanceof DataBufferFloat) { return FloatBuffer.wrap(((DataBufferFloat) data).getData()); } else if (data instanceof DataBufferInt) { return IntBuffer.wrap(((DataBufferInt) data).getData()); } else if (data instanceof DataBufferShort) { return ShortBuffer.wrap(((DataBufferShort) data).getData()); } else if (data instanceof DataBufferUShort) { return ShortBuffer.wrap(((DataBufferUShort) data).getData()); } else { throw new RuntimeException("Unexpected DataBuffer type?"); } } }
/** * Sets all values in the specified bank to the specified value. * * @param buffer The databuffer in which to set all sample values. * @param bank Index of the bank to set. * @param value The value. */ private static void fill(final DataBuffer buffer, final int bank, final int value) { switch (buffer.getDataType()) { case DataBuffer.TYPE_BYTE : Arrays.fill(((DataBufferByte) buffer).getData(bank), (byte)value); break; case DataBuffer.TYPE_SHORT: Arrays.fill(((DataBufferShort) buffer).getData(bank), (short)value); break; case DataBuffer.TYPE_USHORT: Arrays.fill(((DataBufferUShort)buffer).getData(bank), (short)value); break; case DataBuffer.TYPE_INT: Arrays.fill(((DataBufferInt) buffer).getData(bank), (int)value); break; case DataBuffer.TYPE_FLOAT: Arrays.fill(((DataBufferFloat) buffer).getData(bank), (float)value); break; case DataBuffer.TYPE_DOUBLE: Arrays.fill(((DataBufferDouble)buffer).getData(bank),(double)value); break; default: throw new RasterFormatException(String.valueOf(buffer)); } }
public static ByteBuffer createImageBuffer(BufferedImage image) { SampleModel model = image.getSampleModel(); Raster raster = image.getRaster(); DataBuffer inBuffer = raster.getDataBuffer(); ByteBuffer outBuffer = ByteBuffer.allocateDirect(image.getWidth() * image.getHeight() * model.getNumBands()); int x = -raster.getSampleModelTranslateX(); int y = -raster.getSampleModelTranslateY(); int step = model.getWidth() * model.getNumBands(); int channels = model.getNumBands(); byte[] a = ((DataBufferByte) inBuffer).getData(); copy(ByteBuffer.wrap(a, start, a.length - start), step, outBuffer, step, false); short[] a = ((DataBufferShort) inBuffer).getData(); copy(ShortBuffer.wrap(a, start, a.length - start), step, outBuffer.asShortBuffer(), step / 2, true); short[] a = ((DataBufferUShort) inBuffer).getData(); copy(ShortBuffer.wrap(a, start, a.length - start), step, outBuffer.asShortBuffer(), step / 2, false); int[] a = ((DataBufferInt) inBuffer).getData(); copy(IntBuffer.wrap(a, start, a.length - start), step, outBuffer.asIntBuffer(), step / 4);
checkReadParamBandSettings(param, rawType.getNumBands(), destRaster.getNumBands()); WritableRaster destChannel = destRaster.createWritableChild(destRaster.getMinX(), destRaster.getMinY(), destRaster.getWidth(), destRaster.getHeight(), 0, 0, new int[] {c}); Raster srcChannel = clippedRow.createChild(clippedRow.getMinX(), 0, clippedRow.getWidth(), 1, 0, 0, new int[] {c}); switch (header.getBytesPerPixel()) { case 1: byte[] rowDataByte = ((DataBufferByte) rowRaster.getDataBuffer()).getData(c); readRowByte(height, srcRegion, scanlineOffsets, scanlineLengths, compression, xSub, ySub, c, rowDataByte, destChannel, srcChannel, y); break; case 2: short[] rowDataUShort = ((DataBufferUShort) rowRaster.getDataBuffer()).getData(c); readRowUShort(height, srcRegion, scanlineOffsets, scanlineLengths, compression, xSub, ySub, c, rowDataUShort, destChannel, srcChannel, y); break;
private Object extractImageData(BufferedImage img){ DataBuffer buf = img.getRaster().getDataBuffer(); switch (buf.getDataType()){ case DataBuffer.TYPE_BYTE: DataBufferByte byteBuf = (DataBufferByte) buf; return byteBuf.getData(); case DataBuffer.TYPE_USHORT: DataBufferUShort shortBuf = (DataBufferUShort) buf; return shortBuf.getData(); } return null; }
.getRaster(); DataBuffer buffer = rowRaster.getDataBuffer(); for (int y = 0; y < raster.getHeight(); y++) { if (abortRequested()) { break; switch (buffer.getDataType()) { case DataBuffer.TYPE_BYTE: rowRaster.setDataElements(0, 0, raster.createChild(0, y, raster.getWidth(), 1, 0, 0, null)); imageOutput.write(((DataBufferByte) buffer).getData()); break; case DataBuffer.TYPE_USHORT: rowRaster.setDataElements(0, 0, raster.createChild(0, y, raster.getWidth(), 1, 0, 0, null)); short[] shorts = ((DataBufferUShort) buffer).getData(); imageOutput.writeShorts(shorts, 0, shorts.length); break;
/** Extracts pixel data as arrays of unsigned shorts, one per channel. */ public static short[][] getShorts(WritableRaster r, int x, int y, int w, 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(); } int c = r.getNumBands(); short[][] samples = new short[c][w * h]; 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; }
private void nullifyUnusedBits(int bitsStored, BufferedImage bi) { DataBuffer db = bi.getRaster().getDataBuffer(); switch (db.getDataType()) { case DataBuffer.TYPE_USHORT: nullifyUnusedBits(bitsStored, ((DataBufferUShort) db).getData()); break; case DataBuffer.TYPE_SHORT: nullifyUnusedBits(bitsStored, ((DataBufferShort) db).getData()); break; } }
ImageTypeSpecifier singleBandRowSpec = ImageTypeSpecifiers.createGrayscale(header.bits, pSourceCM.getTransferType()); WritableRaster rowRaster = singleBandRowSpec.createBufferedImage(header.width, 1).getRaster(); boolean banded = destRaster.getDataBuffer().getNumBanks() > 1; int interleavedBands = banded ? 1 : destRaster.getNumBands(); byte[] row1 = ((DataBufferByte) rowRaster.getDataBuffer()).getData(); read1bitChannel(c, channels, destRaster.getDataBuffer(), interleavedBands, bandOffset, pSourceCM, row1, pSource, pDest, pXSub, pYSub, header.width, header.height, pByteCounts, pCompression == PSD.COMPRESSION_RLE); break; case 8: byte[] row8 = ((DataBufferByte) rowRaster.getDataBuffer()).getData(); read8bitChannel(c, channels, destRaster.getDataBuffer(), interleavedBands, bandOffset, pSourceCM, row8, pSource, pDest, pXSub, pYSub, header.width, header.height, pByteCounts, c * header.height, pCompression == PSD.COMPRESSION_RLE); break; case 16: short[] row16 = ((DataBufferUShort) rowRaster.getDataBuffer()).getData(); read16bitChannel(c, channels, destRaster.getDataBuffer(), interleavedBands, bandOffset, pSourceCM, row16, pSource, pDest, pXSub, pYSub, header.width, header.height, pByteCounts, c * header.height, pCompression == PSD.COMPRESSION_RLE); break; case 32: int[] row32 = ((DataBufferInt) rowRaster.getDataBuffer()).getData(); read32bitChannel(c, channels, destRaster.getDataBuffer(), interleavedBands, bandOffset, pSourceCM, row32, pSource, pDest, pXSub, pYSub, header.width, header.height, pByteCounts, c * header.height, pCompression == PSD.COMPRESSION_RLE); break;
public void lookup(Raster src, Raster dst, int alpha, int channels, int skip) { int srcWidth = src.getWidth(); int dstWidth = dst.getWidth(); int srcHeight = src.getHeight(); + " != dst.height:" + dstHeight); if (srcHeight * dstScanlineStride > dstdata.getSize()) { throw new IllegalArgumentException("srcHeight:" + srcHeight + " * dstScanlineStride:" + dstScanlineStride byte[][] data = ((DataBufferByte) dstdata).getBankData(); for (int bank = 0; bank < data.length; bank++) { lookup(srcdata, srcWidth, srcHeight, srcScanlineStride, case DataBuffer.TYPE_USHORT: lookup(srcdata, srcScanlineStride, srcHeight, srcScanlineStride, ((DataBufferUShort) dstdata).getData(), dstScanlineStride); break; case DataBuffer.TYPE_SHORT: lookup(srcdata, srcScanlineStride, srcHeight, srcScanlineStride, ((DataBufferShort) dstdata).getData(), dstScanlineStride); break; case DataBuffer.TYPE_INT: lookup(srcdata, srcScanlineStride, srcHeight, srcScanlineStride, ((DataBufferInt) dstdata).getData(), dstScanlineStride, alpha); break; default:
checkReadParamBandSettings(param, rawType.getNumBands(), destRaster.getNumBands()); case 2: case 4: rowRaster = Raster.createPackedRaster(new DataBufferByte(rowSizeBytes), width, 1, header.getBitCount(), null); break; case 8: case 24: rowRaster = Raster.createInterleavedRaster(new DataBufferByte(rowSizeBytes), width, 1, rowSizeBytes, header.getBitCount() / 8, createOffsets(rawType.getNumBands()), null); break; case 16: case 8: case 24: byte[] rowDataByte = ((DataBufferByte) rowRaster.getDataBuffer()).getData(); readRowByte(input, height, srcRegion, xSub, ySub, rowDataByte, destRaster, clippedRow, y); break; short[] rowDataUShort = ((DataBufferUShort) rowRaster.getDataBuffer()).getData(); readRowUShort(input, height, srcRegion, xSub, ySub, rowDataUShort, destRaster, clippedRow, y); break; int[] rowDataInt = ((DataBufferInt) rowRaster.getDataBuffer()).getData(); readRowInt(input, height, srcRegion, xSub, ySub, rowDataInt, destRaster, clippedRow, y); break;
? param.getDestinationBands() : null); checkReadParamBandSettings(param, rawType.getNumBands(), destRaster.getNumBands()); param != null ? param.getSourceXSubsampling() : 1); int transferType = rowRaster.getTransferType(); int samplesPerPixel = header.getSamplesPerPixel(); rowDataByte = ((DataBufferByte) rowRaster.getDataBuffer()).getData(); break; case DataBuffer.TYPE_USHORT: rowDataUShort = ((DataBufferUShort) rowRaster.getDataBuffer()).getData(); break; case DataBuffer.TYPE_FLOAT:
SampleModel sm = raster.getSampleModel(); if (!isBinary(sm)) { throw new IllegalArgumentException("Not a binary raster!"); int rectHeight = rect.height; DataBuffer dataBuffer = raster.getDataBuffer(); int dx = rectX - raster.getSampleModelTranslateX(); int dy = rectY - raster.getSampleModelTranslateY(); int eltOffset = dataBuffer.getOffset() + mpp.getOffset(dx, dy); int bitOffset = mpp.getBitOffset(dx); byte[] data = ((DataBufferByte) dataBuffer).getData(); for (int y = rectY; y < maxY; y++) { int bOffset = eltOffset * 8 + bitOffset; short[] data = dataBuffer instanceof DataBufferShort ? ((DataBufferShort) dataBuffer).getData() : ((DataBufferUShort) dataBuffer).getData(); for (int y = rectY; y < maxY; y++) { int bOffset = eltOffset * 16 + bitOffset; int[] data = ((DataBufferInt) dataBuffer).getData(); for (int y = rectY; y < maxY; y++) { int bOffset = eltOffset * 32 + bitOffset;
private DataBuffer createDataBufferFilledWithNoDataValues( WritableRaster raster, int pixelSize) { int dataType = raster.getDataBuffer().getDataType(); int dataBufferSize = raster.getDataBuffer().getSize(); int nrBanks = raster.getDataBuffer().getNumBanks(); DataBuffer dataBuffer; switch (dataType) { Arrays.fill(intDataArray[i], noDataValue.intValue()); dataBuffer = new DataBufferInt(intDataArray, dataBufferSize); break; case DataBuffer.TYPE_FLOAT: Arrays.fill(shortDataArray[i], noDataValue.shortValue()); dataBuffer = new DataBufferShort(shortDataArray, dataBufferSize); break; Arrays.fill(byteDataArray[i], noDataValue.byteValue()); dataBuffer = new DataBufferByte(byteDataArray, dataBufferSize); break; Arrays.fill(ushortDataArray[i], noDataValue.shortValue()); dataBuffer = new DataBufferUShort(ushortDataArray, dataBufferSize); break;