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