1 /*=========================================================================
4 Module: $RCSfile: gdcmFile.cxx,v $
6 Date: $Date: 2005/01/21 12:37:15 $
7 Version: $Revision: 1.196 $
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.html 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 =========================================================================*/
20 #include "gdcmGlobal.h"
22 #include "gdcmDebug.h"
24 #include "gdcmValEntry.h"
25 #include "gdcmBinEntry.h"
26 #include <stdio.h> //sscanf
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
36 * @param filename name of the file whose header we want to analyze
38 File::File( std::string const &filename ):
41 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
42 // We may encounter the 'RETired' (0x0028, 0x0200) tag
43 // (Image Location") . This entry contains the number of
44 // the group that contains the pixel data (hence the "Pixel Data"
45 // is found by indirection through the "Image Location").
46 // Inside the group pointed by "Image Location" the searched element
47 // is conventionally the element 0x0010 (when the norm is respected).
48 // When the "Image Location" is Missing we default to group 0x7fe0.
49 // Note: this IS the right place for the code
52 const std::string &imgLocation = GetEntry(0x0028, 0x0200);
53 if ( imgLocation == GDCM_UNFOUND )
60 GrPixel = (uint16_t) atoi( imgLocation.c_str() );
63 // sometimes Image Location value doesn't follow
64 // the supposed processor endianness.
65 // see gdcmData/cr172241.dcm
66 if ( GrPixel == 0xe07f )
71 if ( GrPixel != 0x7fe0 )
73 // This is a kludge for old dirty Philips imager.
81 // Now, we know GrPixel and NumPixel.
82 // Let's create a VirtualDictEntry to allow a further VR modification
83 // and force VR to match with BitsAllocated.
84 DocEntry *entry = GetDocEntry(GrPixel, NumPixel);
89 // 8 bits allocated is a 'O Bytes' , as well as 24 (old ACR-NEMA RGB)
90 // more than 8 (i.e 12, 16) is a 'O Words'
91 if ( GetBitsAllocated() == 8 || GetBitsAllocated() == 24 )
96 DictEntry* newEntry = NewVirtualDictEntry(
98 PixelVR, "PXL", "Pixel Data");
100 entry->SetDictEntry( newEntry );
105 * \brief Constructor used when we want to generate dicom files from scratch
110 InitializeDefaultFile();
114 * \brief Canonical destructor.
121 * \brief Performs some consistency checking on various 'File related'
122 * (as opposed to 'DicomDir related') entries
123 * then writes in a file all the (Dicom Elements) included the Pixels
124 * @param fileName file name to write to
125 * @param filetype Type of the File to be written
126 * (ACR-NEMA, ExplicitVR, ImplicitVR)
128 bool File::Write(std::string fileName, FileType filetype)
130 std::ofstream *fp = new std::ofstream(fileName.c_str(),
131 std::ios::out | std::ios::binary);
134 gdcmVerboseMacro("Failed to open (write) File: " << fileName.c_str());
139 if ( GetEntry(0x0028,0x0100) == "12")
141 SetEntry("16", 0x0028,0x0100);
144 /// \todo correct 'Pixel group' Length if necessary
146 int i_lgPix = GetEntryLength(GrPixel, NumPixel);
149 // no (GrPixel, NumPixel) element
150 std::string s_lgPix = Util::Format("%d", i_lgPix+12);
151 s_lgPix = Util::DicomString( s_lgPix.c_str() );
152 ReplaceOrCreate(s_lgPix,GrPixel, 0x0000);
155 // FIXME : should be nice if we could move it to File
156 // (or in future gdcmPixelData class)
158 // Drop Palette Color, if necessary
160 if ( GetEntry(0x0028,0x0002).c_str()[0] == '3' )
162 // if SamplesPerPixel = 3, sure we don't need any LUT !
163 // Drop 0028|1101, 0028|1102, 0028|1103
164 // Drop 0028|1201, 0028|1202, 0028|1203
166 DocEntry *e = GetDocEntry(0x0028,0x01101);
169 RemoveEntryNoDestroy(e);
171 e = GetDocEntry(0x0028,0x1102);
174 RemoveEntryNoDestroy(e);
176 e = GetDocEntry(0x0028,0x1103);
179 RemoveEntryNoDestroy(e);
181 e = GetDocEntry(0x0028,0x01201);
184 RemoveEntryNoDestroy(e);
186 e = GetDocEntry(0x0028,0x1202);
189 RemoveEntryNoDestroy(e);
191 e = GetDocEntry(0x0028,0x1203);
194 RemoveEntryNoDestroy(e);
199 #ifdef GDCM_WORDS_BIGENDIAN
200 // Super Super hack that will make gdcm a BOMB ! but should
201 // Fix temporarily the dashboard
202 BinEntry *b = GetBinEntry(GrPixel,NumPixel);
203 if ( GetPixelSize() == 16 )
205 uint16_t *im16 = (uint16_t*)b->GetBinArea();
206 int lgr = b->GetLength();
207 for( int i = 0; i < lgr / 2; i++ )
209 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
212 #endif //GDCM_WORDS_BIGENDIAN
215 Document::WriteContent(fp,filetype);
218 #ifdef GDCM_WORDS_BIGENDIAN
219 // Flip back the pixel ... I told you this is a hack
220 if ( GetPixelSize() == 16 )
222 uint16_t *im16 = (uint16_t*)b->GetBinArea();
223 int lgr = b->GetLength();
224 for( int i = 0; i < lgr / 2; i++ )
226 im16[i]= (im16[i] >> 8) | (im16[i] << 8 );
229 #endif //GDCM_WORDS_BIGENDIAN
238 //-----------------------------------------------------------------------------
242 //-----------------------------------------------------------------------------
246 * \brief This predicate, based on hopefully reasonable heuristics,
247 * decides whether or not the current File was properly parsed
248 * and contains the mandatory information for being considered as
249 * a well formed and usable Dicom/Acr File.
250 * @return true when File is the one of a reasonable Dicom/Acr file,
253 bool File::IsReadable()
255 if( !Document::IsReadable() )
260 const std::string &res = GetEntry(0x0028, 0x0005);
261 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
263 return false; // Image Dimensions
265 if ( !GetDocEntry(0x0028, 0x0100) )
267 return false; // "Bits Allocated"
269 if ( !GetDocEntry(0x0028, 0x0101) )
271 return false; // "Bits Stored"
273 if ( !GetDocEntry(0x0028, 0x0102) )
275 return false; // "High Bit"
277 if ( !GetDocEntry(0x0028, 0x0103) )
279 return false; // "Pixel Representation" i.e. 'Sign'
286 * \brief Retrieve the number of columns of image.
287 * @return The encountered size when found, 0 by default.
288 * 0 means the file is NOT USABLE. The caller will have to check
292 const std::string &strSize = GetEntry(0x0028,0x0011);
293 if ( strSize == GDCM_UNFOUND )
298 return atoi( strSize.c_str() );
302 * \brief Retrieve the number of lines of image.
303 * \warning The defaulted value is 1 as opposed to File::GetXSize()
304 * @return The encountered size when found, 1 by default
305 * (The ACR-NEMA file contains a Signal, not an Image).
309 const std::string &strSize = GetEntry(0x0028,0x0010);
310 if ( strSize != GDCM_UNFOUND )
312 return atoi( strSize.c_str() );
319 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
320 // hence be a signal (1D image). So we default to 1:
325 * \brief Retrieve the number of planes of volume or the number
326 * of frames of a multiframe.
327 * \warning When present we consider the "Number of Frames" as the third
328 * dimension. When Missing we consider the third dimension as
329 * being the ACR-NEMA "Planes" tag content.
330 * @return The encountered size when found, 1 by default (single image).
334 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
335 // as the third dimension.
336 const std::string &strSize = GetEntry(0x0028,0x0008);
337 if ( strSize != GDCM_UNFOUND )
339 return atoi( strSize.c_str() );
342 // We then consider the "Planes" entry as the third dimension
343 const std::string &strSize2 = GetEntry(0x0028,0x0012);
344 if ( strSize2 != GDCM_UNFOUND )
346 return atoi( strSize2.c_str() );
353 * \brief gets the info from 0028,0030 : Pixel Spacing
355 * @return X dimension of a pixel
357 float File::GetXSpacing()
359 float xspacing, yspacing;
360 const std::string &strSpacing = GetEntry(0x0028,0x0030);
362 if ( strSpacing == GDCM_UNFOUND )
364 gdcmVerboseMacro( "Unfound Pixel Spacing (0028,0030)" );
369 if( ( nbValues = sscanf( strSpacing.c_str(),
370 "%f\\%f", &yspacing, &xspacing)) != 2 )
372 // if single value is found, xspacing is defaulted to yspacing
378 if ( xspacing == 0.0 )
385 // to avoid troubles with David Clunie's-like images
386 if ( xspacing == 0. && yspacing == 0.)
391 gdcmVerboseMacro("gdcmData/CT-MONO2-8-abdo.dcm problem");
392 // seems to be a bug in the header ...
393 nbValues = sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
394 gdcmAssertMacro( nbValues == 2 );
401 * \brief gets the info from 0028,0030 : Pixel Spacing
403 * @return Y dimension of a pixel
405 float File::GetYSpacing()
408 std::string strSpacing = GetEntry(0x0028,0x0030);
410 if ( strSpacing == GDCM_UNFOUND )
412 gdcmVerboseMacro("Unfound Pixel Spacing (0028,0030)");
416 // if sscanf cannot read any float value, it won't affect yspacing
417 sscanf( strSpacing.c_str(), "%f", &yspacing);
419 if ( yspacing == 0.0 )
426 * \brief gets the info from 0018,0088 : Space Between Slices
427 * else from 0018,0050 : Slice Thickness
429 * @return Z dimension of a voxel-to be
431 float File::GetZSpacing()
433 // Spacing Between Slices : distance entre le milieu de chaque coupe
434 // Les coupes peuvent etre :
435 // jointives (Spacing between Slices = Slice Thickness)
436 // chevauchantes (Spacing between Slices < Slice Thickness)
437 // disjointes (Spacing between Slices > Slice Thickness)
438 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
439 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
440 // Si le Spacing Between Slices est Missing,
441 // on suppose que les coupes sont jointives
443 const std::string &strSpacingBSlices = GetEntry(0x0018,0x0088);
445 if ( strSpacingBSlices == GDCM_UNFOUND )
447 gdcmVerboseMacro("Unfound Spacing Between Slices (0018,0088)");
448 const std::string &strSliceThickness = GetEntry(0x0018,0x0050);
449 if ( strSliceThickness == GDCM_UNFOUND )
451 gdcmVerboseMacro("Unfound Slice Thickness (0018,0050)");
456 // if no 'Spacing Between Slices' is found,
457 // we assume slices join together
458 // (no overlapping, no interslice gap)
459 // if they don't, we're fucked up
460 return (float)atof( strSliceThickness.c_str() );
464 return (float)atof( strSpacingBSlices.c_str() );
468 *\brief gets the info from 0028,1052 : Rescale Intercept
469 * @return Rescale Intercept
471 float File::GetRescaleIntercept()
474 /// 0028 1052 DS IMG Rescale Intercept
475 const std::string &strRescInter = GetEntry(0x0028,0x1052);
476 if ( strRescInter != GDCM_UNFOUND )
478 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
480 // bug in the element 0x0028,0x1052
481 gdcmVerboseMacro( "Rescale Intercept (0028,1052) is empty." );
489 *\brief gets the info from 0028,1053 : Rescale Slope
490 * @return Rescale Slope
492 float File::GetRescaleSlope()
495 //0028 1053 DS IMG Rescale Slope
496 std::string strRescSlope = GetEntry(0x0028,0x1053);
497 if ( strRescSlope != GDCM_UNFOUND )
499 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
501 // bug in the element 0x0028,0x1053
502 gdcmVerboseMacro( "Rescale Slope (0028,1053) is empty.");
510 * \brief This function is intended to user who doesn't want
511 * to have to manage a LUT and expects to get an RBG Pixel image
512 * (or a monochrome one ...)
513 * \warning to be used with GetImagePixels()
514 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
516 int File::GetNumberOfScalarComponents()
518 if ( GetSamplesPerPixel() == 3 )
523 // 0028 0100 US IMG Bits Allocated
524 // (in order no to be messed up by old RGB images)
525 if ( GetEntry(0x0028,0x0100) == "24" )
530 std::string strPhotometricInterpretation = GetEntry(0x0028,0x0004);
532 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
534 if ( HasLUT() )// PALETTE COLOR is NOT enough
544 // beware of trailing space at end of string
545 // DICOM tags are never of odd length
546 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
547 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
548 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
554 // we assume that *all* kinds of YBR are dealt with
560 * \brief This function is intended to user that DOESN'T want
561 * to get RGB pixels image when it's stored as a PALETTE COLOR image
562 * - the (vtk) user is supposed to know how deal with LUTs -
563 * \warning to be used with GetImagePixelsRaw()
564 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
566 int File::GetNumberOfScalarComponentsRaw()
568 // 0028 0100 US IMG Bits Allocated
569 // (in order no to be messed up by old RGB images)
570 if ( File::GetEntry(0x0028,0x0100) == "24" )
575 // we assume that *all* kinds of YBR are dealt with
576 return GetSamplesPerPixel();
580 // -------------- Remember ! ----------------------------------
582 // Image Position Patient (0020,0032):
583 // If not found (ACR_NEMA) we try Image Position (0020,0030)
584 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
585 // or Location (0020,0050)
586 // as the Z coordinate,
587 // 0. for all the coordinates if nothing is found
589 // \todo find a way to inform the caller nothing was found
590 // \todo How to tell the caller a wrong number of values was found?
592 // ---------------------------------------------------------------
596 * \brief gets the info from 0020,0032 : Image Position Patient
597 * else from 0020,0030 : Image Position (RET)
599 * @return up-left image corner X position
601 float File::GetXOrigin()
603 float xImPos, yImPos, zImPos;
604 std::string strImPos = GetEntry(0x0020,0x0032);
606 if ( strImPos == GDCM_UNFOUND )
608 gdcmVerboseMacro( "Unfound Image Position Patient (0020,0032)");
609 strImPos = GetEntry(0x0020,0x0030); // For ACR-NEMA images
610 if ( strImPos == GDCM_UNFOUND )
612 gdcmVerboseMacro( "Unfound Image Position (RET) (0020,0030)");
613 /// \todo How to tell the caller nothing was found ?
618 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
627 * \brief gets the info from 0020,0032 : Image Position Patient
628 * else from 0020,0030 : Image Position (RET)
630 * @return up-left image corner Y position
632 float File::GetYOrigin()
634 float xImPos, yImPos, zImPos;
635 std::string strImPos = GetEntry(0x0020,0x0032);
637 if ( strImPos == GDCM_UNFOUND)
639 gdcmVerboseMacro( "Unfound Image Position Patient (0020,0032)");
640 strImPos = GetEntry(0x0020,0x0030); // For ACR-NEMA images
641 if ( strImPos == GDCM_UNFOUND )
643 gdcmVerboseMacro( "Unfound Image Position (RET) (0020,0030)");
644 /// \todo How to tell the caller nothing was found ?
649 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
658 * \brief gets the info from 0020,0032 : Image Position Patient
659 * else from 0020,0030 : Image Position (RET)
660 * else from 0020,1041 : Slice Location
661 * else from 0020,0050 : Location
663 * @return up-left image corner Z position
665 float File::GetZOrigin()
667 float xImPos, yImPos, zImPos;
668 std::string strImPos = GetEntry(0x0020,0x0032);
670 if ( strImPos != GDCM_UNFOUND )
672 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
674 gdcmVerboseMacro( "Wrong Image Position Patient (0020,0032)");
675 return 0.; // bug in the element 0x0020,0x0032
683 strImPos = GetEntry(0x0020,0x0030); // For ACR-NEMA images
684 if ( strImPos != GDCM_UNFOUND )
686 if( sscanf( strImPos.c_str(),
687 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
689 gdcmVerboseMacro( "Wrong Image Position (RET) (0020,0030)");
690 return 0.; // bug in the element 0x0020,0x0032
698 std::string strSliceLocation = GetEntry(0x0020,0x1041); // for *very* old ACR-NEMA images
699 if ( strSliceLocation != GDCM_UNFOUND )
701 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
703 gdcmVerboseMacro( "Wrong Slice Location (0020,1041)");
704 return 0.; // bug in the element 0x0020,0x1041
711 gdcmVerboseMacro( "Unfound Slice Location (0020,1041)");
713 std::string strLocation = GetEntry(0x0020,0x0050);
714 if ( strLocation != GDCM_UNFOUND )
716 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
718 gdcmVerboseMacro( "Wrong Location (0020,0050)");
719 return 0.; // bug in the element 0x0020,0x0050
726 gdcmVerboseMacro( "Unfound Location (0020,0050)");
728 return 0.; // Hopeless
732 * \brief gets the info from 0020,0013 : Image Number else 0.
733 * @return image number
735 int File::GetImageNumber()
737 // The function i atoi() takes the address of an area of memory as
738 // parameter and converts the string stored at that location to an integer
739 // using the external decimal to internal binary conversion rules. This may
740 // be preferable to sscanf() since atoi() is a much smaller, simpler and
741 // faster function. sscanf() can do all possible conversions whereas
742 // atoi() can only do single decimal integer conversions.
743 //0020 0013 IS REL Image Number
744 std::string strImNumber = GetEntry(0x0020,0x0013);
745 if ( strImNumber != GDCM_UNFOUND )
747 return atoi( strImNumber.c_str() );
753 * \brief gets the info from 0008,0060 : Modality
754 * @return Modality Type
756 ModalityType File::GetModality()
758 // 0008 0060 CS ID Modality
759 std::string strModality = GetEntry(0x0008,0x0060);
760 if ( strModality != GDCM_UNFOUND )
762 if ( strModality.find("AU") < strModality.length()) return AU;
763 else if ( strModality.find("AS") < strModality.length()) return AS;
764 else if ( strModality.find("BI") < strModality.length()) return BI;
765 else if ( strModality.find("CF") < strModality.length()) return CF;
766 else if ( strModality.find("CP") < strModality.length()) return CP;
767 else if ( strModality.find("CR") < strModality.length()) return CR;
768 else if ( strModality.find("CT") < strModality.length()) return CT;
769 else if ( strModality.find("CS") < strModality.length()) return CS;
770 else if ( strModality.find("DD") < strModality.length()) return DD;
771 else if ( strModality.find("DF") < strModality.length()) return DF;
772 else if ( strModality.find("DG") < strModality.length()) return DG;
773 else if ( strModality.find("DM") < strModality.length()) return DM;
774 else if ( strModality.find("DS") < strModality.length()) return DS;
775 else if ( strModality.find("DX") < strModality.length()) return DX;
776 else if ( strModality.find("ECG") < strModality.length()) return ECG;
777 else if ( strModality.find("EPS") < strModality.length()) return EPS;
778 else if ( strModality.find("FA") < strModality.length()) return FA;
779 else if ( strModality.find("FS") < strModality.length()) return FS;
780 else if ( strModality.find("HC") < strModality.length()) return HC;
781 else if ( strModality.find("HD") < strModality.length()) return HD;
782 else if ( strModality.find("LP") < strModality.length()) return LP;
783 else if ( strModality.find("LS") < strModality.length()) return LS;
784 else if ( strModality.find("MA") < strModality.length()) return MA;
785 else if ( strModality.find("MR") < strModality.length()) return MR;
786 else if ( strModality.find("NM") < strModality.length()) return NM;
787 else if ( strModality.find("OT") < strModality.length()) return OT;
788 else if ( strModality.find("PT") < strModality.length()) return PT;
789 else if ( strModality.find("RF") < strModality.length()) return RF;
790 else if ( strModality.find("RG") < strModality.length()) return RG;
791 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
792 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
793 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
794 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
795 else if ( strModality.find("SM") < strModality.length()) return SM;
796 else if ( strModality.find("ST") < strModality.length()) return ST;
797 else if ( strModality.find("TG") < strModality.length()) return TG;
798 else if ( strModality.find("US") < strModality.length()) return US;
799 else if ( strModality.find("VF") < strModality.length()) return VF;
800 else if ( strModality.find("XA") < strModality.length()) return XA;
801 else if ( strModality.find("XC") < strModality.length()) return XC;
805 /// \todo throw error return value ???
806 /// specified <> unknown in our database
815 * \brief Retrieve the number of Bits Stored (actually used)
816 * (as opposite to number of Bits Allocated)
817 * @return The encountered number of Bits Stored, 0 by default.
818 * 0 means the file is NOT USABLE. The caller has to check it !
820 int File::GetBitsStored()
822 std::string strSize = GetEntry( 0x0028, 0x0101 );
823 if ( strSize == GDCM_UNFOUND )
825 gdcmVerboseMacro("(0028,0101) is supposed to be mandatory");
826 return 0; // It's supposed to be mandatory
827 // the caller will have to check
829 return atoi( strSize.c_str() );
833 * \brief Retrieve the high bit position.
834 * \warning The method defaults to 0 when information is Missing.
835 * The responsability of checking this value is left to the caller.
836 * @return The high bit positin when present. 0 when Missing.
838 int File::GetHighBitPosition()
840 std::string strSize = GetEntry( 0x0028, 0x0102 );
841 if ( strSize == GDCM_UNFOUND )
843 gdcmVerboseMacro( "(0028,0102) is supposed to be mandatory");
846 return atoi( strSize.c_str() );
850 * \brief Check whether the pixels are signed or UNsigned data.
851 * \warning The method defaults to false (UNsigned) when information is Missing.
852 * The responsability of checking this value is left to the caller.
853 * @return True when signed, false when UNsigned
855 bool File::IsSignedPixelData()
857 std::string strSize = GetEntry( 0x0028, 0x0103 );
858 if ( strSize == GDCM_UNFOUND )
860 gdcmVerboseMacro( "(0028,0103) is supposed to be mandatory");
863 int sign = atoi( strSize.c_str() );
872 * \brief Retrieve the number of Bits Allocated
873 * (8, 12 -compacted ACR-NEMA files, 16, ...)
874 * @return The encountered number of Bits Allocated, 0 by default.
875 * 0 means the file is NOT USABLE. The caller has to check it !
877 int File::GetBitsAllocated()
879 std::string strSize = GetEntry(0x0028,0x0100);
880 if ( strSize == GDCM_UNFOUND )
882 gdcmVerboseMacro( "(0028,0100) is supposed to be mandatory");
883 return 0; // It's supposed to be mandatory
884 // the caller will have to check
886 return atoi( strSize.c_str() );
890 * \brief Retrieve the number of Samples Per Pixel
891 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
892 * @return The encountered number of Samples Per Pixel, 1 by default.
893 * (Gray level Pixels)
895 int File::GetSamplesPerPixel()
897 const std::string& strSize = GetEntry(0x0028,0x0002);
898 if ( strSize == GDCM_UNFOUND )
900 gdcmVerboseMacro( "(0028,0002) is supposed to be mandatory");
901 return 1; // Well, it's supposed to be mandatory ...
902 // but sometimes it's missing : *we* assume Gray pixels
904 return atoi( strSize.c_str() );
908 * \brief Check whether this a monochrome picture or not by accessing
909 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
910 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
912 bool File::IsMonochrome()
914 const std::string& PhotometricInterp = GetEntry( 0x0028, 0x0004 );
915 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
916 || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
920 if ( PhotometricInterp == GDCM_UNFOUND )
922 gdcmVerboseMacro( "Not found : Photometric Interpretation (0028,0004)");
928 * \brief Check whether this a "PALETTE COLOR" picture or not by accessing
929 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
930 * @return true when "PALETTE COLOR". False otherwise.
932 bool File::IsPaletteColor()
934 std::string PhotometricInterp = GetEntry( 0x0028, 0x0004 );
935 if ( PhotometricInterp == "PALETTE COLOR " )
939 if ( PhotometricInterp == GDCM_UNFOUND )
941 gdcmVerboseMacro( "Not found : Palette color (0028,0004)");
947 * \brief Check whether this a "YBR_FULL" color picture or not by accessing
948 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
949 * @return true when "YBR_FULL". False otherwise.
951 bool File::IsYBRFull()
953 std::string PhotometricInterp = GetEntry( 0x0028, 0x0004 );
954 if ( PhotometricInterp == "YBR_FULL" )
958 if ( PhotometricInterp == GDCM_UNFOUND )
960 gdcmVerboseMacro( "Not found : YBR Full (0028,0004)");
966 * \brief Retrieve the Planar Configuration for RGB images
967 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
968 * @return The encountered Planar Configuration, 0 by default.
970 int File::GetPlanarConfiguration()
972 std::string strSize = GetEntry(0x0028,0x0006);
973 if ( strSize == GDCM_UNFOUND )
975 gdcmVerboseMacro( "Not found : Planar Configuration (0028,0006)");
978 return atoi( strSize.c_str() );
982 * \brief Return the size (in bytes) of a single pixel of data.
983 * @return The size in bytes of a single pixel of data; 0 by default
984 * 0 means the file is NOT USABLE; the caller will have to check
986 int File::GetPixelSize()
988 // 0028 0100 US IMG Bits Allocated
989 // (in order no to be messed up by old RGB images)
990 // if (File::GetEntry(0x0028,0x0100) == "24")
993 std::string pixelType = GetPixelType();
994 if ( pixelType == "8U" || pixelType == "8S" )
998 if ( pixelType == "16U" || pixelType == "16S")
1002 if ( pixelType == "32U" || pixelType == "32S")
1006 if ( pixelType == "FD" )
1010 gdcmVerboseMacro( "Unknown pixel type");
1015 * \brief Build the Pixel Type of the image.
1016 * Possible values are:
1017 * - 8U unsigned 8 bit,
1018 * - 8S signed 8 bit,
1019 * - 16U unsigned 16 bit,
1020 * - 16S signed 16 bit,
1021 * - 32U unsigned 32 bit,
1022 * - 32S signed 32 bit,
1023 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
1024 * \warning 12 bit images appear as 16 bit.
1025 * 24 bit images appear as 8 bit
1026 * @return 0S if nothing found. NOT USABLE file. The caller has to check
1028 std::string File::GetPixelType()
1030 std::string bitsAlloc = GetEntry(0x0028, 0x0100); // Bits Allocated
1031 if ( bitsAlloc == GDCM_UNFOUND )
1033 gdcmVerboseMacro( "Missing Bits Allocated (0028,0100)");
1034 bitsAlloc = "16"; // default and arbitrary value, not to polute the output
1037 if ( bitsAlloc == "64" )
1041 else if ( bitsAlloc == "12" )
1043 // It will be unpacked
1046 else if ( bitsAlloc == "24" )
1048 // (in order no to be messed up
1049 bitsAlloc = "8"; // by old RGB images)
1052 std::string sign = GetEntry(0x0028, 0x0103);//"Pixel Representation"
1054 if (sign == GDCM_UNFOUND )
1056 gdcmVerboseMacro( "Missing Pixel Representation (0028,0103)");
1057 sign = "U"; // default and arbitrary value, not to polute the output
1059 else if ( sign == "0" )
1067 return bitsAlloc + sign;
1072 * \brief Recover the offset (from the beginning of the file)
1073 * of *image* pixels (not *icone image* pixels, if any !)
1074 * @return Pixel Offset
1076 size_t File::GetPixelOffset()
1078 DocEntry* pxlElement = GetDocEntry(GrPixel,NumPixel);
1081 return pxlElement->GetOffset();
1086 std::cout << "Big trouble : Pixel Element ("
1087 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1094 /// \todo TODO : unify those two (previous one and next one)
1096 * \brief Recover the pixel area length (in Bytes)
1097 * @return Pixel Element Length, as stored in the header
1098 * (NOT the memory space necessary to hold the Pixels
1099 * -in case of embeded compressed image-)
1100 * 0 : NOT USABLE file. The caller has to check.
1102 size_t File::GetPixelAreaLength()
1104 DocEntry* pxlElement = GetDocEntry(GrPixel,NumPixel);
1107 return pxlElement->GetLength();
1112 std::cout << "Big trouble : Pixel Element ("
1113 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found"
1121 * \brief tells us if LUT are used
1122 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1123 * are NOT considered as LUT, since nobody knows
1124 * how to deal with them
1125 * Please warn me if you know sbdy that *does* know ... jprx
1126 * @return true if LUT Descriptors and LUT Tables were found
1130 // Check the presence of the LUT Descriptors, and LUT Tables
1132 if ( !GetDocEntry(0x0028,0x1101) )
1136 // LutDescriptorGreen
1137 if ( !GetDocEntry(0x0028,0x1102) )
1141 // LutDescriptorBlue
1142 if ( !GetDocEntry(0x0028,0x1103) )
1146 // Red Palette Color Lookup Table Data
1147 if ( !GetDocEntry(0x0028,0x1201) )
1151 // Green Palette Color Lookup Table Data
1152 if ( !GetDocEntry(0x0028,0x1202) )
1156 // Blue Palette Color Lookup Table Data
1157 if ( !GetDocEntry(0x0028,0x1203) )
1162 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1163 // NOT taken into account, but we don't know how to use it ...
1168 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1170 * @return Lookup Table number of Bits , 0 by default
1171 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1172 * @ return bit number of each LUT item
1174 int File::GetLUTNbits()
1176 std::vector<std::string> tokens;
1179 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1180 // = Lookup Table Desc-Blue
1181 // Consistency already checked in GetLUTLength
1182 std::string lutDescription = GetEntry(0x0028,0x1101);
1183 if ( lutDescription == GDCM_UNFOUND )
1188 tokens.clear(); // clean any previous value
1189 Util::Tokenize ( lutDescription, tokens, "\\" );
1190 //LutLength=atoi(tokens[0].c_str());
1191 //LutDepth=atoi(tokens[1].c_str());
1193 lutNbits = atoi( tokens[2].c_str() );
1200 //-----------------------------------------------------------------------------
1204 * \brief anonymize a File (removes Patient's personal info)
1205 * (read the code to see which ones ...)
1207 bool File::AnonymizeFile()
1209 // If exist, replace by spaces
1210 SetEntry (" ",0x0010, 0x2154); // Telephone
1211 SetEntry (" ",0x0010, 0x1040); // Adress
1212 SetEntry (" ",0x0010, 0x0020); // Patient ID
1214 DocEntry* patientNameHE = GetDocEntry (0x0010, 0x0010);
1216 if ( patientNameHE ) // we replace it by Study Instance UID (why not)
1218 std::string studyInstanceUID = GetEntry (0x0020, 0x000d);
1219 if ( studyInstanceUID != GDCM_UNFOUND )
1221 ReplaceOrCreate(studyInstanceUID, 0x0010, 0x0010);
1225 ReplaceOrCreate("anonymised", 0x0010, 0x0010);
1230 // (if any) remove or replace all the stuff that contains a Date
1232 //0008 0012 DA ID Instance Creation Date
1233 //0008 0020 DA ID Study Date
1234 //0008 0021 DA ID Series Date
1235 //0008 0022 DA ID Acquisition Date
1236 //0008 0023 DA ID Content Date
1237 //0008 0024 DA ID Overlay Date
1238 //0008 0025 DA ID Curve Date
1239 //0008 002a DT ID Acquisition Datetime
1240 //0018 9074 DT ACQ Frame Acquisition Datetime
1241 //0018 9151 DT ACQ Frame Reference Datetime
1242 //0018 a002 DT ACQ Contribution Date Time
1243 //0020 3403 SH REL Modified Image Date (RET)
1244 //0032 0032 DA SDY Study Verified Date
1245 //0032 0034 DA SDY Study Read Date
1246 //0032 1000 DA SDY Scheduled Study Start Date
1247 //0032 1010 DA SDY Scheduled Study Stop Date
1248 //0032 1040 DA SDY Study Arrival Date
1249 //0032 1050 DA SDY Study Completion Date
1250 //0038 001a DA VIS Scheduled Admission Date
1251 //0038 001c DA VIS Scheduled Discharge Date
1252 //0038 0020 DA VIS Admitting Date
1253 //0038 0030 DA VIS Discharge Date
1254 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1255 //0040 0004 DA PRC Scheduled Procedure Step End Date
1256 //0040 0244 DA PRC Performed Procedure Step Start Date
1257 //0040 0250 DA PRC Performed Procedure Step End Date
1258 //0040 2004 DA PRC Issue Date of Imaging Service Request
1259 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1260 //0040 4011 DT PRC Expected Completion Date and Time
1261 //0040 a030 DT PRC Verification Date Time
1262 //0040 a032 DT PRC Observation Date Time
1263 //0040 a120 DT PRC DateTime
1264 //0040 a121 DA PRC Date
1265 //0040 a13a DT PRC Referenced Datetime
1266 //0070 0082 DA ??? Presentation Creation Date
1267 //0100 0420 DT ??? SOP Autorization Date and Time
1268 //0400 0105 DT ??? Digital Signature DateTime
1269 //2100 0040 DA PJ Creation Date
1270 //3006 0008 DA SSET Structure Set Date
1271 //3008 0024 DA ??? Treatment Control Point Date
1272 //3008 0054 DA ??? First Treatment Date
1273 //3008 0056 DA ??? Most Recent Treatment Date
1274 //3008 0162 DA ??? Safe Position Exit Date
1275 //3008 0166 DA ??? Safe Position Return Date
1276 //3008 0250 DA ??? Treatment Date
1277 //300a 0006 DA RT RT Plan Date
1278 //300a 022c DA RT Air Kerma Rate Reference Date
1279 //300e 0004 DA RT Review Date
1285 * \brief gets the info from 0020,0037 : Image Orientation Patient
1286 * @param iop adress of the (6)float aray to receive values
1287 * @return cosines of image orientation patient
1289 void File::GetImageOrientationPatient( float iop[6] )
1291 std::string strImOriPat;
1292 //iop is supposed to be float[6]
1293 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
1295 // 0020 0037 DS REL Image Orientation (Patient)
1296 if ( (strImOriPat = GetEntry(0x0020,0x0037)) != GDCM_UNFOUND )
1298 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1299 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1301 gdcmVerboseMacro( "Wrong Image Orientation Patient (0020,0037). Less than 6 values were found." );
1305 // 0020 0035 DS REL Image Orientation (RET)
1306 else if ( (strImOriPat = GetEntry(0x0020,0x0035)) != GDCM_UNFOUND )
1308 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
1309 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
1311 gdcmVerboseMacro( "wrong Image Orientation Patient (0020,0035). Less than 6 values were found." );
1317 * \brief Initialize a default DICOM File that should contain all the
1318 * field require by other reader. DICOM standard does not
1319 * explicitely defines those fields, heuristic has been choosen.
1320 * This is not perfect as we are writting a CT image...
1322 void File::InitializeDefaultFile()
1329 } DICOM_DEFAULT_VALUE;
1331 std::string date = Util::GetCurrentDate();
1332 std::string time = Util::GetCurrentTime();
1333 std::string uid = Util::CreateUniqueUID();
1334 std::string uidMedia = uid;
1335 std::string uidClass = uid + ".1";
1336 std::string uidInst = uid + ".10";
1337 std::string uidStudy = uid + ".100";
1338 std::string uidSerie = uid + ".1000";
1340 static DICOM_DEFAULT_VALUE defaultvalue[] = {
1341 { "146 ", 0x0002, 0x0000}, // Meta Element Group Length // FIXME: how to recompute ?
1342 { "1.2.840.10008.5.1.4.1.1.2", 0x0002, 0x0002}, // Media Storage SOP Class UID (CT Image Storage)
1343 { uidClass.c_str(), 0x0002, 0x0003}, // Media Storage SOP Instance UID
1344 { "1.2.840.10008.1.2.1 ", 0x0002, 0x0010}, // Transfer Syntax UID (Explicit VR Little Endian)
1345 { uidClass.c_str(), 0x0002, 0x0012}, // META Implementation Class UID
1346 { "GDCM", 0x0002, 0x0016}, // Source Application Entity Title
1348 { date.c_str(), 0x0008, 0x0012}, // Instance Creation Date
1349 { time.c_str(), 0x0008, 0x0013}, // Instance Creation Time
1350 { "1.2.840.10008.5.1.4.1.1.2", 0x0008, 0x0016}, // SOP Class UID
1351 { uidInst.c_str(), 0x0008, 0x0018}, // SOP Instance UID
1352 { "CT", 0x0008, 0x0060}, // Modality
1353 { "GDCM", 0x0008, 0x0070}, // Manufacturer
1354 { "GDCM", 0x0008, 0x0080}, // Institution Name
1355 { "http://www-creatis.insa-lyon.fr/Public/Gdcm", 0x0008, 0x0081}, // Institution Address
1357 { "GDCM", 0x0010, 0x0010}, // Patient's Name
1358 { "GDCMID", 0x0010, 0x0020}, // Patient ID
1360 { uidStudy.c_str(), 0x0020, 0x000d}, // Study Instance UID
1361 { uidSerie.c_str(), 0x0020, 0x000e}, // Series Instance UID
1362 { "1", 0x0020, 0x0010}, // StudyID
1363 { "1", 0x0020, 0x0011}, // SeriesNumber
1365 { "1", 0x0028, 0x0002}, // Samples per pixel 1 or 3
1366 { "MONOCHROME1", 0x0028, 0x0004}, // photochromatic interpretation
1367 { "0", 0x0028, 0x0010}, // nbRows
1368 { "0", 0x0028, 0x0011}, // nbCols
1369 { "8", 0x0028, 0x0100}, // BitsAllocated 8 or 12 or 16
1370 { "8", 0x0028, 0x0101}, // BitsStored <= BitsAllocated
1371 { "7", 0x0028, 0x0102}, // HighBit <= BitsAllocated - 1
1372 { "0", 0x0028, 0x0103}, // Pixel Representation 0(unsigned) or 1(signed)
1377 // Special case this is the image (not a string)
1380 ReplaceOrCreate(0, 0, GrPixel, NumPixel);
1382 // All remaining strings:
1384 DICOM_DEFAULT_VALUE current = defaultvalue[i];
1385 while( current.value )
1387 ReplaceOrCreate(current.value, current.group, current.elem);
1388 current = defaultvalue[++i];
1393 //-----------------------------------------------------------------------------
1396 //-----------------------------------------------------------------------------
1398 } // end namespace gdcm