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