1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/09/20 18:14:23 $
7 Version: $Revision: 1.187 $
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 // FIXME : should be nice if we could move it to gdcmFile
125 // (or in future gdcmPixelData class)
127 // Drop Palette Color, if necessary
129 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
135 gdcmDocEntry* e = GetDocEntryByNumber(0x0028,0x01101);
138 RemoveEntryNoDestroy(e);
140 e = GetDocEntryByNumber(0x0028,0x1102);
143 RemoveEntryNoDestroy(e);
145 e = GetDocEntryByNumber(0x0028,0x1103);
148 RemoveEntryNoDestroy(e);
150 e = GetDocEntryByNumber(0x0028,0x01201);
153 RemoveEntryNoDestroy(e);
155 e = GetDocEntryByNumber(0x0028,0x1202);
158 RemoveEntryNoDestroy(e);
160 e = GetDocEntryByNumber(0x0028,0x1203);
163 RemoveEntryNoDestroy(e);
166 gdcmDocument::Write(fp,filetype);
169 //-----------------------------------------------------------------------------
173 //-----------------------------------------------------------------------------
177 * \brief This predicate, based on hopefully reasonable heuristics,
178 * decides whether or not the current gdcmHeader was properly parsed
179 * and contains the mandatory information for being considered as
180 * a well formed and usable Dicom/Acr File.
181 * @return true when gdcmHeader is the one of a reasonable Dicom/Acr file,
184 bool gdcmHeader::IsReadable()
186 if( !gdcmDocument::IsReadable() )
191 std::string res = GetEntryByNumber(0x0028, 0x0005);
192 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
194 return false; // Image Dimensions
196 if ( !GetDocEntryByNumber(0x0028, 0x0100) )
198 return false; // "Bits Allocated"
200 if ( !GetDocEntryByNumber(0x0028, 0x0101) )
202 return false; // "Bits Stored"
204 if ( !GetDocEntryByNumber(0x0028, 0x0102) )
206 return false; // "High Bit"
208 if ( !GetDocEntryByNumber(0x0028, 0x0103) )
210 return false; // "Pixel Representation" i.e. 'Sign'
217 * \brief Retrieve the number of columns of image.
218 * @return The encountered size when found, 0 by default.
219 * 0 means the file is NOT USABLE. The caller will have to check
221 int gdcmHeader::GetXSize()
224 strSize = GetEntryByNumber(0x0028,0x0011);
225 if ( strSize == GDCM_UNFOUND )
230 return atoi( strSize.c_str() );
234 * \ingroup gdcmHeader
235 * \brief Retrieve the number of lines of image.
236 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
237 * @return The encountered size when found, 1 by default
238 * (The ACR-NEMA file contains a Signal, not an Image).
240 int gdcmHeader::GetYSize()
242 std::string strSize = GetEntryByNumber(0x0028,0x0010);
243 if ( strSize != GDCM_UNFOUND )
245 return atoi( strSize.c_str() );
252 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
253 // hence be a signal (1d image). So we default to 1:
258 * \ingroup gdcmHeader
259 * \brief Retrieve the number of planes of volume or the number
260 * of frames of a multiframe.
261 * \warning When present we consider the "Number of Frames" as the third
262 * dimension. When absent we consider the third dimension as
263 * being the ACR-NEMA "Planes" tag content.
264 * @return The encountered size when found, 1 by default (single image).
266 int gdcmHeader::GetZSize()
268 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
269 // as the third dimension.
270 std::string strSize = GetEntryByNumber(0x0028,0x0008);
271 if ( strSize != GDCM_UNFOUND )
273 return atoi( strSize.c_str() );
276 // We then consider the "Planes" entry as the third dimension
277 strSize = GetEntryByNumber(0x0028,0x0012);
278 if ( strSize != GDCM_UNFOUND )
280 return atoi( strSize.c_str() );
287 * \ingroup gdcmHeader
288 * \brief gets the info from 0028,0030 : Pixel Spacing
290 * @return X dimension of a pixel
292 float gdcmHeader::GetXSpacing()
294 float xspacing, yspacing;
295 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
297 if ( strSpacing == GDCM_UNFOUND )
299 dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
304 if( ( nbValues = sscanf( strSpacing.c_str(),
305 "%f\\%f", &yspacing, &xspacing)) != 2 )
307 // if single value is found, xspacing is defaulted to yspacing
315 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
316 // seems to be a bug in the header ...
317 sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
324 * \ingroup gdcmHeader
325 * \brief gets the info from 0028,0030 : Pixel Spacing
327 * @return Y dimension of a pixel
329 float gdcmHeader::GetYSpacing()
332 std::string strSpacing = GetEntryByNumber(0x0028,0x0030);
334 if ( strSpacing == GDCM_UNFOUND )
336 dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
340 // if sscanf cannot read any float value, it won't affect yspacing
341 sscanf( strSpacing.c_str(), "%f", &yspacing);
348 *\brief gets the info from 0018,0088 : Space Between Slices
349 * else from 0018,0050 : Slice Thickness
351 * @return Z dimension of a voxel-to be
353 float gdcmHeader::GetZSpacing()
355 // Spacing Between Slices : distance entre le milieu de chaque coupe
356 // Les coupes peuvent etre :
357 // jointives (Spacing between Slices = Slice Thickness)
358 // chevauchantes (Spacing between Slices < Slice Thickness)
359 // disjointes (Spacing between Slices > Slice Thickness)
360 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
361 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
362 // Si le Spacing Between Slices est absent,
363 // on suppose que les coupes sont jointives
365 std::string strSpacingBSlices = GetEntryByNumber(0x0018,0x0088);
367 if ( strSpacingBSlices == GDCM_UNFOUND )
369 dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
370 std::string strSliceThickness = GetEntryByNumber(0x0018,0x0050);
371 if ( strSliceThickness == GDCM_UNFOUND )
377 // if no 'Spacing Between Slices' is found,
378 // we assume slices join together
379 // (no overlapping, no interslice gap)
380 // if they don't, we're fucked up
381 return atof( strSliceThickness.c_str() );
386 return atof( strSpacingBSlices.c_str() );
392 *\brief gets the info from 0028,1052 : Rescale Intercept
393 * @return Rescale Intercept
395 float gdcmHeader::GetRescaleIntercept()
398 std::string strRescInter = GetEntryByNumber(0x0028,0x1052); //0028 1052 DS IMG Rescale Intercept
399 if ( strRescInter != GDCM_UNFOUND )
401 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
403 // bug in the element 0x0028,0x1052
404 dbg.Verbose(0, "gdcmHeader::GetRescaleIntercept: Rescale Slope is empty");
413 *\brief gets the info from 0028,1053 : Rescale Slope
414 * @return Rescale Slope
416 float gdcmHeader::GetRescaleSlope()
419 std::string strRescSlope = GetEntryByNumber(0x0028,0x1053); //0028 1053 DS IMG Rescale Slope
420 if ( strRescSlope != GDCM_UNFOUND )
422 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
424 // bug in the element 0x0028,0x1053
425 dbg.Verbose(0, "gdcmHeader::GetRescaleSlope: Rescale Slope is empty");
433 * \ingroup gdcmHeader
434 * \brief This function is intended to user who doesn't want
435 * to have to manage a LUT and expects to get an RBG Pixel image
436 * (or a monochrome one ...)
437 * \warning to be used with GetImagePixels()
438 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
440 int gdcmHeader::GetNumberOfScalarComponents()
442 if ( GetSamplesPerPixel() == 3 )
447 // 0028 0100 US IMG Bits Allocated
448 // (in order no to be messed up by old RGB images)
449 if ( GetEntryByNumber(0x0028,0x0100) == "24" )
454 std::string strPhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);
456 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
458 if ( HasLUT() )// PALETTE COLOR is NOT enough
468 // beware of trailing space at end of string
469 // DICOM tags are never of odd length
470 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
471 strPhotometricInterpretation == "MONOCHROME1 " ||
472 strPhotometricInterpretation == "MONOCHROME2 " )
478 // we assume that *all* kinds of YBR are dealt with
484 * \ingroup gdcmHeader
485 * \brief This function is intended to user that DOESN'T want
486 * to get RGB pixels image when it's stored as a PALETTE COLOR image
487 * - the (vtk) user is supposed to know how deal with LUTs -
488 * \warning to be used with GetImagePixelsRaw()
489 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
491 int gdcmHeader::GetNumberOfScalarComponentsRaw()
493 // 0028 0100 US IMG Bits Allocated
494 // (in order no to be messed up by old RGB images)
495 if ( gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24" )
500 // we assume that *all* kinds of YBR are dealt with
501 return GetSamplesPerPixel();
505 // -------------- Remember ! ----------------------------------
507 // Image Position Patient (0020,0032):
508 // If not found (ACR_NEMA) we try Image Position (0020,0030)
509 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
510 // or Location (0020,0050)
511 // as the Z coordinate,
512 // 0. for all the coordinates if nothing is found
514 // \todo find a way to inform the caller nothing was found
515 // \todo How to tell the caller a wrong number of values was found?
517 // ---------------------------------------------------------------
521 * \brief gets the info from 0020,0032 : Image Position Patient
522 * else from 0020,0030 : Image Position (RET)
524 * @return up-left image corner X position
527 float gdcmHeader::GetXOrigin()
529 float xImPos, yImPos, zImPos;
530 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
532 if ( strImPos == GDCM_UNFOUND )
534 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position Patient (0020,0032)");
535 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
536 if ( strImPos == GDCM_UNFOUND )
538 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position (RET) (0020,0030)");
539 /// \todo How to tell the caller nothing was found ?
544 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
553 * \brief gets the info from 0020,0032 : Image Position Patient
554 * else from 0020,0030 : Image Position (RET)
556 * @return up-left image corner Y position
558 float gdcmHeader::GetYOrigin()
560 float xImPos, yImPos, zImPos;
561 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
563 if ( strImPos == GDCM_UNFOUND)
565 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position Patient (0020,0032)");
566 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
567 if ( strImPos == GDCM_UNFOUND )
569 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position (RET) (0020,0030)");
570 /// \todo How to tell the caller nothing was found ?
575 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
584 * \brief gets the info from 0020,0032 : Image Position Patient
585 * \ else from 0020,0030 : Image Position (RET)
586 * \ else from 0020,1041 : Slice Location
587 * \ else from 0020,0050 : Location
589 * @return up-left image corner Z position
591 float gdcmHeader::GetZOrigin()
593 float xImPos, yImPos, zImPos;
594 std::string strImPos = GetEntryByNumber(0x0020,0x0032);
596 if ( strImPos != GDCM_UNFOUND )
598 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
600 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position Patient (0020,0032)");
601 return 0.; // bug in the element 0x0020,0x0032
609 strImPos = GetEntryByNumber(0x0020,0x0030); // For ACR-NEMA images
610 if ( strImPos != GDCM_UNFOUND )
612 if( sscanf( strImPos.c_str(),
613 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
615 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
616 return 0.; // bug in the element 0x0020,0x0032
624 std::string strSliceLocation = GetEntryByNumber(0x0020,0x1041); // for *very* old ACR-NEMA images
625 if ( strSliceLocation != GDCM_UNFOUND )
627 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
629 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Slice Location (0020,1041)");
630 return 0.; // bug in the element 0x0020,0x1041
637 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: unfound Slice Location (0020,1041)");
639 std::string strLocation = GetEntryByNumber(0x0020,0x0050);
640 if ( strLocation != GDCM_UNFOUND )
642 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
644 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Location (0020,0050)");
645 return 0.; // bug in the element 0x0020,0x0050
652 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Location (0020,0050)");
654 return 0.; // Hopeless
658 * \brief gets the info from 0020,0013 : Image Number
660 * @return image number
662 int gdcmHeader::GetImageNumber()
664 // The function i atoi() takes the address of an area of memory as
665 // parameter and converts the string stored at that location to an integer
666 // using the external decimal to internal binary conversion rules. This may
667 // be preferable to sscanf() since atoi() is a much smaller, simpler and
668 // faster function. sscanf() can do all possible conversions whereas
669 // atoi() can only do single decimal integer conversions.
670 std::string strImNumber = GetEntryByNumber(0x0020,0x0013); //0020 0013 IS REL Image Number
671 if ( strImNumber != GDCM_UNFOUND )
673 return atoi( strImNumber.c_str() );
679 * \brief gets the info from 0008,0060 : Modality
680 * @return Modality Type
682 ModalityType gdcmHeader::GetModality()
684 // 0008 0060 CS ID Modality
685 std::string strModality = GetEntryByNumber(0x0008,0x0060);
686 if ( strModality != GDCM_UNFOUND )
688 if ( strModality.find("AU") < strModality.length()) return AU;
689 else if ( strModality.find("AS") < strModality.length()) return AS;
690 else if ( strModality.find("BI") < strModality.length()) return BI;
691 else if ( strModality.find("CF") < strModality.length()) return CF;
692 else if ( strModality.find("CP") < strModality.length()) return CP;
693 else if ( strModality.find("CR") < strModality.length()) return CR;
694 else if ( strModality.find("CT") < strModality.length()) return CT;
695 else if ( strModality.find("CS") < strModality.length()) return CS;
696 else if ( strModality.find("DD") < strModality.length()) return DD;
697 else if ( strModality.find("DF") < strModality.length()) return DF;
698 else if ( strModality.find("DG") < strModality.length()) return DG;
699 else if ( strModality.find("DM") < strModality.length()) return DM;
700 else if ( strModality.find("DS") < strModality.length()) return DS;
701 else if ( strModality.find("DX") < strModality.length()) return DX;
702 else if ( strModality.find("ECG") < strModality.length()) return ECG;
703 else if ( strModality.find("EPS") < strModality.length()) return EPS;
704 else if ( strModality.find("FA") < strModality.length()) return FA;
705 else if ( strModality.find("FS") < strModality.length()) return FS;
706 else if ( strModality.find("HC") < strModality.length()) return HC;
707 else if ( strModality.find("HD") < strModality.length()) return HD;
708 else if ( strModality.find("LP") < strModality.length()) return LP;
709 else if ( strModality.find("LS") < strModality.length()) return LS;
710 else if ( strModality.find("MA") < strModality.length()) return MA;
711 else if ( strModality.find("MR") < strModality.length()) return MR;
712 else if ( strModality.find("NM") < strModality.length()) return NM;
713 else if ( strModality.find("OT") < strModality.length()) return OT;
714 else if ( strModality.find("PT") < strModality.length()) return PT;
715 else if ( strModality.find("RF") < strModality.length()) return RF;
716 else if ( strModality.find("RG") < strModality.length()) return RG;
717 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
718 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
719 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
720 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
721 else if ( strModality.find("SM") < strModality.length()) return SM;
722 else if ( strModality.find("ST") < strModality.length()) return ST;
723 else if ( strModality.find("TG") < strModality.length()) return TG;
724 else if ( strModality.find("US") < strModality.length()) return US;
725 else if ( strModality.find("VF") < strModality.length()) return VF;
726 else if ( strModality.find("XA") < strModality.length()) return XA;
727 else if ( strModality.find("XC") < strModality.length()) return XC;
731 /// \todo throw error return value ???
732 /// specified <> unknow in our database
741 * \ingroup gdcmHeader
742 * \brief Retrieve the number of Bits Stored (actually used)
743 * (as opposite to number of Bits Allocated)
744 * @return The encountered number of Bits Stored, 0 by default.
745 * 0 means the file is NOT USABLE. The caller has to check it !
747 int gdcmHeader::GetBitsStored()
749 std::string strSize = GetEntryByNumber(0x0028,0x0101);
750 if ( strSize == GDCM_UNFOUND )
752 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to be mandatory");
753 return 0; // It's supposed to be mandatory
754 // the caller will have to check
756 return atoi( strSize.c_str() );
760 * \ingroup gdcmHeader
761 * \brief Retrieve the number of Bits Allocated
762 * (8, 12 -compacted ACR-NEMA files, 16, ...)
763 * @return The encountered number of Bits Allocated, 0 by default.
764 * 0 means the file is NOT USABLE. The caller has to check it !
766 int gdcmHeader::GetBitsAllocated()
768 std::string strSize = GetEntryByNumber(0x0028,0x0100);
769 if ( strSize == GDCM_UNFOUND )
771 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to be mandatory");
772 return 0; // It's supposed to be mandatory
773 // the caller will have to check
775 return atoi( strSize.c_str() );
779 * \ingroup gdcmHeader
780 * \brief Retrieve the number of Samples Per Pixel
781 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
782 * @return The encountered number of Samples Per Pixel, 1 by default.
783 * (Gray level Pixels)
785 int gdcmHeader::GetSamplesPerPixel()
787 std::string strSize = GetEntryByNumber(0x0028,0x0002);
788 if ( strSize == GDCM_UNFOUND )
790 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to be mandatory");
791 return 1; // Well, it's supposed to be mandatory ...
792 // but sometimes it's missing : *we* assume Gray pixels
794 return atoi( strSize.c_str() );
798 * \ingroup gdcmHeader
799 * \brief Retrieve the Planar Configuration for RGB images
800 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
801 * @return The encountered Planar Configuration, 0 by default.
803 int gdcmHeader::GetPlanarConfiguration()
805 std::string strSize = GetEntryByNumber(0x0028,0x0006);
806 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" )
843 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
848 * \ingroup gdcmHeader
849 * \brief Build the Pixel Type of the image.
850 * Possible values are:
851 * - 8U unsigned 8 bit,
853 * - 16U unsigned 16 bit,
854 * - 16S signed 16 bit,
855 * - 32U unsigned 32 bit,
856 * - 32S signed 32 bit,
857 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
858 * \warning 12 bit images appear as 16 bit.
859 * 24 bit images appear as 8 bit
860 * @return 0S if nothing found. NOT USABLE file. The caller has to check
862 std::string gdcmHeader::GetPixelType()
864 std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
865 if ( bitsAlloc == GDCM_UNFOUND )
867 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
871 if ( bitsAlloc == "64" )
875 if ( bitsAlloc == "12" )
877 // It will be unpacked
880 else if ( bitsAlloc == "24" )
882 // (in order no to be messed up
883 bitsAlloc = "8"; // by old RGB images)
886 std::string sign = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
888 if (sign == GDCM_UNFOUND )
890 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
901 return bitsAlloc + sign;
906 * \ingroup gdcmHeader
907 * \brief Recover the offset (from the beginning of the file)
908 * of *image* pixels (not *icone image* pixels, if any !)
909 * @return Pixel Offset
911 size_t gdcmHeader::GetPixelOffset()
913 gdcmDocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
916 return pxlElement->GetOffset();
921 std::cout << "Big trouble : Pixel Element ("
922 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
929 // TODO : unify those two (previous one and next one)
931 * \ingroup gdcmHeader
932 * \brief Recover the pixel area length (in Bytes)
933 * @return Pixel Element Length, as stored in the header
934 * (NOT the memory space necessary to hold the Pixels
935 * -in case of embeded compressed image-)
936 * 0 : NOT USABLE file. The caller has to check.
938 size_t gdcmHeader::GetPixelAreaLength()
940 gdcmDocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
943 return pxlElement->GetLength();
948 std::cout << "Big trouble : Pixel Element ("
949 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
957 * \ingroup gdcmHeader
958 * \brief tells us if LUT are used
959 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
960 * are NOT considered as LUT, since nobody knows
961 * how to deal with them
962 * Please warn me if you know sbdy that *does* know ... jprx
963 * @return true if LUT Descriptors and LUT Tables were found
965 bool gdcmHeader::HasLUT()
967 // Check the presence of the LUT Descriptors, and LUT Tables
969 if ( !GetDocEntryByNumber(0x0028,0x1101) )
973 // LutDescriptorGreen
974 if ( !GetDocEntryByNumber(0x0028,0x1102) )
979 if ( !GetDocEntryByNumber(0x0028,0x1103) )
983 // Red Palette Color Lookup Table Data
984 if ( !GetDocEntryByNumber(0x0028,0x1201) )
988 // Green Palette Color Lookup Table Data
989 if ( !GetDocEntryByNumber(0x0028,0x1202) )
993 // Blue Palette Color Lookup Table Data
994 if ( !GetDocEntryByNumber(0x0028,0x1203) )
999 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1000 // NOT taken into account, but we don't know how to use it ...
1005 * \ingroup gdcmHeader
1006 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1008 * @return Lookup Table number of Bits , 0 by default
1009 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1010 * @ return bit number of each LUT item
1012 int gdcmHeader::GetLUTNbits()
1014 std::vector<std::string> tokens;
1017 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
1018 // Consistency already checked in GetLUTLength
1019 std::string lutDescription = GetEntryByNumber(0x0028,0x1101);
1020 if ( lutDescription == GDCM_UNFOUND )
1025 tokens.clear(); // clean any previous value
1026 Tokenize ( lutDescription, tokens, "\\" );
1027 //LutLength=atoi(tokens[0].c_str());
1028 //LutDepth=atoi(tokens[1].c_str());
1030 lutNbits = atoi( tokens[2].c_str() );
1037 * \ingroup gdcmHeader
1038 * \brief builts Red/Green/Blue/Alpha LUT from Header
1039 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1040 * and (0028,1101),(0028,1102),(0028,1102)
1041 * - xxx Palette Color Lookup Table Descriptor - are found
1042 * and (0028,1201),(0028,1202),(0028,1202)
1043 * - xxx Palette Color Lookup Table Data - are found
1044 * \warning does NOT deal with :
1045 * 0028 1100 Gray Lookup Table Descriptor (Retired)
1046 * 0028 1221 Segmented Red Palette Color Lookup Table Data
1047 * 0028 1222 Segmented Green Palette Color Lookup Table Data
1048 * 0028 1223 Segmented Blue Palette Color Lookup Table Data
1049 * no known Dicom reader deals with them :-(
1050 * @return a RGBA Lookup Table
1052 uint8_t* gdcmHeader::GetLUTRGBA()
1054 // Not so easy : see
1055 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
1057 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
1058 if ( GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR " )
1063 int lengthR, debR, nbitsR;
1064 int lengthG, debG, nbitsG;
1065 int lengthB, debB, nbitsB;
1067 // Get info from Lut Descriptors
1068 // (the 3 LUT descriptors may be different)
1069 std::string lutDescriptionR = GetEntryByNumber(0x0028,0x1101);
1070 if ( lutDescriptionR == GDCM_UNFOUND )
1075 std::string lutDescriptionG = GetEntryByNumber(0x0028,0x1102);
1076 if ( lutDescriptionG == GDCM_UNFOUND )
1081 std::string lutDescriptionB = GetEntryByNumber(0x0028,0x1103);
1082 if ( lutDescriptionB == GDCM_UNFOUND )
1087 // lengthR: Red LUT length in Bytes
1088 // debR: subscript of the first Lut Value
1089 // nbitsR: Lut item size (in Bits)
1090 int nbRead = sscanf( lutDescriptionR.c_str(), "%d\\%d\\%d",
1091 &lengthR, &debR, &nbitsR );
1094 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading red LUT");
1097 // lengthG: Green LUT length in Bytes
1098 // debG: subscript of the first Lut Value
1099 // nbitsG: Lut item size (in Bits)
1100 nbRead = sscanf( lutDescriptionG.c_str(), "%d\\%d\\%d",
1101 &lengthG, &debG, &nbitsG );
1104 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading green LUT");
1107 // lengthB: Blue LUT length in Bytes
1108 // debB: subscript of the first Lut Value
1109 // nbitsB: Lut item size (in Bits)
1110 nbRead = sscanf( lutDescriptionB.c_str(), "%d\\%d\\%d",
1111 &lengthB, &debB, &nbitsB );
1114 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading blue LUT");
1117 // Load LUTs into memory, (as they were stored on disk)
1118 uint8_t* lutR = (uint8_t*) GetEntryVoidAreaByNumber(0x0028,0x1201);
1119 uint8_t* lutG = (uint8_t*) GetEntryVoidAreaByNumber(0x0028,0x1202);
1120 uint8_t* lutB = (uint8_t*) GetEntryVoidAreaByNumber(0x0028,0x1203);
1122 if ( !lutR || !lutG || !lutB )
1124 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble with one of the LUT");
1127 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
1129 uint8_t* LUTRGBA = new uint8_t[1024]; // 256 * 4 (R, G, B, Alpha)
1134 memset(LUTRGBA, 0, 1024);
1138 std::string str_nb = GetEntryByNumber(0x0028,0x0100);
1139 if ( str_nb == GDCM_UNFOUND )
1145 nb = atoi( str_nb.c_str() );
1149 if ( nbitsR == 16 && nb == 8)
1151 // when LUT item size is different than pixel size
1152 mult = 2; // high byte must be = low byte
1156 // See PS 3.3-2003 C.11.1.1.2 p 619
1160 // if we get a black image, let's just remove the '+1'
1161 // from 'i*mult+1' and check again
1162 // if it works, we shall have to check the 3 Palettes
1163 // to see which byte is ==0 (first one, or second one)
1165 // We give up the checking to avoid some (useless ?)overhead
1166 // (optimistic asumption)
1171 for( i=0; i < lengthR; ++i)
1173 *a = lutR[i*mult+1];
1178 for( i=0; i < lengthG; ++i)
1180 *a = lutG[i*mult+1];
1185 for(i=0; i < lengthB; ++i)
1187 *a = lutB[i*mult+1];
1192 for(i=0; i < 256; ++i)
1194 *a = 1; // Alpha component
1201 * \brief Accesses the info from 0002,0010 : Transfert Syntax and gdcmTS
1203 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1205 std::string gdcmHeader::GetTransfertSyntaxName()
1207 // use the gdcmTS (TS : Transfert Syntax)
1208 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1210 if ( transfertSyntax == GDCM_NOTLOADED ) { // fusible
1211 std::cout << "Transfert Syntax not loaded. " << std::endl
1212 << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE"
1214 return "Uncompressed ACR-NEMA";
1216 if ( transfertSyntax == GDCM_UNFOUND )
1218 dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName:"
1219 " unfound Transfert Syntax (0002,0010)");
1220 return "Uncompressed ACR-NEMA";
1223 while ( ! isdigit(transfertSyntax[transfertSyntax.length()-1]) )
1225 transfertSyntax.erase(transfertSyntax.length()-1, 1);
1227 // we do it only when we need it
1228 gdcmTS* ts = gdcmGlobal::GetTS();
1229 std::string tsName = ts->GetValue( transfertSyntax );
1231 //delete ts; /// \todo Seg Fault when deleted ?!
1236 * \brief Sets the Pixel Area size in the Header
1237 * --> not-for-rats function
1238 * @param ImageDataSize new Pixel Area Size
1239 * warning : nothing else is checked
1241 void gdcmHeader::SetImageDataSize(size_t ImageDataSize)
1244 sprintf(car,"%d",ImageDataSize);
1246 gdcmDocEntry *a = GetDocEntryByNumber(GrPixel, NumPixel);
1247 a->SetLength(ImageDataSize);
1250 sprintf(car,"%d",ImageDataSize);
1252 const std::string content1 = car;
1253 SetEntryByNumber(content1, GrPixel, NumPixel);
1256 //-----------------------------------------------------------------------------
1260 * \brief anonymize a Header (removes Patient's personal info)
1261 * (read the code to see which ones ...)
1263 bool gdcmHeader::AnonymizeHeader()
1265 // If exist, replace by spaces
1266 SetEntryByNumber (" ",0x0010, 0x2154); // Telephone
1267 SetEntryByNumber (" ",0x0010, 0x1040); // Adress
1268 SetEntryByNumber (" ",0x0010, 0x0020); // Patient ID
1270 gdcmDocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1272 if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1274 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1275 if ( studyInstanceUID != GDCM_UNFOUND )
1277 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1281 ReplaceOrCreateByNumber(std::string("anonymised"), 0x0010, 0x0010);
1286 // (if any) remove or replace all the stuff that contains a Date
1288 //0008 0012 DA ID Instance Creation Date
1289 //0008 0020 DA ID Study Date
1290 //0008 0021 DA ID Series Date
1291 //0008 0022 DA ID Acquisition Date
1292 //0008 0023 DA ID Content Date
1293 //0008 0024 DA ID Overlay Date
1294 //0008 0025 DA ID Curve Date
1295 //0008 002a DT ID Acquisition Datetime
1296 //0018 9074 DT ACQ Frame Acquisition Datetime
1297 //0018 9151 DT ACQ Frame Reference Datetime
1298 //0018 a002 DT ACQ Contribution Date Time
1299 //0020 3403 SH REL Modified Image Date (RET)
1300 //0032 0032 DA SDY Study Verified Date
1301 //0032 0034 DA SDY Study Read Date
1302 //0032 1000 DA SDY Scheduled Study Start Date
1303 //0032 1010 DA SDY Scheduled Study Stop Date
1304 //0032 1040 DA SDY Study Arrival Date
1305 //0032 1050 DA SDY Study Completion Date
1306 //0038 001a DA VIS Scheduled Admission Date
1307 //0038 001c DA VIS Scheduled Discharge Date
1308 //0038 0020 DA VIS Admitting Date
1309 //0038 0030 DA VIS Discharge Date
1310 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1311 //0040 0004 DA PRC Scheduled Procedure Step End Date
1312 //0040 0244 DA PRC Performed Procedure Step Start Date
1313 //0040 0250 DA PRC Performed Procedure Step End Date
1314 //0040 2004 DA PRC Issue Date of Imaging Service Request
1315 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1316 //0040 4011 DT PRC Expected Completion Date and Time
1317 //0040 a030 DT PRC Verification Date Time
1318 //0040 a032 DT PRC Observation Date Time
1319 //0040 a120 DT PRC DateTime
1320 //0040 a121 DA PRC Date
1321 //0040 a13a DT PRC Referenced Datetime
1322 //0070 0082 DA ??? Presentation Creation Date
1323 //0100 0420 DT ??? SOP Autorization Date and Time
1324 //0400 0105 DT ??? Digital Signature DateTime
1325 //2100 0040 DA PJ Creation Date
1326 //3006 0008 DA SSET Structure Set Date
1327 //3008 0024 DA ??? Treatment Control Point Date
1328 //3008 0054 DA ??? First Treatment Date
1329 //3008 0056 DA ??? Most Recent Treatment Date
1330 //3008 0162 DA ??? Safe Position Exit Date
1331 //3008 0166 DA ??? Safe Position Return Date
1332 //3008 0250 DA ??? Treatment Date
1333 //300a 0006 DA RT RT Plan Date
1334 //300a 022c DA RT Air Kerma Rate Reference Date
1335 //300e 0004 DA RT Review Date
1341 * \brief gets the info from 0020,0037 : Image Orientation Patient
1342 * @param iop adress of the (6)float aray to receive values
1343 * @return cosines of image orientation patient
1345 void gdcmHeader::GetImageOrientationPatient( float iop[6] )
1347 std::string strImOriPat;
1348 //iop is supposed to be float[6]
1349 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1351 // 0020 0037 DS REL Image Orientation (Patient)
1352 if ( (strImOriPat = GetEntryByNumber(0x0020,0x0037)) != GDCM_UNFOUND )
1354 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1355 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1357 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0037)");
1358 // bug in the element 0x0020,0x0037
1362 // 0020 0035 DS REL Image Orientation (RET)
1363 else if ( (strImOriPat = GetEntryByNumber(0x0020,0x0035)) != GDCM_UNFOUND )
1365 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1366 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1368 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0035)");
1369 // bug in the element 0x0020,0x0035
1374 //-----------------------------------------------------------------------------
1377 //-----------------------------------------------------------------------------