GDCM 3.0.24
csa2img.cxx
/*=========================================================================
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.
=========================================================================*/
/*
* I do not know what the format is, just guessing from info found on the net:
*
* http://atonal.ucdavis.edu/matlab/fmri/spm5/spm_dicom_convert.m
*
* This example is an attempt at understanding the format used by SIEMENS
* their "SIEMENS CSA NON-IMAGE" DICOM file (1.3.12.2.1107.5.9.1)
*
* Everything done in this code is for the sole purpose of writing interoperable
* software under Sect. 1201 (f) Reverse Engineering exception of the DMCA.
* If you believe anything in this code violates any law or any of your rights,
* please contact us (gdcm-developers@lists.sourceforge.net) so that we can
* find a solution.
*
*/
#include "gdcmReader.h"
#include "gdcmCSAHeader.h"
#include "gdcmAttribute.h"
#include "gdcmPrivateTag.h"
#include <math.h>
int main(int argc, char *argv [])
{
if( argc < 2 ) return 1;
// gdcmDataExtra/gdcmNonImageData/exCSA_Non-Image_Storage.dcm
// PHANTOM.MR.CARDIO_COEUR_S_QUENCE_DE_REP_RAGE.9.257.2008.03.20.14.53.25.578125.43151705.IMA
const char *filename = argv[1];
gdcm::Reader reader; // Do not use ImageReader
reader.SetFileName( filename );
if( !reader.Read() )
{
std::cerr << "Failed to read: " << filename << std::endl;
return 1;
}
const gdcm::DataSet& ds = reader.GetFile().GetDataSet();
//std::cout << t1 << std::endl;
//const gdcm::PrivateTag &t2 = csa.GetCSASeriesHeaderInfoTag();
if( ds.FindDataElement( t1 ) )
{
csa.Print( std::cout );
}
int dims[2] = {};
if( csa.FindCSAElementByName( "Columns" ) )
{
const gdcm::CSAElement &csael = csa.GetCSAElementByName( "Columns" );
std::cout << csael << std::endl;
//const gdcm::ByteValue *bv = csael.GetByteValue();
el.Set( csael.GetValue() );
dims[0] = el.GetValue();
std::cout << "Columns:" << el.GetValue() << std::endl;
}
if( csa.FindCSAElementByName( "Rows" ) )
{
const gdcm::CSAElement &csael2 = csa.GetCSAElementByName( "Rows" );
std::cout << csael2 << std::endl;
el2.Set( csael2.GetValue() );
dims[1] = el2.GetValue();
std::cout << "Rows:" << el2.GetValue() << std::endl;
}
double spacing[2] = { 1. , 1. };
bool spacingfound = false;
if( csa.FindCSAElementByName( "PixelSpacing" ) )
{
const gdcm::CSAElement &csael3 = csa.GetCSAElementByName( "PixelSpacing" );
if( !csael3.IsEmpty() )
{
std::cout << csael3 << std::endl;
el3.Set( csael3.GetValue() );
spacing[0] = el3.GetValue(0);
spacing[1] = el3.GetValue(1);
std::cout << "PixelSpacing:" << el3.GetValue() << "," << el3.GetValue(1) << std::endl;
spacingfound = true;
}
}
if( !spacingfound )
{
std::cerr << "Problem with PixelSpacing" << std::endl;
//return 1;
}
if( !dims[0] || !dims[1] )
{
std::cerr << "Problem with dims" << std::endl;
return 1;
}
gdcm::Image &image = writer.GetImage();
image.SetNumberOfDimensions( 2 ); // good default
image.SetDimension(0, dims[0] );
image.SetDimension(1, dims[1] );
image.SetSpacing(0, spacing[0] );
image.SetSpacing(1, spacing[1] );
gdcm::PixelFormat pixeltype = gdcm::PixelFormat::INT16; // bytepix = spm_type('int16','bits')/8;
//unsigned long l = image.GetBufferLength();
//const int p = l / (dims[0] * dims[1]);
//image.SetNumberOfDimensions( 3 );
//image.SetDimension(2, p / pixeltype.GetPixelSize() );
//pixeltype.SetSamplesPerPixel( );
image.SetPixelFormat( pixeltype );
//image.SetIntercept( inputimage.GetIntercept() );
//image.SetSlope( inputimage.GetSlope() );
//gdcm::DataElement pixeldata( gdcm::Tag(0x7fe1,0x1010) );
//pixeldata.SetByteValue( &outbuf[0], outbuf.size() );
gdcm::PrivateTag csanonimaget(0x7fe1,0x10,"SIEMENS CSA NON-IMAGE");
const gdcm::DataElement &pixeldata = ds.GetDataElement( csanonimaget );
image.SetDataElement( pixeldata );
std::string outfilename = "outcsa.dcm";
//writer.SetFile( reader.GetFile() );
writer.SetFileName( outfilename.c_str() );
if( !writer.Write() )
{
std::cerr << "could not write: " << outfilename << std::endl;
return 1;
}
return 0;
}
void SetNumberOfDimensions(unsigned int dim)
void SetDataElement(DataElement const &de)
Definition gdcmBitmap.h:76
void SetDimension(unsigned int idx, unsigned int dim)
void SetPhotometricInterpretation(PhotometricInterpretation const &pi)
void SetPixelFormat(PixelFormat const &pf)
Definition gdcmBitmap.h:115
Class to represent a CSA Element.
Definition gdcmCSAElement.h:30
bool IsEmpty() const
Check if CSA Element is empty.
Definition gdcmCSAElement.h:68
Value const & GetValue() const
Set/Get Value (bytes array, SQ of items, SQ of fragments):
Definition gdcmCSAElement.h:61
Class for CSAHeader.
Definition gdcmCSAHeader.h:64
static const PrivateTag & GetCSAImageHeaderInfoTag()
bool FindCSAElementByName(const char *name)
const CSAElement & GetCSAElementByName(const char *name)
void Print(std::ostream &os) const
Print the CSAHeader (use only if Format == SV10 or NOMAGIC)
bool LoadFromDataElement(DataElement const &de)
Decode the CSAHeader from element 'de'.
Class to represent a Data Element either Implicit or Explicit.
Definition gdcmDataElement.h:59
Class to represent a Data Set (which contains Data Elements)
Definition gdcmDataSet.h:56
const DataElement & GetDataElement(const Tag &t) const
Definition gdcmDataSet.h:188
bool FindDataElement(const PrivateTag &t) const
Look up if private tag 't' is present in the dataset:
Element class.
Definition gdcmElement.h:70
const VRToType< TVR >::Type & GetValue(unsigned int idx=0) const
Definition gdcmElement.h:94
void Set(Value const &v)
Definition gdcmElement.h:161
const DataSet & GetDataSet() const
Get Data Set.
Definition gdcmFile.h:57
ImageWriter.
Definition gdcmImageWriter.h:33
bool Write() override
Write.
const Image & GetImage() const override
Definition gdcmImageWriter.h:41
Image.
Definition gdcmImage.h:47
void SetSpacing(const double spacing[3])
Class to represent an PhotometricInterpretation.
Definition gdcmPhotometricInterpretation.h:29
@ MONOCHROME2
Definition gdcmPhotometricInterpretation.h:34
PixelFormat.
Definition gdcmPixelFormat.h:46
@ INT16
Definition gdcmPixelFormat.h:57
Class to represent a Private DICOM Data Element (Attribute) Tag (Group, Element, Owner)
Definition gdcmPrivateTag.h:39
Reader ala DOM (Document Object Model)
Definition gdcmReader.h:54
const File & GetFile() const
Set/Get File.
Definition gdcmReader.h:72
virtual bool Read()
Main function to read a file.
void SetFileName(const char *filename_native)
void SetFileName(const char *filename_native)
Set the filename of DICOM file to write: