Search in sources :

Example 1 with ShortIndexer

use of org.bytedeco.javacpp.indexer.ShortIndexer in project javacpp by bytedeco.

the class IndexerTest method testShortIndexer.

@Test
public void testShortIndexer() {
    System.out.println("ShortIndexer");
    long size = 7 * 5 * 3 * 2;
    long[] sizes = { 7, 5, 3, 2 };
    long[] strides = { 5 * 3 * 2, 3 * 2, 2, 1 };
    final ShortPointer ptr = new ShortPointer(size);
    for (int i = 0; i < size; i++) {
        ptr.position(i).put((short) i);
    }
    ShortIndexer arrayIndexer = ShortIndexer.create(ptr.position(0), sizes, strides, false);
    ShortIndexer directIndexer = ShortIndexer.create(ptr.position(0), sizes, strides, true);
    int n = 0;
    for (int i = 0; i < sizes[0]; i++) {
        assertEquals(n, arrayIndexer.get(i));
        assertEquals(n, directIndexer.get(i));
        for (int j = 0; j < sizes[1]; j++) {
            assertEquals(n, arrayIndexer.get(i, j));
            assertEquals(n, directIndexer.get(i, j));
            for (int k = 0; k < sizes[2]; k++) {
                assertEquals(n, arrayIndexer.get(i, j, k));
                assertEquals(n, directIndexer.get(i, j, k));
                for (int m = 0; m < sizes[3]; m++) {
                    long[] index = { i, j, k, m };
                    assertEquals(n, arrayIndexer.get(index));
                    assertEquals(n, directIndexer.get(index));
                    arrayIndexer.put(index, (short) (2 * n));
                    directIndexer.put(index, (short) (3 * n));
                    n++;
                }
            }
        }
    }
    try {
        arrayIndexer.get(size);
        fail("IndexOutOfBoundsException should have been thrown.");
    } catch (IndexOutOfBoundsException e) {
    }
    try {
        directIndexer.get(size);
        fail("IndexOutOfBoundsException should have been thrown.");
    } catch (IndexOutOfBoundsException e) {
    }
    System.out.println("arrayIndexer" + arrayIndexer);
    System.out.println("directIndexer" + directIndexer);
    for (int i = 0; i < size; i++) {
        assertEquals(3 * i, ptr.position(i).get());
    }
    arrayIndexer.release();
    for (int i = 0; i < size; i++) {
        assertEquals(2 * i, ptr.position(i).get());
    }
    System.gc();
    if (Loader.sizeof(Pointer.class) > 4)
        try {
            long longSize = 0x80000000L + 8192;
            final ShortPointer longPointer = new ShortPointer(longSize);
            assertEquals(longSize, longPointer.capacity());
            ShortIndexer longIndexer = ShortIndexer.create(longPointer);
            assertEquals(longIndexer.pointer(), longPointer);
            for (long i = 0; i < 8192; i++) {
                longPointer.put(longSize - i - 1, (short) i);
            }
            for (long i = 0; i < 8192; i++) {
                assertEquals(longIndexer.get(longSize - i - 1), (short) i);
            }
            System.out.println("longIndexer[0x" + Long.toHexString(longSize - 8192) + "] = " + longIndexer.get(longSize - 8192));
        } catch (OutOfMemoryError e) {
            System.out.println(e);
        }
    System.out.println();
}
Also used : UShortIndexer(org.bytedeco.javacpp.indexer.UShortIndexer) ShortIndexer(org.bytedeco.javacpp.indexer.ShortIndexer) Test(org.junit.Test)

Example 2 with ShortIndexer

use of org.bytedeco.javacpp.indexer.ShortIndexer in project qupath by qupath.

the class OpenCVTools method putPixels.

/**
 * Put the pixels for the specified raster into a preallocated Mat.
 *
 * @param raster
 * @param mat
 */
private static void putPixels(WritableRaster raster, Mat mat) {
    Indexer indexer = mat.createIndexer();
    if (indexer instanceof UByteIndexer)
        putPixels(raster, (UByteIndexer) indexer);
    else if (indexer instanceof ShortIndexer)
        putPixels(raster, (ShortIndexer) indexer);
    else if (indexer instanceof UShortIndexer)
        putPixels(raster, (UShortIndexer) indexer);
    else if (indexer instanceof FloatIndexer)
        putPixels(raster, (FloatIndexer) indexer);
    else {
        double[] pixels = null;
        int width = raster.getWidth();
        int height = raster.getHeight();
        long[] indices = new long[3];
        for (int b = 0; b < raster.getNumBands(); b++) {
            pixels = raster.getSamples(0, 0, width, height, b, pixels);
            indices[2] = b;
            for (int y = 0; y < height; y++) {
                indices[0] = y;
                for (int x = 0; x < width; x++) {
                    indices[1] = x;
                    indexer.putDouble(indices, pixels[y * width + x]);
                }
            }
        }
    }
    indexer.release();
}
Also used : UShortIndexer(org.bytedeco.javacpp.indexer.UShortIndexer) ByteIndexer(org.bytedeco.javacpp.indexer.ByteIndexer) IntIndexer(org.bytedeco.javacpp.indexer.IntIndexer) ShortIndexer(org.bytedeco.javacpp.indexer.ShortIndexer) DoubleIndexer(org.bytedeco.javacpp.indexer.DoubleIndexer) UByteIndexer(org.bytedeco.javacpp.indexer.UByteIndexer) UShortIndexer(org.bytedeco.javacpp.indexer.UShortIndexer) FloatIndexer(org.bytedeco.javacpp.indexer.FloatIndexer) Indexer(org.bytedeco.javacpp.indexer.Indexer) ShortIndexer(org.bytedeco.javacpp.indexer.ShortIndexer) UShortIndexer(org.bytedeco.javacpp.indexer.UShortIndexer) FloatIndexer(org.bytedeco.javacpp.indexer.FloatIndexer) UByteIndexer(org.bytedeco.javacpp.indexer.UByteIndexer)

Aggregations

ShortIndexer (org.bytedeco.javacpp.indexer.ShortIndexer)2 UShortIndexer (org.bytedeco.javacpp.indexer.UShortIndexer)2 ByteIndexer (org.bytedeco.javacpp.indexer.ByteIndexer)1 DoubleIndexer (org.bytedeco.javacpp.indexer.DoubleIndexer)1 FloatIndexer (org.bytedeco.javacpp.indexer.FloatIndexer)1 Indexer (org.bytedeco.javacpp.indexer.Indexer)1 IntIndexer (org.bytedeco.javacpp.indexer.IntIndexer)1 UByteIndexer (org.bytedeco.javacpp.indexer.UByteIndexer)1 Test (org.junit.Test)1