Search in sources :

Example 36 with IntBuffer

use of java.nio.IntBuffer in project processing by processing.

the class PGL method initTexture.

protected void initTexture(int target, int format, int width, int height, int initColor) {
    int[] glcolor = new int[16 * 16];
    Arrays.fill(glcolor, javaToNativeARGB(initColor));
    IntBuffer texels = allocateDirectIntBuffer(16 * 16);
    texels.put(glcolor);
    texels.rewind();
    for (int y = 0; y < height; y += 16) {
        int h = PApplet.min(16, height - y);
        for (int x = 0; x < width; x += 16) {
            int w = PApplet.min(16, width - x);
            texSubImage2D(target, 0, x, y, w, h, format, UNSIGNED_BYTE, texels);
        }
    }
}
Also used : IntBuffer(java.nio.IntBuffer)

Example 37 with IntBuffer

use of java.nio.IntBuffer in project processing by processing.

the class PGL method endRender.

protected void endRender(int windowColor) {
    if (fboLayerEnabled) {
        syncBackTexture();
        // Draw the contents of the back texture to the screen framebuffer.
        bindFramebufferImpl(FRAMEBUFFER, 0);
        if (presentMode) {
            float wa = ((windowColor >> 24) & 0xff) / 255.0f;
            float wr = ((windowColor >> 16) & 0xff) / 255.0f;
            float wg = ((windowColor >> 8) & 0xff) / 255.0f;
            float wb = (windowColor & 0xff) / 255.0f;
            clearDepth(1);
            clearColor(wr, wg, wb, wa);
            clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT);
            if (showStopButton) {
                if (closeButtonTex == null) {
                    closeButtonTex = allocateIntBuffer(1);
                    genTextures(1, closeButtonTex);
                    bindTexture(TEXTURE_2D, closeButtonTex.get(0));
                    texParameteri(TEXTURE_2D, TEXTURE_MIN_FILTER, NEAREST);
                    texParameteri(TEXTURE_2D, TEXTURE_MAG_FILTER, NEAREST);
                    texParameteri(TEXTURE_2D, TEXTURE_WRAP_S, CLAMP_TO_EDGE);
                    texParameteri(TEXTURE_2D, TEXTURE_WRAP_T, CLAMP_TO_EDGE);
                    texImage2D(TEXTURE_2D, 0, RGBA, stopButtonWidth, stopButtonHeight, 0, RGBA, UNSIGNED_BYTE, null);
                    int[] color = new int[closeButtonPix.length];
                    PApplet.arrayCopy(closeButtonPix, color);
                    // Multiply the texture by the button color
                    float ba = ((stopButtonColor >> 24) & 0xFF) / 255f;
                    float br = ((stopButtonColor >> 16) & 0xFF) / 255f;
                    float bg = ((stopButtonColor >> 8) & 0xFF) / 255f;
                    float bb = ((stopButtonColor >> 0) & 0xFF) / 255f;
                    for (int i = 0; i < color.length; i++) {
                        int c = closeButtonPix[i];
                        int a = (int) (ba * ((c >> 24) & 0xFF));
                        int r = (int) (br * ((c >> 16) & 0xFF));
                        int g = (int) (bg * ((c >> 8) & 0xFF));
                        int b = (int) (bb * ((c >> 0) & 0xFF));
                        color[i] = javaToNativeARGB((a << 24) | (r << 16) | (g << 8) | b);
                    }
                    IntBuffer buf = allocateIntBuffer(color);
                    copyToTexture(TEXTURE_2D, RGBA, closeButtonTex.get(0), 0, 0, stopButtonWidth, stopButtonHeight, buf);
                    bindTexture(TEXTURE_2D, 0);
                }
                drawTexture(TEXTURE_2D, closeButtonTex.get(0), stopButtonWidth, stopButtonHeight, 0, 0, stopButtonX + stopButtonWidth, closeButtonY + stopButtonHeight, 0, stopButtonHeight, stopButtonWidth, 0, stopButtonX, closeButtonY, stopButtonX + stopButtonWidth, closeButtonY + stopButtonHeight);
            }
        } else {
            clearDepth(1);
            clearColor(0, 0, 0, 0);
            clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT);
        }
        // Render current back texture to screen, without blending.
        disable(BLEND);
        int x = 0;
        int y = 0;
        if (presentMode) {
            x = (int) presentX;
            y = (int) presentY;
        }
        float scale = getPixelScale();
        drawTexture(TEXTURE_2D, glColorTex.get(backTex), fboWidth, fboHeight, x, y, graphics.width, graphics.height, 0, 0, (int) (scale * graphics.width), (int) (scale * graphics.height), 0, 0, graphics.width, graphics.height);
        // Swapping front and back textures.
        int temp = frontTex;
        frontTex = backTex;
        backTex = temp;
        if (fboLayerDisableReq) {
            fboLayerEnabled = false;
            fboLayerDisableReq = false;
        }
    } else {
        if (SINGLE_BUFFERED && sketch.frameCount == 0) {
            saveFirstFrame();
        }
        if (!clearColor && 0 < sketch.frameCount || !sketch.isLooping()) {
            enableFBOLayer();
            if (SINGLE_BUFFERED) {
                createFBOLayer();
            }
        }
    }
}
Also used : IntBuffer(java.nio.IntBuffer)

