]> Creatis software - gdcm.git/blob - src/gdcmHeaderHelper.cxx
gdcmHeaderHelper::GetNumberOfScalarComponents() added, to allow displaying RGB images...
[gdcm.git] / src / gdcmHeaderHelper.cxx
1 // $Header: /cvs/public/gdcm/src/Attic/gdcmHeaderHelper.cxx,v 1.10 2003/10/03 14:26:11 jpr Exp $
2
3 #include "gdcmHeaderHelper.h"
4
5 #include "gdcmUtil.h" //for debug
6 #include <math.h>
7 #include <algorithm>
8 #include <string.h> //for bzero
9
10 //directory manipulation (os indep).
11 //cygwin ???? -> _WIN32 ??
12 #ifdef _MSC_VER 
13 #include <windows.h> 
14 int GetDir(std::string dPath, std::list<std::string> &filenames)
15 {
16   //For now dPath should have an ending "\"
17   WIN32_FIND_DATA FileData; 
18   HANDLE hFile; 
19   hFile = FindFirstFile((dPath+"*").c_str(), &FileData); 
20   if ( hFile == INVALID_HANDLE_VALUE ) 
21   { 
22     //No files !
23     return false; 
24   } 
25   
26   if( strncmp(FileData.cFileName, ".", 1) != 0 )
27     filenames.push_back( dPath+FileData.cFileName );
28   while( FindNextFile(hFile, &FileData ) != 0)
29   { 
30     if( strncmp(FileData.cFileName, ".", 1) != 0 )
31       filenames.push_back( dPath+FileData.cFileName );
32   }
33   return true;
34 }
35
36 #else
37 #include <dirent.h>
38
39 int GetDir(std::string dPath, std::list<std::string> &filenames)
40 {
41  DIR *dir = opendir( dPath.c_str() );
42  struct dirent *entry;
43  while((entry = readdir(dir)) != NULL)
44  {
45 //   if( strncmp(entry->d_name, ".", 1) != 0 && strncmp(entry->d_name, "..", 2) != 0)
46    if( strncmp(entry->d_name, ".", 1) != 0 )
47    {
48       filenames.push_back( dPath + "/" + entry->d_name );
49    }
50  }
51  closedir(dir);
52  return true;
53 }
54
55 #endif
56
57 //----------------------------------------------------------------------------
58 /**
59  * \ingroup gdcmHeaderHelper
60  * \brief   cstor
61  */
62 gdcmHeaderHelper::gdcmHeaderHelper() : gdcmHeader( )
63 {
64 }
65 //----------------------------------------------------------------------------
66 /**
67  * \ingroup gdcmHeaderHelper
68  * \brief   cstor
69  */
70 gdcmHeaderHelper::gdcmHeaderHelper(const char *InFilename, 
71     bool exception_on_error) : gdcmHeader( InFilename , exception_on_error)
72 {
73 }
74 //----------------------------------------------------------------------------
75 /**
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.
79  *
80  */
81 int gdcmHeaderHelper::GetPixelSize() {
82    std::string PixelType = GetPixelType();
83    if (PixelType == "8U"  || PixelType == "8S")
84       return 1;
85    if (PixelType == "16U" || PixelType == "16S")
86       return 2;
87    if (PixelType == "32U" || PixelType == "32S")
88       return 4;
89    dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
90    return 0;
91 }
92
93 //----------------------------------------------------------------------------
94 /**
95   * \ingroup gdcmHeaderHelper
96   * \brief gets the info from 0028,0004 : Photometric Interp
97   * \           else 1.
98   * @return 1 if Gray level, 3 if Color
99   */
100 int gdcmHeaderHelper::GetNumberOfScalarComponents() {
101       std::string PhotometricInterpretation = 
102                   gdcmHeader::GetPubElValByNumber(0x0028,0x0004);
103
104 // The compiler will optimze, if it feels like !
105
106       if (PhotometricInterpretation == GDCM_UNFOUND) return 1;
107       if (PhotometricInterpretation == "MONOCHROME1") return 1;
108       if (PhotometricInterpretation == "MONOCHROME2") return 1;
109
110       return 3;
111 }
112 //----------------------------------------------------------------------------
113 /**
114  * \ingroup gdcmHeaderHelper
115  * \brief   Build the Pixel Type of the image.
116  *          Possible values are:
117  *          - 8U  unsigned  8 bit,
118  *          - 8S    signed  8 bit,
119  *          - 16U unsigned 16 bit,
120  *          - 16S   signed 16 bit,
121  *          - 32U unsigned 32 bit,
122  *          - 32S   signed 32 bit,
123  * \warning 12 bit images appear as 16 bit.
124  * @return  
125  */
126 std::string gdcmHeaderHelper::GetPixelType() {
127    std::string BitsAlloc;
128    BitsAlloc = GetElValByName("Bits Allocated");
129    if (BitsAlloc == GDCM_UNFOUND) {
130       dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
131       BitsAlloc = std::string("16");
132    }
133    if (BitsAlloc == "12")
134       BitsAlloc = std::string("16");
135
136    std::string Signed;
137    Signed = GetElValByName("Pixel Representation");
138    if (Signed == GDCM_UNFOUND) {
139       dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
140       BitsAlloc = std::string("0");
141    }
142    if (Signed == "0")
143       Signed = std::string("U");
144    else
145       Signed = std::string("S");
146
147    return( BitsAlloc + Signed);
148 }
149 //----------------------------------------------------------------------------
150 /**
151   * \ingroup gdcmHeaderHelper
152   * \brief gets the info from 0028,0030 : Pixel Spacing
153   * \           else 1.
154   * @return X dimension of a pixel
155   */
156 float gdcmHeaderHelper::GetXSpacing() {
157     float xspacing, yspacing;
158     std::string StrSpacing = GetPubElValByNumber(0x0028,0x0030);
159
160     if (StrSpacing == GDCM_UNFOUND) {
161        dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
162        return 1.;
163      }
164    if( sscanf( StrSpacing.c_str(), "%f\\%f", &xspacing, &yspacing) != 2)
165      return 0.;
166    //else
167    return xspacing;
168 }
169 //----------------------------------------------------------------------------
170 /**
171   * \ingroup gdcmHeaderHelper
172   * \brief gets the info from 0028,0030 : Pixel Spacing
173   * \           else 1.
174   * @return Y dimension of a pixel
175   */
176 float gdcmHeaderHelper::GetYSpacing() {
177    float xspacing, yspacing;
178    std::string StrSpacing = GetPubElValByNumber(0x0028,0x0030);
179   
180    if (StrSpacing == GDCM_UNFOUND) {
181       dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
182       return 1.;
183     }
184   if( sscanf( StrSpacing.c_str(), "%f\\%f", &xspacing, &yspacing) != 2)
185     return 0.;
186   if (yspacing == 0.) {
187     dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
188     // seems to be a bug in the header ...
189     sscanf( StrSpacing.c_str(), "%f\\0\\%f", &xspacing, &yspacing);
190   }
191   return yspacing;
192
193
194 //----------------------------------------------------------------------------
195 /**
196   *\ingroup gdcmHeaderHelper
197   *\brief gets the info from 0018,0088 : Space Between Slices
198   *\               else from 0018,0050 : Slice Thickness
199   *\               else 1.
200   * @return Z dimension of a voxel-to be
201   */
202 float gdcmHeaderHelper::GetZSpacing() {
203    // TODO : translate into English
204    // Spacing Between Slices : distance entre le milieu de chaque coupe
205    // Les coupes peuvent etre :
206    //   jointives     (Spacing between Slices = Slice Thickness)
207    //   chevauchantes (Spacing between Slices < Slice Thickness)
208    //   disjointes    (Spacing between Slices > Slice Thickness)
209    // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
210    //   ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
211    //   Si le Spacing Between Slices est absent, 
212    //   on suppose que les coupes sont jointives
213    
214    std::string StrSpacingBSlices = GetPubElValByNumber(0x0018,0x0088);
215
216    if (StrSpacingBSlices == GDCM_UNFOUND) {
217       dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
218       std::string StrSliceThickness = GetPubElValByNumber(0x0018,0x0050);       
219       if (StrSliceThickness == GDCM_UNFOUND)
220          return 1.;
221       else
222          // if no 'Spacing Between Slices' is found, 
223          // we assume slices join together
224          // (no overlapping, no interslice gap)
225          // if they don't, we're fucked up
226          return atof(StrSliceThickness.c_str());  
227    } else {
228       return atof(StrSpacingBSlices.c_str());
229    }
230 }
231
232 //----------------------------------------------------------------------------
233 //
234 // Image Position Patient                              (0020,0032):
235 // If not found (ACR_NEMA) we try Image Position       (0020,0030)
236 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
237 //                                   or Location       (0020,0050) 
238 // as the Z coordinate, 
239 // 0. for all the coordinates if nothing is found
240
241 // TODO : find a way to inform the caller nothing was found
242 // TODO : How to tell the caller a wrong number of values was found?
243
244 /**
245   * \ingroup gdcmHeaderHelper
246   * \brief gets the info from 0020,0032 : Image Position Patient
247   *\                else from 0020,0030 : Image Position (RET)
248   *\                else 0.
249   * @return up-left image corner position
250   */
251 float gdcmHeaderHelper::GetXOrigin() {
252     float xImPos, yImPos, zImPos;  
253     std::string StrImPos = GetPubElValByNumber(0x0020,0x0032);
254
255     if (StrImPos == GDCM_UNFOUND) {
256        dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position Patient (0020,0032)");
257        StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
258        if (StrImPos == GDCM_UNFOUND) {
259           dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position (RET) (0020,0030)");
260           // How to tell the caller nothing was found ?
261          return 0.;
262        }  
263      }
264    if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
265      return 0.;
266    return xImPos;
267 }
268 //----------------------------------------------------------------------------
269 /**
270   * \ingroup gdcmHeaderHelper
271   * \brief gets the info from 0020,0032 : Image Position Patient
272   * \               else from 0020,0030 : Image Position (RET)
273   * \               else 0.
274   * @return up-left image corner position
275   */
276 float gdcmHeaderHelper::GetYOrigin() {
277     float xImPos, yImPos, zImPos;
278     std::string StrImPos = GetPubElValByNumber(0x0020,0x0032);
279
280     if (StrImPos == GDCM_UNFOUND) {
281        dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position Patient (0020,0032)");
282        StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
283        if (StrImPos == GDCM_UNFOUND) {
284           dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position (RET) (0020,0030)");
285           // How to tell the caller nothing was found ?
286            return 0.;
287        }  
288      }
289    if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
290      return 0.;
291    return yImPos;
292 }
293 //----------------------------------------------------------------------------
294 /**
295   * \ingroup gdcmHeaderHelper
296   * \brief gets the info from 0020,0032 : Image Position Patient
297   * \               else from 0020,0030 : Image Position (RET)
298   * \               else from 0020,1041 : Slice Location
299   * \               else from 0020,0050 : Location
300   * \               else 0.
301   * @return up-left image corner position
302   */
303 float gdcmHeaderHelper::GetZOrigin() {
304    float xImPos, yImPos, zImPos; 
305    std::string StrImPos = GetPubElValByNumber(0x0020,0x0032);
306    if (StrImPos != GDCM_UNFOUND) {
307       if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
308          dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position Patient (0020,0032)");
309          return 0.;  // bug in the element 0x0020,0x0032
310       } else {
311          return zImPos;
312       }    
313    }  
314    StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
315    if (StrImPos != GDCM_UNFOUND) {
316       if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
317          dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
318          return 0.;  // bug in the element 0x0020,0x0032
319       } else {
320          return zImPos;
321       }    
322    }                
323    std::string StrSliceLocation = GetPubElValByNumber(0x0020,0x1041);// for *very* old ACR-NEMA images
324    if (StrSliceLocation != GDCM_UNFOUND) {
325       if( sscanf( StrSliceLocation.c_str(), "%f", &zImPos) !=1) {
326          dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Slice Location (0020,1041)");
327          return 0.;  // bug in the element 0x0020,0x1041
328       } else {
329          return zImPos;
330       }
331    }   
332    dbg.Verbose(0, "gdcmHeader::GetZImagePosition: unfound Slice Location (0020,1041)");
333    std::string StrLocation = GetPubElValByNumber(0x0020,0x0050);
334    if (StrLocation != GDCM_UNFOUND) {
335       if( sscanf( StrLocation.c_str(), "%f", &zImPos) !=1) {
336          dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Location (0020,0050)");
337          return 0.;  // bug in the element 0x0020,0x0050
338       } else {
339          return zImPos;
340       }
341    }
342    dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Location (0020,0050)");  
343    return 0.; // Hopeless
344 }
345 //----------------------------------------------------------------------------
346 /**
347   * \ingroup gdcmHeaderHelper
348   * \brief gets the info from 0020,0013 : Image Number
349   * \               else 0.
350   * @return image number
351   */
352 int gdcmHeaderHelper::GetImageNumber() {
353   //The function i atoi() takes the address of an area of memory as parameter and converts 
354   //the string stored at that location to an integer using the external decimal to internal
355   //binary conversion rules. This may be preferable to sscanf() since atoi() is a much smaller,
356   // simpler and faster function. sscanf() can do all possible conversions whereas atoi() can 
357   //only do single decimal integer conversions.
358   std::string StrImNumber = GetPubElValByNumber(0x0020,0x0013); //0020 0013 IS REL Image Number
359   if (StrImNumber != GDCM_UNFOUND) {
360     return atoi( StrImNumber.c_str() );
361   }
362   return 0;   //Hopeless
363 }
364 //----------------------------------------------------------------------------
365 /**
366   * \ingroup gdcmHeaderHelper
367   * \brief gets the info from 0008,0060 : Modality
368   * @return ModalityType
369   */
370 ModalityType gdcmHeaderHelper::GetModality(void) {
371   std::string StrModality = GetPubElValByNumber(0x0008,0x0060); //0008 0060 CS ID Modality
372   if (StrModality != GDCM_UNFOUND) {
373          if ( StrModality.find("AU") < StrModality.length()) return AU;
374     else if ( StrModality.find("AS") < StrModality.length()) return AS;
375     else if ( StrModality.find("BI") < StrModality.length()) return BI;
376     else if ( StrModality.find("CF") < StrModality.length()) return CF;
377     else if ( StrModality.find("CP") < StrModality.length()) return CP;
378     else if ( StrModality.find("CR") < StrModality.length()) return CR;
379     else if ( StrModality.find("CT") < StrModality.length()) return CT;
380     else if ( StrModality.find("CS") < StrModality.length()) return CS;
381     else if ( StrModality.find("DD") < StrModality.length()) return DD;
382     else if ( StrModality.find("DF") < StrModality.length()) return DF;
383     else if ( StrModality.find("DG") < StrModality.length()) return DG;
384     else if ( StrModality.find("DM") < StrModality.length()) return DM;
385     else if ( StrModality.find("DS") < StrModality.length()) return DS;
386     else if ( StrModality.find("DX") < StrModality.length()) return DX;
387     else if ( StrModality.find("ECG") < StrModality.length()) return ECG;
388     else if ( StrModality.find("EPS") < StrModality.length()) return EPS;
389     else if ( StrModality.find("FA") < StrModality.length()) return FA;
390     else if ( StrModality.find("FS") < StrModality.length()) return FS;
391     else if ( StrModality.find("HC") < StrModality.length()) return HC;
392     else if ( StrModality.find("HD") < StrModality.length()) return HD;
393     else if ( StrModality.find("LP") < StrModality.length()) return LP;
394     else if ( StrModality.find("LS") < StrModality.length()) return LS;
395     else if ( StrModality.find("MA") < StrModality.length()) return MA;
396     else if ( StrModality.find("MR") < StrModality.length()) return MR;
397     else if ( StrModality.find("NM") < StrModality.length()) return NM;
398     else if ( StrModality.find("OT") < StrModality.length()) return OT;
399     else if ( StrModality.find("PT") < StrModality.length()) return PT;
400     else if ( StrModality.find("RF") < StrModality.length()) return RF;
401     else if ( StrModality.find("RG") < StrModality.length()) return RG;
402     else if ( StrModality.find("RTDOSE")  < StrModality.length()) return RTDOSE;
403     else if ( StrModality.find("RTIMAGE") < StrModality.length()) return RTIMAGE;
404     else if ( StrModality.find("RTPLAN")  < StrModality.length()) return RTPLAN;
405     else if ( StrModality.find("RTSTRUCT")< StrModality.length()) return RTSTRUCT;
406     else if ( StrModality.find("SM") < StrModality.length()) return SM;
407     else if ( StrModality.find("ST") < StrModality.length()) return ST;
408     else if ( StrModality.find("TG") < StrModality.length()) return TG;
409     else if ( StrModality.find("US") < StrModality.length()) return US;
410     else if ( StrModality.find("VF") < StrModality.length()) return VF;
411     else if ( StrModality.find("XA") < StrModality.length()) return XA;
412     else if ( StrModality.find("XC") < StrModality.length()) return XC;
413
414     else
415     {
416       //throw error return value ???
417       // specified <> unknow in our database
418       return Unknow;
419     }
420   }
421   return Unknow;
422 }
423
424 //----------------------------------------------------------------------------
425 std::string gdcmHeaderHelper::GetStudyUID()
426 {
427   return GetPubElValByNumber(0x0020,0x000d); //0020 000d UI REL Study Instance UID
428 }
429 //----------------------------------------------------------------------------
430 std::string gdcmHeaderHelper::GetSeriesUID()
431 {
432   return GetPubElValByNumber(0x0020,0x000e); //0020 000e UI REL Series Instance UID
433 }
434 //----------------------------------------------------------------------------
435 std::string gdcmHeaderHelper::GetClassUID()
436 {
437   return GetPubElValByNumber(0x0008,0x0016); //0008 0016 UI ID SOP Class UID
438 }
439 //----------------------------------------------------------------------------
440 std::string gdcmHeaderHelper::GetInstanceUID()
441 {
442   return GetPubElValByNumber(0x0008,0x0018); //0008 0018 UI ID SOP Instance UID
443 }
444
445
446
447
448
449
450
451
452
453 gdcmSerieHeaderHelper::~gdcmSerieHeaderHelper()
454 {
455   //! \todo
456   for (std::list<gdcmHeaderHelper*>::iterator it  = CoherentGdcmFileList.begin();
457         it != CoherentGdcmFileList.end(); it++)
458   {
459     delete *it;
460   }
461   CoherentGdcmFileList.clear();
462 }
463 //----------------------------------------------------------------------------
464 /**
465   * \ingroup gdcmHeaderHelper
466   * \brief gets the info from 0020,0037 : Image Orientation Patient
467   * @return cosines of image orientation patient
468   */
469 void gdcmHeaderHelper::GetImageOrientationPatient( float* iop ) {
470
471   //iop is supposed to be float[6]
472   iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0;
473   
474   std::string StrImOriPat = GetPubElValByNumber(0x0020,0x0037); // 0020 0037 DS REL Image Orientation (Patient)
475   if (StrImOriPat != GDCM_UNFOUND) {
476     if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f", 
477             &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
478          dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0037)");
479          return ;  // bug in the element 0x0020,0x0037
480     } 
481     else
482       return ;
483   }
484   
485   //For ACR-NEMA
486   StrImOriPat = GetPubElValByNumber(0x0020,0x0035); //0020 0035 DS REL Image Orientation (RET)
487   if (StrImOriPat != GDCM_UNFOUND) {
488     if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f", 
489             &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
490          dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0035)");
491          return ;  // bug in the element 0x0020,0x0035
492     } 
493     else
494       return ;
495   }
496 }
497
498 //----------------------------------------------------------------------------
499 /**
500   * \ingroup gdcmHeaderHelper
501   * \brief add a gdcmFile to the list based on file name
502   */
503 void gdcmSerieHeaderHelper::AddFileName(std::string filename)
504 {
505   gdcmHeaderHelper *GdcmFile = new gdcmHeaderHelper( filename.c_str() );
506   this->CoherentGdcmFileList.push_back( GdcmFile );
507 }
508 //----------------------------------------------------------------------------
509 /**
510   * \ingroup gdcmHeaderHelper
511   * \brief add a gdcmFile to the list
512   */
513 void gdcmSerieHeaderHelper::AddGdcmFile(gdcmHeaderHelper *file)
514 {
515   this->CoherentGdcmFileList.push_back( file );
516 }
517 //----------------------------------------------------------------------------
518 /**
519   * \ingroup gdcmHeaderHelper
520   * \brief \todo
521   */
522 void gdcmSerieHeaderHelper::SetDirectory(std::string dir)
523 {
524   std::list<std::string> filenames_list;
525   GetDir(dir, filenames_list);  //OS specific
526   
527   for(std::list<std::string>::iterator it = filenames_list.begin(); it !=
528   filenames_list.end(); it++)
529   {
530     gdcmHeaderHelper *file = new gdcmHeaderHelper( it->c_str() );
531     this->CoherentGdcmFileList.push_back( file );
532   }
533 }
534 //----------------------------------------------------------------------------
535 //This could be implemented in a 'Strategy Pattern' approach
536 //But as I don't know how to do it, I leave it this way
537 //BTW, this is also a Strategy, I don't know this is the best approach :)
538 void gdcmSerieHeaderHelper::OrderGdcmFileList()
539 {
540   if( ImagePositionPatientOrdering() )
541   {
542     return ;
543   }
544   else if( ImageNumberOrdering() )
545   {
546     return ;
547   }
548   else
549   {
550     FileNameOrdering();
551   }
552 }
553 //----------------------------------------------------------------------------
554 /**
555   * \ingroup gdcmHeaderHelper
556   * \brief 
557     We may order, considering :
558       -# Image Number
559       -# Image Position Patient
560       -# More to come :)
561 */
562 //based on Jolinda's algorithm
563 bool gdcmSerieHeaderHelper::ImagePositionPatientOrdering()
564 {
565   //iop is calculated based on the file file
566   float *cosines = new float[6];
567   float normal[3];
568   float ipp[3];
569   float dist;
570   float min, max;
571   bool first = true;
572   int n=0;
573   std::vector<float> distlist;
574
575   //!\todo rewrite this for loop.
576   for (std::list<gdcmHeaderHelper*>::iterator it  = CoherentGdcmFileList.begin();
577         it != CoherentGdcmFileList.end(); it++)
578   {
579     if(first) {
580       (*it)->GetImageOrientationPatient(cosines);
581       
582       //You only have to do this once for all slices in the volume. Next, for
583       //each slice, calculate the distance along the slice normal using the IPP
584       //tag ("dist" is initialized to zero before reading the first slice) :
585       normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
586       normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
587       normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
588   
589       ipp[0] = (*it)->GetXOrigin();
590       ipp[1] = (*it)->GetYOrigin();
591       ipp[2] = (*it)->GetZOrigin();
592
593       dist = 0;
594       for (int i = 0; i < 3; ++i)
595           dist += normal[i]*ipp[i];
596     
597       if( dist == 0 )
598       {
599         delete[] cosines;
600         return false;
601       }
602
603       distlist.push_back( dist );
604
605       max = min = dist;
606       first = false;
607     }
608     else {
609       ipp[0] = (*it)->GetXOrigin();
610       ipp[1] = (*it)->GetYOrigin();
611       ipp[2] = (*it)->GetZOrigin();
612   
613       dist = 0;
614       for (int i = 0; i < 3; ++i)
615           dist += normal[i]*ipp[i];
616
617       if( dist == 0 )
618       {
619         delete[] cosines;
620         return false;
621       }
622
623       
624       distlist.push_back( dist );
625
626       min = (min < dist) ? min : dist;
627       max = (max > dist) ? max : dist;
628     }
629     n++;
630   }
631
632     //Then I order the slices according to the value "dist". Finally, once
633     //I've read in all the slices, I calculate the z-spacing as the difference
634     //between the "dist" values for the first two slices.
635     std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
636     //CoherentGdcmFileVector.reserve( n );
637     CoherentGdcmFileVector.resize( n );
638     //assert( CoherentGdcmFileVector.capacity() >= n );
639
640     float step = (max - min)/(n - 1);
641     int pos;
642     n = 0;
643     
644     //VC++ don't understand what scope is !! it -> it2
645     for (std::list<gdcmHeaderHelper*>::iterator it2  = CoherentGdcmFileList.begin();
646         it2 != CoherentGdcmFileList.end(); it2++, n++)
647     {
648       //2*n sort algo !!
649       //Assumption: all files are present (no one missing)
650       pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
651             
652       CoherentGdcmFileVector[pos] = *it2;
653     }
654
655   CoherentGdcmFileList.clear();  //this doesn't delete list's element, node only
656   
657   //VC++ don't understand what scope is !! it -> it3
658   for (std::vector<gdcmHeaderHelper*>::iterator it3  = CoherentGdcmFileVector.begin();
659         it3 != CoherentGdcmFileVector.end(); it3++)
660   {
661     CoherentGdcmFileList.push_back( *it3 );
662   }
663
664   distlist.clear();
665   CoherentGdcmFileVector.clear();
666   delete[] cosines;
667   
668   return true;
669 }
670 //----------------------------------------------------------------------------
671 //Based on Image Number
672 bool gdcmSerieHeaderHelper::ImageNumberOrdering()
673 {
674   int min, max, pos;
675   int n = 0;//CoherentGdcmFileList.size(); //O(N) operation !!
676   unsigned char *partition;
677   
678   std::list<gdcmHeaderHelper*>::iterator it  = CoherentGdcmFileList.begin();
679   min = max = (*it)->GetImageNumber();
680
681   for (; it != CoherentGdcmFileList.end(); it++, n++)
682   {
683     pos = (*it)->GetImageNumber();
684
685     //else
686     min = (min < pos) ? min : pos;
687   }
688
689   //bzeros(partition, n); //Cette fonction est déconseillée, utilisez plutôt memset.
690   partition = new unsigned char[n];
691   memset(partition, 0, n);
692
693   std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
694
695   //VC++ don't understand what scope is !! it -> it2
696   for (std::list<gdcmHeaderHelper*>::iterator it2  = CoherentGdcmFileList.begin();
697         it2 != CoherentGdcmFileList.end(); it2++)
698   {
699     pos = (*it2)->GetImageNumber();
700     CoherentGdcmFileVector[pos - min] = *it2;
701     partition[pos - min]++;
702   }
703   
704   unsigned char mult;
705   for(int i=0; i<n ; i++)
706   {
707     mult *= partition[i];
708   }
709
710   //VC++ don't understand what scope is !! it -> it3
711   CoherentGdcmFileList.clear();  //this doesn't delete list's element, node only
712   for (std::vector<gdcmHeaderHelper*>::iterator it3  = CoherentGdcmFileVector.begin();
713         it3 != CoherentGdcmFileVector.end(); it3++)
714   {
715     CoherentGdcmFileList.push_back( *it3 );
716   }
717   CoherentGdcmFileVector.clear();
718   
719   delete[] partition;
720   return mult;
721 }
722 //----------------------------------------------------------------------------
723 bool gdcmSerieHeaderHelper::FileNameOrdering()
724 {
725   //using the sort
726   //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
727   return true;
728 }
729 //----------------------------------------------------------------------------
730 std::list<gdcmHeaderHelper*> &gdcmSerieHeaderHelper::GetGdcmFileList()
731 {
732   return CoherentGdcmFileList;
733 }
734 //----------------------------------------------------------------------------