List of usage examples for java.nio DoubleBuffer put
public DoubleBuffer put(DoubleBuffer src)
From source file:nitf.imageio.NITFReader.java
/** * Optimization to read the entire image in one fell swoop... This is most * likely the common use case for this codec, so we hope this optimization * will be helpful.//from w w w.jav a 2 s. c o m * * @param imageIndex * @param sourceXSubsampling * @param sourceYSubsampling * @param bandOffsets * @param pixelSize * @param imRas * @throws IOException */ protected void readFullImage(int imageIndex, Rectangle destRegion, int sourceXSubsampling, int sourceYSubsampling, int[] bandOffsets, int pixelSize, WritableRaster imRas) throws IOException { try { ImageSubheader subheader = record.getImages()[imageIndex].getSubheader(); int numCols = destRegion.width; int numRows = destRegion.height; int nBands = subheader.getBandCount(); /* * NOTE: This is a "fix" that will be removed once the underlying * NITRO library gets patched. Currently, if you make a request of a * single band, it doesn't matter which band you request - the data * from the first band will be returned regardless. This is * obviously wrong. To thwart this, we will read all bands, then * scale down what we return to the user based on their actual * request. */ int[] requestBands = bandOffsets; /* * if (nBands != bandOffsets.length && bandOffsets.length == 1 * && bandOffsets[0] != 0) * { * requestBands = new int[nBands]; * for (int i = 0; i < nBands; ++i) * requestBands[i] = i; * } */ int bufSize = numCols * numRows * pixelSize; byte[][] imageBuf = new byte[requestBands.length][bufSize]; // make a SubWindow from the params // TODO may want to read by blocks or rows to make faster and more // memory efficient SubWindow window; window = new SubWindow(); window.setNumBands(requestBands.length); window.setBandList(requestBands); window.setNumCols(numCols); window.setNumRows(numRows); window.setStartCol(0); window.setStartRow(0); // the NITRO library can do the subsampling for us if (sourceYSubsampling != 1 || sourceXSubsampling != 1) { DownSampler downSampler = new PixelSkipDownSampler(sourceYSubsampling, sourceXSubsampling); window.setDownSampler(downSampler); } // String pixelJustification = subheader.getPixelJustification() // .getStringData().trim(); // boolean shouldSwap = pixelJustification.equals("R"); // since this is Java, we need the data in big-endian format // boolean shouldSwap = ByteOrder.nativeOrder() != // ByteOrder.BIG_ENDIAN; nitf.ImageReader imageReader = getImageReader(imageIndex); imageReader.read(window, imageBuf); List<ByteBuffer> bandBufs = new ArrayList<ByteBuffer>(); for (int i = 0; i < bandOffsets.length; ++i) { ByteBuffer bandBuf = null; // the special "fix" we added needs to do this if (bandOffsets.length != requestBands.length) { bandBuf = ByteBuffer.wrap(imageBuf[bandOffsets[i]]); } else { bandBuf = ByteBuffer.wrap(imageBuf[i]); } // ban dBuf.order(ByteOrder.nativeOrder()); // shouldSwap ? ByteOrder.LITTLE_ENDIAN // : ByteOrder.BIG_ENDIAN); bandBufs.add(bandBuf); } // optimization for 1 band case... just dump the whole thing if (bandOffsets.length == 1) { ByteBuffer bandBuf = bandBufs.get(0); switch (pixelSize) { case 1: ByteBuffer rasterByteBuf = ByteBuffer.wrap(((DataBufferByte) imRas.getDataBuffer()).getData()); rasterByteBuf.put(bandBuf); break; case 2: ShortBuffer rasterShortBuf = ShortBuffer .wrap(((DataBufferUShort) imRas.getDataBuffer()).getData()); rasterShortBuf.put(bandBuf.asShortBuffer()); break; case 4: FloatBuffer rasterFloatBuf = FloatBuffer .wrap(((DataBufferFloat) imRas.getDataBuffer()).getData()); rasterFloatBuf.put(bandBuf.asFloatBuffer()); break; case 8: DoubleBuffer rasterDoubleBuf = DoubleBuffer .wrap(((DataBufferDouble) imRas.getDataBuffer()).getData()); rasterDoubleBuf.put(bandBuf.asDoubleBuffer()); break; } } else { // for multi-band case, we need to iterate over each pixel... // TODO -- optimize this!... somehow for (int srcY = 0, srcX = 0; srcY < numRows; srcY++) { // Copy each (subsampled) source pixel into imRas for (int dstX = 0; dstX < numCols; srcX += pixelSize, dstX++) { for (int i = 0; i < bandOffsets.length; ++i) { ByteBuffer bandBuf = bandBufs.get(i); switch (pixelSize) { case 1: imRas.setSample(dstX, srcY, i, bandBuf.get(srcX)); break; case 2: imRas.setSample(dstX, srcY, i, bandBuf.getShort(srcX)); break; case 4: imRas.setSample(dstX, srcY, i, bandBuf.getFloat(srcX)); break; case 8: imRas.setSample(dstX, srcY, i, bandBuf.getDouble(srcX)); break; } } } } } } catch (NITFException e1) { throw new IOException(ExceptionUtils.getStackTrace(e1)); } }
From source file:org.bimserver.geometry.StreamingGeometryGenerator.java
void setTransformationMatrix(VirtualObject geometryInfo, double[] transformationMatrix) throws BimserverDatabaseException { ByteBuffer byteBuffer = ByteBuffer.allocate(16 * 8); byteBuffer.order(ByteOrder.nativeOrder()); DoubleBuffer asDoubleBuffer = byteBuffer.asDoubleBuffer(); for (double d : transformationMatrix) { asDoubleBuffer.put(d); }// w w w . java 2 s . c o m geometryInfo.setAttribute(GeometryPackage.eINSTANCE.getGeometryInfo_Transformation(), byteBuffer.array()); }
From source file:org.bimserver.utils.BinUtils.java
public static byte[] doubleToByteArray(Double inDouble) { byte[] bArray = new byte[8]; ByteBuffer bBuffer = ByteBuffer.wrap(bArray); DoubleBuffer lBuffer = bBuffer.asDoubleBuffer(); lBuffer.put(inDouble); return bArray; }
From source file:org.mitre.math.linear.BufferRealMatrix.java
/** * Compute the sum of this and <code>m</code>. * * @param m matrix to be added// w w w.j a va 2 s . co m * @return this + m * @throws IllegalArgumentException if m is not the same size as this */ public BufferRealMatrix add(final BufferRealMatrix b) throws IllegalArgumentException { // safety checks if (b == this) { return this.addSelf(); } MatrixUtils.checkAdditionCompatible(this, b); try { final BufferRealMatrix c = new BufferRealMatrix(rows, columns, null); // perform addition block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < this.blockRows * this.blockColumns; ++blockIndex) { // all the same size, so should all be the same blockOffsets and layout final long blockOffset = this.getBlockOffset(blockIndex); DoubleBuffer adb = this.dataFileChannel .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer(); adb.clear(); DoubleBuffer bdb = b.dataFileChannel .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer(); bdb.clear(); DoubleBuffer cdb = c.dataFileChannel .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer(); cdb.clear(); for (int k = 0; k < BLOCK_BYTE_SIZE / DOUBLE_BYTE_SIZE; k++) { try { cdb.put(adb.get() + bdb.get()); } catch (BufferUnderflowException e) { LOG.fatal(String.format("BufferUnderflowException while adding elements at %d in block %d", k, blockIndex)); throw e; } } } return c; } catch (IOException ioe) { throw new RuntimeException(ioe); } }
From source file:org.mitre.math.linear.BufferRealMatrix.java
/** Special case for adding to ourself */ private BufferRealMatrix addSelf() { try {// www . jav a2s .c om final BufferRealMatrix c = new BufferRealMatrix(rows, columns, null); // perform addition block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < this.blockRows * this.blockColumns; ++blockIndex) { // all the same size, so should all be the same blockOffsets and layout final long blockOffset = this.getBlockOffset(blockIndex); DoubleBuffer adb = this.dataFileChannel .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer(); adb.clear(); DoubleBuffer cdb = c.dataFileChannel .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer(); cdb.clear(); for (int k = 0; k < BLOCK_BYTE_SIZE / DOUBLE_BYTE_SIZE; k++) { try { double ad = adb.get(); cdb.put(ad + ad); } catch (BufferUnderflowException e) { LOG.fatal(String.format("BufferUnderflowException while adding elements at %d in block %d", k, blockIndex)); throw e; } } } return c; } catch (IOException ioe) { throw new RuntimeException(ioe); } }
From source file:org.mitre.math.linear.BufferRealMatrix.java
/** {@inheritDoc} */ public BufferRealMatrix subtract(final BufferRealMatrix b) throws IllegalArgumentException { // if (b == this) { // return this.subtractSelf(); // }//from w w w .j a v a2 s .c o m // safety check MatrixUtils.checkSubtractionCompatible(this, b); try { final BufferRealMatrix c = new BufferRealMatrix(rows, columns, null); // perform addition block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < this.blockRows * this.blockColumns; ++blockIndex) { // all the same size, so should all be the same blockOffsets and layout final long blockOffset = this.getBlockOffset(blockIndex); DoubleBuffer adb = this.dataFileChannel .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer(); adb.clear(); DoubleBuffer bdb = b.dataFileChannel .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer(); bdb.clear(); DoubleBuffer cdb = c.dataFileChannel .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer(); cdb.clear(); for (int k = 0; k < BLOCK_BYTE_SIZE / DOUBLE_BYTE_SIZE; k++) { try { cdb.put(adb.get() - bdb.get()); } catch (BufferUnderflowException e) { LOG.fatal(String.format("BufferUnderflowException while adding elements at %d in block %d", k, blockIndex)); throw e; } } } return c; } catch (IOException ioe) { throw new RuntimeException(ioe); } }
From source file:org.mitre.math.linear.BufferRealMatrix.java
/** {@inheritDoc} */ public BufferRealMatrix scalarAdd(final double d) { try {// w w w .j ava 2 s . c o m final BufferRealMatrix c = new BufferRealMatrix(rows, columns, null); // perform addition block-wise, to ensure good cache behavior for (int blockIndex = 0; blockIndex < this.blockRows * this.blockColumns; ++blockIndex) { // all the same size, so should all be the same blockOffsets and layout final long blockOffset = this.getBlockOffset(blockIndex); DoubleBuffer adb = this.dataFileChannel .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer(); adb.clear(); DoubleBuffer cdb = c.dataFileChannel .map(FileChannel.MapMode.READ_WRITE, blockOffset, BLOCK_BYTE_SIZE).asDoubleBuffer(); cdb.clear(); for (int k = 0; k < BLOCK_BYTE_SIZE / DOUBLE_BYTE_SIZE; k++) { try { double ad = adb.get(); cdb.put(ad + d); } catch (BufferUnderflowException e) { LOG.fatal(String.format("BufferUnderflowException while adding elements at %d in block %d", k, blockIndex)); throw e; } } } return c; } catch (IllegalArgumentException ex) { LOG.fatal(ex); throw new RuntimeException(ex); } catch (IOException ex) { LOG.fatal(ex); throw new RuntimeException(ex); } }
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);//from w ww. ja v a2s . c o 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; }