Example 38 with IntBuffer

use of java.nio.IntBuffer in project processing by processing.

the class PJOGL method initFBOLayerES.

private void initFBOLayerES() {
    IntBuffer buf = allocateDirectIntBuffer(fboWidth * fboHeight);
    if (hasReadBuffer())
        readBuffer(BACK);
    readPixelsImpl(0, 0, fboWidth, fboHeight, RGBA, UNSIGNED_BYTE, buf);
    bindTexture(TEXTURE_2D, glColorTex.get(frontTex));
    texSubImage2D(TEXTURE_2D, 0, 0, 0, fboWidth, fboHeight, RGBA, UNSIGNED_BYTE, buf);
    bindTexture(TEXTURE_2D, glColorTex.get(backTex));
    texSubImage2D(TEXTURE_2D, 0, 0, 0, fboWidth, fboHeight, RGBA, UNSIGNED_BYTE, buf);
    bindTexture(TEXTURE_2D, 0);
    bindFramebufferImpl(FRAMEBUFFER, 0);
}
Also used : IntBuffer(java.nio.IntBuffer)

Example 39 with IntBuffer

use of java.nio.IntBuffer in project robovm by robovm.

the class Main method storeValues.

static void storeValues(ByteBuffer directBuf) {
    directBuf.position(0);
    ShortBuffer shortBuf = directBuf.asShortBuffer();
    CharBuffer charBuf = directBuf.asCharBuffer();
    IntBuffer intBuf = directBuf.asIntBuffer();
    FloatBuffer floatBuf = directBuf.asFloatBuffer();
    LongBuffer longBuf = directBuf.asLongBuffer();
    DoubleBuffer doubleBuf = directBuf.asDoubleBuffer();
    final byte byteValue = -5;
    final short shortValue = -1234;
    final char charValue = 49200;
    final int intValue = 0x12345678;
    final float floatValue = 3.14159f;
    final long longValue = 0x1122334455667788L;
    final double doubleValue = Double.MIN_VALUE;
    if (directBuf.put(1, byteValue).get(1) != byteValue) {
        throw new RuntimeException("byte get/store failed");
    }
    if (shortBuf.put(1, shortValue).get(1) != shortValue) {
        throw new RuntimeException("short get/store failed");
    }
    if (charBuf.put(2, charValue).get(2) != charValue) {
        throw new RuntimeException("char get/store failed");
    }
    if (intBuf.put(2, intValue).get(2) != intValue) {
        throw new RuntimeException("int get/store failed");
    }
    if (floatBuf.put(3, floatValue).get(3) != floatValue) {
        throw new RuntimeException("float get/store failed");
    }
    if (longBuf.put(2, longValue).get(2) != longValue) {
        throw new RuntimeException("long get/store failed");
    }
    if (doubleBuf.put(3, doubleValue).get(3) != doubleValue) {
        throw new RuntimeException("double get/store failed");
    }
    directBuf.position(0);
    char[] outBuf = new char[directBuf.limit() * 2];
    for (int i = 0; i < directBuf.limit(); i++) {
        byte b = directBuf.get();
        outBuf[i * 2] = hexChar((byte) ((b >> 4) & 0x0f));
        outBuf[i * 2 + 1] = hexChar((byte) (b & 0x0f));
    }
    System.out.println(new String(outBuf));
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) LongBuffer(java.nio.LongBuffer) CharBuffer(java.nio.CharBuffer) FloatBuffer(java.nio.FloatBuffer) IntBuffer(java.nio.IntBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 40 with IntBuffer

use of java.nio.IntBuffer in project robovm by robovm.

the class OldAndroidNIOTest method byteBufferTest.

private void byteBufferTest(ByteBuffer b) {
    checkBuffer(b);
    // Duplicate buffers revert to big-endian.
    b.order(ByteOrder.LITTLE_ENDIAN);
    ByteBuffer dupe = b.duplicate();
    assertEquals(ByteOrder.BIG_ENDIAN, dupe.order());
    b.order(ByteOrder.BIG_ENDIAN);
    // Bounds checks
    try {
        b.put(-1, (byte) 0);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        b.put(b.limit(), (byte) 0);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // IndexOutOfBoundsException: offset < 0
    try {
        byte[] data = new byte[8];
        b.position(0);
        b.put(data, -1, 2);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // IndexOutOfBoundsException: length > array.length - offset
    try {
        byte[] data = new byte[8];
        b.position(0);
        b.put(data, 1, 8);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // BufferOverflowException: length > remaining()
    try {
        byte[] data = new byte[8];
        b.position(b.limit() - 2);
        b.put(data, 0, 3);
        fail("expected exception not thrown");
    } catch (BufferOverflowException e) {
    // expected
    }
    // Fill buffer with bytes A0 A1 A2 A3 ...
    b.position(0);
    for (int i = 0; i < b.capacity(); i++) {
        b.put((byte) (0xA0 + i));
    }
    try {
        b.put((byte) 0xFF);
        fail("expected exception not thrown");
    } catch (BufferOverflowException e) {
    // expected
    }
    b.position(0);
    assertEquals((byte) 0xA7, b.get(7));
    try {
        b.get(12);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        b.get(-10);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    b.position(0);
    b.order(ByteOrder.LITTLE_ENDIAN);
    assertEquals((byte) 0xA0, b.get());
    assertEquals((byte) 0xA1, b.get());
    assertEquals((byte) 0xA2, b.get());
    assertEquals((byte) 0xA3, b.get());
    assertEquals((byte) 0xA4, b.get());
    assertEquals((byte) 0xA5, b.get());
    assertEquals((byte) 0xA6, b.get());
    assertEquals((byte) 0xA7, b.get());
    assertEquals((byte) 0xA8, b.get());
    assertEquals((byte) 0xA9, b.get());
    assertEquals((byte) 0xAA, b.get());
    assertEquals((byte) 0xAB, b.get());
    try {
        b.get();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.position(0);
    b.order(ByteOrder.BIG_ENDIAN);
    assertEquals((byte) 0xA0, b.get());
    assertEquals((byte) 0xA1, b.get());
    assertEquals((byte) 0xA2, b.get());
    assertEquals((byte) 0xA3, b.get());
    assertEquals((byte) 0xA4, b.get());
    assertEquals((byte) 0xA5, b.get());
    assertEquals((byte) 0xA6, b.get());
    assertEquals((byte) 0xA7, b.get());
    assertEquals((byte) 0xA8, b.get());
    assertEquals((byte) 0xA9, b.get());
    assertEquals((byte) 0xAA, b.get());
    assertEquals((byte) 0xAB, b.get());
    try {
        b.get();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.position(6);
    b.limit(10);
    assertEquals((byte) 0xA6, b.get());
    // Check sliced buffer
    b.position(6);
    ByteBuffer bb = b.slice();
    checkBuffer(bb);
    assertEquals(0, bb.position());
    assertEquals(4, bb.limit());
    assertEquals(4, bb.capacity());
    assertEquals((byte) 0xA6, bb.get());
    assertEquals((byte) 0xA7, bb.get());
    assertEquals((byte) 0xA8, bb.get());
    assertEquals((byte) 0xA9, bb.get());
    try {
        bb.get();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    // Reset position and limit
    b.position(0);
    b.limit(b.capacity());
    // Check 'getShort'
    b.order(ByteOrder.LITTLE_ENDIAN);
    b.position(0);
    assertEquals((short) 0xA1A0, b.getShort());
    assertEquals((short) 0xA3A2, b.getShort());
    assertEquals((short) 0xA5A4, b.getShort());
    assertEquals((short) 0xA7A6, b.getShort());
    assertEquals((short) 0xA9A8, b.getShort());
    assertEquals((short) 0xABAA, b.getShort());
    try {
        bb.getShort();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.order(ByteOrder.BIG_ENDIAN);
    b.position(0);
    assertEquals((short) 0xA0A1, b.getShort());
    assertEquals((short) 0xA2A3, b.getShort());
    assertEquals((short) 0xA4A5, b.getShort());
    assertEquals((short) 0xA6A7, b.getShort());
    assertEquals((short) 0xA8A9, b.getShort());
    assertEquals((short) 0xAAAB, b.getShort());
    try {
        bb.getShort();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    // Check 'getInt'
    b.order(ByteOrder.LITTLE_ENDIAN);
    b.position(0);
    assertEquals(0xA3A2A1A0, b.getInt());
    assertEquals(0xA7A6A5A4, b.getInt());
    assertEquals(0xABAAA9A8, b.getInt());
    try {
        bb.getInt();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.order(ByteOrder.BIG_ENDIAN);
    b.position(0);
    assertEquals(0xA0A1A2A3, b.getInt());
    assertEquals(0xA4A5A6A7, b.getInt());
    assertEquals(0xA8A9AAAB, b.getInt());
    try {
        bb.getInt();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    // Check 'getFloat'
    b.order(ByteOrder.LITTLE_ENDIAN);
    b.position(0);
    assertEquals(0xA3A2A1A0, Float.floatToRawIntBits(b.getFloat()));
    assertEquals(0xA7A6A5A4, Float.floatToRawIntBits(b.getFloat()));
    assertEquals(0xABAAA9A8, Float.floatToRawIntBits(b.getFloat()));
    try {
        b.getFloat();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.order(ByteOrder.BIG_ENDIAN);
    b.position(0);
    assertEquals(0xA0A1A2A3, Float.floatToRawIntBits(b.getFloat()));
    assertEquals(0xA4A5A6A7, Float.floatToRawIntBits(b.getFloat()));
    assertEquals(0xA8A9AAAB, Float.floatToRawIntBits(b.getFloat()));
    try {
        b.getFloat();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    // Check 'getDouble(int position)'
    b.order(ByteOrder.LITTLE_ENDIAN);
    assertEquals(0xA7A6A5A4A3A2A1A0L, Double.doubleToRawLongBits(b.getDouble(0)));
    assertEquals(0xA8A7A6A5A4A3A2A1L, Double.doubleToRawLongBits(b.getDouble(1)));
    try {
        b.getDouble(-1);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        b.getDouble(5);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    b.order(ByteOrder.BIG_ENDIAN);
    assertEquals(0xA0A1A2A3A4A5A6A7L, Double.doubleToRawLongBits(b.getDouble(0)));
    assertEquals(0xA1A2A3A4A5A6A7A8L, Double.doubleToRawLongBits(b.getDouble(1)));
    try {
        b.getDouble(-1);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        b.getDouble(5);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // Slice and check 'getInt'
    b.position(1);
    b.limit(5);
    b.order(ByteOrder.LITTLE_ENDIAN);
    bb = b.slice();
    assertEquals(4, bb.capacity());
    assertEquals(ByteOrder.BIG_ENDIAN, bb.order());
    assertEquals(0xA1A2A3A4, bb.getInt(0));
    bb.order(ByteOrder.LITTLE_ENDIAN);
    assertEquals(0xA4A3A2A1, bb.getInt(0));
    bb.order(ByteOrder.LITTLE_ENDIAN);
    ShortBuffer sb = bb.asShortBuffer();
    checkBuffer(sb);
    assertEquals(2, sb.capacity());
    assertEquals((short) 0xA2A1, sb.get());
    assertEquals((short) 0xA4A3, sb.get());
    bb.order(ByteOrder.BIG_ENDIAN);
    sb = bb.asShortBuffer();
    checkBuffer(sb);
    assertEquals(2, sb.capacity());
    assertEquals((short) 0xA1A2, sb.get());
    assertEquals((short) 0xA3A4, sb.get());
    bb.order(ByteOrder.LITTLE_ENDIAN);
    IntBuffer ib = bb.asIntBuffer();
    checkBuffer(ib);
    assertEquals(1, ib.capacity());
    assertEquals(0xA4A3A2A1, ib.get());
    bb.order(ByteOrder.BIG_ENDIAN);
    ib = bb.asIntBuffer();
    checkBuffer(ib);
    assertEquals(1, ib.capacity());
    assertEquals(0xA1A2A3A4, ib.get());
    bb.order(ByteOrder.LITTLE_ENDIAN);
    FloatBuffer fb = bb.asFloatBuffer();
    checkBuffer(fb);
    assertEquals(1, fb.capacity());
    assertEquals(0xA4A3A2A1, Float.floatToRawIntBits(fb.get()));
    bb.order(ByteOrder.BIG_ENDIAN);
    fb = bb.asFloatBuffer();
    checkBuffer(fb);
    assertEquals(1, fb.capacity());
    assertEquals(0xA1A2A3A4, Float.floatToRawIntBits(fb.get()));
}
Also used : IntBuffer(java.nio.IntBuffer) FloatBuffer(java.nio.FloatBuffer) BufferOverflowException(java.nio.BufferOverflowException) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Aggregations

IntBuffer (java.nio.IntBuffer)286 ByteBuffer (java.nio.ByteBuffer)97 FloatBuffer (java.nio.FloatBuffer)47 ShortBuffer (java.nio.ShortBuffer)36 Test (org.junit.Test)35 Bitmap (android.graphics.Bitmap)18 DoubleBuffer (java.nio.DoubleBuffer)18 IOException (java.io.IOException)14 BaseTest (org.apache.jena.atlas.junit.BaseTest)14 FileOutputStream (java.io.FileOutputStream)13 CharBuffer (java.nio.CharBuffer)11 LongBuffer (java.nio.LongBuffer)11 UTF8CodePointDecoder (org.antlr.v4.runtime.UTF8CodePointDecoder)11 ArrayList (java.util.ArrayList)10 File (java.io.File)9 Buffer (java.nio.Buffer)9 VertexBuffer (com.jme3.scene.VertexBuffer)7 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)6 BufferOverflowException (java.nio.BufferOverflowException)6 FileChannel (java.nio.channels.FileChannel)6