Example usage for java.nio ByteOrder nativeOrder

List of usage examples for java.nio ByteOrder nativeOrder

Introduction

In this page you can find the example usage for java.nio ByteOrder nativeOrder.

Prototype

public static ByteOrder nativeOrder() 

Source Link

Document

Returns the current platform byte order.

Usage

From source file:cpcc.ros.services.RosImageConverterTest.java

@Test(dataProvider = "imageDataprovider")
public void shouldConvertGenericImages(int height, int width, String encoding, String imageName)
        throws IOException {
    InputStream stream = RosImageConverterTest.class.getResourceAsStream(imageName);
    byte[] imageData = IOUtils.toByteArray(stream);

    when(buffer.array()).thenReturn(imageData);
    //        when(buffer.hasArray()).thenReturn(true);

    when(message.getEncoding()).thenReturn(encoding);
    when(message.getHeight()).thenReturn(height);
    when(message.getWidth()).thenReturn(width);
    when(message.getData()).thenReturn(ChannelBuffers.copiedBuffer(ByteOrder.nativeOrder(), imageData));

    BufferedImage result = conv.messageToBufferedImage(message);
    assertThat(result).isNotNull();//  w  w w.  j a va2  s. co  m

    assertThat(result.getHeight()).isEqualTo(height);
    assertThat(result.getWidth()).isEqualTo(width);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageIO.write(result, encoding, bos);

    byte[] resultImageData = bos.toByteArray();

    assertThat(resultImageData.length).isEqualTo(imageData.length);
    // assertThat(resultImageData).isEqualTo(imageData);
}

From source file:com.alvermont.terraj.fracplanet.geom.TriangleBufferArray.java

/**
 * Create a TriangleBufferArray with a specified capacity
 *
 * @param capacity The number of elements this buffer should be capable of holding
 *//*from w  w  w  . j a va 2s. c  om*/
public TriangleBufferArray(int capacity) {
    this.buffer = ByteBuffer.allocateDirect(capacity * INTS_PER_ENTRY * SIZEOF_INT)
            .order(ByteOrder.nativeOrder()).asIntBuffer();
    this.buffer.limit(0);
}

From source file:Main.java

public static IntBuffer createIntBuffer(final int size) {
    final IntBuffer buf = ByteBuffer.allocateDirect(4 * size).order(ByteOrder.nativeOrder()).asIntBuffer();
    buf.clear();/* w w  w.  ja  v a  2s .com*/
    return buf;
}

From source file:com.kentdisplays.synccardboarddemo.Page.java

/**
 * Sets up the drawing object data for use in an OpenGL ES context.
 *
 * @param is InputStream to the page to load the path data from.
 *//*from  w ww. ja v a2s  .c o m*/
