1 // gdcmHeaderHelper.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmHeaderHelper.h"
4 #include "gdcmDirList.h"
6 #include "gdcmUtil.h" //for debug
10 //-----------------------------------------------------------------------------
12 //-----------------------------------------------------------------------------
13 // Constructor / Destructor
15 * \ingroup gdcmHeaderHelper
18 gdcmHeaderHelper::gdcmHeaderHelper() : gdcmHeader( ) {
23 * \ingroup gdcmHeaderHelper
25 * @param InFilename Name of the file to deal with
26 * @param exception_on_error
27 * @param enable_sequences = true to allow the header
28 * to be parsed *inside* the SeQuences,
29 * when they have an actual length
30 * @param ignore_shadow = true if user wants to skip shadow groups
31 * during parsing, to save memory space
33 gdcmHeaderHelper::gdcmHeaderHelper(const char *InFilename,
34 bool exception_on_error,
35 bool enable_sequences,
37 : gdcmHeader( InFilename,
44 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
50 * \ingroup gdcmHeaderHelper
51 * \brief Returns the size (in bytes) of a single pixel of data.
52 * @return The size in bytes of a single pixel of data.
55 int gdcmHeaderHelper::GetPixelSize() {
57 // 0028 0100 US IMG Bits Allocated
58 // (in order no to be messed up by old RGB images)
59 if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
62 std::string PixelType = GetPixelType();
63 if (PixelType == "8U" || PixelType == "8S")
65 if (PixelType == "16U" || PixelType == "16S")
67 if (PixelType == "32U" || PixelType == "32S")
69 if (PixelType == "FD") // to help unfortunate users to manage DOUBLE
71 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
76 * \ingroup gdcmHeaderHelper
77 * \brief Build the Pixel Type of the image.
78 * Possible values are:
79 * - 8U unsigned 8 bit,
81 * - 16U unsigned 16 bit,
82 * - 16S signed 16 bit,
83 * - 32U unsigned 32 bit,
84 * - 32S signed 32 bit,
86 * \warning 12 bit images appear as 16 bit.
87 * 24 bit images appear as 8 bit
88 * 64 bit means 'DOUBLE' images
89 * (no DOUBLE images in kosher DICOM,
90 * but so usefull for people that miss them ;-)
93 std::string gdcmHeaderHelper::GetPixelType() {
94 std::string BitsAlloc;
95 BitsAlloc = GetEntryByNumber(0x0028, 0x0100);
96 if (BitsAlloc == GDCM_UNFOUND) { // Bits Allocated
97 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
98 BitsAlloc = std::string("16");
100 if (BitsAlloc == "12") // It will be unpacked
101 BitsAlloc = std::string("16");
102 else if (BitsAlloc == "24") // (in order no to be messed up
103 BitsAlloc = std::string("8"); // by old RGB images)
106 Signed = GetEntryByNumber(0x0028, 0x0103);
107 if (Signed == GDCM_UNFOUND) { // "Pixel Representation"
108 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
109 BitsAlloc = std::string("0");
111 if (BitsAlloc == "64") // to help users that want to deal with DOUBLE
115 Signed = std::string("U");
117 Signed = std::string("S");
119 return( BitsAlloc + Signed);
123 * \ingroup gdcmHeaderHelper
124 * \brief gets the info from 0028,0030 : Pixel Spacing
126 * @return X dimension of a pixel
128 float gdcmHeaderHelper::GetXSpacing() {
129 float xspacing, yspacing;
130 std::string StrSpacing = GetEntryByNumber(0x0028,0x0030);
132 if (StrSpacing == GDCM_UNFOUND) {
133 dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
136 if( sscanf( StrSpacing.c_str(), "%f\\%f", &yspacing, &xspacing) != 2)
138 if (xspacing == 0.) {
139 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
140 // seems to be a bug in the header ...
141 sscanf( StrSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
147 * \ingroup gdcmHeaderHelper
148 * \brief gets the info from 0028,0030 : Pixel Spacing
150 * @return Y dimension of a pixel
152 float gdcmHeaderHelper::GetYSpacing() {
153 float xspacing, yspacing;
154 std::string StrSpacing = GetEntryByNumber(0x0028,0x0030);
156 if (StrSpacing == GDCM_UNFOUND) {
157 dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
160 if( sscanf( StrSpacing.c_str(), "%f\\%f", &yspacing, &xspacing) != 2)
162 if (xspacing == 0.) {
163 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
164 // seems to be a bug in the header ...
165 sscanf( StrSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
171 *\ingroup gdcmHeaderHelper
172 *\brief gets the info from 0018,0088 : Space Between Slices
173 * else from 0018,0050 : Slice Thickness
175 * @return Z dimension of a voxel-to be
177 float gdcmHeaderHelper::GetZSpacing() {
178 // Spacing Between Slices : distance entre le milieu de chaque coupe
179 // Les coupes peuvent etre :
180 // jointives (Spacing between Slices = Slice Thickness)
181 // chevauchantes (Spacing between Slices < Slice Thickness)
182 // disjointes (Spacing between Slices > Slice Thickness)
183 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
184 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
185 // Si le Spacing Between Slices est absent,
186 // on suppose que les coupes sont jointives
188 std::string StrSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
190 if (StrSpacingBSlices == GDCM_UNFOUND) {
191 dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
192 std::string StrSliceThickness = GetEntryByNumber(0x0018,0x0050);
193 if (StrSliceThickness == GDCM_UNFOUND)
196 // if no 'Spacing Between Slices' is found,
197 // we assume slices join together
198 // (no overlapping, no interslice gap)
199 // if they don't, we're fucked up
200 return atof(StrSliceThickness.c_str());
202 return atof(StrSpacingBSlices.c_str());
207 *\ingroup gdcmHeaderHelper
208 *\brief gets the info from 0028,1052 : Rescale Intercept
209 * @return Rescale Intercept
211 float gdcmHeaderHelper::GetRescaleIntercept() {
213 std::string StrRescInter = GetEntryByNumber(0x0028,0x1052); //0028 1052 DS IMG Rescale Intercept
214 if (StrRescInter != GDCM_UNFOUND) {
215 if( sscanf( StrRescInter.c_str(), "%f", &resInter) != 1) {
216 dbg.Verbose(0, "gdcmHeader::GetRescaleIntercept: Rescale Slope is empty");
217 // bug in the element 0x0028,0x1052
224 *\ingroup gdcmHeaderHelper
225 *\brief gets the info from 0028,1053 : Rescale Slope
226 * @return Rescale Slope
228 float gdcmHeaderHelper::GetRescaleSlope() {
230 std::string StrRescSlope = GetEntryByNumber(0x0028,0x1053); //0028 1053 DS IMG Rescale Slope
231 if (StrRescSlope != GDCM_UNFOUND) {
232 if( sscanf( StrRescSlope.c_str(), "%f", &resSlope) != 1) {
233 dbg.Verbose(0, "gdcmHeader::GetRescaleSlope: Rescale Slope is empty");
234 // bug in the element 0x0028,0x1053
241 * \ingroup gdcmHeaderHelper
242 * \brief This function is intended to user who doesn't want
243 * to have to manage a LUT and expects to get an RBG Pixel image
244 * (or a monochrome one ...)
245 * \warning to be used with GetImagePixels()
246 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
248 int gdcmHeaderHelper::GetNumberOfScalarComponents() {
249 if (GetSamplesPerPixel() ==3)
252 // 0028 0100 US IMG Bits Allocated
253 // (in order no to be messed up by old RGB images)
254 if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
257 std::string PhotometricInterpretation =
258 gdcmHeader::GetEntryByNumber(0x0028,0x0004);
260 if ( ( PhotometricInterpretation == "PALETTE COLOR ") ) {
261 if (HasLUT()) // PALETTE COLOR is NOT enough
267 //beware of trailing space at end of string
268 if (PhotometricInterpretation.find(GDCM_UNFOUND) <
269 PhotometricInterpretation.length() ||
270 PhotometricInterpretation.find("MONOCHROME1") <
271 PhotometricInterpretation.length() ||
272 PhotometricInterpretation.find("MONOCHROME2") <
273 PhotometricInterpretation.length() )
276 // we assume that *all* kinds of YBR are dealt with
281 * \ingroup gdcmHeaderHelper
282 * \brief This function is intended to user that DOESN'T want
283 * \to get RGB pixels image when it's stored as a PALETTE COLOR image
284 * \ - the (vtk) user is supposed to know how deal with LUTs -
285 * \warning to be used with GetImagePixelsRaw()
286 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
288 int gdcmHeaderHelper::GetNumberOfScalarComponentsRaw() {
290 // 0028 0100 US IMG Bits Allocated
291 // (in order no to be messed up by old RGB images)
292 if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
295 // we assume that *all* kinds of YBR are dealt with
296 return GetSamplesPerPixel();
300 *\ingroup gdcmHeaderHelper
301 *\brief gets the info from 0020,000d : Study Instance UID
302 *\todo ? : return the ACR-NEMA element value if DICOM one is not found
303 * @return Study Instance UID
305 std::string gdcmHeaderHelper::GetStudyUID(){
306 return GetEntryByNumber(0x0020,0x000d); //0020 000d UI REL Study Instance UID
310 *\ingroup gdcmHeaderHelper
311 *\brief gets the info from 0020,000e : Series Instance UID
312 *\todo ? : return the ACR-NEMA element value if DICOM one is not found
313 * @return Series Instance UID
315 std::string gdcmHeaderHelper::GetSeriesUID(){
316 return GetEntryByNumber(0x0020,0x000e); //0020 000e UI REL Series Instance UID
320 *\ingroup gdcmHeaderHelper
321 *\brief gets the info from 0008,0016 : SOP Class UID
322 *\todo ? : return the ACR-NEMA element value if DICOM one is not found
323 * @return SOP Class UID
325 std::string gdcmHeaderHelper::GetClassUID(){
326 return GetEntryByNumber(0x0008,0x0016); //0008 0016 UI ID SOP Class UID
330 *\ingroup gdcmHeaderHelper
331 *\brief gets the info from 0008,0018 : SOP Instance UID
332 *\todo ? : return the ACR-NEMA element value if DICOM one is not found
333 * @return SOP Instance UID
335 std::string gdcmHeaderHelper::GetInstanceUID(){
336 return GetEntryByNumber(0x0008,0x0018); //0008 0018 UI ID SOP Instance UID
339 // -------------- Remember ! ----------------------------------
341 // Image Position Patient (0020,0032):
342 // If not found (ACR_NEMA) we try Image Position (0020,0030)
343 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
344 // or Location (0020,0050)
345 // as the Z coordinate,
346 // 0. for all the coordinates if nothing is found
348 // TODO : find a way to inform the caller nothing was found
349 // TODO : How to tell the caller a wrong number of values was found?
351 // ---------------------------------------------------------------
355 * \ingroup gdcmHeaderHelper
356 * \brief gets the info from 0020,0032 : Image Position Patient
357 * else from 0020,0030 : Image Position (RET)
359 * @return up-left image corner X position
362 float gdcmHeaderHelper::GetXOrigin() {
363 float xImPos, yImPos, zImPos;
364 std::string StrImPos = GetEntryByNumber(0x0020,0x0032);
366 if (StrImPos == GDCM_UNFOUND) {
367 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position Patient (0020,0032)");
368 StrImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
369 if (StrImPos == GDCM_UNFOUND) {
370 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position (RET) (0020,0030)");
371 // How to tell the caller nothing was found ?
375 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
381 * \ingroup gdcmHeaderHelper
382 * \brief gets the info from 0020,0032 : Image Position Patient
383 * else from 0020,0030 : Image Position (RET)
385 * @return up-left image corner Y position
387 float gdcmHeaderHelper::GetYOrigin() {
388 float xImPos, yImPos, zImPos;
389 std::string StrImPos = GetEntryByNumber(0x0020,0x0032);
391 if (StrImPos == GDCM_UNFOUND) {
392 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position Patient (0020,0032)");
393 StrImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
394 if (StrImPos == GDCM_UNFOUND) {
395 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position (RET) (0020,0030)");
396 // How to tell the caller nothing was found ?
400 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
406 * \ingroup gdcmHeaderHelper
407 * \brief gets the info from 0020,0032 : Image Position Patient
408 * \ else from 0020,0030 : Image Position (RET)
409 * \ else from 0020,1041 : Slice Location
410 * \ else from 0020,0050 : Location
412 * @return up-left image corner Z position
414 float gdcmHeaderHelper::GetZOrigin() {
415 float xImPos, yImPos, zImPos;
416 std::string StrImPos = GetEntryByNumber(0x0020,0x0032);
417 if (StrImPos != GDCM_UNFOUND) {
418 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
419 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position Patient (0020,0032)");
420 return 0.; // bug in the element 0x0020,0x0032
425 StrImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
426 if (StrImPos != GDCM_UNFOUND) {
427 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
428 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
429 return 0.; // bug in the element 0x0020,0x0032
434 std::string StrSliceLocation = GetEntryByNumber(0x0020,0x1041);// for *very* old ACR-NEMA images
435 if (StrSliceLocation != GDCM_UNFOUND) {
436 if( sscanf( StrSliceLocation.c_str(), "%f", &zImPos) !=1) {
437 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Slice Location (0020,1041)");
438 return 0.; // bug in the element 0x0020,0x1041
443 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: unfound Slice Location (0020,1041)");
444 std::string StrLocation = GetEntryByNumber(0x0020,0x0050);
445 if (StrLocation != GDCM_UNFOUND) {
446 if( sscanf( StrLocation.c_str(), "%f", &zImPos) !=1) {
447 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Location (0020,0050)");
448 return 0.; // bug in the element 0x0020,0x0050
453 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Location (0020,0050)");
454 return 0.; // Hopeless
458 * \ingroup gdcmHeaderHelper
459 * \brief gets the info from 0020,0013 : Image Number
461 * @return image number
463 int gdcmHeaderHelper::GetImageNumber() {
464 //The function i atoi() takes the address of an area of memory as parameter and converts
465 //the string stored at that location to an integer using the external decimal to internal
466 //binary conversion rules. This may be preferable to sscanf() since atoi() is a much smaller,
467 // simpler and faster function. sscanf() can do all possible conversions whereas atoi() can
468 //only do single decimal integer conversions.
469 std::string StrImNumber = GetEntryByNumber(0x0020,0x0013); //0020 0013 IS REL Image Number
470 if (StrImNumber != GDCM_UNFOUND) {
471 return atoi( StrImNumber.c_str() );
477 * \ingroup gdcmHeaderHelper
478 * \brief gets the info from 0008,0060 : Modality
479 * @return Modality Type
481 ModalityType gdcmHeaderHelper::GetModality(void) {
482 std::string StrModality = GetEntryByNumber(0x0008,0x0060); //0008 0060 CS ID Modality
483 if (StrModality != GDCM_UNFOUND) {
484 if ( StrModality.find("AU") < StrModality.length()) return AU;
485 else if ( StrModality.find("AS") < StrModality.length()) return AS;
486 else if ( StrModality.find("BI") < StrModality.length()) return BI;
487 else if ( StrModality.find("CF") < StrModality.length()) return CF;
488 else if ( StrModality.find("CP") < StrModality.length()) return CP;
489 else if ( StrModality.find("CR") < StrModality.length()) return CR;
490 else if ( StrModality.find("CT") < StrModality.length()) return CT;
491 else if ( StrModality.find("CS") < StrModality.length()) return CS;
492 else if ( StrModality.find("DD") < StrModality.length()) return DD;
493 else if ( StrModality.find("DF") < StrModality.length()) return DF;
494 else if ( StrModality.find("DG") < StrModality.length()) return DG;
495 else if ( StrModality.find("DM") < StrModality.length()) return DM;
496 else if ( StrModality.find("DS") < StrModality.length()) return DS;
497 else if ( StrModality.find("DX") < StrModality.length()) return DX;
498 else if ( StrModality.find("ECG") < StrModality.length()) return ECG;
499 else if ( StrModality.find("EPS") < StrModality.length()) return EPS;
500 else if ( StrModality.find("FA") < StrModality.length()) return FA;
501 else if ( StrModality.find("FS") < StrModality.length()) return FS;
502 else if ( StrModality.find("HC") < StrModality.length()) return HC;
503 else if ( StrModality.find("HD") < StrModality.length()) return HD;
504 else if ( StrModality.find("LP") < StrModality.length()) return LP;
505 else if ( StrModality.find("LS") < StrModality.length()) return LS;
506 else if ( StrModality.find("MA") < StrModality.length()) return MA;
507 else if ( StrModality.find("MR") < StrModality.length()) return MR;
508 else if ( StrModality.find("NM") < StrModality.length()) return NM;
509 else if ( StrModality.find("OT") < StrModality.length()) return OT;
510 else if ( StrModality.find("PT") < StrModality.length()) return PT;
511 else if ( StrModality.find("RF") < StrModality.length()) return RF;
512 else if ( StrModality.find("RG") < StrModality.length()) return RG;
513 else if ( StrModality.find("RTDOSE") < StrModality.length()) return RTDOSE;
514 else if ( StrModality.find("RTIMAGE") < StrModality.length()) return RTIMAGE;
515 else if ( StrModality.find("RTPLAN") < StrModality.length()) return RTPLAN;
516 else if ( StrModality.find("RTSTRUCT")< StrModality.length()) return RTSTRUCT;
517 else if ( StrModality.find("SM") < StrModality.length()) return SM;
518 else if ( StrModality.find("ST") < StrModality.length()) return ST;
519 else if ( StrModality.find("TG") < StrModality.length()) return TG;
520 else if ( StrModality.find("US") < StrModality.length()) return US;
521 else if ( StrModality.find("VF") < StrModality.length()) return VF;
522 else if ( StrModality.find("XA") < StrModality.length()) return XA;
523 else if ( StrModality.find("XC") < StrModality.length()) return XC;
527 //throw error return value ???
528 // specified <> unknow in our database
536 * \ingroup gdcmHeaderHelper
537 * \brief gets the info from 0020,0037 : Image Orientation Patient
538 * @param iop adress of the (6)float aray to receive values
539 * @return cosines of image orientation patient
541 void gdcmHeaderHelper::GetImageOrientationPatient( float* iop ) {
543 //iop is supposed to be float[6]
544 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0;
546 std::string StrImOriPat = GetEntryByNumber(0x0020,0x0037); // 0020 0037 DS REL Image Orientation (Patient)
547 if (StrImOriPat != GDCM_UNFOUND) {
548 if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
549 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
550 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0037)");
551 return ; // bug in the element 0x0020,0x0037
558 StrImOriPat = GetEntryByNumber(0x0020,0x0035); //0020 0035 DS REL Image Orientation (RET)
559 if (StrImOriPat != GDCM_UNFOUND) {
560 if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
561 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
562 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0035)");
563 return ; // bug in the element 0x0020,0x0035
570 //-----------------------------------------------------------------------------
573 //-----------------------------------------------------------------------------
576 //-----------------------------------------------------------------------------
580 //-----------------------------------------------------------------------------
581 // gdcmSerieHeaderHelper
582 //-----------------------------------------------------------------------------
583 // Constructor / Destructor
584 gdcmSerieHeaderHelper::~gdcmSerieHeaderHelper(){
586 for (std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
587 it != CoherentGdcmFileList.end(); it++)
591 CoherentGdcmFileList.clear();
594 //-----------------------------------------------------------------------------
597 //-----------------------------------------------------------------------------
600 * \ingroup gdcmHeaderHelper
601 * \brief add a gdcmFile to the list based on file name
602 * @param filename Name of the file to deal with
604 void gdcmSerieHeaderHelper::AddFileName(std::string filename) {
605 gdcmHeaderHelper *GdcmFile = new gdcmHeaderHelper( filename.c_str() );
606 this->CoherentGdcmFileList.push_back( GdcmFile );
610 * \ingroup gdcmHeaderHelper
611 * \brief add a gdcmFile to the list
612 * @param file gdcmHeaderHelper to add
614 void gdcmSerieHeaderHelper::AddGdcmFile(gdcmHeaderHelper *file){
615 this->CoherentGdcmFileList.push_back( file );
619 * \ingroup gdcmHeaderHelper
620 * \brief Sets the Directory
621 * @param dir Name of the directory to deal with
623 void gdcmSerieHeaderHelper::SetDirectory(std::string dir){
624 gdcmDirList filenames_list(dir); //OS specific
626 for(gdcmDirList::iterator it = filenames_list.begin();
627 it !=filenames_list.end(); it++)
629 gdcmHeaderHelper *file = new gdcmHeaderHelper( it->c_str() );
630 this->CoherentGdcmFileList.push_back( file );
635 * \ingroup gdcmHeaderHelper
636 * \brief Sorts the File List
637 * \warning This could be implemented in a 'Strategy Pattern' approach
638 * But as I don't know how to do it, I leave it this way
639 * BTW, this is also a Strategy, I don't know this is the best approach :)
641 void gdcmSerieHeaderHelper::OrderGdcmFileList(){
642 if( ImagePositionPatientOrdering() ) {
645 else if( ImageNumberOrdering() ) {
653 * \ingroup gdcmHeaderHelper
654 * \brief Gets the *coherent* File List
655 * @return the *coherent* File List
657 std::list<gdcmHeaderHelper*> &gdcmSerieHeaderHelper::GetGdcmFileList() {
658 return CoherentGdcmFileList;
661 //-----------------------------------------------------------------------------
664 //-----------------------------------------------------------------------------
667 * \ingroup gdcmHeaderHelper
668 * \brief sorts the images, according to their Patient Position
669 * We may order, considering :
671 * -# Image Position Patient
673 * @return false only if the header is bugged !
675 bool gdcmSerieHeaderHelper::ImagePositionPatientOrdering()
676 //based on Jolinda's algorithm
678 //iop is calculated based on the file file
679 float *cosines = new float[6];
686 std::vector<float> distlist;
688 //!\todo rewrite this for loop.
689 for (std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
690 it != CoherentGdcmFileList.end(); it++)
693 (*it)->GetImageOrientationPatient(cosines);
695 //You only have to do this once for all slices in the volume. Next, for
696 //each slice, calculate the distance along the slice normal using the IPP
697 //tag ("dist" is initialized to zero before reading the first slice) :
698 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
699 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
700 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
702 ipp[0] = (*it)->GetXOrigin();
703 ipp[1] = (*it)->GetYOrigin();
704 ipp[2] = (*it)->GetZOrigin();
707 for (int i = 0; i < 3; ++i)
708 dist += normal[i]*ipp[i];
716 distlist.push_back( dist );
722 ipp[0] = (*it)->GetXOrigin();
723 ipp[1] = (*it)->GetYOrigin();
724 ipp[2] = (*it)->GetZOrigin();
727 for (int i = 0; i < 3; ++i)
728 dist += normal[i]*ipp[i];
736 distlist.push_back( dist );
738 min = (min < dist) ? min : dist;
739 max = (max > dist) ? max : dist;
744 //Then I order the slices according to the value "dist". Finally, once
745 //I've read in all the slices, I calculate the z-spacing as the difference
746 //between the "dist" values for the first two slices.
747 std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
748 //CoherentGdcmFileVector.reserve( n );
749 CoherentGdcmFileVector.resize( n );
750 //assert( CoherentGdcmFileVector.capacity() >= n );
752 float step = (max - min)/(n - 1);
756 //VC++ don't understand what scope is !! it -> it2
757 for (std::list<gdcmHeaderHelper*>::iterator it2 = CoherentGdcmFileList.begin();
758 it2 != CoherentGdcmFileList.end(); it2++, n++)
761 //Assumption: all files are present (no one missing)
762 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
764 CoherentGdcmFileVector[pos] = *it2;
767 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
769 //VC++ don't understand what scope is !! it -> it3
770 for (std::vector<gdcmHeaderHelper*>::iterator it3 = CoherentGdcmFileVector.begin();
771 it3 != CoherentGdcmFileVector.end(); it3++)
773 CoherentGdcmFileList.push_back( *it3 );
777 CoherentGdcmFileVector.clear();
784 * \ingroup gdcmHeaderHelper
785 * \brief sorts the images, according to their Image Number
786 * @return false only if the header is bugged !
789 bool gdcmSerieHeaderHelper::ImageNumberOrdering() {
791 int n = 0;//CoherentGdcmFileList.size(); //O(N) operation !!
792 unsigned char *partition;
794 std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
795 min = max = (*it)->GetImageNumber();
797 for (; it != CoherentGdcmFileList.end(); it++, n++)
799 pos = (*it)->GetImageNumber();
802 min = (min < pos) ? min : pos;
805 //bzeros(partition, n); //Cette fonction est déconseillée, utilisez plutôt memset.
806 partition = new unsigned char[n];
807 memset(partition, 0, n);
809 std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
811 //VC++ don't understand what scope is !! it -> it2
812 for (std::list<gdcmHeaderHelper*>::iterator it2 = CoherentGdcmFileList.begin();
813 it2 != CoherentGdcmFileList.end(); it2++)
815 pos = (*it2)->GetImageNumber();
816 CoherentGdcmFileVector[pos - min] = *it2;
817 partition[pos - min]++;
820 unsigned char mult = 1;
821 for(int i=0; i<n ; i++)
823 mult *= partition[i];
826 //VC++ don't understand what scope is !! it -> it3
827 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
828 for (std::vector<gdcmHeaderHelper*>::iterator it3 = CoherentGdcmFileVector.begin();
829 it3 != CoherentGdcmFileVector.end(); it3++)
831 CoherentGdcmFileList.push_back( *it3 );
833 CoherentGdcmFileVector.clear();
841 * \ingroup gdcmHeaderHelper
842 * \brief sorts the images, according to their File Name
843 * @return false only if the header is bugged !
845 bool gdcmSerieHeaderHelper::FileNameOrdering() {
847 //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
851 //-----------------------------------------------------------------------------