28 #include <vnl/vnl_vector.h>
29 #include <vnl/vnl_matrix.h>
30 #include <vnl/vnl_cross.h>
34 #include <itksys/SystemTools.hxx>
35 #include <itksys/Base64.h>
37 #if GDCM_MAJOR_VERSION < 2
38 #include "gdcmValEntry.h"
39 #include "gdcmBinEntry.h"
40 #include "gdcmFileHelper.h"
42 #include "gdcmGlobal.h"
43 #include "gdcmDictSet.h"
45 #include "gdcmImageHelper.h"
46 #include "gdcmFileExplicitFilter.h"
47 #include "gdcmImageChangeTransferSyntax.h"
48 #include "gdcmDataSetHelper.h"
49 #include "gdcmStringFilter.h"
50 #include "gdcmImageApplyLookupTable.h"
51 #include "gdcmImageChangePlanarConfiguration.h"
52 #include "gdcmUnpacker12Bits.h"
53 #include "gdcmRescaler.h"
54 #include "gdcmFileMetaInformation.h"
55 #include "gdcmImageReader.h"
56 #include "gdcmImageWriter.h"
57 #include "gdcmUIDGenerator.h"
58 #include "gdcmAttribute.h"
59 #include "gdcmGlobal.h"
60 #include "gdcmDicts.h"
61 #include "gdcmDictEntry.h"
66 #include <itksys/ios/sstream>
103 #if GDCM_MAJOR_VERSION < 2
110 unsigned char VBitsAllocated,
111 unsigned char VBitsStored,
112 unsigned char VHighBit,
113 unsigned char VPixelRepresentation
123 static unsigned short min(
void) {
return 0; }
124 static unsigned short max(
void) {
return 4096; }
130 static signed short min(
void) {
return -2048; }
131 static signed short max(
void) {
return 2047; }
138 double operator() (
double value,
double slope,
double intercept)
140 return value * slope + intercept;
146 double operator() (
double value,
double slope,
double intercept)
148 return ( value - intercept ) / slope;
152 template <
typename PixelType,
typename TOperation>
162 dmax = op(maximum, slope , intercept);
163 dmin = op(minimum, slope , intercept);
209 : m_LoadSequences( m_LoadSequencesDefault ),
210 m_LoadPrivateTags( m_LoadPrivateTagsDefault )
252 const char* filename)
255 if ( *filename == 0 )
257 itkExceptionMacro(<<
"A FileName must be specified.");
267 itkDebugMacro(<<
"Initialize: opening file " << filename);
270 os.open( filename, std::ios::in | std::ios::binary );
282 const char* filename)
285 if ( *filename == 0 )
287 itkExceptionMacro(<<
"A FileName must be specified.");
297 itkDebugMacro(<<
"Initialize: opening file " << filename);
301 std::ofstream tFile(filename,std::ios::out);
306 os.open( filename, std::ios::out | std::ios::binary );
310 itkExceptionMacro(<<
"Could not open file: "
311 << filename <<
" for writing."
314 << itksys::SystemTools::GetLastSystemError());
326 std::string fname(filename);
330 itkDebugMacro(<<
"No filename specified.");
342 #if GDCM_MAJOR_VERSION < 2
344 if( gdcm::Document::CanReadFile(file, preamble) )
346 itkWarningMacro(<<
"The DICOM file: "
348 <<
" does not have a preamble.");
352 gdcm::ImageReader reader;
353 reader.SetFileName( filename );
362 #if GDCM_MAJOR_VERSION < 2
365 #define DUFF_DEVICE_8(aCount, aAction) \
367 const size_t count_ = (aCount); \
368 register size_t times_ = (count_ + 7) >> 3; \
369 switch (count_ & 7){ \
370 case 0: do { aAction; \
378 } while (--times_ > 0); \
383 template<
class TBuffer,
class TSource>
385 double slope,
double intercept,
size_t size)
387 size /=
sizeof(TSource);
389 if (slope != 1.0 && intercept != 0.0)
399 DUFF_DEVICE_8(size, *buffer++ = (TBuffer)((*source++)*slope + intercept) );
401 else if (slope == 1.0 && intercept != 0.0)
411 TSource sintercept = (TSource)intercept;
412 if (sintercept == intercept)
416 DUFF_DEVICE_8(size, *buffer++ = (TBuffer)(*source++ + sintercept) );
420 DUFF_DEVICE_8(size, *buffer++ = (TBuffer)(*source++ + intercept) );
423 else if (slope != 1.0 && intercept == 0.0)
433 DUFF_DEVICE_8(size, *buffer++ = (TBuffer)((*source++)*slope) );
454 template<
class TBuffer,
class TSource>
456 double slope,
double intercept,
size_t size)
458 size /=
sizeof(TSource);
460 if (slope != 1.0 && intercept != 0.0)
470 DUFF_DEVICE_8(size, *buffer++ = (TBuffer)((*source++ - intercept) / slope) );
472 else if (slope == 1.0 && intercept != 0.0)
482 TSource sintercept = (TSource)intercept;
483 if (sintercept == intercept)
487 DUFF_DEVICE_8(size, *buffer++ = (TBuffer)(*source++ - sintercept) );
491 DUFF_DEVICE_8(size, *buffer++ = (TBuffer)(*source++ - intercept) );
494 else if (slope != 1.0 && intercept == 0.0)
504 DUFF_DEVICE_8(size, *buffer++ = (TBuffer)((*source++) / slope) );
521 template<
class TSource>
523 void* buffer, TSource *source,
524 double slope,
double intercept,
size_t size)
529 RescaleFunction( (
unsigned char *)buffer, source, slope, intercept, size);
535 RescaleFunction( (
unsigned short *)buffer, source, slope, intercept,size);
541 RescaleFunction( (
unsigned int *)buffer, source, slope, intercept, size);
553 ::itk::OStringStream message;
554 message <<
"itk::ERROR: GDCMImageIO: Unknown component type : " << bufferType;
560 template<
class TSource>
562 void* buffer, TSource *source,
563 double slope,
double intercept,
size_t size)
593 ::itk::OStringStream message;
594 message <<
"itk::ERROR: GDCMImageIO: Unknown component type : " << bufferType;
602 #if GDCM_MAJOR_VERSION < 2
609 if( !header->IsReadable() )
611 itkExceptionMacro(<<
"Could not read file: "
614 << itksys::SystemTools::GetLastSystemError());
616 gdcm::FileHelper gfile(header);
618 size_t size = gfile.GetImageDataSize();
622 size = gfile.GetImageDataRawSize();
626 unsigned char *source = (
unsigned char*)gfile.GetImageData();
688 memcpy(buffer, source, size);
699 assert( gdcm::ImageHelper::GetForceRescaleInterceptSlope() );
700 gdcm::ImageReader reader;
701 reader.SetFileName( filename );
704 itkExceptionMacro(<<
"Cannot read requested file");
708 gdcm::Image &image = reader.GetImage();
709 assert( image.GetNumberOfDimensions() == 2 || image.GetNumberOfDimensions() == 3 );
710 unsigned long len = image.GetBufferLength();
712 const unsigned long numberOfBytesToBeRead =
716 if( image.GetPlanarConfiguration() == 1 )
718 gdcm::ImageChangePlanarConfiguration icpc;
719 icpc.SetInput( image );
720 icpc.SetPlanarConfiguration( 0 );
722 image = icpc.GetOutput();
725 if( image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::PALETTE_COLOR )
727 gdcm::ImageApplyLookupTable ialut;
728 ialut.SetInput( image );
730 image = ialut.GetOutput();
734 image.GetBuffer((
char*)pointer);
736 const gdcm::PixelFormat &pixeltype = image.GetPixelFormat();
737 if( pixeltype == gdcm::PixelFormat::UINT12 || pixeltype == gdcm::PixelFormat::INT12 )
740 assert( pixeltype.GetSamplesPerPixel() == 1 );
742 char * copy =
new char[len];
743 memcpy(copy, pointer, len);
744 gdcm::Unpacker12Bits u12;
745 u12.Unpack((
char*)pointer, copy, len);
755 r.SetPixelFormat( pixeltype );
756 gdcm::PixelFormat outputpt = r.ComputeInterceptSlopePixelType();
757 char * copy =
new char[len];
758 memcpy(copy, (
char*)pointer, len);
759 r.Rescale((
char*)pointer,copy,len);
762 len = len * outputpt.GetPixelSize() / pixeltype.GetPixelSize();
767 assert( numberOfBytesToBeRead == len );
773 #if GDCM_MAJOR_VERSION < 2
779 itkExceptionMacro(<<
"Could not read file: "
782 << itksys::SystemTools::GetLastSystemError());
785 gdcm::File *header =
new gdcm::File;
792 bool headerLoaded = header->Load();
795 itkExceptionMacro(<<
"Could not load header from file: "
798 << itksys::SystemTools::GetLastSystemError());
800 if( !header->IsReadable() )
802 itkExceptionMacro(<<
"Could not read header from file: "
805 << itksys::SystemTools::GetLastSystemError());
813 int numComp = header->GetNumberOfScalarComponents();
823 std::string type = header->GetPixelType();
828 else if( type ==
"8S")
832 else if( type ==
"16U")
836 else if( type ==
"16S")
840 else if( type ==
"32U")
844 else if( type ==
"32S")
848 else if ( type ==
"FD" )
855 itkExceptionMacro(<<
"Unrecognized type:" << type <<
" in file " <<
m_FileName);
872 float imageOrientation[6];
873 header->GetImageOrientationPatient(imageOrientation);
874 vnl_vector<double> rowDirection(3), columnDirection(3);
876 rowDirection[0] = imageOrientation[0];
877 rowDirection[1] = imageOrientation[1];
878 rowDirection[2] = imageOrientation[2];
880 columnDirection[0] = imageOrientation[3];
881 columnDirection[1] = imageOrientation[4];
882 columnDirection[2] = imageOrientation[5];
884 vnl_vector<double> sliceDirection = vnl_cross_3d(rowDirection, columnDirection);
912 if (slope_dif > 0.0 || inter_dif > 0.0)
955 if( header->GetEntryValue(0x0028, 0x0101) ==
"12" )
957 std::string sign = header->GetEntryValue(0x0028, 0x0103);
963 else if ( sign ==
"1" )
970 itkExceptionMacro(<<
"Pixel Representation cannot be handled: " << sign );
978 gdcm::DocEntry* d = header->GetFirstEntry();
984 if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
986 if (b->GetName() !=
"Pixel Data" && b->GetName() != gdcm::GDCM_UNKNOWN
987 && b->GetVR() !=
"UN" )
989 if (b->GetValue() == gdcm::GDCM_BINLOADED )
992 int encodedLengthEstimate = 2 * b->GetLength();
993 encodedLengthEstimate = ((encodedLengthEstimate / 4) + 1) * 4;
995 char *bin =
new char[encodedLengthEstimate];
996 unsigned int encodedLengthActual =
static_cast<unsigned int>(
998 (
const unsigned char *) b->GetBinArea(),
999 static_cast< unsigned long>( b->GetLength() ),
1000 (
unsigned char *) bin,
1001 static_cast< int >( 0 ) ));
1002 std::string encodedValue(bin, encodedLengthActual);
1003 EncapsulateMetaData<std::string>(dico, b->GetKey(), encodedValue);
1008 else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
1011 if( v->GetName() != gdcm::GDCM_UNKNOWN )
1013 EncapsulateMetaData<std::string>(dico, v->GetKey(), v->GetValue() );
1019 d = header->GetNextEntry();
1045 std::string PrintAsPipeSeparatedString(
const gdcm::Tag& tag)
1047 itksys_ios::ostringstream os;
1048 os << std::hex << std::setw( 4 ) << std::setfill(
'0' )
1049 << tag[0] <<
'|' << std::setw( 4 ) << std::setfill(
'0' )
1051 std::string ret = os.str();
1060 itkExceptionMacro(<<
"Cannot read requested file");
1064 gdcm::ImageHelper::SetForceRescaleInterceptSlope(
true);
1067 gdcm::ImageReader reader;
1068 reader.SetFileName( filename );
1069 if( !reader.Read() )
1071 itkExceptionMacro(<<
"Cannot read requested file");
1073 const gdcm::Image &image = reader.GetImage();
1074 const gdcm::File &f = reader.GetFile();
1075 const gdcm::DataSet &ds = f.GetDataSet();
1076 const unsigned int *dims = image.GetDimensions();
1078 const gdcm::PixelFormat &pixeltype = image.GetPixelFormat();
1085 r.SetPixelFormat( pixeltype );
1086 gdcm::PixelFormat::ScalarType outputpt = r.ComputeInterceptSlopePixelType();
1088 assert( pixeltype <= outputpt );
1093 case gdcm::PixelFormat::INT8:
1096 case gdcm::PixelFormat::UINT8:
1100 case gdcm::PixelFormat::INT12:
1103 case gdcm::PixelFormat::UINT12:
1106 case gdcm::PixelFormat::INT16:
1109 case gdcm::PixelFormat::UINT16:
1113 case gdcm::PixelFormat::INT32:
1116 case gdcm::PixelFormat::UINT32:
1120 case gdcm::PixelFormat::FLOAT32:
1123 case gdcm::PixelFormat::FLOAT64:
1127 itkExceptionMacro(
"Unhandled PixelFormat: " << outputpt );
1131 if( image.GetPhotometricInterpretation() ==
1132 gdcm::PhotometricInterpretation::PALETTE_COLOR )
1154 const double *spacing = image.GetSpacing();
1159 const double *origin = image.GetOrigin();
1164 if( image.GetNumberOfDimensions() == 3 )
1173 const double *dircos = image.GetDirectionCosines();
1174 vnl_vector<double> rowDirection(3), columnDirection(3);
1175 rowDirection[0] = dircos[0];
1176 rowDirection[1] = dircos[1];
1177 rowDirection[2] = dircos[2];
1178 columnDirection[0] = dircos[3];
1179 columnDirection[1] = dircos[4];
1180 columnDirection[2] = dircos[5];
1182 vnl_vector<double> sliceDirection = vnl_cross_3d(rowDirection, columnDirection);
1190 gdcm::StringFilter sf;
1192 gdcm::DataSet::ConstIterator it = ds.Begin();
1195 for(; it != ds.End(); ++it)
1197 const gdcm::DataElement &ref = *it;
1198 const gdcm::Tag &tag = ref.GetTag();
1200 gdcm::VR vr = gdcm::DataSetHelper::ComputeVR(f, ds, tag);
1204 if ( vr & (gdcm::VR::OB | gdcm::VR::OF | gdcm::VR::OW | gdcm::VR::SQ | gdcm::VR::UN) )
1212 && tag != gdcm::Tag(0x7fe0,0x0010) )
1214 const gdcm::ByteValue *bv = ref.GetByteValue();
1218 int encodedLengthEstimate = 2 * bv->GetLength();
1219 encodedLengthEstimate = ((encodedLengthEstimate / 4) + 1) * 4;
1221 char *bin =
new char[encodedLengthEstimate];
1222 unsigned int encodedLengthActual =
static_cast<unsigned int>(
1223 itksysBase64_Encode(
1224 (
const unsigned char *) bv->GetPointer(),
1225 static_cast< unsigned long>( bv->GetLength() ),
1226 (
unsigned char *) bin,
1227 static_cast< int >( 0 ) ));
1228 std::string encodedValue(bin, encodedLengthActual);
1229 EncapsulateMetaData<std::string>(dico, PrintAsPipeSeparatedString(tag), encodedValue);
1239 EncapsulateMetaData<std::string>(dico, PrintAsPipeSeparatedString(tag), sf.ToString( tag ) );
1277 std::string filename = name;
1279 if( filename ==
"" )
1281 itkDebugMacro(<<
"No filename specified.");
1285 std::string::size_type dcmPos = filename.rfind(
".dcm");
1286 if ( (dcmPos != std::string::npos)
1287 && (dcmPos == filename.length() - 4) )
1292 dcmPos = filename.rfind(
".DCM");
1293 if ( (dcmPos != std::string::npos)
1294 && (dcmPos == filename.length() - 4) )
1299 std::string::size_type dicomPos = filename.rfind(
".dicom");
1300 if ( (dicomPos != std::string::npos)
1301 && (dicomPos == filename.length() - 6) )
1306 dicomPos = filename.rfind(
".DICOM");
1307 if ( (dicomPos != std::string::npos)
1308 && (dicomPos == filename.length() - 6) )
1320 #if GDCM_MAJOR_VERSION < 2
1330 gdcm::File *header =
new gdcm::File();
1331 gdcm::FileHelper *gfile =
new gdcm::FileHelper( header );
1335 #if defined(_MSC_VER) && _MSC_VER < 1300
1339 std::vector<std::string> keys = dict.
GetKeys();
1340 for( std::vector<std::string>::const_iterator it = keys.begin();
1341 it != keys.end(); ++it )
1343 const std::string &key = *it;
1350 const std::string &key = itr->first;
1352 ExposeMetaData<std::string>(dict, key, value);
1355 gdcm::DictEntry *dictEntry =
1356 header->GetPubDict()->GetEntry(key);
1361 if (dictEntry->GetVR() !=
"OB" && dictEntry->GetVR() !=
"OW")
1366 if(dictEntry->GetElement() != 0)
1368 header->InsertValEntry( value,
1369 dictEntry->GetGroup(),
1370 dictEntry->GetElement());
1377 unsigned int decodedLengthActual =
static_cast<unsigned int>(
1378 itksysBase64_Decode(
1379 (
const unsigned char *) value.c_str(),
1380 static_cast<unsigned long>( 0 ),
1381 (
unsigned char *) bin,
1382 static_cast<unsigned long>( value.size())));
1383 if(dictEntry->GetGroup() != 0 || dictEntry->GetElement() != 0)
1385 header->InsertBinEntry( bin,
1386 decodedLengthActual,
1387 dictEntry->GetGroup(),
1388 dictEntry->GetElement());
1399 unsigned int numberOfDimensions = 0;
1400 ExposeMetaData<unsigned int>(dict, key, numberOfDimensions);
1402 m_Origin.resize( m_GlobalNumberOfDimensions );
1403 m_Spacing.resize( m_GlobalNumberOfDimensions );
1408 DoubleArrayType originArray;
1409 ExposeMetaData< DoubleArrayType >( dict, key, originArray );
1411 m_Origin[1] = originArray[1];
1412 m_Origin[2] = originArray[2];
1417 DoubleArrayType spacingArray;
1418 ExposeMetaData< DoubleArrayType >( dict, key, spacingArray );
1420 m_Spacing[1] = spacingArray[1];
1421 m_Spacing[2] = spacingArray[2];
1426 <<
"GDCMImageIO: non-DICOM and non-ITK standard key = " << key );
1430 #if !(defined(_MSC_VER) && _MSC_VER < 1300)
1436 itksys_ios::ostringstream str;
1438 header->InsertValEntry( str.str(), 0x0028,0x0011);
1441 str << m_Dimensions[1];
1442 header->InsertValEntry( str.str(), 0x0028,0x0010);
1444 if( m_Dimensions.size() > 2 && m_Dimensions[2] > 1 )
1447 str << m_Dimensions[2];
1449 header->InsertValEntry(str.str(),0x0028,0x0008);
1454 str.setf( itksys_ios::ios::fixed );
1456 header->InsertValEntry(str.str(),0x0028,0x0030);
1460 if(m_Dimensions.size() > 2 && m_Dimensions[2]>1)
1463 str << m_Spacing[2];
1464 header->InsertValEntry(str.str(),0x0018,0x0088);
1478 if( !header->GetValEntry(0x0020,0x0032 ) )
1483 if( m_Origin.size() == 3 )
1493 header->InsertValEntry(str.str(),0x0020,0x0032);
1499 if( !header->GetValEntry(0x0020,0x0037 ) )
1508 if( m_Direction.size() == 3 )
1510 str << m_Direction[0][2] <<
"\\";
1516 str << m_Direction[1][0] <<
"\\"
1517 << m_Direction[1][1] <<
"\\";
1518 if( m_Direction.size() == 3 )
1520 str << m_Direction[1][2];
1526 header->InsertValEntry(str.str(),0x0020,0x0037);
1529 str.unsetf( itksys_ios::ios::fixed );
1536 std::string rescaleintercept;
1537 ExposeMetaData<std::string>(dict,
"0028|1052" , rescaleintercept);
1538 std::string rescaleslope;
1539 ExposeMetaData<std::string>(dict,
"0028|1053" , rescaleslope);
1540 if( rescaleintercept !=
"" && rescaleslope !=
"" )
1542 itksys_ios::stringstream sstr1;
1543 sstr1 << rescaleintercept;
1546 itkExceptionMacro(
"Problem reading RescaleIntercept: " << rescaleintercept );
1548 itksys_ios::stringstream sstr2;
1549 sstr2 << rescaleslope;
1552 itkExceptionMacro(
"Problem reading RescaleSlope: " << rescaleslope );
1554 header->InsertValEntry(
"US", 0x0028, 0x1054 );
1556 else if( rescaleintercept !=
"" || rescaleslope !=
"" )
1558 itkExceptionMacro(
"Both RescaleSlope & RescaleIntercept need to be present" );
1562 gfile->SetWriteTypeToDcmExplVR();
1565 std::string bitsAllocated;
1566 std::string bitsStored;
1567 std::string highBit;
1568 std::string pixelRep;
1570 ExposeMetaData<std::string>(dict,
"0028|0100", bitsAllocated);
1571 ExposeMetaData<std::string>(dict,
"0028|0101", bitsStored);
1572 ExposeMetaData<std::string>(dict,
"0028|0102", highBit);
1573 ExposeMetaData<std::string>(dict,
"0028|0103", pixelRep);
1575 if( bitsAllocated ==
"" || bitsStored ==
"" || highBit ==
"" || pixelRep ==
"" )
1582 bitsAllocated =
"8";
1589 bitsAllocated =
"8";
1596 bitsAllocated =
"16";
1603 bitsAllocated =
"16";
1611 bitsAllocated =
"32";
1618 bitsAllocated =
"32";
1628 itkExceptionMacro(<<
"A Floating point buffer was passed but the stored pixel type was not specified."
1629 "This is currently not supported" );
1632 itkExceptionMacro(<<
"DICOM does not support this component type");
1638 gfile->SetWriteModeToRGB();
1642 bitsAllocated =
"8";
1649 bitsAllocated =
"8";
1655 itkExceptionMacro(<<
"DICOM does not support this component type");
1661 <<
"DICOM does not support RGBPixels with components != 3");
1665 header->InsertValEntry( bitsAllocated, 0x0028, 0x0100 );
1666 header->InsertValEntry( bitsStored, 0x0028, 0x0101 );
1667 header->InsertValEntry( highBit, 0x0028, 0x0102 );
1668 header->InsertValEntry( pixelRep, 0x0028, 0x0103 );
1672 header->InsertValEntry(str.str(),0x0028,0x0002);
1675 std::string type = header->GetPixelType();
1680 else if( type ==
"8S")
1684 else if( type ==
"16U")
1688 else if( type ==
"16S")
1692 else if( type ==
"32U")
1696 else if( type ==
"32S")
1702 itkExceptionMacro(<<
"Unrecognized type:" << type <<
" in file " <<
m_FileName);
1717 std::string uid = gdcm::Util::CreateUniqueUID(
m_UIDPrefix );
1719 header->InsertValEntry( uid, 0x0008, 0x0018);
1720 header->InsertValEntry( uid, 0x0002, 0x0003);
1733 size_t numberOfBytes;
1734 #if GDCM_MAJOR_VERSION <= 1 && GDCM_MINOR_VERSION <= 2 && GDCM_BUILD_VERSION <= 3
1735 numberOfBytes = size;
1737 numberOfBytes = gfile->ComputeExpectedImageDataSize();
1749 if( m_NumberOfComponents == 1 )
1808 memcpy(imageData, buffer, numberOfBytes);
1816 gfile->SetWriteTypeToJPEG();
1820 gfile->SetWriteTypeToJPEG2000();
1824 itkExceptionMacro(<<
"Unknown compression type" );
1828 gfile->SetUserData( imageData, numberOfBytes);
1831 itkExceptionMacro(<<
"Cannot write the requested file:"
1835 << itksys::SystemTools::GetLastSystemError());
1839 delete [] imageData;
1853 gdcm::UIDGenerator::SetRoot(
m_UIDPrefix.c_str() );
1856 gdcm::FileMetaInformation::AppendImplementationClassUID(
"111.124.113" );
1858 gdcm::FileMetaInformation::SetSourceApplicationEntityTitle( project_name.c_str() );
1860 gdcm::ImageWriter writer;
1861 gdcm::DataSet &header = writer.GetFile().GetDataSet();
1862 gdcm::Global &g = gdcm::Global::GetInstance();
1863 const gdcm::Dicts &dicts = g.GetDicts();
1864 const gdcm::Dict &pubdict = dicts.GetPublicDict();
1872 gdcm::StringFilter sf;
1873 sf.SetFile( writer.GetFile() );
1877 const std::string &key = itr->first;
1878 ExposeMetaData<std::string>(dict, key, value);
1881 bool b = tag.ReadFromPipeSeparatedString( key.c_str() );
1887 const gdcm::DictEntry &dictEntry = pubdict.GetDictEntry(tag);
1888 gdcm::VR::VRType vrtype = dictEntry.GetVR();
1889 if ( dictEntry.GetVR() == gdcm::VR::SQ )
1893 else if ( vrtype & (gdcm::VR::OB | gdcm::VR::OF | gdcm::VR::OW | gdcm::VR::UN) )
1898 unsigned int decodedLengthActual =
static_cast<unsigned int>(
1899 itksysBase64_Decode(
1900 (
const unsigned char *) value.c_str(),
1901 static_cast<unsigned long>( 0 ),
1902 (
unsigned char *) bin,
1903 static_cast<unsigned long>( value.size())));
1904 if( tag.GetElement() != 0)
1906 gdcm::DataElement de( tag );
1907 de.SetByteValue( (
char*)bin, decodedLengthActual );
1908 de.SetVR( dictEntry.GetVR() );
1909 header.Insert( de );
1918 if(!tag.IsGroupLength())
1920 gdcm::DataElement de( tag );
1921 if( dictEntry.GetVR().IsVRFile() )
1922 de.SetVR( dictEntry.GetVR() );
1923 #if GDCM_MAJOR_VERSION == 2 && GDCM_MINOR_VERSION <= 12
1925 de.SetByteValue( value.c_str(), value.size() );
1927 std::string si = sf.FromString( tag, value.c_str(), value.size() );
1928 de.SetByteValue( si.c_str(), si.size() );
1930 header.Insert( de );
1940 unsigned int numberOfDimensions = 0;
1941 ExposeMetaData<unsigned int>(dict, key, numberOfDimensions);
1943 m_Origin.resize( m_GlobalNumberOfDimensions );
1944 m_Spacing.resize( m_GlobalNumberOfDimensions );
1949 DoubleArrayType originArray;
1950 ExposeMetaData< DoubleArrayType >( dict, key, originArray );
1952 m_Origin[1] = originArray[1];
1953 m_Origin[2] = originArray[2];
1958 DoubleArrayType spacingArray;
1959 ExposeMetaData< DoubleArrayType >( dict, key, spacingArray );
1961 m_Spacing[1] = spacingArray[1];
1962 m_Spacing[2] = spacingArray[2];
1967 <<
"GDCMImageIO: non-DICOM and non-ITK standard key = " << key );
1977 gdcm::SmartPointer<gdcm::Image> simage =
new gdcm::Image;
1978 gdcm::Image &image = *simage;
1979 image.SetNumberOfDimensions( 2 );
1992 image.SetNumberOfDimensions( 3 );
2009 std::string rescaleintercept;
2010 ExposeMetaData<std::string>(dict,
"0028|1052" , rescaleintercept);
2011 std::string rescaleslope;
2012 ExposeMetaData<std::string>(dict,
"0028|1053" , rescaleslope);
2013 if( rescaleintercept !=
"" && rescaleslope !=
"" )
2015 itksys_ios::stringstream sstr1;
2016 sstr1 << rescaleintercept;
2019 itkExceptionMacro(
"Problem reading RescaleIntercept: " << rescaleintercept );
2021 itksys_ios::stringstream sstr2;
2022 sstr2 << rescaleslope;
2025 itkExceptionMacro(
"Problem reading RescaleSlope: " << rescaleslope );
2029 else if( rescaleintercept !=
"" || rescaleslope !=
"" )
2031 itkExceptionMacro(
"Both RescaleSlope & RescaleIntercept need to be present" );
2035 std::string bitsAllocated;
2036 std::string bitsStored;
2037 std::string highBit;
2038 std::string pixelRep;
2040 ExposeMetaData<std::string>(dict,
"0028|0100", bitsAllocated);
2041 ExposeMetaData<std::string>(dict,
"0028|0101", bitsStored);
2042 ExposeMetaData<std::string>(dict,
"0028|0102", highBit);
2043 ExposeMetaData<std::string>(dict,
"0028|0103", pixelRep);
2049 pixeltype = gdcm::PixelFormat::INT8;
2052 pixeltype = gdcm::PixelFormat::UINT8;
2055 pixeltype = gdcm::PixelFormat::INT16;
2058 pixeltype = gdcm::PixelFormat::UINT16;
2061 pixeltype = gdcm::PixelFormat::INT32;
2064 pixeltype = gdcm::PixelFormat::UINT32;
2068 pixeltype = gdcm::PixelFormat::FLOAT32;
2071 pixeltype = gdcm::PixelFormat::FLOAT64;
2074 itkExceptionMacro(<<
"DICOM does not support this component type");
2077 gdcm::PhotometricInterpretation pi;
2080 pi = gdcm::PhotometricInterpretation::MONOCHROME2;
2084 pi = gdcm::PhotometricInterpretation::RGB;
2089 itkExceptionMacro(<<
"DICOM does not support this component type");
2095 if( pixeltype == gdcm::PixelFormat::FLOAT32 || pixeltype == gdcm::PixelFormat::FLOAT64 )
2097 if( bitsAllocated !=
"" && bitsStored !=
"" && highBit !=
"" && pixelRep !=
"" )
2099 outpixeltype.SetBitsAllocated( atoi(bitsAllocated.c_str()) );
2100 outpixeltype.SetBitsStored( atoi(bitsStored.c_str()) );
2101 outpixeltype.SetHighBit( atoi(highBit.c_str()) );
2102 outpixeltype.SetPixelRepresentation( atoi(pixelRep.c_str()) );
2105 itkExceptionMacro(<<
"Sorry Dave I can't do that" );
2111 itkExceptionMacro(<<
"A Floating point buffer was passed but the stored pixel type was not specified."
2112 "This is currently not supported" );
2116 image.SetPhotometricInterpretation( pi );
2118 image.SetPixelFormat( outpixeltype );
2120 image.SetPixelFormat( pixeltype );
2121 unsigned long len = image.GetBufferLength();
2125 gdcm::DataElement pixeldata( gdcm::Tag(0x7fe0,0x0010) );
2134 ir.SetPixelFormat( pixeltype );
2135 ir.SetMinMaxForPixelType( outpixeltype.GetMin(), outpixeltype.GetMax() );
2138 char* copy =
new char[len];
2139 ir.InverseRescale(copy,(
char*)buffer,numberOfBytes );
2140 pixeldata.SetByteValue( copy, len);
2145 assert( len == numberOfBytes );
2147 pixeldata.SetByteValue( (
char*)buffer, numberOfBytes );
2149 image.SetDataElement( pixeldata );
2156 gdcm::ImageChangeTransferSyntax change;
2159 change.SetTransferSyntax( gdcm::TransferSyntax::JPEGLosslessProcess14_1 );
2163 change.SetTransferSyntax( gdcm::TransferSyntax::JPEG2000Lossless );
2167 itkExceptionMacro(<<
"Unknown compression type" );
2169 change.SetInput( image );
2170 bool b = change.Change();
2173 itkExceptionMacro(<<
"Could not change the Transfer Syntax for Compression" );
2175 writer.SetImage( change.GetOutput() );
2179 writer.SetImage( image );
2190 gdcm::UIDGenerator uid;
2198 gdcm::DataElement de( gdcm::Tag(0x0020,0x000d) );
2199 de.SetByteValue( studyuid, strlen(studyuid) );
2200 de.SetVR( gdcm::Attribute<0x0020, 0x000d>::GetVR() );
2201 header.Insert( de );
2205 gdcm::DataElement de( gdcm::Tag(0x0020,0x000e) );
2206 de.SetByteValue( seriesuid, strlen(seriesuid) );
2207 de.SetVR( gdcm::Attribute<0x0020, 0x000e>::GetVR() );
2208 header.Insert( de );
2212 if( image.GetTransferSyntax() != gdcm::TransferSyntax::ImplicitVRLittleEndian )
2214 gdcm::FileExplicitFilter fef;
2216 fef.SetFile( writer.GetFile() );
2219 itkExceptionMacro(<<
"Failed to change to Explicit Transfer Syntax");
2224 writer.SetFileName( filename );
2225 if( !writer.Write() )
2227 itkExceptionMacro(<<
"DICOM does not support this component type");
2238 ExposeMetaData<std::string>(dict,
"0010|0010",
m_PatientName);
2245 ExposeMetaData<std::string>(dict,
"0010|0020",
m_PatientID);
2252 ExposeMetaData<std::string>(dict,
"0010|0040",
m_PatientSex);
2259 ExposeMetaData<std::string>(dict,
"0010|1010",
m_PatientAge);
2266 ExposeMetaData<std::string>(dict,
"0020|0010",
m_StudyID);
2273 ExposeMetaData<std::string>(dict,
"0010|0030",
m_PatientDOB);
2287 ExposeMetaData<std::string>(dict,
"0018|0015",
m_BodyPart);
2308 ExposeMetaData<std::string>(dict,
"0008|0020",
m_StudyDate);
2315 ExposeMetaData<std::string>(dict,
"0008|0060",
m_Modality);
2329 ExposeMetaData<std::string>(dict,
"0008|0080",
m_Institution);
2336 ExposeMetaData<std::string>(dict,
"0008|1090",
m_Model);
2337 strcpy (name,
m_Model.c_str());
2343 ExposeMetaData<std::string>(dict,
"0018|0022",
m_ScanOptions);
2350 return ExposeMetaData<std::string>(dict, tag, value);
2353 #if GDCM_MAJOR_VERSION < 2
2355 std::string & labelId )
2357 gdcm::Dict *pubDict = gdcm::Global::GetDicts()->GetDefaultPubDict();
2359 gdcm::DictEntry *dictentry = pubDict->GetEntry( tagkey );
2367 labelId = dictentry->GetName();
2372 labelId =
"Unknown";
2379 std::string & labelId )
2382 if( t.ReadFromPipeSeparatedString( tag.c_str() ) && t.IsPublic() )
2384 const gdcm::Global &g = gdcm::Global::GetInstance();
2385 const gdcm::Dicts &dicts = g.GetDicts();
2386 const gdcm::DictEntry &entry = dicts.GetDictEntry(t);
2387 labelId = entry.GetName();
2403 os << indent <<
"KeepOriginalUID:" << (
m_KeepOriginalUID ?
"On" :
"Off") << std::endl;
2404 os << indent <<
"UIDPrefix: " <<
m_UIDPrefix << std::endl;
2412 os << indent <<
"Patient Name:" <<
m_PatientName << std::endl;
2413 os << indent <<
"Patient ID:" <<
m_PatientID << std::endl;
2414 os << indent <<
"Patient Sex:" <<
m_PatientSex << std::endl;
2415 os << indent <<
"Patient Age:" <<
m_PatientAge << std::endl;
2416 os << indent <<
"Study ID:" <<
m_StudyID << std::endl;
2417 os << indent <<
"Patient DOB:" <<
m_PatientDOB << std::endl;
2419 os << indent <<
"Body Part:" <<
m_BodyPart << std::endl;
2422 os << indent <<
"Study Date:" <<
m_StudyDate << std::endl;
2423 os << indent <<
"Modality:" <<
m_Modality << std::endl;
2425 os << indent <<
"Institution Name:" <<
m_Institution << std::endl;
2426 os << indent <<
"Model:" <<
m_Model << std::endl;
2427 os << indent <<
"Scan Options:" <<
m_ScanOptions << std::endl;