Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions src/main/java/qupath/ext/imglib2/ImgCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ private ImgCreator(Builder<T> builder, Function<BufferedImage, A> cellCreator) {
* @return a builder to create an instance of this class
* @throws IllegalArgumentException if the provided image has less than one channel
*/
public static Builder<?> builder(ImageServer<BufferedImage> server) {
return new Builder<>(server);
public static <T extends NativeType<T> & NumericType<T>> Builder<T> builder(ImageServer<BufferedImage> server) {
// Despite the potential warning, T is necessary, otherwise a cannot infer type arguments error occurs
return new Builder<T>(server, getTypeOfServer(server));
}

/**
Expand Down Expand Up @@ -236,10 +237,6 @@ public static class Builder<T extends NativeType<T> & NumericType<T>> {
private final T type;
private CellCache cellCache = defaultCellCache;

private Builder(ImageServer<BufferedImage> server) {
this(server, getTypeOfServer(server));
}

private Builder(ImageServer<BufferedImage> server, T type) {
checkType(server, type);
if (server.nChannels() <= 0) {
Expand Down Expand Up @@ -282,24 +279,6 @@ public Builder<T> cellCache(CellCache cellCache) {
}
}

@SuppressWarnings("unchecked")
private static <T extends NativeType<T> & NumericType<T>> T getTypeOfServer(ImageServer<?> server) {
if (server.isRGB()) {
return (T) new ARGBType();
}

return switch (server.getPixelType()) {
case UINT8 -> (T) new UnsignedByteType();
case INT8 -> (T) new ByteType();
case UINT16 -> (T) new UnsignedShortType();
case INT16 -> (T) new ShortType();
case UINT32 -> (T) new UnsignedIntType();
case INT32 -> (T) new IntType();
case FLOAT32 -> (T) new FloatType();
case FLOAT64 -> (T) new DoubleType();
};
}

private static <T> void checkType(ImageServer<?> server, T type) {
if (server.isRGB()) {
if (!(type instanceof ARGBType)) {
Expand Down Expand Up @@ -379,6 +358,24 @@ private static <T> void checkType(ImageServer<?> server, T type) {
}
}

@SuppressWarnings("unchecked")
private static <T extends NativeType<T> & NumericType<T>> T getTypeOfServer(ImageServer<?> server) {
if (server.isRGB()) {
return (T) new ARGBType();
}

return switch (server.getPixelType()) {
case UINT8 -> (T) new UnsignedByteType();
case INT8 -> (T) new ByteType();
case UINT16 -> (T) new UnsignedShortType();
case INT16 -> (T) new ShortType();
case UINT32 -> (T) new UnsignedIntType();
case INT32 -> (T) new IntType();
case FLOAT32 -> (T) new FloatType();
case FLOAT64 -> (T) new DoubleType();
};
}

private Cell<A> createCell(TileRequest tile) {
BufferedImage image;
try {
Expand Down