Skip to content

Commit dd26251

Browse files
committed
support overhanging and incomplete blocks for cell images
only the intersection of block size and cell size will be filled among other use-cases, this supports zarr-like trailing blocks
1 parent c121f78 commit dd26251

4 files changed

Lines changed: 202 additions & 52 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.scijava</groupId>
88
<artifactId>pom-scijava</artifactId>
9-
<version>23.2.0</version>
9+
<version>25.0.0</version>
1010
<relativePath />
1111
</parent>
1212

‎src/main/java/org/janelia/saalfeldlab/n5/imglib2/N5CellLoader.java‎

Lines changed: 117 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@
3737
import org.janelia.saalfeldlab.n5.N5Reader;
3838

3939
import net.imglib2.Cursor;
40+
import net.imglib2.FinalInterval;
41+
import net.imglib2.Interval;
4042
import net.imglib2.IterableInterval;
4143
import net.imglib2.RandomAccessibleInterval;
4244
import net.imglib2.cache.img.CellLoader;
4345
import net.imglib2.cache.img.SingleCellArrayImg;
46+
import net.imglib2.img.array.ArrayImg;
47+
import net.imglib2.img.array.ArrayImgs;
4448
import net.imglib2.type.NativeType;
4549
import net.imglib2.type.Type;
4650
import net.imglib2.type.numeric.integer.GenericByteType;
@@ -49,6 +53,8 @@
4953
import net.imglib2.type.numeric.integer.GenericShortType;
5054
import net.imglib2.type.numeric.real.DoubleType;
5155
import net.imglib2.type.numeric.real.FloatType;
56+
import net.imglib2.util.Intervals;
57+
import net.imglib2.view.IntervalView;
5258
import net.imglib2.view.Views;
5359

