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);
}
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);
}
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();
}
}
Aggregations