|
31 | 31 | import static org.junit.Assert.assertEquals; |
32 | 32 | import static org.junit.Assert.assertTrue; |
33 | 33 |
|
| 34 | +import javax.imageio.ImageIO; |
| 35 | +import javax.imageio.ImageReader; |
| 36 | +import java.awt.image.BufferedImage; |
| 37 | +import java.io.File; |
34 | 38 | import java.io.IOException; |
35 | 39 | import java.net.URISyntaxException; |
36 | | -import java.net.URL; |
37 | 40 | import java.nio.file.Files; |
38 | 41 | import java.nio.file.Paths; |
| 42 | +import java.util.ArrayList; |
39 | 43 | import java.util.HashMap; |
40 | 44 | import java.util.Iterator; |
| 45 | +import java.util.List; |
41 | 46 | import java.util.Map; |
42 | 47 |
|
43 | 48 | import gnu.trove.list.array.TLongArrayList; |
44 | 49 | import org.apache.commons.math3.geometry.euclidean.threed.Vector3D; |
45 | 50 | import org.junit.Test; |
46 | 51 |
|
47 | | -import io.scif.img.IO; |
48 | 52 | import net.imglib2.Point; |
49 | 53 | import net.imglib2.RandomAccess; |
50 | 54 | import net.imglib2.RandomAccessibleInterval; |
51 | 55 | import net.imglib2.img.Img; |
52 | 56 | import net.imglib2.img.array.ArrayImgs; |
| 57 | +import net.imglib2.img.basictypeaccess.array.FloatArray; |
| 58 | +import net.imglib2.img.planar.PlanarImg; |
53 | 59 | import net.imglib2.mesh.impl.naive.NaiveDoubleMesh; |
54 | 60 | import net.imglib2.roi.labeling.ImgLabeling; |
55 | 61 | import net.imglib2.roi.labeling.LabelRegion; |
|
58 | 64 | import net.imglib2.type.numeric.RealType; |
59 | 65 | import net.imglib2.type.numeric.integer.IntType; |
60 | 66 | import net.imglib2.type.numeric.real.FloatType; |
| 67 | +import net.imglib2.util.Fraction; |
61 | 68 | import net.imglib2.view.RandomAccessibleIntervalCursor; |
62 | 69 |
|
63 | 70 | public class MeshesTest |
@@ -173,8 +180,14 @@ private static Mesh createMeshWithNoise() |
173 | 180 |
|
174 | 181 | private static Img< FloatType > getTestImage3D() |
175 | 182 | { |
176 | | - final URL url = MeshesTest.class.getResource( "3d_geometric_features_testlabel.tif" ); |
177 | | - return IO.openAllFloat( url.getPath() ).get( 0 ).getImg(); |
| 183 | + try |
| 184 | + { |
| 185 | + return openFloatImg( "3d_geometric_features_testlabel.tif" ); |
| 186 | + } |
| 187 | + catch ( IOException exc ) |
| 188 | + { |
| 189 | + throw new RuntimeException( exc ); |
| 190 | + } |
178 | 191 | } |
179 | 192 |
|
180 | 193 | private static Mesh getMesh() |
@@ -239,7 +252,49 @@ protected static < T extends RealType< T > > LabelRegion< String > createLabelRe |
239 | 252 | final LabelRegions< String > labelRegions = new LabelRegions<>( labeling ); |
240 | 253 |
|
241 | 254 | return labelRegions.getLabelRegion( "1" ); |
242 | | - |
243 | 255 | } |
244 | 256 |
|
| 257 | + private static Img< FloatType > openFloatImg( String path ) throws IOException |
| 258 | + { |
| 259 | + final ImageReader reader = ImageIO.getImageReadersByFormatName( "TIFF" ).next(); |
| 260 | + |
| 261 | + // TODO: Make this work from a resource URL rather than a File. |
| 262 | + //final URL url = MeshesTest.class.getResource( path ); |
| 263 | + //reader.setInput( ImageIO.createImageInputStream( url ) ); |
| 264 | + final File file = new File( "src/test/resources/net/imglib2/mesh/" + path ); |
| 265 | + reader.setInput( ImageIO.createImageInputStream( file ) ); |
| 266 | + |
| 267 | + final int pageCount = reader.getNumImages( true ); |
| 268 | + if ( pageCount < 1 ) |
| 269 | + { |
| 270 | + throw new IOException( "No image planes detected for path: " + path ); |
| 271 | + } |
| 272 | + final List< FloatArray > planes = new ArrayList<>(); |
| 273 | + int width = -1, height = -1; |
| 274 | + for ( int i = 0; i < pageCount; i++ ) |
| 275 | + { |
| 276 | + final BufferedImage bufferedImage = reader.read( i ); |
| 277 | + final int w = bufferedImage.getWidth(); |
| 278 | + final int h = bufferedImage.getHeight(); |
| 279 | + if (i == 0) |
| 280 | + { |
| 281 | + width = w; |
| 282 | + height = h; |
| 283 | + } |
| 284 | + else if ( width != w || height != h ) |
| 285 | + { |
| 286 | + throw new UnsupportedOperationException( |
| 287 | + "Multi-plane image dimensions differ between planes: " + |
| 288 | + w + "x" + h + " != " + width + "x" + height |
| 289 | + ); |
| 290 | + } |
| 291 | + final float[] pixels = new float[ 4 * w * h ]; |
| 292 | + bufferedImage.getData().getPixels(0, 0, w, h, pixels ); |
| 293 | + planes.add( new FloatArray( pixels ) ); |
| 294 | + } |
| 295 | + final PlanarImg< FloatType, FloatArray > img = new PlanarImg<>( planes, new long[] { width, height, pageCount }, new Fraction() ); |
| 296 | + final FloatType t = new FloatType( img ); |
| 297 | + img.setLinkedType( t ); |
| 298 | + return img; |
| 299 | + } |
245 | 300 | } |
0 commit comments