5460
/**
@@ -69,7 +75,7 @@ public class N5CellLoader<T extends NativeType<T>> implements CellLoader<T> {
6975

7076
private final DatasetAttributes attributes;
7177

72-
private final BiConsumer<IterableInterval<T>, DataBlock<?>> copyFromBlock;
78+
private final BiConsumer<SingleCellArrayImg<T, ?>, DataBlock<?>> copyFromBlock;
7379

7480
private final Consumer<IterableInterval<T>> blockNotFoundHandler;
7581

@@ -170,61 +176,79 @@ public static <T extends Type<T>> boolean burnInTestAllEqual(
170176
return equal;
171177
}
172178

173-
public static <T extends NativeType<T>> BiConsumer<IterableInterval<T>, DataBlock<?>> createCopy(
179+
public static <T extends NativeType<T>, I extends RandomAccessibleInterval<T> & IterableInterval<T>> BiConsumer<I, DataBlock<?>> createCopy(
174180
final DataType dataType) {
175181

176182
switch (dataType) {
177183
case INT8:
178184
case UINT8:
179185
return (a, b) -> {
180-
final byte[] data = (byte[])b.getData();
181-
@SuppressWarnings("unchecked")
182-
final Cursor<? extends GenericByteType<?>> c = (Cursor<? extends GenericByteType<?>>)a.cursor();
183-
for (int i = 0; i < data.length; ++i)
184-
c.next().setByte(data[i]);
186+
if (sizeEquals(a, b)) {
187+
final byte[] data = (byte[])b.getData();
188+
@SuppressWarnings("unchecked")
189+
final Cursor<? extends GenericByteType<?>> c = (Cursor<? extends GenericByteType<?>>)a.cursor();
190+
for (int i = 0; i < data.length; ++i)
191+
c.next().setByte(data[i]);
192+
} else
193+
copyIntersection(a, b, dataType);
185194
};
186195
case INT16:
187196
case UINT16:
188197
return (a, b) -> {
189-
final short[] data = (short[])b.getData();
190-
@SuppressWarnings("unchecked")
191-
final Cursor<? extends GenericShortType<?>> c = (Cursor<? extends GenericShortType<?>>)a.cursor();
192-
for (int i = 0; i < data.length; ++i)
193-
c.next().setShort(data[i]);
198+
if (sizeEquals(a, b)) {
199+
final short[] data = (short[])b.getData();
200+
@SuppressWarnings("unchecked")
201+
final Cursor<? extends GenericShortType<?>> c = (Cursor<? extends GenericShortType<?>>)a.cursor();
202+
for (int i = 0; i < data.length; ++i)
203+
c.next().setShort(data[i]);
204+
} else
205+
copyIntersection(a, b, dataType);
194206
};
195207
case INT32:
196208
case UINT32:
197209
return (a, b) -> {
198-
final int[] data = (int[])b.getData();
199-
@SuppressWarnings("unchecked")
200-
final Cursor<? extends GenericIntType<?>> c = (Cursor<? extends GenericIntType<?>>)a.cursor();
201-
for (int i = 0; i < data.length; ++i)
202-
c.next().setInt(data[i]);
210+
if (sizeEquals(a, b)) {
211+
final int[] data = (int[])b.getData();
212+
@SuppressWarnings("unchecked")
213+
final Cursor<? extends GenericIntType<?>> c = (Cursor<? extends GenericIntType<?>>)a.cursor();
214+
for (int i = 0; i < data.length; ++i)
215+
c.next().setInt(data[i]);
216+
} else
217+
copyIntersection(a, b, dataType);
203218
};
204219
case INT64:
205220
case UINT64:
206221
return (a, b) -> {
207-
final long[] data = (long[])b.getData();
208-
@SuppressWarnings("unchecked")
209-
final Cursor<? extends GenericLongType<?>> c = (Cursor<? extends GenericLongType<?>>)a.cursor();
210-
for (int i = 0; i < data.length; ++i)
211-
c.next().setLong(data[i]);
222+
if (sizeEquals(a, b)) {
223+
final long[] data = (long[])b.getData();
224+
@SuppressWarnings("unchecked")
225+
final Cursor<? extends GenericLongType<?>> c = (Cursor<? extends GenericLongType<?>>)a.cursor();
226+
for (int i = 0; i < data.length; ++i)
227+
c.next().setLong(data[i]);
228+
} else
229+
copyIntersection(a, b, dataType);
212230
};
213231
case FLOAT32:
214232
return (a, b) -> {
215-
final float[] data = (float[])b.getData();
216-
@SuppressWarnings("unchecked")
217-
final Cursor<? extends FloatType> c = (Cursor<? extends FloatType>)a.cursor();
218-
for (int i = 0; i < data.length; ++i)
219-
c.next().set(data[i]);
233+
if (sizeEquals(a, b)) {
234+
final float[] data = (float[])b.getData();
235+
@SuppressWarnings("unchecked")
236+
final Cursor<? extends FloatType> c = (Cursor<? extends FloatType>)a.cursor();
237+
for (int i = 0; i < data.length; ++i)
238+
c.next().set(data[i]);
239+
} else
240+
copyIntersection(a, b, dataType);
220241
};
221242
case FLOAT64:
222243
return (a, b) -> {
223-
final double[] data = (double[])b.getData();
224-
@SuppressWarnings("unchecked")
225-
final Cursor<? extends DoubleType> c = (Cursor<? extends DoubleType>)a.cursor();
226-
for (int i = 0; i < data.length; ++i)
227-
c.next().set(data[i]);
244+
if (sizeEquals(a, b)) {
245+
final double[] data = (double[])b.getData();
246+
@SuppressWarnings("unchecked")
247+
final Cursor<? extends DoubleType> c = (Cursor<? extends DoubleType>)a.cursor();
248+
for (int i = 0; i < data.length; ++i)
249+
c.next().set(data[i]);
250+
} else
251+
copyIntersection(a, b, dataType);
228252
};
229253
default:
230254
throw new IllegalArgumentException("Type " + dataType.name() + " not supported!");
@@ -242,4 +266,65 @@ public static <T extends Type<T>, I extends IterableInterval<T>> Consumer<I> set
242266

243267
return rai -> rai.forEach(pixel -> pixel.set(defaultValue));
244268
}
269+
270+
private static boolean sizeEquals(final Interval a, final DataBlock<?> b) {
271+
272+
final int[] dataBlockSize = b.getSize();
273+
for (int d = 0; d < dataBlockSize.length; ++d) {
274+
if (a.dimension(d) != dataBlockSize[d])
275+
return false;
276+
}
277+
return true;
278+
}
279+
280+
@SuppressWarnings("rawtypes")
281+
private static ArrayImg dataBlock2ArrayImg(
282+
final DataBlock<?> dataBlock,
283+
final DataType dataType) {
284+
285+
final int[] dataBlockSize = dataBlock.getSize();
286+
final long[] dims = new long[dataBlockSize.length];
287+
for (int d = 0; d < dataBlockSize.length; ++d)
288+
dims[d] = dataBlockSize[d];
289+
290+
switch (dataType) {
291+
case INT8:
292+
return ArrayImgs.bytes((byte[])dataBlock.getData(), dims);
293+
case UINT8:
294+
return ArrayImgs.unsignedBytes((byte[])dataBlock.getData(), dims);
295+
case INT16:
296+
return ArrayImgs.shorts((short[])dataBlock.getData(), dims);
297+
case UINT16:
298+
return ArrayImgs.unsignedShorts((short[])dataBlock.getData(), dims);
299+
case INT32:
300+
return ArrayImgs.ints((int[])dataBlock.getData(), dims);
301+
case UINT32:
302+
return ArrayImgs.unsignedInts((int[])dataBlock.getData(), dims);
303+
case INT64:
304+
return ArrayImgs.longs((long[])dataBlock.getData(), dims);
305+
case UINT64:
306+
return ArrayImgs.unsignedLongs((long[])dataBlock.getData(), dims);
307+
case FLOAT32:
308+
return ArrayImgs.floats((float[])dataBlock.getData(), dims);
309+
case FLOAT64:
310+
return ArrayImgs.doubles((double[])dataBlock.getData(), dims);
311+
default:
312+
return null;
313+
}
314+
}
315+
316+
private static <T extends NativeType<T>, I extends RandomAccessibleInterval<T> & IterableInterval<T>> void copyIntersection(
317+
final I a,
318+
final DataBlock<?> b,
319+
final DataType dataType) {
320+
321+
@SuppressWarnings("unchecked")
322+
final ArrayImg<T, ?> block = dataBlock2ArrayImg(b, dataType);
323+
final IntervalView<T> za = Views.zeroMin(a);
324+
final FinalInterval intersection = Intervals.intersect(block, za);
325+
final Cursor<T> c = Views.interval(za, intersection).cursor();
326+
final Cursor<T> d = Views.interval(block, intersection).cursor();
327+
while (c.hasNext())
328+
c.next().set(d.next());
329+
}
245330
}

‎src/main/java/org/janelia/saalfeldlab/n5/imglib2/N5Utils.java‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
import java.util.function.Function;
3737
import java.util.function.IntFunction;
3838

39-
import net.imglib2.cache.LoaderCache;
40-
import net.imglib2.cache.ref.BoundedSoftRefLoaderCache;
41-
import net.imglib2.img.basictypeaccess.array.ArrayDataAccess;
4239
import org.janelia.saalfeldlab.n5.Compression;
4340
import org.janelia.saalfeldlab.n5.DataBlock;
4441
import org.janelia.saalfeldlab.n5.DataType;
@@ -50,14 +47,17 @@
5047
import net.imglib2.IterableInterval;
5148
import net.imglib2.RandomAccessibleInterval;
5249
import net.imglib2.cache.Cache;
50+
import net.imglib2.cache.LoaderCache;
5351
import net.imglib2.cache.img.CachedCellImg;
5452
import net.imglib2.cache.img.DiskCachedCellImgFactory;
5553
import net.imglib2.cache.img.DiskCachedCellImgOptions;
5654
import net.imglib2.cache.img.LoadedCellCacheLoader;
55+
import net.imglib2.cache.ref.BoundedSoftRefLoaderCache;
5756
import net.imglib2.cache.ref.SoftRefLoaderCache;
5857
import net.imglib2.img.array.ArrayImgs;
5958
import net.imglib2.img.basictypeaccess.AccessFlags;
6059
import net.imglib2.img.basictypeaccess.ArrayDataAccessFactory;
60+
import net.imglib2.img.basictypeaccess.array.ArrayDataAccess;
6161
import net.imglib2.img.basictypeaccess.volatiles.VolatileAccess;
6262
import net.imglib2.img.cell.Cell;
6363
import net.imglib2.img.cell.CellGrid;
@@ -119,6 +119,7 @@ public static final <T extends NativeType<T>> DataType dataType(final T type) {
119119
return null;
120120
}
121121

122+
@SuppressWarnings("unchecked")
122123
public static final <T extends NativeType<T>> T type(final DataType dataType) {
123124

124125
switch (dataType) {
@@ -647,7 +648,7 @@ public static final <T extends NativeType<T>> RandomAccessibleInterval<T> open(
647648

648649
final DatasetAttributes attributes = n5.getDatasetAttributes(dataset);
649650
final LoaderCache loaderCache = loaderCacheFactory.apply(attributes.getDataType());
650-
T type = type(attributes.getDataType());
651+
final T type = type(attributes.getDataType());
651652
return type == null
652653
? null
653654
: open(n5, dataset, blockNotFoundHandler, loaderCache, accessFlags, type);
@@ -667,8 +668,8 @@ public static final <T extends NativeType<T>, A extends ArrayDataAccess<A>> Cach
667668
final N5Reader n5,
668669
final String dataset,
669670
final Consumer<IterableInterval<T>> blockNotFoundHandler,
670-
LoaderCache<Long, Cell<A>> loaderCache,
671-
Set<AccessFlags> accessFlags,
671+
final LoaderCache<Long, Cell<A>> loaderCache,
672+
final Set<AccessFlags> accessFlags,
672673
final T type) throws IOException {
673674

674675
final DatasetAttributes attributes = n5.getDatasetAttributes(dataset);
@@ -679,7 +680,7 @@ public static final <T extends NativeType<T>, A extends ArrayDataAccess<A>> Cach
679680

680681
final CellGrid grid = new CellGrid(dimensions, blockSize);
681682

682-
Cache<Long, Cell<A>> cache = loaderCache.withLoader(LoadedCellCacheLoader.get(grid, loader, type, accessFlags));
683+
final Cache<Long, Cell<A>> cache = loaderCache.withLoader(LoadedCellCacheLoader.get(grid, loader, type, accessFlags));
683684
final CachedCellImg img = new CachedCellImg(grid, type, cache, ArrayDataAccessFactory.get(type, accessFlags));
684685
return img;
685686
}

0 commit comments

Comments
 (0)