diff --git a/native/clesperantoj/include/clesperantoj.hpp b/native/clesperantoj/include/clesperantoj.hpp index 4a00f32..99ee465 100644 --- a/native/clesperantoj/include/clesperantoj.hpp +++ b/native/clesperantoj/include/clesperantoj.hpp @@ -33,9 +33,11 @@ class DeviceJ std::string getName() const; std::string getInfo() const; + void setWaitForKernelFinish(bool flag); + std::shared_ptr get() const; - bool operator==(const DeviceJ& other) const; + bool operator==(const DeviceJ &other) const; }; enum class DTypeJ @@ -70,8 +72,8 @@ class ArrayJ friend class MemoryJ; protected: - void writeFrom( void *data, const size_t &origin_x, const size_t &origin_y, const size_t &origin_z, const size_t &width, const size_t &height, const size_t &depth) const; - void readTo( void *data, const size_t &origin_x, const size_t &origin_y, const size_t &origin_z, const size_t &width, const size_t &height, const size_t &depth) const; + void writeFrom(void *data, const size_t &origin_x, const size_t &origin_y, const size_t &origin_z, const size_t &width, const size_t &height, const size_t &depth) const; + void readTo(void *data, const size_t &origin_x, const size_t &origin_y, const size_t &origin_z, const size_t &width, const size_t &height, const size_t &depth) const; public: ArrayJ() = default; @@ -87,7 +89,7 @@ class ArrayJ std::shared_ptr get() const; - static ArrayJ create( const size_t &width, const size_t &height, const size_t &depth, const size_t &dimension, const DTypeJ &data_type, const MTypeJ &memory_type, const DeviceJ &device); + static ArrayJ create(const size_t &width, const size_t &height, const size_t &depth, const size_t &dimension, const DTypeJ &data_type, const MTypeJ &memory_type, const DeviceJ &device); DTypeJ dtype() const; MTypeJ mtype() const; DeviceJ device() const; diff --git a/native/clesperantoj/src/clesperantoj.cpp b/native/clesperantoj/src/clesperantoj.cpp index 8168e4c..1b16e28 100644 --- a/native/clesperantoj/src/clesperantoj.cpp +++ b/native/clesperantoj/src/clesperantoj.cpp @@ -23,7 +23,7 @@ DeviceJ::DeviceJ(const std::shared_ptr &device) : device_(device) { } -bool DeviceJ::operator==(const DeviceJ& other) const +bool DeviceJ::operator==(const DeviceJ &other) const { return (device_ == other.device_); } @@ -48,6 +48,11 @@ std::string DeviceJ::getInfo() const return this->device_->getInfo(); } +void DeviceJ::setWaitForKernelFinish(bool flag) +{ + this->device_->setWaitToFinish(flag); +} + std::shared_ptr DeviceJ::get() const { return this->device_; @@ -92,7 +97,6 @@ void ArrayJ::copyDataTo(ArrayJ &dst) this->array_->copyTo(dst.get()); } - std::vector UtilsJ::getKeys(const std::unordered_map> &map) { std::vector keys; @@ -115,15 +119,23 @@ inline cle::dType to_cle_dType(const DTypeJ &dtype) { switch (dtype) { - case DTypeJ::INT8: return cle::dType::INT8; - case DTypeJ::UINT8: return cle::dType::UINT8; - case DTypeJ::INT16: return cle::dType::INT16; - case DTypeJ::UINT16: return cle::dType::UINT16; - case DTypeJ::INT32: return cle::dType::INT32; - case DTypeJ::UINT32: return cle::dType::UINT32; - case DTypeJ::FLOAT: return cle::dType::FLOAT; - case DTypeJ::UNKNOWN: - default: return cle::dType::UNKNOWN; + case DTypeJ::INT8: + return cle::dType::INT8; + case DTypeJ::UINT8: + return cle::dType::UINT8; + case DTypeJ::INT16: + return cle::dType::INT16; + case DTypeJ::UINT16: + return cle::dType::UINT16; + case DTypeJ::INT32: + return cle::dType::INT32; + case DTypeJ::UINT32: + return cle::dType::UINT32; + case DTypeJ::FLOAT: + return cle::dType::FLOAT; + case DTypeJ::UNKNOWN: + default: + return cle::dType::UNKNOWN; } } @@ -131,15 +143,23 @@ inline DTypeJ from_cle_dType(const cle::dType &dtype) { switch (dtype) { - case cle::dType::INT8: return DTypeJ::INT8; - case cle::dType::UINT8: return DTypeJ::UINT8; - case cle::dType::INT16: return DTypeJ::INT16; - case cle::dType::UINT16: return DTypeJ::UINT16; - case cle::dType::INT32: return DTypeJ::INT32; - case cle::dType::UINT32: return DTypeJ::UINT32; - case cle::dType::FLOAT: return DTypeJ::FLOAT; -// case cle::dType::UNKNOWN: // TODO: uncomment after https://github.com/clEsperanto/CLIc/pull/353 is merged, released, and we depend on that version - default: return DTypeJ::UNKNOWN; + case cle::dType::INT8: + return DTypeJ::INT8; + case cle::dType::UINT8: + return DTypeJ::UINT8; + case cle::dType::INT16: + return DTypeJ::INT16; + case cle::dType::UINT16: + return DTypeJ::UINT16; + case cle::dType::INT32: + return DTypeJ::INT32; + case cle::dType::UINT32: + return DTypeJ::UINT32; + case cle::dType::FLOAT: + return DTypeJ::FLOAT; + // case cle::dType::UNKNOWN: // TODO: uncomment after https://github.com/clEsperanto/CLIc/pull/353 is merged, released, and we depend on that version + default: + return DTypeJ::UNKNOWN; } } @@ -147,9 +167,11 @@ inline cle::mType to_cle_mType(const MTypeJ &mtype) { switch (mtype) { - case MTypeJ::IMAGE: return cle::mType::IMAGE; - case MTypeJ::BUFFER: - default: return cle::mType::BUFFER; + case MTypeJ::IMAGE: + return cle::mType::IMAGE; + case MTypeJ::BUFFER: + default: + return cle::mType::BUFFER; } } @@ -157,9 +179,11 @@ inline MTypeJ from_cle_mType(const cle::mType &mtype) { switch (mtype) { - case cle::mType::IMAGE: return MTypeJ::IMAGE; - case cle::mType::BUFFER: - default: return MTypeJ::BUFFER; + case cle::mType::IMAGE: + return MTypeJ::IMAGE; + case cle::mType::BUFFER: + default: + return MTypeJ::BUFFER; } } @@ -258,4 +282,3 @@ void MemoryJ::writeFromInt(const ArrayJ &array, int *data, const size_t &origin_ { array.writeFrom(static_cast(data), origin_x, origin_y, origin_z, width, height, depth); } - diff --git a/src/main/java/net/clesperanto/core/DeviceJ.java b/src/main/java/net/clesperanto/core/DeviceJ.java index 6b4c1e5..b706192 100644 --- a/src/main/java/net/clesperanto/core/DeviceJ.java +++ b/src/main/java/net/clesperanto/core/DeviceJ.java @@ -40,7 +40,8 @@ import net.clesperanto._internals.jclic._StringVector; /** - * Class to interact with the divide that is going to be used to do the operations + * Class to interact with the divide that is going to be used to do the + * operations */ public class DeviceJ { @@ -60,12 +61,14 @@ private DeviceJ() { * Constructor that initializes the wanted device * IMPORTANT: Does not initialize the backend. * - * TODO provide a better explanation of what deviceName is and what device type is + * TODO provide a better explanation of what deviceName is and what device type + * is * * @param deviceName - * the name of the device that wants to be initialized + * the name of the device that wants to be initialized * @param deviceType - * the type that wants to be initialized. If any type works, the argument should be "all" + * the type that wants to be initialized. If any type works, + * the argument should be "all" */ private DeviceJ(String deviceName, String deviceType) { Objects.requireNonNull(deviceName, "The device name cannot be null"); @@ -79,8 +82,10 @@ private DeviceJ(String deviceName, String deviceType) { } /** - * Get the first device available. By default this method tries to use an OpenCL backend. + * Get the first device available. By default this method tries to use an OpenCL + * backend. * If not OpenCL backend is available the method will fail. + * * @return the default device where Clesperanto operations can be done. */ public static DeviceJ getDefaultDevice() { @@ -92,9 +97,11 @@ public static DeviceJ getDefaultDevice() { * Get the first device available and select the wanted backend. * If the wanted backend is not available, the method will fall back to OpenCL. * And if OpenCL is not available either the method will fail. + * * @param backend - * the type of backend that wants to be used. It should be either "cuda" or "opencl", - * if it is anything else it will be set to "opencl" + * the type of backend that wants to be used. It should be either + * "cuda" or "opencl", + * if it is anything else it will be set to "opencl" * @return the default device where Clesperanto operations can be done. */ public static DeviceJ getDefaultDevice(String backend) { @@ -103,15 +110,18 @@ public static DeviceJ getDefaultDevice(String backend) { } /** - * Get the wanted device by its name and device type. Initialize the device with openCL backend. + * Get the wanted device by its name and device type. Initialize the device with + * openCL backend. * If not OpenCL backend is available the method will fail. * - * TODO provide a better explanation of what deviceName is and what device type is + * TODO provide a better explanation of what deviceName is and what device type + * is * * @param deviceName - * the name of the device that wants to be initialized + * the name of the device that wants to be initialized * @param deviceType - * the type that wants to be initialized. If any type works, the argument should be "all" + * the type that wants to be initialized. If any type works, + * the argument should be "all" * @return the wanted device where Clesperanto operations can be done. */ public static DeviceJ getDeviceWithDefaultBackend(String deviceName, String deviceType) { @@ -121,18 +131,22 @@ public static DeviceJ getDeviceWithDefaultBackend(String deviceName, String devi /** * Get the wanted device by its name and device type. Initialize the device with - * the wanted backend. If the backend is not available it will fallback to OpenCl backend. + * the wanted backend. If the backend is not available it will fallback to + * OpenCl backend. * If OpenCL backend is not available the method will fail. * - * TODO provide a better explanation of what deviceName is and what device type is + * TODO provide a better explanation of what deviceName is and what device type + * is * * @param deviceName - * the name of the device that wants to be initialized + * the name of the device that wants to be initialized * @param deviceType - * the type that wants to be initialized. If any type works, the argument should be "all" + * the type that wants to be initialized. If any type works, + * the argument should be "all" * @param backend - * the type of backend that wants to be used. It should be either "cuda" or "opencl", - * if it is anything else it will be set to "opencl" + * the type of backend that wants to be used. It should be + * either "cuda" or "opencl", + * if it is anything else it will be set to "opencl" * @return the wanted device where Clesperanto operations can be done. */ public static DeviceJ getDevice(String deviceName, String deviceType, String backend) { @@ -156,42 +170,53 @@ public String getInfo() { return this._deviceJ.getInfo(); } + /** + * + * @return void + */ + public void setWaitForKernelFinish(boolean wait) { + this._deviceJ.setWaitForKernelFinish(wait); + } + /** * TODO confirm if the devices are only GPUs or can be other hardware * Method that returns the available devices (GPUs) on the computer. * * @return a list of the available devices in the computer */ - public static List getAvailableDevices(){ + public static List getAvailableDevices() { _StringVector devices = _DeviceJ.getAvailableDevices(); List devicesList = new ArrayList(); for (int i = 0; i < devices.size(); i++) { devicesList.add(devices.get(i)); - } + } return devicesList; } /** * TODO confirm if the devices are only GPUs or can be other hardware - * Method that returns the available devices (GPUs) of the given {@code deviceType} on the computer. - * Using the {@code deviceType} "all" returns all the devices available, it is the same as using {@link #getAvailableDevices()}. + * Method that returns the available devices (GPUs) of the given + * {@code deviceType} on the computer. + * Using the {@code deviceType} "all" returns all the devices available, it is + * the same as using {@link #getAvailableDevices()}. * * @param deviceType the type of device to look for * @return a list of the available devices in the computer of the specific type */ - public static List getAvailableDevices(String deviceType){ + public static List getAvailableDevices(String deviceType) { Objects.requireNonNull(deviceType, "The device type cannot be null, if any device type works, use \"all\" or" + " use the method \"DeviceJ.getAvailableDevices()\""); _StringVector devices = _DeviceJ.getAvailableDevices(deviceType); List devicesList = new ArrayList(); for (int i = 0; i < devices.size(); i++) { devicesList.add(devices.get(i)); - } + } return devicesList; } /** * Return the backend that the device is using. + * * @return the backend (opencl, cuda) that the device is using * @throws RuntimeException if there is any error finding the backend */ @@ -200,20 +225,23 @@ public String getBackend() { int ind = info.indexOf(")"); - if (ind == -1) throw new RuntimeException("Unable to retrieve backend"); + if (ind == -1) + throw new RuntimeException("Unable to retrieve backend"); return info.substring(1, ind).toLowerCase(); } @Override public boolean equals(Object o) { - if (this == o) return true; - if (!(o instanceof DeviceJ)) return false; + if (this == o) + return true; + if (!(o instanceof DeviceJ)) + return false; DeviceJ deviceJ = (DeviceJ) o; return _deviceJ.equals(deviceJ._deviceJ); // NB: _DeviceJ.equals method is a native method overloading (not - // overriding) Object.equals() Therefore, Objects.equals(_deviceJ, - // deviceJ._deviceJ) will *not* work here! + // overriding) Object.equals() Therefore, Objects.equals(_deviceJ, + // deviceJ._deviceJ) will *not* work here! } @Override @@ -243,10 +271,11 @@ public ArrayJ createArray(final DataType dataType, final MemoryType memoryType, } /** - * - * @return the raw object that is going to be sent to the native Clesperanto library. Without Java wrappers - */ - public _DeviceJ getRaw() { - return this._deviceJ; - } + * + * @return the raw object that is going to be sent to the native Clesperanto + * library. Without Java wrappers + */ + public _DeviceJ getRaw() { + return this._deviceJ; + } } diff --git a/src/test/java/TestAbsolute.java b/src/test/java/TestAbsolute.java index 99818c8..ff006ac 100644 --- a/src/test/java/TestAbsolute.java +++ b/src/test/java/TestAbsolute.java @@ -46,7 +46,9 @@ public class TestAbsolute { @Test public void testAbsolute() { - DeviceJ device = DeviceJ.getDefaultDevice(); + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = device.createArray(DataType.INT32, MemoryType.BUFFER, 2, 2); in.fillMemory(-1); ArrayJ out = device.createArray(DataType.INT32, MemoryType.BUFFER, 2, 2); @@ -64,9 +66,10 @@ public void testAbsolute() { @Test public void testAbsolute1() { - DeviceJ device = DeviceJ.getDefaultDevice(); + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.INT32, MemoryType.BUFFER, 2, 2); - in.fillMemory(-1); + in.fillMemory(-1); ArrayJ out = Tier1.absolute(device, in, null); int[] result = new int[4]; @@ -74,8 +77,8 @@ public void testAbsolute1() { out.readToBuffer(resultBuff); for (int val : result) - assertEquals(1, val); - in = null; - out = null; + assertEquals(1, val); + in = null; + out = null; } } diff --git a/src/test/java/TestAbsoluteImagePlus.java b/src/test/java/TestAbsoluteImagePlus.java index 4e6322a..592eb9f 100644 --- a/src/test/java/TestAbsoluteImagePlus.java +++ b/src/test/java/TestAbsoluteImagePlus.java @@ -44,81 +44,80 @@ public class TestAbsoluteImagePlus { - @Test - public void testAbsoluteImagePlus() { - ImagePlus inputImp = IJ.createImage("input", 2, 2, 1, 32); - ImagePlus outputImp = IJ.createImage("input", 2, 2, 1, 32); - ImageProcessor inpIp = inputImp.getProcessor(); - ImageProcessor outIp = outputImp.getProcessor(); - - int[] vals = {-1, -1, 1, 1}; - int c = 0; - for (int x = 0; x < 2; x ++) { - for (int y = 0; y < 2; y ++) { - inpIp.putPixelValue(x, y, vals[c]); - outIp.putPixelValue(x, y, vals[c ++]); - } - } - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.BUFFER); - ArrayJ out = ImageJConverters.copyImagePlus2ToArrayJ(outputImp, device, MemoryType.BUFFER); - - Tier1.absolute(device, in, out); - - outputImp = ImageJConverters.copyArrayJToImagePlus(out); - outIp = outputImp.getProcessor(); - - - double min = Double.MAX_VALUE; + @Test + public void testAbsoluteImagePlus() { + ImagePlus inputImp = IJ.createImage("input", 2, 2, 1, 32); + ImagePlus outputImp = IJ.createImage("input", 2, 2, 1, 32); + ImageProcessor inpIp = inputImp.getProcessor(); + ImageProcessor outIp = outputImp.getProcessor(); + + int[] vals = { -1, -1, 1, 1 }; + int c = 0; + for (int x = 0; x < 2; x++) { + for (int y = 0; y < 2; y++) { + inpIp.putPixelValue(x, y, vals[c]); + outIp.putPixelValue(x, y, vals[c++]); + } + } + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.BUFFER); + ArrayJ out = ImageJConverters.copyImagePlus2ToArrayJ(outputImp, device, MemoryType.BUFFER); + + Tier1.absolute(device, in, out); + + outputImp = ImageJConverters.copyArrayJToImagePlus(out); + outIp = outputImp.getProcessor(); + + double min = Double.MAX_VALUE; double max = Double.MIN_VALUE; double mean = 0; - for (int x = 0; x < 2; x ++) { - for (int y = 0; y < 2; y ++) { - double val = outIp.getPixelValue(x, y); - mean += val / 4; - min = Math.min(min,val); - max = Math.max(max,val); - } - } - - assertEquals(1, min); - assertEquals(1, max); - assertEquals(1, mean); + for (int x = 0; x < 2; x++) { + for (int y = 0; y < 2; y++) { + double val = outIp.getPixelValue(x, y); + 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 testAbsolute1ImagePlus() { - ImagePlus inputImp = IJ.createImage("input", 2, 2, 1, 32); - ImageProcessor inpIp = inputImp.getProcessor(); - - int[] vals = {-1, -1, 1, 1}; - int c = 0; - for (int x = 0; x < 2; x ++) { - for (int y = 0; y < 2; y ++) { - inpIp.putPixelValue(x, y, vals[c ++]); - } - } - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.BUFFER); - - ArrayJ out = Tier1.absolute(device, in, null); - - ImagePlus outputImp = ImageJConverters.copyArrayJToImagePlus(out); - ImageProcessor outIp = outputImp.getProcessor(); - - for (int x = 0; x < 2; x ++) { - for (int y = 0; y < 2; y ++) { - double val = outIp.getPixelValue(x, y); - assertEquals(1, val); - } - } + } + + @Test + public void testAbsolute1ImagePlus() { + ImagePlus inputImp = IJ.createImage("input", 2, 2, 1, 32); + ImageProcessor inpIp = inputImp.getProcessor(); + + int[] vals = { -1, -1, 1, 1 }; + int c = 0; + for (int x = 0; x < 2; x++) { + for (int y = 0; y < 2; y++) { + inpIp.putPixelValue(x, y, vals[c++]); + } + } + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.BUFFER); + + ArrayJ out = Tier1.absolute(device, in, null); + + ImagePlus outputImp = ImageJConverters.copyArrayJToImagePlus(out); + ImageProcessor outIp = outputImp.getProcessor(); + + for (int x = 0; x < 2; x++) { + for (int y = 0; y < 2; y++) { + double val = outIp.getPixelValue(x, y); + assertEquals(1, val); + } + } in = null; out = null; - } + } } diff --git a/src/test/java/TestAbsoluteImgLib2.java b/src/test/java/TestAbsoluteImgLib2.java index 42f740a..4e5d0d8 100644 --- a/src/test/java/TestAbsoluteImgLib2.java +++ b/src/test/java/TestAbsoluteImgLib2.java @@ -44,59 +44,57 @@ public class TestAbsoluteImgLib2 { - @Test - public void testAbsoluteImgLib2() { - int[] flatVals = {1, 1, -1, -1}; - RandomAccessibleInterval inputImg = ArrayImgs.ints(flatVals, new long[] {2, 2}); - RandomAccessibleInterval outputImg = ArrayImgs.ints(flatVals, new long[] {2, 2}); + @Test + public void testAbsoluteImgLib2() { + int[] flatVals = { 1, 1, -1, -1 }; + RandomAccessibleInterval inputImg = ArrayImgs.ints(flatVals, new long[] { 2, 2 }); + RandomAccessibleInterval outputImg = ArrayImgs.ints(flatVals, new long[] { 2, 2 }); + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); + ArrayJ out = ImgLib2Converters.copyImgLib2ToArrayJ(outputImg, device, MemoryType.BUFFER); - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); - ArrayJ out = ImgLib2Converters.copyImgLib2ToArrayJ(outputImg, device, MemoryType.BUFFER); + Tier1.absolute(device, in, out); - Tier1.absolute(device, in, out); + outputImg = ImgLib2Converters.copyArrayJToImgLib2(out); - outputImg = ImgLib2Converters.copyArrayJToImgLib2(out); - - - double min = outputImg.firstElement().getRealDouble(); + double min = outputImg.firstElement().getRealDouble(); double max = min; double mean = 0; for (IntType px : outputImg) { double val = px.getRealDouble(); mean += val / 4; - min = Math.min(min,val); - max = Math.max(max,val); + min = Math.min(min, val); + max = Math.max(max, val); } - assertEquals(1, min); - assertEquals(1, max); - assertEquals(1, mean); + assertEquals(1, min); + assertEquals(1, max); + assertEquals(1, mean); in = null; out = null; - } - - @Test - public void testAbsolute1ImgLib2() { - int[] flatVals = {1, 1, -1, -1}; - RandomAccessibleInterval inputImg = ArrayImgs.ints(flatVals, new long[] {2, 2}); - + } - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); + @Test + public void testAbsolute1ImgLib2() { + int[] flatVals = { 1, 1, -1, -1 }; + RandomAccessibleInterval inputImg = ArrayImgs.ints(flatVals, new long[] { 2, 2 }); - ArrayJ out = Tier1.absolute(device, in, null); + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(out); + ArrayJ out = Tier1.absolute(device, in, null); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(out); for (IntType px : outputImg) { double val = px.getRealDouble(); - assertEquals(1, val); + assertEquals(1, val); } in = null; out = null; - } + } } diff --git a/src/test/java/TestPullImagePlus.java b/src/test/java/TestPullImagePlus.java index f09f1d5..9ae4079 100644 --- a/src/test/java/TestPullImagePlus.java +++ b/src/test/java/TestPullImagePlus.java @@ -47,182 +47,189 @@ public class TestPullImagePlus { public final static int MAX_UINT16 = 65536; - public final static int MAX_INT16 = 65536 / 2 - 1; + 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() { + @Test + public void testPullFloat() { - float[] flatVals = new float[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = ThreadLocalRandom.current().nextFloat(); + float[] flatVals = new float[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = ThreadLocalRandom.current().nextFloat(); - DeviceJ device = DeviceJ.getDefaultDevice(); + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.FLOAT32, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - 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 testPullInt() { - - int[] flatVals = new int[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = ThreadLocalRandom.current().nextInt(); - - DeviceJ device = DeviceJ.getDefaultDevice(); + 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 testPullInt() { + + int[] flatVals = new int[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = ThreadLocalRandom.current().nextInt(); + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.INT32, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - 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 testPullUint() { - - int[] flatVals = new int[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = (int) ThreadLocalRandom.current().nextLong(0, 0xffffffffL); - - DeviceJ device = DeviceJ.getDefaultDevice(); + 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 testPullUint() { + + int[] flatVals = new int[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = (int) ThreadLocalRandom.current().nextLong(0, 0xffffffffL); + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.UINT32, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - 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 ++) { + 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++) { final long flatVal = flatVals[c++] & 0xffffffffL; assertEquals(flatVal, outIp.getPixelValue(x, y)); - } - } - } - } + } + } + } + } - @Test - public void testPullShort() { + @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); + 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(); + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.INT16, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - 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 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(); + 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 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(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.UINT16, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - 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 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(); + 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 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(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.INT8, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - 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 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(); + 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 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(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.UINT8, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - 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)); - } - } - } - } + 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)); + } + } + } + } } diff --git a/src/test/java/TestPullImgLib2.java b/src/test/java/TestPullImgLib2.java index a14f1e4..d75bb4d 100644 --- a/src/test/java/TestPullImgLib2.java +++ b/src/test/java/TestPullImgLib2.java @@ -45,7 +45,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; - public class TestPullImgLib2 { public final static long MAX_UINT32 = (long) Math.pow(2, 32); @@ -54,154 +53,161 @@ public class TestPullImgLib2 { public final static int MAX_UINT16 = 65536; - public final static int MAX_INT16 = 65536 / 2 - 1; + 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() { + @Test + public void testPullFloat() { - float[] flatVals = new float[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = ThreadLocalRandom.current().nextFloat(); + float[] flatVals = new float[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = ThreadLocalRandom.current().nextFloat(); - DeviceJ device = DeviceJ.getDefaultDevice(); + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.FLOAT32, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - Cursor outCursor = outputImg.cursor(); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + Cursor outCursor = outputImg.cursor(); - int c = 0; - while (outCursor.hasNext()) { - outCursor.next(); - assertEquals(flatVals[c ++], outCursor.get().get()); - } - } + int c = 0; + while (outCursor.hasNext()) { + outCursor.next(); + assertEquals(flatVals[c++], outCursor.get().get()); + } + } - @Test - public void testPullInt() { + @Test + public void testPullInt() { - int[] flatVals = new int[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = ThreadLocalRandom.current().nextInt(); + int[] flatVals = new int[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = ThreadLocalRandom.current().nextInt(); - DeviceJ device = DeviceJ.getDefaultDevice(); + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.INT32, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - Cursor outCursor = outputImg.cursor(); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + Cursor outCursor = outputImg.cursor(); - int c = 0; - while (outCursor.hasNext()) { - outCursor.next(); - assertEquals(flatVals[c ++], outCursor.get().get()); - } - } + int c = 0; + while (outCursor.hasNext()) { + outCursor.next(); + assertEquals(flatVals[c++], outCursor.get().get()); + } + } - @Test - public void testPullUint() { + @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); + 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(); + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.UINT32, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - Cursor outCursor = outputImg.cursor(); - - int c = 0; - while (outCursor.hasNext()) { - outCursor.next(); - long val = flatVals[c ++]; - assertEquals(val < 0 ? MAX_UINT32 + val : val, outCursor.get().get()); - } - } - - @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(); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + Cursor outCursor = outputImg.cursor(); + + int c = 0; + while (outCursor.hasNext()) { + outCursor.next(); + long val = flatVals[c++]; + assertEquals(val < 0 ? MAX_UINT32 + val : val, outCursor.get().get()); + } + } + + @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(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.INT16, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - Cursor outCursor = outputImg.cursor(); - - int c = 0; - while (outCursor.hasNext()) { - outCursor.next(); - short val = flatVals[c ++]; - assertEquals(val, outCursor.get().get()); - } - } - - @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(); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + Cursor outCursor = outputImg.cursor(); + + int c = 0; + while (outCursor.hasNext()) { + outCursor.next(); + short val = flatVals[c++]; + assertEquals(val, outCursor.get().get()); + } + } + + @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(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.UINT16, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - Cursor outCursor = outputImg.cursor(); - - int c = 0; - while (outCursor.hasNext()) { - outCursor.next(); - short val = flatVals[c ++]; - assertEquals(val < 0 ? MAX_UINT16 + val : val, outCursor.get().get()); - } - } - - @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(); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + Cursor outCursor = outputImg.cursor(); + + int c = 0; + while (outCursor.hasNext()) { + outCursor.next(); + short val = flatVals[c++]; + assertEquals(val < 0 ? MAX_UINT16 + val : val, outCursor.get().get()); + } + } + + @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(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.INT8, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - Cursor outCursor = outputImg.cursor(); - - int c = 0; - while (outCursor.hasNext()) { - outCursor.next(); - short val = flatVals[c ++]; - assertEquals(val, outCursor.get().get()); - } - } - - @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(); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + Cursor outCursor = outputImg.cursor(); + + int c = 0; + while (outCursor.hasNext()) { + outCursor.next(); + short val = flatVals[c++]; + assertEquals(val, outCursor.get().get()); + } + } + + @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(); + device.setWaitForKernelFinish(true); ArrayJ in = device.createArray(DataType.UINT8, MemoryType.BUFFER, 3, 3, 2); in.writeFromArray(flatVals); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - Cursor outCursor = outputImg.cursor(); - - int c = 0; - while (outCursor.hasNext()) { - outCursor.next(); - short val = flatVals[c ++]; - assertEquals(val < 0 ? MAX_UINT8 + val : val, outCursor.get().get()); - } - } + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + Cursor outCursor = outputImg.cursor(); + + int c = 0; + while (outCursor.hasNext()) { + outCursor.next(); + short val = flatVals[c++]; + assertEquals(val < 0 ? MAX_UINT8 + val : val, outCursor.get().get()); + } + } } diff --git a/src/test/java/TestPushAndPullImagePlus.java b/src/test/java/TestPushAndPullImagePlus.java index 2945765..c0148bf 100644 --- a/src/test/java/TestPushAndPullImagePlus.java +++ b/src/test/java/TestPushAndPullImagePlus.java @@ -47,280 +47,280 @@ public class TestPushAndPullImagePlus { public final static int MAX_UINT16 = 65536; - public final static int MAX_INT16 = 65536 / 2 - 1; + 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); - - float[] flatVals = new float[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = ThreadLocalRandom.current().nextFloat(); - - int c = 0; - for (int z = 0; z < 2; z ++) { - inputImp.setPositionWithoutUpdate(1, 1 + z, 1); - ImageProcessor inpIp = inputImp.getProcessor(); - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - inpIp.putPixelValue(x, y, flatVals[c ++]); - } - } - } - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); - 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(inpIp.getPixelValue(x, y), outIp.getPixelValue(x, y)); - assertEquals(flatVals[c ++], outIp.getPixelValue(x, y)); - } - } - } - } - - @Test - public void testImagePlusPushAndPullInt() { - ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 32); - - int[] flatVals = new int[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = ThreadLocalRandom.current().nextInt(100); - - int c = 0; - for (int z = 0; z < 2; z ++) { - inputImp.setPositionWithoutUpdate(1, 1 + z, 1); - ImageProcessor inpIp = inputImp.getProcessor(); - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - inpIp.putPixelValue(x, y, flatVals[c ++]); - } - } - } - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); - 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(inpIp.getPixelValue(x, y), outIp.getPixelValue(x, y)); - 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]; - for (int i = 0; i < flatVals.length; i++) - 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); - ImageProcessor inpIp = inputImp.getProcessor(); - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - inpIp.putPixelValue(x, y, flatVals[c ++]); - } - } - } - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); - 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(inpIp.getPixelValue(x, y), outIp.getPixelValue(x, y)); - 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); - int c = 0; - for (int z = 0; z < 2; z ++) { - inputImp.setPositionWithoutUpdate(1, 1 + z, 1); - ImageProcessor inpIp = inputImp.getProcessor(); - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - inpIp.putPixelValue(x, y, flatVals[c] < 0 ? MAX_UINT16 + flatVals[c]: flatVals[c]); - c ++; - } - } - } - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); - 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 outVal = outIp.getPixelValue(x, y); - assertEquals(inpIp.getPixelValue(x, y), outVal); - assertEquals(flatVals[c ++], (short) outVal); - } - } - } - } - - @Test - public void testImagePlusPushAndPullUshort() { - ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 16); - - int[] flatVals = new int[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = ThreadLocalRandom.current().nextInt(0, MAX_UINT16); - int c = 0; - for (int z = 0; z < 2; z ++) { - inputImp.setPositionWithoutUpdate(1, 1 + z, 1); - ImageProcessor inpIp = inputImp.getProcessor(); - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - inpIp.putPixelValue(x, y, flatVals[c ++]); - } - } - } - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); - 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(inpIp.getPixelValue(x, y), outIp.getPixelValue(x, y)); - assertEquals(flatVals[c ++], outIp.getPixelValue(x, y)); - } - } - } - } - - @Test - public void testImagePlusPushAndPullByte() { - ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 8); - - 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); - int c = 0; - for (int z = 0; z < 2; z ++) { - inputImp.setPositionWithoutUpdate(1, 1 + z, 1); - ImageProcessor inpIp = inputImp.getProcessor(); - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - inpIp.putPixelValue(x, y, flatVals[c] < 0 ? MAX_UINT8 + flatVals[c] : flatVals[c]); - c ++; - } - } - } - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); - 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(inpIp.getPixelValue(x, y), outIp.getPixelValue(x, y)); - assertEquals(flatVals[c ++], (byte) outIp.getPixelValue(x, y)); - } - } - } - } - - @Test - public void testImagePlusPushAndPullUbyte() { - ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 8); - - short[] flatVals = new short[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = (short) ThreadLocalRandom.current().nextInt(0, MAX_UINT8); - int c = 0; - for (int z = 0; z < 2; z ++) { - inputImp.setPositionWithoutUpdate(1, 1 + z, 1); - ImageProcessor inpIp = inputImp.getProcessor(); - for (int y = 0; y < 3; y ++) { - for (int x = 0; x < 3; x ++) { - inpIp.putPixelValue(x, y, flatVals[c ++]); - } - } - } - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); - 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(inpIp.getPixelValue(x, y), outIp.getPixelValue(x, y)); - assertEquals(flatVals[c ++], outIp.getPixelValue(x, y)); - } - } - } - } + @Test + public void testImagePlusPushAndPullFloat() { + ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 32); + + float[] flatVals = new float[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = ThreadLocalRandom.current().nextFloat(); + + int c = 0; + for (int z = 0; z < 2; z++) { + inputImp.setPositionWithoutUpdate(1, 1 + z, 1); + ImageProcessor inpIp = inputImp.getProcessor(); + for (int y = 0; y < 3; y++) { + for (int x = 0; x < 3; x++) { + inpIp.putPixelValue(x, y, flatVals[c++]); + } + } + } + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); + 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(inpIp.getPixelValue(x, y), outIp.getPixelValue(x, y)); + assertEquals(flatVals[c++], outIp.getPixelValue(x, y)); + } + } + } + } + + @Test + public void testImagePlusPushAndPullInt() { + ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 32); + + int[] flatVals = new int[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = ThreadLocalRandom.current().nextInt(100); + + int c = 0; + for (int z = 0; z < 2; z++) { + inputImp.setPositionWithoutUpdate(1, 1 + z, 1); + ImageProcessor inpIp = inputImp.getProcessor(); + for (int y = 0; y < 3; y++) { + for (int x = 0; x < 3; x++) { + inpIp.putPixelValue(x, y, flatVals[c++]); + } + } + } + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); + 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(inpIp.getPixelValue(x, y), outIp.getPixelValue(x, y)); + 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]; + for (int i = 0; i < flatVals.length; i++) + 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); + ImageProcessor inpIp = inputImp.getProcessor(); + for (int y = 0; y < 3; y++) { + for (int x = 0; x < 3; x++) { + inpIp.putPixelValue(x, y, flatVals[c++]); + } + } + } + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); + 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(inpIp.getPixelValue(x, y), outIp.getPixelValue(x, y)); + 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); + int c = 0; + for (int z = 0; z < 2; z++) { + inputImp.setPositionWithoutUpdate(1, 1 + z, 1); + ImageProcessor inpIp = inputImp.getProcessor(); + for (int y = 0; y < 3; y++) { + for (int x = 0; x < 3; x++) { + inpIp.putPixelValue(x, y, flatVals[c] < 0 ? MAX_UINT16 + flatVals[c] : flatVals[c]); + c++; + } + } + } + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); + 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 outVal = outIp.getPixelValue(x, y); + assertEquals(inpIp.getPixelValue(x, y), outVal); + assertEquals(flatVals[c++], (short) outVal); + } + } + } + } + + @Test + public void testImagePlusPushAndPullUshort() { + ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 16); + + int[] flatVals = new int[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = ThreadLocalRandom.current().nextInt(0, MAX_UINT16); + int c = 0; + for (int z = 0; z < 2; z++) { + inputImp.setPositionWithoutUpdate(1, 1 + z, 1); + ImageProcessor inpIp = inputImp.getProcessor(); + for (int y = 0; y < 3; y++) { + for (int x = 0; x < 3; x++) { + inpIp.putPixelValue(x, y, flatVals[c++]); + } + } + } + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); + 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(inpIp.getPixelValue(x, y), outIp.getPixelValue(x, y)); + assertEquals(flatVals[c++], outIp.getPixelValue(x, y)); + } + } + } + } + + @Test + public void testImagePlusPushAndPullByte() { + ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 8); + + 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); + int c = 0; + for (int z = 0; z < 2; z++) { + inputImp.setPositionWithoutUpdate(1, 1 + z, 1); + ImageProcessor inpIp = inputImp.getProcessor(); + for (int y = 0; y < 3; y++) { + for (int x = 0; x < 3; x++) { + inpIp.putPixelValue(x, y, flatVals[c] < 0 ? MAX_UINT8 + flatVals[c] : flatVals[c]); + c++; + } + } + } + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); + 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(inpIp.getPixelValue(x, y), outIp.getPixelValue(x, y)); + assertEquals(flatVals[c++], (byte) outIp.getPixelValue(x, y)); + } + } + } + } + + @Test + public void testImagePlusPushAndPullUbyte() { + ImagePlus inputImp = IJ.createImage("input", 3, 3, 2, 8); + + short[] flatVals = new short[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = (short) ThreadLocalRandom.current().nextInt(0, MAX_UINT8); + int c = 0; + for (int z = 0; z < 2; z++) { + inputImp.setPositionWithoutUpdate(1, 1 + z, 1); + ImageProcessor inpIp = inputImp.getProcessor(); + for (int y = 0; y < 3; y++) { + for (int x = 0; x < 3; x++) { + inpIp.putPixelValue(x, y, flatVals[c++]); + } + } + } + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImageJConverters.copyImagePlus2ToArrayJ(inputImp, device, MemoryType.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(); + 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(inpIp.getPixelValue(x, y), outIp.getPixelValue(x, y)); + assertEquals(flatVals[c++], outIp.getPixelValue(x, y)); + } + } + } + } } diff --git a/src/test/java/TestPushAndPullImgLib2.java b/src/test/java/TestPushAndPullImgLib2.java index a3ffbc5..ffd1133 100644 --- a/src/test/java/TestPushAndPullImgLib2.java +++ b/src/test/java/TestPushAndPullImgLib2.java @@ -45,7 +45,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; - public class TestPushAndPullImgLib2 { public final static long MAX_UINT32 = (long) Math.pow(2, 32); @@ -54,185 +53,186 @@ public class TestPushAndPullImgLib2 { public final static int MAX_UINT16 = 65536; - public final static int MAX_INT16 = 65536 / 2 - 1; + 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 testImgLib2PushAndPullFloat() { - float[] flatVals = new float[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = ThreadLocalRandom.current().nextFloat(); - RandomAccessibleInterval inputImg = ArrayImgs.floats(flatVals, new long[] {3, 3, 2}); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - - Cursor inCursor = inputImg.cursor(); - Cursor outCursor = outputImg.cursor(); - - int c = 0; - while (inCursor.hasNext()) { - inCursor.next(); - outCursor.next(); - assertEquals(inCursor.get().get(), outCursor.get().get()); - assertEquals(flatVals[c ++], outCursor.get().get()); - } - } - - @Test - public void testImgLib2PushAndPullInt() { - int[] flatVals = new int[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = ThreadLocalRandom.current().nextInt(); - RandomAccessibleInterval inputImg = ArrayImgs.ints(flatVals, new long[] {3, 3, 2}); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - - Cursor inCursor = inputImg.cursor(); - Cursor outCursor = outputImg.cursor(); - - int c = 0; - while (inCursor.hasNext()) { - inCursor.next(); - outCursor.next(); - assertEquals(inCursor.get().get(), outCursor.get().get()); - assertEquals(flatVals[c ++], outCursor.get().get()); - } - } - - @Test - public void testImgLib2PushAndPullUint() { - int[] flatVals = new int[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = Integer.MAX_VALUE + ThreadLocalRandom.current().nextInt(0, 100); - RandomAccessibleInterval inputImg = ArrayImgs.unsignedInts(flatVals, new long[] {3, 3, 2}); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - - Cursor inCursor = inputImg.cursor(); - Cursor outCursor = outputImg.cursor(); - - int c = 0; - while (inCursor.hasNext()) { - inCursor.next(); - outCursor.next(); - long val = flatVals[c ++]; - assertEquals(inCursor.get().get(), outCursor.get().get()); - assertEquals(val < 0 ? val + MAX_UINT32 : val, outCursor.get().get()); - } - } - - @Test - public void testImgLib2PushAndPullShort() { - 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); - - RandomAccessibleInterval inputImg = ArrayImgs.shorts(flatVals, new long[] {3, 3, 2}); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - - Cursor inCursor = inputImg.cursor(); - Cursor outCursor = outputImg.cursor(); - - int c = 0; - while (inCursor.hasNext()) { - inCursor.next(); - outCursor.next(); - assertEquals(inCursor.get().get(), outCursor.get().get()); - assertEquals(flatVals[c ++], outCursor.get().get()); - } - } - - @Test - public void testImgLib2PushAndPullUshort() { - - short[] flatVals = new short[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = (short) ThreadLocalRandom.current().nextInt(0, MAX_UINT16); - - RandomAccessibleInterval inputImg = ArrayImgs.unsignedShorts(flatVals, new long[] {3, 3, 2}); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - - Cursor inCursor = inputImg.cursor(); - Cursor outCursor = outputImg.cursor(); - - int c = 0; - while (inCursor.hasNext()) { - inCursor.next(); - outCursor.next(); - int val = flatVals[c ++]; - assertEquals(inCursor.get().get(), outCursor.get().get()); - assertEquals(val < 0 ? MAX_UINT16 + val : val, outCursor.get().get()); - } - } - - @Test - public void testImgLib2PushAndPullByte() { - 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); - - RandomAccessibleInterval inputImg = ArrayImgs.bytes(flatVals, new long[] {3, 3, 2}); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - - Cursor inCursor = inputImg.cursor(); - Cursor outCursor = outputImg.cursor(); - - int c = 0; - while (inCursor.hasNext()) { - inCursor.next(); - outCursor.next(); - assertEquals(inCursor.get().get(), outCursor.get().get()); - assertEquals(flatVals[c ++], outCursor.get().get()); - } - } - - @Test - public void testImgLib2PushAndPullUbyte() { - byte[] flatVals = new byte[18]; - for (int i = 0; i < flatVals.length; i++) - flatVals[i] = (byte) ThreadLocalRandom.current().nextInt(0, MAX_UINT8); - - RandomAccessibleInterval inputImg = ArrayImgs.unsignedBytes(flatVals, new long[] {3, 3, 2}); - - - DeviceJ device = DeviceJ.getDefaultDevice(); - ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); - RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); - - Cursor inCursor = inputImg.cursor(); - Cursor outCursor = outputImg.cursor(); - - int c = 0; - while (inCursor.hasNext()) { - inCursor.next(); - outCursor.next(); - int val = flatVals[c ++]; - assertEquals(inCursor.get().get(), outCursor.get().get()); - assertEquals(val < 0 ? MAX_UINT8 + val : val, outCursor.get().get()); - } - } + @Test + public void testImgLib2PushAndPullFloat() { + float[] flatVals = new float[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = ThreadLocalRandom.current().nextFloat(); + RandomAccessibleInterval inputImg = ArrayImgs.floats(flatVals, new long[] { 3, 3, 2 }); + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + + Cursor inCursor = inputImg.cursor(); + Cursor outCursor = outputImg.cursor(); + + int c = 0; + while (inCursor.hasNext()) { + inCursor.next(); + outCursor.next(); + assertEquals(inCursor.get().get(), outCursor.get().get()); + assertEquals(flatVals[c++], outCursor.get().get()); + } + } + + @Test + public void testImgLib2PushAndPullInt() { + int[] flatVals = new int[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = ThreadLocalRandom.current().nextInt(); + RandomAccessibleInterval inputImg = ArrayImgs.ints(flatVals, new long[] { 3, 3, 2 }); + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + + Cursor inCursor = inputImg.cursor(); + Cursor outCursor = outputImg.cursor(); + + int c = 0; + while (inCursor.hasNext()) { + inCursor.next(); + outCursor.next(); + assertEquals(inCursor.get().get(), outCursor.get().get()); + assertEquals(flatVals[c++], outCursor.get().get()); + } + } + + @Test + public void testImgLib2PushAndPullUint() { + int[] flatVals = new int[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = Integer.MAX_VALUE + ThreadLocalRandom.current().nextInt(0, 100); + RandomAccessibleInterval inputImg = ArrayImgs.unsignedInts(flatVals, new long[] { 3, 3, 2 }); + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + + Cursor inCursor = inputImg.cursor(); + Cursor outCursor = outputImg.cursor(); + + int c = 0; + while (inCursor.hasNext()) { + inCursor.next(); + outCursor.next(); + long val = flatVals[c++]; + assertEquals(inCursor.get().get(), outCursor.get().get()); + assertEquals(val < 0 ? val + MAX_UINT32 : val, outCursor.get().get()); + } + } + + @Test + public void testImgLib2PushAndPullShort() { + 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); + + RandomAccessibleInterval inputImg = ArrayImgs.shorts(flatVals, new long[] { 3, 3, 2 }); + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + + Cursor inCursor = inputImg.cursor(); + Cursor outCursor = outputImg.cursor(); + + int c = 0; + while (inCursor.hasNext()) { + inCursor.next(); + outCursor.next(); + assertEquals(inCursor.get().get(), outCursor.get().get()); + assertEquals(flatVals[c++], outCursor.get().get()); + } + } + + @Test + public void testImgLib2PushAndPullUshort() { + + short[] flatVals = new short[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = (short) ThreadLocalRandom.current().nextInt(0, MAX_UINT16); + + RandomAccessibleInterval inputImg = ArrayImgs.unsignedShorts(flatVals, + new long[] { 3, 3, 2 }); + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + + Cursor inCursor = inputImg.cursor(); + Cursor outCursor = outputImg.cursor(); + + int c = 0; + while (inCursor.hasNext()) { + inCursor.next(); + outCursor.next(); + int val = flatVals[c++]; + assertEquals(inCursor.get().get(), outCursor.get().get()); + assertEquals(val < 0 ? MAX_UINT16 + val : val, outCursor.get().get()); + } + } + + @Test + public void testImgLib2PushAndPullByte() { + 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); + + RandomAccessibleInterval inputImg = ArrayImgs.bytes(flatVals, new long[] { 3, 3, 2 }); + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + + Cursor inCursor = inputImg.cursor(); + Cursor outCursor = outputImg.cursor(); + + int c = 0; + while (inCursor.hasNext()) { + inCursor.next(); + outCursor.next(); + assertEquals(inCursor.get().get(), outCursor.get().get()); + assertEquals(flatVals[c++], outCursor.get().get()); + } + } + + @Test + public void testImgLib2PushAndPullUbyte() { + byte[] flatVals = new byte[18]; + for (int i = 0; i < flatVals.length; i++) + flatVals[i] = (byte) ThreadLocalRandom.current().nextInt(0, MAX_UINT8); + + RandomAccessibleInterval inputImg = ArrayImgs.unsignedBytes(flatVals, new long[] { 3, 3, 2 }); + + DeviceJ device = DeviceJ.getDefaultDevice(); + device.setWaitForKernelFinish(true); + ArrayJ in = ImgLib2Converters.copyImgLib2ToArrayJ(inputImg, device, MemoryType.BUFFER); + RandomAccessibleInterval outputImg = ImgLib2Converters.copyArrayJToImgLib2(in); + + Cursor inCursor = inputImg.cursor(); + Cursor outCursor = outputImg.cursor(); + + int c = 0; + while (inCursor.hasNext()) { + inCursor.next(); + outCursor.next(); + int val = flatVals[c++]; + assertEquals(inCursor.get().get(), outCursor.get().get()); + assertEquals(val < 0 ? MAX_UINT8 + val : val, outCursor.get().get()); + } + } }