1 // $Header: /cvs/public/gdcm/src/Attic/gdcmHeaderHelper.cxx,v 1.17 2003/11/12 14:06:35 malaterre 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 = GetElValByName("Bits Allocated");
179 if (BitsAlloc == GDCM_UNFOUND) {
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 = GetElValByName("Pixel Representation");
190 if (Signed == GDCM_UNFOUND) {
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 // TODO : translate into English
260 // Spacing Between Slices : distance entre le milieu de chaque coupe
261 // Les coupes peuvent etre :
262 // jointives (Spacing between Slices = Slice Thickness)
263 // chevauchantes (Spacing between Slices < Slice Thickness)
264 // disjointes (Spacing between Slices > Slice Thickness)
265 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
266 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
267 // Si le Spacing Between Slices est absent,
268 // on suppose que les coupes sont jointives
270 std::string StrSpacingBSlices = GetPubElValByNumber(0x0018,0x0088);
272 if (StrSpacingBSlices == GDCM_UNFOUND) {
273 dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
274 std::string StrSliceThickness = GetPubElValByNumber(0x0018,0x0050);
275 if (StrSliceThickness == GDCM_UNFOUND)
278 // if no 'Spacing Between Slices' is found,
279 // we assume slices join together
280 // (no overlapping, no interslice gap)
281 // if they don't, we're fucked up
282 return atof(StrSliceThickness.c_str());
284 return atof(StrSpacingBSlices.c_str());
288 //----------------------------------------------------------------------------
290 // Image Position Patient (0020,0032):
291 // If not found (ACR_NEMA) we try Image Position (0020,0030)
292 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
293 // or Location (0020,0050)
294 // as the Z coordinate,
295 // 0. for all the coordinates if nothing is found
297 // TODO : find a way to inform the caller nothing was found
298 // TODO : How to tell the caller a wrong number of values was found?
301 * \ingroup gdcmHeaderHelper
302 * \brief gets the info from 0020,0032 : Image Position Patient
303 *\ else from 0020,0030 : Image Position (RET)
305 * @return up-left image corner position
307 float gdcmHeaderHelper::GetXOrigin() {
308 float xImPos, yImPos, zImPos;
309 std::string StrImPos = GetPubElValByNumber(0x0020,0x0032);
311 if (StrImPos == GDCM_UNFOUND) {
312 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position Patient (0020,0032)");
313 StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
314 if (StrImPos == GDCM_UNFOUND) {
315 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position (RET) (0020,0030)");
316 // How to tell the caller nothing was found ?
320 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
324 //----------------------------------------------------------------------------
326 * \ingroup gdcmHeaderHelper
327 * \brief gets the info from 0020,0032 : Image Position Patient
328 * \ else from 0020,0030 : Image Position (RET)
330 * @return up-left image corner position
332 float gdcmHeaderHelper::GetYOrigin() {
333 float xImPos, yImPos, zImPos;
334 std::string StrImPos = GetPubElValByNumber(0x0020,0x0032);
336 if (StrImPos == GDCM_UNFOUND) {
337 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position Patient (0020,0032)");
338 StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
339 if (StrImPos == GDCM_UNFOUND) {
340 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position (RET) (0020,0030)");
341 // How to tell the caller nothing was found ?
345 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
349 //----------------------------------------------------------------------------
351 * \ingroup gdcmHeaderHelper
352 * \brief gets the info from 0020,0032 : Image Position Patient
353 * \ else from 0020,0030 : Image Position (RET)
354 * \ else from 0020,1041 : Slice Location
355 * \ else from 0020,0050 : Location
357 * @return up-left image corner position
359 float gdcmHeaderHelper::GetZOrigin() {
360 float xImPos, yImPos, zImPos;
361 std::string StrImPos = GetPubElValByNumber(0x0020,0x0032);
362 if (StrImPos != GDCM_UNFOUND) {
363 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
364 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position Patient (0020,0032)");
365 return 0.; // bug in the element 0x0020,0x0032
370 StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
371 if (StrImPos != GDCM_UNFOUND) {
372 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
373 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
374 return 0.; // bug in the element 0x0020,0x0032
379 std::string StrSliceLocation = GetPubElValByNumber(0x0020,0x1041);// for *very* old ACR-NEMA images
380 if (StrSliceLocation != GDCM_UNFOUND) {
381 if( sscanf( StrSliceLocation.c_str(), "%f", &zImPos) !=1) {
382 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Slice Location (0020,1041)");
383 return 0.; // bug in the element 0x0020,0x1041
388 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: unfound Slice Location (0020,1041)");
389 std::string StrLocation = GetPubElValByNumber(0x0020,0x0050);
390 if (StrLocation != GDCM_UNFOUND) {
391 if( sscanf( StrLocation.c_str(), "%f", &zImPos) !=1) {
392 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Location (0020,0050)");
393 return 0.; // bug in the element 0x0020,0x0050
398 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Location (0020,0050)");
399 return 0.; // Hopeless
401 //----------------------------------------------------------------------------
403 * \ingroup gdcmHeaderHelper
404 * \brief gets the info from 0020,0013 : Image Number
406 * @return image number
408 int gdcmHeaderHelper::GetImageNumber() {
409 //The function i atoi() takes the address of an area of memory as parameter and converts
410 //the string stored at that location to an integer using the external decimal to internal
411 //binary conversion rules. This may be preferable to sscanf() since atoi() is a much smaller,
412 // simpler and faster function. sscanf() can do all possible conversions whereas atoi() can
413 //only do single decimal integer conversions.
414 std::string StrImNumber = GetPubElValByNumber(0x0020,0x0013); //0020 0013 IS REL Image Number
415 if (StrImNumber != GDCM_UNFOUND) {
416 return atoi( StrImNumber.c_str() );
420 //----------------------------------------------------------------------------
422 * \ingroup gdcmHeaderHelper
423 * \brief gets the info from 0008,0060 : Modality
424 * @return ModalityType
426 ModalityType gdcmHeaderHelper::GetModality(void) {
427 std::string StrModality = GetPubElValByNumber(0x0008,0x0060); //0008 0060 CS ID Modality
428 if (StrModality != GDCM_UNFOUND) {
429 if ( StrModality.find("AU") < StrModality.length()) return AU;
430 else if ( StrModality.find("AS") < StrModality.length()) return AS;
431 else if ( StrModality.find("BI") < StrModality.length()) return BI;
432 else if ( StrModality.find("CF") < StrModality.length()) return CF;
433 else if ( StrModality.find("CP") < StrModality.length()) return CP;
434 else if ( StrModality.find("CR") < StrModality.length()) return CR;
435 else if ( StrModality.find("CT") < StrModality.length()) return CT;
436 else if ( StrModality.find("CS") < StrModality.length()) return CS;
437 else if ( StrModality.find("DD") < StrModality.length()) return DD;
438 else if ( StrModality.find("DF") < StrModality.length()) return DF;
439 else if ( StrModality.find("DG") < StrModality.length()) return DG;
440 else if ( StrModality.find("DM") < StrModality.length()) return DM;
441 else if ( StrModality.find("DS") < StrModality.length()) return DS;
442 else if ( StrModality.find("DX") < StrModality.length()) return DX;
443 else if ( StrModality.find("ECG") < StrModality.length()) return ECG;
444 else if ( StrModality.find("EPS") < StrModality.length()) return EPS;
445 else if ( StrModality.find("FA") < StrModality.length()) return FA;
446 else if ( StrModality.find("FS") < StrModality.length()) return FS;
447 else if ( StrModality.find("HC") < StrModality.length()) return HC;
448 else if ( StrModality.find("HD") < StrModality.length()) return HD;
449 else if ( StrModality.find("LP") < StrModality.length()) return LP;
450 else if ( StrModality.find("LS") < StrModality.length()) return LS;
451 else if ( StrModality.find("MA") < StrModality.length()) return MA;
452 else if ( StrModality.find("MR") < StrModality.length()) return MR;
453 else if ( StrModality.find("NM") < StrModality.length()) return NM;
454 else if ( StrModality.find("OT") < StrModality.length()) return OT;
455 else if ( StrModality.find("PT") < StrModality.length()) return PT;
456 else if ( StrModality.find("RF") < StrModality.length()) return RF;
457 else if ( StrModality.find("RG") < StrModality.length()) return RG;
458 else if ( StrModality.find("RTDOSE") < StrModality.length()) return RTDOSE;
459 else if ( StrModality.find("RTIMAGE") < StrModality.length()) return RTIMAGE;
460 else if ( StrModality.find("RTPLAN") < StrModality.length()) return RTPLAN;
461 else if ( StrModality.find("RTSTRUCT")< StrModality.length()) return RTSTRUCT;
462 else if ( StrModality.find("SM") < StrModality.length()) return SM;
463 else if ( StrModality.find("ST") < StrModality.length()) return ST;
464 else if ( StrModality.find("TG") < StrModality.length()) return TG;
465 else if ( StrModality.find("US") < StrModality.length()) return US;
466 else if ( StrModality.find("VF") < StrModality.length()) return VF;
467 else if ( StrModality.find("XA") < StrModality.length()) return XA;
468 else if ( StrModality.find("XC") < StrModality.length()) return XC;
472 //throw error return value ???
473 // specified <> unknow in our database
480 //----------------------------------------------------------------------------
481 std::string gdcmHeaderHelper::GetStudyUID()
483 return GetPubElValByNumber(0x0020,0x000d); //0020 000d UI REL Study Instance UID
485 //----------------------------------------------------------------------------
486 std::string gdcmHeaderHelper::GetSeriesUID()
488 return GetPubElValByNumber(0x0020,0x000e); //0020 000e UI REL Series Instance UID
490 //----------------------------------------------------------------------------
491 std::string gdcmHeaderHelper::GetClassUID()
493 return GetPubElValByNumber(0x0008,0x0016); //0008 0016 UI ID SOP Class UID
495 //----------------------------------------------------------------------------
496 std::string gdcmHeaderHelper::GetInstanceUID()
498 return GetPubElValByNumber(0x0008,0x0018); //0008 0018 UI ID SOP Instance UID
500 //----------------------------------------------------------------------------
501 float gdcmHeaderHelper::GetRescaleIntercept()
504 std::string StrRescInter = GetPubElValByNumber(0x0028,0x1052); //0028 1052 DS IMG Rescale Intercept
505 if (StrRescInter != GDCM_UNFOUND) {
506 if( sscanf( StrRescInter.c_str(), "%f", &resInter) != 1) {
507 dbg.Verbose(0, "gdcmHeader::GetRescaleIntercept: Rescale Slope is empty");
508 // bug in the element 0x0028,0x1052
513 //----------------------------------------------------------------------------
514 float gdcmHeaderHelper::GetRescaleSlope()
517 std::string StrRescSlope = GetPubElValByNumber(0x0028,0x1053); //0028 1053 DS IMG Rescale Slope
518 if (StrRescSlope != GDCM_UNFOUND) {
519 if( sscanf( StrRescSlope.c_str(), "%f", &resSlope) != 1) {
520 dbg.Verbose(0, "gdcmHeader::GetRescaleSlope: Rescale Slope is empty");
521 // bug in the element 0x0028,0x1053
535 gdcmSerieHeaderHelper::~gdcmSerieHeaderHelper()
538 for (std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
539 it != CoherentGdcmFileList.end(); it++)
543 CoherentGdcmFileList.clear();
545 //----------------------------------------------------------------------------
547 * \ingroup gdcmHeaderHelper
548 * \brief gets the info from 0020,0037 : Image Orientation Patient
549 * @return cosines of image orientation patient
551 void gdcmHeaderHelper::GetImageOrientationPatient( float* iop ) {
553 //iop is supposed to be float[6]
554 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0;
556 std::string StrImOriPat = GetPubElValByNumber(0x0020,0x0037); // 0020 0037 DS REL Image Orientation (Patient)
557 if (StrImOriPat != GDCM_UNFOUND) {
558 if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
559 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
560 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0037)");
561 return ; // bug in the element 0x0020,0x0037
568 StrImOriPat = GetPubElValByNumber(0x0020,0x0035); //0020 0035 DS REL Image Orientation (RET)
569 if (StrImOriPat != GDCM_UNFOUND) {
570 if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
571 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
572 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0035)");
573 return ; // bug in the element 0x0020,0x0035
580 //----------------------------------------------------------------------------
582 * \ingroup gdcmHeaderHelper
583 * \brief add a gdcmFile to the list based on file name
585 void gdcmSerieHeaderHelper::AddFileName(std::string filename)
587 gdcmHeaderHelper *GdcmFile = new gdcmHeaderHelper( filename.c_str() );
588 this->CoherentGdcmFileList.push_back( GdcmFile );
590 //----------------------------------------------------------------------------
592 * \ingroup gdcmHeaderHelper
593 * \brief add a gdcmFile to the list
595 void gdcmSerieHeaderHelper::AddGdcmFile(gdcmHeaderHelper *file)
597 this->CoherentGdcmFileList.push_back( file );
599 //----------------------------------------------------------------------------
601 * \ingroup gdcmHeaderHelper
604 void gdcmSerieHeaderHelper::SetDirectory(std::string dir)
606 std::list<std::string> filenames_list;
607 GetDir(dir, filenames_list); //OS specific
609 for(std::list<std::string>::iterator it = filenames_list.begin(); it !=
610 filenames_list.end(); it++)
612 gdcmHeaderHelper *file = new gdcmHeaderHelper( it->c_str() );
613 this->CoherentGdcmFileList.push_back( file );
616 //----------------------------------------------------------------------------
617 //This could be implemented in a 'Strategy Pattern' approach
618 //But as I don't know how to do it, I leave it this way
619 //BTW, this is also a Strategy, I don't know this is the best approach :)
620 void gdcmSerieHeaderHelper::OrderGdcmFileList()
622 if( ImagePositionPatientOrdering() )
626 else if( ImageNumberOrdering() )
635 //----------------------------------------------------------------------------
637 * \ingroup gdcmHeaderHelper
639 We may order, considering :
641 -# Image Position Patient
644 //based on Jolinda's algorithm
645 bool gdcmSerieHeaderHelper::ImagePositionPatientOrdering()
647 //iop is calculated based on the file file
648 float *cosines = new float[6];
655 std::vector<float> distlist;
657 //!\todo rewrite this for loop.
658 for (std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
659 it != CoherentGdcmFileList.end(); it++)
662 (*it)->GetImageOrientationPatient(cosines);
664 //You only have to do this once for all slices in the volume. Next, for
665 //each slice, calculate the distance along the slice normal using the IPP
666 //tag ("dist" is initialized to zero before reading the first slice) :
667 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
668 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
669 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
671 ipp[0] = (*it)->GetXOrigin();
672 ipp[1] = (*it)->GetYOrigin();
673 ipp[2] = (*it)->GetZOrigin();
676 for (int i = 0; i < 3; ++i)
677 dist += normal[i]*ipp[i];
685 distlist.push_back( dist );
691 ipp[0] = (*it)->GetXOrigin();
692 ipp[1] = (*it)->GetYOrigin();
693 ipp[2] = (*it)->GetZOrigin();
696 for (int i = 0; i < 3; ++i)
697 dist += normal[i]*ipp[i];
706 distlist.push_back( dist );
708 min = (min < dist) ? min : dist;
709 max = (max > dist) ? max : dist;
714 //Then I order the slices according to the value "dist". Finally, once
715 //I've read in all the slices, I calculate the z-spacing as the difference
716 //between the "dist" values for the first two slices.
717 std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
718 //CoherentGdcmFileVector.reserve( n );
719 CoherentGdcmFileVector.resize( n );
720 //assert( CoherentGdcmFileVector.capacity() >= n );
722 float step = (max - min)/(n - 1);
726 //VC++ don't understand what scope is !! it -> it2
727 for (std::list<gdcmHeaderHelper*>::iterator it2 = CoherentGdcmFileList.begin();
728 it2 != CoherentGdcmFileList.end(); it2++, n++)
731 //Assumption: all files are present (no one missing)
732 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
734 CoherentGdcmFileVector[pos] = *it2;
737 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
739 //VC++ don't understand what scope is !! it -> it3
740 for (std::vector<gdcmHeaderHelper*>::iterator it3 = CoherentGdcmFileVector.begin();
741 it3 != CoherentGdcmFileVector.end(); it3++)
743 CoherentGdcmFileList.push_back( *it3 );
747 CoherentGdcmFileVector.clear();
752 //----------------------------------------------------------------------------
753 //Based on Image Number
754 bool gdcmSerieHeaderHelper::ImageNumberOrdering()
757 int n = 0;//CoherentGdcmFileList.size(); //O(N) operation !!
758 unsigned char *partition;
760 std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
761 min = max = (*it)->GetImageNumber();
763 for (; it != CoherentGdcmFileList.end(); it++, n++)
765 pos = (*it)->GetImageNumber();
768 min = (min < pos) ? min : pos;
771 //bzeros(partition, n); //Cette fonction est déconseillée, utilisez plutôt memset.
772 partition = new unsigned char[n];
773 memset(partition, 0, n);
775 std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
777 //VC++ don't understand what scope is !! it -> it2
778 for (std::list<gdcmHeaderHelper*>::iterator it2 = CoherentGdcmFileList.begin();
779 it2 != CoherentGdcmFileList.end(); it2++)
781 pos = (*it2)->GetImageNumber();
782 CoherentGdcmFileVector[pos - min] = *it2;
783 partition[pos - min]++;
787 for(int i=0; i<n ; i++)
789 mult *= partition[i];
792 //VC++ don't understand what scope is !! it -> it3
793 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
794 for (std::vector<gdcmHeaderHelper*>::iterator it3 = CoherentGdcmFileVector.begin();
795 it3 != CoherentGdcmFileVector.end(); it3++)
797 CoherentGdcmFileList.push_back( *it3 );
799 CoherentGdcmFileVector.clear();
804 //----------------------------------------------------------------------------
805 bool gdcmSerieHeaderHelper::FileNameOrdering()
808 //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
811 //----------------------------------------------------------------------------
812 std::list<gdcmHeaderHelper*> &gdcmSerieHeaderHelper::GetGdcmFileList()
814 return CoherentGdcmFileList;
816 //----------------------------------------------------------------------------