1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/09/29 17:33:17 $
7 Version: $Revision: 1.190 $
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"
28 //-----------------------------------------------------------------------------
29 // Constructor / Destructor
32 * @param filename name of the file whose header we want to analyze
34 gdcmHeader::gdcmHeader( std::string const & filename ):
35 gdcmDocument( filename )
37 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
38 // We may encounter the 'RETired' (0x0028, 0x0200) tag
39 // (Image Location") . This Element contains the number of
40 // the group that contains the pixel data (hence the "Pixel Data"
41 // is found by indirection through the "Image Location").
42 // Inside the group pointed by "Image Location" the searched element
43 // is conventionally the element 0x0010 (when the norm is respected).
44 // When the "Image Location" is absent we default to group 0x7fe0.
45 // Note: this IS the right place for the code
48 std::string imgLocation = GetEntryByNumber(0x0028, 0x0200);
49 if ( imgLocation == GDCM_UNFOUND )
56 GrPixel = (uint16_t) atoi( imgLocation.c_str() );
59 // sometimes Image Location value doesn't follow
60 // the supposed processor endianity.
61 // see gdcmData/cr172241.dcm
62 if ( GrPixel == 0xe07f )
67 if ( GrPixel != 0x7fe0 )
69 // This is a kludge for old dirty Philips imager.
81 gdcmHeader::gdcmHeader()
87 * \brief Canonical destructor.
89 gdcmHeader::~gdcmHeader ()
95 * \brief Performs some consistency checking on various 'File related'
96 * (as opposed to 'DicomDir related') entries
97 * then writes in a file all the (Dicom Elements) included the Pixels
98 * @param fp file pointer on an already open file
99 * @param filetype Type of the File to be written
100 * (ACR-NEMA, ExplicitVR, ImplicitVR)
102 void gdcmHeader::Write(FILE* fp,FileType filetype)
105 if ( GetEntryByNumber(0x0028,0x0100) == "12")
107 SetEntryByNumber("16", 0x0028,0x0100);
110 /// \todo correct 'Pixel group' Length if necessary
112 int i_lgPix = GetEntryLengthByNumber(GrPixel, NumPixel);
115 // no (GrPixel, NumPixel) element
116 char* dumm = new char[20];
117 sprintf(dumm ,"%d", i_lgPix+12);
118 std::string s_lgPix = dumm;
120 ReplaceOrCreateByNumber(s_lgPix,GrPixel, 0x0000);
123 // FIXME : should be nice if we could move it to gdcmFile
124 // (or in future gdcmPixelData class)
126 // Drop Palette Color, if necessary
128 if ( GetEntryByNumber(0x0028,0x0002).c_str()[0] == '3' )
130 // if SamplesPerPixel = 3, sure we don't need any LUT !
131 // Drop 0028|1101, 0028|1102, 0028|1103
132 // Drop 0028|1201, 0028|1202, 0028|1203
134 gdcmDocEntry* e = GetDocEntryByNumber(0x0028,0x01101);
137 RemoveEntryNoDestroy(e);
139 e = GetDocEntryByNumber(0x0028,0x1102);
142 RemoveEntryNoDestroy(e);
144 e = GetDocEntryByNumber(0x0028,0x1103);
147 RemoveEntryNoDestroy(e);
149 e = GetDocEntryByNumber(0x0028,0x01201);
152 RemoveEntryNoDestroy(e);
154 e = GetDocEntryByNumber(0x0028,0x1202);
157 RemoveEntryNoDestroy(e);
159 e = GetDocEntryByNumber(0x0028,0x1203);
162 RemoveEntryNoDestroy(e);
165 gdcmDocument::Write(fp,filetype);
168 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
176 * \brief This predicate, based on hopefully reasonable heuristics,
177 * decides whether or not the current gdcmHeader was properly parsed
178 * and contains the mandatory information for being considered as
179 * a well formed and usable Dicom/Acr File.
180 * @return true when gdcmHeader is the one of a reasonable Dicom/Acr file,
183 bool gdcmHeader::IsReadable()
185 if( !gdcmDocument::IsReadable() )
190 std::string res = GetEntryByNumber(0x0028, 0x0005);
191 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
193 return false; // Image Dimensions
195 if ( !GetDocEntryByNumber(0x0028, 0x0100) )
197 return false; // "Bits Allocated"
199 if ( !GetDocEntryByNumber(0x0028, 0x0101) )
201 return false; // "Bits Stored"
203 if ( !GetDocEntryByNumber(0x0028, 0x0102) )
205 return false; // "High Bit"
207 if ( !GetDocEntryByNumber(0x0028, 0x0103) )
209 return false; // "Pixel Representation" i.e. 'Sign'
216 * \brief Retrieve the number of columns of image.
217 * @return The encountered size when found, 0 by default.
218 * 0 means the file is NOT USABLE. The caller will have to check
220 int gdcmHeader::GetXSize()
223 strSize = GetEntryByNumber(0x0028,0x0011);
224 if ( strSize == GDCM_UNFOUND )
229 return atoi( strSize.c_str() );
233 * \brief Retrieve the number of lines of image.
234 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
235 * @return The encountered size when found, 1 by default
236 * (The ACR-NEMA file contains a Signal, not an Image).
238 int gdcmHeader::GetYSize()
240 std::string strSize = GetEntryByNumber(0x0028,0x0010);
241 if ( strSize != GDCM_UNFOUND )
243 return atoi( strSize.c_str() );
250 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
251 // hence be a signal (1d image). So we default to 1:
256 * \brief Retrieve the number of planes of volume or the number
257 * of frames of a multiframe.
258 * \warning When present we consider the "Number of Frames" as the third
259 * dimension. When absent we consider the third dimension as
260 * being the ACR-NEMA "Planes" tag content.
261 * @return The encountered size when found, 1 by default (single image).
263 int gdcmHeader::GetZSize()
265 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
266 // as the third dimension.
267 std::string strSize = GetEntryByNumber(0x0028,0x0008);
268 if ( strSize != GDCM_UNFOUND )
270 return atoi( strSize.c_str() );
273 // We then consider the "Planes" entry as the third dimension
274 strSize = GetEntryByNumber(0x0028,0x0012);
275 if ( strSize != GDCM_UNFOUND )
277 return atoi( strSize.c_str() );
284 * \brief gets the info from 0028,0030 : Pixel Spacing
286 * @return X dimension of a pixel
288 float gdcmHeader::GetXSpacing()
290 float xspacing, yspacing;
291 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
293 if ( strSpacing == GDCM_UNFOUND )
295 dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
300 if( ( nbValues = sscanf( strSpacing.c_str(),
301 "%f\\%f", &yspacing, &xspacing)) != 2 )
303 // if single value is found, xspacing is defaulted to yspacing
311 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
312 // seems to be a bug in the header ...
313 sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
320 * \brief gets the info from 0028,0030 : Pixel Spacing
322 * @return Y dimension of a pixel
324 float gdcmHeader::GetYSpacing()
327 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
329 if ( strSpacing == GDCM_UNFOUND )
331 dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
335 // if sscanf cannot read any float value, it won't affect yspacing
336 sscanf( strSpacing.c_str(), "%f", &yspacing);
342 * \brief gets the info from 0018,0088 : Space Between Slices
343 * else from 0018,0050 : Slice Thickness
345 * @return Z dimension of a voxel-to be
347 float gdcmHeader::GetZSpacing()
349 // Spacing Between Slices : distance entre le milieu de chaque coupe
350 // Les coupes peuvent etre :
351 // jointives (Spacing between Slices = Slice Thickness)
352 // chevauchantes (Spacing between Slices < Slice Thickness)
353 // disjointes (Spacing between Slices > Slice Thickness)
354 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
355 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
356 // Si le Spacing Between Slices est absent,
357 // on suppose que les coupes sont jointives
359 std::string strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
361 if ( strSpacingBSlices == GDCM_UNFOUND )
363 dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
364 std::string strSliceThickness = GetEntryByNumber(0x0018,0x0050);
365 if ( strSliceThickness == GDCM_UNFOUND )
371 // if no 'Spacing Between Slices' is found,
372 // we assume slices join together
373 // (no overlapping, no interslice gap)
374 // if they don't, we're fucked up
375 return atof( strSliceThickness.c_str() );
380 return atof( strSpacingBSlices.c_str() );
385 *\brief gets the info from 0028,1052 : Rescale Intercept
386 * @return Rescale Intercept
388 float gdcmHeader::GetRescaleIntercept()
391 /// 0028 1052 DS IMG Rescale Intercept
392 std::string strRescInter = GetEntryByNumber(0x0028,0x1052);
393 if ( strRescInter != GDCM_UNFOUND )
395 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
397 // bug in the element 0x0028,0x1052
398 dbg.Verbose(0, "gdcmHeader::GetRescaleIntercept: Rescale Slope "
407 *\brief gets the info from 0028,1053 : Rescale Slope
408 * @return Rescale Slope
410 float gdcmHeader::GetRescaleSlope()
413 //0028 1053 DS IMG Rescale Slope
414 std::string strRescSlope = GetEntryByNumber(0x0028,0x1053);
415 if ( strRescSlope != GDCM_UNFOUND )
417 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
419 // bug in the element 0x0028,0x1053
420 dbg.Verbose(0, "gdcmHeader::GetRescaleSlope: Rescale Slope is empty");
428 * \brief This function is intended to user who doesn't want
429 * to have to manage a LUT and expects to get an RBG Pixel image
430 * (or a monochrome one ...)
431 * \warning to be used with GetImagePixels()
432 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
434 int gdcmHeader::GetNumberOfScalarComponents()
436 if ( GetSamplesPerPixel() == 3 )
441 // 0028 0100 US IMG Bits Allocated
442 // (in order no to be messed up by old RGB images)
443 if ( GetEntryByNumber(0x0028,0x0100) == "24" )
448 std::string strPhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
450 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
452 if ( HasLUT() )// PALETTE COLOR is NOT enough
462 // beware of trailing space at end of string
463 // DICOM tags are never of odd length
464 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
465 strPhotometricInterpretation == "MONOCHROME1 " ||
466 strPhotometricInterpretation == "MONOCHROME2 " )
472 // we assume that *all* kinds of YBR are dealt with
478 * \brief This function is intended to user that DOESN'T want
479 * to get RGB pixels image when it's stored as a PALETTE COLOR image
480 * - the (vtk) user is supposed to know how deal with LUTs -
481 * \warning to be used with GetImagePixelsRaw()
482 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
484 int gdcmHeader::GetNumberOfScalarComponentsRaw()
486 // 0028 0100 US IMG Bits Allocated
487 // (in order no to be messed up by old RGB images)
488 if ( gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24" )
493 // we assume that *all* kinds of YBR are dealt with
494 return GetSamplesPerPixel();
498 // -------------- Remember ! ----------------------------------
500 // Image Position Patient (0020,0032):
501 // If not found (ACR_NEMA) we try Image Position (0020,0030)
502 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
503 // or Location (0020,0050)
504 // as the Z coordinate,
505 // 0. for all the coordinates if nothing is found
507 // \todo find a way to inform the caller nothing was found
508 // \todo How to tell the caller a wrong number of values was found?
510 // ---------------------------------------------------------------
514 * \brief gets the info from 0020,0032 : Image Position Patient
515 * else from 0020,0030 : Image Position (RET)
517 * @return up-left image corner X position
519 float gdcmHeader::GetXOrigin()
521 float xImPos, yImPos, zImPos;
522 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
524 if ( strImPos == GDCM_UNFOUND )
526 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image "
527 "Position Patient (0020,0032)");
528 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
529 if ( strImPos == GDCM_UNFOUND )
531 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image "
532 "Position (RET) (0020,0030)");
533 /// \todo How to tell the caller nothing was found ?
538 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
547 * \brief gets the info from 0020,0032 : Image Position Patient
548 * else from 0020,0030 : Image Position (RET)
550 * @return up-left image corner Y position
552 float gdcmHeader::GetYOrigin()
554 float xImPos, yImPos, zImPos;
555 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
557 if ( strImPos == GDCM_UNFOUND)
559 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image "
560 "Position Patient (0020,0032)");
561 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
562 if ( strImPos == GDCM_UNFOUND )
564 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image "
565 "Position (RET) (0020,0030)");
566 /// \todo How to tell the caller nothing was found ?
571 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
580 * \brief gets the info from 0020,0032 : Image Position Patient
581 * else from 0020,0030 : Image Position (RET)
582 * else from 0020,1041 : Slice Location
583 * else from 0020,0050 : Location
585 * @return up-left image corner Z position
587 float gdcmHeader::GetZOrigin()
589 float xImPos, yImPos, zImPos;
590 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
592 if ( strImPos != GDCM_UNFOUND )
594 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
596 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image "
597 "Position Patient (0020,0032)");
598 return 0.; // bug in the element 0x0020,0x0032
606 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
607 if ( strImPos != GDCM_UNFOUND )
609 if( sscanf( strImPos.c_str(),
610 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
612 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
613 return 0.; // bug in the element 0x0020,0x0032
621 std::string strSliceLocation = GetEntryByNumber(0x0020,0x1041); // for *very* old ACR-NEMA images
622 if ( strSliceLocation != GDCM_UNFOUND )
624 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
626 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Slice Location (0020,1041)");
627 return 0.; // bug in the element 0x0020,0x1041
634 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: unfound Slice Location (0020,1041)");
636 std::string strLocation = GetEntryByNumber(0x0020,0x0050);
637 if ( strLocation != GDCM_UNFOUND )
639 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
641 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Location (0020,0050)");
642 return 0.; // bug in the element 0x0020,0x0050
649 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Location (0020,0050)");
651 return 0.; // Hopeless
655 * \brief gets the info from 0020,0013 : Image Number else 0.
656 * @return image number
658 int gdcmHeader::GetImageNumber()
660 // The function i atoi() takes the address of an area of memory as
661 // parameter and converts the string stored at that location to an integer
662 // using the external decimal to internal binary conversion rules. This may
663 // be preferable to sscanf() since atoi() is a much smaller, simpler and
664 // faster function. sscanf() can do all possible conversions whereas
665 // atoi() can only do single decimal integer conversions.
666 //0020 0013 IS REL Image Number
667 std::string strImNumber = GetEntryByNumber(0x0020,0x0013);
668 if ( strImNumber != GDCM_UNFOUND )
670 return atoi( strImNumber.c_str() );
676 * \brief gets the info from 0008,0060 : Modality
677 * @return Modality Type
679 ModalityType gdcmHeader::GetModality()
681 // 0008 0060 CS ID Modality
682 std::string strModality = GetEntryByNumber(0x0008,0x0060);
683 if ( strModality != GDCM_UNFOUND )
685 if ( strModality.find("AU") < strModality.length()) return AU;
686 else if ( strModality.find("AS") < strModality.length()) return AS;
687 else if ( strModality.find("BI") < strModality.length()) return BI;
688 else if ( strModality.find("CF") < strModality.length()) return CF;
689 else if ( strModality.find("CP") < strModality.length()) return CP;
690 else if ( strModality.find("CR") < strModality.length()) return CR;
691 else if ( strModality.find("CT") < strModality.length()) return CT;
692 else if ( strModality.find("CS") < strModality.length()) return CS;
693 else if ( strModality.find("DD") < strModality.length()) return DD;
694 else if ( strModality.find("DF") < strModality.length()) return DF;
695 else if ( strModality.find("DG") < strModality.length()) return DG;
696 else if ( strModality.find("DM") < strModality.length()) return DM;
697 else if ( strModality.find("DS") < strModality.length()) return DS;
698 else if ( strModality.find("DX") < strModality.length()) return DX;
699 else if ( strModality.find("ECG") < strModality.length()) return ECG;
700 else if ( strModality.find("EPS") < strModality.length()) return EPS;
701 else if ( strModality.find("FA") < strModality.length()) return FA;
702 else if ( strModality.find("FS") < strModality.length()) return FS;
703 else if ( strModality.find("HC") < strModality.length()) return HC;
704 else if ( strModality.find("HD") < strModality.length()) return HD;
705 else if ( strModality.find("LP") < strModality.length()) return LP;
706 else if ( strModality.find("LS") < strModality.length()) return LS;
707 else if ( strModality.find("MA") < strModality.length()) return MA;
708 else if ( strModality.find("MR") < strModality.length()) return MR;
709 else if ( strModality.find("NM") < strModality.length()) return NM;
710 else if ( strModality.find("OT") < strModality.length()) return OT;
711 else if ( strModality.find("PT") < strModality.length()) return PT;
712 else if ( strModality.find("RF") < strModality.length()) return RF;
713 else if ( strModality.find("RG") < strModality.length()) return RG;
714 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
715 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
716 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
717 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
718 else if ( strModality.find("SM") < strModality.length()) return SM;
719 else if ( strModality.find("ST") < strModality.length()) return ST;
720 else if ( strModality.find("TG") < strModality.length()) return TG;
721 else if ( strModality.find("US") < strModality.length()) return US;
722 else if ( strModality.find("VF") < strModality.length()) return VF;
723 else if ( strModality.find("XA") < strModality.length()) return XA;
724 else if ( strModality.find("XC") < strModality.length()) return XC;
728 /// \todo throw error return value ???
729 /// specified <> unknow in our database
738 * \brief Retrieve the number of Bits Stored (actually used)
739 * (as opposite to number of Bits Allocated)
740 * @return The encountered number of Bits Stored, 0 by default.
741 * 0 means the file is NOT USABLE. The caller has to check it !
743 int gdcmHeader::GetBitsStored()
745 std::string strSize = GetEntryByNumber( 0x0028, 0x0101 );
746 if ( strSize == GDCM_UNFOUND )
748 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to "
750 return 0; // It's supposed to be mandatory
751 // the caller will have to check
753 return atoi( strSize.c_str() );
757 * \brief Retrieve the high bit position.
758 * \warning The method defaults to 0 when information is absent.
759 * The responsability of checking this value is left to the caller.
760 * @return The high bit positin when present. 0 when absent.
762 int gdcmHeader::GetHighBitPosition()
764 std::string strSize = GetEntryByNumber( 0x0028, 0x0102 );
765 if ( strSize == GDCM_UNFOUND )
767 dbg.Verbose(0, "gdcmHeader::GetHighBitPosition: this is supposed "
771 return atoi( strSize.c_str() );
775 * \brief Check wether the pixels are signed or UNsigned data.
776 * \warning The method defaults to false (UNsigned) when information is absent.
777 * The responsability of checking this value is left to the caller.
778 * @return True when signed, false when UNsigned
780 bool gdcmHeader::IsSignedPixelData()
782 std::string strSize = GetEntryByNumber( 0x0028, 0x0103 );
783 if ( strSize == GDCM_UNFOUND )
785 dbg.Verbose(0, "gdcmHeader::IsSignedPixelData: this is supposed "
789 int sign = atoi( strSize.c_str() );
798 * \brief Retrieve the number of Bits Allocated
799 * (8, 12 -compacted ACR-NEMA files, 16, ...)
800 * @return The encountered number of Bits Allocated, 0 by default.
801 * 0 means the file is NOT USABLE. The caller has to check it !
803 int gdcmHeader::GetBitsAllocated()
805 std::string strSize = GetEntryByNumber(0x0028,0x0100);
806 if ( strSize == GDCM_UNFOUND )
808 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to "
810 return 0; // It's supposed to be mandatory
811 // the caller will have to check
813 return atoi( strSize.c_str() );
817 * \brief Retrieve the number of Samples Per Pixel
818 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
819 * @return The encountered number of Samples Per Pixel, 1 by default.
820 * (Gray level Pixels)
822 int gdcmHeader::GetSamplesPerPixel()
824 std::string strSize = GetEntryByNumber(0x0028,0x0002);
825 if ( strSize == GDCM_UNFOUND )
827 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to "
829 return 1; // Well, it's supposed to be mandatory ...
830 // but sometimes it's missing : *we* assume Gray pixels
832 return atoi( strSize.c_str() );
836 * \brief Check wether this a monochrome picture or not by accessing
837 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
838 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
840 bool gdcmHeader::IsMonochrome()
842 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
843 if ( PhotometricInterp == "MONOCHROME1 "
844 || PhotometricInterp == "MONOCHROME2 " )
848 if ( PhotometricInterp == GDCM_UNFOUND )
850 dbg.Verbose(0, "gdcmHeader::IsMonochrome: absent Photometric "
857 * \brief Check wether this a "PALETTE COLOR" picture or not by accessing
858 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
859 * @return true when "PALETTE COLOR". False otherwise.
861 bool gdcmHeader::IsPaletteColor()
863 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
864 if ( PhotometricInterp == "PALETTE COLOR " )
868 if ( PhotometricInterp == GDCM_UNFOUND )
870 dbg.Verbose(0, "gdcmHeader::IsPaletteColor: absent Photometric "
877 * \brief Check wether this a "YBR_FULL" color picture or not by accessing
878 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
879 * @return true when "YBR_FULL". False otherwise.
881 bool gdcmHeader::IsYBRFull()
883 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
884 if ( PhotometricInterp == "YBR_FULL" )
888 if ( PhotometricInterp == GDCM_UNFOUND )
890 dbg.Verbose(0, "gdcmHeader::IsYBRFull: absent Photometric "
897 * \brief Retrieve the Planar Configuration for RGB images
898 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
899 * @return The encountered Planar Configuration, 0 by default.
901 int gdcmHeader::GetPlanarConfiguration()
903 std::string strSize = GetEntryByNumber(0x0028,0x0006);
904 if ( strSize == GDCM_UNFOUND )
908 return atoi( strSize.c_str() );
912 * \brief Return the size (in bytes) of a single pixel of data.
913 * @return The size in bytes of a single pixel of data; 0 by default
914 * 0 means the file is NOT USABLE; the caller will have to check
916 int gdcmHeader::GetPixelSize()
918 // 0028 0100 US IMG Bits Allocated
919 // (in order no to be messed up by old RGB images)
920 // if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
923 std::string pixelType = GetPixelType();
924 if ( pixelType == "8U" || pixelType == "8S" )
928 if ( pixelType == "16U" || pixelType == "16S")
932 if ( pixelType == "32U" || pixelType == "32S")
936 if ( pixelType == "FD" )
940 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
945 * \brief Build the Pixel Type of the image.
946 * Possible values are:
947 * - 8U unsigned 8 bit,
949 * - 16U unsigned 16 bit,
950 * - 16S signed 16 bit,
951 * - 32U unsigned 32 bit,
952 * - 32S signed 32 bit,
953 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
954 * \warning 12 bit images appear as 16 bit.
955 * 24 bit images appear as 8 bit
956 * @return 0S if nothing found. NOT USABLE file. The caller has to check
958 std::string gdcmHeader::GetPixelType()
960 std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
961 if ( bitsAlloc == GDCM_UNFOUND )
963 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
967 if ( bitsAlloc == "64" )
971 if ( bitsAlloc == "12" )
973 // It will be unpacked
976 else if ( bitsAlloc == "24" )
978 // (in order no to be messed up
979 bitsAlloc = "8"; // by old RGB images)
982 std::string sign = GetEntryByNumber(0x0028, 0x0103);//"Pixel Representation"
984 if (sign == GDCM_UNFOUND )
986 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
997 return bitsAlloc + sign;
1002 * \brief Recover the offset (from the beginning of the file)
1003 * of *image* pixels (not *icone image* pixels, if any !)
1004 * @return Pixel Offset
1006 size_t gdcmHeader::GetPixelOffset()
1008 gdcmDocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1011 return pxlElement->GetOffset();
1016 std::cout << "Big trouble : Pixel Element ("
1017 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1024 /// \todo TODO : unify those two (previous one and next one)
1026 * \brief Recover the pixel area length (in Bytes)
1027 * @return Pixel Element Length, as stored in the header
1028 * (NOT the memory space necessary to hold the Pixels
1029 * -in case of embeded compressed image-)
1030 * 0 : NOT USABLE file. The caller has to check.
1032 size_t gdcmHeader::GetPixelAreaLength()
1034 gdcmDocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1037 return pxlElement->GetLength();
1042 std::cout << "Big trouble : Pixel Element ("
1043 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1051 * \brief tells us if LUT are used
1052 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1053 * are NOT considered as LUT, since nobody knows
1054 * how to deal with them
1055 * Please warn me if you know sbdy that *does* know ... jprx
1056 * @return true if LUT Descriptors and LUT Tables were found
1058 bool gdcmHeader::HasLUT()
1060 // Check the presence of the LUT Descriptors, and LUT Tables
1062 if ( !GetDocEntryByNumber(0x0028,0x1101) )
1066 // LutDescriptorGreen
1067 if ( !GetDocEntryByNumber(0x0028,0x1102) )
1071 // LutDescriptorBlue
1072 if ( !GetDocEntryByNumber(0x0028,0x1103) )
1076 // Red Palette Color Lookup Table Data
1077 if ( !GetDocEntryByNumber(0x0028,0x1201) )
1081 // Green Palette Color Lookup Table Data
1082 if ( !GetDocEntryByNumber(0x0028,0x1202) )
1086 // Blue Palette Color Lookup Table Data
1087 if ( !GetDocEntryByNumber(0x0028,0x1203) )
1092 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1093 // NOT taken into account, but we don't know how to use it ...
1098 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1100 * @return Lookup Table number of Bits , 0 by default
1101 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1102 * @ return bit number of each LUT item
1104 int gdcmHeader::GetLUTNbits()
1106 std::vector<std::string> tokens;
1109 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1110 // = Lookup Table Desc-Blue
1111 // Consistency already checked in GetLUTLength
1112 std::string lutDescription = GetEntryByNumber(0x0028,0x1101);
1113 if ( lutDescription == GDCM_UNFOUND )
1118 tokens.clear(); // clean any previous value
1119 Tokenize ( lutDescription, tokens, "\\" );
1120 //LutLength=atoi(tokens[0].c_str());
1121 //LutDepth=atoi(tokens[1].c_str());
1123 lutNbits = atoi( tokens[2].c_str() );
1130 * \brief builts Red/Green/Blue/Alpha LUT from Header
1131 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1132 * and (0028,1101),(0028,1102),(0028,1102)
1133 * - xxx Palette Color Lookup Table Descriptor - are found
1134 * and (0028,1201),(0028,1202),(0028,1202)
1135 * - xxx Palette Color Lookup Table Data - are found
1136 * \warning does NOT deal with :
1137 * 0028 1100 Gray Lookup Table Descriptor (Retired)
1138 * 0028 1221 Segmented Red Palette Color Lookup Table Data
1139 * 0028 1222 Segmented Green Palette Color Lookup Table Data
1140 * 0028 1223 Segmented Blue Palette Color Lookup Table Data
1141 * no known Dicom reader deals with them :-(
1142 * @return a RGBA Lookup Table
1144 uint8_t* gdcmHeader::GetLUTRGBA()
1146 // Not so easy : see
1147 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
1149 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
1150 if ( GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR " )
1155 int lengthR, debR, nbitsR;
1156 int lengthG, debG, nbitsG;
1157 int lengthB, debB, nbitsB;
1159 // Get info from Lut Descriptors
1160 // (the 3 LUT descriptors may be different)
1161 std::string lutDescriptionR = GetEntryByNumber(0x0028,0x1101);
1162 if ( lutDescriptionR == GDCM_UNFOUND )
1167 std::string lutDescriptionG = GetEntryByNumber(0x0028,0x1102);
1168 if ( lutDescriptionG == GDCM_UNFOUND )
1173 std::string lutDescriptionB = GetEntryByNumber(0x0028,0x1103);
1174 if ( lutDescriptionB == GDCM_UNFOUND )
1179 // lengthR: Red LUT length in Bytes
1180 // debR: subscript of the first Lut Value
1181 // nbitsR: Lut item size (in Bits)
1182 int nbRead = sscanf( lutDescriptionR.c_str(), "%d\\%d\\%d",
1183 &lengthR, &debR, &nbitsR );
1186 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading red LUT");
1189 // lengthG: Green LUT length in Bytes
1190 // debG: subscript of the first Lut Value
1191 // nbitsG: Lut item size (in Bits)
1192 nbRead = sscanf( lutDescriptionG.c_str(), "%d\\%d\\%d",
1193 &lengthG, &debG, &nbitsG );
1196 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading green LUT");
1199 // lengthB: Blue LUT length in Bytes
1200 // debB: subscript of the first Lut Value
1201 // nbitsB: Lut item size (in Bits)
1202 nbRead = sscanf( lutDescriptionB.c_str(), "%d\\%d\\%d",
1203 &lengthB, &debB, &nbitsB );
1206 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading blue LUT");
1209 // Load LUTs into memory, (as they were stored on disk)
1210 uint8_t* lutR = (uint8_t*) GetEntryBinAreaByNumber(0x0028,0x1201);
1211 uint8_t* lutG = (uint8_t*) GetEntryBinAreaByNumber(0x0028,0x1202);
1212 uint8_t* lutB = (uint8_t*) GetEntryBinAreaByNumber(0x0028,0x1203);
1214 if ( !lutR || !lutG || !lutB )
1216 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble with one of the LUT");
1219 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
1221 uint8_t* LUTRGBA = new uint8_t[1024]; // 256 * 4 (R, G, B, Alpha)
1226 memset(LUTRGBA, 0, 1024);
1230 std::string str_nb = GetEntryByNumber(0x0028,0x0100);
1231 if ( str_nb == GDCM_UNFOUND )
1237 nb = atoi( str_nb.c_str() );
1241 if ( nbitsR == 16 && nb == 8)
1243 // when LUT item size is different than pixel size
1244 mult = 2; // high byte must be = low byte
1248 // See PS 3.3-2003 C.11.1.1.2 p 619
1252 // if we get a black image, let's just remove the '+1'
1253 // from 'i*mult+1' and check again
1254 // if it works, we shall have to check the 3 Palettes
1255 // to see which byte is ==0 (first one, or second one)
1257 // We give up the checking to avoid some (useless ?)overhead
1258 // (optimistic asumption)
1263 for( i=0; i < lengthR; ++i)
1265 *a = lutR[i*mult+1];
1270 for( i=0; i < lengthG; ++i)
1272 *a = lutG[i*mult+1];
1277 for(i=0; i < lengthB; ++i)
1279 *a = lutB[i*mult+1];
1284 for(i=0; i < 256; ++i)
1286 *a = 1; // Alpha component
1293 * \brief Accesses the info from 0002,0010 : Transfert Syntax and gdcmTS
1295 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1297 std::string gdcmHeader::GetTransfertSyntaxName()
1299 // use the gdcmTS (TS : Transfert Syntax)
1300 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1302 if ( transfertSyntax == GDCM_NOTLOADED )
1304 std::cout << "Transfert Syntax not loaded. " << std::endl
1305 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
1307 return "Uncompressed ACR-NEMA";
1309 if ( transfertSyntax == GDCM_UNFOUND )
1311 dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName:"
1312 " unfound Transfert Syntax (0002,0010)");
1313 return "Uncompressed ACR-NEMA";
1316 while ( ! isdigit(transfertSyntax[transfertSyntax.length()-1]) )
1318 transfertSyntax.erase(transfertSyntax.length()-1, 1);
1320 // we do it only when we need it
1321 gdcmTS* ts = gdcmGlobal::GetTS();
1322 std::string tsName = ts->GetValue( transfertSyntax );
1324 //delete ts; /// \todo Seg Fault when deleted ?!
1329 * \brief Sets the Pixel Area size in the Header
1330 * --> not-for-rats function
1331 * @param ImageDataSize new Pixel Area Size
1332 * warning : nothing else is checked
1334 void gdcmHeader::SetImageDataSize(size_t ImageDataSize)
1337 sprintf(car,"%d",ImageDataSize);
1339 gdcmDocEntry *a = GetDocEntryByNumber(GrPixel, NumPixel);
1340 a->SetLength(ImageDataSize);
1343 sprintf(car,"%d",ImageDataSize);
1345 const std::string content1 = car;
1346 SetEntryByNumber(content1, GrPixel, NumPixel);
1349 //-----------------------------------------------------------------------------
1353 * \brief anonymize a Header (removes Patient's personal info)
1354 * (read the code to see which ones ...)
1356 bool gdcmHeader::AnonymizeHeader()
1358 // If exist, replace by spaces
1359 SetEntryByNumber (" ",0x0010, 0x2154); // Telephone
1360 SetEntryByNumber (" ",0x0010, 0x1040); // Adress
1361 SetEntryByNumber (" ",0x0010, 0x0020); // Patient ID
1363 gdcmDocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1365 if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1367 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1368 if ( studyInstanceUID != GDCM_UNFOUND )
1370 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1374 ReplaceOrCreateByNumber(std::string("anonymised"), 0x0010, 0x0010);
1379 // (if any) remove or replace all the stuff that contains a Date
1381 //0008 0012 DA ID Instance Creation Date
1382 //0008 0020 DA ID Study Date
1383 //0008 0021 DA ID Series Date
1384 //0008 0022 DA ID Acquisition Date
1385 //0008 0023 DA ID Content Date
1386 //0008 0024 DA ID Overlay Date
1387 //0008 0025 DA ID Curve Date
1388 //0008 002a DT ID Acquisition Datetime
1389 //0018 9074 DT ACQ Frame Acquisition Datetime
1390 //0018 9151 DT ACQ Frame Reference Datetime
1391 //0018 a002 DT ACQ Contribution Date Time
1392 //0020 3403 SH REL Modified Image Date (RET)
1393 //0032 0032 DA SDY Study Verified Date
1394 //0032 0034 DA SDY Study Read Date
1395 //0032 1000 DA SDY Scheduled Study Start Date
1396 //0032 1010 DA SDY Scheduled Study Stop Date
1397 //0032 1040 DA SDY Study Arrival Date
1398 //0032 1050 DA SDY Study Completion Date
1399 //0038 001a DA VIS Scheduled Admission Date
1400 //0038 001c DA VIS Scheduled Discharge Date
1401 //0038 0020 DA VIS Admitting Date
1402 //0038 0030 DA VIS Discharge Date
1403 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1404 //0040 0004 DA PRC Scheduled Procedure Step End Date
1405 //0040 0244 DA PRC Performed Procedure Step Start Date
1406 //0040 0250 DA PRC Performed Procedure Step End Date
1407 //0040 2004 DA PRC Issue Date of Imaging Service Request
1408 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1409 //0040 4011 DT PRC Expected Completion Date and Time
1410 //0040 a030 DT PRC Verification Date Time
1411 //0040 a032 DT PRC Observation Date Time
1412 //0040 a120 DT PRC DateTime
1413 //0040 a121 DA PRC Date
1414 //0040 a13a DT PRC Referenced Datetime
1415 //0070 0082 DA ??? Presentation Creation Date
1416 //0100 0420 DT ??? SOP Autorization Date and Time
1417 //0400 0105 DT ??? Digital Signature DateTime
1418 //2100 0040 DA PJ Creation Date
1419 //3006 0008 DA SSET Structure Set Date
1420 //3008 0024 DA ??? Treatment Control Point Date
1421 //3008 0054 DA ??? First Treatment Date
1422 //3008 0056 DA ??? Most Recent Treatment Date
1423 //3008 0162 DA ??? Safe Position Exit Date
1424 //3008 0166 DA ??? Safe Position Return Date
1425 //3008 0250 DA ??? Treatment Date
1426 //300a 0006 DA RT RT Plan Date
1427 //300a 022c DA RT Air Kerma Rate Reference Date
1428 //300e 0004 DA RT Review Date
1434 * \brief gets the info from 0020,0037 : Image Orientation Patient
1435 * @param iop adress of the (6)float aray to receive values
1436 * @return cosines of image orientation patient
1438 void gdcmHeader::GetImageOrientationPatient( float iop[6] )
1440 std::string strImOriPat;
1441 //iop is supposed to be float[6]
1442 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1444 // 0020 0037 DS REL Image Orientation (Patient)
1445 if ( (strImOriPat = GetEntryByNumber(0x0020,0x0037)) != GDCM_UNFOUND )
1447 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1448 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1450 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: "
1451 "wrong Image Orientation Patient (0020,0037)");
1455 // 0020 0035 DS REL Image Orientation (RET)
1456 else if ( (strImOriPat = GetEntryByNumber(0x0020,0x0035)) != GDCM_UNFOUND )
1458 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1459 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1461 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: "
1462 "wrong Image Orientation Patient (0020,0035)");
1467 //-----------------------------------------------------------------------------
1470 //-----------------------------------------------------------------------------