Skip to content

Commit f81de53

Browse files
committed
Remove duplicate code
1 parent 277ccc1 commit f81de53

1 file changed

Lines changed: 58 additions & 204 deletions

File tree

src/main/java/qupath/ext/imglib2/ImgBuilder.java

Lines changed: 58 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -107,56 +107,7 @@ private ImgBuilder(ImageServer<BufferedImage> server, T type, Function<BufferedI
107107
if (server.isRGB()) {
108108
return new ImgBuilder<>(server, new ARGBType(), ArgbBufferedImageAccess::new, 1);
109109
} else {
110-
return switch (server.getPixelType()) {
111-
case UINT8 -> new ImgBuilder<>(
112-
server,
113-
new UnsignedByteType(),
114-
image -> new ByteRasterAccess(image.getRaster()),
115-
server.nChannels()
116-
);
117-
case INT8 -> new ImgBuilder<>(
118-
server,
119-
new ByteType(),
120-
image -> new ByteRasterAccess(image.getRaster()),
121-
server.nChannels()
122-
);
123-
case UINT16 -> new ImgBuilder<>(
124-
server,
125-
new UnsignedShortType(),
126-
image -> new ShortRasterAccess(image.getRaster()),
127-
server.nChannels()
128-
);
129-
case INT16 -> new ImgBuilder<>(
130-
server,
131-
new ShortType(),
132-
image -> new ShortRasterAccess(image.getRaster()),
133-
server.nChannels()
134-
);
135-
case UINT32 -> new ImgBuilder<>(
136-
server,
137-
new UnsignedIntType(),
138-
image -> new IntRasterAccess(image.getRaster()),
139-
server.nChannels()
140-
);
141-
case INT32 -> new ImgBuilder<>(
142-
server,
143-
new IntType(),
144-
image -> new IntRasterAccess(image.getRaster()),
145-
server.nChannels()
146-
);
147-
case FLOAT32 -> new ImgBuilder<>(
148-
server,
149-
new FloatType(),
150-
image -> new FloatRasterAccess(image.getRaster()),
151-
server.nChannels()
152-
);
153-
case FLOAT64 -> new ImgBuilder<>(
154-
server,
155-
new DoubleType(),
156-
image -> new DoubleRasterAccess(image.getRaster()),
157-
server.nChannels()
158-
);
159-
};
110+
return createRealBuilderFromNonRgbServer(server);
160111
}
161112
}
162113

@@ -271,56 +222,7 @@ private ImgBuilder(ImageServer<BufferedImage> server, T type, Function<BufferedI
271222
if (server.isRGB()) {
272223
return new ImgBuilder<>(server, new UnsignedByteType(), ByteBufferedImageAccess::new, 3);
273224
} else {
274-
return switch (server.getPixelType()) {
275-
case UINT8 -> new ImgBuilder<>(
276-
server,
277-
new UnsignedByteType(),
278-
image -> new ByteRasterAccess(image.getRaster()),
279-
server.nChannels()
280-
);
281-
case INT8 -> new ImgBuilder<>(
282-
server,
283-
new ByteType(),
284-
image -> new ByteRasterAccess(image.getRaster()),
285-
server.nChannels()
286-
);
287-
case UINT16 -> new ImgBuilder<>(
288-
server,
289-
new UnsignedShortType(),
290-
image -> new ShortRasterAccess(image.getRaster()),
291-
server.nChannels()
292-
);
293-
case INT16 -> new ImgBuilder<>(
294-
server,
295-
new ShortType(),
296-
image -> new ShortRasterAccess(image.getRaster()),
297-
server.nChannels()
298-
);
299-
case UINT32 -> new ImgBuilder<>(
300-
server,
301-
new UnsignedIntType(),
302-
image -> new IntRasterAccess(image.getRaster()),
303-
server.nChannels()
304-
);
305-
case INT32 -> new ImgBuilder<>(
306-
server,
307-
new IntType(),
308-
image -> new IntRasterAccess(image.getRaster()),
309-
server.nChannels()
310-
);
311-
case FLOAT32 -> new ImgBuilder<>(
312-
server,
313-
new FloatType(),
314-
image -> new FloatRasterAccess(image.getRaster()),
315-
server.nChannels()
316-
);
317-
case FLOAT64 -> new ImgBuilder<>(
318-
server,
319-
new DoubleType(),
320-
image -> new DoubleRasterAccess(image.getRaster()),
321-
server.nChannels()
322-
);
323-
};
225+
return createRealBuilderFromNonRgbServer(server);
324226
}
325227
}
326228

