Skip to content

Commit

Permalink
Merge branch 'add-missing-types-imagej' into resolve-conflicts-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Sep 2, 2024
2 parents c96b901 + 354dc24 commit 2b67e11
Show file tree
Hide file tree
Showing 5 changed files with 948 additions and 7 deletions.
32 changes: 30 additions & 2 deletions src/main/java/net/clesperanto/imagej/ImageJConverters.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -98,17 +99,44 @@ public static ArrayJ copyImagePlus2ToArrayJ(ImagePlus rai, DeviceJ device, Strin
private static ImagePlus fromBuffer(ByteBuffer byteBuffer, ImageJDataType type, long[] dimensions) {
ImagePlus im = IJ.createImage("image", (int) dimensions[0], (int) dimensions[1], (int) dimensions[2], type.getBitDepth());

Supplier<Number> byteSupplier = () -> {
byte bb = byteBuffer.get();
return bb < 0 ? 256 + bb : bb;
};


switch (type) {
case FLOAT32:
FloatBuffer floatBuff = byteBuffer.asFloatBuffer();
fillImage(im, dimensions, floatBuff::get);
break;
case UINT8:
fillImage(im, dimensions, byteBuffer::get);
fillImage(im, dimensions, byteSupplier);
break;
case INT8:
fillImage(im, dimensions, byteSupplier);
break;
case UINT16:
ShortBuffer uShortBuff = byteBuffer.asShortBuffer();
fillImage(im, dimensions, () -> {
short bb = uShortBuff.get();
return bb < 0 ? 65536 + bb : bb;
});
break;
case INT16:
ShortBuffer shortBuff = byteBuffer.asShortBuffer();
fillImage(im, dimensions, shortBuff::get);
fillImage(im, dimensions, () -> {
short bb = shortBuff.get();
return bb < 0 ? 65536 + bb : bb;
});
break;
case UINT32:
IntBuffer uIntBuff = byteBuffer.asIntBuffer();
fillImage(im, dimensions, uIntBuff::get);
break;
case INT32:
IntBuffer intBuff = byteBuffer.asIntBuffer();
fillImage(im, dimensions, intBuff::get);
break;
default:
throw new IllegalArgumentException("Data type not supported.");
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/net/clesperanto/imagej/ImageJDataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import net.clesperanto.core.DataType;
import net.clesperanto.core.DeviceJ;

// TODO add all types ImagePlus.GRAY8, ImagePlus.GRAY16, ImagePlus.GRAY32, ImagePlus.COLOR_256 or ImagePlus.COLOR_RGB)
public enum ImageJDataType {
FLOAT32(DataType.fromString("float"), ImagePlus.GRAY32, float[].class, 32),
INT32(DataType.fromString("int"), ImagePlus.GRAY32, int[].class, 32),
UINT32(DataType.fromString("uint"), ImagePlus.GRAY32, int[].class, 32),
UINT16(DataType.fromString("ushort"), ImagePlus.GRAY16, short[].class, 16),
UINT8(DataType.fromString("uchar"), ImagePlus.GRAY8, byte[].class, 8);
INT16(DataType.fromString("short"), ImagePlus.GRAY16, short[].class, 16),
UINT8(DataType.fromString("uchar"), ImagePlus.GRAY8, byte[].class, 8),
INT8(DataType.fromString("char"), ImagePlus.GRAY8, byte[].class, 8);

private final DataType dt;
private final int imgDtype;
Expand All @@ -25,6 +28,10 @@ public enum ImageJDataType {
this.bitDepth = bitDepth;
}

public static ImageJDataType fromImagePlus(ImagePlus imp) {
return fromImgPlusDataType(imp.getType());
}

public static ImageJDataType fromString(String dType) {
for (ImageJDataType type : values()) {
if (type.dt.getName().equals(dType)) {
Expand Down
Loading

0 comments on commit 2b67e11

Please sign in to comment.