Search in sources :

Example 31 with IntBuffer

use of java.nio.IntBuffer in project MagicCamera by wuhaoyu1990.

the class EglSurfaceBase method saveFrame.

/**
     * Saves the EGL surface to a file.
     * <p>
     * Expects that this object's EGL surface is current.
     */
public void saveFrame(File file) throws IOException {
    if (!mEglCore.isCurrent(mEGLSurface)) {
        throw new RuntimeException("Expected EGL context/surface is not current");
    }
    // glReadPixels fills in a "direct" ByteBuffer with what is essentially big-endian RGBA
    // data (i.e. a byte of red, followed by a byte of green...).  While the Bitmap
    // constructor that takes an int[] wants little-endian ARGB (blue/red swapped), the
    // Bitmap "copy pixels" method wants the same format GL provides.
    //
    // Ideally we'd have some way to re-use the ByteBuffer, especially if we're calling
    // here often.
    //
    // Making this even more interesting is the upside-down nature of GL, which means
    // our output will look upside down relative to what appears on screen if the
    // typical GL conventions are used.
    String filename = file.toString();
    int width = getWidth();
    int height = getHeight();
    IntBuffer ib = IntBuffer.allocate(width * height);
    GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ib);
    OpenGlUtils.checkGlError("glReadPixels");
    BufferedOutputStream bos = null;
    try {
        bos = new BufferedOutputStream(new FileOutputStream(filename));
        Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bmp.copyPixelsFromBuffer(IntBuffer.wrap(ib.array()));
        bmp.compress(Bitmap.CompressFormat.PNG, 90, bos);
        bmp.recycle();
    } finally {
        if (bos != null)
            bos.close();
    }
    Log.d(TAG, "Saved " + width + "x" + height + " frame as '" + filename + "'");
}
Also used : Bitmap(android.graphics.Bitmap) IntBuffer(java.nio.IntBuffer) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 32 with IntBuffer

use of java.nio.IntBuffer in project XobotOS by xamarin.

the class MediaItem method generateBlankFrame.

/**
     * Generates a black frame to be used for generating
     * begin transition at first media item in storyboard
     * or end transition at last media item in storyboard
     *
     * @param ClipSettings object
     *{@link android.media.videoeditor.MediaArtistNativeHelper.ClipSettings}
     */
void generateBlankFrame(ClipSettings clipSettings) {
    if (!mBlankFrameGenerated) {
        int mWidth = 64;
        int mHeight = 64;
        mBlankFrameFilename = String.format(mProjectPath + "/" + "ghost.rgb");
        FileOutputStream fl = null;
        try {
            fl = new FileOutputStream(mBlankFrameFilename);
        } catch (IOException e) {
        /* catch IO exception */
        }
        final DataOutputStream dos = new DataOutputStream(fl);
        final int[] framingBuffer = new int[mWidth];
        ByteBuffer byteBuffer = ByteBuffer.allocate(framingBuffer.length * 4);
        IntBuffer intBuffer;
        byte[] array = byteBuffer.array();
        int tmp = 0;
        while (tmp < mHeight) {
            intBuffer = byteBuffer.asIntBuffer();
            intBuffer.put(framingBuffer, 0, mWidth);
            try {
                dos.write(array);
            } catch (IOException e) {
            /* catch file write error */
            }
            tmp += 1;
        }
        try {
            fl.close();
        } catch (IOException e) {
        /* file close error */
        }
        mBlankFrameGenerated = true;
    }
    clipSettings.clipPath = mBlankFrameFilename;
    clipSettings.fileType = FileType.JPG;
    clipSettings.beginCutTime = 0;
    clipSettings.endCutTime = 0;
    clipSettings.mediaRendering = MediaRendering.RESIZING;
    clipSettings.rgbWidth = 64;
    clipSettings.rgbHeight = 64;
}
Also used : DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) IntBuffer(java.nio.IntBuffer) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 33 with IntBuffer

use of java.nio.IntBuffer in project XobotOS by xamarin.

the class OverlayFrame method generateOverlayWithRenderingMode.

