1 /*=========================================================================
4 Module: $RCSfile: gdcmFile.cxx,v $
6 Date: $Date: 2005/07/19 14:43:48 $
7 Version: $Revision: 1.250 $
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;
73 Load( filename ); // gdcm::Document is first Loaded, then the 'File part'
77 * \brief Canonical destructor.
87 //-----------------------------------------------------------------------------
91 * @return false if file cannot be open or no swap info was found,
92 * or no tag was found.
96 if ( ! this->Document::Load( ) )
99 return DoTheLoadingJob( );
103 * \brief Does the Loading Job (internal use only)
104 * @return false if file cannot be open or no swap info was found,
105 * or no tag was found.
107 bool File::DoTheLoadingJob( )
110 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
111 // We may encounter the 'RETired' (0x0028, 0x0200) tag
112 // (Image Location") . This entry contains the number of
113 // the group that contains the pixel data (hence the "Pixel Data"
114 // is found by indirection through the "Image Location").
115 // Inside the group pointed by "Image Location" the searched element
116 // is conventionally the element 0x0010 (when the norm is respected).
117 // When the "Image Location" is missing we default to group 0x7fe0.
118 // Note: this IS the right place for the code
121 const std::string &imgLocation = GetEntryValue(0x0028, 0x0200);
122 if ( imgLocation == GDCM_UNFOUND )
129 GrPixel = (uint16_t) atoi( imgLocation.c_str() );
132 // sometimes Image Location value doesn't follow
133 // the supposed processor endianness.
134 // see gdcmData/cr172241.dcm
135 if ( GrPixel == 0xe07f )
140 if ( GrPixel != 0x7fe0 )
142 // This is a kludge for old dirty Philips imager.
150 // Now, we know GrPixel and NumPixel.
151 // Let's create a VirtualDictEntry to allow a further VR modification
152 // and force VR to match with BitsAllocated.
153 DocEntry *entry = GetDocEntry(GrPixel, NumPixel);
156 // Compute the RLE or JPEG info
158 const std::string &ts = GetTransferSyntax();
159 Fp->seekg( entry->GetOffset(), std::ios::beg );
160 if ( Global::GetTS()->IsRLELossless(ts) )
162 else if ( Global::GetTS()->IsJPEG(ts) )
163 ComputeJPEGFragmentInfo();
166 // Create a new BinEntry to change the DictEntry
167 // The changed DictEntry will have
168 // - a correct PixelVR OB or OW)
169 // - the name to "Pixel Data"
170 BinEntry *oldEntry = dynamic_cast<BinEntry *>(entry);
174 // 8 bits allocated is a 'O Bytes' , as well as 24 (old ACR-NEMA RGB)
175 // more than 8 (i.e 12, 16) is a 'O Words'
176 if ( GetBitsAllocated() == 8 || GetBitsAllocated() == 24 )
181 // Change only made if usefull
182 if ( PixelVR != oldEntry->GetVR() )
184 DictEntry* newDict = NewVirtualDictEntry(GrPixel,NumPixel,
185 PixelVR,"1","Pixel Data");
187 BinEntry *newEntry = new BinEntry(newDict);
188 newEntry->Copy(entry);
189 newEntry->SetBinArea(oldEntry->GetBinArea(),oldEntry->IsSelfArea());
190 oldEntry->SetSelfArea(false);
192 RemoveEntry(oldEntry);
200 * \brief This predicate, based on hopefully reasonable heuristics,
201 * decides whether or not the current File was properly parsed
202 * and contains the mandatory information for being considered as
203 * a well formed and usable Dicom/Acr File.
204 * @return true when File is the one of a reasonable Dicom/Acr file,
207 bool File::IsReadable()
209 if ( !Document::IsReadable() )
214 const std::string &res = GetEntryValue(0x0028, 0x0005);
215 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
217 gdcmWarningMacro("Wrong Image Dimensions" << res);
218 return false; // Image Dimensions
220 bool b0028_0100 = true;
221 if ( !GetDocEntry(0x0028, 0x0100) )
223 gdcmWarningMacro("Bits Allocated (0028|0100) not found");
224 //return false; // "Bits Allocated"
227 bool b0028_0101 = true;
228 if ( !GetDocEntry(0x0028, 0x0101) )
230 gdcmWarningMacro("Bits Stored (0028|0101) not found");
231 //return false; // "Bits Stored"
234 bool b0028_0102 = true;
235 if ( !GetDocEntry(0x0028, 0x0102) )
237 gdcmWarningMacro("Hight Bit (0028|0102) not found");
238 //return false; // "High Bit"
241 bool b0028_0103 = true;
242 if ( !GetDocEntry(0x0028, 0x0103) )
244 gdcmWarningMacro("Pixel Representation (0028|0103) not found");
245 //return false; // "Pixel Representation" i.e. 'Sign' ( 0 : unsigned, 1 : signed)
249 if ( !b0028_0100 && !b0028_0101 && !b0028_0102 && !b0028_0103)
251 gdcmWarningMacro("Too much mandatory Tags missing !");
255 if ( !GetDocEntry(GrPixel, NumPixel) )
257 gdcmWarningMacro("Pixel Dicom Element " << std::hex <<
258 GrPixel << "|" << NumPixel << "not found");
259 return false; // Pixel Dicom Element not found :-(
265 * \brief gets the info from 0020,0013 : Image Number else 0.
266 * @return image number
268 int File::GetImageNumber()
270 //0020 0013 : Image Number
271 std::string strImNumber = GetEntryValue(0x0020,0x0013);
272 if ( strImNumber != GDCM_UNFOUND )
274 return atoi( strImNumber.c_str() );
280 * \brief gets the info from 0008,0060 : Modality
281 * @return Modality Type
283 ModalityType File::GetModality()
285 // 0008 0060 : Modality
286 std::string strModality = GetEntryValue(0x0008,0x0060);
287 if ( strModality != GDCM_UNFOUND )
289 if ( strModality.find("AU") < strModality.length()) return AU;
290 else if ( strModality.find("AS") < strModality.length()) return AS;
291 else if ( strModality.find("BI") < strModality.length()) return BI;
292 else if ( strModality.find("CF") < strModality.length()) return CF;
293 else if ( strModality.find("CP") < strModality.length()) return CP;
294 else if ( strModality.find("CR") < strModality.length()) return CR;
295 else if ( strModality.find("CT") < strModality.length()) return CT;
296 else if ( strModality.find("CS") < strModality.length()) return CS;
297 else if ( strModality.find("DD") < strModality.length()) return DD;
298 else if ( strModality.find("DF") < strModality.length()) return DF;
299 else if ( strModality.find("DG") < strModality.length()) return DG;
300 else if ( strModality.find("DM") < strModality.length()) return DM;
301 else if ( strModality.find("DS") < strModality.length()) return DS;
302 else if ( strModality.find("DX") < strModality.length()) return DX;
303 else if ( strModality.find("ECG") < strModality.length()) return ECG;
304 else if ( strModality.find("EPS") < strModality.length()) return EPS;
305 else if ( strModality.find("FA") < strModality.length()) return FA;
306 else if ( strModality.find("FS") < strModality.length()) return FS;
307 else if ( strModality.find("HC") < strModality.length()) return HC;
308 else if ( strModality.find("HD") < strModality.length()) return HD;
309 else if ( strModality.find("LP") < strModality.length()) return LP;
310 else if ( strModality.find("LS") < strModality.length()) return LS;
311 else if ( strModality.find("MA") < strModality.length()) return MA;
312 else if ( strModality.find("MR") < strModality.length()) return MR;
313 else if ( strModality.find("NM") < strModality.length()) return NM;
314 else if ( strModality.find("OT") < strModality.length()) return OT;
315 else if ( strModality.find("PT") < strModality.length()) return PT;
316 else if ( strModality.find("RF") < strModality.length()) return RF;
317 else if ( strModality.find("RG") < strModality.length()) return RG;
318 else if ( strModality.find("RTDOSE")
319 < strModality.length()) return RTDOSE;
320 else if ( strModality.find("RTIMAGE")
321 < strModality.length()) return RTIMAGE;
322 else if ( strModality.find("RTPLAN")
323 < strModality.length()) return RTPLAN;
324 else if ( strModality.find("RTSTRUCT")
325 < strModality.length()) return RTSTRUCT;
326 else if ( strModality.find("SM") < strModality.length()) return SM;
327 else if ( strModality.find("ST") < strModality.length()) return ST;
328 else if ( strModality.find("TG") < strModality.length()) return TG;
329 else if ( strModality.find("US") < strModality.length()) return US;
330 else if ( strModality.find("VF") < strModality.length()) return VF;
331 else if ( strModality.find("XA") < strModality.length()) return XA;
332 else if ( strModality.find("XC") < strModality.length()) return XC;
336 /// \todo throw error return value ???
337 /// specified <> unknown in our database
345 * \brief Retrieve the number of columns of image.
346 * @return The encountered size when found, 0 by default.
347 * 0 means the file is NOT USABLE. The caller will have to check
351 const std::string &strSize = GetEntryValue(0x0028,0x0011);
352 if ( strSize == GDCM_UNFOUND )
356 return atoi( strSize.c_str() );
360 * \brief Retrieve the number of lines of image.
361 * \warning The defaulted value is 1 as opposed to File::GetXSize()
362 * @return The encountered size when found, 1 by default
363 * (The ACR-NEMA file contains a Signal, not an Image).
367 const std::string &strSize = GetEntryValue(0x0028,0x0010);
368 if ( strSize != GDCM_UNFOUND )
370 return atoi( strSize.c_str() );
377 // The Rows (0028,0010) entry was optional for ACR/NEMA.
378 // (at least some images didn't have it.)
379 // It might hence be a signal (1D image). So we default to 1:
384 * \brief Retrieve the number of planes of volume or the number
385 * of frames of a multiframe.
386 * \warning When present we consider the "Number of Frames" as the third
387 * dimension. When missing we consider the third dimension as
388 * being the ACR-NEMA "Planes" tag content.
389 * @return The encountered size when found, 1 by default (single image).
393 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
394 // as the third dimension.
395 const std::string &strSize = GetEntryValue(0x0028,0x0008);
396 if ( strSize != GDCM_UNFOUND )
398 return atoi( strSize.c_str() );
401 // We then consider the "Planes" entry as the third dimension
402 const std::string &strSize2 = GetEntryValue(0x0028,0x0012);
403 if ( strSize2 != GDCM_UNFOUND )
405 return atoi( strSize2.c_str() );
411 * \brief gets the info from 0018,1164 : ImagerPixelSpacing
412 * then 0028,0030 : Pixel Spacing
414 * @return X dimension of a pixel
416 float File::GetXSpacing()
418 float xspacing = 1.0;
419 float yspacing = 1.0;
422 // To follow David Clunie's advice, we first check ImagerPixelSpacing
424 const std::string &strImagerPixelSpacing = GetEntryValue(0x0018,0x1164);
425 if ( strImagerPixelSpacing != GDCM_UNFOUND )
427 if ( ( nbValues = sscanf( strImagerPixelSpacing.c_str(),
428 "%f\\%f", &yspacing, &xspacing)) != 2 )
430 // if no values, xspacing is set to 1.0
433 // if single value is found, xspacing is defaulted to yspacing
437 if ( xspacing == 0.0 )
444 const std::string &strSpacing = GetEntryValue(0x0028,0x0030);
446 if ( strSpacing == GDCM_UNFOUND )
448 gdcmWarningMacro( "Unfound Pixel Spacing (0028,0030)" );
452 if ( ( nbValues = sscanf( strSpacing.c_str(),
453 "%f \\%f ", &yspacing, &xspacing)) != 2 )
455 // if no values, xspacing is set to 1.0
458 // if single value is found, xspacing is defaulted to yspacing
462 if ( xspacing == 0.0 )
468 // to avoid troubles with David Clunie's-like images (at least one)
469 if ( xspacing == 0. && yspacing == 0.)
474 gdcmWarningMacro("gdcmData/CT-MONO2-8-abdo.dcm-like problem");
475 // seems to be a bug in the header ...
476 nbValues = sscanf( strSpacing.c_str(), "%f \\0\\%f ", &yspacing, &xspacing);
477 gdcmAssertMacro( nbValues == 2 );
484 * \brief gets the info from 0018,1164 : ImagerPixelSpacing
485 * then from 0028,0030 : Pixel Spacing
487 * @return Y dimension of a pixel
489 float File::GetYSpacing()
493 // To follow David Clunie's advice, we first check ImagerPixelSpacing
495 const std::string &strImagerPixelSpacing = GetEntryValue(0x0018,0x1164);
496 if ( strImagerPixelSpacing != GDCM_UNFOUND )
498 nbValues = sscanf( strImagerPixelSpacing.c_str(), "%f", &yspacing);
500 // if sscanf cannot read any float value, it won't affect yspacing
504 if ( yspacing == 0.0 )
510 std::string strSpacing = GetEntryValue(0x0028,0x0030);
511 if ( strSpacing == GDCM_UNFOUND )
513 gdcmWarningMacro("Unfound Pixel Spacing (0028,0030)");
517 // if sscanf cannot read any float value, it won't affect yspacing
518 nbValues = sscanf( strSpacing.c_str(), "%f", &yspacing);
520 // if no values, yspacing is set to 1.0
524 if ( yspacing == 0.0 )
531 * \brief gets the info from 0018,0088 : Space Between Slices
532 * else from 0018,0050 : Slice Thickness
534 * @return Z dimension of a voxel-to be
536 float File::GetZSpacing()
538 // Spacing Between Slices : distance between the middle of 2 slices
540 // jointives (Spacing between Slices = Slice Thickness)
541 // overlapping (Spacing between Slices < Slice Thickness)
542 // disjointes (Spacing between Slices > Slice Thickness)
543 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
544 // It only concerns the MRI guys, not people wanting to visualize volumes
545 // If Spacing Between Slices is missing,
546 // we suppose slices joint together
548 const std::string &strSpacingBSlices = GetEntryValue(0x0018,0x0088);
550 if ( strSpacingBSlices == GDCM_UNFOUND )
552 gdcmWarningMacro("Unfound Spacing Between Slices (0018,0088)");
553 const std::string &strSliceThickness = GetEntryValue(0x0018,0x0050);
554 if ( strSliceThickness == GDCM_UNFOUND )
556 gdcmWarningMacro("Unfound Slice Thickness (0018,0050)");
561 // if no 'Spacing Between Slices' is found,
562 // we assume slices join together
563 // (no overlapping, no interslice gap)
564 // if they don't, we're fucked up
565 return (float)atof( strSliceThickness.c_str() );
569 return (float)atof( strSpacingBSlices.c_str() );
573 * \brief gets the info from 0020,0032 : Image Position Patient
574 * else from 0020,0030 : Image Position (RET)
576 * @return up-left image corner X position
578 float File::GetXOrigin()
580 float xImPos, yImPos, zImPos;
581 std::string strImPos = GetEntryValue(0x0020,0x0032);
583 if ( strImPos == GDCM_UNFOUND )
585 gdcmWarningMacro( "Unfound Image Position Patient (0020,0032)");
586 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
587 if ( strImPos == GDCM_UNFOUND )
589 gdcmWarningMacro( "Unfound Image Position (RET) (0020,0030)");
594 if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos) != 3 )
603 * \brief gets the info from 0020,0032 : Image Position Patient
604 * else from 0020,0030 : Image Position (RET)
606 * @return up-left image corner Y position
608 float File::GetYOrigin()
610 float xImPos, yImPos, zImPos;
611 std::string strImPos = GetEntryValue(0x0020,0x0032);
613 if ( strImPos == GDCM_UNFOUND)
615 gdcmWarningMacro( "Unfound Image Position Patient (0020,0032)");
616 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
617 if ( strImPos == GDCM_UNFOUND )
619 gdcmWarningMacro( "Unfound Image Position (RET) (0020,0030)");
624 if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos) != 3 )
633 * \brief gets the info from 0020,0032 : Image Position Patient
634 * else from 0020,0030 : Image Position (RET)
635 * else from 0020,1041 : Slice Location
636 * else from 0020,0050 : Location
638 * @return up-left image corner Z position
640 float File::GetZOrigin()
642 float xImPos, yImPos, zImPos;
643 std::string strImPos = GetEntryValue(0x0020,0x0032);
645 if ( strImPos != GDCM_UNFOUND )
647 if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos) != 3)
649 gdcmWarningMacro( "Wrong Image Position Patient (0020,0032)");
650 return 0.; // bug in the element 0x0020,0x0032
658 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
659 if ( strImPos != GDCM_UNFOUND )
661 if ( sscanf( strImPos.c_str(),
662 "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos ) != 3 )
664 gdcmWarningMacro( "Wrong Image Position (RET) (0020,0030)");
665 return 0.; // bug in the element 0x0020,0x0032
673 // for *very* old ACR-NEMA images
674 std::string strSliceLocation = GetEntryValue(0x0020,0x1041);
675 if ( strSliceLocation != GDCM_UNFOUND )
677 if ( sscanf( strSliceLocation.c_str(), "%f ", &zImPos) != 1)
679 gdcmWarningMacro( "Wrong Slice Location (0020,1041)");
680 return 0.; // bug in the element 0x0020,0x1041
687 gdcmWarningMacro( "Unfound Slice Location (0020,1041)");
689 std::string strLocation = GetEntryValue(0x0020,0x0050);
690 if ( strLocation != GDCM_UNFOUND )
692 if ( sscanf( strLocation.c_str(), "%f ", &zImPos) != 1 )
694 gdcmWarningMacro( "Wrong Location (0020,0050)");
695 return 0.; // bug in the element 0x0020,0x0050
702 gdcmWarningMacro( "Unfound Location (0020,0050)");
704 return 0.; // Hopeless
708 * \brief gets the info from 0020,0037 : Image Orientation Patient
709 * (needed to organize DICOM files based on their x,y,z position)
710 * @param iop adress of the (6)float array to receive values
711 * @return cosines of image orientation patient
713 void File::GetImageOrientationPatient( float iop[6] )
715 std::string strImOriPat;
716 //iop is supposed to be float[6]
717 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
719 // 0020 0037 DS REL Image Orientation (Patient)
720 if ( (strImOriPat = GetEntryValue(0x0020,0x0037)) != 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,0037). Less than 6 values were found." );
729 // 0020 0035 DS REL Image Orientation (RET)
730 else if ( (strImOriPat = GetEntryValue(0x0020,0x0035)) != GDCM_UNFOUND )
732 if ( sscanf( strImOriPat.c_str(), "%f \\ %f \\%f \\%f \\%f \\%f ",
733 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
735 gdcmWarningMacro( "wrong Image Orientation Patient (0020,0035). Less than 6 values were found." );
741 * \brief Retrieve the number of Bits Stored (actually used)
742 * (as opposed to number of Bits Allocated)
743 * @return The encountered number of Bits Stored, 0 by default.
744 * 0 means the file is NOT USABLE. The caller has to check it !
746 int File::GetBitsStored()
748 std::string strSize = GetEntryValue( 0x0028, 0x0101 );
749 if ( strSize == GDCM_UNFOUND )
751 gdcmWarningMacro("(0028,0101) is supposed to be mandatory");
752 return 0; // It's supposed to be mandatory
753 // the caller will have to check
755 return atoi( strSize.c_str() );
759 * \brief Retrieve the number of Bits Allocated
760 * (8, 12 -compacted ACR-NEMA files-, 16, ...)
761 * @return The encountered number of Bits Allocated, 0 by default.
762 * 0 means the file is NOT USABLE. The caller has to check it !
764 int File::GetBitsAllocated()
766 std::string strSize = GetEntryValue(0x0028,0x0100);
767 if ( strSize == GDCM_UNFOUND )
769 gdcmWarningMacro( "(0028,0100) is supposed to be mandatory");
770 return 0; // It's supposed to be mandatory
771 // the caller will have to check
773 return atoi( strSize.c_str() );
777 * \brief Retrieve the high bit position.
778 * \warning The method defaults to 0 when information is missing.
779 * The responsability of checking this value is left to the caller.
780 * @return The high bit position when present. 0 when missing.
782 int File::GetHighBitPosition()
784 std::string strSize = GetEntryValue( 0x0028, 0x0102 );
785 if ( strSize == GDCM_UNFOUND )
787 gdcmWarningMacro( "(0028,0102) is supposed to be mandatory");
790 return atoi( strSize.c_str() );
794 * \brief Retrieve the number of Samples Per Pixel
795 * (1 : gray level, 3 : RGB/YBR -1 or 3 Planes-)
796 * @return The encountered number of Samples Per Pixel, 1 by default.
797 * (we assume Gray level Pixels)
799 int File::GetSamplesPerPixel()
801 const std::string &strSize = GetEntryValue(0x0028,0x0002);
802 if ( strSize == GDCM_UNFOUND )
804 gdcmWarningMacro( "(0028,0002) is supposed to be mandatory");
805 return 1; // Well, it's supposed to be mandatory ...
806 // but sometimes it's missing : *we* assume Gray pixels
808 return atoi( strSize.c_str() );
812 * \brief Retrieve the Planar Configuration for RGB images
813 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
814 * @return The encountered Planar Configuration, 0 by default.
816 int File::GetPlanarConfiguration()
818 std::string strSize = GetEntryValue(0x0028,0x0006);
819 if ( strSize == GDCM_UNFOUND )
821 gdcmWarningMacro( "Not found : Planar Configuration (0028,0006)");
824 return atoi( strSize.c_str() );
828 * \brief Return the size (in bytes) of a single pixel of data.
829 * @return The size in bytes of a single pixel of data; 0 by default
830 * 0 means the file is NOT USABLE; the caller will have to check
832 int File::GetPixelSize()
834 // 0028 0100 US IMG Bits Allocated
835 // (in order no to be messed up by old ACR-NEMA RGB images)
836 // if (File::GetEntryValue(0x0028,0x0100) == "24")
839 std::string pixelType = GetPixelType();
840 if ( pixelType == "8U" || pixelType == "8S" )
844 if ( pixelType == "16U" || pixelType == "16S")
848 if ( pixelType == "32U" || pixelType == "32S")
852 if ( pixelType == "FD" )
856 gdcmWarningMacro( "Unknown pixel type");
861 * \brief Build the Pixel Type of the image.
862 * Possible values are:
863 * - 8U unsigned 8 bit,
865 * - 16U unsigned 16 bit,
866 * - 16S signed 16 bit,
867 * - 32U unsigned 32 bit,
868 * - 32S signed 32 bit,
869 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
870 * \warning 12 bit images appear as 16 bit.
871 * 24 bit images appear as 8 bit + photochromatic interp ="RGB "
872 * + Planar Configuration = 0
873 * @return 0S if nothing found. NOT USABLE file. The caller has to check
875 std::string File::GetPixelType()
877 std::string bitsAlloc = GetEntryValue(0x0028, 0x0100); // Bits Allocated
878 if ( bitsAlloc == GDCM_UNFOUND )
880 gdcmWarningMacro( "Missing Bits Allocated (0028,0100)");
881 bitsAlloc = "16"; // default and arbitrary value, not to polute the output
884 if ( bitsAlloc == "64" )
888 else if ( bitsAlloc == "12" )
890 // It will be unpacked
893 else if ( bitsAlloc == "24" )
895 // (in order no to be messed up
896 bitsAlloc = "8"; // by old RGB images)
899 std::string sign = GetEntryValue(0x0028, 0x0103);//"Pixel Representation"
901 if (sign == GDCM_UNFOUND )
903 gdcmWarningMacro( "Missing Pixel Representation (0028,0103)");
904 sign = "U"; // default and arbitrary value, not to polute the output
906 else if ( sign == "0" )
914 return bitsAlloc + sign;
918 * \brief Check whether the pixels are signed (1) or UNsigned (0) data.
919 * \warning The method defaults to false (UNsigned) when tag 0028|0103
921 * The responsability of checking this value is left to the caller
922 * (NO transformation is performed on the pixels to make then >0)
923 * @return True when signed, false when UNsigned
925 bool File::IsSignedPixelData()
927 std::string strSign = GetEntryValue( 0x0028, 0x0103 );
928 if ( strSign == GDCM_UNFOUND )
930 gdcmWarningMacro( "(0028,0103) is supposed to be mandatory");
933 int sign = atoi( strSign.c_str() );
942 * \brief Check whether this a monochrome picture (gray levels) or not,
943 * using "Photometric Interpretation" tag (0x0028,0x0004).
944 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
946 bool File::IsMonochrome()
948 const std::string &PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
949 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
950 || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
954 if ( PhotometricInterp == GDCM_UNFOUND )
956 gdcmWarningMacro( "Not found : Photometric Interpretation (0028,0004)");
962 * \brief Check whether this a MONOCHROME1 picture (high values = dark)
963 * or not using "Photometric Interpretation" tag (0x0028,0x0004).
964 * @return true when "MONOCHROME1" . False otherwise.
966 bool File::IsMonochrome1()
968 const std::string &PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
969 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1") )
973 if ( PhotometricInterp == GDCM_UNFOUND )
975 gdcmWarningMacro( "Not found : Photometric Interpretation (0028,0004)");
981 * \brief Check whether this a "PALETTE COLOR" picture or not by accessing
982 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
983 * @return true when "PALETTE COLOR". False otherwise.
985 bool File::IsPaletteColor()
987 std::string PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
988 if ( PhotometricInterp == "PALETTE COLOR " )
992 if ( PhotometricInterp == GDCM_UNFOUND )
994 gdcmWarningMacro( "Not found : Palette color (0028,0004)");
1000 * \brief Check whether this a "YBR_FULL" color picture or not by accessing
1001 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
1002 * @return true when "YBR_FULL". False otherwise.
1004 bool File::IsYBRFull()
1006 std::string PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
1007 if ( PhotometricInterp == "YBR_FULL" )
1011 if ( PhotometricInterp == GDCM_UNFOUND )
1013 gdcmWarningMacro( "Not found : YBR Full (0028,0004)");
1019 * \brief tells us if LUT are used
1020 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1021 * are NOT considered as LUT, since nobody knows
1022 * how to deal with them
1023 * Please warn me if you know sbdy that *does* know ... jprx
1024 * @return true if LUT Descriptors and LUT Tables were found
1028 // Check the presence of the LUT Descriptors, and LUT Tables
1030 if ( !GetDocEntry(0x0028,0x1101) )
1034 // LutDescriptorGreen
1035 if ( !GetDocEntry(0x0028,0x1102) )
1039 // LutDescriptorBlue
1040 if ( !GetDocEntry(0x0028,0x1103) )
1044 // Red Palette Color Lookup Table Data
1045 if ( !GetDocEntry(0x0028,0x1201) )
1049 // Green Palette Color Lookup Table Data
1050 if ( !GetDocEntry(0x0028,0x1202) )
1054 // Blue Palette Color Lookup Table Data
1055 if ( !GetDocEntry(0x0028,0x1203) )
1060 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1061 // NOT taken into account, but we don't know how to use it ...
1066 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1068 * @return Lookup Table number of Bits , 0 by default
1069 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1070 * @ return bit number of each LUT item
1072 int File::GetLUTNbits()
1074 std::vector<std::string> tokens;
1077 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1078 // = Lookup Table Desc-Blue
1079 // Consistency already checked in GetLUTLength
1080 std::string lutDescription = GetEntryValue(0x0028,0x1101);
1081 if ( lutDescription == GDCM_UNFOUND )
1086 tokens.clear(); // clean any previous value
1087 Util::Tokenize ( lutDescription, tokens, "\\" );
1088 //LutLength=atoi(tokens[0].c_str());
1089 //LutDepth=atoi(tokens[1].c_str());
1091 lutNbits = atoi( tokens[2].c_str() );
1098 *\brief gets the info from 0028,1052 : Rescale Intercept
1099 * @return Rescale Intercept
1101 float File::GetRescaleIntercept()
1103 float resInter = 0.;
1104 /// 0028 1052 DS IMG Rescale Intercept
1105 const std::string &strRescInter = GetEntryValue(0x0028,0x1052);
1106 if ( strRescInter != GDCM_UNFOUND )
1108 if ( sscanf( strRescInter.c_str(), "%f ", &resInter) != 1 )
1110 // bug in the element 0x0028,0x1052
1111 gdcmWarningMacro( "Rescale Intercept (0028,1052) is empty." );
1119 *\brief gets the info from 0028,1053 : Rescale Slope
1120 * @return Rescale Slope
1122 float File::GetRescaleSlope()
1124 float resSlope = 1.;
1125 //0028 1053 DS IMG Rescale Slope
1126 std::string strRescSlope = GetEntryValue(0x0028,0x1053);
1127 if ( strRescSlope != GDCM_UNFOUND )
1129 if ( sscanf( strRescSlope.c_str(), "%f ", &resSlope) != 1 )
1131 // bug in the element 0x0028,0x1053
1132 gdcmWarningMacro( "Rescale Slope (0028,1053) is empty.");
1140 * \brief This function is intended to user who doesn't want
1141 * to have to manage a LUT and expects to get an RBG Pixel image
1142 * (or a monochrome one ...)
1143 * \warning to be used with GetImagePixels()
1144 * @return 1 if Gray level, 3 if Color (RGB, YBR, *or PALETTE COLOR*)
1146 int File::GetNumberOfScalarComponents()
1148 if ( GetSamplesPerPixel() == 3 )
1153 // 0028 0100 US IMG Bits Allocated
1154 // (in order no to be messed up by old RGB images)
1155 if ( GetEntryValue(0x0028,0x0100) == "24" )
1160 std::string strPhotometricInterpretation = GetEntryValue(0x0028,0x0004);
1162 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
1164 if ( HasLUT() )// PALETTE COLOR is NOT enough
1174 // beware of trailing space at end of string
1175 // DICOM tags are never of odd length
1176 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
1177 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
1178 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
1184 // we assume that *all* kinds of YBR are dealt with
1190 * \brief This function is intended to user that DOESN'T want
1191 * to get RGB pixels image when it's stored as a PALETTE COLOR image
1192 * - the (vtk) user is supposed to know how deal with LUTs -
1193 * \warning to be used with GetImagePixelsRaw()
1194 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
1196 int File::GetNumberOfScalarComponentsRaw()
1198 // 0028 0100 US IMG Bits Allocated
1199 // (in order no to be messed up by old RGB images)
1200 if ( File::GetEntryValue(0x0028,0x0100) == "24" )
1205 // we assume that *all* kinds of YBR are dealt with
1206 return GetSamplesPerPixel();
1210 * \brief Recover the offset (from the beginning of the file)
1211 * of *image* pixels (not *icone image* pixels, if any !)
1212 * @return Pixel Offset
1214 size_t File::GetPixelOffset()
1216 DocEntry *pxlElement = GetDocEntry(GrPixel, NumPixel);
1219 return pxlElement->GetOffset();
1223 gdcmDebugMacro( "Big trouble : Pixel Element ("
1224 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found" );
1230 * \brief Recover the pixel area length (in Bytes)
1231 * @return Pixel Element Length, as stored in the header
1232 * (NOT the memory space necessary to hold the Pixels
1233 * -in case of embeded compressed image-)
1234 * 0 : NOT USABLE file. The caller has to check.
1236 size_t File::GetPixelAreaLength()
1238 DocEntry *pxlElement = GetDocEntry(GrPixel, NumPixel);
1241 return pxlElement->GetLength();
1245 gdcmDebugMacro( "Big trouble : Pixel Element ("
1246 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found" );
1252 * \brief Adds the characteristics of a new element we want to anonymize
1253 * @param group Group number of the target tag.
1254 * @param elem Element number of the target tag.
1255 * @param value new value (string) to substitute with
1257 void File::AddAnonymizeElement (uint16_t group, uint16_t elem,
1258 std::string const &value)
1264 AnonymizeList.push_back(el);
1268 * \brief Overwrites in the file the values of the DicomElements
1271 void File::AnonymizeNoLoad()
1273 std::fstream *fp = new std::fstream(Filename.c_str(),
1274 std::ios::in | std::ios::out | std::ios::binary);
1278 uint32_t valLgth = 0;
1279 std::string *spaces;
1280 for (ListElements::iterator it = AnonymizeList.begin();
1281 it != AnonymizeList.end();
1284 d = GetDocEntry( (*it).Group, (*it).Elem);
1289 if ( dynamic_cast<BinEntry *>(d)
1290 || dynamic_cast<SeqEntry *>(d) )
1293 offset = d->GetOffset();
1294 lgth = d->GetLength();
1297 spaces = new std::string( lgth-valLgth, ' ');
1298 (*it).Value = (*it).Value + *spaces;
1301 fp->seekp( offset, std::ios::beg );
1302 fp->write( (*it).Value.c_str(), lgth );
1310 * \brief anonymize a File (remove Patient's personal info passed with
1311 * AddAnonymizeElement()
1313 bool File::AnonymizeFile()
1315 // If Anonymisation list is empty, let's perform some basic anonymization
1316 if ( AnonymizeList.begin() == AnonymizeList.end() )
1318 // If exist, replace by spaces
1319 SetValEntry (" ",0x0010, 0x2154); // Telephone
1320 SetValEntry (" ",0x0010, 0x1040); // Adress
1321 SetValEntry (" ",0x0010, 0x0020); // Patient ID
1323 DocEntry* patientNameHE = GetDocEntry (0x0010, 0x0010);
1325 if ( patientNameHE ) // we replace it by Study Instance UID (why not ?)
1327 std::string studyInstanceUID = GetEntryValue (0x0020, 0x000d);
1328 if ( studyInstanceUID != GDCM_UNFOUND )
1330 SetValEntry(studyInstanceUID, 0x0010, 0x0010);
1334 SetValEntry("anonymised", 0x0010, 0x0010);
1341 for (ListElements::iterator it = AnonymizeList.begin();
1342 it != AnonymizeList.end();
1345 d = GetDocEntry( (*it).Group, (*it).Elem);
1350 if ( dynamic_cast<BinEntry *>(d)
1351 || dynamic_cast<SeqEntry *>(d) )
1354 SetValEntry ((*it).Value, (*it).Group, (*it).Elem);
1358 // In order to make definitively impossible any further identification
1359 // remove or replace all the stuff that contains a Date
1361 //0008 0012 DA ID Instance Creation Date
1362 //0008 0020 DA ID Study Date
1363 //0008 0021 DA ID Series Date
1364 //0008 0022 DA ID Acquisition Date
1365 //0008 0023 DA ID Content Date
1366 //0008 0024 DA ID Overlay Date
1367 //0008 0025 DA ID Curve Date
1368 //0008 002a DT ID Acquisition Datetime
1369 //0018 9074 DT ACQ Frame Acquisition Datetime
1370 //0018 9151 DT ACQ Frame Reference Datetime
1371 //0018 a002 DT ACQ Contribution Date Time
1372 //0020 3403 SH REL Modified Image Date (RET)
1373 //0032 0032 DA SDY Study Verified Date
1374 //0032 0034 DA SDY Study Read Date
1375 //0032 1000 DA SDY Scheduled Study Start Date
1376 //0032 1010 DA SDY Scheduled Study Stop Date
1377 //0032 1040 DA SDY Study Arrival Date
1378 //0032 1050 DA SDY Study Completion Date
1379 //0038 001a DA VIS Scheduled Admission Date
1380 //0038 001c DA VIS Scheduled Discharge Date
1381 //0038 0020 DA VIS Admitting Date
1382 //0038 0030 DA VIS Discharge Date
1383 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1384 //0040 0004 DA PRC Scheduled Procedure Step End Date
1385 //0040 0244 DA PRC Performed Procedure Step Start Date
1386 //0040 0250 DA PRC Performed Procedure Step End Date
1387 //0040 2004 DA PRC Issue Date of Imaging Service Request
1388 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1389 //0040 4011 DT PRC Expected Completion Date and Time
1390 //0040 a030 DT PRC Verification Date Time
1391 //0040 a032 DT PRC Observation Date Time
1392 //0040 a120 DT PRC DateTime
1393 //0040 a121 DA PRC Date
1394 //0040 a13a DT PRC Referenced Datetime
1395 //0070 0082 DA ??? Presentation Creation Date
1396 //0100 0420 DT ??? SOP Autorization Date and Time
1397 //0400 0105 DT ??? Digital Signature DateTime
1398 //2100 0040 DA PJ Creation Date
1399 //3006 0008 DA SSET Structure Set Date
1400 //3008 0024 DA ??? Treatment Control Point Date
1401 //3008 0054 DA ??? First Treatment Date
1402 //3008 0056 DA ??? Most Recent Treatment Date
1403 //3008 0162 DA ??? Safe Position Exit Date
1404 //3008 0166 DA ??? Safe Position Return Date
1405 //3008 0250 DA ??? Treatment Date
1406 //300a 0006 DA RT RT Plan Date
1407 //300a 022c DA RT Air Kerma Rate Reference Date
1408 //300e 0004 DA RT Review Date
1414 * \brief Performs some consistency checking on various 'File related'
1415 * (as opposed to 'DicomDir related') entries
1416 * then writes in a file all the (Dicom Elements) included the Pixels
1417 * @param fileName file name to write to
1418 * @param writetype type of the file to be written
1419 * (ACR, ExplicitVR, ImplicitVR)
1421 bool File::Write(std::string fileName, FileType writetype)
1423 std::ofstream *fp = new std::ofstream(fileName.c_str(),
1424 std::ios::out | std::ios::binary);
1427 gdcmWarningMacro("Failed to open (write) File: " << fileName.c_str());
1431 // Entry : 0002|0000 = group length -> recalculated
1432 ValEntry*e0000 = GetValEntry(0x0002,0x0000);
1435 std::ostringstream sLen;
1436 sLen << ComputeGroup0002Length(writetype);
1437 e0000->SetValue(sLen.str());
1440 int i_lgPix = GetEntryLength(GrPixel, NumPixel);
1443 // no (GrPixel, NumPixel) element
1444 std::string s_lgPix = Util::Format("%d", i_lgPix+12);
1445 s_lgPix = Util::DicomString( s_lgPix.c_str() );
1446 InsertValEntry(s_lgPix,GrPixel, 0x0000);
1449 Document::WriteContent(fp, writetype);
1457 //-----------------------------------------------------------------------------
1461 //-----------------------------------------------------------------------------
1464 * \brief Parse pixel data from disk of [multi-]fragment RLE encoding.
1465 * Compute the RLE extra information and store it in \ref RLEInfo
1466 * for later pixel retrieval usage.
1468 void File::ComputeRLEInfo()
1470 std::string ts = GetTransferSyntax();
1471 if ( !Global::GetTS()->IsRLELossless(ts) )
1476 // Encoded pixel data: for the time being we are only concerned with
1477 // Jpeg or RLE Pixel data encodings.
1478 // As stated in PS 3.5-2003, section 8.2 p44:
1479 // "If sent in Encapsulated Format (i.e. other than the Native Format) the
1480 // value representation OB is used".
1481 // Hence we expect an OB value representation. Concerning OB VR,
1482 // the section PS 3.5-2003, section A.4.c p 58-59, states:
1483 // "For the Value Representations OB and OW, the encoding shall meet the
1484 // following specifications depending on the Data element tag:"
1486 // - the first item in the sequence of items before the encoded pixel
1487 // data stream shall be basic offset table item. The basic offset table
1488 // item value, however, is not required to be present"
1489 ReadAndSkipEncapsulatedBasicOffsetTable();
1491 // Encapsulated RLE Compressed Images (see PS 3.5-2003, Annex G)
1492 // Loop on the individual frame[s] and store the information
1493 // on the RLE fragments in a RLEFramesInfo.
1494 // Note: - when only a single frame is present, this is a
1496 // - when more than one frame are present, then we are in
1497 // the case of a multi-frame image.
1499 while ( (frameLength = ReadTagLength(0xfffe, 0xe000)) != 0 )
1501 // Parse the RLE Header and store the corresponding RLE Segment
1502 // Offset Table information on fragments of this current Frame.
1503 // Note that the fragment pixels themselves are not loaded
1504 // (but just skipped).
1505 long frameOffset = Fp->tellg();
1507 uint32_t nbRleSegments = ReadInt32();
1508 if ( nbRleSegments > 16 )
1510 // There should be at most 15 segments (refer to RLEFrame class)
1511 gdcmWarningMacro( "Too many segments.");
1514 uint32_t rleSegmentOffsetTable[16];
1515 for( int k = 1; k <= 15; k++ )
1517 rleSegmentOffsetTable[k] = ReadInt32();
1520 // Deduce from both RLE Header and frameLength
1521 // the fragment length, and again store this info
1522 // in a RLEFramesInfo.
1523 long rleSegmentLength[15];
1524 // skipping (not reading) RLE Segments
1525 if ( nbRleSegments > 1)
1527 for(unsigned int k = 1; k <= nbRleSegments-1; k++)
1529 rleSegmentLength[k] = rleSegmentOffsetTable[k+1]
1530 - rleSegmentOffsetTable[k];
1531 SkipBytes(rleSegmentLength[k]);
1535 rleSegmentLength[nbRleSegments] = frameLength
1536 - rleSegmentOffsetTable[nbRleSegments];
1537 SkipBytes(rleSegmentLength[nbRleSegments]);
1539 // Store the collected info
1540 RLEFrame *newFrame = new RLEFrame;
1541 newFrame->SetNumberOfFragments(nbRleSegments);
1542 for( unsigned int uk = 1; uk <= nbRleSegments; uk++ )
1544 newFrame->SetOffset(uk,frameOffset + rleSegmentOffsetTable[uk]);
1545 newFrame->SetLength(uk,rleSegmentLength[uk]);
1547 RLEInfo->AddFrame(newFrame);
1550 // Make sure that we encounter a 'Sequence Delimiter Item'
1551 // at the end of the item :
1552 if ( !ReadTag(0xfffe, 0xe0dd) )
1554 gdcmWarningMacro( "No sequence delimiter item at end of RLE item sequence");
1559 * \brief Parse pixel data from disk of [multi-]fragment Jpeg encoding.
1560 * Compute the jpeg extra information (fragment[s] offset[s] and
1561 * length) and store it[them] in \ref JPEGInfo for later pixel
1564 void File::ComputeJPEGFragmentInfo()
1566 // If you need to, look for comments of ComputeRLEInfo().
1567 std::string ts = GetTransferSyntax();
1568 if ( ! Global::GetTS()->IsJPEG(ts) )
1573 ReadAndSkipEncapsulatedBasicOffsetTable();
1575 // Loop on the fragments[s] and store the parsed information in a
1577 long fragmentLength;
1578 while ( (fragmentLength = ReadTagLength(0xfffe, 0xe000)) != 0 )
1580 long fragmentOffset = Fp->tellg();
1582 // Store the collected info
1583 JPEGFragment *newFragment = new JPEGFragment;
1584 newFragment->SetOffset(fragmentOffset);
1585 newFragment->SetLength(fragmentLength);
1586 JPEGInfo->AddFragment(newFragment);
1588 SkipBytes(fragmentLength);
1591 // Make sure that we encounter a 'Sequence Delimiter Item'
1592 // at the end of the item :
1593 if ( !ReadTag(0xfffe, 0xe0dd) )
1595 gdcmWarningMacro( "No sequence delimiter item at end of JPEG item sequence");
1600 * \brief Assuming the internal file pointer \ref Document::Fp
1601 * is placed at the beginning of a tag check whether this
1602 * tag is (TestGroup, TestElem).
1603 * \warning On success the internal file pointer \ref Document::Fp
1604 * is modified to point after the tag.
1605 * On failure (i.e. when the tag wasn't the expected tag
1606 * (TestGroup, TestElem) the internal file pointer
1607 * \ref Document::Fp is restored to it's original position.
1608 * @param testGroup The expected group of the tag.
1609 * @param testElem The expected Element of the tag.
1610 * @return True on success, false otherwise.
1612 bool File::ReadTag(uint16_t testGroup, uint16_t testElem)
1614 long positionOnEntry = Fp->tellg();
1615 long currentPosition = Fp->tellg(); // On debugging purposes
1617 // Read the Item Tag group and element, and make
1618 // sure they are what we expected:
1619 uint16_t itemTagGroup;
1620 uint16_t itemTagElem;
1623 itemTagGroup = ReadInt16();
1624 itemTagElem = ReadInt16();
1626 catch ( FormatError e )
1628 //std::cerr << e << std::endl;
1631 if ( itemTagGroup != testGroup || itemTagElem != testElem )
1633 gdcmWarningMacro( "Wrong Item Tag found:"
1634 << " We should have found tag ("
1635 << std::hex << testGroup << "," << testElem << ")" << std::endl
1636 << " but instead we encountered tag ("
1637 << std::hex << itemTagGroup << "," << itemTagElem << ")"
1638 << " at address: " << " 0x(" << (unsigned int)currentPosition << ")"
1640 Fp->seekg(positionOnEntry, std::ios::beg);
1648 * \brief Assuming the internal file pointer \ref Document::Fp
1649 * is placed at the beginning of a tag (TestGroup, TestElement),
1650 * read the length associated to the Tag.
1651 * \warning On success the internal file pointer \ref Document::Fp
1652 * is modified to point after the tag and it's length.
1653 * On failure (i.e. when the tag wasn't the expected tag
1654 * (TestGroup, TestElement) the internal file pointer
1655 * \ref Document::Fp is restored to it's original position.
1656 * @param testGroup The expected Group of the tag.
1657 * @param testElem The expected Element of the tag.
1658 * @return On success returns the length associated to the tag. On failure
1661 uint32_t File::ReadTagLength(uint16_t testGroup, uint16_t testElem)
1664 if ( !ReadTag(testGroup, testElem) )
1669 //// Then read the associated Item Length
1670 long currentPosition = Fp->tellg();
1671 uint32_t itemLength = ReadInt32();
1673 gdcmWarningMacro( "Basic Item Length is: "
1674 << itemLength << std::endl
1675 << " at address: " << std::hex << (unsigned int)currentPosition);
1681 * \brief When parsing the Pixel Data of an encapsulated file, read
1682 * the basic offset table (when present, and BTW dump it).
1684 void File::ReadAndSkipEncapsulatedBasicOffsetTable()
1686 //// Read the Basic Offset Table Item Tag length...
1687 uint32_t itemLength = ReadTagLength(0xfffe, 0xe000);
1689 // When present, read the basic offset table itself.
1690 // Notes: - since the presence of this basic offset table is optional
1691 // we can't rely on it for the implementation, and we will simply
1692 // trash it's content (when present).
1693 // - still, when present, we could add some further checks on the
1694 // lengths, but we won't bother with such fuses for the time being.
1695 if ( itemLength != 0 )
1697 char *basicOffsetTableItemValue = new char[itemLength + 1];
1698 Fp->read(basicOffsetTableItemValue, itemLength);
1701 for (unsigned int i=0; i < itemLength; i += 4 )
1703 uint32_t individualLength = str2num( &basicOffsetTableItemValue[i],
1705 gdcmWarningMacro( "Read one length: " <<
1706 std::hex << individualLength );
1710 delete[] basicOffsetTableItemValue;
1714 // These are the deprecated method that one day should be removed (after the next release)
1715 #ifndef GDCM_LEGACY_REMOVE
1717 * \brief Loader. (DEPRECATED : not to break the API)
1718 * @param fileName file to be open for parsing
1719 * @return false if file cannot be open or no swap info was found,
1720 * or no tag was found.
1721 * @deprecated Use the Load() function instead
1723 bool File::Load( std::string const &fileName )
1725 GDCM_LEGACY_REPLACED_BODY(File::Load(std::string), "1.2",
1727 SetFileName( fileName );
1728 if ( ! this->Document::Load( ) )
1731 return DoTheLoadingJob( );
1735 //-----------------------------------------------------------------------------
1738 //-----------------------------------------------------------------------------
1739 } // end namespace gdcm