List of usage examples for java.nio FloatBuffer wrap
public static FloatBuffer wrap(float[] array)
From source file:Main.java
public static void main(String[] args) { FloatBuffer floatBuffer = FloatBuffer.wrap(new float[] { 1.23F }); floatBuffer.rewind();/*from w ww . j a v a 2 s . com*/ System.out.println(Arrays.toString(floatBuffer.array())); }
From source file:Main.java
/** * Convert from singed 16-bit PCM to 32-bit float PCM. * @param byteArray byte array// w w w . j a va2 s .c o m * @return byte array */ public static byte[] convert16BitTo32Bit(final byte[] byteArray) { float[] audioDataF = shortToFloat(byteToShort(byteArray)); for (int i = 0; i < audioDataF.length; i++) { audioDataF[i] /= 32768.0; } FloatBuffer fb = FloatBuffer.wrap(audioDataF); ByteBuffer byteBuffer = ByteBuffer.allocate(fb.capacity() * 4); byteBuffer.order(ByteOrder.LITTLE_ENDIAN); byteBuffer.asFloatBuffer().put(fb); return byteBuffer.array(); }
From source file:org.springframework.cloud.stream.app.pose.estimation.processor.PoseEstimationTensorflowInputConverter.java
private Tensor<Float> makeImageTensor(byte[] imageBytes) throws IOException { ByteArrayInputStream is = new ByteArrayInputStream(imageBytes); BufferedImage img = ImageIO.read(is); if (img.getType() != BufferedImage.TYPE_3BYTE_BGR) { throw new IllegalArgumentException( String.format("Expected 3-byte BGR encoding in BufferedImage, found %d", img.getType())); }//from ww w. j av a2 s .com // ImageIO.read produces BGR-encoded images, while the model expects RGB. int[] data = toIntArray(img); //Expand dimensions since the model expects images to have shape: [1, None, None, 3] long[] shape = new long[] { BATCH_SIZE, img.getHeight(), img.getWidth(), CHANNELS }; return Tensor.create(shape, FloatBuffer.wrap(toRgbFloat(data))); }
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 ww .j ava2 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:haven.Utils.java
public static FloatBuffer wfbuf(int n) { return (FloatBuffer.wrap(new float[n])); }
From source file:io.druid.segment.IndexMaker.java
private static void makeMetricColumn(final FileSmoosher v9Smoosher, final ProgressIndicator progress, final Iterable<Rowboat> theRows, final int metricIndex, final String metric, final Map<String, ValueType> valueTypes, final Map<String, String> metricTypeNames, final int rowCount, final CompressedObjectStrategy.CompressionStrategy compressionStrategy) throws IOException { final String section = String.format("make column[%s]", metric); progress.startSection(section);/*from w ww . j a va 2 s. c o m*/ final ColumnDescriptor.Builder metBuilder = ColumnDescriptor.builder(); ValueType type = valueTypes.get(metric); switch (type) { case FLOAT: { metBuilder.setValueType(ValueType.FLOAT); float[] arr = new float[rowCount]; int rowNum = 0; for (Rowboat theRow : theRows) { Object obj = theRow.getMetrics()[metricIndex]; arr[rowNum++] = (obj == null) ? 0 : ((Number) obj).floatValue(); } CompressedFloatsIndexedSupplier compressedFloats = CompressedFloatsIndexedSupplier .fromFloatBuffer(FloatBuffer.wrap(arr), IndexIO.BYTE_ORDER, compressionStrategy); writeColumn(v9Smoosher, new FloatGenericColumnPartSerde(compressedFloats, IndexIO.BYTE_ORDER), metBuilder, metric); break; } case LONG: { metBuilder.setValueType(ValueType.LONG); long[] arr = new long[rowCount]; int rowNum = 0; for (Rowboat theRow : theRows) { Object obj = theRow.getMetrics()[metricIndex]; arr[rowNum++] = (obj == null) ? 0 : ((Number) obj).longValue(); } CompressedLongsIndexedSupplier compressedLongs = CompressedLongsIndexedSupplier .fromLongBuffer(LongBuffer.wrap(arr), IndexIO.BYTE_ORDER, compressionStrategy); writeColumn(v9Smoosher, new LongGenericColumnPartSerde(compressedLongs, IndexIO.BYTE_ORDER), metBuilder, metric); break; } case COMPLEX: String complexType = metricTypeNames.get(metric); ComplexMetricSerde serde = ComplexMetrics.getSerdeForType(complexType); if (serde == null) { throw new ISE("Unknown type[%s]", complexType); } final GenericIndexed metricColumn = GenericIndexed .fromIterable(Iterables.transform(theRows, new Function<Rowboat, Object>() { @Override public Object apply(Rowboat input) { return input.getMetrics()[metricIndex]; } }), serde.getObjectStrategy()); metBuilder.setValueType(ValueType.COMPLEX); writeColumn(v9Smoosher, new ComplexColumnPartSerde(metricColumn, complexType), metBuilder, metric); break; default: throw new ISE("Unknown type[%s]", type); } progress.stopSection(section); }