]> Creatis software - gdcm.git/blob - src/gdcmDocument.cxx
STYLE: Minor tweaks
[gdcm.git] / src / gdcmDocument.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/02/09 21:37:45 $
7   Version:   $Revision: 1.226 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmDocument.h"
20 #include "gdcmValEntry.h"
21 #include "gdcmBinEntry.h"
22 #include "gdcmSeqEntry.h"
23 #include "gdcmGlobal.h"
24 #include "gdcmUtil.h"
25 #include "gdcmDebug.h"
26 #include "gdcmTS.h"
27 #include "gdcmDictSet.h"
28 #include "gdcmDocEntrySet.h"
29 #include "gdcmSQItem.h"
30
31 #include <vector>
32 #include <iomanip>
33 #include <fstream>
34
35 // For nthos:
36 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MINGW32__) 
37    #include <winsock.h>
38 #endif
39
40 #ifdef CMAKE_HAVE_NETINET_IN_H
41    #include <netinet/in.h>
42 #endif
43
44 namespace gdcm 
45 {
46 //-----------------------------------------------------------------------------
47 // Refer to Document::CheckSwap()
48 //const unsigned int Document::HEADER_LENGTH_TO_READ = 256;
49
50 // Refer to Document::SetMaxSizeLoadEntry()
51 const unsigned int Document::MAX_SIZE_LOAD_ELEMENT_VALUE = 0xfff; // 4096
52 const unsigned int Document::MAX_SIZE_PRINT_ELEMENT_VALUE = 0x7fffffff;
53
54 //-----------------------------------------------------------------------------
55 // Constructor / Destructor
56 // Constructors and destructors are protected to avoid user to invoke directly
57 /**
58  * \brief   constructor  
59  * @param   filename 'Document' (File or DicomDir) to be opened for parsing
60  */
61 Document::Document( std::string const &filename ) 
62          :ElementSet(-1)
63 {
64    SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE); 
65    Filename = filename;
66    Initialize();
67
68    Fp = 0;
69    if ( !OpenFile() )
70    {
71       return;
72    }
73
74    Group0002Parsed = false;
75
76    gdcmWarningMacro( "Starting parsing of file: " << Filename.c_str());
77
78    Fp->seekg(0, std::ios::end);
79    long lgt = Fp->tellg();
80            
81    Fp->seekg(0, std::ios::beg);
82
83    CheckSwap();
84    long beg = Fp->tellg();
85    lgt -= beg;
86    
87    ParseDES( this, beg, lgt, false); // Loading is done during parsing
88
89    Fp->seekg( 0,  std::ios::beg);
90    
91    // Load 'non string' values
92       
93    std::string PhotometricInterpretation = GetEntryValue(0x0028,0x0004);   
94    if( PhotometricInterpretation == "PALETTE COLOR " )
95    {
96       LoadEntryBinArea(0x0028,0x1200);  // gray LUT   
97       /// FIXME FIXME FIXME
98       /// The tags refered by the three following lines used to be CORRECTLY
99       /// defined as having an US Value Representation in the public
100       /// dictionary. BUT the semantics implied by the three following
101       /// lines state that the corresponding tag contents are in fact
102       /// the ones of a BinEntry.
103       /// In order to fix things "Quick and Dirty" the dictionary was
104       /// altered on PURPOSE but now contains a WRONG value.
105       /// In order to fix things and restore the dictionary to its
106       /// correct value, one needs to decided of the semantics by deciding
107       /// whether the following tags are either:
108       /// - multivaluated US, and hence loaded as ValEntry, but afterwards
109       ///   also used as BinEntry, which requires the proper conversion,
110       /// - OW, and hence loaded as BinEntry, but afterwards also used
111       ///   as ValEntry, which requires the proper conversion.
112       LoadEntryBinArea(0x0028,0x1201);  // R    LUT
113       LoadEntryBinArea(0x0028,0x1202);  // G    LUT
114       LoadEntryBinArea(0x0028,0x1203);  // B    LUT
115       
116       // Segmented Red   Palette Color LUT Data
117       LoadEntryBinArea(0x0028,0x1221);
118       // Segmented Green Palette Color LUT Data
119       LoadEntryBinArea(0x0028,0x1222);
120       // Segmented Blue  Palette Color LUT Data
121       LoadEntryBinArea(0x0028,0x1223);
122    } 
123    //FIXME later : how to use it?
124    LoadEntryBinArea(0x0028,0x3006);  //LUT Data (CTX dependent) 
125
126    CloseFile(); 
127   
128    // ----------------------------
129    // Specific code to allow gdcm to read ACR-LibIDO formated images
130    // Note: ACR-LibIDO is an extension of the ACR standard that was
131    //       used at CREATIS. For the time being (say a couple years)
132    //       we keep this kludge to allow a smooth move to gdcm for
133    //       CREATIS developpers (sorry folks).
134    //
135    // if recognition code tells us we deal with a LibIDO image
136    // we switch lineNumber and columnNumber
137    //
138    std::string RecCode;
139    RecCode = GetEntryValue(0x0008, 0x0010); // recognition code (RET)
140    if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
141        RecCode == "CANRME_AILIBOD1_1." )  // for brain-damaged softwares
142                                           // with "little-endian strings"
143    {
144          Filetype = ACR_LIBIDO; 
145          std::string rows    = GetEntryValue(0x0028, 0x0010);
146          std::string columns = GetEntryValue(0x0028, 0x0011);
147          SetValEntry(columns, 0x0028, 0x0010);
148          SetValEntry(rows   , 0x0028, 0x0011);
149    }
150    // --- End of ACR-LibIDO kludge --- 
151 }
152
153 /**
154  * \brief This default constructor doesn't parse the file. You should
155  *        then invoke \ref Document::SetFileName and then the parsing.
156  */
157 Document::Document() 
158          :ElementSet(-1)
159 {
160    Fp = 0;
161
162    SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
163    Initialize();
164    SwapCode = 1234;
165    Filetype = ExplicitVR;
166    Group0002Parsed = false;
167 }
168
169 /**
170  * \brief   Canonical destructor.
171  */
172 Document::~Document ()
173 {
174    RefPubDict = NULL;
175    RefShaDict = NULL;
176 }
177
178 //-----------------------------------------------------------------------------
179 // Public
180 /**
181  * \brief   Get the public dictionary used
182  */
183 Dict *Document::GetPubDict()
184 {
185    return RefPubDict;
186 }
187
188 /**
189  * \brief   Get the shadow dictionary used
190  */
191 Dict *Document::GetShaDict()
192 {
193    return RefShaDict;
194 }
195
196 /**
197  * \brief   Set the shadow dictionary used
198  * @param   dict dictionary to use in shadow
199  */
200 bool Document::SetShaDict(Dict *dict)
201 {
202    RefShaDict = dict;
203    return !RefShaDict;
204 }
205
206 /**
207  * \brief   Set the shadow dictionary used
208  * @param   dictName name of the dictionary to use in shadow
209  */
210 bool Document::SetShaDict(DictKey const &dictName)
211 {
212    RefShaDict = Global::GetDicts()->GetDict(dictName);
213    return !RefShaDict;
214 }
215
216 /**
217  * \brief  This predicate, based on hopefully reasonable heuristics,
218  *         decides whether or not the current Document was properly parsed
219  *         and contains the mandatory information for being considered as
220  *         a well formed and usable Dicom/Acr File.
221  * @return true when Document is the one of a reasonable Dicom/Acr file,
222  *         false otherwise. 
223  */
224 bool Document::IsReadable()
225 {
226    if( Filetype == Unknown)
227    {
228       gdcmWarningMacro( "Wrong filetype");
229       return false;
230    }
231
232    if ( IsEmpty() )
233    { 
234       gdcmWarningMacro( "No tag in internal hash table.");
235       return false;
236    }
237
238    return true;
239 }
240
241 /**
242  * \brief   Predicate for dicom version 3 file.
243  * @return  True when the file is a dicom version 3.
244  */
245 bool Document::IsDicomV3()
246 {
247    // Checking if Transfer Syntax exists is enough
248    // Anyway, it's to late check if the 'Preamble' was found ...
249    // And ... would it be a rich idea to check ?
250    // (some 'no Preamble' DICOM images exist !)
251    return GetDocEntry(0x0002, 0x0010) != NULL;
252 }
253
254 /**
255  * \brief   Predicate for Papyrus file
256  *          Dedicated to whomsoever it may concern
257  * @return  True when the file is a Papyrus file.
258  */
259 bool Document::IsPapyrus()
260 {
261    // check for Papyrus private Sequence
262    DocEntry *e = GetDocEntry(0x0041, 0x1050);
263    if ( !e )
264       return false;
265    // check if it's actually a Sequence
266    if ( !dynamic_cast<SeqEntry*>(e) )
267       return  false;
268    return true;
269 }
270
271 /**
272  * \brief  returns the File Type 
273  *         (ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown)
274  * @return the FileType code
275  */
276 FileType Document::GetFileType()
277 {
278    return Filetype;
279 }
280
281 /**
282  * \brief   Accessor to the Transfer Syntax (when present) of the
283  *          current document (it internally handles reading the
284  *          value from disk when only parsing occured).
285  * @return  The encountered Transfer Syntax of the current document.
286  */
287 std::string Document::GetTransferSyntax()
288 {
289    DocEntry *entry = GetDocEntry(0x0002, 0x0010);
290    if ( !entry )
291    {
292       return GDCM_UNKNOWN;
293    }
294
295    // The entry might be present but not loaded (parsing and loading
296    // happen at different stages): try loading and proceed with check...
297    LoadDocEntrySafe(entry);
298    if (ValEntry *valEntry = dynamic_cast< ValEntry* >(entry) )
299    {
300       std::string transfer = valEntry->GetValue();
301       // The actual transfer (as read from disk) might be padded. We
302       // first need to remove the potential padding. We can make the
303       // weak assumption that padding was not executed with digits...
304       if  ( transfer.length() == 0 )
305       {
306          // for brain damaged headers
307          return GDCM_UNKNOWN;
308       }
309       while ( !isdigit((unsigned char)transfer[transfer.length()-1]) )
310       {
311          transfer.erase(transfer.length()-1, 1);
312       }
313       return transfer;
314    }
315    return GDCM_UNKNOWN;
316 }
317
318 /**
319  * \brief Accesses the info from 0002,0010 : Transfer Syntax and TS
320  * @return The full Transfer Syntax Name (as opposed to Transfer Syntax UID)
321  */
322 std::string Document::GetTransferSyntaxName()
323 {
324    // use the TS (TS : Transfer Syntax)
325    std::string transferSyntax = GetEntryValue(0x0002,0x0010);
326
327    if ( (transferSyntax.find(GDCM_NOTLOADED) < transferSyntax.length()) )
328    {
329       gdcmErrorMacro( "Transfer Syntax not loaded. " << std::endl
330                << "Better you increase MAX_SIZE_LOAD_ELEMENT_VALUE" );
331       return "Uncompressed ACR-NEMA";
332    }
333    if ( transferSyntax == GDCM_UNFOUND )
334    {
335       gdcmWarningMacro( "Unfound Transfer Syntax (0002,0010)");
336       return "Uncompressed ACR-NEMA";
337    }
338
339    // we do it only when we need it
340    const TSKey &tsName = Global::GetTS()->GetValue( transferSyntax );
341
342    // Global::GetTS() is a global static you shall never try to delete it!
343    return tsName;
344 }
345 //
346 // --------------- Swap Code ------------------
347 /**
348  * \brief   Swaps the bytes so they agree with the processor order
349  * @return  The properly swaped 16 bits integer.
350  */
351 uint16_t Document::SwapShort(uint16_t a)
352 {
353    if ( SwapCode == 4321 || SwapCode == 2143 )
354    {
355       a = ((( a << 8 ) & 0xff00 ) | (( a >> 8 ) & 0x00ff ) );
356    }
357    return a;
358 }
359
360 /**
361  * \brief   Swaps back the bytes of 4-byte long integer accordingly to
362  *          processor order.
363  * @return  The properly swaped 32 bits integer.
364  */
365 uint32_t Document::SwapLong(uint32_t a)
366 {
367    switch (SwapCode)
368    {
369       case 1234 :
370          break;
371       case 4321 :
372          a=( ((a<<24) & 0xff000000) | ((a<<8)  & 0x00ff0000) | 
373              ((a>>8)  & 0x0000ff00) | ((a>>24) & 0x000000ff) );
374          break;   
375       case 3412 :
376          a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
377          break;  
378       case 2143 :
379          a=( ((a<< 8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff)  );
380       break;
381       default :
382          gdcmErrorMacro( "Unset swap code:" << SwapCode );
383          a = 0;
384    }
385    return a;
386
387
388 //
389 // -----------------File I/O ---------------
390 /**
391  * \brief  Tries to open the file \ref Document::Filename and
392  *         checks the preamble when existing.
393  * @return The FILE pointer on success. 
394  */
395 std::ifstream *Document::OpenFile()
396 {
397    HasDCMPreamble = false;
398    if (Filename.length() == 0) 
399    {
400       return 0;
401    }
402
403    if(Fp)
404    {
405       gdcmWarningMacro( "File already open: " << Filename.c_str());
406       CloseFile();
407    }
408
409    Fp = new std::ifstream(Filename.c_str(), std::ios::in | std::ios::binary);
410    if( ! *Fp )
411    {
412       gdcmDebugMacro( "Cannot open file: " << Filename.c_str());
413       delete Fp;
414       Fp = 0;
415       return 0;
416    }
417  
418    uint16_t zero = 0;
419    Fp->read((char*)&zero, (size_t)2);
420    if( Fp->eof() )
421    {
422       CloseFile();
423       return 0;
424    }
425  
426    //ACR -- or DICOM with no Preamble; may start with a Shadow Group --
427    if( 
428        zero == 0x0001 || zero == 0x0100 || zero == 0x0002 || zero == 0x0200 ||
429        zero == 0x0003 || zero == 0x0300 || zero == 0x0004 || zero == 0x0400 ||
430        zero == 0x0005 || zero == 0x0500 || zero == 0x0006 || zero == 0x0600 ||
431        zero == 0x0007 || zero == 0x0700 || zero == 0x0008 || zero == 0x0800 )
432    {
433       std::string msg 
434          = Util::Format("ACR/DICOM with no preamble: (%04x)\n", zero);
435       gdcmWarningMacro( msg.c_str() );
436       return Fp;
437    }
438  
439    //DICOM
440    Fp->seekg(126L, std::ios::cur);
441    char dicm[4] = {' ',' ',' ',' '};
442    Fp->read(dicm,  (size_t)4);
443    if( Fp->eof() )
444    {
445       CloseFile();
446       return 0;
447    }
448    if( memcmp(dicm, "DICM", 4) == 0 )
449    {
450       HasDCMPreamble = true;
451       return Fp;
452    }
453  
454    CloseFile();
455    gdcmWarningMacro( "Not DICOM/ACR (missing preamble)" << Filename.c_str());
456  
457    return 0;
458 }
459
460 /**
461  * \brief closes the file  
462  * @return  TRUE if the close was successfull 
463  */
464 bool Document::CloseFile()
465 {
466    if( Fp )
467    {
468       Fp->close();
469       delete Fp;
470       Fp = 0;
471    }
472    return true; //FIXME how do we detect a non-closed ifstream ?
473 }
474
475 /**
476  * \brief Writes in a file all the Header Entries (Dicom Elements) 
477  * @param fp file pointer on an already open file (actually: Output File Stream)
478  * @param filetype Type of the File to be written 
479  *          (ACR-NEMA, ExplicitVR, ImplicitVR)
480  * @return Always true.
481  */
482 void Document::WriteContent(std::ofstream *fp, FileType filetype)
483 {
484    // \TODO move the following lines (and a lot of others, to be written)
485    // to a future function CheckAndCorrectHeader  
486
487    // (necessary if user wants to write a DICOM V3 file
488    // starting from an ACR-NEMA (V2) Header
489
490    if ( filetype == ImplicitVR || filetype == ExplicitVR )
491    {
492       // writing Dicom File Preamble
493       char filePreamble[128];
494       memset(filePreamble, 0, 128);
495       fp->write(filePreamble, 128);
496       fp->write("DICM", 4);
497    }
498
499    /*
500     * \todo rewrite later, if really usefull
501     *       - 'Group Length' element is optional in DICOM
502     *       - but un-updated odd groups lengthes can causes pb
503     *         (xmedcon breaker)
504     *
505     * if ( (filetype == ImplicitVR) || (filetype == ExplicitVR) )
506     *    UpdateGroupLength(false,filetype);
507     * if ( filetype == ACR)
508     *    UpdateGroupLength(true,ACR);
509     */
510
511    ElementSet::WriteContent(fp, filetype); // This one is recursive
512 }
513
514 // -----------------------------------------
515 // Content entries 
516 /**
517  * \brief Loads (from disk) the element content 
518  *        when a string is not suitable
519  * @param group   group number of the Entry 
520  * @param elem  element number of the Entry
521  */
522 void Document::LoadEntryBinArea(uint16_t group, uint16_t elem)
523 {
524    // Search the corresponding DocEntry
525    DocEntry *docElement = GetDocEntry(group, elem);
526    if ( !docElement )
527       return;
528
529    BinEntry *binElement = dynamic_cast<BinEntry *>(docElement);
530    if( !binElement )
531       return;
532
533    LoadEntryBinArea(binElement);
534 }
535
536 /**
537  * \brief Loads (from disk) the element content 
538  *        when a string is not suitable
539  * @param elem  Entry whose binArea is going to be loaded
540  */
541 void Document::LoadEntryBinArea(BinEntry *elem) 
542 {
543    if(elem->GetBinArea())
544       return;
545
546    bool openFile = !Fp;
547    if(openFile)
548       OpenFile();
549
550    size_t o =(size_t)elem->GetOffset();
551    Fp->seekg(o, std::ios::beg);
552
553    size_t l = elem->GetLength();
554    uint8_t *a = new uint8_t[l];
555    if( !a )
556    {
557       gdcmWarningMacro( "Cannot allocate BinEntry content");
558       return;
559    }
560
561    /// \todo check the result 
562    Fp->read((char*)a, l);
563    if( Fp->fail() || Fp->eof())
564    {
565       delete[] a;
566       return;
567    }
568
569    elem->SetBinArea(a);
570
571    if(openFile)
572       CloseFile();
573 }
574
575 /**
576  * \brief  Loads the element while preserving the current
577  *         underlying file position indicator as opposed to
578  *        LoadDocEntry that modifies it.
579  * @param entry   DocEntry whose value will be loaded. 
580  */
581 void Document::LoadDocEntrySafe(DocEntry *entry)
582 {
583    if(Fp)
584    {
585       long PositionOnEntry = Fp->tellg();
586       LoadDocEntry(entry);
587       Fp->seekg(PositionOnEntry, std::ios::beg);
588    }
589 }
590
591 /**
592  * \brief   Compares two documents, according to \ref DicomDir rules
593  * \warning Does NOT work with ACR-NEMA files
594  * \todo    Find a trick to solve the pb (use RET fields ?)
595  * @param   document to compare with current one
596  * @return  true if 'smaller'
597  */
598 bool Document::operator<(Document &document)
599 {
600    // Patient Name
601    std::string s1 = GetEntryValue(0x0010,0x0010);
602    std::string s2 = document.GetEntryValue(0x0010,0x0010);
603    if(s1 < s2)
604    {
605       return true;
606    }
607    else if( s1 > s2 )
608    {
609       return false;
610    }
611    else
612    {
613       // Patient ID
614       s1 = GetEntryValue(0x0010,0x0020);
615       s2 = document.GetEntryValue(0x0010,0x0020);
616       if ( s1 < s2 )
617       {
618          return true;
619       }
620       else if ( s1 > s2 )
621       {
622          return false;
623       }
624       else
625       {
626          // Study Instance UID
627          s1 = GetEntryValue(0x0020,0x000d);
628          s2 = document.GetEntryValue(0x0020,0x000d);
629          if ( s1 < s2 )
630          {
631             return true;
632          }
633          else if( s1 > s2 )
634          {
635             return false;
636          }
637          else
638          {
639             // Serie Instance UID
640             s1 = GetEntryValue(0x0020,0x000e);
641             s2 = document.GetEntryValue(0x0020,0x000e);    
642             if ( s1 < s2 )
643             {
644                return true;
645             }
646             else if( s1 > s2 )
647             {
648                return false;
649             }
650          }
651       }
652    }
653    return false;
654 }
655
656 //-----------------------------------------------------------------------------
657 // Protected
658 /**
659  * \brief Reads a supposed to be 16 Bits integer
660  *       (swaps it depending on processor endianness) 
661  * @return read value
662  */
663 uint16_t Document::ReadInt16()
664    throw( FormatError )
665 {
666    uint16_t g;
667    Fp->read ((char*)&g, (size_t)2);
668    if ( Fp->fail() )
669    {
670       throw FormatError( "Document::ReadInt16()", " file error." );
671    }
672    if( Fp->eof() )
673    {
674       throw FormatError( "Document::ReadInt16()", "EOF." );
675    }
676    g = SwapShort(g); 
677    return g;
678 }
679
680 /**
681  * \brief  Reads a supposed to be 32 Bits integer
682  *        (swaps it depending on processor endianness)  
683  * @return read value
684  */
685 uint32_t Document::ReadInt32()
686    throw( FormatError )
687 {
688    uint32_t g;
689    Fp->read ((char*)&g, (size_t)4);
690    if ( Fp->fail() )
691    {
692       throw FormatError( "Document::ReadInt32()", " file error." );
693    }
694    if( Fp->eof() )
695    {
696       throw FormatError( "Document::ReadInt32()", "EOF." );
697    }
698    g = SwapLong(g);
699    return g;
700 }
701
702 /**
703  * \brief skips bytes inside the source file 
704  * \warning NOT end user intended method !
705  * @return 
706  */
707 void Document::SkipBytes(uint32_t nBytes)
708 {
709    //FIXME don't dump the returned value
710    Fp->seekg((long)nBytes, std::ios::cur);
711 }
712
713 /**
714  * \brief   Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
715  * @param filetype Type of the File to be written 
716  */
717 int Document::ComputeGroup0002Length( FileType filetype ) 
718 {
719    uint16_t gr;
720    std::string vr;
721    
722    int groupLength = 0;
723    bool found0002 = false;   
724   
725    // for each zero-level Tag in the DCM Header
726    DocEntry *entry = GetFirstEntry();
727    while( entry )
728    {
729       gr = entry->GetGroup();
730
731       if( gr == 0x0002 )
732       {
733          found0002 = true;
734
735          if( entry->GetElement() != 0x0000 )
736          {
737             vr = entry->GetVR();
738  
739             if( filetype == ExplicitVR )
740             {
741                if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) 
742                {
743                   // explicit VR AND OB, OW, SQ : 4 more bytes
744                   groupLength +=  4;
745                }
746             }
747             groupLength += 2 + 2 + 4 + entry->GetLength();   
748          }
749       }
750       else if (found0002 )
751          break;
752
753       entry = GetNextEntry();
754    }
755    return groupLength; 
756 }
757
758 //-----------------------------------------------------------------------------
759 // Private
760 /**
761  * \brief Loads all the needed Dictionaries
762  * \warning NOT end user intended method !   
763  */
764 void Document::Initialize() 
765 {
766    RefPubDict = Global::GetDicts()->GetDefaultPubDict();
767    RefShaDict = NULL;
768    Filetype   = Unknown;
769 }
770
771 /**
772  * \brief   Parses a DocEntrySet (Zero-level DocEntries or SQ Item DocEntries)
773  * @return  length of the parsed set. 
774  */ 
775 void Document::ParseDES(DocEntrySet *set, long offset, 
776                         long l_max, bool delim_mode)
777 {
778    DocEntry *newDocEntry = 0;
779    ValEntry *newValEntry;
780    BinEntry *newBinEntry;
781    SeqEntry *newSeqEntry;
782    VRKey vr;
783    bool used = false;
784
785    while (true)
786    {
787       if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
788       {
789          break;
790       }
791
792       used = true;
793       newDocEntry = ReadNextDocEntry( );
794
795       if ( !newDocEntry )
796       {
797          break;
798       }
799
800       vr = newDocEntry->GetVR();
801       newValEntry = dynamic_cast<ValEntry*>(newDocEntry);
802       newBinEntry = dynamic_cast<BinEntry*>(newDocEntry);
803       newSeqEntry = dynamic_cast<SeqEntry*>(newDocEntry);
804
805       if ( newValEntry || newBinEntry )
806       {
807          if ( newBinEntry )
808          {
809             if ( Filetype == ExplicitVR && 
810                  !Global::GetVR()->IsVROfBinaryRepresentable(vr) )
811             { 
812                 ////// Neither ValEntry NOR BinEntry: should mean UNKOWN VR
813                 gdcmWarningMacro( std::hex << newDocEntry->GetGroup() 
814                                   << "|" << newDocEntry->GetElement()
815                                   << " : Neither Valentry, nor BinEntry." 
816                                   "Probably unknown VR.");
817             }
818
819          //////////////////// BinEntry or UNKOWN VR:
820             // When "this" is a Document the Key is simply of the
821             // form ( group, elem )...
822             if ( dynamic_cast< Document* > ( set ) )
823             {
824                newBinEntry->SetKey( newBinEntry->GetKey() );
825             }
826             // but when "this" is a SQItem, we are inserting this new
827             // valEntry in a sequence item, and the key has the
828             // generalized form (refer to \ref BaseTagKey):
829             if (SQItem *parentSQItem = dynamic_cast< SQItem* > ( set ) )
830             {
831                newBinEntry->SetKey(  parentSQItem->GetBaseTagKey()
832                                    + newBinEntry->GetKey() );
833             }
834
835             LoadDocEntry( newBinEntry );
836             if( !set->AddEntry( newBinEntry ) )
837             {
838               //Expect big troubles if here
839               //delete newBinEntry;
840               used=false;
841             }
842          }
843          else
844          {
845          /////////////////////// ValEntry
846             // When "set" is a Document, then we are at the top of the
847             // hierarchy and the Key is simply of the form ( group, elem )...
848             if ( dynamic_cast< Document* > ( set ) )
849             {
850                newValEntry->SetKey( newValEntry->GetKey() );
851             }
852             // ...but when "set" is a SQItem, we are inserting this new
853             // valEntry in a sequence item. Hence the key has the
854             // generalized form (refer to \ref BaseTagKey):
855             if (SQItem *parentSQItem = dynamic_cast< SQItem* > ( set ) )
856             {
857                newValEntry->SetKey(  parentSQItem->GetBaseTagKey()
858                                    + newValEntry->GetKey() );
859             }
860              
861             LoadDocEntry( newValEntry );
862             bool delimitor=newValEntry->IsItemDelimitor();
863             if( !set->AddEntry( newValEntry ) )
864             {
865               // If here expect big troubles
866               //delete newValEntry; //otherwise mem leak
867               used=false;
868             }
869
870             if (delimitor)
871             {
872                if(!used)
873                   delete newDocEntry;
874                break;
875             }
876             if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
877             {
878                if(!used)
879                   delete newDocEntry;
880                break;
881             }
882          }
883
884          // Just to make sure we are at the beginning of next entry.
885          SkipToNextDocEntry(newDocEntry);
886       }
887       else
888       {
889          // VR = "SQ"
890          unsigned long l = newDocEntry->GetReadLength();            
891          if ( l != 0 ) // don't mess the delim_mode for zero-length sequence
892          {
893             if ( l == 0xffffffff )
894             {
895               delim_mode = true;
896             }
897             else
898             {
899               delim_mode = false;
900             }
901          }
902          // no other way to create it ...
903          newSeqEntry->SetDelimitorMode( delim_mode );
904
905          // At the top of the hierarchy, stands a Document. When "set"
906          // is a Document, then we are building the first depth level.
907          // Hence the SeqEntry we are building simply has a depth
908          // level of one:
909          if (/*Document *dummy =*/ dynamic_cast< Document* > ( set ) )
910          {
911             //(void)dummy;
912             newSeqEntry->SetDepthLevel( 1 );
913             newSeqEntry->SetKey( newSeqEntry->GetKey() );
914          }
915          // But when "set" is already a SQItem, we are building a nested
916          // sequence, and hence the depth level of the new SeqEntry
917          // we are building, is one level deeper:
918          if (SQItem *parentSQItem = dynamic_cast< SQItem* > ( set ) )
919          {
920             newSeqEntry->SetDepthLevel( parentSQItem->GetDepthLevel() + 1 );
921             newSeqEntry->SetKey(  parentSQItem->GetBaseTagKey()
922                                 + newSeqEntry->GetKey() );
923          }
924
925          if ( l != 0 )
926          {  // Don't try to parse zero-length sequences
927             ParseSQ( newSeqEntry, 
928                      newDocEntry->GetOffset(),
929                      l, delim_mode);
930          }
931          if( !set->AddEntry( newSeqEntry ) )
932          {
933             used = false;
934          }
935          if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
936          {
937             if( !used )
938                delete newDocEntry;
939             break;
940          }
941       }
942
943       if( !used )
944          delete newDocEntry;
945    }
946 }
947
948 /**
949  * \brief   Parses a Sequence ( SeqEntry after SeqEntry)
950  * @return  parsed length for this level
951  */ 
952 void Document::ParseSQ( SeqEntry *seqEntry,
953                         long offset, long l_max, bool delim_mode)
954 {
955    int SQItemNumber = 0;
956    bool dlm_mod;
957    long offsetStartCurrentSQItem = offset;
958
959    while (true)
960    {
961       // the first time, we read the fff0,e000 of the first SQItem
962       DocEntry *newDocEntry = ReadNextDocEntry();
963
964       if ( !newDocEntry )
965       {
966          // FIXME Should warn user
967          break;
968       }
969       if( delim_mode )
970       {
971          if ( newDocEntry->IsSequenceDelimitor() )
972          {
973             seqEntry->SetDelimitationItem( newDocEntry ); 
974             break;
975          }
976       }
977       if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
978       {
979          delete newDocEntry;
980          break;
981       }
982       // create the current SQItem
983       SQItem *itemSQ = new SQItem( seqEntry->GetDepthLevel() );
984       std::ostringstream newBase;
985       newBase << seqEntry->GetKey()
986               << "/"
987               << SQItemNumber
988               << "#";
989       itemSQ->SetBaseTagKey( newBase.str() );
990       unsigned int l = newDocEntry->GetReadLength();
991       
992       if ( l == 0xffffffff )
993       {
994          dlm_mod = true;
995       }
996       else
997       {
998          dlm_mod = false;
999       }
1000       // FIXME, TODO
1001       // when we're here, element fffe,e000 is already passed.
1002       // it's lost for the SQItem we're going to process !!
1003
1004       //ParseDES(itemSQ, newDocEntry->GetOffset(), l, dlm_mod);
1005       //delete newDocEntry; // FIXME well ... it's too late to use it !
1006
1007       // Let's try :------------
1008       // remove fff0,e000, created out of the SQItem
1009       delete newDocEntry;
1010       Fp->seekg(offsetStartCurrentSQItem, std::ios::beg);
1011       // fill up the current SQItem, starting at the beginning of fff0,e000
1012       ParseDES(itemSQ, offsetStartCurrentSQItem, l+8, dlm_mod);
1013       offsetStartCurrentSQItem = Fp->tellg();
1014       // end try -----------------
1015  
1016       seqEntry->AddSQItem( itemSQ, SQItemNumber ); 
1017       SQItemNumber++;
1018       if ( !delim_mode && ((long)(Fp->tellg())-offset ) >= l_max )
1019       {
1020          break;
1021       }
1022    }
1023 }
1024
1025 /**
1026  * \brief   Loads the element content if its length doesn't exceed
1027  *          the value specified with Document::SetMaxSizeLoadEntry()
1028  * @param   entry Header Entry (Dicom Element) to be dealt with
1029  */
1030 void Document::LoadDocEntry(DocEntry *entry)
1031 {
1032    uint16_t group  = entry->GetGroup();
1033    std::string  vr = entry->GetVR();
1034    uint32_t length = entry->GetLength();
1035
1036    Fp->seekg((long)entry->GetOffset(), std::ios::beg);
1037
1038    // A SeQuence "contains" a set of Elements.  
1039    //          (fffe e000) tells us an Element is beginning
1040    //          (fffe e00d) tells us an Element just ended
1041    //          (fffe e0dd) tells us the current SeQuence just ended
1042    if( group == 0xfffe )
1043    {
1044       // NO more value field for SQ !
1045       return;
1046    }
1047
1048    // When the length is zero things are easy:
1049    if ( length == 0 )
1050    {
1051       ((ValEntry *)entry)->SetValue("");
1052       return;
1053    }
1054
1055    // The elements whose length is bigger than the specified upper bound
1056    // are not loaded. Instead we leave a short notice of the offset of
1057    // the element content and it's length.
1058
1059    std::ostringstream s;
1060    if (length > MaxSizeLoadEntry)
1061    {
1062       if (BinEntry *binEntryPtr = dynamic_cast< BinEntry* >(entry) )
1063       {  
1064          //s << "gdcm::NotLoaded (BinEntry)";
1065          s << GDCM_NOTLOADED;
1066          s << " Address:" << (long)entry->GetOffset();
1067          s << " Length:"  << entry->GetLength();
1068          s << " x(" << std::hex << entry->GetLength() << ")";
1069          binEntryPtr->SetValue(s.str());
1070       }
1071       // Be carefull : a BinEntry IS_A ValEntry ... 
1072       else if (ValEntry *valEntryPtr = dynamic_cast< ValEntry* >(entry) )
1073       {
1074         // s << "gdcm::NotLoaded. (ValEntry)";
1075          s << GDCM_NOTLOADED;  
1076          s << " Address:" << (long)entry->GetOffset();
1077          s << " Length:"  << entry->GetLength();
1078          s << " x(" << std::hex << entry->GetLength() << ")";
1079          valEntryPtr->SetValue(s.str());
1080       }
1081       else
1082       {
1083          // fusible
1084          gdcmErrorMacro( "MaxSizeLoadEntry exceeded, neither a BinEntry "
1085                       << "nor a ValEntry ?! Should never print that !" );
1086       }
1087
1088       // to be sure we are at the end of the value ...
1089       Fp->seekg((long)entry->GetOffset()+(long)entry->GetLength(),
1090                 std::ios::beg);
1091       return;
1092    }
1093
1094    // When we find a BinEntry not very much can be done :
1095    if (BinEntry *binEntryPtr = dynamic_cast< BinEntry* >(entry) )
1096    {
1097       s << GDCM_BINLOADED;
1098       binEntryPtr->SetValue(s.str());
1099       LoadEntryBinArea(binEntryPtr); // last one, not to erase length !
1100       return;
1101    }
1102
1103    /// \todo Any compacter code suggested (?)
1104    if ( IsDocEntryAnInteger(entry) )
1105    {   
1106       uint32_t NewInt;
1107       int nbInt;
1108       // When short integer(s) are expected, read and convert the following 
1109       // n *two characters properly i.e. consider them as short integers as
1110       // opposed to strings.
1111       // Elements with Value Multiplicity > 1
1112       // contain a set of integers (not a single one)       
1113       if (vr == "US" || vr == "SS")
1114       {
1115          nbInt = length / 2;
1116          NewInt = ReadInt16();
1117          s << NewInt;
1118          if (nbInt > 1)
1119          {
1120             for (int i=1; i < nbInt; i++)
1121             {
1122                s << '\\';
1123                NewInt = ReadInt16();
1124                s << NewInt;
1125             }
1126          }
1127       }
1128       // See above comment on multiple integers (mutatis mutandis).
1129       else if (vr == "UL" || vr == "SL")
1130       {
1131          nbInt = length / 4;
1132          NewInt = ReadInt32();
1133          s << NewInt;
1134          if (nbInt > 1)
1135          {
1136             for (int i=1; i < nbInt; i++)
1137             {
1138                s << '\\';
1139                NewInt = ReadInt32();
1140                s << NewInt;
1141             }
1142          }
1143       }
1144 #ifdef GDCM_NO_ANSI_STRING_STREAM
1145       s << std::ends; // to avoid oddities on Solaris
1146 #endif //GDCM_NO_ANSI_STRING_STREAM
1147
1148       ((ValEntry *)entry)->SetValue(s.str());
1149       return;
1150    }
1151    
1152   // FIXME: We need an additional byte for storing \0 that is not on disk
1153    char *str = new char[length+1];
1154    Fp->read(str, (size_t)length);
1155    str[length] = '\0'; //this is only useful when length is odd
1156    // Special DicomString call to properly handle \0 and even length
1157    std::string newValue;
1158    if( length % 2 )
1159    {
1160       newValue = Util::DicomString(str, length+1);
1161       gdcmWarningMacro("Warning: bad length: " << length <<
1162                        ",For string :" <<  newValue.c_str()); 
1163       // Since we change the length of string update it length
1164       //entry->SetReadLength(length+1);
1165    }
1166    else
1167    {
1168       newValue = Util::DicomString(str, length);
1169    }
1170    delete[] str;
1171
1172    if ( ValEntry *valEntry = dynamic_cast<ValEntry* >(entry) )
1173    {
1174       if ( Fp->fail() || Fp->eof())
1175       {
1176          gdcmWarningMacro("Unread element value");
1177          valEntry->SetValue(GDCM_UNREAD);
1178          return;
1179       }
1180
1181       if( vr == "UI" )
1182       {
1183          // Because of correspondance with the VR dic
1184          valEntry->SetValue(newValue);
1185       }
1186       else
1187       {
1188          valEntry->SetValue(newValue);
1189       }
1190    }
1191    else
1192    {
1193       gdcmErrorMacro( "Should have a ValEntry, here !");
1194    }
1195 }
1196
1197 /**
1198  * \brief  Find the value Length of the passed Header Entry
1199  * @param  entry Header Entry whose length of the value shall be loaded. 
1200  */
1201 void Document::FindDocEntryLength( DocEntry *entry )
1202    throw ( FormatError )
1203 {
1204    std::string  vr  = entry->GetVR();
1205    uint16_t length16;       
1206    
1207    if ( Filetype == ExplicitVR && !entry->IsImplicitVR() ) 
1208    {
1209       if ( vr == "OB" || vr == "OW" || vr == "SQ" || vr == "UN" ) 
1210       {
1211          // The following reserved two bytes (see PS 3.5-2003, section
1212          // "7.1.2 Data element structure with explicit vr", p 27) must be
1213          // skipped before proceeding on reading the length on 4 bytes.
1214          Fp->seekg( 2L, std::ios::cur);
1215          uint32_t length32 = ReadInt32();
1216
1217          if ( (vr == "OB" || vr == "OW") && length32 == 0xffffffff ) 
1218          {
1219             uint32_t lengthOB;
1220             try 
1221             {
1222                lengthOB = FindDocEntryLengthOBOrOW();
1223             }
1224             catch ( FormatUnexpected )
1225             {
1226                // Computing the length failed (this happens with broken
1227                // files like gdcm-JPEG-LossLess3a.dcm). We still have a
1228                // chance to get the pixels by deciding the element goes
1229                // until the end of the file. Hence we artificially fix the
1230                // the length and proceed.
1231                long currentPosition = Fp->tellg();
1232                Fp->seekg(0L,std::ios::end);
1233
1234                long lengthUntilEOF = (long)(Fp->tellg())-currentPosition;
1235                Fp->seekg(currentPosition, std::ios::beg);
1236
1237                entry->SetReadLength(lengthUntilEOF);
1238                entry->SetLength(lengthUntilEOF);
1239                return;
1240             }
1241             entry->SetReadLength(lengthOB);
1242             entry->SetLength(lengthOB);
1243             return;
1244          }
1245          FixDocEntryFoundLength(entry, length32); 
1246          return;
1247       }
1248
1249       // Length is encoded on 2 bytes.
1250       length16 = ReadInt16();
1251
1252       // FIXME : This heuristic supposes that the first group following
1253       //         group 0002 *has* and element 0000.
1254       // BUT ... Element 0000 is optionnal :-(
1255
1256
1257    // Fixed using : HandleOutOfGroup0002()
1258    //              (first hereafter strategy ...)
1259       
1260       // We can tell the current file is encoded in big endian (like
1261       // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
1262       // and it's value is the one of the encoding of a big endian file.
1263       // In order to deal with such big endian encoded files, we have
1264       // (at least) two strategies:
1265       // * when we load the "Transfer Syntax" tag with value of big endian
1266       //   encoding, we raise the proper flags. Then we wait for the end
1267       //   of the META group (0x0002) among which is "Transfer Syntax",
1268       //   before switching the swap code to big endian. We have to postpone
1269       //   the switching of the swap code since the META group is fully encoded
1270       //   in little endian, and big endian coding only starts at the next
1271       //   group. The corresponding code can be hard to analyse and adds
1272       //   many additional unnecessary tests for regular tags.
1273       // * the second strategy consists in waiting for trouble, that shall
1274       //   appear when we find the first group with big endian encoding. This
1275       //   is easy to detect since the length of a "Group Length" tag (the
1276       //   ones with zero as element number) has to be of 4 (0x0004). When we
1277       //   encounter 1024 (0x0400) chances are the encoding changed and we
1278       //   found a group with big endian encoding.
1279       //---> Unfortunately, element 0000 is optional.
1280       //---> This will not work when missing!
1281       // We shall use this second strategy. In order to make sure that we
1282       // can interpret the presence of an apparently big endian encoded
1283       // length of a "Group Length" without committing a big mistake, we
1284       // add an additional check: we look in the already parsed elements
1285       // for the presence of a "Transfer Syntax" whose value has to be "big
1286       // endian encoding". When this is the case, chances are we have got our
1287       // hands on a big endian encoded file: we switch the swap code to
1288       // big endian and proceed...
1289
1290 //      if ( element  == 0x0000 && length16 == 0x0400 ) 
1291 //      {
1292 //         std::string ts = GetTransferSyntax();
1293 //         if ( Global::GetTS()->GetSpecialTransferSyntax(ts) 
1294 //                != TS::ExplicitVRBigEndian ) 
1295 //         {
1296 //            throw FormatError( "Document::FindDocEntryLength()",
1297 //                               " not explicit VR." );
1298 //           return;
1299 //        }
1300 //        length16 = 4;
1301 //        SwitchByteSwapCode();
1302 //
1303 //         // Restore the unproperly loaded values i.e. the group, the element
1304 //         // and the dictionary entry depending on them.
1305 //         uint16_t correctGroup = SwapShort( entry->GetGroup() );
1306 //         uint16_t correctElem  = SwapShort( entry->GetElement() );
1307 //         DictEntry *newTag = GetDictEntry( correctGroup, correctElem );
1308 //         if ( !newTag )
1309 //         {
1310 //            // This correct tag is not in the dictionary. Create a new one.
1311 //            newTag = NewVirtualDictEntry(correctGroup, correctElem);
1312 //         }
1313 //         // FIXME this can create a memory leaks on the old entry that be
1314 //         // left unreferenced.
1315 //         entry->SetDictEntry( newTag );
1316 //      }
1317   
1318       // 0xffff means that we deal with 'No Length' Sequence 
1319       //        or 'No Length' SQItem
1320       if ( length16 == 0xffff) 
1321       {           
1322          length16 = 0;
1323       }
1324       FixDocEntryFoundLength( entry, (uint32_t)length16 );
1325       return;
1326    }
1327    else
1328    {
1329       // Either implicit VR or a non DICOM conformal (see note below) explicit
1330       // VR that ommited the VR of (at least) this element. Farts happen.
1331       // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1332       // on Data elements "Implicit and Explicit VR Data Elements shall
1333       // not coexist in a Data Set and Data Sets nested within it".]
1334       // Length is on 4 bytes.
1335
1336      // Well ... group 0002 is always coded in 'Explicit VR Litle Endian'
1337      // even if Transfer Syntax is 'Implicit VR ...' 
1338       
1339       FixDocEntryFoundLength( entry, ReadInt32() );
1340       return;
1341    }
1342 }
1343
1344 /**
1345  * \brief  Find the Length till the next sequence delimiter
1346  * \warning NOT end user intended method !
1347  * @return 
1348  */
1349 uint32_t Document::FindDocEntryLengthOBOrOW()
1350    throw( FormatUnexpected )
1351 {
1352    // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
1353    long positionOnEntry = Fp->tellg();
1354    bool foundSequenceDelimiter = false;
1355    uint32_t totalLength = 0;
1356
1357    while ( !foundSequenceDelimiter )
1358    {
1359       uint16_t group;
1360       uint16_t elem;
1361       try
1362       {
1363          group = ReadInt16();
1364          elem  = ReadInt16();   
1365       }
1366       catch ( FormatError )
1367       {
1368          throw FormatError("Unexpected end of file encountered during ",
1369                            "Document::FindDocEntryLengthOBOrOW()");
1370       }
1371       // We have to decount the group and element we just read
1372       totalLength += 4;     
1373       if ( group != 0xfffe || ( ( elem != 0xe0dd ) && ( elem != 0xe000 ) ) )
1374       {
1375          long filePosition = Fp->tellg();
1376          gdcmWarningMacro( "Neither an Item tag nor a Sequence delimiter tag on :" 
1377            << std::hex << group << " , " << elem 
1378            << ") -before- position x(" << filePosition << ")" );
1379   
1380          Fp->seekg(positionOnEntry, std::ios::beg);
1381          throw FormatUnexpected( "Neither an Item tag nor a Sequence delimiter tag.");
1382       }
1383       if ( elem == 0xe0dd )
1384       {
1385          foundSequenceDelimiter = true;
1386       }
1387       uint32_t itemLength = ReadInt32();
1388       // We add 4 bytes since we just read the ItemLength with ReadInt32
1389       totalLength += itemLength + 4;
1390       SkipBytes(itemLength);
1391       
1392       if ( foundSequenceDelimiter )
1393       {
1394          break;
1395       }
1396    }
1397    Fp->seekg( positionOnEntry, std::ios::beg);
1398    return totalLength;
1399 }
1400
1401 /**
1402  * \brief     Find the Value Representation of the current Dicom Element.
1403  * @return    Value Representation of the current Entry
1404  */
1405 std::string Document::FindDocEntryVR()
1406 {
1407    if ( Filetype != ExplicitVR )
1408       return GDCM_UNKNOWN;
1409
1410    long positionOnEntry = Fp->tellg();
1411    // Warning: we believe this is explicit VR (Value Representation) because
1412    // we used a heuristic that found "UL" in the first tag. Alas this
1413    // doesn't guarantee that all the tags will be in explicit VR. In some
1414    // cases (see e-film filtered files) one finds implicit VR tags mixed
1415    // within an explicit VR file. Hence we make sure the present tag
1416    // is in explicit VR and try to fix things if it happens not to be
1417    // the case.
1418
1419    char vr[3];
1420    Fp->read (vr, (size_t)2);
1421    vr[2] = 0;
1422
1423    if( !CheckDocEntryVR(vr) )
1424    {
1425       Fp->seekg(positionOnEntry, std::ios::beg);
1426       return GDCM_UNKNOWN;
1427    }
1428    return vr;
1429 }
1430
1431 /**
1432  * \brief     Check the correspondance between the VR of the header entry
1433  *            and the taken VR. If they are different, the header entry is 
1434  *            updated with the new VR.
1435  * @param     vr    Dicom Value Representation
1436  * @return    false if the VR is incorrect of if the VR isn't referenced
1437  *            otherwise, it returns true
1438 */
1439 bool Document::CheckDocEntryVR(VRKey vr)
1440 {
1441    if ( !Global::GetVR()->IsValidVR(vr) )
1442       return false;
1443
1444    return true; 
1445 }
1446
1447 /**
1448  * \brief   Get the transformed value of the header entry. The VR value 
1449  *          is used to define the transformation to operate on the value
1450  * \warning NOT end user intended method !
1451  * @param   entry entry to tranform
1452  * @return  Transformed entry value
1453  */
1454 std::string Document::GetDocEntryValue(DocEntry *entry)
1455 {
1456    if ( IsDocEntryAnInteger(entry) && entry->IsImplicitVR() )
1457    {
1458       std::string val = ((ValEntry *)entry)->GetValue();
1459       std::string vr  = entry->GetVR();
1460       uint32_t length = entry->GetLength();
1461       std::ostringstream s;
1462       int nbInt;
1463
1464       // When short integer(s) are expected, read and convert the following 
1465       // n * 2 bytes properly i.e. as a multivaluated strings
1466       // (each single value is separated fromthe next one by '\'
1467       // as usual for standard multivaluated filels
1468       // Elements with Value Multiplicity > 1
1469       // contain a set of short integers (not a single one) 
1470    
1471       if( vr == "US" || vr == "SS" )
1472       {
1473          uint16_t newInt16;
1474
1475          nbInt = length / 2;
1476          for (int i=0; i < nbInt; i++) 
1477          {
1478             if( i != 0 )
1479             {
1480                s << '\\';
1481             }
1482             newInt16 = ( val[2*i+0] & 0xFF ) + ( ( val[2*i+1] & 0xFF ) << 8);
1483             newInt16 = SwapShort( newInt16 );
1484             s << newInt16;
1485          }
1486       }
1487
1488       // When integer(s) are expected, read and convert the following 
1489       // n * 4 bytes properly i.e. as a multivaluated strings
1490       // (each single value is separated fromthe next one by '\'
1491       // as usual for standard multivaluated filels
1492       // Elements with Value Multiplicity > 1
1493       // contain a set of integers (not a single one) 
1494       else if( vr == "UL" || vr == "SL" )
1495       {
1496          uint32_t newInt32;
1497
1498          nbInt = length / 4;
1499          for (int i=0; i < nbInt; i++) 
1500          {
1501             if( i != 0)
1502             {
1503                s << '\\';
1504             }
1505             newInt32 = ( val[4*i+0] & 0xFF )
1506                     + (( val[4*i+1] & 0xFF ) <<  8 )
1507                     + (( val[4*i+2] & 0xFF ) << 16 )
1508                     + (( val[4*i+3] & 0xFF ) << 24 );
1509             newInt32 = SwapLong( newInt32 );
1510             s << newInt32;
1511          }
1512       }
1513 #ifdef GDCM_NO_ANSI_STRING_STREAM
1514       s << std::ends; // to avoid oddities on Solaris
1515 #endif //GDCM_NO_ANSI_STRING_STREAM
1516       return s.str();
1517    }
1518    return ((ValEntry *)entry)->GetValue();
1519 }
1520
1521 /**
1522  * \brief   Get the reverse transformed value of the header entry. The VR 
1523  *          value is used to define the reverse transformation to operate on
1524  *          the value
1525  * \warning NOT end user intended method !
1526  * @param   entry Entry to reverse transform
1527  * @return  Reverse transformed entry value
1528  */
1529 std::string Document::GetDocEntryUnvalue(DocEntry *entry)
1530 {
1531    if ( IsDocEntryAnInteger(entry) && entry->IsImplicitVR() )
1532    {
1533       std::string vr = entry->GetVR();
1534       std::vector<std::string> tokens;
1535       std::ostringstream s;
1536
1537       if ( vr == "US" || vr == "SS" ) 
1538       {
1539          uint16_t newInt16;
1540
1541          tokens.erase( tokens.begin(), tokens.end()); // clean any previous value
1542          Util::Tokenize (((ValEntry *)entry)->GetValue(), tokens, "\\");
1543          for (unsigned int i=0; i<tokens.size(); i++) 
1544          {
1545             newInt16 = atoi(tokens[i].c_str());
1546             s << (  newInt16        & 0xFF ) 
1547               << (( newInt16 >> 8 ) & 0xFF );
1548          }
1549          tokens.clear();
1550       }
1551       if ( vr == "UL" || vr == "SL")
1552       {
1553          uint32_t newInt32;
1554
1555          tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1556          Util::Tokenize (((ValEntry *)entry)->GetValue(), tokens, "\\");
1557          for (unsigned int i=0; i<tokens.size();i++) 
1558          {
1559             newInt32 = atoi(tokens[i].c_str());
1560             s << (char)(  newInt32         & 0xFF ) 
1561               << (char)(( newInt32 >>  8 ) & 0xFF )
1562               << (char)(( newInt32 >> 16 ) & 0xFF )
1563               << (char)(( newInt32 >> 24 ) & 0xFF );
1564          }
1565          tokens.clear();
1566       }
1567
1568 #ifdef GDCM_NO_ANSI_STRING_STREAM
1569       s << std::ends; // to avoid oddities on Solaris
1570 #endif //GDCM_NO_ANSI_STRING_STREAM
1571       return s.str();
1572    }
1573
1574    return ((ValEntry *)entry)->GetValue();
1575 }
1576
1577 /**
1578  * \brief   Skip a given Header Entry 
1579  * \warning NOT end user intended method !
1580  * @param   entry entry to skip
1581  */
1582 void Document::SkipDocEntry(DocEntry *entry) 
1583 {
1584    SkipBytes(entry->GetLength());
1585 }
1586
1587 /**
1588  * \brief   Skips to the beginning of the next Header Entry 
1589  * \warning NOT end user intended method !
1590  * @param   currentDocEntry entry to skip
1591  */
1592 void Document::SkipToNextDocEntry(DocEntry *currentDocEntry) 
1593 {
1594    Fp->seekg((long)(currentDocEntry->GetOffset()),     std::ios::beg);
1595    if (currentDocEntry->GetGroup() != 0xfffe)  // for fffe pb
1596       Fp->seekg( (long)(currentDocEntry->GetReadLength()),std::ios::cur);
1597 }
1598
1599 /**
1600  * \brief   When the length of an element value is obviously wrong (because
1601  *          the parser went Jabberwocky) one can hope improving things by
1602  *          applying some heuristics.
1603  * @param   entry entry to check
1604  * @param   foundLength first assumption about length    
1605  */
1606 void Document::FixDocEntryFoundLength(DocEntry *entry,
1607                                       uint32_t foundLength)
1608 {
1609    entry->SetReadLength( foundLength ); // will be updated only if a bug is found        
1610    if ( foundLength == 0xffffffff)
1611    {
1612       foundLength = 0;
1613    }
1614    
1615    uint16_t gr   = entry->GetGroup();
1616    uint16_t elem = entry->GetElement(); 
1617      
1618    if ( foundLength % 2)
1619    {
1620       gdcmWarningMacro( "Warning : Tag with uneven length " << foundLength 
1621         <<  " in x(" << std::hex << gr << "," << elem <<")");
1622    }
1623       
1624    //////// Fix for some naughty General Electric images.
1625    // Allthough not recent many such GE corrupted images are still present
1626    // on Creatis hard disks. Hence this fix shall remain when such images
1627    // are no longer in use (we are talking a few years, here)...
1628    // Note: XMedCom probably uses such a trick since it is able to read
1629    //       those pesky GE images ...
1630    if ( foundLength == 13)
1631    {
1632       // Only happens for this length !
1633       if ( gr != 0x0008 || ( elem != 0x0070 && elem != 0x0080 ) )
1634       {
1635          foundLength = 10;
1636          entry->SetReadLength(10); /// \todo a bug is to be fixed !?
1637       }
1638    }
1639
1640    //////// Fix for some brain-dead 'Leonardo' Siemens images.
1641    // Occurence of such images is quite low (unless one leaves close to a
1642    // 'Leonardo' source. Hence, one might consider commenting out the
1643    // following fix on efficiency reasons.
1644    else if ( gr   == 0x0009 && ( elem == 0x1113 || elem == 0x1114 ) )
1645    {
1646       foundLength = 4;
1647       entry->SetReadLength(4); /// \todo a bug is to be fixed !?
1648    } 
1649  
1650    else if ( entry->GetVR() == "SQ" )
1651    {
1652       foundLength = 0;      // ReadLength is unchanged 
1653    } 
1654     
1655    //////// We encountered a 'delimiter' element i.e. a tag of the form 
1656    // "fffe|xxxx" which is just a marker. Delimiters length should not be
1657    // taken into account.
1658    else if( gr == 0xfffe )
1659    {    
1660      // According to the norm, fffe|0000 shouldn't exist. BUT the Philips
1661      // image gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm happens to
1662      // causes extra troubles...
1663      if( entry->GetElement() != 0x0000 )
1664      {
1665         foundLength = 0;
1666      }
1667    }            
1668    entry->SetLength(foundLength);
1669 }
1670
1671 /**
1672  * \brief   Apply some heuristics to predict whether the considered 
1673  *          element value contains/represents an integer or not.
1674  * @param   entry The element value on which to apply the predicate.
1675  * @return  The result of the heuristical predicate.
1676  */
1677 bool Document::IsDocEntryAnInteger(DocEntry *entry)
1678 {
1679    uint16_t elem          = entry->GetElement();
1680    uint16_t group         = entry->GetGroup();
1681    const std::string &vr  = entry->GetVR();
1682    uint32_t length        = entry->GetLength();
1683
1684    // When we have some semantics on the element we just read, and if we
1685    // a priori know we are dealing with an integer, then we shall be
1686    // able to swap it's element value properly.
1687    if ( elem == 0 )  // This is the group length of the group
1688    {  
1689       if ( length == 4 )
1690       {
1691          return true;
1692       }
1693       else 
1694       {
1695          // Allthough this should never happen, still some images have a
1696          // corrupted group length [e.g. have a glance at offset x(8336) of
1697          // gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm].
1698          // Since for dicom compliant and well behaved headers, the present
1699          // test is useless (and might even look a bit paranoid), when we
1700          // encounter such an ill-formed image, we simply display a warning
1701          // message and proceed on parsing (while crossing fingers).
1702          long filePosition = Fp->tellg();
1703          gdcmWarningMacro( "Erroneous Group Length element length  on : (" 
1704            << std::hex << group << " , " << elem
1705            << ") -before- position x(" << filePosition << ")"
1706            << "lgt : " << length );
1707       }
1708    }
1709
1710    if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
1711    {
1712       return true;
1713    }   
1714    return false;
1715 }
1716
1717 /**
1718  * \brief   Discover what the swap code is (among little endian, big endian,
1719  *          bad little endian, bad big endian).
1720  *          sw is set
1721  * @return false when we are absolutely sure 
1722  *               it's neither ACR-NEMA nor DICOM
1723  *         true  when we hope ours assuptions are OK
1724  */
1725 bool Document::CheckSwap()
1726 {
1727    // The only guaranted way of finding the swap code is to find a
1728    // group tag since we know it's length has to be of four bytes i.e.
1729    // 0x00000004. Finding the swap code in then straigthforward. Trouble
1730    // occurs when we can't find such group...
1731    
1732    uint32_t  x = 4;  // x : for ntohs
1733    bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
1734    uint32_t  s32;
1735    uint16_t  s16;
1736        
1737    char deb[256];
1738     
1739    // First, compare HostByteOrder and NetworkByteOrder in order to
1740    // determine if we shall need to swap bytes (i.e. the Endian type).
1741    if ( x == ntohs(x) )
1742    {
1743       net2host = true;
1744    }
1745    else
1746    {
1747       net2host = false;
1748    }
1749          
1750    // The easiest case is the one of a 'true' DICOM header, we just have
1751    // to look for the string "DICM" inside the file preamble.
1752    Fp->read(deb, 256);
1753    
1754    char *entCur = deb + 128;
1755    if( memcmp(entCur, "DICM", (size_t)4) == 0 )
1756    {
1757       gdcmWarningMacro( "Looks like DICOM Version3 (preamble + DCM)" );
1758       
1759       // Group 0002 should always be VR, and the first element 0000
1760       // Let's be carefull (so many wrong headers ...)
1761       // and determine the value representation (VR) : 
1762       // Let's skip to the first element (0002,0000) and check there if we find
1763       // "UL"  - or "OB" if the 1st one is (0002,0001) -,
1764       // in which case we (almost) know it is explicit VR.
1765       // WARNING: if it happens to be implicit VR then what we will read
1766       // is the length of the group. If this ascii representation of this
1767       // length happens to be "UL" then we shall believe it is explicit VR.
1768       // We need to skip :
1769       // * the 128 bytes of File Preamble (often padded with zeroes),
1770       // * the 4 bytes of "DICM" string,
1771       // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
1772       // i.e. a total of  136 bytes.
1773       entCur = deb + 136;
1774      
1775       // group 0x0002 *is always* Explicit VR Sometimes ,
1776       // even if elem 0002,0010 (Transfer Syntax) tells us the file is
1777       // *Implicit* VR  (see former 'gdcmData/icone.dcm')
1778       
1779       if( memcmp(entCur, "UL", (size_t)2) == 0 ||
1780           memcmp(entCur, "OB", (size_t)2) == 0 ||
1781           memcmp(entCur, "UI", (size_t)2) == 0 ||
1782           memcmp(entCur, "CS", (size_t)2) == 0 )  // CS, to remove later
1783                                                   // when Write DCM *adds*
1784       // FIXME
1785       // Use Document::dicom_vr to test all the possibilities
1786       // instead of just checking for UL, OB and UI !? group 0000 
1787       {
1788          Filetype = ExplicitVR;
1789          gdcmWarningMacro( "Group 0002 : Explicit Value Representation");
1790       } 
1791       else 
1792       {
1793          Filetype = ImplicitVR;
1794          gdcmWarningMacro( "Group 0002 :Not an explicit Value Representation;"
1795                         << "Looks like a bugged Header!");
1796       }
1797       
1798       if ( net2host )
1799       {
1800          SwapCode = 4321;
1801          gdcmWarningMacro( "HostByteOrder != NetworkByteOrder");
1802       }
1803       else 
1804       {
1805          SwapCode = 1234;
1806          gdcmWarningMacro( "HostByteOrder = NetworkByteOrder");
1807       }
1808       
1809       // Position the file position indicator at first tag 
1810       // (i.e. after the file preamble and the "DICM" string).
1811       Fp->seekg(0, std::ios::beg);
1812       Fp->seekg ( 132L, std::ios::beg);
1813       return true;
1814    } // End of DicomV3
1815
1816    // Alas, this is not a DicomV3 file and whatever happens there is no file
1817    // preamble. We can reset the file position indicator to where the data
1818    // is (i.e. the beginning of the file).
1819    gdcmWarningMacro( "Not a DICOM Version3 file");
1820    Fp->seekg(0, std::ios::beg);
1821
1822    // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
1823    // By clean we mean that the length of the first tag is written down.
1824    // If this is the case and since the length of the first group HAS to be
1825    // four (bytes), then determining the proper swap code is straightforward.
1826
1827    entCur = deb + 4;
1828    // We assume the array of char we are considering contains the binary
1829    // representation of a 32 bits integer. Hence the following dirty
1830    // trick :
1831    s32 = *((uint32_t *)(entCur));
1832
1833    switch( s32 )
1834    {
1835       case 0x00040000 :
1836          SwapCode = 3412;
1837          Filetype = ACR;
1838          return true;
1839       case 0x04000000 :
1840          SwapCode = 4321;
1841          Filetype = ACR;
1842          return true;
1843       case 0x00000400 :
1844          SwapCode = 2143;
1845          Filetype = ACR;
1846          return true;
1847       case 0x00000004 :
1848          SwapCode = 1234;
1849          Filetype = ACR;
1850          return true;
1851       default :
1852          // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
1853          // It is time for despaired wild guesses. 
1854          // So, let's check if this file wouldn't happen to be 'dirty' ACR/NEMA,
1855          //  i.e. the 'group length' element is not present :     
1856          
1857          //  check the supposed-to-be 'group number'
1858          //  in ( 0x0001 .. 0x0008 )
1859          //  to determine ' SwapCode' value .
1860          //  Only 0 or 4321 will be possible 
1861          //  (no oportunity to check for the formerly well known
1862          //  ACR-NEMA 'Bad Big Endian' or 'Bad Little Endian' 
1863          //  if unsuccessfull (i.e. neither 0x0002 nor 0x0200 etc -3, 4, ..., 8-) 
1864          //  the file IS NOT ACR-NEMA nor DICOM V3
1865          //  Find a trick to tell it the caller...
1866       
1867          s16 = *((uint16_t *)(deb));
1868       
1869          switch ( s16 )
1870          {
1871             case 0x0001 :
1872             case 0x0002 :
1873             case 0x0003 :
1874             case 0x0004 :
1875             case 0x0005 :
1876             case 0x0006 :
1877             case 0x0007 :
1878             case 0x0008 :
1879                SwapCode = 1234;
1880                Filetype = ACR;
1881                return true;
1882             case 0x0100 :
1883             case 0x0200 :
1884             case 0x0300 :
1885             case 0x0400 :
1886             case 0x0500 :
1887             case 0x0600 :
1888             case 0x0700 :
1889             case 0x0800 :
1890                SwapCode = 4321;
1891                Filetype = ACR;
1892                return true;
1893             default :
1894                gdcmWarningMacro( "ACR/NEMA unfound swap info (Really hopeless !)");
1895                Filetype = Unknown;
1896                return false;
1897          }
1898    }
1899 }
1900
1901 /**
1902  * \brief Change the Byte Swap code. 
1903  */
1904 void Document::SwitchByteSwapCode() 
1905 {
1906    gdcmWarningMacro( "Switching Byte Swap code from "<< SwapCode);
1907    if ( SwapCode == 1234 ) 
1908    {
1909       SwapCode = 4321;
1910    }
1911    else if ( SwapCode == 4321 ) 
1912    {
1913       SwapCode = 1234;
1914    }
1915    else if ( SwapCode == 3412 ) 
1916    {
1917       SwapCode = 2143;
1918    }
1919    else if ( SwapCode == 2143 )
1920    {
1921       SwapCode = 3412;
1922    }
1923 }
1924
1925 /**
1926  * \brief  during parsing, Header Elements too long are not loaded in memory 
1927  * @param newSize
1928  */
1929 void Document::SetMaxSizeLoadEntry(long newSize) 
1930 {
1931    if ( newSize < 0 )
1932    {
1933       return;
1934    }
1935    if ((uint32_t)newSize >= (uint32_t)0xffffffff )
1936    {
1937       MaxSizeLoadEntry = 0xffffffff;
1938       return;
1939    }
1940    MaxSizeLoadEntry = newSize;
1941 }
1942
1943 /**
1944  * \brief Header Elements too long will not be printed
1945  * \todo  See comments of \ref Document::MAX_SIZE_PRINT_ELEMENT_VALUE 
1946  * @param newSize
1947  */
1948 void Document::SetMaxSizePrintEntry(long newSize) 
1949 {
1950    if ( newSize < 0 )
1951    {
1952       return;
1953    }
1954    if ((uint32_t)newSize >= (uint32_t)0xffffffff )
1955    {
1956       MaxSizePrintEntry = 0xffffffff;
1957       return;
1958    }
1959    MaxSizePrintEntry = newSize;
1960 }
1961
1962
1963 /**
1964  * \brief   Read the next tag but WITHOUT loading it's value
1965  *          (read the 'Group Number', the 'Element Number',
1966  *          gets the Dict Entry
1967  *          gets the VR, gets the length, gets the offset value)
1968  * @return  On succes the newly created DocEntry, NULL on failure.      
1969  */
1970 DocEntry *Document::ReadNextDocEntry()
1971 {
1972    uint16_t group;
1973    uint16_t elem;
1974
1975    try
1976    {
1977       group = ReadInt16();
1978       elem  = ReadInt16();
1979    }
1980    catch ( FormatError e )
1981    {
1982       // We reached the EOF (or an error occured) therefore 
1983       // header parsing has to be considered as finished.
1984       return 0;
1985    }
1986
1987    // Sometimes file contains groups of tags with reversed endianess.
1988    HandleBrokenEndian(group, elem);
1989
1990    // In 'true DICOM' files Group 0002 is always little endian
1991    if ( HasDCMPreamble )
1992       HandleOutOfGroup0002(group, elem);
1993  
1994    std::string vr = FindDocEntryVR();
1995    std::string realVR = vr;
1996
1997    if( vr == GDCM_UNKNOWN)
1998    {
1999       DictEntry *dictEntry = GetDictEntry(group,elem);
2000       if( dictEntry )
2001          realVR = dictEntry->GetVR();
2002    }
2003
2004    DocEntry *newEntry;
2005    if( Global::GetVR()->IsVROfSequence(realVR) )
2006       newEntry = NewSeqEntry(group, elem);
2007    else if( Global::GetVR()->IsVROfStringRepresentable(realVR) )
2008       newEntry = NewValEntry(group, elem,vr);
2009    else
2010       newEntry = NewBinEntry(group, elem,vr);
2011
2012    if( vr == GDCM_UNKNOWN )
2013    {
2014       if( Filetype == ExplicitVR )
2015       {
2016          // We thought this was explicit VR, but we end up with an
2017          // implicit VR tag. Let's backtrack.
2018          if ( newEntry->GetGroup() != 0xfffe )
2019          { 
2020             std::string msg;
2021             msg = Util::Format("Entry (%04x,%04x) should be Explicit VR\n", 
2022                           newEntry->GetGroup(), newEntry->GetElement());
2023             gdcmWarningMacro( msg.c_str() );
2024           }
2025       }
2026       newEntry->SetImplicitVR();
2027    }
2028
2029    try
2030    {
2031       FindDocEntryLength(newEntry);
2032    }
2033    catch ( FormatError e )
2034    {
2035       // Call it quits
2036       //std::cout << e;
2037       delete newEntry;
2038       return 0;
2039    }
2040
2041    newEntry->SetOffset(Fp->tellg());  
2042
2043    return newEntry;
2044 }
2045
2046 /**
2047  * \brief   Handle broken private tag from Philips NTSCAN
2048  *          where the endianess is being switch to BigEndian for no
2049  *          apparent reason
2050  * @return  no return
2051  */
2052 void Document::HandleBrokenEndian(uint16_t &group, uint16_t &elem)
2053 {
2054    // Endian reversion. Some files contain groups of tags with reversed endianess.
2055    static int reversedEndian = 0;
2056    // try to fix endian switching in the middle of headers
2057    if ((group == 0xfeff) && (elem == 0x00e0))
2058    {
2059      // start endian swap mark for group found
2060      reversedEndian++;
2061      SwitchByteSwapCode();
2062      // fix the tag
2063      group = 0xfffe;
2064      elem  = 0xe000;
2065    } 
2066    else if (group == 0xfffe && elem == 0xe00d && reversedEndian) 
2067    {
2068      // end of reversed endian group
2069      reversedEndian--;
2070      SwitchByteSwapCode();
2071    }
2072 }
2073
2074 /**
2075  * \brief   Group 0002 is always coded Little Endian
2076  *          whatever Transfer Syntax is
2077  * @return  no return
2078  */
2079 void Document::HandleOutOfGroup0002(uint16_t &group, uint16_t &elem)
2080 {
2081    // Endian reversion. Some files contain groups of tags with reversed endianess.
2082    if ( !Group0002Parsed && group != 0x0002)
2083    {
2084       Group0002Parsed = true;
2085       // we just came out of group 0002
2086       // if Transfer syntax is Big Endian we have to change CheckSwap
2087
2088       std::string ts = GetTransferSyntax();
2089       if ( !Global::GetTS()->IsTransferSyntax(ts) )
2090       {
2091          gdcmWarningMacro("True DICOM File, with NO Tansfer Syntax: " << ts );
2092          return;
2093       }
2094
2095       // Group 0002 is always 'Explicit ...' enven when Transfer Syntax says 'Implicit ..." 
2096
2097       if ( Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ImplicitVRLittleEndian )
2098          {
2099             Filetype = ImplicitVR;
2100          }
2101        
2102       // FIXME Strangely, this works with 
2103       //'Implicit VR Transfer Syntax (GE Private)
2104       if ( Global::GetTS()->GetSpecialTransferSyntax(ts) == TS::ExplicitVRBigEndian )
2105       {
2106          gdcmWarningMacro("Transfer Syntax Name = [" 
2107                         << GetTransferSyntaxName() << "]" );
2108          SwitchByteSwapCode();
2109          group = SwapShort(group);
2110          elem  = SwapShort(elem);
2111       }
2112    }
2113 }
2114
2115 //-----------------------------------------------------------------------------
2116 // Print
2117
2118 //-----------------------------------------------------------------------------
2119 } // end namespace gdcm