]> Creatis software - gdcm.git/blob - src/gdcmHeaderHelper.cxx
cc3053cb9b986ec688ec5653fe9cef669cb22a67
[gdcm.git] / src / gdcmHeaderHelper.cxx
1 // $Header: /cvs/public/gdcm/src/Attic/gdcmHeaderHelper.cxx,v 1.16 2003/11/07 14:57:58 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;
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          return 0.;  // bug in the element 0x0028,0x1052
509       } else {
510          return resInter;
511       }    
512    }  
513 }
514 //----------------------------------------------------------------------------
515 float gdcmHeaderHelper::GetRescaleSlope()
516 {
517   float resSlope;
518   std::string StrRescSlope = GetPubElValByNumber(0x0028,0x1053); //0028 1053 DS IMG Rescale Slope
519   if (StrRescSlope != GDCM_UNFOUND) {
520       if( sscanf( StrRescSlope.c_str(), "%f", &resSlope) != 1) {
521          dbg.Verbose(0, "gdcmHeader::GetRescaleSlope: Rescale Slope is empty");
522          return 1.;  // bug in the element 0x0028,0x1053
523       } else {
524          return resSlope;
525       }    
526    }  
527  
528 }
529
530
531
532
533
534
535
536
537
538 gdcmSerieHeaderHelper::~gdcmSerieHeaderHelper()
539 {
540   //! \todo
541   for (std::list<gdcmHeaderHelper*>::iterator it  = CoherentGdcmFileList.begin();
542         it != CoherentGdcmFileList.end(); it++)
543   {
544     delete *it;
545   }
546   CoherentGdcmFileList.clear();
547 }
548 //----------------------------------------------------------------------------
549 /**
550   * \ingroup gdcmHeaderHelper
551   * \brief gets the info from 0020,0037 : Image Orientation Patient
552   * @return cosines of image orientation patient
553   */
554 void gdcmHeaderHelper::GetImageOrientationPatient( float* iop ) {
555
556   //iop is supposed to be float[6]
557   iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0;
558   
559   std::string StrImOriPat = GetPubElValByNumber(0x0020,0x0037); // 0020 0037 DS REL Image Orientation (Patient)
560   if (StrImOriPat != GDCM_UNFOUND) {
561     if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f", 
562             &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
563          dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0037)");
564          return ;  // bug in the element 0x0020,0x0037
565     } 
566     else
567       return ;
568   }
569   
570   //For ACR-NEMA
571   StrImOriPat = GetPubElValByNumber(0x0020,0x0035); //0020 0035 DS REL Image Orientation (RET)
572   if (StrImOriPat != GDCM_UNFOUND) {
573     if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f", 
574             &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
575          dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0035)");
576          return ;  // bug in the element 0x0020,0x0035
577     } 
578     else
579       return ;
580   }
581 }
582
583 //----------------------------------------------------------------------------
584 /**
585   * \ingroup gdcmHeaderHelper
586   * \brief add a gdcmFile to the list based on file name
587   */
588 void gdcmSerieHeaderHelper::AddFileName(std::string filename)
589 {
590   gdcmHeaderHelper *GdcmFile = new gdcmHeaderHelper( filename.c_str() );
591   this->CoherentGdcmFileList.push_back( GdcmFile );
592 }
593 //----------------------------------------------------------------------------
594 /**
595   * \ingroup gdcmHeaderHelper
596   * \brief add a gdcmFile to the list
597   */
598 void gdcmSerieHeaderHelper::AddGdcmFile(gdcmHeaderHelper *file)
599 {
600   this->CoherentGdcmFileList.push_back( file );
601 }
602 //----------------------------------------------------------------------------
603 /**
604   * \ingroup gdcmHeaderHelper
605   * \brief \todo
606   */
607 void gdcmSerieHeaderHelper::SetDirectory(std::string dir)
608 {
609   std::list<std::string> filenames_list;
610   GetDir(dir, filenames_list);  //OS specific
611   
612   for(std::list<std::string>::iterator it = filenames_list.begin(); it !=
613   filenames_list.end(); it++)
614   {
615     gdcmHeaderHelper *file = new gdcmHeaderHelper( it->c_str() );
616     this->CoherentGdcmFileList.push_back( file );
617   }
618 }
619 //----------------------------------------------------------------------------
620 //This could be implemented in a 'Strategy Pattern' approach
621 //But as I don't know how to do it, I leave it this way
622 //BTW, this is also a Strategy, I don't know this is the best approach :)
623 void gdcmSerieHeaderHelper::OrderGdcmFileList()
624 {
625   if( ImagePositionPatientOrdering() )
626   {
627     return ;
628   }
629   else if( ImageNumberOrdering() )
630   {
631     return ;
632   }
633   else
634   {
635     FileNameOrdering();
636   }
637 }
638 //----------------------------------------------------------------------------
639 /**
640   * \ingroup gdcmHeaderHelper
641   * \brief 
642     We may order, considering :
643       -# Image Number
644       -# Image Position Patient
645       -# More to come :)
646 */
647 //based on Jolinda's algorithm
648 bool gdcmSerieHeaderHelper::ImagePositionPatientOrdering()
649 {
650   //iop is calculated based on the file file
651   float *cosines = new float[6];
652   float normal[3];
653   float ipp[3];
654   float dist;
655   float min, max;
656   bool first = true;
657   int n=0;
658   std::vector<float> distlist;
659
660   //!\todo rewrite this for loop.
661   for (std::list<gdcmHeaderHelper*>::iterator it  = CoherentGdcmFileList.begin();
662         it != CoherentGdcmFileList.end(); it++)
663   {
664     if(first) {
665       (*it)->GetImageOrientationPatient(cosines);
666       
667       //You only have to do this once for all slices in the volume. Next, for
668       //each slice, calculate the distance along the slice normal using the IPP
669       //tag ("dist" is initialized to zero before reading the first slice) :
670       normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
671       normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
672       normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
673   
674       ipp[0] = (*it)->GetXOrigin();
675       ipp[1] = (*it)->GetYOrigin();
676       ipp[2] = (*it)->GetZOrigin();
677
678       dist = 0;
679       for (int i = 0; i < 3; ++i)
680           dist += normal[i]*ipp[i];
681     
682       if( dist == 0 )
683       {
684         delete[] cosines;
685         return false;
686       }
687
688       distlist.push_back( dist );
689
690       max = min = dist;
691       first = false;
692     }
693     else {
694       ipp[0] = (*it)->GetXOrigin();
695       ipp[1] = (*it)->GetYOrigin();
696       ipp[2] = (*it)->GetZOrigin();
697   
698       dist = 0;
699       for (int i = 0; i < 3; ++i)
700           dist += normal[i]*ipp[i];
701
702       if( dist == 0 )
703       {
704         delete[] cosines;
705         return false;
706       }
707
708       
709       distlist.push_back( dist );
710
711       min = (min < dist) ? min : dist;
712       max = (max > dist) ? max : dist;
713     }
714     n++;
715   }
716
717     //Then I order the slices according to the value "dist". Finally, once
718     //I've read in all the slices, I calculate the z-spacing as the difference
719     //between the "dist" values for the first two slices.
720     std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
721     //CoherentGdcmFileVector.reserve( n );
722     CoherentGdcmFileVector.resize( n );
723     //assert( CoherentGdcmFileVector.capacity() >= n );
724
725     float step = (max - min)/(n - 1);
726     int pos;
727     n = 0;
728     
729     //VC++ don't understand what scope is !! it -> it2
730     for (std::list<gdcmHeaderHelper*>::iterator it2  = CoherentGdcmFileList.begin();
731         it2 != CoherentGdcmFileList.end(); it2++, n++)
732     {
733       //2*n sort algo !!
734       //Assumption: all files are present (no one missing)
735       pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
736             
737       CoherentGdcmFileVector[pos] = *it2;
738     }
739
740   CoherentGdcmFileList.clear();  //this doesn't delete list's element, node only
741   
742   //VC++ don't understand what scope is !! it -> it3
743   for (std::vector<gdcmHeaderHelper*>::iterator it3  = CoherentGdcmFileVector.begin();
744         it3 != CoherentGdcmFileVector.end(); it3++)
745   {
746     CoherentGdcmFileList.push_back( *it3 );
747   }
748
749   distlist.clear();
750   CoherentGdcmFileVector.clear();
751   delete[] cosines;
752   
753   return true;
754 }
755 //----------------------------------------------------------------------------
756 //Based on Image Number
757 bool gdcmSerieHeaderHelper::ImageNumberOrdering()
758 {
759   int min, max, pos;
760   int n = 0;//CoherentGdcmFileList.size(); //O(N) operation !!
761   unsigned char *partition;
762   
763   std::list<gdcmHeaderHelper*>::iterator it  = CoherentGdcmFileList.begin();
764   min = max = (*it)->GetImageNumber();
765
766   for (; it != CoherentGdcmFileList.end(); it++, n++)
767   {
768     pos = (*it)->GetImageNumber();
769
770     //else
771     min = (min < pos) ? min : pos;
772   }
773
774   //bzeros(partition, n); //Cette fonction est déconseillée, utilisez plutôt memset.
775   partition = new unsigned char[n];
776   memset(partition, 0, n);
777
778   std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
779
780   //VC++ don't understand what scope is !! it -> it2
781   for (std::list<gdcmHeaderHelper*>::iterator it2  = CoherentGdcmFileList.begin();
782         it2 != CoherentGdcmFileList.end(); it2++)
783   {
784     pos = (*it2)->GetImageNumber();
785     CoherentGdcmFileVector[pos - min] = *it2;
786     partition[pos - min]++;
787   }
788   
789   unsigned char mult;
790   for(int i=0; i<n ; i++)
791   {
792     mult *= partition[i];
793   }
794
795   //VC++ don't understand what scope is !! it -> it3
796   CoherentGdcmFileList.clear();  //this doesn't delete list's element, node only
797   for (std::vector<gdcmHeaderHelper*>::iterator it3  = CoherentGdcmFileVector.begin();
798         it3 != CoherentGdcmFileVector.end(); it3++)
799   {
800     CoherentGdcmFileList.push_back( *it3 );
801   }
802   CoherentGdcmFileVector.clear();
803   
804   delete[] partition;
805   return mult;
806 }
807 //----------------------------------------------------------------------------
808 bool gdcmSerieHeaderHelper::FileNameOrdering()
809 {
810   //using the sort
811   //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
812   return true;
813 }
814 //----------------------------------------------------------------------------
815 std::list<gdcmHeaderHelper*> &gdcmSerieHeaderHelper::GetGdcmFileList()
816 {
817   return CoherentGdcmFileList;
818 }
819 //----------------------------------------------------------------------------