1 /*=========================================================================
4 Module: $RCSfile: gdcmFile.cxx,v $
6 Date: $Date: 2005/07/20 14:49:41 $
7 Version: $Revision: 1.254 $
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 // Image Position (Patient) (0020,0032) What is it used for?
31 // The attribute Patient Orientation (0020,0020) from the General Image Module
32 // is of type 2C and has the condition Required if image does not require
33 // Image Orientation (0020,0037) and Image Position (0020,0032).
34 // However, if the image does require the attributes Image Orientation (0020,0037)
35 // and Image Position (0020,0032) then attribute Patient Orientation (0020,0020)
36 // should not be present in the images.
39 // Patient Position (0018,5100) values : HFP = Head First-Prone
40 // HFS = Head First-Supine
41 // HFDR = Head First-Decubitus Right
42 // HFDL = Head First-Decubitus Left
43 // FFDR = Feet First-Decubitus Right
44 // FFDL = Feet First-Decubitus Left
45 // FFP = Feet First-Prone
46 // FFS = Feet First-Supine
47 // CS 2 Patient Orientation (0020 0020)
48 // When the coordinates of the image
49 // are always present, this field is almost never used
50 // ---------------------------------------------------------------
53 #include "gdcmGlobal.h"
55 #include "gdcmDebug.h"
57 #include "gdcmValEntry.h"
58 #include "gdcmBinEntry.h"
59 #include "gdcmSeqEntry.h"
60 #include "gdcmRLEFramesInfo.h"
61 #include "gdcmJPEGFragmentsInfo.h"
64 #include <stdio.h> //sscanf
65 #include <stdlib.h> // for atoi
69 //-----------------------------------------------------------------------------
70 // Constructor / Destructor
73 * \brief Constructor used when we want to generate dicom files from scratch
78 RLEInfo = new RLEFramesInfo;
79 JPEGInfo = new JPEGFragmentsInfo;
80 GrPixel = 0x7fe0; // to avoid further troubles
86 * \brief Canonical destructor.
96 //-----------------------------------------------------------------------------
100 * @return false if file cannot be open or no swap info was found,
101 * or no tag was found.
105 if ( ! this->Document::Load( ) )
108 return DoTheLoadingJob( );
112 * \brief Does the Loading Job (internal use only)
113 * @return false if file cannot be open or no swap info was found,
114 * or no tag was found.
116 bool File::DoTheLoadingJob( )
119 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
120 // We may encounter the 'RETired' (0x0028, 0x0200) tag
121 // (Image Location") . This entry contains the number of
122 // the group that contains the pixel data (hence the "Pixel Data"
123 // is found by indirection through the "Image Location").
124 // Inside the group pointed by "Image Location" the searched element
125 // is conventionally the element 0x0010 (when the norm is respected).
126 // When the "Image Location" is missing we default to group 0x7fe0.
127 // Note: this IS the right place for the code
130 const std::string &imgLocation = GetEntryValue(0x0028, 0x0200);
131 if ( imgLocation == GDCM_UNFOUND )
138 GrPixel = (uint16_t) atoi( imgLocation.c_str() );
141 // sometimes Image Location value doesn't follow
142 // the supposed processor endianness.
143 // see gdcmData/cr172241.dcm
144 if ( GrPixel == 0xe07f )
149 if ( GrPixel != 0x7fe0 )
151 // This is a kludge for old dirty Philips imager.
159 // Now, we know GrPixel and NumPixel.
160 // Let's create a VirtualDictEntry to allow a further VR modification
161 // and force VR to match with BitsAllocated.
162 DocEntry *entry = GetDocEntry(GrPixel, NumPixel);
165 // Compute the RLE or JPEG info
167 const std::string &ts = GetTransferSyntax();
168 Fp->seekg( entry->GetOffset(), std::ios::beg );
169 if ( Global::GetTS()->IsRLELossless(ts) )
171 else if ( Global::GetTS()->IsJPEG(ts) )
172 ComputeJPEGFragmentInfo();
175 // Create a new BinEntry to change the DictEntry
176 // The changed DictEntry will have
177 // - a correct PixelVR OB or OW)
178 // - the name to "Pixel Data"
179 BinEntry *oldEntry = dynamic_cast<BinEntry *>(entry);
183 // 8 bits allocated is a 'O Bytes' , as well as 24 (old ACR-NEMA RGB)
184 // more than 8 (i.e 12, 16) is a 'O Words'
185 if ( GetBitsAllocated() == 8 || GetBitsAllocated() == 24 )
190 // Change only made if usefull
191 if ( PixelVR != oldEntry->GetVR() )
193 DictEntry* newDict = NewVirtualDictEntry(GrPixel,NumPixel,
194 PixelVR,"1","Pixel Data");
196 BinEntry *newEntry = new BinEntry(newDict);
197 newEntry->Copy(entry);
198 newEntry->SetBinArea(oldEntry->GetBinArea(),oldEntry->IsSelfArea());
199 oldEntry->SetSelfArea(false);
201 RemoveEntry(oldEntry);
209 * \brief This predicate, based on hopefully reasonable heuristics,
210 * decides whether or not the current File was properly parsed
211 * and contains the mandatory information for being considered as
212 * a well formed and usable Dicom/Acr File.
213 * @return true when File is the one of a reasonable Dicom/Acr file,
216 bool File::IsReadable()
218 if ( !Document::IsReadable() )
223 const std::string &res = GetEntryValue(0x0028, 0x0005);
224 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
226 gdcmWarningMacro("Wrong Image Dimensions" << res);
227 return false; // Image Dimensions
229 bool b0028_0100 = true;
230 if ( !GetDocEntry(0x0028, 0x0100) )
232 gdcmWarningMacro("Bits Allocated (0028|0100) not found");
233 //return false; // "Bits Allocated"
236 bool b0028_0101 = true;
237 if ( !GetDocEntry(0x0028, 0x0101) )
239 gdcmWarningMacro("Bits Stored (0028|0101) not found");
240 //return false; // "Bits Stored"
243 bool b0028_0102 = true;
244 if ( !GetDocEntry(0x0028, 0x0102) )
246 gdcmWarningMacro("Hight Bit (0028|0102) not found");
247 //return false; // "High Bit"
250 bool b0028_0103 = true;
251 if ( !GetDocEntry(0x0028, 0x0103) )
253 gdcmWarningMacro("Pixel Representation (0028|0103) not found");
254 //return false; // "Pixel Representation" i.e. 'Sign' ( 0 : unsigned, 1 : signed)
258 if ( !b0028_0100 && !b0028_0101 && !b0028_0102 && !b0028_0103)
260 gdcmWarningMacro("Too much mandatory Tags missing !");
264 if ( !GetDocEntry(GrPixel, NumPixel) )
266 gdcmWarningMacro("Pixel Dicom Element " << std::hex <<
267 GrPixel << "|" << NumPixel << "not found");
268 return false; // Pixel Dicom Element not found :-(
274 * \brief gets the info from 0020,0013 : Image Number else 0.
275 * @return image number
277 int File::GetImageNumber()
279 //0020 0013 : Image Number
280 std::string strImNumber = GetEntryValue(0x0020,0x0013);
281 if ( strImNumber != GDCM_UNFOUND )
283 return atoi( strImNumber.c_str() );
289 * \brief gets the info from 0008,0060 : Modality
290 * @return Modality Type
292 ModalityType File::GetModality()
294 // 0008 0060 : Modality
295 std::string strModality = GetEntryValue(0x0008,0x0060);
296 if ( strModality != GDCM_UNFOUND )
298 if ( strModality.find("AU") < strModality.length()) return AU;
299 else if ( strModality.find("AS") < strModality.length()) return AS;
300 else if ( strModality.find("BI") < strModality.length()) return BI;
301 else if ( strModality.find("CF") < strModality.length()) return CF;
302 else if ( strModality.find("CP") < strModality.length()) return CP;
303 else if ( strModality.find("CR") < strModality.length()) return CR;
304 else if ( strModality.find("CT") < strModality.length()) return CT;
305 else if ( strModality.find("CS") < strModality.length()) return CS;
306 else if ( strModality.find("DD") < strModality.length()) return DD;
307 else if ( strModality.find("DF") < strModality.length()) return DF;
308 else if ( strModality.find("DG") < strModality.length()) return DG;
309 else if ( strModality.find("DM") < strModality.length()) return DM;
310 else if ( strModality.find("DS") < strModality.length()) return DS;
311 else if ( strModality.find("DX") < strModality.length()) return DX;
312 else if ( strModality.find("ECG") < strModality.length()) return ECG;
313 else if ( strModality.find("EPS") < strModality.length()) return EPS;
314 else if ( strModality.find("FA") < strModality.length()) return FA;
315 else if ( strModality.find("FS") < strModality.length()) return FS;
316 else if ( strModality.find("HC") < strModality.length()) return HC;
317 else if ( strModality.find("HD") < strModality.length()) return HD;
318 else if ( strModality.find("LP") < strModality.length()) return LP;
319 else if ( strModality.find("LS") < strModality.length()) return LS;
320 else if ( strModality.find("MA") < strModality.length()) return MA;
321 else if ( strModality.find("MR") < strModality.length()) return MR;
322 else if ( strModality.find("NM") < strModality.length()) return NM;
323 else if ( strModality.find("OT") < strModality.length()) return OT;
324 else if ( strModality.find("PT") < strModality.length()) return PT;
325 else if ( strModality.find("RF") < strModality.length()) return RF;
326 else if ( strModality.find("RG") < strModality.length()) return RG;
327 else if ( strModality.find("RTDOSE")
328 < strModality.length()) return RTDOSE;
329 else if ( strModality.find("RTIMAGE")
330 < strModality.length()) return RTIMAGE;
331 else if ( strModality.find("RTPLAN")
332 < strModality.length()) return RTPLAN;
333 else if ( strModality.find("RTSTRUCT")
334 < strModality.length()) return RTSTRUCT;
335 else if ( strModality.find("SM") < strModality.length()) return SM;
336 else if ( strModality.find("ST") < strModality.length()) return ST;
337 else if ( strModality.find("TG") < strModality.length()) return TG;
338 else if ( strModality.find("US") < strModality.length()) return US;
339 else if ( strModality.find("VF") < strModality.length()) return VF;
340 else if ( strModality.find("XA") < strModality.length()) return XA;
341 else if ( strModality.find("XC") < strModality.length()) return XC;
345 /// \todo throw error return value ???
346 /// specified <> unknown in our database
354 * \brief Retrieve the number of columns of image.
355 * @return The encountered size when found, 0 by default.
356 * 0 means the file is NOT USABLE. The caller will have to check
360 const std::string &strSize = GetEntryValue(0x0028,0x0011);
361 if ( strSize == GDCM_UNFOUND )
365 return atoi( strSize.c_str() );
369 * \brief Retrieve the number of lines of image.
370 * \warning The defaulted value is 1 as opposed to File::GetXSize()
371 * @return The encountered size when found, 1 by default
372 * (The ACR-NEMA file contains a Signal, not an Image).
376 const std::string &strSize = GetEntryValue(0x0028,0x0010);
377 if ( strSize != GDCM_UNFOUND )
379 return atoi( strSize.c_str() );
386 // The Rows (0028,0010) entry was optional for ACR/NEMA.
387 // (at least some images didn't have it.)
388 // It might hence be a signal (1D image). So we default to 1:
393 * \brief Retrieve the number of planes of volume or the number
394 * of frames of a multiframe.
395 * \warning When present we consider the "Number of Frames" as the third
396 * dimension. When missing we consider the third dimension as
397 * being the ACR-NEMA "Planes" tag content.
398 * @return The encountered size when found, 1 by default (single image).
402 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
403 // as the third dimension.
404 const std::string &strSize = GetEntryValue(0x0028,0x0008);
405 if ( strSize != GDCM_UNFOUND )
407 return atoi( strSize.c_str() );
410 // We then consider the "Planes" entry as the third dimension
411 const std::string &strSize2 = GetEntryValue(0x0028,0x0012);
412 if ( strSize2 != GDCM_UNFOUND )
414 return atoi( strSize2.c_str() );
420 * \brief gets the info from 0018,1164 : ImagerPixelSpacing
421 * then 0028,0030 : Pixel Spacing
423 * @return X dimension of a pixel
425 float File::GetXSpacing()
427 float xspacing = 1.0;
428 float yspacing = 1.0;
431 // To follow David Clunie's advice, we first check ImagerPixelSpacing
433 const std::string &strImagerPixelSpacing = GetEntryValue(0x0018,0x1164);
434 if ( strImagerPixelSpacing != GDCM_UNFOUND )
436 if ( ( nbValues = sscanf( strImagerPixelSpacing.c_str(),
437 "%f\\%f", &yspacing, &xspacing)) != 2 )
439 // if no values, xspacing is set to 1.0
442 // if single value is found, xspacing is defaulted to yspacing
446 if ( xspacing == 0.0 )
453 const std::string &strSpacing = GetEntryValue(0x0028,0x0030);
455 if ( strSpacing == GDCM_UNFOUND )
457 gdcmWarningMacro( "Unfound Pixel Spacing (0028,0030)" );
461 if ( ( nbValues = sscanf( strSpacing.c_str(),
462 "%f \\%f ", &yspacing, &xspacing)) != 2 )
464 // if no values, xspacing is set to 1.0
467 // if single value is found, xspacing is defaulted to yspacing
471 if ( xspacing == 0.0 )
477 // to avoid troubles with David Clunie's-like images (at least one)
478 if ( xspacing == 0. && yspacing == 0.)
483 gdcmWarningMacro("gdcmData/CT-MONO2-8-abdo.dcm-like problem");
484 // seems to be a bug in the header ...
485 nbValues = sscanf( strSpacing.c_str(), "%f \\0\\%f ", &yspacing, &xspacing);
486 gdcmAssertMacro( nbValues == 2 );
493 * \brief gets the info from 0018,1164 : ImagerPixelSpacing
494 * then from 0028,0030 : Pixel Spacing
496 * @return Y dimension of a pixel
498 float File::GetYSpacing()
502 // To follow David Clunie's advice, we first check ImagerPixelSpacing
504 const std::string &strImagerPixelSpacing = GetEntryValue(0x0018,0x1164);
505 if ( strImagerPixelSpacing != GDCM_UNFOUND )
507 nbValues = sscanf( strImagerPixelSpacing.c_str(), "%f", &yspacing);
509 // if sscanf cannot read any float value, it won't affect yspacing
513 if ( yspacing == 0.0 )
519 std::string strSpacing = GetEntryValue(0x0028,0x0030);
520 if ( strSpacing == GDCM_UNFOUND )
522 gdcmWarningMacro("Unfound Pixel Spacing (0028,0030)");
526 // if sscanf cannot read any float value, it won't affect yspacing
527 nbValues = sscanf( strSpacing.c_str(), "%f", &yspacing);
529 // if no values, yspacing is set to 1.0
533 if ( yspacing == 0.0 )
540 * \brief gets the info from 0018,0088 : Space Between Slices
541 * else from 0018,0050 : Slice Thickness
543 * @return Z dimension of a voxel-to be
545 float File::GetZSpacing()
547 // Spacing Between Slices : distance between the middle of 2 slices
549 // jointives (Spacing between Slices = Slice Thickness)
550 // overlapping (Spacing between Slices < Slice Thickness)
551 // disjointes (Spacing between Slices > Slice Thickness)
552 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
553 // It only concerns the MRI guys, not people wanting to visualize volumes
554 // If Spacing Between Slices is missing,
555 // we suppose slices joint together
557 const std::string &strSpacingBSlices = GetEntryValue(0x0018,0x0088);
559 if ( strSpacingBSlices == GDCM_UNFOUND )
561 gdcmWarningMacro("Unfound Spacing Between Slices (0018,0088)");
562 const std::string &strSliceThickness = GetEntryValue(0x0018,0x0050);
563 if ( strSliceThickness == GDCM_UNFOUND )
565 gdcmWarningMacro("Unfound Slice Thickness (0018,0050)");
570 // if no 'Spacing Between Slices' is found,
571 // we assume slices join together
572 // (no overlapping, no interslice gap)
573 // if they don't, we're fucked up
574 return (float)atof( strSliceThickness.c_str() );
578 return (float)atof( strSpacingBSlices.c_str() );
582 * \brief gets the info from 0020,0032 : Image Position Patient
583 * else from 0020,0030 : Image Position (RET)
585 * @return up-left image corner X position
587 float File::GetXOrigin()
589 float xImPos, yImPos, zImPos;
590 std::string strImPos = GetEntryValue(0x0020,0x0032);
592 if ( strImPos == GDCM_UNFOUND )
594 gdcmWarningMacro( "Unfound Image Position Patient (0020,0032)");
595 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
596 if ( strImPos == GDCM_UNFOUND )
598 gdcmWarningMacro( "Unfound Image Position (RET) (0020,0030)");
603 if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos) != 3 )
612 * \brief gets the info from 0020,0032 : Image Position Patient
613 * else from 0020,0030 : Image Position (RET)
615 * @return up-left image corner Y position
617 float File::GetYOrigin()
619 float xImPos, yImPos, zImPos;
620 std::string strImPos = GetEntryValue(0x0020,0x0032);
622 if ( strImPos == GDCM_UNFOUND)
624 gdcmWarningMacro( "Unfound Image Position Patient (0020,0032)");
625 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
626 if ( strImPos == GDCM_UNFOUND )
628 gdcmWarningMacro( "Unfound Image Position (RET) (0020,0030)");
633 if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos) != 3 )
642 * \brief gets the info from 0020,0032 : Image Position Patient
643 * else from 0020,0030 : Image Position (RET)
644 * else from 0020,1041 : Slice Location
645 * else from 0020,0050 : Location
647 * @return up-left image corner Z position
649 float File::GetZOrigin()
651 float xImPos, yImPos, zImPos;
652 std::string strImPos = GetEntryValue(0x0020,0x0032);
654 if ( strImPos != GDCM_UNFOUND )
656 if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos) != 3)
658 gdcmWarningMacro( "Wrong Image Position Patient (0020,0032)");
659 return 0.; // bug in the element 0x0020,0x0032
667 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
668 if ( strImPos != GDCM_UNFOUND )
670 if ( sscanf( strImPos.c_str(),
671 "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos ) != 3 )
673 gdcmWarningMacro( "Wrong Image Position (RET) (0020,0030)");
674 return 0.; // bug in the element 0x0020,0x0032
682 // for *very* old ACR-NEMA images
683 std::string strSliceLocation = GetEntryValue(0x0020,0x1041);
684 if ( strSliceLocation != GDCM_UNFOUND )
686 if ( sscanf( strSliceLocation.c_str(), "%f ", &zImPos) != 1)
688 gdcmWarningMacro( "Wrong Slice Location (0020,1041)");
689 return 0.; // bug in the element 0x0020,0x1041
696 gdcmWarningMacro( "Unfound Slice Location (0020,1041)");
698 std::string strLocation = GetEntryValue(0x0020,0x0050);
699 if ( strLocation != GDCM_UNFOUND )
701 if ( sscanf( strLocation.c_str(), "%f ", &zImPos) != 1 )
703 gdcmWarningMacro( "Wrong Location (0020,0050)");
704 return 0.; // bug in the element 0x0020,0x0050
711 gdcmWarningMacro( "Unfound Location (0020,0050)");
713 return 0.; // Hopeless
717 * \brief gets the info from 0020,0037 : Image Orientation Patient
718 * (needed to organize DICOM files based on their x,y,z position)
719 * @param iop adress of the (6)float array to receive values
720 * @return cosines of image orientation patient
722 void File::GetImageOrientationPatient( float iop[6] )
724 std::string strImOriPat;
725 //iop is supposed to be float[6]
726 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
728 // 0020 0037 DS REL Image Orientation (Patient)
729 if ( (strImOriPat = GetEntryValue(0x0020,0x0037)) != GDCM_UNFOUND )
731 if ( sscanf( strImOriPat.c_str(), "%f \\ %f \\%f \\%f \\%f \\%f ",
732 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
734 gdcmWarningMacro( "Wrong Image Orientation Patient (0020,0037). Less than 6 values were found." );
738 // 0020 0035 DS REL Image Orientation (RET)
739 else if ( (strImOriPat = GetEntryValue(0x0020,0x0035)) != GDCM_UNFOUND )
741 if ( sscanf( strImOriPat.c_str(), "%f \\ %f \\%f \\%f \\%f \\%f ",
742 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
744 gdcmWarningMacro( "wrong Image Orientation Patient (0020,0035). Less than 6 values were found." );
750 * \brief Retrieve the number of Bits Stored (actually used)
751 * (as opposed to number of Bits Allocated)
752 * @return The encountered number of Bits Stored, 0 by default.
753 * 0 means the file is NOT USABLE. The caller has to check it !
755 int File::GetBitsStored()
757 std::string strSize = GetEntryValue( 0x0028, 0x0101 );
758 if ( strSize == GDCM_UNFOUND )
760 gdcmWarningMacro("(0028,0101) is supposed to be mandatory");
761 return 0; // It's supposed to be mandatory
762 // the caller will have to check
764 return atoi( strSize.c_str() );
768 * \brief Retrieve the number of Bits Allocated
769 * (8, 12 -compacted ACR-NEMA files-, 16, ...)
770 * @return The encountered number of Bits Allocated, 0 by default.
771 * 0 means the file is NOT USABLE. The caller has to check it !
773 int File::GetBitsAllocated()
775 std::string strSize = GetEntryValue(0x0028,0x0100);
776 if ( strSize == GDCM_UNFOUND )
778 gdcmWarningMacro( "(0028,0100) is supposed to be mandatory");
779 return 0; // It's supposed to be mandatory
780 // the caller will have to check
782 return atoi( strSize.c_str() );
786 * \brief Retrieve the high bit position.
787 * \warning The method defaults to 0 when information is missing.
788 * The responsability of checking this value is left to the caller.
789 * @return The high bit position when present. 0 when missing.
791 int File::GetHighBitPosition()
793 std::string strSize = GetEntryValue( 0x0028, 0x0102 );
794 if ( strSize == GDCM_UNFOUND )
796 gdcmWarningMacro( "(0028,0102) is supposed to be mandatory");
799 return atoi( strSize.c_str() );
803 * \brief Retrieve the number of Samples Per Pixel
804 * (1 : gray level, 3 : RGB/YBR -1 or 3 Planes-)
805 * @return The encountered number of Samples Per Pixel, 1 by default.
806 * (we assume Gray level Pixels)
808 int File::GetSamplesPerPixel()
810 const std::string &strSize = GetEntryValue(0x0028,0x0002);
811 if ( strSize == GDCM_UNFOUND )
813 gdcmWarningMacro( "(0028,0002) is supposed to be mandatory");
814 return 1; // Well, it's supposed to be mandatory ...
815 // but sometimes it's missing : *we* assume Gray pixels
817 return atoi( strSize.c_str() );
821 * \brief Retrieve the Planar Configuration for RGB images
822 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
823 * @return The encountered Planar Configuration, 0 by default.
825 int File::GetPlanarConfiguration()
827 std::string strSize = GetEntryValue(0x0028,0x0006);
828 if ( strSize == GDCM_UNFOUND )
830 gdcmWarningMacro( "Not found : Planar Configuration (0028,0006)");
833 return atoi( strSize.c_str() );
837 * \brief Return the size (in bytes) of a single pixel of data.
838 * @return The size in bytes of a single pixel of data; 0 by default
839 * 0 means the file is NOT USABLE; the caller will have to check
841 int File::GetPixelSize()
843 // 0028 0100 US IMG Bits Allocated
844 // (in order no to be messed up by old ACR-NEMA RGB images)
845 // if (File::GetEntryValue(0x0028,0x0100) == "24")
848 std::string pixelType = GetPixelType();
849 if ( pixelType == "8U" || pixelType == "8S" )
853 if ( pixelType == "16U" || pixelType == "16S")
857 if ( pixelType == "32U" || pixelType == "32S")
861 if ( pixelType == "FD" )
865 gdcmWarningMacro( "Unknown pixel type");
870 * \brief Build the Pixel Type of the image.
871 * Possible values are:
872 * - 8U unsigned 8 bit,
874 * - 16U unsigned 16 bit,
875 * - 16S signed 16 bit,
876 * - 32U unsigned 32 bit,
877 * - 32S signed 32 bit,
878 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
879 * \warning 12 bit images appear as 16 bit.
880 * 24 bit images appear as 8 bit + photochromatic interp ="RGB "
881 * + Planar Configuration = 0
882 * @return 0S if nothing found. NOT USABLE file. The caller has to check
884 std::string File::GetPixelType()
886 std::string bitsAlloc = GetEntryValue(0x0028, 0x0100); // Bits Allocated
887 if ( bitsAlloc == GDCM_UNFOUND )
889 gdcmWarningMacro( "Missing Bits Allocated (0028,0100)");
890 bitsAlloc = "16"; // default and arbitrary value, not to polute the output
893 if ( bitsAlloc == "64" )
897 else if ( bitsAlloc == "12" )
899 // It will be unpacked
902 else if ( bitsAlloc == "24" )
904 // (in order no to be messed up
905 bitsAlloc = "8"; // by old RGB images)
908 std::string sign = GetEntryValue(0x0028, 0x0103);//"Pixel Representation"
910 if (sign == GDCM_UNFOUND )
912 gdcmWarningMacro( "Missing Pixel Representation (0028,0103)");
913 sign = "U"; // default and arbitrary value, not to polute the output
915 else if ( sign == "0" )
923 return bitsAlloc + sign;
927 * \brief Check whether the pixels are signed (1) or UNsigned (0) data.
928 * \warning The method defaults to false (UNsigned) when tag 0028|0103
930 * The responsability of checking this value is left to the caller
931 * (NO transformation is performed on the pixels to make then >0)
932 * @return True when signed, false when UNsigned
934 bool File::IsSignedPixelData()
936 std::string strSign = GetEntryValue( 0x0028, 0x0103 );
937 if ( strSign == GDCM_UNFOUND )
939 gdcmWarningMacro( "(0028,0103) is supposed to be mandatory");
942 int sign = atoi( strSign.c_str() );
951 * \brief Check whether this a monochrome picture (gray levels) or not,
952 * using "Photometric Interpretation" tag (0x0028,0x0004).
953 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
955 bool File::IsMonochrome()
957 const std::string &PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
958 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
959 || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
963 if ( PhotometricInterp == GDCM_UNFOUND )
965 gdcmWarningMacro( "Not found : Photometric Interpretation (0028,0004)");
971 * \brief Check whether this a MONOCHROME1 picture (high values = dark)
972 * or not using "Photometric Interpretation" tag (0x0028,0x0004).
973 * @return true when "MONOCHROME1" . False otherwise.
975 bool File::IsMonochrome1()
977 const std::string &PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
978 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1") )
982 if ( PhotometricInterp == GDCM_UNFOUND )
984 gdcmWarningMacro( "Not found : Photometric Interpretation (0028,0004)");
990 * \brief Check whether this a "PALETTE COLOR" picture or not by accessing
991 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
992 * @return true when "PALETTE COLOR". False otherwise.
994 bool File::IsPaletteColor()
996 std::string PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
997 if ( PhotometricInterp == "PALETTE COLOR " )
1001 if ( PhotometricInterp == GDCM_UNFOUND )
1003 gdcmWarningMacro( "Not found : Palette color (0028,0004)");
1009 * \brief Check whether this a "YBR_FULL" color picture or not by accessing
1010 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
1011 * @return true when "YBR_FULL". False otherwise.
1013 bool File::IsYBRFull()
1015 std::string PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
1016 if ( PhotometricInterp == "YBR_FULL" )
1020 if ( PhotometricInterp == GDCM_UNFOUND )
1022 gdcmWarningMacro( "Not found : YBR Full (0028,0004)");
1028 * \brief tells us if LUT are used
1029 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1030 * are NOT considered as LUT, since nobody knows
1031 * how to deal with them
1032 * Please warn me if you know sbdy that *does* know ... jprx
1033 * @return true if LUT Descriptors and LUT Tables were found
1037 // Check the presence of the LUT Descriptors, and LUT Tables
1039 if ( !GetDocEntry(0x0028,0x1101) )
1043 // LutDescriptorGreen
1044 if ( !GetDocEntry(0x0028,0x1102) )
1048 // LutDescriptorBlue
1049 if ( !GetDocEntry(0x0028,0x1103) )
1053 // Red Palette Color Lookup Table Data
1054 if ( !GetDocEntry(0x0028,0x1201) )
1058 // Green Palette Color Lookup Table Data
1059 if ( !GetDocEntry(0x0028,0x1202) )
1063 // Blue Palette Color Lookup Table Data
1064 if ( !GetDocEntry(0x0028,0x1203) )
1069 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1070 // NOT taken into account, but we don't know how to use it ...
1075 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1077 * @return Lookup Table number of Bits , 0 by default
1078 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1079 * @ return bit number of each LUT item
1081 int File::GetLUTNbits()
1083 std::vector<std::string> tokens;
1086 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1087 // = Lookup Table Desc-Blue
1088 // Consistency already checked in GetLUTLength
1089 std::string lutDescription = GetEntryValue(0x0028,0x1101);
1090 if ( lutDescription == GDCM_UNFOUND )
1095 tokens.clear(); // clean any previous value
1096 Util::Tokenize ( lutDescription, tokens, "\\" );
1097 //LutLength=atoi(tokens[0].c_str());
1098 //LutDepth=atoi(tokens[1].c_str());
1100 lutNbits = atoi( tokens[2].c_str() );
1107 *\brief gets the info from 0028,1052 : Rescale Intercept
1108 * @return Rescale Intercept
1110 float File::GetRescaleIntercept()
1112 float resInter = 0.;
1113 /// 0028 1052 DS IMG Rescale Intercept
1114 const std::string &strRescInter = GetEntryValue(0x0028,0x1052);
1115 if ( strRescInter != GDCM_UNFOUND )
1117 if ( sscanf( strRescInter.c_str(), "%f ", &resInter) != 1 )
1119 // bug in the element 0x0028,0x1052
1120 gdcmWarningMacro( "Rescale Intercept (0028,1052) is empty." );
1128 *\brief gets the info from 0028,1053 : Rescale Slope
1129 * @return Rescale Slope
1131 float File::GetRescaleSlope()
1133 float resSlope = 1.;
1134 //0028 1053 DS IMG Rescale Slope
1135 std::string strRescSlope = GetEntryValue(0x0028,0x1053);
1136 if ( strRescSlope != GDCM_UNFOUND )
1138 if ( sscanf( strRescSlope.c_str(), "%f ", &resSlope) != 1 )
1140 // bug in the element 0x0028,0x1053
1141 gdcmWarningMacro( "Rescale Slope (0028,1053) is empty.");
1149 * \brief This function is intended to user who doesn't want
1150 * to have to manage a LUT and expects to get an RBG Pixel image
1151 * (or a monochrome one ...)
1152 * \warning to be used with GetImagePixels()
1153 * @return 1 if Gray level, 3 if Color (RGB, YBR, *or PALETTE COLOR*)
1155 int File::GetNumberOfScalarComponents()
1157 if ( GetSamplesPerPixel() == 3 )
1162 // 0028 0100 US IMG Bits Allocated
1163 // (in order no to be messed up by old RGB images)
1164 if ( GetEntryValue(0x0028,0x0100) == "24" )
1169 std::string strPhotometricInterpretation = GetEntryValue(0x0028,0x0004);
1171 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
1173 if ( HasLUT() )// PALETTE COLOR is NOT enough
1183 // beware of trailing space at end of string
1184 // DICOM tags are never of odd length
1185 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
1186 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
1187 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
1193 // we assume that *all* kinds of YBR are dealt with
1199 * \brief This function is intended to user that DOESN'T want
1200 * to get RGB pixels image when it's stored as a PALETTE COLOR image
1201 * - the (vtk) user is supposed to know how deal with LUTs -
1202 * \warning to be used with GetImagePixelsRaw()
1203 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
1205 int File::GetNumberOfScalarComponentsRaw()
1207 // 0028 0100 US IMG Bits Allocated
1208 // (in order no to be messed up by old RGB images)
1209 if ( File::GetEntryValue(0x0028,0x0100) == "24" )
1214 // we assume that *all* kinds of YBR are dealt with
1215 return GetSamplesPerPixel();
1219 * \brief Recover the offset (from the beginning of the file)
1220 * of *image* pixels (not *icone image* pixels, if any !)
1221 * @return Pixel Offset
1223 size_t File::GetPixelOffset()
1225 DocEntry *pxlElement = GetDocEntry(GrPixel, NumPixel);
1228 return pxlElement->GetOffset();
1232 gdcmDebugMacro( "Big trouble : Pixel Element ("
1233 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found" );
1239 * \brief Recover the pixel area length (in Bytes)
1240 * @return Pixel Element Length, as stored in the header
1241 * (NOT the memory space necessary to hold the Pixels
1242 * -in case of embeded compressed image-)
1243 * 0 : NOT USABLE file. The caller has to check.
1245 size_t File::GetPixelAreaLength()
1247 DocEntry *pxlElement = GetDocEntry(GrPixel, NumPixel);
1250 return pxlElement->GetLength();
1254 gdcmDebugMacro( "Big trouble : Pixel Element ("
1255 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found" );
1261 * \brief Adds the characteristics of a new element we want to anonymize
1262 * @param group Group number of the target tag.
1263 * @param elem Element number of the target tag.
1264 * @param value new value (string) to substitute with
1266 void File::AddAnonymizeElement (uint16_t group, uint16_t elem,
1267 std::string const &value)
1273 AnonymizeList.push_back(el);
1277 * \brief Overwrites in the file the values of the DicomElements
1280 void File::AnonymizeNoLoad()
1282 std::fstream *fp = new std::fstream(Filename.c_str(),
1283 std::ios::in | std::ios::out | std::ios::binary);
1287 uint32_t valLgth = 0;
1288 std::string *spaces;
1289 for (ListElements::iterator it = AnonymizeList.begin();
1290 it != AnonymizeList.end();
1293 d = GetDocEntry( (*it).Group, (*it).Elem);
1298 if ( dynamic_cast<SeqEntry *>(d) )
1300 gdcmWarningMacro( "You cannot 'Anonymize a SeqEntry ");
1304 offset = d->GetOffset();
1305 lgth = d->GetLength();
1308 spaces = new std::string( lgth-valLgth, ' ');
1309 (*it).Value = (*it).Value + *spaces;
1312 fp->seekp( offset, std::ios::beg );
1313 fp->write( (*it).Value.c_str(), lgth );
1321 * \brief anonymize a File (remove Patient's personal info passed with
1322 * AddAnonymizeElement()
1323 * \note You cannot Anonymize a BinEntry (to be fixed)
1325 bool File::AnonymizeFile()
1327 // If Anonymisation list is empty, let's perform some basic anonymization
1328 if ( AnonymizeList.begin() == AnonymizeList.end() )
1330 // If exist, replace by spaces
1331 SetValEntry (" ",0x0010, 0x2154); // Telephone
1332 SetValEntry (" ",0x0010, 0x1040); // Adress
1333 SetValEntry (" ",0x0010, 0x0020); // Patient ID
1335 DocEntry* patientNameHE = GetDocEntry (0x0010, 0x0010);
1337 if ( patientNameHE ) // we replace it by Study Instance UID (why not ?)
1339 std::string studyInstanceUID = GetEntryValue (0x0020, 0x000d);
1340 if ( studyInstanceUID != GDCM_UNFOUND )
1342 SetValEntry(studyInstanceUID, 0x0010, 0x0010);
1346 SetValEntry("anonymised", 0x0010, 0x0010);
1353 for (ListElements::iterator it = AnonymizeList.begin();
1354 it != AnonymizeList.end();
1357 d = GetDocEntry( (*it).Group, (*it).Elem);
1362 if ( dynamic_cast<SeqEntry *>(d) )
1364 gdcmWarningMacro( "You cannot 'Anonymize' a SeqEntry ");
1368 if ( dynamic_cast<BinEntry *>(d) )
1370 gdcmWarningMacro( "To 'Anonymize' a BinEntry, better use AnonymizeNoLoad (FIXME) ");
1374 SetValEntry ((*it).Value, (*it).Group, (*it).Elem);
1378 // In order to make definitively impossible any further identification
1379 // remove or replace all the stuff that contains a Date
1381 //0008 0012 DA ID Instance Creation Date
1382 //0008 0020 DA ID Study Date
1383 //0008 0021 DA ID Series Date
1384 //0008 0022 DA ID Acquisition Date
1385 //0008 0023 DA ID Content Date
1386 //0008 0024 DA ID Overlay Date
1387 //0008 0025 DA ID Curve Date
1388 //0008 002a DT ID Acquisition Datetime
1389 //0018 9074 DT ACQ Frame Acquisition Datetime
1390 //0018 9151 DT ACQ Frame Reference Datetime
1391 //0018 a002 DT ACQ Contribution Date Time
1392 //0020 3403 SH REL Modified Image Date (RET)
1393 //0032 0032 DA SDY Study Verified Date
1394 //0032 0034 DA SDY Study Read Date
1395 //0032 1000 DA SDY Scheduled Study Start Date
1396 //0032 1010 DA SDY Scheduled Study Stop Date
1397 //0032 1040 DA SDY Study Arrival Date
1398 //0032 1050 DA SDY Study Completion Date
1399 //0038 001a DA VIS Scheduled Admission Date
1400 //0038 001c DA VIS Scheduled Discharge Date
1401 //0038 0020 DA VIS Admitting Date
1402 //0038 0030 DA VIS Discharge Date
1403 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1404 //0040 0004 DA PRC Scheduled Procedure Step End Date
1405 //0040 0244 DA PRC Performed Procedure Step Start Date
1406 //0040 0250 DA PRC Performed Procedure Step End Date
1407 //0040 2004 DA PRC Issue Date of Imaging Service Request
1408 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1409 //0040 4011 DT PRC Expected Completion Date and Time
1410 //0040 a030 DT PRC Verification Date Time
1411 //0040 a032 DT PRC Observation Date Time
1412 //0040 a120 DT PRC DateTime
1413 //0040 a121 DA PRC Date
1414 //0040 a13a DT PRC Referenced Datetime
1415 //0070 0082 DA ??? Presentation Creation Date
1416 //0100 0420 DT ??? SOP Autorization Date and Time
1417 //0400 0105 DT ??? Digital Signature DateTime
1418 //2100 0040 DA PJ Creation Date
1419 //3006 0008 DA SSET Structure Set Date
1420 //3008 0024 DA ??? Treatment Control Point Date
1421 //3008 0054 DA ??? First Treatment Date
1422 //3008 0056 DA ??? Most Recent Treatment Date
1423 //3008 0162 DA ??? Safe Position Exit Date
1424 //3008 0166 DA ??? Safe Position Return Date
1425 //3008 0250 DA ??? Treatment Date
1426 //300a 0006 DA RT RT Plan Date
1427 //300a 022c DA RT Air Kerma Rate Reference Date
1428 //300e 0004 DA RT Review Date
1434 * \brief Performs some consistency checking on various 'File related'
1435 * (as opposed to 'DicomDir related') entries
1436 * then writes in a file all the (Dicom Elements) included the Pixels
1437 * @param fileName file name to write to
1438 * @param writetype type of the file to be written
1439 * (ACR, ExplicitVR, ImplicitVR)
1441 bool File::Write(std::string fileName, FileType writetype)
1443 std::ofstream *fp = new std::ofstream(fileName.c_str(),
1444 std::ios::out | std::ios::binary);
1447 gdcmWarningMacro("Failed to open (write) File: " << fileName.c_str());
1451 // Entry : 0002|0000 = group length -> recalculated
1452 ValEntry*e0000 = GetValEntry(0x0002,0x0000);
1455 std::ostringstream sLen;
1456 sLen << ComputeGroup0002Length(writetype);
1457 e0000->SetValue(sLen.str());
1460 int i_lgPix = GetEntryLength(GrPixel, NumPixel);
1463 // no (GrPixel, NumPixel) element
1464 std::string s_lgPix = Util::Format("%d", i_lgPix+12);
1465 s_lgPix = Util::DicomString( s_lgPix.c_str() );
1466 InsertValEntry(s_lgPix,GrPixel, 0x0000);
1469 Document::WriteContent(fp, writetype);
1477 //-----------------------------------------------------------------------------
1481 //-----------------------------------------------------------------------------
1484 * \brief Parse pixel data from disk of [multi-]fragment RLE encoding.
1485 * Compute the RLE extra information and store it in \ref RLEInfo
1486 * for later pixel retrieval usage.
1488 void File::ComputeRLEInfo()
1490 std::string ts = GetTransferSyntax();
1491 if ( !Global::GetTS()->IsRLELossless(ts) )
1496 // Encoded pixel data: for the time being we are only concerned with
1497 // Jpeg or RLE Pixel data encodings.
1498 // As stated in PS 3.5-2003, section 8.2 p44:
1499 // "If sent in Encapsulated Format (i.e. other than the Native Format) the
1500 // value representation OB is used".
1501 // Hence we expect an OB value representation. Concerning OB VR,
1502 // the section PS 3.5-2003, section A.4.c p 58-59, states:
1503 // "For the Value Representations OB and OW, the encoding shall meet the
1504 // following specifications depending on the Data element tag:"
1506 // - the first item in the sequence of items before the encoded pixel
1507 // data stream shall be basic offset table item. The basic offset table
1508 // item value, however, is not required to be present"
1509 ReadAndSkipEncapsulatedBasicOffsetTable();
1511 // Encapsulated RLE Compressed Images (see PS 3.5-2003, Annex G)
1512 // Loop on the individual frame[s] and store the information
1513 // on the RLE fragments in a RLEFramesInfo.
1514 // Note: - when only a single frame is present, this is a
1516 // - when more than one frame are present, then we are in
1517 // the case of a multi-frame image.
1519 while ( (frameLength = ReadTagLength(0xfffe, 0xe000)) != 0 )
1521 // Parse the RLE Header and store the corresponding RLE Segment
1522 // Offset Table information on fragments of this current Frame.
1523 // Note that the fragment pixels themselves are not loaded
1524 // (but just skipped).
1525 long frameOffset = Fp->tellg();
1527 uint32_t nbRleSegments = ReadInt32();
1528 if ( nbRleSegments > 16 )
1530 // There should be at most 15 segments (refer to RLEFrame class)
1531 gdcmWarningMacro( "Too many segments.");
1534 uint32_t rleSegmentOffsetTable[16];
1535 for( int k = 1; k <= 15; k++ )
1537 rleSegmentOffsetTable[k] = ReadInt32();
1540 // Deduce from both RLE Header and frameLength
1541 // the fragment length, and again store this info
1542 // in a RLEFramesInfo.
1543 long rleSegmentLength[15];
1544 // skipping (not reading) RLE Segments
1545 if ( nbRleSegments > 1)
1547 for(unsigned int k = 1; k <= nbRleSegments-1; k++)
1549 rleSegmentLength[k] = rleSegmentOffsetTable[k+1]
1550 - rleSegmentOffsetTable[k];
1551 SkipBytes(rleSegmentLength[k]);
1555 rleSegmentLength[nbRleSegments] = frameLength
1556 - rleSegmentOffsetTable[nbRleSegments];
1557 SkipBytes(rleSegmentLength[nbRleSegments]);
1559 // Store the collected info
1560 RLEFrame *newFrame = new RLEFrame;
1561 newFrame->SetNumberOfFragments(nbRleSegments);
1562 for( unsigned int uk = 1; uk <= nbRleSegments; uk++ )
1564 newFrame->SetOffset(uk,frameOffset + rleSegmentOffsetTable[uk]);
1565 newFrame->SetLength(uk,rleSegmentLength[uk]);
1567 RLEInfo->AddFrame(newFrame);
1570 // Make sure that we encounter a 'Sequence Delimiter Item'
1571 // at the end of the item :
1572 if ( !ReadTag(0xfffe, 0xe0dd) )
1574 gdcmWarningMacro( "No sequence delimiter item at end of RLE item sequence");
1579 * \brief Parse pixel data from disk of [multi-]fragment Jpeg encoding.
1580 * Compute the jpeg extra information (fragment[s] offset[s] and
1581 * length) and store it[them] in \ref JPEGInfo for later pixel
1584 void File::ComputeJPEGFragmentInfo()
1586 // If you need to, look for comments of ComputeRLEInfo().
1587 std::string ts = GetTransferSyntax();
1588 if ( ! Global::GetTS()->IsJPEG(ts) )
1593 ReadAndSkipEncapsulatedBasicOffsetTable();
1595 // Loop on the fragments[s] and store the parsed information in a
1597 long fragmentLength;
1598 while ( (fragmentLength = ReadTagLength(0xfffe, 0xe000)) != 0 )
1600 long fragmentOffset = Fp->tellg();
1602 // Store the collected info
1603 JPEGFragment *newFragment = new JPEGFragment;
1604 newFragment->SetOffset(fragmentOffset);
1605 newFragment->SetLength(fragmentLength);
1606 JPEGInfo->AddFragment(newFragment);
1608 SkipBytes(fragmentLength);
1611 // Make sure that we encounter a 'Sequence Delimiter Item'
1612 // at the end of the item :
1613 if ( !ReadTag(0xfffe, 0xe0dd) )
1615 gdcmWarningMacro( "No sequence delimiter item at end of JPEG item sequence");
1620 * \brief Assuming the internal file pointer \ref Document::Fp
1621 * is placed at the beginning of a tag check whether this
1622 * tag is (TestGroup, TestElem).
1623 * \warning On success the internal file pointer \ref Document::Fp
1624 * is modified to point after the tag.
1625 * On failure (i.e. when the tag wasn't the expected tag
1626 * (TestGroup, TestElem) the internal file pointer
1627 * \ref Document::Fp is restored to it's original position.
1628 * @param testGroup The expected group of the tag.
1629 * @param testElem The expected Element of the tag.
1630 * @return True on success, false otherwise.
1632 bool File::ReadTag(uint16_t testGroup, uint16_t testElem)
1634 long positionOnEntry = Fp->tellg();
1635 long currentPosition = Fp->tellg(); // On debugging purposes
1637 // Read the Item Tag group and element, and make
1638 // sure they are what we expected:
1639 uint16_t itemTagGroup;
1640 uint16_t itemTagElem;
1643 itemTagGroup = ReadInt16();
1644 itemTagElem = ReadInt16();
1646 catch ( FormatError e )
1648 //std::cerr << e << std::endl;
1651 if ( itemTagGroup != testGroup || itemTagElem != testElem )
1653 gdcmWarningMacro( "Wrong Item Tag found:"
1654 << " We should have found tag ("
1655 << std::hex << testGroup << "," << testElem << ")" << std::endl
1656 << " but instead we encountered tag ("
1657 << std::hex << itemTagGroup << "," << itemTagElem << ")"
1658 << " at address: " << " 0x(" << (unsigned int)currentPosition << ")"
1660 Fp->seekg(positionOnEntry, std::ios::beg);
1668 * \brief Assuming the internal file pointer \ref Document::Fp
1669 * is placed at the beginning of a tag (TestGroup, TestElement),
1670 * read the length associated to the Tag.
1671 * \warning On success the internal file pointer \ref Document::Fp
1672 * is modified to point after the tag and it's length.
1673 * On failure (i.e. when the tag wasn't the expected tag
1674 * (TestGroup, TestElement) the internal file pointer
1675 * \ref Document::Fp is restored to it's original position.
1676 * @param testGroup The expected Group of the tag.
1677 * @param testElem The expected Element of the tag.
1678 * @return On success returns the length associated to the tag. On failure
1681 uint32_t File::ReadTagLength(uint16_t testGroup, uint16_t testElem)
1684 if ( !ReadTag(testGroup, testElem) )
1689 //// Then read the associated Item Length
1690 long currentPosition = Fp->tellg();
1691 uint32_t itemLength = ReadInt32();
1693 gdcmWarningMacro( "Basic Item Length is: "
1694 << itemLength << std::endl
1695 << " at address: " << std::hex << (unsigned int)currentPosition);
1701 * \brief When parsing the Pixel Data of an encapsulated file, read
1702 * the basic offset table (when present, and BTW dump it).
1704 void File::ReadAndSkipEncapsulatedBasicOffsetTable()
1706 //// Read the Basic Offset Table Item Tag length...
1707 uint32_t itemLength = ReadTagLength(0xfffe, 0xe000);
1709 // When present, read the basic offset table itself.
1710 // Notes: - since the presence of this basic offset table is optional
1711 // we can't rely on it for the implementation, and we will simply
1712 // trash it's content (when present).
1713 // - still, when present, we could add some further checks on the
1714 // lengths, but we won't bother with such fuses for the time being.
1715 if ( itemLength != 0 )
1717 char *basicOffsetTableItemValue = new char[itemLength + 1];
1718 Fp->read(basicOffsetTableItemValue, itemLength);
1721 for (unsigned int i=0; i < itemLength; i += 4 )
1723 uint32_t individualLength = str2num( &basicOffsetTableItemValue[i],
1725 gdcmWarningMacro( "Read one length: " <<
1726 std::hex << individualLength );
1730 delete[] basicOffsetTableItemValue;
1734 // These are the deprecated method that one day should be removed (after the next release)
1736 #ifndef GDCM_LEGACY_REMOVE
1738 * \brief Constructor (DEPRECATED : temporaryly kept not to break the API)
1739 * @param filename name of the file whose header we want to analyze
1740 * @deprecated do not use any longer
1742 File::File( std::string const &filename )
1745 RLEInfo = new RLEFramesInfo;
1746 JPEGInfo = new JPEGFragmentsInfo;
1748 Load( filename ); // gdcm::Document is first Loaded, then the 'File part'
1752 * \brief Loader. (DEPRECATED : temporaryly kept not to break the API)
1753 * @param fileName file to be open for parsing
1754 * @return false if file cannot be open or no swap info was found,
1755 * or no tag was found.
1756 * @deprecated Use the Load() [ + SetLoadMode() ] + SetFileName() functions instead
1758 bool File::Load( std::string const &fileName )
1760 GDCM_LEGACY_REPLACED_BODY(File::Load(std::string), "1.2",
1762 SetFileName( fileName );
1763 if ( ! this->Document::Load( ) )
1766 return DoTheLoadingJob( );
1770 //-----------------------------------------------------------------------------
1773 //-----------------------------------------------------------------------------
1774 } // end namespace gdcm