1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/10/10 00:42:54 $
7 Version: $Revision: 1.192 $
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
117 s_lgPix = gdcmUtil::Format("%d", i_lgPix+12);
118 ReplaceOrCreateByNumber(s_lgPix,GrPixel, 0x0000);
121 // FIXME : should be nice if we could move it to gdcmFile
122 // (or in future gdcmPixelData class)
124 // Drop Palette Color, if necessary
126 if ( GetEntryByNumber(0x0028,0x0002).c_str()[0] == '3' )
128 // if SamplesPerPixel = 3, sure we don't need any LUT !
129 // Drop 0028|1101, 0028|1102, 0028|1103
130 // Drop 0028|1201, 0028|1202, 0028|1203
132 gdcmDocEntry* e = GetDocEntryByNumber(0x0028,0x01101);
135 RemoveEntryNoDestroy(e);
137 e = GetDocEntryByNumber(0x0028,0x1102);
140 RemoveEntryNoDestroy(e);
142 e = GetDocEntryByNumber(0x0028,0x1103);
145 RemoveEntryNoDestroy(e);
147 e = GetDocEntryByNumber(0x0028,0x01201);
150 RemoveEntryNoDestroy(e);
152 e = GetDocEntryByNumber(0x0028,0x1202);
155 RemoveEntryNoDestroy(e);
157 e = GetDocEntryByNumber(0x0028,0x1203);
160 RemoveEntryNoDestroy(e);
163 gdcmDocument::Write(fp,filetype);
166 //-----------------------------------------------------------------------------
170 //-----------------------------------------------------------------------------
174 * \brief This predicate, based on hopefully reasonable heuristics,
175 * decides whether or not the current gdcmHeader was properly parsed
176 * and contains the mandatory information for being considered as
177 * a well formed and usable Dicom/Acr File.
178 * @return true when gdcmHeader is the one of a reasonable Dicom/Acr file,
181 bool gdcmHeader::IsReadable()
183 if( !gdcmDocument::IsReadable() )
188 std::string res = GetEntryByNumber(0x0028, 0x0005);
189 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
191 return false; // Image Dimensions
193 if ( !GetDocEntryByNumber(0x0028, 0x0100) )
195 return false; // "Bits Allocated"
197 if ( !GetDocEntryByNumber(0x0028, 0x0101) )
199 return false; // "Bits Stored"
201 if ( !GetDocEntryByNumber(0x0028, 0x0102) )
203 return false; // "High Bit"
205 if ( !GetDocEntryByNumber(0x0028, 0x0103) )
207 return false; // "Pixel Representation" i.e. 'Sign'
214 * \brief Retrieve the number of columns of image.
215 * @return The encountered size when found, 0 by default.
216 * 0 means the file is NOT USABLE. The caller will have to check
218 int gdcmHeader::GetXSize()
221 strSize = GetEntryByNumber(0x0028,0x0011);
222 if ( strSize == GDCM_UNFOUND )
227 return atoi( strSize.c_str() );
231 * \brief Retrieve the number of lines of image.
232 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
233 * @return The encountered size when found, 1 by default
234 * (The ACR-NEMA file contains a Signal, not an Image).
236 int gdcmHeader::GetYSize()
238 std::string strSize = GetEntryByNumber(0x0028,0x0010);
239 if ( strSize != GDCM_UNFOUND )
241 return atoi( strSize.c_str() );
248 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
249 // hence be a signal (1d image). So we default to 1:
254 * \brief Retrieve the number of planes of volume or the number
255 * of frames of a multiframe.
256 * \warning When present we consider the "Number of Frames" as the third
257 * dimension. When absent we consider the third dimension as
258 * being the ACR-NEMA "Planes" tag content.
259 * @return The encountered size when found, 1 by default (single image).
261 int gdcmHeader::GetZSize()
263 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
264 // as the third dimension.
265 std::string strSize = GetEntryByNumber(0x0028,0x0008);
266 if ( strSize != GDCM_UNFOUND )
268 return atoi( strSize.c_str() );
271 // We then consider the "Planes" entry as the third dimension
272 strSize = GetEntryByNumber(0x0028,0x0012);
273 if ( strSize != GDCM_UNFOUND )
275 return atoi( strSize.c_str() );
282 * \brief gets the info from 0028,0030 : Pixel Spacing
284 * @return X dimension of a pixel
286 float gdcmHeader::GetXSpacing()
288 float xspacing, yspacing;
289 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
291 if ( strSpacing == GDCM_UNFOUND )
293 dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
298 if( ( nbValues = sscanf( strSpacing.c_str(),
299 "%f\\%f", &yspacing, &xspacing)) != 2 )
301 // if single value is found, xspacing is defaulted to yspacing
309 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
310 // seems to be a bug in the header ...
311 sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
318 * \brief gets the info from 0028,0030 : Pixel Spacing
320 * @return Y dimension of a pixel
322 float gdcmHeader::GetYSpacing()
325 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
327 if ( strSpacing == GDCM_UNFOUND )
329 dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
333 // if sscanf cannot read any float value, it won't affect yspacing
334 sscanf( strSpacing.c_str(), "%f", &yspacing);
340 * \brief gets the info from 0018,0088 : Space Between Slices
341 * else from 0018,0050 : Slice Thickness
343 * @return Z dimension of a voxel-to be
345 float gdcmHeader::GetZSpacing()
347 // Spacing Between Slices : distance entre le milieu de chaque coupe
348 // Les coupes peuvent etre :
349 // jointives (Spacing between Slices = Slice Thickness)
350 // chevauchantes (Spacing between Slices < Slice Thickness)
351 // disjointes (Spacing between Slices > Slice Thickness)
352 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
353 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
354 // Si le Spacing Between Slices est absent,
355 // on suppose que les coupes sont jointives
357 std::string strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
359 if ( strSpacingBSlices == GDCM_UNFOUND )
361 dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
362 std::string strSliceThickness = GetEntryByNumber(0x0018,0x0050);
363 if ( strSliceThickness == GDCM_UNFOUND )
369 // if no 'Spacing Between Slices' is found,
370 // we assume slices join together
371 // (no overlapping, no interslice gap)
372 // if they don't, we're fucked up
373 return atof( strSliceThickness.c_str() );
378 return atof( strSpacingBSlices.c_str() );
383 *\brief gets the info from 0028,1052 : Rescale Intercept
384 * @return Rescale Intercept
386 float gdcmHeader::GetRescaleIntercept()
389 /// 0028 1052 DS IMG Rescale Intercept
390 std::string strRescInter = GetEntryByNumber(0x0028,0x1052);
391 if ( strRescInter != GDCM_UNFOUND )
393 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
395 // bug in the element 0x0028,0x1052
396 dbg.Verbose(0, "gdcmHeader::GetRescaleIntercept: Rescale Slope "
405 *\brief gets the info from 0028,1053 : Rescale Slope
406 * @return Rescale Slope
408 float gdcmHeader::GetRescaleSlope()
411 //0028 1053 DS IMG Rescale Slope
412 std::string strRescSlope = GetEntryByNumber(0x0028,0x1053);
413 if ( strRescSlope != GDCM_UNFOUND )
415 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
417 // bug in the element 0x0028,0x1053
418 dbg.Verbose(0, "gdcmHeader::GetRescaleSlope: Rescale Slope is empty");
426 * \brief This function is intended to user who doesn't want
427 * to have to manage a LUT and expects to get an RBG Pixel image
428 * (or a monochrome one ...)
429 * \warning to be used with GetImagePixels()
430 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
432 int gdcmHeader::GetNumberOfScalarComponents()
434 if ( GetSamplesPerPixel() == 3 )
439 // 0028 0100 US IMG Bits Allocated
440 // (in order no to be messed up by old RGB images)
441 if ( GetEntryByNumber(0x0028,0x0100) == "24" )
446 std::string strPhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
448 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
450 if ( HasLUT() )// PALETTE COLOR is NOT enough
460 // beware of trailing space at end of string
461 // DICOM tags are never of odd length
462 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
463 strPhotometricInterpretation == "MONOCHROME1 " ||
464 strPhotometricInterpretation == "MONOCHROME2 " )
470 // we assume that *all* kinds of YBR are dealt with
476 * \brief This function is intended to user that DOESN'T want
477 * to get RGB pixels image when it's stored as a PALETTE COLOR image
478 * - the (vtk) user is supposed to know how deal with LUTs -
479 * \warning to be used with GetImagePixelsRaw()
480 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
482 int gdcmHeader::GetNumberOfScalarComponentsRaw()
484 // 0028 0100 US IMG Bits Allocated
485 // (in order no to be messed up by old RGB images)
486 if ( gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24" )
491 // we assume that *all* kinds of YBR are dealt with
492 return GetSamplesPerPixel();
496 // -------------- Remember ! ----------------------------------
498 // Image Position Patient (0020,0032):
499 // If not found (ACR_NEMA) we try Image Position (0020,0030)
500 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
501 // or Location (0020,0050)
502 // as the Z coordinate,
503 // 0. for all the coordinates if nothing is found
505 // \todo find a way to inform the caller nothing was found
506 // \todo How to tell the caller a wrong number of values was found?
508 // ---------------------------------------------------------------
512 * \brief gets the info from 0020,0032 : Image Position Patient
513 * else from 0020,0030 : Image Position (RET)
515 * @return up-left image corner X position
517 float gdcmHeader::GetXOrigin()
519 float xImPos, yImPos, zImPos;
520 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
522 if ( strImPos == GDCM_UNFOUND )
524 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image "
525 "Position Patient (0020,0032)");
526 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
527 if ( strImPos == GDCM_UNFOUND )
529 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image "
530 "Position (RET) (0020,0030)");
531 /// \todo How to tell the caller nothing was found ?
536 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
545 * \brief gets the info from 0020,0032 : Image Position Patient
546 * else from 0020,0030 : Image Position (RET)
548 * @return up-left image corner Y position
550 float gdcmHeader::GetYOrigin()
552 float xImPos, yImPos, zImPos;
553 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
555 if ( strImPos == GDCM_UNFOUND)
557 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image "
558 "Position Patient (0020,0032)");
559 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
560 if ( strImPos == GDCM_UNFOUND )
562 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image "
563 "Position (RET) (0020,0030)");
564 /// \todo How to tell the caller nothing was found ?
569 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
578 * \brief gets the info from 0020,0032 : Image Position Patient
579 * else from 0020,0030 : Image Position (RET)
580 * else from 0020,1041 : Slice Location
581 * else from 0020,0050 : Location
583 * @return up-left image corner Z position
585 float gdcmHeader::GetZOrigin()
587 float xImPos, yImPos, zImPos;
588 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
590 if ( strImPos != GDCM_UNFOUND )
592 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
594 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image "
595 "Position Patient (0020,0032)");
596 return 0.; // bug in the element 0x0020,0x0032
604 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
605 if ( strImPos != GDCM_UNFOUND )
607 if( sscanf( strImPos.c_str(),
608 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
610 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
611 return 0.; // bug in the element 0x0020,0x0032
619 std::string strSliceLocation = GetEntryByNumber(0x0020,0x1041); // for *very* old ACR-NEMA images
620 if ( strSliceLocation != GDCM_UNFOUND )
622 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
624 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Slice Location (0020,1041)");
625 return 0.; // bug in the element 0x0020,0x1041
632 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: unfound Slice Location (0020,1041)");
634 std::string strLocation = GetEntryByNumber(0x0020,0x0050);
635 if ( strLocation != GDCM_UNFOUND )
637 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
639 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Location (0020,0050)");
640 return 0.; // bug in the element 0x0020,0x0050
647 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Location (0020,0050)");
649 return 0.; // Hopeless
653 * \brief gets the info from 0020,0013 : Image Number else 0.
654 * @return image number
656 int gdcmHeader::GetImageNumber()
658 // The function i atoi() takes the address of an area of memory as
659 // parameter and converts the string stored at that location to an integer
660 // using the external decimal to internal binary conversion rules. This may
661 // be preferable to sscanf() since atoi() is a much smaller, simpler and
662 // faster function. sscanf() can do all possible conversions whereas
663 // atoi() can only do single decimal integer conversions.
664 //0020 0013 IS REL Image Number
665 std::string strImNumber = GetEntryByNumber(0x0020,0x0013);
666 if ( strImNumber != GDCM_UNFOUND )
668 return atoi( strImNumber.c_str() );
674 * \brief gets the info from 0008,0060 : Modality
675 * @return Modality Type
677 ModalityType gdcmHeader::GetModality()
679 // 0008 0060 CS ID Modality
680 std::string strModality = GetEntryByNumber(0x0008,0x0060);
681 if ( strModality != GDCM_UNFOUND )
683 if ( strModality.find("AU") < strModality.length()) return AU;
684 else if ( strModality.find("AS") < strModality.length()) return AS;
685 else if ( strModality.find("BI") < strModality.length()) return BI;
686 else if ( strModality.find("CF") < strModality.length()) return CF;
687 else if ( strModality.find("CP") < strModality.length()) return CP;
688 else if ( strModality.find("CR") < strModality.length()) return CR;
689 else if ( strModality.find("CT") < strModality.length()) return CT;
690 else if ( strModality.find("CS") < strModality.length()) return CS;
691 else if ( strModality.find("DD") < strModality.length()) return DD;
692 else if ( strModality.find("DF") < strModality.length()) return DF;
693 else if ( strModality.find("DG") < strModality.length()) return DG;
694 else if ( strModality.find("DM") < strModality.length()) return DM;
695 else if ( strModality.find("DS") < strModality.length()) return DS;
696 else if ( strModality.find("DX") < strModality.length()) return DX;
697 else if ( strModality.find("ECG") < strModality.length()) return ECG;
698 else if ( strModality.find("EPS") < strModality.length()) return EPS;
699 else if ( strModality.find("FA") < strModality.length()) return FA;
700 else if ( strModality.find("FS") < strModality.length()) return FS;
701 else if ( strModality.find("HC") < strModality.length()) return HC;
702 else if ( strModality.find("HD") < strModality.length()) return HD;
703 else if ( strModality.find("LP") < strModality.length()) return LP;
704 else if ( strModality.find("LS") < strModality.length()) return LS;
705 else if ( strModality.find("MA") < strModality.length()) return MA;
706 else if ( strModality.find("MR") < strModality.length()) return MR;
707 else if ( strModality.find("NM") < strModality.length()) return NM;
708 else if ( strModality.find("OT") < strModality.length()) return OT;
709 else if ( strModality.find("PT") < strModality.length()) return PT;
710 else if ( strModality.find("RF") < strModality.length()) return RF;
711 else if ( strModality.find("RG") < strModality.length()) return RG;
712 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
713 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
714 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
715 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
716 else if ( strModality.find("SM") < strModality.length()) return SM;
717 else if ( strModality.find("ST") < strModality.length()) return ST;
718 else if ( strModality.find("TG") < strModality.length()) return TG;
719 else if ( strModality.find("US") < strModality.length()) return US;
720 else if ( strModality.find("VF") < strModality.length()) return VF;
721 else if ( strModality.find("XA") < strModality.length()) return XA;
722 else if ( strModality.find("XC") < strModality.length()) return XC;
726 /// \todo throw error return value ???
727 /// specified <> unknow in our database
736 * \brief Retrieve the number of Bits Stored (actually used)
737 * (as opposite to number of Bits Allocated)
738 * @return The encountered number of Bits Stored, 0 by default.
739 * 0 means the file is NOT USABLE. The caller has to check it !
741 int gdcmHeader::GetBitsStored()
743 std::string strSize = GetEntryByNumber( 0x0028, 0x0101 );
744 if ( strSize == GDCM_UNFOUND )
746 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to "
748 return 0; // It's supposed to be mandatory
749 // the caller will have to check
751 return atoi( strSize.c_str() );
755 * \brief Retrieve the high bit position.
756 * \warning The method defaults to 0 when information is absent.
757 * The responsability of checking this value is left to the caller.
758 * @return The high bit positin when present. 0 when absent.
760 int gdcmHeader::GetHighBitPosition()
762 std::string strSize = GetEntryByNumber( 0x0028, 0x0102 );
763 if ( strSize == GDCM_UNFOUND )
765 dbg.Verbose(0, "gdcmHeader::GetHighBitPosition: this is supposed "
769 return atoi( strSize.c_str() );
773 * \brief Check wether the pixels are signed or UNsigned data.
774 * \warning The method defaults to false (UNsigned) when information is absent.
775 * The responsability of checking this value is left to the caller.
776 * @return True when signed, false when UNsigned
778 bool gdcmHeader::IsSignedPixelData()
780 std::string strSize = GetEntryByNumber( 0x0028, 0x0103 );
781 if ( strSize == GDCM_UNFOUND )
783 dbg.Verbose(0, "gdcmHeader::IsSignedPixelData: this is supposed "
787 int sign = atoi( strSize.c_str() );
796 * \brief Retrieve the number of Bits Allocated
797 * (8, 12 -compacted ACR-NEMA files, 16, ...)
798 * @return The encountered number of Bits Allocated, 0 by default.
799 * 0 means the file is NOT USABLE. The caller has to check it !
801 int gdcmHeader::GetBitsAllocated()
803 std::string strSize = GetEntryByNumber(0x0028,0x0100);
804 if ( strSize == GDCM_UNFOUND )
806 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to "
808 return 0; // It's supposed to be mandatory
809 // the caller will have to check
811 return atoi( strSize.c_str() );
815 * \brief Retrieve the number of Samples Per Pixel
816 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
817 * @return The encountered number of Samples Per Pixel, 1 by default.
818 * (Gray level Pixels)
820 int gdcmHeader::GetSamplesPerPixel()
822 std::string strSize = GetEntryByNumber(0x0028,0x0002);
823 if ( strSize == GDCM_UNFOUND )
825 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to "
827 return 1; // Well, it's supposed to be mandatory ...
828 // but sometimes it's missing : *we* assume Gray pixels
830 return atoi( strSize.c_str() );
834 * \brief Check wether this a monochrome picture or not by accessing
835 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
836 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
838 bool gdcmHeader::IsMonochrome()
840 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
841 if ( PhotometricInterp == "MONOCHROME1 "
842 || PhotometricInterp == "MONOCHROME2 " )
846 if ( PhotometricInterp == GDCM_UNFOUND )
848 dbg.Verbose(0, "gdcmHeader::IsMonochrome: absent Photometric "
855 * \brief Check wether this a "PALETTE COLOR" picture or not by accessing
856 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
857 * @return true when "PALETTE COLOR". False otherwise.
859 bool gdcmHeader::IsPaletteColor()
861 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
862 if ( PhotometricInterp == "PALETTE COLOR " )
866 if ( PhotometricInterp == GDCM_UNFOUND )
868 dbg.Verbose(0, "gdcmHeader::IsPaletteColor: absent Photometric "
875 * \brief Check wether this a "YBR_FULL" color picture or not by accessing
876 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
877 * @return true when "YBR_FULL". False otherwise.
879 bool gdcmHeader::IsYBRFull()
881 std::string PhotometricInterp = GetEntryByNumber( 0x0028, 0x0004 );
882 if ( PhotometricInterp == "YBR_FULL" )
886 if ( PhotometricInterp == GDCM_UNFOUND )
888 dbg.Verbose(0, "gdcmHeader::IsYBRFull: absent Photometric "
895 * \brief Retrieve the Planar Configuration for RGB images
896 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
897 * @return The encountered Planar Configuration, 0 by default.
899 int gdcmHeader::GetPlanarConfiguration()
901 std::string strSize = GetEntryByNumber(0x0028,0x0006);
902 if ( strSize == GDCM_UNFOUND )
906 return atoi( strSize.c_str() );
910 * \brief Return the size (in bytes) of a single pixel of data.
911 * @return The size in bytes of a single pixel of data; 0 by default
912 * 0 means the file is NOT USABLE; the caller will have to check
914 int gdcmHeader::GetPixelSize()
916 // 0028 0100 US IMG Bits Allocated
917 // (in order no to be messed up by old RGB images)
918 // if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
921 std::string pixelType = GetPixelType();
922 if ( pixelType == "8U" || pixelType == "8S" )
926 if ( pixelType == "16U" || pixelType == "16S")
930 if ( pixelType == "32U" || pixelType == "32S")
934 if ( pixelType == "FD" )
938 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
943 * \brief Build the Pixel Type of the image.
944 * Possible values are:
945 * - 8U unsigned 8 bit,
947 * - 16U unsigned 16 bit,
948 * - 16S signed 16 bit,
949 * - 32U unsigned 32 bit,
950 * - 32S signed 32 bit,
951 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
952 * \warning 12 bit images appear as 16 bit.
953 * 24 bit images appear as 8 bit
954 * @return 0S if nothing found. NOT USABLE file. The caller has to check
956 std::string gdcmHeader::GetPixelType()
958 std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
959 if ( bitsAlloc == GDCM_UNFOUND )
961 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
965 if ( bitsAlloc == "64" )
969 if ( bitsAlloc == "12" )
971 // It will be unpacked
974 else if ( bitsAlloc == "24" )
976 // (in order no to be messed up
977 bitsAlloc = "8"; // by old RGB images)
980 std::string sign = GetEntryByNumber(0x0028, 0x0103);//"Pixel Representation"
982 if (sign == GDCM_UNFOUND )
984 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
995 return bitsAlloc + sign;
1000 * \brief Recover the offset (from the beginning of the file)
1001 * of *image* pixels (not *icone image* pixels, if any !)
1002 * @return Pixel Offset
1004 size_t gdcmHeader::GetPixelOffset()
1006 gdcmDocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1009 return pxlElement->GetOffset();
1014 std::cout << "Big trouble : Pixel Element ("
1015 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1022 /// \todo TODO : unify those two (previous one and next one)
1024 * \brief Recover the pixel area length (in Bytes)
1025 * @return Pixel Element Length, as stored in the header
1026 * (NOT the memory space necessary to hold the Pixels
1027 * -in case of embeded compressed image-)
1028 * 0 : NOT USABLE file. The caller has to check.
1030 size_t gdcmHeader::GetPixelAreaLength()
1032 gdcmDocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
1035 return pxlElement->GetLength();
1040 std::cout << "Big trouble : Pixel Element ("
1041 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1049 * \brief tells us if LUT are used
1050 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1051 * are NOT considered as LUT, since nobody knows
1052 * how to deal with them
1053 * Please warn me if you know sbdy that *does* know ... jprx
1054 * @return true if LUT Descriptors and LUT Tables were found
1056 bool gdcmHeader::HasLUT()
1058 // Check the presence of the LUT Descriptors, and LUT Tables
1060 if ( !GetDocEntryByNumber(0x0028,0x1101) )
1064 // LutDescriptorGreen
1065 if ( !GetDocEntryByNumber(0x0028,0x1102) )
1069 // LutDescriptorBlue
1070 if ( !GetDocEntryByNumber(0x0028,0x1103) )
1074 // Red Palette Color Lookup Table Data
1075 if ( !GetDocEntryByNumber(0x0028,0x1201) )
1079 // Green Palette Color Lookup Table Data
1080 if ( !GetDocEntryByNumber(0x0028,0x1202) )
1084 // Blue Palette Color Lookup Table Data
1085 if ( !GetDocEntryByNumber(0x0028,0x1203) )
1090 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1091 // NOT taken into account, but we don't know how to use it ...
1096 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1098 * @return Lookup Table number of Bits , 0 by default
1099 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1100 * @ return bit number of each LUT item
1102 int gdcmHeader::GetLUTNbits()
1104 std::vector<std::string> tokens;
1107 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1108 // = Lookup Table Desc-Blue
1109 // Consistency already checked in GetLUTLength
1110 std::string lutDescription = GetEntryByNumber(0x0028,0x1101);
1111 if ( lutDescription == GDCM_UNFOUND )
1116 tokens.clear(); // clean any previous value
1117 gdcmUtil::Tokenize ( lutDescription, tokens, "\\" );
1118 //LutLength=atoi(tokens[0].c_str());
1119 //LutDepth=atoi(tokens[1].c_str());
1121 lutNbits = atoi( tokens[2].c_str() );
1128 * \brief builts Red/Green/Blue/Alpha LUT from Header
1129 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1130 * and (0028,1101),(0028,1102),(0028,1102)
1131 * - xxx Palette Color Lookup Table Descriptor - are found
1132 * and (0028,1201),(0028,1202),(0028,1202)
1133 * - xxx Palette Color Lookup Table Data - are found
1134 * \warning does NOT deal with :
1135 * 0028 1100 Gray Lookup Table Descriptor (Retired)
1136 * 0028 1221 Segmented Red Palette Color Lookup Table Data
1137 * 0028 1222 Segmented Green Palette Color Lookup Table Data
1138 * 0028 1223 Segmented Blue Palette Color Lookup Table Data
1139 * no known Dicom reader deals with them :-(
1140 * @return a RGBA Lookup Table
1142 uint8_t* gdcmHeader::GetLUTRGBA()
1144 // Not so easy : see
1145 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
1147 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
1148 if ( GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR " )
1153 int lengthR, debR, nbitsR;
1154 int lengthG, debG, nbitsG;
1155 int lengthB, debB, nbitsB;
1157 // Get info from Lut Descriptors
1158 // (the 3 LUT descriptors may be different)
1159 std::string lutDescriptionR = GetEntryByNumber(0x0028,0x1101);
1160 if ( lutDescriptionR == GDCM_UNFOUND )
1165 std::string lutDescriptionG = GetEntryByNumber(0x0028,0x1102);
1166 if ( lutDescriptionG == GDCM_UNFOUND )
1171 std::string lutDescriptionB = GetEntryByNumber(0x0028,0x1103);
1172 if ( lutDescriptionB == GDCM_UNFOUND )
1177 // lengthR: Red LUT length in Bytes
1178 // debR: subscript of the first Lut Value
1179 // nbitsR: Lut item size (in Bits)
1180 int nbRead = sscanf( lutDescriptionR.c_str(), "%d\\%d\\%d",
1181 &lengthR, &debR, &nbitsR );
1184 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading red LUT");
1187 // lengthG: Green LUT length in Bytes
1188 // debG: subscript of the first Lut Value
1189 // nbitsG: Lut item size (in Bits)
1190 nbRead = sscanf( lutDescriptionG.c_str(), "%d\\%d\\%d",
1191 &lengthG, &debG, &nbitsG );
1194 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading green LUT");
1197 // lengthB: Blue LUT length in Bytes
1198 // debB: subscript of the first Lut Value
1199 // nbitsB: Lut item size (in Bits)
1200 nbRead = sscanf( lutDescriptionB.c_str(), "%d\\%d\\%d",
1201 &lengthB, &debB, &nbitsB );
1204 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading blue LUT");
1207 // Load LUTs into memory, (as they were stored on disk)
1208 uint8_t* lutR = (uint8_t*) GetEntryBinAreaByNumber(0x0028,0x1201);
1209 uint8_t* lutG = (uint8_t*) GetEntryBinAreaByNumber(0x0028,0x1202);
1210 uint8_t* lutB = (uint8_t*) GetEntryBinAreaByNumber(0x0028,0x1203);
1212 if ( !lutR || !lutG || !lutB )
1214 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble with one of the LUT");
1217 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
1219 uint8_t* LUTRGBA = new uint8_t[1024]; // 256 * 4 (R, G, B, Alpha)
1224 memset(LUTRGBA, 0, 1024);
1228 std::string str_nb = GetEntryByNumber(0x0028,0x0100);
1229 if ( str_nb == GDCM_UNFOUND )
1235 nb = atoi( str_nb.c_str() );
1239 if ( nbitsR == 16 && nb == 8)
1241 // when LUT item size is different than pixel size
1242 mult = 2; // high byte must be = low byte
1246 // See PS 3.3-2003 C.11.1.1.2 p 619
1250 // if we get a black image, let's just remove the '+1'
1251 // from 'i*mult+1' and check again
1252 // if it works, we shall have to check the 3 Palettes
1253 // to see which byte is ==0 (first one, or second one)
1255 // We give up the checking to avoid some (useless ?)overhead
1256 // (optimistic asumption)
1261 for( i=0; i < lengthR; ++i)
1263 *a = lutR[i*mult+1];
1268 for( i=0; i < lengthG; ++i)
1270 *a = lutG[i*mult+1];
1275 for(i=0; i < lengthB; ++i)
1277 *a = lutB[i*mult+1];
1282 for(i=0; i < 256; ++i)
1284 *a = 1; // Alpha component
1291 * \brief Accesses the info from 0002,0010 : Transfert Syntax and gdcmTS
1293 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1295 std::string gdcmHeader::GetTransfertSyntaxName()
1297 // use the gdcmTS (TS : Transfert Syntax)
1298 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1300 if ( transfertSyntax == GDCM_NOTLOADED )
1302 std::cout << "Transfert Syntax not loaded. " << std::endl
1303 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
1305 return "Uncompressed ACR-NEMA";
1307 if ( transfertSyntax == GDCM_UNFOUND )
1309 dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName:"
1310 " unfound Transfert Syntax (0002,0010)");
1311 return "Uncompressed ACR-NEMA";
1314 while ( ! isdigit(transfertSyntax[transfertSyntax.length()-1]) )
1316 transfertSyntax.erase(transfertSyntax.length()-1, 1);
1318 // we do it only when we need it
1319 gdcmTS* ts = gdcmGlobal::GetTS();
1320 std::string tsName = ts->GetValue( transfertSyntax );
1322 //delete ts; /// \todo Seg Fault when deleted ?!
1327 * \brief Sets the Pixel Area size in the Header
1328 * --> not-for-rats function
1329 * @param ImageDataSize new Pixel Area Size
1330 * warning : nothing else is checked
1332 void gdcmHeader::SetImageDataSize(size_t ImageDataSize)
1334 ///FIXME I don't understand this code wh ydo we set two times 'car' ?
1335 std::string car = gdcmUtil::Format("%d", ImageDataSize);
1337 gdcmDocEntry *a = GetDocEntryByNumber(GrPixel, NumPixel);
1338 a->SetLength(ImageDataSize);
1341 car = gdcmUtil::Format("%d", ImageDataSize);
1343 SetEntryByNumber(car, GrPixel, NumPixel);
1346 //-----------------------------------------------------------------------------
1350 * \brief anonymize a Header (removes Patient's personal info)
1351 * (read the code to see which ones ...)
1353 bool gdcmHeader::AnonymizeHeader()
1355 // If exist, replace by spaces
1356 SetEntryByNumber (" ",0x0010, 0x2154); // Telephone
1357 SetEntryByNumber (" ",0x0010, 0x1040); // Adress
1358 SetEntryByNumber (" ",0x0010, 0x0020); // Patient ID
1360 gdcmDocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1362 if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1364 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1365 if ( studyInstanceUID != GDCM_UNFOUND )
1367 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1371 ReplaceOrCreateByNumber(std::string("anonymised"), 0x0010, 0x0010);
1376 // (if any) remove or replace all the stuff that contains a Date
1378 //0008 0012 DA ID Instance Creation Date
1379 //0008 0020 DA ID Study Date
1380 //0008 0021 DA ID Series Date
1381 //0008 0022 DA ID Acquisition Date
1382 //0008 0023 DA ID Content Date
1383 //0008 0024 DA ID Overlay Date
1384 //0008 0025 DA ID Curve Date
1385 //0008 002a DT ID Acquisition Datetime
1386 //0018 9074 DT ACQ Frame Acquisition Datetime
1387 //0018 9151 DT ACQ Frame Reference Datetime
1388 //0018 a002 DT ACQ Contribution Date Time
1389 //0020 3403 SH REL Modified Image Date (RET)
1390 //0032 0032 DA SDY Study Verified Date
1391 //0032 0034 DA SDY Study Read Date
1392 //0032 1000 DA SDY Scheduled Study Start Date
1393 //0032 1010 DA SDY Scheduled Study Stop Date
1394 //0032 1040 DA SDY Study Arrival Date
1395 //0032 1050 DA SDY Study Completion Date
1396 //0038 001a DA VIS Scheduled Admission Date
1397 //0038 001c DA VIS Scheduled Discharge Date
1398 //0038 0020 DA VIS Admitting Date
1399 //0038 0030 DA VIS Discharge Date
1400 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1401 //0040 0004 DA PRC Scheduled Procedure Step End Date
1402 //0040 0244 DA PRC Performed Procedure Step Start Date
1403 //0040 0250 DA PRC Performed Procedure Step End Date
1404 //0040 2004 DA PRC Issue Date of Imaging Service Request
1405 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1406 //0040 4011 DT PRC Expected Completion Date and Time
1407 //0040 a030 DT PRC Verification Date Time
1408 //0040 a032 DT PRC Observation Date Time
1409 //0040 a120 DT PRC DateTime
1410 //0040 a121 DA PRC Date
1411 //0040 a13a DT PRC Referenced Datetime
1412 //0070 0082 DA ??? Presentation Creation Date
1413 //0100 0420 DT ??? SOP Autorization Date and Time
1414 //0400 0105 DT ??? Digital Signature DateTime
1415 //2100 0040 DA PJ Creation Date
1416 //3006 0008 DA SSET Structure Set Date
1417 //3008 0024 DA ??? Treatment Control Point Date
1418 //3008 0054 DA ??? First Treatment Date
1419 //3008 0056 DA ??? Most Recent Treatment Date
1420 //3008 0162 DA ??? Safe Position Exit Date
1421 //3008 0166 DA ??? Safe Position Return Date
1422 //3008 0250 DA ??? Treatment Date
1423 //300a 0006 DA RT RT Plan Date
1424 //300a 022c DA RT Air Kerma Rate Reference Date
1425 //300e 0004 DA RT Review Date
1431 * \brief gets the info from 0020,0037 : Image Orientation Patient
1432 * @param iop adress of the (6)float aray to receive values
1433 * @return cosines of image orientation patient
1435 void gdcmHeader::GetImageOrientationPatient( float iop[6] )
1437 std::string strImOriPat;
1438 //iop is supposed to be float[6]
1439 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1441 // 0020 0037 DS REL Image Orientation (Patient)
1442 if ( (strImOriPat = GetEntryByNumber(0x0020,0x0037)) != GDCM_UNFOUND )
1444 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1445 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1447 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: "
1448 "wrong Image Orientation Patient (0020,0037)");
1452 // 0020 0035 DS REL Image Orientation (RET)
1453 else if ( (strImOriPat = GetEntryByNumber(0x0020,0x0035)) != GDCM_UNFOUND )
1455 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1456 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1458 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: "
1459 "wrong Image Orientation Patient (0020,0035)");
1464 //-----------------------------------------------------------------------------
1467 //-----------------------------------------------------------------------------