1 /*=========================================================================
4 Module: $RCSfile: gdcmFile.cxx,v $
6 Date: $Date: 2005/07/30 18:17:08 $
7 Version: $Revision: 1.264 $
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) VM=3 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
35 // - Image Orientation (Patient) (0020,0037), VM=6
36 // - Image Position Patient (0020,0032), VM=3
37 // then attribute Patient Orientation (0020,0020) should not be present
41 // Patient Position (0018,5100) values : HFP = Head First-Prone
42 // HFS = Head First-Supine
43 // HFDR = Head First-Decubitus Right
44 // HFDL = Head First-Decubitus Left
45 // FFDR = Feet First-Decubitus Right
46 // FFDL = Feet First-Decubitus Left
47 // FFP = Feet First-Prone
48 // FFS = Feet First-Supine
49 // can also find SEMIERECT
51 // CS 2 Patient Orientation (0020 0020)
52 // When the coordinates of the image
53 // are always present, this field is almost never used.
54 // Better we don't tust it too much ...
55 // Found Values are : L\P
62 // (0020|0037) [Image Orientation (Patient)] [1\0\0\0\1\0 ]
65 // ---------------------------------------------------------------
68 #include "gdcmGlobal.h"
70 #include "gdcmDebug.h"
72 #include "gdcmValEntry.h"
73 #include "gdcmBinEntry.h"
74 #include "gdcmSeqEntry.h"
75 #include "gdcmRLEFramesInfo.h"
76 #include "gdcmJPEGFragmentsInfo.h"
79 #include <stdio.h> //sscanf
80 #include <stdlib.h> // for atoi
85 //-----------------------------------------------------------------------------
86 // Constructor / Destructor
89 * \brief Constructor used when we want to generate dicom files from scratch
94 RLEInfo = new RLEFramesInfo;
95 JPEGInfo = new JPEGFragmentsInfo;
96 GrPixel = 0x7fe0; // to avoid further troubles
102 * \brief Canonical destructor.
112 //-----------------------------------------------------------------------------
116 * @return false if file cannot be open or no swap info was found,
117 * or no tag was found.
121 if ( ! this->Document::Load( ) )
124 return DoTheLoadingJob( );
128 * \brief Does the Loading Job (internal use only)
129 * @return false if file cannot be open or no swap info was found,
130 * or no tag was found.
132 bool File::DoTheLoadingJob( )
135 // for some ACR-NEMA images GrPixel, NumPixel is *not* 7fe0,0010
136 // We may encounter the 'RETired' (0x0028, 0x0200) tag
137 // (Image Location") . This entry contains the number of
138 // the group that contains the pixel data (hence the "Pixel Data"
139 // is found by indirection through the "Image Location").
140 // Inside the group pointed by "Image Location" the searched element
141 // is conventionally the element 0x0010 (when the norm is respected).
142 // When the "Image Location" is missing we default to group 0x7fe0.
143 // Note: this IS the right place for the code
146 const std::string &imgLocation = GetEntryValue(0x0028, 0x0200);
147 if ( imgLocation == GDCM_UNFOUND )
154 GrPixel = (uint16_t) atoi( imgLocation.c_str() );
157 // sometimes Image Location value doesn't follow
158 // the supposed processor endianness.
159 // see gdcmData/cr172241.dcm
160 if ( GrPixel == 0xe07f )
165 if ( GrPixel != 0x7fe0 )
167 // This is a kludge for old dirty Philips imager.
175 // Now, we know GrPixel and NumPixel.
176 // Let's create a VirtualDictEntry to allow a further VR modification
177 // and force VR to match with BitsAllocated.
178 DocEntry *entry = GetDocEntry(GrPixel, NumPixel);
181 // Compute the RLE or JPEG info
183 const std::string &ts = GetTransferSyntax();
184 Fp->seekg( entry->GetOffset(), std::ios::beg );
185 if ( Global::GetTS()->IsRLELossless(ts) )
187 else if ( Global::GetTS()->IsJPEG(ts) )
188 ComputeJPEGFragmentInfo();
191 // Create a new BinEntry to change the DictEntry
192 // The changed DictEntry will have
193 // - a correct PixelVR OB or OW)
194 // - the name to "Pixel Data"
195 BinEntry *oldEntry = dynamic_cast<BinEntry *>(entry);
199 // 8 bits allocated is a 'O Bytes' , as well as 24 (old ACR-NEMA RGB)
200 // more than 8 (i.e 12, 16) is a 'O Words'
201 if ( GetBitsAllocated() == 8 || GetBitsAllocated() == 24 )
206 // Change only made if usefull
207 if ( PixelVR != oldEntry->GetVR() )
209 DictEntry* newDict = NewVirtualDictEntry(GrPixel,NumPixel,
210 PixelVR,"1","Pixel Data");
212 BinEntry *newEntry = new BinEntry(newDict);
213 newEntry->Copy(entry);
214 newEntry->SetBinArea(oldEntry->GetBinArea(),oldEntry->IsSelfArea());
215 oldEntry->SetSelfArea(false);
217 RemoveEntry(oldEntry);
225 * \brief This predicate, based on hopefully reasonable heuristics,
226 * decides whether or not the current File was properly parsed
227 * and contains the mandatory information for being considered as
228 * a well formed and usable Dicom/Acr File.
229 * @return true when File is the one of a reasonable Dicom/Acr file,
232 bool File::IsReadable()
234 if ( !Document::IsReadable() )
239 const std::string &res = GetEntryValue(0x0028, 0x0005);
240 if ( res != GDCM_UNFOUND && atoi(res.c_str()) > 4 )
242 gdcmWarningMacro("Wrong Image Dimensions" << res);
243 return false; // Image Dimensions
245 bool b0028_0100 = true;
246 if ( !GetDocEntry(0x0028, 0x0100) )
248 gdcmWarningMacro("Bits Allocated (0028|0100) not found");
249 //return false; // "Bits Allocated"
252 bool b0028_0101 = true;
253 if ( !GetDocEntry(0x0028, 0x0101) )
255 gdcmWarningMacro("Bits Stored (0028|0101) not found");
256 //return false; // "Bits Stored"
259 bool b0028_0102 = true;
260 if ( !GetDocEntry(0x0028, 0x0102) )
262 gdcmWarningMacro("Hight Bit (0028|0102) not found");
263 //return false; // "High Bit"
266 bool b0028_0103 = true;
267 if ( !GetDocEntry(0x0028, 0x0103) )
269 gdcmWarningMacro("Pixel Representation (0028|0103) not found");
270 //return false; // "Pixel Representation" i.e. 'Sign' ( 0 : unsigned, 1 : signed)
274 if ( !b0028_0100 && !b0028_0101 && !b0028_0102 && !b0028_0103)
276 gdcmWarningMacro("Too much mandatory Tags missing !");
280 if ( !GetDocEntry(GrPixel, NumPixel) )
282 gdcmWarningMacro("Pixel Dicom Element " << std::hex <<
283 GrPixel << "|" << NumPixel << "not found");
284 return false; // Pixel Dicom Element not found :-(
290 * \brief gets the info from 0020,0013 : Image Number else 0.
291 * @return image number
293 int File::GetImageNumber()
295 //0020 0013 : Image Number
296 std::string strImNumber = GetEntryValue(0x0020,0x0013);
297 if ( strImNumber != GDCM_UNFOUND )
299 return atoi( strImNumber.c_str() );
305 * \brief gets the info from 0008,0060 : Modality
306 * @return Modality Type
308 ModalityType File::GetModality()
310 // 0008 0060 : Modality
311 std::string strModality = GetEntryValue(0x0008,0x0060);
312 if ( strModality != GDCM_UNFOUND )
314 if ( strModality.find("AU") < strModality.length()) return AU;
315 else if ( strModality.find("AS") < strModality.length()) return AS;
316 else if ( strModality.find("BI") < strModality.length()) return BI;
317 else if ( strModality.find("CF") < strModality.length()) return CF;
318 else if ( strModality.find("CP") < strModality.length()) return CP;
319 else if ( strModality.find("CR") < strModality.length()) return CR;
320 else if ( strModality.find("CT") < strModality.length()) return CT;
321 else if ( strModality.find("CS") < strModality.length()) return CS;
322 else if ( strModality.find("DD") < strModality.length()) return DD;
323 else if ( strModality.find("DF") < strModality.length()) return DF;
324 else if ( strModality.find("DG") < strModality.length()) return DG;
325 else if ( strModality.find("DM") < strModality.length()) return DM;
326 else if ( strModality.find("DS") < strModality.length()) return DS;
327 else if ( strModality.find("DX") < strModality.length()) return DX;
328 else if ( strModality.find("ECG") < strModality.length()) return ECG;
329 else if ( strModality.find("EPS") < strModality.length()) return EPS;
330 else if ( strModality.find("FA") < strModality.length()) return FA;
331 else if ( strModality.find("FS") < strModality.length()) return FS;
332 else if ( strModality.find("HC") < strModality.length()) return HC;
333 else if ( strModality.find("HD") < strModality.length()) return HD;
334 else if ( strModality.find("LP") < strModality.length()) return LP;
335 else if ( strModality.find("LS") < strModality.length()) return LS;
336 else if ( strModality.find("MA") < strModality.length()) return MA;
337 else if ( strModality.find("MR") < strModality.length()) return MR;
338 else if ( strModality.find("NM") < strModality.length()) return NM;
339 else if ( strModality.find("OT") < strModality.length()) return OT;
340 else if ( strModality.find("PT") < strModality.length()) return PT;
341 else if ( strModality.find("RF") < strModality.length()) return RF;
342 else if ( strModality.find("RG") < strModality.length()) return RG;
343 else if ( strModality.find("RTDOSE")
344 < strModality.length()) return RTDOSE;
345 else if ( strModality.find("RTIMAGE")
346 < strModality.length()) return RTIMAGE;
347 else if ( strModality.find("RTPLAN")
348 < strModality.length()) return RTPLAN;
349 else if ( strModality.find("RTSTRUCT")
350 < strModality.length()) return RTSTRUCT;
351 else if ( strModality.find("SM") < strModality.length()) return SM;
352 else if ( strModality.find("ST") < strModality.length()) return ST;
353 else if ( strModality.find("TG") < strModality.length()) return TG;
354 else if ( strModality.find("US") < strModality.length()) return US;
355 else if ( strModality.find("VF") < strModality.length()) return VF;
356 else if ( strModality.find("XA") < strModality.length()) return XA;
357 else if ( strModality.find("XC") < strModality.length()) return XC;
361 /// \todo throw error return value ???
362 /// specified <> unknown in our database
370 * \brief Retrieve the number of columns of image.
371 * @return The encountered size when found, 0 by default.
372 * 0 means the file is NOT USABLE. The caller will have to check
376 const std::string &strSize = GetEntryValue(0x0028,0x0011);
377 if ( strSize == GDCM_UNFOUND )
381 return atoi( strSize.c_str() );
385 * \brief Retrieve the number of lines of image.
386 * \warning The defaulted value is 1 as opposed to File::GetXSize()
387 * @return The encountered size when found, 1 by default
388 * (The ACR-NEMA file contains a Signal, not an Image).
392 const std::string &strSize = GetEntryValue(0x0028,0x0010);
393 if ( strSize != GDCM_UNFOUND )
395 return atoi( strSize.c_str() );
402 // The Rows (0028,0010) entry was optional for ACR/NEMA.
403 // (at least some images didn't have it.)
404 // It might hence be a signal (1D image). So we default to 1:
409 * \brief Retrieve the number of planes of volume or the number
410 * of frames of a multiframe.
411 * \warning When present we consider the "Number of Frames" as the third
412 * dimension. When missing we consider the third dimension as
413 * being the ACR-NEMA "Planes" tag content.
414 * @return The encountered size when found, 1 by default (single image).
418 // Both DicomV3 and ACR/Nema consider the "Number of Frames"
419 // as the third dimension.
420 const std::string &strSize = GetEntryValue(0x0028,0x0008);
421 if ( strSize != GDCM_UNFOUND )
423 return atoi( strSize.c_str() );
426 // We then consider the "Planes" entry as the third dimension
427 const std::string &strSize2 = GetEntryValue(0x0028,0x0012);
428 if ( strSize2 != GDCM_UNFOUND )
430 return atoi( strSize2.c_str() );
436 * \brief gets the info from 0018,1164 : ImagerPixelSpacing
437 * then 0028,0030 : Pixel Spacing
439 * @return X dimension of a pixel
441 float File::GetXSpacing()
443 float xspacing = 1.0;
444 float yspacing = 1.0;
447 // To follow David Clunie's advice, we first check ImagerPixelSpacing
449 const std::string &strImagerPixelSpacing = GetEntryValue(0x0018,0x1164);
450 if ( strImagerPixelSpacing != GDCM_UNFOUND )
452 if ( ( nbValues = sscanf( strImagerPixelSpacing.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 )
469 const std::string &strSpacing = GetEntryValue(0x0028,0x0030);
471 if ( strSpacing == GDCM_UNFOUND )
473 gdcmWarningMacro( "Unfound Pixel Spacing (0028,0030)" );
477 if ( ( nbValues = sscanf( strSpacing.c_str(),
478 "%f \\%f ", &yspacing, &xspacing)) != 2 )
480 // if no values, xspacing is set to 1.0
483 // if single value is found, xspacing is defaulted to yspacing
487 if ( xspacing == 0.0 )
493 // to avoid troubles with David Clunie's-like images (at least one)
494 if ( xspacing == 0. && yspacing == 0.)
499 gdcmWarningMacro("gdcmData/CT-MONO2-8-abdo.dcm-like problem");
500 // seems to be a bug in the header ...
501 nbValues = sscanf( strSpacing.c_str(), "%f \\0\\%f ", &yspacing, &xspacing);
502 gdcmAssertMacro( nbValues == 2 );
509 * \brief gets the info from 0018,1164 : ImagerPixelSpacing
510 * then from 0028,0030 : Pixel Spacing
512 * @return Y dimension of a pixel
514 float File::GetYSpacing()
518 // To follow David Clunie's advice, we first check ImagerPixelSpacing
520 const std::string &strImagerPixelSpacing = GetEntryValue(0x0018,0x1164);
521 if ( strImagerPixelSpacing != GDCM_UNFOUND )
523 nbValues = sscanf( strImagerPixelSpacing.c_str(), "%f", &yspacing);
525 // if sscanf cannot read any float value, it won't affect yspacing
529 if ( yspacing == 0.0 )
535 std::string strSpacing = GetEntryValue(0x0028,0x0030);
536 if ( strSpacing == GDCM_UNFOUND )
538 gdcmWarningMacro("Unfound Pixel Spacing (0028,0030)");
542 // if sscanf cannot read any float value, it won't affect yspacing
543 nbValues = sscanf( strSpacing.c_str(), "%f", &yspacing);
545 // if no values, yspacing is set to 1.0
549 if ( yspacing == 0.0 )
556 * \brief gets the info from 0018,0088 : Space Between Slices
557 * else from 0018,0050 : Slice Thickness
559 * @return Z dimension of a voxel-to be
561 float File::GetZSpacing()
563 // Spacing Between Slices : distance between the middle of 2 slices
565 // jointives (Spacing between Slices = Slice Thickness)
566 // overlapping (Spacing between Slices < Slice Thickness)
567 // disjointes (Spacing between Slices > Slice Thickness)
568 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
569 // It only concerns the MRI guys, not people wanting to visualize volumes
570 // If Spacing Between Slices is missing,
571 // we suppose slices joint together
573 const std::string &strSpacingBSlices = GetEntryValue(0x0018,0x0088);
575 if ( strSpacingBSlices == GDCM_UNFOUND )
577 gdcmWarningMacro("Unfound Spacing Between Slices (0018,0088)");
578 const std::string &strSliceThickness = GetEntryValue(0x0018,0x0050);
579 if ( strSliceThickness == GDCM_UNFOUND )
581 gdcmWarningMacro("Unfound Slice Thickness (0018,0050)");
586 // if no 'Spacing Between Slices' is found,
587 // we assume slices join together
588 // (no overlapping, no interslice gap)
589 // if they don't, we're fucked up
590 return (float)atof( strSliceThickness.c_str() );
594 return (float)atof( strSpacingBSlices.c_str() );
598 * \brief gets the info from 0020,0032 : Image Position Patient
599 * else from 0020,0030 : Image Position (RET)
601 * @return up-left image corner X position
603 float File::GetXOrigin()
605 float xImPos, yImPos, zImPos;
606 std::string strImPos = GetEntryValue(0x0020,0x0032);
608 if ( strImPos == GDCM_UNFOUND )
610 gdcmWarningMacro( "Unfound Image Position Patient (0020,0032)");
611 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
612 if ( strImPos == GDCM_UNFOUND )
614 gdcmWarningMacro( "Unfound Image Position (RET) (0020,0030)");
619 if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos) != 3 )
628 * \brief gets the info from 0020,0032 : Image Position Patient
629 * else from 0020,0030 : Image Position (RET)
631 * @return up-left image corner Y position
633 float File::GetYOrigin()
635 float xImPos, yImPos, zImPos;
636 std::string strImPos = GetEntryValue(0x0020,0x0032);
638 if ( strImPos == GDCM_UNFOUND)
640 gdcmWarningMacro( "Unfound Image Position Patient (0020,0032)");
641 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
642 if ( strImPos == GDCM_UNFOUND )
644 gdcmWarningMacro( "Unfound Image Position (RET) (0020,0030)");
649 if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos) != 3 )
658 * \brief gets the info from 0020,0032 : Image Position Patient
659 * else from 0020,0030 : Image Position (RET)
660 * else from 0020,1041 : Slice Location
661 * else from 0020,0050 : Location
663 * @return up-left image corner Z position
665 float File::GetZOrigin()
667 float xImPos, yImPos, zImPos;
668 std::string strImPos = GetEntryValue(0x0020,0x0032);
670 if ( strImPos != GDCM_UNFOUND )
672 if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos) != 3)
674 gdcmWarningMacro( "Wrong Image Position Patient (0020,0032)");
675 return 0.; // bug in the element 0x0020,0x0032
683 strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
684 if ( strImPos != GDCM_UNFOUND )
686 if ( sscanf( strImPos.c_str(),
687 "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos ) != 3 )
689 gdcmWarningMacro( "Wrong Image Position (RET) (0020,0030)");
690 return 0.; // bug in the element 0x0020,0x0032
698 // for *very* old ACR-NEMA images
699 std::string strSliceLocation = GetEntryValue(0x0020,0x1041);
700 if ( strSliceLocation != GDCM_UNFOUND )
702 if ( sscanf( strSliceLocation.c_str(), "%f ", &zImPos) != 1)
704 gdcmWarningMacro( "Wrong Slice Location (0020,1041)");
705 return 0.; // bug in the element 0x0020,0x1041
712 gdcmWarningMacro( "Unfound Slice Location (0020,1041)");
714 std::string strLocation = GetEntryValue(0x0020,0x0050);
715 if ( strLocation != GDCM_UNFOUND )
717 if ( sscanf( strLocation.c_str(), "%f ", &zImPos) != 1 )
719 gdcmWarningMacro( "Wrong Location (0020,0050)");
720 return 0.; // bug in the element 0x0020,0x0050
727 gdcmWarningMacro( "Unfound Location (0020,0050)");
729 return 0.; // Hopeless
733 * \brief gets the info from 0020,0037 : Image Orientation Patient
734 * (needed to organize DICOM files based on their x,y,z position)
735 * @param iop adress of the (6)float array to receive values
736 * @return cosines of image orientation patient
738 bool File::GetImageOrientationPatient( float iop[6] )
740 std::string strImOriPat;
741 //iop is supposed to be float[6]
742 iop[0] = iop[1] = iop[2] = iop[3] = iop[4] = iop[5] = 0.;
744 // 0020 0037 DS REL Image Orientation (Patient)
745 if ( (strImOriPat = GetEntryValue(0x0020,0x0037)) != GDCM_UNFOUND )
747 if ( sscanf( strImOriPat.c_str(), "%f \\ %f \\%f \\%f \\%f \\%f ",
748 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
750 gdcmWarningMacro( "Wrong Image Orientation Patient (0020,0037). Less than 6 values were found." );
755 // 0020 0035 DS REL Image Orientation (RET)
756 else if ( (strImOriPat = GetEntryValue(0x0020,0x0035)) != GDCM_UNFOUND )
758 if ( sscanf( strImOriPat.c_str(), "%f \\ %f \\%f \\%f \\%f \\%f ",
759 &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
761 gdcmWarningMacro( "wrong Image Orientation Patient (0020,0035). Less than 6 values were found." );
769 * \brief Retrieve the number of Bits Stored (actually used)
770 * (as opposed to number of Bits Allocated)
771 * @return The encountered number of Bits Stored, 0 by default.
772 * 0 means the file is NOT USABLE. The caller has to check it !
774 int File::GetBitsStored()
776 std::string strSize = GetEntryValue( 0x0028, 0x0101 );
777 if ( strSize == GDCM_UNFOUND )
779 gdcmWarningMacro("(0028,0101) is supposed to be mandatory");
780 return 0; // It's supposed to be mandatory
781 // the caller will have to check
783 return atoi( strSize.c_str() );
787 * \brief Retrieve the number of Bits Allocated
788 * (8, 12 -compacted ACR-NEMA files-, 16, 24 -old RGB ACR-NEMA files-,)
789 * @return The encountered Number of Bits Allocated, 0 by default.
790 * 0 means the file is NOT USABLE. The caller has to check it !
792 int File::GetBitsAllocated()
794 std::string strSize = GetEntryValue(0x0028,0x0100);
795 if ( strSize == GDCM_UNFOUND )
797 gdcmWarningMacro( "(0028,0100) is supposed to be mandatory");
798 return 0; // It's supposed to be mandatory
799 // the caller will have to check
801 return atoi( strSize.c_str() );
805 * \brief Retrieve the high bit position.
806 * \warning The method defaults to 0 when information is missing.
807 * The responsability of checking this value is left to the caller.
808 * @return The high bit position when present. 0 when missing.
810 int File::GetHighBitPosition()
812 std::string strSize = GetEntryValue( 0x0028, 0x0102 );
813 if ( strSize == GDCM_UNFOUND )
815 gdcmWarningMacro( "(0028,0102) is supposed to be mandatory");
818 return atoi( strSize.c_str() );
822 * \brief Retrieve the number of Samples Per Pixel
823 * (1 : gray level, 3 : RGB/YBR -1 or 3 Planes-)
824 * @return The encountered number of Samples Per Pixel, 1 by default.
825 * (we assume Gray level Pixels)
827 int File::GetSamplesPerPixel()
829 const std::string &strSize = GetEntryValue(0x0028,0x0002);
830 if ( strSize == GDCM_UNFOUND )
832 gdcmWarningMacro( "(0028,0002) is supposed to be mandatory");
833 return 1; // Well, it's supposed to be mandatory ...
834 // but sometimes it's missing : *we* assume Gray pixels
836 return atoi( strSize.c_str() );
840 * \brief Retrieve the Planar Configuration for RGB images
841 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
842 * @return The encountered Planar Configuration, 0 by default.
844 int File::GetPlanarConfiguration()
846 std::string strSize = GetEntryValue(0x0028,0x0006);
847 if ( strSize == GDCM_UNFOUND )
849 gdcmWarningMacro( "Not found : Planar Configuration (0028,0006)");
852 return atoi( strSize.c_str() );
856 * \brief Return the size (in bytes) of a single pixel of data.
857 * @return The size in bytes of a single pixel of data; 0 by default
858 * 0 means the file is NOT USABLE; the caller will have to check
860 int File::GetPixelSize()
862 // 0028 0100 US IMG Bits Allocated
863 // (in order no to be messed up by old ACR-NEMA RGB images)
864 // if (File::GetEntryValue(0x0028,0x0100) == "24")
867 std::string pixelType = GetPixelType();
868 if ( pixelType == "8U" || pixelType == "8S" )
872 if ( pixelType == "16U" || pixelType == "16S")
876 if ( pixelType == "32U" || pixelType == "32S")
880 if ( pixelType == "FD" )
884 gdcmWarningMacro( "Unknown pixel type");
889 * \brief Build the Pixel Type of the image.
890 * Possible values are:
891 * - 8U unsigned 8 bit,
893 * - 16U unsigned 16 bit,
894 * - 16S signed 16 bit,
895 * - 32U unsigned 32 bit,
896 * - 32S signed 32 bit,
897 * - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
898 * \warning 12 bit images appear as 16 bit.
899 * 24 bit images appear as 8 bit + photochromatic interp ="RGB "
900 * + Planar Configuration = 0
901 * @return 0S if nothing found. NOT USABLE file. The caller has to check
903 std::string File::GetPixelType()
905 std::string bitsAlloc = GetEntryValue(0x0028, 0x0100); // Bits Allocated
906 if ( bitsAlloc == GDCM_UNFOUND )
908 gdcmWarningMacro( "Missing Bits Allocated (0028,0100)");
909 bitsAlloc = "16"; // default and arbitrary value, not to polute the output
912 if ( bitsAlloc == "64" )
916 else if ( bitsAlloc == "12" )
918 // It will be unpacked
921 else if ( bitsAlloc == "24" )
923 // (in order no to be messed up by old RGB images)
927 std::string sign = GetEntryValue(0x0028, 0x0103);//"Pixel Representation"
929 if (sign == GDCM_UNFOUND )
931 gdcmWarningMacro( "Missing Pixel Representation (0028,0103)");
932 sign = "U"; // default and arbitrary value, not to polute the output
934 else if ( sign == "0" )
942 return bitsAlloc + sign;
946 * \brief Check whether the pixels are signed (1) or UNsigned (0) data.
947 * \warning The method defaults to false (UNsigned) when tag 0028|0103
949 * The responsability of checking this value is left to the caller
950 * (NO transformation is performed on the pixels to make then >0)
951 * @return True when signed, false when UNsigned
953 bool File::IsSignedPixelData()
955 std::string strSign = GetEntryValue( 0x0028, 0x0103 );
956 if ( strSign == GDCM_UNFOUND )
958 gdcmWarningMacro( "(0028,0103) is supposed to be mandatory");
961 int sign = atoi( strSign.c_str() );
970 * \brief Check whether this a monochrome picture (gray levels) or not,
971 * using "Photometric Interpretation" tag (0x0028,0x0004).
972 * @return true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
974 bool File::IsMonochrome()
976 const std::string &PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
977 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
978 || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
982 if ( PhotometricInterp == GDCM_UNFOUND )
984 gdcmWarningMacro( "Not found : Photometric Interpretation (0028,0004)");
990 * \brief Check whether this a MONOCHROME1 picture (high values = dark)
991 * or not using "Photometric Interpretation" tag (0x0028,0x0004).
992 * @return true when "MONOCHROME1" . False otherwise.
994 bool File::IsMonochrome1()
996 const std::string &PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
997 if ( Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1") )
1001 if ( PhotometricInterp == GDCM_UNFOUND )
1003 gdcmWarningMacro( "Not found : Photometric Interpretation (0028,0004)");
1009 * \brief Check whether this a "PALETTE COLOR" picture or not by accessing
1010 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
1011 * @return true when "PALETTE COLOR". False otherwise.
1013 bool File::IsPaletteColor()
1015 std::string PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
1016 if ( PhotometricInterp == "PALETTE COLOR " )
1020 if ( PhotometricInterp == GDCM_UNFOUND )
1022 gdcmWarningMacro( "Not found : Palette color (0028,0004)");
1028 * \brief Check whether this a "YBR_FULL" color picture or not by accessing
1029 * the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
1030 * @return true when "YBR_FULL". False otherwise.
1032 bool File::IsYBRFull()
1034 std::string PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
1035 if ( PhotometricInterp == "YBR_FULL" )
1039 if ( PhotometricInterp == GDCM_UNFOUND )
1041 gdcmWarningMacro( "Not found : YBR Full (0028,0004)");
1047 * \brief tells us if LUT are used
1048 * \warning Right now, 'Segmented xxx Palette Color Lookup Table Data'
1049 * are NOT considered as LUT, since nobody knows
1050 * how to deal with them
1051 * Please warn me if you know sbdy that *does* know ... jprx
1052 * @return true if LUT Descriptors and LUT Tables were found
1056 // Check the presence of the LUT Descriptors, and LUT Tables
1058 if ( !GetDocEntry(0x0028,0x1101) )
1062 // LutDescriptorGreen
1063 if ( !GetDocEntry(0x0028,0x1102) )
1067 // LutDescriptorBlue
1068 if ( !GetDocEntry(0x0028,0x1103) )
1072 // Red Palette Color Lookup Table Data
1073 if ( !GetDocEntry(0x0028,0x1201) )
1077 // Green Palette Color Lookup Table Data
1078 if ( !GetDocEntry(0x0028,0x1202) )
1082 // Blue Palette Color Lookup Table Data
1083 if ( !GetDocEntry(0x0028,0x1203) )
1088 // FIXME : (0x0028,0x3006) : LUT Data (CTX dependent)
1089 // NOT taken into account, but we don't know how to use it ...
1094 * \brief gets the info from 0028,1101 : Lookup Table Desc-Red
1096 * @return Lookup Table number of Bits , 0 by default
1097 * when (0028,0004),Photometric Interpretation = [PALETTE COLOR ]
1098 * @ return bit number of each LUT item
1100 int File::GetLUTNbits()
1102 std::vector<std::string> tokens;
1105 //Just hope Lookup Table Desc-Red = Lookup Table Desc-Red
1106 // = Lookup Table Desc-Blue
1107 // Consistency already checked in GetLUTLength
1108 std::string lutDescription = GetEntryValue(0x0028,0x1101);
1109 if ( lutDescription == GDCM_UNFOUND )
1114 tokens.clear(); // clean any previous value
1115 Util::Tokenize ( lutDescription, tokens, "\\" );
1116 //LutLength=atoi(tokens[0].c_str());
1117 //LutDepth=atoi(tokens[1].c_str());
1119 lutNbits = atoi( tokens[2].c_str() );
1126 *\brief gets the info from 0028,1052 : Rescale Intercept
1127 * @return Rescale Intercept
1129 float File::GetRescaleIntercept()
1131 float resInter = 0.;
1132 /// 0028 1052 DS IMG Rescale Intercept
1133 const std::string &strRescInter = GetEntryValue(0x0028,0x1052);
1134 if ( strRescInter != GDCM_UNFOUND )
1136 if ( sscanf( strRescInter.c_str(), "%f ", &resInter) != 1 )
1138 // bug in the element 0x0028,0x1052
1139 gdcmWarningMacro( "Rescale Intercept (0028,1052) is empty." );
1147 *\brief gets the info from 0028,1053 : Rescale Slope
1148 * @return Rescale Slope
1150 float File::GetRescaleSlope()
1152 float resSlope = 1.;
1153 //0028 1053 DS IMG Rescale Slope
1154 std::string strRescSlope = GetEntryValue(0x0028,0x1053);
1155 if ( strRescSlope != GDCM_UNFOUND )
1157 if ( sscanf( strRescSlope.c_str(), "%f ", &resSlope) != 1 )
1159 // bug in the element 0x0028,0x1053
1160 gdcmWarningMacro( "Rescale Slope (0028,1053) is empty.");
1168 * \brief This function is intended to user who doesn't want
1169 * to have to manage a LUT and expects to get an RBG Pixel image
1170 * (or a monochrome one ...)
1171 * \warning to be used with GetImagePixels()
1172 * @return 1 if Gray level, 3 if Color (RGB, YBR, *or PALETTE COLOR*)
1174 int File::GetNumberOfScalarComponents()
1176 if ( GetSamplesPerPixel() == 3 )
1181 // 0028 0100 US IMG Bits Allocated
1182 // (in order no to be messed up by old RGB images)
1183 if ( GetEntryValue(0x0028,0x0100) == "24" )
1188 std::string strPhotometricInterpretation = GetEntryValue(0x0028,0x0004);
1190 if ( ( strPhotometricInterpretation == "PALETTE COLOR ") )
1192 if ( HasLUT() )// PALETTE COLOR is NOT enough
1202 // beware of trailing space at end of string
1203 // DICOM tags are never of odd length
1204 if ( strPhotometricInterpretation == GDCM_UNFOUND ||
1205 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME1") ||
1206 Util::DicomStringEqual(strPhotometricInterpretation, "MONOCHROME2") )
1212 // we assume that *all* kinds of YBR are dealt with
1218 * \brief This function is intended to user that DOESN'T want
1219 * to get RGB pixels image when it's stored as a PALETTE COLOR image
1220 * - the (vtk) user is supposed to know how deal with LUTs -
1221 * \warning to be used with GetImagePixelsRaw()
1222 * @return 1 if Gray level, 3 if Color (RGB or YBR - NOT 'PALETTE COLOR' -)
1224 int File::GetNumberOfScalarComponentsRaw()
1226 // 0028 0100 US IMG Bits Allocated
1227 // (in order no to be messed up by old RGB images)
1228 if ( File::GetEntryValue(0x0028,0x0100) == "24" )
1233 // we assume that *all* kinds of YBR are dealt with
1234 return GetSamplesPerPixel();
1238 * \brief Recover the offset (from the beginning of the file)
1239 * of *image* pixels (not *icone image* pixels, if any !)
1240 * @return Pixel Offset
1242 size_t File::GetPixelOffset()
1244 DocEntry *pxlElement = GetDocEntry(GrPixel, NumPixel);
1247 return pxlElement->GetOffset();
1251 gdcmDebugMacro( "Big trouble : Pixel Element ("
1252 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found" );
1258 * \brief Recover the pixel area length (in Bytes)
1259 * @return Pixel Element Length, as stored in the header
1260 * (NOT the memory space necessary to hold the Pixels
1261 * -in case of embeded compressed image-)
1262 * 0 : NOT USABLE file. The caller has to check.
1264 size_t File::GetPixelAreaLength()
1266 DocEntry *pxlElement = GetDocEntry(GrPixel, NumPixel);
1269 return pxlElement->GetLength();
1273 gdcmDebugMacro( "Big trouble : Pixel Element ("
1274 << std::hex << GrPixel<<","<< NumPixel<< ") NOT found" );
1280 * \brief Adds the characteristics of a new element we want to anonymize
1281 * @param group Group number of the target tag.
1282 * @param elem Element number of the target tag.
1283 * @param value new value (string) to substitute with
1285 void File::AddAnonymizeElement (uint16_t group, uint16_t elem,
1286 std::string const &value)
1292 AnonymizeList.push_back(el);
1296 * \brief Overwrites in the file the values of the DicomElements
1299 void File::AnonymizeNoLoad()
1301 std::fstream *fp = new std::fstream(Filename.c_str(),
1302 std::ios::in | std::ios::out | std::ios::binary);
1306 uint32_t valLgth = 0;
1307 std::string *spaces;
1308 for (ListElements::iterator it = AnonymizeList.begin();
1309 it != AnonymizeList.end();
1312 d = GetDocEntry( (*it).Group, (*it).Elem);
1317 if ( dynamic_cast<SeqEntry *>(d) )
1319 gdcmWarningMacro( "You cannot 'Anonymize a SeqEntry ");
1323 offset = d->GetOffset();
1324 lgth = d->GetLength();
1327 spaces = new std::string( lgth-valLgth, ' ');
1328 (*it).Value = (*it).Value + *spaces;
1331 fp->seekp( offset, std::ios::beg );
1332 fp->write( (*it).Value.c_str(), lgth );
1340 * \brief anonymize a File (remove Patient's personal info passed with
1341 * AddAnonymizeElement()
1342 * \note You cannot Anonymize a BinEntry (to be fixed)
1344 bool File::AnonymizeFile()
1346 // If Anonymisation list is empty, let's perform some basic anonymization
1347 if ( AnonymizeList.begin() == AnonymizeList.end() )
1349 // If exist, replace by spaces
1350 SetValEntry (" ",0x0010, 0x2154); // Telephone
1351 SetValEntry (" ",0x0010, 0x1040); // Adress
1352 SetValEntry (" ",0x0010, 0x0020); // Patient ID
1354 DocEntry* patientNameHE = GetDocEntry (0x0010, 0x0010);
1356 if ( patientNameHE ) // we replace it by Study Instance UID (why not ?)
1358 std::string studyInstanceUID = GetEntryValue (0x0020, 0x000d);
1359 if ( studyInstanceUID != GDCM_UNFOUND )
1361 SetValEntry(studyInstanceUID, 0x0010, 0x0010);
1365 SetValEntry("anonymised", 0x0010, 0x0010);
1372 for (ListElements::iterator it = AnonymizeList.begin();
1373 it != AnonymizeList.end();
1376 d = GetDocEntry( (*it).Group, (*it).Elem);
1381 if ( dynamic_cast<SeqEntry *>(d) )
1383 gdcmWarningMacro( "You cannot 'Anonymize' a SeqEntry ");
1387 if ( dynamic_cast<BinEntry *>(d) )
1389 gdcmWarningMacro( "To 'Anonymize' a BinEntry, better use AnonymizeNoLoad (FIXME) ");
1393 SetValEntry ((*it).Value, (*it).Group, (*it).Elem);
1397 // In order to make definitively impossible any further identification
1398 // remove or replace all the stuff that contains a Date
1400 //0008 0012 DA ID Instance Creation Date
1401 //0008 0020 DA ID Study Date
1402 //0008 0021 DA ID Series Date
1403 //0008 0022 DA ID Acquisition Date
1404 //0008 0023 DA ID Content Date
1405 //0008 0024 DA ID Overlay Date
1406 //0008 0025 DA ID Curve Date
1407 //0008 002a DT ID Acquisition Datetime
1408 //0018 9074 DT ACQ Frame Acquisition Datetime
1409 //0018 9151 DT ACQ Frame Reference Datetime
1410 //0018 a002 DT ACQ Contribution Date Time
1411 //0020 3403 SH REL Modified Image Date (RET)
1412 //0032 0032 DA SDY Study Verified Date
1413 //0032 0034 DA SDY Study Read Date
1414 //0032 1000 DA SDY Scheduled Study Start Date
1415 //0032 1010 DA SDY Scheduled Study Stop Date
1416 //0032 1040 DA SDY Study Arrival Date
1417 //0032 1050 DA SDY Study Completion Date
1418 //0038 001a DA VIS Scheduled Admission Date
1419 //0038 001c DA VIS Scheduled Discharge Date
1420 //0038 0020 DA VIS Admitting Date
1421 //0038 0030 DA VIS Discharge Date
1422 //0040 0002 DA PRC Scheduled Procedure Step Start Date
1423 //0040 0004 DA PRC Scheduled Procedure Step End Date
1424 //0040 0244 DA PRC Performed Procedure Step Start Date
1425 //0040 0250 DA PRC Performed Procedure Step End Date
1426 //0040 2004 DA PRC Issue Date of Imaging Service Request
1427 //0040 4005 DT PRC Scheduled Procedure Step Start Date and Time
1428 //0040 4011 DT PRC Expected Completion Date and Time
1429 //0040 a030 DT PRC Verification Date Time
1430 //0040 a032 DT PRC Observation Date Time
1431 //0040 a120 DT PRC DateTime
1432 //0040 a121 DA PRC Date
1433 //0040 a13a DT PRC Referenced Datetime
1434 //0070 0082 DA ??? Presentation Creation Date
1435 //0100 0420 DT ??? SOP Autorization Date and Time
1436 //0400 0105 DT ??? Digital Signature DateTime
1437 //2100 0040 DA PJ Creation Date
1438 //3006 0008 DA SSET Structure Set Date
1439 //3008 0024 DA ??? Treatment Control Point Date
1440 //3008 0054 DA ??? First Treatment Date
1441 //3008 0056 DA ??? Most Recent Treatment Date
1442 //3008 0162 DA ??? Safe Position Exit Date
1443 //3008 0166 DA ??? Safe Position Return Date
1444 //3008 0250 DA ??? Treatment Date
1445 //300a 0006 DA RT RT Plan Date
1446 //300a 022c DA RT Air Kerma Rate Reference Date
1447 //300e 0004 DA RT Review Date
1453 * \brief Performs some consistency checking on various 'File related'
1454 * (as opposed to 'DicomDir related') entries
1455 * then writes in a file all the (Dicom Elements) included the Pixels
1456 * @param fileName file name to write to
1457 * @param writetype type of the file to be written
1458 * (ACR, ExplicitVR, ImplicitVR)
1460 bool File::Write(std::string fileName, FileType writetype)
1462 std::ofstream *fp = new std::ofstream(fileName.c_str(),
1463 std::ios::out | std::ios::binary);
1466 gdcmWarningMacro("Failed to open (write) File: " << fileName.c_str());
1470 // Entry : 0002|0000 = group length -> recalculated
1471 ValEntry*e0000 = GetValEntry(0x0002,0x0000);
1474 std::ostringstream sLen;
1475 sLen << ComputeGroup0002Length(writetype);
1476 e0000->SetValue(sLen.str());
1479 int i_lgPix = GetEntryLength(GrPixel, NumPixel);
1482 // no (GrPixel, NumPixel) element
1483 std::string s_lgPix = Util::Format("%d", i_lgPix+12);
1484 s_lgPix = Util::DicomString( s_lgPix.c_str() );
1485 InsertValEntry(s_lgPix,GrPixel, 0x0000);
1488 Document::WriteContent(fp, writetype);
1496 //-----------------------------------------------------------------------------
1500 //-----------------------------------------------------------------------------
1503 * \brief Parse pixel data from disk of [multi-]fragment RLE encoding.
1504 * Compute the RLE extra information and store it in \ref RLEInfo
1505 * for later pixel retrieval usage.
1507 void File::ComputeRLEInfo()
1509 std::string ts = GetTransferSyntax();
1510 if ( !Global::GetTS()->IsRLELossless(ts) )
1515 // Encoded pixel data: for the time being we are only concerned with
1516 // Jpeg or RLE Pixel data encodings.
1517 // As stated in PS 3.5-2003, section 8.2 p44:
1518 // "If sent in Encapsulated Format (i.e. other than the Native Format) the
1519 // value representation OB is used".
1520 // Hence we expect an OB value representation. Concerning OB VR,
1521 // the section PS 3.5-2003, section A.4.c p 58-59, states:
1522 // "For the Value Representations OB and OW, the encoding shall meet the
1523 // following specifications depending on the Data element tag:"
1525 // - the first item in the sequence of items before the encoded pixel
1526 // data stream shall be basic offset table item. The basic offset table
1527 // item value, however, is not required to be present"
1528 ReadAndSkipEncapsulatedBasicOffsetTable();
1530 // Encapsulated RLE Compressed Images (see PS 3.5-2003, Annex G)
1531 // Loop on the individual frame[s] and store the information
1532 // on the RLE fragments in a RLEFramesInfo.
1533 // Note: - when only a single frame is present, this is a
1535 // - when more than one frame are present, then we are in
1536 // the case of a multi-frame image.
1538 while ( (frameLength = ReadTagLength(0xfffe, 0xe000)) != 0 )
1540 // Parse the RLE Header and store the corresponding RLE Segment
1541 // Offset Table information on fragments of this current Frame.
1542 // Note that the fragment pixels themselves are not loaded
1543 // (but just skipped).
1544 long frameOffset = Fp->tellg();
1546 uint32_t nbRleSegments = ReadInt32();
1547 if ( nbRleSegments > 16 )
1549 // There should be at most 15 segments (refer to RLEFrame class)
1550 gdcmWarningMacro( "Too many segments.");
1553 uint32_t rleSegmentOffsetTable[16];
1554 for( int k = 1; k <= 15; k++ )
1556 rleSegmentOffsetTable[k] = ReadInt32();
1559 // Deduce from both RLE Header and frameLength
1560 // the fragment length, and again store this info
1561 // in a RLEFramesInfo.
1562 long rleSegmentLength[15];
1563 // skipping (not reading) RLE Segments
1564 if ( nbRleSegments > 1)
1566 for(unsigned int k = 1; k <= nbRleSegments-1; k++)
1568 rleSegmentLength[k] = rleSegmentOffsetTable[k+1]
1569 - rleSegmentOffsetTable[k];
1570 SkipBytes(rleSegmentLength[k]);
1574 rleSegmentLength[nbRleSegments] = frameLength
1575 - rleSegmentOffsetTable[nbRleSegments];
1576 SkipBytes(rleSegmentLength[nbRleSegments]);
1578 // Store the collected info
1579 RLEFrame *newFrame = new RLEFrame;
1580 newFrame->SetNumberOfFragments(nbRleSegments);
1581 for( unsigned int uk = 1; uk <= nbRleSegments; uk++ )
1583 newFrame->SetOffset(uk,frameOffset + rleSegmentOffsetTable[uk]);
1584 newFrame->SetLength(uk,rleSegmentLength[uk]);
1586 RLEInfo->AddFrame(newFrame);
1589 // Make sure that we encounter a 'Sequence Delimiter Item'
1590 // at the end of the item :
1591 if ( !ReadTag(0xfffe, 0xe0dd) )
1593 gdcmWarningMacro( "No sequence delimiter item at end of RLE item sequence");
1598 * \brief Parse pixel data from disk of [multi-]fragment Jpeg encoding.
1599 * Compute the jpeg extra information (fragment[s] offset[s] and
1600 * length) and store it[them] in \ref JPEGInfo for later pixel
1603 void File::ComputeJPEGFragmentInfo()
1605 // If you need to, look for comments of ComputeRLEInfo().
1606 std::string ts = GetTransferSyntax();
1607 if ( ! Global::GetTS()->IsJPEG(ts) )
1612 ReadAndSkipEncapsulatedBasicOffsetTable();
1614 // Loop on the fragments[s] and store the parsed information in a
1616 long fragmentLength;
1617 while ( (fragmentLength = ReadTagLength(0xfffe, 0xe000)) != 0 )
1619 long fragmentOffset = Fp->tellg();
1621 // Store the collected info
1622 JPEGFragment *newFragment = new JPEGFragment;
1623 newFragment->SetOffset(fragmentOffset);
1624 newFragment->SetLength(fragmentLength);
1625 JPEGInfo->AddFragment(newFragment);
1627 SkipBytes(fragmentLength);
1630 // Make sure that we encounter a 'Sequence Delimiter Item'
1631 // at the end of the item :
1632 if ( !ReadTag(0xfffe, 0xe0dd) )
1634 gdcmWarningMacro( "No sequence delimiter item at end of JPEG item sequence");
1639 * \brief Assuming the internal file pointer \ref Document::Fp
1640 * is placed at the beginning of a tag check whether this
1641 * tag is (TestGroup, TestElem).
1642 * \warning On success the internal file pointer \ref Document::Fp
1643 * is modified to point after the tag.
1644 * On failure (i.e. when the tag wasn't the expected tag
1645 * (TestGroup, TestElem) the internal file pointer
1646 * \ref Document::Fp is restored to it's original position.
1647 * @param testGroup The expected group of the tag.
1648 * @param testElem The expected Element of the tag.
1649 * @return True on success, false otherwise.
1651 bool File::ReadTag(uint16_t testGroup, uint16_t testElem)
1653 long positionOnEntry = Fp->tellg();
1654 long currentPosition = Fp->tellg(); // On debugging purposes
1656 // Read the Item Tag group and element, and make
1657 // sure they are what we expected:
1658 uint16_t itemTagGroup;
1659 uint16_t itemTagElem;
1662 itemTagGroup = ReadInt16();
1663 itemTagElem = ReadInt16();
1665 catch ( FormatError e )
1667 //std::cerr << e << std::endl;
1670 if ( itemTagGroup != testGroup || itemTagElem != testElem )
1672 gdcmWarningMacro( "Wrong Item Tag found:"
1673 << " We should have found tag ("
1674 << std::hex << testGroup << "," << testElem << ")" << std::endl
1675 << " but instead we encountered tag ("
1676 << std::hex << itemTagGroup << "," << itemTagElem << ")"
1677 << " at address: " << " 0x(" << (unsigned int)currentPosition << ")"
1679 Fp->seekg(positionOnEntry, std::ios::beg);
1687 * \brief Assuming the internal file pointer \ref Document::Fp
1688 * is placed at the beginning of a tag (TestGroup, TestElement),
1689 * read the length associated to the Tag.
1690 * \warning On success the internal file pointer \ref Document::Fp
1691 * is modified to point after the tag and it's length.
1692 * On failure (i.e. when the tag wasn't the expected tag
1693 * (TestGroup, TestElement) the internal file pointer
1694 * \ref Document::Fp is restored to it's original position.
1695 * @param testGroup The expected Group of the tag.
1696 * @param testElem The expected Element of the tag.
1697 * @return On success returns the length associated to the tag. On failure
1700 uint32_t File::ReadTagLength(uint16_t testGroup, uint16_t testElem)
1703 if ( !ReadTag(testGroup, testElem) )
1708 //// Then read the associated Item Length
1709 long currentPosition = Fp->tellg();
1710 uint32_t itemLength = ReadInt32();
1712 gdcmWarningMacro( "Basic Item Length is: "
1713 << itemLength << std::endl
1714 << " at address: " << std::hex << (unsigned int)currentPosition);
1720 * \brief When parsing the Pixel Data of an encapsulated file, read
1721 * the basic offset table (when present, and BTW dump it).
1723 void File::ReadAndSkipEncapsulatedBasicOffsetTable()
1725 //// Read the Basic Offset Table Item Tag length...
1726 uint32_t itemLength = ReadTagLength(0xfffe, 0xe000);
1728 // When present, read the basic offset table itself.
1729 // Notes: - since the presence of this basic offset table is optional
1730 // we can't rely on it for the implementation, and we will simply
1731 // trash it's content (when present).
1732 // - still, when present, we could add some further checks on the
1733 // lengths, but we won't bother with such fuses for the time being.
1734 if ( itemLength != 0 )
1736 char *basicOffsetTableItemValue = new char[itemLength + 1];
1737 Fp->read(basicOffsetTableItemValue, itemLength);
1740 for (unsigned int i=0; i < itemLength; i += 4 )
1742 uint32_t individualLength = str2num( &basicOffsetTableItemValue[i],
1744 gdcmWarningMacro( "Read one length: " <<
1745 std::hex << individualLength );
1749 delete[] basicOffsetTableItemValue;
1753 // These are the deprecated method that one day should be removed (after the next release)
1755 #ifndef GDCM_LEGACY_REMOVE
1757 * \brief Constructor (DEPRECATED : temporaryly kept not to break the API)
1758 * @param filename name of the file whose header we want to analyze
1759 * @deprecated do not use any longer
1761 File::File( std::string const &filename )
1764 RLEInfo = new RLEFramesInfo;
1765 JPEGInfo = new JPEGFragmentsInfo;
1767 SetFileName( filename );
1768 Load( ); // gdcm::Document is first Loaded, then the 'File part'
1772 * \brief Loader. (DEPRECATED : temporaryly kept not to break the API)
1773 * @param fileName file to be open for parsing
1774 * @return false if file cannot be open or no swap info was found,
1775 * or no tag was found.
1776 * @deprecated Use the Load() [ + SetLoadMode() ] + SetFileName() functions instead
1778 bool File::Load( std::string const &fileName )
1780 GDCM_LEGACY_REPLACED_BODY(File::Load(std::string), "1.2",
1782 SetFileName( fileName );
1783 if ( ! this->Document::Load( ) )
1786 return DoTheLoadingJob( );
1790 //-----------------------------------------------------------------------------
1793 //-----------------------------------------------------------------------------
1794 } // end namespace gdcm