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