1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
/*=========================================================================
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:
* $ bin/ExtractImageRegion.exe input.dcm
*
* Example:
* $ bin/ExtractImageRegion.exe gdcmData/012345.002.050.dcm
* $ md5sum /tmp/frame.raw
* d594a5e2fde12f32b6633ca859b4d4a6 /tmp/frame.raw
* $ gdcminfo --md5sum gdcmData/012345.002.050.dcm
* [...]
* md5sum: d594a5e2fde12f32b6633ca859b4d4a6
*/
using System;
using gdcm;
public class ExtractImageRegion
{
public static int Main(string[] args)
{
string filename = args[0];
uint file_size = gdcm.PosixEmulation.FileSize(filename);
// instantiate the reader:
gdcm.ImageRegionReader reader = new gdcm.ImageRegionReader();
reader.SetFileName( filename );
// pull DICOM info:
if (!reader.ReadInformation()) return 1;
// store current offset:
uint cur_pos = reader.GetStreamCurrentPosition();
uint remaining = file_size - cur_pos;
Console.WriteLine("Remaining bytes to read (Pixel Data): " + remaining.ToString() );
// Get file infos
gdcm.File f = reader.GetFile();
// get some info about image
UIntArrayType dims = ImageHelper.GetDimensionsValue(f);
PixelFormat pf = ImageHelper.GetPixelFormatValue (f);
int pixelsize = pf.GetPixelSize();
PhotometricInterpretation pi = ImageHelper.GetPhotometricInterpretationValue(f);
Console.WriteLine( pi.toString() );
// buffer to get the pixels
byte[] buffer = new byte[ dims[0] * dims[1] * pixelsize ];
// define a simple box region.
BoxRegion box = new BoxRegion();
for (uint z = 0; z < dims[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[0] - 1, 0, dims[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:
// uint buf_len = reader.ComputeBufferLength(); // take into account pixel size
// to get the exact size of minimum buffer
if (reader.ReadIntoBuffer(buffer, (uint)buffer.Length))
{
using (System.IO.Stream stream =
System.IO.File.Open(@"/tmp/frame.raw",
System.IO.FileMode.Create))
{
System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
writer.Write(buffer);
}
}
else
{
throw new Exception("can't read pixels error");
}
}
return 0;
}
}
|