Skip to content

Commit aaecc3c

Browse files
committed
Add buildForDownsamples() function
1 parent e702a42 commit aaecc3c

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

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

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ private ImgBuilder(ImageServer<BufferedImage> server, T type, Function<BufferedI
198198
* @param type the expected type of the output image
199199
* @return a builder to create an instance of this class
200200
* @param <T> the type corresponding to the provided image
201-
* @throws IllegalArgumentException if the provided type is not compatible with the input image (see above), or if the provided image
202-
* has less than one channel
201+
* @throws IllegalArgumentException if the provided type is not compatible with the input image (see above), or if
202+
* the provided image has less than one channel
203203
*/
204204
public static <T extends NativeType<T> & NumericType<T>> ImgBuilder<T, ?> createBuilder(ImageServer<BufferedImage> server, T type) {
205205
checkType(server, type);
@@ -218,8 +218,9 @@ private ImgBuilder(ImageServer<BufferedImage> server, T type, Function<BufferedI
218218
}
219219

220220
/**
221-
* Accessibles returned by this class will be divided into cells, which will be cached to gain performance. This function sets the
222-
* cache to use. By default, a static cache of maximal size half the amount of the {@link Runtime#maxMemory() max memory} is used.
221+
* Accessibles returned by this class will be divided into cells, which will be cached to gain performance. This
222+
* function sets the cache to use. By default, a static cache of maximal size half the amount of the
223+
* {@link Runtime#maxMemory() max memory} is used.
223224
*
224225
* @param cellCache the cache to use
225226
* @return this builder
@@ -297,11 +298,37 @@ public Img<T> buildForLevel(int level) {
297298
);
298299
}
299300

301+
/**
302+
* Create a list of {@link RandomAccessibleInterval} from the input image and the provided downsamples.
303+
* <p>
304+
* The {@link RandomAccessibleInterval} returned by this class are immutable. This means that any attempt to write
305+
* data to them will result in an {@link UnsupportedOperationException}.
306+
* <p>
307+
* See {@link #AXIS_X}, {@link #AXIS_Y}, {@link #AXIS_CHANNEL}, {@link #AXIS_Z}, and {@link #AXIS_TIME} to get the physical
308+
* interpretation of the dimensions of the returned {@link RandomAccessibleInterval}.
309+
* <p>
310+
* Values of the returned images are lazily fetched.
311+
* <p>
312+
* If the input image has to be scaled and its {@link ImageServerMetadata#getChannelType() channel type} is
313+
* {@link ImageServerMetadata.ChannelType#CLASSIFICATION}, then the nearest neighbor interpolation is used.
314+
* Otherwise, the linear interpolation is used.
315+
*
316+
* @param downsamples the downsamples to apply to the input image. Must be greater than 0
317+
* @return a list of {@link RandomAccessibleInterval} corresponding to the input image with the provided downsamples
318+
* applied. The ith returned {@link RandomAccessibleInterval} corresponds to the ith provided downsample
319+
* @throws IllegalArgumentException if one of the provided downsamples is not greater than 0
320+
*/
321+
public List<? extends RandomAccessibleInterval<T>> buildForDownsamples(List<Double> downsamples) {
322+
return downsamples.stream()
323+
.map(this::buildForDownsample)
324+
.toList();
325+
}
326+
300327
/**
301328
* Create a {@link RandomAccessibleInterval} from the input image and the provided downsample.
302329
* <p>
303-
* The {@link RandomAccessibleInterval} returned by this class is immutable. This means that any attempt to write data to it will result in an
304-
* {@link UnsupportedOperationException}.
330+
* The {@link RandomAccessibleInterval} returned by this class is immutable. This means that any attempt to write
331+
* data to it will result in an {@link UnsupportedOperationException}.
305332
* <p>
306333
* See {@link #AXIS_X}, {@link #AXIS_Y}, {@link #AXIS_CHANNEL}, {@link #AXIS_Z}, and {@link #AXIS_TIME} to get the physical
307334
* interpretation of the dimensions of the returned {@link RandomAccessibleInterval}.
@@ -324,9 +351,15 @@ public RandomAccessibleInterval<T> buildForDownsample(double downsample) {
324351
int level = ServerTools.getPreferredResolutionLevel(server, downsample);
325352

326353
if (server.getMetadata().getChannelType() == ImageServerMetadata.ChannelType.CLASSIFICATION) {
327-
return AccessibleScaler.scaleWithNearestNeighborInterpolation(buildForLevel(level), server.getDownsampleForResolution(level) / downsample);
354+
return AccessibleScaler.scaleWithNearestNeighborInterpolation(
355+
buildForLevel(level),
356+
server.getDownsampleForResolution(level) / downsample
357+
);
328358
} else {
329-
return AccessibleScaler.scaleWithLinearInterpolation(buildForLevel(level), server.getDownsampleForResolution(level) / downsample);
359+
return AccessibleScaler.scaleWithLinearInterpolation(
360+
buildForLevel(level),
361+
server.getDownsampleForResolution(level) / downsample
362+
);
330363
}
331364
}
332365

0 commit comments

Comments
 (0)