public Page(InputStream is, int glProgram, int direction) {

    this.mModel = new float[16];
    this.mGlProgram = glProgram;

    // Calculate the coordinates from the given path.
    ArrayList<Path> paths = pathsFromSamplePageInputStream(is);
    float finalCoords[] = {};
    float finalNormals[] = {};
    float finalColors[] = {};
    mNumberOfPaths = paths.size();
    for (int i = 0; i < mNumberOfPaths; i++) {
        Path path = paths.get(i);
        float x1 = (path.x1 / 13942 * 2) - 1;
        float y1 = (path.y1 / 20280 * 2) - 1;
        float x2 = (path.x2 / 13942 * 2) - 1;
        float y2 = (path.y2 / 20280 * 2) - 1;
        float width = path.width / 3000;
        width = width < 0.013f ? 0.013f : width; // Width should be at least 0.013

        float distance = (float) Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
        float angle = (float) Math.PI / 2 - (float) Math.asin((x2 - x1) / distance);
        float xdiff = (width / 2) * (float) Math.sin(angle);
        float ydiff = (width / 2) * (float) Math.cos(angle);

        float coords[] = { x1 - xdiff, y1 - ydiff, 1.0f, // top left
                x2 - xdiff, y2 - ydiff, 1.0f, // bottom left
                x1 + xdiff, y1 + ydiff, 1.0f, // top right
                x2 - xdiff, y2 - ydiff, 1.0f, // bottom left
                x2 + xdiff, y2 + ydiff, 1.0f, // bottom right
                x1 + xdiff, y1 + ydiff, 1.0f, // top right
        };

        float normals[] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
                1.0f, 0.0f, 0.0f, 1.0f, };

        float colors[] = { 0.2f, 0.709803922f, 0.898039216f, 1.0f, 0.2f, 0.709803922f, 0.898039216f, 1.0f, 0.2f,
                0.709803922f, 0.898039216f, 1.0f, 0.2f, 0.709803922f, 0.898039216f, 1.0f, 0.2f, 0.709803922f,
                0.898039216f, 1.0f, 0.2f, 0.709803922f, 0.898039216f, 1.0f, };

        finalCoords = Floats.concat(finalCoords, coords);
        finalNormals = Floats.concat(finalNormals, normals);
        finalColors = Floats.concat(finalColors, colors);
    }

    ByteBuffer bbVertices = ByteBuffer.allocateDirect(finalCoords.length * 4);
    bbVertices.order(ByteOrder.nativeOrder());
    mPageVertices = bbVertices.asFloatBuffer();
    mPageVertices.put(finalCoords);
    mPageVertices.position(0);

    ByteBuffer bbNormals = ByteBuffer.allocateDirect(finalNormals.length * 4);
    bbNormals.order(ByteOrder.nativeOrder());
    mPageNormals = bbNormals.asFloatBuffer();
    mPageNormals.put(finalNormals);
    mPageNormals.position(0);

    ByteBuffer bbColors = ByteBuffer.allocateDirect(finalColors.length * 4);
    bbColors.order(ByteOrder.nativeOrder());
    mPageColors = bbColors.asFloatBuffer();
    mPageColors.put(finalColors);
    mPageColors.position(0);

    // Correctly place the page in the world.
    Matrix.setIdentityM(mModel, 0);
    switch (direction) {
    case 0:
        Matrix.translateM(mModel, 0, 0, 0, -mDistance); //Front.
        break;
    case 1:
        Matrix.translateM(mModel, 0, -mDistance, 0, 0); // Left.
        Matrix.rotateM(mModel, 0, 90, 0, 1f, 0);
        break;
    case 2:
        Matrix.translateM(mModel, 0, 0, 0, mDistance); // Behind.
        Matrix.rotateM(mModel, 0, 180, 0, 1f, 0);
        break;
    case 3:
        Matrix.translateM(mModel, 0, mDistance, 0, 0); // Right.
        Matrix.rotateM(mModel, 0, 270, 0, 1f, 0);
        break;
    }
}

From source file:org.ballerinalang.stdlib.io.data.DataInputOutputTest.java

@Test(description = "Test boolean read/write")
public void testBoolean() throws IOException {
    String filePath = currentDirectoryPath + "/sample.bin";
    ByteChannel byteChannel = TestUtil.openForReadingAndWriting(filePath);
    Channel channel = new MockByteChannel(byteChannel);
    DataChannel dataChannel = new DataChannel(channel, ByteOrder.nativeOrder());
    dataChannel.writeBoolean(false);/*from  w ww . ja v  a  2s  .  co  m*/
    byteChannel = TestUtil.openForReadingAndWriting(filePath);
    channel = new MockByteChannel(byteChannel);
    dataChannel = new DataChannel(channel, ByteOrder.nativeOrder());
    Assert.assertFalse(dataChannel.readBoolean());
}

From source file:com.google.android.apps.body.LayersLoader.java

private void createColorBuffer(DrawGroup drawGroup) {
    int numVertices = drawGroup.vertexBufferData.capacity() / 8; // 3 pos, 3 norm, 2 texcoord

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(numVertices * 2);
    byteBuffer.order(ByteOrder.nativeOrder());
    drawGroup.colorBufferData = byteBuffer.asShortBuffer();

    for (Draw draw : drawGroup.draws) {
        short selectionColor = mMaxColorIndex++;
        mSelectionColorMap.put((int) selectionColor, draw);

        BodyJni.setColorForIndices(drawGroup.colorBufferData, selectionColor, drawGroup.indexBufferData,
                draw.offset, draw.count);
    }//  w  ww  .ja v  a  2  s . c  o m
}

From source file:com.google.android.apps.body.LayersLoader.java

private static ShortBuffer decodeIndexBuffer(DrawGroup drawGroup, char[] data, int start, int length) {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(length * 2);
    byteBuffer.order(ByteOrder.nativeOrder());
    ShortBuffer indexData = byteBuffer.asShortBuffer();
    int prev = 0;
    for (int i = 0; i < length;) {
        int limit = Math.min(length - i, BUFSIZE);
        int s = start + i;
        for (int j = 0; j < limit; ++j) {
            int word = data[s + j];
            prev += (word >> 1) ^ (-(word & 1));
            mBuffer[j] = (short) prev;
        }//w w w. jav  a2  s  .c  o m
        i += limit;
        indexData.put(mBuffer, 0, limit);
    }
    indexData.rewind();
    return indexData;
}

