3131import net .imglib2 .cache .img .optional .CacheOptions ;
3232import net .imglib2 .type .NativeType ;
3333import net .imglib2 .util .Intervals ;
34+ import sc .fiji .snt .SNTUtils ;
3435
36+ import java .nio .file .Path ;
3537import java .util .function .Consumer ;
3638
3739/**
@@ -51,13 +53,40 @@ private Lazy() { }
5153 final int [] blockSize ,
5254 final T type ,
5355 final CellLoader <T > loader )
56+ {
57+ return createImg (targetInterval , blockSize , type , loader , SNTUtils .getCacheDir ().toPath ());
58+ }
59+
60+ /**
61+ * As {@link #createImg(Interval, int[], NativeType, CellLoader)}, but the disk cache is created
62+ * directly under {@code cacheDir} rather than SNT's shared {@link SNTUtils#getCacheDir()}. Use this
63+ * overload when the caller needs to track and explicitly remove this specific cache later (e.g. it
64+ * backs a long-lived image that may be replaced several times within a session, so its disk usage
65+ * would otherwise accumulate until the JVM exits).
66+ */
67+ public static <T extends NativeType <T >> CachedCellImg <T , ?> createImg (
68+ final Interval targetInterval ,
69+ final int [] blockSize ,
70+ final T type ,
71+ final CellLoader <T > loader ,
72+ final Path cacheDir )
5473 {
5574 return new DiskCachedCellImgFactory <>(
5675 type ,
5776 DiskCachedCellImgOptions .options ()
5877 .cellDimensions (blockSize )
5978 .cacheType (CacheOptions .CacheType .SOFTREF )
60- .initializeCellsAsDirty (true ))
79+ .initializeCellsAsDirty (true )
80+ // Nested under SNTUtils#getCacheDir() (or a caller-supplied subdirectory of it)
81+ // rather than left at this library's own default temp location, so this cache
82+ // stays discoverable at a fixed, SNT-owned path alongside DiskBackedStorageBackend's
83+ .tempDirectory (cacheDir )
84+ // Explicit rather than relying on the library's own default: unless a caller
85+ // tracks and removes cacheDir itself (see the overload above), this cache is
86+ // never disposed of explicitly elsewhere in SNT (unlike DiskBackedStorageBackend,
87+ // whose dispose() removes its own temp dir immediately after each run), so a
88+ // JVM-exit shutdown hook is this cache's only fallback cleanup path
89+ .deleteCacheDirectoryOnExit (true ))
6190 .create (Intervals .dimensionsAsLongArray (targetInterval ), loader );
6291 }
6392
@@ -109,6 +138,29 @@ private Lazy() { }
109138 op ));
110139 }
111140
141+ /**
142+ * As {@link #process(RandomAccessibleInterval, Interval, int[], NativeType, UnaryComputerOp)}, but the
143+ * disk cache is created under the caller-supplied {@code cacheDir} -- see
144+ * {@link #createImg(Interval, int[], NativeType, CellLoader, Path)} for when to use this.
145+ */
146+ public static <I , O extends NativeType <O >> CachedCellImg <O , ?> process (
147+ final RandomAccessibleInterval <I > source ,
148+ final Interval sourceInterval ,
149+ final int [] blockSize ,
150+ final O type ,
151+ final UnaryComputerOp <RandomAccessibleInterval <I >, RandomAccessibleInterval <O >> op ,
152+ final Path cacheDir ) {
153+
154+ return createImg (
155+ sourceInterval ,
156+ blockSize ,
157+ type ,
158+ new UnaryComputerOpCellLoader <>(
159+ source ,
160+ op ),
161+ cacheDir );
162+ }
163+
112164 /**
113165 * Create a {@link DiskCachedCellImg} with a cell generator
114166 * {@link UnaryComputerOp} provided by an {@link OpService}.
0 commit comments