Skip to content

Commit

Permalink
simplify checking for supported DataType
Browse files Browse the repository at this point in the history
  • Loading branch information
tpietzsch committed Sep 25, 2024
1 parent c6e0de2 commit 565a056
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -199,16 +200,10 @@ else if ( storageType == StorageFormat.HDF5 )
}
}

final T type = imgInterval.firstElement().createVariable();
final DataType dataType;

if ( UnsignedByteType.class.isInstance( type ) )
dataType = DataType.UINT8;
else if ( UnsignedShortType.class.isInstance( type ) )
dataType = DataType.UINT16;
else if ( FloatType.class.isInstance( type ) )
dataType = DataType.FLOAT32;
else
final T type = imgInterval.getType();
final DataType dataType = N5Utils.dataType( type );
final EnumSet< DataType > supportedDataTypes = EnumSet.of( DataType.UINT8, DataType.UINT16, DataType.FLOAT32 );
if ( !supportedDataTypes.contains( dataType ) )
throw new RuntimeException( "dataType " + type.getClass().getSimpleName() + " not supported." );

final RandomAccessibleInterval< T > img = Views.zeroMin( imgInterval );
Expand Down
37 changes: 17 additions & 20 deletions src/main/java/net/preibisch/mvrecon/process/n5api/N5ApiTools.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package net.preibisch.mvrecon.process.n5api;

import static org.janelia.saalfeldlab.n5.DataType.FLOAT32;
import static org.janelia.saalfeldlab.n5.DataType.UINT16;
import static org.janelia.saalfeldlab.n5.DataType.UINT8;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -25,8 +30,6 @@
import mpicbg.spim.data.sequence.ViewDescription;
import mpicbg.spim.data.sequence.ViewId;
import mpicbg.spim.data.sequence.ViewSetup;
import mpicbg.spim.data.sequence.VoxelDimensions;
import net.imglib2.Dimensions;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.blocks.BlockAlgoUtils;
import net.imglib2.algorithm.blocks.BlockSupplier;
Expand All @@ -36,10 +39,7 @@
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.integer.IntType;
import net.imglib2.type.numeric.integer.UnsignedByteType;
import net.imglib2.type.numeric.integer.UnsignedShortType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.util.Cast;
import net.imglib2.util.Util;
import net.imglib2.view.Views;
Expand All @@ -50,6 +50,8 @@

public class N5ApiTools
{
static final EnumSet< DataType > supportedDataTypes = EnumSet.of( UINT8, UINT16, FLOAT32 );

public static ViewId gridBlockToViewId( final long[][] gridBlock )
{
if ( gridBlock.length <= 3 )
Expand Down Expand Up @@ -404,7 +406,7 @@ public static < T extends NativeType< T > & RealType< T > > void writeDownsample
final DataType dataType = mrInfo.dataType;// n5.getAttribute( datasetPreviousScale, DatasetAttributes.DATA_TYPE_KEY, DataType.class );
final int[] blockSize = mrInfo.blockSize;// n5.getAttribute( datasetPreviousScale, DatasetAttributes.BLOCK_SIZE_KEY, int[].class );

if ( dataType != DataType.UINT16 && dataType != DataType.UINT8 && dataType != DataType.FLOAT32 )
if ( !supportedDataTypes.contains( dataType ) )
{
n5.close();
throw new RuntimeException("Unsupported pixel type: " + dataType );
Expand Down Expand Up @@ -520,10 +522,10 @@ public static <T extends NativeType<T>> void resaveS0Block(
final ViewId viewId = gridBlockToViewId( gridBlock );
final String dataset = gridBlockToDataset.apply( gridBlock );

if ( dataType != DataType.UINT16 && dataType != DataType.UINT8 && dataType != DataType.FLOAT32 )
if ( !supportedDataTypes.contains( dataType ) )
{
n5.close();
throw new RuntimeException("Unsupported pixel type: " + dataType );
throw new RuntimeException( "Unsupported pixel type: " + dataType );
}

final SetupImgLoader< ? > imgLoader = data.getSequenceDescription().getImgLoader().getSetupImgLoader( viewId.getViewSetupId() );
Expand All @@ -535,25 +537,20 @@ public static <T extends NativeType<T>> void resaveS0Block(
}

public static Map< Integer, DataType > assembleDataTypes(
final AbstractSpimData<?> data,
final AbstractSpimData< ? > data,
final Collection< Integer > viewSetupIds )
{
final HashMap< Integer, DataType > dataTypes = new HashMap<>();

for ( final int viewSetupId : viewSetupIds )
{
final Object type = data.getSequenceDescription().getImgLoader().getSetupImgLoader( viewSetupId ).getImageType();
final DataType dataType;

if ( UnsignedShortType.class.isInstance( type ) )
dataType = DataType.UINT16;
else if ( UnsignedByteType.class.isInstance( type ) )
dataType = DataType.UINT8;
else if ( FloatType.class.isInstance( type ) )
dataType = DataType.FLOAT32;
else
throw new RuntimeException("Unsupported pixel type: " + type.getClass().getCanonicalName() );

final DataType dataType = type instanceof NativeType ? N5Utils.dataType( Cast.unchecked( type ) ) : null;
if ( !supportedDataTypes.contains( dataType ) )
{
throw new RuntimeException( "Unsupported pixel type: " + type.getClass().getCanonicalName() );
}

dataTypes.put( viewSetupId, dataType );
}

Expand Down

0 comments on commit 565a056

Please sign in to comment.