1 // gdcmHeaderHelper.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmHeaderHelper.h"
5 #include "gdcmUtil.h" //for debug
12 int GetDir(std::string dPath, std::list<std::string> &filenames)
14 //For now dPath should have an ending "\"
15 WIN32_FIND_DATA FileData;
17 hFile = FindFirstFile((dPath+"*").c_str(), &FileData);
18 if ( hFile == INVALID_HANDLE_VALUE )
24 if( strncmp(FileData.cFileName, ".", 1) != 0 )
25 filenames.push_back( dPath+FileData.cFileName );
26 while( FindNextFile(hFile, &FileData ) != 0)
28 if( strncmp(FileData.cFileName, ".", 1) != 0 )
29 filenames.push_back( dPath+FileData.cFileName );
37 int GetDir(std::string dPath, std::list<std::string> &filenames)
39 DIR *dir = opendir( dPath.c_str() );
41 while((entry = readdir(dir)) != NULL)
43 // if( strncmp(entry->d_name, ".", 1) != 0 && strncmp(entry->d_name, "..", 2) != 0)
44 if( strncmp(entry->d_name, ".", 1) != 0 )
46 filenames.push_back( dPath + "/" + entry->d_name );
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 // Constructor / Destructor
60 * \ingroup gdcmHeaderHelper
63 gdcmHeaderHelper::gdcmHeaderHelper() : gdcmHeader( )
68 * \ingroup gdcmHeaderHelper
71 gdcmHeaderHelper::gdcmHeaderHelper(const char *InFilename,
72 bool exception_on_error) : gdcmHeader( InFilename , exception_on_error)
76 //-----------------------------------------------------------------------------
79 //-----------------------------------------------------------------------------
82 * \ingroup gdcmHeaderHelper
83 * \brief Return the size (in bytes) of a single pixel of data.
84 * @return The size in bytes of a single pixel of data.
87 int gdcmHeaderHelper::GetPixelSize() {
89 // 0028 0100 US IMG Bits Allocated
90 // (in order no to be messed up by old RGB images)
91 if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
94 std::string PixelType = GetPixelType();
95 if (PixelType == "8U" || PixelType == "8S")
97 if (PixelType == "16U" || PixelType == "16S")
99 if (PixelType == "32U" || PixelType == "32S")
101 if (PixelType == "FD") // to help unfortunate users to manage DOUBLE
103 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
108 * \ingroup gdcmHeaderHelper
109 * \brief Build the Pixel Type of the image.
110 * Possible values are:
111 * - 8U unsigned 8 bit,
113 * - 16U unsigned 16 bit,
114 * - 16S signed 16 bit,
115 * - 32U unsigned 32 bit,
116 * - 32S signed 32 bit,
118 * \warning 12 bit images appear as 16 bit.
119 * \ 24 bit images appear as 8 bit
120 * \ DOUBLE images are coded as 64 bits
121 * \ (no DOUBLE images in kosher DICOM,
122 * \ but so usefull for people that miss them ;-)
125 std::string gdcmHeaderHelper::GetPixelType() {
126 std::string BitsAlloc;
127 BitsAlloc = GetEntryByNumber(0x0028, 0x0100);
128 if (BitsAlloc == GDCM_UNFOUND) { // Bits Allocated
129 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
130 BitsAlloc = std::string("16");
132 if (BitsAlloc == "12") // It will be unpacked
133 BitsAlloc = std::string("16");
134 else if (BitsAlloc == "24") // (in order no to be messed up
135 BitsAlloc = std::string("8"); // by old RGB images)
138 Signed = GetEntryByNumber(0x0028, 0x0103);
139 if (Signed == GDCM_UNFOUND) { // "Pixel Representation"
140 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
141 BitsAlloc = std::string("0");
143 if (BitsAlloc == "64") // to help users that want to deal with DOUBLE
147 Signed = std::string("U");
149 Signed = std::string("S");
151 return( BitsAlloc + Signed);
155 * \ingroup gdcmHeaderHelper
156 * \brief gets the info from 0028,0030 : Pixel Spacing
158 * @return X dimension of a pixel
160 float gdcmHeaderHelper::GetXSpacing() {
161 float xspacing, yspacing;
162 std::string StrSpacing = GetEntryByNumber(0x0028,0x0030);
164 if (StrSpacing == GDCM_UNFOUND) {
165 dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
168 if( sscanf( StrSpacing.c_str(), "%f\\%f", &yspacing, &xspacing) != 2)
170 if (xspacing == 0.) {
171 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
172 // seems to be a bug in the header ...
173 sscanf( StrSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
179 * \ingroup gdcmHeaderHelper
180 * \brief gets the info from 0028,0030 : Pixel Spacing
182 * @return Y dimension of a pixel
184 float gdcmHeaderHelper::GetYSpacing() {
185 float xspacing, yspacing;
186 std::string StrSpacing = GetEntryByNumber(0x0028,0x0030);
188 if (StrSpacing == GDCM_UNFOUND) {
189 dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
192 if( sscanf( StrSpacing.c_str(), "%f\\%f", &yspacing, &xspacing) != 2)
194 if (xspacing == 0.) {
195 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
196 // seems to be a bug in the header ...
197 sscanf( StrSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
203 *\ingroup gdcmHeaderHelper
204 *\brief gets the info from 0018,0088 : Space Between Slices
205 *\ else from 0018,0050 : Slice Thickness
207 * @return Z dimension of a voxel-to be
209 float gdcmHeaderHelper::GetZSpacing() {
210 // Spacing Between Slices : distance entre le milieu de chaque coupe
211 // Les coupes peuvent etre :
212 // jointives (Spacing between Slices = Slice Thickness)
213 // chevauchantes (Spacing between Slices < Slice Thickness)
214 // disjointes (Spacing between Slices > Slice Thickness)
215 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
216 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
217 // Si le Spacing Between Slices est absent,
218 // on suppose que les coupes sont jointives
220 std::string StrSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
222 if (StrSpacingBSlices == GDCM_UNFOUND) {
223 dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
224 std::string StrSliceThickness = GetEntryByNumber(0x0018,0x0050);
225 if (StrSliceThickness == GDCM_UNFOUND)
228 // if no 'Spacing Between Slices' is found,
229 // we assume slices join together
230 // (no overlapping, no interslice gap)
231 // if they don't, we're fucked up
232 return atof(StrSliceThickness.c_str());
234 return atof(StrSpacingBSlices.c_str());
238 float gdcmHeaderHelper::GetRescaleIntercept() {
240 std::string StrRescInter = GetEntryByNumber(0x0028,0x1052); //0028 1052 DS IMG Rescale Intercept
241 if (StrRescInter != GDCM_UNFOUND) {
242 if( sscanf( StrRescInter.c_str(), "%f", &resInter) != 1) {
243 dbg.Verbose(0, "gdcmHeader::GetRescaleIntercept: Rescale Slope is empty");
244 // bug in the element 0x0028,0x1052
250 float gdcmHeaderHelper::GetRescaleSlope() {
252 std::string StrRescSlope = GetEntryByNumber(0x0028,0x1053); //0028 1053 DS IMG Rescale Slope
253 if (StrRescSlope != GDCM_UNFOUND) {
254 if( sscanf( StrRescSlope.c_str(), "%f", &resSlope) != 1) {
255 dbg.Verbose(0, "gdcmHeader::GetRescaleSlope: Rescale Slope is empty");
256 // bug in the element 0x0028,0x1053
263 * \ingroup gdcmHeaderHelper
264 * \brief This function is intended to user who doesn't whan
265 * \ to have to manage a LUT and expects to get an RBG Pixel image
266 * \ (or a monochrome one ...)
267 * \warning to be used with GetImagePixels()
268 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
270 int gdcmHeaderHelper::GetNumberOfScalarComponents() {
272 if (GetSamplesPerPixel() ==3)
275 // 0028 0100 US IMG Bits Allocated
276 // (in order no to be messed up by old RGB images)
277 if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
280 std::string PhotometricInterpretation =
281 gdcmHeader::GetEntryByNumber(0x0028,0x0004);
283 if ( ( PhotometricInterpretation == "PALETTE COLOR ") ) {
284 if (HasLUT()) // PALETTE COLOR is NOT enough
290 //beware of trailing space at end of string
291 if (PhotometricInterpretation.find(GDCM_UNFOUND) <
292 PhotometricInterpretation.length() ||
293 PhotometricInterpretation.find("MONOCHROME1") <
294 PhotometricInterpretation.length() ||
295 PhotometricInterpretation.find("MONOCHROME2") <
296 PhotometricInterpretation.length() )
299 // we assume that *all* kinds of YBR are dealt with
304 * \ingroup gdcmHeaderHelper
305 * \brief This function is intended to user that DOESN'T want
306 * \to get RGB pixels image when it's stored as a PALETTE COLOR image
307 * \ - the (vtk) user is supposed to know how deal with LUTs -
308 * \warning to be used with GetImagePixelsRaw()
309 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
311 int gdcmHeaderHelper::GetNumberOfScalarComponentsRaw() {
313 // 0028 0100 US IMG Bits Allocated
314 // (in order no to be messed up by old RGB images)
315 if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
318 // we assume that *all* kinds of YBR are dealt with
319 return GetSamplesPerPixel();
322 std::string gdcmHeaderHelper::GetStudyUID(){
323 return GetEntryByNumber(0x0020,0x000d); //0020 000d UI REL Study Instance UID
326 std::string gdcmHeaderHelper::GetSeriesUID(){
327 return GetEntryByNumber(0x0020,0x000e); //0020 000e UI REL Series Instance UID
330 std::string gdcmHeaderHelper::GetClassUID(){
331 return GetEntryByNumber(0x0008,0x0016); //0008 0016 UI ID SOP Class UID
334 std::string gdcmHeaderHelper::GetInstanceUID(){
335 return GetEntryByNumber(0x0008,0x0018); //0008 0018 UI ID SOP Instance UID
338 // Image Position Patient (0020,0032):
339 // If not found (ACR_NEMA) we try Image Position (0020,0030)
340 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
341 // or Location (0020,0050)
342 // as the Z coordinate,
343 // 0. for all the coordinates if nothing is found
345 // TODO : find a way to inform the caller nothing was found
346 // TODO : How to tell the caller a wrong number of values was found?
348 * \ingroup gdcmHeaderHelper
349 * \brief gets the info from 0020,0032 : Image Position Patient
350 *\ else from 0020,0030 : Image Position (RET)
352 * @return up-left image corner position
354 float gdcmHeaderHelper::GetXOrigin() {
355 float xImPos, yImPos, zImPos;
356 std::string StrImPos = GetEntryByNumber(0x0020,0x0032);
358 if (StrImPos == GDCM_UNFOUND) {
359 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position Patient (0020,0032)");
360 StrImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
361 if (StrImPos == GDCM_UNFOUND) {
362 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position (RET) (0020,0030)");
363 // How to tell the caller nothing was found ?
367 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
373 * \ingroup gdcmHeaderHelper
374 * \brief gets the info from 0020,0032 : Image Position Patient
375 * \ else from 0020,0030 : Image Position (RET)
377 * @return up-left image corner position
379 float gdcmHeaderHelper::GetYOrigin() {
380 float xImPos, yImPos, zImPos;
381 std::string StrImPos = GetEntryByNumber(0x0020,0x0032);
383 if (StrImPos == GDCM_UNFOUND) {
384 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position Patient (0020,0032)");
385 StrImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
386 if (StrImPos == GDCM_UNFOUND) {
387 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position (RET) (0020,0030)");
388 // How to tell the caller nothing was found ?
392 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
398 * \ingroup gdcmHeaderHelper
399 * \brief gets the info from 0020,0032 : Image Position Patient
400 * \ else from 0020,0030 : Image Position (RET)
401 * \ else from 0020,1041 : Slice Location
402 * \ else from 0020,0050 : Location
404 * @return up-left image corner position
406 float gdcmHeaderHelper::GetZOrigin() {
407 float xImPos, yImPos, zImPos;
408 std::string StrImPos = GetEntryByNumber(0x0020,0x0032);
409 if (StrImPos != GDCM_UNFOUND) {
410 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
411 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position Patient (0020,0032)");
412 return 0.; // bug in the element 0x0020,0x0032
417 StrImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
418 if (StrImPos != GDCM_UNFOUND) {
419 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
420 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
421 return 0.; // bug in the element 0x0020,0x0032
426 std::string StrSliceLocation = GetEntryByNumber(0x0020,0x1041);// for *very* old ACR-NEMA images
427 if (StrSliceLocation != GDCM_UNFOUND) {
428 if( sscanf( StrSliceLocation.c_str(), "%f", &zImPos) !=1) {
429 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Slice Location (0020,1041)");
430 return 0.; // bug in the element 0x0020,0x1041
435 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: unfound Slice Location (0020,1041)");
436 std::string StrLocation = GetEntryByNumber(0x0020,0x0050);
437 if (StrLocation != GDCM_UNFOUND) {
438 if( sscanf( StrLocation.c_str(), "%f", &zImPos) !=1) {
439 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Location (0020,0050)");
440 return 0.; // bug in the element 0x0020,0x0050
445 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Location (0020,0050)");
446 return 0.; // Hopeless
450 * \ingroup gdcmHeaderHelper
451 * \brief gets the info from 0020,0013 : Image Number
453 * @return image number
455 int gdcmHeaderHelper::GetImageNumber() {
456 //The function i atoi() takes the address of an area of memory as parameter and converts
457 //the string stored at that location to an integer using the external decimal to internal
458 //binary conversion rules. This may be preferable to sscanf() since atoi() is a much smaller,
459 // simpler and faster function. sscanf() can do all possible conversions whereas atoi() can
460 //only do single decimal integer conversions.
461 std::string StrImNumber = GetEntryByNumber(0x0020,0x0013); //0020 0013 IS REL Image Number
462 if (StrImNumber != GDCM_UNFOUND) {
463 return atoi( StrImNumber.c_str() );
469 * \ingroup gdcmHeaderHelper
470 * \brief gets the info from 0008,0060 : Modality
471 * @return ModalityType
473 ModalityType gdcmHeaderHelper::GetModality(void) {
474 std::string StrModality = GetEntryByNumber(0x0008,0x0060); //0008 0060 CS ID Modality
475 if (StrModality != GDCM_UNFOUND) {
476 if ( StrModality.find("AU") < StrModality.length()) return AU;
477 else if ( StrModality.find("AS") < StrModality.length()) return AS;
478 else if ( StrModality.find("BI") < StrModality.length()) return BI;
479 else if ( StrModality.find("CF") < StrModality.length()) return CF;
480 else if ( StrModality.find("CP") < StrModality.length()) return CP;
481 else if ( StrModality.find("CR") < StrModality.length()) return CR;
482 else if ( StrModality.find("CT") < StrModality.length()) return CT;
483 else if ( StrModality.find("CS") < StrModality.length()) return CS;
484 else if ( StrModality.find("DD") < StrModality.length()) return DD;
485 else if ( StrModality.find("DF") < StrModality.length()) return DF;
486 else if ( StrModality.find("DG") < StrModality.length()) return DG;
487 else if ( StrModality.find("DM") < StrModality.length()) return DM;
488 else if ( StrModality.find("DS") < StrModality.length()) return DS;
489 else if ( StrModality.find("DX") < StrModality.length()) return DX;
490 else if ( StrModality.find("ECG") < StrModality.length()) return ECG;
491 else if ( StrModality.find("EPS") < StrModality.length()) return EPS;
492 else if ( StrModality.find("FA") < StrModality.length()) return FA;
493 else if ( StrModality.find("FS") < StrModality.length()) return FS;
494 else if ( StrModality.find("HC") < StrModality.length()) return HC;
495 else if ( StrModality.find("HD") < StrModality.length()) return HD;
496 else if ( StrModality.find("LP") < StrModality.length()) return LP;
497 else if ( StrModality.find("LS") < StrModality.length()) return LS;
498 else if ( StrModality.find("MA") < StrModality.length()) return MA;
499 else if ( StrModality.find("MR") < StrModality.length()) return MR;
500 else if ( StrModality.find("NM") < StrModality.length()) return NM;
501 else if ( StrModality.find("OT") < StrModality.length()) return OT;
502 else if ( StrModality.find("PT") < StrModality.length()) return PT;
503 else if ( StrModality.find("RF") < StrModality.length()) return RF;
504 else if ( StrModality.find("RG") < StrModality.length()) return RG;
505 else if ( StrModality.find("RTDOSE") < StrModality.length()) return RTDOSE;
506 else if ( StrModality.find("RTIMAGE") < StrModality.length()) return RTIMAGE;
507 else if ( StrModality.find("RTPLAN") < StrModality.length()) return RTPLAN;
508 else if ( StrModality.find("RTSTRUCT")< StrModality.length()) return RTSTRUCT;
509 else if ( StrModality.find("SM") < StrModality.length()) return SM;
510 else if ( StrModality.find("ST") < StrModality.length()) return ST;
511 else if ( StrModality.find("TG") < StrModality.length()) return TG;
512 else if ( StrModality.find("US") < StrModality.length()) return US;
513 else if ( StrModality.find("VF") < StrModality.length()) return VF;
514 else if ( StrModality.find("XA") < StrModality.length()) return XA;
515 else if ( StrModality.find("XC") < StrModality.length()) return XC;
519 //throw error return value ???
520 // specified <> unknow in our database
528 * \ingroup gdcmHeaderHelper
529 * \brief gets the info from 0020,0037 : Image Orientation Patient
530 * @return cosines of image orientation patient
532 void gdcmHeaderHelper::GetImageOrientationPatient( float* iop ) {
534 //iop is supposed to be float[6]
535 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0;
537 std::string StrImOriPat = GetEntryByNumber(0x0020,0x0037); // 0020 0037 DS REL Image Orientation (Patient)
538 if (StrImOriPat != GDCM_UNFOUND) {
539 if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
540 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
541 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0037)");
542 return ; // bug in the element 0x0020,0x0037
549 StrImOriPat = GetEntryByNumber(0x0020,0x0035); //0020 0035 DS REL Image Orientation (RET)
550 if (StrImOriPat != GDCM_UNFOUND) {
551 if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
552 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
553 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0035)");
554 return ; // bug in the element 0x0020,0x0035
561 //-----------------------------------------------------------------------------
564 //-----------------------------------------------------------------------------
567 //-----------------------------------------------------------------------------
571 //-----------------------------------------------------------------------------
572 // gdcmSerieHeaderHelper
573 //-----------------------------------------------------------------------------
574 // Constructor / Destructor
575 gdcmSerieHeaderHelper::~gdcmSerieHeaderHelper(){
577 for (std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
578 it != CoherentGdcmFileList.end(); it++)
582 CoherentGdcmFileList.clear();
585 //-----------------------------------------------------------------------------
588 //-----------------------------------------------------------------------------
591 * \ingroup gdcmHeaderHelper
592 * \brief add a gdcmFile to the list based on file name
594 void gdcmSerieHeaderHelper::AddFileName(std::string filename) {
595 gdcmHeaderHelper *GdcmFile = new gdcmHeaderHelper( filename.c_str() );
596 this->CoherentGdcmFileList.push_back( GdcmFile );
600 * \ingroup gdcmHeaderHelper
601 * \brief add a gdcmFile to the list
603 void gdcmSerieHeaderHelper::AddGdcmFile(gdcmHeaderHelper *file){
604 this->CoherentGdcmFileList.push_back( file );
608 * \ingroup gdcmHeaderHelper
611 void gdcmSerieHeaderHelper::SetDirectory(std::string dir){
612 std::list<std::string> filenames_list;
613 GetDir(dir, filenames_list); //OS specific
615 for(std::list<std::string>::iterator it = filenames_list.begin(); it !=
616 filenames_list.end(); it++)
618 gdcmHeaderHelper *file = new gdcmHeaderHelper( it->c_str() );
619 this->CoherentGdcmFileList.push_back( file );
623 //This could be implemented in a 'Strategy Pattern' approach
624 //But as I don't know how to do it, I leave it this way
625 //BTW, this is also a Strategy, I don't know this is the best approach :)
626 void gdcmSerieHeaderHelper::OrderGdcmFileList(){
627 if( ImagePositionPatientOrdering() )
631 else if( ImageNumberOrdering() )
641 std::list<gdcmHeaderHelper*> &gdcmSerieHeaderHelper::GetGdcmFileList() {
642 return CoherentGdcmFileList;
645 //-----------------------------------------------------------------------------
648 //-----------------------------------------------------------------------------
651 * \ingroup gdcmHeaderHelper
653 * We may order, considering :
655 * -# Image Position Patient
658 bool gdcmSerieHeaderHelper::ImagePositionPatientOrdering()
659 //based on Jolinda's algorithm
661 //iop is calculated based on the file file
662 float *cosines = new float[6];
669 std::vector<float> distlist;
671 //!\todo rewrite this for loop.
672 for (std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
673 it != CoherentGdcmFileList.end(); it++)
676 (*it)->GetImageOrientationPatient(cosines);
678 //You only have to do this once for all slices in the volume. Next, for
679 //each slice, calculate the distance along the slice normal using the IPP
680 //tag ("dist" is initialized to zero before reading the first slice) :
681 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
682 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
683 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
685 ipp[0] = (*it)->GetXOrigin();
686 ipp[1] = (*it)->GetYOrigin();
687 ipp[2] = (*it)->GetZOrigin();
690 for (int i = 0; i < 3; ++i)
691 dist += normal[i]*ipp[i];
699 distlist.push_back( dist );
705 ipp[0] = (*it)->GetXOrigin();
706 ipp[1] = (*it)->GetYOrigin();
707 ipp[2] = (*it)->GetZOrigin();
710 for (int i = 0; i < 3; ++i)
711 dist += normal[i]*ipp[i];
720 distlist.push_back( dist );
722 min = (min < dist) ? min : dist;
723 max = (max > dist) ? max : dist;
728 //Then I order the slices according to the value "dist". Finally, once
729 //I've read in all the slices, I calculate the z-spacing as the difference
730 //between the "dist" values for the first two slices.
731 std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
732 //CoherentGdcmFileVector.reserve( n );
733 CoherentGdcmFileVector.resize( n );
734 //assert( CoherentGdcmFileVector.capacity() >= n );
736 float step = (max - min)/(n - 1);
740 //VC++ don't understand what scope is !! it -> it2
741 for (std::list<gdcmHeaderHelper*>::iterator it2 = CoherentGdcmFileList.begin();
742 it2 != CoherentGdcmFileList.end(); it2++, n++)
745 //Assumption: all files are present (no one missing)
746 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
748 CoherentGdcmFileVector[pos] = *it2;
751 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
753 //VC++ don't understand what scope is !! it -> it3
754 for (std::vector<gdcmHeaderHelper*>::iterator it3 = CoherentGdcmFileVector.begin();
755 it3 != CoherentGdcmFileVector.end(); it3++)
757 CoherentGdcmFileList.push_back( *it3 );
761 CoherentGdcmFileVector.clear();
767 //Based on Image Number
769 bool gdcmSerieHeaderHelper::ImageNumberOrdering() {
771 int n = 0;//CoherentGdcmFileList.size(); //O(N) operation !!
772 unsigned char *partition;
774 std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
775 min = max = (*it)->GetImageNumber();
777 for (; it != CoherentGdcmFileList.end(); it++, n++)
779 pos = (*it)->GetImageNumber();
782 min = (min < pos) ? min : pos;
785 //bzeros(partition, n); //Cette fonction est déconseillée, utilisez plutôt memset.
786 partition = new unsigned char[n];
787 memset(partition, 0, n);
789 std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
791 //VC++ don't understand what scope is !! it -> it2
792 for (std::list<gdcmHeaderHelper*>::iterator it2 = CoherentGdcmFileList.begin();
793 it2 != CoherentGdcmFileList.end(); it2++)
795 pos = (*it2)->GetImageNumber();
796 CoherentGdcmFileVector[pos - min] = *it2;
797 partition[pos - min]++;
800 unsigned char mult = 1;
801 for(int i=0; i<n ; i++)
803 mult *= partition[i];
806 //VC++ don't understand what scope is !! it -> it3
807 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
808 for (std::vector<gdcmHeaderHelper*>::iterator it3 = CoherentGdcmFileVector.begin();
809 it3 != CoherentGdcmFileVector.end(); it3++)
811 CoherentGdcmFileList.push_back( *it3 );
813 CoherentGdcmFileVector.clear();
819 bool gdcmSerieHeaderHelper::FileNameOrdering() {
821 //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
825 //-----------------------------------------------------------------------------