Class DatasetUtil

java.lang.Object
org.bonej.utilities.DatasetUtil

public final class DatasetUtil extends Object
Utility class for loading raw binary 3D image data into ImageJ2 Datasets.

This class constructs Datasets by wrapping native ImageJ1 ImagePlus objects. This ensures that when legacy plugins convert these Datasets back to ImagePlus, they receive the original native byte[]/short[]/float[] arrays, avoiding the performance overhead of imglib2 wrappers.

  • Method Details

    • loadRaw3D

      public static net.imagej.Dataset loadRaw3D(File filePath, long headerOffset, int width, int height, int depth, String pixelType, boolean zeroOneBinary, String byteOrder, double spacingX, double spacingY, double spacingZ, net.imagej.DatasetService datasetService) throws IOException
      Loads a raw file as a 3D Dataset with spatial calibration.
      Parameters:
      filePath - Path to the raw file on disk
      headerOffset - Number of bytes to skip at the start of the file
      width - Image width (X dimension)
      height - Image height (Y dimension)
      depth - Image depth (Z dimension)
      pixelType - One of: "uint8", "uint16", "int16", "float32"
      zeroOneBinary - true if the raw data is (0,1) binary and we want (0,255) binary. Ignored if image is not 8-bit.
      byteOrder - One of: "Little-endian", "Big-endian"
      spacingX - Voxel spacing in X (mm)
      spacingY - Voxel spacing in Y (mm)
      spacingZ - Voxel spacing in Z (mm)
      datasetService - The DatasetService for creating the Dataset
      Returns:
      A calibrated 3D Dataset backed by a native ImagePlus
      Throws:
      IOException - if the file cannot be read or is too small
      IllegalArgumentException - if parameters are invalid
    • loadRaw3D

      public static net.imagej.Dataset loadRaw3D(File filePath, long headerOffset, int width, int height, int depth, String pixelType, String byteOrder, double spacingX, double spacingY, double spacingZ, net.imagej.DatasetService datasetService) throws IOException
      Overloaded version of loadRaw3D(File, long, int, int, int, String, boolean, String, double, double, double, DatasetService) that does not perform (0,1) to (0,255) binary conversion.
      Parameters:
      filePath - Path to the raw file on disk
      headerOffset - Number of bytes to skip at the start of the file
      width - Image width (X dimension)
      height - Image height (Y dimension)
      depth - Image depth (Z dimension)
      pixelType - One of: "uint8", "uint16", "int16", "float32"
      byteOrder - One of: "Little-endian", "Big-endian"
      spacingX - Voxel spacing in X (mm)
      spacingY - Voxel spacing in Y (mm)
      spacingZ - Voxel spacing in Z (mm)
      datasetService - The DatasetService for creating the Dataset
      Returns:
      A calibrated 3D Dataset backed by a native ImagePlus
      Throws:
      IOException - if the file cannot be read or is too small
      IllegalArgumentException - if parameters are invalid
    • loadRaw2D

      public static net.imagej.Dataset loadRaw2D(File filePath, long headerOffset, int width, int height, String pixelType, boolean zeroOneBinary, String byteOrder, double spacingX, double spacingY, net.imagej.DatasetService datasetService) throws IOException
      Loads a raw file as a 2D Dataset with spatial calibration.
      Parameters:
      filePath - Path to the raw file on disk
      headerOffset - Number of bytes to skip at the start of the file
      width - Image width (X dimension)
      height - Image height (Y dimension)
      pixelType - One of: "uint8", "uint16", "int16", "float32"
      zeroOneBinary - true if the raw data is (0,1) binary and we want (0,255) binary. Ignored if image is not 8-bit.
      byteOrder - One of: "Little-endian", "Big-endian"
      spacingX - Voxel spacing in X (mm)
      spacingY - Voxel spacing in Y (mm)
      datasetService - The DatasetService for creating the Dataset
      Returns:
      A calibrated 3D Dataset backed by a native ImagePlus
      Throws:
      IOException - if the file cannot be read or is too small
      IllegalArgumentException - if parameters are invalid
    • saveAsRaw

      public static void saveAsRaw(net.imagej.Dataset dataset, File outputFile, boolean littleEndian, boolean zeroOneBinary) throws IOException
      Writes the pixel data of a 3D Dataset to a raw binary file. Uses ArrayDataAccess to get the backing array efficiently (no reflection). If the Dataset is backed by a native ImagePlus (not an ArrayImg), it falls back to cursor iteration to write the data correctly.
      Parameters:
      dataset - The 3D Dataset to save
      outputFile - The output file path
      littleEndian - True for little-endian byte order, false for big-endian
      zeroOneBinary - True if writing (0,1) binary data where 1 should be written as 1 byte (not 255)
      Throws:
      IOException - if the file cannot be written
      IllegalArgumentException - if the dataset is not 3D
      UnsupportedOperationException - if the pixel type is unsupported
    • saveAsRaw

      public static void saveAsRaw(net.imagej.Dataset dataset, File outputFile, boolean littleEndian) throws IOException
      Convenience method to save a Dataset to a raw file with default little-endian byte order.
      Parameters:
      dataset - The 3D Dataset to save
      outputFile - The output file path
      littleEndian - Byte order flag
      Throws:
      IOException - if the file cannot be written
    • saveAsRaw

      public static void saveAsRaw(net.imagej.Dataset dataset, File outputFile) throws IOException
      Convenience method to save a Dataset to a raw file with default little-endian byte order and no binary conversion.
      Parameters:
      dataset - The 3D Dataset to save
      outputFile - The output file path
      Throws:
      IOException - if the file cannot be written
    • toImagePlus

      public static ij.ImagePlus toImagePlus(net.imagej.Dataset dataset, org.scijava.convert.ConvertService convertService)
      Converts a Dataset to a native ImagePlus backed by in-memory primitive arrays.
      Parameters:
      dataset - The Dataset to convert
      convertService - The SciJava ConvertService to perform the conversion
      Returns:
      A legacy ImagePlus backed by in-memory ImageStack primitive arrays.
    • toImagePlus

      public static ij.ImagePlus toImagePlus(net.imagej.Dataset dataset, org.scijava.Context context)
      Converts a Dataset to a native ImagePlus using a Context to retrieve the ConvertService.
      Parameters:
      dataset - The Dataset to convert
      context - The SciJava Context
      Returns:
      A legacy ImagePlus backed by in-memory ImageStack primitive arrays.
    • toImagePlus

      public static ij.ImagePlus toImagePlus(net.imagej.Dataset dataset, net.imagej.DatasetService datasetService)
      Converts a Dataset to a native ImagePlus using a DatasetService to retrieve the Context and ConvertService.
      Parameters:
      dataset - The Dataset to convert
      datasetService - The DatasetService providing the Context
      Returns:
      A legacy ImagePlus backed by in-memory ImageStack primitive arrays.