From source file:com.alvermont.terraj.fracplanet.geom.TriangleBufferArray.java

/**
 * Resize this buffer to accomodate a larger number of elements
 *//*from   w  ww. j  a  v  a2  s. co  m*/
protected void resizeBuffer() {
    // we can't resize it so we have to allocate a new one and copy the data across
    final int slots = this.buffer.capacity() / INTS_PER_ENTRY;
    final int newCapacity = this.buffer.capacity()
            + (((slots * CAPACITY_PCT_INCREASE) / PERCENT_100) * INTS_PER_ENTRY);

    final IntBuffer newBuffer = ByteBuffer.allocateDirect(newCapacity * SIZEOF_INT)
            .order(ByteOrder.nativeOrder()).asIntBuffer();

    newBuffer.limit(this.buffer.limit());

    if (log.isDebugEnabled()) {
        log.debug("Resizing triangle buffer capacity to: " + newCapacity + " " + newBuffer.capacity());
    }

    this.buffer.rewind();
    newBuffer.put(this.buffer);

    this.buffer = newBuffer;
}

From source file:org.datavec.image.loader.NativeImageLoader.java

static Mat convert(PIX pix) {
    PIX tempPix = null;//www.  ja va2s .  c  o m
    if (pix.colormap() != null) {
        PIX pix2 = pixRemoveColormap(pix, REMOVE_CMAP_TO_FULL_COLOR);
        tempPix = pix = pix2;
    } else if (pix.d() < 8) {
        PIX pix2 = null;
        switch (pix.d()) {
        case 1:
            pix2 = pixConvert1To8(null, pix, (byte) 0, (byte) 255);
            break;
        case 2:
            pix2 = pixConvert2To8(pix, (byte) 0, (byte) 85, (byte) 170, (byte) 255, 0);
            break;
        case 4:
            pix2 = pixConvert4To8(pix, 0);
            break;
        default:
            assert false;
        }
        tempPix = pix = pix2;
    }
    int height = pix.h();
    int width = pix.w();
    int channels = pix.d() / 8;
    Mat mat = new Mat(height, width, CV_8UC(channels), pix.data(), 4 * pix.wpl());
    Mat mat2 = new Mat(height, width, CV_8UC(channels));
    // swap bytes if needed
    int[] swap = { 0, 3, 1, 2, 2, 1, 3, 0 }, copy = { 0, 0, 1, 1, 2, 2, 3, 3 },
            fromTo = channels > 1 && ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN) ? swap : copy;
    mixChannels(mat, 1, mat2, 1, fromTo, fromTo.length / 2);
    if (tempPix != null) {
        pixDestroy(tempPix);
    }
    return mat2;
}

From source file:ffx.crystal.CCP4MapWriter.java

/**
 * write data to file, does not normalize
 *
 * @param data map data to write out//from  w  w w  . j a v  a  2s .co  m
 * @param norm should the data be normalized by mean/sd?
 */
