1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/12/10 14:35:58 $
7 Version: $Revision: 1.216 $
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 const std::string &strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
374 if ( strSpacingBSlices == GDCM_UNFOUND )
376 dbg.Verbose(0, "Header::GetZSpacing: unfound StrSpacingBSlices");
377 const 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() );
392 return (float)atof( strSpacingBSlices.c_str() );
396 *\brief gets the info from 0028,1052 : Rescale Intercept
397 * @return Rescale Intercept
399 float Header::GetRescaleIntercept()
402 /// 0028 1052 DS IMG Rescale Intercept
403 const std::string &strRescInter = GetEntryByNumber(0x0028,0x1052);
404 if ( strRescInter != GDCM_UNFOUND )
406 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
408 // bug in the element 0x0028,0x1052
409 dbg.Verbose(0, "Header::GetRescaleIntercept: Rescale Slope "
418 *\brief gets the info from 0028,1053 : Rescale Slope
419 * @return Rescale Slope
421 float Header::GetRescaleSlope()
424 //0028 1053 DS IMG Rescale Slope
425 std::string strRescSlope = GetEntryByNumber(0x0028,0x1053);
426 if ( strRescSlope != GDCM_UNFOUND )
428 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
430 // bug in the element 0x0028,0x1053
431 dbg.Verbose(0, "Header::GetRescaleSlope: Rescale Slope is empty");
439 * \brief This function is intended to user who doesn't want
440 * to have to manage a LUT and expects to get an RBG Pixel image
441 * (or a monochrome one ...)
442 * \warning to be used with GetImagePixels()
443 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
445 int Header::GetNumberOfScalarComponents()
447 if ( GetSamplesPerPixel() == 3 )
452 // 0028 0100 US IMG Bits Allocated
453 // (in order no to be messed up by old RGB images)
454 if ( GetEntryByNumber(0x0028,0x0100) == "24" )
459 std::string strPhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
461 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
463 if ( HasLUT() )// PALETTE COLOR is NOT enough
473 // beware of trailing space at end of string
474 // DICOM tags are never of odd length
475 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
476 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
477 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
483 // we assume that *all* kinds of YBR are dealt with
489 * \brief This function is intended to user that DOESN'T want
490 * to get RGB pixels image when it's stored as a PALETTE COLOR image
491 * - the (vtk) user is supposed to know how deal with LUTs -
492 * \warning to be used with GetImagePixelsRaw()
493 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
495 int Header::GetNumberOfScalarComponentsRaw()
497 // 0028 0100 US IMG Bits Allocated
498 // (in order no to be messed up by old RGB images)
499 if ( Header::GetEntryByNumber(0x0028,0x0100) == "24" )
504 // we assume that *all* kinds of YBR are dealt with
505 return GetSamplesPerPixel();
509 // -------------- Remember ! ----------------------------------
511 // Image Position Patient (0020,0032):
512 // If not found (ACR_NEMA) we try Image Position (0020,0030)
513 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
514 // or Location (0020,0050)
515 // as the Z coordinate,
516 // 0. for all the coordinates if nothing is found
518 // \todo find a way to inform the caller nothing was found
519 // \todo How to tell the caller a wrong number of values was found?
521 // ---------------------------------------------------------------
525 * \brief gets the info from 0020,0032 : Image Position Patient
526 * else from 0020,0030 : Image Position (RET)
528 * @return up-left image corner X position
530 float Header::GetXOrigin()
532 float xImPos, yImPos, zImPos;
533 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
535 if ( strImPos == GDCM_UNFOUND )
537 dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
538 "Position Patient (0020,0032)");
539 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
540 if ( strImPos == GDCM_UNFOUND )
542 dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
543 "Position (RET) (0020,0030)");
544 /// \todo How to tell the caller nothing was found ?
549 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
558 * \brief gets the info from 0020,0032 : Image Position Patient
559 * else from 0020,0030 : Image Position (RET)
561 * @return up-left image corner Y position
563 float Header::GetYOrigin()
565 float xImPos, yImPos, zImPos;
566 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
568 if ( strImPos == GDCM_UNFOUND)
570 dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
571 "Position Patient (0020,0032)");
572 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
573 if ( strImPos == GDCM_UNFOUND )
575 dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
576 "Position (RET) (0020,0030)");
577 /// \todo How to tell the caller nothing was found ?
582 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
591 * \brief gets the info from 0020,0032 : Image Position Patient
592 * else from 0020,0030 : Image Position (RET)
593 * else from 0020,1041 : Slice Location
594 * else from 0020,0050 : Location
596 * @return up-left image corner Z position
598 float Header::GetZOrigin()
600 float xImPos, yImPos, zImPos;
601 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
603 if ( strImPos != GDCM_UNFOUND )
605 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
607 dbg.Verbose(0, "Header::GetZImagePosition: wrong Image "
608 "Position Patient (0020,0032)");
609 return 0.; // bug in the element 0x0020,0x0032
617 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
618 if ( strImPos != GDCM_UNFOUND )
620 if( sscanf( strImPos.c_str(),
621 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
623 dbg.Verbose(0, "Header::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
624 return 0.; // bug in the element 0x0020,0x0032
632 std::string strSliceLocation = GetEntryByNumber(0x0020,0x1041); // for *very* old ACR-NEMA images
633 if ( strSliceLocation != GDCM_UNFOUND )
635 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
637 dbg.Verbose(0, "Header::GetZImagePosition: wrong Slice Location (0020,1041)");
638 return 0.; // bug in the element 0x0020,0x1041
645 dbg.Verbose(0, "Header::GetZImagePosition: unfound Slice Location (0020,1041)");
647 std::string strLocation = GetEntryByNumber(0x0020,0x0050);
648 if ( strLocation != GDCM_UNFOUND )
650 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
652 dbg.Verbose(0, "Header::GetZImagePosition: wrong Location (0020,0050)");
653 return 0.; // bug in the element 0x0020,0x0050
660 dbg.Verbose(0, "Header::GetYImagePosition: unfound Location (0020,0050)");
662 return 0.; // Hopeless
666 * \brief gets the info from 0020,0013 : Image Number else 0.
667 * @return image number
669 int Header::GetImageNumber()
671 // The function i atoi() takes the address of an area of memory as
672 // parameter and converts the string stored at that location to an integer
673 // using the external decimal to internal binary conversion rules. This may
674 // be preferable to sscanf() since atoi() is a much smaller, simpler and
675 // faster function. sscanf() can do all possible conversions whereas
676 // atoi() can only do single decimal integer conversions.
677 //0020 0013 IS REL Image Number
678 std::string strImNumber = GetEntryByNumber(0x0020,0x0013);
679 if ( strImNumber != GDCM_UNFOUND )
681 return atoi( strImNumber.c_str() );
687 * \brief gets the info from 0008,0060 : Modality
688 * @return Modality Type
690 ModalityType Header::GetModality()
692 // 0008 0060 CS ID Modality
693 std::string strModality = GetEntryByNumber(0x0008,0x0060);
694 if ( strModality != GDCM_UNFOUND )
696 if ( strModality.find("AU") < strModality.length()) return AU;
697 else if ( strModality.find("AS") < strModality.length()) return AS;
698 else if ( strModality.find("BI") < strModality.length()) return BI;
699 else if ( strModality.find("CF") < strModality.length()) return CF;
700 else if ( strModality.find("CP") < strModality.length()) return CP;
701 else if ( strModality.find("CR") < strModality.length()) return CR;
702 else if ( strModality.find("CT") < strModality.length()) return CT;
703 else if ( strModality.find("CS") < strModality.length()) return CS;
704 else if ( strModality.find("DD") < strModality.length()) return DD;
705 else if ( strModality.find("DF") < strModality.length()) return DF;
706 else if ( strModality.find("DG") < strModality.length()) return DG;
707 else if ( strModality.find("DM") < strModality.length()) return DM;
708 else if ( strModality.find("DS") < strModality.length()) return DS;
709 else if ( strModality.find("DX") < strModality.length()) return DX;
710 else if ( strModality.find("ECG") < strModality.length()) return ECG;
711 else if ( strModality.find("EPS") < strModality.length()) return EPS;
712 else if ( strModality.find("FA") < strModality.length()) return FA;
713 else if ( strModality.find("FS") < strModality.length()) return FS;
714 else if ( strModality.find("HC") < strModality.length()) return HC;
715 else if ( strModality.find("HD") < strModality.length()) return HD;
716 else if ( strModality.find("LP") < strModality.length()) return LP;
717 else if ( strModality.find("LS") < strModality.length()) return LS;
718 else if ( strModality.find("MA") < strModality.length()) return MA;
719 else if ( strModality.find("MR") < strModality.length()) return MR;
720 else if ( strModality.find("NM") < strModality.length()) return NM;
721 else if ( strModality.find("OT") < strModality.length()) return OT;
722 else if ( strModality.find("PT") < strModality.length()) return PT;
723 else if ( strModality.find("RF") < strModality.length()) return RF;
724 else if ( strModality.find("RG") < strModality.length()) return RG;
725 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
726 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
727 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
728 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
729 else if ( strModality.find("SM") < strModality.length()) return SM;
730 else if ( strModality.find("ST") < strModality.length()) return ST;
731 else if ( strModality.find("TG") < strModality.length()) return TG;
732 else if ( strModality.find("US") < strModality.length()) return US;
733 else if ( strModality.find("VF") < strModality.length()) return VF;
734 else if ( strModality.find("XA") < strModality.length()) return XA;
735 else if ( strModality.find("XC") < strModality.length()) return XC;
739 /// \todo throw error return value ???
740 /// specified <> unknow in our database
749 * \brief Retrieve the number of Bits Stored (actually used)
750 * (as opposite to number of Bits Allocated)
751 * @return The encountered number of Bits Stored, 0 by default.
752 * 0 means the file is NOT USABLE. The caller has to check it !
754 int Header::GetBitsStored()
756 std::string strSize = GetEntryByNumber( 0x0028, 0x0101 );
757 if ( strSize == GDCM_UNFOUND )
759 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
761 return 0; // It's supposed to be mandatory
762 // the caller will have to check
764 return atoi( strSize.c_str() );
768 * \brief Retrieve the high bit position.
769 * \warning The method defaults to 0 when information is absent.
770 * The responsability of checking this value is left to the caller.
771 * @return The high bit positin when present. 0 when absent.
773 int Header::GetHighBitPosition()
775 std::string strSize = GetEntryByNumber( 0x0028, 0x0102 );
776 if ( strSize == GDCM_UNFOUND )
778 dbg.Verbose(0, "Header::GetHighBitPosition: this is supposed "
782 return atoi( strSize.c_str() );
786 * \brief Check wether the pixels are signed or UNsigned data.
787 * \warning The method defaults to false (UNsigned) when information is absent.
788 * The responsability of checking this value is left to the caller.
789 * @return True when signed, false when UNsigned
791 bool Header::IsSignedPixelData()
793 std::string strSize = GetEntryByNumber( 0x0028, 0x0103 );
794 if ( strSize == GDCM_UNFOUND )
796 dbg.Verbose(0, "Header::IsSignedPixelData: this is supposed "
800 int sign = atoi( strSize.c_str() );
809 * \brief Retrieve the number of Bits Allocated
810 * (8, 12 -compacted ACR-NEMA files, 16, ...)
811 * @return The encountered number of Bits Allocated, 0 by default.
812 * 0 means the file is NOT USABLE. The caller has to check it !
814 int Header::GetBitsAllocated()
816 std::string strSize = GetEntryByNumber(0x0028,0x0100);
817 if ( strSize == GDCM_UNFOUND )
819 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
821 return 0; // It's supposed to be mandatory
822 // the caller will have to check
824 return atoi( strSize.c_str() );
828 * \brief Retrieve the number of Samples Per Pixel
829 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
830 * @return The encountered number of Samples Per Pixel, 1 by default.
831 * (Gray level Pixels)
833 int Header::GetSamplesPerPixel()
835 const std::string& strSize = GetEntryByNumber(0x0028,0x0002);
836 if ( strSize == GDCM_UNFOUND )
838 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
840 return 1; // Well, it's supposed to be mandatory ...
841 // but sometimes it's missing : *we* assume Gray pixels
843 return atoi( strSize.c_str() );
847 * \brief Check wether this a monochrome picture or not by accessing
848 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
849 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
851 bool Header::IsMonochrome()
853 const std::string& PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
854 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
855 || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
859 if ( PhotometricInterp == GDCM_UNFOUND )
861 dbg.Verbose(0, "Header::IsMonochrome: absent Photometric "
868 * \brief Check wether this a "PALETTE COLOR" picture or not by accessing
869 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
870 * @return true when "PALETTE COLOR". False otherwise.
872 bool Header::IsPaletteColor()
874 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
875 if ( PhotometricInterp == "PALETTE COLOR " )
879 if ( PhotometricInterp == GDCM_UNFOUND )
881 dbg.Verbose(0, "Header::IsPaletteColor: absent Photometric "
888 * \brief Check wether this a "YBR_FULL" color picture or not by accessing
889 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
890 * @return true when "YBR_FULL". False otherwise.
892 bool Header::IsYBRFull()
894 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
895 if ( PhotometricInterp == "YBR_FULL" )
899 if ( PhotometricInterp == GDCM_UNFOUND )
901 dbg.Verbose(0, "Header::IsYBRFull: absent Photometric "
908 * \brief Retrieve the Planar Configuration for RGB images
909 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
910 * @return The encountered Planar Configuration, 0 by default.
912 int Header::GetPlanarConfiguration()
914 std::string strSize = GetEntryByNumber(0x0028,0x0006);
915 if ( strSize == GDCM_UNFOUND )
919 return atoi( strSize.c_str() );
923 * \brief Return the size (in bytes) of a single pixel of data.
924 * @return The size in bytes of a single pixel of data; 0 by default
925 * 0 means the file is NOT USABLE; the caller will have to check
927 int Header::GetPixelSize()
929 // 0028 0100 US IMG Bits Allocated
930 // (in order no to be messed up by old RGB images)
931 // if (Header::GetEntryByNumber(0x0028,0x0100) == "24")
934 std::string pixelType = GetPixelType();
935 if ( pixelType == "8U" || pixelType == "8S" )
939 if ( pixelType == "16U" || pixelType == "16S")
943 if ( pixelType == "32U" || pixelType == "32S")
947 if ( pixelType == "FD" )
951 dbg.Verbose(0, "Header::GetPixelSize: Unknown pixel type");
956 * \brief Build the Pixel Type of the image.
957 * Possible values are:
958 * - 8U unsigned 8 bit,
960 * - 16U unsigned 16 bit,
961 * - 16S signed 16 bit,
962 * - 32U unsigned 32 bit,
963 * - 32S signed 32 bit,
964 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
965 * \warning 12 bit images appear as 16 bit.
966 * 24 bit images appear as 8 bit
967 * @return 0S if nothing found. NOT USABLE file. The caller has to check
969 std::string Header::GetPixelType()
971 std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
972 if ( bitsAlloc == GDCM_UNFOUND )
974 dbg.Verbose(0, "Header::GetPixelType: unfound Bits Allocated");
978 if ( bitsAlloc == "64" )
982 else if ( bitsAlloc == "12" )
984 // It will be unpacked
987 else if ( bitsAlloc == "24" )
989 // (in order no to be messed up
990 bitsAlloc = "8"; // by old RGB images)
993 std::string sign = GetEntryByNumber(0x0028, 0x0103);//"Pixel Representation"
995 if (sign == GDCM_UNFOUND )
997 dbg.Verbose(0, "Header::GetPixelType: unfound Pixel Representation");
1000 else if ( sign == "0" )
1008 return bitsAlloc + sign;
1013 * \brief Recover the offset (from the beginning of the file)
1014 * of *image* pixels (not *icone image* pixels, if any !)
1015 * @return Pixel Offset
1017 size_t Header::GetPixelOffset()
1019 DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1022 return pxlElement->GetOffset();
1027 std::cout << "Big trouble : Pixel Element ("
1028 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1035 /// \todo TODO : unify those two (previous one and next one)
1037 * \brief Recover the pixel area length (in Bytes)
1038 * @return Pixel Element Length, as stored in the header
1039 * (NOT the memory space necessary to hold the Pixels
1040 * -in case of embeded compressed image-)
1041 * 0 : NOT USABLE file. The caller has to check.
1043 size_t Header::GetPixelAreaLength()
1045 DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1048 return pxlElement->GetLength();
1053 std::cout << "Big trouble : Pixel Element ("
1054 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1062 * \brief tells us if LUT are used
1063 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1064 * are NOT considered as LUT, since nobody knows
1065 * how to deal with them
1066 * Please warn me if you know sbdy that *does* know ... jprx
1067 * @return true if LUT Descriptors and LUT Tables were found
1069 bool Header::HasLUT()
1071 // Check the presence of the LUT Descriptors, and LUT Tables
1073 if ( !GetDocEntryByNumber(0x0028,0x1101) )
1077 // LutDescriptorGreen
1078 if ( !GetDocEntryByNumber(0x0028,0x1102) )
1082 // LutDescriptorBlue
1083 if ( !GetDocEntryByNumber(0x0028,0x1103) )
1087 // Red Palette Color Lookup Table Data
1088 if ( !GetDocEntryByNumber(0x0028,0x1201) )
1092 // Green Palette Color Lookup Table Data
1093 if ( !GetDocEntryByNumber(0x0028,0x1202) )
1097 // Blue Palette Color Lookup Table Data
1098 if ( !GetDocEntryByNumber(0x0028,0x1203) )
1103 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1104 // NOT taken into account, but we don't know how to use it ...
1109 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1111 * @return Lookup Table number of Bits , 0 by default
1112 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1113 * @ return bit number of each LUT item
1115 int Header::GetLUTNbits()
1117 std::vector<std::string> tokens;
1120 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1121 // = Lookup Table Desc-Blue
1122 // Consistency already checked in GetLUTLength
1123 std::string lutDescription = GetEntryByNumber(0x0028,0x1101);
1124 if ( lutDescription == GDCM_UNFOUND )
1129 tokens.clear(); // clean any previous value
1130 Util::Tokenize ( lutDescription, tokens, "\\" );
1131 //LutLength=atoi(tokens[0].c_str());
1132 //LutDepth=atoi(tokens[1].c_str());
1134 lutNbits = atoi( tokens[2].c_str() );
1141 * \brief Accesses the info from 0002,0010 : Transfert Syntax and TS
1143 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1145 std::string Header::GetTransfertSyntaxName()
1147 // use the TS (TS : Transfert Syntax)
1148 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1150 if ( transfertSyntax == GDCM_NOTLOADED )
1152 std::cout << "Transfert Syntax not loaded. " << std::endl
1153 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
1155 return "Uncompressed ACR-NEMA";
1157 if ( transfertSyntax == GDCM_UNFOUND )
1159 dbg.Verbose(0, "Header::GetTransfertSyntaxName:"
1160 " unfound Transfert Syntax (0002,0010)");
1161 return "Uncompressed ACR-NEMA";
1164 while ( ! isdigit((unsigned char)transfertSyntax[transfertSyntax.length()-1]) )
1166 transfertSyntax.erase(transfertSyntax.length()-1, 1);
1168 // we do it only when we need it
1169 TS* ts = Global::GetTS();
1170 std::string tsName = ts->GetValue( transfertSyntax );
1172 //delete ts; /// \todo Seg Fault when deleted ?!
1176 //-----------------------------------------------------------------------------
1180 * \brief anonymize a Header (removes Patient's personal info)
1181 * (read the code to see which ones ...)
1183 bool Header::AnonymizeHeader()
1185 // If exist, replace by spaces
1186 SetEntryByNumber (" ",0x0010, 0x2154); // Telephone
1187 SetEntryByNumber (" ",0x0010, 0x1040); // Adress
1188 SetEntryByNumber (" ",0x0010, 0x0020); // Patient ID
1190 DocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1192 if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1194 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1195 if ( studyInstanceUID != GDCM_UNFOUND )
1197 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1201 ReplaceOrCreateByNumber("anonymised", 0x0010, 0x0010);
1206 // (if any) remove or replace all the stuff that contains a Date
1208 //0008 0012 DA ID Instance Creation Date
1209 //0008 0020 DA ID Study Date
1210 //0008 0021 DA ID Series Date
1211 //0008 0022 DA ID Acquisition Date
1212 //0008 0023 DA ID Content Date
1213 //0008 0024 DA ID Overlay Date
1214 //0008 0025 DA ID Curve Date
1215 //0008 002a DT ID Acquisition Datetime
1216 //0018 9074 DT ACQ Frame Acquisition Datetime
1217 //0018 9151 DT ACQ Frame Reference Datetime
1218 //0018 a002 DT ACQ Contribution Date Time
1219 //0020 3403 SH REL Modified Image Date (RET)
1220 //0032 0032 DA SDY Study Verified Date
1221 //0032 0034 DA SDY Study Read Date
1222 //0032 1000 DA SDY Scheduled Study Start Date
1223 //0032 1010 DA SDY Scheduled Study Stop Date
1224 //0032 1040 DA SDY Study Arrival Date
1225 //0032 1050 DA SDY Study Completion Date
1226 //0038 001a DA VIS Scheduled Admission Date
1227 //0038 001c DA VIS Scheduled Discharge Date
1228 //0038 0020 DA VIS Admitting Date
1229 //0038 0030 DA VIS Discharge Date
1230 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1231 //0040 0004 DA PRC Scheduled Procedure Step End Date
1232 //0040 0244 DA PRC Performed Procedure Step Start Date
1233 //0040 0250 DA PRC Performed Procedure Step End Date
1234 //0040 2004 DA PRC Issue Date of Imaging Service Request
1235 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1236 //0040 4011 DT PRC Expected Completion Date and Time
1237 //0040 a030 DT PRC Verification Date Time
1238 //0040 a032 DT PRC Observation Date Time
1239 //0040 a120 DT PRC DateTime
1240 //0040 a121 DA PRC Date
1241 //0040 a13a DT PRC Referenced Datetime
1242 //0070 0082 DA ??? Presentation Creation Date
1243 //0100 0420 DT ??? SOP Autorization Date and Time
1244 //0400 0105 DT ??? Digital Signature DateTime
1245 //2100 0040 DA PJ Creation Date
1246 //3006 0008 DA SSET Structure Set Date
1247 //3008 0024 DA ??? Treatment Control Point Date
1248 //3008 0054 DA ??? First Treatment Date
1249 //3008 0056 DA ??? Most Recent Treatment Date
1250 //3008 0162 DA ??? Safe Position Exit Date
1251 //3008 0166 DA ??? Safe Position Return Date
1252 //3008 0250 DA ??? Treatment Date
1253 //300a 0006 DA RT RT Plan Date
1254 //300a 022c DA RT Air Kerma Rate Reference Date
1255 //300e 0004 DA RT Review Date
1261 * \brief gets the info from 0020,0037 : Image Orientation Patient
1262 * @param iop adress of the (6)float aray to receive values
1263 * @return cosines of image orientation patient
1265 void Header::GetImageOrientationPatient( float iop[6] )
1267 std::string strImOriPat;
1268 //iop is supposed to be float[6]
1269 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1271 // 0020 0037 DS REL Image Orientation (Patient)
1272 if ( (strImOriPat = GetEntryByNumber(0x0020,0x0037)) != GDCM_UNFOUND )
1274 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1275 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1277 dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1278 "wrong Image Orientation Patient (0020,0037)");
1282 // 0020 0035 DS REL Image Orientation (RET)
1283 else if ( (strImOriPat = GetEntryByNumber(0x0020,0x0035)) != GDCM_UNFOUND )
1285 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1286 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1288 dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1289 "wrong Image Orientation Patient (0020,0035)");
1295 * \brief Initialize a default DICOM header that should contain all the
1296 * field require by other reader. DICOM standart does not
1297 * explicitely defines thoses fields, heuristic has been choosen.
1298 * This is not perfect as we are writting a CT image...
1300 void Header::InitializeDefaultHeader()
1307 } DICOM_DEFAULT_VALUE;
1309 std::string date = Util::GetCurrentDate();
1310 std::string time = Util::GetCurrentTime();
1311 std::string uid = Util::CreateUniqueUID();
1312 std::string uidMedia = uid;
1313 std::string uidClass = uid + ".1";
1314 std::string uidInst = uid + ".10";
1315 std::string uidStudy = uid + ".100";
1316 std::string uidSerie = uid + ".1000";
1318 static DICOM_DEFAULT_VALUE defaultvalue[] = {
1319 { "146 ", 0x0002, 0x0000}, // Meta Element Group Length // FIXME: how to recompute ?
1320 { "1.2.840.10008.5.1.4.1.1.2", 0x0002, 0x0002}, // Media Storage SOP Class UID (CT Image Storage)
1321 { uidClass.c_str(), 0x0002, 0x0003}, // Media Storage SOP Instance UID
1322 { uidClass.c_str(), 0x0002, 0x0012}, // META Implementation Class UID
1323 { "GDCM", 0x0002, 0x0016}, // Source Application Entity Title
1325 { date.c_str(), 0x0008, 0x0012}, // Instance Creation Date
1326 { time.c_str(), 0x0008, 0x0013}, // Instance Creation Time
1327 { "1.2.840.10008.5.1.4.1.1.2", 0x0008, 0x0016}, // SOP Class UID
1328 { uidInst.c_str(), 0x0008, 0x0018}, // SOP Instance UID
1329 { "CT", 0x0008, 0x0060}, // Modality
1330 { "GDCM", 0x0008, 0x0070}, // Manufacturer
1331 { "GDCM", 0x0008, 0x0080}, // Institution Name
1332 { "http://www-creatis.insa-lyon.fr/Public/Gdcm", 0x0008, 0x0081}, // Institution Address
1334 { "GDCM", 0x0010, 0x0010}, // Patient's Name
1335 { "GDCMID", 0x0010, 0x0020}, // Patient ID
1337 { uidStudy.c_str(), 0x0020, 0x000d}, // Study Instance UID
1338 { uidSerie.c_str(), 0x0020, 0x000e}, // Series Instance UID
1339 { "1", 0x0020, 0x0010}, // StudyID
1340 { "1", 0x0020, 0x0011}, // SeriesNumber
1342 { "1", 0x0028, 0x0002}, // Samples per pixel 1 or 3
1343 { "MONOCHROME1", 0x0028, 0x0004}, // photochromatic interpretation
1344 { "0", 0x0028, 0x0010}, // nbRows
1345 { "0", 0x0028, 0x0011}, // nbCols
1346 { "8", 0x0028, 0x0100}, // BitsAllocated 8 or 16
1347 { "8", 0x0028, 0x0101}, // BitsStored 8 or 12 or 16
1348 { "7", 0x0028, 0x0102}, // HighBit 7 or 11 or 15
1349 { "0", 0x0028, 0x0103}, // Pixel Representation 0(unsigned) or 1(signed)
1354 // Special case this is the image (not a string)
1357 ReplaceOrCreateByNumber(0, 0, GrPixel, NumPixel);
1359 // All remaining strings:
1361 DICOM_DEFAULT_VALUE current = defaultvalue[i];
1362 while( current.value )
1364 ReplaceOrCreateByNumber(current.value, current.group, current.elem);
1365 current = defaultvalue[++i];
1369 //-----------------------------------------------------------------------------
1372 //-----------------------------------------------------------------------------
1374 } // end namespace gdcm