1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2005/01/18 16:23:52 $
7 Version: $Revision: 1.234 $
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 endianness.
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);
200 #ifdef GDCM_WORDS_BIGENDIAN
201 // Super Super hack that will make gdcm a BOMB ! but should
202 // Fix temporarily the dashboard
203 BinEntry *b = GetBinEntry(GrPixel,NumPixel);
204 if ( GetPixelSize() == 16 )
206 uint16_t *im16 = (uint16_t*)b->GetBinArea();
207 int lgr = b->GetLength();
208 for( int i = 0; i < lgr / 2; i++ )
210 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
213 #endif //GDCM_WORDS_BIGENDIAN
216 Document::WriteContent(fp,filetype);
219 #ifdef GDCM_WORDS_BIGENDIAN
220 // Flip back the pixel ... I told you this is a hack
221 if ( GetPixelSize() == 16 )
223 uint16_t *im16 = (uint16_t*)b->GetBinArea();
224 int lgr = b->GetLength();
225 for( int i = 0; i < lgr / 2; i++ )
227 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
230 #endif //GDCM_WORDS_BIGENDIAN
239 //-----------------------------------------------------------------------------
243 //-----------------------------------------------------------------------------
247 * \brief This predicate, based on hopefully reasonable heuristics,
248 * decides whether or not the current Header was properly parsed
249 * and contains the mandatory information for being considered as
250 * a well formed and usable Dicom/Acr File.
251 * @return true when Header is the one of a reasonable Dicom/Acr file,
254 bool Header::IsReadable()
256 if( !Document::IsReadable() )
261 const std::string &res = GetEntry(0x0028, 0x0005);
262 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
264 return false; // Image Dimensions
266 if ( !GetDocEntry(0x0028, 0x0100) )
268 return false; // "Bits Allocated"
270 if ( !GetDocEntry(0x0028, 0x0101) )
272 return false; // "Bits Stored"
274 if ( !GetDocEntry(0x0028, 0x0102) )
276 return false; // "High Bit"
278 if ( !GetDocEntry(0x0028, 0x0103) )
280 return false; // "Pixel Representation" i.e. 'Sign'
287 * \brief Retrieve the number of columns of image.
288 * @return The encountered size when found, 0 by default.
289 * 0 means the file is NOT USABLE. The caller will have to check
291 int Header::GetXSize()
293 const std::string &strSize = GetEntry(0x0028,0x0011);
294 if ( strSize == GDCM_UNFOUND )
299 return atoi( strSize.c_str() );
303 * \brief Retrieve the number of lines of image.
304 * \warning The defaulted value is 1 as opposed to Header::GetXSize()
305 * @return The encountered size when found, 1 by default
306 * (The ACR-NEMA file contains a Signal, not an Image).
308 int Header::GetYSize()
310 const std::string &strSize = GetEntry(0x0028,0x0010);
311 if ( strSize != GDCM_UNFOUND )
313 return atoi( strSize.c_str() );
320 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
321 // hence be a signal (1D image). So we default to 1:
326 * \brief Retrieve the number of planes of volume or the number
327 * of frames of a multiframe.
328 * \warning When present we consider the "Number of Frames" as the third
329 * dimension. When Missing we consider the third dimension as
330 * being the ACR-NEMA "Planes" tag content.
331 * @return The encountered size when found, 1 by default (single image).
333 int Header::GetZSize()
335 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
336 // as the third dimension.
337 const std::string &strSize = GetEntry(0x0028,0x0008);
338 if ( strSize != GDCM_UNFOUND )
340 return atoi( strSize.c_str() );
343 // We then consider the "Planes" entry as the third dimension
344 const std::string &strSize2 = GetEntry(0x0028,0x0012);
345 if ( strSize2 != GDCM_UNFOUND )
347 return atoi( strSize2.c_str() );
354 * \brief gets the info from 0028,0030 : Pixel Spacing
356 * @return X dimension of a pixel
358 float Header::GetXSpacing()
360 float xspacing, yspacing;
361 const std::string &strSpacing = GetEntry(0x0028,0x0030);
363 if ( strSpacing == GDCM_UNFOUND )
365 gdcmVerboseMacro( "Unfound Pixel Spacing (0028,0030)" );
370 if( ( nbValues = sscanf( strSpacing.c_str(),
371 "%f\\%f", &yspacing, &xspacing)) != 2 )
373 // if single value is found, xspacing is defaulted to yspacing
379 if ( xspacing == 0.0 )
386 // to avoid troubles with David Clunie's-like images
387 if ( xspacing == 0. && yspacing == 0.)
392 gdcmVerboseMacro("gdcmData/CT-MONO2-8-abdo.dcm problem");
393 // seems to be a bug in the header ...
394 nbValues = sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
395 gdcmAssertMacro( nbValues == 2 );
402 * \brief gets the info from 0028,0030 : Pixel Spacing
404 * @return Y dimension of a pixel
406 float Header::GetYSpacing()
409 std::string strSpacing = GetEntry(0x0028,0x0030);
411 if ( strSpacing == GDCM_UNFOUND )
413 gdcmVerboseMacro("Unfound Pixel Spacing (0028,0030)");
417 // if sscanf cannot read any float value, it won't affect yspacing
418 sscanf( strSpacing.c_str(), "%f", &yspacing);
420 if ( yspacing == 0.0 )
427 * \brief gets the info from 0018,0088 : Space Between Slices
428 * else from 0018,0050 : Slice Thickness
430 * @return Z dimension of a voxel-to be
432 float Header::GetZSpacing()
434 // Spacing Between Slices : distance entre le milieu de chaque coupe
435 // Les coupes peuvent etre :
436 // jointives (Spacing between Slices = Slice Thickness)
437 // chevauchantes (Spacing between Slices < Slice Thickness)
438 // disjointes (Spacing between Slices > Slice Thickness)
439 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
440 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
441 // Si le Spacing Between Slices est Missing,
442 // on suppose que les coupes sont jointives
444 const std::string &strSpacingBSlices = GetEntry(0x0018,0x0088);
446 if ( strSpacingBSlices == GDCM_UNFOUND )
448 gdcmVerboseMacro("Unfound Spacing Between Slices (0018,0088)");
449 const std::string &strSliceThickness = GetEntry(0x0018,0x0050);
450 if ( strSliceThickness == GDCM_UNFOUND )
452 gdcmVerboseMacro("Unfound Slice Thickness (0018,0050)");
457 // if no 'Spacing Between Slices' is found,
458 // we assume slices join together
459 // (no overlapping, no interslice gap)
460 // if they don't, we're fucked up
461 return (float)atof( strSliceThickness.c_str() );
465 return (float)atof( strSpacingBSlices.c_str() );
469 *\brief gets the info from 0028,1052 : Rescale Intercept
470 * @return Rescale Intercept
472 float Header::GetRescaleIntercept()
475 /// 0028 1052 DS IMG Rescale Intercept
476 const std::string &strRescInter = GetEntry(0x0028,0x1052);
477 if ( strRescInter != GDCM_UNFOUND )
479 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
481 // bug in the element 0x0028,0x1052
482 gdcmVerboseMacro( "Rescale Intercept (0028,1052) is empty." );
490 *\brief gets the info from 0028,1053 : Rescale Slope
491 * @return Rescale Slope
493 float Header::GetRescaleSlope()
496 //0028 1053 DS IMG Rescale Slope
497 std::string strRescSlope = GetEntry(0x0028,0x1053);
498 if ( strRescSlope != GDCM_UNFOUND )
500 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
502 // bug in the element 0x0028,0x1053
503 gdcmVerboseMacro( "Rescale Slope (0028,1053) is empty.");
511 * \brief This function is intended to user who doesn't want
512 * to have to manage a LUT and expects to get an RBG Pixel image
513 * (or a monochrome one ...)
514 * \warning to be used with GetImagePixels()
515 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
517 int Header::GetNumberOfScalarComponents()
519 if ( GetSamplesPerPixel() == 3 )
524 // 0028 0100 US IMG Bits Allocated
525 // (in order no to be messed up by old RGB images)
526 if ( GetEntry(0x0028,0x0100) == "24" )
531 std::string strPhotometricInterpretation = GetEntry(0x0028,0x0004);
533 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
535 if ( HasLUT() )// PALETTE COLOR is NOT enough
545 // beware of trailing space at end of string
546 // DICOM tags are never of odd length
547 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
548 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
549 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
555 // we assume that *all* kinds of YBR are dealt with
561 * \brief This function is intended to user that DOESN'T want
562 * to get RGB pixels image when it's stored as a PALETTE COLOR image
563 * - the (vtk) user is supposed to know how deal with LUTs -
564 * \warning to be used with GetImagePixelsRaw()
565 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
567 int Header::GetNumberOfScalarComponentsRaw()
569 // 0028 0100 US IMG Bits Allocated
570 // (in order no to be messed up by old RGB images)
571 if ( Header::GetEntry(0x0028,0x0100) == "24" )
576 // we assume that *all* kinds of YBR are dealt with
577 return GetSamplesPerPixel();
581 // -------------- Remember ! ----------------------------------
583 // Image Position Patient (0020,0032):
584 // If not found (ACR_NEMA) we try Image Position (0020,0030)
585 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
586 // or Location (0020,0050)
587 // as the Z coordinate,
588 // 0. for all the coordinates if nothing is found
590 // \todo find a way to inform the caller nothing was found
591 // \todo How to tell the caller a wrong number of values was found?
593 // ---------------------------------------------------------------
597 * \brief gets the info from 0020,0032 : Image Position Patient
598 * else from 0020,0030 : Image Position (RET)
600 * @return up-left image corner X position
602 float Header::GetXOrigin()
604 float xImPos, yImPos, zImPos;
605 std::string strImPos = GetEntry(0x0020,0x0032);
607 if ( strImPos == GDCM_UNFOUND )
609 gdcmVerboseMacro( "Unfound Image Position Patient (0020,0032)");
610 strImPos = GetEntry(0x0020,0x0030); // For ACR-NEMA images
611 if ( strImPos == GDCM_UNFOUND )
613 gdcmVerboseMacro( "Unfound Image Position (RET) (0020,0030)");
614 /// \todo How to tell the caller nothing was found ?
619 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
628 * \brief gets the info from 0020,0032 : Image Position Patient
629 * else from 0020,0030 : Image Position (RET)
631 * @return up-left image corner Y position
633 float Header::GetYOrigin()
635 float xImPos, yImPos, zImPos;
636 std::string strImPos = GetEntry(0x0020,0x0032);
638 if ( strImPos == GDCM_UNFOUND)
640 gdcmVerboseMacro( "Unfound Image Position Patient (0020,0032)");
641 strImPos = GetEntry(0x0020,0x0030); // For ACR-NEMA images
642 if ( strImPos == GDCM_UNFOUND )
644 gdcmVerboseMacro( "Unfound Image Position (RET) (0020,0030)");
645 /// \todo How to tell the caller nothing was found ?
650 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
659 * \brief gets the info from 0020,0032 : Image Position Patient
660 * else from 0020,0030 : Image Position (RET)
661 * else from 0020,1041 : Slice Location
662 * else from 0020,0050 : Location
664 * @return up-left image corner Z position
666 float Header::GetZOrigin()
668 float xImPos, yImPos, zImPos;
669 std::string strImPos = GetEntry(0x0020,0x0032);
671 if ( strImPos != GDCM_UNFOUND )
673 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
675 gdcmVerboseMacro( "Wrong Image Position Patient (0020,0032)");
676 return 0.; // bug in the element 0x0020,0x0032
684 strImPos = GetEntry(0x0020,0x0030); // For ACR-NEMA images
685 if ( strImPos != GDCM_UNFOUND )
687 if( sscanf( strImPos.c_str(),
688 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
690 gdcmVerboseMacro( "Wrong Image Position (RET) (0020,0030)");
691 return 0.; // bug in the element 0x0020,0x0032
699 std::string strSliceLocation = GetEntry(0x0020,0x1041); // for *very* old ACR-NEMA images
700 if ( strSliceLocation != GDCM_UNFOUND )
702 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
704 gdcmVerboseMacro( "Wrong Slice Location (0020,1041)");
705 return 0.; // bug in the element 0x0020,0x1041
712 gdcmVerboseMacro( "Unfound Slice Location (0020,1041)");
714 std::string strLocation = GetEntry(0x0020,0x0050);
715 if ( strLocation != GDCM_UNFOUND )
717 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
719 gdcmVerboseMacro( "Wrong Location (0020,0050)");
720 return 0.; // bug in the element 0x0020,0x0050
727 gdcmVerboseMacro( "Unfound Location (0020,0050)");
729 return 0.; // Hopeless
733 * \brief gets the info from 0020,0013 : Image Number else 0.
734 * @return image number
736 int Header::GetImageNumber()
738 // The function i atoi() takes the address of an area of memory as
739 // parameter and converts the string stored at that location to an integer
740 // using the external decimal to internal binary conversion rules. This may
741 // be preferable to sscanf() since atoi() is a much smaller, simpler and
742 // faster function. sscanf() can do all possible conversions whereas
743 // atoi() can only do single decimal integer conversions.
744 //0020 0013 IS REL Image Number
745 std::string strImNumber = GetEntry(0x0020,0x0013);
746 if ( strImNumber != GDCM_UNFOUND )
748 return atoi( strImNumber.c_str() );
754 * \brief gets the info from 0008,0060 : Modality
755 * @return Modality Type
757 ModalityType Header::GetModality()
759 // 0008 0060 CS ID Modality
760 std::string strModality = GetEntry(0x0008,0x0060);
761 if ( strModality != GDCM_UNFOUND )
763 if ( strModality.find("AU") < strModality.length()) return AU;
764 else if ( strModality.find("AS") < strModality.length()) return AS;
765 else if ( strModality.find("BI") < strModality.length()) return BI;
766 else if ( strModality.find("CF") < strModality.length()) return CF;
767 else if ( strModality.find("CP") < strModality.length()) return CP;
768 else if ( strModality.find("CR") < strModality.length()) return CR;
769 else if ( strModality.find("CT") < strModality.length()) return CT;
770 else if ( strModality.find("CS") < strModality.length()) return CS;
771 else if ( strModality.find("DD") < strModality.length()) return DD;
772 else if ( strModality.find("DF") < strModality.length()) return DF;
773 else if ( strModality.find("DG") < strModality.length()) return DG;
774 else if ( strModality.find("DM") < strModality.length()) return DM;
775 else if ( strModality.find("DS") < strModality.length()) return DS;
776 else if ( strModality.find("DX") < strModality.length()) return DX;
777 else if ( strModality.find("ECG") < strModality.length()) return ECG;
778 else if ( strModality.find("EPS") < strModality.length()) return EPS;
779 else if ( strModality.find("FA") < strModality.length()) return FA;
780 else if ( strModality.find("FS") < strModality.length()) return FS;
781 else if ( strModality.find("HC") < strModality.length()) return HC;
782 else if ( strModality.find("HD") < strModality.length()) return HD;
783 else if ( strModality.find("LP") < strModality.length()) return LP;
784 else if ( strModality.find("LS") < strModality.length()) return LS;
785 else if ( strModality.find("MA") < strModality.length()) return MA;
786 else if ( strModality.find("MR") < strModality.length()) return MR;
787 else if ( strModality.find("NM") < strModality.length()) return NM;
788 else if ( strModality.find("OT") < strModality.length()) return OT;
789 else if ( strModality.find("PT") < strModality.length()) return PT;
790 else if ( strModality.find("RF") < strModality.length()) return RF;
791 else if ( strModality.find("RG") < strModality.length()) return RG;
792 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
793 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
794 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
795 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
796 else if ( strModality.find("SM") < strModality.length()) return SM;
797 else if ( strModality.find("ST") < strModality.length()) return ST;
798 else if ( strModality.find("TG") < strModality.length()) return TG;
799 else if ( strModality.find("US") < strModality.length()) return US;
800 else if ( strModality.find("VF") < strModality.length()) return VF;
801 else if ( strModality.find("XA") < strModality.length()) return XA;
802 else if ( strModality.find("XC") < strModality.length()) return XC;
806 /// \todo throw error return value ???
807 /// specified <> unknown in our database
816 * \brief Retrieve the number of Bits Stored (actually used)
817 * (as opposite to number of Bits Allocated)
818 * @return The encountered number of Bits Stored, 0 by default.
819 * 0 means the file is NOT USABLE. The caller has to check it !
821 int Header::GetBitsStored()
823 std::string strSize = GetEntry( 0x0028, 0x0101 );
824 if ( strSize == GDCM_UNFOUND )
826 gdcmVerboseMacro("(0028,0101) is supposed to be mandatory");
827 return 0; // It's supposed to be mandatory
828 // the caller will have to check
830 return atoi( strSize.c_str() );
834 * \brief Retrieve the high bit position.
835 * \warning The method defaults to 0 when information is Missing.
836 * The responsability of checking this value is left to the caller.
837 * @return The high bit positin when present. 0 when Missing.
839 int Header::GetHighBitPosition()
841 std::string strSize = GetEntry( 0x0028, 0x0102 );
842 if ( strSize == GDCM_UNFOUND )
844 gdcmVerboseMacro( "(0028,0102) is supposed to be mandatory");
847 return atoi( strSize.c_str() );
851 * \brief Check whether the pixels are signed or UNsigned data.
852 * \warning The method defaults to false (UNsigned) when information is Missing.
853 * The responsability of checking this value is left to the caller.
854 * @return True when signed, false when UNsigned
856 bool Header::IsSignedPixelData()
858 std::string strSize = GetEntry( 0x0028, 0x0103 );
859 if ( strSize == GDCM_UNFOUND )
861 gdcmVerboseMacro( "(0028,0103) is supposed to be mandatory");
864 int sign = atoi( strSize.c_str() );
873 * \brief Retrieve the number of Bits Allocated
874 * (8, 12 -compacted ACR-NEMA files, 16, ...)
875 * @return The encountered number of Bits Allocated, 0 by default.
876 * 0 means the file is NOT USABLE. The caller has to check it !
878 int Header::GetBitsAllocated()
880 std::string strSize = GetEntry(0x0028,0x0100);
881 if ( strSize == GDCM_UNFOUND )
883 gdcmVerboseMacro( "(0028,0100) is supposed to be mandatory");
884 return 0; // It's supposed to be mandatory
885 // the caller will have to check
887 return atoi( strSize.c_str() );
891 * \brief Retrieve the number of Samples Per Pixel
892 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
893 * @return The encountered number of Samples Per Pixel, 1 by default.
894 * (Gray level Pixels)
896 int Header::GetSamplesPerPixel()
898 const std::string& strSize = GetEntry(0x0028,0x0002);
899 if ( strSize == GDCM_UNFOUND )
901 gdcmVerboseMacro( "(0028,0002) is supposed to be mandatory");
902 return 1; // Well, it's supposed to be mandatory ...
903 // but sometimes it's missing : *we* assume Gray pixels
905 return atoi( strSize.c_str() );
909 * \brief Check whether this a monochrome picture or not by accessing
910 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
911 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
913 bool Header::IsMonochrome()
915 const std::string& PhotometricInterp = GetEntry( 0x0028, 0x0004 );
916 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
917 || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
921 if ( PhotometricInterp == GDCM_UNFOUND )
923 gdcmVerboseMacro( "Not found : Photometric Interpretation (0028,0004)");
929 * \brief Check whether this a "PALETTE COLOR" picture or not by accessing
930 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
931 * @return true when "PALETTE COLOR". False otherwise.
933 bool Header::IsPaletteColor()
935 std::string PhotometricInterp = GetEntry( 0x0028, 0x0004 );
936 if ( PhotometricInterp == "PALETTE COLOR " )
940 if ( PhotometricInterp == GDCM_UNFOUND )
942 gdcmVerboseMacro( "Not found : Palette color (0028,0004)");
948 * \brief Check whether this a "YBR_FULL" color picture or not by accessing
949 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
950 * @return true when "YBR_FULL". False otherwise.
952 bool Header::IsYBRFull()
954 std::string PhotometricInterp = GetEntry( 0x0028, 0x0004 );
955 if ( PhotometricInterp == "YBR_FULL" )
959 if ( PhotometricInterp == GDCM_UNFOUND )
961 gdcmVerboseMacro( "Not found : YBR Full (0028,0004)");
967 * \brief Retrieve the Planar Configuration for RGB images
968 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
969 * @return The encountered Planar Configuration, 0 by default.
971 int Header::GetPlanarConfiguration()
973 std::string strSize = GetEntry(0x0028,0x0006);
974 if ( strSize == GDCM_UNFOUND )
976 gdcmVerboseMacro( "Not found : Planar Configuration (0028,0006)");
979 return atoi( strSize.c_str() );
983 * \brief Return the size (in bytes) of a single pixel of data.
984 * @return The size in bytes of a single pixel of data; 0 by default
985 * 0 means the file is NOT USABLE; the caller will have to check
987 int Header::GetPixelSize()
989 // 0028 0100 US IMG Bits Allocated
990 // (in order no to be messed up by old RGB images)
991 // if (Header::GetEntry(0x0028,0x0100) == "24")
994 std::string pixelType = GetPixelType();
995 if ( pixelType == "8U" || pixelType == "8S" )
999 if ( pixelType == "16U" || pixelType == "16S")
1003 if ( pixelType == "32U" || pixelType == "32S")
1007 if ( pixelType == "FD" )
1011 gdcmVerboseMacro( "Unknown pixel type");
1016 * \brief Build the Pixel Type of the image.
1017 * Possible values are:
1018 * - 8U unsigned 8 bit,
1019 * - 8S signed 8 bit,
1020 * - 16U unsigned 16 bit,
1021 * - 16S signed 16 bit,
1022 * - 32U unsigned 32 bit,
1023 * - 32S signed 32 bit,
1024 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
1025 * \warning 12 bit images appear as 16 bit.
1026 * 24 bit images appear as 8 bit
1027 * @return 0S if nothing found. NOT USABLE file. The caller has to check
1029 std::string Header::GetPixelType()
1031 std::string bitsAlloc = GetEntry(0x0028, 0x0100); // Bits Allocated
1032 if ( bitsAlloc == GDCM_UNFOUND )
1034 gdcmVerboseMacro( "Missing Bits Allocated (0028,0100)");
1035 bitsAlloc = "16"; // default and arbitrary value, not to polute the output
1038 if ( bitsAlloc == "64" )
1042 else if ( bitsAlloc == "12" )
1044 // It will be unpacked
1047 else if ( bitsAlloc == "24" )
1049 // (in order no to be messed up
1050 bitsAlloc = "8"; // by old RGB images)
1053 std::string sign = GetEntry(0x0028, 0x0103);//"Pixel Representation"
1055 if (sign == GDCM_UNFOUND )
1057 gdcmVerboseMacro( "Missing Pixel Representation (0028,0103)");
1058 sign = "U"; // default and arbitrary value, not to polute the output
1060 else if ( sign == "0" )
1068 return bitsAlloc + sign;
1073 * \brief Recover the offset (from the beginning of the file)
1074 * of *image* pixels (not *icone image* pixels, if any !)
1075 * @return Pixel Offset
1077 size_t Header::GetPixelOffset()
1079 DocEntry* pxlElement = GetDocEntry(GrPixel,NumPixel);
1082 return pxlElement->GetOffset();
1087 std::cout << "Big trouble : Pixel Element ("
1088 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1095 /// \todo TODO : unify those two (previous one and next one)
1097 * \brief Recover the pixel area length (in Bytes)
1098 * @return Pixel Element Length, as stored in the header
1099 * (NOT the memory space necessary to hold the Pixels
1100 * -in case of embeded compressed image-)
1101 * 0 : NOT USABLE file. The caller has to check.
1103 size_t Header::GetPixelAreaLength()
1105 DocEntry* pxlElement = GetDocEntry(GrPixel,NumPixel);
1108 return pxlElement->GetLength();
1113 std::cout << "Big trouble : Pixel Element ("
1114 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1122 * \brief tells us if LUT are used
1123 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1124 * are NOT considered as LUT, since nobody knows
1125 * how to deal with them
1126 * Please warn me if you know sbdy that *does* know ... jprx
1127 * @return true if LUT Descriptors and LUT Tables were found
1129 bool Header::HasLUT()
1131 // Check the presence of the LUT Descriptors, and LUT Tables
1133 if ( !GetDocEntry(0x0028,0x1101) )
1137 // LutDescriptorGreen
1138 if ( !GetDocEntry(0x0028,0x1102) )
1142 // LutDescriptorBlue
1143 if ( !GetDocEntry(0x0028,0x1103) )
1147 // Red Palette Color Lookup Table Data
1148 if ( !GetDocEntry(0x0028,0x1201) )
1152 // Green Palette Color Lookup Table Data
1153 if ( !GetDocEntry(0x0028,0x1202) )
1157 // Blue Palette Color Lookup Table Data
1158 if ( !GetDocEntry(0x0028,0x1203) )
1163 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1164 // NOT taken into account, but we don't know how to use it ...
1169 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1171 * @return Lookup Table number of Bits , 0 by default
1172 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1173 * @ return bit number of each LUT item
1175 int Header::GetLUTNbits()
1177 std::vector<std::string> tokens;
1180 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1181 // = Lookup Table Desc-Blue
1182 // Consistency already checked in GetLUTLength
1183 std::string lutDescription = GetEntry(0x0028,0x1101);
1184 if ( lutDescription == GDCM_UNFOUND )
1189 tokens.clear(); // clean any previous value
1190 Util::Tokenize ( lutDescription, tokens, "\\" );
1191 //LutLength=atoi(tokens[0].c_str());
1192 //LutDepth=atoi(tokens[1].c_str());
1194 lutNbits = atoi( tokens[2].c_str() );
1201 //-----------------------------------------------------------------------------
1205 * \brief anonymize a Header (removes Patient's personal info)
1206 * (read the code to see which ones ...)
1208 bool Header::AnonymizeHeader()
1210 // If exist, replace by spaces
1211 SetEntry (" ",0x0010, 0x2154); // Telephone
1212 SetEntry (" ",0x0010, 0x1040); // Adress
1213 SetEntry (" ",0x0010, 0x0020); // Patient ID
1215 DocEntry* patientNameHE = GetDocEntry (0x0010, 0x0010);
1217 if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1219 std::string studyInstanceUID = GetEntry (0x0020, 0x000d);
1220 if ( studyInstanceUID != GDCM_UNFOUND )
1222 ReplaceOrCreate(studyInstanceUID, 0x0010, 0x0010);
1226 ReplaceOrCreate("anonymised", 0x0010, 0x0010);
1231 // (if any) remove or replace all the stuff that contains a Date
1233 //0008 0012 DA ID Instance Creation Date
1234 //0008 0020 DA ID Study Date
1235 //0008 0021 DA ID Series Date
1236 //0008 0022 DA ID Acquisition Date
1237 //0008 0023 DA ID Content Date
1238 //0008 0024 DA ID Overlay Date
1239 //0008 0025 DA ID Curve Date
1240 //0008 002a DT ID Acquisition Datetime
1241 //0018 9074 DT ACQ Frame Acquisition Datetime
1242 //0018 9151 DT ACQ Frame Reference Datetime
1243 //0018 a002 DT ACQ Contribution Date Time
1244 //0020 3403 SH REL Modified Image Date (RET)
1245 //0032 0032 DA SDY Study Verified Date
1246 //0032 0034 DA SDY Study Read Date
1247 //0032 1000 DA SDY Scheduled Study Start Date
1248 //0032 1010 DA SDY Scheduled Study Stop Date
1249 //0032 1040 DA SDY Study Arrival Date
1250 //0032 1050 DA SDY Study Completion Date
1251 //0038 001a DA VIS Scheduled Admission Date
1252 //0038 001c DA VIS Scheduled Discharge Date
1253 //0038 0020 DA VIS Admitting Date
1254 //0038 0030 DA VIS Discharge Date
1255 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1256 //0040 0004 DA PRC Scheduled Procedure Step End Date
1257 //0040 0244 DA PRC Performed Procedure Step Start Date
1258 //0040 0250 DA PRC Performed Procedure Step End Date
1259 //0040 2004 DA PRC Issue Date of Imaging Service Request
1260 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1261 //0040 4011 DT PRC Expected Completion Date and Time
1262 //0040 a030 DT PRC Verification Date Time
1263 //0040 a032 DT PRC Observation Date Time
1264 //0040 a120 DT PRC DateTime
1265 //0040 a121 DA PRC Date
1266 //0040 a13a DT PRC Referenced Datetime
1267 //0070 0082 DA ??? Presentation Creation Date
1268 //0100 0420 DT ??? SOP Autorization Date and Time
1269 //0400 0105 DT ??? Digital Signature DateTime
1270 //2100 0040 DA PJ Creation Date
1271 //3006 0008 DA SSET Structure Set Date
1272 //3008 0024 DA ??? Treatment Control Point Date
1273 //3008 0054 DA ??? First Treatment Date
1274 //3008 0056 DA ??? Most Recent Treatment Date
1275 //3008 0162 DA ??? Safe Position Exit Date
1276 //3008 0166 DA ??? Safe Position Return Date
1277 //3008 0250 DA ??? Treatment Date
1278 //300a 0006 DA RT RT Plan Date
1279 //300a 022c DA RT Air Kerma Rate Reference Date
1280 //300e 0004 DA RT Review Date
1286 * \brief gets the info from 0020,0037 : Image Orientation Patient
1287 * @param iop adress of the (6)float aray to receive values
1288 * @return cosines of image orientation patient
1290 void Header::GetImageOrientationPatient( float iop[6] )
1292 std::string strImOriPat;
1293 //iop is supposed to be float[6]
1294 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1296 // 0020 0037 DS REL Image Orientation (Patient)
1297 if ( (strImOriPat = GetEntry(0x0020,0x0037)) != GDCM_UNFOUND )
1299 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1300 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1302 gdcmVerboseMacro( "Wrong Image Orientation Patient (0020,0037). Less than 6 values were found." );
1306 // 0020 0035 DS REL Image Orientation (RET)
1307 else if ( (strImOriPat = GetEntry(0x0020,0x0035)) != GDCM_UNFOUND )
1309 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1310 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1312 gdcmVerboseMacro( "wrong Image Orientation Patient (0020,0035). Less than 6 values were found." );
1318 * \brief Initialize a default DICOM header that should contain all the
1319 * field require by other reader. DICOM standard does not
1320 * explicitely defines those fields, heuristic has been choosen.
1321 * This is not perfect as we are writting a CT image...
1323 void Header::InitializeDefaultHeader()
1330 } DICOM_DEFAULT_VALUE;
1332 std::string date = Util::GetCurrentDate();
1333 std::string time = Util::GetCurrentTime();
1334 std::string uid = Util::CreateUniqueUID();
1335 std::string uidMedia = uid;
1336 std::string uidClass = uid + ".1";
1337 std::string uidInst = uid + ".10";
1338 std::string uidStudy = uid + ".100";
1339 std::string uidSerie = uid + ".1000";
1341 static DICOM_DEFAULT_VALUE defaultvalue[] = {
1342 { "146 ", 0x0002, 0x0000}, // Meta Element Group Length // FIXME: how to recompute ?
1343 { "1.2.840.10008.5.1.4.1.1.2", 0x0002, 0x0002}, // Media Storage SOP Class UID (CT Image Storage)
1344 { uidClass.c_str(), 0x0002, 0x0003}, // Media Storage SOP Instance UID
1345 { uidClass.c_str(), 0x0002, 0x0012}, // META Implementation Class UID
1346 { "GDCM", 0x0002, 0x0016}, // Source Application Entity Title
1348 { date.c_str(), 0x0008, 0x0012}, // Instance Creation Date
1349 { time.c_str(), 0x0008, 0x0013}, // Instance Creation Time
1350 { "1.2.840.10008.5.1.4.1.1.2", 0x0008, 0x0016}, // SOP Class UID
1351 { uidInst.c_str(), 0x0008, 0x0018}, // SOP Instance UID
1352 { "CT", 0x0008, 0x0060}, // Modality
1353 { "GDCM", 0x0008, 0x0070}, // Manufacturer
1354 { "GDCM", 0x0008, 0x0080}, // Institution Name
1355 { "http://www-creatis.insa-lyon.fr/Public/Gdcm", 0x0008, 0x0081}, // Institution Address
1357 { "GDCM", 0x0010, 0x0010}, // Patient's Name
1358 { "GDCMID", 0x0010, 0x0020}, // Patient ID
1360 { uidStudy.c_str(), 0x0020, 0x000d}, // Study Instance UID
1361 { uidSerie.c_str(), 0x0020, 0x000e}, // Series Instance UID
1362 { "1", 0x0020, 0x0010}, // StudyID
1363 { "1", 0x0020, 0x0011}, // SeriesNumber
1365 { "1", 0x0028, 0x0002}, // Samples per pixel 1 or 3
1366 { "MONOCHROME1", 0x0028, 0x0004}, // photochromatic interpretation
1367 { "0", 0x0028, 0x0010}, // nbRows
1368 { "0", 0x0028, 0x0011}, // nbCols
1369 { "8", 0x0028, 0x0100}, // BitsAllocated 8 or 12 or 16
1370 { "8", 0x0028, 0x0101}, // BitsStored <= BitsAllocated
1371 { "7", 0x0028, 0x0102}, // HighBit <= BitsAllocated - 1
1372 { "0", 0x0028, 0x0103}, // Pixel Representation 0(unsigned) or 1(signed)
1377 // Special case this is the image (not a string)
1380 ReplaceOrCreate(0, 0, GrPixel, NumPixel);
1382 // All remaining strings:
1384 DICOM_DEFAULT_VALUE current = defaultvalue[i];
1385 while( current.value )
1387 ReplaceOrCreate(current.value, current.group, current.elem);
1388 current = defaultvalue[++i];
1393 //-----------------------------------------------------------------------------
1396 //-----------------------------------------------------------------------------
1398 } // end namespace gdcm