public void write(double data[], boolean norm) {
    ByteOrder b = ByteOrder.nativeOrder();
    FileOutputStream fos;
    DataOutputStream dos;

    double min = Double.POSITIVE_INFINITY;
    double max = Double.NEGATIVE_INFINITY;
    double mean = 0.0;
    double sd = 0.0;

    int n = 0;
    for (int k = 0; k < extz; k++) {
        for (int j = 0; j < exty; j++) {
            for (int i = 0; i < extx; i++) {
                int index = stride * (i + extx * (j + exty * k));
                // int index = k * (exty * (extx + 2)) + j * (extx + 2) + i;
                n++;
                if (data[index] < min) {
                    min = data[index];
                }
                if (data[index] > max) {
                    max = data[index];
                }
                mean += (data[index] - mean) / n;
            }
        }
    }

    n = 0;
    for (int k = 0; k < extz; k++) {
        for (int j = 0; j < exty; j++) {
            for (int i = 0; i < extx; i++) {
                int index = stride * (i + extx * (j + exty * k));
                // int index = k * (exty * (extx + 2)) + j * (extx + 2) + i;
                sd += pow(data[index] - mean, 2.0);
                n++;
            }
        }
    }
    sd = sqrt(sd / n);

    if (norm) {
        for (int k = 0; k < extz; k++) {
            for (int j = 0; j < exty; j++) {
                for (int i = 0; i < extx; i++) {
                    int index = stride * (i + extx * (j + exty * k));
                    data[index] = (data[index] - mean) / sd;
                }
            }
        }
        // recurse
        write(data, false);
    }

    try {
        if (logger.isLoggable(Level.INFO)) {
            StringBuilder sb = new StringBuilder();
            sb.append(String.format("\nwriting CCP4 map file: \"%s\"\n", filename));
            sb.append(String.format("map min: %g max: %g mean: %g standard dev.: %g", min, max, mean, sd));
            logger.info(sb.toString());
        }

        fos = new FileOutputStream(filename);
        dos = new DataOutputStream(fos);

        byte bytes[] = new byte[2048];
        int offset = 0;

        int imapdata;
        float fmapdata;
        String mapstr;

        // header
        ByteBuffer bb = ByteBuffer.wrap(bytes);
        bb.order(b).putInt(extx);
        bb.order(b).putInt(exty);
        bb.order(b).putInt(extz);

        // mode (2 = reals, only one we accept)
        bb.order(b).putInt(2);

        bb.order(b).putInt(orix);
        bb.order(b).putInt(oriy);
        bb.order(b).putInt(oriz);
        bb.order(b).putInt(nx);
        bb.order(b).putInt(ny);
        bb.order(b).putInt(nz);

        bb.order(b).putFloat((float) crystal.a);
        bb.order(b).putFloat((float) crystal.b);
        bb.order(b).putFloat((float) crystal.c);
        bb.order(b).putFloat((float) crystal.alpha);
        bb.order(b).putFloat((float) crystal.beta);
        bb.order(b).putFloat((float) crystal.gamma);

        bb.order(b).putInt(1);
        bb.order(b).putInt(2);
        bb.order(b).putInt(3);

        bb.order(b).putFloat((float) min);
        bb.order(b).putFloat((float) max);
        bb.order(b).putFloat((float) mean);

        bb.order(b).putInt(crystal.spaceGroup.number);
        // bb.order(b).putInt(1);

        // symmetry bytes - should set this up at some point
        // imapdata = swap ? ByteSwap.swap(320) : 320;
        bb.order(b).putInt(80);

        bb.order(b).putInt(0);

        for (int i = 0; i < 12; i++) {
            bb.order(b).putFloat(0.0f);
        }

        for (int i = 0; i < 15; i++) {
            bb.order(b).putInt(0);
        }
        dos.write(bytes, offset, 208);
        bb.rewind();

        mapstr = "MAP ";
        dos.writeBytes(mapstr);

        // machine code: double, float, int, uchar
        // 0x4441 for LE, 0x1111 for BE
        if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
            imapdata = 0x4441;
        } else {
            imapdata = 0x1111;
        }
        bb.order(b).putInt(imapdata);

        bb.order(b).putFloat((float) sd);

        bb.order(b).putInt(1);
        dos.write(bytes, offset, 12);

        StringBuilder sb = new StringBuilder();
        sb.append("map data from ffx");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dos.writeBytes(sb.toString());

        sb = new StringBuilder();
        while (sb.length() < 80) {
            sb.append(" ");
        }
        for (int i = 0; i < 9; i++) {
            dos.writeBytes(sb.toString());
        }

        sb = new StringBuilder();
        sb.append("x,y,z");
        while (sb.length() < 80) {
            sb.append(" ");
        }
        dos.writeBytes(sb.toString());

        bb.rewind();
        for (int k = 0; k < extz; k++) {
            for (int j = 0; j < exty; j++) {
                for (int i = 0; i < extx; i++) {
                    int index = stride * (i + extx * (j + exty * k));
                    // int index = k * (exty * (extx + 2)) + j * (extx + 2) + i;
                    fmapdata = (float) data[index];
                    bb.order(b).putFloat(fmapdata);
                    if (!bb.hasRemaining()) {
                        dos.write(bytes);
                        bb.rewind();
                    }
                }
            }
        }
        if (bb.position() > 0) {
            dos.write(bytes);
            bb.rewind();
        }

        dos.close();
    } catch (Exception e) {
        String message = "Fatal exception evaluating structure factors.\n";
        logger.log(Level.SEVERE, message, e);
        System.exit(-1);
    }
}