22 * #%L
33 * ImgLib2: a general-purpose, multidimensional image processing library.
44 * %%
5- * Copyright (C) 2009 - 2018 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
5+ * Copyright (C) 2009 - 2021 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
66 * John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke,
77 * Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner,
88 * Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert,
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
6262import net .imglib2 .type .numeric .real .DoubleType ;
6363import net .imglib2 .type .numeric .real .FloatType ;
6464import net .imglib2 .util .Intervals ;
65+ import net .imglib2 .view .Views ;
6566
6667/**
67- * Convenience methods to create lazy evaluated cached cell images with ops or consumers.
68- *
69- * Warning: this class or its functionality could be moved elsewhere in the near future, likely
70- * imglib2-cache or imgli2-core.
68+ * Convenience methods to create lazy evaluated cached cell images with
69+ * consumers.
7170 *
72- * @author Stephan Saalfeld <saalfelds@janelia.hhmi.org>
71+ * Warning: this class or its functionality could be moved elsewhere in the near
72+ * future, likely imglib2-cache or imgli2-core.
73+ *
74+ * @author Stephan Saalfeld
7375 */
74- public class Lazy {
75-
76- private Lazy () {}
76+ public class Lazy
77+ {
78+ private Lazy ()
79+ {}
7780
7881 /**
7982 * Create a memory {@link CachedCellImg} with a cell {@link Cache}.
@@ -84,28 +87,37 @@ private Lazy() {}
8487 * @param accessFlags
8588 * @return
8689 */
87- @ SuppressWarnings ({"unchecked" , "rawtypes" })
88- public static <T extends NativeType <T >> CachedCellImg <T , ?> createImg (
89- final CellGrid grid ,
90- final Cache <Long , Cell <?>> cache ,
91- final T type ,
92- final Set <AccessFlags > accessFlags ) {
90+ @ SuppressWarnings ( { "unchecked" , "rawtypes" } )
91+ private static < T extends NativeType < T > > CachedCellImg < T , ? > createImg ( final CellGrid grid , final Cache < Long , Cell < ? > > cache , final T type , final Set < AccessFlags > accessFlags )
92+ {
93+ final CachedCellImg < T , ? > img ;
9394
94- final CachedCellImg <T , ?> img ;
95-
96- if (GenericByteType .class .isInstance (type )) {
97- img = new CachedCellImg (grid , type , cache , ArrayDataAccessFactory .get (BYTE , accessFlags ));
98- } else if (GenericShortType .class .isInstance (type )) {
99- img = new CachedCellImg (grid , type , cache , ArrayDataAccessFactory .get (SHORT , accessFlags ));
100- } else if (GenericIntType .class .isInstance (type )) {
101- img = new CachedCellImg (grid , type , cache , ArrayDataAccessFactory .get (INT , accessFlags ));
102- } else if (GenericLongType .class .isInstance (type )) {
103- img = new CachedCellImg (grid , type , cache , ArrayDataAccessFactory .get (LONG , accessFlags ));
104- } else if (FloatType .class .isInstance (type )) {
105- img = new CachedCellImg (grid , type , cache , ArrayDataAccessFactory .get (FLOAT , accessFlags ));
106- } else if (DoubleType .class .isInstance (type )) {
107- img = new CachedCellImg (grid , type , cache , ArrayDataAccessFactory .get (DOUBLE , accessFlags ));
108- } else {
95+ if ( GenericByteType .class .isInstance ( type ) )
96+ {
97+ img = new CachedCellImg ( grid , type , cache , ArrayDataAccessFactory .get ( BYTE , accessFlags ) );
98+ }
99+ else if ( GenericShortType .class .isInstance ( type ) )
100+ {
101+ img = new CachedCellImg ( grid , type , cache , ArrayDataAccessFactory .get ( SHORT , accessFlags ) );
102+ }
103+ else if ( GenericIntType .class .isInstance ( type ) )
104+ {
105+ img = new CachedCellImg ( grid , type , cache , ArrayDataAccessFactory .get ( INT , accessFlags ) );
106+ }
107+ else if ( GenericLongType .class .isInstance ( type ) )
108+ {
109+ img = new CachedCellImg ( grid , type , cache , ArrayDataAccessFactory .get ( LONG , accessFlags ) );
110+ }
111+ else if ( FloatType .class .isInstance ( type ) )
112+ {
113+ img = new CachedCellImg ( grid , type , cache , ArrayDataAccessFactory .get ( FLOAT , accessFlags ) );
114+ }
115+ else if ( DoubleType .class .isInstance ( type ) )
116+ {
117+ img = new CachedCellImg ( grid , type , cache , ArrayDataAccessFactory .get ( DOUBLE , accessFlags ) );
118+ }
119+ else
120+ {
109121 img = null ;
110122 }
111123 return img ;
@@ -121,25 +133,29 @@ private Lazy() {}
121133 * @param loader
122134 * @return
123135 */
124- public static <T extends NativeType <T >> CachedCellImg <T , ?> createImg (
125- final Interval targetInterval ,
126- final int [] blockSize ,
127- final T type ,
128- final Set <AccessFlags > accessFlags ,
129- final CellLoader <T > loader ) {
136+ private static < T extends NativeType < T > > CachedCellImg < T , ? > createImg ( final Interval targetInterval , final int [] blockSize , final T type , final Set < AccessFlags > accessFlags , final Consumer < RandomAccessibleInterval < T > > loader )
137+ {
138+ final long [] dimensions = Intervals .dimensionsAsLongArray ( targetInterval );
139+ final CellGrid grid = new CellGrid ( dimensions , blockSize );
130140
131- final long [] dimensions = Intervals .dimensionsAsLongArray (targetInterval );
132- final CellGrid grid = new CellGrid (dimensions , blockSize );
141+ final CellLoader < T > offsetLoader ;
142+ if ( Views .isZeroMin ( targetInterval ) )
143+ offsetLoader = loader ::accept ;
144+ else
145+ {
146+ final long [] offset = targetInterval .minAsLongArray ();
147+ offsetLoader = cell -> loader .accept ( Views .translate ( cell , offset ) );
148+ }
133149
134- @ SuppressWarnings ({"unchecked" , "rawtypes" })
135- final Cache <Long , Cell <?>> cache =
136- new SoftRefLoaderCache ().withLoader (LoadedCellCacheLoader .get (grid , loader , type , accessFlags ));
150+ @ SuppressWarnings ( { "unchecked" , "rawtypes" } )
151+ final Cache < Long , Cell < ? > > cache = new SoftRefLoaderCache ().withLoader ( LoadedCellCacheLoader .get ( grid , offsetLoader , type , accessFlags ) );
137152
138- return createImg (grid , cache , type , accessFlags );
153+ return createImg ( grid , cache , type , accessFlags );
139154 }
140155
141156 /**
142- * Create a memory {@link CachedCellImg} with a cell generator {@link Consumer}.
157+ * Create a memory {@link CachedCellImg} with a cell generator
158+ * {@link Consumer}.
143159 *
144160 * @param targetInterval
145161 * @param blockSize
@@ -148,18 +164,8 @@ private Lazy() {}
148164 * @param op
149165 * @return
150166 */
151- public static <T extends NativeType <T >> CachedCellImg <T , ?> process (
152- final Interval targetInterval ,
153- final int [] blockSize ,
154- final T type ,
155- final Set <AccessFlags > accessFlags ,
156- final Consumer <RandomAccessibleInterval <T >> op ) {
157-
158- return createImg (
159- targetInterval ,
160- blockSize ,
161- type ,
162- accessFlags ,
163- op ::accept );
167+ public static < T extends NativeType < T > > CachedCellImg < T , ? > generate ( final Interval targetInterval , final int [] blockSize , final T type , final Set < AccessFlags > accessFlags , final Consumer < RandomAccessibleInterval < T > > op )
168+ {
169+ return createImg ( targetInterval , blockSize , type , accessFlags , op );
164170 }
165- }
171+ }
0 commit comments