1 // $Header: /cvs/public/gdcm/src/Attic/gdcmHeaderHelper.cxx,v 1.18 2004/01/13 11:32:30 jpr Exp $
3 #include "gdcmHeaderHelper.h"
5 #include "gdcmUtil.h" //for debug
8 //#include <string.h> //for bzero
10 //directory manipulation (os indep).
11 //cygwin ???? -> _WIN32 ??
14 int GetDir(std::string dPath, std::list<std::string> &filenames)
16 //For now dPath should have an ending "\"
17 WIN32_FIND_DATA FileData;
19 hFile = FindFirstFile((dPath+"*").c_str(), &FileData);
20 if ( hFile == INVALID_HANDLE_VALUE )
26 if( strncmp(FileData.cFileName, ".", 1) != 0 )
27 filenames.push_back( dPath+FileData.cFileName );
28 while( FindNextFile(hFile, &FileData ) != 0)
30 if( strncmp(FileData.cFileName, ".", 1) != 0 )
31 filenames.push_back( dPath+FileData.cFileName );
39 int GetDir(std::string dPath, std::list<std::string> &filenames)
41 DIR *dir = opendir( dPath.c_str() );
43 while((entry = readdir(dir)) != NULL)
45 // if( strncmp(entry->d_name, ".", 1) != 0 && strncmp(entry->d_name, "..", 2) != 0)
46 if( strncmp(entry->d_name, ".", 1) != 0 )
48 filenames.push_back( dPath + "/" + entry->d_name );
57 //----------------------------------------------------------------------------
59 * \ingroup gdcmHeaderHelper
62 gdcmHeaderHelper::gdcmHeaderHelper() : gdcmHeader( )
65 //----------------------------------------------------------------------------
67 * \ingroup gdcmHeaderHelper
70 gdcmHeaderHelper::gdcmHeaderHelper(const char *InFilename,
71 bool exception_on_error) : gdcmHeader( InFilename , exception_on_error)
74 //----------------------------------------------------------------------------
76 * \ingroup gdcmHeaderHelper
77 * \brief Return the size (in bytes) of a single pixel of data.
78 * @return The size in bytes of a single pixel of data.
81 int gdcmHeaderHelper::GetPixelSize() {
83 // 0028 0100 US IMG Bits Allocated
84 // (in order no to be messed up by old RGB images)
85 if (gdcmHeader::GetPubElValByNumber(0x0028,0x0100) == "24")
88 std::string PixelType = GetPixelType();
89 if (PixelType == "8U" || PixelType == "8S")
91 if (PixelType == "16U" || PixelType == "16S")
93 if (PixelType == "32U" || PixelType == "32S")
95 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
99 //----------------------------------------------------------------------------
101 * \ingroup gdcmHeaderHelper
102 * \brief This function is intended to user who doesn't whan
103 * \ to have to manage a LUT and expects to get an RBG Pixel image
104 * \ (or a monochrome one ...)
105 * \warning to be used with GetImagePixels()
106 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
108 int gdcmHeaderHelper::GetNumberOfScalarComponents() {
110 if (GetSamplesPerPixel() ==3)
113 // 0028 0100 US IMG Bits Allocated
114 // (in order no to be messed up by old RGB images)
115 if (gdcmHeader::GetPubElValByNumber(0x0028,0x0100) == "24")
118 std::string PhotometricInterpretation =
119 gdcmHeader::GetPubElValByNumber(0x0028,0x0004);
121 if ( ( PhotometricInterpretation == "PALETTE COLOR ") ) {
122 if (HasLUT()) // PALETTE COLOR is NOT enough
128 //beware of trailing space at end of string
129 if (PhotometricInterpretation.find(GDCM_UNFOUND) <
130 PhotometricInterpretation.length() ||
131 PhotometricInterpretation.find("MONOCHROME1") <
132 PhotometricInterpretation.length() ||
133 PhotometricInterpretation.find("MONOCHROME2") <
134 PhotometricInterpretation.length() )
137 // we assume that *all* kinds of YBR are dealt with
141 //----------------------------------------------------------------------------
143 * \ingroup gdcmHeaderHelper
144 * \brief This function is intended to user that DOESN'T want
145 * \to get RGB pixels image when it's stored as a PALETTE COLOR image
146 * \ - the (vtk) user is supposed to know how deal with LUTs -
147 * \warning to be used with GetImagePixelsRaw()
148 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
150 int gdcmHeaderHelper::GetNumberOfScalarComponentsRaw() {
152 // 0028 0100 US IMG Bits Allocated
153 // (in order no to be messed up by old RGB images)
154 if (gdcmHeader::GetPubElValByNumber(0x0028,0x0100) == "24")
157 // we assume that *all* kinds of YBR are dealt with
158 return GetSamplesPerPixel();
161 //----------------------------------------------------------------------------
163 * \ingroup gdcmHeaderHelper
164 * \brief Build the Pixel Type of the image.
165 * Possible values are:
166 * - 8U unsigned 8 bit,
168 * - 16U unsigned 16 bit,
169 * - 16S signed 16 bit,
170 * - 32U unsigned 32 bit,
171 * - 32S signed 32 bit,
172 * \warning 12 bit images appear as 16 bit.
173 * \ 24 bit images appear as 8 bit
176 std::string gdcmHeaderHelper::GetPixelType() {
177 std::string BitsAlloc;
178 BitsAlloc = GetPubElValByNumber(0x0028, 0x0100);
179 if (BitsAlloc == GDCM_UNFOUND) { // Bits Allocated
180 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
181 BitsAlloc = std::string("16");
183 if (BitsAlloc == "12") // It will be unpacked
184 BitsAlloc = std::string("16");
185 else if (BitsAlloc == "24") // (in order no to be messed up
186 BitsAlloc = std::string("8"); // by old RGB images)
189 Signed = GetPubElValByNumber(0x0028, 0x0103);
190 if (Signed == GDCM_UNFOUND) { // "Pixel Representation"
191 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
192 BitsAlloc = std::string("0");
195 Signed = std::string("U");
197 Signed = std::string("S");
199 return( BitsAlloc + Signed);
201 //----------------------------------------------------------------------------
203 * \ingroup gdcmHeaderHelper
204 * \brief gets the info from 0028,0030 : Pixel Spacing
206 * @return X dimension of a pixel
208 float gdcmHeaderHelper::GetXSpacing() {
209 float xspacing, yspacing;
210 std::string StrSpacing = GetPubElValByNumber(0x0028,0x0030);
212 if (StrSpacing == GDCM_UNFOUND) {
213 dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
216 if( sscanf( StrSpacing.c_str(), "%f\\%f", &yspacing, &xspacing) != 2)
218 if (xspacing == 0.) {
219 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
220 // seems to be a bug in the header ...
221 sscanf( StrSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
225 //----------------------------------------------------------------------------
227 * \ingroup gdcmHeaderHelper
228 * \brief gets the info from 0028,0030 : Pixel Spacing
230 * @return Y dimension of a pixel
232 float gdcmHeaderHelper::GetYSpacing() {
233 float xspacing, yspacing;
234 std::string StrSpacing = GetPubElValByNumber(0x0028,0x0030);
236 if (StrSpacing == GDCM_UNFOUND) {
237 dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
240 if( sscanf( StrSpacing.c_str(), "%f\\%f", &yspacing, &xspacing) != 2)
242 if (xspacing == 0.) {
243 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
244 // seems to be a bug in the header ...
245 sscanf( StrSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
250 //----------------------------------------------------------------------------
252 *\ingroup gdcmHeaderHelper
253 *\brief gets the info from 0018,0088 : Space Between Slices
254 *\ else from 0018,0050 : Slice Thickness
256 * @return Z dimension of a voxel-to be
258 float gdcmHeaderHelper::GetZSpacing() {
259 // Spacing Between Slices : distance entre le milieu de chaque coupe
260 // Les coupes peuvent etre :
261 // jointives (Spacing between Slices = Slice Thickness)
262 // chevauchantes (Spacing between Slices < Slice Thickness)
263 // disjointes (Spacing between Slices > Slice Thickness)
264 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
265 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
266 // Si le Spacing Between Slices est absent,
267 // on suppose que les coupes sont jointives
269 std::string StrSpacingBSlices = GetPubElValByNumber(0x0018,0x0088);
271 if (StrSpacingBSlices == GDCM_UNFOUND) {
272 dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
273 std::string StrSliceThickness = GetPubElValByNumber(0x0018,0x0050);
274 if (StrSliceThickness == GDCM_UNFOUND)
277 // if no 'Spacing Between Slices' is found,
278 // we assume slices join together
279 // (no overlapping, no interslice gap)
280 // if they don't, we're fucked up
281 return atof(StrSliceThickness.c_str());
283 return atof(StrSpacingBSlices.c_str());
287 //----------------------------------------------------------------------------
289 // Image Position Patient (0020,0032):
290 // If not found (ACR_NEMA) we try Image Position (0020,0030)
291 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
292 // or Location (0020,0050)
293 // as the Z coordinate,
294 // 0. for all the coordinates if nothing is found
296 // TODO : find a way to inform the caller nothing was found
297 // TODO : How to tell the caller a wrong number of values was found?
300 * \ingroup gdcmHeaderHelper
301 * \brief gets the info from 0020,0032 : Image Position Patient
302 *\ else from 0020,0030 : Image Position (RET)
304 * @return up-left image corner position
306 float gdcmHeaderHelper::GetXOrigin() {
307 float xImPos, yImPos, zImPos;
308 std::string StrImPos = GetPubElValByNumber(0x0020,0x0032);
310 if (StrImPos == GDCM_UNFOUND) {
311 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position Patient (0020,0032)");
312 StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
313 if (StrImPos == GDCM_UNFOUND) {
314 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position (RET) (0020,0030)");
315 // How to tell the caller nothing was found ?
319 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
323 //----------------------------------------------------------------------------
325 * \ingroup gdcmHeaderHelper
326 * \brief gets the info from 0020,0032 : Image Position Patient
327 * \ else from 0020,0030 : Image Position (RET)
329 * @return up-left image corner position
331 float gdcmHeaderHelper::GetYOrigin() {
332 float xImPos, yImPos, zImPos;
333 std::string StrImPos = GetPubElValByNumber(0x0020,0x0032);
335 if (StrImPos == GDCM_UNFOUND) {
336 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position Patient (0020,0032)");
337 StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
338 if (StrImPos == GDCM_UNFOUND) {
339 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position (RET) (0020,0030)");
340 // How to tell the caller nothing was found ?
344 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
348 //----------------------------------------------------------------------------
350 * \ingroup gdcmHeaderHelper
351 * \brief gets the info from 0020,0032 : Image Position Patient
352 * \ else from 0020,0030 : Image Position (RET)
353 * \ else from 0020,1041 : Slice Location
354 * \ else from 0020,0050 : Location
356 * @return up-left image corner position
358 float gdcmHeaderHelper::GetZOrigin() {
359 float xImPos, yImPos, zImPos;
360 std::string StrImPos = GetPubElValByNumber(0x0020,0x0032);
361 if (StrImPos != GDCM_UNFOUND) {
362 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
363 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position Patient (0020,0032)");
364 return 0.; // bug in the element 0x0020,0x0032
369 StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
370 if (StrImPos != GDCM_UNFOUND) {
371 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
372 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
373 return 0.; // bug in the element 0x0020,0x0032
378 std::string StrSliceLocation = GetPubElValByNumber(0x0020,0x1041);// for *very* old ACR-NEMA images
379 if (StrSliceLocation != GDCM_UNFOUND) {
380 if( sscanf( StrSliceLocation.c_str(), "%f", &zImPos) !=1) {
381 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Slice Location (0020,1041)");
382 return 0.; // bug in the element 0x0020,0x1041
387 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: unfound Slice Location (0020,1041)");
388 std::string StrLocation = GetPubElValByNumber(0x0020,0x0050);
389 if (StrLocation != GDCM_UNFOUND) {
390 if( sscanf( StrLocation.c_str(), "%f", &zImPos) !=1) {
391 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Location (0020,0050)");
392 return 0.; // bug in the element 0x0020,0x0050
397 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Location (0020,0050)");
398 return 0.; // Hopeless
400 //----------------------------------------------------------------------------
402 * \ingroup gdcmHeaderHelper
403 * \brief gets the info from 0020,0013 : Image Number
405 * @return image number
407 int gdcmHeaderHelper::GetImageNumber() {
408 //The function i atoi() takes the address of an area of memory as parameter and converts
409 //the string stored at that location to an integer using the external decimal to internal
410 //binary conversion rules. This may be preferable to sscanf() since atoi() is a much smaller,
411 // simpler and faster function. sscanf() can do all possible conversions whereas atoi() can
412 //only do single decimal integer conversions.
413 std::string StrImNumber = GetPubElValByNumber(0x0020,0x0013); //0020 0013 IS REL Image Number
414 if (StrImNumber != GDCM_UNFOUND) {
415 return atoi( StrImNumber.c_str() );
419 //----------------------------------------------------------------------------
421 * \ingroup gdcmHeaderHelper
422 * \brief gets the info from 0008,0060 : Modality
423 * @return ModalityType
425 ModalityType gdcmHeaderHelper::GetModality(void) {
426 std::string StrModality = GetPubElValByNumber(0x0008,0x0060); //0008 0060 CS ID Modality
427 if (StrModality != GDCM_UNFOUND) {
428 if ( StrModality.find("AU") < StrModality.length()) return AU;
429 else if ( StrModality.find("AS") < StrModality.length()) return AS;
430 else if ( StrModality.find("BI") < StrModality.length()) return BI;
431 else if ( StrModality.find("CF") < StrModality.length()) return CF;
432 else if ( StrModality.find("CP") < StrModality.length()) return CP;
433 else if ( StrModality.find("CR") < StrModality.length()) return CR;
434 else if ( StrModality.find("CT") < StrModality.length()) return CT;
435 else if ( StrModality.find("CS") < StrModality.length()) return CS;
436 else if ( StrModality.find("DD") < StrModality.length()) return DD;
437 else if ( StrModality.find("DF") < StrModality.length()) return DF;
438 else if ( StrModality.find("DG") < StrModality.length()) return DG;
439 else if ( StrModality.find("DM") < StrModality.length()) return DM;
440 else if ( StrModality.find("DS") < StrModality.length()) return DS;
441 else if ( StrModality.find("DX") < StrModality.length()) return DX;
442 else if ( StrModality.find("ECG") < StrModality.length()) return ECG;
443 else if ( StrModality.find("EPS") < StrModality.length()) return EPS;
444 else if ( StrModality.find("FA") < StrModality.length()) return FA;
445 else if ( StrModality.find("FS") < StrModality.length()) return FS;
446 else if ( StrModality.find("HC") < StrModality.length()) return HC;
447 else if ( StrModality.find("HD") < StrModality.length()) return HD;
448 else if ( StrModality.find("LP") < StrModality.length()) return LP;
449 else if ( StrModality.find("LS") < StrModality.length()) return LS;
450 else if ( StrModality.find("MA") < StrModality.length()) return MA;
451 else if ( StrModality.find("MR") < StrModality.length()) return MR;
452 else if ( StrModality.find("NM") < StrModality.length()) return NM;
453 else if ( StrModality.find("OT") < StrModality.length()) return OT;
454 else if ( StrModality.find("PT") < StrModality.length()) return PT;
455 else if ( StrModality.find("RF") < StrModality.length()) return RF;
456 else if ( StrModality.find("RG") < StrModality.length()) return RG;
457 else if ( StrModality.find("RTDOSE") < StrModality.length()) return RTDOSE;
458 else if ( StrModality.find("RTIMAGE") < StrModality.length()) return RTIMAGE;
459 else if ( StrModality.find("RTPLAN") < StrModality.length()) return RTPLAN;
460 else if ( StrModality.find("RTSTRUCT")< StrModality.length()) return RTSTRUCT;
461 else if ( StrModality.find("SM") < StrModality.length()) return SM;
462 else if ( StrModality.find("ST") < StrModality.length()) return ST;
463 else if ( StrModality.find("TG") < StrModality.length()) return TG;
464 else if ( StrModality.find("US") < StrModality.length()) return US;
465 else if ( StrModality.find("VF") < StrModality.length()) return VF;
466 else if ( StrModality.find("XA") < StrModality.length()) return XA;
467 else if ( StrModality.find("XC") < StrModality.length()) return XC;
471 //throw error return value ???
472 // specified <> unknow in our database
479 //----------------------------------------------------------------------------
480 std::string gdcmHeaderHelper::GetStudyUID()
482 return GetPubElValByNumber(0x0020,0x000d); //0020 000d UI REL Study Instance UID
484 //----------------------------------------------------------------------------
485 std::string gdcmHeaderHelper::GetSeriesUID()
487 return GetPubElValByNumber(0x0020,0x000e); //0020 000e UI REL Series Instance UID
489 //----------------------------------------------------------------------------
490 std::string gdcmHeaderHelper::GetClassUID()
492 return GetPubElValByNumber(0x0008,0x0016); //0008 0016 UI ID SOP Class UID
494 //----------------------------------------------------------------------------
495 std::string gdcmHeaderHelper::GetInstanceUID()
497 return GetPubElValByNumber(0x0008,0x0018); //0008 0018 UI ID SOP Instance UID
499 //----------------------------------------------------------------------------
500 float gdcmHeaderHelper::GetRescaleIntercept()
503 std::string StrRescInter = GetPubElValByNumber(0x0028,0x1052); //0028 1052 DS IMG Rescale Intercept
504 if (StrRescInter != GDCM_UNFOUND) {
505 if( sscanf( StrRescInter.c_str(), "%f", &resInter) != 1) {
506 dbg.Verbose(0, "gdcmHeader::GetRescaleIntercept: Rescale Slope is empty");
507 // bug in the element 0x0028,0x1052
512 //----------------------------------------------------------------------------
513 float gdcmHeaderHelper::GetRescaleSlope()
516 std::string StrRescSlope = GetPubElValByNumber(0x0028,0x1053); //0028 1053 DS IMG Rescale Slope
517 if (StrRescSlope != GDCM_UNFOUND) {
518 if( sscanf( StrRescSlope.c_str(), "%f", &resSlope) != 1) {
519 dbg.Verbose(0, "gdcmHeader::GetRescaleSlope: Rescale Slope is empty");
520 // bug in the element 0x0028,0x1053
534 gdcmSerieHeaderHelper::~gdcmSerieHeaderHelper()
537 for (std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
538 it != CoherentGdcmFileList.end(); it++)
542 CoherentGdcmFileList.clear();
544 //----------------------------------------------------------------------------
546 * \ingroup gdcmHeaderHelper
547 * \brief gets the info from 0020,0037 : Image Orientation Patient
548 * @return cosines of image orientation patient
550 void gdcmHeaderHelper::GetImageOrientationPatient( float* iop ) {
552 //iop is supposed to be float[6]
553 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0;
555 std::string StrImOriPat = GetPubElValByNumber(0x0020,0x0037); // 0020 0037 DS REL Image Orientation (Patient)
556 if (StrImOriPat != GDCM_UNFOUND) {
557 if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
558 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
559 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0037)");
560 return ; // bug in the element 0x0020,0x0037
567 StrImOriPat = GetPubElValByNumber(0x0020,0x0035); //0020 0035 DS REL Image Orientation (RET)
568 if (StrImOriPat != GDCM_UNFOUND) {
569 if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
570 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
571 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0035)");
572 return ; // bug in the element 0x0020,0x0035
579 //----------------------------------------------------------------------------
581 * \ingroup gdcmHeaderHelper
582 * \brief add a gdcmFile to the list based on file name
584 void gdcmSerieHeaderHelper::AddFileName(std::string filename)
586 gdcmHeaderHelper *GdcmFile = new gdcmHeaderHelper( filename.c_str() );
587 this->CoherentGdcmFileList.push_back( GdcmFile );
589 //----------------------------------------------------------------------------
591 * \ingroup gdcmHeaderHelper
592 * \brief add a gdcmFile to the list
594 void gdcmSerieHeaderHelper::AddGdcmFile(gdcmHeaderHelper *file)
596 this->CoherentGdcmFileList.push_back( file );
598 //----------------------------------------------------------------------------
600 * \ingroup gdcmHeaderHelper
603 void gdcmSerieHeaderHelper::SetDirectory(std::string dir)
605 std::list<std::string> filenames_list;
606 GetDir(dir, filenames_list); //OS specific
608 for(std::list<std::string>::iterator it = filenames_list.begin(); it !=
609 filenames_list.end(); it++)
611 gdcmHeaderHelper *file = new gdcmHeaderHelper( it->c_str() );
612 this->CoherentGdcmFileList.push_back( file );
615 //----------------------------------------------------------------------------
616 //This could be implemented in a 'Strategy Pattern' approach
617 //But as I don't know how to do it, I leave it this way
618 //BTW, this is also a Strategy, I don't know this is the best approach :)
619 void gdcmSerieHeaderHelper::OrderGdcmFileList()
621 if( ImagePositionPatientOrdering() )
625 else if( ImageNumberOrdering() )
634 //----------------------------------------------------------------------------
636 * \ingroup gdcmHeaderHelper
638 We may order, considering :
640 -# Image Position Patient
643 //based on Jolinda's algorithm
644 bool gdcmSerieHeaderHelper::ImagePositionPatientOrdering()
646 //iop is calculated based on the file file
647 float *cosines = new float[6];
654 std::vector<float> distlist;
656 //!\todo rewrite this for loop.
657 for (std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
658 it != CoherentGdcmFileList.end(); it++)
661 (*it)->GetImageOrientationPatient(cosines);
663 //You only have to do this once for all slices in the volume. Next, for
664 //each slice, calculate the distance along the slice normal using the IPP
665 //tag ("dist" is initialized to zero before reading the first slice) :
666 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
667 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
668 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
670 ipp[0] = (*it)->GetXOrigin();
671 ipp[1] = (*it)->GetYOrigin();
672 ipp[2] = (*it)->GetZOrigin();
675 for (int i = 0; i < 3; ++i)
676 dist += normal[i]*ipp[i];
684 distlist.push_back( dist );
690 ipp[0] = (*it)->GetXOrigin();
691 ipp[1] = (*it)->GetYOrigin();
692 ipp[2] = (*it)->GetZOrigin();
695 for (int i = 0; i < 3; ++i)
696 dist += normal[i]*ipp[i];
705 distlist.push_back( dist );
707 min = (min < dist) ? min : dist;
708 max = (max > dist) ? max : dist;
713 //Then I order the slices according to the value "dist". Finally, once
714 //I've read in all the slices, I calculate the z-spacing as the difference
715 //between the "dist" values for the first two slices.
716 std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
717 //CoherentGdcmFileVector.reserve( n );
718 CoherentGdcmFileVector.resize( n );
719 //assert( CoherentGdcmFileVector.capacity() >= n );
721 float step = (max - min)/(n - 1);
725 //VC++ don't understand what scope is !! it -> it2
726 for (std::list<gdcmHeaderHelper*>::iterator it2 = CoherentGdcmFileList.begin();
727 it2 != CoherentGdcmFileList.end(); it2++, n++)
730 //Assumption: all files are present (no one missing)
731 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
733 CoherentGdcmFileVector[pos] = *it2;
736 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
738 //VC++ don't understand what scope is !! it -> it3
739 for (std::vector<gdcmHeaderHelper*>::iterator it3 = CoherentGdcmFileVector.begin();
740 it3 != CoherentGdcmFileVector.end(); it3++)
742 CoherentGdcmFileList.push_back( *it3 );
746 CoherentGdcmFileVector.clear();
751 //----------------------------------------------------------------------------
752 //Based on Image Number
753 bool gdcmSerieHeaderHelper::ImageNumberOrdering()
756 int n = 0;//CoherentGdcmFileList.size(); //O(N) operation !!
757 unsigned char *partition;
759 std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
760 min = max = (*it)->GetImageNumber();
762 for (; it != CoherentGdcmFileList.end(); it++, n++)
764 pos = (*it)->GetImageNumber();
767 min = (min < pos) ? min : pos;
770 //bzeros(partition, n); //Cette fonction est déconseillée, utilisez plutôt memset.
771 partition = new unsigned char[n];
772 memset(partition, 0, n);
774 std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
776 //VC++ don't understand what scope is !! it -> it2
777 for (std::list<gdcmHeaderHelper*>::iterator it2 = CoherentGdcmFileList.begin();
778 it2 != CoherentGdcmFileList.end(); it2++)
780 pos = (*it2)->GetImageNumber();
781 CoherentGdcmFileVector[pos - min] = *it2;
782 partition[pos - min]++;
786 for(int i=0; i<n ; i++)
788 mult *= partition[i];
791 //VC++ don't understand what scope is !! it -> it3
792 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
793 for (std::vector<gdcmHeaderHelper*>::iterator it3 = CoherentGdcmFileVector.begin();
794 it3 != CoherentGdcmFileVector.end(); it3++)
796 CoherentGdcmFileList.push_back( *it3 );
798 CoherentGdcmFileVector.clear();
803 //----------------------------------------------------------------------------
804 bool gdcmSerieHeaderHelper::FileNameOrdering()
807 //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
810 //----------------------------------------------------------------------------
811 std::list<gdcmHeaderHelper*> &gdcmSerieHeaderHelper::GetGdcmFileList()
813 return CoherentGdcmFileList;
815 //----------------------------------------------------------------------------