API Example - Extracting an image Slice

This example shows you how to create a simple programme to extract a single image slice from a multi-slice image. The image can be either in UNC, Analyze or NIFTI-1 format. To try out the programme, save the following text to a file called ExtractSlice.java. Make sure that the jar file xinapse5.jar is in your classpath, then compile it with the javac command:

javac ExtractSlice.java

This will create a file called ExtractSlice.class. To run the programme, type (for example):

java ExtractSlice 2 image output_image

where image and output_image are the names of images to extract a slice from and to create, respectively. This example would extract slice 2 from the input image and create a single slice output image containing just slice 2.
import java.io.IOException;

import com.xinapse.loadableimage.*;
import com.xinapse.multisliceimage.*;
import com.xinapse.multisliceimage.UNC.*;
import com.xinapse.multisliceimage.Analyze.*;
import com.xinapse.platform.ExitStatus;

public class ExtractSlice {

  /**
     Java programme to extract a single slice from a multi-slice image.
   */

  public static void main(String argv[]) {

    MultiSliceImage im = null;

    int sliceToExtract = 0;
    int nfiles = argv.length - 1;

    if (nfiles != 2) {
      System.err.println("Usage: ExtractSlice slice_number file op-file");
      System.err.println("     - extract a slice from a multi-slice image");
      System.err.println("       and create a new file");
      System.exit(ExitStatus.INVALID_ARGUMENT);
    }

    try {
      // The user will have supplied a slice indexed from 1. getSlice works with indices
      // from zero.
      sliceToExtract = (new Integer(argv[0])).intValue() - 1;
    }
    catch (NumberFormatException e) {
      System.err.println("Couldn't get slice number to extract from " + argv[0]);
      System.exit(ExitStatus.INVALID_ARGUMENT);
    }

    // Create a MultiSliceImage instance from the supplied file name.
    try {
      im = MultiSliceImage.getInstance(argv[1], "r");
    }
    catch (java.io.IOException ioE) {
      System.err.println("Error processing input data");
      System.err.println(ioE.getMessage());
      System.exit(ExitStatus.IMAGE_OPEN_ERROR);
    }
    
    if (im != null) {
      try {

	int dim = im.getNDim();
	if (dim < 3) {
	  System.err.println
	    ("Can't extract a slice from a " + dim + "-dimensional image"); 
	  System.exit(ExitStatus.INVALID_ARGUMENT);
	}

	int nRows   = im.getNRows();
	int nCols   = im.getNCols();
	int nSlices = im.getTotalNSlices();

	if (sliceToExtract < 0 || sliceToExtract >= nSlices) {
	  System.err.println("Slice to extract out of range: " + (sliceToExtract + 1));
	  System.exit(ExitStatus.INVALID_ARGUMENT);
	}

	// Get the one slice-worth of pixels.
	Object slicePixels = im.getSlice(sliceToExtract);

	// Now do UNC and Analyze-specific things that only that image format can support
	if (im instanceof UNCImage) {

	  int outputDimc = 2;
	  int[] dimv = new int[2];
	  dimv[0] = nRows; 
	  dimv[1] = nCols;
	  try {

	    UNCPixFormat pixformat = ((UNCImage) im).getPixformat();

	    // Create the UNC output image
	    UNCImage UNCim = new UNCImage(argv[2], pixformat, outputDimc, dimv);
	    // Write the pixel values
	    UNCim.putSlice(slicePixels, 0);
	    // Write the info fields. All info is now non-slice specific because in the output
	    // image there's only one slice.
	    // Put the general info to the new image.
	    UNCim.appendInfoList(((UNCImage) im).getInfoList());
	    // Append what was the slice-specific info.
	    UNCim.appendInfoList(((UNCImage) im).getInfoList(0, sliceToExtract));

	    UNCim.close();
	  }
	  catch (UNCException uncE) {
	    System.err.println("Error creating output image: " + uncE.getMessage());
	    System.exit(ExitStatus.IMAGE_CREATE_ERROR);
	  }	  
	  catch (IOException ioE) {
	    System.err.println("Error creating output image: " + ioE.getMessage());
	    System.exit(ExitStatus.IO_ERROR);
	  }	  
	}
	else if (im instanceof Analyze75Image) {
	  try {

	    // Use a different approach for Analyze images.
	    // Clone the original ANZHeader image header
	    Analyze75Header header = null;
	    header = (Analyze75Header) ((ANZImage) im).getHeader().clone();

	    // Set the number of slices and frames to 1;
	    header.setNSlices((short) 1);
	    header.setNFrames((short) 1);

	    // Create the Analyze output image with the new header
	    Analyze75Image ANZim = new Analyze75Image(header);

	    // Write the pixel values
	    ANZim.putSlice(slicePixels, 0);

	    // Write to the output file
	    ANZim.write(argv[2]);
	    ANZim.close();
	  }
	  catch (ANZException anzE) {
	    System.err.println("Error creating output image: " + anzE.getMessage());
	    System.exit(ExitStatus.IMAGE_CREATE_ERROR);
	  }	
	  catch (IOException ioE) {
	    System.err.println("Error creating output image: " + ioE.getMessage());
	    System.exit(ExitStatus.IO_ERROR);
	  }	
	  catch (CloneNotSupportedException cnsE) {
	    System.err.println("Error creating output image: " + cnsE.getMessage());
	    System.exit(ExitStatus.INTERNAL_ERROR);
	  }	  
	}
	
      }
      catch (InvalidImageException iiE) {
	System.err.println("Error writing output image: " + iiE.getMessage());
	System.exit(ExitStatus.IMAGE_CREATE_ERROR);
      }
    }
    System.exit(ExitStatus.NORMAL);
  }
}

    

Jim Home