1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2005/01/05 16:30:50 $
7 Version: $Revision: 1.219 $
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.
80 // Now, we know GrPixel and NumPixel.
81 // Let's create a VirtualDictEntry to allow a further VR modification
82 // and force VR to match with BitsAllocated.
84 DocEntry* entry = GetDocEntryByNumber(GrPixel, NumPixel);
89 // 8 bits allocated is a 'O Bytes' , as well as 24 (old ACR-NEMA RGB)
90 // more than 8 (i.e 12, 16) is a 'O Words'
91 if ( GetBitsAllocated() == 8 || GetBitsAllocated() == 24 )
96 DictEntry* newEntry = NewVirtualDictEntry(
98 PixelVR, "PXL", "Pixel Data");
100 entry->SetDictEntry( newEntry );
106 * \brief Constructor used when we want to generate dicom files from scratch
111 InitializeDefaultHeader();
115 * \brief Canonical destructor.
122 * \brief Performs some consistency checking on various 'File related'
123 * (as opposed to 'DicomDir related') entries
124 * then writes in a file all the (Dicom Elements) included the Pixels
125 * @param fp file pointer on an already open file
126 * @param filetype Type of the File to be written
127 * (ACR-NEMA, ExplicitVR, ImplicitVR)
129 bool Header::Write(std::string fileName, FileType filetype)
131 std::ofstream* fp = new std::ofstream(fileName.c_str(),
132 std::ios::out | std::ios::binary);
135 dbg.Verbose(2, "Failed to open (write) File: " , fileName.c_str());
140 if ( GetEntryByNumber(0x0028,0x0100) == "12")
142 SetEntryByNumber("16", 0x0028,0x0100);
145 /// \todo correct 'Pixel group' Length if necessary
147 int i_lgPix = GetEntryLengthByNumber(GrPixel, NumPixel);
150 // no (GrPixel, NumPixel) element
151 std::string s_lgPix = Util::Format("%d", i_lgPix+12);
152 s_lgPix = Util::DicomString( s_lgPix.c_str() );
153 ReplaceOrCreateByNumber(s_lgPix,GrPixel, 0x0000);
156 // FIXME : should be nice if we could move it to File
157 // (or in future gdcmPixelData class)
159 // Drop Palette Color, if necessary
161 if ( GetEntryByNumber(0x0028,0x0002).c_str()[0] == '3' )
163 // if SamplesPerPixel = 3, sure we don't need any LUT !
164 // Drop 0028|1101, 0028|1102, 0028|1103
165 // Drop 0028|1201, 0028|1202, 0028|1203
167 DocEntry* e = GetDocEntryByNumber(0x0028,0x01101);
170 RemoveEntryNoDestroy(e);
172 e = GetDocEntryByNumber(0x0028,0x1102);
175 RemoveEntryNoDestroy(e);
177 e = GetDocEntryByNumber(0x0028,0x1103);
180 RemoveEntryNoDestroy(e);
182 e = GetDocEntryByNumber(0x0028,0x01201);
185 RemoveEntryNoDestroy(e);
187 e = GetDocEntryByNumber(0x0028,0x1202);
190 RemoveEntryNoDestroy(e);
192 e = GetDocEntryByNumber(0x0028,0x1203);
195 RemoveEntryNoDestroy(e);
198 Document::WriteContent(fp,filetype);
206 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
214 * \brief This predicate, based on hopefully reasonable heuristics,
215 * decides whether or not the current Header was properly parsed
216 * and contains the mandatory information for being considered as
217 * a well formed and usable Dicom/Acr File.
218 * @return true when Header is the one of a reasonable Dicom/Acr file,
221 bool Header::IsReadable()
223 if( !Document::IsReadable() )
228 const std::string &res = GetEntryByNumber(0x0028, 0x0005);
229 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
231 return false; // Image Dimensions
233 if ( !GetDocEntryByNumber(0x0028, 0x0100) )
235 return false; // "Bits Allocated"
237 if ( !GetDocEntryByNumber(0x0028, 0x0101) )
239 return false; // "Bits Stored"
241 if ( !GetDocEntryByNumber(0x0028, 0x0102) )
243 return false; // "High Bit"
245 if ( !GetDocEntryByNumber(0x0028, 0x0103) )
247 return false; // "Pixel Representation" i.e. 'Sign'
254 * \brief Retrieve the number of columns of image.
255 * @return The encountered size when found, 0 by default.
256 * 0 means the file is NOT USABLE. The caller will have to check
258 int Header::GetXSize()
260 const std::string &strSize = GetEntryByNumber(0x0028,0x0011);
261 if ( strSize == GDCM_UNFOUND )
266 return atoi( strSize.c_str() );
270 * \brief Retrieve the number of lines of image.
271 * \warning The defaulted value is 1 as opposed to Header::GetXSize()
272 * @return The encountered size when found, 1 by default
273 * (The ACR-NEMA file contains a Signal, not an Image).
275 int Header::GetYSize()
277 const std::string &strSize = GetEntryByNumber(0x0028,0x0010);
278 if ( strSize != GDCM_UNFOUND )
280 return atoi( strSize.c_str() );
287 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
288 // hence be a signal (1D image). So we default to 1:
293 * \brief Retrieve the number of planes of volume or the number
294 * of frames of a multiframe.
295 * \warning When present we consider the "Number of Frames" as the third
296 * dimension. When absent we consider the third dimension as
297 * being the ACR-NEMA "Planes" tag content.
298 * @return The encountered size when found, 1 by default (single image).
300 int Header::GetZSize()
302 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
303 // as the third dimension.
304 const std::string &strSize = GetEntryByNumber(0x0028,0x0008);
305 if ( strSize != GDCM_UNFOUND )
307 return atoi( strSize.c_str() );
310 // We then consider the "Planes" entry as the third dimension
311 const std::string &strSize2 = GetEntryByNumber(0x0028,0x0012);
312 if ( strSize2 != GDCM_UNFOUND )
314 return atoi( strSize2.c_str() );
321 * \brief gets the info from 0028,0030 : Pixel Spacing
323 * @return X dimension of a pixel
325 float Header::GetXSpacing()
327 float xspacing, yspacing;
328 const std::string &strSpacing = GetEntryByNumber(0x0028,0x0030);
330 if ( strSpacing == GDCM_UNFOUND )
332 dbg.Verbose(0, "Header::GetXSpacing: unfound Pixel Spacing (0028,0030)");
337 if( ( nbValues = sscanf( strSpacing.c_str(),
338 "%f\\%f", &yspacing, &xspacing)) != 2 )
340 // if single value is found, xspacing is defaulted to yspacing
346 if ( xspacing == 0.0 )
353 // to avoid troubles with David Clunie's-like images
354 if ( xspacing == 0. && yspacing == 0.)
359 dbg.Verbose(0, "Header::GetXSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
360 // seems to be a bug in the header ...
361 nbValues = sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
362 assert( nbValues == 2 );
369 * \brief gets the info from 0028,0030 : Pixel Spacing
371 * @return Y dimension of a pixel
373 float Header::GetYSpacing()
376 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
378 if ( strSpacing == GDCM_UNFOUND )
380 dbg.Verbose(0, "Header::GetYSpacing: unfound Pixel Spacing (0028,0030)");
384 // if sscanf cannot read any float value, it won't affect yspacing
385 sscanf( strSpacing.c_str(), "%f", &yspacing);
387 if ( yspacing == 0.0 )
394 * \brief gets the info from 0018,0088 : Space Between Slices
395 * else from 0018,0050 : Slice Thickness
397 * @return Z dimension of a voxel-to be
399 float Header::GetZSpacing()
401 // Spacing Between Slices : distance entre le milieu de chaque coupe
402 // Les coupes peuvent etre :
403 // jointives (Spacing between Slices = Slice Thickness)
404 // chevauchantes (Spacing between Slices < Slice Thickness)
405 // disjointes (Spacing between Slices > Slice Thickness)
406 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
407 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
408 // Si le Spacing Between Slices est absent,
409 // on suppose que les coupes sont jointives
411 const std::string &strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
413 if ( strSpacingBSlices == GDCM_UNFOUND )
415 dbg.Verbose(0, "Header::GetZSpacing: unfound StrSpacingBSlices");
416 const std::string &strSliceThickness = GetEntryByNumber(0x0018,0x0050);
417 if ( strSliceThickness == GDCM_UNFOUND )
423 // if no 'Spacing Between Slices' is found,
424 // we assume slices join together
425 // (no overlapping, no interslice gap)
426 // if they don't, we're fucked up
427 return (float)atof( strSliceThickness.c_str() );
431 return (float)atof( strSpacingBSlices.c_str() );
435 *\brief gets the info from 0028,1052 : Rescale Intercept
436 * @return Rescale Intercept
438 float Header::GetRescaleIntercept()
441 /// 0028 1052 DS IMG Rescale Intercept
442 const std::string &strRescInter = GetEntryByNumber(0x0028,0x1052);
443 if ( strRescInter != GDCM_UNFOUND )
445 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
447 // bug in the element 0x0028,0x1052
448 dbg.Verbose(0, "Header::GetRescaleIntercept: Rescale Slope "
457 *\brief gets the info from 0028,1053 : Rescale Slope
458 * @return Rescale Slope
460 float Header::GetRescaleSlope()
463 //0028 1053 DS IMG Rescale Slope
464 std::string strRescSlope = GetEntryByNumber(0x0028,0x1053);
465 if ( strRescSlope != GDCM_UNFOUND )
467 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
469 // bug in the element 0x0028,0x1053
470 dbg.Verbose(0, "Header::GetRescaleSlope: Rescale Slope is empty");
478 * \brief This function is intended to user who doesn't want
479 * to have to manage a LUT and expects to get an RBG Pixel image
480 * (or a monochrome one ...)
481 * \warning to be used with GetImagePixels()
482 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
484 int Header::GetNumberOfScalarComponents()
486 if ( GetSamplesPerPixel() == 3 )
491 // 0028 0100 US IMG Bits Allocated
492 // (in order no to be messed up by old RGB images)
493 if ( GetEntryByNumber(0x0028,0x0100) == "24" )
498 std::string strPhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
500 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
502 if ( HasLUT() )// PALETTE COLOR is NOT enough
512 // beware of trailing space at end of string
513 // DICOM tags are never of odd length
514 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
515 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
516 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
522 // we assume that *all* kinds of YBR are dealt with
528 * \brief This function is intended to user that DOESN'T want
529 * to get RGB pixels image when it's stored as a PALETTE COLOR image
530 * - the (vtk) user is supposed to know how deal with LUTs -
531 * \warning to be used with GetImagePixelsRaw()
532 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
534 int Header::GetNumberOfScalarComponentsRaw()
536 // 0028 0100 US IMG Bits Allocated
537 // (in order no to be messed up by old RGB images)
538 if ( Header::GetEntryByNumber(0x0028,0x0100) == "24" )
543 // we assume that *all* kinds of YBR are dealt with
544 return GetSamplesPerPixel();
548 // -------------- Remember ! ----------------------------------
550 // Image Position Patient (0020,0032):
551 // If not found (ACR_NEMA) we try Image Position (0020,0030)
552 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
553 // or Location (0020,0050)
554 // as the Z coordinate,
555 // 0. for all the coordinates if nothing is found
557 // \todo find a way to inform the caller nothing was found
558 // \todo How to tell the caller a wrong number of values was found?
560 // ---------------------------------------------------------------
564 * \brief gets the info from 0020,0032 : Image Position Patient
565 * else from 0020,0030 : Image Position (RET)
567 * @return up-left image corner X position
569 float Header::GetXOrigin()
571 float xImPos, yImPos, zImPos;
572 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
574 if ( strImPos == GDCM_UNFOUND )
576 dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
577 "Position Patient (0020,0032)");
578 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
579 if ( strImPos == GDCM_UNFOUND )
581 dbg.Verbose(0, "Header::GetXImagePosition: unfound Image "
582 "Position (RET) (0020,0030)");
583 /// \todo How to tell the caller nothing was found ?
588 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
597 * \brief gets the info from 0020,0032 : Image Position Patient
598 * else from 0020,0030 : Image Position (RET)
600 * @return up-left image corner Y position
602 float Header::GetYOrigin()
604 float xImPos, yImPos, zImPos;
605 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
607 if ( strImPos == GDCM_UNFOUND)
609 dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
610 "Position Patient (0020,0032)");
611 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
612 if ( strImPos == GDCM_UNFOUND )
614 dbg.Verbose(0, "Header::GetYImagePosition: unfound Image "
615 "Position (RET) (0020,0030)");
616 /// \todo How to tell the caller nothing was found ?
621 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
630 * \brief gets the info from 0020,0032 : Image Position Patient
631 * else from 0020,0030 : Image Position (RET)
632 * else from 0020,1041 : Slice Location
633 * else from 0020,0050 : Location
635 * @return up-left image corner Z position
637 float Header::GetZOrigin()
639 float xImPos, yImPos, zImPos;
640 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
642 if ( strImPos != GDCM_UNFOUND )
644 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
646 dbg.Verbose(0, "Header::GetZImagePosition: wrong Image "
647 "Position Patient (0020,0032)");
648 return 0.; // bug in the element 0x0020,0x0032
656 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
657 if ( strImPos != GDCM_UNFOUND )
659 if( sscanf( strImPos.c_str(),
660 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
662 dbg.Verbose(0, "Header::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
663 return 0.; // bug in the element 0x0020,0x0032
671 std::string strSliceLocation = GetEntryByNumber(0x0020,0x1041); // for *very* old ACR-NEMA images
672 if ( strSliceLocation != GDCM_UNFOUND )
674 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
676 dbg.Verbose(0, "Header::GetZImagePosition: wrong Slice Location (0020,1041)");
677 return 0.; // bug in the element 0x0020,0x1041
684 dbg.Verbose(0, "Header::GetZImagePosition: unfound Slice Location (0020,1041)");
686 std::string strLocation = GetEntryByNumber(0x0020,0x0050);
687 if ( strLocation != GDCM_UNFOUND )
689 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
691 dbg.Verbose(0, "Header::GetZImagePosition: wrong Location (0020,0050)");
692 return 0.; // bug in the element 0x0020,0x0050
699 dbg.Verbose(0, "Header::GetYImagePosition: unfound Location (0020,0050)");
701 return 0.; // Hopeless
705 * \brief gets the info from 0020,0013 : Image Number else 0.
706 * @return image number
708 int Header::GetImageNumber()
710 // The function i atoi() takes the address of an area of memory as
711 // parameter and converts the string stored at that location to an integer
712 // using the external decimal to internal binary conversion rules. This may
713 // be preferable to sscanf() since atoi() is a much smaller, simpler and
714 // faster function. sscanf() can do all possible conversions whereas
715 // atoi() can only do single decimal integer conversions.
716 //0020 0013 IS REL Image Number
717 std::string strImNumber = GetEntryByNumber(0x0020,0x0013);
718 if ( strImNumber != GDCM_UNFOUND )
720 return atoi( strImNumber.c_str() );
726 * \brief gets the info from 0008,0060 : Modality
727 * @return Modality Type
729 ModalityType Header::GetModality()
731 // 0008 0060 CS ID Modality
732 std::string strModality = GetEntryByNumber(0x0008,0x0060);
733 if ( strModality != GDCM_UNFOUND )
735 if ( strModality.find("AU") < strModality.length()) return AU;
736 else if ( strModality.find("AS") < strModality.length()) return AS;
737 else if ( strModality.find("BI") < strModality.length()) return BI;
738 else if ( strModality.find("CF") < strModality.length()) return CF;
739 else if ( strModality.find("CP") < strModality.length()) return CP;
740 else if ( strModality.find("CR") < strModality.length()) return CR;
741 else if ( strModality.find("CT") < strModality.length()) return CT;
742 else if ( strModality.find("CS") < strModality.length()) return CS;
743 else if ( strModality.find("DD") < strModality.length()) return DD;
744 else if ( strModality.find("DF") < strModality.length()) return DF;
745 else if ( strModality.find("DG") < strModality.length()) return DG;
746 else if ( strModality.find("DM") < strModality.length()) return DM;
747 else if ( strModality.find("DS") < strModality.length()) return DS;
748 else if ( strModality.find("DX") < strModality.length()) return DX;
749 else if ( strModality.find("ECG") < strModality.length()) return ECG;
750 else if ( strModality.find("EPS") < strModality.length()) return EPS;
751 else if ( strModality.find("FA") < strModality.length()) return FA;
752 else if ( strModality.find("FS") < strModality.length()) return FS;
753 else if ( strModality.find("HC") < strModality.length()) return HC;
754 else if ( strModality.find("HD") < strModality.length()) return HD;
755 else if ( strModality.find("LP") < strModality.length()) return LP;
756 else if ( strModality.find("LS") < strModality.length()) return LS;
757 else if ( strModality.find("MA") < strModality.length()) return MA;
758 else if ( strModality.find("MR") < strModality.length()) return MR;
759 else if ( strModality.find("NM") < strModality.length()) return NM;
760 else if ( strModality.find("OT") < strModality.length()) return OT;
761 else if ( strModality.find("PT") < strModality.length()) return PT;
762 else if ( strModality.find("RF") < strModality.length()) return RF;
763 else if ( strModality.find("RG") < strModality.length()) return RG;
764 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
765 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
766 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
767 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
768 else if ( strModality.find("SM") < strModality.length()) return SM;
769 else if ( strModality.find("ST") < strModality.length()) return ST;
770 else if ( strModality.find("TG") < strModality.length()) return TG;
771 else if ( strModality.find("US") < strModality.length()) return US;
772 else if ( strModality.find("VF") < strModality.length()) return VF;
773 else if ( strModality.find("XA") < strModality.length()) return XA;
774 else if ( strModality.find("XC") < strModality.length()) return XC;
778 /// \todo throw error return value ???
779 /// specified <> unknow in our database
788 * \brief Retrieve the number of Bits Stored (actually used)
789 * (as opposite to number of Bits Allocated)
790 * @return The encountered number of Bits Stored, 0 by default.
791 * 0 means the file is NOT USABLE. The caller has to check it !
793 int Header::GetBitsStored()
795 std::string strSize = GetEntryByNumber( 0x0028, 0x0101 );
796 if ( strSize == GDCM_UNFOUND )
798 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
800 return 0; // It's supposed to be mandatory
801 // the caller will have to check
803 return atoi( strSize.c_str() );
807 * \brief Retrieve the high bit position.
808 * \warning The method defaults to 0 when information is absent.
809 * The responsability of checking this value is left to the caller.
810 * @return The high bit positin when present. 0 when absent.
812 int Header::GetHighBitPosition()
814 std::string strSize = GetEntryByNumber( 0x0028, 0x0102 );
815 if ( strSize == GDCM_UNFOUND )
817 dbg.Verbose(0, "Header::GetHighBitPosition: this is supposed "
821 return atoi( strSize.c_str() );
825 * \brief Check wether the pixels are signed or UNsigned data.
826 * \warning The method defaults to false (UNsigned) when information is absent.
827 * The responsability of checking this value is left to the caller.
828 * @return True when signed, false when UNsigned
830 bool Header::IsSignedPixelData()
832 std::string strSize = GetEntryByNumber( 0x0028, 0x0103 );
833 if ( strSize == GDCM_UNFOUND )
835 dbg.Verbose(0, "Header::IsSignedPixelData: this is supposed "
839 int sign = atoi( strSize.c_str() );
848 * \brief Retrieve the number of Bits Allocated
849 * (8, 12 -compacted ACR-NEMA files, 16, ...)
850 * @return The encountered number of Bits Allocated, 0 by default.
851 * 0 means the file is NOT USABLE. The caller has to check it !
853 int Header::GetBitsAllocated()
855 std::string strSize = GetEntryByNumber(0x0028,0x0100);
856 if ( strSize == GDCM_UNFOUND )
858 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
860 return 0; // It's supposed to be mandatory
861 // the caller will have to check
863 return atoi( strSize.c_str() );
867 * \brief Retrieve the number of Samples Per Pixel
868 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
869 * @return The encountered number of Samples Per Pixel, 1 by default.
870 * (Gray level Pixels)
872 int Header::GetSamplesPerPixel()
874 const std::string& strSize = GetEntryByNumber(0x0028,0x0002);
875 if ( strSize == GDCM_UNFOUND )
877 dbg.Verbose(0, "Header::GetBitsStored: this is supposed to "
879 return 1; // Well, it's supposed to be mandatory ...
880 // but sometimes it's missing : *we* assume Gray pixels
882 return atoi( strSize.c_str() );
886 * \brief Check wether this a monochrome picture or not by accessing
887 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
888 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
890 bool Header::IsMonochrome()
892 const std::string& PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
893 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
894 || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
898 if ( PhotometricInterp == GDCM_UNFOUND )
900 dbg.Verbose(0, "Header::IsMonochrome: absent Photometric "
907 * \brief Check wether this a "PALETTE COLOR" picture or not by accessing
908 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
909 * @return true when "PALETTE COLOR". False otherwise.
911 bool Header::IsPaletteColor()
913 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
914 if ( PhotometricInterp == "PALETTE COLOR " )
918 if ( PhotometricInterp == GDCM_UNFOUND )
920 dbg.Verbose(0, "Header::IsPaletteColor: absent Photometric "
927 * \brief Check wether this a "YBR_FULL" color picture or not by accessing
928 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
929 * @return true when "YBR_FULL". False otherwise.
931 bool Header::IsYBRFull()
933 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
934 if ( PhotometricInterp == "YBR_FULL" )
938 if ( PhotometricInterp == GDCM_UNFOUND )
940 dbg.Verbose(0, "Header::IsYBRFull: absent Photometric "
947 * \brief Retrieve the Planar Configuration for RGB images
948 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
949 * @return The encountered Planar Configuration, 0 by default.
951 int Header::GetPlanarConfiguration()
953 std::string strSize = GetEntryByNumber(0x0028,0x0006);
954 if ( strSize == GDCM_UNFOUND )
958 return atoi( strSize.c_str() );
962 * \brief Return the size (in bytes) of a single pixel of data.
963 * @return The size in bytes of a single pixel of data; 0 by default
964 * 0 means the file is NOT USABLE; the caller will have to check
966 int Header::GetPixelSize()
968 // 0028 0100 US IMG Bits Allocated
969 // (in order no to be messed up by old RGB images)
970 // if (Header::GetEntryByNumber(0x0028,0x0100) == "24")
973 std::string pixelType = GetPixelType();
974 if ( pixelType == "8U" || pixelType == "8S" )
978 if ( pixelType == "16U" || pixelType == "16S")
982 if ( pixelType == "32U" || pixelType == "32S")
986 if ( pixelType == "FD" )
990 dbg.Verbose(0, "Header::GetPixelSize: Unknown pixel type");
995 * \brief Build the Pixel Type of the image.
996 * Possible values are:
997 * - 8U unsigned 8 bit,
999 * - 16U unsigned 16 bit,
1000 * - 16S signed 16 bit,
1001 * - 32U unsigned 32 bit,
1002 * - 32S signed 32 bit,
1003 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
1004 * \warning 12 bit images appear as 16 bit.
1005 * 24 bit images appear as 8 bit
1006 * @return 0S if nothing found. NOT USABLE file. The caller has to check
1008 std::string Header::GetPixelType()
1010 std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
1011 if ( bitsAlloc == GDCM_UNFOUND )
1013 dbg.Verbose(0, "Header::GetPixelType: unfound Bits Allocated");
1017 if ( bitsAlloc == "64" )
1021 else if ( bitsAlloc == "12" )
1023 // It will be unpacked
1026 else if ( bitsAlloc == "24" )
1028 // (in order no to be messed up
1029 bitsAlloc = "8"; // by old RGB images)
1032 std::string sign = GetEntryByNumber(0x0028, 0x0103);//"Pixel Representation"
1034 if (sign == GDCM_UNFOUND )
1036 dbg.Verbose(0, "Header::GetPixelType: unfound Pixel Representation");
1039 else if ( sign == "0" )
1047 return bitsAlloc + sign;
1052 * \brief Recover the offset (from the beginning of the file)
1053 * of *image* pixels (not *icone image* pixels, if any !)
1054 * @return Pixel Offset
1056 size_t Header::GetPixelOffset()
1058 DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1061 return pxlElement->GetOffset();
1066 std::cout << "Big trouble : Pixel Element ("
1067 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1074 /// \todo TODO : unify those two (previous one and next one)
1076 * \brief Recover the pixel area length (in Bytes)
1077 * @return Pixel Element Length, as stored in the header
1078 * (NOT the memory space necessary to hold the Pixels
1079 * -in case of embeded compressed image-)
1080 * 0 : NOT USABLE file. The caller has to check.
1082 size_t Header::GetPixelAreaLength()
1084 DocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1087 return pxlElement->GetLength();
1092 std::cout << "Big trouble : Pixel Element ("
1093 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1101 * \brief tells us if LUT are used
1102 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1103 * are NOT considered as LUT, since nobody knows
1104 * how to deal with them
1105 * Please warn me if you know sbdy that *does* know ... jprx
1106 * @return true if LUT Descriptors and LUT Tables were found
1108 bool Header::HasLUT()
1110 // Check the presence of the LUT Descriptors, and LUT Tables
1112 if ( !GetDocEntryByNumber(0x0028,0x1101) )
1116 // LutDescriptorGreen
1117 if ( !GetDocEntryByNumber(0x0028,0x1102) )
1121 // LutDescriptorBlue
1122 if ( !GetDocEntryByNumber(0x0028,0x1103) )
1126 // Red Palette Color Lookup Table Data
1127 if ( !GetDocEntryByNumber(0x0028,0x1201) )
1131 // Green Palette Color Lookup Table Data
1132 if ( !GetDocEntryByNumber(0x0028,0x1202) )
1136 // Blue Palette Color Lookup Table Data
1137 if ( !GetDocEntryByNumber(0x0028,0x1203) )
1142 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1143 // NOT taken into account, but we don't know how to use it ...
1148 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1150 * @return Lookup Table number of Bits , 0 by default
1151 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1152 * @ return bit number of each LUT item
1154 int Header::GetLUTNbits()
1156 std::vector<std::string> tokens;
1159 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1160 // = Lookup Table Desc-Blue
1161 // Consistency already checked in GetLUTLength
1162 std::string lutDescription = GetEntryByNumber(0x0028,0x1101);
1163 if ( lutDescription == GDCM_UNFOUND )
1168 tokens.clear(); // clean any previous value
1169 Util::Tokenize ( lutDescription, tokens, "\\" );
1170 //LutLength=atoi(tokens[0].c_str());
1171 //LutDepth=atoi(tokens[1].c_str());
1173 lutNbits = atoi( tokens[2].c_str() );
1180 * \brief Accesses the info from 0002,0010 : Transfert Syntax and TS
1182 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1184 std::string Header::GetTransfertSyntaxName()
1186 // use the TS (TS : Transfert Syntax)
1187 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1189 if ( transfertSyntax == GDCM_NOTLOADED )
1191 std::cout << "Transfert Syntax not loaded. " << std::endl
1192 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
1194 return "Uncompressed ACR-NEMA";
1196 if ( transfertSyntax == GDCM_UNFOUND )
1198 dbg.Verbose(0, "Header::GetTransfertSyntaxName:"
1199 " unfound Transfert Syntax (0002,0010)");
1200 return "Uncompressed ACR-NEMA";
1203 while ( ! isdigit((unsigned char)transfertSyntax[transfertSyntax.length()-1]) )
1205 transfertSyntax.erase(transfertSyntax.length()-1, 1);
1207 // we do it only when we need it
1208 TS* ts = Global::GetTS();
1209 std::string tsName = ts->GetValue( transfertSyntax );
1211 //delete ts; /// \todo Seg Fault when deleted ?!
1215 //-----------------------------------------------------------------------------
1219 * \brief anonymize a Header (removes Patient's personal info)
1220 * (read the code to see which ones ...)
1222 bool Header::AnonymizeHeader()
1224 // If exist, replace by spaces
1225 SetEntryByNumber (" ",0x0010, 0x2154); // Telephone
1226 SetEntryByNumber (" ",0x0010, 0x1040); // Adress
1227 SetEntryByNumber (" ",0x0010, 0x0020); // Patient ID
1229 DocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1231 if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1233 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1234 if ( studyInstanceUID != GDCM_UNFOUND )
1236 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1240 ReplaceOrCreateByNumber("anonymised", 0x0010, 0x0010);
1245 // (if any) remove or replace all the stuff that contains a Date
1247 //0008 0012 DA ID Instance Creation Date
1248 //0008 0020 DA ID Study Date
1249 //0008 0021 DA ID Series Date
1250 //0008 0022 DA ID Acquisition Date
1251 //0008 0023 DA ID Content Date
1252 //0008 0024 DA ID Overlay Date
1253 //0008 0025 DA ID Curve Date
1254 //0008 002a DT ID Acquisition Datetime
1255 //0018 9074 DT ACQ Frame Acquisition Datetime
1256 //0018 9151 DT ACQ Frame Reference Datetime
1257 //0018 a002 DT ACQ Contribution Date Time
1258 //0020 3403 SH REL Modified Image Date (RET)
1259 //0032 0032 DA SDY Study Verified Date
1260 //0032 0034 DA SDY Study Read Date
1261 //0032 1000 DA SDY Scheduled Study Start Date
1262 //0032 1010 DA SDY Scheduled Study Stop Date
1263 //0032 1040 DA SDY Study Arrival Date
1264 //0032 1050 DA SDY Study Completion Date
1265 //0038 001a DA VIS Scheduled Admission Date
1266 //0038 001c DA VIS Scheduled Discharge Date
1267 //0038 0020 DA VIS Admitting Date
1268 //0038 0030 DA VIS Discharge Date
1269 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1270 //0040 0004 DA PRC Scheduled Procedure Step End Date
1271 //0040 0244 DA PRC Performed Procedure Step Start Date
1272 //0040 0250 DA PRC Performed Procedure Step End Date
1273 //0040 2004 DA PRC Issue Date of Imaging Service Request
1274 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1275 //0040 4011 DT PRC Expected Completion Date and Time
1276 //0040 a030 DT PRC Verification Date Time
1277 //0040 a032 DT PRC Observation Date Time
1278 //0040 a120 DT PRC DateTime
1279 //0040 a121 DA PRC Date
1280 //0040 a13a DT PRC Referenced Datetime
1281 //0070 0082 DA ??? Presentation Creation Date
1282 //0100 0420 DT ??? SOP Autorization Date and Time
1283 //0400 0105 DT ??? Digital Signature DateTime
1284 //2100 0040 DA PJ Creation Date
1285 //3006 0008 DA SSET Structure Set Date
1286 //3008 0024 DA ??? Treatment Control Point Date
1287 //3008 0054 DA ??? First Treatment Date
1288 //3008 0056 DA ??? Most Recent Treatment Date
1289 //3008 0162 DA ??? Safe Position Exit Date
1290 //3008 0166 DA ??? Safe Position Return Date
1291 //3008 0250 DA ??? Treatment Date
1292 //300a 0006 DA RT RT Plan Date
1293 //300a 022c DA RT Air Kerma Rate Reference Date
1294 //300e 0004 DA RT Review Date
1300 * \brief gets the info from 0020,0037 : Image Orientation Patient
1301 * @param iop adress of the (6)float aray to receive values
1302 * @return cosines of image orientation patient
1304 void Header::GetImageOrientationPatient( float iop[6] )
1306 std::string strImOriPat;
1307 //iop is supposed to be float[6]
1308 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1310 // 0020 0037 DS REL Image Orientation (Patient)
1311 if ( (strImOriPat = GetEntryByNumber(0x0020,0x0037)) != GDCM_UNFOUND )
1313 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1314 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1316 dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1317 "wrong Image Orientation Patient (0020,0037)");
1321 // 0020 0035 DS REL Image Orientation (RET)
1322 else if ( (strImOriPat = GetEntryByNumber(0x0020,0x0035)) != GDCM_UNFOUND )
1324 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1325 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1327 dbg.Verbose(0, "Header::GetImageOrientationPatient: "
1328 "wrong Image Orientation Patient (0020,0035)");
1334 * \brief Initialize a default DICOM header that should contain all the
1335 * field require by other reader. DICOM standard does not
1336 * explicitely defines those fields, heuristic has been choosen.
1337 * This is not perfect as we are writting a CT image...
1339 void Header::InitializeDefaultHeader()
1346 } DICOM_DEFAULT_VALUE;
1348 std::string date = Util::GetCurrentDate();
1349 std::string time = Util::GetCurrentTime();
1350 std::string uid = Util::CreateUniqueUID();
1351 std::string uidMedia = uid;
1352 std::string uidClass = uid + ".1";
1353 std::string uidInst = uid + ".10";
1354 std::string uidStudy = uid + ".100";
1355 std::string uidSerie = uid + ".1000";
1357 static DICOM_DEFAULT_VALUE defaultvalue[] = {
1358 { "146 ", 0x0002, 0x0000}, // Meta Element Group Length // FIXME: how to recompute ?
1359 { "1.2.840.10008.5.1.4.1.1.2", 0x0002, 0x0002}, // Media Storage SOP Class UID (CT Image Storage)
1360 { uidClass.c_str(), 0x0002, 0x0003}, // Media Storage SOP Instance UID
1361 { uidClass.c_str(), 0x0002, 0x0012}, // META Implementation Class UID
1362 { "GDCM", 0x0002, 0x0016}, // Source Application Entity Title
1364 { date.c_str(), 0x0008, 0x0012}, // Instance Creation Date
1365 { time.c_str(), 0x0008, 0x0013}, // Instance Creation Time
1366 { "1.2.840.10008.5.1.4.1.1.2", 0x0008, 0x0016}, // SOP Class UID
1367 { uidInst.c_str(), 0x0008, 0x0018}, // SOP Instance UID
1368 { "CT", 0x0008, 0x0060}, // Modality
1369 { "GDCM", 0x0008, 0x0070}, // Manufacturer
1370 { "GDCM", 0x0008, 0x0080}, // Institution Name
1371 { "http://www-creatis.insa-lyon.fr/Public/Gdcm", 0x0008, 0x0081}, // Institution Address
1373 { "GDCM", 0x0010, 0x0010}, // Patient's Name
1374 { "GDCMID", 0x0010, 0x0020}, // Patient ID
1376 { uidStudy.c_str(), 0x0020, 0x000d}, // Study Instance UID
1377 { uidSerie.c_str(), 0x0020, 0x000e}, // Series Instance UID
1378 { "1", 0x0020, 0x0010}, // StudyID
1379 { "1", 0x0020, 0x0011}, // SeriesNumber
1381 { "1", 0x0028, 0x0002}, // Samples per pixel 1 or 3
1382 { "MONOCHROME1", 0x0028, 0x0004}, // photochromatic interpretation
1383 { "0", 0x0028, 0x0010}, // nbRows
1384 { "0", 0x0028, 0x0011}, // nbCols
1385 { "8", 0x0028, 0x0100}, // BitsAllocated 8 or 12 or 16
1386 { "8", 0x0028, 0x0101}, // BitsStored <= BitsAllocated
1387 { "7", 0x0028, 0x0102}, // HighBit <= BitsAllocated - 1
1388 { "0", 0x0028, 0x0103}, // Pixel Representation 0(unsigned) or 1(signed)
1393 // Special case this is the image (not a string)
1396 ReplaceOrCreateByNumber(0, 0, GrPixel, NumPixel);
1398 // All remaining strings:
1400 DICOM_DEFAULT_VALUE current = defaultvalue[i];
1401 while( current.value )
1403 ReplaceOrCreateByNumber(current.value, current.group, current.elem);
1404 current = defaultvalue[++i];
1409 //-----------------------------------------------------------------------------
1412 //-----------------------------------------------------------------------------
1414 } // end namespace gdcm