Skip to content

Commit

Permalink
test pulling
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Aug 13, 2024
1 parent 07a5457 commit 629e106
Showing 1 changed file with 184 additions and 10 deletions.
194 changes: 184 additions & 10 deletions src/test/java/TestImagePlusPushAndPull.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@
import net.clesperanto.imagej.ImageJConverters;
import net.clesperanto.core.ArrayJ;
import net.clesperanto.core.DeviceJ;
import net.clesperanto.core.MemoryJ;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class TestImagePlusPushAndPull {

public final static int MAX_UINT16 = 65536;

public final static int MAX_INT16 = 65536 / 2 - 1;

public final static int MAX_UINT8 = 256;

public final static int MAX_INT8 = 256 / 2 - 1;

@Test
public void testImagePlusPushAndPullFloat() {
ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 32);

int[] flatVals = new int[18];
Random random = new Random();
float[] flatVals = new float[18];
for (int i = 0; i < flatVals.length; i++)
flatVals[i] = random.nextInt(100);
flatVals[i] = ThreadLocalRandom.current().nextFloat();

int c = 0;
for (int z = 0; z < 2; z ++) {
Expand All @@ -43,7 +46,7 @@ public void testImagePlusPushAndPullFloat() {
ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, "buffer");
ImagePlus outputImp = ImageJConverters.copyArrayJToImagePlus(in);


c = 0;
for (int z = 0; z < 2; z ++) {
inputImp.setPositionWithoutUpdate(1, 1 + z, 1);
ImageProcessor inpIp = inputImp.getProcessor();
Expand All @@ -52,6 +55,31 @@ public void testImagePlusPushAndPullFloat() {
for (int y = 0; y < 3; y ++) {
for (int x = 0; x < 3; x ++) {
assertEquals(inpIp.getPixelValue(x, y), outIp.getPixelValue(x, y));
assertEquals(flatVals[c ++], outIp.getPixelValue(x, y));
}
}
}
}

@Test
public void testPullFloat() {

float[] flatVals = new float[18];
for (int i = 0; i < flatVals.length; i++)
flatVals[i] = ThreadLocalRandom.current().nextFloat();

DeviceJ device = DeviceJ.getDefaultDevice();
ArrayJ in = MemoryJ.makeFloatBuffer(device, new long[] {3, 3, 2}, "buffer");
MemoryJ.writeFloatBuffer(in, flatVals, 18);
ImagePlus outputImp = ImageJConverters.copyArrayJToImagePlus(in);

int c = 0;
for (int z = 0; z < 2; z ++) {
outputImp.setPositionWithoutUpdate(1, 1 + z, 1);
ImageProcessor outIp = outputImp.getProcessor();
for (int y = 0; y < 3; y ++) {
for (int x = 0; x < 3; x ++) {
assertEquals(flatVals[c ++], outIp.getPixelValue(x, y));
}
}
}
Expand All @@ -62,9 +90,8 @@ public void testImagePlusPushAndPullInt() {
ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 32);

int[] flatVals = new int[18];
Random random = new Random();
for (int i = 0; i < flatVals.length; i++)
flatVals[i] = random.nextInt(100);
flatVals[i] = ThreadLocalRandom.current().nextInt(100);

int c = 0;
for (int z = 0; z < 2; z ++) {
Expand Down Expand Up @@ -97,14 +124,37 @@ public void testImagePlusPushAndPullInt() {
}
}

@Test
public void testPullInt() {

int[] flatVals = new int[18];
for (int i = 0; i < flatVals.length; i++)
flatVals[i] = ThreadLocalRandom.current().nextInt();

DeviceJ device = DeviceJ.getDefaultDevice();
ArrayJ in = MemoryJ.makeIntBuffer(device, new long[] {3, 3, 2}, "buffer");
MemoryJ.writeIntBuffer(in, flatVals, 18);
ImagePlus outputImp = ImageJConverters.copyArrayJToImagePlus(in);

int c = 0;
for (int z = 0; z < 2; z ++) {
outputImp.setPositionWithoutUpdate(1, 1 + z, 1);
ImageProcessor outIp = outputImp.getProcessor();
for (int y = 0; y < 3; y ++) {
for (int x = 0; x < 3; x ++) {
assertEquals(flatVals[c ++], outIp.getPixelValue(x, y));
}
}
}
}

@Test
public void testImagePlusPushAndPullUint() {
ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 32);

long[] flatVals = new long[18];
Random random = new Random();
for (int i = 0; i < flatVals.length; i++)
flatVals[i] = (long) Integer.MAX_VALUE + (long) random.nextInt(100);
flatVals[i] = (long) Integer.MAX_VALUE + (long) ThreadLocalRandom.current().nextInt(0, 100);
int c = 0;
for (int z = 0; z < 2; z ++) {
inputImp.setPositionWithoutUpdate(1, 1 + z, 1);
Expand Down Expand Up @@ -136,13 +186,37 @@ public void testImagePlusPushAndPullUint() {
}
}

@Test
public void testPullUint() {

int[] flatVals = new int[18];
for (int i = 0; i < flatVals.length; i++)
flatVals[i] = Integer.MAX_VALUE + ThreadLocalRandom.current().nextInt(0, 100);

DeviceJ device = DeviceJ.getDefaultDevice();
ArrayJ in = MemoryJ.makeUIntBuffer(device, new long[] {3, 3, 2}, "buffer");
MemoryJ.writeUIntBuffer(in, flatVals, 18);
ImagePlus outputImp = ImageJConverters.copyArrayJToImagePlus(in);

int c = 0;
for (int z = 0; z < 2; z ++) {
outputImp.setPositionWithoutUpdate(1, 1 + z, 1);
ImageProcessor outIp = outputImp.getProcessor();
for (int y = 0; y < 3; y ++) {
for (int x = 0; x < 3; x ++) {
assertEquals(flatVals[c ++], outIp.getPixelValue(x, y));
}
}
}
}

@Test
public void testImagePlusPushAndPullShort() {
ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 16);

short[] flatVals = new short[18];
for (int i = 0; i < flatVals.length; i++)
flatVals[i] = (short) ThreadLocalRandom.current().nextInt(Short.MIN_VALUE, Short.MAX_VALUE + 1);;
flatVals[i] = (short) ThreadLocalRandom.current().nextInt(Short.MIN_VALUE, Short.MAX_VALUE + 1);
int c = 0;
for (int z = 0; z < 2; z ++) {
inputImp.setPositionWithoutUpdate(1, 1 + z, 1);
Expand Down Expand Up @@ -176,6 +250,31 @@ public void testImagePlusPushAndPullShort() {
}
}

@Test
public void testPullShort() {

short[] flatVals = new short[18];
for (int i = 0; i < flatVals.length; i++)
flatVals[i] = (short) ThreadLocalRandom.current().nextInt(Short.MIN_VALUE, Short.MAX_VALUE + 1);

DeviceJ device = DeviceJ.getDefaultDevice();
ArrayJ in = MemoryJ.makeShortBuffer(device, new long[] {3, 3, 2}, "buffer");
MemoryJ.writeShortBuffer(in, flatVals, 18);
ImagePlus outputImp = ImageJConverters.copyArrayJToImagePlus(in);

int c = 0;
for (int z = 0; z < 2; z ++) {
outputImp.setPositionWithoutUpdate(1, 1 + z, 1);
ImageProcessor outIp = outputImp.getProcessor();
for (int y = 0; y < 3; y ++) {
for (int x = 0; x < 3; x ++) {
float val = outIp.getPixelValue(x, y);
assertEquals(flatVals[c ++], val > MAX_INT16 ? val - MAX_UINT16: val);
}
}
}
}

@Test
public void testImagePlusPushAndPullUshort() {
ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 16);
Expand Down Expand Up @@ -214,6 +313,31 @@ public void testImagePlusPushAndPullUshort() {
}
}

@Test
public void testPullUshort() {

short[] flatVals = new short[18];
for (int i = 0; i < flatVals.length; i++)
flatVals[i] = (short) ThreadLocalRandom.current().nextInt(0, MAX_UINT16);

DeviceJ device = DeviceJ.getDefaultDevice();
ArrayJ in = MemoryJ.makeUShortBuffer(device, new long[] {3, 3, 2}, "buffer");
MemoryJ.writeUShortBuffer(in, flatVals, 18);
ImagePlus outputImp = ImageJConverters.copyArrayJToImagePlus(in);

int c = 0;
for (int z = 0; z < 2; z ++) {
outputImp.setPositionWithoutUpdate(1, 1 + z, 1);
ImageProcessor outIp = outputImp.getProcessor();
for (int y = 0; y < 3; y ++) {
for (int x = 0; x < 3; x ++) {
short val = flatVals[c ++];
assertEquals(val < 0 ? MAX_UINT16 + val: val, outIp.getPixelValue(x, y));
}
}
}
}

@Test
public void testImagePlusPushAndPullByte() {
ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 8);
Expand Down Expand Up @@ -253,6 +377,31 @@ public void testImagePlusPushAndPullByte() {
}
}

@Test
public void testPullByte() {

byte[] flatVals = new byte[18];
for (int i = 0; i < flatVals.length; i++)
flatVals[i] = (byte) ThreadLocalRandom.current().nextInt(Byte.MIN_VALUE, Byte.MAX_VALUE + 1);

DeviceJ device = DeviceJ.getDefaultDevice();
ArrayJ in = MemoryJ.makeByteBuffer(device, new long[] {3, 3, 2}, "buffer");
MemoryJ.writeByteBuffer(in, flatVals, 18);
ImagePlus outputImp = ImageJConverters.copyArrayJToImagePlus(in);

int c = 0;
for (int z = 0; z < 2; z ++) {
outputImp.setPositionWithoutUpdate(1, 1 + z, 1);
ImageProcessor outIp = outputImp.getProcessor();
for (int y = 0; y < 3; y ++) {
for (int x = 0; x < 3; x ++) {
float val = outIp.getPixelValue(x, y);
assertEquals(flatVals[c ++], val > MAX_INT8 ? val - MAX_UINT8 : val);
}
}
}
}

@Test
public void testImagePlusPushAndPullUbyte() {
ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 8);
Expand Down Expand Up @@ -290,4 +439,29 @@ public void testImagePlusPushAndPullUbyte() {
}
}
}

@Test
public void testPullUbyte() {

byte[] flatVals = new byte[18];
for (int i = 0; i < flatVals.length; i++)
flatVals[i] = (byte) ThreadLocalRandom.current().nextInt(0, MAX_UINT8);

DeviceJ device = DeviceJ.getDefaultDevice();
ArrayJ in = MemoryJ.makeUByteBuffer(device, new long[] {3, 3, 2}, "buffer");
MemoryJ.writeUByteBuffer(in, flatVals, 18);
ImagePlus outputImp = ImageJConverters.copyArrayJToImagePlus(in);

int c = 0;
for (int z = 0; z < 2; z ++) {
outputImp.setPositionWithoutUpdate(1, 1 + z, 1);
ImageProcessor outIp = outputImp.getProcessor();
for (int y = 0; y < 3; y ++) {
for (int x = 0; x < 3; x ++) {
byte val = flatVals[c ++];
assertEquals(val < 0 ? MAX_UINT8 + val: val, outIp.getPixelValue(x, y));
}
}
}
}
}

0 comments on commit 629e106

Please sign in to comment.