1 /*=========================================================================
4 Module: $RCSfile: gdcmHeader.cxx,v $
6 Date: $Date: 2004/07/17 22:45:40 $
7 Version: $Revision: 1.177 $
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 enable_sequences = true to allow the header
35 * to be parsed *inside* the SeQuences, when they have an actual length
36 * @param ignore_shadow = true if user wants to skip shadow groups
37 * during parsing, to save memory space
39 gdcmHeader::gdcmHeader( std::string const & filename,
40 bool exception_on_error,
41 bool enable_sequences,
43 gdcmDocument( filename,
48 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
49 // We may encounter the 'RETired' (0x0028, 0x0200) tag
50 // (Image Location") . This Element contains the number of
51 // the group that contains the pixel data (hence the "Pixel Data"
52 // is found by indirection through the "Image Location").
53 // Inside the group pointed by "Image Location" the searched element
54 // is conventionally the element 0x0010 (when the norm is respected).
55 // When the "Image Location" is absent we default to group 0x7fe0.
57 // This IS the right place for the code
60 std::string imgLocation = GetEntryByNumber(0x0028, 0x0200);
61 if ( imgLocation == GDCM_UNFOUND )
68 GrPixel = (uint16_t) atoi( imgLocation.c_str() );
71 // sometimes Image Location value doesn't follow
72 // the supposed processor endianity.
73 // see gdcmData/cr172241.dcm
74 if ( GrPixel == 0xe07f )
79 if ( GrPixel != 0x7fe0 )
81 // This is a kludge for old dirty Philips imager.
92 * @param exception_on_error whether we want to throw an exception or not
94 gdcmHeader::gdcmHeader(bool exception_on_error) :
95 gdcmDocument( exception_on_error )
100 * \ingroup gdcmHeader
101 * \brief Canonical destructor.
103 gdcmHeader::~gdcmHeader ()
109 * \brief Performs some consistency checking on various 'File related'
110 * (as opposed to 'DicomDir related') entries
111 * then writes in a file all the (Dicom Elements) included the Pixels
112 * @param fp file pointer on an already open file
113 * @param filetype Type of the File to be written
114 * (ACR-NEMA, ExplicitVR, ImplicitVR)
116 void gdcmHeader::Write(FILE* fp,FileType filetype) {
119 if ( GetEntryByNumber(0x0028,0x0100) == "12") {
120 SetEntryByNumber("16", 0x0028,0x0100);
123 // correct Pixel group Length if necessary
125 // TODO : create a gdcmHeader::Write method and move this part.
126 // (only gdcmHeader knows GrPixel, NumPixel)
128 int i_lgPix = GetEntryLengthByNumber(GrPixel, NumPixel);
129 if (i_lgPix != -2) { // no (GrPixel, NumPixel) element
130 char * dumm = new char[20];
131 sprintf(dumm ,"%d", i_lgPix+12);
132 std::string s_lgPix = dumm;
134 ReplaceOrCreateByNumber(s_lgPix,GrPixel, 0x0000);
137 // Drop Palette Color, if necessary
139 if ( GetEntryByNumber(0x0028,0x0002).c_str()[0] == '3' ) {
140 // if SamplesPerPixel = 3, sure we don't need any LUT !
141 // Drop 0028|1101, 0028|1102, 0028|1103
142 // Drop 0028|1201, 0028|1202, 0028|1203
145 e=GetDocEntryByNumber(0x0028,0x01101);
148 e=GetDocEntryByNumber(0x0028,0x1102);
151 e=GetDocEntryByNumber(0x0028,0x1103);
155 e=GetDocEntryByNumber(0x0028,0x01201);
158 e=GetDocEntryByNumber(0x0028,0x1202);
161 e=GetDocEntryByNumber(0x0028,0x1203);
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
757 return atoi( strSize.c_str() );
761 * \ingroup gdcmHeader
762 * \brief Retrieve the number of Bits Allocated
763 * (8, 12 -compacted ACR-NEMA files, 16, ...)
764 * @return The encountered number of Bits Allocated, 0 by default.
765 * 0 means the file is NOT USABLE. The caller has to check it !
767 int gdcmHeader::GetBitsAllocated()
769 std::string strSize = GetEntryByNumber(0x0028,0x0100);
770 if ( strSize == GDCM_UNFOUND )
772 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to be mandatory");
773 return 0; // It's supposed to be mandatory
774 // the caller will have to check
777 return atoi( strSize.c_str() );
781 * \ingroup gdcmHeader
782 * \brief Retrieve the number of Samples Per Pixel
783 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
784 * @return The encountered number of Samples Per Pixel, 1 by default.
785 * (Gray level Pixels)
787 int gdcmHeader::GetSamplesPerPixel()
789 std::string strSize = GetEntryByNumber(0x0028,0x0002);
790 if ( strSize == GDCM_UNFOUND )
792 dbg.Verbose(0, "gdcmHeader::GetBitsStored: this is supposed to be mandatory");
793 return 1; // Well, it's supposed to be mandatory ...
794 // but sometimes it's missing : *we* assume Gray pixels
797 return atoi( strSize.c_str() );
801 * \ingroup gdcmHeader
802 * \brief Retrieve the Planar Configuration for RGB images
803 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
804 * @return The encountered Planar Configuration, 0 by default.
806 int gdcmHeader::GetPlanarConfiguration()
808 std::string strSize = GetEntryByNumber(0x0028,0x0006);
809 if ( strSize == GDCM_UNFOUND )
814 return atoi( strSize.c_str() );
818 * \ingroup gdcmHeader
819 * \brief Return the size (in bytes) of a single pixel of data.
820 * @return The size in bytes of a single pixel of data; 0 by default
821 * 0 means the file is NOT USABLE; the caller will have to check
823 int gdcmHeader::GetPixelSize()
825 // 0028 0100 US IMG Bits Allocated
826 // (in order no to be messed up by old RGB images)
827 // if (gdcmHeader::GetEntryByNumber(0x0028,0x0100) == "24")
830 std::string pixelType = GetPixelType();
831 if ( pixelType == "8U" || pixelType == "8S" )
835 if ( pixelType == "16U" || pixelType == "16S")
839 if ( pixelType == "32U" || pixelType == "32S")
843 if ( pixelType == "FD" )
848 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
853 * \ingroup gdcmHeader
854 * \brief Build the Pixel Type of the image.
855 * Possible values are:
856 * - 8U unsigned 8 bit,
858 * - 16U unsigned 16 bit,
859 * - 16S signed 16 bit,
860 * - 32U unsigned 32 bit,
861 * - 32S signed 32 bit,
862 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
863 * \warning 12 bit images appear as 16 bit.
864 * 24 bit images appear as 8 bit
865 * @return 0S if nothing found. NOT USABLE file. The caller has to check
867 std::string gdcmHeader::GetPixelType()
869 std::string bitsAlloc = GetEntryByNumber(0x0028, 0x0100); // Bits Allocated
870 if ( bitsAlloc == GDCM_UNFOUND )
872 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
876 if ( bitsAlloc == "64" )
880 if ( bitsAlloc == "12" )
882 // It will be unpacked
885 else if ( bitsAlloc == "24" )
887 // (in order no to be messed up
888 bitsAlloc = "8"; // by old RGB images)
891 std::string sign = GetEntryByNumber(0x0028, 0x0103); // "Pixel Representation"
893 if (sign == GDCM_UNFOUND )
895 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
907 return bitsAlloc + sign;
912 * \ingroup gdcmHeader
913 * \brief Recover the offset (from the beginning of the file)
914 * of *image* pixels (not *icone image* pixels, if any !)
915 * @return Pixel Offset
917 size_t gdcmHeader::GetPixelOffset()
919 gdcmDocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
922 return pxlElement->GetOffset();
927 std::cout << "Big trouble : Pixel Element ("
928 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
935 // TODO : unify those two (previous one and next one)
937 * \ingroup gdcmHeader
938 * \brief Recover the pixel area length (in Bytes)
939 * @return Pixel Element Length, as stored in the header
940 * (NOT the memory space necessary to hold the Pixels
941 * -in case of embeded compressed image-)
942 * 0 : NOT USABLE file. The caller has to check.
944 size_t gdcmHeader::GetPixelAreaLength()
946 gdcmDocEntry* pxlElement = GetDocEntryByNumber(GrPixel,NumPixel);
949 return pxlElement->GetLength();
954 std::cout << "Big trouble : Pixel Element ("
955 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
963 * \ingroup gdcmHeader
964 * \brief tells us if LUT are used
965 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
966 * are NOT considered as LUT, since nobody knows
967 * how to deal with them
968 * Please warn me if you know sbdy that *does* know ... jprx
969 * @return true if LUT Descriptors and LUT Tables were found
971 bool gdcmHeader::HasLUT()
973 // Check the presence of the LUT Descriptors, and LUT Tables
975 if ( !GetDocEntryByNumber(0x0028,0x1101) )
979 // LutDescriptorGreen
980 if ( !GetDocEntryByNumber(0x0028,0x1102) )
985 if ( !GetDocEntryByNumber(0x0028,0x1103) )
989 // Red Palette Color Lookup Table Data
990 if ( !GetDocEntryByNumber(0x0028,0x1201) )
994 // Green Palette Color Lookup Table Data
995 if ( !GetDocEntryByNumber(0x0028,0x1202) )
999 // Blue Palette Color Lookup Table Data
1000 if ( !GetDocEntryByNumber(0x0028,0x1203) )
1005 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1006 // NOT taken into account, but we don't know how to use it ...
1011 * \ingroup gdcmHeader
1012 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1014 * @return Lookup Table number of Bits , 0 by default
1015 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1016 * @ return bit number of each LUT item
1018 int gdcmHeader::GetLUTNbits()
1020 std::vector<std::string> tokens;
1023 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red = Lookup Table Desc-Blue
1024 // Consistency already checked in GetLUTLength
1025 std::string lutDescription = GetEntryByNumber(0x0028,0x1101);
1026 if ( lutDescription == GDCM_UNFOUND )
1031 tokens.clear(); // clean any previous value
1032 Tokenize ( lutDescription, tokens, "\\" );
1033 //LutLength=atoi(tokens[0].c_str());
1034 //LutDepth=atoi(tokens[1].c_str());
1036 lutNbits = atoi( tokens[2].c_str() );
1043 * \ingroup gdcmHeader
1044 * \brief builts Red/Green/Blue/Alpha LUT from Header
1045 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1046 * and (0028,1101),(0028,1102),(0028,1102)
1047 * - xxx Palette Color Lookup Table Descriptor - are found
1048 * and (0028,1201),(0028,1202),(0028,1202)
1049 * - xxx Palette Color Lookup Table Data - are found
1050 * \warning does NOT deal with :
1051 * 0028 1100 Gray Lookup Table Descriptor (Retired)
1052 * 0028 1221 Segmented Red Palette Color Lookup Table Data
1053 * 0028 1222 Segmented Green Palette Color Lookup Table Data
1054 * 0028 1223 Segmented Blue Palette Color Lookup Table Data
1055 * no known Dicom reader deals with them :-(
1056 * @return a RGBA Lookup Table
1058 uint8_t* gdcmHeader::GetLUTRGBA()
1060 // Not so easy : see
1061 // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables
1063 // if Photometric Interpretation # PALETTE COLOR, no LUT to be done
1064 if ( GetEntryByNumber(0x0028,0x0004) != "PALETTE COLOR " )
1069 int lengthR, debR, nbitsR;
1070 int lengthG, debG, nbitsG;
1071 int lengthB, debB, nbitsB;
1073 // Get info from Lut Descriptors
1074 // (the 3 LUT descriptors may be different)
1075 std::string lutDescriptionR = GetEntryByNumber(0x0028,0x1101);
1076 if ( lutDescriptionR == GDCM_UNFOUND )
1081 std::string lutDescriptionG = GetEntryByNumber(0x0028,0x1102);
1082 if ( lutDescriptionG == GDCM_UNFOUND )
1087 std::string lutDescriptionB = GetEntryByNumber(0x0028,0x1103);
1088 if ( lutDescriptionB == GDCM_UNFOUND )
1093 // lengthR: Red LUT length in Bytes
1094 // debR: subscript of the first Lut Value
1095 // nbitsR: Lut item size (in Bits)
1096 int nbRead = sscanf( lutDescriptionR.c_str(), "%d\\%d\\%d",
1097 &lengthR, &debR, &nbitsR );
1100 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading red LUT");
1103 // lengthG: Green LUT length in Bytes
1104 // debG: subscript of the first Lut Value
1105 // nbitsG: Lut item size (in Bits)
1106 nbRead = sscanf( lutDescriptionG.c_str(), "%d\\%d\\%d",
1107 &lengthG, &debG, &nbitsG );
1110 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading green LUT");
1113 // lengthB: Blue LUT length in Bytes
1114 // debB: subscript of the first Lut Value
1115 // nbitsB: Lut item size (in Bits)
1116 nbRead = sscanf( lutDescriptionB.c_str(), "%d\\%d\\%d",
1117 &lengthB, &debB, &nbitsB );
1120 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble reading blue LUT");
1123 // Load LUTs into memory, (as they were stored on disk)
1124 uint8_t* lutR = (uint8_t*) GetEntryVoidAreaByNumber(0x0028,0x1201);
1125 uint8_t* lutG = (uint8_t*) GetEntryVoidAreaByNumber(0x0028,0x1202);
1126 uint8_t* lutB = (uint8_t*) GetEntryVoidAreaByNumber(0x0028,0x1203);
1128 if ( !lutR || !lutG || !lutB )
1130 dbg.Verbose(0, "gdcmHeader::GetLUTRGBA: trouble with one of the LUT");
1133 // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT
1135 uint8_t* LUTRGBA = new uint8_t[1024]; // 256 * 4 (R, G, B, Alpha)
1140 memset(LUTRGBA, 0, 1024);
1144 std::string str_nb = GetEntryByNumber(0x0028,0x0100);
1145 if ( str_nb == GDCM_UNFOUND )
1151 nb = atoi( str_nb.c_str() );
1155 if ( nbitsR == 16 && nb == 8)
1157 // when LUT item size is different than pixel size
1158 mult = 2; // high byte must be = low byte
1162 // See PS 3.3-2003 C.11.1.1.2 p 619
1166 // if we get a black image, let's just remove the '+1'
1167 // from 'i*mult+1' and check again
1168 // if it works, we shall have to check the 3 Palettes
1169 // to see which byte is ==0 (first one, or second one)
1171 // We give up the checking to avoid some (useless ?)overhead
1172 // (optimistic asumption)
1177 for( i=0; i < lengthR; ++i)
1179 *a = lutR[i*mult+1];
1184 for( i=0; i < lengthG; ++i)
1186 *a = lutG[i*mult+1];
1191 for(i=0; i < lengthB; ++i)
1193 *a = lutB[i*mult+1];
1198 for(i=0; i < 256; ++i)
1200 *a = 1; // Alpha component
1208 * \brief Accesses the info from 0002,0010 : Transfert Syntax and gdcmTS
1210 * @return The full Transfert Syntax Name (as opposed to Transfert Syntax UID)
1212 std::string gdcmHeader::GetTransfertSyntaxName()
1214 // use the gdcmTS (TS : Transfert Syntax)
1215 std::string transfertSyntax = GetEntryByNumber(0x0002,0x0010);
1216 if ( transfertSyntax == GDCM_UNFOUND )
1218 dbg.Verbose(0, "gdcmHeader::GetTransfertSyntaxName:"
1219 " unfound Transfert Syntax (0002,0010)");
1220 return "Uncompressed ACR-NEMA";
1223 // we do it only when we need it
1224 gdcmTS* ts = gdcmGlobal::GetTS();
1225 std::string tsName = ts->GetValue( transfertSyntax );
1227 //delete ts; /// \todo Seg Fault when deleted ?!
1232 * \brief Sets the Pixel Area size in the Header
1233 * --> not-for-rats function
1234 * @param ImageDataSize new Pixel Area Size
1235 * warning : nothing else is checked
1237 void gdcmHeader::SetImageDataSize(size_t ImageDataSize)
1239 std::string content1;
1242 sprintf(car,"%d",ImageDataSize);
1244 gdcmDocEntry *a = GetDocEntryByNumber(GrPixel, NumPixel);
1245 a->SetLength(ImageDataSize);
1248 sprintf(car,"%d",ImageDataSize);
1250 SetEntryByNumber(content1, GrPixel, NumPixel);
1253 //-----------------------------------------------------------------------------
1257 * \brief anonymize a Header (removes Patient's personal info)
1258 * (read the code to see which ones ...)
1260 bool gdcmHeader::AnonymizeHeader()
1262 gdcmDocEntry* patientNameHE = GetDocEntryByNumber (0x0010, 0x0010);
1264 ReplaceIfExistByNumber (" ",0x0010, 0x2154); // Telephone
1265 ReplaceIfExistByNumber (" ",0x0010, 0x1040); // Adress
1266 ReplaceIfExistByNumber (" ",0x0010, 0x0020); // Patient ID
1268 if ( patientNameHE )
1270 std::string studyInstanceUID = GetEntryByNumber (0x0020, 0x000d);
1271 if ( studyInstanceUID != GDCM_UNFOUND )
1273 ReplaceOrCreateByNumber(studyInstanceUID, 0x0010, 0x0010);
1277 ReplaceOrCreateByNumber(std::string("anonymised"), 0x0010, 0x0010);
1282 // (if any) remove or replace all the stuff that contains a Date
1284 //0008 0012 DA ID Instance Creation Date
1285 //0008 0020 DA ID Study Date
1286 //0008 0021 DA ID Series Date
1287 //0008 0022 DA ID Acquisition Date
1288 //0008 0023 DA ID Content Date
1289 //0008 0024 DA ID Overlay Date
1290 //0008 0025 DA ID Curve Date
1291 //0008 002a DT ID Acquisition Datetime
1292 //0018 9074 DT ACQ Frame Acquisition Datetime
1293 //0018 9151 DT ACQ Frame Reference Datetime
1294 //0018 a002 DT ACQ Contribution Date Time
1295 //0020 3403 SH REL Modified Image Date (RET)
1296 //0032 0032 DA SDY Study Verified Date
1297 //0032 0034 DA SDY Study Read Date
1298 //0032 1000 DA SDY Scheduled Study Start Date
1299 //0032 1010 DA SDY Scheduled Study Stop Date
1300 //0032 1040 DA SDY Study Arrival Date
1301 //0032 1050 DA SDY Study Completion Date
1302 //0038 001a DA VIS Scheduled Admission Date
1303 //0038 001c DA VIS Scheduled Discharge Date
1304 //0038 0020 DA VIS Admitting Date
1305 //0038 0030 DA VIS Discharge Date
1306 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1307 //0040 0004 DA PRC Scheduled Procedure Step End Date
1308 //0040 0244 DA PRC Performed Procedure Step Start Date
1309 //0040 0250 DA PRC Performed Procedure Step End Date
1310 //0040 2004 DA PRC Issue Date of Imaging Service Request
1311 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1312 //0040 4011 DT PRC Expected Completion Date and Time
1313 //0040 a030 DT PRC Verification Date Time
1314 //0040 a032 DT PRC Observation Date Time
1315 //0040 a120 DT PRC DateTime
1316 //0040 a121 DA PRC Date
1317 //0040 a13a DT PRC Referenced Datetime
1318 //0070 0082 DA ??? Presentation Creation Date
1319 //0100 0420 DT ??? SOP Autorization Date and Time
1320 //0400 0105 DT ??? Digital Signature DateTime
1321 //2100 0040 DA PJ Creation Date
1322 //3006 0008 DA SSET Structure Set Date
1323 //3008 0024 DA ??? Treatment Control Point Date
1324 //3008 0054 DA ??? First Treatment Date
1325 //3008 0056 DA ??? Most Recent Treatment Date
1326 //3008 0162 DA ??? Safe Position Exit Date
1327 //3008 0166 DA ??? Safe Position Return Date
1328 //3008 0250 DA ??? Treatment Date
1329 //300a 0006 DA RT RT Plan Date
1330 //300a 022c DA RT Air Kerma Rate Reference Date
1331 //300e 0004 DA RT Review Date
1337 * \brief gets the info from 0020,0037 : Image Orientation Patient
1338 * @param iop adress of the (6)float aray to receive values
1339 * @return cosines of image orientation patient
1341 void gdcmHeader::GetImageOrientationPatient( float* iop )
1343 //iop is supposed to be float[6]
1344 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0;
1346 // 0020 0037 DS REL Image Orientation (Patient)
1347 std::string strImOriPat = GetEntryByNumber(0x0020,0x0037);
1348 if ( strImOriPat != GDCM_UNFOUND )
1350 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1351 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1353 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0037)");
1354 return ; // bug in the element 0x0020,0x0037
1363 // 0020 0035 DS REL Image Orientation (RET)
1364 strImOriPat = GetEntryByNumber(0x0020,0x0035);
1365 if ( strImOriPat != GDCM_UNFOUND )
1367 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1368 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1370 dbg.Verbose(0, "gdcmHeader::GetImageOrientationPatient: wrong Image Orientation Patient (0020,0035)");
1371 return ; // bug in the element 0x0020,0x0035
1380 //-----------------------------------------------------------------------------
1383 //-----------------------------------------------------------------------------