1 /*=========================================================================
4 Module: $RCSfile: gdcmHeaderHelper.cxx,v $
6 Date: $Date: 2004/06/20 18:08:48 $
7 Version: $Revision: 1.37 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.htm for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #include "gdcmHeaderHelper.h"
20 #include "gdcmDirList.h"
22 #include "gdcmDebug.h"
27 //-----------------------------------------------------------------------------
31 gdcmHeaderHelper::gdcmHeaderHelper() : gdcmHeader( ) {
37 * @param InFilename Name of the file to deal with
38 * @param exception_on_error
39 * @param enable_sequences = true to allow the header
40 * to be parsed *inside* the SeQuences,
41 * when they have an actual length
42 * @param ignore_shadow = true if user wants to skip shadow groups
43 * during parsing, to save memory space
45 gdcmHeaderHelper::gdcmHeaderHelper(const char *InFilename,
46 bool exception_on_error,
47 bool enable_sequences,
49 : gdcmHeader( InFilename,
56 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
62 * \brief Returns the size (in bytes) of a single pixel of data.
63 * @return The size in bytes of a single pixel of data.
66 int gdcmHeaderHelper::GetPixelSize() {
68 // 0028 0100 US IMG Bits Allocated
69 // (in order no to be messed up by old RGB images)
70 if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
73 std::string PixelType = GetPixelType();
74 if (PixelType == "8U" || PixelType == "8S")
76 if (PixelType == "16U" || PixelType == "16S")
78 if (PixelType == "32U" || PixelType == "32S")
80 if (PixelType == "FD") // to help unfortunate users to manage DOUBLE
82 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
87 * \ingroup gdcmHeaderHelper
88 * \brief Build the Pixel Type of the image.
89 * Possible values are:
90 * - 8U unsigned 8 bit,
92 * - 16U unsigned 16 bit,
93 * - 16S signed 16 bit,
94 * - 32U unsigned 32 bit,
95 * - 32S signed 32 bit,
97 * \warning 12 bit images appear as 16 bit.
98 * 24 bit images appear as 8 bit
99 * 64 bit means 'DOUBLE' images
100 * (no DOUBLE images in kosher DICOM,
101 * but so usefull for people that miss them ;-)
104 std::string gdcmHeaderHelper::GetPixelType() {
105 std::string BitsAlloc;
106 BitsAlloc = GetEntryByNumber(0x0028, 0x0100);
107 if (BitsAlloc == GDCM_UNFOUND) { // Bits Allocated
108 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
109 BitsAlloc = std::string("16");
111 if (BitsAlloc == "12") // It will be unpacked
112 BitsAlloc = std::string("16");
113 else if (BitsAlloc == "24") // (in order no to be messed up
114 BitsAlloc = std::string("8"); // by old RGB images)
117 Signed = GetEntryByNumber(0x0028, 0x0103);
118 if (Signed == GDCM_UNFOUND) { // "Pixel Representation"
119 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
120 BitsAlloc = std::string("0");
122 if (BitsAlloc == "64") // to help users that want to deal with DOUBLE
126 Signed = std::string("U");
128 Signed = std::string("S");
130 return( BitsAlloc + Signed);
134 * \ingroup gdcmHeaderHelper
135 * \brief gets the info from 0028,0030 : Pixel Spacing
137 * @return X dimension of a pixel
139 float gdcmHeaderHelper::GetXSpacing() {
140 float xspacing, yspacing;
141 std::string StrSpacing = GetEntryByNumber(0x0028,0x0030);
143 if (StrSpacing == GDCM_UNFOUND) {
144 dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
148 if( (nbValues = sscanf( StrSpacing.c_str(), "%f\\%f", &yspacing, &xspacing)) != 2) {
149 if (nbValues==1) // if single value is found, xspacing is defaulted to yspacing
152 if (xspacing == 0.) {
153 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
154 // seems to be a bug in the header ...
155 sscanf( StrSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
161 * \ingroup gdcmHeaderHelper
162 * \brief gets the info from 0028,0030 : Pixel Spacing
164 * @return Y dimension of a pixel
166 float gdcmHeaderHelper::GetYSpacing() {
168 std::string StrSpacing = GetEntryByNumber(0x0028,0x0030);
170 if (StrSpacing == GDCM_UNFOUND) {
171 dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
174 sscanf( StrSpacing.c_str(), "%f", &yspacing);
179 *\ingroup gdcmHeaderHelper
180 *\brief gets the info from 0018,0088 : Space Between Slices
181 * else from 0018,0050 : Slice Thickness
183 * @return Z dimension of a voxel-to be
185 float gdcmHeaderHelper::GetZSpacing() {
186 // Spacing Between Slices : distance entre le milieu de chaque coupe
187 // Les coupes peuvent etre :
188 // jointives (Spacing between Slices = Slice Thickness)
189 // chevauchantes (Spacing between Slices < Slice Thickness)
190 // disjointes (Spacing between Slices > Slice Thickness)
191 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
192 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
193 // Si le Spacing Between Slices est absent,
194 // on suppose que les coupes sont jointives
196 std::string StrSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
198 if (StrSpacingBSlices == GDCM_UNFOUND) {
199 dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
200 std::string StrSliceThickness = GetEntryByNumber(0x0018,0x0050);
201 if (StrSliceThickness == GDCM_UNFOUND)
204 // if no 'Spacing Between Slices' is found,
205 // we assume slices join together
206 // (no overlapping, no interslice gap)
207 // if they don't, we're fucked up
208 return atof(StrSliceThickness.c_str());
210 return atof(StrSpacingBSlices.c_str());
215 *\ingroup gdcmHeaderHelper
216 *\brief gets the info from 0028,1052 : Rescale Intercept
217 * @return Rescale Intercept
219 float gdcmHeaderHelper::GetRescaleIntercept() {
221 std::string StrRescInter = GetEntryByNumber(0x0028,0x1052); //0028 1052 DS IMG Rescale Intercept
222 if (StrRescInter != GDCM_UNFOUND) {
223 if( sscanf( StrRescInter.c_str(), "%f", &resInter) != 1) {
224 dbg.Verbose(0, "gdcmHeader::GetRescaleIntercept: Rescale Slope is empty");
225 // bug in the element 0x0028,0x1052
232 *\ingroup gdcmHeaderHelper
233 *\brief gets the info from 0028,1053 : Rescale Slope
234 * @return Rescale Slope
236 float gdcmHeaderHelper::GetRescaleSlope() {
238 std::string StrRescSlope = GetEntryByNumber(0x0028,0x1053); //0028 1053 DS IMG Rescale Slope
239 if (StrRescSlope != GDCM_UNFOUND) {
240 if( sscanf( StrRescSlope.c_str(), "%f", &resSlope) != 1) {
241 dbg.Verbose(0, "gdcmHeader::GetRescaleSlope: Rescale Slope is empty");
242 // bug in the element 0x0028,0x1053
249 * \ingroup gdcmHeaderHelper
250 * \brief This function is intended to user who doesn't want
251 * to have to manage a LUT and expects to get an RBG Pixel image
252 * (or a monochrome one ...)
253 * \warning to be used with GetImagePixels()
254 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
256 int gdcmHeaderHelper::GetNumberOfScalarComponents() {
257 if (GetSamplesPerPixel() ==3)
260 // 0028 0100 US IMG Bits Allocated
261 // (in order no to be messed up by old RGB images)
262 if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
265 std::string PhotometricInterpretation =
266 gdcmHeader::GetEntryByNumber(0x0028,0x0004);
268 if ( ( PhotometricInterpretation == "PALETTE COLOR ") ) {
269 if (HasLUT()) // PALETTE COLOR is NOT enough
275 //beware of trailing space at end of string
276 if (PhotometricInterpretation.find(GDCM_UNFOUND) <
277 PhotometricInterpretation.length() ||
278 PhotometricInterpretation.find("MONOCHROME1") <
279 PhotometricInterpretation.length() ||
280 PhotometricInterpretation.find("MONOCHROME2") <
281 PhotometricInterpretation.length() )
284 // we assume that *all* kinds of YBR are dealt with
289 * \ingroup gdcmHeaderHelper
290 * \brief This function is intended to user that DOESN'T want
291 * to get RGB pixels image when it's stored as a PALETTE COLOR image
292 * - the (vtk) user is supposed to know how deal with LUTs -
293 * \warning to be used with GetImagePixelsRaw()
294 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
296 int gdcmHeaderHelper::GetNumberOfScalarComponentsRaw() {
298 // 0028 0100 US IMG Bits Allocated
299 // (in order no to be messed up by old RGB images)
300 if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
303 // we assume that *all* kinds of YBR are dealt with
304 return GetSamplesPerPixel();
308 *\ingroup gdcmHeaderHelper
309 *\brief gets the info from 0020,000d : Study Instance UID
310 *\todo ? : return the ACR-NEMA element value if DICOM one is not found
311 * @return Study Instance UID
313 std::string gdcmHeaderHelper::GetStudyUID(){
314 return GetEntryByNumber(0x0020,0x000d); //0020 000d UI REL Study Instance UID
318 *\ingroup gdcmHeaderHelper
319 *\brief gets the info from 0020,000e : Series Instance UID
320 *\todo ? : return the ACR-NEMA element value if DICOM one is not found
321 * @return Series Instance UID
323 std::string gdcmHeaderHelper::GetSeriesUID(){
324 return GetEntryByNumber(0x0020,0x000e); //0020 000e UI REL Series Instance UID
328 *\ingroup gdcmHeaderHelper
329 *\brief gets the info from 0008,0016 : SOP Class UID
330 *\todo ? : return the ACR-NEMA element value if DICOM one is not found
331 * @return SOP Class UID
333 std::string gdcmHeaderHelper::GetClassUID(){
334 return GetEntryByNumber(0x0008,0x0016); //0008 0016 UI ID SOP Class UID
338 *\brief gets the info from 0008,0018 : SOP Instance UID
339 *\todo ? : return the ACR-NEMA element value if DICOM one is not found
340 * @return SOP Instance UID
342 std::string gdcmHeaderHelper::GetInstanceUID(){
343 return GetEntryByNumber(0x0008,0x0018); //0008 0018 UI ID SOP Instance UID
346 // -------------- Remember ! ----------------------------------
348 // Image Position Patient (0020,0032):
349 // If not found (ACR_NEMA) we try Image Position (0020,0030)
350 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
351 // or Location (0020,0050)
352 // as the Z coordinate,
353 // 0. for all the coordinates if nothing is found
355 // \todo find a way to inform the caller nothing was found
356 // \todo How to tell the caller a wrong number of values was found?
358 // ---------------------------------------------------------------
362 * \brief gets the info from 0020,0032 : Image Position Patient
363 * else from 0020,0030 : Image Position (RET)
365 * @return up-left image corner X position
368 float gdcmHeaderHelper::GetXOrigin() {
369 float xImPos, yImPos, zImPos;
370 std::string StrImPos = GetEntryByNumber(0x0020,0x0032);
372 if (StrImPos == GDCM_UNFOUND) {
373 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position Patient (0020,0032)");
374 StrImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
375 if (StrImPos == GDCM_UNFOUND) {
376 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position (RET) (0020,0030)");
377 /// \todo How to tell the caller nothing was found ?
381 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
387 * \brief gets the info from 0020,0032 : Image Position Patient
388 * else from 0020,0030 : Image Position (RET)
390 * @return up-left image corner Y position
392 float gdcmHeaderHelper::GetYOrigin() {
393 float xImPos, yImPos, zImPos;
394 std::string StrImPos = GetEntryByNumber(0x0020,0x0032);
396 if (StrImPos == GDCM_UNFOUND) {
397 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position Patient (0020,0032)");
398 StrImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
399 if (StrImPos == GDCM_UNFOUND) {
400 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position (RET) (0020,0030)");
401 /// \todo How to tell the caller nothing was found ?
405 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
411 * \brief gets the info from 0020,0032 : Image Position Patient
412 * \ else from 0020,0030 : Image Position (RET)
413 * \ else from 0020,1041 : Slice Location
414 * \ else from 0020,0050 : Location
416 * @return up-left image corner Z position
418 float gdcmHeaderHelper::GetZOrigin() {
419 float xImPos, yImPos, zImPos;
420 std::string StrImPos = GetEntryByNumber(0x0020,0x0032);
421 if (StrImPos != GDCM_UNFOUND) {
422 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
423 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position Patient (0020,0032)");
424 return 0.; // bug in the element 0x0020,0x0032
429 StrImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
430 if (StrImPos != GDCM_UNFOUND) {
431 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
432 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
433 return 0.; // bug in the element 0x0020,0x0032
438 std::string StrSliceLocation = GetEntryByNumber(0x0020,0x1041);// for *very* old ACR-NEMA images
439 if (StrSliceLocation != GDCM_UNFOUND) {
440 if( sscanf( StrSliceLocation.c_str(), "%f", &zImPos) !=1) {
441 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Slice Location (0020,1041)");
442 return 0.; // bug in the element 0x0020,0x1041
447 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: unfound Slice Location (0020,1041)");
448 std::string StrLocation = GetEntryByNumber(0x0020,0x0050);
449 if (StrLocation != GDCM_UNFOUND) {
450 if( sscanf( StrLocation.c_str(), "%f", &zImPos) !=1) {
451 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Location (0020,0050)");
452 return 0.; // bug in the element 0x0020,0x0050
457 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Location (0020,0050)");
458 return 0.; // Hopeless
462 * \brief gets the info from 0020,0013 : Image Number
464 * @return image number
466 int gdcmHeaderHelper::GetImageNumber() {
467 // The function i atoi() takes the address of an area of memory as
468 // parameter and converts the string stored at that location to an integer
469 // using the external decimal to internal binary conversion rules. This may
470 // be preferable to sscanf() since atoi() is a much smaller, simpler and
471 // faster function. sscanf() can do all possible conversions whereas
472 // atoi() can only do single decimal integer conversions.
473 std::string StrImNumber = GetEntryByNumber(0x0020,0x0013); //0020 0013 IS REL Image Number
474 if (StrImNumber != GDCM_UNFOUND) {
475 return atoi( StrImNumber.c_str() );
481 * \brief gets the info from 0008,0060 : Modality
482 * @return Modality Type
484 ModalityType gdcmHeaderHelper::GetModality(void) {
485 // 0008 0060 CS ID Modality
486 std::string StrModality = GetEntryByNumber(0x0008,0x0060);
487 if (StrModality != GDCM_UNFOUND) {
488 if ( StrModality.find("AU") < StrModality.length()) return AU;
489 else if ( StrModality.find("AS") < StrModality.length()) return AS;
490 else if ( StrModality.find("BI") < StrModality.length()) return BI;
491 else if ( StrModality.find("CF") < StrModality.length()) return CF;
492 else if ( StrModality.find("CP") < StrModality.length()) return CP;
493 else if ( StrModality.find("CR") < StrModality.length()) return CR;
494 else if ( StrModality.find("CT") < StrModality.length()) return CT;
495 else if ( StrModality.find("CS") < StrModality.length()) return CS;
496 else if ( StrModality.find("DD") < StrModality.length()) return DD;
497 else if ( StrModality.find("DF") < StrModality.length()) return DF;
498 else if ( StrModality.find("DG") < StrModality.length()) return DG;
499 else if ( StrModality.find("DM") < StrModality.length()) return DM;
500 else if ( StrModality.find("DS") < StrModality.length()) return DS;
501 else if ( StrModality.find("DX") < StrModality.length()) return DX;
502 else if ( StrModality.find("ECG") < StrModality.length()) return ECG;
503 else if ( StrModality.find("EPS") < StrModality.length()) return EPS;
504 else if ( StrModality.find("FA") < StrModality.length()) return FA;
505 else if ( StrModality.find("FS") < StrModality.length()) return FS;
506 else if ( StrModality.find("HC") < StrModality.length()) return HC;
507 else if ( StrModality.find("HD") < StrModality.length()) return HD;
508 else if ( StrModality.find("LP") < StrModality.length()) return LP;
509 else if ( StrModality.find("LS") < StrModality.length()) return LS;
510 else if ( StrModality.find("MA") < StrModality.length()) return MA;
511 else if ( StrModality.find("MR") < StrModality.length()) return MR;
512 else if ( StrModality.find("NM") < StrModality.length()) return NM;
513 else if ( StrModality.find("OT") < StrModality.length()) return OT;
514 else if ( StrModality.find("PT") < StrModality.length()) return PT;
515 else if ( StrModality.find("RF") < StrModality.length()) return RF;
516 else if ( StrModality.find("RG") < StrModality.length()) return RG;
517 else if ( StrModality.find("RTDOSE") < StrModality.length()) return RTDOSE;
518 else if ( StrModality.find("RTIMAGE") < StrModality.length()) return RTIMAGE;
519 else if ( StrModality.find("RTPLAN") < StrModality.length()) return RTPLAN;
520 else if ( StrModality.find("RTSTRUCT")< StrModality.length()) return RTSTRUCT;
521 else if ( StrModality.find("SM") < StrModality.length()) return SM;
522 else if ( StrModality.find("ST") < StrModality.length()) return ST;
523 else if ( StrModality.find("TG") < StrModality.length()) return TG;
524 else if ( StrModality.find("US") < StrModality.length()) return US;
525 else if ( StrModality.find("VF") < StrModality.length()) return VF;
526 else if ( StrModality.find("XA") < StrModality.length()) return XA;
527 else if ( StrModality.find("XC") < StrModality.length()) return XC;
531 /// \todo throw error return value ???
532 /// specified <> unknow in our database
540 * \brief gets the info from 0020,0037 : Image Orientation Patient
541 * @param iop adress of the (6)float aray to receive values
542 * @return cosines of image orientation patient
544 void gdcmHeaderHelper::GetImageOrientationPatient( float* iop ) {
546 //iop is supposed to be float[6]
547 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0;
549 // 0020 0037 DS REL Image Orientation (Patient)
550 std::string StrImOriPat = GetEntryByNumber(0x0020,0x0037);
551 if (StrImOriPat != GDCM_UNFOUND) {
552 if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
553 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
554 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0037)");
555 return ; // bug in the element 0x0020,0x0037
562 // 0020 0035 DS REL Image Orientation (RET)
563 StrImOriPat = GetEntryByNumber(0x0020,0x0035);
564 if (StrImOriPat != GDCM_UNFOUND) {
565 if( sscanf( StrImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
566 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6) {
567 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0035)");
568 return ; // bug in the element 0x0020,0x0035
575 //-----------------------------------------------------------------------------
578 //-----------------------------------------------------------------------------
581 //-----------------------------------------------------------------------------
585 //-----------------------------------------------------------------------------
586 // Constructor / Destructor
587 gdcmSerieHeaderHelper::~gdcmSerieHeaderHelper(){
589 for (std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
590 it != CoherentGdcmFileList.end(); it++)
594 CoherentGdcmFileList.clear();
597 //-----------------------------------------------------------------------------
600 //-----------------------------------------------------------------------------
603 * \brief add a gdcmFile to the list based on file name
604 * @param filename Name of the file to deal with
606 void gdcmSerieHeaderHelper::AddFileName(std::string filename) {
607 gdcmHeaderHelper *GdcmFile = new gdcmHeaderHelper( filename.c_str() );
608 this->CoherentGdcmFileList.push_back( GdcmFile );
612 * \brief add a gdcmFile to the list
613 * @param file gdcmHeaderHelper to add
615 void gdcmSerieHeaderHelper::AddGdcmFile(gdcmHeaderHelper *file){
616 this->CoherentGdcmFileList.push_back( file );
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 * \brief Sorts the File List
636 * \warning This could be implemented in a 'Strategy Pattern' approach
637 * But as I don't know how to do it, I leave it this way
638 * BTW, this is also a Strategy, I don't know this is the best approach :)
640 void gdcmSerieHeaderHelper::OrderGdcmFileList(){
641 if( ImagePositionPatientOrdering() ) {
644 else if( ImageNumberOrdering() ) {
652 * \brief Gets the *coherent* File List
653 * @return the *coherent* File List
655 std::list<gdcmHeaderHelper*> &gdcmSerieHeaderHelper::GetGdcmFileList() {
656 return CoherentGdcmFileList;
659 //-----------------------------------------------------------------------------
662 //-----------------------------------------------------------------------------
665 * \ingroup gdcmHeaderHelper
666 * \brief sorts the images, according to their Patient Position
667 * We may order, considering :
669 * -# Image Position Patient
671 * @return false only if the header is bugged !
673 bool gdcmSerieHeaderHelper::ImagePositionPatientOrdering()
674 //based on Jolinda's algorithm
676 //iop is calculated based on the file file
677 float *cosines = new float[6];
684 std::vector<float> distlist;
686 //!\todo rewrite this for loop.
687 for (std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
688 it != CoherentGdcmFileList.end(); it++)
691 (*it)->GetImageOrientationPatient(cosines);
693 //You only have to do this once for all slices in the volume. Next, for
694 //each slice, calculate the distance along the slice normal using the IPP
695 //tag ("dist" is initialized to zero before reading the first slice) :
696 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
697 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
698 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
700 ipp[0] = (*it)->GetXOrigin();
701 ipp[1] = (*it)->GetYOrigin();
702 ipp[2] = (*it)->GetZOrigin();
705 for (int i = 0; i < 3; ++i)
706 dist += normal[i]*ipp[i];
714 distlist.push_back( dist );
720 ipp[0] = (*it)->GetXOrigin();
721 ipp[1] = (*it)->GetYOrigin();
722 ipp[2] = (*it)->GetZOrigin();
725 for (int i = 0; i < 3; ++i)
726 dist += normal[i]*ipp[i];
734 distlist.push_back( dist );
736 min = (min < dist) ? min : dist;
737 max = (max > dist) ? max : dist;
742 //Then I order the slices according to the value "dist". Finally, once
743 //I've read in all the slices, I calculate the z-spacing as the difference
744 //between the "dist" values for the first two slices.
745 std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
746 //CoherentGdcmFileVector.reserve( n );
747 CoherentGdcmFileVector.resize( n );
748 //assert( CoherentGdcmFileVector.capacity() >= n );
750 float step = (max - min)/(n - 1);
754 //VC++ don't understand what scope is !! it -> it2
755 for (std::list<gdcmHeaderHelper*>::iterator it2 = CoherentGdcmFileList.begin();
756 it2 != CoherentGdcmFileList.end(); it2++, n++)
759 //Assumption: all files are present (no one missing)
760 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
762 CoherentGdcmFileVector[pos] = *it2;
765 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
767 //VC++ don't understand what scope is !! it -> it3
768 for (std::vector<gdcmHeaderHelper*>::iterator it3 = CoherentGdcmFileVector.begin();
769 it3 != CoherentGdcmFileVector.end(); it3++)
771 CoherentGdcmFileList.push_back( *it3 );
775 CoherentGdcmFileVector.clear();
782 * \ingroup gdcmHeaderHelper
783 * \brief sorts the images, according to their Image Number
784 * @return false only if the header is bugged !
787 bool gdcmSerieHeaderHelper::ImageNumberOrdering() {
789 int n = 0;//CoherentGdcmFileList.size() is a O(N) operation !!
790 unsigned char *partition;
792 std::list<gdcmHeaderHelper*>::iterator it = CoherentGdcmFileList.begin();
793 min = max = (*it)->GetImageNumber();
795 for (; it != CoherentGdcmFileList.end(); it++, n++)
797 pos = (*it)->GetImageNumber();
800 min = (min < pos) ? min : pos;
803 //bzeros(partition, n); //Cette fonction est déconseillée, utilisez plutôt memset.
804 partition = new unsigned char[n];
805 memset(partition, 0, n);
807 std::vector<gdcmHeaderHelper*> CoherentGdcmFileVector(n);
809 //VC++ don't understand what scope is !! it -> it2
810 for (std::list<gdcmHeaderHelper*>::iterator it2 = CoherentGdcmFileList.begin();
811 it2 != CoherentGdcmFileList.end(); it2++)
813 pos = (*it2)->GetImageNumber();
814 CoherentGdcmFileVector[pos - min] = *it2;
815 partition[pos - min]++;
818 unsigned char mult = 1;
819 for(int i=0; i<n ; i++)
821 mult *= partition[i];
824 //VC++ don't understand what scope is !! it -> it3
825 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
826 for (std::vector<gdcmHeaderHelper*>::iterator it3 = CoherentGdcmFileVector.begin();
827 it3 != CoherentGdcmFileVector.end(); it3++)
829 CoherentGdcmFileList.push_back( *it3 );
831 CoherentGdcmFileVector.clear();
839 * \ingroup gdcmHeaderHelper
840 * \brief sorts the images, according to their File Name
841 * @return false only if the header is bugged !
843 bool gdcmSerieHeaderHelper::FileNameOrdering() {
845 //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
849 //-----------------------------------------------------------------------------