Skip to content

Commit ebe999f

Browse files
committed
Replace SCIFIO test dep with twelvemonkeys imageio
At some point we could consider putting some general-purpose "read an ImgLib2 Img from javax.imageio" code somewhere more visible, but for now, it gets the job done.
1 parent f6270e3 commit ebe999f

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

pom.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@
120120

121121
<!-- NB: Deploy releases to the SciJava Maven repository. -->
122122
<releaseProfiles>sign,deploy-to-scijava</releaseProfiles>
123+
124+
<imageio-tiff.version>3.9.4</imageio-tiff.version>
123125
</properties>
124126

125127
<repositories>
@@ -176,8 +178,9 @@
176178
<scope>test</scope>
177179
</dependency>
178180
<dependency>
179-
<groupId>io.scif</groupId>
180-
<artifactId>scifio</artifactId>
181+
<groupId>com.twelvemonkeys.imageio</groupId>
182+
<artifactId>imageio-tiff</artifactId>
183+
<version>${imageio-tiff.version}</version>
181184
<scope>test</scope>
182185
</dependency>
183186
</dependencies>

src/test/java/net/imglib2/mesh/MeshesTest.java

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,31 @@
3131
import static org.junit.Assert.assertEquals;
3232
import static org.junit.Assert.assertTrue;
3333

34+
import javax.imageio.ImageIO;
35+
import javax.imageio.ImageReader;
36+
import java.awt.image.BufferedImage;
37+
import java.io.File;
3438
import java.io.IOException;
3539
import java.net.URISyntaxException;
36-
import java.net.URL;
3740
import java.nio.file.Files;
3841
import java.nio.file.Paths;
42+
import java.util.ArrayList;
3943
import java.util.HashMap;
4044
import java.util.Iterator;
45+
import java.util.List;
4146
import java.util.Map;
4247

4348
import gnu.trove.list.array.TLongArrayList;
4449
import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;
4550
import org.junit.Test;
4651

47-
import io.scif.img.IO;
4852
import net.imglib2.Point;
4953
import net.imglib2.RandomAccess;
5054
import net.imglib2.RandomAccessibleInterval;
5155
import net.imglib2.img.Img;
5256
import net.imglib2.img.array.ArrayImgs;
57+
import net.imglib2.img.basictypeaccess.array.FloatArray;
58+
import net.imglib2.img.planar.PlanarImg;
5359
import net.imglib2.mesh.impl.naive.NaiveDoubleMesh;
5460
import net.imglib2.roi.labeling.ImgLabeling;
5561
import net.imglib2.roi.labeling.LabelRegion;
@@ -58,6 +64,7 @@
5864
import net.imglib2.type.numeric.RealType;
5965
import net.imglib2.type.numeric.integer.IntType;
6066
import net.imglib2.type.numeric.real.FloatType;
67+
import net.imglib2.util.Fraction;
6168
import net.imglib2.view.RandomAccessibleIntervalCursor;
6269

6370
public class MeshesTest
@@ -173,8 +180,14 @@ private static Mesh createMeshWithNoise()
173180

174181
private static Img< FloatType > getTestImage3D()
175182
{
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+
}
178191
}
179192

180193
private static Mesh getMesh()
@@ -239,7 +252,49 @@ protected static < T extends RealType< T > > LabelRegion< String > createLabelRe
239252
final LabelRegions< String > labelRegions = new LabelRegions<>( labeling );
240253

241254
return labelRegions.getLabelRegion( "1" );
242-
243255
}
244256

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+
}
245300
}

0 commit comments

Comments
 (0)