GDCM: ExtractImageRegion.java

GDCM 3.2.11

ExtractImageRegion.java
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*
* This small code shows how to use the gdcm.ImageRegionReader API
* In this example we are taking each frame by frame and dump them to
* /tmp/frame.raw.
*
* Usage:
* $ LD_LIBRARY_PATH=. CLASSPATH=gdcm.jar:. java ExtractImageRegion input.dcm
*/
import gdcm.*;
import java.io.FileOutputStream;
public class ExtractImageRegion
{
public static void main(String[] args) throws Exception
{
String filename = args[0];
// instantiate the reader:
ImageRegionReader reader = new ImageRegionReader();
reader.SetFileName( filename );
// pull DICOM info:
if (!reader.ReadInformation()) return;
// Get file infos
File f = reader.GetFile();
// get some info about image
UIntArrayType dims = ImageHelper.GetDimensionsValue(f);
PixelFormat pf = ImageHelper.GetPixelFormatValue (f);
int pixelsize = pf.GetPixelSize();
// buffer to get the pixels
long buffer_length = dims.get(0) * dims.get(1) * pixelsize;
byte[] buffer = new byte[ (int)buffer_length ];
// define a simple box region.
BoxRegion box = new BoxRegion();
for (int z = 0; z < dims.get(2); z++)
{
// Define that I want the image 0, full size (dimx x dimy pixels)
// and do that for each z:
box.SetDomain(0, dims.get(0) - 1, 0, dims.get(1) - 1, z, z);
//System.Console.WriteLine( box.toString() );
reader.SetRegion( box );
// reader will try to load the uncompressed image region into buffer.
// the call returns an error when buffer.Length is too small. For instance
// one can call:
// long buf_len = reader.ComputeBufferLength(); // take into account pixel size
// to get the exact size of minimum buffer
if (reader.ReadIntoBuffer(buffer, buffer_length))
{
FileOutputStream fos = new FileOutputStream("/tmp/frame.raw");
fos.write(buffer);
fos.close();
}
else
{
throw new Exception("can't read pixels error");
}
}
}
}
gdcm
Definition gdcmASN1.h:21