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