void generateOverlayWithRenderingMode(MediaItem mediaItemsList, OverlayFrame overlay, int height, int width) throws FileNotFoundException, IOException {
    final MediaItem t = mediaItemsList;
    /* get the rendering mode */
    int renderMode = t.getRenderingMode();
    Bitmap overlayBitmap = ((OverlayFrame) overlay).getBitmap();
    /*
         * Check if the resize of Overlay is needed with rendering mode applied
         * because of change in export dimensions
         */
    int resizedRGBFileHeight = ((OverlayFrame) overlay).getResizedRGBSizeHeight();
    int resizedRGBFileWidth = ((OverlayFrame) overlay).getResizedRGBSizeWidth();
    /* Get original bitmap width if it is not resized */
    if (resizedRGBFileWidth == 0) {
        resizedRGBFileWidth = overlayBitmap.getWidth();
    }
    /* Get original bitmap height if it is not resized */
    if (resizedRGBFileHeight == 0) {
        resizedRGBFileHeight = overlayBitmap.getHeight();
    }
    if (resizedRGBFileWidth != width || resizedRGBFileHeight != height || (!(new File(((OverlayFrame) overlay).getFilename()).exists()))) {
        /*
             *  Create the canvas bitmap
             */
        final Bitmap destBitmap = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);
        final Canvas overlayCanvas = new Canvas(destBitmap);
        final Rect destRect;
        final Rect srcRect;
        switch(renderMode) {
            case MediaItem.RENDERING_MODE_STRETCH:
                {
                    destRect = new Rect(0, 0, overlayCanvas.getWidth(), overlayCanvas.getHeight());
                    srcRect = new Rect(0, 0, overlayBitmap.getWidth(), overlayBitmap.getHeight());
                    break;
                }
            case MediaItem.RENDERING_MODE_BLACK_BORDER:
                {
                    int left, right, top, bottom;
                    float aROverlayImage, aRCanvas;
                    aROverlayImage = (float) (overlayBitmap.getWidth()) / (float) (overlayBitmap.getHeight());
                    aRCanvas = (float) (overlayCanvas.getWidth()) / (float) (overlayCanvas.getHeight());
                    if (aROverlayImage > aRCanvas) {
                        int newHeight = ((overlayCanvas.getWidth() * overlayBitmap.getHeight()) / overlayBitmap.getWidth());
                        left = 0;
                        top = (overlayCanvas.getHeight() - newHeight) / 2;
                        right = overlayCanvas.getWidth();
                        bottom = top + newHeight;
                    } else {
                        int newWidth = ((overlayCanvas.getHeight() * overlayBitmap.getWidth()) / overlayBitmap.getHeight());
                        left = (overlayCanvas.getWidth() - newWidth) / 2;
                        top = 0;
                        right = left + newWidth;
                        bottom = overlayCanvas.getHeight();
                    }
                    destRect = new Rect(left, top, right, bottom);
                    srcRect = new Rect(0, 0, overlayBitmap.getWidth(), overlayBitmap.getHeight());
                    break;
                }
            case MediaItem.RENDERING_MODE_CROPPING:
                {
                    // Calculate the source rect
                    int left, right, top, bottom;
                    float aROverlayImage, aRCanvas;
                    aROverlayImage = (float) (overlayBitmap.getWidth()) / (float) (overlayBitmap.getHeight());
                    aRCanvas = (float) (overlayCanvas.getWidth()) / (float) (overlayCanvas.getHeight());
                    if (aROverlayImage < aRCanvas) {
                        int newHeight = ((overlayBitmap.getWidth() * overlayCanvas.getHeight()) / overlayCanvas.getWidth());
                        left = 0;
                        top = (overlayBitmap.getHeight() - newHeight) / 2;
                        right = overlayBitmap.getWidth();
                        bottom = top + newHeight;
                    } else {
                        int newWidth = ((overlayBitmap.getHeight() * overlayCanvas.getWidth()) / overlayCanvas.getHeight());
                        left = (overlayBitmap.getWidth() - newWidth) / 2;
                        top = 0;
                        right = left + newWidth;
                        bottom = overlayBitmap.getHeight();
                    }
                    srcRect = new Rect(left, top, right, bottom);
                    destRect = new Rect(0, 0, overlayCanvas.getWidth(), overlayCanvas.getHeight());
                    break;
                }
            default:
                {
                    throw new IllegalStateException("Rendering mode: " + renderMode);
                }
        }
        overlayCanvas.drawBitmap(overlayBitmap, srcRect, destRect, sResizePaint);
        overlayCanvas.setBitmap(null);
        /*
             * Write to the dest file
             */
        String outFileName = ((OverlayFrame) overlay).getFilename();
        /*
             * Save the image to same rgb file
             */
        if (outFileName != null) {
            new File(outFileName).delete();
        }
        final FileOutputStream fl = new FileOutputStream(outFileName);
        final DataOutputStream dos = new DataOutputStream(fl);
        /*
             * Populate the rgb file with bitmap data
             */
        final int[] framingBuffer = new int[width];
        ByteBuffer byteBuffer = ByteBuffer.allocate(framingBuffer.length * 4);
        IntBuffer intBuffer;
        byte[] array = byteBuffer.array();
        int tmp = 0;
        while (tmp < height) {
            destBitmap.getPixels(framingBuffer, 0, width, 0, tmp, width, 1);
            intBuffer = byteBuffer.asIntBuffer();
            intBuffer.put(framingBuffer, 0, width);
            dos.write(array);
            tmp += 1;
        }
        fl.flush();
        fl.close();
        /*
             * Set the resized RGB width and height
             */
        ((OverlayFrame) overlay).setResizedRGBSize(width, height);
    }
}
Also used : Rect(android.graphics.Rect) DataOutputStream(java.io.DataOutputStream) Canvas(android.graphics.Canvas) ByteBuffer(java.nio.ByteBuffer) Paint(android.graphics.Paint) Bitmap(android.graphics.Bitmap) FileOutputStream(java.io.FileOutputStream) IntBuffer(java.nio.IntBuffer) File(java.io.File)

