1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/12/02 15:14:18 $
7 Version: $Revision: 1.210 $
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 <stdio.h> //sscanf
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 bool Header::Write(std::string fileName,FileType filetype)
107 std::ofstream* fp = new std::ofstream(fileName.c_str(),
108 std::ios::out | std::ios::binary);
111 dbg.Verbose(2, "Failed to open (write) File: " , fileName.c_str());
116 if ( GetEntryByNumber(0x0028,0x0100) == "12")
118 SetEntryByNumber("16", 0x0028,0x0100);
121 /// \todo correct 'Pixel group' Length if necessary
123 int i_lgPix = GetEntryLengthByNumber(GrPixel, NumPixel);
126 // no (GrPixel, NumPixel) element
127 std::string s_lgPix = Util::Format("%d", i_lgPix+12);
128 s_lgPix = Util::DicomString( s_lgPix.c_str() );
129 ReplaceOrCreateByNumber(s_lgPix,GrPixel, 0x0000);
132 // FIXME : should be nice if we could move it to File
133 // (or in future gdcmPixelData class)
135 // Drop Palette Color, if necessary
137 if ( GetEntryByNumber(0x0028,0x0002).c_str()[0] == '3' )
139 // if SamplesPerPixel = 3, sure we don't need any LUT !
140 // Drop 0028|1101, 0028|1102, 0028|1103
141 // Drop 0028|1201, 0028|1202, 0028|1203
143 DocEntry* e = GetDocEntryByNumber(0x0028,0x01101);
146 RemoveEntryNoDestroy(e);
148 e = GetDocEntryByNumber(0x0028,0x1102);
151 RemoveEntryNoDestroy(e);
153 e = GetDocEntryByNumber(0x0028,0x1103);
156 RemoveEntryNoDestroy(e);
158 e = GetDocEntryByNumber(0x0028,0x01201);
161 RemoveEntryNoDestroy(e);
163 e = GetDocEntryByNumber(0x0028,0x1202);
166 RemoveEntryNoDestroy(e);
168 e = GetDocEntryByNumber(0x0028,0x1203);
171 RemoveEntryNoDestroy(e);
174 Document::WriteContent(fp,filetype);
182 //-----------------------------------------------------------------------------
186 //-----------------------------------------------------------------------------
190 * \brief This predicate, based on hopefully reasonable heuristics,
191 * decides whether or not the current Header was properly parsed
192 * and contains the mandatory information for being considered as
193 * a well formed and usable Dicom/Acr File.
194 * @return true when Header is the one of a reasonable Dicom/Acr file,
197 bool Header::IsReadable()
199 if( !Document::IsReadable() )
204 const std::string &res = GetEntryByNumber(0x0028, 0x0005);
205 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
207 return false; // Image Dimensions
209 if ( !GetDocEntryByNumber(0x0028, 0x0100) )
211 return false; // "Bits Allocated"
213 if ( !GetDocEntryByNumber(0x0028, 0x0101) )
215 return false; // "Bits Stored"
217 if ( !GetDocEntryByNumber(0x0028, 0x0102) )
219 return false; // "High Bit"
221 if ( !GetDocEntryByNumber(0x0028, 0x0103) )
223 return false; // "Pixel Representation" i.e. 'Sign'
230 * \brief Retrieve the number of columns of image.
231 * @return The encountered size when found, 0 by default.
232 * 0 means the file is NOT USABLE. The caller will have to check
234 int Header::GetXSize()
236 const std::string &strSize = GetEntryByNumber(0x0028,0x0011);
237 if ( strSize == GDCM_UNFOUND )
242 return atoi( strSize.c_str() );
246 * \brief Retrieve the number of lines of image.
247 * \warning The defaulted value is 1 as opposed to Header::GetXSize()
248 * @return The encountered size when found, 1 by default
249 * (The ACR-NEMA file contains a Signal, not an Image).
251 int Header::GetYSize()
253 const std::string &strSize = GetEntryByNumber(0x0028,0x0010);
254 if ( strSize != GDCM_UNFOUND )
256 return atoi( strSize.c_str() );
263 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
264 // hence be a signal (1d image). So we default to 1:
269 * \brief Retrieve the number of planes of volume or the number
270 * of frames of a multiframe.
271 * \warning When present we consider the "Number of Frames" as the third
272 * dimension. When absent we consider the third dimension as
273 * being the ACR-NEMA "Planes" tag content.
274 * @return The encountered size when found, 1 by default (single image).
276 int Header::GetZSize()
278 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
279 // as the third dimension.
280 const std::string &strSize = GetEntryByNumber(0x0028,0x0008);
281 if ( strSize != GDCM_UNFOUND )
283 return atoi( strSize.c_str() );
286 // We then consider the "Planes" entry as the third dimension
287 const std::string &strSize2 = GetEntryByNumber(0x0028,0x0012);
288 if ( strSize2 != GDCM_UNFOUND )
290 return atoi( strSize2.c_str() );
297 * \brief gets the info from 0028,0030 : Pixel Spacing
299 * @return X dimension of a pixel
301 float Header::GetXSpacing()
303 float xspacing, yspacing;
304 const std::string &strSpacing = GetEntryByNumber(0x0028,0x0030);
306 if ( strSpacing == GDCM_UNFOUND )
308 dbg.Verbose(0, "Header::GetXSpacing: unfound Pixel Spacing (0028,0030)");
313 if( ( nbValues = sscanf( strSpacing.c_str(),
314 "%f\\%f", &yspacing, &xspacing)) != 2 )
316 // if single value is found, xspacing is defaulted to yspacing
324 dbg.Verbose(0, "Header::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
325 // seems to be a bug in the header ...
326 sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
333 * \brief gets the info from 0028,0030 : Pixel Spacing
335 * @return Y dimension of a pixel
337 float Header::GetYSpacing()
340 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
342 if ( strSpacing == GDCM_UNFOUND )
344 dbg.Verbose(0, "Header::GetYSpacing: unfound Pixel Spacing (0028,0030)");
348 // if sscanf cannot read any float value, it won't affect yspacing
349 sscanf( strSpacing.c_str(), "%f", &yspacing);
355 * \brief gets the info from 0018,0088 : Space Between Slices
356 * else from 0018,0050 : Slice Thickness
358 * @return Z dimension of a voxel-to be
360 float Header::GetZSpacing()
362 // Spacing Between Slices : distance entre le milieu de chaque coupe
363 // Les coupes peuvent etre :
364 // jointives (Spacing between Slices = Slice Thickness)
365 // chevauchantes (Spacing between Slices < Slice Thickness)
366 // disjointes (Spacing between Slices > Slice Thickness)
367 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
368 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
369 // Si le Spacing Between Slices est absent,
370 // on suppose que les coupes sont jointives
372 std::string strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
374 if ( strSpacingBSlices == GDCM_UNFOUND )
376 dbg.Verbose(0, "Header::GetZSpacing: unfound StrSpacingBSlices");
377 std::string strSliceThickness = GetEntryByNumber(0x0018,0x0050);
378 if ( strSliceThickness == GDCM_UNFOUND )
384 // if no 'Spacing Between Slices' is found,
385 // we assume slices join together
386 // (no overlapping, no interslice gap)
387 // if they don't, we're fucked up
388 return (float)atof( strSliceThickness.c_str() );
393 return (float)atof( strSpacingBSlices.c_str() );
399 *\brief gets the info from 0028,1052 : Rescale Intercept
400 * @return Rescale Intercept
402 float Header::GetRescaleIntercept()
405 /// 0028 1052 DS IMG Rescale Intercept
406 std::string strRescInter = GetEntryByNumber(0x0028,0x1052);
407 if ( strRescInter != GDCM_UNFOUND )
409 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
411 // bug in the element 0x0028,0x1052
412 dbg.Verbose(0, "Header::GetRescaleIntercept: Rescale Slope "
421 *\brief gets the info from 0028,1053 : Rescale Slope
422 * @return Rescale Slope
424 float Header::GetRescaleSlope()
427 //0028 1053 DS IMG Rescale Slope
428 std::string strRescSlope = GetEntryByNumber(0x0028,0x1053);
429 if ( strRescSlope != GDCM_UNFOUND )
431 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
433 // bug in the element 0x0028,0x1053
434 dbg.Verbose(0, "Header::GetRescaleSlope: Rescale Slope is empty");
442 * \brief This function is intended to user who doesn't want
443 * to have to manage a LUT and expects to get an RBG Pixel image
444 * (or a monochrome one ...)
445 * \warning to be used with GetImagePixels()
446 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
448 int Header::GetNumberOfScalarComponents()
450 if ( GetSamplesPerPixel() == 3 )
455 // 0028 0100 US IMG Bits Allocated
456 // (in order no to be messed up by old RGB images)
457 if ( GetEntryByNumber(0x0028,0x0100) == "24" )
462 std::string strPhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
464 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
466 if ( HasLUT() )// PALETTE COLOR is NOT enough
476 // beware of trailing space at end of string
477 // DICOM tags are never of odd length
478 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
479 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
480 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
486 // we assume that *all* kinds of YBR are dealt with
492 * \brief This function is intended to user that DOESN'T want
493 * to get RGB pixels image when it's stored as a PALETTE COLOR image
494 * - the (vtk) user is supposed to know how deal with LUTs -
495 * \warning to be used with GetImagePixelsRaw()
496 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
498 int Header::GetNumberOfScalarComponentsRaw()
500 // 0028 0100 US IMG Bits Allocated
501 // (in order no to be messed up by old RGB images)
502 if ( Header::GetEntryByNumber(0x0028,0x0100) == "24" )
507 // we assume that *all* kinds of YBR are dealt with
508 return GetSamplesPerPixel();
512 // -------------- Remember ! ----------------------------------
514 // Image Position Patient (0020,0032):
515 // If not found (ACR_NEMA) we try Image Position (0020,0030)
516 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
517 // or Location (0020,0050)
518 // as the Z coordinate,
519 // 0. for all the coordinates if nothing is found
521 // \todo find a way to inform the caller nothing was found
522 // \todo How to tell the caller a wrong number of values was found?
524 // ---------------------------------------------------------------
528 * \brief gets the info from 0020,0032 : Image Position Patient
529 * else from 0020,0030 : Image Position (RET)
531 * @return up-left image corner X position
533 float Header::GetXOrigin()
535 float xImPos, yImPos, zImPos;
536 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
538 if ( strImPos == GDCM_UNFOUND )
540 dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
541 "Position Patient (0020,0032)");
542 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
543 if ( strImPos == GDCM_UNFOUND )
545 dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
546 "Position (RET) (0020,0030)");
547 /// \todo How to tell the caller nothing was found ?
552 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
561 * \brief gets the info from 0020,0032 : Image Position Patient
562 * else from 0020,0030 : Image Position (RET)
564 * @return up-left image corner Y position
566 float Header::GetYOrigin()
568 float xImPos, yImPos, zImPos;
569 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
571 if ( strImPos == GDCM_UNFOUND)
573 dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
574 "Position Patient (0020,0032)");
575 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
576 if ( strImPos == GDCM_UNFOUND )
578 dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
579 "Position (RET) (0020,0030)");
580 /// \todo How to tell the caller nothing was found ?
585 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
594 * \brief gets the info from 0020,0032 : Image Position Patient
595 * else from 0020,0030 : Image Position (RET)
596 * else from 0020,1041 : Slice Location
597 * else from 0020,0050 : Location
599 * @return up-left image corner Z position
601 float Header::GetZOrigin()
603 float xImPos, yImPos, zImPos;
604 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
606 if ( strImPos != GDCM_UNFOUND )
608 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
610 dbg.Verbose(0, "Header::GetZImagePosition: wrong Image "
611 "Position Patient (0020,0032)");
612 return 0.; // bug in the element 0x0020,0x0032
620 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
621 if ( strImPos != GDCM_UNFOUND )
623 if( sscanf( strImPos.c_str(),
624 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
626 dbg.Verbose(0, "Header::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
627 return 0.; // bug in the element 0x0020,0x0032
635 std::string strSliceLocation = GetEntryByNumber(0x0020,0x1041); // for *very* old ACR-NEMA images
636 if ( strSliceLocation != GDCM_UNFOUND )
638 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
640 dbg.Verbose(0, "Header::GetZImagePosition: wrong Slice Location (0020,1041)");
641 return 0.; // bug in the element 0x0020,0x1041
648 dbg.Verbose(0, "Header::GetZImagePosition: unfound Slice Location (0020,1041)");
650 std::string strLocation = GetEntryByNumber(0x0020,0x0050);
651 if ( strLocation != GDCM_UNFOUND )
653 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
655 dbg.Verbose(0, "Header::GetZImagePosition: wrong Location (0020,0050)");
656 return 0.; // bug in the element 0x0020,0x0050
663 dbg.Verbose(0, "Header::GetYImagePosition: unfound Location (0020,0050)");
665 return 0.; // Hopeless
669 * \brief gets the info from 0020,0013 : Image Number else 0.
670 * @return image number
672 int Header::GetImageNumber()
674 // The function i atoi() takes the address of an area of memory as
675 // parameter and converts the string stored at that location to an integer
676 // using the external decimal to internal binary conversion rules. This may
677 // be preferable to sscanf() since atoi() is a much smaller, simpler and
678 // faster function. sscanf() can do all possible conversions whereas
679 // atoi() can only do single decimal integer conversions.
680 //0020 0013 IS REL Image Number
681 std::string strImNumber = GetEntryByNumber(0x0020,0x0013);
682 if ( strImNumber != GDCM_UNFOUND )
684 return atoi( strImNumber.c_str() );
690 * \brief gets the info from 0008,0060 : Modality
691 * @return Modality Type
693 ModalityType Header::GetModality()
695 // 0008 0060 CS ID Modality
696 std::string strModality = GetEntryByNumber(0x0008,0x0060);
697 if ( strModality != GDCM_UNFOUND )
699 if ( strModality.find("AU") < strModality.length()) return AU;
700 else if ( strModality.find("AS") < strModality.length()) return AS;
701 else if ( strModality.find("BI") < strModality.length()) return BI;
702 else if ( strModality.find("CF") < strModality.length()) return CF;
703 else if ( strModality.find("CP") < strModality.length()) return CP;
704 else if ( strModality.find("CR") < strModality.length()) return CR;
705 else if ( strModality.find("CT") < strModality.length()) return CT;
706 else if ( strModality.find("CS") < strModality.length()) return CS;
707 else if ( strModality.find("DD") < strModality.length()) return DD;
708 else if ( strModality.find("DF") < strModality.length()) return DF;
709 else if ( strModality.find("DG") < strModality.length()) return DG;
710 else if ( strModality.find("DM") < strModality.length()) return DM;
711 else if ( strModality.find("DS") < strModality.length()) return DS;
712 else if ( strModality.find("DX") < strModality.length()) return DX;
713 else if ( strModality.find("ECG") < strModality.length()) return ECG;
714 else if ( strModality.find("EPS") < strModality.length()) return EPS;
715 else if ( strModality.find("FA") < strModality.length()) return FA;
716 else if ( strModality.find("FS") < strModality.length()) return FS;
717 else if ( strModality.find("HC") < strModality.length()) return HC;
718 else if ( strModality.find("HD") < strModality.length()) return HD;
719 else if ( strModality.find("LP") < strModality.length()) return LP;
720 else if ( strModality.find("LS") < strModality.length()) return LS;
721 else if ( strModality.find("MA") < strModality.length()) return MA;
722 else if ( strModality.find("MR") < strModality.length()) return MR;
723 else if ( strModality.find("NM") < strModality.length()) return NM;
724 else if ( strModality.find("OT") < strModality.length()) return OT;
725 else if ( strModality.find("PT") < strModality.length()) return PT;
726 else if ( strModality.find("RF") < strModality.length()) return RF;
727 else if ( strModality.find("RG") < strModality.length()) return RG;
728 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
729 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
730 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
731 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
732 else if ( strModality.find("SM") < strModality.length()) return SM;
733 else if ( strModality.find("ST") < strModality.length()) return ST;
734 else if ( strModality.find("TG") < strModality.length()) return TG;
735 else if ( strModality.find("US") < strModality.length()) return US;
736 else if ( strModality.find("VF") < strModality.length()) return VF;
737 else if ( strModality.find("XA") < strModality.length()) return XA;
738 else if ( strModality.find("XC") < strModality.length()) return XC;
742 /// \todo throw error return value ???
743 /// specified <> unknow in our database
752 * \brief Retrieve the number of Bits Stored (actually used)
753 * (as opposite to number of Bits Allocated)
754 * @return The encountered number of Bits Stored, 0 by default.
755 * 0 means the file is NOT USABLE. The caller has to check it !
757 int Header::GetBitsStored()
759 std::string strSize = GetEntryByNumber( 0x0028, 0x0101 );
760 if ( strSize == GDCM_UNFOUND )
762 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
764 return 0; // It's supposed to be mandatory
765 // the caller will have to check
767 return atoi( strSize.c_str() );
771 * \brief Retrieve the high bit position.
772 * \warning The method defaults to 0 when information is absent.
773 * The responsability of checking this value is left to the caller.
774 * @return The high bit positin when present. 0 when absent.
776 int Header::GetHighBitPosition()
778 std::string strSize = GetEntryByNumber( 0x0028, 0x0102 );
779 if ( strSize == GDCM_UNFOUND )
781 dbg.Verbose(0, "Header::GetHighBitPosition: this is supposed "
785 return atoi( strSize.c_str() );
789 * \brief Check wether the pixels are signed or UNsigned data.
790 * \warning The method defaults to false (UNsigned) when information is absent.
791 * The responsability of checking this value is left to the caller.
792 * @return True when signed, false when UNsigned
794 bool Header::IsSignedPixelData()
796 std::string strSize = GetEntryByNumber( 0x0028, 0x0103 );
797 if ( strSize == GDCM_UNFOUND )
799 dbg.Verbose(0, "Header::IsSignedPixelData: this is supposed "
803 int sign = atoi( strSize.c_str() );
812 * \brief Retrieve the number of Bits Allocated
813 * (8, 12 -compacted ACR-NEMA files, 16, ...)
814 * @return The encountered number of Bits Allocated, 0 by default.
815 * 0 means the file is NOT USABLE. The caller has to check it !
817 int Header::GetBitsAllocated()
819 std::string strSize = GetEntryByNumber(0x0028,0x0100);
820 if ( strSize == GDCM_UNFOUND )
822 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
824 return 0; // It's supposed to be mandatory
825 // the caller will have to check
827 return atoi( strSize.c_str() );
831 * \brief Retrieve the number of Samples Per Pixel
832 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
833 * @return The encountered number of Samples Per Pixel, 1 by default.
834 * (Gray level Pixels)
836 int Header::GetSamplesPerPixel()
838 const std::string& strSize = GetEntryByNumber(0x0028,0x0002);
839 if ( strSize == GDCM_UNFOUND )
841 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
843 return 1; // Well, it's supposed to be mandatory ...
844 // but sometimes it's missing : *we* assume Gray pixels
846 return atoi( strSize.c_str() );
850 * \brief Check wether this a monochrome picture or not by accessing
851 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
852 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
854 bool Header::IsMonochrome()
856 const std::string& PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
857 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
858 || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
862 if ( PhotometricInterp == GDCM_UNFOUND )
864 dbg.Verbose(0, "Header::IsMonochrome: absent Photometric "
871 * \brief Check wether this a "PALETTE COLOR" picture or not by accessing
872 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
873 * @return true when "PALETTE COLOR". False otherwise.
875 bool Header::IsPaletteColor()
877 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
878 if ( PhotometricInterp == "PALETTE COLOR " )
882 if ( PhotometricInterp == GDCM_UNFOUND )
884 dbg.Verbose(0, "Header::IsPaletteColor: absent Photometric "
891 * \brief Check wether this a "YBR_FULL" color picture or not by accessing
892 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
893 * @return true when "YBR_FULL". False otherwise.
895 bool Header::IsYBRFull()
897 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
898 if ( PhotometricInterp == "YBR_FULL" )
902 if ( PhotometricInterp == GDCM_UNFOUND )
904 dbg.Verbose(0, "Header::IsYBRFull: absent Photometric "
911 * \brief Retrieve the Planar Configuration for RGB images
912 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
913 * @return The encountered Planar Configuration, 0 by default.
915 int Header::GetPlanarConfiguration()
917 std::string strSize = GetEntryByNumber(0x0028,0x0006);
918 if ( strSize == GDCM_UNFOUND )
922 return atoi( strSize.c_str() );
926 * \brief Return the size (in bytes) of a single pixel of data.
927 * @return The size in bytes of a single pixel of data; 0 by default
928 * 0 means the file is NOT USABLE; the caller will have to check
930 int Header::GetPixelSize()
932 // 0028 0100 US IMG Bits Allocated
933 // (in order no to be messed up by old RGB images)
934 // if (Header::GetEntryByNumber(0x0028,0x0100) == "24")
937 std::string pixelType = GetPixelType();
938 if ( pixelType == "8U" || pixelType == "8S" )
942 if ( pixelType == "16U" || pixelType == "16S")
946 if ( pixelType == "32U" || pixelType == "32S")
950 if ( pixelType == "FD" )
954 dbg.Verbose(0, "Header::GetPixelSize: Unknown pixel type");
959 * \brief Build the Pixel Type of the image.
960 * Possible values are:
961 * - 8U unsigned 8 bit,
963 * - 16U unsigned 16 bit,
964 * - 16S signed 16 bit,
965 * - 32U unsigned 32 bit,
966 * - 32S signed 32 bit,
967 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
968 * \warning 12 bit images appear as 16 bit.
969 * 24 bit images appear as 8 bit
970 * @return 0S if nothing found. NOT USABLE file. The caller has to check
972 std::string Header::GetPixelType()
974 std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
975 if ( bitsAlloc == GDCM_UNFOUND )
977 dbg.Verbose(0, "Header::GetPixelType: unfound Bits Allocated");
981 if ( bitsAlloc == "64" )
985 if ( bitsAlloc == "12" )
987 // It will be unpacked
990 else if ( bitsAlloc == "24" )
992 // (in order no to be messed up
993 bitsAlloc = "8"; // by old RGB images)
996 std::string sign = GetEntryByNumber(0x0028, 0x0103);//"Pixel Representation"
998 if (sign == GDCM_UNFOUND )
1000 dbg.Verbose(0, "Header::GetPixelType: unfound Pixel Representation");
1011 return bitsAlloc + sign;
1016 * \brief Recover the offset (from the beginning of the file)
1017 * of *image* pixels (not *icone image* pixels, if any !)
1018 * @return Pixel Offset
1020 size_t Header::GetPixelOffset()
1022 DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1025 return pxlElement->GetOffset();
1030 std::cout << "Big trouble : Pixel Element ("
1031 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1038 /// \todo TODO : unify those two (previous one and next one)
1040 * \brief Recover the pixel area length (in Bytes)
1041 * @return Pixel Element Length, as stored in the header
1042 * (NOT the memory space necessary to hold the Pixels
1043 * -in case of embeded compressed image-)
1044 * 0 : NOT USABLE file. The caller has to check.
1046 size_t Header::GetPixelAreaLength()
1048 DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1051 return pxlElement->GetLength();
1056 std::cout << "Big trouble : Pixel Element ("
1057 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1065 * \brief tells us if LUT are used
1066 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1067 * are NOT considered as LUT, since nobody knows
1068 * how to deal with them
1069 * Please warn me if you know sbdy that *does* know ... jprx
1070 * @return true if LUT Descriptors and LUT Tables were found
1072 bool Header::HasLUT()
1074 // Check the presence of the LUT Descriptors, and LUT Tables
1076 if ( !GetDocEntryByNumber(0x0028,0x1101) )
1080 // LutDescriptorGreen
1081 if ( !GetDocEntryByNumber(0x0028,0x1102) )
1085 // LutDescriptorBlue
1086 if ( !GetDocEntryByNumber(0x0028,0x1103) )
1090 // Red Palette Color Lookup Table Data
1091 if ( !GetDocEntryByNumber(0x0028,0x1201) )
1095 // Green Palette Color Lookup Table Data
1096 if ( !GetDocEntryByNumber(0x0028,0x1202) )
1100 // Blue Palette Color Lookup Table Data
1101 if ( !GetDocEntryByNumber(0x0028,0x1203) )
1106 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1107 // NOT taken into account, but we don't know how to use it ...
1112 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1114 * @return Lookup Table number of Bits , 0 by default
1115 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1116 * @ return bit number of each LUT item
1118 int Header::GetLUTNbits()
1120 std::vector<std::string> tokens;
1123 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1124 // = Lookup Table Desc-Blue
1125 // Consistency already checked in GetLUTLength
1126 std::string lutDescription = GetEntryByNumber(0x0028,0x1101);
1127 if ( lutDescription == GDCM_UNFOUND )
1132 tokens.clear(); // clean any previous value
1133 Util::Tokenize ( lutDescription, tokens, "\\" );
1134 //LutLength=atoi(tokens[0].c_str());
1135 //LutDepth=atoi(tokens[1].c_str());
1137 lutNbits = atoi( tokens[2].c_str() );
1144 * \brief Accesses the info from 0002,0010 : Transfert Syntax and TS
1146 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1148 std::string Header::GetTransfertSyntaxName()
1150 // use the TS (TS : Transfert Syntax)
1151 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1153 if ( transfertSyntax == GDCM_NOTLOADED )
1155 std::cout << "Transfert Syntax not loaded. " << std::endl
1156 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
1158 return "Uncompressed ACR-NEMA";
1160 if ( transfertSyntax == GDCM_UNFOUND )
1162 dbg.Verbose(0, "Header::GetTransfertSyntaxName:"
1163 " unfound Transfert Syntax (0002,0010)");
1164 return "Uncompressed ACR-NEMA";
1167 while ( ! isdigit((unsigned char)transfertSyntax[transfertSyntax.length()-1]) )
1169 transfertSyntax.erase(transfertSyntax.length()-1, 1);
1171 // we do it only when we need it
1172 TS* ts = Global::GetTS();
1173 std::string tsName = ts->GetValue( transfertSyntax );
1175 //delete ts; /// \todo Seg Fault when deleted ?!
1179 //-----------------------------------------------------------------------------
1183 * \brief anonymize a Header (removes Patient's personal info)
1184 * (read the code to see which ones ...)
1186 bool Header::AnonymizeHeader()
1188 // If exist, replace by spaces
1189 SetEntryByNumber (" ",0x0010, 0x2154); // Telephone
1190 SetEntryByNumber (" ",0x0010, 0x1040); // Adress
1191 SetEntryByNumber (" ",0x0010, 0x0020); // Patient ID
1193 DocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1195 if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1197 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1198 if ( studyInstanceUID != GDCM_UNFOUND )
1200 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1204 ReplaceOrCreateByNumber("anonymised", 0x0010, 0x0010);
1209 // (if any) remove or replace all the stuff that contains a Date
1211 //0008 0012 DA ID Instance Creation Date
1212 //0008 0020 DA ID Study Date
1213 //0008 0021 DA ID Series Date
1214 //0008 0022 DA ID Acquisition Date
1215 //0008 0023 DA ID Content Date
1216 //0008 0024 DA ID Overlay Date
1217 //0008 0025 DA ID Curve Date
1218 //0008 002a DT ID Acquisition Datetime
1219 //0018 9074 DT ACQ Frame Acquisition Datetime
1220 //0018 9151 DT ACQ Frame Reference Datetime
1221 //0018 a002 DT ACQ Contribution Date Time
1222 //0020 3403 SH REL Modified Image Date (RET)
1223 //0032 0032 DA SDY Study Verified Date
1224 //0032 0034 DA SDY Study Read Date
1225 //0032 1000 DA SDY Scheduled Study Start Date
1226 //0032 1010 DA SDY Scheduled Study Stop Date
1227 //0032 1040 DA SDY Study Arrival Date
1228 //0032 1050 DA SDY Study Completion Date
1229 //0038 001a DA VIS Scheduled Admission Date
1230 //0038 001c DA VIS Scheduled Discharge Date
1231 //0038 0020 DA VIS Admitting Date
1232 //0038 0030 DA VIS Discharge Date
1233 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1234 //0040 0004 DA PRC Scheduled Procedure Step End Date
1235 //0040 0244 DA PRC Performed Procedure Step Start Date
1236 //0040 0250 DA PRC Performed Procedure Step End Date
1237 //0040 2004 DA PRC Issue Date of Imaging Service Request
1238 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1239 //0040 4011 DT PRC Expected Completion Date and Time
1240 //0040 a030 DT PRC Verification Date Time
1241 //0040 a032 DT PRC Observation Date Time
1242 //0040 a120 DT PRC DateTime
1243 //0040 a121 DA PRC Date
1244 //0040 a13a DT PRC Referenced Datetime
1245 //0070 0082 DA ??? Presentation Creation Date
1246 //0100 0420 DT ??? SOP Autorization Date and Time
1247 //0400 0105 DT ??? Digital Signature DateTime
1248 //2100 0040 DA PJ Creation Date
1249 //3006 0008 DA SSET Structure Set Date
1250 //3008 0024 DA ??? Treatment Control Point Date
1251 //3008 0054 DA ??? First Treatment Date
1252 //3008 0056 DA ??? Most Recent Treatment Date
1253 //3008 0162 DA ??? Safe Position Exit Date
1254 //3008 0166 DA ??? Safe Position Return Date
1255 //3008 0250 DA ??? Treatment Date
1256 //300a 0006 DA RT RT Plan Date
1257 //300a 022c DA RT Air Kerma Rate Reference Date
1258 //300e 0004 DA RT Review Date
1264 * \brief gets the info from 0020,0037 : Image Orientation Patient
1265 * @param iop adress of the (6)float aray to receive values
1266 * @return cosines of image orientation patient
1268 void Header::GetImageOrientationPatient( float iop[6] )
1270 std::string strImOriPat;
1271 //iop is supposed to be float[6]
1272 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1274 // 0020 0037 DS REL Image Orientation (Patient)
1275 if ( (strImOriPat = GetEntryByNumber(0x0020,0x0037)) != GDCM_UNFOUND )
1277 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1278 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1280 dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1281 "wrong Image Orientation Patient (0020,0037)");
1285 // 0020 0035 DS REL Image Orientation (RET)
1286 else if ( (strImOriPat = GetEntryByNumber(0x0020,0x0035)) != GDCM_UNFOUND )
1288 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1289 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1291 dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1292 "wrong Image Orientation Patient (0020,0035)");
1298 * \brief Initialize a default DICOM header that should contain all the
1299 * field require by other reader. DICOM standart does not
1300 * explicitely defines thoses fields, heuristic has been choosen.
1301 * This is not perfect as we are writting a CT image...
1303 void Header::InitializeDefaultHeader()
1310 } DICOM_DEFAULT_VALUE;
1312 std::string date = Util::GetCurrentDate();
1313 std::string time = Util::GetCurrentTime();
1314 std::string uid = Util::CreateUniqueUID();
1316 static DICOM_DEFAULT_VALUE defaultvalue[] = {
1317 "76", 0x0002, 0x0000, // MetaElementGroup Length // FIXME: how to recompute ?
1318 "1.2.840.10008.5.1.4.1.1.2", 0x0002, 0x0002, // MediaStorageSOPInstanceUID (CT Image Storage)
1319 uid.c_str(), 0x0002, 0x0012, // META Implementation Class UID
1320 "ISO_IR 100",0x0008, 0x0005, // Specific Character Set
1321 "DERIVED\\SECONDARY\\OTHER\\GDCM", 0x0008, 0x0008, // Image Type
1322 "1.2.840.10008.5.1.4.1.1.2", 0x0008, 0x0016, // SOP Class UID
1323 "", 0x0008, 0x0050, // Accession Number
1324 "GDCM", 0x0008, 0x0070, // Manufacturer
1325 "GDCM", 0x0008, 0x0080, // Institution Name
1326 "http://www-creatis.insa-lyon.fr/Public/Gdcm/", 0x0008, 0x0081, // Institution Address
1327 "", 0x0008, 0x0090, // Refering Physician Name
1328 "", 0x0008, 0x1030, // Study Description
1329 "", 0x0008, 0x1050, // Performing Physician's Name
1330 "", 0x0008, 0x1060, // Name of Physician(s) Reading Study
1331 "", 0x0010, 0x0040, // Patient's Sex
1332 uid.c_str(), 0x0020, 0x000d, // StudyInstanceUID
1333 uid.c_str(), 0x0020, 0x000e, // SeriesInstanceUID
1334 "", 0x0020, 0x0011, // AcquisitionNumber
1335 "1\\0\\0\\0\\1\\0", 0x0020, 0x0037, // Image Orientation Patient
1336 "1", 0x0028, 0x0002, // Samples per pixel 1 or 3
1337 "MONOCHROME2",0x0028, 0x0004, // photochromatic interpretation
1341 date.c_str(), 0x0008, 0x0012, // Instance Creation Date
1342 time.c_str(), 0x0008, 0x0013, // Instance Creation Time
1343 date.c_str(), 0x0008, 0x0020, // Study Date
1344 date.c_str(), 0x0008, 0x0022, // Acquisition Date
1345 date.c_str(), 0x0008, 0x0023, // Content Date
1346 time.c_str(), 0x0008, 0x0030, // Study Time
1347 "CT", 0x0008, 0x0060, // Modality
1348 "GDCM", 0x0010, 0x0010, // Patient's Name
1349 "GDCMID", 0x0010, 0x0020, // Patient ID
1350 "1", 0x0020, 0x0010, // StudyID
1351 "1", 0x0020, 0x0011, // SeriesNumber
1352 "1.0", 0x0018, 0x0050, // slice Thickness
1353 "1.0", 0x0018, 0x0088, // space between slices
1354 "1.0\\1.0", 0x0028, 0x0030, // pixelSpacing
1355 "64", 0x0028, 0x0010, // nbRows
1356 "64", 0x0028, 0x0011, // nbCols
1357 "16", 0x0028, 0x0100, // BitsAllocated 8 or 16
1358 "12", 0x0028, 0x0101, // BitsStored 8 or 12
1359 "11", 0x0028, 0x0102, // HighBit 7 or 11
1360 "0", 0x0028, 0x0103, // Pixel Representation 0(unsigned) or 1(signed)
1361 "1000.0", 0x0028, 0x1051, // Window Width
1362 "500.0", 0x0028, 0x1050, // Window Center
1367 // Special case this is the image (not a string)
1370 ReplaceOrCreateByNumber(0, 0, GrPixel, NumPixel);
1372 // All remaining strings:
1374 DICOM_DEFAULT_VALUE current = defaultvalue[i];
1375 while( current.value )
1377 std::string value = Util::DicomString( current.value ); //pad the string properly
1378 ReplaceOrCreateByNumber(value, current.group, current.elem);
1379 current = defaultvalue[++i];
1383 //-----------------------------------------------------------------------------
1386 //-----------------------------------------------------------------------------
1388 } // end namespace gdcm