GDCM: ExtractImageRegionWithLUT.cs

GDCM 3.2.11

ExtractImageRegionWithLUT.cs
/*=========================================================================
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.
* Furthermore we are applying the LUT on this image.
* Special care should be taken in case the image is not PALETTE COLOR
*
* Usage:
* $ bin/ExtractImageRegionWithLUT.exe input.dcm
*
* Example:
* $ bin/ExtractImageRegionWithLUT.exe gdcmData/rle16loo.dcm
* $ md5sum /tmp/frame_rgb.raw
* 73bf61325fdb6e2830244a2b7b0c4ae2 /tmp/frame_rgb.raw
* $ gdcmimg --depth 16 --spp 3 --size 600,430 /tmp/frame_rgb.raw rgb.dcm
* $ gdcmviewer rgb.dcm
*/
using System;
using gdcm;
public class ExtractImageRegion
{
public static int Main(string[] args)
{
string filename = args[0];
// instantiate the reader:
gdcm.ImageRegionReader reader = new gdcm.ImageRegionReader();
reader. SetFileName( filename );
// pull DICOM info:
if (!reader. ReadInformation()) return 1;
// Get file infos
gdcm.File f = reader. GetFile();
gdcm.LookupTable lut = reader. GetImage().GetLUT();
// get some info about image
UIntArrayType dims = ImageHelper. GetDimensionsValue(f);
PixelFormat pf = ImageHelper.GetPixelFormatValue (f);
int pixelsize = pf. GetPixelSize();
// buffer to get the pixels
byte[] buffer = new byte[ dims[0] * dims[1] * pixelsize ];
// output buffer for the RGB decoded image:
byte[] buffer2 = new byte[ dims[0] * dims[1] * pixelsize * 3 ];
// 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))
{
if( !lut. Decode( buffer2, (uint)buffer2.Length, buffer, (uint)buffer.Length ) )
{
throw new Exception("can't decode");
}
using (System.IO.Stream stream =
System.IO.File.Open(@"/tmp/frame_rgb.raw",
System.IO.FileMode.Create))
{
System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
writer.Write(buffer2);
}
}
else
{
throw new Exception("can't read pixels error");
}
}
return 0;
}
}
gdcm::Bitmap::GetLUT
const LookupTable & GetLUT() const
Definition gdcmBitmap.h:87
gdcm::BoxRegion
Class for manipulation box region.
Definition gdcmBoxRegion.h:31
gdcm::BoxRegion::SetDomain
void SetDomain(unsigned int xmin, unsigned int xmax, unsigned int ymin, unsigned int ymax, unsigned int zmin, unsigned int zmax)
Set domain.
gdcm::Exception
Exception.
Definition gdcmException.h:46
gdcm::File
a DICOM File
Definition gdcmFile.h:34
gdcm::ImageHelper
ImageHelper (internal class, not intended for user level).
Definition gdcmImageHelper.h:60
gdcm::ImageHelper::GetPixelFormatValue
static PixelFormat GetPixelFormatValue(const File &f)
gdcm::ImageHelper::GetDimensionsValue
static std::vector< unsigned int > GetDimensionsValue(const File &f)
gdcm::ImageReader::GetImage
const Image & GetImage() const
Return the read image.
gdcm::ImageRegionReader
ImageRegionReader.
Definition gdcmImageRegionReader.h:35
gdcm::ImageRegionReader::ReadInformation
bool ReadInformation()
gdcm::ImageRegionReader::ReadIntoBuffer
bool ReadIntoBuffer(char *inreadbuffer, size_t buflen)
gdcm::ImageRegionReader::SetRegion
void SetRegion(Region const &region)
Set/Get Region to be read.
gdcm::LookupTable
LookupTable class.
Definition gdcmLookupTable.h:30
gdcm::LookupTable::Decode
void Decode(std::istream &is, std::ostream &os) const
Decode the LUT.
gdcm::PixelFormat
PixelFormat.
Definition gdcmPixelFormat.h:46
gdcm::PixelFormat::GetPixelSize
uint8_t GetPixelSize() const
gdcm::Reader::GetFile
const File & GetFile() const
Set/Get File.
Definition gdcmReader.h:72
gdcm::Reader::SetFileName
void SetFileName(const char *filename_native)
gdcm::System
Class to do system operation.
Definition gdcmSystem.h:27
gdcm
Definition gdcmASN1.h:21