Example usage for java.nio ShortBuffer put

List of usage examples for java.nio ShortBuffer put

Introduction

In this page you can find the example usage for java.nio ShortBuffer put.

Prototype

public ShortBuffer put(ShortBuffer src) 

Source Link

Document

Writes all the remaining shorts of the src short buffer to this buffer's current position, and increases both buffers' position by the number of shorts copied.

Usage

From source file:Main.java

public static ShortBuffer createShortBuffer(short[] data) {
    ShortBuffer sb = ByteBuffer.allocateDirect(data.length * BYTES_PER_SHORT).order(ByteOrder.nativeOrder())
            .asShortBuffer();//  ww w.  ja  v  a 2s  .c  o m
    sb.put(data).position(0);
    return sb;
}

From source file:Main.java

public static ShortBuffer shortToBuffer(short[] a) {
    ShortBuffer shortBuffer;
    ByteBuffer ibb = ByteBuffer.allocateDirect(a.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    shortBuffer = ibb.asShortBuffer();//ww w . ja va2 s .c  om
    shortBuffer.put(a);
    shortBuffer.position(0);
    return shortBuffer;
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Converts the specified short buffer to native endian and returns this new
 * buffer. If buffer is already in correct endian format then it is returned
 * right away.//  ww w  .  j av a2s.  com
 *
 * @param buffer
 *            The short buffer to convert
 * @return The converted short buffer or the source buffer if no conversion
 *         is needed
 */

public static ShortBuffer convertToNativeEndian(final ShortBuffer buffer) {
    if (buffer.order() == ByteOrder.nativeOrder())
        return buffer;

    final ByteBuffer bytes = ByteBuffer.allocateDirect(buffer.capacity());
    bytes.order(ByteOrder.nativeOrder());
    final ShortBuffer shorts = bytes.asShortBuffer();
    shorts.put(buffer).rewind();
    return shorts;
}

From source file:org.bimserver.utils.BinUtils.java

public static byte[] shortToByteArray(short inShort) {
    byte[] bArray = new byte[2];
    ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
    ShortBuffer lBuffer = bBuffer.asShortBuffer();
    lBuffer.put(inShort);
    return bArray;
}

From source file:Main.java

public static Bitmap fromYUV420P(byte[] yuv, int width, int height) {
    if (yuv == null) {
        Log.e(TAG, "yuv data==null");
        return null;
    }/*from  w w  w .j a v  a  2 s .co  m*/
    if (yuv.length != width * height * 1.5) {
        Log.e(TAG, "yudData does not match the provided width and height");
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    int offsetY = 0;
    ShortBuffer buffer = ShortBuffer.allocate(width * height * 2);
    for (int line = 0; line < height; line++) {
        for (int col = 0; col < width; col++) {
            int y = yuv[offsetY++] & 0xFF;
            buffer.put((short) ((y >> 3) << 11 | (y >> 2) << 5 | (y >> 3)));
        }
    }
    bitmap.copyPixelsFromBuffer(buffer);
    return bitmap;
}

From source file:Main.java

public static ShortBuffer createShortBuffer(short[] shortData) {
    ShortBuffer drawListBuffer;
    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            shortData.length * 2);//from  w  w w . j a v a2  s  .co  m
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(shortData);
    drawListBuffer.position(0);
    return drawListBuffer;
}

From source file:Main.java

public static ShortBuffer setupShortBuffer(ShortBuffer preBuffer, short[] array) {

    if (preBuffer == null || preBuffer.capacity() < array.length) {
        preBuffer = createShortBuffer(array.length * 2);
    } else {//from w  ww .j  av a2s .  c o  m
        preBuffer.clear();
    }

    preBuffer.clear();
    preBuffer.put(array);
    preBuffer.position(0);

    return preBuffer;
}

From source file:Main.java

public static ShortBuffer clone(final ShortBuffer buf) {
    if (buf == null) {
        return null;
    }/*w  ww.j a v  a2 s .  co m*/
    buf.rewind();

    final ShortBuffer copy;
    if (buf.isDirect()) {
        copy = createShortBuffer(buf.limit());
    } else {
        copy = createShortBufferOnHeap(buf.limit());
    }
    copy.put(buf);

    return copy;
}

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 www .  ja v a2s.  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:haven.Utils.java

public static ShortBuffer bufcp(short[] a) {
    ShortBuffer b = mksbuf(a.length);
    b.put(a);
    b.rewind();/*from  w  ww. j  a v a2  s  .c  om*/
    return (b);
}