#include "gdcmReader.h"
#include "gdcmImageReader.h"
#include "gdcmImageWriter.h"
#include "gdcmCSAHeader.h"
#include "gdcmAttribute.h"
#include "gdcmPrivateTag.h"
#include <math.h>
bool reorganize_mosaic(const unsigned short *input, const unsigned int *inputdims, unsigned int square,
const unsigned int *outputdims, unsigned short *output )
{
for(unsigned x = 0; x < outputdims[0]; ++x)
{
for(unsigned y = 0; y < outputdims[1]; ++y)
{
for(unsigned z = 0; z < outputdims[2]; ++z)
{
output[ x + y*outputdims[0] + z*outputdims[0]*outputdims[1] ] =
input[ (x + z * outputdims[0]) + (y + (z/square)*outputdims[0])*inputdims[0] ];
}
}
}
return true;
}
int main(int argc, char *argv [])
{
if( argc < 2 ) return 1;
const char *filename = argv[1];
gdcm::Reader reader;
reader.SetFileName( filename );
if( !reader.Read() )
{
std::cerr << "Failed to read: " << filename << std::endl;
return 1;
}
gdcm::CSAHeader csa;
const gdcm::DataSet& ds = reader.GetFile().GetDataSet();
const gdcm::PrivateTag &t1 = csa.GetCSAImageHeaderInfoTag();
const gdcm::PrivateTag &t2 = csa.GetCSASeriesHeaderInfoTag();
if( ds.FindDataElement( t1 ) )
{
csa.LoadFromDataElement( ds.GetDataElement( t1 ) );
csa.Print( std::cout );
}
#if 1
int dims[2];
const gdcm::CSAElement &csael = csa.GetCSAElementByName( "Columns" );
std::cout << csael << std::endl;
const gdcm::ByteValue *bv = csael.GetByteValue();
gdcm::Element<gdcm::VR::IS, gdcm::VM::VM1> el;
el.Set( csael.GetValue() );
dims[0] = el.GetValue();
std::cout << "Columns:" << el.GetValue() << std::endl;
const gdcm::CSAElement &csael2 = csa.GetCSAElementByName( "Rows" );
std::cout << csael2 << std::endl;
gdcm::Element<gdcm::VR::IS, gdcm::VM::VM1> el2;
el2.Set( csael2.GetValue() );
dims[1] = el2.GetValue();
std::cout << "Rows:" << el2.GetValue() << std::endl;
const gdcm::CSAElement &csael3 = csa.GetCSAElementByName( "PixelSpacing" );
std::cout << csael3 << std::endl;
gdcm::Element<gdcm::VR::DS, gdcm::VM::VM2> el3;
el3.Set( csael3.GetValue() );
double spacing[2];
spacing[0] = el3.GetValue(0);
spacing[1] = el3.GetValue(1);
std::cout << "PixelSpacing:" << el3.GetValue() << "," << el3.GetValue(1) << std::endl;
gdcm::ImageWriter writer;
gdcm::Image &image = writer.GetImage();
image.SetNumberOfDimensions( 2 );
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;
unsigned long l = image.GetBufferLength();
const int p = l / (dims[0] * dims[1]);
gdcm::PhotometricInterpretation pi;
pi = gdcm::PhotometricInterpretation::MONOCHROME2;
image.SetPhotometricInterpretation( pi );
image.SetPixelFormat( pixeltype );
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.SetFileName( outfilename.c_str() );
if( !writer.Write() )
{
std::cerr << "could not write: " << outfilename << std::endl;
return 1;
}
#else
unsigned int dims[3] = {};
{
const gdcm::DataElement& de = ds.GetDataElement( gdcm::Tag(0x0028, 0x0011) );
gdcm::Attribute<0x0028,0x0011> at;
at.SetFromDataElement( de );
dims[0] = at.GetValue();
}
{
const gdcm::DataElement& de = ds.GetDataElement( gdcm::Tag(0x0028, 0x0010) );
gdcm::Attribute<0x0028,0x0010> at;
at.SetFromDataElement( de );
dims[1] = at.GetValue();
}
const gdcm::CSAElement &csael4 = csa.GetCSAElementByName( "NumberOfImagesInMosaic" );
std::cout << csael4 << std::endl;
gdcm::Element<gdcm::VR::IS, gdcm::VM::VM1> el4;
el4.Set( csael4.GetValue() );
int numberOfImagesInMosaic = el4.GetValue();
unsigned int div = (unsigned int)ceil(sqrt( (double)numberOfImagesInMosaic ) );
dims[0] /= div;
dims[1] /= div;
dims[2] = numberOfImagesInMosaic;
std::cout << "NumberOfImagesInMosaic:" << numberOfImagesInMosaic << std::endl;
const double *spacing = inputimage.GetSpacing();
unsigned long l = inputimage.GetBufferLength();
std::vector<char> buf;
buf.resize(l);
inputimage.GetBuffer( &buf[0] );
gdcm::DataElement pixeldata( gdcm::Tag(0x7fe1,0x1010) );
std::vector<char> outbuf;
outbuf.resize(l);
bool b = reorganize_mosaic((unsigned short*)&buf[0], inputimage.GetDimensions(), div, dims, (unsigned short*)&outbuf[0] );
pixeldata.SetByteValue( &outbuf[0], outbuf.size() );
const int p = l / (dims[0] * dims[1]);
std::cout << "VL:" << l << std::endl;
std::cout << "pixel:" << p << std::endl;
gdcm::ImageWriter writer;
gdcm::Image &image = writer.GetImage();
image.SetNumberOfDimensions( 2 );
image.SetDimension(0, dims[0] );
image.SetDimension(1, dims[1] );
image.SetSpacing(0, spacing[0] );
image.SetSpacing(1, spacing[1] );
gdcm::PixelFormat pixeltype = inputimage.GetPixelFormat();
image.SetNumberOfDimensions( 3 );
image.SetDimension(2, numberOfImagesInMosaic );
gdcm::PhotometricInterpretation pi;
pi = gdcm::PhotometricInterpretation::MONOCHROME2;
image.SetPhotometricInterpretation( pi );
image.SetPixelFormat( pixeltype );
image.SetIntercept( inputimage.GetIntercept() );
image.SetSlope( inputimage.GetSlope() );
image.SetDataElement( pixeldata );
std::string outfilename = "outcsa.dcm";
writer.SetFileName( outfilename.c_str() );
if( !writer.Write() )
{
std::cerr << "could not write: " << outfilename << std::endl;
return 1;
}
#endif
return 0;
}