|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * ImgLib2: a general-purpose, multidimensional image processing library. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2025 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld, |
| 6 | + * John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke, |
| 7 | + * Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner, |
| 8 | + * Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert, |
| 9 | + * Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin, |
| 10 | + * Jean-Yves Tinevez and Michael Zinsmaier. |
| 11 | + * %% |
| 12 | + * Redistribution and use in source and binary forms, with or without |
| 13 | + * modification, are permitted provided that the following conditions are met: |
| 14 | + * |
| 15 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimer. |
| 17 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 18 | + * this list of conditions and the following disclaimer in the documentation |
| 19 | + * and/or other materials provided with the distribution. |
| 20 | + * |
| 21 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 25 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | + * POSSIBILITY OF SUCH DAMAGE. |
| 32 | + * #L% |
| 33 | + */ |
| 34 | + |
| 35 | +package net.imglib2.imagej.imageplus; |
| 36 | + |
| 37 | +import java.util.Random; |
| 38 | +import java.util.function.Function; |
| 39 | + |
| 40 | +import net.imglib2.exception.InvalidDimensionsException; |
| 41 | +import net.imglib2.util.Util; |
| 42 | +import org.junit.Assert; |
| 43 | +import net.imglib2.Cursor; |
| 44 | +import net.imglib2.RandomAccess; |
| 45 | +import net.imglib2.img.Img; |
| 46 | +import net.imglib2.img.ImgFactory; |
| 47 | +import net.imglib2.outofbounds.OutOfBoundsPeriodicFactory; |
| 48 | +import net.imglib2.type.numeric.real.FloatType; |
| 49 | +import net.imglib2.view.ExtendedRandomAccessibleInterval; |
| 50 | + |
| 51 | +/** |
| 52 | + * Helper class for {@link Img} subclass unit tests. |
| 53 | + * |
| 54 | + * @author Stephan Preibisch |
| 55 | + * @author Stephan Saalfeld |
| 56 | + * @author Curtis Rueden |
| 57 | + * @author Philipp Hanslovsky |
| 58 | + */ |
| 59 | +public class ImgTestHelper |
| 60 | +{ |
| 61 | + // which dimensions to test |
| 62 | + private static final long[][] DIM = |
| 63 | + new long[][] { |
| 64 | + { 127 }, |
| 65 | + { 288 }, |
| 66 | + { 135, 111 }, |
| 67 | + { 172, 131 }, |
| 68 | + { 15, 13, 33 }, |
| 69 | + { 110, 38, 30 }, |
| 70 | + { 109, 34, 111 }, |
| 71 | + { 12, 43, 92, 10 }, |
| 72 | + { 21, 34, 29, 13 }, |
| 73 | + { 5, 12, 30, 4, 21 }, |
| 74 | + { 14, 21, 13, 9, 12 } |
| 75 | + }; |
| 76 | + |
| 77 | + public static long[][] dims() |
| 78 | + { |
| 79 | + return DIM.clone(); |
| 80 | + } |
| 81 | + |
| 82 | + private static final long[][] INVALID_DIM = { |
| 83 | + { -127 }, |
| 84 | + { 0 }, |
| 85 | + { -1, 0 }, |
| 86 | + { -1, 1 }, |
| 87 | + { -3, -2 }, |
| 88 | + { 0, 0, 0 }, |
| 89 | + { -1, 2, 3 }, |
| 90 | + { 4, -2, 100 }, |
| 91 | + { 3151, -235, 8341 }, |
| 92 | + { 34, 5135, 35163, 0 }, |
| 93 | + { 35135, 3531, -2, 1 }, |
| 94 | + }; |
| 95 | + |
| 96 | + public static void assertInvalidDims( final ImgFactory< ? > factory ) |
| 97 | + { |
| 98 | + assertInvalidDims( factory::create ); |
| 99 | + } |
| 100 | + |
| 101 | + public static void assertInvalidDims( final Function< long[], Img< ? > > factory ) |
| 102 | + { |
| 103 | + for ( final long[] dims : INVALID_DIM ) |
| 104 | + assertInvalidDims( dims, factory ); |
| 105 | + } |
| 106 | + |
| 107 | + private static void assertInvalidDims( final long[] dims, final Function< long[], Img< ? > > factory ) |
| 108 | + { |
| 109 | + try |
| 110 | + { |
| 111 | + factory.apply( dims ); |
| 112 | + } |
| 113 | + catch ( final InvalidDimensionsException exception ) |
| 114 | + { |
| 115 | + Assert.assertArrayEquals( dims, exception.getDimenionsCopy() ); |
| 116 | + return; |
| 117 | + } |
| 118 | + catch ( final Throwable exception ) |
| 119 | + { |
| 120 | + |
| 121 | + Assert.fail( String.format( |
| 122 | + "Expected exception of type %s but %s was thrown.", |
| 123 | + InvalidDimensionsException.class.getName(), |
| 124 | + exception.getClass().getName() ) ); |
| 125 | + } |
| 126 | + |
| 127 | + Assert.fail( String.format( |
| 128 | + "Expected exception of type %s but no exception was thrown.", |
| 129 | + InvalidDimensionsException.class.getName() ) ); |
| 130 | + } |
| 131 | + |
| 132 | + public static boolean testImg( final long[] size, final ImgFactory< FloatType > factory1, final ImgFactory< FloatType > factory2 ) |
| 133 | + { |
| 134 | + // create the image |
| 135 | + final Img< FloatType > img1 = factory1.create( size ); |
| 136 | + final Img< FloatType > img2 = factory2.create( size ); |
| 137 | + |
| 138 | + final int numDimensions = img1.numDimensions(); |
| 139 | + |
| 140 | + // get a reference to compare to |
| 141 | + final float[] reference = createReference( img1 ); |
| 142 | + |
| 143 | + // copy into a second image using simple cursors |
| 144 | + final Cursor< FloatType > cursor1 = img1.cursor(); |
| 145 | + final Cursor< FloatType > cursor2 = img2.cursor(); |
| 146 | + |
| 147 | + while ( cursor1.hasNext() ) |
| 148 | + { |
| 149 | + cursor1.fwd(); |
| 150 | + cursor2.fwd(); |
| 151 | + |
| 152 | + cursor2.get().set( cursor1.get() ); |
| 153 | + } |
| 154 | + |
| 155 | + cursor1.reset(); |
| 156 | + cursor2.reset(); |
| 157 | + |
| 158 | + // and copy right back |
| 159 | + while ( cursor2.hasNext() ) |
| 160 | + { |
| 161 | + cursor1.fwd(); |
| 162 | + cursor2.fwd(); |
| 163 | + |
| 164 | + cursor1.get().set( cursor2.get() ); |
| 165 | + } |
| 166 | + |
| 167 | + // copy back into a second image using localizable and positionable |
| 168 | + // cursors |
| 169 | + final Cursor< FloatType > localizableCursor1 = img1.localizingCursor(); |
| 170 | + final RandomAccess< FloatType > positionable2 = img2.randomAccess(); |
| 171 | + |
| 172 | + int i = 0; |
| 173 | + |
| 174 | + while ( localizableCursor1.hasNext() ) |
| 175 | + { |
| 176 | + localizableCursor1.fwd(); |
| 177 | + ++i; |
| 178 | + |
| 179 | + if ( i % 2 == 0 ) |
| 180 | + positionable2.setPosition( localizableCursor1 ); |
| 181 | + else |
| 182 | + positionable2.setPosition( localizableCursor1 ); |
| 183 | + |
| 184 | + final FloatType t2 = positionable2.get(); |
| 185 | + final FloatType t1 = localizableCursor1.get(); |
| 186 | +// float f1 = t1.getRealFloat(); |
| 187 | +// float f2 = t2.getRealFloat(); |
| 188 | + t2.set( t1 ); |
| 189 | +// positionable2.get().set( localizableCursor1.get() ); |
| 190 | + } |
| 191 | + |
| 192 | + // copy again to the first image using a LocalizableByDimOutsideCursor |
| 193 | + // and a LocalizableByDimCursor |
| 194 | + final ExtendedRandomAccessibleInterval< FloatType, Img< FloatType > > extendedImg2 = new ExtendedRandomAccessibleInterval< FloatType, Img< FloatType > >( img2, new OutOfBoundsPeriodicFactory< FloatType, Img< FloatType > >() ); |
| 195 | + final RandomAccess< FloatType > outsideCursor2 = extendedImg2.randomAccess(); |
| 196 | + localizableCursor1.reset(); |
| 197 | + |
| 198 | + final int[] pos = new int[ numDimensions ]; |
| 199 | + i = 0; |
| 200 | + int direction = 1; |
| 201 | + |
| 202 | + try |
| 203 | + { |
| 204 | + while ( localizableCursor1.hasNext() ) |
| 205 | + { |
| 206 | + localizableCursor1.fwd(); |
| 207 | + localizableCursor1.localize( pos ); |
| 208 | + ++i; |
| 209 | + |
| 210 | + // how many times far away from the original image do we grab |
| 211 | + // the pixel |
| 212 | + final int distance = i % 5; |
| 213 | + direction *= -1; |
| 214 | + |
| 215 | + pos[ i % numDimensions ] += img1.dimension( i % numDimensions ) * distance * direction; |
| 216 | + |
| 217 | + if ( i % 7 == 0 ) |
| 218 | + outsideCursor2.setPosition( pos ); |
| 219 | + else |
| 220 | + outsideCursor2.setPosition( pos ); |
| 221 | + |
| 222 | + final FloatType t1 = localizableCursor1.get(); |
| 223 | + final FloatType t2 = outsideCursor2.get(); |
| 224 | + |
| 225 | +// final float f1 = t1.getRealFloat(); |
| 226 | +// final float f2 = t2.getRealFloat(); |
| 227 | + |
| 228 | + t1.set( t2 ); |
| 229 | + |
| 230 | + } |
| 231 | + } |
| 232 | + catch ( final ArrayIndexOutOfBoundsException e ) |
| 233 | + { |
| 234 | + System.err.println( ( i % 7 == 0 ? "setPosition() " : "moveTo() " ) + Util.printCoordinates( pos ) ); |
| 235 | + e.printStackTrace(); |
| 236 | + System.exit( 1 ); |
| 237 | + } |
| 238 | + |
| 239 | + final boolean success = test( img1, reference ); |
| 240 | + |
| 241 | + return success; |
| 242 | + } |
| 243 | + |
| 244 | + private static float[] createReference( final Img< FloatType > img ) |
| 245 | + { |
| 246 | + // use a random number generator |
| 247 | + final Random rnd = new Random( 1241234 ); |
| 248 | + |
| 249 | + // create reference array |
| 250 | + final float[] reference = new float[ ( int ) img.size() ]; |
| 251 | + |
| 252 | + // iterate over image and reference array and fill with data |
| 253 | + final Cursor< FloatType > cursor = img.cursor(); |
| 254 | + int i = 0; |
| 255 | + |
| 256 | + while ( cursor.hasNext() ) |
| 257 | + { |
| 258 | + cursor.fwd(); |
| 259 | + |
| 260 | + final float value = rnd.nextFloat(); |
| 261 | + reference[ i++ ] = value; |
| 262 | + cursor.get().set( value ); |
| 263 | + } |
| 264 | + |
| 265 | + return reference; |
| 266 | + } |
| 267 | + |
| 268 | + private static boolean test( final Img< FloatType > img, final float[] reference ) |
| 269 | + { |
| 270 | + boolean allEqual = true; |
| 271 | + |
| 272 | + final Cursor< FloatType > cursor = img.cursor(); |
| 273 | + int i = 0; |
| 274 | + |
| 275 | + while ( cursor.hasNext() ) |
| 276 | + { |
| 277 | + cursor.fwd(); |
| 278 | + allEqual &= cursor.get().get() == reference[ i++ ]; |
| 279 | + } |
| 280 | + |
| 281 | + return allEqual; |
| 282 | + } |
| 283 | +} |
0 commit comments