1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2005/01/11 23:06:35 $
7 Version: $Revision: 1.231 $
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"
25 #include "gdcmBinEntry.h"
26 #include <stdio.h> //sscanf
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
36 * @param filename name of the file whose header we want to analyze
38 Header::Header( std::string const &filename ):
41 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
42 // We may encounter the 'RETired' (0x0028, 0x0200) tag
43 // (Image Location") . This Element contains the number of
44 // the group that contains the pixel data (hence the "Pixel Data"
45 // is found by indirection through the "Image Location").
46 // Inside the group pointed by "Image Location" the searched element
47 // is conventionally the element 0x0010 (when the norm is respected).
48 // When the "Image Location" is Missing we default to group 0x7fe0.
49 // Note: this IS the right place for the code
52 const std::string &imgLocation = GetEntry(0x0028, 0x0200);
53 if ( imgLocation == GDCM_UNFOUND )
60 GrPixel = (uint16_t) atoi( imgLocation.c_str() );
63 // sometimes Image Location value doesn't follow
64 // the supposed processor endianity.
65 // see gdcmData/cr172241.dcm
66 if ( GrPixel == 0xe07f )
71 if ( GrPixel != 0x7fe0 )
73 // This is a kludge for old dirty Philips imager.
81 // Now, we know GrPixel and NumPixel.
82 // Let's create a VirtualDictEntry to allow a further VR modification
83 // and force VR to match with BitsAllocated.
85 DocEntry *entry = GetDocEntry(GrPixel, NumPixel);
90 // 8 bits allocated is a 'O Bytes' , as well as 24 (old ACR-NEMA RGB)
91 // more than 8 (i.e 12, 16) is a 'O Words'
92 if ( GetBitsAllocated() == 8 || GetBitsAllocated() == 24 )
97 DictEntry* newEntry = NewVirtualDictEntry(
99 PixelVR, "PXL", "Pixel Data");
101 entry->SetDictEntry( newEntry );
106 * \brief Constructor used when we want to generate dicom files from scratch
111 InitializeDefaultHeader();
115 * \brief Canonical destructor.
122 * \brief Performs some consistency checking on various 'File related'
123 * (as opposed to 'DicomDir related') entries
124 * then writes in a file all the (Dicom Elements) included the Pixels
125 * @param fileName file name to write to
126 * @param filetype Type of the File to be written
127 * (ACR-NEMA, ExplicitVR, ImplicitVR)
129 bool Header::Write(std::string fileName, FileType filetype)
131 std::ofstream *fp = new std::ofstream(fileName.c_str(),
132 std::ios::out | std::ios::binary);
135 gdcmVerboseMacro("Failed to open (write) File: " << fileName.c_str());
140 if ( GetEntry(0x0028,0x0100) == "12")
142 SetEntry("16", 0x0028,0x0100);
145 /// \todo correct 'Pixel group' Length if necessary
147 int i_lgPix = GetEntryLength(GrPixel, NumPixel);
150 // no (GrPixel, NumPixel) element
151 std::string s_lgPix = Util::Format("%d", i_lgPix+12);
152 s_lgPix = Util::DicomString( s_lgPix.c_str() );
153 ReplaceOrCreate(s_lgPix,GrPixel, 0x0000);
156 // FIXME : should be nice if we could move it to File
157 // (or in future gdcmPixelData class)
159 // Drop Palette Color, if necessary
161 if ( GetEntry(0x0028,0x0002).c_str()[0] == '3' )
163 // if SamplesPerPixel = 3, sure we don't need any LUT !
164 // Drop 0028|1101, 0028|1102, 0028|1103
165 // Drop 0028|1201, 0028|1202, 0028|1203
167 DocEntry *e = GetDocEntry(0x0028,0x01101);
170 RemoveEntryNoDestroy(e);
172 e = GetDocEntry(0x0028,0x1102);
175 RemoveEntryNoDestroy(e);
177 e = GetDocEntry(0x0028,0x1103);
180 RemoveEntryNoDestroy(e);
182 e = GetDocEntry(0x0028,0x01201);
185 RemoveEntryNoDestroy(e);
187 e = GetDocEntry(0x0028,0x1202);
190 RemoveEntryNoDestroy(e);
192 e = GetDocEntry(0x0028,0x1203);
195 RemoveEntryNoDestroy(e);
199 #ifdef GDCM_WORDS_BIGENDIAN
200 // Super Duper hack that will make gdcm a BOMB ! but should
201 // Fix temporarily the dashboard
202 BinEntry *b = GetBinEntry(0x7fe0,0x0010);
203 if ( GetEntry(0x0028,0x0100) == "16")
205 uint16_t *im16 = (uint16_t*)b->GetBinArea();
206 int lgr = b->GetLength();
207 for( int i = 0; i < lgr / 2; i++ )
209 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
212 #endif //GDCM_WORDS_BIGENDIAN
214 Document::WriteContent(fp,filetype);
216 #ifdef GDCM_WORDS_BIGENDIAN
217 // Flip back the pixel ... I told you this is a hack
218 if ( GetEntry(0x0028,0x0100) == "16")
220 uint16_t *im16 = (uint16_t*)b->GetBinArea();
221 int lgr = b->GetLength();
222 for( int i = 0; i < lgr / 2; i++ )
224 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
227 #endif //GDCM_WORDS_BIGENDIAN
235 //-----------------------------------------------------------------------------
239 //-----------------------------------------------------------------------------
243 * \brief This predicate, based on hopefully reasonable heuristics,
244 * decides whether or not the current Header was properly parsed
245 * and contains the mandatory information for being considered as
246 * a well formed and usable Dicom/Acr File.
247 * @return true when Header is the one of a reasonable Dicom/Acr file,
250 bool Header::IsReadable()
252 if( !Document::IsReadable() )
257 const std::string &res = GetEntry(0x0028, 0x0005);
258 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
260 return false; // Image Dimensions
262 if ( !GetDocEntry(0x0028, 0x0100) )
264 return false; // "Bits Allocated"
266 if ( !GetDocEntry(0x0028, 0x0101) )
268 return false; // "Bits Stored"
270 if ( !GetDocEntry(0x0028, 0x0102) )
272 return false; // "High Bit"
274 if ( !GetDocEntry(0x0028, 0x0103) )
276 return false; // "Pixel Representation" i.e. 'Sign'
283 * \brief Retrieve the number of columns of image.
284 * @return The encountered size when found, 0 by default.
285 * 0 means the file is NOT USABLE. The caller will have to check
287 int Header::GetXSize()
289 const std::string &strSize = GetEntry(0x0028,0x0011);
290 if ( strSize == GDCM_UNFOUND )
295 return atoi( strSize.c_str() );
299 * \brief Retrieve the number of lines of image.
300 * \warning The defaulted value is 1 as opposed to Header::GetXSize()
301 * @return The encountered size when found, 1 by default
302 * (The ACR-NEMA file contains a Signal, not an Image).
304 int Header::GetYSize()
306 const std::string &strSize = GetEntry(0x0028,0x0010);
307 if ( strSize != GDCM_UNFOUND )
309 return atoi( strSize.c_str() );
316 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
317 // hence be a signal (1D image). So we default to 1:
322 * \brief Retrieve the number of planes of volume or the number
323 * of frames of a multiframe.
324 * \warning When present we consider the "Number of Frames" as the third
325 * dimension. When Missing we consider the third dimension as
326 * being the ACR-NEMA "Planes" tag content.
327 * @return The encountered size when found, 1 by default (single image).
329 int Header::GetZSize()
331 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
332 // as the third dimension.
333 const std::string &strSize = GetEntry(0x0028,0x0008);
334 if ( strSize != GDCM_UNFOUND )
336 return atoi( strSize.c_str() );
339 // We then consider the "Planes" entry as the third dimension
340 const std::string &strSize2 = GetEntry(0x0028,0x0012);
341 if ( strSize2 != GDCM_UNFOUND )
343 return atoi( strSize2.c_str() );
350 * \brief gets the info from 0028,0030 : Pixel Spacing
352 * @return X dimension of a pixel
354 float Header::GetXSpacing()
356 float xspacing, yspacing;
357 const std::string &strSpacing = GetEntry(0x0028,0x0030);
359 if ( strSpacing == GDCM_UNFOUND )
361 gdcmVerboseMacro( "Unfound Pixel Spacing (0028,0030)" );
366 if( ( nbValues = sscanf( strSpacing.c_str(),
367 "%f\\%f", &yspacing, &xspacing)) != 2 )
369 // if single value is found, xspacing is defaulted to yspacing
375 if ( xspacing == 0.0 )
382 // to avoid troubles with David Clunie's-like images
383 if ( xspacing == 0. && yspacing == 0.)
388 gdcmVerboseMacro("gdcmData/CT-MONO2-8-abdo.dcm problem");
389 // seems to be a bug in the header ...
390 nbValues = sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
391 gdcmAssertMacro( nbValues == 2 );
398 * \brief gets the info from 0028,0030 : Pixel Spacing
400 * @return Y dimension of a pixel
402 float Header::GetYSpacing()
405 std::string strSpacing = GetEntry(0x0028,0x0030);
407 if ( strSpacing == GDCM_UNFOUND )
409 gdcmVerboseMacro("Unfound Pixel Spacing (0028,0030)");
413 // if sscanf cannot read any float value, it won't affect yspacing
414 sscanf( strSpacing.c_str(), "%f", &yspacing);
416 if ( yspacing == 0.0 )
423 * \brief gets the info from 0018,0088 : Space Between Slices
424 * else from 0018,0050 : Slice Thickness
426 * @return Z dimension of a voxel-to be
428 float Header::GetZSpacing()
430 // Spacing Between Slices : distance entre le milieu de chaque coupe
431 // Les coupes peuvent etre :
432 // jointives (Spacing between Slices = Slice Thickness)
433 // chevauchantes (Spacing between Slices < Slice Thickness)
434 // disjointes (Spacing between Slices > Slice Thickness)
435 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
436 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
437 // Si le Spacing Between Slices est Missing,
438 // on suppose que les coupes sont jointives
440 const std::string &strSpacingBSlices = GetEntry(0x0018,0x0088);
442 if ( strSpacingBSlices == GDCM_UNFOUND )
444 gdcmVerboseMacro("Unfound Spacing Between Slices (0018,0088)");
445 const std::string &strSliceThickness = GetEntry(0x0018,0x0050);
446 if ( strSliceThickness == GDCM_UNFOUND )
448 gdcmVerboseMacro("Unfound Slice Thickness (0018,0050)");
453 // if no 'Spacing Between Slices' is found,
454 // we assume slices join together
455 // (no overlapping, no interslice gap)
456 // if they don't, we're fucked up
457 return (float)atof( strSliceThickness.c_str() );
461 return (float)atof( strSpacingBSlices.c_str() );
465 *\brief gets the info from 0028,1052 : Rescale Intercept
466 * @return Rescale Intercept
468 float Header::GetRescaleIntercept()
471 /// 0028 1052 DS IMG Rescale Intercept
472 const std::string &strRescInter = GetEntry(0x0028,0x1052);
473 if ( strRescInter != GDCM_UNFOUND )
475 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
477 // bug in the element 0x0028,0x1052
478 gdcmVerboseMacro( "Rescale Intercept (0028,1052) is empty." );
486 *\brief gets the info from 0028,1053 : Rescale Slope
487 * @return Rescale Slope
489 float Header::GetRescaleSlope()
492 //0028 1053 DS IMG Rescale Slope
493 std::string strRescSlope = GetEntry(0x0028,0x1053);
494 if ( strRescSlope != GDCM_UNFOUND )
496 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
498 // bug in the element 0x0028,0x1053
499 gdcmVerboseMacro( "Rescale Slope (0028,1053) is empty.");
507 * \brief This function is intended to user who doesn't want
508 * to have to manage a LUT and expects to get an RBG Pixel image
509 * (or a monochrome one ...)
510 * \warning to be used with GetImagePixels()
511 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
513 int Header::GetNumberOfScalarComponents()
515 if ( GetSamplesPerPixel() == 3 )
520 // 0028 0100 US IMG Bits Allocated
521 // (in order no to be messed up by old RGB images)
522 if ( GetEntry(0x0028,0x0100) == "24" )
527 std::string strPhotometricInterpretation = GetEntry(0x0028,0x0004);
529 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
531 if ( HasLUT() )// PALETTE COLOR is NOT enough
541 // beware of trailing space at end of string
542 // DICOM tags are never of odd length
543 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
544 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
545 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
551 // we assume that *all* kinds of YBR are dealt with
557 * \brief This function is intended to user that DOESN'T want
558 * to get RGB pixels image when it's stored as a PALETTE COLOR image
559 * - the (vtk) user is supposed to know how deal with LUTs -
560 * \warning to be used with GetImagePixelsRaw()
561 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
563 int Header::GetNumberOfScalarComponentsRaw()
565 // 0028 0100 US IMG Bits Allocated
566 // (in order no to be messed up by old RGB images)
567 if ( Header::GetEntry(0x0028,0x0100) == "24" )
572 // we assume that *all* kinds of YBR are dealt with
573 return GetSamplesPerPixel();
577 // -------------- Remember ! ----------------------------------
579 // Image Position Patient (0020,0032):
580 // If not found (ACR_NEMA) we try Image Position (0020,0030)
581 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
582 // or Location (0020,0050)
583 // as the Z coordinate,
584 // 0. for all the coordinates if nothing is found
586 // \todo find a way to inform the caller nothing was found
587 // \todo How to tell the caller a wrong number of values was found?
589 // ---------------------------------------------------------------
593 * \brief gets the info from 0020,0032 : Image Position Patient
594 * else from 0020,0030 : Image Position (RET)
596 * @return up-left image corner X position
598 float Header::GetXOrigin()
600 float xImPos, yImPos, zImPos;
601 std::string strImPos = GetEntry(0x0020,0x0032);
603 if ( strImPos == GDCM_UNFOUND )
605 gdcmVerboseMacro( "Unfound Image Position Patient (0020,0032)");
606 strImPos = GetEntry(0x0020,0x0030); // For ACR-NEMA images
607 if ( strImPos == GDCM_UNFOUND )
609 gdcmVerboseMacro( "Unfound Image Position (RET) (0020,0030)");
610 /// \todo How to tell the caller nothing was found ?
615 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
624 * \brief gets the info from 0020,0032 : Image Position Patient
625 * else from 0020,0030 : Image Position (RET)
627 * @return up-left image corner Y position
629 float Header::GetYOrigin()
631 float xImPos, yImPos, zImPos;
632 std::string strImPos = GetEntry(0x0020,0x0032);
634 if ( strImPos == GDCM_UNFOUND)
636 gdcmVerboseMacro( "Unfound Image Position Patient (0020,0032)");
637 strImPos = GetEntry(0x0020,0x0030); // For ACR-NEMA images
638 if ( strImPos == GDCM_UNFOUND )
640 gdcmVerboseMacro( "Unfound Image Position (RET) (0020,0030)");
641 /// \todo How to tell the caller nothing was found ?
646 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
655 * \brief gets the info from 0020,0032 : Image Position Patient
656 * else from 0020,0030 : Image Position (RET)
657 * else from 0020,1041 : Slice Location
658 * else from 0020,0050 : Location
660 * @return up-left image corner Z position
662 float Header::GetZOrigin()
664 float xImPos, yImPos, zImPos;
665 std::string strImPos = GetEntry(0x0020,0x0032);
667 if ( strImPos != GDCM_UNFOUND )
669 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
671 gdcmVerboseMacro( "Wrong Image Position Patient (0020,0032)");
672 return 0.; // bug in the element 0x0020,0x0032
680 strImPos = GetEntry(0x0020,0x0030); // For ACR-NEMA images
681 if ( strImPos != GDCM_UNFOUND )
683 if( sscanf( strImPos.c_str(),
684 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
686 gdcmVerboseMacro( "Wrong Image Position (RET) (0020,0030)");
687 return 0.; // bug in the element 0x0020,0x0032
695 std::string strSliceLocation = GetEntry(0x0020,0x1041); // for *very* old ACR-NEMA images
696 if ( strSliceLocation != GDCM_UNFOUND )
698 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
700 gdcmVerboseMacro( "Wrong Slice Location (0020,1041)");
701 return 0.; // bug in the element 0x0020,0x1041
708 gdcmVerboseMacro( "Unfound Slice Location (0020,1041)");
710 std::string strLocation = GetEntry(0x0020,0x0050);
711 if ( strLocation != GDCM_UNFOUND )
713 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
715 gdcmVerboseMacro( "Wrong Location (0020,0050)");
716 return 0.; // bug in the element 0x0020,0x0050
723 gdcmVerboseMacro( "Unfound Location (0020,0050)");
725 return 0.; // Hopeless
729 * \brief gets the info from 0020,0013 : Image Number else 0.
730 * @return image number
732 int Header::GetImageNumber()
734 // The function i atoi() takes the address of an area of memory as
735 // parameter and converts the string stored at that location to an integer
736 // using the external decimal to internal binary conversion rules. This may
737 // be preferable to sscanf() since atoi() is a much smaller, simpler and
738 // faster function. sscanf() can do all possible conversions whereas
739 // atoi() can only do single decimal integer conversions.
740 //0020 0013 IS REL Image Number
741 std::string strImNumber = GetEntry(0x0020,0x0013);
742 if ( strImNumber != GDCM_UNFOUND )
744 return atoi( strImNumber.c_str() );
750 * \brief gets the info from 0008,0060 : Modality
751 * @return Modality Type
753 ModalityType Header::GetModality()
755 // 0008 0060 CS ID Modality
756 std::string strModality = GetEntry(0x0008,0x0060);
757 if ( strModality != GDCM_UNFOUND )
759 if ( strModality.find("AU") < strModality.length()) return AU;
760 else if ( strModality.find("AS") < strModality.length()) return AS;
761 else if ( strModality.find("BI") < strModality.length()) return BI;
762 else if ( strModality.find("CF") < strModality.length()) return CF;
763 else if ( strModality.find("CP") < strModality.length()) return CP;
764 else if ( strModality.find("CR") < strModality.length()) return CR;
765 else if ( strModality.find("CT") < strModality.length()) return CT;
766 else if ( strModality.find("CS") < strModality.length()) return CS;
767 else if ( strModality.find("DD") < strModality.length()) return DD;
768 else if ( strModality.find("DF") < strModality.length()) return DF;
769 else if ( strModality.find("DG") < strModality.length()) return DG;
770 else if ( strModality.find("DM") < strModality.length()) return DM;
771 else if ( strModality.find("DS") < strModality.length()) return DS;
772 else if ( strModality.find("DX") < strModality.length()) return DX;
773 else if ( strModality.find("ECG") < strModality.length()) return ECG;
774 else if ( strModality.find("EPS") < strModality.length()) return EPS;
775 else if ( strModality.find("FA") < strModality.length()) return FA;
776 else if ( strModality.find("FS") < strModality.length()) return FS;
777 else if ( strModality.find("HC") < strModality.length()) return HC;
778 else if ( strModality.find("HD") < strModality.length()) return HD;
779 else if ( strModality.find("LP") < strModality.length()) return LP;
780 else if ( strModality.find("LS") < strModality.length()) return LS;
781 else if ( strModality.find("MA") < strModality.length()) return MA;
782 else if ( strModality.find("MR") < strModality.length()) return MR;
783 else if ( strModality.find("NM") < strModality.length()) return NM;
784 else if ( strModality.find("OT") < strModality.length()) return OT;
785 else if ( strModality.find("PT") < strModality.length()) return PT;
786 else if ( strModality.find("RF") < strModality.length()) return RF;
787 else if ( strModality.find("RG") < strModality.length()) return RG;
788 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
789 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
790 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
791 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
792 else if ( strModality.find("SM") < strModality.length()) return SM;
793 else if ( strModality.find("ST") < strModality.length()) return ST;
794 else if ( strModality.find("TG") < strModality.length()) return TG;
795 else if ( strModality.find("US") < strModality.length()) return US;
796 else if ( strModality.find("VF") < strModality.length()) return VF;
797 else if ( strModality.find("XA") < strModality.length()) return XA;
798 else if ( strModality.find("XC") < strModality.length()) return XC;
802 /// \todo throw error return value ???
803 /// specified <> unknow in our database
812 * \brief Retrieve the number of Bits Stored (actually used)
813 * (as opposite to number of Bits Allocated)
814 * @return The encountered number of Bits Stored, 0 by default.
815 * 0 means the file is NOT USABLE. The caller has to check it !
817 int Header::GetBitsStored()
819 std::string strSize = GetEntry( 0x0028, 0x0101 );
820 if ( strSize == GDCM_UNFOUND )
822 gdcmVerboseMacro("(0028,0101) is supposed to be mandatory");
823 return 0; // It's supposed to be mandatory
824 // the caller will have to check
826 return atoi( strSize.c_str() );
830 * \brief Retrieve the high bit position.
831 * \warning The method defaults to 0 when information is Missing.
832 * The responsability of checking this value is left to the caller.
833 * @return The high bit positin when present. 0 when Missing.
835 int Header::GetHighBitPosition()
837 std::string strSize = GetEntry( 0x0028, 0x0102 );
838 if ( strSize == GDCM_UNFOUND )
840 gdcmVerboseMacro( "(0028,0102) is supposed to be mandatory");
843 return atoi( strSize.c_str() );
847 * \brief Check wether the pixels are signed or UNsigned data.
848 * \warning The method defaults to false (UNsigned) when information is Missing.
849 * The responsability of checking this value is left to the caller.
850 * @return True when signed, false when UNsigned
852 bool Header::IsSignedPixelData()
854 std::string strSize = GetEntry( 0x0028, 0x0103 );
855 if ( strSize == GDCM_UNFOUND )
857 gdcmVerboseMacro( "(0028,0103) is supposed to be mandatory");
860 int sign = atoi( strSize.c_str() );
869 * \brief Retrieve the number of Bits Allocated
870 * (8, 12 -compacted ACR-NEMA files, 16, ...)
871 * @return The encountered number of Bits Allocated, 0 by default.
872 * 0 means the file is NOT USABLE. The caller has to check it !
874 int Header::GetBitsAllocated()
876 std::string strSize = GetEntry(0x0028,0x0100);
877 if ( strSize == GDCM_UNFOUND )
879 gdcmVerboseMacro( "(0028,0100) is supposed to be mandatory");
880 return 0; // It's supposed to be mandatory
881 // the caller will have to check
883 return atoi( strSize.c_str() );
887 * \brief Retrieve the number of Samples Per Pixel
888 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
889 * @return The encountered number of Samples Per Pixel, 1 by default.
890 * (Gray level Pixels)
892 int Header::GetSamplesPerPixel()
894 const std::string& strSize = GetEntry(0x0028,0x0002);
895 if ( strSize == GDCM_UNFOUND )
897 gdcmVerboseMacro( "(0028,0002) is supposed to be mandatory");
898 return 1; // Well, it's supposed to be mandatory ...
899 // but sometimes it's missing : *we* assume Gray pixels
901 return atoi( strSize.c_str() );
905 * \brief Check wether this a monochrome picture or not by accessing
906 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
907 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
909 bool Header::IsMonochrome()
911 const std::string& PhotometricInterp = GetEntry( 0x0028, 0x0004 );
912 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
913 || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
917 if ( PhotometricInterp == GDCM_UNFOUND )
919 gdcmVerboseMacro( "Not found : Photometric Interpretation (0028,0004)");
925 * \brief Check wether this a "PALETTE COLOR" picture or not by accessing
926 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
927 * @return true when "PALETTE COLOR". False otherwise.
929 bool Header::IsPaletteColor()
931 std::string PhotometricInterp = GetEntry( 0x0028, 0x0004 );
932 if ( PhotometricInterp == "PALETTE COLOR " )
936 if ( PhotometricInterp == GDCM_UNFOUND )
938 gdcmVerboseMacro( "Not found : Palette color (0028,0004)");
944 * \brief Check wether this a "YBR_FULL" color picture or not by accessing
945 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
946 * @return true when "YBR_FULL". False otherwise.
948 bool Header::IsYBRFull()
950 std::string PhotometricInterp = GetEntry( 0x0028, 0x0004 );
951 if ( PhotometricInterp == "YBR_FULL" )
955 if ( PhotometricInterp == GDCM_UNFOUND )
957 gdcmVerboseMacro( "Not found : YBR Full (0028,0004)");
963 * \brief Retrieve the Planar Configuration for RGB images
964 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
965 * @return The encountered Planar Configuration, 0 by default.
967 int Header::GetPlanarConfiguration()
969 std::string strSize = GetEntry(0x0028,0x0006);
970 if ( strSize == GDCM_UNFOUND )
972 gdcmVerboseMacro( "Not found : Planar Configuration (0028,0006)");
975 return atoi( strSize.c_str() );
979 * \brief Return the size (in bytes) of a single pixel of data.
980 * @return The size in bytes of a single pixel of data; 0 by default
981 * 0 means the file is NOT USABLE; the caller will have to check
983 int Header::GetPixelSize()
985 // 0028 0100 US IMG Bits Allocated
986 // (in order no to be messed up by old RGB images)
987 // if (Header::GetEntry(0x0028,0x0100) == "24")
990 std::string pixelType = GetPixelType();
991 if ( pixelType == "8U" || pixelType == "8S" )
995 if ( pixelType == "16U" || pixelType == "16S")
999 if ( pixelType == "32U" || pixelType == "32S")
1003 if ( pixelType == "FD" )
1007 gdcmVerboseMacro( "Unknown pixel type");
1012 * \brief Build the Pixel Type of the image.
1013 * Possible values are:
1014 * - 8U unsigned 8 bit,
1015 * - 8S signed 8 bit,
1016 * - 16U unsigned 16 bit,
1017 * - 16S signed 16 bit,
1018 * - 32U unsigned 32 bit,
1019 * - 32S signed 32 bit,
1020 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
1021 * \warning 12 bit images appear as 16 bit.
1022 * 24 bit images appear as 8 bit
1023 * @return 0S if nothing found. NOT USABLE file. The caller has to check
1025 std::string Header::GetPixelType()
1027 std::string bitsAlloc = GetEntry(0x0028, 0x0100); // Bits Allocated
1028 if ( bitsAlloc == GDCM_UNFOUND )
1030 gdcmVerboseMacro( "Missing Bits Allocated (0028,0100)");
1034 if ( bitsAlloc == "64" )
1038 else if ( bitsAlloc == "12" )
1040 // It will be unpacked
1043 else if ( bitsAlloc == "24" )
1045 // (in order no to be messed up
1046 bitsAlloc = "8"; // by old RGB images)
1049 std::string sign = GetEntry(0x0028, 0x0103);//"Pixel Representation"
1051 if (sign == GDCM_UNFOUND )
1053 gdcmVerboseMacro( "Missing Pixel Representation (0028,0103)");
1056 else if ( sign == "0" )
1064 return bitsAlloc + sign;
1069 * \brief Recover the offset (from the beginning of the file)
1070 * of *image* pixels (not *icone image* pixels, if any !)
1071 * @return Pixel Offset
1073 size_t Header::GetPixelOffset()
1075 DocEntry* pxlElement = GetDocEntry(GrPixel,NumPixel);
1078 return pxlElement->GetOffset();
1083 std::cout << "Big trouble : Pixel Element ("
1084 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1091 /// \todo TODO : unify those two (previous one and next one)
1093 * \brief Recover the pixel area length (in Bytes)
1094 * @return Pixel Element Length, as stored in the header
1095 * (NOT the memory space necessary to hold the Pixels
1096 * -in case of embeded compressed image-)
1097 * 0 : NOT USABLE file. The caller has to check.
1099 size_t Header::GetPixelAreaLength()
1101 DocEntry* pxlElement = GetDocEntry(GrPixel,NumPixel);
1104 return pxlElement->GetLength();
1109 std::cout << "Big trouble : Pixel Element ("
1110 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1118 * \brief tells us if LUT are used
1119 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1120 * are NOT considered as LUT, since nobody knows
1121 * how to deal with them
1122 * Please warn me if you know sbdy that *does* know ... jprx
1123 * @return true if LUT Descriptors and LUT Tables were found
1125 bool Header::HasLUT()
1127 // Check the presence of the LUT Descriptors, and LUT Tables
1129 if ( !GetDocEntry(0x0028,0x1101) )
1133 // LutDescriptorGreen
1134 if ( !GetDocEntry(0x0028,0x1102) )
1138 // LutDescriptorBlue
1139 if ( !GetDocEntry(0x0028,0x1103) )
1143 // Red Palette Color Lookup Table Data
1144 if ( !GetDocEntry(0x0028,0x1201) )
1148 // Green Palette Color Lookup Table Data
1149 if ( !GetDocEntry(0x0028,0x1202) )
1153 // Blue Palette Color Lookup Table Data
1154 if ( !GetDocEntry(0x0028,0x1203) )
1159 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1160 // NOT taken into account, but we don't know how to use it ...
1165 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1167 * @return Lookup Table number of Bits , 0 by default
1168 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1169 * @ return bit number of each LUT item
1171 int Header::GetLUTNbits()
1173 std::vector<std::string> tokens;
1176 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1177 // = Lookup Table Desc-Blue
1178 // Consistency already checked in GetLUTLength
1179 std::string lutDescription = GetEntry(0x0028,0x1101);
1180 if ( lutDescription == GDCM_UNFOUND )
1185 tokens.clear(); // clean any previous value
1186 Util::Tokenize ( lutDescription, tokens, "\\" );
1187 //LutLength=atoi(tokens[0].c_str());
1188 //LutDepth=atoi(tokens[1].c_str());
1190 lutNbits = atoi( tokens[2].c_str() );
1197 //-----------------------------------------------------------------------------
1201 * \brief anonymize a Header (removes Patient's personal info)
1202 * (read the code to see which ones ...)
1204 bool Header::AnonymizeHeader()
1206 // If exist, replace by spaces
1207 SetEntry (" ",0x0010, 0x2154); // Telephone
1208 SetEntry (" ",0x0010, 0x1040); // Adress
1209 SetEntry (" ",0x0010, 0x0020); // Patient ID
1211 DocEntry* patientNameHE = GetDocEntry (0x0010, 0x0010);
1213 if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1215 std::string studyInstanceUID = GetEntry (0x0020, 0x000d);
1216 if ( studyInstanceUID != GDCM_UNFOUND )
1218 ReplaceOrCreate(studyInstanceUID, 0x0010, 0x0010);
1222 ReplaceOrCreate("anonymised", 0x0010, 0x0010);
1227 // (if any) remove or replace all the stuff that contains a Date
1229 //0008 0012 DA ID Instance Creation Date
1230 //0008 0020 DA ID Study Date
1231 //0008 0021 DA ID Series Date
1232 //0008 0022 DA ID Acquisition Date
1233 //0008 0023 DA ID Content Date
1234 //0008 0024 DA ID Overlay Date
1235 //0008 0025 DA ID Curve Date
1236 //0008 002a DT ID Acquisition Datetime
1237 //0018 9074 DT ACQ Frame Acquisition Datetime
1238 //0018 9151 DT ACQ Frame Reference Datetime
1239 //0018 a002 DT ACQ Contribution Date Time
1240 //0020 3403 SH REL Modified Image Date (RET)
1241 //0032 0032 DA SDY Study Verified Date
1242 //0032 0034 DA SDY Study Read Date
1243 //0032 1000 DA SDY Scheduled Study Start Date
1244 //0032 1010 DA SDY Scheduled Study Stop Date
1245 //0032 1040 DA SDY Study Arrival Date
1246 //0032 1050 DA SDY Study Completion Date
1247 //0038 001a DA VIS Scheduled Admission Date
1248 //0038 001c DA VIS Scheduled Discharge Date
1249 //0038 0020 DA VIS Admitting Date
1250 //0038 0030 DA VIS Discharge Date
1251 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1252 //0040 0004 DA PRC Scheduled Procedure Step End Date
1253 //0040 0244 DA PRC Performed Procedure Step Start Date
1254 //0040 0250 DA PRC Performed Procedure Step End Date
1255 //0040 2004 DA PRC Issue Date of Imaging Service Request
1256 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1257 //0040 4011 DT PRC Expected Completion Date and Time
1258 //0040 a030 DT PRC Verification Date Time
1259 //0040 a032 DT PRC Observation Date Time
1260 //0040 a120 DT PRC DateTime
1261 //0040 a121 DA PRC Date
1262 //0040 a13a DT PRC Referenced Datetime
1263 //0070 0082 DA ??? Presentation Creation Date
1264 //0100 0420 DT ??? SOP Autorization Date and Time
1265 //0400 0105 DT ??? Digital Signature DateTime
1266 //2100 0040 DA PJ Creation Date
1267 //3006 0008 DA SSET Structure Set Date
1268 //3008 0024 DA ??? Treatment Control Point Date
1269 //3008 0054 DA ??? First Treatment Date
1270 //3008 0056 DA ??? Most Recent Treatment Date
1271 //3008 0162 DA ??? Safe Position Exit Date
1272 //3008 0166 DA ??? Safe Position Return Date
1273 //3008 0250 DA ??? Treatment Date
1274 //300a 0006 DA RT RT Plan Date
1275 //300a 022c DA RT Air Kerma Rate Reference Date
1276 //300e 0004 DA RT Review Date
1282 * \brief gets the info from 0020,0037 : Image Orientation Patient
1283 * @param iop adress of the (6)float aray to receive values
1284 * @return cosines of image orientation patient
1286 void Header::GetImageOrientationPatient( float iop[6] )
1288 std::string strImOriPat;
1289 //iop is supposed to be float[6]
1290 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1292 // 0020 0037 DS REL Image Orientation (Patient)
1293 if ( (strImOriPat = GetEntry(0x0020,0x0037)) != GDCM_UNFOUND )
1295 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1296 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1298 gdcmVerboseMacro( "Wrong Image Orientation Patient (0020,0037). Less than 6 values were found." );
1302 // 0020 0035 DS REL Image Orientation (RET)
1303 else if ( (strImOriPat = GetEntry(0x0020,0x0035)) != GDCM_UNFOUND )
1305 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1306 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1308 gdcmVerboseMacro( "wrong Image Orientation Patient (0020,0035). Less than 6 values were found." );
1314 * \brief Initialize a default DICOM header that should contain all the
1315 * field require by other reader. DICOM standard does not
1316 * explicitely defines those fields, heuristic has been choosen.
1317 * This is not perfect as we are writting a CT image...
1319 void Header::InitializeDefaultHeader()
1326 } DICOM_DEFAULT_VALUE;
1328 std::string date = Util::GetCurrentDate();
1329 std::string time = Util::GetCurrentTime();
1330 std::string uid = Util::CreateUniqueUID();
1331 std::string uidMedia = uid;
1332 std::string uidClass = uid + ".1";
1333 std::string uidInst = uid + ".10";
1334 std::string uidStudy = uid + ".100";
1335 std::string uidSerie = uid + ".1000";
1337 static DICOM_DEFAULT_VALUE defaultvalue[] = {
1338 { "146 ", 0x0002, 0x0000}, // Meta Element Group Length // FIXME: how to recompute ?
1339 { "1.2.840.10008.5.1.4.1.1.2", 0x0002, 0x0002}, // Media Storage SOP Class UID (CT Image Storage)
1340 { uidClass.c_str(), 0x0002, 0x0003}, // Media Storage SOP Instance UID
1341 { uidClass.c_str(), 0x0002, 0x0012}, // META Implementation Class UID
1342 { "GDCM", 0x0002, 0x0016}, // Source Application Entity Title
1344 { date.c_str(), 0x0008, 0x0012}, // Instance Creation Date
1345 { time.c_str(), 0x0008, 0x0013}, // Instance Creation Time
1346 { "1.2.840.10008.5.1.4.1.1.2", 0x0008, 0x0016}, // SOP Class UID
1347 { uidInst.c_str(), 0x0008, 0x0018}, // SOP Instance UID
1348 { "CT", 0x0008, 0x0060}, // Modality
1349 { "GDCM", 0x0008, 0x0070}, // Manufacturer
1350 { "GDCM", 0x0008, 0x0080}, // Institution Name
1351 { "http://www-creatis.insa-lyon.fr/Public/Gdcm", 0x0008, 0x0081}, // Institution Address
1353 { "GDCM", 0x0010, 0x0010}, // Patient's Name
1354 { "GDCMID", 0x0010, 0x0020}, // Patient ID
1356 { uidStudy.c_str(), 0x0020, 0x000d}, // Study Instance UID
1357 { uidSerie.c_str(), 0x0020, 0x000e}, // Series Instance UID
1358 { "1", 0x0020, 0x0010}, // StudyID
1359 { "1", 0x0020, 0x0011}, // SeriesNumber
1361 { "1", 0x0028, 0x0002}, // Samples per pixel 1 or 3
1362 { "MONOCHROME1", 0x0028, 0x0004}, // photochromatic interpretation
1363 { "0", 0x0028, 0x0010}, // nbRows
1364 { "0", 0x0028, 0x0011}, // nbCols
1365 { "8", 0x0028, 0x0100}, // BitsAllocated 8 or 12 or 16
1366 { "8", 0x0028, 0x0101}, // BitsStored <= BitsAllocated
1367 { "7", 0x0028, 0x0102}, // HighBit <= BitsAllocated - 1
1368 { "0", 0x0028, 0x0103}, // Pixel Representation 0(unsigned) or 1(signed)
1373 // Special case this is the image (not a string)
1376 ReplaceOrCreate(0, 0, GrPixel, NumPixel);
1378 // All remaining strings:
1380 DICOM_DEFAULT_VALUE current = defaultvalue[i];
1381 while( current.value )
1383 ReplaceOrCreate(current.value, current.group, current.elem);
1384 current = defaultvalue[++i];
1389 //-----------------------------------------------------------------------------
1392 //-----------------------------------------------------------------------------
1394 } // end namespace gdcm