Skip to content

Commit dca339c

Browse files
committed
Downsample: add operator factories, refactor, javadoc
1 parent c84f67d commit dca339c

File tree

7 files changed

+304
-180
lines changed

7 files changed

+304
-180
lines changed

src/main/java/net/imglib2/algorithm/blocks/downsample/Downsample.java

Lines changed: 271 additions & 106 deletions
Large diffs are not rendered by default.

src/test/java/net/imglib2/algorithm/blocks/downsample/DownsampleBdvBenchmark.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
import net.imglib2.RandomAccess;
4545
import net.imglib2.RandomAccessible;
4646
import net.imglib2.RandomAccessibleInterval;
47+
import net.imglib2.algorithm.blocks.BlockSupplier;
4748
import net.imglib2.algorithm.blocks.downsample.Downsample.ComputationType;
4849
import net.imglib2.algorithm.blocks.downsample.Downsample.Offset;
4950
import net.imglib2.algorithm.blocks.BlockAlgoUtils;
50-
import net.imglib2.blocks.PrimitiveBlocks;
5151
import net.imglib2.cache.img.CachedCellImg;
5252
import net.imglib2.cache.img.CellLoader;
5353
import net.imglib2.cache.img.ReadOnlyCachedCellImgFactory;
@@ -130,35 +130,21 @@ public void benchmarkBdv()
130130
@Benchmark
131131
public void benchmarkDownsampleDouble()
132132
{
133-
final PrimitiveBlocks< UnsignedByteType > blocks = PrimitiveBlocks.of( Views.extendBorder( raw ) );
134-
135-
// final CellLoader< DoubleType> loader = cellLoader( blocks, net.imglib2.algorithm.blocks.downsample.Downsample.downsample( new DoubleType(), ComputationType.AUTO, Offset.CENTERED, downsampleInDim ) );
136-
// final CachedCellImg< DoubleType, ? > downsampleDouble = new ReadOnlyCachedCellImgFactory().create(
137-
// downsampledDimensions,
138-
// new DoubleType(),
139-
// loader,
140-
// ReadOnlyCachedCellImgOptions.options().cellDimensions( cellDimensions).cacheType( BOUNDED ).maxCacheSize( 1 ) );
141-
final CachedCellImg< UnsignedByteType, ? > downsampleDouble = BlockAlgoUtils.cellImg(
142-
blocks, Downsample.downsample( new UnsignedByteType(), ComputationType.DOUBLE, Offset.HALF_PIXEL, downsampleInDim ), new UnsignedByteType(), downsampledDimensions, cellDimensions );
143-
133+
final BlockSupplier< UnsignedByteType > blocks = BlockSupplier
134+
.of( Views.extendBorder( raw ) )
135+
.andThen( Downsample.downsample( ComputationType.DOUBLE, Offset.HALF_PIXEL, downsampleInDim ) );
136+
final CachedCellImg< UnsignedByteType, ? > downsampleDouble = BlockAlgoUtils.cellImg( blocks, downsampledDimensions, cellDimensions );
144137
touchAllCellsSingleThreaded( downsampleDouble );
145138
downsampleDouble.getCache().invalidateAll();
146139
}
147140

148141
@Benchmark
149142
public void benchmarkDownsampleFloat()
150143
{
151-
final PrimitiveBlocks< UnsignedByteType > blocksFloat = PrimitiveBlocks.of( Views.extendBorder( raw ) );
152-
153-
// final CellLoader< FloatType> loader = cellLoader( blocksFloat, net.imglib2.algorithm.blocks.downsample.Downsample.downsample( new FloatType(), ComputationType.AUTO, Offset.CENTERED, downsampleInDim ) );
154-
// final CachedCellImg< FloatType, ? > downsampleFloat = new ReadOnlyCachedCellImgFactory().create(
155-
// downsampledDimensions,
156-
// new FloatType(),
157-
// loader,
158-
// ReadOnlyCachedCellImgOptions.options().cellDimensions( cellDimensions).cacheType( BOUNDED ).maxCacheSize( 1 ) );
159-
final CachedCellImg< UnsignedByteType, ? > downsampleFloat = BlockAlgoUtils.cellImg(
160-
blocksFloat, Downsample.downsample( new UnsignedByteType(), ComputationType.FLOAT, Offset.HALF_PIXEL, downsampleInDim ), new UnsignedByteType(), downsampledDimensions, cellDimensions );
161-
144+
final BlockSupplier< UnsignedByteType > blocks = BlockSupplier
145+
.of( Views.extendBorder( raw ) )
146+
.andThen( Downsample.downsample( ComputationType.FLOAT, Offset.HALF_PIXEL, downsampleInDim ) );
147+
final CachedCellImg< UnsignedByteType, ? > downsampleFloat = BlockAlgoUtils.cellImg( blocks, downsampledDimensions, cellDimensions );
162148
touchAllCellsSingleThreaded( downsampleFloat );
163149
downsampleFloat.getCache().invalidateAll();
164150
}

