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