1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/11/17 10:23:30 $
7 Version: $Revision: 1.203 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #include "gdcmHeader.h"
20 #include "gdcmGlobal.h"
22 #include "gdcmDebug.h"
24 #include "gdcmValEntry.h"
31 //-----------------------------------------------------------------------------
32 // Constructor / Destructor
35 * @param filename name of the file whose header we want to analyze
37 Header::Header( std::string const & filename ):
40 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
41 // We may encounter the 'RETired' (0x0028, 0x0200) tag
42 // (Image Location") . This Element contains the number of
43 // the group that contains the pixel data (hence the "Pixel Data"
44 // is found by indirection through the "Image Location").
45 // Inside the group pointed by "Image Location" the searched element
46 // is conventionally the element 0x0010 (when the norm is respected).
47 // When the "Image Location" is absent we default to group 0x7fe0.
48 // Note: this IS the right place for the code
51 std::string imgLocation = GetEntryByNumber(0x0028, 0x0200);
52 if ( imgLocation == GDCM_UNFOUND )
59 GrPixel = (uint16_t) atoi( imgLocation.c_str() );
62 // sometimes Image Location value doesn't follow
63 // the supposed processor endianity.
64 // see gdcmData/cr172241.dcm
65 if ( GrPixel == 0xe07f )
70 if ( GrPixel != 0x7fe0 )
72 // This is a kludge for old dirty Philips imager.
82 * \brief Constructor used when we want to generate dicom files from scratch
87 InitializeDefaultHeader();
91 * \brief Canonical destructor.
98 * \brief Performs some consistency checking on various 'File related'
99 * (as opposed to 'DicomDir related') entries
100 * then writes in a file all the (Dicom Elements) included the Pixels
101 * @param fp file pointer on an already open file
102 * @param filetype Type of the File to be written
103 * (ACR-NEMA, ExplicitVR, ImplicitVR)
105 void Header::Write(std::ofstream* fp,FileType filetype)
108 if ( GetEntryByNumber(0x0028,0x0100) == "12")
110 SetEntryByNumber("16", 0x0028,0x0100);
113 /// \todo correct 'Pixel group' Length if necessary
115 int i_lgPix = GetEntryLengthByNumber(GrPixel, NumPixel);
118 // no (GrPixel, NumPixel) element
119 std::string s_lgPix = Util::Format("%d", i_lgPix+12);
120 s_lgPix = Util::DicomString( s_lgPix.c_str() );
121 ReplaceOrCreateByNumber(s_lgPix,GrPixel, 0x0000);
124 // FIXME : should be nice if we could move it to File
125 // (or in future gdcmPixelData class)
127 // Drop Palette Color, if necessary
129 if ( GetEntryByNumber(0x0028,0x0002).c_str()[0] == '3' )
131 // if SamplesPerPixel = 3, sure we don't need any LUT !
132 // Drop 0028|1101, 0028|1102, 0028|1103
133 // Drop 0028|1201, 0028|1202, 0028|1203
135 DocEntry* e = GetDocEntryByNumber(0x0028,0x01101);
138 RemoveEntryNoDestroy(e);
140 e = GetDocEntryByNumber(0x0028,0x1102);
143 RemoveEntryNoDestroy(e);
145 e = GetDocEntryByNumber(0x0028,0x1103);
148 RemoveEntryNoDestroy(e);
150 e = GetDocEntryByNumber(0x0028,0x01201);
153 RemoveEntryNoDestroy(e);
155 e = GetDocEntryByNumber(0x0028,0x1202);
158 RemoveEntryNoDestroy(e);
160 e = GetDocEntryByNumber(0x0028,0x1203);
163 RemoveEntryNoDestroy(e);
166 Document::Write(fp,filetype);
169 //-----------------------------------------------------------------------------
173 //-----------------------------------------------------------------------------
177 * \brief This predicate, based on hopefully reasonable heuristics,
178 * decides whether or not the current Header was properly parsed
179 * and contains the mandatory information for being considered as
180 * a well formed and usable Dicom/Acr File.
181 * @return true when Header is the one of a reasonable Dicom/Acr file,
184 bool Header::IsReadable()
186 if( !Document::IsReadable() )
191 std::string res = GetEntryByNumber(0x0028, 0x0005);
192 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
194 return false; // Image Dimensions
196 if ( !GetDocEntryByNumber(0x0028, 0x0100) )
198 return false; // "Bits Allocated"
200 if ( !GetDocEntryByNumber(0x0028, 0x0101) )
202 return false; // "Bits Stored"
204 if ( !GetDocEntryByNumber(0x0028, 0x0102) )
206 return false; // "High Bit"
208 if ( !GetDocEntryByNumber(0x0028, 0x0103) )
210 return false; // "Pixel Representation" i.e. 'Sign'
217 * \brief Retrieve the number of columns of image.
218 * @return The encountered size when found, 0 by default.
219 * 0 means the file is NOT USABLE. The caller will have to check
221 int Header::GetXSize()
224 strSize = GetEntryByNumber(0x0028,0x0011);
225 if ( strSize == GDCM_UNFOUND )
230 return atoi( strSize.c_str() );
234 * \brief Retrieve the number of lines of image.
235 * \warning The defaulted value is 1 as opposed to Header::GetXSize()
236 * @return The encountered size when found, 1 by default
237 * (The ACR-NEMA file contains a Signal, not an Image).
239 int Header::GetYSize()
241 std::string strSize = GetEntryByNumber(0x0028,0x0010);
242 if ( strSize != GDCM_UNFOUND )
244 return atoi( strSize.c_str() );
251 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
252 // hence be a signal (1d image). So we default to 1:
257 * \brief Retrieve the number of planes of volume or the number
258 * of frames of a multiframe.
259 * \warning When present we consider the "Number of Frames" as the third
260 * dimension. When absent we consider the third dimension as
261 * being the ACR-NEMA "Planes" tag content.
262 * @return The encountered size when found, 1 by default (single image).
264 int Header::GetZSize()
266 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
267 // as the third dimension.
268 std::string strSize = GetEntryByNumber(0x0028,0x0008);
269 if ( strSize != GDCM_UNFOUND )
271 return atoi( strSize.c_str() );
274 // We then consider the "Planes" entry as the third dimension
275 strSize = GetEntryByNumber(0x0028,0x0012);
276 if ( strSize != GDCM_UNFOUND )
278 return atoi( strSize.c_str() );
285 * \brief gets the info from 0028,0030 : Pixel Spacing
287 * @return X dimension of a pixel
289 float Header::GetXSpacing()
291 float xspacing, yspacing;
292 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
294 if ( strSpacing == GDCM_UNFOUND )
296 dbg.Verbose(0, "Header::GetXSpacing: unfound Pixel Spacing (0028,0030)");
301 if( ( nbValues = sscanf( strSpacing.c_str(),
302 "%f\\%f", &yspacing, &xspacing)) != 2 )
304 // if single value is found, xspacing is defaulted to yspacing
312 dbg.Verbose(0, "Header::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
313 // seems to be a bug in the header ...
314 sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
321 * \brief gets the info from 0028,0030 : Pixel Spacing
323 * @return Y dimension of a pixel
325 float Header::GetYSpacing()
328 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
330 if ( strSpacing == GDCM_UNFOUND )
332 dbg.Verbose(0, "Header::GetYSpacing: unfound Pixel Spacing (0028,0030)");
336 // if sscanf cannot read any float value, it won't affect yspacing
337 sscanf( strSpacing.c_str(), "%f", &yspacing);
343 * \brief gets the info from 0018,0088 : Space Between Slices
344 * else from 0018,0050 : Slice Thickness
346 * @return Z dimension of a voxel-to be
348 float Header::GetZSpacing()
350 // Spacing Between Slices : distance entre le milieu de chaque coupe
351 // Les coupes peuvent etre :
352 // jointives (Spacing between Slices = Slice Thickness)
353 // chevauchantes (Spacing between Slices < Slice Thickness)
354 // disjointes (Spacing between Slices > Slice Thickness)
355 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
356 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
357 // Si le Spacing Between Slices est absent,
358 // on suppose que les coupes sont jointives
360 std::string strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
362 if ( strSpacingBSlices == GDCM_UNFOUND )
364 dbg.Verbose(0, "Header::GetZSpacing: unfound StrSpacingBSlices");
365 std::string strSliceThickness = GetEntryByNumber(0x0018,0x0050);
366 if ( strSliceThickness == GDCM_UNFOUND )
372 // if no 'Spacing Between Slices' is found,
373 // we assume slices join together
374 // (no overlapping, no interslice gap)
375 // if they don't, we're fucked up
376 return (float)atof( strSliceThickness.c_str() );
381 return (float)atof( strSpacingBSlices.c_str() );
387 *\brief gets the info from 0028,1052 : Rescale Intercept
388 * @return Rescale Intercept
390 float Header::GetRescaleIntercept()
393 /// 0028 1052 DS IMG Rescale Intercept
394 std::string strRescInter = GetEntryByNumber(0x0028,0x1052);
395 if ( strRescInter != GDCM_UNFOUND )
397 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
399 // bug in the element 0x0028,0x1052
400 dbg.Verbose(0, "Header::GetRescaleIntercept: Rescale Slope "
409 *\brief gets the info from 0028,1053 : Rescale Slope
410 * @return Rescale Slope
412 float Header::GetRescaleSlope()
415 //0028 1053 DS IMG Rescale Slope
416 std::string strRescSlope = GetEntryByNumber(0x0028,0x1053);
417 if ( strRescSlope != GDCM_UNFOUND )
419 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
421 // bug in the element 0x0028,0x1053
422 dbg.Verbose(0, "Header::GetRescaleSlope: Rescale Slope is empty");
430 * \brief This function is intended to user who doesn't want
431 * to have to manage a LUT and expects to get an RBG Pixel image
432 * (or a monochrome one ...)
433 * \warning to be used with GetImagePixels()
434 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
436 int Header::GetNumberOfScalarComponents()
438 if ( GetSamplesPerPixel() == 3 )
443 // 0028 0100 US IMG Bits Allocated
444 // (in order no to be messed up by old RGB images)
445 if ( GetEntryByNumber(0x0028,0x0100) == "24" )
450 std::string strPhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
452 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
454 if ( HasLUT() )// PALETTE COLOR is NOT enough
464 // beware of trailing space at end of string
465 // DICOM tags are never of odd length
466 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
467 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
468 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
474 // we assume that *all* kinds of YBR are dealt with
480 * \brief This function is intended to user that DOESN'T want
481 * to get RGB pixels image when it's stored as a PALETTE COLOR image
482 * - the (vtk) user is supposed to know how deal with LUTs -
483 * \warning to be used with GetImagePixelsRaw()
484 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
486 int Header::GetNumberOfScalarComponentsRaw()
488 // 0028 0100 US IMG Bits Allocated
489 // (in order no to be messed up by old RGB images)
490 if ( Header::GetEntryByNumber(0x0028,0x0100) == "24" )
495 // we assume that *all* kinds of YBR are dealt with
496 return GetSamplesPerPixel();
500 // -------------- Remember ! ----------------------------------
502 // Image Position Patient (0020,0032):
503 // If not found (ACR_NEMA) we try Image Position (0020,0030)
504 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
505 // or Location (0020,0050)
506 // as the Z coordinate,
507 // 0. for all the coordinates if nothing is found
509 // \todo find a way to inform the caller nothing was found
510 // \todo How to tell the caller a wrong number of values was found?
512 // ---------------------------------------------------------------
516 * \brief gets the info from 0020,0032 : Image Position Patient
517 * else from 0020,0030 : Image Position (RET)
519 * @return up-left image corner X position
521 float Header::GetXOrigin()
523 float xImPos, yImPos, zImPos;
524 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
526 if ( strImPos == GDCM_UNFOUND )
528 dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
529 "Position Patient (0020,0032)");
530 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
531 if ( strImPos == GDCM_UNFOUND )
533 dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
534 "Position (RET) (0020,0030)");
535 /// \todo How to tell the caller nothing was found ?
540 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
549 * \brief gets the info from 0020,0032 : Image Position Patient
550 * else from 0020,0030 : Image Position (RET)
552 * @return up-left image corner Y position
554 float Header::GetYOrigin()
556 float xImPos, yImPos, zImPos;
557 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
559 if ( strImPos == GDCM_UNFOUND)
561 dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
562 "Position Patient (0020,0032)");
563 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
564 if ( strImPos == GDCM_UNFOUND )
566 dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
567 "Position (RET) (0020,0030)");
568 /// \todo How to tell the caller nothing was found ?
573 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
582 * \brief gets the info from 0020,0032 : Image Position Patient
583 * else from 0020,0030 : Image Position (RET)
584 * else from 0020,1041 : Slice Location
585 * else from 0020,0050 : Location
587 * @return up-left image corner Z position
589 float Header::GetZOrigin()
591 float xImPos, yImPos, zImPos;
592 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
594 if ( strImPos != GDCM_UNFOUND )
596 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
598 dbg.Verbose(0, "Header::GetZImagePosition: wrong Image "
599 "Position Patient (0020,0032)");
600 return 0.; // bug in the element 0x0020,0x0032
608 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
609 if ( strImPos != GDCM_UNFOUND )
611 if( sscanf( strImPos.c_str(),
612 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
614 dbg.Verbose(0, "Header::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
615 return 0.; // bug in the element 0x0020,0x0032
623 std::string strSliceLocation = GetEntryByNumber(0x0020,0x1041); // for *very* old ACR-NEMA images
624 if ( strSliceLocation != GDCM_UNFOUND )
626 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
628 dbg.Verbose(0, "Header::GetZImagePosition: wrong Slice Location (0020,1041)");
629 return 0.; // bug in the element 0x0020,0x1041
636 dbg.Verbose(0, "Header::GetZImagePosition: unfound Slice Location (0020,1041)");
638 std::string strLocation = GetEntryByNumber(0x0020,0x0050);
639 if ( strLocation != GDCM_UNFOUND )
641 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
643 dbg.Verbose(0, "Header::GetZImagePosition: wrong Location (0020,0050)");
644 return 0.; // bug in the element 0x0020,0x0050
651 dbg.Verbose(0, "Header::GetYImagePosition: unfound Location (0020,0050)");
653 return 0.; // Hopeless
657 * \brief gets the info from 0020,0013 : Image Number else 0.
658 * @return image number
660 int Header::GetImageNumber()
662 // The function i atoi() takes the address of an area of memory as
663 // parameter and converts the string stored at that location to an integer
664 // using the external decimal to internal binary conversion rules. This may
665 // be preferable to sscanf() since atoi() is a much smaller, simpler and
666 // faster function. sscanf() can do all possible conversions whereas
667 // atoi() can only do single decimal integer conversions.
668 //0020 0013 IS REL Image Number
669 std::string strImNumber = GetEntryByNumber(0x0020,0x0013);
670 if ( strImNumber != GDCM_UNFOUND )
672 return atoi( strImNumber.c_str() );
678 * \brief gets the info from 0008,0060 : Modality
679 * @return Modality Type
681 ModalityType Header::GetModality()
683 // 0008 0060 CS ID Modality
684 std::string strModality = GetEntryByNumber(0x0008,0x0060);
685 if ( strModality != GDCM_UNFOUND )
687 if ( strModality.find("AU") < strModality.length()) return AU;
688 else if ( strModality.find("AS") < strModality.length()) return AS;
689 else if ( strModality.find("BI") < strModality.length()) return BI;
690 else if ( strModality.find("CF") < strModality.length()) return CF;
691 else if ( strModality.find("CP") < strModality.length()) return CP;
692 else if ( strModality.find("CR") < strModality.length()) return CR;
693 else if ( strModality.find("CT") < strModality.length()) return CT;
694 else if ( strModality.find("CS") < strModality.length()) return CS;
695 else if ( strModality.find("DD") < strModality.length()) return DD;
696 else if ( strModality.find("DF") < strModality.length()) return DF;
697 else if ( strModality.find("DG") < strModality.length()) return DG;
698 else if ( strModality.find("DM") < strModality.length()) return DM;
699 else if ( strModality.find("DS") < strModality.length()) return DS;
700 else if ( strModality.find("DX") < strModality.length()) return DX;
701 else if ( strModality.find("ECG") < strModality.length()) return ECG;
702 else if ( strModality.find("EPS") < strModality.length()) return EPS;
703 else if ( strModality.find("FA") < strModality.length()) return FA;
704 else if ( strModality.find("FS") < strModality.length()) return FS;
705 else if ( strModality.find("HC") < strModality.length()) return HC;
706 else if ( strModality.find("HD") < strModality.length()) return HD;
707 else if ( strModality.find("LP") < strModality.length()) return LP;
708 else if ( strModality.find("LS") < strModality.length()) return LS;
709 else if ( strModality.find("MA") < strModality.length()) return MA;
710 else if ( strModality.find("MR") < strModality.length()) return MR;
711 else if ( strModality.find("NM") < strModality.length()) return NM;
712 else if ( strModality.find("OT") < strModality.length()) return OT;
713 else if ( strModality.find("PT") < strModality.length()) return PT;
714 else if ( strModality.find("RF") < strModality.length()) return RF;
715 else if ( strModality.find("RG") < strModality.length()) return RG;
716 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
717 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
718 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
719 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
720 else if ( strModality.find("SM") < strModality.length()) return SM;
721 else if ( strModality.find("ST") < strModality.length()) return ST;
722 else if ( strModality.find("TG") < strModality.length()) return TG;
723 else if ( strModality.find("US") < strModality.length()) return US;
724 else if ( strModality.find("VF") < strModality.length()) return VF;
725 else if ( strModality.find("XA") < strModality.length()) return XA;
726 else if ( strModality.find("XC") < strModality.length()) return XC;
730 /// \todo throw error return value ???
731 /// specified <> unknow in our database
740 * \brief Retrieve the number of Bits Stored (actually used)
741 * (as opposite to number of Bits Allocated)
742 * @return The encountered number of Bits Stored, 0 by default.
743 * 0 means the file is NOT USABLE. The caller has to check it !
745 int Header::GetBitsStored()
747 std::string strSize = GetEntryByNumber( 0x0028, 0x0101 );
748 if ( strSize == GDCM_UNFOUND )
750 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
752 return 0; // It's supposed to be mandatory
753 // the caller will have to check
755 return atoi( strSize.c_str() );
759 * \brief Retrieve the high bit position.
760 * \warning The method defaults to 0 when information is absent.
761 * The responsability of checking this value is left to the caller.
762 * @return The high bit positin when present. 0 when absent.
764 int Header::GetHighBitPosition()
766 std::string strSize = GetEntryByNumber( 0x0028, 0x0102 );
767 if ( strSize == GDCM_UNFOUND )
769 dbg.Verbose(0, "Header::GetHighBitPosition: this is supposed "
773 return atoi( strSize.c_str() );
777 * \brief Check wether the pixels are signed or UNsigned data.
778 * \warning The method defaults to false (UNsigned) when information is absent.
779 * The responsability of checking this value is left to the caller.
780 * @return True when signed, false when UNsigned
782 bool Header::IsSignedPixelData()
784 std::string strSize = GetEntryByNumber( 0x0028, 0x0103 );
785 if ( strSize == GDCM_UNFOUND )
787 dbg.Verbose(0, "Header::IsSignedPixelData: this is supposed "
791 int sign = atoi( strSize.c_str() );
800 * \brief Retrieve the number of Bits Allocated
801 * (8, 12 -compacted ACR-NEMA files, 16, ...)
802 * @return The encountered number of Bits Allocated, 0 by default.
803 * 0 means the file is NOT USABLE. The caller has to check it !
805 int Header::GetBitsAllocated()
807 std::string strSize = GetEntryByNumber(0x0028,0x0100);
808 if ( strSize == GDCM_UNFOUND )
810 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
812 return 0; // It's supposed to be mandatory
813 // the caller will have to check
815 return atoi( strSize.c_str() );
819 * \brief Retrieve the number of Samples Per Pixel
820 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
821 * @return The encountered number of Samples Per Pixel, 1 by default.
822 * (Gray level Pixels)
824 int Header::GetSamplesPerPixel()
826 const std::string& strSize = GetEntryByNumber(0x0028,0x0002);
827 if ( strSize == GDCM_UNFOUND )
829 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
831 return 1; // Well, it's supposed to be mandatory ...
832 // but sometimes it's missing : *we* assume Gray pixels
834 return atoi( strSize.c_str() );
838 * \brief Check wether this a monochrome picture or not by accessing
839 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
840 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
842 bool Header::IsMonochrome()
844 const std::string& PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
845 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
846 || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
850 if ( PhotometricInterp == GDCM_UNFOUND )
852 dbg.Verbose(0, "Header::IsMonochrome: absent Photometric "
859 * \brief Check wether this a "PALETTE COLOR" picture or not by accessing
860 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
861 * @return true when "PALETTE COLOR". False otherwise.
863 bool Header::IsPaletteColor()
865 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
866 if ( PhotometricInterp == "PALETTE COLOR " )
870 if ( PhotometricInterp == GDCM_UNFOUND )
872 dbg.Verbose(0, "Header::IsPaletteColor: absent Photometric "
879 * \brief Check wether this a "YBR_FULL" color picture or not by accessing
880 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
881 * @return true when "YBR_FULL". False otherwise.
883 bool Header::IsYBRFull()
885 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
886 if ( PhotometricInterp == "YBR_FULL" )
890 if ( PhotometricInterp == GDCM_UNFOUND )
892 dbg.Verbose(0, "Header::IsYBRFull: absent Photometric "
899 * \brief Retrieve the Planar Configuration for RGB images
900 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
901 * @return The encountered Planar Configuration, 0 by default.
903 int Header::GetPlanarConfiguration()
905 std::string strSize = GetEntryByNumber(0x0028,0x0006);
906 if ( strSize == GDCM_UNFOUND )
910 return atoi( strSize.c_str() );
914 * \brief Return the size (in bytes) of a single pixel of data.
915 * @return The size in bytes of a single pixel of data; 0 by default
916 * 0 means the file is NOT USABLE; the caller will have to check
918 int Header::GetPixelSize()
920 // 0028 0100 US IMG Bits Allocated
921 // (in order no to be messed up by old RGB images)
922 // if (Header::GetEntryByNumber(0x0028,0x0100) == "24")
925 std::string pixelType = GetPixelType();
926 if ( pixelType == "8U" || pixelType == "8S" )
930 if ( pixelType == "16U" || pixelType == "16S")
934 if ( pixelType == "32U" || pixelType == "32S")
938 if ( pixelType == "FD" )
942 dbg.Verbose(0, "Header::GetPixelSize: Unknown pixel type");
947 * \brief Build the Pixel Type of the image.
948 * Possible values are:
949 * - 8U unsigned 8 bit,
951 * - 16U unsigned 16 bit,
952 * - 16S signed 16 bit,
953 * - 32U unsigned 32 bit,
954 * - 32S signed 32 bit,
955 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
956 * \warning 12 bit images appear as 16 bit.
957 * 24 bit images appear as 8 bit
958 * @return 0S if nothing found. NOT USABLE file. The caller has to check
960 std::string Header::GetPixelType()
962 std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
963 if ( bitsAlloc == GDCM_UNFOUND )
965 dbg.Verbose(0, "Header::GetPixelType: unfound Bits Allocated");
969 if ( bitsAlloc == "64" )
973 if ( bitsAlloc == "12" )
975 // It will be unpacked
978 else if ( bitsAlloc == "24" )
980 // (in order no to be messed up
981 bitsAlloc = "8"; // by old RGB images)
984 std::string sign = GetEntryByNumber(0x0028, 0x0103);//"Pixel Representation"
986 if (sign == GDCM_UNFOUND )
988 dbg.Verbose(0, "Header::GetPixelType: unfound Pixel Representation");
999 return bitsAlloc + sign;
1004 * \brief Recover the offset (from the beginning of the file)
1005 * of *image* pixels (not *icone image* pixels, if any !)
1006 * @return Pixel Offset
1008 size_t Header::GetPixelOffset()
1010 DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1013 return pxlElement->GetOffset();
1018 std::cout << "Big trouble : Pixel Element ("
1019 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1026 /// \todo TODO : unify those two (previous one and next one)
1028 * \brief Recover the pixel area length (in Bytes)
1029 * @return Pixel Element Length, as stored in the header
1030 * (NOT the memory space necessary to hold the Pixels
1031 * -in case of embeded compressed image-)
1032 * 0 : NOT USABLE file. The caller has to check.
1034 size_t Header::GetPixelAreaLength()
1036 DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1039 return pxlElement->GetLength();
1044 std::cout << "Big trouble : Pixel Element ("
1045 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1053 * \brief tells us if LUT are used
1054 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1055 * are NOT considered as LUT, since nobody knows
1056 * how to deal with them
1057 * Please warn me if you know sbdy that *does* know ... jprx
1058 * @return true if LUT Descriptors and LUT Tables were found
1060 bool Header::HasLUT()
1062 // Check the presence of the LUT Descriptors, and LUT Tables
1064 if ( !GetDocEntryByNumber(0x0028,0x1101) )
1068 // LutDescriptorGreen
1069 if ( !GetDocEntryByNumber(0x0028,0x1102) )
1073 // LutDescriptorBlue
1074 if ( !GetDocEntryByNumber(0x0028,0x1103) )
1078 // Red Palette Color Lookup Table Data
1079 if ( !GetDocEntryByNumber(0x0028,0x1201) )
1083 // Green Palette Color Lookup Table Data
1084 if ( !GetDocEntryByNumber(0x0028,0x1202) )
1088 // Blue Palette Color Lookup Table Data
1089 if ( !GetDocEntryByNumber(0x0028,0x1203) )
1094 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1095 // NOT taken into account, but we don't know how to use it ...
1100 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1102 * @return Lookup Table number of Bits , 0 by default
1103 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1104 * @ return bit number of each LUT item
1106 int Header::GetLUTNbits()
1108 std::vector<std::string> tokens;
1111 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1112 // = Lookup Table Desc-Blue
1113 // Consistency already checked in GetLUTLength
1114 std::string lutDescription = GetEntryByNumber(0x0028,0x1101);
1115 if ( lutDescription == GDCM_UNFOUND )
1120 tokens.clear(); // clean any previous value
1121 Util::Tokenize ( lutDescription, tokens, "\\" );
1122 //LutLength=atoi(tokens[0].c_str());
1123 //LutDepth=atoi(tokens[1].c_str());
1125 lutNbits = atoi( tokens[2].c_str() );
1132 * \brief Accesses the info from 0002,0010 : Transfert Syntax and TS
1134 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1136 std::string Header::GetTransfertSyntaxName()
1138 // use the TS (TS : Transfert Syntax)
1139 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1141 if ( transfertSyntax == GDCM_NOTLOADED )
1143 std::cout << "Transfert Syntax not loaded. " << std::endl
1144 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
1146 return "Uncompressed ACR-NEMA";
1148 if ( transfertSyntax == GDCM_UNFOUND )
1150 dbg.Verbose(0, "Header::GetTransfertSyntaxName:"
1151 " unfound Transfert Syntax (0002,0010)");
1152 return "Uncompressed ACR-NEMA";
1155 while ( ! isdigit((unsigned char)transfertSyntax[transfertSyntax.length()-1]) )
1157 transfertSyntax.erase(transfertSyntax.length()-1, 1);
1159 // we do it only when we need it
1160 TS* ts = Global::GetTS();
1161 std::string tsName = ts->GetValue( transfertSyntax );
1163 //delete ts; /// \todo Seg Fault when deleted ?!
1168 * \brief Sets the Pixel Area size in the Header
1169 * --> not-for-rats function
1170 * @param ImageDataSize new Pixel Area Size
1171 * warning : nothing else is checked
1173 void Header::SetImageDataSize(size_t ImageDataSize)
1175 ///FIXME I don't understand this code why do we set two times 'car' ?
1176 std::string car = Util::Format("%d", ImageDataSize);
1178 DocEntry *a = GetDocEntryByNumber(GrPixel, NumPixel);
1179 a->SetLength(ImageDataSize);
1182 car = Util::Format("%d", ImageDataSize);
1183 car = Util::DicomString( car.c_str() );
1185 SetEntryByNumber(car, GrPixel, NumPixel);
1188 //-----------------------------------------------------------------------------
1192 * \brief anonymize a Header (removes Patient's personal info)
1193 * (read the code to see which ones ...)
1195 bool Header::AnonymizeHeader()
1197 // If exist, replace by spaces
1198 SetEntryByNumber (" ",0x0010, 0x2154); // Telephone
1199 SetEntryByNumber (" ",0x0010, 0x1040); // Adress
1200 SetEntryByNumber (" ",0x0010, 0x0020); // Patient ID
1202 DocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1204 if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1206 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1207 if ( studyInstanceUID != GDCM_UNFOUND )
1209 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1213 ReplaceOrCreateByNumber("anonymised", 0x0010, 0x0010);
1218 // (if any) remove or replace all the stuff that contains a Date
1220 //0008 0012 DA ID Instance Creation Date
1221 //0008 0020 DA ID Study Date
1222 //0008 0021 DA ID Series Date
1223 //0008 0022 DA ID Acquisition Date
1224 //0008 0023 DA ID Content Date
1225 //0008 0024 DA ID Overlay Date
1226 //0008 0025 DA ID Curve Date
1227 //0008 002a DT ID Acquisition Datetime
1228 //0018 9074 DT ACQ Frame Acquisition Datetime
1229 //0018 9151 DT ACQ Frame Reference Datetime
1230 //0018 a002 DT ACQ Contribution Date Time
1231 //0020 3403 SH REL Modified Image Date (RET)
1232 //0032 0032 DA SDY Study Verified Date
1233 //0032 0034 DA SDY Study Read Date
1234 //0032 1000 DA SDY Scheduled Study Start Date
1235 //0032 1010 DA SDY Scheduled Study Stop Date
1236 //0032 1040 DA SDY Study Arrival Date
1237 //0032 1050 DA SDY Study Completion Date
1238 //0038 001a DA VIS Scheduled Admission Date
1239 //0038 001c DA VIS Scheduled Discharge Date
1240 //0038 0020 DA VIS Admitting Date
1241 //0038 0030 DA VIS Discharge Date
1242 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1243 //0040 0004 DA PRC Scheduled Procedure Step End Date
1244 //0040 0244 DA PRC Performed Procedure Step Start Date
1245 //0040 0250 DA PRC Performed Procedure Step End Date
1246 //0040 2004 DA PRC Issue Date of Imaging Service Request
1247 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1248 //0040 4011 DT PRC Expected Completion Date and Time
1249 //0040 a030 DT PRC Verification Date Time
1250 //0040 a032 DT PRC Observation Date Time
1251 //0040 a120 DT PRC DateTime
1252 //0040 a121 DA PRC Date
1253 //0040 a13a DT PRC Referenced Datetime
1254 //0070 0082 DA ??? Presentation Creation Date
1255 //0100 0420 DT ??? SOP Autorization Date and Time
1256 //0400 0105 DT ??? Digital Signature DateTime
1257 //2100 0040 DA PJ Creation Date
1258 //3006 0008 DA SSET Structure Set Date
1259 //3008 0024 DA ??? Treatment Control Point Date
1260 //3008 0054 DA ??? First Treatment Date
1261 //3008 0056 DA ??? Most Recent Treatment Date
1262 //3008 0162 DA ??? Safe Position Exit Date
1263 //3008 0166 DA ??? Safe Position Return Date
1264 //3008 0250 DA ??? Treatment Date
1265 //300a 0006 DA RT RT Plan Date
1266 //300a 022c DA RT Air Kerma Rate Reference Date
1267 //300e 0004 DA RT Review Date
1273 * \brief gets the info from 0020,0037 : Image Orientation Patient
1274 * @param iop adress of the (6)float aray to receive values
1275 * @return cosines of image orientation patient
1277 void Header::GetImageOrientationPatient( float iop[6] )
1279 std::string strImOriPat;
1280 //iop is supposed to be float[6]
1281 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1283 // 0020 0037 DS REL Image Orientation (Patient)
1284 if ( (strImOriPat = GetEntryByNumber(0x0020,0x0037)) != GDCM_UNFOUND )
1286 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1287 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1289 dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1290 "wrong Image Orientation Patient (0020,0037)");
1294 // 0020 0035 DS REL Image Orientation (RET)
1295 else if ( (strImOriPat = GetEntryByNumber(0x0020,0x0035)) != GDCM_UNFOUND )
1297 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1298 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1300 dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1301 "wrong Image Orientation Patient (0020,0035)");
1307 * \brief Initialize a default DICOM header that should contain all the
1308 * field require by other reader. DICOM standart does not
1309 * explicitely defines thoses fields, heuristic has been choosen.
1310 * This is not perfect as we are writting a CT image...
1312 void Header::InitializeDefaultHeader()
1319 } DICOM_DEFAULT_VALUE;
1321 const char *date = Util::GetCurrentDate().c_str();
1322 const char *time = Util::GetCurrentTime().c_str();
1323 const char *uid = Util::CreateUniqueUID().c_str();
1325 static DICOM_DEFAULT_VALUE defaultvalue[] = {
1326 "76", 0x0002, 0x0000, // MetaElementGroup Length // FIXME: how to recompute ?
1327 "1.2.840.10008.5.1.4.1.1.2", 0x0002, 0x0002, // MediaStorageSOPInstanceUID (CT Image Storage)
1328 uid, 0x0002, 0x0012, // META Implementation Class UID
1329 "ISO_IR 100", 0x0008, 0x0005, // Specific Character Set
1330 "DERIVED\\SECONDARY\\OTHER\\GDCM", 0x0008, 0x0008, // Image Type
1331 "1.2.840.10008.5.1.4.1.1.2", 0x0008, 0x0016, // SOP Class UID
1332 "", 0x0008, 0x0050, // Accession Number
1333 "GDCM", 0x0008, 0x0070, // Manufacturer
1334 "GDCM", 0x0008, 0x0080, // Institution Name
1335 "http://www-creatis.insa-lyon.fr/Public/Gdcm/", 0x0008, 0x0081, // Institution Address
1336 "", 0x0008, 0x0090, // Refering Physician Name
1337 "", 0x0008, 0x1030, // Study Description
1338 "", 0x0008, 0x1050, // Performing Physician's Name
1339 "", 0x0008, 0x1060, // Name of Physician(s) Reading Study
1340 "", 0x0010, 0x0040, // Patient's Sex
1341 uid, 0x0020, 0x000d, // StudyInstanceUID
1342 uid, 0x0020, 0x000e, // SeriesInstanceUID
1343 "", 0x0020, 0x0011, // AcquisitionNumber
1344 "1\\0\\0\\0\\1\\0", 0x0020, 0x0037, // Image Orientation Patient
1345 "1", 0x0028, 0x0002, // Samples per pixel 1 or 3
1346 "MONOCHROME2", 0x0028, 0x0004, // photochromatic interpretation
1350 date, 0x0008, 0x0012, // Instance Creation Date
1351 time, 0x0008, 0x0013, // Instance Creation Time
1352 date, 0x0008, 0x0020, // Study Date
1353 date, 0x0008, 0x0022, // Acquisition Date
1354 date, 0x0008, 0x0023, // Content Date
1355 time, 0x0008, 0x0030, // Study Time
1356 "CT", 0x0008, 0x0060, // Modality
1357 "GDCM", 0x0010, 0x0010, // Patient's Name
1358 "GDCMID", 0x0010, 0x0020, // Patient ID
1359 "1", 0x0020, 0x0010, // StudyID
1360 "1", 0x0020, 0x0011, // SeriesNumber
1361 "1.0", 0x0018, 0x0050, // slice Thickness
1362 "1.0", 0x0018, 0x0088, // space between slices
1363 "1.0\\1.0", 0x0028, 0x0030, // pixelSpacing
1364 "64", 0x0028, 0x0010, // nbRows
1365 "64", 0x0028, 0x0011, // nbCols
1366 "16", 0x0028, 0x0100, // BitsAllocated 8 or 16
1367 "12", 0x0028, 0x0101, // BitsStored 8 or 12
1368 "11", 0x0028, 0x0102, // HighBit 7 or 11
1369 "0", 0x0028, 0x0103, // Pixel Representation 0(unsigned) or 1(signed)
1370 "1000.0", 0x0028, 0x1051, // Window Width
1371 "500.0", 0x0028, 0x1050, // Window Center
1376 // Special case this is the image (not a string)
1379 ReplaceOrCreateByNumber(0, 0, GrPixel, NumPixel);
1381 // All remaining strings:
1383 DICOM_DEFAULT_VALUE current = defaultvalue[i];
1384 while( current.value )
1386 std::string value = Util::DicomString( current.value ); //pad the string properly
1387 ReplaceOrCreateByNumber(value, current.group, current.elem);
1388 current = defaultvalue[++i];
1392 //-----------------------------------------------------------------------------
1395 //-----------------------------------------------------------------------------
1397 } // end namespace gdcm