Example 34 with IntBuffer

use of java.nio.IntBuffer in project presto by prestodb.

the class TestUnscaledDecimal128Arithmetic method toBigInteger.

private static BigInteger toBigInteger(int[] data) {
    byte[] array = new byte[data.length * 4];
    ByteBuffer byteBuffer = ByteBuffer.wrap(array);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    IntBuffer intBuffer = byteBuffer.asIntBuffer();
    intBuffer.put(data);
    Collections.reverse(Bytes.asList(array));
    array[0] &= ~(1 << 7);
    return new BigInteger((array[0] & (1 << 7)) > 0 ? -1 : 1, array);
}
Also used : IntBuffer(java.nio.IntBuffer) BigInteger(java.math.BigInteger) UnscaledDecimal128Arithmetic.unscaledDecimalToBigInteger(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimalToBigInteger) ByteBuffer(java.nio.ByteBuffer)

Example 35 with IntBuffer

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

the class PGL method restoreFirstFrame.

protected void restoreFirstFrame() {
    if (firstFrame == null)
        return;
    IntBuffer tex = allocateIntBuffer(1);
    genTextures(1, tex);
    int w, h;
    float scale = getPixelScale();
    if (hasNpotTexSupport()) {
        w = (int) (scale * graphics.width);
        h = (int) (scale * graphics.height);
    } else {
        w = nextPowerOfTwo((int) (scale * graphics.width));
        h = nextPowerOfTwo((int) (scale * graphics.height));
    }
    bindTexture(TEXTURE_2D, tex.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, w, h, 0, RGBA, UNSIGNED_BYTE, null);
    texSubImage2D(TEXTURE_2D, 0, 0, 0, graphics.width, graphics.height, RGBA, UNSIGNED_BYTE, firstFrame);
    drawTexture(TEXTURE_2D, tex.get(0), w, h, 0, 0, graphics.width, graphics.height, 0, 0, (int) (scale * graphics.width), (int) (scale * graphics.height), 0, 0, graphics.width, graphics.height);
    deleteTextures(1, tex);
    firstFrame.clear();
    firstFrame = null;
}
Also used : IntBuffer(java.nio.IntBuffer)

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