Search in sources :

Example 1 with Indexer

use of org.bytedeco.javacpp.indexer.Indexer in project nd4j by deeplearning4j.

the class DoubleDataBufferTest method testPointerCreation.

@Test
public void testPointerCreation() {
    DoublePointer floatPointer = new DoublePointer(1, 2, 3, 4);
    Indexer indexer = DoubleIndexer.create(floatPointer);
    DataBuffer buffer = Nd4j.createBuffer(floatPointer, DataBuffer.Type.DOUBLE, 4, indexer);
    DataBuffer other = Nd4j.createBuffer(new double[] { 1, 2, 3, 4 });
    assertArrayEquals(other.asDouble(), buffer.asDouble(), 0.001);
}
Also used : Indexer(org.bytedeco.javacpp.indexer.Indexer) DoubleIndexer(org.bytedeco.javacpp.indexer.DoubleIndexer) DoublePointer(org.bytedeco.javacpp.DoublePointer) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 2 with Indexer

use of org.bytedeco.javacpp.indexer.Indexer in project nd4j by deeplearning4j.

the class FloatDataBufferTest method testPointerCreation.

@Test
public void testPointerCreation() {
    FloatPointer floatPointer = new FloatPointer(1, 2, 3, 4);
    Indexer indexer = FloatIndexer.create(floatPointer);
    DataBuffer buffer = Nd4j.createBuffer(floatPointer, DataBuffer.Type.FLOAT, 4, indexer);
    DataBuffer other = Nd4j.createBuffer(new float[] { 1, 2, 3, 4 });
    assertArrayEquals(other.asFloat(), buffer.asFloat(), 0.001f);
}
Also used : FloatIndexer(org.bytedeco.javacpp.indexer.FloatIndexer) Indexer(org.bytedeco.javacpp.indexer.Indexer) FloatPointer(org.bytedeco.javacpp.FloatPointer) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 3 with Indexer

use of org.bytedeco.javacpp.indexer.Indexer in project javacv by bytedeco.

the class FrameConverterTest method testJava2DFrameConverter.

@Test
public void testJava2DFrameConverter() {
    System.out.println("Java2DFrameConverter");
    int[] depths = { Frame.DEPTH_UBYTE, Frame.DEPTH_SHORT, Frame.DEPTH_FLOAT };
    int[] channels = { 1, 3, 4 };
    for (int i = 0; i < depths.length; i++) {
        for (int j = 0; j < channels.length; j++) {
            Frame frame = new Frame(640 + 1, 480, depths[i], channels[j]);
            Java2DFrameConverter converter = new Java2DFrameConverter();
            Indexer frameIdx = frame.createIndexer();
            for (int y = 0; y < frameIdx.rows(); y++) {
                for (int x = 0; x < frameIdx.cols(); x++) {
                    for (int z = 0; z < frameIdx.channels(); z++) {
                        frameIdx.putDouble(new long[] { y, x, z }, y + x + z);
                    }
                }
            }
            BufferedImage image = converter.convert(frame);
            converter.frame = null;
            Frame frame2 = converter.convert(image);
            Indexer frame2Idx = frame2.createIndexer();
            for (int y = 0; y < frameIdx.rows(); y++) {
                for (int x = 0; x < frameIdx.cols(); x++) {
                    for (int z = 0; z < frameIdx.channels(); z++) {
                        double value = frameIdx.getDouble(y, x, z);
                        assertEquals(value, frame2Idx.getDouble(y, x, z), 0);
                    }
                }
            }
            try {
                frame2Idx.getDouble(frameIdx.rows() + 1, frameIdx.cols() + 1);
                fail("IndexOutOfBoundsException should have been thrown.");
            } catch (IndexOutOfBoundsException e) {
            }
            frameIdx.release();
            frame2Idx.release();
            converter.close();
            frame.close();
        }
    }
    int[] types = { BufferedImage.TYPE_INT_RGB, BufferedImage.TYPE_INT_ARGB, BufferedImage.TYPE_INT_ARGB_PRE, BufferedImage.TYPE_INT_BGR };
    for (int i = 0; i < types.length; i++) {
        BufferedImage image = new BufferedImage(640 + 1, 480, types[i]);
        Java2DFrameConverter converter = new Java2DFrameConverter();
        WritableRaster raster = image.getRaster();
        int[] array = ((DataBufferInt) raster.getDataBuffer()).getData();
        for (int j = 0; j < array.length; j++) {
            array[j] = j;
        }
        Frame frame = converter.convert(image);
        converter.bufferedImage = null;
        BufferedImage image2 = converter.convert(frame);
        WritableRaster raster2 = image2.getRaster();
        byte[] array2 = ((DataBufferByte) raster2.getDataBuffer()).getData();
        for (int j = 0; j < array.length; j++) {
            int n = ((array2[4 * j] & 0xFF) << 24) | ((array2[4 * j + 1] & 0xFF) << 16) | ((array2[4 * j + 2] & 0xFF) << 8) | (array2[4 * j + 3] & 0xFF);
            assertEquals(array[j], n);
        }
        converter.close();
    }
}
Also used : UByteIndexer(org.bytedeco.javacpp.indexer.UByteIndexer) Indexer(org.bytedeco.javacpp.indexer.Indexer) WritableRaster(java.awt.image.WritableRaster) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Aggregations

Indexer (org.bytedeco.javacpp.indexer.Indexer)3 Test (org.junit.Test)3 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)2 BufferedImage (java.awt.image.BufferedImage)1 DataBufferByte (java.awt.image.DataBufferByte)1 DataBufferInt (java.awt.image.DataBufferInt)1 WritableRaster (java.awt.image.WritableRaster)1 DoublePointer (org.bytedeco.javacpp.DoublePointer)1 FloatPointer (org.bytedeco.javacpp.FloatPointer)1 DoubleIndexer (org.bytedeco.javacpp.indexer.DoubleIndexer)1 FloatIndexer (org.bytedeco.javacpp.indexer.FloatIndexer)1 UByteIndexer (org.bytedeco.javacpp.indexer.UByteIndexer)1