Skip to content

Commit ee936c9

Browse files
author
Stefan Hahmann
committed
Rename ImgSizeUtils to ImgUtils and update references across the project.
1 parent b4bc3bb commit ee936c9

5 files changed

Lines changed: 57 additions & 73 deletions

File tree

src/main/java/org/mastodon/mamut/detection/DeepLearningDetector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
import net.imglib2.view.Views;
4343

4444
import org.mastodon.mamut.util.ByteFormatter;
45-
import org.mastodon.mamut.util.ImgSizeUtils;
4645
import org.mastodon.mamut.detection.util.SpimImageProperties;
4746
import org.mastodon.mamut.model.ModelGraph;
47+
import org.mastodon.mamut.util.ImgUtils;
4848
import org.mastodon.mamut.util.LabelImageUtils;
4949
import org.mastodon.tracking.detection.DetectionUtil;
5050
import org.mastodon.tracking.detection.DetectorKeys;
@@ -177,7 +177,7 @@ private void detectAndAddSpots( final List< SourceAndConverter< ? > > sources, f
177177
long theoreticalImageSize = 0;
178178
try
179179
{
180-
theoreticalImageSize = ImgSizeUtils.getSizeInBytes( image );
180+
theoreticalImageSize = ImgUtils.getSizeInBytes( image );
181181
}
182182
catch ( IllegalArgumentException e )
183183
{

src/main/java/org/mastodon/mamut/util/ImgSizeUtils.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/main/java/org/mastodon/mamut/util/ImgUtils.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
import java.util.stream.Collectors;
55

66
import net.imglib2.RandomAccessibleInterval;
7+
import net.imglib2.type.numeric.integer.ByteType;
8+
import net.imglib2.type.numeric.integer.IntType;
9+
import net.imglib2.type.numeric.integer.LongType;
10+
import net.imglib2.type.numeric.integer.ShortType;
11+
import net.imglib2.type.numeric.integer.UnsignedByteType;
12+
import net.imglib2.type.numeric.integer.UnsignedIntType;
13+
import net.imglib2.type.numeric.integer.UnsignedLongType;
14+
import net.imglib2.type.numeric.integer.UnsignedShortType;
15+
import net.imglib2.type.numeric.real.DoubleType;
16+
import net.imglib2.type.numeric.real.FloatType;
717

818
public class ImgUtils
919
{
@@ -17,4 +27,35 @@ public static String getImageDimensionsAsString( final RandomAccessibleInterval<
1727
return Arrays.stream( image.dimensionsAsLongArray() ).mapToObj( String::valueOf ).collect( Collectors.joining( ", " ) );
1828
}
1929

30+
/**
31+
* Computes the size in bytes of the provided {@code RandomAccessibleInterval}.
32+
* The size is calculated based on the number of elements in the interval and the memory size
33+
* of each element type.
34+
*
35+
* @param rai the {@code RandomAccessibleInterval} whose theoretical size is to be calculated
36+
* @return the size in bytes of the {@code RandomAccessibleInterval}
37+
* @throws IllegalArgumentException if the element type of the {@code RandomAccessibleInterval} is not supported.
38+
* Supported types are: {@link UnsignedByteType}, {@link ByteType}, {@link UnsignedShortType}, {@link ShortType}, {@link UnsignedIntType},
39+
* {@link IntType}, {@link FloatType}, {@link UnsignedLongType}, {@link LongType}, {@link DoubleType}
40+
*/
41+
public static long getSizeInBytes( final RandomAccessibleInterval< ? > rai )
42+
{
43+
long bytesPerElement;
44+
Object type = rai.randomAccess().getType();
45+
if ( type instanceof UnsignedByteType )
46+
bytesPerElement = 1;
47+
else if ( type instanceof ByteType )
48+
bytesPerElement = 1;
49+
else if ( type instanceof UnsignedShortType || type instanceof ShortType )
50+
bytesPerElement = 2;
51+
else if ( type instanceof UnsignedIntType || type instanceof IntType || type instanceof FloatType )
52+
bytesPerElement = 4;
53+
else if ( type instanceof UnsignedLongType || type instanceof LongType || type instanceof DoubleType )
54+
bytesPerElement = 8;
55+
else
56+
throw new IllegalArgumentException( "Unknown element type: " + type.getClass() );
57+
58+
long size = rai.size();
59+
return size * bytesPerElement;
60+
}
2061
}

src/test/java/org/mastodon/mamut/detection/ShmImgDemo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import net.imglib2.img.array.ArrayImgs;
66

77
import org.mastodon.mamut.util.ByteFormatter;
8-
import org.mastodon.mamut.util.ImgSizeUtils;
8+
import org.mastodon.mamut.util.ImgUtils;
99

1010
public class ShmImgDemo
1111
{
1212
public static void main( String[] args )
1313
{
1414
ArrayImg< ?, ? > works = ArrayImgs.unsignedShorts( 1024, 1024, 1023 );
15-
long size = ImgSizeUtils.getSizeInBytes( works );
15+
long size = ImgUtils.getSizeInBytes( works );
1616
String formattedSize = ByteFormatter.humanReadableByteCount( size );
1717
System.out.println( "Size of the image: " + size + " bytes (" + formattedSize + ")" );
1818
try (ShmImg< ? > shmImg = ShmImg.copyOf( works ))

src/test/java/org/mastodon/mamut/util/ImgSizeUtilsTest.java renamed to src/test/java/org/mastodon/mamut/util/ImgUtilsTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.junit.jupiter.api.Assertions;
1919
import org.junit.jupiter.api.Test;
2020

21-
class ImgSizeUtilsTest
21+
class ImgUtilsTest
2222
{
2323

2424
/**
@@ -30,86 +30,86 @@ class ImgSizeUtilsTest
3030
void testGetSizeInBytes_WithUnsignedByteType()
3131
{
3232
RandomAccessibleInterval< UnsignedByteType > rai = ArrayImgs.unsignedBytes( 10, 10 );
33-
long result = ImgSizeUtils.getSizeInBytes( rai );
33+
long result = ImgUtils.getSizeInBytes( rai );
3434
Assertions.assertEquals( 100, result );
3535
}
3636

3737
@Test
3838
void testGetSizeInBytes_WithByteType()
3939
{
4040
RandomAccessibleInterval< ByteType > rai = ArrayImgs.bytes( 5, 20 );
41-
long result = ImgSizeUtils.getSizeInBytes( rai );
41+
long result = ImgUtils.getSizeInBytes( rai );
4242
Assertions.assertEquals( 100, result );
4343
}
4444

4545
@Test
4646
void testGetSizeInBytes_WithUnsignedShortType()
4747
{
4848
RandomAccessibleInterval< UnsignedShortType > rai = ArrayImgs.unsignedShorts( 5, 4 );
49-
long result = ImgSizeUtils.getSizeInBytes( rai );
49+
long result = ImgUtils.getSizeInBytes( rai );
5050
Assertions.assertEquals( 40, result );
5151
}
5252

5353
@Test
5454
void testGetSizeInBytes_WithShortType()
5555
{
5656
RandomAccessibleInterval< ShortType > rai = ArrayImgs.shorts( 3, 7 );
57-
long result = ImgSizeUtils.getSizeInBytes( rai );
57+
long result = ImgUtils.getSizeInBytes( rai );
5858
Assertions.assertEquals( 42, result );
5959
}
6060

6161
@Test
6262
void testGetSizeInBytes_WithUnsignedIntType()
6363
{
6464
RandomAccessibleInterval< UnsignedIntType > rai = ArrayImgs.unsignedInts( 2, 5 );
65-
long result = ImgSizeUtils.getSizeInBytes( rai );
65+
long result = ImgUtils.getSizeInBytes( rai );
6666
Assertions.assertEquals( 40, result );
6767
}
6868

6969
@Test
7070
void testGetSizeInBytes_WithIntType()
7171
{
7272
RandomAccessibleInterval< IntType > rai = ArrayImgs.ints( 2, 10 );
73-
long result = ImgSizeUtils.getSizeInBytes( rai );
73+
long result = ImgUtils.getSizeInBytes( rai );
7474
Assertions.assertEquals( 80, result );
7575
}
7676

7777
@Test
7878
void testGetSizeInBytes_WithFloatType()
7979
{
8080
RandomAccessibleInterval< FloatType > rai = ArrayImgs.floats( 10 );
81-
long result = ImgSizeUtils.getSizeInBytes( rai );
81+
long result = ImgUtils.getSizeInBytes( rai );
8282
Assertions.assertEquals( 40, result );
8383
}
8484

8585
@Test
8686
void testGetSizeInBytes_WithUnsignedLongType()
8787
{
8888
RandomAccessibleInterval< UnsignedLongType > rai = ArrayImgs.unsignedLongs( 5, 2 );
89-
long result = ImgSizeUtils.getSizeInBytes( rai );
89+
long result = ImgUtils.getSizeInBytes( rai );
9090
Assertions.assertEquals( 80, result );
9191
}
9292

9393
@Test
9494
void testGetSizeInBytes_WithLongType()
9595
{
9696
RandomAccessibleInterval< LongType > rai = ArrayImgs.longs( 4, 5 );
97-
long result = ImgSizeUtils.getSizeInBytes( rai );
97+
long result = ImgUtils.getSizeInBytes( rai );
9898
Assertions.assertEquals( 160, result );
9999
}
100100

101101
@Test
102102
void testGetSizeInBytes_WithDoubleType()
103103
{
104104
RandomAccessibleInterval< DoubleType > rai = ArrayImgs.doubles( 3, 5 );
105-
long result = ImgSizeUtils.getSizeInBytes( rai );
105+
long result = ImgUtils.getSizeInBytes( rai );
106106
Assertions.assertEquals( 120, result );
107107
}
108108

109109
@Test
110110
void testGetSizeInBytes_WithUnknownType()
111111
{
112112
RandomAccessibleInterval< ComplexDoubleType > rai = ArrayImgs.complexDoubles( 10 );
113-
Assertions.assertThrows( IllegalArgumentException.class, () -> ImgSizeUtils.getSizeInBytes( rai ) );
113+
Assertions.assertThrows( IllegalArgumentException.class, () -> ImgUtils.getSizeInBytes( rai ) );
114114
}
115115
}

0 commit comments

Comments
 (0)