src/test/java/net/imglib2/algorithm/blocks/downsample/DownsampleBdvPlayground.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@
4343
import ij.ImagePlus;
4444
import java.util.Arrays;
4545
import net.imglib2.RandomAccess;
46+
import net.imglib2.algorithm.blocks.BlockSupplier;
4647
import net.imglib2.algorithm.blocks.downsample.Downsample.ComputationType;
4748
import net.imglib2.algorithm.blocks.downsample.Downsample.Offset;
4849
import net.imglib2.algorithm.blocks.BlockAlgoUtils;
49-
import net.imglib2.blocks.PrimitiveBlocks;
5050
import net.imglib2.cache.img.CachedCellImg;
5151
import net.imglib2.cache.img.CellLoader;
5252
import net.imglib2.cache.img.ReadOnlyCachedCellImgFactory;
@@ -58,8 +58,6 @@
5858
import net.imglib2.view.ExtendedRandomAccessibleInterval;
5959
import net.imglib2.view.Views;
6060

61-
import static net.imglib2.algorithm.blocks.downsample.Downsample.downsample;
62-
6361
public class DownsampleBdvPlayground
6462
{
6563
public static void main( String[] args )
@@ -83,7 +81,6 @@ public static void main( String[] args )
8381
final int[] cellDimensions = { 64, 64, 64 };
8482

8583
final ExtendedRandomAccessibleInterval< UnsignedByteType, Img< UnsignedByteType > > extended = Views.extendBorder( img );
86-
final PrimitiveBlocks< UnsignedByteType > blocks = PrimitiveBlocks.of( extended );
8784
final UnsignedByteType type = new UnsignedByteType();
8885

8986
final double[] calib = new double[ 3 ];
@@ -121,13 +118,10 @@ public static void main( String[] args )
121118
out.setColor( new ARGBType( 0xff0000 ) );
122119

123120

124-
125-
final CachedCellImg< UnsignedByteType, ? > downsampled2 = BlockAlgoUtils.cellImg(
126-
blocks,
127-
downsample( type, ComputationType.AUTO, Offset.HALF_PIXEL, downsampleInDim ),
128-
type,
129-
downsampledDimensions,
130-
cellDimensions );
121+
final BlockSupplier< UnsignedByteType > blocks = BlockSupplier
122+
.of( extended )
123+
.andThen( Downsample.downsample( ComputationType.AUTO, Offset.HALF_PIXEL, downsampleInDim ) );
124+
final CachedCellImg< UnsignedByteType, ? > downsampled2 = BlockAlgoUtils.cellImg( blocks, downsampledDimensions, cellDimensions );
131125
final BdvSource out2 = BdvFunctions.show(
132126
VolatileViews.wrapAsVolatile( downsampled2 ),
133127
"downsampled half-pixel",

src/test/java/net/imglib2/algorithm/blocks/downsample/DownsampleBdvPlayground2.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
* %%
1212
* Redistribution and use in source and binary forms, with or without
1313
* modification, are permitted provided that the following conditions are met:
14-
*
14+
*
1515
* 1. Redistributions of source code must retain the above copyright notice,
1616
* this list of conditions and the following disclaimer.
1717
* 2. Redistributions in binary form must reproduce the above copyright notice,
1818
* this list of conditions and the following disclaimer in the documentation
1919
* and/or other materials provided with the distribution.
20-
*
20+
*
2121
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2222
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2323
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -45,10 +45,7 @@
4545
import ij.ImagePlus;
4646
import net.imglib2.algorithm.blocks.BlockAlgoUtils;
4747
import net.imglib2.algorithm.blocks.BlockSupplier;
48-
import net.imglib2.algorithm.blocks.UnaryBlockOperator;
49-
import net.imglib2.algorithm.blocks.convert.Convert;
5048
import net.imglib2.algorithm.blocks.downsample.Downsample.Offset;
51-
import net.imglib2.cache.img.CachedCellImg;
5249
import net.imglib2.img.Img;
5350
import net.imglib2.img.display.imagej.ImageJFunctions;
5451
import net.imglib2.type.numeric.ARGBType;
@@ -80,12 +77,10 @@ public static void main( String[] args )
8077
final double[] calib = new double[ 3 ];
8178
Arrays.setAll(calib, d -> downsampleInDim[ d ] ? 2 : 1 );
8279

83-
final BlockSupplier< UnsignedByteType > blocks = BlockSupplier
80+
BlockSupplier< UnsignedByteType > blocks = BlockSupplier
8481
.of( Views.extendMirrorDouble( img ) )
85-
.andThen( Downsample.downsample(
86-
img.getType(),
87-
Offset.HALF_PIXEL,
88-
img.numDimensions() ) );
82+
.andThen( Downsample.downsample( Offset.HALF_PIXEL ) )
83+
.tile( 16 );
8984

9085
final Img< UnsignedByteType > downsampled = BlockAlgoUtils.cellImg(
9186
blocks,

src/test/java/net/imglib2/algorithm/blocks/downsample/DownsampleDoublePlayground.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@
4343
import java.util.Arrays;
4444

4545
import net.imglib2.algorithm.blocks.BlockSupplier;
46-
import net.imglib2.algorithm.blocks.downsample.DownsampleBlockProcessors.CenterDouble;
47-
import net.imglib2.algorithm.blocks.downsample.DownsampleBlockProcessors.HalfPixelDouble;
4846
import net.imglib2.algorithm.blocks.BlockAlgoUtils;
49-
import net.imglib2.blocks.PrimitiveBlocks;
5047
import net.imglib2.cache.img.CachedCellImg;
5148
import net.imglib2.converter.Converters;
5249
import net.imglib2.converter.RealDoubleConverter;
@@ -83,10 +80,8 @@ public static void main( String[] args )
8380
final int[] cellDimensions = { 64, 64, 64 };
8481
final CachedCellImg< DoubleType, ? > downsampled = BlockAlgoUtils.cellImg(
8582
blocks.andThen( Downsample.downsample(
86-
new DoubleType(),
8783
Downsample.ComputationType.DOUBLE,
88-
Downsample.Offset.CENTERED,
89-
3 )
84+
Downsample.Offset.CENTERED )
9085
),
9186
downsampledDimensions, cellDimensions );
9287

@@ -103,10 +98,8 @@ public static void main( String[] args )
10398

10499
final CachedCellImg< DoubleType, ? > downsampled2 = BlockAlgoUtils.cellImg(
105100
blocks.andThen( Downsample.downsample(
106-
new DoubleType(),
107101
Downsample.ComputationType.DOUBLE,
108-
Downsample.Offset.HALF_PIXEL,
109-
3 )
102+
Downsample.Offset.HALF_PIXEL )
110103
),
111104
downsampledDimensions, cellDimensions );
112105
final BdvSource out2 = BdvFunctions.show(

src/test/java/net/imglib2/algorithm/blocks/downsample/DownsampleMemProfile.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@
3636
import java.util.concurrent.atomic.AtomicLong;
3737
import java.util.stream.Collectors;
3838
import java.util.stream.IntStream;
39+
3940
import net.imglib2.Cursor;
4041
import net.imglib2.IterableInterval;
4142
import net.imglib2.RandomAccessibleInterval;
43+
import net.imglib2.algorithm.blocks.BlockAlgoUtils;
44+
import net.imglib2.algorithm.blocks.BlockSupplier;
4245
import net.imglib2.algorithm.blocks.downsample.Downsample.ComputationType;
4346
import net.imglib2.algorithm.blocks.downsample.Downsample.Offset;
44-
import net.imglib2.algorithm.blocks.BlockAlgoUtils;
45-
import net.imglib2.blocks.PrimitiveBlocks;
4647
import net.imglib2.cache.img.CachedCellImg;
4748
import net.imglib2.img.array.ArrayImgs;
4849
import net.imglib2.img.cell.AbstractCellImg;
@@ -76,16 +77,12 @@ public DownsampleMemProfile()
7677

7778
public void benchmarkDownsampleFloat()
7879
{
79-
final PrimitiveBlocks< UnsignedByteType > blocksFloat = PrimitiveBlocks.of( Views.extendBorder( raw ) );
80+
final BlockSupplier< UnsignedByteType > blocksFloat = BlockSupplier
81+
.of( Views.extendBorder( raw ) )
82+
.andThen( Downsample.downsample(ComputationType.FLOAT, Offset.CENTERED, downsampleInDim ) );
8083

81-
// final CellLoader< FloatType> loader = cellLoader( blocksFloat, net.imglib2.algorithm.blocks.downsample.Downsample.downsample( new FloatType(), ComputationType.AUTO, Offset.CENTERED, downsampleInDim ) );
82-
// final CachedCellImg< FloatType, ? > downsampleFloat = new ReadOnlyCachedCellImgFactory().create(
83-
// downsampledDimensions,
84-
// new FloatType(),
85-
// loader,
86-
// ReadOnlyCachedCellImgOptions.options().cellDimensions( cellDimensions).cacheType( BOUNDED ).maxCacheSize( 1 ) );
8784
final CachedCellImg< UnsignedByteType, ? > downsampleFloat = BlockAlgoUtils.cellImg(
88-
blocksFloat, Downsample.downsample( new UnsignedByteType(), ComputationType.FLOAT, Offset.CENTERED, downsampleInDim ), new UnsignedByteType(), downsampledDimensions, cellDimensions );
85+
blocksFloat, downsampledDimensions, cellDimensions );
8986

9087
// touchAllCellsSingleThreaded( downsampleFloat );
9188
touchAllCells( downsampleFloat );

src/test/java/net/imglib2/algorithm/blocks/downsample/DownsampleUnsignedBytePlayground.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,18 @@
4141
import ij.IJ;
4242
import ij.ImagePlus;
4343
import java.util.Arrays;
44+
45+
import net.imglib2.algorithm.blocks.BlockSupplier;
4446
import net.imglib2.algorithm.blocks.downsample.Downsample.ComputationType;
4547
import net.imglib2.algorithm.blocks.downsample.Downsample.Offset;
4648
import net.imglib2.algorithm.blocks.BlockAlgoUtils;
47-
import net.imglib2.blocks.PrimitiveBlocks;
4849
import net.imglib2.cache.img.CachedCellImg;
4950
import net.imglib2.img.Img;
5051
import net.imglib2.img.display.imagej.ImageJFunctions;
5152
import net.imglib2.type.numeric.ARGBType;
5253
import net.imglib2.type.numeric.integer.UnsignedByteType;
5354
import net.imglib2.view.Views;
5455

55-
import static net.imglib2.algorithm.blocks.downsample.Downsample.downsample;
56-
5756
public class DownsampleUnsignedBytePlayground
5857
{
5958
public static void main( String[] args )
@@ -76,13 +75,10 @@ public static void main( String[] args )
7675
final long[] downsampledDimensions = Downsample.getDownsampledDimensions( img.dimensionsAsLongArray(), downsampleInDim );
7776
final int[] cellDimensions = { 64, 64, 64 };
7877

79-
final PrimitiveBlocks< UnsignedByteType > blocks = PrimitiveBlocks.of( Views.extendBorder( img ) );
80-
final UnsignedByteType type = new UnsignedByteType();
78+
final BlockSupplier< UnsignedByteType > blocks = BlockSupplier.of( Views.extendBorder( img ) );
8179

8280
final CachedCellImg< UnsignedByteType, ? > downsampled = BlockAlgoUtils.cellImg(
83-
blocks,
84-
downsample( type, ComputationType.AUTO, Offset.CENTERED, downsampleInDim ),
85-
type,
81+
blocks.andThen( Downsample.downsample( ComputationType.AUTO, Offset.CENTERED, downsampleInDim ) ),
8682
downsampledDimensions,
8783
cellDimensions );
8884

@@ -98,9 +94,7 @@ public static void main( String[] args )
9894
out.setColor( new ARGBType( 0xff0000 ) );
9995

10096
final CachedCellImg< UnsignedByteType, ? > downsampled2 = BlockAlgoUtils.cellImg(
101-
blocks,
102-
downsample( type, ComputationType.AUTO, Offset.HALF_PIXEL, downsampleInDim ),
103-
type,
97+
blocks.andThen( Downsample.downsample( ComputationType.AUTO, Offset.HALF_PIXEL, downsampleInDim ) ),
10498
downsampledDimensions,
10599
cellDimensions );
106100
final BdvSource out2 = BdvFunctions.show(

0 commit comments

Comments
 (0)