1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/12/16 23:17:27 $
7 Version: $Revision: 1.217 $
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
322 if ( xspacing == 0.0 ) xspacing = 1.0;
327 // to avoid troubles with David Clunie's-like images
328 if ( xspacing == 0. && yspacing == 0.) return 1.;
332 dbg.Verbose(0, "Header::GetXSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
333 // seems to be a bug in the header ...
334 nbValues = sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
335 assert( nbValues == 3 );
342 * \brief gets the info from 0028,0030 : Pixel Spacing
344 * @return Y dimension of a pixel
346 float Header::GetYSpacing()
349 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
351 if ( strSpacing == GDCM_UNFOUND )
353 dbg.Verbose(0, "Header::GetYSpacing: unfound Pixel Spacing (0028,0030)");
357 // if sscanf cannot read any float value, it won't affect yspacing
358 sscanf( strSpacing.c_str(), "%f", &yspacing);
360 if ( yspacing == 0.0 ) yspacing = 1.0;
366 * \brief gets the info from 0018,0088 : Space Between Slices
367 * else from 0018,0050 : Slice Thickness
369 * @return Z dimension of a voxel-to be
371 float Header::GetZSpacing()
373 // Spacing Between Slices : distance entre le milieu de chaque coupe
374 // Les coupes peuvent etre :
375 // jointives (Spacing between Slices = Slice Thickness)
376 // chevauchantes (Spacing between Slices < Slice Thickness)
377 // disjointes (Spacing between Slices > Slice Thickness)
378 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
379 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
380 // Si le Spacing Between Slices est absent,
381 // on suppose que les coupes sont jointives
383 const std::string &strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
385 if ( strSpacingBSlices == GDCM_UNFOUND )
387 dbg.Verbose(0, "Header::GetZSpacing: unfound StrSpacingBSlices");
388 const std::string &strSliceThickness = GetEntryByNumber(0x0018,0x0050);
389 if ( strSliceThickness == GDCM_UNFOUND )
395 // if no 'Spacing Between Slices' is found,
396 // we assume slices join together
397 // (no overlapping, no interslice gap)
398 // if they don't, we're fucked up
399 return (float)atof( strSliceThickness.c_str() );
403 return (float)atof( strSpacingBSlices.c_str() );
407 *\brief gets the info from 0028,1052 : Rescale Intercept
408 * @return Rescale Intercept
410 float Header::GetRescaleIntercept()
413 /// 0028 1052 DS IMG Rescale Intercept
414 const std::string &strRescInter = GetEntryByNumber(0x0028,0x1052);
415 if ( strRescInter != GDCM_UNFOUND )
417 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
419 // bug in the element 0x0028,0x1052
420 dbg.Verbose(0, "Header::GetRescaleIntercept: Rescale Slope "
429 *\brief gets the info from 0028,1053 : Rescale Slope
430 * @return Rescale Slope
432 float Header::GetRescaleSlope()
435 //0028 1053 DS IMG Rescale Slope
436 std::string strRescSlope = GetEntryByNumber(0x0028,0x1053);
437 if ( strRescSlope != GDCM_UNFOUND )
439 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
441 // bug in the element 0x0028,0x1053
442 dbg.Verbose(0, "Header::GetRescaleSlope: Rescale Slope is empty");
450 * \brief This function is intended to user who doesn't want
451 * to have to manage a LUT and expects to get an RBG Pixel image
452 * (or a monochrome one ...)
453 * \warning to be used with GetImagePixels()
454 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
456 int Header::GetNumberOfScalarComponents()
458 if ( GetSamplesPerPixel() == 3 )
463 // 0028 0100 US IMG Bits Allocated
464 // (in order no to be messed up by old RGB images)
465 if ( GetEntryByNumber(0x0028,0x0100) == "24" )
470 std::string strPhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
472 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
474 if ( HasLUT() )// PALETTE COLOR is NOT enough
484 // beware of trailing space at end of string
485 // DICOM tags are never of odd length
486 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
487 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
488 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
494 // we assume that *all* kinds of YBR are dealt with
500 * \brief This function is intended to user that DOESN'T want
501 * to get RGB pixels image when it's stored as a PALETTE COLOR image
502 * - the (vtk) user is supposed to know how deal with LUTs -
503 * \warning to be used with GetImagePixelsRaw()
504 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
506 int Header::GetNumberOfScalarComponentsRaw()
508 // 0028 0100 US IMG Bits Allocated
509 // (in order no to be messed up by old RGB images)
510 if ( Header::GetEntryByNumber(0x0028,0x0100) == "24" )
515 // we assume that *all* kinds of YBR are dealt with
516 return GetSamplesPerPixel();
520 // -------------- Remember ! ----------------------------------
522 // Image Position Patient (0020,0032):
523 // If not found (ACR_NEMA) we try Image Position (0020,0030)
524 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
525 // or Location (0020,0050)
526 // as the Z coordinate,
527 // 0. for all the coordinates if nothing is found
529 // \todo find a way to inform the caller nothing was found
530 // \todo How to tell the caller a wrong number of values was found?
532 // ---------------------------------------------------------------
536 * \brief gets the info from 0020,0032 : Image Position Patient
537 * else from 0020,0030 : Image Position (RET)
539 * @return up-left image corner X position
541 float Header::GetXOrigin()
543 float xImPos, yImPos, zImPos;
544 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
546 if ( strImPos == GDCM_UNFOUND )
548 dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
549 "Position Patient (0020,0032)");
550 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
551 if ( strImPos == GDCM_UNFOUND )
553 dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
554 "Position (RET) (0020,0030)");
555 /// \todo How to tell the caller nothing was found ?
560 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
569 * \brief gets the info from 0020,0032 : Image Position Patient
570 * else from 0020,0030 : Image Position (RET)
572 * @return up-left image corner Y position
574 float Header::GetYOrigin()
576 float xImPos, yImPos, zImPos;
577 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
579 if ( strImPos == GDCM_UNFOUND)
581 dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
582 "Position Patient (0020,0032)");
583 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
584 if ( strImPos == GDCM_UNFOUND )
586 dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
587 "Position (RET) (0020,0030)");
588 /// \todo How to tell the caller nothing was found ?
593 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
602 * \brief gets the info from 0020,0032 : Image Position Patient
603 * else from 0020,0030 : Image Position (RET)
604 * else from 0020,1041 : Slice Location
605 * else from 0020,0050 : Location
607 * @return up-left image corner Z position
609 float Header::GetZOrigin()
611 float xImPos, yImPos, zImPos;
612 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
614 if ( strImPos != GDCM_UNFOUND )
616 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
618 dbg.Verbose(0, "Header::GetZImagePosition: wrong Image "
619 "Position Patient (0020,0032)");
620 return 0.; // bug in the element 0x0020,0x0032
628 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
629 if ( strImPos != GDCM_UNFOUND )
631 if( sscanf( strImPos.c_str(),
632 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
634 dbg.Verbose(0, "Header::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
635 return 0.; // bug in the element 0x0020,0x0032
643 std::string strSliceLocation = GetEntryByNumber(0x0020,0x1041); // for *very* old ACR-NEMA images
644 if ( strSliceLocation != GDCM_UNFOUND )
646 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
648 dbg.Verbose(0, "Header::GetZImagePosition: wrong Slice Location (0020,1041)");
649 return 0.; // bug in the element 0x0020,0x1041
656 dbg.Verbose(0, "Header::GetZImagePosition: unfound Slice Location (0020,1041)");
658 std::string strLocation = GetEntryByNumber(0x0020,0x0050);
659 if ( strLocation != GDCM_UNFOUND )
661 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
663 dbg.Verbose(0, "Header::GetZImagePosition: wrong Location (0020,0050)");
664 return 0.; // bug in the element 0x0020,0x0050
671 dbg.Verbose(0, "Header::GetYImagePosition: unfound Location (0020,0050)");
673 return 0.; // Hopeless
677 * \brief gets the info from 0020,0013 : Image Number else 0.
678 * @return image number
680 int Header::GetImageNumber()
682 // The function i atoi() takes the address of an area of memory as
683 // parameter and converts the string stored at that location to an integer
684 // using the external decimal to internal binary conversion rules. This may
685 // be preferable to sscanf() since atoi() is a much smaller, simpler and
686 // faster function. sscanf() can do all possible conversions whereas
687 // atoi() can only do single decimal integer conversions.
688 //0020 0013 IS REL Image Number
689 std::string strImNumber = GetEntryByNumber(0x0020,0x0013);
690 if ( strImNumber != GDCM_UNFOUND )
692 return atoi( strImNumber.c_str() );
698 * \brief gets the info from 0008,0060 : Modality
699 * @return Modality Type
701 ModalityType Header::GetModality()
703 // 0008 0060 CS ID Modality
704 std::string strModality = GetEntryByNumber(0x0008,0x0060);
705 if ( strModality != GDCM_UNFOUND )
707 if ( strModality.find("AU") < strModality.length()) return AU;
708 else if ( strModality.find("AS") < strModality.length()) return AS;
709 else if ( strModality.find("BI") < strModality.length()) return BI;
710 else if ( strModality.find("CF") < strModality.length()) return CF;
711 else if ( strModality.find("CP") < strModality.length()) return CP;
712 else if ( strModality.find("CR") < strModality.length()) return CR;
713 else if ( strModality.find("CT") < strModality.length()) return CT;
714 else if ( strModality.find("CS") < strModality.length()) return CS;
715 else if ( strModality.find("DD") < strModality.length()) return DD;
716 else if ( strModality.find("DF") < strModality.length()) return DF;
717 else if ( strModality.find("DG") < strModality.length()) return DG;
718 else if ( strModality.find("DM") < strModality.length()) return DM;
719 else if ( strModality.find("DS") < strModality.length()) return DS;
720 else if ( strModality.find("DX") < strModality.length()) return DX;
721 else if ( strModality.find("ECG") < strModality.length()) return ECG;
722 else if ( strModality.find("EPS") < strModality.length()) return EPS;
723 else if ( strModality.find("FA") < strModality.length()) return FA;
724 else if ( strModality.find("FS") < strModality.length()) return FS;
725 else if ( strModality.find("HC") < strModality.length()) return HC;
726 else if ( strModality.find("HD") < strModality.length()) return HD;
727 else if ( strModality.find("LP") < strModality.length()) return LP;
728 else if ( strModality.find("LS") < strModality.length()) return LS;
729 else if ( strModality.find("MA") < strModality.length()) return MA;
730 else if ( strModality.find("MR") < strModality.length()) return MR;
731 else if ( strModality.find("NM") < strModality.length()) return NM;
732 else if ( strModality.find("OT") < strModality.length()) return OT;
733 else if ( strModality.find("PT") < strModality.length()) return PT;
734 else if ( strModality.find("RF") < strModality.length()) return RF;
735 else if ( strModality.find("RG") < strModality.length()) return RG;
736 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
737 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
738 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
739 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
740 else if ( strModality.find("SM") < strModality.length()) return SM;
741 else if ( strModality.find("ST") < strModality.length()) return ST;
742 else if ( strModality.find("TG") < strModality.length()) return TG;
743 else if ( strModality.find("US") < strModality.length()) return US;
744 else if ( strModality.find("VF") < strModality.length()) return VF;
745 else if ( strModality.find("XA") < strModality.length()) return XA;
746 else if ( strModality.find("XC") < strModality.length()) return XC;
750 /// \todo throw error return value ???
751 /// specified <> unknow in our database
760 * \brief Retrieve the number of Bits Stored (actually used)
761 * (as opposite to number of Bits Allocated)
762 * @return The encountered number of Bits Stored, 0 by default.
763 * 0 means the file is NOT USABLE. The caller has to check it !
765 int Header::GetBitsStored()
767 std::string strSize = GetEntryByNumber( 0x0028, 0x0101 );
768 if ( strSize == GDCM_UNFOUND )
770 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
772 return 0; // It's supposed to be mandatory
773 // the caller will have to check
775 return atoi( strSize.c_str() );
779 * \brief Retrieve the high bit position.
780 * \warning The method defaults to 0 when information is absent.
781 * The responsability of checking this value is left to the caller.
782 * @return The high bit positin when present. 0 when absent.
784 int Header::GetHighBitPosition()
786 std::string strSize = GetEntryByNumber( 0x0028, 0x0102 );
787 if ( strSize == GDCM_UNFOUND )
789 dbg.Verbose(0, "Header::GetHighBitPosition: this is supposed "
793 return atoi( strSize.c_str() );
797 * \brief Check wether the pixels are signed or UNsigned data.
798 * \warning The method defaults to false (UNsigned) when information is absent.
799 * The responsability of checking this value is left to the caller.
800 * @return True when signed, false when UNsigned
802 bool Header::IsSignedPixelData()
804 std::string strSize = GetEntryByNumber( 0x0028, 0x0103 );
805 if ( strSize == GDCM_UNFOUND )
807 dbg.Verbose(0, "Header::IsSignedPixelData: this is supposed "
811 int sign = atoi( strSize.c_str() );
820 * \brief Retrieve the number of Bits Allocated
821 * (8, 12 -compacted ACR-NEMA files, 16, ...)
822 * @return The encountered number of Bits Allocated, 0 by default.
823 * 0 means the file is NOT USABLE. The caller has to check it !
825 int Header::GetBitsAllocated()
827 std::string strSize = GetEntryByNumber(0x0028,0x0100);
828 if ( strSize == GDCM_UNFOUND )
830 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
832 return 0; // It's supposed to be mandatory
833 // the caller will have to check
835 return atoi( strSize.c_str() );
839 * \brief Retrieve the number of Samples Per Pixel
840 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
841 * @return The encountered number of Samples Per Pixel, 1 by default.
842 * (Gray level Pixels)
844 int Header::GetSamplesPerPixel()
846 const std::string& strSize = GetEntryByNumber(0x0028,0x0002);
847 if ( strSize == GDCM_UNFOUND )
849 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
851 return 1; // Well, it's supposed to be mandatory ...
852 // but sometimes it's missing : *we* assume Gray pixels
854 return atoi( strSize.c_str() );
858 * \brief Check wether this a monochrome picture or not by accessing
859 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
860 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
862 bool Header::IsMonochrome()
864 const std::string& PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
865 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
866 || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
870 if ( PhotometricInterp == GDCM_UNFOUND )
872 dbg.Verbose(0, "Header::IsMonochrome: absent Photometric "
879 * \brief Check wether this a "PALETTE COLOR" picture or not by accessing
880 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
881 * @return true when "PALETTE COLOR". False otherwise.
883 bool Header::IsPaletteColor()
885 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
886 if ( PhotometricInterp == "PALETTE COLOR " )
890 if ( PhotometricInterp == GDCM_UNFOUND )
892 dbg.Verbose(0, "Header::IsPaletteColor: absent Photometric "
899 * \brief Check wether this a "YBR_FULL" color picture or not by accessing
900 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
901 * @return true when "YBR_FULL". False otherwise.
903 bool Header::IsYBRFull()
905 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
906 if ( PhotometricInterp == "YBR_FULL" )
910 if ( PhotometricInterp == GDCM_UNFOUND )
912 dbg.Verbose(0, "Header::IsYBRFull: absent Photometric "
919 * \brief Retrieve the Planar Configuration for RGB images
920 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
921 * @return The encountered Planar Configuration, 0 by default.
923 int Header::GetPlanarConfiguration()
925 std::string strSize = GetEntryByNumber(0x0028,0x0006);
926 if ( strSize == GDCM_UNFOUND )
930 return atoi( strSize.c_str() );
934 * \brief Return the size (in bytes) of a single pixel of data.
935 * @return The size in bytes of a single pixel of data; 0 by default
936 * 0 means the file is NOT USABLE; the caller will have to check
938 int Header::GetPixelSize()
940 // 0028 0100 US IMG Bits Allocated
941 // (in order no to be messed up by old RGB images)
942 // if (Header::GetEntryByNumber(0x0028,0x0100) == "24")
945 std::string pixelType = GetPixelType();
946 if ( pixelType == "8U" || pixelType == "8S" )
950 if ( pixelType == "16U" || pixelType == "16S")
954 if ( pixelType == "32U" || pixelType == "32S")
958 if ( pixelType == "FD" )
962 dbg.Verbose(0, "Header::GetPixelSize: Unknown pixel type");
967 * \brief Build the Pixel Type of the image.
968 * Possible values are:
969 * - 8U unsigned 8 bit,
971 * - 16U unsigned 16 bit,
972 * - 16S signed 16 bit,
973 * - 32U unsigned 32 bit,
974 * - 32S signed 32 bit,
975 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
976 * \warning 12 bit images appear as 16 bit.
977 * 24 bit images appear as 8 bit
978 * @return 0S if nothing found. NOT USABLE file. The caller has to check
980 std::string Header::GetPixelType()
982 std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
983 if ( bitsAlloc == GDCM_UNFOUND )
985 dbg.Verbose(0, "Header::GetPixelType: unfound Bits Allocated");
989 if ( bitsAlloc == "64" )
993 else if ( bitsAlloc == "12" )
995 // It will be unpacked
998 else if ( bitsAlloc == "24" )
1000 // (in order no to be messed up
1001 bitsAlloc = "8"; // by old RGB images)
1004 std::string sign = GetEntryByNumber(0x0028, 0x0103);//"Pixel Representation"
1006 if (sign == GDCM_UNFOUND )
1008 dbg.Verbose(0, "Header::GetPixelType: unfound Pixel Representation");
1011 else if ( sign == "0" )
1019 return bitsAlloc + sign;
1024 * \brief Recover the offset (from the beginning of the file)
1025 * of *image* pixels (not *icone image* pixels, if any !)
1026 * @return Pixel Offset
1028 size_t Header::GetPixelOffset()
1030 DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1033 return pxlElement->GetOffset();
1038 std::cout << "Big trouble : Pixel Element ("
1039 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1046 /// \todo TODO : unify those two (previous one and next one)
1048 * \brief Recover the pixel area length (in Bytes)
1049 * @return Pixel Element Length, as stored in the header
1050 * (NOT the memory space necessary to hold the Pixels
1051 * -in case of embeded compressed image-)
1052 * 0 : NOT USABLE file. The caller has to check.
1054 size_t Header::GetPixelAreaLength()
1056 DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1059 return pxlElement->GetLength();
1064 std::cout << "Big trouble : Pixel Element ("
1065 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1073 * \brief tells us if LUT are used
1074 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1075 * are NOT considered as LUT, since nobody knows
1076 * how to deal with them
1077 * Please warn me if you know sbdy that *does* know ... jprx
1078 * @return true if LUT Descriptors and LUT Tables were found
1080 bool Header::HasLUT()
1082 // Check the presence of the LUT Descriptors, and LUT Tables
1084 if ( !GetDocEntryByNumber(0x0028,0x1101) )
1088 // LutDescriptorGreen
1089 if ( !GetDocEntryByNumber(0x0028,0x1102) )
1093 // LutDescriptorBlue
1094 if ( !GetDocEntryByNumber(0x0028,0x1103) )
1098 // Red Palette Color Lookup Table Data
1099 if ( !GetDocEntryByNumber(0x0028,0x1201) )
1103 // Green Palette Color Lookup Table Data
1104 if ( !GetDocEntryByNumber(0x0028,0x1202) )
1108 // Blue Palette Color Lookup Table Data
1109 if ( !GetDocEntryByNumber(0x0028,0x1203) )
1114 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1115 // NOT taken into account, but we don't know how to use it ...
1120 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1122 * @return Lookup Table number of Bits , 0 by default
1123 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1124 * @ return bit number of each LUT item
1126 int Header::GetLUTNbits()
1128 std::vector<std::string> tokens;
1131 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1132 // = Lookup Table Desc-Blue
1133 // Consistency already checked in GetLUTLength
1134 std::string lutDescription = GetEntryByNumber(0x0028,0x1101);
1135 if ( lutDescription == GDCM_UNFOUND )
1140 tokens.clear(); // clean any previous value
1141 Util::Tokenize ( lutDescription, tokens, "\\" );
1142 //LutLength=atoi(tokens[0].c_str());
1143 //LutDepth=atoi(tokens[1].c_str());
1145 lutNbits = atoi( tokens[2].c_str() );
1152 * \brief Accesses the info from 0002,0010 : Transfert Syntax and TS
1154 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1156 std::string Header::GetTransfertSyntaxName()
1158 // use the TS (TS : Transfert Syntax)
1159 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1161 if ( transfertSyntax == GDCM_NOTLOADED )
1163 std::cout << "Transfert Syntax not loaded. " << std::endl
1164 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
1166 return "Uncompressed ACR-NEMA";
1168 if ( transfertSyntax == GDCM_UNFOUND )
1170 dbg.Verbose(0, "Header::GetTransfertSyntaxName:"
1171 " unfound Transfert Syntax (0002,0010)");
1172 return "Uncompressed ACR-NEMA";
1175 while ( ! isdigit((unsigned char)transfertSyntax[transfertSyntax.length()-1]) )
1177 transfertSyntax.erase(transfertSyntax.length()-1, 1);
1179 // we do it only when we need it
1180 TS* ts = Global::GetTS();
1181 std::string tsName = ts->GetValue( transfertSyntax );
1183 //delete ts; /// \todo Seg Fault when deleted ?!
1187 //-----------------------------------------------------------------------------
1191 * \brief anonymize a Header (removes Patient's personal info)
1192 * (read the code to see which ones ...)
1194 bool Header::AnonymizeHeader()
1196 // If exist, replace by spaces
1197 SetEntryByNumber (" ",0x0010, 0x2154); // Telephone
1198 SetEntryByNumber (" ",0x0010, 0x1040); // Adress
1199 SetEntryByNumber (" ",0x0010, 0x0020); // Patient ID
1201 DocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1203 if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1205 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1206 if ( studyInstanceUID != GDCM_UNFOUND )
1208 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1212 ReplaceOrCreateByNumber("anonymised", 0x0010, 0x0010);
1217 // (if any) remove or replace all the stuff that contains a Date
1219 //0008 0012 DA ID Instance Creation Date
1220 //0008 0020 DA ID Study Date
1221 //0008 0021 DA ID Series Date
1222 //0008 0022 DA ID Acquisition Date
1223 //0008 0023 DA ID Content Date
1224 //0008 0024 DA ID Overlay Date
1225 //0008 0025 DA ID Curve Date
1226 //0008 002a DT ID Acquisition Datetime
1227 //0018 9074 DT ACQ Frame Acquisition Datetime
1228 //0018 9151 DT ACQ Frame Reference Datetime
1229 //0018 a002 DT ACQ Contribution Date Time
1230 //0020 3403 SH REL Modified Image Date (RET)
1231 //0032 0032 DA SDY Study Verified Date
1232 //0032 0034 DA SDY Study Read Date
1233 //0032 1000 DA SDY Scheduled Study Start Date
1234 //0032 1010 DA SDY Scheduled Study Stop Date
1235 //0032 1040 DA SDY Study Arrival Date
1236 //0032 1050 DA SDY Study Completion Date
1237 //0038 001a DA VIS Scheduled Admission Date
1238 //0038 001c DA VIS Scheduled Discharge Date
1239 //0038 0020 DA VIS Admitting Date
1240 //0038 0030 DA VIS Discharge Date
1241 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1242 //0040 0004 DA PRC Scheduled Procedure Step End Date
1243 //0040 0244 DA PRC Performed Procedure Step Start Date
1244 //0040 0250 DA PRC Performed Procedure Step End Date
1245 //0040 2004 DA PRC Issue Date of Imaging Service Request
1246 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1247 //0040 4011 DT PRC Expected Completion Date and Time
1248 //0040 a030 DT PRC Verification Date Time
1249 //0040 a032 DT PRC Observation Date Time
1250 //0040 a120 DT PRC DateTime
1251 //0040 a121 DA PRC Date
1252 //0040 a13a DT PRC Referenced Datetime
1253 //0070 0082 DA ??? Presentation Creation Date
1254 //0100 0420 DT ??? SOP Autorization Date and Time
1255 //0400 0105 DT ??? Digital Signature DateTime
1256 //2100 0040 DA PJ Creation Date
1257 //3006 0008 DA SSET Structure Set Date
1258 //3008 0024 DA ??? Treatment Control Point Date
1259 //3008 0054 DA ??? First Treatment Date
1260 //3008 0056 DA ??? Most Recent Treatment Date
1261 //3008 0162 DA ??? Safe Position Exit Date
1262 //3008 0166 DA ??? Safe Position Return Date
1263 //3008 0250 DA ??? Treatment Date
1264 //300a 0006 DA RT RT Plan Date
1265 //300a 022c DA RT Air Kerma Rate Reference Date
1266 //300e 0004 DA RT Review Date
1272 * \brief gets the info from 0020,0037 : Image Orientation Patient
1273 * @param iop adress of the (6)float aray to receive values
1274 * @return cosines of image orientation patient
1276 void Header::GetImageOrientationPatient( float iop[6] )
1278 std::string strImOriPat;
1279 //iop is supposed to be float[6]
1280 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1282 // 0020 0037 DS REL Image Orientation (Patient)
1283 if ( (strImOriPat = GetEntryByNumber(0x0020,0x0037)) != 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,0037)");
1293 // 0020 0035 DS REL Image Orientation (RET)
1294 else if ( (strImOriPat = GetEntryByNumber(0x0020,0x0035)) != GDCM_UNFOUND )
1296 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1297 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1299 dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1300 "wrong Image Orientation Patient (0020,0035)");
1306 * \brief Initialize a default DICOM header that should contain all the
1307 * field require by other reader. DICOM standart does not
1308 * explicitely defines thoses fields, heuristic has been choosen.
1309 * This is not perfect as we are writting a CT image...
1311 void Header::InitializeDefaultHeader()
1318 } DICOM_DEFAULT_VALUE;
1320 std::string date = Util::GetCurrentDate();
1321 std::string time = Util::GetCurrentTime();
1322 std::string uid = Util::CreateUniqueUID();
1323 std::string uidMedia = uid;
1324 std::string uidClass = uid + ".1";
1325 std::string uidInst = uid + ".10";
1326 std::string uidStudy = uid + ".100";
1327 std::string uidSerie = uid + ".1000";
1329 static DICOM_DEFAULT_VALUE defaultvalue[] = {
1330 { "146 ", 0x0002, 0x0000}, // Meta Element Group Length // FIXME: how to recompute ?
1331 { "1.2.840.10008.5.1.4.1.1.2", 0x0002, 0x0002}, // Media Storage SOP Class UID (CT Image Storage)
1332 { uidClass.c_str(), 0x0002, 0x0003}, // Media Storage SOP Instance UID
1333 { uidClass.c_str(), 0x0002, 0x0012}, // META Implementation Class UID
1334 { "GDCM", 0x0002, 0x0016}, // Source Application Entity Title
1336 { date.c_str(), 0x0008, 0x0012}, // Instance Creation Date
1337 { time.c_str(), 0x0008, 0x0013}, // Instance Creation Time
1338 { "1.2.840.10008.5.1.4.1.1.2", 0x0008, 0x0016}, // SOP Class UID
1339 { uidInst.c_str(), 0x0008, 0x0018}, // SOP Instance UID
1340 { "CT", 0x0008, 0x0060}, // Modality
1341 { "GDCM", 0x0008, 0x0070}, // Manufacturer
1342 { "GDCM", 0x0008, 0x0080}, // Institution Name
1343 { "http://www-creatis.insa-lyon.fr/Public/Gdcm", 0x0008, 0x0081}, // Institution Address
1345 { "GDCM", 0x0010, 0x0010}, // Patient's Name
1346 { "GDCMID", 0x0010, 0x0020}, // Patient ID
1348 { uidStudy.c_str(), 0x0020, 0x000d}, // Study Instance UID
1349 { uidSerie.c_str(), 0x0020, 0x000e}, // Series Instance UID
1350 { "1", 0x0020, 0x0010}, // StudyID
1351 { "1", 0x0020, 0x0011}, // SeriesNumber
1353 { "1", 0x0028, 0x0002}, // Samples per pixel 1 or 3
1354 { "MONOCHROME1", 0x0028, 0x0004}, // photochromatic interpretation
1355 { "0", 0x0028, 0x0010}, // nbRows
1356 { "0", 0x0028, 0x0011}, // nbCols
1357 { "8", 0x0028, 0x0100}, // BitsAllocated 8 or 16
1358 { "8", 0x0028, 0x0101}, // BitsStored 8 or 12 or 16
1359 { "7", 0x0028, 0x0102}, // HighBit 7 or 11 or 15
1360 { "0", 0x0028, 0x0103}, // Pixel Representation 0(unsigned) or 1(signed)
1365 // Special case this is the image (not a string)
1368 ReplaceOrCreateByNumber(0, 0, GrPixel, NumPixel);
1370 // All remaining strings:
1372 DICOM_DEFAULT_VALUE current = defaultvalue[i];
1373 while( current.value )
1375 ReplaceOrCreateByNumber(current.value, current.group, current.elem);
1376 current = defaultvalue[++i];
1380 //-----------------------------------------------------------------------------
1383 //-----------------------------------------------------------------------------
1385 } // end namespace gdcm