1 /*=========================================================================
4 Module: $RCSfile: gdcmFile.cxx,v $
6 Date: $Date: 2005/07/02 18:09:36 $
7 Version: $Revision: 1.246 $
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"
43 #include <stdio.h> //sscanf
44 #include <stdlib.h> // for atoi
48 //-----------------------------------------------------------------------------
49 // Constructor / Destructor
52 * \brief Constructor used when we want to generate dicom files from scratch
57 RLEInfo = new RLEFramesInfo;
58 JPEGInfo = new JPEGFragmentsInfo;
59 GrPixel = 0x7fe0; // to avoid further troubles
65 * @param filename name of the file whose header we want to analyze
67 File::File( std::string const &filename )
70 RLEInfo = new RLEFramesInfo;
71 JPEGInfo = new JPEGFragmentsInfo;
77 * \brief Canonical destructor.
87 //-----------------------------------------------------------------------------
92 * @param fileName file to be open for parsing
93 * @return false if file cannot be open or no swap info was found,
94 * or no tag was found.
96 bool File::Load( std::string const &fileName )
98 this->Document::Load( fileName );
100 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
101 // We may encounter the 'RETired' (0x0028, 0x0200) tag
102 // (Image Location") . This entry contains the number of
103 // the group that contains the pixel data (hence the "Pixel Data"
104 // is found by indirection through the "Image Location").
105 // Inside the group pointed by "Image Location" the searched element
106 // is conventionally the element 0x0010 (when the norm is respected).
107 // When the "Image Location" is missing we default to group 0x7fe0.
108 // Note: this IS the right place for the code
111 const std::string &imgLocation = GetEntryValue(0x0028, 0x0200);
112 if ( imgLocation == GDCM_UNFOUND )
119 GrPixel = (uint16_t) atoi( imgLocation.c_str() );
122 // sometimes Image Location value doesn't follow
123 // the supposed processor endianness.
124 // see gdcmData/cr172241.dcm
125 if ( GrPixel == 0xe07f )
130 if ( GrPixel != 0x7fe0 )
132 // This is a kludge for old dirty Philips imager.
140 // Now, we know GrPixel and NumPixel.
141 // Let's create a VirtualDictEntry to allow a further VR modification
142 // and force VR to match with BitsAllocated.
143 DocEntry *entry = GetDocEntry(GrPixel, NumPixel);
146 // Compute the RLE or JPEG info
148 const std::string &ts = GetTransferSyntax();
149 Fp->seekg( entry->GetOffset(), std::ios::beg );
150 if ( Global::GetTS()->IsRLELossless(ts) )
152 else if ( Global::GetTS()->IsJPEG(ts) )
153 ComputeJPEGFragmentInfo();
156 // Create a new BinEntry to change the DictEntry
157 // The changed DictEntry will have
158 // - a correct PixelVR OB or OW)
159 // - the name to "Pixel Data"
160 BinEntry *oldEntry = dynamic_cast<BinEntry *>(entry);
164 // 8 bits allocated is a 'O Bytes' , as well as 24 (old ACR-NEMA RGB)
165 // more than 8 (i.e 12, 16) is a 'O Words'
166 if ( GetBitsAllocated() == 8 || GetBitsAllocated() == 24 )
171 // Change only made if usefull
172 if ( PixelVR != oldEntry->GetVR() )
174 DictEntry* newDict = NewVirtualDictEntry(GrPixel,NumPixel,
175 PixelVR,"1","Pixel Data");
177 BinEntry *newEntry = new BinEntry(newDict);
178 newEntry->Copy(entry);
179 newEntry->SetBinArea(oldEntry->GetBinArea(),oldEntry->IsSelfArea());
180 oldEntry->SetSelfArea(false);
182 RemoveEntry(oldEntry);
190 * \brief This predicate, based on hopefully reasonable heuristics,
191 * decides whether or not the current File was properly parsed
192 * and contains the mandatory information for being considered as
193 * a well formed and usable Dicom/Acr File.
194 * @return true when File is the one of a reasonable Dicom/Acr file,
197 bool File::IsReadable()
199 if ( !Document::IsReadable() )
204 const std::string &res = GetEntryValue(0x0028, 0x0005);
205 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
207 gdcmWarningMacro("Wrong Image Dimensions" << res);
208 return false; // Image Dimensions
210 bool b0028_0100 = true;
211 if ( !GetDocEntry(0x0028, 0x0100) )
213 gdcmWarningMacro("Bits Allocated (0028|0100) not found");
214 //return false; // "Bits Allocated"
217 bool b0028_0101 = true;
218 if ( !GetDocEntry(0x0028, 0x0101) )
220 gdcmWarningMacro("Bits Stored (0028|0101) not found");
221 //return false; // "Bits Stored"
224 bool b0028_0102 = true;
225 if ( !GetDocEntry(0x0028, 0x0102) )
227 gdcmWarningMacro("Hight Bit (0028|0102) not found");
228 //return false; // "High Bit"
231 bool b0028_0103 = true;
232 if ( !GetDocEntry(0x0028, 0x0103) )
234 gdcmWarningMacro("Pixel Representation (0028|0103) not found");
235 //return false; // "Pixel Representation" i.e. 'Sign' ( 0 : unsigned, 1 : signed)
239 if ( !b0028_0100 && !b0028_0101 && !b0028_0102 && !b0028_0103)
241 gdcmWarningMacro("Too much mandatory Tags missing !");
245 if ( !GetDocEntry(GrPixel, NumPixel) )
247 gdcmWarningMacro("Pixel Dicom Element " << std::hex <<
248 GrPixel << "|" << NumPixel << "not found");
249 return false; // Pixel Dicom Element not found :-(
255 * \brief gets the info from 0020,0013 : Image Number else 0.
256 * @return image number
258 int File::GetImageNumber()
260 //0020 0013 : Image Number
261 std::string strImNumber = GetEntryValue(0x0020,0x0013);
262 if ( strImNumber != GDCM_UNFOUND )
264 return atoi( strImNumber.c_str() );
270 * \brief gets the info from 0008,0060 : Modality
271 * @return Modality Type
273 ModalityType File::GetModality()
275 // 0008 0060 : Modality
276 std::string strModality = GetEntryValue(0x0008,0x0060);
277 if ( strModality != GDCM_UNFOUND )
279 if ( strModality.find("AU") < strModality.length()) return AU;
280 else if ( strModality.find("AS") < strModality.length()) return AS;
281 else if ( strModality.find("BI") < strModality.length()) return BI;
282 else if ( strModality.find("CF") < strModality.length()) return CF;
283 else if ( strModality.find("CP") < strModality.length()) return CP;
284 else if ( strModality.find("CR") < strModality.length()) return CR;
285 else if ( strModality.find("CT") < strModality.length()) return CT;
286 else if ( strModality.find("CS") < strModality.length()) return CS;
287 else if ( strModality.find("DD") < strModality.length()) return DD;
288 else if ( strModality.find("DF") < strModality.length()) return DF;
289 else if ( strModality.find("DG") < strModality.length()) return DG;
290 else if ( strModality.find("DM") < strModality.length()) return DM;
291 else if ( strModality.find("DS") < strModality.length()) return DS;
292 else if ( strModality.find("DX") < strModality.length()) return DX;
293 else if ( strModality.find("ECG") < strModality.length()) return ECG;
294 else if ( strModality.find("EPS") < strModality.length()) return EPS;
295 else if ( strModality.find("FA") < strModality.length()) return FA;
296 else if ( strModality.find("FS") < strModality.length()) return FS;
297 else if ( strModality.find("HC") < strModality.length()) return HC;
298 else if ( strModality.find("HD") < strModality.length()) return HD;
299 else if ( strModality.find("LP") < strModality.length()) return LP;
300 else if ( strModality.find("LS") < strModality.length()) return LS;
301 else if ( strModality.find("MA") < strModality.length()) return MA;
302 else if ( strModality.find("MR") < strModality.length()) return MR;
303 else if ( strModality.find("NM") < strModality.length()) return NM;
304 else if ( strModality.find("OT") < strModality.length()) return OT;
305 else if ( strModality.find("PT") < strModality.length()) return PT;
306 else if ( strModality.find("RF") < strModality.length()) return RF;
307 else if ( strModality.find("RG") < strModality.length()) return RG;
308 else if ( strModality.find("RTDOSE")
309 < strModality.length()) return RTDOSE;
310 else if ( strModality.find("RTIMAGE")
311 < strModality.length()) return RTIMAGE;
312 else if ( strModality.find("RTPLAN")
313 < strModality.length()) return RTPLAN;
314 else if ( strModality.find("RTSTRUCT")
315 < strModality.length()) return RTSTRUCT;
316 else if ( strModality.find("SM") < strModality.length()) return SM;
317 else if ( strModality.find("ST") < strModality.length()) return ST;
318 else if ( strModality.find("TG") < strModality.length()) return TG;
319 else if ( strModality.find("US") < strModality.length()) return US;
320 else if ( strModality.find("VF") < strModality.length()) return VF;
321 else if ( strModality.find("XA") < strModality.length()) return XA;
322 else if ( strModality.find("XC") < strModality.length()) return XC;
326 /// \todo throw error return value ???
327 /// specified <> unknown in our database
335 * \brief Retrieve the number of columns of image.
336 * @return The encountered size when found, 0 by default.
337 * 0 means the file is NOT USABLE. The caller will have to check
341 const std::string &strSize = GetEntryValue(0x0028,0x0011);
342 if ( strSize == GDCM_UNFOUND )
346 return atoi( strSize.c_str() );
350 * \brief Retrieve the number of lines of image.
351 * \warning The defaulted value is 1 as opposed to File::GetXSize()
352 * @return The encountered size when found, 1 by default
353 * (The ACR-NEMA file contains a Signal, not an Image).
357 const std::string &strSize = GetEntryValue(0x0028,0x0010);
358 if ( strSize != GDCM_UNFOUND )
360 return atoi( strSize.c_str() );
367 // The Rows (0028,0010) entry was optional for ACR/NEMA.
368 // (at least some images didn't have it.)
369 // It might hence be a signal (1D image). So we default to 1:
374 * \brief Retrieve the number of planes of volume or the number
375 * of frames of a multiframe.
376 * \warning When present we consider the "Number of Frames" as the third
377 * dimension. When missing we consider the third dimension as
378 * being the ACR-NEMA "Planes" tag content.
379 * @return The encountered size when found, 1 by default (single image).
383 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
384 // as the third dimension.
385 const std::string &strSize = GetEntryValue(0x0028,0x0008);
386 if ( strSize != GDCM_UNFOUND )
388 return atoi( strSize.c_str() );
391 // We then consider the "Planes" entry as the third dimension
392 const std::string &strSize2 = GetEntryValue(0x0028,0x0012);
393 if ( strSize2 != GDCM_UNFOUND )
395 return atoi( strSize2.c_str() );
401 * \brief gets the info from 0018,1164 : ImagerPixelSpacing
402 * then 0028,0030 : Pixel Spacing
404 * @return X dimension of a pixel
406 float File::GetXSpacing()
408 float xspacing = 1.0;
409 float yspacing = 1.0;
412 // To follow David Clunie's advice, we first check ImagerPixelSpacing
414 const std::string &strImagerPixelSpacing = GetEntryValue(0x0018,0x1164);
415 if ( strImagerPixelSpacing != GDCM_UNFOUND )
417 if ( ( nbValues = sscanf( strImagerPixelSpacing.c_str(),
418 "%f\\%f", &yspacing, &xspacing)) != 2 )
420 // if no values, xspacing is set to 1.0
423 // if single value is found, xspacing is defaulted to yspacing
427 if ( xspacing == 0.0 )
434 const std::string &strSpacing = GetEntryValue(0x0028,0x0030);
436 if ( strSpacing == GDCM_UNFOUND )
438 gdcmWarningMacro( "Unfound Pixel Spacing (0028,0030)" );
442 if ( ( nbValues = sscanf( strSpacing.c_str(),
443 "%f \\%f ", &yspacing, &xspacing)) != 2 )
445 // if no values, xspacing is set to 1.0
448 // if single value is found, xspacing is defaulted to yspacing
452 if ( xspacing == 0.0 )
458 // to avoid troubles with David Clunie's-like images (at least one)
459 if ( xspacing == 0. && yspacing == 0.)
464 gdcmWarningMacro("gdcmData/CT-MONO2-8-abdo.dcm-like problem");
465 // seems to be a bug in the header ...
466 nbValues = sscanf( strSpacing.c_str(), "%f \\0\\%f ", &yspacing, &xspacing);
467 gdcmAssertMacro( nbValues == 2 );
474 * \brief gets the info from 0018,1164 : ImagerPixelSpacing
475 * then from 0028,0030 : Pixel Spacing
477 * @return Y dimension of a pixel
479 float File::GetYSpacing()
483 // To follow David Clunie's advice, we first check ImagerPixelSpacing
485 const std::string &strImagerPixelSpacing = GetEntryValue(0x0018,0x1164);
486 if ( strImagerPixelSpacing != GDCM_UNFOUND )
488 nbValues = sscanf( strImagerPixelSpacing.c_str(), "%f", &yspacing);
490 // if sscanf cannot read any float value, it won't affect yspacing
494 if ( yspacing == 0.0 )
500 std::string strSpacing = GetEntryValue(0x0028,0x0030);
501 if ( strSpacing == GDCM_UNFOUND )
503 gdcmWarningMacro("Unfound Pixel Spacing (0028,0030)");
507 // if sscanf cannot read any float value, it won't affect yspacing
508 nbValues = sscanf( strSpacing.c_str(), "%f", &yspacing);
510 // if no values, yspacing is set to 1.0
514 if ( yspacing == 0.0 )
521 * \brief gets the info from 0018,0088 : Space Between Slices
522 * else from 0018,0050 : Slice Thickness
524 * @return Z dimension of a voxel-to be
526 float File::GetZSpacing()
528 // Spacing Between Slices : distance between the middle of 2 slices
530 // jointives (Spacing between Slices = Slice Thickness)
531 // overlapping (Spacing between Slices < Slice Thickness)
532 // disjointes (Spacing between Slices > Slice Thickness)
533 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
534 // It only concerns the MRI guys, not people wanting to visualize volumes
535 // If Spacing Between Slices is missing,
536 // we suppose slices joint together
538 const std::string &strSpacingBSlices = GetEntryValue(0x0018,0x0088);
540 if ( strSpacingBSlices == GDCM_UNFOUND )
542 gdcmWarningMacro("Unfound Spacing Between Slices (0018,0088)");
543 const std::string &strSliceThickness = GetEntryValue(0x0018,0x0050);
544 if ( strSliceThickness == GDCM_UNFOUND )
546 gdcmWarningMacro("Unfound Slice Thickness (0018,0050)");
551 // if no 'Spacing Between Slices' is found,
552 // we assume slices join together
553 // (no overlapping, no interslice gap)
554 // if they don't, we're fucked up
555 return (float)atof( strSliceThickness.c_str() );
559 return (float)atof( strSpacingBSlices.c_str() );
563 * \brief gets the info from 0020,0032 : Image Position Patient
564 * else from 0020,0030 : Image Position (RET)
566 * @return up-left image corner X position
568 float File::GetXOrigin()
570 float xImPos, yImPos, zImPos;
571 std::string strImPos = GetEntryValue(0x0020,0x0032);
573 if ( strImPos == GDCM_UNFOUND )
575 gdcmWarningMacro( "Unfound Image Position Patient (0020,0032)");
576 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
577 if ( strImPos == GDCM_UNFOUND )
579 gdcmWarningMacro( "Unfound Image Position (RET) (0020,0030)");
584 if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos) != 3 )
593 * \brief gets the info from 0020,0032 : Image Position Patient
594 * else from 0020,0030 : Image Position (RET)
596 * @return up-left image corner Y position
598 float File::GetYOrigin()
600 float xImPos, yImPos, zImPos;
601 std::string strImPos = GetEntryValue(0x0020,0x0032);
603 if ( strImPos == GDCM_UNFOUND)
605 gdcmWarningMacro( "Unfound Image Position Patient (0020,0032)");
606 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
607 if ( strImPos == GDCM_UNFOUND )
609 gdcmWarningMacro( "Unfound Image Position (RET) (0020,0030)");
614 if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos) != 3 )
623 * \brief gets the info from 0020,0032 : Image Position Patient
624 * else from 0020,0030 : Image Position (RET)
625 * else from 0020,1041 : Slice Location
626 * else from 0020,0050 : Location
628 * @return up-left image corner Z position
630 float File::GetZOrigin()
632 float xImPos, yImPos, zImPos;
633 std::string strImPos = GetEntryValue(0x0020,0x0032);
635 if ( strImPos != GDCM_UNFOUND )
637 if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos) != 3)
639 gdcmWarningMacro( "Wrong Image Position Patient (0020,0032)");
640 return 0.; // bug in the element 0x0020,0x0032
648 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
649 if ( strImPos != GDCM_UNFOUND )
651 if ( sscanf( strImPos.c_str(),
652 "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos ) != 3 )
654 gdcmWarningMacro( "Wrong Image Position (RET) (0020,0030)");
655 return 0.; // bug in the element 0x0020,0x0032
663 // for *very* old ACR-NEMA images
664 std::string strSliceLocation = GetEntryValue(0x0020,0x1041);
665 if ( strSliceLocation != GDCM_UNFOUND )
667 if ( sscanf( strSliceLocation.c_str(), "%f ", &zImPos) != 1)
669 gdcmWarningMacro( "Wrong Slice Location (0020,1041)");
670 return 0.; // bug in the element 0x0020,0x1041
677 gdcmWarningMacro( "Unfound Slice Location (0020,1041)");
679 std::string strLocation = GetEntryValue(0x0020,0x0050);
680 if ( strLocation != GDCM_UNFOUND )
682 if ( sscanf( strLocation.c_str(), "%f ", &zImPos) != 1 )
684 gdcmWarningMacro( "Wrong Location (0020,0050)");
685 return 0.; // bug in the element 0x0020,0x0050
692 gdcmWarningMacro( "Unfound Location (0020,0050)");
694 return 0.; // Hopeless
698 * \brief gets the info from 0020,0037 : Image Orientation Patient
699 * (needed to organize DICOM files based on their x,y,z position)
700 * @param iop adress of the (6)float array to receive values
701 * @return cosines of image orientation patient
703 void File::GetImageOrientationPatient( float iop[6] )
705 std::string strImOriPat;
706 //iop is supposed to be float[6]
707 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
709 // 0020 0037 DS REL Image Orientation (Patient)
710 if ( (strImOriPat = GetEntryValue(0x0020,0x0037)) != GDCM_UNFOUND )
712 if ( sscanf( strImOriPat.c_str(), "%f \\ %f \\%f \\%f \\%f \\%f ",
713 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
715 gdcmWarningMacro( "Wrong Image Orientation Patient (0020,0037). Less than 6 values were found." );
719 // 0020 0035 DS REL Image Orientation (RET)
720 else if ( (strImOriPat = GetEntryValue(0x0020,0x0035)) != GDCM_UNFOUND )
722 if ( sscanf( strImOriPat.c_str(), "%f \\ %f \\%f \\%f \\%f \\%f ",
723 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
725 gdcmWarningMacro( "wrong Image Orientation Patient (0020,0035). Less than 6 values were found." );
731 * \brief Retrieve the number of Bits Stored (actually used)
732 * (as opposed to number of Bits Allocated)
733 * @return The encountered number of Bits Stored, 0 by default.
734 * 0 means the file is NOT USABLE. The caller has to check it !
736 int File::GetBitsStored()
738 std::string strSize = GetEntryValue( 0x0028, 0x0101 );
739 if ( strSize == GDCM_UNFOUND )
741 gdcmWarningMacro("(0028,0101) is supposed to be mandatory");
742 return 0; // It's supposed to be mandatory
743 // the caller will have to check
745 return atoi( strSize.c_str() );
749 * \brief Retrieve the number of Bits Allocated
750 * (8, 12 -compacted ACR-NEMA files-, 16, ...)
751 * @return The encountered number of Bits Allocated, 0 by default.
752 * 0 means the file is NOT USABLE. The caller has to check it !
754 int File::GetBitsAllocated()
756 std::string strSize = GetEntryValue(0x0028,0x0100);
757 if ( strSize == GDCM_UNFOUND )
759 gdcmWarningMacro( "(0028,0100) is supposed to be mandatory");
760 return 0; // It's supposed to be mandatory
761 // the caller will have to check
763 return atoi( strSize.c_str() );
767 * \brief Retrieve the high bit position.
768 * \warning The method defaults to 0 when information is missing.
769 * The responsability of checking this value is left to the caller.
770 * @return The high bit position when present. 0 when missing.
772 int File::GetHighBitPosition()
774 std::string strSize = GetEntryValue( 0x0028, 0x0102 );
775 if ( strSize == GDCM_UNFOUND )
777 gdcmWarningMacro( "(0028,0102) is supposed to be mandatory");
780 return atoi( strSize.c_str() );
784 * \brief Retrieve the number of Samples Per Pixel
785 * (1 : gray level, 3 : RGB/YBR -1 or 3 Planes-)
786 * @return The encountered number of Samples Per Pixel, 1 by default.
787 * (we assume Gray level Pixels)
789 int File::GetSamplesPerPixel()
791 const std::string &strSize = GetEntryValue(0x0028,0x0002);
792 if ( strSize == GDCM_UNFOUND )
794 gdcmWarningMacro( "(0028,0002) is supposed to be mandatory");
795 return 1; // Well, it's supposed to be mandatory ...
796 // but sometimes it's missing : *we* assume Gray pixels
798 return atoi( strSize.c_str() );
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 File::GetPlanarConfiguration()
808 std::string strSize = GetEntryValue(0x0028,0x0006);
809 if ( strSize == GDCM_UNFOUND )
811 gdcmWarningMacro( "Not found : Planar Configuration (0028,0006)");
814 return atoi( strSize.c_str() );
818 * \brief Return the size (in bytes) of a single pixel of data.
819 * @return The size in bytes of a single pixel of data; 0 by default
820 * 0 means the file is NOT USABLE; the caller will have to check
822 int File::GetPixelSize()
824 // 0028 0100 US IMG Bits Allocated
825 // (in order no to be messed up by old ACR-NEMA RGB images)
826 // if (File::GetEntryValue(0x0028,0x0100) == "24")
829 std::string pixelType = GetPixelType();
830 if ( pixelType == "8U" || pixelType == "8S" )
834 if ( pixelType == "16U" || pixelType == "16S")
838 if ( pixelType == "32U" || pixelType == "32S")
842 if ( pixelType == "FD" )
846 gdcmWarningMacro( "Unknown pixel type");
851 * \brief Build the Pixel Type of the image.
852 * Possible values are:
853 * - 8U unsigned 8 bit,
855 * - 16U unsigned 16 bit,
856 * - 16S signed 16 bit,
857 * - 32U unsigned 32 bit,
858 * - 32S signed 32 bit,
859 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
860 * \warning 12 bit images appear as 16 bit.
861 * 24 bit images appear as 8 bit + photochromatic interp ="RGB "
862 * + Planar Configuration = 0
863 * @return 0S if nothing found. NOT USABLE file. The caller has to check
865 std::string File::GetPixelType()
867 std::string bitsAlloc = GetEntryValue(0x0028, 0x0100); // Bits Allocated
868 if ( bitsAlloc == GDCM_UNFOUND )
870 gdcmWarningMacro( "Missing Bits Allocated (0028,0100)");
871 bitsAlloc = "16"; // default and arbitrary value, not to polute the output
874 if ( bitsAlloc == "64" )
878 else if ( bitsAlloc == "12" )
880 // It will be unpacked
883 else if ( bitsAlloc == "24" )
885 // (in order no to be messed up
886 bitsAlloc = "8"; // by old RGB images)
889 std::string sign = GetEntryValue(0x0028, 0x0103);//"Pixel Representation"
891 if (sign == GDCM_UNFOUND )
893 gdcmWarningMacro( "Missing Pixel Representation (0028,0103)");
894 sign = "U"; // default and arbitrary value, not to polute the output
896 else if ( sign == "0" )
904 return bitsAlloc + sign;
908 * \brief Check whether the pixels are signed (1) or UNsigned (0) data.
909 * \warning The method defaults to false (UNsigned) when tag 0028|0103
911 * The responsability of checking this value is left to the caller
912 * (NO transformation is performed on the pixels to make then >0)
913 * @return True when signed, false when UNsigned
915 bool File::IsSignedPixelData()
917 std::string strSign = GetEntryValue( 0x0028, 0x0103 );
918 if ( strSign == GDCM_UNFOUND )
920 gdcmWarningMacro( "(0028,0103) is supposed to be mandatory");
923 int sign = atoi( strSign.c_str() );
932 * \brief Check whether this a monochrome picture (gray levels) or not,
933 * using "Photometric Interpretation" tag (0x0028,0x0004).
934 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
936 bool File::IsMonochrome()
938 const std::string &PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
939 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
940 || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
944 if ( PhotometricInterp == GDCM_UNFOUND )
946 gdcmWarningMacro( "Not found : Photometric Interpretation (0028,0004)");
952 * \brief Check whether this a MONOCHROME1 picture (high values = dark)
953 * or not using "Photometric Interpretation" tag (0x0028,0x0004).
954 * @return true when "MONOCHROME1" . False otherwise.
956 bool File::IsMonochrome1()
958 const std::string &PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
959 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1") )
963 if ( PhotometricInterp == GDCM_UNFOUND )
965 gdcmWarningMacro( "Not found : Photometric Interpretation (0028,0004)");
971 * \brief Check whether this a "PALETTE COLOR" picture or not by accessing
972 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
973 * @return true when "PALETTE COLOR". False otherwise.
975 bool File::IsPaletteColor()
977 std::string PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
978 if ( PhotometricInterp == "PALETTE COLOR " )
982 if ( PhotometricInterp == GDCM_UNFOUND )
984 gdcmWarningMacro( "Not found : Palette color (0028,0004)");
990 * \brief Check whether this a "YBR_FULL" color picture or not by accessing
991 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
992 * @return true when "YBR_FULL". False otherwise.
994 bool File::IsYBRFull()
996 std::string PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
997 if ( PhotometricInterp == "YBR_FULL" )
1001 if ( PhotometricInterp == GDCM_UNFOUND )
1003 gdcmWarningMacro( "Not found : YBR Full (0028,0004)");
1009 * \brief tells us if LUT are used
1010 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1011 * are NOT considered as LUT, since nobody knows
1012 * how to deal with them
1013 * Please warn me if you know sbdy that *does* know ... jprx
1014 * @return true if LUT Descriptors and LUT Tables were found
1018 // Check the presence of the LUT Descriptors, and LUT Tables
1020 if ( !GetDocEntry(0x0028,0x1101) )
1024 // LutDescriptorGreen
1025 if ( !GetDocEntry(0x0028,0x1102) )
1029 // LutDescriptorBlue
1030 if ( !GetDocEntry(0x0028,0x1103) )
1034 // Red Palette Color Lookup Table Data
1035 if ( !GetDocEntry(0x0028,0x1201) )
1039 // Green Palette Color Lookup Table Data
1040 if ( !GetDocEntry(0x0028,0x1202) )
1044 // Blue Palette Color Lookup Table Data
1045 if ( !GetDocEntry(0x0028,0x1203) )
1050 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1051 // NOT taken into account, but we don't know how to use it ...
1056 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1058 * @return Lookup Table number of Bits , 0 by default
1059 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1060 * @ return bit number of each LUT item
1062 int File::GetLUTNbits()
1064 std::vector<std::string> tokens;
1067 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1068 // = Lookup Table Desc-Blue
1069 // Consistency already checked in GetLUTLength
1070 std::string lutDescription = GetEntryValue(0x0028,0x1101);
1071 if ( lutDescription == GDCM_UNFOUND )
1076 tokens.clear(); // clean any previous value
1077 Util::Tokenize ( lutDescription, tokens, "\\" );
1078 //LutLength=atoi(tokens[0].c_str());
1079 //LutDepth=atoi(tokens[1].c_str());
1081 lutNbits = atoi( tokens[2].c_str() );
1088 *\brief gets the info from 0028,1052 : Rescale Intercept
1089 * @return Rescale Intercept
1091 float File::GetRescaleIntercept()
1093 float resInter = 0.;
1094 /// 0028 1052 DS IMG Rescale Intercept
1095 const std::string &strRescInter = GetEntryValue(0x0028,0x1052);
1096 if ( strRescInter != GDCM_UNFOUND )
1098 if ( sscanf( strRescInter.c_str(), "%f ", &resInter) != 1 )
1100 // bug in the element 0x0028,0x1052
1101 gdcmWarningMacro( "Rescale Intercept (0028,1052) is empty." );
1109 *\brief gets the info from 0028,1053 : Rescale Slope
1110 * @return Rescale Slope
1112 float File::GetRescaleSlope()
1114 float resSlope = 1.;
1115 //0028 1053 DS IMG Rescale Slope
1116 std::string strRescSlope = GetEntryValue(0x0028,0x1053);
1117 if ( strRescSlope != GDCM_UNFOUND )
1119 if ( sscanf( strRescSlope.c_str(), "%f ", &resSlope) != 1 )
1121 // bug in the element 0x0028,0x1053
1122 gdcmWarningMacro( "Rescale Slope (0028,1053) is empty.");
1130 * \brief This function is intended to user who doesn't want
1131 * to have to manage a LUT and expects to get an RBG Pixel image
1132 * (or a monochrome one ...)
1133 * \warning to be used with GetImagePixels()
1134 * @return 1 if Gray level, 3 if Color (RGB, YBR, *or PALETTE COLOR*)
1136 int File::GetNumberOfScalarComponents()
1138 if ( GetSamplesPerPixel() == 3 )
1143 // 0028 0100 US IMG Bits Allocated
1144 // (in order no to be messed up by old RGB images)
1145 if ( GetEntryValue(0x0028,0x0100) == "24" )
1150 std::string strPhotometricInterpretation = GetEntryValue(0x0028,0x0004);
1152 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
1154 if ( HasLUT() )// PALETTE COLOR is NOT enough
1164 // beware of trailing space at end of string
1165 // DICOM tags are never of odd length
1166 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
1167 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
1168 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
1174 // we assume that *all* kinds of YBR are dealt with
1180 * \brief This function is intended to user that DOESN'T want
1181 * to get RGB pixels image when it's stored as a PALETTE COLOR image
1182 * - the (vtk) user is supposed to know how deal with LUTs -
1183 * \warning to be used with GetImagePixelsRaw()
1184 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
1186 int File::GetNumberOfScalarComponentsRaw()
1188 // 0028 0100 US IMG Bits Allocated
1189 // (in order no to be messed up by old RGB images)
1190 if ( File::GetEntryValue(0x0028,0x0100) == "24" )
1195 // we assume that *all* kinds of YBR are dealt with
1196 return GetSamplesPerPixel();
1200 * \brief Recover the offset (from the beginning of the file)
1201 * of *image* pixels (not *icone image* pixels, if any !)
1202 * @return Pixel Offset
1204 size_t File::GetPixelOffset()
1206 DocEntry *pxlElement = GetDocEntry(GrPixel, NumPixel);
1209 return pxlElement->GetOffset();
1213 gdcmDebugMacro( "Big trouble : Pixel Element ("
1214 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found" );
1220 * \brief Recover the pixel area length (in Bytes)
1221 * @return Pixel Element Length, as stored in the header
1222 * (NOT the memory space necessary to hold the Pixels
1223 * -in case of embeded compressed image-)
1224 * 0 : NOT USABLE file. The caller has to check.
1226 size_t File::GetPixelAreaLength()
1228 DocEntry *pxlElement = GetDocEntry(GrPixel, NumPixel);
1231 return pxlElement->GetLength();
1235 gdcmDebugMacro( "Big trouble : Pixel Element ("
1236 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found" );
1242 * \brief Adds the characteristics of a new element we want to anonymize
1243 * @param group Group number of the target tag.
1244 * @param elem Element number of the target tag.
1245 * @param value new value (string) to substitute with
1247 void File::AddAnonymizeElement (uint16_t group, uint16_t elem,
1248 std::string const &value)
1254 AnonymizeList.push_back(el);
1258 * \brief Overwrites in the file the values of the DicomElements
1261 void File::AnonymizeNoLoad()
1263 std::fstream *fp = new std::fstream(Filename.c_str(),
1264 std::ios::in | std::ios::out | std::ios::binary);
1268 uint32_t valLgth = 0;
1269 std::string *spaces;
1270 for (ListElements::iterator it = AnonymizeList.begin();
1271 it != AnonymizeList.end();
1274 d = GetDocEntry( (*it).Group, (*it).Elem);
1279 if ( dynamic_cast<BinEntry *>(d)
1280 || dynamic_cast<SeqEntry *>(d) )
1283 offset = d->GetOffset();
1284 lgth = d->GetLength();
1287 spaces = new std::string( lgth-valLgth, ' ');
1288 (*it).Value = (*it).Value + *spaces;
1291 fp->seekp( offset, std::ios::beg );
1292 fp->write( (*it).Value.c_str(), lgth );
1300 * \brief anonymize a File (remove Patient's personal info passed with
1301 * AddAnonymizeElement()
1303 bool File::AnonymizeFile()
1305 // If Anonymisation list is empty, let's perform some basic anonymization
1306 if ( AnonymizeList.begin() == AnonymizeList.end() )
1308 // If exist, replace by spaces
1309 SetValEntry (" ",0x0010, 0x2154); // Telephone
1310 SetValEntry (" ",0x0010, 0x1040); // Adress
1311 SetValEntry (" ",0x0010, 0x0020); // Patient ID
1313 DocEntry* patientNameHE = GetDocEntry (0x0010, 0x0010);
1315 if ( patientNameHE ) // we replace it by Study Instance UID (why not ?)
1317 std::string studyInstanceUID = GetEntryValue (0x0020, 0x000d);
1318 if ( studyInstanceUID != GDCM_UNFOUND )
1320 SetValEntry(studyInstanceUID, 0x0010, 0x0010);
1324 SetValEntry("anonymised", 0x0010, 0x0010);
1331 for (ListElements::iterator it = AnonymizeList.begin();
1332 it != AnonymizeList.end();
1335 d = GetDocEntry( (*it).Group, (*it).Elem);
1340 if ( dynamic_cast<BinEntry *>(d)
1341 || dynamic_cast<SeqEntry *>(d) )
1344 SetValEntry ((*it).Value, (*it).Group, (*it).Elem);
1348 // In order to make definitively impossible any further identification
1349 // remove or replace all the stuff that contains a Date
1351 //0008 0012 DA ID Instance Creation Date
1352 //0008 0020 DA ID Study Date
1353 //0008 0021 DA ID Series Date
1354 //0008 0022 DA ID Acquisition Date
1355 //0008 0023 DA ID Content Date
1356 //0008 0024 DA ID Overlay Date
1357 //0008 0025 DA ID Curve Date
1358 //0008 002a DT ID Acquisition Datetime
1359 //0018 9074 DT ACQ Frame Acquisition Datetime
1360 //0018 9151 DT ACQ Frame Reference Datetime
1361 //0018 a002 DT ACQ Contribution Date Time
1362 //0020 3403 SH REL Modified Image Date (RET)
1363 //0032 0032 DA SDY Study Verified Date
1364 //0032 0034 DA SDY Study Read Date
1365 //0032 1000 DA SDY Scheduled Study Start Date
1366 //0032 1010 DA SDY Scheduled Study Stop Date
1367 //0032 1040 DA SDY Study Arrival Date
1368 //0032 1050 DA SDY Study Completion Date
1369 //0038 001a DA VIS Scheduled Admission Date
1370 //0038 001c DA VIS Scheduled Discharge Date
1371 //0038 0020 DA VIS Admitting Date
1372 //0038 0030 DA VIS Discharge Date
1373 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1374 //0040 0004 DA PRC Scheduled Procedure Step End Date
1375 //0040 0244 DA PRC Performed Procedure Step Start Date
1376 //0040 0250 DA PRC Performed Procedure Step End Date
1377 //0040 2004 DA PRC Issue Date of Imaging Service Request
1378 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1379 //0040 4011 DT PRC Expected Completion Date and Time
1380 //0040 a030 DT PRC Verification Date Time
1381 //0040 a032 DT PRC Observation Date Time
1382 //0040 a120 DT PRC DateTime
1383 //0040 a121 DA PRC Date
1384 //0040 a13a DT PRC Referenced Datetime
1385 //0070 0082 DA ??? Presentation Creation Date
1386 //0100 0420 DT ??? SOP Autorization Date and Time
1387 //0400 0105 DT ??? Digital Signature DateTime
1388 //2100 0040 DA PJ Creation Date
1389 //3006 0008 DA SSET Structure Set Date
1390 //3008 0024 DA ??? Treatment Control Point Date
1391 //3008 0054 DA ??? First Treatment Date
1392 //3008 0056 DA ??? Most Recent Treatment Date
1393 //3008 0162 DA ??? Safe Position Exit Date
1394 //3008 0166 DA ??? Safe Position Return Date
1395 //3008 0250 DA ??? Treatment Date
1396 //300a 0006 DA RT RT Plan Date
1397 //300a 022c DA RT Air Kerma Rate Reference Date
1398 //300e 0004 DA RT Review Date
1404 * \brief Performs some consistency checking on various 'File related'
1405 * (as opposed to 'DicomDir related') entries
1406 * then writes in a file all the (Dicom Elements) included the Pixels
1407 * @param fileName file name to write to
1408 * @param writetype type of the file to be written
1409 * (ACR, ExplicitVR, ImplicitVR)
1411 bool File::Write(std::string fileName, FileType writetype)
1413 std::ofstream *fp = new std::ofstream(fileName.c_str(),
1414 std::ios::out | std::ios::binary);
1417 gdcmWarningMacro("Failed to open (write) File: " << fileName.c_str());
1421 // Entry : 0002|0000 = group length -> recalculated
1422 ValEntry*e0000 = GetValEntry(0x0002,0x0000);
1425 std::ostringstream sLen;
1426 sLen << ComputeGroup0002Length(writetype);
1427 e0000->SetValue(sLen.str());
1430 int i_lgPix = GetEntryLength(GrPixel, NumPixel);
1433 // no (GrPixel, NumPixel) element
1434 std::string s_lgPix = Util::Format("%d", i_lgPix+12);
1435 s_lgPix = Util::DicomString( s_lgPix.c_str() );
1436 InsertValEntry(s_lgPix,GrPixel, 0x0000);
1439 Document::WriteContent(fp, writetype);
1447 //-----------------------------------------------------------------------------
1451 //-----------------------------------------------------------------------------
1454 * \brief Parse pixel data from disk of [multi-]fragment RLE encoding.
1455 * Compute the RLE extra information and store it in \ref RLEInfo
1456 * for later pixel retrieval usage.
1458 void File::ComputeRLEInfo()
1460 std::string ts = GetTransferSyntax();
1461 if ( !Global::GetTS()->IsRLELossless(ts) )
1466 // Encoded pixel data: for the time being we are only concerned with
1467 // Jpeg or RLE Pixel data encodings.
1468 // As stated in PS 3.5-2003, section 8.2 p44:
1469 // "If sent in Encapsulated Format (i.e. other than the Native Format) the
1470 // value representation OB is used".
1471 // Hence we expect an OB value representation. Concerning OB VR,
1472 // the section PS 3.5-2003, section A.4.c p 58-59, states:
1473 // "For the Value Representations OB and OW, the encoding shall meet the
1474 // following specifications depending on the Data element tag:"
1476 // - the first item in the sequence of items before the encoded pixel
1477 // data stream shall be basic offset table item. The basic offset table
1478 // item value, however, is not required to be present"
1479 ReadAndSkipEncapsulatedBasicOffsetTable();
1481 // Encapsulated RLE Compressed Images (see PS 3.5-2003, Annex G)
1482 // Loop on the individual frame[s] and store the information
1483 // on the RLE fragments in a RLEFramesInfo.
1484 // Note: - when only a single frame is present, this is a
1486 // - when more than one frame are present, then we are in
1487 // the case of a multi-frame image.
1489 while ( (frameLength = ReadTagLength(0xfffe, 0xe000)) != 0 )
1491 // Parse the RLE Header and store the corresponding RLE Segment
1492 // Offset Table information on fragments of this current Frame.
1493 // Note that the fragment pixels themselves are not loaded
1494 // (but just skipped).
1495 long frameOffset = Fp->tellg();
1497 uint32_t nbRleSegments = ReadInt32();
1498 if ( nbRleSegments > 16 )
1500 // There should be at most 15 segments (refer to RLEFrame class)
1501 gdcmWarningMacro( "Too many segments.");
1504 uint32_t rleSegmentOffsetTable[16];
1505 for( int k = 1; k <= 15; k++ )
1507 rleSegmentOffsetTable[k] = ReadInt32();
1510 // Deduce from both RLE Header and frameLength
1511 // the fragment length, and again store this info
1512 // in a RLEFramesInfo.
1513 long rleSegmentLength[15];
1514 // skipping (not reading) RLE Segments
1515 if ( nbRleSegments > 1)
1517 for(unsigned int k = 1; k <= nbRleSegments-1; k++)
1519 rleSegmentLength[k] = rleSegmentOffsetTable[k+1]
1520 - rleSegmentOffsetTable[k];
1521 SkipBytes(rleSegmentLength[k]);
1525 rleSegmentLength[nbRleSegments] = frameLength
1526 - rleSegmentOffsetTable[nbRleSegments];
1527 SkipBytes(rleSegmentLength[nbRleSegments]);
1529 // Store the collected info
1530 RLEFrame *newFrame = new RLEFrame;
1531 newFrame->SetNumberOfFragments(nbRleSegments);
1532 for( unsigned int uk = 1; uk <= nbRleSegments; uk++ )
1534 newFrame->SetOffset(uk,frameOffset + rleSegmentOffsetTable[uk]);
1535 newFrame->SetLength(uk,rleSegmentLength[uk]);
1537 RLEInfo->AddFrame(newFrame);
1540 // Make sure that we encounter a 'Sequence Delimiter Item'
1541 // at the end of the item :
1542 if ( !ReadTag(0xfffe, 0xe0dd) )
1544 gdcmWarningMacro( "No sequence delimiter item at end of RLE item sequence");
1549 * \brief Parse pixel data from disk of [multi-]fragment Jpeg encoding.
1550 * Compute the jpeg extra information (fragment[s] offset[s] and
1551 * length) and store it[them] in \ref JPEGInfo for later pixel
1554 void File::ComputeJPEGFragmentInfo()
1556 // If you need to, look for comments of ComputeRLEInfo().
1557 std::string ts = GetTransferSyntax();
1558 if ( ! Global::GetTS()->IsJPEG(ts) )
1563 ReadAndSkipEncapsulatedBasicOffsetTable();
1565 // Loop on the fragments[s] and store the parsed information in a
1567 long fragmentLength;
1568 while ( (fragmentLength = ReadTagLength(0xfffe, 0xe000)) != 0 )
1570 long fragmentOffset = Fp->tellg();
1572 // Store the collected info
1573 JPEGFragment *newFragment = new JPEGFragment;
1574 newFragment->SetOffset(fragmentOffset);
1575 newFragment->SetLength(fragmentLength);
1576 JPEGInfo->AddFragment(newFragment);
1578 SkipBytes(fragmentLength);
1581 // Make sure that we encounter a 'Sequence Delimiter Item'
1582 // at the end of the item :
1583 if ( !ReadTag(0xfffe, 0xe0dd) )
1585 gdcmWarningMacro( "No sequence delimiter item at end of JPEG item sequence");
1590 * \brief Assuming the internal file pointer \ref Document::Fp
1591 * is placed at the beginning of a tag check whether this
1592 * tag is (TestGroup, TestElem).
1593 * \warning On success the internal file pointer \ref Document::Fp
1594 * is modified to point after the tag.
1595 * On failure (i.e. when the tag wasn't the expected tag
1596 * (TestGroup, TestElem) the internal file pointer
1597 * \ref Document::Fp is restored to it's original position.
1598 * @param testGroup The expected group of the tag.
1599 * @param testElem The expected Element of the tag.
1600 * @return True on success, false otherwise.
1602 bool File::ReadTag(uint16_t testGroup, uint16_t testElem)
1604 long positionOnEntry = Fp->tellg();
1605 long currentPosition = Fp->tellg(); // On debugging purposes
1607 // Read the Item Tag group and element, and make
1608 // sure they are what we expected:
1609 uint16_t itemTagGroup;
1610 uint16_t itemTagElem;
1613 itemTagGroup = ReadInt16();
1614 itemTagElem = ReadInt16();
1616 catch ( FormatError e )
1618 //std::cerr << e << std::endl;
1621 if ( itemTagGroup != testGroup || itemTagElem != testElem )
1623 gdcmWarningMacro( "Wrong Item Tag found:"
1624 << " We should have found tag ("
1625 << std::hex << testGroup << "," << testElem << ")" << std::endl
1626 << " but instead we encountered tag ("
1627 << std::hex << itemTagGroup << "," << itemTagElem << ")"
1628 << " at address: " << " 0x(" << (unsigned int)currentPosition << ")"
1630 Fp->seekg(positionOnEntry, std::ios::beg);
1638 * \brief Assuming the internal file pointer \ref Document::Fp
1639 * is placed at the beginning of a tag (TestGroup, TestElement),
1640 * read the length associated to the Tag.
1641 * \warning On success the internal file pointer \ref Document::Fp
1642 * is modified to point after the tag and it's length.
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 testElem The expected Element of the tag.
1648 * @return On success returns the length associated to the tag. On failure
1651 uint32_t File::ReadTagLength(uint16_t testGroup, uint16_t testElem)
1654 if ( !ReadTag(testGroup, testElem) )
1659 //// Then read the associated Item Length
1660 long currentPosition = Fp->tellg();
1661 uint32_t itemLength = ReadInt32();
1663 gdcmWarningMacro( "Basic Item Length is: "
1664 << itemLength << std::endl
1665 << " at address: " << std::hex << (unsigned int)currentPosition);
1671 * \brief When parsing the Pixel Data of an encapsulated file, read
1672 * the basic offset table (when present, and BTW dump it).
1674 void File::ReadAndSkipEncapsulatedBasicOffsetTable()
1676 //// Read the Basic Offset Table Item Tag length...
1677 uint32_t itemLength = ReadTagLength(0xfffe, 0xe000);
1679 // When present, read the basic offset table itself.
1680 // Notes: - since the presence of this basic offset table is optional
1681 // we can't rely on it for the implementation, and we will simply
1682 // trash it's content (when present).
1683 // - still, when present, we could add some further checks on the
1684 // lengths, but we won't bother with such fuses for the time being.
1685 if ( itemLength != 0 )
1687 char *basicOffsetTableItemValue = new char[itemLength + 1];
1688 Fp->read(basicOffsetTableItemValue, itemLength);
1691 for (unsigned int i=0; i < itemLength; i += 4 )
1693 uint32_t individualLength = str2num( &basicOffsetTableItemValue[i],
1695 gdcmWarningMacro( "Read one length: " <<
1696 std::hex << individualLength );
1700 delete[] basicOffsetTableItemValue;
1704 //-----------------------------------------------------------------------------
1707 //-----------------------------------------------------------------------------
1708 } // end namespace gdcm