1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/09/10 18:54:39 $
7 Version: $Revision: 1.185 $
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.htm 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()
88 * \brief Canonical destructor.
90 gdcmHeader::~gdcmHeader ()
96 * \brief Performs some consistency checking on various 'File related'
97 * (as opposed to 'DicomDir related') entries
98 * then writes in a file all the (Dicom Elements) included the Pixels
99 * @param fp file pointer on an already open file
100 * @param filetype Type of the File to be written
101 * (ACR-NEMA, ExplicitVR, ImplicitVR)
103 void gdcmHeader::Write(FILE* fp,FileType filetype)
106 if ( GetEntryByNumber(0x0028,0x0100) == "12")
108 SetEntryByNumber("16", 0x0028,0x0100);
111 // TODO : correct 'Pixel group' Length if necessary
113 int i_lgPix = GetEntryLengthByNumber(GrPixel, NumPixel);
116 // no (GrPixel, NumPixel) element
117 char* dumm = new char[20];
118 sprintf(dumm ,"%d", i_lgPix+12);
119 std::string s_lgPix = dumm;
121 ReplaceOrCreateByNumber(s_lgPix,GrPixel, 0x0000);
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);
137 e = GetDocEntryByNumber(0x0028,0x1102);
142 e = GetDocEntryByNumber(0x0028,0x1103);
147 e = GetDocEntryByNumber(0x0028,0x01201);
152 e = GetDocEntryByNumber(0x0028,0x1202);
157 e = GetDocEntryByNumber(0x0028,0x1203);
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 * \ingroup gdcmHeader
232 * \brief Retrieve the number of lines of image.
233 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
234 * @return The encountered size when found, 1 by default
235 * (The ACR-NEMA file contains a Signal, not an Image).
237 int gdcmHeader::GetYSize()
239 std::string strSize = GetEntryByNumber(0x0028,0x0010);
240 if ( strSize != GDCM_UNFOUND )
242 return atoi( strSize.c_str() );
249 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
250 // hence be a signal (1d image). So we default to 1:
255 * \ingroup gdcmHeader
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 * \ingroup gdcmHeader
285 * \brief gets the info from 0028,0030 : Pixel Spacing
287 * @return X dimension of a pixel
289 float gdcmHeader::GetXSpacing()
291 float xspacing, yspacing;
292 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
294 if ( strSpacing == GDCM_UNFOUND )
296 dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
301 if( ( nbValues = sscanf( strSpacing.c_str(),
302 "%f\\%f", &yspacing, &xspacing)) != 2 )
304 // if single value is found, xspacing is defaulted to yspacing
312 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
313 // seems to be a bug in the header ...
314 sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
321 * \ingroup gdcmHeader
322 * \brief gets the info from 0028,0030 : Pixel Spacing
324 * @return Y dimension of a pixel
326 float gdcmHeader::GetYSpacing()
329 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
331 if ( strSpacing == GDCM_UNFOUND )
333 dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
337 // if sscanf cannot read any float value, it won't affect yspacing
338 sscanf( strSpacing.c_str(), "%f", &yspacing);
345 *\brief gets the info from 0018,0088 : Space Between Slices
346 * else from 0018,0050 : Slice Thickness
348 * @return Z dimension of a voxel-to be
350 float gdcmHeader::GetZSpacing()
352 // Spacing Between Slices : distance entre le milieu de chaque coupe
353 // Les coupes peuvent etre :
354 // jointives (Spacing between Slices = Slice Thickness)
355 // chevauchantes (Spacing between Slices < Slice Thickness)
356 // disjointes (Spacing between Slices > Slice Thickness)
357 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
358 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
359 // Si le Spacing Between Slices est absent,
360 // on suppose que les coupes sont jointives
362 std::string strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
364 if ( strSpacingBSlices == GDCM_UNFOUND )
366 dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
367 std::string strSliceThickness = GetEntryByNumber(0x0018,0x0050);
368 if ( strSliceThickness == GDCM_UNFOUND )
374 // if no 'Spacing Between Slices' is found,
375 // we assume slices join together
376 // (no overlapping, no interslice gap)
377 // if they don't, we're fucked up
378 return atof( strSliceThickness.c_str() );
383 return atof( strSpacingBSlices.c_str() );
389 *\brief gets the info from 0028,1052 : Rescale Intercept
390 * @return Rescale Intercept
392 float gdcmHeader::GetRescaleIntercept()
395 std::string strRescInter = GetEntryByNumber(0x0028,0x1052); //0028 1052 DS IMG Rescale Intercept
396 if ( strRescInter != GDCM_UNFOUND )
398 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
400 // bug in the element 0x0028,0x1052
401 dbg.Verbose(0, "gdcmHeader::GetRescaleIntercept: Rescale Slope is empty");
410 *\brief gets the info from 0028,1053 : Rescale Slope
411 * @return Rescale Slope
413 float gdcmHeader::GetRescaleSlope()
416 std::string strRescSlope = GetEntryByNumber(0x0028,0x1053); //0028 1053 DS IMG Rescale Slope
417 if ( strRescSlope != GDCM_UNFOUND )
419 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
421 // bug in the element 0x0028,0x1053
422 dbg.Verbose(0, "gdcmHeader::GetRescaleSlope: Rescale Slope is empty");
430 * \ingroup gdcmHeader
431 * \brief This function is intended to user who doesn't want
432 * to have to manage a LUT and expects to get an RBG Pixel image
433 * (or a monochrome one ...)
434 * \warning to be used with GetImagePixels()
435 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
437 int gdcmHeader::GetNumberOfScalarComponents()
439 if ( GetSamplesPerPixel() == 3 )
444 // 0028 0100 US IMG Bits Allocated
445 // (in order no to be messed up by old RGB images)
446 if ( GetEntryByNumber(0x0028,0x0100) == "24" )
451 std::string strPhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
453 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
455 if ( HasLUT() )// PALETTE COLOR is NOT enough
465 // beware of trailing space at end of string
466 // DICOM tags are never of odd length
467 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
468 strPhotometricInterpretation == "MONOCHROME1 " ||
469 strPhotometricInterpretation == "MONOCHROME2 " )
475 // we assume that *all* kinds of YBR are dealt with
481 * \ingroup gdcmHeader
482 * \brief This function is intended to user that DOESN'T want
483 * to get RGB pixels image when it's stored as a PALETTE COLOR image
484 * - the (vtk) user is supposed to know how deal with LUTs -
485 * \warning to be used with GetImagePixelsRaw()
486 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
488 int gdcmHeader::GetNumberOfScalarComponentsRaw()
490 // 0028 0100 US IMG Bits Allocated
491 // (in order no to be messed up by old RGB images)
492 if ( gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24" )
497 // we assume that *all* kinds of YBR are dealt with
498 return GetSamplesPerPixel();
502 // -------------- Remember ! ----------------------------------
504 // Image Position Patient (0020,0032):
505 // If not found (ACR_NEMA) we try Image Position (0020,0030)
506 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
507 // or Location (0020,0050)
508 // as the Z coordinate,
509 // 0. for all the coordinates if nothing is found
511 // \todo find a way to inform the caller nothing was found
512 // \todo How to tell the caller a wrong number of values was found?
514 // ---------------------------------------------------------------
518 * \brief gets the info from 0020,0032 : Image Position Patient
519 * else from 0020,0030 : Image Position (RET)
521 * @return up-left image corner X position
524 float gdcmHeader::GetXOrigin()
526 float xImPos, yImPos, zImPos;
527 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
529 if ( strImPos == GDCM_UNFOUND )
531 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position Patient (0020,0032)");
532 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
533 if ( strImPos == GDCM_UNFOUND )
535 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position (RET) (0020,0030)");
536 /// \todo How to tell the caller nothing was found ?
541 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
550 * \brief gets the info from 0020,0032 : Image Position Patient
551 * else from 0020,0030 : Image Position (RET)
553 * @return up-left image corner Y position
555 float gdcmHeader::GetYOrigin()
557 float xImPos, yImPos, zImPos;
558 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
560 if ( strImPos == GDCM_UNFOUND)
562 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position Patient (0020,0032)");
563 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
564 if ( strImPos == GDCM_UNFOUND )
566 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position (RET) (0020,0030)");
567 /// \todo How to tell the caller nothing was found ?
572 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
581 * \brief gets the info from 0020,0032 : Image Position Patient
582 * \ else from 0020,0030 : Image Position (RET)
583 * \ else from 0020,1041 : Slice Location
584 * \ else from 0020,0050 : Location
586 * @return up-left image corner Z position
588 float gdcmHeader::GetZOrigin()
590 float xImPos, yImPos, zImPos;
591 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
593 if ( strImPos != GDCM_UNFOUND )
595 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
597 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image 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
657 * @return image number
659 int gdcmHeader::GetImageNumber()
661 // The function i atoi() takes the address of an area of memory as
662 // parameter and converts the string stored at that location to an integer
663 // using the external decimal to internal binary conversion rules. This may
664 // be preferable to sscanf() since atoi() is a much smaller, simpler and
665 // faster function. sscanf() can do all possible conversions whereas
666 // atoi() can only do single decimal integer conversions.
667 std::string strImNumber = GetEntryByNumber(0x0020,0x0013); //0020 0013 IS REL Image Number
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 * \ingroup gdcmHeader
739 * \brief Retrieve the number of Bits Stored (actually used)
740 * (as opposite to number of Bits Allocated)
741 * @return The encountered number of Bits Stored, 0 by default.
742 * 0 means the file is NOT USABLE. The caller has to check it !
744 int gdcmHeader::GetBitsStored()
746 std::string strSize = GetEntryByNumber(0x0028,0x0101);
747 if ( strSize == GDCM_UNFOUND )
749 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to be mandatory");
750 return 0; // It's supposed to be mandatory
751 // the caller will have to check
753 return atoi( strSize.c_str() );
757 * \ingroup gdcmHeader
758 * \brief Retrieve the number of Bits Allocated
759 * (8, 12 -compacted ACR-NEMA files, 16, ...)
760 * @return The encountered number of Bits Allocated, 0 by default.
761 * 0 means the file is NOT USABLE. The caller has to check it !
763 int gdcmHeader::GetBitsAllocated()
765 std::string strSize = GetEntryByNumber(0x0028,0x0100);
766 if ( strSize == GDCM_UNFOUND )
768 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to be mandatory");
769 return 0; // It's supposed to be mandatory
770 // the caller will have to check
772 return atoi( strSize.c_str() );
776 * \ingroup gdcmHeader
777 * \brief Retrieve the number of Samples Per Pixel
778 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
779 * @return The encountered number of Samples Per Pixel, 1 by default.
780 * (Gray level Pixels)
782 int gdcmHeader::GetSamplesPerPixel()
784 std::string strSize = GetEntryByNumber(0x0028,0x0002);
785 if ( strSize == GDCM_UNFOUND )
787 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to be mandatory");
788 return 1; // Well, it's supposed to be mandatory ...
789 // but sometimes it's missing : *we* assume Gray pixels
791 return atoi( strSize.c_str() );
795 * \ingroup gdcmHeader
796 * \brief Retrieve the Planar Configuration for RGB images
797 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
798 * @return The encountered Planar Configuration, 0 by default.
800 int gdcmHeader::GetPlanarConfiguration()
802 std::string strSize = GetEntryByNumber(0x0028,0x0006);
803 if ( strSize == GDCM_UNFOUND )
807 return atoi( strSize.c_str() );
811 * \ingroup gdcmHeader
812 * \brief Return the size (in bytes) of a single pixel of data.
813 * @return The size in bytes of a single pixel of data; 0 by default
814 * 0 means the file is NOT USABLE; the caller will have to check
816 int gdcmHeader::GetPixelSize()
818 // 0028 0100 US IMG Bits Allocated
819 // (in order no to be messed up by old RGB images)
820 // if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
823 std::string pixelType = GetPixelType();
824 if ( pixelType == "8U" || pixelType == "8S" )
828 if ( pixelType == "16U" || pixelType == "16S")
832 if ( pixelType == "32U" || pixelType == "32S")
836 if ( pixelType == "FD" )
840 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
845 * \ingroup gdcmHeader
846 * \brief Build the Pixel Type of the image.
847 * Possible values are:
848 * - 8U unsigned 8 bit,
850 * - 16U unsigned 16 bit,
851 * - 16S signed 16 bit,
852 * - 32U unsigned 32 bit,
853 * - 32S signed 32 bit,
854 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
855 * \warning 12 bit images appear as 16 bit.
856 * 24 bit images appear as 8 bit
857 * @return 0S if nothing found. NOT USABLE file. The caller has to check
859 std::string gdcmHeader::GetPixelType()
861 std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
862 if ( bitsAlloc == GDCM_UNFOUND )
864 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
868 if ( bitsAlloc == "64" )
872 if ( bitsAlloc == "12" )
874 // It will be unpacked
877 else if ( bitsAlloc == "24" )
879 // (in order no to be messed up
880 bitsAlloc = "8"; // by old RGB images)
883 std::string sign = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
885 if (sign == GDCM_UNFOUND )
887 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
898 return bitsAlloc + sign;
903 * \ingroup gdcmHeader
904 * \brief Recover the offset (from the beginning of the file)
905 * of *image* pixels (not *icone image* pixels, if any !)
906 * @return Pixel Offset
908 size_t gdcmHeader::GetPixelOffset()
910 gdcmDocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
913 return pxlElement->GetOffset();
918 std::cout << "Big trouble : Pixel Element ("
919 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
926 // TODO : unify those two (previous one and next one)
928 * \ingroup gdcmHeader
929 * \brief Recover the pixel area length (in Bytes)
930 * @return Pixel Element Length, as stored in the header
931 * (NOT the memory space necessary to hold the Pixels
932 * -in case of embeded compressed image-)
933 * 0 : NOT USABLE file. The caller has to check.
935 size_t gdcmHeader::GetPixelAreaLength()
937 gdcmDocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
940 return pxlElement->GetLength();
945 std::cout << "Big trouble : Pixel Element ("
946 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
954 * \ingroup gdcmHeader
955 * \brief tells us if LUT are used
956 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
957 * are NOT considered as LUT, since nobody knows
958 * how to deal with them
959 * Please warn me if you know sbdy that *does* know ... jprx
960 * @return true if LUT Descriptors and LUT Tables were found
962 bool gdcmHeader::HasLUT()
964 // Check the presence of the LUT Descriptors, and LUT Tables
966 if ( !GetDocEntryByNumber(0x0028,0x1101) )
970 // LutDescriptorGreen
971 if ( !GetDocEntryByNumber(0x0028,0x1102) )
976 if ( !GetDocEntryByNumber(0x0028,0x1103) )
980 // Red Palette Color Lookup Table Data
981 if ( !GetDocEntryByNumber(0x0028,0x1201) )
985 // Green Palette Color Lookup Table Data
986 if ( !GetDocEntryByNumber(0x0028,0x1202) )
990 // Blue Palette Color Lookup Table Data
991 if ( !GetDocEntryByNumber(0x0028,0x1203) )
996 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
997 // NOT taken into account, but we don't know how to use it ...
1002 * \ingroup gdcmHeader
1003 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1005 * @return Lookup Table number of Bits , 0 by default
1006 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1007 * @ return bit number of each LUT item
1009 int gdcmHeader::GetLUTNbits()
1011 std::vector<std::string> tokens;
1014 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
1015 // Consistency already checked in GetLUTLength
1016 std::string lutDescription = GetEntryByNumber(0x0028,0x1101);
1017 if ( lutDescription == GDCM_UNFOUND )
1022 tokens.clear(); // clean any previous value
1023 Tokenize ( lutDescription, tokens, "\\" );
1024 //LutLength=atoi(tokens[0].c_str());
1025 //LutDepth=atoi(tokens[1].c_str());
1027 lutNbits = atoi( tokens[2].c_str() );
1034 * \ingroup gdcmHeader
1035 * \brief builts Red/Green/Blue/Alpha LUT from Header
1036 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1037 * and (0028,1101),(0028,1102),(0028,1102)
1038 * - xxx Palette Color Lookup Table Descriptor - are found
1039 * and (0028,1201),(0028,1202),(0028,1202)
1040 * - xxx Palette Color Lookup Table Data - are found
1041 * \warning does NOT deal with :
1042 * 0028 1100 Gray Lookup Table Descriptor (Retired)
1043 * 0028 1221 Segmented Red Palette Color Lookup Table Data
1044 * 0028 1222 Segmented Green Palette Color Lookup Table Data
1045 * 0028 1223 Segmented Blue Palette Color Lookup Table Data
1046 * no known Dicom reader deals with them :-(
1047 * @return a RGBA Lookup Table
1049 uint8_t* gdcmHeader::GetLUTRGBA()
1051 // Not so easy : see
1052 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
1054 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
1055 if ( GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR " )
1060 int lengthR, debR, nbitsR;
1061 int lengthG, debG, nbitsG;
1062 int lengthB, debB, nbitsB;
1064 // Get info from Lut Descriptors
1065 // (the 3 LUT descriptors may be different)
1066 std::string lutDescriptionR = GetEntryByNumber(0x0028,0x1101);
1067 if ( lutDescriptionR == GDCM_UNFOUND )
1072 std::string lutDescriptionG = GetEntryByNumber(0x0028,0x1102);
1073 if ( lutDescriptionG == GDCM_UNFOUND )
1078 std::string lutDescriptionB = GetEntryByNumber(0x0028,0x1103);
1079 if ( lutDescriptionB == GDCM_UNFOUND )
1084 // lengthR: Red LUT length in Bytes
1085 // debR: subscript of the first Lut Value
1086 // nbitsR: Lut item size (in Bits)
1087 int nbRead = sscanf( lutDescriptionR.c_str(), "%d\\%d\\%d",
1088 &lengthR, &debR, &nbitsR );
1091 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading red LUT");
1094 // lengthG: Green LUT length in Bytes
1095 // debG: subscript of the first Lut Value
1096 // nbitsG: Lut item size (in Bits)
1097 nbRead = sscanf( lutDescriptionG.c_str(), "%d\\%d\\%d",
1098 &lengthG, &debG, &nbitsG );
1101 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading green LUT");
1104 // lengthB: Blue LUT length in Bytes
1105 // debB: subscript of the first Lut Value
1106 // nbitsB: Lut item size (in Bits)
1107 nbRead = sscanf( lutDescriptionB.c_str(), "%d\\%d\\%d",
1108 &lengthB, &debB, &nbitsB );
1111 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading blue LUT");
1114 // Load LUTs into memory, (as they were stored on disk)
1115 uint8_t* lutR = (uint8_t*) GetEntryVoidAreaByNumber(0x0028,0x1201);
1116 uint8_t* lutG = (uint8_t*) GetEntryVoidAreaByNumber(0x0028,0x1202);
1117 uint8_t* lutB = (uint8_t*) GetEntryVoidAreaByNumber(0x0028,0x1203);
1119 if ( !lutR || !lutG || !lutB )
1121 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble with one of the LUT");
1124 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
1126 uint8_t* LUTRGBA = new uint8_t[1024]; // 256 * 4 (R, G, B, Alpha)
1131 memset(LUTRGBA, 0, 1024);
1135 std::string str_nb = GetEntryByNumber(0x0028,0x0100);
1136 if ( str_nb == GDCM_UNFOUND )
1142 nb = atoi( str_nb.c_str() );
1146 if ( nbitsR == 16 && nb == 8)
1148 // when LUT item size is different than pixel size
1149 mult = 2; // high byte must be = low byte
1153 // See PS 3.3-2003 C.11.1.1.2 p 619
1157 // if we get a black image, let's just remove the '+1'
1158 // from 'i*mult+1' and check again
1159 // if it works, we shall have to check the 3 Palettes
1160 // to see which byte is ==0 (first one, or second one)
1162 // We give up the checking to avoid some (useless ?)overhead
1163 // (optimistic asumption)
1168 for( i=0; i < lengthR; ++i)
1170 *a = lutR[i*mult+1];
1175 for( i=0; i < lengthG; ++i)
1177 *a = lutG[i*mult+1];
1182 for(i=0; i < lengthB; ++i)
1184 *a = lutB[i*mult+1];
1189 for(i=0; i < 256; ++i)
1191 *a = 1; // Alpha component
1198 * \brief Accesses the info from 0002,0010 : Transfert Syntax and gdcmTS
1200 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1202 std::string gdcmHeader::GetTransfertSyntaxName()
1204 // use the gdcmTS (TS : Transfert Syntax)
1205 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1207 if ( transfertSyntax == GDCM_NOTLOADED ) { // fusible
1208 std::cout << "Transfert Syntax not loaded. " << std::endl
1209 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
1211 return "Uncompressed ACR-NEMA";
1213 if ( transfertSyntax == GDCM_UNFOUND )
1215 dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName:"
1216 " unfound Transfert Syntax (0002,0010)");
1217 return "Uncompressed ACR-NEMA";
1220 while ( ! isdigit(transfertSyntax[transfertSyntax.length()-1]) )
1222 transfertSyntax.erase(transfertSyntax.length()-1, 1);
1224 // we do it only when we need it
1225 gdcmTS* ts = gdcmGlobal::GetTS();
1226 std::string tsName = ts->GetValue( transfertSyntax );
1228 //delete ts; /// \todo Seg Fault when deleted ?!
1233 * \brief Sets the Pixel Area size in the Header
1234 * --> not-for-rats function
1235 * @param ImageDataSize new Pixel Area Size
1236 * warning : nothing else is checked
1238 void gdcmHeader::SetImageDataSize(size_t ImageDataSize)
1241 sprintf(car,"%d",ImageDataSize);
1243 gdcmDocEntry *a = GetDocEntryByNumber(GrPixel, NumPixel);
1244 a->SetLength(ImageDataSize);
1247 sprintf(car,"%d",ImageDataSize);
1249 const std::string content1 = car;
1250 SetEntryByNumber(content1, GrPixel, NumPixel);
1253 //-----------------------------------------------------------------------------
1257 * \brief anonymize a Header (removes Patient's personal info)
1258 * (read the code to see which ones ...)
1260 bool gdcmHeader::AnonymizeHeader()
1262 gdcmDocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1264 ReplaceIfExistByNumber (" ",0x0010, 0x2154); // Telephone
1265 ReplaceIfExistByNumber (" ",0x0010, 0x1040); // Adress
1266 ReplaceIfExistByNumber (" ",0x0010, 0x0020); // Patient ID
1268 if ( patientNameHE )
1270 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1271 if ( studyInstanceUID != GDCM_UNFOUND )
1273 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1277 ReplaceOrCreateByNumber(std::string("anonymised"), 0x0010, 0x0010);
1282 // (if any) remove or replace all the stuff that contains a Date
1284 //0008 0012 DA ID Instance Creation Date
1285 //0008 0020 DA ID Study Date
1286 //0008 0021 DA ID Series Date
1287 //0008 0022 DA ID Acquisition Date
1288 //0008 0023 DA ID Content Date
1289 //0008 0024 DA ID Overlay Date
1290 //0008 0025 DA ID Curve Date
1291 //0008 002a DT ID Acquisition Datetime
1292 //0018 9074 DT ACQ Frame Acquisition Datetime
1293 //0018 9151 DT ACQ Frame Reference Datetime
1294 //0018 a002 DT ACQ Contribution Date Time
1295 //0020 3403 SH REL Modified Image Date (RET)
1296 //0032 0032 DA SDY Study Verified Date
1297 //0032 0034 DA SDY Study Read Date
1298 //0032 1000 DA SDY Scheduled Study Start Date
1299 //0032 1010 DA SDY Scheduled Study Stop Date
1300 //0032 1040 DA SDY Study Arrival Date
1301 //0032 1050 DA SDY Study Completion Date
1302 //0038 001a DA VIS Scheduled Admission Date
1303 //0038 001c DA VIS Scheduled Discharge Date
1304 //0038 0020 DA VIS Admitting Date
1305 //0038 0030 DA VIS Discharge Date
1306 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1307 //0040 0004 DA PRC Scheduled Procedure Step End Date
1308 //0040 0244 DA PRC Performed Procedure Step Start Date
1309 //0040 0250 DA PRC Performed Procedure Step End Date
1310 //0040 2004 DA PRC Issue Date of Imaging Service Request
1311 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1312 //0040 4011 DT PRC Expected Completion Date and Time
1313 //0040 a030 DT PRC Verification Date Time
1314 //0040 a032 DT PRC Observation Date Time
1315 //0040 a120 DT PRC DateTime
1316 //0040 a121 DA PRC Date
1317 //0040 a13a DT PRC Referenced Datetime
1318 //0070 0082 DA ??? Presentation Creation Date
1319 //0100 0420 DT ??? SOP Autorization Date and Time
1320 //0400 0105 DT ??? Digital Signature DateTime
1321 //2100 0040 DA PJ Creation Date
1322 //3006 0008 DA SSET Structure Set Date
1323 //3008 0024 DA ??? Treatment Control Point Date
1324 //3008 0054 DA ??? First Treatment Date
1325 //3008 0056 DA ??? Most Recent Treatment Date
1326 //3008 0162 DA ??? Safe Position Exit Date
1327 //3008 0166 DA ??? Safe Position Return Date
1328 //3008 0250 DA ??? Treatment Date
1329 //300a 0006 DA RT RT Plan Date
1330 //300a 022c DA RT Air Kerma Rate Reference Date
1331 //300e 0004 DA RT Review Date
1337 * \brief gets the info from 0020,0037 : Image Orientation Patient
1338 * @param iop adress of the (6)float aray to receive values
1339 * @return cosines of image orientation patient
1341 void gdcmHeader::GetImageOrientationPatient( float iop[6] )
1343 std::string strImOriPat;
1344 //iop is supposed to be float[6]
1345 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1347 // 0020 0037 DS REL Image Orientation (Patient)
1348 if ( (strImOriPat = GetEntryByNumber(0x0020,0x0037)) != GDCM_UNFOUND )
1350 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1351 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1353 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0037)");
1354 // bug in the element 0x0020,0x0037
1358 // 0020 0035 DS REL Image Orientation (RET)
1359 else if ( (strImOriPat = GetEntryByNumber(0x0020,0x0035)) != GDCM_UNFOUND )
1361 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1362 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1364 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0035)");
1365 // bug in the element 0x0020,0x0035
1370 //-----------------------------------------------------------------------------
1373 //-----------------------------------------------------------------------------