1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/10/12 04:35:46 $
7 Version: $Revision: 1.193 $
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.html 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 "gdcmHeader.h"
20 #include "gdcmGlobal.h"
22 #include "gdcmDebug.h"
24 #include "gdcmValEntry.h"
31 //-----------------------------------------------------------------------------
32 // Constructor / Destructor
35 * @param filename name of the file whose header we want to analyze
37 Header::Header( std::string const & filename ):
40 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
41 // We may encounter the 'RETired' (0x0028, 0x0200) tag
42 // (Image Location") . This Element contains the number of
43 // the group that contains the pixel data (hence the "Pixel Data"
44 // is found by indirection through the "Image Location").
45 // Inside the group pointed by "Image Location" the searched element
46 // is conventionally the element 0x0010 (when the norm is respected).
47 // When the "Image Location" is absent we default to group 0x7fe0.
48 // Note: this IS the right place for the code
51 std::string imgLocation = GetEntryByNumber(0x0028, 0x0200);
52 if ( imgLocation == GDCM_UNFOUND )
59 GrPixel = (uint16_t) atoi( imgLocation.c_str() );
62 // sometimes Image Location value doesn't follow
63 // the supposed processor endianity.
64 // see gdcmData/cr172241.dcm
65 if ( GrPixel == 0xe07f )
70 if ( GrPixel != 0x7fe0 )
72 // This is a kludge for old dirty Philips imager.
90 * \brief Canonical destructor.
98 * \brief Performs some consistency checking on various 'File related'
99 * (as opposed to 'DicomDir related') entries
100 * then writes in a file all the (Dicom Elements) included the Pixels
101 * @param fp file pointer on an already open file
102 * @param filetype Type of the File to be written
103 * (ACR-NEMA, ExplicitVR, ImplicitVR)
105 void Header::Write(FILE* fp,FileType filetype)
108 if ( GetEntryByNumber(0x0028,0x0100) == "12")
110 SetEntryByNumber("16", 0x0028,0x0100);
113 /// \todo correct 'Pixel group' Length if necessary
115 int i_lgPix = GetEntryLengthByNumber(GrPixel, NumPixel);
118 // no (GrPixel, NumPixel) element
120 s_lgPix = Util::Format("%d", i_lgPix+12);
121 ReplaceOrCreateByNumber(s_lgPix,GrPixel, 0x0000);
124 // FIXME : should be nice if we could move it to File
125 // (or in future gdcmPixelData class)
127 // Drop Palette Color, if necessary
129 if ( GetEntryByNumber(0x0028,0x0002).c_str()[0] == '3' )
131 // if SamplesPerPixel = 3, sure we don't need any LUT !
132 // Drop 0028|1101, 0028|1102, 0028|1103
133 // Drop 0028|1201, 0028|1202, 0028|1203
135 DocEntry* e = GetDocEntryByNumber(0x0028,0x01101);
138 RemoveEntryNoDestroy(e);
140 e = GetDocEntryByNumber(0x0028,0x1102);
143 RemoveEntryNoDestroy(e);
145 e = GetDocEntryByNumber(0x0028,0x1103);
148 RemoveEntryNoDestroy(e);
150 e = GetDocEntryByNumber(0x0028,0x01201);
153 RemoveEntryNoDestroy(e);
155 e = GetDocEntryByNumber(0x0028,0x1202);
158 RemoveEntryNoDestroy(e);
160 e = GetDocEntryByNumber(0x0028,0x1203);
163 RemoveEntryNoDestroy(e);
166 Document::Write(fp,filetype);
169 //-----------------------------------------------------------------------------
173 //-----------------------------------------------------------------------------
177 * \brief This predicate, based on hopefully reasonable heuristics,
178 * decides whether or not the current Header was properly parsed
179 * and contains the mandatory information for being considered as
180 * a well formed and usable Dicom/Acr File.
181 * @return true when Header is the one of a reasonable Dicom/Acr file,
184 bool Header::IsReadable()
186 if( !Document::IsReadable() )
191 std::string res = GetEntryByNumber(0x0028, 0x0005);
192 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
194 return false; // Image Dimensions
196 if ( !GetDocEntryByNumber(0x0028, 0x0100) )
198 return false; // "Bits Allocated"
200 if ( !GetDocEntryByNumber(0x0028, 0x0101) )
202 return false; // "Bits Stored"
204 if ( !GetDocEntryByNumber(0x0028, 0x0102) )
206 return false; // "High Bit"
208 if ( !GetDocEntryByNumber(0x0028, 0x0103) )
210 return false; // "Pixel Representation" i.e. 'Sign'
217 * \brief Retrieve the number of columns of image.
218 * @return The encountered size when found, 0 by default.
219 * 0 means the file is NOT USABLE. The caller will have to check
221 int Header::GetXSize()
224 strSize = GetEntryByNumber(0x0028,0x0011);
225 if ( strSize == GDCM_UNFOUND )
230 return atoi( strSize.c_str() );
234 * \brief Retrieve the number of lines of image.
235 * \warning The defaulted value is 1 as opposed to Header::GetXSize()
236 * @return The encountered size when found, 1 by default
237 * (The ACR-NEMA file contains a Signal, not an Image).
239 int Header::GetYSize()
241 std::string strSize = GetEntryByNumber(0x0028,0x0010);
242 if ( strSize != GDCM_UNFOUND )
244 return atoi( strSize.c_str() );
251 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
252 // hence be a signal (1d image). So we default to 1:
257 * \brief Retrieve the number of planes of volume or the number
258 * of frames of a multiframe.
259 * \warning When present we consider the "Number of Frames" as the third
260 * dimension. When absent we consider the third dimension as
261 * being the ACR-NEMA "Planes" tag content.
262 * @return The encountered size when found, 1 by default (single image).
264 int Header::GetZSize()
266 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
267 // as the third dimension.
268 std::string strSize = GetEntryByNumber(0x0028,0x0008);
269 if ( strSize != GDCM_UNFOUND )
271 return atoi( strSize.c_str() );
274 // We then consider the "Planes" entry as the third dimension
275 strSize = GetEntryByNumber(0x0028,0x0012);
276 if ( strSize != GDCM_UNFOUND )
278 return atoi( strSize.c_str() );
285 * \brief gets the info from 0028,0030 : Pixel Spacing
287 * @return X dimension of a pixel
289 float Header::GetXSpacing()
291 float xspacing, yspacing;
292 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
294 if ( strSpacing == GDCM_UNFOUND )
296 dbg.Verbose(0, "Header::GetXSpacing: unfound Pixel Spacing (0028,0030)");
301 if( ( nbValues = sscanf( strSpacing.c_str(),
302 "%f\\%f", &yspacing, &xspacing)) != 2 )
304 // if single value is found, xspacing is defaulted to yspacing
312 dbg.Verbose(0, "Header::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
313 // seems to be a bug in the header ...
314 sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
321 * \brief gets the info from 0028,0030 : Pixel Spacing
323 * @return Y dimension of a pixel
325 float Header::GetYSpacing()
328 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
330 if ( strSpacing == GDCM_UNFOUND )
332 dbg.Verbose(0, "Header::GetYSpacing: unfound Pixel Spacing (0028,0030)");
336 // if sscanf cannot read any float value, it won't affect yspacing
337 sscanf( strSpacing.c_str(), "%f", &yspacing);
343 * \brief gets the info from 0018,0088 : Space Between Slices
344 * else from 0018,0050 : Slice Thickness
346 * @return Z dimension of a voxel-to be
348 float Header::GetZSpacing()
350 // Spacing Between Slices : distance entre le milieu de chaque coupe
351 // Les coupes peuvent etre :
352 // jointives (Spacing between Slices = Slice Thickness)
353 // chevauchantes (Spacing between Slices < Slice Thickness)
354 // disjointes (Spacing between Slices > Slice Thickness)
355 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
356 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
357 // Si le Spacing Between Slices est absent,
358 // on suppose que les coupes sont jointives
360 std::string strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
362 if ( strSpacingBSlices == GDCM_UNFOUND )
364 dbg.Verbose(0, "Header::GetZSpacing: unfound StrSpacingBSlices");
365 std::string strSliceThickness = GetEntryByNumber(0x0018,0x0050);
366 if ( strSliceThickness == GDCM_UNFOUND )
372 // if no 'Spacing Between Slices' is found,
373 // we assume slices join together
374 // (no overlapping, no interslice gap)
375 // if they don't, we're fucked up
376 return atof( strSliceThickness.c_str() );
381 return atof( strSpacingBSlices.c_str() );
386 *\brief gets the info from 0028,1052 : Rescale Intercept
387 * @return Rescale Intercept
389 float Header::GetRescaleIntercept()
392 /// 0028 1052 DS IMG Rescale Intercept
393 std::string strRescInter = GetEntryByNumber(0x0028,0x1052);
394 if ( strRescInter != GDCM_UNFOUND )
396 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
398 // bug in the element 0x0028,0x1052
399 dbg.Verbose(0, "Header::GetRescaleIntercept: Rescale Slope "
408 *\brief gets the info from 0028,1053 : Rescale Slope
409 * @return Rescale Slope
411 float Header::GetRescaleSlope()
414 //0028 1053 DS IMG Rescale Slope
415 std::string strRescSlope = GetEntryByNumber(0x0028,0x1053);
416 if ( strRescSlope != GDCM_UNFOUND )
418 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
420 // bug in the element 0x0028,0x1053
421 dbg.Verbose(0, "Header::GetRescaleSlope: Rescale Slope is empty");
429 * \brief This function is intended to user who doesn't want
430 * to have to manage a LUT and expects to get an RBG Pixel image
431 * (or a monochrome one ...)
432 * \warning to be used with GetImagePixels()
433 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
435 int Header::GetNumberOfScalarComponents()
437 if ( GetSamplesPerPixel() == 3 )
442 // 0028 0100 US IMG Bits Allocated
443 // (in order no to be messed up by old RGB images)
444 if ( GetEntryByNumber(0x0028,0x0100) == "24" )
449 std::string strPhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
451 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
453 if ( HasLUT() )// PALETTE COLOR is NOT enough
463 // beware of trailing space at end of string
464 // DICOM tags are never of odd length
465 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
466 strPhotometricInterpretation == "MONOCHROME1 " ||
467 strPhotometricInterpretation == "MONOCHROME2 " )
473 // we assume that *all* kinds of YBR are dealt with
479 * \brief This function is intended to user that DOESN'T want
480 * to get RGB pixels image when it's stored as a PALETTE COLOR image
481 * - the (vtk) user is supposed to know how deal with LUTs -
482 * \warning to be used with GetImagePixelsRaw()
483 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
485 int Header::GetNumberOfScalarComponentsRaw()
487 // 0028 0100 US IMG Bits Allocated
488 // (in order no to be messed up by old RGB images)
489 if ( Header::GetEntryByNumber(0x0028,0x0100) == "24" )
494 // we assume that *all* kinds of YBR are dealt with
495 return GetSamplesPerPixel();
499 // -------------- Remember ! ----------------------------------
501 // Image Position Patient (0020,0032):
502 // If not found (ACR_NEMA) we try Image Position (0020,0030)
503 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
504 // or Location (0020,0050)
505 // as the Z coordinate,
506 // 0. for all the coordinates if nothing is found
508 // \todo find a way to inform the caller nothing was found
509 // \todo How to tell the caller a wrong number of values was found?
511 // ---------------------------------------------------------------
515 * \brief gets the info from 0020,0032 : Image Position Patient
516 * else from 0020,0030 : Image Position (RET)
518 * @return up-left image corner X position
520 float Header::GetXOrigin()
522 float xImPos, yImPos, zImPos;
523 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
525 if ( strImPos == GDCM_UNFOUND )
527 dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
528 "Position Patient (0020,0032)");
529 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
530 if ( strImPos == GDCM_UNFOUND )
532 dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
533 "Position (RET) (0020,0030)");
534 /// \todo How to tell the caller nothing was found ?
539 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
548 * \brief gets the info from 0020,0032 : Image Position Patient
549 * else from 0020,0030 : Image Position (RET)
551 * @return up-left image corner Y position
553 float Header::GetYOrigin()
555 float xImPos, yImPos, zImPos;
556 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
558 if ( strImPos == GDCM_UNFOUND)
560 dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
561 "Position Patient (0020,0032)");
562 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
563 if ( strImPos == GDCM_UNFOUND )
565 dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
566 "Position (RET) (0020,0030)");
567 /// \todo How to tell the caller nothing was found ?
572 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
581 * \brief gets the info from 0020,0032 : Image Position Patient
582 * else from 0020,0030 : Image Position (RET)
583 * else from 0020,1041 : Slice Location
584 * else from 0020,0050 : Location
586 * @return up-left image corner Z position
588 float Header::GetZOrigin()
590 float xImPos, yImPos, zImPos;
591 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
593 if ( strImPos != GDCM_UNFOUND )
595 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
597 dbg.Verbose(0, "Header::GetZImagePosition: wrong Image "
598 "Position Patient (0020,0032)");
599 return 0.; // bug in the element 0x0020,0x0032
607 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
608 if ( strImPos != GDCM_UNFOUND )
610 if( sscanf( strImPos.c_str(),
611 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
613 dbg.Verbose(0, "Header::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
614 return 0.; // bug in the element 0x0020,0x0032
622 std::string strSliceLocation = GetEntryByNumber(0x0020,0x1041); // for *very* old ACR-NEMA images
623 if ( strSliceLocation != GDCM_UNFOUND )
625 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
627 dbg.Verbose(0, "Header::GetZImagePosition: wrong Slice Location (0020,1041)");
628 return 0.; // bug in the element 0x0020,0x1041
635 dbg.Verbose(0, "Header::GetZImagePosition: unfound Slice Location (0020,1041)");
637 std::string strLocation = GetEntryByNumber(0x0020,0x0050);
638 if ( strLocation != GDCM_UNFOUND )
640 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
642 dbg.Verbose(0, "Header::GetZImagePosition: wrong Location (0020,0050)");
643 return 0.; // bug in the element 0x0020,0x0050
650 dbg.Verbose(0, "Header::GetYImagePosition: unfound Location (0020,0050)");
652 return 0.; // Hopeless
656 * \brief gets the info from 0020,0013 : Image Number else 0.
657 * @return image number
659 int Header::GetImageNumber()
661 // The function i atoi() takes the address of an area of memory as
662 // parameter and converts the string stored at that location to an integer
663 // using the external decimal to internal binary conversion rules. This may
664 // be preferable to sscanf() since atoi() is a much smaller, simpler and
665 // faster function. sscanf() can do all possible conversions whereas
666 // atoi() can only do single decimal integer conversions.
667 //0020 0013 IS REL Image Number
668 std::string strImNumber = GetEntryByNumber(0x0020,0x0013);
669 if ( strImNumber != GDCM_UNFOUND )
671 return atoi( strImNumber.c_str() );
677 * \brief gets the info from 0008,0060 : Modality
678 * @return Modality Type
680 ModalityType Header::GetModality()
682 // 0008 0060 CS ID Modality
683 std::string strModality = GetEntryByNumber(0x0008,0x0060);
684 if ( strModality != GDCM_UNFOUND )
686 if ( strModality.find("AU") < strModality.length()) return AU;
687 else if ( strModality.find("AS") < strModality.length()) return AS;
688 else if ( strModality.find("BI") < strModality.length()) return BI;
689 else if ( strModality.find("CF") < strModality.length()) return CF;
690 else if ( strModality.find("CP") < strModality.length()) return CP;
691 else if ( strModality.find("CR") < strModality.length()) return CR;
692 else if ( strModality.find("CT") < strModality.length()) return CT;
693 else if ( strModality.find("CS") < strModality.length()) return CS;
694 else if ( strModality.find("DD") < strModality.length()) return DD;
695 else if ( strModality.find("DF") < strModality.length()) return DF;
696 else if ( strModality.find("DG") < strModality.length()) return DG;
697 else if ( strModality.find("DM") < strModality.length()) return DM;
698 else if ( strModality.find("DS") < strModality.length()) return DS;
699 else if ( strModality.find("DX") < strModality.length()) return DX;
700 else if ( strModality.find("ECG") < strModality.length()) return ECG;
701 else if ( strModality.find("EPS") < strModality.length()) return EPS;
702 else if ( strModality.find("FA") < strModality.length()) return FA;
703 else if ( strModality.find("FS") < strModality.length()) return FS;
704 else if ( strModality.find("HC") < strModality.length()) return HC;
705 else if ( strModality.find("HD") < strModality.length()) return HD;
706 else if ( strModality.find("LP") < strModality.length()) return LP;
707 else if ( strModality.find("LS") < strModality.length()) return LS;
708 else if ( strModality.find("MA") < strModality.length()) return MA;
709 else if ( strModality.find("MR") < strModality.length()) return MR;
710 else if ( strModality.find("NM") < strModality.length()) return NM;
711 else if ( strModality.find("OT") < strModality.length()) return OT;
712 else if ( strModality.find("PT") < strModality.length()) return PT;
713 else if ( strModality.find("RF") < strModality.length()) return RF;
714 else if ( strModality.find("RG") < strModality.length()) return RG;
715 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
716 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
717 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
718 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
719 else if ( strModality.find("SM") < strModality.length()) return SM;
720 else if ( strModality.find("ST") < strModality.length()) return ST;
721 else if ( strModality.find("TG") < strModality.length()) return TG;
722 else if ( strModality.find("US") < strModality.length()) return US;
723 else if ( strModality.find("VF") < strModality.length()) return VF;
724 else if ( strModality.find("XA") < strModality.length()) return XA;
725 else if ( strModality.find("XC") < strModality.length()) return XC;
729 /// \todo throw error return value ???
730 /// specified <> unknow in our database
739 * \brief Retrieve the number of Bits Stored (actually used)
740 * (as opposite to number of Bits Allocated)
741 * @return The encountered number of Bits Stored, 0 by default.
742 * 0 means the file is NOT USABLE. The caller has to check it !
744 int Header::GetBitsStored()
746 std::string strSize = GetEntryByNumber( 0x0028, 0x0101 );
747 if ( strSize == GDCM_UNFOUND )
749 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
751 return 0; // It's supposed to be mandatory
752 // the caller will have to check
754 return atoi( strSize.c_str() );
758 * \brief Retrieve the high bit position.
759 * \warning The method defaults to 0 when information is absent.
760 * The responsability of checking this value is left to the caller.
761 * @return The high bit positin when present. 0 when absent.
763 int Header::GetHighBitPosition()
765 std::string strSize = GetEntryByNumber( 0x0028, 0x0102 );
766 if ( strSize == GDCM_UNFOUND )
768 dbg.Verbose(0, "Header::GetHighBitPosition: this is supposed "
772 return atoi( strSize.c_str() );
776 * \brief Check wether the pixels are signed or UNsigned data.
777 * \warning The method defaults to false (UNsigned) when information is absent.
778 * The responsability of checking this value is left to the caller.
779 * @return True when signed, false when UNsigned
781 bool Header::IsSignedPixelData()
783 std::string strSize = GetEntryByNumber( 0x0028, 0x0103 );
784 if ( strSize == GDCM_UNFOUND )
786 dbg.Verbose(0, "Header::IsSignedPixelData: this is supposed "
790 int sign = atoi( strSize.c_str() );
799 * \brief Retrieve the number of Bits Allocated
800 * (8, 12 -compacted ACR-NEMA files, 16, ...)
801 * @return The encountered number of Bits Allocated, 0 by default.
802 * 0 means the file is NOT USABLE. The caller has to check it !
804 int Header::GetBitsAllocated()
806 std::string strSize = GetEntryByNumber(0x0028,0x0100);
807 if ( strSize == GDCM_UNFOUND )
809 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
811 return 0; // It's supposed to be mandatory
812 // the caller will have to check
814 return atoi( strSize.c_str() );
818 * \brief Retrieve the number of Samples Per Pixel
819 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
820 * @return The encountered number of Samples Per Pixel, 1 by default.
821 * (Gray level Pixels)
823 int Header::GetSamplesPerPixel()
825 std::string strSize = GetEntryByNumber(0x0028,0x0002);
826 if ( strSize == GDCM_UNFOUND )
828 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
830 return 1; // Well, it's supposed to be mandatory ...
831 // but sometimes it's missing : *we* assume Gray pixels
833 return atoi( strSize.c_str() );
837 * \brief Check wether this a monochrome picture or not by accessing
838 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
839 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
841 bool Header::IsMonochrome()
843 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
844 if ( PhotometricInterp == "MONOCHROME1 "
845 || PhotometricInterp == "MONOCHROME2 " )
849 if ( PhotometricInterp == GDCM_UNFOUND )
851 dbg.Verbose(0, "Header::IsMonochrome: absent Photometric "
858 * \brief Check wether this a "PALETTE COLOR" picture or not by accessing
859 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
860 * @return true when "PALETTE COLOR". False otherwise.
862 bool Header::IsPaletteColor()
864 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
865 if ( PhotometricInterp == "PALETTE COLOR " )
869 if ( PhotometricInterp == GDCM_UNFOUND )
871 dbg.Verbose(0, "Header::IsPaletteColor: absent Photometric "
878 * \brief Check wether this a "YBR_FULL" color picture or not by accessing
879 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
880 * @return true when "YBR_FULL". False otherwise.
882 bool Header::IsYBRFull()
884 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
885 if ( PhotometricInterp == "YBR_FULL" )
889 if ( PhotometricInterp == GDCM_UNFOUND )
891 dbg.Verbose(0, "Header::IsYBRFull: absent Photometric "
898 * \brief Retrieve the Planar Configuration for RGB images
899 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
900 * @return The encountered Planar Configuration, 0 by default.
902 int Header::GetPlanarConfiguration()
904 std::string strSize = GetEntryByNumber(0x0028,0x0006);
905 if ( strSize == GDCM_UNFOUND )
909 return atoi( strSize.c_str() );
913 * \brief Return the size (in bytes) of a single pixel of data.
914 * @return The size in bytes of a single pixel of data; 0 by default
915 * 0 means the file is NOT USABLE; the caller will have to check
917 int Header::GetPixelSize()
919 // 0028 0100 US IMG Bits Allocated
920 // (in order no to be messed up by old RGB images)
921 // if (Header::GetEntryByNumber(0x0028,0x0100) == "24")
924 std::string pixelType = GetPixelType();
925 if ( pixelType == "8U" || pixelType == "8S" )
929 if ( pixelType == "16U" || pixelType == "16S")
933 if ( pixelType == "32U" || pixelType == "32S")
937 if ( pixelType == "FD" )
941 dbg.Verbose(0, "Header::GetPixelSize: Unknown pixel type");
946 * \brief Build the Pixel Type of the image.
947 * Possible values are:
948 * - 8U unsigned 8 bit,
950 * - 16U unsigned 16 bit,
951 * - 16S signed 16 bit,
952 * - 32U unsigned 32 bit,
953 * - 32S signed 32 bit,
954 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
955 * \warning 12 bit images appear as 16 bit.
956 * 24 bit images appear as 8 bit
957 * @return 0S if nothing found. NOT USABLE file. The caller has to check
959 std::string Header::GetPixelType()
961 std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
962 if ( bitsAlloc == GDCM_UNFOUND )
964 dbg.Verbose(0, "Header::GetPixelType: unfound Bits Allocated");
968 if ( bitsAlloc == "64" )
972 if ( bitsAlloc == "12" )
974 // It will be unpacked
977 else if ( bitsAlloc == "24" )
979 // (in order no to be messed up
980 bitsAlloc = "8"; // by old RGB images)
983 std::string sign = GetEntryByNumber(0x0028, 0x0103);//"Pixel Representation"
985 if (sign == GDCM_UNFOUND )
987 dbg.Verbose(0, "Header::GetPixelType: unfound Pixel Representation");
998 return bitsAlloc + sign;
1003 * \brief Recover the offset (from the beginning of the file)
1004 * of *image* pixels (not *icone image* pixels, if any !)
1005 * @return Pixel Offset
1007 size_t Header::GetPixelOffset()
1009 DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1012 return pxlElement->GetOffset();
1017 std::cout << "Big trouble : Pixel Element ("
1018 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1025 /// \todo TODO : unify those two (previous one and next one)
1027 * \brief Recover the pixel area length (in Bytes)
1028 * @return Pixel Element Length, as stored in the header
1029 * (NOT the memory space necessary to hold the Pixels
1030 * -in case of embeded compressed image-)
1031 * 0 : NOT USABLE file. The caller has to check.
1033 size_t Header::GetPixelAreaLength()
1035 DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1038 return pxlElement->GetLength();
1043 std::cout << "Big trouble : Pixel Element ("
1044 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1052 * \brief tells us if LUT are used
1053 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1054 * are NOT considered as LUT, since nobody knows
1055 * how to deal with them
1056 * Please warn me if you know sbdy that *does* know ... jprx
1057 * @return true if LUT Descriptors and LUT Tables were found
1059 bool Header::HasLUT()
1061 // Check the presence of the LUT Descriptors, and LUT Tables
1063 if ( !GetDocEntryByNumber(0x0028,0x1101) )
1067 // LutDescriptorGreen
1068 if ( !GetDocEntryByNumber(0x0028,0x1102) )
1072 // LutDescriptorBlue
1073 if ( !GetDocEntryByNumber(0x0028,0x1103) )
1077 // Red Palette Color Lookup Table Data
1078 if ( !GetDocEntryByNumber(0x0028,0x1201) )
1082 // Green Palette Color Lookup Table Data
1083 if ( !GetDocEntryByNumber(0x0028,0x1202) )
1087 // Blue Palette Color Lookup Table Data
1088 if ( !GetDocEntryByNumber(0x0028,0x1203) )
1093 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1094 // NOT taken into account, but we don't know how to use it ...
1099 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1101 * @return Lookup Table number of Bits , 0 by default
1102 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1103 * @ return bit number of each LUT item
1105 int Header::GetLUTNbits()
1107 std::vector<std::string> tokens;
1110 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1111 // = Lookup Table Desc-Blue
1112 // Consistency already checked in GetLUTLength
1113 std::string lutDescription = GetEntryByNumber(0x0028,0x1101);
1114 if ( lutDescription == GDCM_UNFOUND )
1119 tokens.clear(); // clean any previous value
1120 Util::Tokenize ( lutDescription, tokens, "\\" );
1121 //LutLength=atoi(tokens[0].c_str());
1122 //LutDepth=atoi(tokens[1].c_str());
1124 lutNbits = atoi( tokens[2].c_str() );
1131 * \brief builts Red/Green/Blue/Alpha LUT from Header
1132 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1133 * and (0028,1101),(0028,1102),(0028,1102)
1134 * - xxx Palette Color Lookup Table Descriptor - are found
1135 * and (0028,1201),(0028,1202),(0028,1202)
1136 * - xxx Palette Color Lookup Table Data - are found
1137 * \warning does NOT deal with :
1138 * 0028 1100 Gray Lookup Table Descriptor (Retired)
1139 * 0028 1221 Segmented Red Palette Color Lookup Table Data
1140 * 0028 1222 Segmented Green Palette Color Lookup Table Data
1141 * 0028 1223 Segmented Blue Palette Color Lookup Table Data
1142 * no known Dicom reader deals with them :-(
1143 * @return a RGBA Lookup Table
1145 uint8_t* Header::GetLUTRGBA()
1147 // Not so easy : see
1148 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
1150 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
1151 if ( GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR " )
1156 int lengthR, debR, nbitsR;
1157 int lengthG, debG, nbitsG;
1158 int lengthB, debB, nbitsB;
1160 // Get info from Lut Descriptors
1161 // (the 3 LUT descriptors may be different)
1162 std::string lutDescriptionR = GetEntryByNumber(0x0028,0x1101);
1163 if ( lutDescriptionR == GDCM_UNFOUND )
1168 std::string lutDescriptionG = GetEntryByNumber(0x0028,0x1102);
1169 if ( lutDescriptionG == GDCM_UNFOUND )
1174 std::string lutDescriptionB = GetEntryByNumber(0x0028,0x1103);
1175 if ( lutDescriptionB == GDCM_UNFOUND )
1180 // lengthR: Red LUT length in Bytes
1181 // debR: subscript of the first Lut Value
1182 // nbitsR: Lut item size (in Bits)
1183 int nbRead = sscanf( lutDescriptionR.c_str(), "%d\\%d\\%d",
1184 &lengthR, &debR, &nbitsR );
1187 dbg.Verbose(0, "Header::GetLUTRGBA: trouble reading red LUT");
1190 // lengthG: Green LUT length in Bytes
1191 // debG: subscript of the first Lut Value
1192 // nbitsG: Lut item size (in Bits)
1193 nbRead = sscanf( lutDescriptionG.c_str(), "%d\\%d\\%d",
1194 &lengthG, &debG, &nbitsG );
1197 dbg.Verbose(0, "Header::GetLUTRGBA: trouble reading green LUT");
1200 // lengthB: Blue LUT length in Bytes
1201 // debB: subscript of the first Lut Value
1202 // nbitsB: Lut item size (in Bits)
1203 nbRead = sscanf( lutDescriptionB.c_str(), "%d\\%d\\%d",
1204 &lengthB, &debB, &nbitsB );
1207 dbg.Verbose(0, "Header::GetLUTRGBA: trouble reading blue LUT");
1210 // Load LUTs into memory, (as they were stored on disk)
1211 uint8_t* lutR = (uint8_t*) GetEntryBinAreaByNumber(0x0028,0x1201);
1212 uint8_t* lutG = (uint8_t*) GetEntryBinAreaByNumber(0x0028,0x1202);
1213 uint8_t* lutB = (uint8_t*) GetEntryBinAreaByNumber(0x0028,0x1203);
1215 if ( !lutR || !lutG || !lutB )
1217 dbg.Verbose(0, "Header::GetLUTRGBA: trouble with one of the LUT");
1220 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
1222 uint8_t* LUTRGBA = new uint8_t[1024]; // 256 * 4 (R, G, B, Alpha)
1227 memset(LUTRGBA, 0, 1024);
1231 std::string str_nb = GetEntryByNumber(0x0028,0x0100);
1232 if ( str_nb == GDCM_UNFOUND )
1238 nb = atoi( str_nb.c_str() );
1242 if ( nbitsR == 16 && nb == 8)
1244 // when LUT item size is different than pixel size
1245 mult = 2; // high byte must be = low byte
1249 // See PS 3.3-2003 C.11.1.1.2 p 619
1253 // if we get a black image, let's just remove the '+1'
1254 // from 'i*mult+1' and check again
1255 // if it works, we shall have to check the 3 Palettes
1256 // to see which byte is ==0 (first one, or second one)
1258 // We give up the checking to avoid some (useless ?)overhead
1259 // (optimistic asumption)
1264 for( i=0; i < lengthR; ++i)
1266 *a = lutR[i*mult+1];
1271 for( i=0; i < lengthG; ++i)
1273 *a = lutG[i*mult+1];
1278 for(i=0; i < lengthB; ++i)
1280 *a = lutB[i*mult+1];
1285 for(i=0; i < 256; ++i)
1287 *a = 1; // Alpha component
1294 * \brief Accesses the info from 0002,0010 : Transfert Syntax and TS
1296 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1298 std::string Header::GetTransfertSyntaxName()
1300 // use the TS (TS : Transfert Syntax)
1301 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1303 if ( transfertSyntax == GDCM_NOTLOADED )
1305 std::cout << "Transfert Syntax not loaded. " << std::endl
1306 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
1308 return "Uncompressed ACR-NEMA";
1310 if ( transfertSyntax == GDCM_UNFOUND )
1312 dbg.Verbose(0, "Header::GetTransfertSyntaxName:"
1313 " unfound Transfert Syntax (0002,0010)");
1314 return "Uncompressed ACR-NEMA";
1317 while ( ! isdigit(transfertSyntax[transfertSyntax.length()-1]) )
1319 transfertSyntax.erase(transfertSyntax.length()-1, 1);
1321 // we do it only when we need it
1322 TS* ts = Global::GetTS();
1323 std::string tsName = ts->GetValue( transfertSyntax );
1325 //delete ts; /// \todo Seg Fault when deleted ?!
1330 * \brief Sets the Pixel Area size in the Header
1331 * --> not-for-rats function
1332 * @param ImageDataSize new Pixel Area Size
1333 * warning : nothing else is checked
1335 void Header::SetImageDataSize(size_t ImageDataSize)
1337 ///FIXME I don't understand this code wh ydo we set two times 'car' ?
1338 std::string car = Util::Format("%d", ImageDataSize);
1340 DocEntry *a = GetDocEntryByNumber(GrPixel, NumPixel);
1341 a->SetLength(ImageDataSize);
1344 car = Util::Format("%d", ImageDataSize);
1346 SetEntryByNumber(car, GrPixel, NumPixel);
1349 //-----------------------------------------------------------------------------
1353 * \brief anonymize a Header (removes Patient's personal info)
1354 * (read the code to see which ones ...)
1356 bool Header::AnonymizeHeader()
1358 // If exist, replace by spaces
1359 SetEntryByNumber (" ",0x0010, 0x2154); // Telephone
1360 SetEntryByNumber (" ",0x0010, 0x1040); // Adress
1361 SetEntryByNumber (" ",0x0010, 0x0020); // Patient ID
1363 DocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1365 if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1367 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1368 if ( studyInstanceUID != GDCM_UNFOUND )
1370 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1374 ReplaceOrCreateByNumber(std::string("anonymised"), 0x0010, 0x0010);
1379 // (if any) remove or replace all the stuff that contains a Date
1381 //0008 0012 DA ID Instance Creation Date
1382 //0008 0020 DA ID Study Date
1383 //0008 0021 DA ID Series Date
1384 //0008 0022 DA ID Acquisition Date
1385 //0008 0023 DA ID Content Date
1386 //0008 0024 DA ID Overlay Date
1387 //0008 0025 DA ID Curve Date
1388 //0008 002a DT ID Acquisition Datetime
1389 //0018 9074 DT ACQ Frame Acquisition Datetime
1390 //0018 9151 DT ACQ Frame Reference Datetime
1391 //0018 a002 DT ACQ Contribution Date Time
1392 //0020 3403 SH REL Modified Image Date (RET)
1393 //0032 0032 DA SDY Study Verified Date
1394 //0032 0034 DA SDY Study Read Date
1395 //0032 1000 DA SDY Scheduled Study Start Date
1396 //0032 1010 DA SDY Scheduled Study Stop Date
1397 //0032 1040 DA SDY Study Arrival Date
1398 //0032 1050 DA SDY Study Completion Date
1399 //0038 001a DA VIS Scheduled Admission Date
1400 //0038 001c DA VIS Scheduled Discharge Date
1401 //0038 0020 DA VIS Admitting Date
1402 //0038 0030 DA VIS Discharge Date
1403 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1404 //0040 0004 DA PRC Scheduled Procedure Step End Date
1405 //0040 0244 DA PRC Performed Procedure Step Start Date
1406 //0040 0250 DA PRC Performed Procedure Step End Date
1407 //0040 2004 DA PRC Issue Date of Imaging Service Request
1408 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1409 //0040 4011 DT PRC Expected Completion Date and Time
1410 //0040 a030 DT PRC Verification Date Time
1411 //0040 a032 DT PRC Observation Date Time
1412 //0040 a120 DT PRC DateTime
1413 //0040 a121 DA PRC Date
1414 //0040 a13a DT PRC Referenced Datetime
1415 //0070 0082 DA ??? Presentation Creation Date
1416 //0100 0420 DT ??? SOP Autorization Date and Time
1417 //0400 0105 DT ??? Digital Signature DateTime
1418 //2100 0040 DA PJ Creation Date
1419 //3006 0008 DA SSET Structure Set Date
1420 //3008 0024 DA ??? Treatment Control Point Date
1421 //3008 0054 DA ??? First Treatment Date
1422 //3008 0056 DA ??? Most Recent Treatment Date
1423 //3008 0162 DA ??? Safe Position Exit Date
1424 //3008 0166 DA ??? Safe Position Return Date
1425 //3008 0250 DA ??? Treatment Date
1426 //300a 0006 DA RT RT Plan Date
1427 //300a 022c DA RT Air Kerma Rate Reference Date
1428 //300e 0004 DA RT Review Date
1434 * \brief gets the info from 0020,0037 : Image Orientation Patient
1435 * @param iop adress of the (6)float aray to receive values
1436 * @return cosines of image orientation patient
1438 void Header::GetImageOrientationPatient( float iop[6] )
1440 std::string strImOriPat;
1441 //iop is supposed to be float[6]
1442 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1444 // 0020 0037 DS REL Image Orientation (Patient)
1445 if ( (strImOriPat = GetEntryByNumber(0x0020,0x0037)) != GDCM_UNFOUND )
1447 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1448 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1450 dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1451 "wrong Image Orientation Patient (0020,0037)");
1455 // 0020 0035 DS REL Image Orientation (RET)
1456 else if ( (strImOriPat = GetEntryByNumber(0x0020,0x0035)) != GDCM_UNFOUND )
1458 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1459 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1461 dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1462 "wrong Image Orientation Patient (0020,0035)");
1467 //-----------------------------------------------------------------------------
1470 //-----------------------------------------------------------------------------
1472 } // end namespace gdcm