1 /*=========================================================================
4 Module: $RCSfile: gdcmFile.cxx,v $
6 Date: $Date: 2005/02/21 17:47:19 $
7 Version: $Revision: 1.227 $
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 // -------------- Remember ! ----------------------------------
22 // Image Position Patient (0020,0032):
23 // If not found (ACR_NEMA) we try Image Position (0020,0030)
24 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
25 // or Location (0020,0050)
26 // as the Z coordinate,
27 // 0. for all the coordinates if nothing is found
29 // ---------------------------------------------------------------
32 #include "gdcmGlobal.h"
34 #include "gdcmDebug.h"
36 #include "gdcmValEntry.h"
37 #include "gdcmBinEntry.h"
38 #include "gdcmSeqEntry.h"
39 #include "gdcmRLEFramesInfo.h"
40 #include "gdcmJPEGFragmentsInfo.h"
42 #include <stdio.h> //sscanf
47 //-----------------------------------------------------------------------------
48 // Constructor / Destructor
51 * @param filename name of the file whose header we want to analyze
53 File::File( std::string const &filename )
56 RLEInfo = new RLEFramesInfo;
57 JPEGInfo = new JPEGFragmentsInfo;
59 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
60 // We may encounter the 'RETired' (0x0028, 0x0200) tag
61 // (Image Location") . This entry contains the number of
62 // the group that contains the pixel data (hence the "Pixel Data"
63 // is found by indirection through the "Image Location").
64 // Inside the group pointed by "Image Location" the searched element
65 // is conventionally the element 0x0010 (when the norm is respected).
66 // When the "Image Location" is missing we default to group 0x7fe0.
67 // Note: this IS the right place for the code
70 const std::string &imgLocation = GetEntryValue(0x0028, 0x0200);
71 if ( imgLocation == GDCM_UNFOUND )
78 GrPixel = (uint16_t) atoi( imgLocation.c_str() );
81 // sometimes Image Location value doesn't follow
82 // the supposed processor endianness.
83 // see gdcmData/cr172241.dcm
84 if ( GrPixel == 0xe07f )
89 if ( GrPixel != 0x7fe0 )
91 // This is a kludge for old dirty Philips imager.
99 // Now, we know GrPixel and NumPixel.
100 // Let's create a VirtualDictEntry to allow a further VR modification
101 // and force VR to match with BitsAllocated.
102 DocEntry *entry = GetDocEntry(GrPixel, NumPixel);
105 // Compute the RLE or JPEG info
107 std::string ts = GetTransferSyntax();
108 Fp->seekg( entry->GetOffset(), std::ios::beg );
109 if ( Global::GetTS()->IsRLELossless(ts) )
111 else if ( Global::GetTS()->IsJPEG(ts) )
112 ComputeJPEGFragmentInfo();
115 // Create a new BinEntry to change the the DictEntry
116 // The changed DictEntry will have
117 // - a correct PixelVR OB or OW)
118 // - the name to "Pixel Data"
119 BinEntry *oldEntry = dynamic_cast<BinEntry *>(entry);
123 // 8 bits allocated is a 'O Bytes' , as well as 24 (old ACR-NEMA RGB)
124 // more than 8 (i.e 12, 16) is a 'O Words'
125 if ( GetBitsAllocated() == 8 || GetBitsAllocated() == 24 )
130 // Change only made if usefull
131 if( PixelVR != oldEntry->GetVR() )
133 DictEntry* newDict = NewVirtualDictEntry(GrPixel,NumPixel,
134 PixelVR,"1","Pixel Data");
136 BinEntry *newEntry = new BinEntry(newDict);
137 newEntry->Copy(entry);
138 newEntry->SetBinArea(oldEntry->GetBinArea(),oldEntry->IsSelfArea());
139 oldEntry->SetSelfArea(false);
141 RemoveEntry(oldEntry);
149 * \brief Constructor used when we want to generate dicom files from scratch
154 RLEInfo = new RLEFramesInfo;
155 JPEGInfo = new JPEGFragmentsInfo;
156 InitializeDefaultFile();
160 * \brief Canonical destructor.
170 //-----------------------------------------------------------------------------
173 * \brief This predicate, based on hopefully reasonable heuristics,
174 * decides whether or not the current File was properly parsed
175 * and contains the mandatory information for being considered as
176 * a well formed and usable Dicom/Acr File.
177 * @return true when File is the one of a reasonable Dicom/Acr file,
180 bool File::IsReadable()
182 if( !Document::IsReadable() )
187 const std::string &res = GetEntryValue(0x0028, 0x0005);
188 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
190 return false; // Image Dimensions
192 if ( !GetDocEntry(0x0028, 0x0100) )
194 return false; // "Bits Allocated"
196 if ( !GetDocEntry(0x0028, 0x0101) )
198 return false; // "Bits Stored"
200 if ( !GetDocEntry(0x0028, 0x0102) )
202 return false; // "High Bit"
204 if ( !GetDocEntry(0x0028, 0x0103) )
206 return false; // "Pixel Representation" i.e. 'Sign'
213 * \brief gets the info from 0020,0013 : Image Number else 0.
214 * @return image number
216 int File::GetImageNumber()
218 //0020 0013 : Image Number
219 std::string strImNumber = GetEntryValue(0x0020,0x0013);
220 if ( strImNumber != GDCM_UNFOUND )
222 return atoi( strImNumber.c_str() );
228 * \brief gets the info from 0008,0060 : Modality
229 * @return Modality Type
231 ModalityType File::GetModality()
233 // 0008 0060 : Modality
234 std::string strModality = GetEntryValue(0x0008,0x0060);
235 if ( strModality != GDCM_UNFOUND )
237 if ( strModality.find("AU") < strModality.length()) return AU;
238 else if ( strModality.find("AS") < strModality.length()) return AS;
239 else if ( strModality.find("BI") < strModality.length()) return BI;
240 else if ( strModality.find("CF") < strModality.length()) return CF;
241 else if ( strModality.find("CP") < strModality.length()) return CP;
242 else if ( strModality.find("CR") < strModality.length()) return CR;
243 else if ( strModality.find("CT") < strModality.length()) return CT;
244 else if ( strModality.find("CS") < strModality.length()) return CS;
245 else if ( strModality.find("DD") < strModality.length()) return DD;
246 else if ( strModality.find("DF") < strModality.length()) return DF;
247 else if ( strModality.find("DG") < strModality.length()) return DG;
248 else if ( strModality.find("DM") < strModality.length()) return DM;
249 else if ( strModality.find("DS") < strModality.length()) return DS;
250 else if ( strModality.find("DX") < strModality.length()) return DX;
251 else if ( strModality.find("ECG") < strModality.length()) return ECG;
252 else if ( strModality.find("EPS") < strModality.length()) return EPS;
253 else if ( strModality.find("FA") < strModality.length()) return FA;
254 else if ( strModality.find("FS") < strModality.length()) return FS;
255 else if ( strModality.find("HC") < strModality.length()) return HC;
256 else if ( strModality.find("HD") < strModality.length()) return HD;
257 else if ( strModality.find("LP") < strModality.length()) return LP;
258 else if ( strModality.find("LS") < strModality.length()) return LS;
259 else if ( strModality.find("MA") < strModality.length()) return MA;
260 else if ( strModality.find("MR") < strModality.length()) return MR;
261 else if ( strModality.find("NM") < strModality.length()) return NM;
262 else if ( strModality.find("OT") < strModality.length()) return OT;
263 else if ( strModality.find("PT") < strModality.length()) return PT;
264 else if ( strModality.find("RF") < strModality.length()) return RF;
265 else if ( strModality.find("RG") < strModality.length()) return RG;
266 else if ( strModality.find("RTDOSE") < strModality.length()) return RTDOSE;
267 else if ( strModality.find("RTIMAGE") < strModality.length()) return RTIMAGE;
268 else if ( strModality.find("RTPLAN") < strModality.length()) return RTPLAN;
269 else if ( strModality.find("RTSTRUCT") < strModality.length()) return RTSTRUCT;
270 else if ( strModality.find("SM") < strModality.length()) return SM;
271 else if ( strModality.find("ST") < strModality.length()) return ST;
272 else if ( strModality.find("TG") < strModality.length()) return TG;
273 else if ( strModality.find("US") < strModality.length()) return US;
274 else if ( strModality.find("VF") < strModality.length()) return VF;
275 else if ( strModality.find("XA") < strModality.length()) return XA;
276 else if ( strModality.find("XC") < strModality.length()) return XC;
280 /// \todo throw error return value ???
281 /// specified <> unknown in our database
290 * \brief Retrieve the number of columns of image.
291 * @return The encountered size when found, 0 by default.
292 * 0 means the file is NOT USABLE. The caller will have to check
296 const std::string &strSize = GetEntryValue(0x0028,0x0011);
297 if ( strSize == GDCM_UNFOUND )
302 return atoi( strSize.c_str() );
306 * \brief Retrieve the number of lines of image.
307 * \warning The defaulted value is 1 as opposed to File::GetXSize()
308 * @return The encountered size when found, 1 by default
309 * (The ACR-NEMA file contains a Signal, not an Image).
313 const std::string &strSize = GetEntryValue(0x0028,0x0010);
314 if ( strSize != GDCM_UNFOUND )
316 return atoi( strSize.c_str() );
323 // The Rows (0028,0010) entry was optional for ACR/NEMA. It might
324 // hence be a signal (1D image). So we default to 1:
329 * \brief Retrieve the number of planes of volume or the number
330 * of frames of a multiframe.
331 * \warning When present we consider the "Number of Frames" as the third
332 * dimension. When Missing we consider the third dimension as
333 * being the ACR-NEMA "Planes" tag content.
334 * @return The encountered size when found, 1 by default (single image).
338 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
339 // as the third dimension.
340 const std::string &strSize = GetEntryValue(0x0028,0x0008);
341 if ( strSize != GDCM_UNFOUND )
343 return atoi( strSize.c_str() );
346 // We then consider the "Planes" entry as the third dimension
347 const std::string &strSize2 = GetEntryValue(0x0028,0x0012);
348 if ( strSize2 != GDCM_UNFOUND )
350 return atoi( strSize2.c_str() );
357 * \brief gets the info from 0028,0030 : Pixel Spacing
359 * @return X dimension of a pixel
361 float File::GetXSpacing()
363 float xspacing = 1.0;
364 float yspacing = 1.0;
365 const std::string &strSpacing = GetEntryValue(0x0028,0x0030);
367 if( strSpacing == GDCM_UNFOUND )
369 gdcmWarningMacro( "Unfound Pixel Spacing (0028,0030)" );
374 if( ( nbValues = sscanf( strSpacing.c_str(),
375 "%f\\%f", &yspacing, &xspacing)) != 2 )
377 // if no values, xspacing is set to 1.0
380 // if single value is found, xspacing is defaulted to yspacing
384 if ( xspacing == 0.0 )
391 // to avoid troubles with David Clunie's-like images
392 if ( xspacing == 0. && yspacing == 0.)
397 gdcmWarningMacro("gdcmData/CT-MONO2-8-abdo.dcm-like problem");
398 // seems to be a bug in the header ...
399 nbValues = sscanf( strSpacing.c_str(), "%f\\0\\%f", &yspacing, &xspacing);
400 gdcmAssertMacro( nbValues == 2 );
407 * \brief gets the info from 0028,0030 : Pixel Spacing
409 * @return Y dimension of a pixel
411 float File::GetYSpacing()
414 std::string strSpacing = GetEntryValue(0x0028,0x0030);
416 if ( strSpacing == GDCM_UNFOUND )
418 gdcmWarningMacro("Unfound Pixel Spacing (0028,0030)");
422 // if sscanf cannot read any float value, it won't affect yspacing
423 int nbValues = sscanf( strSpacing.c_str(), "%f", &yspacing);
425 // if no values, xspacing is set to 1.0
429 if ( yspacing == 0.0 )
436 * \brief gets the info from 0018,0088 : Space Between Slices
437 * else from 0018,0050 : Slice Thickness
439 * @return Z dimension of a voxel-to be
441 float File::GetZSpacing()
443 // Spacing Between Slices : distance between the middle of 2 slices
445 // jointives (Spacing between Slices = Slice Thickness)
446 // overlapping (Spacing between Slices < Slice Thickness)
447 // disjointes (Spacing between Slices > Slice Thickness)
448 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
449 // It only concerns the MRI guys, not people wanting to visualize volmues
450 // If Spacing Between Slices is Missing,
451 // we suppose slices joint together
453 const std::string &strSpacingBSlices = GetEntryValue(0x0018,0x0088);
455 if ( strSpacingBSlices == GDCM_UNFOUND )
457 gdcmWarningMacro("Unfound Spacing Between Slices (0018,0088)");
458 const std::string &strSliceThickness = GetEntryValue(0x0018,0x0050);
459 if ( strSliceThickness == GDCM_UNFOUND )
461 gdcmWarningMacro("Unfound Slice Thickness (0018,0050)");
466 // if no 'Spacing Between Slices' is found,
467 // we assume slices join together
468 // (no overlapping, no interslice gap)
469 // if they don't, we're fucked up
470 return (float)atof( strSliceThickness.c_str() );
474 return (float)atof( strSpacingBSlices.c_str() );
478 * \brief gets the info from 0020,0032 : Image Position Patient
479 * else from 0020,0030 : Image Position (RET)
481 * @return up-left image corner X position
483 float File::GetXOrigin()
485 float xImPos, yImPos, zImPos;
486 std::string strImPos = GetEntryValue(0x0020,0x0032);
488 if ( strImPos == GDCM_UNFOUND )
490 gdcmWarningMacro( "Unfound Image Position Patient (0020,0032)");
491 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
492 if ( strImPos == GDCM_UNFOUND )
494 gdcmWarningMacro( "Unfound Image Position (RET) (0020,0030)");
499 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
508 * \brief gets the info from 0020,0032 : Image Position Patient
509 * else from 0020,0030 : Image Position (RET)
511 * @return up-left image corner Y position
513 float File::GetYOrigin()
515 float xImPos, yImPos, zImPos;
516 std::string strImPos = GetEntryValue(0x0020,0x0032);
518 if ( strImPos == GDCM_UNFOUND)
520 gdcmWarningMacro( "Unfound Image Position Patient (0020,0032)");
521 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
522 if ( strImPos == GDCM_UNFOUND )
524 gdcmWarningMacro( "Unfound Image Position (RET) (0020,0030)");
529 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3 )
538 * \brief gets the info from 0020,0032 : Image Position Patient
539 * else from 0020,0030 : Image Position (RET)
540 * else from 0020,1041 : Slice Location
541 * else from 0020,0050 : Location
543 * @return up-left image corner Z position
545 float File::GetZOrigin()
547 float xImPos, yImPos, zImPos;
548 std::string strImPos = GetEntryValue(0x0020,0x0032);
550 if ( strImPos != GDCM_UNFOUND )
552 if( sscanf( strImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
554 gdcmWarningMacro( "Wrong Image Position Patient (0020,0032)");
555 return 0.; // bug in the element 0x0020,0x0032
563 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
564 if ( strImPos != GDCM_UNFOUND )
566 if( sscanf( strImPos.c_str(),
567 "%f\\%f\\%f", &xImPos, &yImPos, &zImPos ) != 3 )
569 gdcmWarningMacro( "Wrong Image Position (RET) (0020,0030)");
570 return 0.; // bug in the element 0x0020,0x0032
578 std::string strSliceLocation = GetEntryValue(0x0020,0x1041); // for *very* old ACR-NEMA images
579 if ( strSliceLocation != GDCM_UNFOUND )
581 if( sscanf( strSliceLocation.c_str(), "%f", &zImPos) != 1)
583 gdcmWarningMacro( "Wrong Slice Location (0020,1041)");
584 return 0.; // bug in the element 0x0020,0x1041
591 gdcmWarningMacro( "Unfound Slice Location (0020,1041)");
593 std::string strLocation = GetEntryValue(0x0020,0x0050);
594 if ( strLocation != GDCM_UNFOUND )
596 if( sscanf( strLocation.c_str(), "%f", &zImPos) != 1)
598 gdcmWarningMacro( "Wrong Location (0020,0050)");
599 return 0.; // bug in the element 0x0020,0x0050
606 gdcmWarningMacro( "Unfound Location (0020,0050)");
608 return 0.; // Hopeless
612 * \brief gets the info from 0020,0037 : Image Orientation Patient
613 * (needed to organize DICOM files based on their x,y,z position)
614 * @param iop adress of the (6)float aray to receive values
615 * @return cosines of image orientation patient
617 void File::GetImageOrientationPatient( float iop[6] )
619 std::string strImOriPat;
620 //iop is supposed to be float[6]
621 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
623 // 0020 0037 DS REL Image Orientation (Patient)
624 if ( (strImOriPat = GetEntryValue(0x0020,0x0037)) != GDCM_UNFOUND )
626 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
627 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
629 gdcmWarningMacro( "Wrong Image Orientation Patient (0020,0037). Less than 6 values were found." );
633 // 0020 0035 DS REL Image Orientation (RET)
634 else if ( (strImOriPat = GetEntryValue(0x0020,0x0035)) != GDCM_UNFOUND )
636 if( sscanf( strImOriPat.c_str(), "%f\\%f\\%f\\%f\\%f\\%f",
637 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
639 gdcmWarningMacro( "wrong Image Orientation Patient (0020,0035). Less than 6 values were found." );
645 * \brief Retrieve the number of Bits Stored (actually used)
646 * (as opposed to number of Bits Allocated)
647 * @return The encountered number of Bits Stored, 0 by default.
648 * 0 means the file is NOT USABLE. The caller has to check it !
650 int File::GetBitsStored()
652 std::string strSize = GetEntryValue( 0x0028, 0x0101 );
653 if ( strSize == GDCM_UNFOUND )
655 gdcmWarningMacro("(0028,0101) is supposed to be mandatory");
656 return 0; // It's supposed to be mandatory
657 // the caller will have to check
659 return atoi( strSize.c_str() );
663 * \brief Retrieve the number of Bits Allocated
664 * (8, 12 -compacted ACR-NEMA files, 16, ...)
665 * @return The encountered number of Bits Allocated, 0 by default.
666 * 0 means the file is NOT USABLE. The caller has to check it !
668 int File::GetBitsAllocated()
670 std::string strSize = GetEntryValue(0x0028,0x0100);
671 if ( strSize == GDCM_UNFOUND )
673 gdcmWarningMacro( "(0028,0100) is supposed to be mandatory");
674 return 0; // It's supposed to be mandatory
675 // the caller will have to check
677 return atoi( strSize.c_str() );
681 * \brief Retrieve the high bit position.
682 * \warning The method defaults to 0 when information is Missing.
683 * The responsability of checking this value is left to the caller.
684 * @return The high bit positin when present. 0 when Missing.
686 int File::GetHighBitPosition()
688 std::string strSize = GetEntryValue( 0x0028, 0x0102 );
689 if ( strSize == GDCM_UNFOUND )
691 gdcmWarningMacro( "(0028,0102) is supposed to be mandatory");
694 return atoi( strSize.c_str() );
698 * \brief Retrieve the number of Samples Per Pixel
699 * (1 : gray level, 3 : RGB -1 or 3 Planes-)
700 * @return The encountered number of Samples Per Pixel, 1 by default.
701 * (Gray level Pixels)
703 int File::GetSamplesPerPixel()
705 const std::string &strSize = GetEntryValue(0x0028,0x0002);
706 if ( strSize == GDCM_UNFOUND )
708 gdcmWarningMacro( "(0028,0002) is supposed to be mandatory");
709 return 1; // Well, it's supposed to be mandatory ...
710 // but sometimes it's missing : *we* assume Gray pixels
712 return atoi( strSize.c_str() );
716 * \brief Retrieve the Planar Configuration for RGB images
717 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
718 * @return The encountered Planar Configuration, 0 by default.
720 int File::GetPlanarConfiguration()
722 std::string strSize = GetEntryValue(0x0028,0x0006);
723 if ( strSize == GDCM_UNFOUND )
725 gdcmWarningMacro( "Not found : Planar Configuration (0028,0006)");
728 return atoi( strSize.c_str() );
732 * \brief Return the size (in bytes) of a single pixel of data.
733 * @return The size in bytes of a single pixel of data; 0 by default
734 * 0 means the file is NOT USABLE; the caller will have to check
736 int File::GetPixelSize()
738 // 0028 0100 US IMG Bits Allocated
739 // (in order no to be messed up by old RGB images)
740 // if (File::GetEntryValue(0x0028,0x0100) == "24")
743 std::string pixelType = GetPixelType();
744 if ( pixelType == "8U" || pixelType == "8S" )
748 if ( pixelType == "16U" || pixelType == "16S")
752 if ( pixelType == "32U" || pixelType == "32S")
756 if ( pixelType == "FD" )
760 gdcmWarningMacro( "Unknown pixel type");
765 * \brief Build the Pixel Type of the image.
766 * Possible values are:
767 * - 8U unsigned 8 bit,
769 * - 16U unsigned 16 bit,
770 * - 16S signed 16 bit,
771 * - 32U unsigned 32 bit,
772 * - 32S signed 32 bit,
773 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
774 * \warning 12 bit images appear as 16 bit.
775 * 24 bit images appear as 8 bit + photochromatic interp ="RGB "
776 * @return 0S if nothing found. NOT USABLE file. The caller has to check
778 std::string File::GetPixelType()
780 std::string bitsAlloc = GetEntryValue(0x0028, 0x0100); // Bits Allocated
781 if ( bitsAlloc == GDCM_UNFOUND )
783 gdcmWarningMacro( "Missing Bits Allocated (0028,0100)");
784 bitsAlloc = "16"; // default and arbitrary value, not to polute the output
787 if ( bitsAlloc == "64" )
791 else if ( bitsAlloc == "12" )
793 // It will be unpacked
796 else if ( bitsAlloc == "24" )
798 // (in order no to be messed up
799 bitsAlloc = "8"; // by old RGB images)
802 std::string sign = GetEntryValue(0x0028, 0x0103);//"Pixel Representation"
804 if (sign == GDCM_UNFOUND )
806 gdcmWarningMacro( "Missing Pixel Representation (0028,0103)");
807 sign = "U"; // default and arbitrary value, not to polute the output
809 else if ( sign == "0" )
817 return bitsAlloc + sign;
821 * \brief Check whether the pixels are signed or UNsigned data.
822 * \warning The method defaults to false (UNsigned) when information is Missing.
823 * The responsability of checking this value is left to the caller.
824 * @return True when signed, false when UNsigned
826 bool File::IsSignedPixelData()
828 std::string strSize = GetEntryValue( 0x0028, 0x0103 );
829 if ( strSize == GDCM_UNFOUND )
831 gdcmWarningMacro( "(0028,0103) is supposed to be mandatory");
834 int sign = atoi( strSize.c_str() );
843 * \brief Check whether this a monochrome picture (gray levels) or not,
844 * using "Photometric Interpretation" tag (0x0028,0x0004).
845 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
847 bool File::IsMonochrome()
849 const std::string &PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
850 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
851 || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
855 if ( PhotometricInterp == GDCM_UNFOUND )
857 gdcmWarningMacro( "Not found : Photometric Interpretation (0028,0004)");
863 * \brief Check whether this a MONOCHROME1 picture (high values = dark)
864 * or not using "Photometric Interpretation" tag (0x0028,0x0004).
865 * @return true when "MONOCHROME1" . False otherwise.
867 bool File::IsMonochrome1()
869 const std::string &PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
870 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1") )
874 if ( PhotometricInterp == GDCM_UNFOUND )
876 gdcmWarningMacro( "Not found : Photometric Interpretation (0028,0004)");
882 * \brief Check whether this a "PALETTE COLOR" picture or not by accessing
883 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
884 * @return true when "PALETTE COLOR". False otherwise.
886 bool File::IsPaletteColor()
888 std::string PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
889 if ( PhotometricInterp == "PALETTE COLOR " )
893 if ( PhotometricInterp == GDCM_UNFOUND )
895 gdcmWarningMacro( "Not found : Palette color (0028,0004)");
901 * \brief Check whether this a "YBR_FULL" color picture or not by accessing
902 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
903 * @return true when "YBR_FULL". False otherwise.
905 bool File::IsYBRFull()
907 std::string PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
908 if ( PhotometricInterp == "YBR_FULL" )
912 if ( PhotometricInterp == GDCM_UNFOUND )
914 gdcmWarningMacro( "Not found : YBR Full (0028,0004)");
920 * \brief tells us if LUT are used
921 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
922 * are NOT considered as LUT, since nobody knows
923 * how to deal with them
924 * Please warn me if you know sbdy that *does* know ... jprx
925 * @return true if LUT Descriptors and LUT Tables were found
929 // Check the presence of the LUT Descriptors, and LUT Tables
931 if ( !GetDocEntry(0x0028,0x1101) )
935 // LutDescriptorGreen
936 if ( !GetDocEntry(0x0028,0x1102) )
941 if ( !GetDocEntry(0x0028,0x1103) )
945 // Red Palette Color Lookup Table Data
946 if ( !GetDocEntry(0x0028,0x1201) )
950 // Green Palette Color Lookup Table Data
951 if ( !GetDocEntry(0x0028,0x1202) )
955 // Blue Palette Color Lookup Table Data
956 if ( !GetDocEntry(0x0028,0x1203) )
961 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
962 // NOT taken into account, but we don't know how to use it ...
967 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
969 * @return Lookup Table number of Bits , 0 by default
970 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
971 * @ return bit number of each LUT item
973 int File::GetLUTNbits()
975 std::vector<std::string> tokens;
978 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
979 // = Lookup Table Desc-Blue
980 // Consistency already checked in GetLUTLength
981 std::string lutDescription = GetEntryValue(0x0028,0x1101);
982 if ( lutDescription == GDCM_UNFOUND )
987 tokens.clear(); // clean any previous value
988 Util::Tokenize ( lutDescription, tokens, "\\" );
989 //LutLength=atoi(tokens[0].c_str());
990 //LutDepth=atoi(tokens[1].c_str());
992 lutNbits = atoi( tokens[2].c_str() );
999 *\brief gets the info from 0028,1052 : Rescale Intercept
1000 * @return Rescale Intercept
1002 float File::GetRescaleIntercept()
1004 float resInter = 0.;
1005 /// 0028 1052 DS IMG Rescale Intercept
1006 const std::string &strRescInter = GetEntryValue(0x0028,0x1052);
1007 if ( strRescInter != GDCM_UNFOUND )
1009 if( sscanf( strRescInter.c_str(), "%f", &resInter) != 1 )
1011 // bug in the element 0x0028,0x1052
1012 gdcmWarningMacro( "Rescale Intercept (0028,1052) is empty." );
1020 *\brief gets the info from 0028,1053 : Rescale Slope
1021 * @return Rescale Slope
1023 float File::GetRescaleSlope()
1025 float resSlope = 1.;
1026 //0028 1053 DS IMG Rescale Slope
1027 std::string strRescSlope = GetEntryValue(0x0028,0x1053);
1028 if ( strRescSlope != GDCM_UNFOUND )
1030 if( sscanf( strRescSlope.c_str(), "%f", &resSlope) != 1)
1032 // bug in the element 0x0028,0x1053
1033 gdcmWarningMacro( "Rescale Slope (0028,1053) is empty.");
1041 * \brief This function is intended to user who doesn't want
1042 * to have to manage a LUT and expects to get an RBG Pixel image
1043 * (or a monochrome one ...)
1044 * \warning to be used with GetImagePixels()
1045 * @return 1 if Gray level, 3 if Color (RGB, YBR or PALETTE COLOR)
1047 int File::GetNumberOfScalarComponents()
1049 if ( GetSamplesPerPixel() == 3 )
1054 // 0028 0100 US IMG Bits Allocated
1055 // (in order no to be messed up by old RGB images)
1056 if ( GetEntryValue(0x0028,0x0100) == "24" )
1061 std::string strPhotometricInterpretation = GetEntryValue(0x0028,0x0004);
1063 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
1065 if ( HasLUT() )// PALETTE COLOR is NOT enough
1075 // beware of trailing space at end of string
1076 // DICOM tags are never of odd length
1077 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
1078 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
1079 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
1085 // we assume that *all* kinds of YBR are dealt with
1091 * \brief This function is intended to user that DOESN'T want
1092 * to get RGB pixels image when it's stored as a PALETTE COLOR image
1093 * - the (vtk) user is supposed to know how deal with LUTs -
1094 * \warning to be used with GetImagePixelsRaw()
1095 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
1097 int File::GetNumberOfScalarComponentsRaw()
1099 // 0028 0100 US IMG Bits Allocated
1100 // (in order no to be messed up by old RGB images)
1101 if ( File::GetEntryValue(0x0028,0x0100) == "24" )
1106 // we assume that *all* kinds of YBR are dealt with
1107 return GetSamplesPerPixel();
1111 * \brief Recover the offset (from the beginning of the file)
1112 * of *image* pixels (not *icone image* pixels, if any !)
1113 * @return Pixel Offset
1115 size_t File::GetPixelOffset()
1117 DocEntry *pxlElement = GetDocEntry(GrPixel, NumPixel);
1120 return pxlElement->GetOffset();
1124 gdcmDebugMacro( "Big trouble : Pixel Element ("
1125 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found" );
1131 * \brief Recover the pixel area length (in Bytes)
1132 * @return Pixel Element Length, as stored in the header
1133 * (NOT the memory space necessary to hold the Pixels
1134 * -in case of embeded compressed image-)
1135 * 0 : NOT USABLE file. The caller has to check.
1137 size_t File::GetPixelAreaLength()
1139 DocEntry *pxlElement = GetDocEntry(GrPixel, NumPixel);
1142 return pxlElement->GetLength();
1146 gdcmDebugMacro( "Big trouble : Pixel Element ("
1147 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found" );
1153 * \brief Adds the characteristics of a new element we want to anonymize
1156 void File::AddAnonymizeElement (uint16_t group, uint16_t elem,
1157 std::string const &value)
1164 AnonymizeList.push_back(el);
1168 * \brief Overwrites in the file the values of the DicomElements
1171 void File::AnonymizeNoLoad()
1173 std::fstream *fp = new std::fstream(Filename.c_str(),
1174 std::ios::in | std::ios::out | std::ios::binary);
1177 // how to white out disk space if longer than 50 ?
1183 uint32_t valLgth = 0;
1184 std::string *spaces;
1185 for (ListElements::iterator it = AnonymizeList.begin();
1186 it != AnonymizeList.end();
1189 d = GetDocEntry( (*it).Group, (*it).Elem);
1194 if ( dynamic_cast<BinEntry *>(d)
1195 || dynamic_cast<SeqEntry *>(d) )
1198 offset = d->GetOffset();
1199 lgth = d->GetLength();
1202 spaces = new std::string( lgth-valLgth, ' ');
1203 (*it).Value = (*it).Value + *spaces;
1206 fp->seekp( offset, std::ios::beg );
1207 fp->write( (*it).Value.c_str(), lgth );
1215 * \brief anonymize a File (removes Patient's personal info passed with
1216 * AddAnonymizeElement()
1218 bool File::AnonymizeFile()
1220 // If Anonymisation list is empty, let's perform some basic anonymization
1221 if ( AnonymizeList.begin() == AnonymizeList.end() )
1223 // If exist, replace by spaces
1224 SetValEntry (" ",0x0010, 0x2154); // Telephone
1225 SetValEntry (" ",0x0010, 0x1040); // Adress
1226 SetValEntry (" ",0x0010, 0x0020); // Patient ID
1228 DocEntry* patientNameHE = GetDocEntry (0x0010, 0x0010);
1230 if ( patientNameHE ) // we replace it by Study Instance UID (why not ?)
1232 std::string studyInstanceUID = GetEntryValue (0x0020, 0x000d);
1233 if ( studyInstanceUID != GDCM_UNFOUND )
1235 SetValEntry(studyInstanceUID, 0x0010, 0x0010);
1239 SetValEntry("anonymised", 0x0010, 0x0010);
1246 for (ListElements::iterator it = AnonymizeList.begin();
1247 it != AnonymizeList.end();
1250 d = GetDocEntry( (*it).Group, (*it).Elem);
1255 if ( dynamic_cast<BinEntry *>(d)
1256 || dynamic_cast<SeqEntry *>(d) )
1259 SetValEntry ((*it).Value, (*it).Group, (*it).Elem);
1263 // In order to make definitively impossible any further identification
1264 // remove or replace all the stuff that contains a Date
1266 //0008 0012 DA ID Instance Creation Date
1267 //0008 0020 DA ID Study Date
1268 //0008 0021 DA ID Series Date
1269 //0008 0022 DA ID Acquisition Date
1270 //0008 0023 DA ID Content Date
1271 //0008 0024 DA ID Overlay Date
1272 //0008 0025 DA ID Curve Date
1273 //0008 002a DT ID Acquisition Datetime
1274 //0018 9074 DT ACQ Frame Acquisition Datetime
1275 //0018 9151 DT ACQ Frame Reference Datetime
1276 //0018 a002 DT ACQ Contribution Date Time
1277 //0020 3403 SH REL Modified Image Date (RET)
1278 //0032 0032 DA SDY Study Verified Date
1279 //0032 0034 DA SDY Study Read Date
1280 //0032 1000 DA SDY Scheduled Study Start Date
1281 //0032 1010 DA SDY Scheduled Study Stop Date
1282 //0032 1040 DA SDY Study Arrival Date
1283 //0032 1050 DA SDY Study Completion Date
1284 //0038 001a DA VIS Scheduled Admission Date
1285 //0038 001c DA VIS Scheduled Discharge Date
1286 //0038 0020 DA VIS Admitting Date
1287 //0038 0030 DA VIS Discharge Date
1288 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1289 //0040 0004 DA PRC Scheduled Procedure Step End Date
1290 //0040 0244 DA PRC Performed Procedure Step Start Date
1291 //0040 0250 DA PRC Performed Procedure Step End Date
1292 //0040 2004 DA PRC Issue Date of Imaging Service Request
1293 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1294 //0040 4011 DT PRC Expected Completion Date and Time
1295 //0040 a030 DT PRC Verification Date Time
1296 //0040 a032 DT PRC Observation Date Time
1297 //0040 a120 DT PRC DateTime
1298 //0040 a121 DA PRC Date
1299 //0040 a13a DT PRC Referenced Datetime
1300 //0070 0082 DA ??? Presentation Creation Date
1301 //0100 0420 DT ??? SOP Autorization Date and Time
1302 //0400 0105 DT ??? Digital Signature DateTime
1303 //2100 0040 DA PJ Creation Date
1304 //3006 0008 DA SSET Structure Set Date
1305 //3008 0024 DA ??? Treatment Control Point Date
1306 //3008 0054 DA ??? First Treatment Date
1307 //3008 0056 DA ??? Most Recent Treatment Date
1308 //3008 0162 DA ??? Safe Position Exit Date
1309 //3008 0166 DA ??? Safe Position Return Date
1310 //3008 0250 DA ??? Treatment Date
1311 //300a 0006 DA RT RT Plan Date
1312 //300a 022c DA RT Air Kerma Rate Reference Date
1313 //300e 0004 DA RT Review Date
1319 * \brief Performs some consistency checking on various 'File related'
1320 * (as opposed to 'DicomDir related') entries
1321 * then writes in a file all the (Dicom Elements) included the Pixels
1322 * @param fileName file name to write to
1323 * @param writetype Type of the File to be written
1324 * (ACR, ExplicitVR, ImplicitVR)
1326 bool File::Write(std::string fileName, FileType writetype)
1328 std::ofstream *fp = new std::ofstream(fileName.c_str(),
1329 std::ios::out | std::ios::binary);
1332 gdcmWarningMacro("Failed to open (write) File: " << fileName.c_str());
1336 // Entry : 0002|0000 = group length -> recalculated
1337 ValEntry*e0000 = GetValEntry(0x0002,0x0000);
1340 std::ostringstream sLen;
1341 sLen << ComputeGroup0002Length(writetype);
1342 e0000->SetValue(sLen.str());
1346 if ( GetEntryValue(0x0028,0x0100) == "12")
1348 SetValEntry("16", 0x0028,0x0100);
1351 int i_lgPix = GetEntryLength(GrPixel, NumPixel);
1354 // no (GrPixel, NumPixel) element
1355 std::string s_lgPix = Util::Format("%d", i_lgPix+12);
1356 s_lgPix = Util::DicomString( s_lgPix.c_str() );
1357 InsertValEntry(s_lgPix,GrPixel, 0x0000);
1360 // FIXME : should be nice if we could move it to File
1361 // (or in future gdcmPixelData class)
1363 // Drop Palette Color, if necessary
1364 if ( GetEntryValue(0x0028,0x0002).c_str()[0] == '3' )
1366 // if SamplesPerPixel = 3, sure we don't need any LUT !
1367 // Drop 0028|1101, 0028|1102, 0028|1103
1368 // Drop 0028|1201, 0028|1202, 0028|1203
1370 DocEntry *e = GetDocEntry(0x0028,0x01101);
1373 RemoveEntryNoDestroy(e);
1375 e = GetDocEntry(0x0028,0x1102);
1378 RemoveEntryNoDestroy(e);
1380 e = GetDocEntry(0x0028,0x1103);
1383 RemoveEntryNoDestroy(e);
1385 e = GetDocEntry(0x0028,0x01201);
1388 RemoveEntryNoDestroy(e);
1390 e = GetDocEntry(0x0028,0x1202);
1393 RemoveEntryNoDestroy(e);
1395 e = GetDocEntry(0x0028,0x1203);
1398 RemoveEntryNoDestroy(e);
1402 Document::WriteContent(fp, writetype);
1410 //-----------------------------------------------------------------------------
1413 * \brief Initialize a default DICOM File that should contain all the
1414 * fields required by other readers. DICOM standard does not
1415 * explicitely defines those fields, heuristic has been choosen.
1416 * \todo some more tests to be performed to avoid collisions
1417 * with FileHelper::CheckMandatoryElements()
1419 void File::InitializeDefaultFile()
1422 std::string date = Util::GetCurrentDate();
1423 std::string time = Util::GetCurrentTime();
1424 std::string uid = Util::CreateUniqueUID();
1425 std::string uidMedia = uid;
1426 std::string uidInst = uid;
1427 std::string uidClass = Util::CreateUniqueUID();
1428 std::string uidStudy = Util::CreateUniqueUID();
1429 std::string uidSerie = Util::CreateUniqueUID();
1431 // Meta Element Group Length
1432 InsertValEntry("146 ", 0x0002, 0x0000);
1433 // Media Storage SOP Class UID (Secondary Capture Image Storage)
1434 InsertValEntry("1.2.840.10008.5.1.4.1.1.7", 0x0002, 0x0002);
1435 // Media Storage SOP Instance UID
1436 InsertValEntry(uidMedia.c_str(), 0x0002, 0x0003);
1437 // Transfer Syntax UID (Explicit VR Little Endian)
1438 InsertValEntry("1.2.840.10008.1.2.1 ", 0x0002, 0x0010);
1439 // META Implementation Class UID
1440 InsertValEntry(uidClass.c_str(), 0x0002, 0x0012);
1441 // Source Application Entity Title
1442 InsertValEntry("GDCM", 0x0002, 0x0016);
1444 // Instance Creation Date
1445 InsertValEntry(date.c_str(), 0x0008, 0x0012);
1446 // Instance Creation Time
1447 InsertValEntry(time.c_str(), 0x0008, 0x0013);
1449 InsertValEntry("1.2.840.10008.5.1.4.1.1.2", 0x0008, 0x0016);
1451 InsertValEntry(uidInst.c_str(), 0x0008, 0x0018);
1453 InsertValEntry("OT", 0x0008, 0x0060);
1455 InsertValEntry("GDCM", 0x0008, 0x0070);
1457 InsertValEntry("GDCM", 0x0008, 0x0080);
1458 // Institution Address
1459 InsertValEntry("http://www-creatis.insa-lyon.fr/Public/Gdcm", 0x0008, 0x0081);
1462 InsertValEntry("GDCM^patient", 0x0010, 0x0010);
1464 InsertValEntry("GDCMID", 0x0010, 0x0020);
1466 // Study Instance UID
1467 InsertValEntry(uidStudy.c_str(), 0x0020, 0x000d);
1468 // Series Instance UID
1469 InsertValEntry(uidSerie.c_str(), 0x0020, 0x000e);
1471 InsertValEntry("1", 0x0020, 0x0010);
1473 InsertValEntry("1", 0x0020, 0x0011);
1475 // Samples per pixel 1 or 3
1476 InsertValEntry("1", 0x0028, 0x0002);
1477 // photochromatic interpretation
1478 InsertValEntry("MONOCHROME2", 0x0028, 0x0004);
1480 InsertValEntry("0", 0x0028, 0x0010);
1482 InsertValEntry("0", 0x0028, 0x0011);
1483 // BitsAllocated 8 or 12 or 16
1484 InsertValEntry("8", 0x0028, 0x0100);
1485 // BitsStored <= BitsAllocated
1486 InsertValEntry("8", 0x0028, 0x0101);
1487 // HighBit <= BitsAllocated - 1
1488 InsertValEntry("7", 0x0028, 0x0102);
1489 // Pixel Representation 0(unsigned) or 1(signed)
1490 InsertValEntry("0", 0x0028, 0x0103);
1493 // Special case this is the image (not a string)
1496 InsertBinEntry(0, 0, GrPixel, NumPixel);
1499 //-----------------------------------------------------------------------------
1502 * \brief Parse pixel data from disk of [multi-]fragment RLE encoding.
1503 * Compute the RLE extra information and store it in \ref RLEInfo
1504 * for later pixel retrieval usage.
1506 void File::ComputeRLEInfo()
1508 std::string ts = GetTransferSyntax();
1509 if ( !Global::GetTS()->IsRLELossless(ts) )
1514 // Encoded pixel data: for the time being we are only concerned with
1515 // Jpeg or RLE Pixel data encodings.
1516 // As stated in PS 3.5-2003, section 8.2 p44:
1517 // "If sent in Encapsulated Format (i.e. other than the Native Format) the
1518 // value representation OB is used".
1519 // Hence we expect an OB value representation. Concerning OB VR,
1520 // the section PS 3.5-2003, section A.4.c p 58-59, states:
1521 // "For the Value Representations OB and OW, the encoding shall meet the
1522 // following specifications depending on the Data element tag:"
1524 // - the first item in the sequence of items before the encoded pixel
1525 // data stream shall be basic offset table item. The basic offset table
1526 // item value, however, is not required to be present"
1527 ReadAndSkipEncapsulatedBasicOffsetTable();
1529 // Encapsulated RLE Compressed Images (see PS 3.5-2003, Annex G)
1530 // Loop on the individual frame[s] and store the information
1531 // on the RLE fragments in a RLEFramesInfo.
1532 // Note: - when only a single frame is present, this is a
1534 // - when more than one frame are present, then we are in
1535 // the case of a multi-frame image.
1537 while ( (frameLength = ReadTagLength(0xfffe, 0xe000)) )
1539 // Parse the RLE Header and store the corresponding RLE Segment
1540 // Offset Table information on fragments of this current Frame.
1541 // Note that the fragment pixels themselves are not loaded
1542 // (but just skipped).
1543 long frameOffset = Fp->tellg();
1545 uint32_t nbRleSegments = ReadInt32();
1546 if ( nbRleSegments > 16 )
1548 // There should be at most 15 segments (refer to RLEFrame class)
1549 gdcmWarningMacro( "Too many segments.");
1552 uint32_t rleSegmentOffsetTable[16];
1553 for( int k = 1; k <= 15; k++ )
1555 rleSegmentOffsetTable[k] = ReadInt32();
1558 // Deduce from both the RLE Header and the frameLength the
1559 // fragment length, and again store this info in a
1561 long rleSegmentLength[15];
1562 // skipping (not reading) RLE Segments
1563 if ( nbRleSegments > 1)
1565 for(unsigned int k = 1; k <= nbRleSegments-1; k++)
1567 rleSegmentLength[k] = rleSegmentOffsetTable[k+1]
1568 - rleSegmentOffsetTable[k];
1569 SkipBytes(rleSegmentLength[k]);
1573 rleSegmentLength[nbRleSegments] = frameLength
1574 - rleSegmentOffsetTable[nbRleSegments];
1575 SkipBytes(rleSegmentLength[nbRleSegments]);
1577 // Store the collected info
1578 RLEFrame *newFrame = new RLEFrame;
1579 newFrame->SetNumberOfFragments(nbRleSegments);
1580 for( unsigned int uk = 1; uk <= nbRleSegments; uk++ )
1582 newFrame->SetOffset(uk,frameOffset + rleSegmentOffsetTable[uk]);
1583 newFrame->SetLength(uk,rleSegmentLength[uk]);
1585 RLEInfo->AddFrame(newFrame);
1588 // Make sure that at the end of the item we encounter a 'Sequence
1590 if ( !ReadTag(0xfffe, 0xe0dd) )
1592 gdcmWarningMacro( "No sequence delimiter item at end of RLE item sequence");
1597 * \brief Parse pixel data from disk of [multi-]fragment Jpeg encoding.
1598 * Compute the jpeg extra information (fragment[s] offset[s] and
1599 * length) and store it[them] in \ref JPEGInfo for later pixel
1602 void File::ComputeJPEGFragmentInfo()
1604 // If you need to, look for comments of ComputeRLEInfo().
1605 std::string ts = GetTransferSyntax();
1606 if ( ! Global::GetTS()->IsJPEG(ts) )
1611 ReadAndSkipEncapsulatedBasicOffsetTable();
1613 // Loop on the fragments[s] and store the parsed information in a
1615 long fragmentLength;
1616 while ( (fragmentLength = ReadTagLength(0xfffe, 0xe000)) )
1618 long fragmentOffset = Fp->tellg();
1620 // Store the collected info
1621 JPEGFragment *newFragment = new JPEGFragment;
1622 newFragment->SetOffset(fragmentOffset);
1623 newFragment->SetLength(fragmentLength);
1624 JPEGInfo->AddFragment(newFragment);
1626 SkipBytes(fragmentLength);
1629 // Make sure that at the end of the item we encounter a 'Sequence
1631 if ( !ReadTag(0xfffe, 0xe0dd) )
1633 gdcmWarningMacro( "No sequence delimiter item at end of JPEG item sequence");
1638 * \brief Assuming the internal file pointer \ref Document::Fp
1639 * is placed at the beginning of a tag check whether this
1640 * tag is (TestGroup, TestElement).
1641 * \warning On success the internal file pointer \ref Document::Fp
1642 * is modified to point after the tag.
1643 * On failure (i.e. when the tag wasn't the expected tag
1644 * (TestGroup, TestElement) the internal file pointer
1645 * \ref Document::Fp is restored to it's original position.
1646 * @param testGroup The expected group of the tag.
1647 * @param testElement The expected Element of the tag.
1648 * @return True on success, false otherwise.
1650 bool File::ReadTag(uint16_t testGroup, uint16_t testElement)
1652 long positionOnEntry = Fp->tellg();
1653 long currentPosition = Fp->tellg(); // On debugging purposes
1655 // Read the Item Tag group and element, and make
1656 // sure they are what we expected:
1657 uint16_t itemTagGroup;
1658 uint16_t itemTagElement;
1661 itemTagGroup = ReadInt16();
1662 itemTagElement = ReadInt16();
1664 catch ( FormatError e )
1666 //std::cerr << e << std::endl;
1669 if ( itemTagGroup != testGroup || itemTagElement != testElement )
1671 gdcmWarningMacro( "Wrong Item Tag found:"
1672 << " We should have found tag ("
1673 << std::hex << testGroup << "," << testElement << ")" << std::endl
1674 << " but instead we encountered tag ("
1675 << std::hex << itemTagGroup << "," << itemTagElement << ")"
1676 << " at address: " << " 0x(" << (unsigned int)currentPosition << ")"
1678 Fp->seekg(positionOnEntry, std::ios::beg);
1686 * \brief Assuming the internal file pointer \ref Document::Fp
1687 * is placed at the beginning of a tag (TestGroup, TestElement),
1688 * read the length associated to the Tag.
1689 * \warning On success the internal file pointer \ref Document::Fp
1690 * is modified to point after the tag and it's length.
1691 * On failure (i.e. when the tag wasn't the expected tag
1692 * (TestGroup, TestElement) the internal file pointer
1693 * \ref Document::Fp is restored to it's original position.
1694 * @param testGroup The expected group of the tag.
1695 * @param testElement The expected Element of the tag.
1696 * @return On success returns the length associated to the tag. On failure
1699 uint32_t File::ReadTagLength(uint16_t testGroup, uint16_t testElement)
1702 if ( !ReadTag(testGroup, testElement) )
1707 //// Then read the associated Item Length
1708 long currentPosition = Fp->tellg();
1709 uint32_t itemLength = ReadInt32();
1711 gdcmWarningMacro( "Basic Item Length is: "
1712 << itemLength << std::endl
1713 << " at address: " << std::hex << (unsigned int)currentPosition);
1719 * \brief When parsing the Pixel Data of an encapsulated file, read
1720 * the basic offset table (when present, and BTW dump it).
1722 void File::ReadAndSkipEncapsulatedBasicOffsetTable()
1724 //// Read the Basic Offset Table Item Tag length...
1725 uint32_t itemLength = ReadTagLength(0xfffe, 0xe000);
1727 // When present, read the basic offset table itself.
1728 // Notes: - since the presence of this basic offset table is optional
1729 // we can't rely on it for the implementation, and we will simply
1730 // trash it's content (when present).
1731 // - still, when present, we could add some further checks on the
1732 // lengths, but we won't bother with such fuses for the time being.
1733 if ( itemLength != 0 )
1735 char *basicOffsetTableItemValue = new char[itemLength + 1];
1736 Fp->read(basicOffsetTableItemValue, itemLength);
1739 for (unsigned int i=0; i < itemLength; i += 4 )
1741 uint32_t individualLength = str2num( &basicOffsetTableItemValue[i],
1743 gdcmWarningMacro( "Read one length: " <<
1744 std::hex << individualLength );
1748 delete[] basicOffsetTableItemValue;
1752 //-----------------------------------------------------------------------------
1755 //-----------------------------------------------------------------------------
1756 } // end namespace gdcm