1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/07/30 16:09:27 $
7 Version: $Revision: 1.180 $
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
33 * @param exception_on_error whether we want to throw an exception or not
35 gdcmHeader::gdcmHeader( std::string const & filename,
36 bool exception_on_error ):
37 gdcmDocument( filename,
40 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
41 // We may encounter the 'RETired' (0x0028, 0x0200) tag
42 // (Image Location") . This Element contains the number of
43 // the group that contains the pixel data (hence the "Pixel Data"
44 // is found by indirection through the "Image Location").
45 // Inside the group pointed by "Image Location" the searched element
46 // is conventionally the element 0x0010 (when the norm is respected).
47 // When the "Image Location" is absent we default to group 0x7fe0.
48 // Note: this IS the right place for the code
51 std::string imgLocation = GetEntryByNumber(0x0028, 0x0200);
52 if ( imgLocation == GDCM_UNFOUND )
59 GrPixel = (uint16_t) atoi( imgLocation.c_str() );
62 // sometimes Image Location value doesn't follow
63 // the supposed processor endianity.
64 // see gdcmData/cr172241.dcm
65 if ( GrPixel == 0xe07f )
70 if ( GrPixel != 0x7fe0 )
72 // This is a kludge for old dirty Philips imager.
83 * @param exception_on_error whether we want to throw an exception or not
85 gdcmHeader::gdcmHeader(bool exception_on_error) :
86 gdcmDocument( exception_on_error )
92 * \brief Canonical destructor.
94 gdcmHeader::~gdcmHeader ()
100 * \brief Performs some consistency checking on various 'File related'
101 * (as opposed to 'DicomDir related') entries
102 * then writes in a file all the (Dicom Elements) included the Pixels
103 * @param fp file pointer on an already open file
104 * @param filetype Type of the File to be written
105 * (ACR-NEMA, ExplicitVR, ImplicitVR)
107 void gdcmHeader::Write(FILE* fp,FileType filetype) {
110 if ( GetEntryByNumber(0x0028,0x0100) == "12") {
111 SetEntryByNumber("16", 0x0028,0x0100);
114 // correct Pixel group Length if necessary
116 // TODO : create a gdcmHeader::Write method and move this part.
117 // (only gdcmHeader knows GrPixel, NumPixel)
119 int i_lgPix = GetEntryLengthByNumber(GrPixel, NumPixel);
120 if (i_lgPix != -2) { // no (GrPixel, NumPixel) element
121 char * dumm = new char[20];
122 sprintf(dumm ,"%d", i_lgPix+12);
123 std::string s_lgPix = dumm;
125 ReplaceOrCreateByNumber(s_lgPix,GrPixel, 0x0000);
128 // Drop Palette Color, if necessary
130 if ( GetEntryByNumber(0x0028,0x0002).c_str()[0] == '3' ) {
131 // if SamplesPerPixel = 3, sure we don't need any LUT !
132 // Drop 0028|1101, 0028|1102, 0028|1103
133 // Drop 0028|1201, 0028|1202, 0028|1203
136 e=GetDocEntryByNumber(0x0028,0x01101);
139 e=GetDocEntryByNumber(0x0028,0x1102);
142 e=GetDocEntryByNumber(0x0028,0x1103);
146 e=GetDocEntryByNumber(0x0028,0x01201);
149 e=GetDocEntryByNumber(0x0028,0x1202);
152 e=GetDocEntryByNumber(0x0028,0x1203);
157 gdcmDocument::Write(fp,filetype);
160 //-----------------------------------------------------------------------------
164 //-----------------------------------------------------------------------------
168 * \brief This predicate, based on hopefully reasonable heuristics,
169 * decides whether or not the current gdcmHeader was properly parsed
170 * and contains the mandatory information for being considered as
171 * a well formed and usable Dicom/Acr File.
172 * @return true when gdcmHeader is the one of a reasonable Dicom/Acr file,
175 bool gdcmHeader::IsReadable()
177 if( !gdcmDocument::IsReadable() )
182 std::string res = GetEntryByNumber(0x0028, 0x0005);
183 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
185 return false; // Image Dimensions
187 if ( !GetDocEntryByNumber(0x0028, 0x0100) )
189 return false; // "Bits Allocated"
191 if ( !GetDocEntryByNumber(0x0028, 0x0101) )
193 return false; // "Bits Stored"
195 if ( !GetDocEntryByNumber(0x0028, 0x0102) )
197 return false; // "High Bit"
199 if ( !GetDocEntryByNumber(0x0028, 0x0103) )
201 return false; // "Pixel Representation" i.e. 'Sign'
208 * \brief Retrieve the number of columns of image.
209 * @return The encountered size when found, 0 by default.
210 * 0 means the file is NOT USABLE. The caller will have to check
212 int gdcmHeader::GetXSize()
215 strSize = GetEntryByNumber(0x0028,0x0011);
216 if ( strSize == GDCM_UNFOUND )
221 return atoi( strSize.c_str() );
225 * \ingroup gdcmHeader
226 * \brief Retrieve the number of lines of image.
227 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
228 * @return The encountered size when found, 1 by default
229 * (The ACR-NEMA file contains a Signal, not an Image).
231 int gdcmHeader::GetYSize()
233 std::string strSize = GetEntryByNumber(0x0028,0x0010);
234 if ( strSize != GDCM_UNFOUND )
236 return atoi( strSize.c_str() );
243 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
244 // hence be a signal (1d image). So we default to 1:
249 * \ingroup gdcmHeader
250 * \brief Retrieve the number of planes of volume or the number
251 * of frames of a multiframe.
252 * \warning When present we consider the "Number of Frames" as the third
253 * dimension. When absent we consider the third dimension as
254 * being the ACR-NEMA "Planes" tag content.
255 * @return The encountered size when found, 1 by default (single image).
257 int gdcmHeader::GetZSize()
259 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
260 // as the third dimension.
261 std::string strSize = GetEntryByNumber(0x0028,0x0008);
262 if ( strSize != GDCM_UNFOUND )
264 return atoi( strSize.c_str() );
267 // We then consider the "Planes" entry as the third dimension
268 strSize = GetEntryByNumber(0x0028,0x0012);
269 if ( strSize != GDCM_UNFOUND )
271 return atoi( strSize.c_str() );
278 * \ingroup gdcmHeader
279 * \brief gets the info from 0028,0030 : Pixel Spacing
281 * @return X dimension of a pixel
283 float gdcmHeader::GetXSpacing()
285 float xspacing, yspacing;
286 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
288 if ( strSpacing == GDCM_UNFOUND )
290 dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
295 if( ( nbValues = sscanf( strSpacing.c_str(),
296 "%f\\%f", &yspacing, &xspacing)) != 2 )
298 // if single value is found, xspacing is defaulted to yspacing
306 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
307 // seems to be a bug in the header ...
308 sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
315 * \ingroup gdcmHeader
316 * \brief gets the info from 0028,0030 : Pixel Spacing
318 * @return Y dimension of a pixel
320 float gdcmHeader::GetYSpacing()
323 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
325 if ( strSpacing == GDCM_UNFOUND )
327 dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
331 // if sscanf cannot read any float value, it won't affect yspacing
332 sscanf( strSpacing.c_str(), "%f", &yspacing);
339 *\brief gets the info from 0018,0088 : Space Between Slices
340 * else from 0018,0050 : Slice Thickness
342 * @return Z dimension of a voxel-to be
344 float gdcmHeader::GetZSpacing()
346 // Spacing Between Slices : distance entre le milieu de chaque coupe
347 // Les coupes peuvent etre :
348 // jointives (Spacing between Slices = Slice Thickness)
349 // chevauchantes (Spacing between Slices < Slice Thickness)
350 // disjointes (Spacing between Slices > Slice Thickness)
351 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
352 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
353 // Si le Spacing Between Slices est absent,
354 // on suppose que les coupes sont jointives
356 std::string strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
358 if ( strSpacingBSlices == GDCM_UNFOUND )
360 dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
361 std::string strSliceThickness = GetEntryByNumber(0x0018,0x0050);
362 if ( strSliceThickness == GDCM_UNFOUND )
368 // if no 'Spacing Between Slices' is found,
369 // we assume slices join together
370 // (no overlapping, no interslice gap)
371 // if they don't, we're fucked up
372 return atof( strSliceThickness.c_str() );
377 return atof( strSpacingBSlices.c_str() );
383 *\brief gets the info from 0028,1052 : Rescale Intercept
384 * @return Rescale Intercept
386 float gdcmHeader::GetRescaleIntercept()
389 std::string strRescInter = GetEntryByNumber(0x0028,0x1052); //0028 1052 DS IMG Rescale Intercept
390 if ( strRescInter != GDCM_UNFOUND )
392 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
394 // bug in the element 0x0028,0x1052
395 dbg.Verbose(0, "gdcmHeader::GetRescaleIntercept: Rescale Slope is empty");
404 *\brief gets the info from 0028,1053 : Rescale Slope
405 * @return Rescale Slope
407 float gdcmHeader::GetRescaleSlope()
410 std::string strRescSlope = GetEntryByNumber(0x0028,0x1053); //0028 1053 DS IMG Rescale Slope
411 if ( strRescSlope != GDCM_UNFOUND )
413 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
415 // bug in the element 0x0028,0x1053
416 dbg.Verbose(0, "gdcmHeader::GetRescaleSlope: Rescale Slope is empty");
424 * \ingroup gdcmHeader
425 * \brief This function is intended to user who doesn't want
426 * to have to manage a LUT and expects to get an RBG Pixel image
427 * (or a monochrome one ...)
428 * \warning to be used with GetImagePixels()
429 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
431 int gdcmHeader::GetNumberOfScalarComponents()
433 if ( GetSamplesPerPixel() == 3 )
438 // 0028 0100 US IMG Bits Allocated
439 // (in order no to be messed up by old RGB images)
440 if ( GetEntryByNumber(0x0028,0x0100) == "24" )
445 std::string strPhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
447 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
449 if ( HasLUT() )// PALETTE COLOR is NOT enough
459 // beware of trailing space at end of string
460 // DICOM tags are never of odd length
461 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
462 strPhotometricInterpretation == "MONOCHROME1 " ||
463 strPhotometricInterpretation == "MONOCHROME2 " )
469 // we assume that *all* kinds of YBR are dealt with
475 * \ingroup gdcmHeader
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
518 float gdcmHeader::GetXOrigin()
520 float xImPos, yImPos, zImPos;
521 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
523 if ( strImPos == GDCM_UNFOUND )
525 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image 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 Position (RET) (0020,0030)");
530 /// \todo How to tell the caller nothing was found ?
535 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
544 * \brief gets the info from 0020,0032 : Image Position Patient
545 * else from 0020,0030 : Image Position (RET)
547 * @return up-left image corner Y position
549 float gdcmHeader::GetYOrigin()
551 float xImPos, yImPos, zImPos;
552 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
554 if ( strImPos == GDCM_UNFOUND)
556 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position Patient (0020,0032)");
557 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
558 if ( strImPos == GDCM_UNFOUND )
560 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position (RET) (0020,0030)");
561 /// \todo How to tell the caller nothing was found ?
566 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
575 * \brief gets the info from 0020,0032 : Image Position Patient
576 * \ else from 0020,0030 : Image Position (RET)
577 * \ else from 0020,1041 : Slice Location
578 * \ else from 0020,0050 : Location
580 * @return up-left image corner Z position
582 float gdcmHeader::GetZOrigin()
584 float xImPos, yImPos, zImPos;
585 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
587 if ( strImPos != GDCM_UNFOUND )
589 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
591 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position Patient (0020,0032)");
592 return 0.; // bug in the element 0x0020,0x0032
600 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
601 if ( strImPos != GDCM_UNFOUND )
603 if( sscanf( strImPos.c_str(),
604 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
606 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
607 return 0.; // bug in the element 0x0020,0x0032
615 std::string strSliceLocation = GetEntryByNumber(0x0020,0x1041); // for *very* old ACR-NEMA images
616 if ( strSliceLocation != GDCM_UNFOUND )
618 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
620 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Slice Location (0020,1041)");
621 return 0.; // bug in the element 0x0020,0x1041
628 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: unfound Slice Location (0020,1041)");
630 std::string strLocation = GetEntryByNumber(0x0020,0x0050);
631 if ( strLocation != GDCM_UNFOUND )
633 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
635 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Location (0020,0050)");
636 return 0.; // bug in the element 0x0020,0x0050
643 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Location (0020,0050)");
645 return 0.; // Hopeless
649 * \brief gets the info from 0020,0013 : Image Number
651 * @return image number
653 int gdcmHeader::GetImageNumber()
655 // The function i atoi() takes the address of an area of memory as
656 // parameter and converts the string stored at that location to an integer
657 // using the external decimal to internal binary conversion rules. This may
658 // be preferable to sscanf() since atoi() is a much smaller, simpler and
659 // faster function. sscanf() can do all possible conversions whereas
660 // atoi() can only do single decimal integer conversions.
661 std::string strImNumber = GetEntryByNumber(0x0020,0x0013); //0020 0013 IS REL Image Number
662 if ( strImNumber != GDCM_UNFOUND )
664 return atoi( strImNumber.c_str() );
670 * \brief gets the info from 0008,0060 : Modality
671 * @return Modality Type
673 ModalityType gdcmHeader::GetModality()
675 // 0008 0060 CS ID Modality
676 std::string strModality = GetEntryByNumber(0x0008,0x0060);
677 if ( strModality != GDCM_UNFOUND )
679 if ( strModality.find("AU") < strModality.length()) return AU;
680 else if ( strModality.find("AS") < strModality.length()) return AS;
681 else if ( strModality.find("BI") < strModality.length()) return BI;
682 else if ( strModality.find("CF") < strModality.length()) return CF;
683 else if ( strModality.find("CP") < strModality.length()) return CP;
684 else if ( strModality.find("CR") < strModality.length()) return CR;
685 else if ( strModality.find("CT") < strModality.length()) return CT;
686 else if ( strModality.find("CS") < strModality.length()) return CS;
687 else if ( strModality.find("DD") < strModality.length()) return DD;
688 else if ( strModality.find("DF") < strModality.length()) return DF;
689 else if ( strModality.find("DG") < strModality.length()) return DG;
690 else if ( strModality.find("DM") < strModality.length()) return DM;
691 else if ( strModality.find("DS") < strModality.length()) return DS;
692 else if ( strModality.find("DX") < strModality.length()) return DX;
693 else if ( strModality.find("ECG") < strModality.length()) return ECG;
694 else if ( strModality.find("EPS") < strModality.length()) return EPS;
695 else if ( strModality.find("FA") < strModality.length()) return FA;
696 else if ( strModality.find("FS") < strModality.length()) return FS;
697 else if ( strModality.find("HC") < strModality.length()) return HC;
698 else if ( strModality.find("HD") < strModality.length()) return HD;
699 else if ( strModality.find("LP") < strModality.length()) return LP;
700 else if ( strModality.find("LS") < strModality.length()) return LS;
701 else if ( strModality.find("MA") < strModality.length()) return MA;
702 else if ( strModality.find("MR") < strModality.length()) return MR;
703 else if ( strModality.find("NM") < strModality.length()) return NM;
704 else if ( strModality.find("OT") < strModality.length()) return OT;
705 else if ( strModality.find("PT") < strModality.length()) return PT;
706 else if ( strModality.find("RF") < strModality.length()) return RF;
707 else if ( strModality.find("RG") < strModality.length()) return RG;
708 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
709 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
710 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
711 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
712 else if ( strModality.find("SM") < strModality.length()) return SM;
713 else if ( strModality.find("ST") < strModality.length()) return ST;
714 else if ( strModality.find("TG") < strModality.length()) return TG;
715 else if ( strModality.find("US") < strModality.length()) return US;
716 else if ( strModality.find("VF") < strModality.length()) return VF;
717 else if ( strModality.find("XA") < strModality.length()) return XA;
718 else if ( strModality.find("XC") < strModality.length()) return XC;
722 /// \todo throw error return value ???
723 /// specified <> unknow in our database
732 * \ingroup gdcmHeader
733 * \brief Retrieve the number of Bits Stored (actually used)
734 * (as opposite to number of Bits Allocated)
735 * @return The encountered number of Bits Stored, 0 by default.
736 * 0 means the file is NOT USABLE. The caller has to check it !
738 int gdcmHeader::GetBitsStored()
740 std::string strSize = GetEntryByNumber(0x0028,0x0101);
741 if ( strSize == GDCM_UNFOUND )
743 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to be mandatory");
744 return 0; // It's supposed to be mandatory
745 // the caller will have to check
748 return atoi( strSize.c_str() );
752 * \ingroup gdcmHeader
753 * \brief Retrieve the number of Bits Allocated
754 * (8, 12 -compacted ACR-NEMA files, 16, ...)
755 * @return The encountered number of Bits Allocated, 0 by default.
756 * 0 means the file is NOT USABLE. The caller has to check it !
758 int gdcmHeader::GetBitsAllocated()
760 std::string strSize = GetEntryByNumber(0x0028,0x0100);
761 if ( strSize == GDCM_UNFOUND )
763 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to be mandatory");
764 return 0; // It's supposed to be mandatory
765 // the caller will have to check
768 return atoi( strSize.c_str() );
772 * \ingroup gdcmHeader
773 * \brief Retrieve the number of Samples Per Pixel
774 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
775 * @return The encountered number of Samples Per Pixel, 1 by default.
776 * (Gray level Pixels)
778 int gdcmHeader::GetSamplesPerPixel()
780 std::string strSize = GetEntryByNumber(0x0028,0x0002);
781 if ( strSize == GDCM_UNFOUND )
783 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to be mandatory");
784 return 1; // Well, it's supposed to be mandatory ...
785 // but sometimes it's missing : *we* assume Gray pixels
788 return atoi( strSize.c_str() );
792 * \ingroup gdcmHeader
793 * \brief Retrieve the Planar Configuration for RGB images
794 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
795 * @return The encountered Planar Configuration, 0 by default.
797 int gdcmHeader::GetPlanarConfiguration()
799 std::string strSize = GetEntryByNumber(0x0028,0x0006);
800 if ( strSize == GDCM_UNFOUND )
805 return atoi( strSize.c_str() );
809 * \ingroup gdcmHeader
810 * \brief Return the size (in bytes) of a single pixel of data.
811 * @return The size in bytes of a single pixel of data; 0 by default
812 * 0 means the file is NOT USABLE; the caller will have to check
814 int gdcmHeader::GetPixelSize()
816 // 0028 0100 US IMG Bits Allocated
817 // (in order no to be messed up by old RGB images)
818 // if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
821 std::string pixelType = GetPixelType();
822 if ( pixelType == "8U" || pixelType == "8S" )
826 if ( pixelType == "16U" || pixelType == "16S")
830 if ( pixelType == "32U" || pixelType == "32S")
834 if ( pixelType == "FD" )
839 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
844 * \ingroup gdcmHeader
845 * \brief Build the Pixel Type of the image.
846 * Possible values are:
847 * - 8U unsigned 8 bit,
849 * - 16U unsigned 16 bit,
850 * - 16S signed 16 bit,
851 * - 32U unsigned 32 bit,
852 * - 32S signed 32 bit,
853 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
854 * \warning 12 bit images appear as 16 bit.
855 * 24 bit images appear as 8 bit
856 * @return 0S if nothing found. NOT USABLE file. The caller has to check
858 std::string gdcmHeader::GetPixelType()
860 std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
861 if ( bitsAlloc == GDCM_UNFOUND )
863 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
867 if ( bitsAlloc == "64" )
871 if ( bitsAlloc == "12" )
873 // It will be unpacked
876 else if ( bitsAlloc == "24" )
878 // (in order no to be messed up
879 bitsAlloc = "8"; // by old RGB images)
882 std::string sign = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
884 if (sign == GDCM_UNFOUND )
886 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
1199 * \brief Accesses the info from 0002,0010 : Transfert Syntax and gdcmTS
1201 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1203 std::string gdcmHeader::GetTransfertSyntaxName()
1205 // use the gdcmTS (TS : Transfert Syntax)
1206 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1208 if ( transfertSyntax == GDCM_UNFOUND )
1210 dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName:"
1211 " unfound Transfert Syntax (0002,0010)");
1212 return "Uncompressed ACR-NEMA";
1215 while ( ! isdigit(transfertSyntax[transfertSyntax.length()-1]) )
1217 transfertSyntax.erase(transfertSyntax.length()-1, 1);
1219 // we do it only when we need it
1220 gdcmTS* ts = gdcmGlobal::GetTS();
1221 std::string tsName = ts->GetValue( transfertSyntax );
1223 //delete ts; /// \todo Seg Fault when deleted ?!
1228 * \brief Sets the Pixel Area size in the Header
1229 * --> not-for-rats function
1230 * @param ImageDataSize new Pixel Area Size
1231 * warning : nothing else is checked
1233 void gdcmHeader::SetImageDataSize(size_t ImageDataSize)
1235 std::string content1;
1238 sprintf(car,"%d",ImageDataSize);
1240 gdcmDocEntry *a = GetDocEntryByNumber(GrPixel, NumPixel);
1241 a->SetLength(ImageDataSize);
1244 sprintf(car,"%d",ImageDataSize);
1246 SetEntryByNumber(content1, GrPixel, NumPixel);
1249 //-----------------------------------------------------------------------------
1253 * \brief anonymize a Header (removes Patient's personal info)
1254 * (read the code to see which ones ...)
1256 bool gdcmHeader::AnonymizeHeader()
1258 gdcmDocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1260 ReplaceIfExistByNumber (" ",0x0010, 0x2154); // Telephone
1261 ReplaceIfExistByNumber (" ",0x0010, 0x1040); // Adress
1262 ReplaceIfExistByNumber (" ",0x0010, 0x0020); // Patient ID
1264 if ( patientNameHE )
1266 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1267 if ( studyInstanceUID != GDCM_UNFOUND )
1269 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1273 ReplaceOrCreateByNumber(std::string("anonymised"), 0x0010, 0x0010);
1278 // (if any) remove or replace all the stuff that contains a Date
1280 //0008 0012 DA ID Instance Creation Date
1281 //0008 0020 DA ID Study Date
1282 //0008 0021 DA ID Series Date
1283 //0008 0022 DA ID Acquisition Date
1284 //0008 0023 DA ID Content Date
1285 //0008 0024 DA ID Overlay Date
1286 //0008 0025 DA ID Curve Date
1287 //0008 002a DT ID Acquisition Datetime
1288 //0018 9074 DT ACQ Frame Acquisition Datetime
1289 //0018 9151 DT ACQ Frame Reference Datetime
1290 //0018 a002 DT ACQ Contribution Date Time
1291 //0020 3403 SH REL Modified Image Date (RET)
1292 //0032 0032 DA SDY Study Verified Date
1293 //0032 0034 DA SDY Study Read Date
1294 //0032 1000 DA SDY Scheduled Study Start Date
1295 //0032 1010 DA SDY Scheduled Study Stop Date
1296 //0032 1040 DA SDY Study Arrival Date
1297 //0032 1050 DA SDY Study Completion Date
1298 //0038 001a DA VIS Scheduled Admission Date
1299 //0038 001c DA VIS Scheduled Discharge Date
1300 //0038 0020 DA VIS Admitting Date
1301 //0038 0030 DA VIS Discharge Date
1302 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1303 //0040 0004 DA PRC Scheduled Procedure Step End Date
1304 //0040 0244 DA PRC Performed Procedure Step Start Date
1305 //0040 0250 DA PRC Performed Procedure Step End Date
1306 //0040 2004 DA PRC Issue Date of Imaging Service Request
1307 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1308 //0040 4011 DT PRC Expected Completion Date and Time
1309 //0040 a030 DT PRC Verification Date Time
1310 //0040 a032 DT PRC Observation Date Time
1311 //0040 a120 DT PRC DateTime
1312 //0040 a121 DA PRC Date
1313 //0040 a13a DT PRC Referenced Datetime
1314 //0070 0082 DA ??? Presentation Creation Date
1315 //0100 0420 DT ??? SOP Autorization Date and Time
1316 //0400 0105 DT ??? Digital Signature DateTime
1317 //2100 0040 DA PJ Creation Date
1318 //3006 0008 DA SSET Structure Set Date
1319 //3008 0024 DA ??? Treatment Control Point Date
1320 //3008 0054 DA ??? First Treatment Date
1321 //3008 0056 DA ??? Most Recent Treatment Date
1322 //3008 0162 DA ??? Safe Position Exit Date
1323 //3008 0166 DA ??? Safe Position Return Date
1324 //3008 0250 DA ??? Treatment Date
1325 //300a 0006 DA RT RT Plan Date
1326 //300a 022c DA RT Air Kerma Rate Reference Date
1327 //300e 0004 DA RT Review Date
1333 * \brief gets the info from 0020,0037 : Image Orientation Patient
1334 * @param iop adress of the (6)float aray to receive values
1335 * @return cosines of image orientation patient
1337 void gdcmHeader::GetImageOrientationPatient( float* iop )
1339 //iop is supposed to be float[6]
1340 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0;
1342 // 0020 0037 DS REL Image Orientation (Patient)
1343 std::string strImOriPat = GetEntryByNumber(0x0020,0x0037);
1344 if ( strImOriPat != GDCM_UNFOUND )
1346 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1347 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1349 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0037)");
1350 return ; // bug in the element 0x0020,0x0037
1359 // 0020 0035 DS REL Image Orientation (RET)
1360 strImOriPat = GetEntryByNumber(0x0020,0x0035);
1361 if ( strImOriPat != GDCM_UNFOUND )
1363 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1364 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1366 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0035)");
1367 return ; // bug in the element 0x0020,0x0035
1376 //-----------------------------------------------------------------------------
1379 //-----------------------------------------------------------------------------