FileDocCategorySizeDatePackage
NativeFrame.javaAPI DocAndroid 5.1 API9659Thu Mar 12 22:22:30 GMT 2015android.filterfw.core

NativeFrame

public class NativeFrame extends android.filterfw.core.Frame
hide

Fields Summary
private int
nativeFrameId
Constructors Summary
NativeFrame(android.filterfw.core.FrameFormat format, android.filterfw.core.FrameManager frameManager)


        
        super(format, frameManager);
        int capacity = format.getSize();
        nativeAllocate(capacity);
        setReusable(capacity != 0);
    
Methods Summary
public android.graphics.BitmapgetBitmap()

        if (getFormat().getNumberOfDimensions() != 2) {
            throw new RuntimeException("Attempting to get Bitmap for non 2-dimensional native frame!");
        }
        Bitmap result = Bitmap.createBitmap(getFormat().getWidth(),
                                            getFormat().getHeight(),
                                            Bitmap.Config.ARGB_8888);
        int byteCount = result.getByteCount();
        int bps = getFormat().getBytesPerSample();
        if (!getNativeBitmap(result, byteCount, bps)) {
            throw new RuntimeException("Could not get bitmap data from native frame!");
        }
        return result;
    
public intgetCapacity()

        return getNativeCapacity();
    
public java.nio.ByteBuffergetData()

        byte[] data = getNativeData(getFormat().getSize());
        return data == null ? null : ByteBuffer.wrap(data);
    
public float[]getFloats()

        return getNativeFloats(getFormat().getSize());
    
public int[]getInts()

        return getNativeInts(getFormat().getSize());
    
private native booleangetNativeBitmap(android.graphics.Bitmap bitmap, int size, int bytesPerSample)

private native booleangetNativeBuffer(android.filterfw.core.NativeBuffer buffer)

private native intgetNativeCapacity()

private native byte[]getNativeData(int byteCount)

private native float[]getNativeFloats(int byteCount)

private native int[]getNativeInts(int byteCount)

public java.lang.ObjectgetObjectValue()
Returns the native frame's Object value. If the frame's base-type is not TYPE_OBJECT, this returns a data buffer containing the native data (this is equivalent to calling getData(). If the frame is based on an object type, this type is expected to be a subclass of NativeBuffer. The NativeBuffer returned is only valid for as long as the frame is alive. If you need to hold on to the returned value, you must retain it.

        // If this is not a structured frame, return our data
        if (getFormat().getBaseType() != FrameFormat.TYPE_OBJECT) {
            return getData();
        }

        // Get the structure class
        Class structClass = getFormat().getObjectClass();
        if (structClass == null) {
            throw new RuntimeException("Attempting to get object data from frame that does " +
                                       "not specify a structure object class!");
        }

        // Make sure it is a NativeBuffer subclass
        if (!NativeBuffer.class.isAssignableFrom(structClass)) {
            throw new RuntimeException("NativeFrame object class must be a subclass of " +
                                       "NativeBuffer!");
        }

        // Instantiate a new empty instance of this class
        NativeBuffer structData = null;
        try {
          structData = (NativeBuffer)structClass.newInstance();
        } catch (Exception e) {
          throw new RuntimeException("Could not instantiate new structure instance of type '" +
                                     structClass + "'!");
        }

        // Wrap it around our data
        if (!getNativeBuffer(structData)) {
            throw new RuntimeException("Could not get the native structured data for frame!");
        }

        // Attach this frame to it
        structData.attachToFrame(this);

        return structData;
    
protected synchronized booleanhasNativeAllocation()

        return nativeFrameId != -1;
    
private native booleannativeAllocate(int capacity)

private native booleannativeCopyFromGL(android.filterfw.core.GLFrame frame)

private native booleannativeCopyFromNative(android.filterfw.core.NativeFrame frame)

private native booleannativeDeallocate()

private static native intnativeFloatSize()

private static native intnativeIntSize()

protected synchronized voidreleaseNativeAllocation()

        nativeDeallocate();
        nativeFrameId = -1;
    
public voidsetBitmap(android.graphics.Bitmap bitmap)

        assertFrameMutable();
        if (getFormat().getNumberOfDimensions() != 2) {
            throw new RuntimeException("Attempting to set Bitmap for non 2-dimensional native frame!");
        } else if (getFormat().getWidth()  != bitmap.getWidth() ||
                   getFormat().getHeight() != bitmap.getHeight()) {
            throw new RuntimeException("Bitmap dimensions do not match native frame dimensions!");
        } else {
            Bitmap rgbaBitmap = convertBitmapToRGBA(bitmap);
            int byteCount = rgbaBitmap.getByteCount();
            int bps = getFormat().getBytesPerSample();
            if (!setNativeBitmap(rgbaBitmap, byteCount, bps)) {
                throw new RuntimeException("Could not set native frame bitmap data!");
            }
        }
    
public voidsetData(java.nio.ByteBuffer buffer, int offset, int length)

        assertFrameMutable();
        byte[] bytes = buffer.array();
        if ((length + offset) > buffer.limit()) {
            throw new RuntimeException("Offset and length exceed buffer size in native setData: " +
                                       (length + offset) + " bytes given, but only " + buffer.limit() +
                                       " bytes available!");
        } else if (getFormat().getSize() != length) {
            throw new RuntimeException("Data size in setData does not match native frame size: " +
                                       "Frame size is " + getFormat().getSize() + " bytes, but " +
                                       length + " bytes given!");
        } else if (!setNativeData(bytes, offset, length)) {
            throw new RuntimeException("Could not set native frame data!");
        }
    
public voidsetDataFromFrame(android.filterfw.core.Frame frame)

        // Make sure frame fits
        if (getFormat().getSize() < frame.getFormat().getSize()) {
            throw new RuntimeException(
                "Attempting to assign frame of size " + frame.getFormat().getSize() + " to " +
                "smaller native frame of size " + getFormat().getSize() + "!");
        }

        // Invoke optimized implementations if possible
        if (frame instanceof NativeFrame) {
            nativeCopyFromNative((NativeFrame)frame);
        } else if (frame instanceof GLFrame) {
            nativeCopyFromGL((GLFrame)frame);
        } else if (frame instanceof SimpleFrame) {
            setObjectValue(frame.getObjectValue());
        } else {
            super.setDataFromFrame(frame);
        }
    
public voidsetFloats(float[] floats)

        assertFrameMutable();
        if (floats.length * nativeFloatSize() > getFormat().getSize()) {
            throw new RuntimeException(
                "NativeFrame cannot hold " + floats.length + " floats. (Can only hold " +
                (getFormat().getSize() / nativeFloatSize()) + " floats).");
        } else if (!setNativeFloats(floats)) {
            throw new RuntimeException("Could not set int values for native frame!");
        }
    
public voidsetInts(int[] ints)

        assertFrameMutable();
        if (ints.length * nativeIntSize() > getFormat().getSize()) {
            throw new RuntimeException(
                "NativeFrame cannot hold " + ints.length + " integers. (Can only hold " +
                (getFormat().getSize() / nativeIntSize()) + " integers).");
        } else if (!setNativeInts(ints)) {
            throw new RuntimeException("Could not set int values for native frame!");
        }
    
private native booleansetNativeBitmap(android.graphics.Bitmap bitmap, int size, int bytesPerSample)

private native booleansetNativeData(byte[] data, int offset, int length)

private native booleansetNativeFloats(float[] floats)

private native booleansetNativeInts(int[] ints)

public java.lang.StringtoString()

        return "NativeFrame id: " + nativeFrameId + " (" + getFormat() + ") of size "
            + getCapacity();