diff --git a/.gitignore b/.gitignore index 4510f4f..beea55b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ lib/ +libs/ native/cppbuild # from CLIJ2 diff --git a/native/clesperantoj/CMakeLists.txt b/native/clesperantoj/CMakeLists.txt index 03c88a5..575c52f 100644 --- a/native/clesperantoj/CMakeLists.txt +++ b/native/clesperantoj/CMakeLists.txt @@ -22,7 +22,7 @@ set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) # Export all symbols for shared library option(BUILD_TESTING OFF) option(BUILD_BENCHMARK OFF) option(BUILD_SHARED_LIBS ON) -set(CLIC_REPO_TAG master) # branch name for dev +set(CLIC_REPO_TAG 0.12.1) # branch name for dev set(CLIC_REPO_URL https://github.com/clEsperanto/CLIc_prototype.git) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/clic) diff --git a/pom.xml b/pom.xml index 4e2f2b3..2139390 100644 --- a/pom.xml +++ b/pom.xml @@ -1,23 +1,20 @@ - + 4.0.0 - net.haesleinhuepf - clij-parent-pom - 2.2.0.14 + org.scijava + pom-scijava + 38.0.1 net.clesperanto - clesperantoj_ - 0.0.0.1 + clesperantoj + 0.0.1-SNAPSHOT - clesperantoj_ - clesperantoj + clesperantoj + Java wrapper for Clesperanto http://haesleinhuepf.net 2022 @@ -26,7 +23,8 @@ - Simplified BSD License + BSD 3-Clause License + https://opensource.org/license/bsd-3-clause repo @@ -34,14 +32,48 @@ haesleinhuepf - Robert Haase` + Robert Haase http://haesleinhuepf.net PoL TUD developer + + StRigaud + Stephane Rigaud + https://github.com/StRigaud + Institut Pasteur + + developer + + + + carlosuc3m + Carlos Javier Garcia Lopez de Haro + https://github.com/carlosuc3m + + developer + + + + + Robert Haase + http://haesleinhuepf.net + haesleinhuepf + + + Stephane Rigaud + https://github.com/StRigaud + StRigaud + + + Carlos Javier Garcia Lopez de Haro + https://github.com/carlosuc3m + carlosuc3m + + @@ -50,15 +82,29 @@ + + scm:git:https://github.com/clEsperanto/clesperantoj_prototype + scm:git:git@github.com:clEsperanto/clesperantoj_prototype + HEAD + https://github.com/clEsperanto/clesperantoj_prototype + + + GitHub Issues + https://github.com/clEsperanto/clesperantoj_prototype/issues + + + GitHub Actions + https://github.com/clEsperanto/clesperantoj_prototype/actions + + - net.haesleinhuepf + net.clesperanto bsd_3 - Robert Haase, MPI CBG - - /home/stephane/Libraries/Fiji.app/ - - 11.2 + Robert Haase, MPI CBG and Stephane Rigaud, Institut Pasteur + + sign,deploy-to-scijava + win @@ -102,11 +148,6 @@ - - org.bioimageanalysis.icy - icy-kernel - 2.5.1 - net.imglib2 imglib2 @@ -121,6 +162,7 @@ org.bytedeco javacpp + 1.5.10 @@ -130,13 +172,11 @@ org.junit.jupiter junit-jupiter-api - 5.7.0 test org.junit.jupiter junit-jupiter-engine - 5.7.0 test @@ -146,10 +186,6 @@ scijava.public https://maven.scijava.org/content/groups/public - - icy - https://icy-nexus.pasteur.fr/repository/Icy/ - @@ -177,7 +213,7 @@ exec-maven-plugin - 1.6.0 + org.codehaus.mojo @@ -197,7 +233,7 @@ org.codehaus.mojo build-helper-maven-plugin - 3.3.0 + add-source @@ -215,7 +251,7 @@ maven-compiler-plugin - 3.12.1 + 1.8 1.8 @@ -309,14 +345,34 @@ org.apache.maven.plugins maven-surefire-plugin - 2.22.2 + - true 1 false + -Xmx1024m - + + maven-resources-plugin + + + copy + validate + + copy-resources + + + ${project.basedir}/target/classes/net/clesperanto + + + ${project.basedir}/libs + false + + + + + + diff --git a/src/main/java/net/clesperanto/icy/IcyConverters.java b/src/main/java/net/clesperanto/icy/IcyConverters.java deleted file mode 100644 index 45cd07c..0000000 --- a/src/main/java/net/clesperanto/icy/IcyConverters.java +++ /dev/null @@ -1,205 +0,0 @@ -package net.clesperanto.icy; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.FloatBuffer; -import java.nio.IntBuffer; -import java.nio.ShortBuffer; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Supplier; -import java.util.stream.Collectors; - -import icy.image.IcyBufferedImage; -import icy.preferences.ApplicationPreferences; -import icy.preferences.GeneralPreferences; -import icy.preferences.IcyPreferences; -import icy.sequence.Sequence; -import icy.sequence.SequenceCursor; -import net.clesperanto.core.ArrayJ; -import net.clesperanto.core.DeviceJ; - -/** - * TODO - * - define whether the buffers follow big or little endian and whether the copies are c- or fortran order - * - using array img and blocks copy limits to images whose array size is smaller than the integer max - */ -/** - * Class to copy {@link RandomAccessibleInteral}s into {@link ArrayJ}s and vice-versa - */ -public class IcyConverters { - - // Icy requires that the preferences are initialized if they have not bee initialized before. - // TODO check if htey have been initialized before - static { - initIcyPreferences(); - } - - /** TODO extend to RandomAccessibleInterval - * Conert an {@link ArrayJ} into an ImgLib2 {@link ArrayImg} of the same dimensions and data type. - * Creates a copy of the ArrayJ in the GPU into an ArrayImg in the CPU - * - * @param arrayj - * array that is located in the GPU for clesperanto to do some operations - * @return and ImgLib2 {@link ArrayImg} on the CPU copied from the {@link ArrayJ} on the GPU - */ - public static Sequence copyArrayJToSequence( ArrayJ arrayj ) - { - long flatDims = arrayj.getHeight() * arrayj.getDepth() * arrayj.getWidth(); - IcyDataType dataType = IcyDataType.fromString(arrayj.getDataType()); - if (flatDims * dataType.getByteSize() > Integer.MAX_VALUE) - throw new IllegalArgumentException("The ArrayJ provided is too big to be converted into an ImgLib2 ArrayImg."); - - ByteBuffer byteBuffer = ByteBuffer.allocateDirect((int) flatDims * dataType.getByteSize()) - .order(ByteOrder.LITTLE_ENDIAN); - dataType.readToBuffer(arrayj, byteBuffer); - - return fromBuffer(byteBuffer, dataType, arrayj.getDimensions()); - } - - /** - * Copy a {@link RandomAccessibleInterval} on the CPU into an {@link ArrayJ} on the device (GPU) of interest. - * The {@link RandomAccessibleInterval} should have at most 3 dimensions, and the order of the dimensions - * should be [width, height, depth] - * - * - * @param rai - * the {@link RandomAccessibleInterval} that is going to be copied into the GPU - * @param device - * the device into which the rai is going to be copied. If null, the default system device is used. - * @param memoryType - * the type of memory array that we are working with. The options are image or buffer. For image use the - * String "image", for buffer use "buffer" - * @return an {@link ArrayJ} copied from the {@link RandomAccessibleInterval} of the CPU - */ - public static ArrayJ copySequenceToArrayJ(Sequence rai, DeviceJ device, String memoryType) { - IcyDataType dataType = IcyDataType.fromIcyDataType(rai.getDataType_()); - Map sizeMap = checkSize(rai, dataType.getByteSize()); - long totalSize = sizeMap.values().stream().reduce((int) 1L, (a, b) -> a * b); - - long[] dims = sizeMap.values().stream().mapToLong(i -> (long) i).toArray(); - Object flatArr = dataType.createArray((int) totalSize); - SequenceCursor cursor = new SequenceCursor(rai); - int sizeT = sizeMap.get("t") != null ? sizeMap.get("t"): 1; - int sizeZ = sizeMap.get("z") != null ? sizeMap.get("z"): 1; - int sizeC = sizeMap.get("c") != null ? sizeMap.get("c"): 1; - int sizeY = sizeMap.get("y") != null ? sizeMap.get("y"): 1; - int sizeX = sizeMap.get("x") != null ? sizeMap.get("x"): 1; - int pos = 0; - for (int t = 0; t < sizeT; t ++) { - for (int z = 0; z < sizeZ; z ++) { - for (int c = 0; c < sizeC; c ++) { - for (int y = 0; y < sizeY; y ++) { - for (int x = 0; x < sizeX; x ++) { - dataType.putValInArray(flatArr, pos ++, cursor.get(x, y, z, t, c)); - } - } - } - } - } - - return dataType.makeAndWriteArrayJ(flatArr, device, dims, memoryType); - } - - private static Sequence fromBuffer(ByteBuffer byteBuffer, IcyDataType type, long[] dimensions) { - Sequence im = createSequence(dimensions, type.createType()); - switch (type) { - case FLOAT32: - FloatBuffer floatBuff = byteBuffer.asFloatBuffer(); - fillImage(im, dimensions, floatBuff::get); - break; - case INT8: - fillImage(im, dimensions, byteBuffer::get); - break; - case UINT8: - fillImage(im, dimensions, byteBuffer::get); - break; - case UINT16: - ShortBuffer uShortBuff = byteBuffer.asShortBuffer(); - fillImage(im, dimensions, uShortBuff::get); - break; - case INT16: - ShortBuffer shortBuff = byteBuffer.asShortBuffer(); - fillImage(im, dimensions, shortBuff::get); - break; - case INT32: - IntBuffer intBuff = byteBuffer.asIntBuffer(); - fillImage(im, dimensions, intBuff::get); - break; - case UINT32: - IntBuffer uIntBuff = byteBuffer.asIntBuffer(); - fillImage(im, dimensions, uIntBuff::get); - break; - default: - throw new IllegalArgumentException("Data type not supported."); - } - - return im; - } - - private static void fillImage(Sequence im, long[] dimensions, Supplier getValue) { - SequenceCursor cursor = new SequenceCursor(im); - for (int z = 0; z < dimensions[2]; z++) { - for (int y = 0; y < dimensions[1]; y++) { - for (int x = 0; x < dimensions[0]; x++) { - cursor.set(x, y, z, 0, 0, getValue.get().doubleValue()); - } - } - } - cursor.commitChanges(); - } - - private static Map checkSize(Sequence imp, long byteSize) { - Map sizeMap = new LinkedHashMap(); - sizeMap.put("x", imp.getWidth() == 0 ? 1 : imp.getWidth()); - sizeMap.put("y", imp.getHeight() == 0 ? 1 : imp.getHeight()); - sizeMap.put("z", imp.getSizeZ() == 0 ? 1 : imp.getSizeZ()); - for (Integer vv : sizeMap.values()) { - byteSize *= (long) vv; - } - if (byteSize > Integer.MAX_VALUE) - throw new IllegalArgumentException(); - return sizeMap; - } - - /** - * Create a sequence of the wanted size and data type - * @param dims - * wanted size of the sequence in the order [xyczt] - * @param type - * type of the sequence - * @return empty Icy sequence of the wanted type and dimensions - */ - public static Sequence createSequence(long[] dims, icy.type.DataType type) - { - while (dims.length < 3) { - long[] newArray = new long[dims.length + 1]; - System.arraycopy(dims, 0, newArray, 0, dims.length); - newArray[dims.length] = 1; - dims = newArray; - } - Sequence seq = new Sequence(); - int z; - for (z = 0; z < dims[2]; z++) - { - seq.setImage(0, z, new IcyBufferedImage((int) dims[0], (int) dims[1], 1, type)); - } - return seq; - } - - /** - * Initialize the Icy meta data that is required to use Icy Sequences. - * Only initializes it if it has not been done before. - */ - public static void initIcyPreferences() { - if (ApplicationPreferences.getPreferences() == null - || GeneralPreferences.getPreferences() == null) { - IcyPreferences.init(); - } - } - - public static void main(String[] args) { - - createSequence(new long[] {5, 5, 5}, icy.type.DataType.FLOAT); - } -} diff --git a/src/main/java/net/clesperanto/icy/IcyDataType.java b/src/main/java/net/clesperanto/icy/IcyDataType.java deleted file mode 100644 index 3c09a86..0000000 --- a/src/main/java/net/clesperanto/icy/IcyDataType.java +++ /dev/null @@ -1,121 +0,0 @@ -package net.clesperanto.icy; - -import java.nio.ByteBuffer; - -import net.clesperanto.core.ArrayJ; -import net.clesperanto.core.DataType; -import net.clesperanto.core.DeviceJ; - - -public enum IcyDataType { - FLOAT32(DataType.fromString("float"), icy.type.DataType.FLOAT, float[].class), - INT32(DataType.fromString("int"), icy.type.DataType.INT, int[].class), - UINT32(DataType.fromString("uint"), icy.type.DataType.UINT, int[].class), - INT16(DataType.fromString("short"), icy.type.DataType.SHORT, short[].class), - UINT16(DataType.fromString("ushort"), icy.type.DataType.USHORT, short[].class), - INT8(DataType.fromString("char"), icy.type.DataType.BYTE, byte[].class), - UINT8(DataType.fromString("uchar"), icy.type.DataType.UBYTE, byte[].class); - - private final DataType dt; - private final icy.type.DataType icyDT; - private final Class arrayClass; - - public final static long MAX_UINT32 = (long) Math.pow(2, 32); - - public final static int MAX_INT32 = Integer.MAX_VALUE; - - 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; - - IcyDataType(DataType dt, icy.type.DataType icyDT, Class arrayClass) { - this.dt = dt; - this.icyDT = icyDT; - this.arrayClass = arrayClass; - } - - public static IcyDataType fromString(String dType) { - for (IcyDataType type : values()) { - if (type.dt.getName().equals(dType)) { - return type; - } - } - throw new IllegalArgumentException("Unsupported data type: " + dType); - } - - public String getName() { - return dt.getName(); - } - - public static IcyDataType fromIcyDataType(icy.type.DataType icyType) { - for (IcyDataType type : values()) { - if (type.createType() == icyType) { - return type; - } - } - throw new IllegalArgumentException("Unsupported data type: " + icyType.toString()); - } - - public Object createArray(int size) { - return dt.createArray(size); - } - - public int getByteSize() { - return dt.getByteSize(); - } - - public void readToBuffer(ArrayJ arrayj, ByteBuffer buffer) { - this.dt.readToBuffer(arrayj, buffer); - } - - public void writeToBuffer(ArrayJ arrayj, ByteBuffer buffer) { - dt.writeToBuffer(arrayj, buffer); - } - - public ArrayJ makeEmptyArrayJ(DeviceJ device, long[] dims, String memoryType) { - return dt.makeEmptyArrayJ(device, dims, memoryType); - } - - public ArrayJ makeAndWriteArrayJ(Object buffer, DeviceJ device, long[] dims, String memoryType) { - return dt.makeAndWriteArrayJ(buffer, device, dims, memoryType); - } - - public icy.type.DataType createType() { - return this.icyDT; - } - - public void putValInArray(Object arr, int pos, Number value) { - if (!arrayClass.isInstance(arr)) - throw new IllegalArgumentException("The input argument 'arr' is not an instance of " + arrayClass.getSimpleName() + "."); - - switch (this) { - case FLOAT32: - ((float[]) arr)[pos] = value.floatValue(); - break; - case UINT32: - ((int[]) arr)[pos] = (int) (value.doubleValue() > Integer.MAX_VALUE ? value.doubleValue() - MAX_UINT32 : value.doubleValue()); - break; - case INT32: - ((int[]) arr)[pos] = value.intValue(); - break; - case UINT16: - ((short[]) arr)[pos] = (short) (value.doubleValue() > Short.MAX_VALUE ? value.doubleValue() - MAX_UINT16 : value.doubleValue()); - break; - case INT16: - ((short[]) arr)[pos] = value.shortValue(); - break; - case UINT8: - ((byte[]) arr)[pos] = (byte) (value.doubleValue() > Byte.MAX_VALUE ? value.doubleValue() - MAX_UINT8 : value.doubleValue()); - break; - case INT8: - ((byte[]) arr)[pos] = value.byteValue(); - break; - default: - throw new IllegalArgumentException("Unsupported data type"); - } - } -} diff --git a/src/test/java/TestAbsoluteIcySequence.java b/src/test/java/TestAbsoluteIcySequence.java deleted file mode 100644 index 466538b..0000000 --- a/src/test/java/TestAbsoluteIcySequence.java +++ /dev/null @@ -1,100 +0,0 @@ -import org.junit.jupiter.api.Test; - -import icy.image.IcyBufferedImage; -import icy.sequence.Sequence; -import icy.sequence.SequenceCursor; -import net.clesperanto.icy.IcyConverters; -import net.clesperanto.kernels.Tier1; -import net.clesperanto.core.ArrayJ; -import net.clesperanto.core.DeviceJ; - -import static org.junit.jupiter.api.Assertions.*; - -public class TestAbsoluteIcySequence { - - @Test - public void testAbsoluteIcySequence() { - int[] data = new int[] {1, 1, -1, -1}; - Sequence inputSeq = createSequence(new long[] {2, 2}, data); - Sequence outputSeq = createSequence(new long[] {2, 2}, data); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = IcyConverters.copySequenceToArrayJ(inputSeq, device, "buffer"); - ArrayJ out = IcyConverters.copySequenceToArrayJ(outputSeq, device, "buffer"); - - Tier1.absolute(device, in, out); - - outputSeq = IcyConverters.copyArrayJToSequence(out); - - double min = Double.MAX_VALUE; - double max = Double.MIN_VALUE; - double mean = 0; - SequenceCursor cursor = new SequenceCursor(outputSeq); - for (int y = 0; y < 2; y++) { - for (int x = 0; x < 2; x++) { - double val = cursor.get(x, y, 0, 0, 0); - mean += val / 4; - min = Math.min(min,val); - max = Math.max(max,val); - } - } - - assertEquals(1, min); - assertEquals(1, max); - assertEquals(1, mean); - in = null; - out = null; - } - - @Test - public void testAbsolute1IcySequence() { - int[] data = new int[] {1, 1, -1, -1}; - Sequence inputSeq = createSequence(new long[] {2, 2}, data); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = IcyConverters.copySequenceToArrayJ(inputSeq, device, "buffer"); - - ArrayJ out = Tier1.absolute(device, in, null); - - Sequence outputSeq = IcyConverters.copyArrayJToSequence(out); - - SequenceCursor cursor = new SequenceCursor(outputSeq); - for (int y = 0; y < 2; y++) { - for (int x = 0; x < 2; x++) { - double val = cursor.get(x, y, 0, 0, 0); - assertEquals(1, val); - } - } - in = null; - out = null; - } - - private static Sequence createSequence(long[] dims, int[] data) - { - IcyConverters.initIcyPreferences(); - while (dims.length < 3) { - long[] newArray = new long[dims.length + 1]; - System.arraycopy(dims, 0, newArray, 0, dims.length); - newArray[dims.length] = 1; - dims = newArray; - } - Sequence seq = new Sequence(); - int z; - for (z = 0; z < dims[2]; z++) - { - seq.setImage(0, z, new IcyBufferedImage((int) dims[0], (int) dims[1], 1, icy.type.DataType.INT)); - } - - SequenceCursor cursor = new SequenceCursor(seq); - int c = 0; - for (int x = 0; x < dims[0]; x ++) { - for (int y = 0; y < dims[1]; y ++) { - cursor.set(x, y, 0, 0, 0, data[c ++]); - } - } - cursor.commitChanges(); - return seq; - } -} diff --git a/src/test/java/TestPullIcySequence.java b/src/test/java/TestPullIcySequence.java deleted file mode 100644 index ff42236..0000000 --- a/src/test/java/TestPullIcySequence.java +++ /dev/null @@ -1,198 +0,0 @@ -import org.junit.jupiter.api.Test; - -import icy.sequence.Sequence; -import icy.sequence.SequenceCursor; -import net.clesperanto.core.ArrayJ; -import net.clesperanto.core.DeviceJ; -import net.clesperanto.core.MemoryJ; -import net.clesperanto.icy.IcyConverters; - -import static org.junit.jupiter.api.Assertions.*; - -import java.util.concurrent.ThreadLocalRandom; - - -public class TestPullIcySequence { - - static { - IcyConverters.initIcyPreferences(); - } - - public final static long MAX_UINT32 = (long) Math.pow(2, 32); - - public final static int MAX_INT32 = Integer.MAX_VALUE; - - 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 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); - Sequence outputImg = IcyConverters.copyArrayJToSequence(in); - SequenceCursor outCursor = new SequenceCursor(outputImg); - - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - assertEquals(flatVals[c ++], outCursor.get(x, y, z, 0, 0)); - } - } - } - } - - @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); - Sequence outputImg = IcyConverters.copyArrayJToSequence(in); - SequenceCursor outCursor = new SequenceCursor(outputImg); - - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - assertEquals(flatVals[c ++], outCursor.get(x, y, z, 0, 0)); - } - } - } - } - - @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); - Sequence outputImg = IcyConverters.copyArrayJToSequence(in); - SequenceCursor outCursor = new SequenceCursor(outputImg); - - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - long val = flatVals[c ++]; - assertEquals(val < 0 ? MAX_UINT32 + val : val, outCursor.get(x, y, z, 0, 0)); - } - } - } - } - - @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); - Sequence outputImg = IcyConverters.copyArrayJToSequence(in); - SequenceCursor outCursor = new SequenceCursor(outputImg); - - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - long val = flatVals[c ++]; - assertEquals(val, outCursor.get(x, y, z, 0, 0)); - } - } - } - } - - @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); - Sequence outputImg = IcyConverters.copyArrayJToSequence(in); - SequenceCursor outCursor = new SequenceCursor(outputImg); - - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - long val = flatVals[c ++]; - assertEquals(val < 0 ? MAX_UINT16 + val : val, outCursor.get(x, y, z, 0, 0)); - } - } - } - } - - @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); - Sequence outputImg = IcyConverters.copyArrayJToSequence(in); - SequenceCursor outCursor = new SequenceCursor(outputImg); - - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - long val = flatVals[c ++]; - assertEquals(val, outCursor.get(x, y, z, 0, 0)); - } - } - } - } - - @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); - Sequence outputImg = IcyConverters.copyArrayJToSequence(in); - SequenceCursor outCursor = new SequenceCursor(outputImg); - - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - long val = flatVals[c ++]; - assertEquals(val < 0 ? MAX_UINT8 + val : val, outCursor.get(x, y, z, 0, 0)); - } - } - } - } -} diff --git a/src/test/java/TestPushAndPullIcySequence.java b/src/test/java/TestPushAndPullIcySequence.java deleted file mode 100644 index cf40e7f..0000000 --- a/src/test/java/TestPushAndPullIcySequence.java +++ /dev/null @@ -1,319 +0,0 @@ -import org.junit.jupiter.api.Test; - -import icy.image.IcyBufferedImage; -import icy.sequence.Sequence; -import icy.sequence.SequenceCursor; -import icy.type.DataType; -import net.clesperanto.icy.IcyConverters; -import net.clesperanto.core.ArrayJ; -import net.clesperanto.core.DeviceJ; - -import static org.junit.jupiter.api.Assertions.*; - -import java.util.concurrent.ThreadLocalRandom; - -public class TestPushAndPullIcySequence { - - static { - IcyConverters.initIcyPreferences(); - } - - public final static long MAX_UINT32 = (long) Math.pow(2, 32); - - public final static int MAX_INT32 = Integer.MAX_VALUE; - - 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 testIcySequencePushAndPullFloat() { - float[] flatVals = new float[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = ThreadLocalRandom.current().nextFloat(); - Sequence inputSeq = createSequence(new long[] {3, 3, 2}, DataType.FLOAT); - - SequenceCursor cursor = new SequenceCursor(inputSeq); - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - cursor.set(x, y, z, 0, 0, flatVals[c ++]); - } - } - } - cursor.commitChanges(); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = IcyConverters.copySequenceToArrayJ(inputSeq, device, "buffer"); - - Sequence outputSeq = IcyConverters.copyArrayJToSequence(in); - - SequenceCursor inCursor = new SequenceCursor(inputSeq); - SequenceCursor outCursor = new SequenceCursor(outputSeq); - - c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - assertEquals(inCursor.get(x, y, z, 0, 0), outCursor.get(x, y, z, 0, 0)); - assertEquals(flatVals[c ++], outCursor.get(x, y, z, 0, 0)); - } - } - } - } - - @Test - public void testIcySequencePushAndPullInt() { - int[] flatVals = new int[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = ThreadLocalRandom.current().nextInt(); - Sequence inputSeq = createSequence(new long[] {3, 3, 2}, DataType.INT); - - SequenceCursor cursor = new SequenceCursor(inputSeq); - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - cursor.set(x, y, z, 0, 0, flatVals[c ++]); - } - } - } - cursor.commitChanges(); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = IcyConverters.copySequenceToArrayJ(inputSeq, device, "buffer"); - - Sequence outputSeq = IcyConverters.copyArrayJToSequence(in); - - SequenceCursor inCursor = new SequenceCursor(inputSeq); - SequenceCursor outCursor = new SequenceCursor(outputSeq); - - c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - assertEquals(inCursor.get(x, y, z, 0, 0), outCursor.get(x, y, z, 0, 0)); - assertEquals(flatVals[c ++], outCursor.get(x, y, z, 0, 0)); - } - } - } - } - - @Test - public void testIcySequencePushAndPullUint() { - int[] flatVals = new int[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = Integer.MAX_VALUE + ThreadLocalRandom.current().nextInt(0, 100); - Sequence inputSeq = createSequence(new long[] {3, 3, 2}, DataType.UINT); - - SequenceCursor cursor = new SequenceCursor(inputSeq); - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - cursor.set(x, y, z, 0, 0, flatVals[c ++]); - } - } - } - cursor.commitChanges(); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = IcyConverters.copySequenceToArrayJ(inputSeq, device, "buffer"); - - Sequence outputSeq = IcyConverters.copyArrayJToSequence(in); - - SequenceCursor inCursor = new SequenceCursor(inputSeq); - SequenceCursor outCursor = new SequenceCursor(outputSeq); - - c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - long val = flatVals[c ++]; - assertEquals(inCursor.get(x, y, z, 0, 0), outCursor.get(x, y, z, 0, 0)); - assertEquals(val < 0 ? val + MAX_UINT32 : val, outCursor.get(x, y, z, 0, 0)); - } - } - } - } - - @Test - public void testIcySequencePushAndPullShort() { - 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); - Sequence inputSeq = createSequence(new long[] {3, 3, 2}, DataType.SHORT); - - SequenceCursor cursor = new SequenceCursor(inputSeq); - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - cursor.set(x, y, z, 0, 0, flatVals[c ++]); - } - } - } - cursor.commitChanges(); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = IcyConverters.copySequenceToArrayJ(inputSeq, device, "buffer"); - - Sequence outputSeq = IcyConverters.copyArrayJToSequence(in); - - SequenceCursor inCursor = new SequenceCursor(inputSeq); - SequenceCursor outCursor = new SequenceCursor(outputSeq); - - c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - assertEquals(inCursor.get(x, y, z, 0, 0), outCursor.get(x, y, z, 0, 0)); - assertEquals(flatVals[c ++], outCursor.get(x, y, z, 0, 0)); - } - } - } - } - - @Test - public void testIcySequencePushAndPullUshort() { - short[] flatVals = new short[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = (short) ThreadLocalRandom.current().nextInt(0, MAX_UINT16); - Sequence inputSeq = createSequence(new long[] {3, 3, 2}, DataType.USHORT); - - SequenceCursor cursor = new SequenceCursor(inputSeq); - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - cursor.set(x, y, z, 0, 0, flatVals[c ++]); - } - } - } - cursor.commitChanges(); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = IcyConverters.copySequenceToArrayJ(inputSeq, device, "buffer"); - - Sequence outputSeq = IcyConverters.copyArrayJToSequence(in); - - SequenceCursor inCursor = new SequenceCursor(inputSeq); - SequenceCursor outCursor = new SequenceCursor(outputSeq); - - c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - int val = flatVals[c ++]; - assertEquals(inCursor.get(x, y, z, 0, 0), outCursor.get(x, y, z, 0, 0)); - assertEquals(val < 0 ? val + MAX_UINT16 : val, outCursor.get(x, y, z, 0, 0)); - } - } - } - } - - @Test - public void testIcySequencePushAndPullByte() { - 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); - Sequence inputSeq = createSequence(new long[] {3, 3, 2}, DataType.BYTE); - - SequenceCursor cursor = new SequenceCursor(inputSeq); - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - cursor.set(x, y, z, 0, 0, flatVals[c ++]); - } - } - } - cursor.commitChanges(); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = IcyConverters.copySequenceToArrayJ(inputSeq, device, "buffer"); - - Sequence outputSeq = IcyConverters.copyArrayJToSequence(in); - - SequenceCursor inCursor = new SequenceCursor(inputSeq); - SequenceCursor outCursor = new SequenceCursor(outputSeq); - - c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - assertEquals(inCursor.get(x, y, z, 0, 0), outCursor.get(x, y, z, 0, 0)); - assertEquals(flatVals[c ++], outCursor.get(x, y, z, 0, 0)); - } - } - } - } - - @Test - public void testIcySequencePushAndPullUbyte() { - byte[] flatVals = new byte[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = (byte) ThreadLocalRandom.current().nextInt(0, MAX_UINT8); - Sequence inputSeq = createSequence(new long[] {3, 3, 2}, DataType.UBYTE); - - SequenceCursor cursor = new SequenceCursor(inputSeq); - int c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - cursor.set(x, y, z, 0, 0, flatVals[c ++]); - } - } - } - cursor.commitChanges(); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = IcyConverters.copySequenceToArrayJ(inputSeq, device, "buffer"); - - Sequence outputSeq = IcyConverters.copyArrayJToSequence(in); - - SequenceCursor inCursor = new SequenceCursor(inputSeq); - SequenceCursor outCursor = new SequenceCursor(outputSeq); - - c = 0; - for (int z = 0; z < 2; z ++) { - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - short val = flatVals[c ++]; - assertEquals(inCursor.get(x, y, z, 0, 0), outCursor.get(x, y, z, 0, 0)); - assertEquals(val < 0 ? val + MAX_UINT8 : val, outCursor.get(x, y, z, 0, 0)); - } - } - } - } - - private static Sequence createSequence(long[] dims, DataType dt) - { - IcyConverters.initIcyPreferences(); - while (dims.length < 3) { - long[] newArray = new long[dims.length + 1]; - System.arraycopy(dims, 0, newArray, 0, dims.length); - newArray[dims.length] = 1; - dims = newArray; - } - Sequence seq = new Sequence(); - int z; - for (z = 0; z < dims[2]; z++) - { - seq.setImage(0, z, new IcyBufferedImage((int) dims[0], (int) dims[1], 1, dt)); - } - return seq; - } -}