Search in sources :

Example 16 with IntBuffer

use of java.nio.IntBuffer in project gl-react-native by ProjectSeptemberInc.

the class GLCanvas method parseAsIntArray.

private IntBuffer parseAsIntArray(ReadableArray array) {
    int size = array.size();
    IntBuffer buf = ByteBuffer.allocateDirect(size * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
    for (int i = 0; i < size; i++) buf.put(array.getInt(i));
    buf.position(0);
    return buf;
}
Also used : IntBuffer(java.nio.IntBuffer)

Example 17 with IntBuffer

use of java.nio.IntBuffer in project gl-react-native by ProjectSeptemberInc.

the class GLCanvas method recSyncData.

private GLRenderData recSyncData(GLData data, HashMap<Uri, GLImage> images) {
    Map<Uri, GLImage> prevImages = this.images;
    GLShader shader = getShader(data.shader);
    if (shader == null || !shader.ensureCompile())
        return null;
    Map<String, Integer> uniformsInteger = new HashMap<>();
    Map<String, Float> uniformsFloat = new HashMap<>();
    Map<String, IntBuffer> uniformsIntBuffer = new HashMap<>();
    Map<String, FloatBuffer> uniformsFloatBuffer = new HashMap<>();
    Map<String, GLTexture> textures = new HashMap<>();
    List<GLRenderData> contextChildren = new ArrayList<>();
    List<GLRenderData> children = new ArrayList<>();
    for (GLData child : data.contextChildren) {
        GLRenderData node = recSyncData(child, images);
        if (node == null)
            return null;
        contextChildren.add(node);
    }
    for (GLData child : data.children) {
        GLRenderData node = recSyncData(child, images);
        if (node == null)
            return null;
        children.add(node);
    }
    Map<String, Integer> uniformTypes = shader.getUniformTypes();
    List<String> uniformNames = shader.getUniformNames();
    Map<String, Integer> uniformSizes = shader.getUniformSizes();
    int units = 0;
    ReadableMapKeySetIterator iterator = data.uniforms.keySetIterator();
    while (iterator.hasNextKey()) {
        String uniformName = iterator.nextKey();
        int type = uniformTypes.get(uniformName);
        int size = uniformSizes.get(uniformName);
        ReadableMap dataUniforms = data.uniforms;
        if (type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE) {
            uniformsInteger.put(uniformName, units++);
            if (dataUniforms.isNull(uniformName)) {
                GLTexture emptyTexture = new GLTexture(this);
                emptyTexture.setPixelsEmpty();
                textures.put(uniformName, emptyTexture);
            } else {
                ReadableMap value = null;
                try {
                    value = dataUniforms.getMap(uniformName);
                } catch (Exception e) {
                    shader.runtimeException("texture uniform '" + uniformName + "': you cannot directly give require('./img.png') " + "to gl-react, use resolveAssetSource(require('./img.png')) instead.");
                    return null;
                }
                String t = value.getString("type");
                if (t.equals("content")) {
                    int id = value.getInt("id");
                    if (id >= contentTextures.size()) {
                        resizeUniformContentTextures(id + 1);
                    }
                    textures.put(uniformName, contentTextures.get(id));
                } else if (t.equals("fbo")) {
                    int id = value.getInt("id");
                    GLFBO fbo = getFBO(id);
                    textures.put(uniformName, fbo.color.get(0));
                } else if (t.equals("uri")) {
                    final Uri src = srcResource(value);
                    if (src == null) {
                        shader.runtimeException("texture uniform '" + uniformName + "': Invalid uri format '" + value + "'");
                    }
                    GLImage image = images.get(src);
                    if (image == null) {
                        image = prevImages.get(src);
                        if (image != null)
                            images.put(src, image);
                    }
                    if (image == null) {
                        image = new GLImage(this, executorSupplier.forDecode(), new Runnable() {

                            public void run() {
                                onImageLoad(src);
                            }
                        });
                        image.setSrc(src);
                        images.put(src, image);
                    }
                    textures.put(uniformName, image.getTexture());
                } else {
                    shader.runtimeException("texture uniform '" + uniformName + "': Unexpected type '" + type + "'");
                }
            }
        } else {
            if (size == 1) {
                switch(type) {
                    case GL_INT:
                        uniformsInteger.put(uniformName, dataUniforms.getInt(uniformName));
                        break;
                    case GL_BOOL:
                        uniformsInteger.put(uniformName, dataUniforms.getBoolean(uniformName) ? 1 : 0);
                        break;
                    case GL_FLOAT:
                        uniformsFloat.put(uniformName, (float) dataUniforms.getDouble(uniformName));
                        break;
                    case GL_FLOAT_VEC2:
                    case GL_FLOAT_VEC3:
                    case GL_FLOAT_VEC4:
                    case GL_FLOAT_MAT2:
                    case GL_FLOAT_MAT3:
                    case GL_FLOAT_MAT4:
                        ReadableArray arr = dataUniforms.getArray(uniformName);
                        if (arraySizeForType(type) != arr.size()) {
                            shader.runtimeException("uniform '" + uniformName + "': Invalid array size: " + arr.size() + ". Expected: " + arraySizeForType(type));
                        }
                        uniformsFloatBuffer.put(uniformName, parseAsFloatArray(arr));
                        break;
                    case GL_INT_VEC2:
                    case GL_INT_VEC3:
                    case GL_INT_VEC4:
                    case GL_BOOL_VEC2:
                    case GL_BOOL_VEC3:
                    case GL_BOOL_VEC4:
                        ReadableArray arr2 = dataUniforms.getArray(uniformName);
                        if (arraySizeForType(type) != arr2.size()) {
                            shader.runtimeException("uniform '" + uniformName + "': Invalid array size: " + arr2.size() + ". Expected: " + arraySizeForType(type));
                        }
                        uniformsIntBuffer.put(uniformName, parseAsIntArray(arr2));
                        break;
                    default:
                        shader.runtimeException("uniform '" + uniformName + "': type not supported: " + type);
                }
            } else {
                ReadableArray array = dataUniforms.getArray(uniformName);
                if (size != array.size()) {
                    shader.runtimeException("uniform '" + uniformName + "': Invalid array size: " + array.size() + ". Expected: " + size);
                }
                for (int i = 0; i < size; i++) {
                    String name = uniformName + "[" + i + "]";
                    switch(type) {
                        case GL_INT:
                            uniformsInteger.put(name, array.getInt(i));
                            break;
                        case GL_BOOL:
                            uniformsInteger.put(name, array.getBoolean(i) ? 1 : 0);
                            break;
                        case GL_FLOAT:
                            uniformsFloat.put(name, (float) array.getDouble(i));
                            break;
                        case GL_FLOAT_VEC2:
                        case GL_FLOAT_VEC3:
                        case GL_FLOAT_VEC4:
                        case GL_FLOAT_MAT2:
                        case GL_FLOAT_MAT3:
                        case GL_FLOAT_MAT4:
                            ReadableArray arr = array.getArray(i);
                            if (arraySizeForType(type) != arr.size()) {
                                shader.runtimeException("uniform '" + name + "': Invalid array size: " + arr.size() + ". Expected: " + arraySizeForType(type));
                            }
                            uniformsFloatBuffer.put(name, parseAsFloatArray(arr));
                            break;
                        case GL_INT_VEC2:
                        case GL_INT_VEC3:
                        case GL_INT_VEC4:
                        case GL_BOOL_VEC2:
                        case GL_BOOL_VEC3:
                        case GL_BOOL_VEC4:
                            ReadableArray arr2 = array.getArray(i);
                            if (arraySizeForType(type) != arr2.size()) {
                                shader.runtimeException("uniform '" + name + "': Invalid array size: " + arr2.size() + ". Expected: " + arraySizeForType(type));
                            }
                            uniformsIntBuffer.put(name, parseAsIntArray(arr2));
                            break;
                        default:
                            shader.runtimeException("uniform '" + name + "': type not supported: " + type);
                    }
                }
            }
        }
    }
    int[] maxTextureUnits = new int[1];
    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, maxTextureUnits, 0);
    if (units > maxTextureUnits[0]) {
        shader.runtimeException("Maximum number of texture reach. got " + units + " >= max " + maxTextureUnits);
    }
    for (String uniformName : uniformNames) {
        int size = uniformSizes.get(uniformName);
        if (size == 1) {
            if (!uniformsFloat.containsKey(uniformName) && !uniformsInteger.containsKey(uniformName) && !uniformsFloatBuffer.containsKey(uniformName) && !uniformsIntBuffer.containsKey(uniformName)) {
                shader.runtimeException("All defined uniforms must be provided. Missing '" + uniformName + "'");
            }
        } else {
            for (int i = 0; i < size; i++) {
                String name = uniformName + "[" + i + "]";
                if (!uniformsFloat.containsKey(name) && !uniformsInteger.containsKey(name) && !uniformsFloatBuffer.containsKey(name) && !uniformsIntBuffer.containsKey(name)) {
                    shader.runtimeException("All defined uniforms must be provided. Missing '" + name + "'");
                }
            }
        }
    }
    return new GLRenderData(shader, uniformsInteger, uniformsFloat, uniformsIntBuffer, uniformsFloatBuffer, textures, (int) (data.width * data.pixelRatio), (int) (data.height * data.pixelRatio), data.fboId, contextChildren, children);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FloatBuffer(java.nio.FloatBuffer) Uri(android.net.Uri) ReadableMap(com.facebook.react.bridge.ReadableMap) ReadableArray(com.facebook.react.bridge.ReadableArray) GLException(android.opengl.GLException) ReadableMapKeySetIterator(com.facebook.react.bridge.ReadableMapKeySetIterator) IntBuffer(java.nio.IntBuffer)

Example 18 with IntBuffer

use of java.nio.IntBuffer in project LogisticsPipes by RS485.

the class RequestMonitorPopup method saveTreeToImage.

private void saveTreeToImage() {
    int useWidth = mc.displayWidth;
    int useHeight = mc.displayHeight;
    int left = minX - (width / 2);
    int top = minY;
    int right = maxX - (width / 2);
    int bottom = maxY;
    int k = useWidth * useHeight;
    IntBuffer pixels = BufferUtils.createIntBuffer(k);
    int[] intArray = new int[k];
    int imgPosX = 0;
    int imgPosY = 0;
    for (int x = left; x < right + width; x += width) {
        imgPosY = 0;
        for (int y = top; y < bottom + height; y += height) {
            imgPosY += useHeight;
        }
        imgPosX += useWidth;
    }
    BufferedImage bufferedimage = new BufferedImage(imgPosX, imgPosY, 1);
    imgPosX = 0;
    imgPosY = 0;
    //Clear everything
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glLoadIdentity();
    mc.entityRenderer.setupOverlayRendering();
    drawForSreenShot(0, 0);
    //Start Creating the Image
    for (int x = left; x < right + width; x += width) {
        imgPosY = 0;
        for (int y = top; y < bottom + height; y += height) {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            mc.entityRenderer.setupOverlayRendering();
            drawForSreenShot(y, x);
            pixels.clear();
            GL11.glReadPixels(0, 0, useWidth, useHeight, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
            pixels.get(intArray);
            RequestMonitorPopup.mirror(intArray, useWidth, useHeight);
            bufferedimage.setRGB(imgPosX, imgPosY, Math.min(useWidth, bufferedimage.getWidth() - imgPosX), Math.min(useHeight, bufferedimage.getHeight() - imgPosY), intArray, 0, useWidth);
            imgPosY += useHeight;
        }
        imgPosX += useWidth;
    }
    saveImage(bufferedimage);
}
Also used : IntBuffer(java.nio.IntBuffer) BufferedImage(java.awt.image.BufferedImage)

Example 19 with IntBuffer

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

the class ScreenGrab method getPixelsFromBuffer.

/**
	 * Grabs the pixels from the buffer
	 * 
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @return
	 */
public static Bitmap getPixelsFromBuffer(int x, int y, int width, int height) {
    int[] b = new int[width * (y + height)];
    int[] bt = new int[width * height];
    IntBuffer ib = IntBuffer.wrap(b);
    ib.position(0);
    GLES20.glReadPixels(x, y, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ib);
    for (int i = 0, k = 0; i < height; i++, k++) {
        for (int j = 0; j < width; j++) {
            int pix = b[i * width + j];
            int pb = (pix >> 16) & 0xff;
            int pr = (pix << 16) & 0x00ff0000;
            int pix1 = (pix & 0xff00ff00) | pr | pb;
            bt[(height - k - 1) * width + j] = pix1;
        }
    }
    return Bitmap.createBitmap(bt, width, height, Bitmap.Config.ARGB_8888);
}
Also used : IntBuffer(java.nio.IntBuffer)

Example 20 with IntBuffer

use of java.nio.IntBuffer in project StickerCamera by Skykai521.

the class GPUImageView method capture.

/**
     * Capture the current image with the size as it is displayed and retrieve it as Bitmap.
     * @return current output as Bitmap
     * @throws InterruptedException
     */
public Bitmap capture() throws InterruptedException {
    final Semaphore waiter = new Semaphore(0);
    final int width = mGLSurfaceView.getMeasuredWidth();
    final int height = mGLSurfaceView.getMeasuredHeight();
    // Take picture on OpenGL thread
    final int[] pixelMirroredArray = new int[width * height];
    mGPUImage.runOnGLThread(new Runnable() {

        @Override
        public void run() {
            final IntBuffer pixelBuffer = IntBuffer.allocate(width * height);
            GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixelBuffer);
            int[] pixelArray = pixelBuffer.array();
            // Convert upside down mirror-reversed image to right-side up normal image.
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    pixelMirroredArray[(height - i - 1) * width + j] = pixelArray[i * width + j];
                }
            }
            waiter.release();
        }
    });
    requestRender();
    waiter.acquire();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.copyPixelsFromBuffer(IntBuffer.wrap(pixelMirroredArray));
    return bitmap;
}
Also used : Bitmap(android.graphics.Bitmap) IntBuffer(java.nio.IntBuffer) Semaphore(java.util.concurrent.Semaphore)

Aggregations

IntBuffer (java.nio.IntBuffer)283 ByteBuffer (java.nio.ByteBuffer)95 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