@@ -331,45 +233,9 @@ private ImgBuilder(ImageServer<BufferedImage> server, T type, Function<BufferedI
331233
* <ul>
332234
* <li>
333235
* If the input image is {@link ImageServer#isRGB() RGB}, the type must be {@link UnsignedByteType}. Images
334-
* created by the returned builder will have 4 channels (RGBA).
335-
* </li>
336-
* <li>
337-
* Else:
338-
* <ul>
339-
* <li>
340-
* If the input image has the {@link PixelType#UINT8} {@link ImageServer#getPixelType() pixel type},
341-
* the type must be {@link UnsignedByteType}.
342-
* </li>
343-
* <li>
344-
* If the input image has the {@link PixelType#INT8} {@link ImageServer#getPixelType() pixel type},
345-
* the type must be {@link ByteType}.
346-
* </li>
347-
* <li>
348-
* If the input image has the {@link PixelType#UINT16} {@link ImageServer#getPixelType() pixel type},
349-
* the type must be {@link UnsignedShortType}.
350-
* </li>
351-
* <li>
352-
* If the input image has the {@link PixelType#INT16} {@link ImageServer#getPixelType() pixel type},
353-
* the type must be {@link ShortType}.
354-
* </li>
355-
* <li>
356-
* If the input image has the {@link PixelType#UINT32} {@link ImageServer#getPixelType() pixel type},
357-
* the type must be {@link UnsignedIntType}.
358-
* </li>
359-
* <li>
360-
* If the input image has the {@link PixelType#INT32} {@link ImageServer#getPixelType() pixel type},
361-
* the type must be {@link IntType}.
362-
* </li>
363-
* <li>
364-
* If the input image has the {@link PixelType#FLOAT32} {@link ImageServer#getPixelType() pixel type},
365-
* the type must be {@link FloatType}.
366-
* </li>
367-
* <li>
368-
* If the input image has the {@link PixelType#FLOAT64} {@link ImageServer#getPixelType() pixel type},
369-
* the type must be {@link DoubleType}.
370-
* </li>
371-
* </ul>
236+
* created by the returned builder will have 3 channels (RGB).
372237
* </li>
238+
* <li>Else, see {@link #createBuilder(ImageServer, NativeType)}.</li>
373239
* </ul>
374240
*
375241
* @param server the input image
@@ -566,6 +432,59 @@ public RandomAccessibleInterval<T> buildForDownsample(double downsample) {
566432
}
567433
}
568434

435+
private static ImgBuilder<? extends RealType<?>, ?> createRealBuilderFromNonRgbServer(ImageServer<BufferedImage> server) {
436+
return switch (server.getPixelType()) {
437+
case UINT8 -> new ImgBuilder<>(
438+
server,
439+
new UnsignedByteType(),
440+
image -> new ByteRasterAccess(image.getRaster()),
441+
server.nChannels()
442+
);
443+
case INT8 -> new ImgBuilder<>(
444+
server,
445+
new ByteType(),
446+
image -> new ByteRasterAccess(image.getRaster()),
447+
server.nChannels()
448+
);
449+
case UINT16 -> new ImgBuilder<>(
450+
server,
451+
new UnsignedShortType(),
452+
image -> new ShortRasterAccess(image.getRaster()),
453+
server.nChannels()
454+
);
455+
case INT16 -> new ImgBuilder<>(
456+
server,
457+
new ShortType(),
458+
image -> new ShortRasterAccess(image.getRaster()),
459+
server.nChannels()
460+
);
461+
case UINT32 -> new ImgBuilder<>(
462+
server,
463+
new UnsignedIntType(),
464+
image -> new IntRasterAccess(image.getRaster()),
465+
server.nChannels()
466+
);
467+
case INT32 -> new ImgBuilder<>(
468+
server,
469+
new IntType(),
470+
image -> new IntRasterAccess(image.getRaster()),
471+
server.nChannels()
472+
);
473+
case FLOAT32 -> new ImgBuilder<>(
474+
server,
475+
new FloatType(),
476+
image -> new FloatRasterAccess(image.getRaster()),
477+
server.nChannels()
478+
);
479+
case FLOAT64 -> new ImgBuilder<>(
480+
server,
481+
new DoubleType(),
482+
image -> new DoubleRasterAccess(image.getRaster()),
483+
server.nChannels()
484+
);
485+
};
486+
}
487+
569488
private static <T> void checkType(ImageServer<?> server, T type) {
570489
if (server.isRGB()) {
571490
if (!(type instanceof ARGBType)) {
@@ -653,72 +572,7 @@ private static <T> void checkRealType(ImageServer<?> server, T type) {
653572
));
654573
}
655574
} else {
656-
switch (server.getPixelType()) {
657-
case UINT8 -> {
658-
if (!(type instanceof UnsignedByteType)) {
659-
throw new IllegalArgumentException(String.format(
660-
"The provided type %s is not a UnsignedByteType, which is the one expected for non-RGB UINT8 images",
661-
type.getClass()
662-
));
663-
}
664-
}
665-
case INT8 -> {
666-
if (!(type instanceof ByteType)) {
667-
throw new IllegalArgumentException(String.format(
668-
"The provided type %s is not a ByteType, which is the one expected for non-RGB INT8 images",
669-
type.getClass()
670-
));
671-
}
672-
}
673-
case UINT16 -> {
674-
if (!(type instanceof UnsignedShortType)) {
675-
throw new IllegalArgumentException(String.format(
676-
"The provided type %s is not a UnsignedShortType, which is the one expected for non-RGB UINT16 images",
677-
type.getClass()
678-
));
679-
}
680-
}
681-
case INT16 -> {
682-
if (!(type instanceof ShortType)) {
683-
throw new IllegalArgumentException(String.format(
684-
"The provided type %s is not a ShortType, which is the one expected for non-RGB INT16 images",
685-
type.getClass()
686-
));
687-
}
688-
}
689-
case UINT32 -> {
690-
if (!(type instanceof UnsignedIntType)) {
691-
throw new IllegalArgumentException(String.format(
692-
"The provided type %s is not a UnsignedIntType, which is the one expected for non-RGB UINT32 images",
693-
type.getClass()
694-
));
695-
}
696-
}
697-
case INT32 -> {
698-
if (!(type instanceof IntType)) {
699-
throw new IllegalArgumentException(String.format(
700-
"The provided type %s is not a IntType, which is the one expected for non-RGB INT32 images",
701-
type.getClass()
702-
));
703-
}
704-
}
705-
case FLOAT32 -> {
706-
if (!(type instanceof FloatType)) {
707-
throw new IllegalArgumentException(String.format(
708-
"The provided type %s is not a FloatType, which is the one expected for non-RGB FLOAT32 images",
709-
type.getClass()
710-
));
711-
}
712-
}
713-
case FLOAT64 -> {
714-
if (!(type instanceof DoubleType)) {
715-
throw new IllegalArgumentException(String.format(
716-
"The provided type %s is not a DoubleType, which is the one expected for non-RGB FLOAT64 images",
717-
type.getClass()
718-
));
719-
}
720-
}
721-
}
575+
checkType(server, type);
722576
}
723577
}
724578

0 commit comments

Comments
 (0)