1 // $Header: /cvs/public/gdcm/src/Attic/gdcmHeaderHelper.cxx,v 1.2 2003/09/09 08:46:32 malaterre Exp $
3 //This is needed when compiling in debug mode
5 #pragma warning ( disable : 4800 )
6 // 'identifier' : class 'type' needs to have dll-interface to be used by
7 // clients of class 'type2'
8 #pragma warning ( disable : 4251 )
9 // 'identifier' : identifier was truncated to 'number' characters in the
11 #pragma warning ( disable : 4786 )
14 #include "gdcmHeaderHelper.h"
15 #include "gdcmUtil.h" //for debug
18 #include <string.h> //for bzero
21 //directory manipulation (os indep).
22 //cygwin ???? -> _WIN32 ??
25 int GetDir(string dPath, list<string> &filenames)
27 WIN32_FIND_DATA FileData;
29 hFile = FindFirstFile(dPath.c_str(), &FileData);
30 if ( hFile == INVALID_HANDLE_VALUE )
36 filenames.push_back( FileData.cFileName );
37 while( FindNextFile(hFile, &FileData ) != 0)
39 filenames.push_back( FileData.cFileName );
47 int GetDir(string dPath, list<string> &filenames)
49 DIR *dir = opendir( dPath.c_str() );
51 while((entry = readdir(dir)) != NULL)
53 // if( strncmp(entry->d_name, ".", 1) != 0 && strncmp(entry->d_name, "..", 2) != 0)
54 if( strncmp(entry->d_name, ".", 1) != 0 )
56 filenames.push_back( dPath + "/" + entry->d_name );
65 //----------------------------------------------------------------------------
67 * \ingroup gdcmHeaderHelper
70 gdcmHeaderHelper::gdcmHeaderHelper() : gdcmHeader( )
73 //----------------------------------------------------------------------------
75 * \ingroup gdcmHeaderHelper
78 gdcmHeaderHelper::gdcmHeaderHelper(const char *InFilename,
79 bool exception_on_error) : gdcmHeader( InFilename , exception_on_error)
82 //----------------------------------------------------------------------------
84 * \ingroup gdcmHeaderHelper
85 * \brief Return the size (in bytes) of a single pixel of data.
86 * @return The size in bytes of a single pixel of data.
89 int gdcmHeaderHelper::GetPixelSize() {
90 string PixelType = GetPixelType();
91 if (PixelType == "8U" || PixelType == "8S")
93 if (PixelType == "16U" || PixelType == "16S")
95 if (PixelType == "32U" || PixelType == "32S")
97 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
100 //----------------------------------------------------------------------------
102 * \ingroup gdcmHeaderHelper
103 * \brief Build the Pixel Type of the image.
104 * Possible values are:
105 * - 8U unsigned 8 bit,
107 * - 16U unsigned 16 bit,
108 * - 16S signed 16 bit,
109 * - 32U unsigned 32 bit,
110 * - 32S signed 32 bit,
111 * \warning 12 bit images appear as 16 bit.
114 string gdcmHeaderHelper::GetPixelType() {
116 BitsAlloc = GetElValByName("Bits Allocated");
117 if (BitsAlloc == "gdcm::Unfound") {
118 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
119 BitsAlloc = string("16");
121 if (BitsAlloc == "12")
122 BitsAlloc = string("16");
125 Signed = GetElValByName("Pixel Representation");
126 if (Signed == "gdcm::Unfound") {
127 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
128 BitsAlloc = string("0");
131 Signed = string("U");
133 Signed = string("S");
135 return( BitsAlloc + Signed);
137 //----------------------------------------------------------------------------
139 * \ingroup gdcmHeaderHelper
140 * \brief gets the info from 0028,0030 : Pixel Spacing
142 * @return X dimension of a pixel
144 float gdcmHeaderHelper::GetXSpacing() {
145 float xspacing, yspacing;
146 string StrSpacing = GetPubElValByNumber(0x0028,0x0030);
148 if (StrSpacing == "gdcm::Unfound") {
149 dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
152 if( sscanf( StrSpacing.c_str(), "%f\\%f", &xspacing, &yspacing) != 2)
157 //----------------------------------------------------------------------------
159 * \ingroup gdcmHeaderHelper
160 * \brief gets the info from 0028,0030 : Pixel Spacing
162 * @return Y dimension of a pixel
164 float gdcmHeaderHelper::GetYSpacing() {
165 float xspacing, yspacing;
166 string StrSpacing = GetPubElValByNumber(0x0028,0x0030);
168 if (StrSpacing == "gdcm::Unfound") {
169 dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
172 if( sscanf( StrSpacing.c_str(), "%f\\%f", &xspacing, &yspacing) != 2)
174 if (yspacing == 0.) {
175 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
176 // seems to be a bug in the header ...
177 sscanf( StrSpacing.c_str(), "%f\\0\\%f", &xspacing, &yspacing);
182 //----------------------------------------------------------------------------
184 *\ingroup gdcmHeaderHelper
185 *\brief gets the info from 0018,0088 : Space Between Slices
186 *\ else from 0018,0050 : Slice Thickness
188 * @return Z dimension of a voxel-to be
190 float gdcmHeaderHelper::GetZSpacing() {
191 // TODO : translate into English
192 // Spacing Between Slices : distance entre le milieu de chaque coupe
193 // Les coupes peuvent etre :
194 // jointives (Spacing between Slices = Slice Thickness)
195 // chevauchantes (Spacing between Slices < Slice Thickness)
196 // disjointes (Spacing between Slices > Slice Thickness)
197 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
198 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
199 // Si le Spacing Between Slices est absent,
200 // on suppose que les coupes sont jointives
202 string StrSpacingBSlices = GetPubElValByNumber(0x0018,0x0088);
204 if (StrSpacingBSlices == "gdcm::Unfound") {
205 dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
206 string StrSliceThickness = GetPubElValByNumber(0x0018,0x0050);
207 if (StrSliceThickness == "gdcm::Unfound")
210 // if no 'Spacing Between Slices' is found,
211 // we assume slices join together
212 // (no overlapping, no interslice gap)
213 // if they don't, we're fucked up
214 return atof(StrSliceThickness.c_str());
216 return atof(StrSpacingBSlices.c_str());
220 //----------------------------------------------------------------------------
222 // Image Position Patient (0020,0032):
223 // If not found (ACR_NEMA) we try Image Position (0020,0030)
224 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
225 // or Location (0020,0050)
226 // as the Z coordinate,
227 // 0. for all the coordinates if nothing is found
229 // TODO : find a way to inform the caller nothing was found
230 // TODO : How to tell the caller a wrong number of values was found?
233 * \ingroup gdcmHeaderHelper
234 * \brief gets the info from 0020,0032 : Image Position Patient
235 *\ else from 0020,0030 : Image Position (RET)
237 * @return up-left image corner position
239 float gdcmHeaderHelper::GetXOrigin() {
240 float xImPos, yImPos, zImPos;
241 string StrImPos = GetPubElValByNumber(0x0020,0x0032);
243 if (StrImPos == "gdcm::Unfound") {
244 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position Patient (0020,0032)");
245 StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
246 if (StrImPos == "gdcm::Unfound") {
247 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position (RET) (0020,0030)");
248 // How to tell the caller nothing was found ?
252 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
256 //----------------------------------------------------------------------------
258 * \ingroup gdcmHeaderHelper
259 * \brief gets the info from 0020,0032 : Image Position Patient
260 * \ else from 0020,0030 : Image Position (RET)
262 * @return up-left image corner position
264 float gdcmHeaderHelper::GetYOrigin() {
265 float xImPos, yImPos, zImPos;
266 string StrImPos = GetPubElValByNumber(0x0020,0x0032);
268 if (StrImPos == "gdcm::Unfound") {
269 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position Patient (0020,0032)");
270 StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
271 if (StrImPos == "gdcm::Unfound") {
272 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position (RET) (0020,0030)");
273 // How to tell the caller nothing was found ?
277 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
281 //----------------------------------------------------------------------------
283 * \ingroup gdcmHeaderHelper
284 * \brief gets the info from 0020,0032 : Image Position Patient
285 * \ else from 0020,0030 : Image Position (RET)
286 * \ else from 0020,1041 : Slice Location
287 * \ else from 0020,0050 : Location
289 * @return up-left image corner position
291 float gdcmHeaderHelper::GetZOrigin() {
292 float xImPos, yImPos, zImPos;
293 string StrImPos = GetPubElValByNumber(0x0020,0x0032);
294 if (StrImPos != "gdcm::Unfound") {
295 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
296 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position Patient (0020,0032)");
297 return 0.; // bug in the element 0x0020,0x0032
302 StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
303 if (StrImPos != "gdcm::Unfound") {
304 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
305 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
306 return 0.; // bug in the element 0x0020,0x0032
311 string StrSliceLocation = GetPubElValByNumber(0x0020,0x1041);// for *very* old ACR-NEMA images
312 if (StrSliceLocation != "gdcm::Unfound") {
313 if( sscanf( StrSliceLocation.c_str(), "%f", &zImPos) !=1) {
314 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Slice Location (0020,1041)");
315 return 0.; // bug in the element 0x0020,0x1041
320 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: unfound Slice Location (0020,1041)");
321 string StrLocation = GetPubElValByNumber(0x0020,0x0050);
322 if (StrLocation != "gdcm::Unfound") {
323 if( sscanf( StrLocation.c_str(), "%f", &zImPos) !=1) {
324 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Location (0020,0050)");
325 return 0.; // bug in the element 0x0020,0x0050
330 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Location (0020,0050)");
331 return 0.; // Hopeless
333 //----------------------------------------------------------------------------
335 * \ingroup gdcmHeaderHelper
336 * \brief gets the info from 0020,0013 : Image Number
338 * @return image number
340 int gdcmHeaderHelper::GetImageNumber() {
341 //The function i atoi() takes the address of an area of memory as parameter and converts
342 //the string stored at that location to an integer using the external decimal to internal
343 //binary conversion rules. This may be preferable to sscanf() since atoi() is a much smaller,
344 // simpler and faster function. sscanf() can do all possible conversions whereas atoi() can
345 //only do single decimal integer conversions.
346 string StrImNumber = GetPubElValByNumber(0x0020,0x0013); //0020 0013 IS REL Image Number
347 if (StrImNumber != "gdcm::Unfound") {
348 return atoi( StrImNumber.c_str() );
352 //----------------------------------------------------------------------------
354 * \ingroup gdcmHeaderHelper
355 * \brief gets the info from 0008,0060 : Modality
356 * @return ModalityType
358 ModalityType gdcmHeaderHelper::GetModality(void) {
359 string StrModality = GetPubElValByNumber(0x0008,0x0060); //0008 0060 CS ID Modality
360 if (StrModality != "gdcm::Unfound") {
361 if ( StrModality.find("CR") < StrModality.length())
365 else if ( StrModality.find("CT") < StrModality.length() )
369 else if ( StrModality.find("MR") < StrModality.length())
373 else if ( StrModality.find("NM") < StrModality.length())
377 else if ( StrModality.find("OT") < StrModality.length())
381 else if ( StrModality.find("US") < StrModality.length())
385 else if ( StrModality.find("XA") < StrModality.length())
391 //throw error return value ???
392 // specified <> unknow in our database
399 //----------------------------------------------------------------------------
400 string gdcmHeaderHelper::GetStudyUID()
402 return GetPubElValByNumber(0x0020,0x000d); //!0020 000d UI REL Study Instance UID
404 //----------------------------------------------------------------------------
405 string gdcmHeaderHelper::GetSeriesUID()
407 return GetPubElValByNumber(0x0020,0x000e); //!0020 000e UI REL Series Instance UID
409 //----------------------------------------------------------------------------
410 string gdcmHeaderHelper::GetClassUID()
412 return GetPubElValByNumber(0x0008,0x0016); //!0008 0016 UI ID SOP Class UID
414 //----------------------------------------------------------------------------
415 string gdcmHeaderHelper::GetInstanceUID()
417 return GetPubElValByNumber(0x0008,0x0018); //!0008 0018 UI ID SOP Instance UID
428 gdcmSerieHeaderHelper::~gdcmSerieHeaderHelper()
431 for (list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
432 it != CoherentGdcmFileList.end(); it++)
436 CoherentGdcmFileList.clear();
438 //----------------------------------------------------------------------------
440 * \ingroup gdcmHeaderHelper
441 * \brief gets the info from 0020,0037 : Image Orientation Patient
442 * @return cosines of image orientation patient
444 void gdcmHeaderHelper::GetImageOrientationPatient( float* iop ) {
446 //iop is supposed to be float[6]
447 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0;
449 string StrImOriPat = GetPubElValByNumber(0x0020,0x0037); // 0020 0037 DS REL Image Orientation (Patient)
450 if (StrImOriPat != "gdcm::Unfound") {
451 if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
452 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
453 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0037)");
454 return ; // bug in the element 0x0020,0x0037
461 StrImOriPat = GetPubElValByNumber(0x0020,0x0035); //0020 0035 DS REL Image Orientation (RET)
462 if (StrImOriPat != "gdcm::Unfound") {
463 if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
464 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
465 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0035)");
466 return ; // bug in the element 0x0020,0x0035
473 //----------------------------------------------------------------------------
475 * \ingroup gdcmHeaderHelper
476 * \brief add a gdcmFile to the list based on file name
478 void gdcmSerieHeaderHelper::AddFileName(string filename)
480 gdcmHeaderHelper *GdcmFile = new gdcmHeaderHelper( filename.c_str() );
481 this->CoherentGdcmFileList.push_back( GdcmFile );
483 //----------------------------------------------------------------------------
485 * \ingroup gdcmHeaderHelper
486 * \brief add a gdcmFile to the list
488 void gdcmSerieHeaderHelper::AddGdcmFile(gdcmHeaderHelper *file)
490 this->CoherentGdcmFileList.push_back( file );
492 //----------------------------------------------------------------------------
494 * \ingroup gdcmHeaderHelper
497 void gdcmSerieHeaderHelper::SetDirectory(string dir)
499 list<string> filenames_list;
500 GetDir(dir, filenames_list); //OS specific
502 for(list<string>::iterator it = filenames_list.begin(); it !=
503 filenames_list.end(); it++)
505 gdcmHeaderHelper *file = new gdcmHeaderHelper( it->c_str() );
506 this->CoherentGdcmFileList.push_back( file );
509 //----------------------------------------------------------------------------
510 //This could be implemented in a 'Strategy Pattern' approach
511 //But as I don't know how to do it, I leave it this way
512 //BTW, this is also a Strategy, I don't know this is the best approcah :)
513 void gdcmSerieHeaderHelper::OrderGdcmFileList()
515 if( ImagePositionPatientOrdering() )
519 else if( ImageNumberOrdering() )
528 //----------------------------------------------------------------------------
530 * \ingroup gdcmHeaderHelper
532 We may order, considering :
534 -# Image Position Patient
537 //based on Jolinda's algorithm
538 bool gdcmSerieHeaderHelper::ImagePositionPatientOrdering()
540 //iop is calculated based on the file file
541 float *cosines = new float[6];
548 vector<float> distlist;
550 //!\todo rewrite this for loop.
551 for (list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
552 it != CoherentGdcmFileList.end(); it++)
555 (*it)->GetImageOrientationPatient(cosines);
557 //You only have to do this once for all slices in the volume. Next, for
558 //each slice, calculate the distance along the slice normal using the IPP
559 //tag ("dist" is initialized to zero before reading the first slice) :
560 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
561 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
562 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
564 ipp[0] = (*it)->GetXOrigin();
565 ipp[1] = (*it)->GetYOrigin();
566 ipp[2] = (*it)->GetZOrigin();
569 for (int i = 0; i < 3; ++i)
570 dist += normal[i]*ipp[i];
578 distlist.push_back( dist );
584 ipp[0] = (*it)->GetXOrigin();
585 ipp[1] = (*it)->GetYOrigin();
586 ipp[2] = (*it)->GetZOrigin();
589 for (int i = 0; i < 3; ++i)
590 dist += normal[i]*ipp[i];
599 distlist.push_back( dist );
601 min = (min < dist) ? min : dist;
602 max = (max > dist) ? max : dist;
607 //Then I order the slices according to the value "dist". Finally, once
608 //I've read in all the slices, I calculate the z-spacing as the difference
609 //between the "dist" values for the first two slices.
610 vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
611 //CoherentGdcmFileVector.reserve( n );
612 CoherentGdcmFileVector.resize( n );
613 //assert( CoherentGdcmFileVector.capacity() >= n );
615 float step = (max - min)/(n - 1);
619 //VC++ don't understand what scope is !! it -> it2
620 for (list<gdcmHeaderHelper*>::iterator it2 = CoherentGdcmFileList.begin();
621 it2 != CoherentGdcmFileList.end(); it2++, n++)
624 //Assumption: all files are present (no one missing)
625 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
627 CoherentGdcmFileVector[pos] = *it2;
630 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
632 //VC++ don't understand what scope is !! it -> it3
633 for (vector<gdcmHeaderHelper*>::iterator it3 = CoherentGdcmFileVector.begin();
634 it3 != CoherentGdcmFileVector.end(); it3++)
636 CoherentGdcmFileList.push_back( *it3 );
640 CoherentGdcmFileVector.clear();
645 //----------------------------------------------------------------------------
646 //Based on Image Number
647 bool gdcmSerieHeaderHelper::ImageNumberOrdering()
650 int n = 0;//CoherentGdcmFileList.size(); //O(N) operation !!
651 unsigned char *partition;
653 list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
654 min = max = (*it)->GetImageNumber();
656 for (; it != CoherentGdcmFileList.end(); it++, n++)
658 pos = (*it)->GetImageNumber();
661 min = (min < pos) ? min : pos;
664 //bzeros(partition, n); //Cette fonction est déconseillée, utilisez plutôt memset.
665 partition = new unsigned char[n];
666 memset(partition, 0, n);
668 vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
670 //VC++ don't understand what scope is !! it -> it2
671 for (list<gdcmHeaderHelper*>::iterator it2 = CoherentGdcmFileList.begin();
672 it2 != CoherentGdcmFileList.end(); it2++)
674 pos = (*it2)->GetImageNumber();
675 CoherentGdcmFileVector[pos - min] = *it2;
676 partition[pos - min]++;
680 for(int i=0; i<n ; i++)
682 mult *= partition[i];
685 //VC++ don't understand what scope is !! it -> it3
686 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
687 for (vector<gdcmHeaderHelper*>::iterator it3 = CoherentGdcmFileVector.begin();
688 it3 != CoherentGdcmFileVector.end(); it3++)
690 CoherentGdcmFileList.push_back( *it3 );
692 CoherentGdcmFileVector.clear();
697 //----------------------------------------------------------------------------
698 bool gdcmSerieHeaderHelper::FileNameOrdering()
701 //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
704 //----------------------------------------------------------------------------
705 list<gdcmHeaderHelper*> &gdcmSerieHeaderHelper::GetGdcmFileList()
707 return CoherentGdcmFileList;
709 //----------------------------------------------------------------------------