1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/07/30 11:40:13 $
7 Version: $Revision: 1.179 $
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
34 * @param ignore_shadow = true if user wants to skip shadow groups
35 * during parsing, to save memory space
37 gdcmHeader::gdcmHeader( std::string const & filename,
38 bool exception_on_error,
40 gdcmDocument( filename,
44 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
45 // We may encounter the 'RETired' (0x0028, 0x0200) tag
46 // (Image Location") . This Element contains the number of
47 // the group that contains the pixel data (hence the "Pixel Data"
48 // is found by indirection through the "Image Location").
49 // Inside the group pointed by "Image Location" the searched element
50 // is conventionally the element 0x0010 (when the norm is respected).
51 // When the "Image Location" is absent we default to group 0x7fe0.
53 // This IS the right place for the code
56 std::string imgLocation = GetEntryByNumber(0x0028, 0x0200);
57 if ( imgLocation == GDCM_UNFOUND )
64 GrPixel = (uint16_t) atoi( imgLocation.c_str() );
67 // sometimes Image Location value doesn't follow
68 // the supposed processor endianity.
69 // see gdcmData/cr172241.dcm
70 if ( GrPixel == 0xe07f )
75 if ( GrPixel != 0x7fe0 )
77 // This is a kludge for old dirty Philips imager.
88 * @param exception_on_error whether we want to throw an exception or not
90 gdcmHeader::gdcmHeader(bool exception_on_error) :
91 gdcmDocument( exception_on_error )
97 * \brief Canonical destructor.
99 gdcmHeader::~gdcmHeader ()
105 * \brief Performs some consistency checking on various 'File related'
106 * (as opposed to 'DicomDir related') entries
107 * then writes in a file all the (Dicom Elements) included the Pixels
108 * @param fp file pointer on an already open file
109 * @param filetype Type of the File to be written
110 * (ACR-NEMA, ExplicitVR, ImplicitVR)
112 void gdcmHeader::Write(FILE* fp,FileType filetype) {
115 if ( GetEntryByNumber(0x0028,0x0100) == "12") {
116 SetEntryByNumber("16", 0x0028,0x0100);
119 // correct Pixel group Length if necessary
121 // TODO : create a gdcmHeader::Write method and move this part.
122 // (only gdcmHeader knows GrPixel, NumPixel)
124 int i_lgPix = GetEntryLengthByNumber(GrPixel, NumPixel);
125 if (i_lgPix != -2) { // no (GrPixel, NumPixel) element
126 char * dumm = new char[20];
127 sprintf(dumm ,"%d", i_lgPix+12);
128 std::string s_lgPix = dumm;
130 ReplaceOrCreateByNumber(s_lgPix,GrPixel, 0x0000);
133 // Drop Palette Color, if necessary
135 if ( GetEntryByNumber(0x0028,0x0002).c_str()[0] == '3' ) {
136 // if SamplesPerPixel = 3, sure we don't need any LUT !
137 // Drop 0028|1101, 0028|1102, 0028|1103
138 // Drop 0028|1201, 0028|1202, 0028|1203
141 e=GetDocEntryByNumber(0x0028,0x01101);
144 e=GetDocEntryByNumber(0x0028,0x1102);
147 e=GetDocEntryByNumber(0x0028,0x1103);
151 e=GetDocEntryByNumber(0x0028,0x01201);
154 e=GetDocEntryByNumber(0x0028,0x1202);
157 e=GetDocEntryByNumber(0x0028,0x1203);
162 gdcmDocument::Write(fp,filetype);
165 //-----------------------------------------------------------------------------
169 //-----------------------------------------------------------------------------
173 * \brief This predicate, based on hopefully reasonable heuristics,
174 * decides whether or not the current gdcmHeader was properly parsed
175 * and contains the mandatory information for being considered as
176 * a well formed and usable Dicom/Acr File.
177 * @return true when gdcmHeader is the one of a reasonable Dicom/Acr file,
180 bool gdcmHeader::IsReadable()
182 if( !gdcmDocument::IsReadable() )
187 std::string res = GetEntryByNumber(0x0028, 0x0005);
188 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
190 return false; // Image Dimensions
192 if ( !GetDocEntryByNumber(0x0028, 0x0100) )
194 return false; // "Bits Allocated"
196 if ( !GetDocEntryByNumber(0x0028, 0x0101) )
198 return false; // "Bits Stored"
200 if ( !GetDocEntryByNumber(0x0028, 0x0102) )
202 return false; // "High Bit"
204 if ( !GetDocEntryByNumber(0x0028, 0x0103) )
206 return false; // "Pixel Representation" i.e. 'Sign'
213 * \brief Retrieve the number of columns of image.
214 * @return The encountered size when found, 0 by default.
215 * 0 means the file is NOT USABLE. The caller will have to check
217 int gdcmHeader::GetXSize()
220 strSize = GetEntryByNumber(0x0028,0x0011);
221 if ( strSize == GDCM_UNFOUND )
226 return atoi( strSize.c_str() );
230 * \ingroup gdcmHeader
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 * \ingroup gdcmHeader
255 * \brief Retrieve the number of planes of volume or the number
256 * of frames of a multiframe.
257 * \warning When present we consider the "Number of Frames" as the third
258 * dimension. When absent we consider the third dimension as
259 * being the ACR-NEMA "Planes" tag content.
260 * @return The encountered size when found, 1 by default (single image).
262 int gdcmHeader::GetZSize()
264 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
265 // as the third dimension.
266 std::string strSize = GetEntryByNumber(0x0028,0x0008);
267 if ( strSize != GDCM_UNFOUND )
269 return atoi( strSize.c_str() );
272 // We then consider the "Planes" entry as the third dimension
273 strSize = GetEntryByNumber(0x0028,0x0012);
274 if ( strSize != GDCM_UNFOUND )
276 return atoi( strSize.c_str() );
283 * \ingroup gdcmHeader
284 * \brief gets the info from 0028,0030 : Pixel Spacing
286 * @return X dimension of a pixel
288 float gdcmHeader::GetXSpacing()
290 float xspacing, yspacing;
291 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
293 if ( strSpacing == GDCM_UNFOUND )
295 dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
300 if( ( nbValues = sscanf( strSpacing.c_str(),
301 "%f\\%f", &yspacing, &xspacing)) != 2 )
303 // if single value is found, xspacing is defaulted to yspacing
311 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
312 // seems to be a bug in the header ...
313 sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
320 * \ingroup gdcmHeader
321 * \brief gets the info from 0028,0030 : Pixel Spacing
323 * @return Y dimension of a pixel
325 float gdcmHeader::GetYSpacing()
328 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
330 if ( strSpacing == GDCM_UNFOUND )
332 dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
336 // if sscanf cannot read any float value, it won't affect yspacing
337 sscanf( strSpacing.c_str(), "%f", &yspacing);
344 *\brief gets the info from 0018,0088 : Space Between Slices
345 * else from 0018,0050 : Slice Thickness
347 * @return Z dimension of a voxel-to be
349 float gdcmHeader::GetZSpacing()
351 // Spacing Between Slices : distance entre le milieu de chaque coupe
352 // Les coupes peuvent etre :
353 // jointives (Spacing between Slices = Slice Thickness)
354 // chevauchantes (Spacing between Slices < Slice Thickness)
355 // disjointes (Spacing between Slices > Slice Thickness)
356 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
357 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
358 // Si le Spacing Between Slices est absent,
359 // on suppose que les coupes sont jointives
361 std::string strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
363 if ( strSpacingBSlices == GDCM_UNFOUND )
365 dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
366 std::string strSliceThickness = GetEntryByNumber(0x0018,0x0050);
367 if ( strSliceThickness == GDCM_UNFOUND )
373 // if no 'Spacing Between Slices' is found,
374 // we assume slices join together
375 // (no overlapping, no interslice gap)
376 // if they don't, we're fucked up
377 return atof( strSliceThickness.c_str() );
382 return atof( strSpacingBSlices.c_str() );
388 *\brief gets the info from 0028,1052 : Rescale Intercept
389 * @return Rescale Intercept
391 float gdcmHeader::GetRescaleIntercept()
394 std::string strRescInter = GetEntryByNumber(0x0028,0x1052); //0028 1052 DS IMG Rescale Intercept
395 if ( strRescInter != GDCM_UNFOUND )
397 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
399 // bug in the element 0x0028,0x1052
400 dbg.Verbose(0, "gdcmHeader::GetRescaleIntercept: Rescale Slope is empty");
409 *\brief gets the info from 0028,1053 : Rescale Slope
410 * @return Rescale Slope
412 float gdcmHeader::GetRescaleSlope()
415 std::string strRescSlope = GetEntryByNumber(0x0028,0x1053); //0028 1053 DS IMG Rescale Slope
416 if ( strRescSlope != GDCM_UNFOUND )
418 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
420 // bug in the element 0x0028,0x1053
421 dbg.Verbose(0, "gdcmHeader::GetRescaleSlope: Rescale Slope is empty");
429 * \ingroup gdcmHeader
430 * \brief This function is intended to user who doesn't want
431 * to have to manage a LUT and expects to get an RBG Pixel image
432 * (or a monochrome one ...)
433 * \warning to be used with GetImagePixels()
434 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
436 int gdcmHeader::GetNumberOfScalarComponents()
438 if ( GetSamplesPerPixel() == 3 )
443 // 0028 0100 US IMG Bits Allocated
444 // (in order no to be messed up by old RGB images)
445 if ( GetEntryByNumber(0x0028,0x0100) == "24" )
450 std::string strPhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
452 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
454 if ( HasLUT() )// PALETTE COLOR is NOT enough
464 // beware of trailing space at end of string
465 // DICOM tags are never of odd length
466 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
467 strPhotometricInterpretation == "MONOCHROME1 " ||
468 strPhotometricInterpretation == "MONOCHROME2 " )
474 // we assume that *all* kinds of YBR are dealt with
480 * \ingroup gdcmHeader
481 * \brief This function is intended to user that DOESN'T want
482 * to get RGB pixels image when it's stored as a PALETTE COLOR image
483 * - the (vtk) user is supposed to know how deal with LUTs -
484 * \warning to be used with GetImagePixelsRaw()
485 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
487 int gdcmHeader::GetNumberOfScalarComponentsRaw()
489 // 0028 0100 US IMG Bits Allocated
490 // (in order no to be messed up by old RGB images)
491 if ( gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24" )
496 // we assume that *all* kinds of YBR are dealt with
497 return GetSamplesPerPixel();
501 // -------------- Remember ! ----------------------------------
503 // Image Position Patient (0020,0032):
504 // If not found (ACR_NEMA) we try Image Position (0020,0030)
505 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
506 // or Location (0020,0050)
507 // as the Z coordinate,
508 // 0. for all the coordinates if nothing is found
510 // \todo find a way to inform the caller nothing was found
511 // \todo How to tell the caller a wrong number of values was found?
513 // ---------------------------------------------------------------
517 * \brief gets the info from 0020,0032 : Image Position Patient
518 * else from 0020,0030 : Image Position (RET)
520 * @return up-left image corner X position
523 float gdcmHeader::GetXOrigin()
525 float xImPos, yImPos, zImPos;
526 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
528 if ( strImPos == GDCM_UNFOUND )
530 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position Patient (0020,0032)");
531 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
532 if ( strImPos == GDCM_UNFOUND )
534 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position (RET) (0020,0030)");
535 /// \todo How to tell the caller nothing was found ?
540 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
549 * \brief gets the info from 0020,0032 : Image Position Patient
550 * else from 0020,0030 : Image Position (RET)
552 * @return up-left image corner Y position
554 float gdcmHeader::GetYOrigin()
556 float xImPos, yImPos, zImPos;
557 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
559 if ( strImPos == GDCM_UNFOUND)
561 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position Patient (0020,0032)");
562 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
563 if ( strImPos == GDCM_UNFOUND )
565 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position (RET) (0020,0030)");
566 /// \todo How to tell the caller nothing was found ?
571 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
580 * \brief gets the info from 0020,0032 : Image Position Patient
581 * \ else from 0020,0030 : Image Position (RET)
582 * \ else from 0020,1041 : Slice Location
583 * \ else from 0020,0050 : Location
585 * @return up-left image corner Z position
587 float gdcmHeader::GetZOrigin()
589 float xImPos, yImPos, zImPos;
590 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
592 if ( strImPos != GDCM_UNFOUND )
594 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
596 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position Patient (0020,0032)");
597 return 0.; // bug in the element 0x0020,0x0032
605 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
606 if ( strImPos != GDCM_UNFOUND )
608 if( sscanf( strImPos.c_str(),
609 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
611 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
612 return 0.; // bug in the element 0x0020,0x0032
620 std::string strSliceLocation = GetEntryByNumber(0x0020,0x1041); // for *very* old ACR-NEMA images
621 if ( strSliceLocation != GDCM_UNFOUND )
623 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
625 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Slice Location (0020,1041)");
626 return 0.; // bug in the element 0x0020,0x1041
633 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: unfound Slice Location (0020,1041)");
635 std::string strLocation = GetEntryByNumber(0x0020,0x0050);
636 if ( strLocation != GDCM_UNFOUND )
638 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
640 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Location (0020,0050)");
641 return 0.; // bug in the element 0x0020,0x0050
648 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Location (0020,0050)");
650 return 0.; // Hopeless
654 * \brief gets the info from 0020,0013 : Image Number
656 * @return image number
658 int gdcmHeader::GetImageNumber()
660 // The function i atoi() takes the address of an area of memory as
661 // parameter and converts the string stored at that location to an integer
662 // using the external decimal to internal binary conversion rules. This may
663 // be preferable to sscanf() since atoi() is a much smaller, simpler and
664 // faster function. sscanf() can do all possible conversions whereas
665 // atoi() can only do single decimal integer conversions.
666 std::string strImNumber = GetEntryByNumber(0x0020,0x0013); //0020 0013 IS REL Image Number
667 if ( strImNumber != GDCM_UNFOUND )
669 return atoi( strImNumber.c_str() );
675 * \brief gets the info from 0008,0060 : Modality
676 * @return Modality Type
678 ModalityType gdcmHeader::GetModality()
680 // 0008 0060 CS ID Modality
681 std::string strModality = GetEntryByNumber(0x0008,0x0060);
682 if ( strModality != GDCM_UNFOUND )
684 if ( strModality.find("AU") < strModality.length()) return AU;
685 else if ( strModality.find("AS") < strModality.length()) return AS;
686 else if ( strModality.find("BI") < strModality.length()) return BI;
687 else if ( strModality.find("CF") < strModality.length()) return CF;
688 else if ( strModality.find("CP") < strModality.length()) return CP;
689 else if ( strModality.find("CR") < strModality.length()) return CR;
690 else if ( strModality.find("CT") < strModality.length()) return CT;
691 else if ( strModality.find("CS") < strModality.length()) return CS;
692 else if ( strModality.find("DD") < strModality.length()) return DD;
693 else if ( strModality.find("DF") < strModality.length()) return DF;
694 else if ( strModality.find("DG") < strModality.length()) return DG;
695 else if ( strModality.find("DM") < strModality.length()) return DM;
696 else if ( strModality.find("DS") < strModality.length()) return DS;
697 else if ( strModality.find("DX") < strModality.length()) return DX;
698 else if ( strModality.find("ECG") < strModality.length()) return ECG;
699 else if ( strModality.find("EPS") < strModality.length()) return EPS;
700 else if ( strModality.find("FA") < strModality.length()) return FA;
701 else if ( strModality.find("FS") < strModality.length()) return FS;
702 else if ( strModality.find("HC") < strModality.length()) return HC;
703 else if ( strModality.find("HD") < strModality.length()) return HD;
704 else if ( strModality.find("LP") < strModality.length()) return LP;
705 else if ( strModality.find("LS") < strModality.length()) return LS;
706 else if ( strModality.find("MA") < strModality.length()) return MA;
707 else if ( strModality.find("MR") < strModality.length()) return MR;
708 else if ( strModality.find("NM") < strModality.length()) return NM;
709 else if ( strModality.find("OT") < strModality.length()) return OT;
710 else if ( strModality.find("PT") < strModality.length()) return PT;
711 else if ( strModality.find("RF") < strModality.length()) return RF;
712 else if ( strModality.find("RG") < strModality.length()) return RG;
713 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
714 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
715 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
716 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
717 else if ( strModality.find("SM") < strModality.length()) return SM;
718 else if ( strModality.find("ST") < strModality.length()) return ST;
719 else if ( strModality.find("TG") < strModality.length()) return TG;
720 else if ( strModality.find("US") < strModality.length()) return US;
721 else if ( strModality.find("VF") < strModality.length()) return VF;
722 else if ( strModality.find("XA") < strModality.length()) return XA;
723 else if ( strModality.find("XC") < strModality.length()) return XC;
727 /// \todo throw error return value ???
728 /// specified <> unknow in our database
737 * \ingroup gdcmHeader
738 * \brief Retrieve the number of Bits Stored (actually used)
739 * (as opposite to number of Bits Allocated)
740 * @return The encountered number of Bits Stored, 0 by default.
741 * 0 means the file is NOT USABLE. The caller has to check it !
743 int gdcmHeader::GetBitsStored()
745 std::string strSize = GetEntryByNumber(0x0028,0x0101);
746 if ( strSize == GDCM_UNFOUND )
748 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to be mandatory");
749 return 0; // It's supposed to be mandatory
750 // 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
773 return atoi( strSize.c_str() );
777 * \ingroup gdcmHeader
778 * \brief Retrieve the number of Samples Per Pixel
779 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
780 * @return The encountered number of Samples Per Pixel, 1 by default.
781 * (Gray level Pixels)
783 int gdcmHeader::GetSamplesPerPixel()
785 std::string strSize = GetEntryByNumber(0x0028,0x0002);
786 if ( strSize == GDCM_UNFOUND )
788 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to be mandatory");
789 return 1; // Well, it's supposed to be mandatory ...
790 // but sometimes it's missing : *we* assume Gray pixels
793 return atoi( strSize.c_str() );
797 * \ingroup gdcmHeader
798 * \brief Retrieve the Planar Configuration for RGB images
799 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
800 * @return The encountered Planar Configuration, 0 by default.
802 int gdcmHeader::GetPlanarConfiguration()
804 std::string strSize = GetEntryByNumber(0x0028,0x0006);
805 if ( strSize == GDCM_UNFOUND )
810 return atoi( strSize.c_str() );
814 * \ingroup gdcmHeader
815 * \brief Return the size (in bytes) of a single pixel of data.
816 * @return The size in bytes of a single pixel of data; 0 by default
817 * 0 means the file is NOT USABLE; the caller will have to check
819 int gdcmHeader::GetPixelSize()
821 // 0028 0100 US IMG Bits Allocated
822 // (in order no to be messed up by old RGB images)
823 // if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
826 std::string pixelType = GetPixelType();
827 if ( pixelType == "8U" || pixelType == "8S" )
831 if ( pixelType == "16U" || pixelType == "16S")
835 if ( pixelType == "32U" || pixelType == "32S")
839 if ( pixelType == "FD" )
844 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
849 * \ingroup gdcmHeader
850 * \brief Build the Pixel Type of the image.
851 * Possible values are:
852 * - 8U unsigned 8 bit,
854 * - 16U unsigned 16 bit,
855 * - 16S signed 16 bit,
856 * - 32U unsigned 32 bit,
857 * - 32S signed 32 bit,
858 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
859 * \warning 12 bit images appear as 16 bit.
860 * 24 bit images appear as 8 bit
861 * @return 0S if nothing found. NOT USABLE file. The caller has to check
863 std::string gdcmHeader::GetPixelType()
865 std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
866 if ( bitsAlloc == GDCM_UNFOUND )
868 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
872 if ( bitsAlloc == "64" )
876 if ( bitsAlloc == "12" )
878 // It will be unpacked
881 else if ( bitsAlloc == "24" )
883 // (in order no to be messed up
884 bitsAlloc = "8"; // by old RGB images)
887 std::string sign = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
889 if (sign == GDCM_UNFOUND )
891 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
903 return bitsAlloc + sign;
908 * \ingroup gdcmHeader
909 * \brief Recover the offset (from the beginning of the file)
910 * of *image* pixels (not *icone image* pixels, if any !)
911 * @return Pixel Offset
913 size_t gdcmHeader::GetPixelOffset()
915 gdcmDocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
918 return pxlElement->GetOffset();
923 std::cout << "Big trouble : Pixel Element ("
924 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
931 // TODO : unify those two (previous one and next one)
933 * \ingroup gdcmHeader
934 * \brief Recover the pixel area length (in Bytes)
935 * @return Pixel Element Length, as stored in the header
936 * (NOT the memory space necessary to hold the Pixels
937 * -in case of embeded compressed image-)
938 * 0 : NOT USABLE file. The caller has to check.
940 size_t gdcmHeader::GetPixelAreaLength()
942 gdcmDocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
945 return pxlElement->GetLength();
950 std::cout << "Big trouble : Pixel Element ("
951 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
959 * \ingroup gdcmHeader
960 * \brief tells us if LUT are used
961 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
962 * are NOT considered as LUT, since nobody knows
963 * how to deal with them
964 * Please warn me if you know sbdy that *does* know ... jprx
965 * @return true if LUT Descriptors and LUT Tables were found
967 bool gdcmHeader::HasLUT()
969 // Check the presence of the LUT Descriptors, and LUT Tables
971 if ( !GetDocEntryByNumber(0x0028,0x1101) )
975 // LutDescriptorGreen
976 if ( !GetDocEntryByNumber(0x0028,0x1102) )
981 if ( !GetDocEntryByNumber(0x0028,0x1103) )
985 // Red Palette Color Lookup Table Data
986 if ( !GetDocEntryByNumber(0x0028,0x1201) )
990 // Green Palette Color Lookup Table Data
991 if ( !GetDocEntryByNumber(0x0028,0x1202) )
995 // Blue Palette Color Lookup Table Data
996 if ( !GetDocEntryByNumber(0x0028,0x1203) )
1001 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1002 // NOT taken into account, but we don't know how to use it ...
1007 * \ingroup gdcmHeader
1008 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1010 * @return Lookup Table number of Bits , 0 by default
1011 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1012 * @ return bit number of each LUT item
1014 int gdcmHeader::GetLUTNbits()
1016 std::vector<std::string> tokens;
1019 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
1020 // Consistency already checked in GetLUTLength
1021 std::string lutDescription = GetEntryByNumber(0x0028,0x1101);
1022 if ( lutDescription == GDCM_UNFOUND )
1027 tokens.clear(); // clean any previous value
1028 Tokenize ( lutDescription, tokens, "\\" );
1029 //LutLength=atoi(tokens[0].c_str());
1030 //LutDepth=atoi(tokens[1].c_str());
1032 lutNbits = atoi( tokens[2].c_str() );
1039 * \ingroup gdcmHeader
1040 * \brief builts Red/Green/Blue/Alpha LUT from Header
1041 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1042 * and (0028,1101),(0028,1102),(0028,1102)
1043 * - xxx Palette Color Lookup Table Descriptor - are found
1044 * and (0028,1201),(0028,1202),(0028,1202)
1045 * - xxx Palette Color Lookup Table Data - are found
1046 * \warning does NOT deal with :
1047 * 0028 1100 Gray Lookup Table Descriptor (Retired)
1048 * 0028 1221 Segmented Red Palette Color Lookup Table Data
1049 * 0028 1222 Segmented Green Palette Color Lookup Table Data
1050 * 0028 1223 Segmented Blue Palette Color Lookup Table Data
1051 * no known Dicom reader deals with them :-(
1052 * @return a RGBA Lookup Table
1054 uint8_t* gdcmHeader::GetLUTRGBA()
1056 // Not so easy : see
1057 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
1059 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
1060 if ( GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR " )
1065 int lengthR, debR, nbitsR;
1066 int lengthG, debG, nbitsG;
1067 int lengthB, debB, nbitsB;
1069 // Get info from Lut Descriptors
1070 // (the 3 LUT descriptors may be different)
1071 std::string lutDescriptionR = GetEntryByNumber(0x0028,0x1101);
1072 if ( lutDescriptionR == GDCM_UNFOUND )
1077 std::string lutDescriptionG = GetEntryByNumber(0x0028,0x1102);
1078 if ( lutDescriptionG == GDCM_UNFOUND )
1083 std::string lutDescriptionB = GetEntryByNumber(0x0028,0x1103);
1084 if ( lutDescriptionB == GDCM_UNFOUND )
1089 // lengthR: Red LUT length in Bytes
1090 // debR: subscript of the first Lut Value
1091 // nbitsR: Lut item size (in Bits)
1092 int nbRead = sscanf( lutDescriptionR.c_str(), "%d\\%d\\%d",
1093 &lengthR, &debR, &nbitsR );
1096 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading red LUT");
1099 // lengthG: Green LUT length in Bytes
1100 // debG: subscript of the first Lut Value
1101 // nbitsG: Lut item size (in Bits)
1102 nbRead = sscanf( lutDescriptionG.c_str(), "%d\\%d\\%d",
1103 &lengthG, &debG, &nbitsG );
1106 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading green LUT");
1109 // lengthB: Blue LUT length in Bytes
1110 // debB: subscript of the first Lut Value
1111 // nbitsB: Lut item size (in Bits)
1112 nbRead = sscanf( lutDescriptionB.c_str(), "%d\\%d\\%d",
1113 &lengthB, &debB, &nbitsB );
1116 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading blue LUT");
1119 // Load LUTs into memory, (as they were stored on disk)
1120 uint8_t* lutR = (uint8_t*) GetEntryVoidAreaByNumber(0x0028,0x1201);
1121 uint8_t* lutG = (uint8_t*) GetEntryVoidAreaByNumber(0x0028,0x1202);
1122 uint8_t* lutB = (uint8_t*) GetEntryVoidAreaByNumber(0x0028,0x1203);
1124 if ( !lutR || !lutG || !lutB )
1126 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble with one of the LUT");
1129 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
1131 uint8_t* LUTRGBA = new uint8_t[1024]; // 256 * 4 (R, G, B, Alpha)
1136 memset(LUTRGBA, 0, 1024);
1140 std::string str_nb = GetEntryByNumber(0x0028,0x0100);
1141 if ( str_nb == GDCM_UNFOUND )
1147 nb = atoi( str_nb.c_str() );
1151 if ( nbitsR == 16 && nb == 8)
1153 // when LUT item size is different than pixel size
1154 mult = 2; // high byte must be = low byte
1158 // See PS 3.3-2003 C.11.1.1.2 p 619
1162 // if we get a black image, let's just remove the '+1'
1163 // from 'i*mult+1' and check again
1164 // if it works, we shall have to check the 3 Palettes
1165 // to see which byte is ==0 (first one, or second one)
1167 // We give up the checking to avoid some (useless ?)overhead
1168 // (optimistic asumption)
1173 for( i=0; i < lengthR; ++i)
1175 *a = lutR[i*mult+1];
1180 for( i=0; i < lengthG; ++i)
1182 *a = lutG[i*mult+1];
1187 for(i=0; i < lengthB; ++i)
1189 *a = lutB[i*mult+1];
1194 for(i=0; i < 256; ++i)
1196 *a = 1; // Alpha component
1204 * \brief Accesses the info from 0002,0010 : Transfert Syntax and gdcmTS
1206 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1208 std::string gdcmHeader::GetTransfertSyntaxName()
1210 // use the gdcmTS (TS : Transfert Syntax)
1211 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
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)
1240 std::string content1;
1243 sprintf(car,"%d",ImageDataSize);
1245 gdcmDocEntry *a = GetDocEntryByNumber(GrPixel, NumPixel);
1246 a->SetLength(ImageDataSize);
1249 sprintf(car,"%d",ImageDataSize);
1251 SetEntryByNumber(content1, GrPixel, NumPixel);
1254 //-----------------------------------------------------------------------------
1258 * \brief anonymize a Header (removes Patient's personal info)
1259 * (read the code to see which ones ...)
1261 bool gdcmHeader::AnonymizeHeader()
1263 gdcmDocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1265 ReplaceIfExistByNumber (" ",0x0010, 0x2154); // Telephone
1266 ReplaceIfExistByNumber (" ",0x0010, 0x1040); // Adress
1267 ReplaceIfExistByNumber (" ",0x0010, 0x0020); // Patient ID
1269 if ( patientNameHE )
1271 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1272 if ( studyInstanceUID != GDCM_UNFOUND )
1274 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1278 ReplaceOrCreateByNumber(std::string("anonymised"), 0x0010, 0x0010);
1283 // (if any) remove or replace all the stuff that contains a Date
1285 //0008 0012 DA ID Instance Creation Date
1286 //0008 0020 DA ID Study Date
1287 //0008 0021 DA ID Series Date
1288 //0008 0022 DA ID Acquisition Date
1289 //0008 0023 DA ID Content Date
1290 //0008 0024 DA ID Overlay Date
1291 //0008 0025 DA ID Curve Date
1292 //0008 002a DT ID Acquisition Datetime
1293 //0018 9074 DT ACQ Frame Acquisition Datetime
1294 //0018 9151 DT ACQ Frame Reference Datetime
1295 //0018 a002 DT ACQ Contribution Date Time
1296 //0020 3403 SH REL Modified Image Date (RET)
1297 //0032 0032 DA SDY Study Verified Date
1298 //0032 0034 DA SDY Study Read Date
1299 //0032 1000 DA SDY Scheduled Study Start Date
1300 //0032 1010 DA SDY Scheduled Study Stop Date
1301 //0032 1040 DA SDY Study Arrival Date
1302 //0032 1050 DA SDY Study Completion Date
1303 //0038 001a DA VIS Scheduled Admission Date
1304 //0038 001c DA VIS Scheduled Discharge Date
1305 //0038 0020 DA VIS Admitting Date
1306 //0038 0030 DA VIS Discharge Date
1307 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1308 //0040 0004 DA PRC Scheduled Procedure Step End Date
1309 //0040 0244 DA PRC Performed Procedure Step Start Date
1310 //0040 0250 DA PRC Performed Procedure Step End Date
1311 //0040 2004 DA PRC Issue Date of Imaging Service Request
1312 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1313 //0040 4011 DT PRC Expected Completion Date and Time
1314 //0040 a030 DT PRC Verification Date Time
1315 //0040 a032 DT PRC Observation Date Time
1316 //0040 a120 DT PRC DateTime
1317 //0040 a121 DA PRC Date
1318 //0040 a13a DT PRC Referenced Datetime
1319 //0070 0082 DA ??? Presentation Creation Date
1320 //0100 0420 DT ??? SOP Autorization Date and Time
1321 //0400 0105 DT ??? Digital Signature DateTime
1322 //2100 0040 DA PJ Creation Date
1323 //3006 0008 DA SSET Structure Set Date
1324 //3008 0024 DA ??? Treatment Control Point Date
1325 //3008 0054 DA ??? First Treatment Date
1326 //3008 0056 DA ??? Most Recent Treatment Date
1327 //3008 0162 DA ??? Safe Position Exit Date
1328 //3008 0166 DA ??? Safe Position Return Date
1329 //3008 0250 DA ??? Treatment Date
1330 //300a 0006 DA RT RT Plan Date
1331 //300a 022c DA RT Air Kerma Rate Reference Date
1332 //300e 0004 DA RT Review Date
1338 * \brief gets the info from 0020,0037 : Image Orientation Patient
1339 * @param iop adress of the (6)float aray to receive values
1340 * @return cosines of image orientation patient
1342 void gdcmHeader::GetImageOrientationPatient( float* iop )
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 std::string strImOriPat = GetEntryByNumber(0x0020,0x0037);
1349 if ( strImOriPat != GDCM_UNFOUND )
1351 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1352 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1354 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0037)");
1355 return ; // bug in the element 0x0020,0x0037
1364 // 0020 0035 DS REL Image Orientation (RET)
1365 strImOriPat = GetEntryByNumber(0x0020,0x0035);
1366 if ( strImOriPat != GDCM_UNFOUND )
1368 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1369 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1371 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0035)");
1372 return ; // bug in the element 0x0020,0x0035
1381 //-----------------------------------------------------------------------------
1384 //-----------------------------------------------------------------------------