1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/11/23 01:10:40 $
7 Version: $Revision: 1.204 $
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 const 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.
82 * \brief Constructor used when we want to generate dicom files from scratch
87 InitializeDefaultHeader();
91 * \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(std::ofstream* 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
119 std::string s_lgPix = Util::Format("%d", i_lgPix+12);
120 s_lgPix = Util::DicomString( s_lgPix.c_str() );
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 const 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()
223 const std::string &strSize = GetEntryByNumber(0x0028,0x0011);
224 if ( strSize == GDCM_UNFOUND )
229 return atoi( strSize.c_str() );
233 * \brief Retrieve the number of lines of image.
234 * \warning The defaulted value is 1 as opposed to Header::GetXSize()
235 * @return The encountered size when found, 1 by default
236 * (The ACR-NEMA file contains a Signal, not an Image).
238 int Header::GetYSize()
240 const std::string &strSize = GetEntryByNumber(0x0028,0x0010);
241 if ( strSize != GDCM_UNFOUND )
243 return atoi( strSize.c_str() );
250 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
251 // hence be a signal (1d image). So we default to 1:
256 * \brief Retrieve the number of planes of volume or the number
257 * of frames of a multiframe.
258 * \warning When present we consider the "Number of Frames" as the third
259 * dimension. When absent we consider the third dimension as
260 * being the ACR-NEMA "Planes" tag content.
261 * @return The encountered size when found, 1 by default (single image).
263 int Header::GetZSize()
265 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
266 // as the third dimension.
267 const std::string &strSize = GetEntryByNumber(0x0028,0x0008);
268 if ( strSize != GDCM_UNFOUND )
270 return atoi( strSize.c_str() );
273 // We then consider the "Planes" entry as the third dimension
274 const std::string &strSize2 = GetEntryByNumber(0x0028,0x0012);
275 if ( strSize2 != GDCM_UNFOUND )
277 return atoi( strSize2.c_str() );
284 * \brief gets the info from 0028,0030 : Pixel Spacing
286 * @return X dimension of a pixel
288 float Header::GetXSpacing()
290 float xspacing, yspacing;
291 const std::string &strSpacing = GetEntryByNumber(0x0028,0x0030);
293 if ( strSpacing == GDCM_UNFOUND )
295 dbg.Verbose(0, "Header::GetXSpacing: unfound Pixel Spacing (0028,0030)");
300 if( ( nbValues = sscanf( strSpacing.c_str(),
301 "%f\\%f", &yspacing, &xspacing)) != 2 )
303 // if single value is found, xspacing is defaulted to yspacing
311 dbg.Verbose(0, "Header::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
312 // seems to be a bug in the header ...
313 sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
320 * \brief gets the info from 0028,0030 : Pixel Spacing
322 * @return Y dimension of a pixel
324 float Header::GetYSpacing()
327 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
329 if ( strSpacing == GDCM_UNFOUND )
331 dbg.Verbose(0, "Header::GetYSpacing: unfound Pixel Spacing (0028,0030)");
335 // if sscanf cannot read any float value, it won't affect yspacing
336 sscanf( strSpacing.c_str(), "%f", &yspacing);
342 * \brief gets the info from 0018,0088 : Space Between Slices
343 * else from 0018,0050 : Slice Thickness
345 * @return Z dimension of a voxel-to be
347 float Header::GetZSpacing()
349 // Spacing Between Slices : distance entre le milieu de chaque coupe
350 // Les coupes peuvent etre :
351 // jointives (Spacing between Slices = Slice Thickness)
352 // chevauchantes (Spacing between Slices < Slice Thickness)
353 // disjointes (Spacing between Slices > Slice Thickness)
354 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
355 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
356 // Si le Spacing Between Slices est absent,
357 // on suppose que les coupes sont jointives
359 std::string strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
361 if ( strSpacingBSlices == GDCM_UNFOUND )
363 dbg.Verbose(0, "Header::GetZSpacing: unfound StrSpacingBSlices");
364 std::string strSliceThickness = GetEntryByNumber(0x0018,0x0050);
365 if ( strSliceThickness == GDCM_UNFOUND )
371 // if no 'Spacing Between Slices' is found,
372 // we assume slices join together
373 // (no overlapping, no interslice gap)
374 // if they don't, we're fucked up
375 return (float)atof( strSliceThickness.c_str() );
380 return (float)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 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
467 Util::DicomStringEqual(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 const 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 const std::string& PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
844 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
845 || Util::DicomStringEqual(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 Accesses the info from 0002,0010 : Transfert Syntax and TS
1133 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1135 std::string Header::GetTransfertSyntaxName()
1137 // use the TS (TS : Transfert Syntax)
1138 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1140 if ( transfertSyntax == GDCM_NOTLOADED )
1142 std::cout << "Transfert Syntax not loaded. " << std::endl
1143 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
1145 return "Uncompressed ACR-NEMA";
1147 if ( transfertSyntax == GDCM_UNFOUND )
1149 dbg.Verbose(0, "Header::GetTransfertSyntaxName:"
1150 " unfound Transfert Syntax (0002,0010)");
1151 return "Uncompressed ACR-NEMA";
1154 while ( ! isdigit((unsigned char)transfertSyntax[transfertSyntax.length()-1]) )
1156 transfertSyntax.erase(transfertSyntax.length()-1, 1);
1158 // we do it only when we need it
1159 TS* ts = Global::GetTS();
1160 std::string tsName = ts->GetValue( transfertSyntax );
1162 //delete ts; /// \todo Seg Fault when deleted ?!
1167 * \brief Sets the Pixel Area size in the Header
1168 * --> not-for-rats function
1169 * @param ImageDataSize new Pixel Area Size
1170 * warning : nothing else is checked
1172 void Header::SetImageDataSize(size_t ImageDataSize)
1174 ///FIXME I don't understand this code why do we set two times 'car' ?
1175 std::string car = Util::Format("%d", ImageDataSize);
1177 DocEntry *a = GetDocEntryByNumber(GrPixel, NumPixel);
1178 a->SetLength(ImageDataSize);
1181 car = Util::Format("%d", ImageDataSize);
1182 car = Util::DicomString( car.c_str() );
1184 SetEntryByNumber(car, GrPixel, NumPixel);
1187 //-----------------------------------------------------------------------------
1191 * \brief anonymize a Header (removes Patient's personal info)
1192 * (read the code to see which ones ...)
1194 bool Header::AnonymizeHeader()
1196 // If exist, replace by spaces
1197 SetEntryByNumber (" ",0x0010, 0x2154); // Telephone
1198 SetEntryByNumber (" ",0x0010, 0x1040); // Adress
1199 SetEntryByNumber (" ",0x0010, 0x0020); // Patient ID
1201 DocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1203 if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1205 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1206 if ( studyInstanceUID != GDCM_UNFOUND )
1208 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1212 ReplaceOrCreateByNumber("anonymised", 0x0010, 0x0010);
1217 // (if any) remove or replace all the stuff that contains a Date
1219 //0008 0012 DA ID Instance Creation Date
1220 //0008 0020 DA ID Study Date
1221 //0008 0021 DA ID Series Date
1222 //0008 0022 DA ID Acquisition Date
1223 //0008 0023 DA ID Content Date
1224 //0008 0024 DA ID Overlay Date
1225 //0008 0025 DA ID Curve Date
1226 //0008 002a DT ID Acquisition Datetime
1227 //0018 9074 DT ACQ Frame Acquisition Datetime
1228 //0018 9151 DT ACQ Frame Reference Datetime
1229 //0018 a002 DT ACQ Contribution Date Time
1230 //0020 3403 SH REL Modified Image Date (RET)
1231 //0032 0032 DA SDY Study Verified Date
1232 //0032 0034 DA SDY Study Read Date
1233 //0032 1000 DA SDY Scheduled Study Start Date
1234 //0032 1010 DA SDY Scheduled Study Stop Date
1235 //0032 1040 DA SDY Study Arrival Date
1236 //0032 1050 DA SDY Study Completion Date
1237 //0038 001a DA VIS Scheduled Admission Date
1238 //0038 001c DA VIS Scheduled Discharge Date
1239 //0038 0020 DA VIS Admitting Date
1240 //0038 0030 DA VIS Discharge Date
1241 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1242 //0040 0004 DA PRC Scheduled Procedure Step End Date
1243 //0040 0244 DA PRC Performed Procedure Step Start Date
1244 //0040 0250 DA PRC Performed Procedure Step End Date
1245 //0040 2004 DA PRC Issue Date of Imaging Service Request
1246 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1247 //0040 4011 DT PRC Expected Completion Date and Time
1248 //0040 a030 DT PRC Verification Date Time
1249 //0040 a032 DT PRC Observation Date Time
1250 //0040 a120 DT PRC DateTime
1251 //0040 a121 DA PRC Date
1252 //0040 a13a DT PRC Referenced Datetime
1253 //0070 0082 DA ??? Presentation Creation Date
1254 //0100 0420 DT ??? SOP Autorization Date and Time
1255 //0400 0105 DT ??? Digital Signature DateTime
1256 //2100 0040 DA PJ Creation Date
1257 //3006 0008 DA SSET Structure Set Date
1258 //3008 0024 DA ??? Treatment Control Point Date
1259 //3008 0054 DA ??? First Treatment Date
1260 //3008 0056 DA ??? Most Recent Treatment Date
1261 //3008 0162 DA ??? Safe Position Exit Date
1262 //3008 0166 DA ??? Safe Position Return Date
1263 //3008 0250 DA ??? Treatment Date
1264 //300a 0006 DA RT RT Plan Date
1265 //300a 022c DA RT Air Kerma Rate Reference Date
1266 //300e 0004 DA RT Review Date
1272 * \brief gets the info from 0020,0037 : Image Orientation Patient
1273 * @param iop adress of the (6)float aray to receive values
1274 * @return cosines of image orientation patient
1276 void Header::GetImageOrientationPatient( float iop[6] )
1278 std::string strImOriPat;
1279 //iop is supposed to be float[6]
1280 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1282 // 0020 0037 DS REL Image Orientation (Patient)
1283 if ( (strImOriPat = GetEntryByNumber(0x0020,0x0037)) != GDCM_UNFOUND )
1285 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1286 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1288 dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1289 "wrong Image Orientation Patient (0020,0037)");
1293 // 0020 0035 DS REL Image Orientation (RET)
1294 else if ( (strImOriPat = GetEntryByNumber(0x0020,0x0035)) != GDCM_UNFOUND )
1296 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1297 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1299 dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1300 "wrong Image Orientation Patient (0020,0035)");
1306 * \brief Initialize a default DICOM header that should contain all the
1307 * field require by other reader. DICOM standart does not
1308 * explicitely defines thoses fields, heuristic has been choosen.
1309 * This is not perfect as we are writting a CT image...
1311 void Header::InitializeDefaultHeader()
1318 } DICOM_DEFAULT_VALUE;
1320 const char *date = Util::GetCurrentDate().c_str();
1321 const char *time = Util::GetCurrentTime().c_str();
1322 const char *uid = Util::CreateUniqueUID().c_str();
1324 static DICOM_DEFAULT_VALUE defaultvalue[] = {
1325 "76", 0x0002, 0x0000, // MetaElementGroup Length // FIXME: how to recompute ?
1326 "1.2.840.10008.5.1.4.1.1.2", 0x0002, 0x0002, // MediaStorageSOPInstanceUID (CT Image Storage)
1327 uid, 0x0002, 0x0012, // META Implementation Class UID
1328 "ISO_IR 100", 0x0008, 0x0005, // Specific Character Set
1329 "DERIVED\\SECONDARY\\OTHER\\GDCM", 0x0008, 0x0008, // Image Type
1330 "1.2.840.10008.5.1.4.1.1.2", 0x0008, 0x0016, // SOP Class UID
1331 "", 0x0008, 0x0050, // Accession Number
1332 "GDCM", 0x0008, 0x0070, // Manufacturer
1333 "GDCM", 0x0008, 0x0080, // Institution Name
1334 "http://www-creatis.insa-lyon.fr/Public/Gdcm/", 0x0008, 0x0081, // Institution Address
1335 "", 0x0008, 0x0090, // Refering Physician Name
1336 "", 0x0008, 0x1030, // Study Description
1337 "", 0x0008, 0x1050, // Performing Physician's Name
1338 "", 0x0008, 0x1060, // Name of Physician(s) Reading Study
1339 "", 0x0010, 0x0040, // Patient's Sex
1340 uid, 0x0020, 0x000d, // StudyInstanceUID
1341 uid, 0x0020, 0x000e, // SeriesInstanceUID
1342 "", 0x0020, 0x0011, // AcquisitionNumber
1343 "1\\0\\0\\0\\1\\0", 0x0020, 0x0037, // Image Orientation Patient
1344 "1", 0x0028, 0x0002, // Samples per pixel 1 or 3
1345 "MONOCHROME2", 0x0028, 0x0004, // photochromatic interpretation
1349 date, 0x0008, 0x0012, // Instance Creation Date
1350 time, 0x0008, 0x0013, // Instance Creation Time
1351 date, 0x0008, 0x0020, // Study Date
1352 date, 0x0008, 0x0022, // Acquisition Date
1353 date, 0x0008, 0x0023, // Content Date
1354 time, 0x0008, 0x0030, // Study Time
1355 "CT", 0x0008, 0x0060, // Modality
1356 "GDCM", 0x0010, 0x0010, // Patient's Name
1357 "GDCMID", 0x0010, 0x0020, // Patient ID
1358 "1", 0x0020, 0x0010, // StudyID
1359 "1", 0x0020, 0x0011, // SeriesNumber
1360 "1.0", 0x0018, 0x0050, // slice Thickness
1361 "1.0", 0x0018, 0x0088, // space between slices
1362 "1.0\\1.0", 0x0028, 0x0030, // pixelSpacing
1363 "64", 0x0028, 0x0010, // nbRows
1364 "64", 0x0028, 0x0011, // nbCols
1365 "16", 0x0028, 0x0100, // BitsAllocated 8 or 16
1366 "12", 0x0028, 0x0101, // BitsStored 8 or 12
1367 "11", 0x0028, 0x0102, // HighBit 7 or 11
1368 "0", 0x0028, 0x0103, // Pixel Representation 0(unsigned) or 1(signed)
1369 "1000.0", 0x0028, 0x1051, // Window Width
1370 "500.0", 0x0028, 0x1050, // Window Center
1375 // Special case this is the image (not a string)
1378 ReplaceOrCreateByNumber(0, 0, GrPixel, NumPixel);
1380 // All remaining strings:
1382 DICOM_DEFAULT_VALUE current = defaultvalue[i];
1383 while( current.value )
1385 std::string value = Util::DicomString( current.value ); //pad the string properly
1386 ReplaceOrCreateByNumber(value, current.group, current.elem);
1387 current = defaultvalue[++i];
1391 //-----------------------------------------------------------------------------
1394 //-----------------------------------------------------------------------------
1396 } // end namespace gdcm