]> Creatis software - gdcm.git/blob - src/gdcmDocument.cxx
24777f51729a1ecef83fea09ed7850b9b00059a9
[gdcm.git] / src / gdcmDocument.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/11 22:46:22 $
7   Version:   $Revision: 1.179 $
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 "gdcmException.h"
28 #include "gdcmDictSet.h"
29 #include "gdcmRLEFramesInfo.h"
30 #include "gdcmJPEGFragmentsInfo.h"
31 #include "gdcmDocEntrySet.h"
32 #include "gdcmSQItem.h"
33
34 #include <vector>
35 #include <iomanip>
36
37 // For nthos:
38 #if defined(_MSC_VER) || defined(__BORLANDC__)
39    #include <winsock.h>
40 #else
41    #include <netinet/in.h>
42 #endif
43
44 namespace gdcm 
45 {
46
47 //-----------------------------------------------------------------------------
48 // Refer to Document::CheckSwap()
49 //const unsigned int Document::HEADER_LENGTH_TO_READ = 256;
50
51 // Refer to Document::SetMaxSizeLoadEntry()
52 const unsigned int Document::MAX_SIZE_LOAD_ELEMENT_VALUE = 0xfff; // 4096
53 const unsigned int Document::MAX_SIZE_PRINT_ELEMENT_VALUE = 0x7fffffff;
54
55 //-----------------------------------------------------------------------------
56 // Constructor / Destructor
57
58 /**
59  * \brief   constructor  
60  * @param   filename file to be opened for parsing
61  */
62 Document::Document( std::string const &filename ) : ElementSet(-1)
63 {
64    SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE); 
65    Filename = filename;
66    Initialise();
67
68    Fp = 0;
69    if ( !OpenFile() )
70    {
71       return;
72    }
73
74    Group0002Parsed = false;
75
76    gdcmVerboseMacro( "Starting parsing of file: " << Filename.c_str());
77   // Fp->seekg( 0,  std::ios::beg);
78    
79    Fp->seekg(0,  std::ios::end);
80    long lgt = Fp->tellg();
81            
82    Fp->seekg( 0,  std::ios::beg);
83    CheckSwap();
84    long beg = Fp->tellg();
85    lgt -= beg;
86    
87    ParseDES( this, beg, lgt, false); // le Load sera fait a la volee
88
89    Fp->seekg( 0,  std::ios::beg);
90    
91    // Load 'non string' values
92       
93    std::string PhotometricInterpretation = GetEntry(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       /// dictionnary. 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 dictionnary was
104       /// altered on PURPOUS 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       /// wether 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 = GetEntry(0x0008, 0x0010); // recognition code
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    = GetEntry(0x0028, 0x0010);
146          std::string columns = GetEntry(0x0028, 0x0011);
147          SetEntry(columns, 0x0028, 0x0010);
148          SetEntry(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() : ElementSet(-1)
158 {
159    Fp = 0;
160
161    SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
162    Initialise();
163    SwapCode = 1234;
164    Filetype = ExplicitVR;
165    Group0002Parsed = false;
166 }
167
168 /**
169  * \brief   Canonical destructor.
170  */
171 Document::~Document ()
172 {
173    RefPubDict = NULL;
174    RefShaDict = NULL;
175
176    delete RLEInfo;
177    delete JPEGInfo;
178 }
179
180 //-----------------------------------------------------------------------------
181 // Print
182
183 /**
184   * \brief   Prints The Dict Entries of THE public Dicom Dictionary
185   * @return
186   */  
187 void Document::PrintPubDict(std::ostream &os)
188 {
189    RefPubDict->SetPrintLevel(PrintLevel);
190    RefPubDict->Print(os);
191 }
192
193 /**
194   * \brief   Prints The Dict Entries of THE shadow Dicom Dictionary
195   * @return
196   */
197 void Document::PrintShaDict(std::ostream &os)
198 {
199    RefShaDict->SetPrintLevel(PrintLevel);
200    RefShaDict->Print(os);
201 }
202
203 //-----------------------------------------------------------------------------
204 // Public
205 /**
206  * \brief   Get the public dictionary used
207  */
208 Dict *Document::GetPubDict()
209 {
210    return RefPubDict;
211 }
212
213 /**
214  * \brief   Get the shadow dictionary used
215  */
216 Dict *Document::GetShaDict()
217 {
218    return RefShaDict;
219 }
220
221 /**
222  * \brief   Set the shadow dictionary used
223  * @param   dict dictionary to use in shadow
224  */
225 bool Document::SetShaDict(Dict *dict)
226 {
227    RefShaDict = dict;
228    return !RefShaDict;
229 }
230
231 /**
232  * \brief   Set the shadow dictionary used
233  * @param   dictName name of the dictionary to use in shadow
234  */
235 bool Document::SetShaDict(DictKey const &dictName)
236 {
237    RefShaDict = Global::GetDicts()->GetDict(dictName);
238    return !RefShaDict;
239 }
240
241 /**
242  * \brief  This predicate, based on hopefully reasonable heuristics,
243  *         decides whether or not the current Document was properly parsed
244  *         and contains the mandatory information for being considered as
245  *         a well formed and usable Dicom/Acr File.
246  * @return true when Document is the one of a reasonable Dicom/Acr file,
247  *         false otherwise. 
248  */
249 bool Document::IsReadable()
250 {
251    if( Filetype == Unknown)
252    {
253       gdcmVerboseMacro( "Wrong filetype");
254       return false;
255    }
256
257    if( TagHT.empty() )
258    {
259       gdcmVerboseMacro( "No tags in internal hash table.");
260       return false;
261    }
262
263    return true;
264 }
265
266 /**
267  * \brief   Accessor to the Transfer Syntax (when present) of the
268  *          current document (it internally handles reading the
269  *          value from disk when only parsing occured).
270  * @return  The encountered Transfer Syntax of the current document.
271  */
272 std::string Document::GetTransferSyntax()
273 {
274    DocEntry *entry = GetDocEntry(0x0002, 0x0010);
275    if ( !entry )
276    {
277       return GDCM_UNKNOWN;
278    }
279
280    // The entry might be present but not loaded (parsing and loading
281    // happen at different stages): try loading and proceed with check...
282    LoadDocEntrySafe(entry);
283    if (ValEntry *valEntry = dynamic_cast< ValEntry* >(entry) )
284    {
285       std::string transfer = valEntry->GetValue();
286       // The actual transfer (as read from disk) might be padded. We
287       // first need to remove the potential padding. We can make the
288       // weak assumption that padding was not executed with digits...
289       if  ( transfer.length() == 0 )
290       {
291          // for brain damaged headers
292          return GDCM_UNKNOWN;
293       }
294       while ( !isdigit((unsigned char)transfer[transfer.length()-1]) )
295       {
296          transfer.erase(transfer.length()-1, 1);
297       }
298       return transfer;
299    }
300    return GDCM_UNKNOWN;
301 }
302
303 /**
304  * \brief   Predicate for dicom version 3 file.
305  * @return  True when the file is a dicom version 3.
306  */
307 bool Document::IsDicomV3()
308 {
309    // Checking if Transfer Syntax exists is enough
310    // Anyway, it's to late check if the 'Preamble' was found ...
311    // And ... would it be a rich idea to check ?
312    // (some 'no Preamble' DICOM images exist !)
313    return GetDocEntry(0x0002, 0x0010) != NULL;
314 }
315
316 /**
317  * \brief  returns the File Type 
318  *         (ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown)
319  * @return the FileType code
320  */
321 FileType Document::GetFileType()
322 {
323    return Filetype;
324 }
325
326 /**
327  * \brief  Tries to open the file \ref Document::Filename and
328  *         checks the preamble when existing.
329  * @return The FILE pointer on success. 
330  */
331 std::ifstream *Document::OpenFile()
332 {
333
334    HasDCMPreamble = false;
335    if (Filename.length() == 0) 
336    {
337       return 0;
338    }
339
340    if(Fp)
341    {
342       gdcmVerboseMacro( "File already open: " << Filename.c_str());
343    }
344
345    Fp = new std::ifstream(Filename.c_str(), std::ios::in | std::ios::binary);
346    if( ! *Fp )
347    {
348       gdcmVerboseMacro( "Cannot open file: " << Filename.c_str());
349       delete Fp;
350       Fp = 0;
351       return 0;
352    }
353  
354    uint16_t zero;
355    Fp->read((char*)&zero, (size_t)2);
356    if( Fp->eof() )
357    {
358       CloseFile();
359       return 0;
360    }
361  
362    //ACR -- or DICOM with no Preamble; may start with a Shadow Group --
363    if( 
364        zero == 0x0001 || zero == 0x0100 || zero == 0x0002 || zero == 0x0200 ||
365        zero == 0x0003 || zero == 0x0300 || zero == 0x0004 || zero == 0x0400 ||
366        zero == 0x0005 || zero == 0x0500 || zero == 0x0006 || zero == 0x0600 ||
367        zero == 0x0007 || zero == 0x0700 || zero == 0x0008 || zero == 0x0800 )
368    {
369       std::string msg 
370          = Util::Format("ACR/DICOM with no preamble: (%04x)\n", zero);
371       gdcmVerboseMacro( msg.c_str() );
372       return Fp;
373    }
374  
375    //DICOM
376    Fp->seekg(126L, std::ios::cur);
377    char dicm[4];
378    Fp->read(dicm,  (size_t)4);
379    if( Fp->eof() )
380    {
381       CloseFile();
382       return 0;
383    }
384    if( memcmp(dicm, "DICM", 4) == 0 )
385    {
386       HasDCMPreamble = true;
387       return Fp;
388    }
389  
390    CloseFile();
391    gdcmVerboseMacro( "Not DICOM/ACR (missing preamble)" << Filename.c_str());
392  
393    return 0;
394 }
395
396 /**
397  * \brief closes the file  
398  * @return  TRUE if the close was successfull 
399  */
400 bool Document::CloseFile()
401 {
402    if( Fp )
403    {
404       Fp->close();
405       delete Fp;
406       Fp = 0;
407    }
408
409    return true; //FIXME how do we detect a non-close ifstream ?
410 }
411
412 /**
413  * \brief Writes in a file all the Header Entries (Dicom Elements) 
414  * @param fp file pointer on an already open file
415  * @param filetype Type of the File to be written 
416  *          (ACR-NEMA, ExplicitVR, ImplicitVR)
417  * \return Always true.
418  */
419 void Document::WriteContent(std::ofstream *fp, FileType filetype)
420 {
421    /// \todo move the following lines (and a lot of others, to be written)
422    /// to a future function CheckAndCorrectHeader  
423    /// (necessary if user wants to write a DICOM V3 file
424    /// starting from an  ACR-NEMA (V2)  Header
425
426    if ( filetype == ImplicitVR || filetype == ExplicitVR )
427    {
428       // writing Dicom File Preamble
429       char filePreamble[128];
430       memset(filePreamble, 0, 128);
431       fp->write(filePreamble, 128);
432       fp->write("DICM", 4);
433    }
434
435 /**
436  * \todo rewrite later, if really usefull
437  *       - 'Group Length' element is optional in DICOM
438  *       - but un-updated odd groups lengthes can causes pb
439  *         (xmedcon breaker)
440  *
441  * if ( (filetype == ImplicitVR) || (filetype == ExplicitVR) )
442  *    UpdateGroupLength(false,filetype);
443  * if ( filetype == ACR)
444  *    UpdateGroupLength(true,ACR);
445  */
446  
447    ElementSet::WriteContent(fp, filetype); // This one is recursive
448 }
449
450 /**
451  * \brief   Modifies the value of a given Doc Entry (Dicom Element)
452  *          when it exists. Create it with the given value when unexistant.
453  * @param   value (string) Value to be set
454  * @param   group   Group number of the Entry 
455  * @param   elem  Element number of the Entry
456  * @param   vr  V(alue) R(epresentation) of the Entry -if private Entry-
457  * \return  pointer to the modified/created Header Entry (NULL when creation
458  *          failed).
459  */ 
460 ValEntry *Document::ReplaceOrCreate(std::string const &value, 
461                                     uint16_t group, 
462                                     uint16_t elem,
463                                     TagName const &vr )
464 {
465    ValEntry *valEntry = 0;
466    DocEntry *currentEntry = GetDocEntry( group, elem);
467    
468    if (currentEntry)
469    {
470       valEntry = dynamic_cast< ValEntry* >(currentEntry);
471
472       // Verify the VR
473       if( valEntry )
474          if( valEntry->GetVR()!=vr )
475             valEntry=NULL;
476
477       // if currentEntry doesn't correspond to the requested valEntry
478       if( !valEntry)
479       {
480          if (!RemoveEntry(currentEntry))
481          {
482             gdcmVerboseMacro( "Removal of previous DocEntry failed.");
483
484             return NULL;
485          }
486       }
487    }
488
489    // Create a new valEntry if necessary
490    if (!valEntry)
491    {
492       valEntry = NewValEntry(group, elem, vr);
493
494       if ( !AddEntry(valEntry))
495       {
496          gdcmVerboseMacro("AddEntry failed although this is a creation.");
497
498          delete valEntry;
499          return NULL;
500       }
501    }
502
503    // Set the binEntry value
504    SetEntry(value, valEntry);
505    return valEntry;
506 }   
507
508 /*
509  * \brief   Modifies the value of a given Header Entry (Dicom Element)
510  *          when it exists. Create it with the given value when unexistant.
511  *          A copy of the binArea is made to be kept in the Document.
512  * @param   binArea (binary) value to be set
513  * @param   Group   Group number of the Entry 
514  * @param   Elem  Element number of the Entry
515  * @param   vr  V(alue) R(epresentation) of the Entry -if private Entry-
516  * \return  pointer to the modified/created Header Entry (NULL when creation
517  *          failed).
518  */
519 BinEntry *Document::ReplaceOrCreate(uint8_t *binArea,
520                                     int lgth, 
521                                     uint16_t group, 
522                                     uint16_t elem,
523                                     TagName const &vr )
524 {
525    BinEntry *binEntry = 0;
526    DocEntry *currentEntry = GetDocEntry( group, elem);
527
528    // Verify the currentEntry
529    if (currentEntry)
530    {
531       binEntry = dynamic_cast< BinEntry* >(currentEntry);
532
533       // Verify the VR
534       if( binEntry )
535          if( binEntry->GetVR()!=vr )
536             binEntry=NULL;
537
538       // if currentEntry doesn't correspond to the requested valEntry
539       if( !binEntry)
540       {
541          if (!RemoveEntry(currentEntry))
542          {
543             gdcmVerboseMacro( "Removal of previous DocEntry failed.");
544
545             return NULL;
546          }
547       }
548    }
549
550    // Create a new binEntry if necessary
551    if (!binEntry)
552    {
553       binEntry = NewBinEntry(group, elem, vr);
554
555       if ( !AddEntry(binEntry))
556       {
557          gdcmVerboseMacro( "AddEntry failed allthough this is a creation.");
558
559          delete binEntry;
560          return NULL;
561       }
562    }
563
564    // Set the binEntry value
565    uint8_t *tmpArea;
566    if (lgth>0 && binArea)
567    {
568       tmpArea = new uint8_t[lgth];
569       memcpy(tmpArea,binArea,lgth);
570    }
571    else
572    {
573       tmpArea = 0;
574    }
575    if (!SetEntry(tmpArea,lgth,binEntry))
576    {
577       if (tmpArea)
578       {
579          delete[] tmpArea;
580       }
581    }
582
583    return binEntry;
584 }  
585
586 /*
587  * \brief   Modifies the value of a given Header Entry (Dicom Element)
588  *          when it exists. Create it when unexistant.
589  * @param   Group   Group number of the Entry 
590  * @param   Elem  Element number of the Entry
591  * \return  pointer to the modified/created SeqEntry (NULL when creation
592  *          failed).
593  */
594 SeqEntry *Document::ReplaceOrCreate( uint16_t group, uint16_t elem)
595 {
596    SeqEntry *seqEntry = 0;
597    DocEntry *currentEntry = GetDocEntry( group, elem);
598
599    // Verify the currentEntry
600    if (currentEntry)
601    {
602       seqEntry = dynamic_cast< SeqEntry* >(currentEntry);
603
604       // Verify the VR
605       if( seqEntry )
606          if( seqEntry->GetVR()!="SQ" )
607             seqEntry=NULL;
608
609       // if currentEntry doesn't correspond to the requested valEntry
610       if( !seqEntry)
611       {
612          if (!RemoveEntry(currentEntry))
613          {
614             gdcmVerboseMacro( "Removal of previous DocEntry failed.");
615
616             return NULL;
617          }
618       }
619    }
620
621    // Create a new seqEntry if necessary
622    if (!seqEntry)
623    {
624       seqEntry = NewSeqEntry(group, elem);
625
626       if ( !AddEntry(seqEntry))
627       {
628          gdcmVerboseMacro( "AddEntry failed allthough this is a creation.");
629
630          delete seqEntry;
631          return NULL;
632       }
633    }
634
635    return seqEntry;
636
637  
638 /**
639  * \brief Set a new value if the invoked element exists
640  *        Seems to be useless !!!
641  * @param value new element value
642  * @param group  group number of the Entry 
643  * @param elem element number of the Entry
644  * \return  boolean 
645  */
646 bool Document::ReplaceIfExist(std::string const &value, 
647                               uint16_t group, uint16_t elem ) 
648 {
649    SetEntry(value, group, elem);
650
651    return true;
652
653
654 //-----------------------------------------------------------------------------
655 // Protected
656
657 /**
658  * \brief   Checks if a given Dicom Element exists within the H table
659  * @param   group      Group number of the searched Dicom Element 
660  * @param   element  Element number of the searched Dicom Element 
661  * @return true is found
662  */
663 bool Document::CheckIfEntryExist(uint16_t group, uint16_t element )
664 {
665    const std::string &key = DictEntry::TranslateToKey(group, element );
666    return TagHT.count(key) != 0;
667 }
668
669
670 /**
671  * \brief   Searches within Header Entries (Dicom Elements) parsed with 
672  *          the public and private dictionaries 
673  *          for the element value representation of a given tag.
674  * @param   group Group number of the searched tag.
675  * @param   element Element number of the searched tag.
676  * @return  Corresponding element value representation when it exists,
677  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
678  */
679 std::string Document::GetEntry(uint16_t group, uint16_t element)
680 {
681    TagKey key = DictEntry::TranslateToKey(group, element);
682    if ( !TagHT.count(key))
683    {
684       return GDCM_UNFOUND;
685    }
686
687    return ((ValEntry *)TagHT.find(key)->second)->GetValue();
688 }
689
690 /**
691  * \brief   Searches within Header Entries (Dicom Elements) parsed with 
692  *          the public and private dictionaries 
693  *          for the element value representation of a given tag..
694  *
695  *          Obtaining the VR (Value Representation) might be needed by caller
696  *          to convert the string typed content to caller's native type 
697  *          (think of C++ vs Python). The VR is actually of a higher level
698  *          of semantics than just the native C++ type.
699  * @param   group     Group number of the searched tag.
700  * @param   element Element number of the searched tag.
701  * @return  Corresponding element value representation when it exists,
702  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
703  */
704 std::string Document::GetEntryVR(uint16_t group, uint16_t element)
705 {
706    DocEntry *elem = GetDocEntry(group, element);
707    if ( !elem )
708    {
709       return GDCM_UNFOUND;
710    }
711    return elem->GetVR();
712 }
713
714 /**
715  * \brief   Searches within Header Entries (Dicom Elements) parsed with 
716  *          the public and private dictionaries 
717  *          for the value length of a given tag..
718  * @param   group     Group number of the searched tag.
719  * @param   element Element number of the searched tag.
720  * @return  Corresponding element length; -2 if not found
721  */
722 int Document::GetEntryLength(uint16_t group, uint16_t element)
723 {
724    DocEntry *elem =  GetDocEntry(group, element);
725    if ( !elem )
726    {
727       return -2;  //magic number
728    }
729    return elem->GetLength();
730 }
731
732 /**
733  * \brief   Accesses an existing DocEntry (i.e. a Dicom Element)
734  *          through it's (group, element) and modifies it's content with
735  *          the given value.
736  * @param   content new value (string) to substitute with
737  * @param   group     group number of the Dicom Element to modify
738  * @param   element element number of the Dicom Element to modify
739  */
740 bool Document::SetEntry(std::string const& content, 
741                         uint16_t group, uint16_t element) 
742 {
743    ValEntry *entry = GetValEntry(group, element);
744    if (!entry )
745    {
746       gdcmVerboseMacro( "No corresponding ValEntry (try promotion first).");
747       return false;
748    }
749    return SetEntry(content,entry);
750
751
752 /**
753  * \brief   Accesses an existing DocEntry (i.e. a Dicom Element)
754  *          through it's (group, element) and modifies it's content with
755  *          the given value.
756  * @param   content new value (void*  -> uint8_t*) to substitute with
757  * @param   lgth new value length
758  * @param   group     group number of the Dicom Element to modify
759  * @param   element element number of the Dicom Element to modify
760  */
761 bool Document::SetEntry(uint8_t*content, int lgth, 
762                         uint16_t group, uint16_t element) 
763 {
764    BinEntry *entry = GetBinEntry(group, element);
765    if (!entry )
766    {
767       gdcmVerboseMacro( "No corresponding ValEntry (try promotion first).");
768       return false;
769    }
770
771    return SetEntry(content,lgth,entry);
772
773
774 /**
775  * \brief   Accesses an existing DocEntry (i.e. a Dicom Element)
776  *          and modifies it's content with the given value.
777  * @param  content new value (string) to substitute with
778  * @param  entry Entry to be modified
779  */
780 bool Document::SetEntry(std::string const &content,ValEntry *entry)
781 {
782    if(entry)
783    {
784       entry->SetValue(content);
785       return true;
786    }
787    return false;
788 }
789
790 /**
791  * \brief   Accesses an existing BinEntry (i.e. a Dicom Element)
792  *          and modifies it's content with the given value.
793  * @param   content new value (void*  -> uint8_t*) to substitute with
794  * @param  entry Entry to be modified 
795  * @param   lgth new value length
796  */
797 bool Document::SetEntry(uint8_t *content, int lgth, BinEntry *entry)
798 {
799    if(entry)
800    {
801       // Hope Binary field length is *never* wrong    
802       /*if(lgth%2) // Non even length are padded with a space (020H).
803       {  
804          lgth++;
805          //content = content + '\0'; // fing a trick to enlarge a binary field?
806       }*/
807       
808       entry->SetBinArea(content);  
809       entry->SetLength(lgth);
810       entry->SetValue(GDCM_BINLOADED);
811       return true;
812    }
813    return false;
814 }
815
816 /**
817  * \brief   Gets (from Header) a 'non string' element value 
818  *          (LoadElementValues has already be executed)  
819  * @param group   group number of the Entry 
820  * @param elem  element number of the Entry
821  * @return Pointer to the 'non string' area
822  */
823 void *Document::GetEntryBinArea(uint16_t group, uint16_t elem) 
824 {
825    DocEntry *entry = GetDocEntry(group, elem);
826    if (!entry) 
827    {
828       gdcmVerboseMacro( "No entry");
829       return 0;
830    }
831    if ( BinEntry *binEntry = dynamic_cast<BinEntry*>(entry) )
832    {
833       return binEntry->GetBinArea();
834    }
835
836    return 0;
837 }
838
839 /**
840  * \brief         Loads (from disk) the element content 
841  *                when a string is not suitable
842  * @param group   group number of the Entry 
843  * @param elem  element number of the Entry
844  */
845 void Document::LoadEntryBinArea(uint16_t group, uint16_t elem)
846 {
847    // Search the corresponding DocEntry
848    DocEntry *docElement = GetDocEntry(group, elem);
849    if ( !docElement )
850       return;
851
852    BinEntry *binElement = dynamic_cast<BinEntry *>(docElement);
853    if( !binElement )
854       return;
855
856    LoadEntryBinArea(binElement);
857 }
858
859 /**
860  * \brief         Loads (from disk) the element content 
861  *                when a string is not suitable
862  * @param element  Entry whose binArea is going to be loaded
863  */
864 void Document::LoadEntryBinArea(BinEntry *element) 
865 {
866    if(element->GetBinArea())
867       return;
868
869    bool openFile = !Fp;
870    if(openFile)
871       OpenFile();
872
873    size_t o =(size_t)element->GetOffset();
874    Fp->seekg(o, std::ios::beg);
875
876    size_t l = element->GetLength();
877    uint8_t *a = new uint8_t[l];
878    if( !a )
879    {
880       gdcmVerboseMacro( "Cannot allocate a");
881       return;
882    }
883
884    /// \todo check the result 
885    Fp->read((char*)a, l);
886    if( Fp->fail() || Fp->eof())
887    {
888       delete[] a;
889       return;
890    }
891
892    element->SetBinArea(a);
893
894    if(openFile)
895       CloseFile();
896 }
897
898 /**
899  * \brief   Sets a 'non string' value to a given Dicom Element
900  * @param   area area containing the 'non string' value
901  * @param   group     Group number of the searched Dicom Element 
902  * @param   element Element number of the searched Dicom Element 
903  * @return  
904  */
905 /*bool Document::SetEntryBinArea(uint8_t *area,
906                                  uint16_t group, uint16_t element) 
907 {
908    DocEntry *currentEntry = GetDocEntry(group, element);
909    if ( !currentEntry )
910    {
911       return false;
912    }
913
914    if ( BinEntry *binEntry = dynamic_cast<BinEntry*>(currentEntry) )
915    {
916       binEntry->SetBinArea( area );
917       return true;
918    }
919
920    return false;
921 }*/
922
923 /**
924  * \brief  retrieves a Dicom Element (the first one) using (group, element)
925  * \warning (group, element) IS NOT an identifier inside the Dicom Header
926  *           if you think it's NOT UNIQUE, check the count number
927  *           and use iterators to retrieve ALL the Dicoms Elements within
928  *           a given couple (group, element)
929  * @param   group Group number of the searched Dicom Element 
930  * @param   element Element number of the searched Dicom Element 
931  * @return  
932  */
933 DocEntry *Document::GetDocEntry(uint16_t group, uint16_t element) 
934 {
935    TagKey key = DictEntry::TranslateToKey(group, element);
936    if ( !TagHT.count(key))
937    {
938       return NULL;
939    }
940    return TagHT.find(key)->second;
941 }
942
943 /**
944  * \brief  Same as \ref Document::GetDocEntry except it only
945  *         returns a result when the corresponding entry is of type
946  *         ValEntry.
947  * @return When present, the corresponding ValEntry. 
948  */
949 ValEntry *Document::GetValEntry(uint16_t group, uint16_t element)
950 {
951    DocEntry *currentEntry = GetDocEntry(group, element);
952    if ( !currentEntry )
953    {
954       return 0;
955    }
956    if ( ValEntry *entry = dynamic_cast<ValEntry*>(currentEntry) )
957    {
958       return entry;
959    }
960    gdcmVerboseMacro( "Unfound ValEntry.");
961
962    return 0;
963 }
964
965 /**
966  * \brief  Same as \ref Document::GetDocEntry except it only
967  *         returns a result when the corresponding entry is of type
968  *         BinEntry.
969  * @return When present, the corresponding BinEntry. 
970  */
971 BinEntry *Document::GetBinEntry(uint16_t group, uint16_t element)
972 {
973    DocEntry *currentEntry = GetDocEntry(group, element);
974    if ( !currentEntry )
975    {
976       return 0;
977    }
978    if ( BinEntry *entry = dynamic_cast<BinEntry*>(currentEntry) )
979    {
980       return entry;
981    }
982    gdcmVerboseMacro( "Unfound BinEntry.");
983
984    return 0;
985 }
986
987 /**
988  * \brief         Loads the element while preserving the current
989  *               underlying file position indicator as opposed to
990  *                to LoadDocEntry that modifies it.
991  * @param entry   Header Entry whose value shall be loaded. 
992  * @return  
993  */
994 void Document::LoadDocEntrySafe(DocEntry *entry)
995 {
996    if(Fp)
997    {
998       long PositionOnEntry = Fp->tellg();
999       LoadDocEntry(entry);
1000       Fp->seekg(PositionOnEntry, std::ios::beg);
1001    }
1002 }
1003
1004 /**
1005  * \brief   Swaps back the bytes of 4-byte long integer accordingly to
1006  *          processor order.
1007  * @return  The properly swaped 32 bits integer.
1008  */
1009 uint32_t Document::SwapLong(uint32_t a)
1010 {
1011    switch (SwapCode)
1012    {
1013       case 1234 :
1014          break;
1015       case 4321 :
1016          a=( ((a<<24) & 0xff000000) | ((a<<8)  & 0x00ff0000) | 
1017              ((a>>8)  & 0x0000ff00) | ((a>>24) & 0x000000ff) );
1018          break;
1019    
1020       case 3412 :
1021          a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
1022          break;
1023    
1024       case 2143 :
1025          a=( ((a<< 8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff)  );
1026          break;
1027       default :
1028          gdcmErrorMacro( "Unset swap code:" << SwapCode );
1029          a = 0;
1030    }
1031    return a;
1032
1033
1034 /**
1035  * \brief   Unswaps back the bytes of 4-byte long integer accordingly to
1036  *          processor order.
1037  * @return  The properly unswaped 32 bits integer.
1038  */
1039 uint32_t Document::UnswapLong(uint32_t a)
1040 {
1041    return SwapLong(a);
1042 }
1043
1044 /**
1045  * \brief   Swaps the bytes so they agree with the processor order
1046  * @return  The properly swaped 16 bits integer.
1047  */
1048 uint16_t Document::SwapShort(uint16_t a)
1049 {
1050    if ( SwapCode == 4321 || SwapCode == 2143 )
1051    {
1052       a = ((( a << 8 ) & 0x0ff00 ) | (( a >> 8 ) & 0x00ff ) );
1053    }
1054    return a;
1055 }
1056
1057 /**
1058  * \brief   Unswaps the bytes so they agree with the processor order
1059  * @return  The properly unswaped 16 bits integer.
1060  */
1061 uint16_t Document::UnswapShort(uint16_t a)
1062 {
1063    return SwapShort(a);
1064 }
1065
1066 //-----------------------------------------------------------------------------
1067 // Private
1068
1069 /**
1070  * \brief   Parses a DocEntrySet (Zero-level DocEntries or SQ Item DocEntries)
1071  * @return  length of the parsed set. 
1072  */ 
1073 void Document::ParseDES(DocEntrySet *set, long offset, 
1074                         long l_max, bool delim_mode)
1075 {
1076    DocEntry *newDocEntry = 0;
1077    ValEntry *newValEntry;
1078    BinEntry *newBinEntry;
1079    SeqEntry *newSeqEntry;
1080    VRKey vr;
1081    bool used=false;
1082
1083    while (true)
1084    {
1085       if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
1086       {
1087          break;
1088       }
1089
1090       used=true;
1091       newDocEntry = ReadNextDocEntry( );
1092       if ( !newDocEntry )
1093       {
1094          break;
1095       }
1096
1097       vr = newDocEntry->GetVR();
1098       newValEntry = dynamic_cast<ValEntry*>(newDocEntry);
1099       newBinEntry = dynamic_cast<BinEntry*>(newDocEntry);
1100       newSeqEntry = dynamic_cast<SeqEntry*>(newDocEntry);
1101
1102       if ( newValEntry || newBinEntry )
1103       {
1104          if ( newBinEntry )
1105          {
1106             if ( ! Global::GetVR()->IsVROfBinaryRepresentable(vr) )
1107             { 
1108                 ////// Neither ValEntry NOR BinEntry: should mean UNKOWN VR
1109                 gdcmVerboseMacro( "Neither Valentry, nor BinEntry." 
1110                                   "Probably unknown VR.");
1111             }
1112
1113          //////////////////// BinEntry or UNKOWN VR:
1114             // When "this" is a Document the Key is simply of the
1115             // form ( group, elem )...
1116             if (Document *dummy = dynamic_cast< Document* > ( set ) )
1117             {
1118                (void)dummy;
1119                newBinEntry->SetKey( newBinEntry->GetKey() );
1120             }
1121             // but when "this" is a SQItem, we are inserting this new
1122             // valEntry in a sequence item, and the kay has the
1123             // generalized form (refer to \ref BaseTagKey):
1124             if (SQItem *parentSQItem = dynamic_cast< SQItem* > ( set ) )
1125             {
1126                newBinEntry->SetKey(  parentSQItem->GetBaseTagKey()
1127                                    + newBinEntry->GetKey() );
1128             }
1129
1130             LoadDocEntry( newBinEntry );
1131             if( !set->AddEntry( newBinEntry ) )
1132             {
1133               //Expect big troubles if here
1134               //delete newBinEntry;
1135               used=false;
1136             }
1137          }
1138          else
1139          {
1140          /////////////////////// ValEntry
1141             // When "set" is a Document, then we are at the top of the
1142             // hierarchy and the Key is simply of the form ( group, elem )...
1143             if (Document *dummy = dynamic_cast< Document* > ( set ) )
1144             {
1145                (void)dummy;
1146                newValEntry->SetKey( newValEntry->GetKey() );
1147             }
1148             // ...but when "set" is a SQItem, we are inserting this new
1149             // valEntry in a sequence item. Hence the key has the
1150             // generalized form (refer to \ref BaseTagKey):
1151             if (SQItem *parentSQItem = dynamic_cast< SQItem* > ( set ) )
1152             {
1153                newValEntry->SetKey(  parentSQItem->GetBaseTagKey()
1154                                    + newValEntry->GetKey() );
1155             }
1156              
1157             LoadDocEntry( newValEntry );
1158             bool delimitor=newValEntry->IsItemDelimitor();
1159             if( !set->AddEntry( newValEntry ) )
1160             {
1161               // If here expect big troubles
1162               //delete newValEntry; //otherwise mem leak
1163               used=false;
1164             }
1165
1166             if (delimitor)
1167             {
1168                if(!used)
1169                   delete newDocEntry;
1170                break;
1171             }
1172             if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
1173             {
1174                if(!used)
1175                   delete newDocEntry;
1176                break;
1177             }
1178          }
1179
1180          if (    ( newDocEntry->GetGroup()   == 0x7fe0 )
1181               && ( newDocEntry->GetElement() == 0x0010 ) )
1182          {
1183              std::string ts = GetTransferSyntax();
1184              if ( Global::GetTS()->IsRLELossless(ts) ) 
1185              {
1186                 long positionOnEntry = Fp->tellg();
1187                 Fp->seekg( newDocEntry->GetOffset(), std::ios::beg );
1188                 ComputeRLEInfo();
1189                 Fp->seekg( positionOnEntry, std::ios::beg );
1190              }
1191              else if ( Global::GetTS()->IsJPEG(ts) )
1192              {
1193                 long positionOnEntry = Fp->tellg();
1194                 Fp->seekg( newDocEntry->GetOffset(), std::ios::beg );
1195                 ComputeJPEGFragmentInfo();
1196                 Fp->seekg( positionOnEntry, std::ios::beg );
1197              }
1198          }
1199
1200          // Just to make sure we are at the beginning of next entry.
1201          SkipToNextDocEntry(newDocEntry);
1202       }
1203       else
1204       {
1205          // VR = "SQ"
1206          unsigned long l = newDocEntry->GetReadLength();            
1207          if ( l != 0 ) // don't mess the delim_mode for zero-length sequence
1208          {
1209             if ( l == 0xffffffff )
1210             {
1211               delim_mode = true;
1212             }
1213             else
1214             {
1215               delim_mode = false;
1216             }
1217          }
1218          // no other way to create it ...
1219          newSeqEntry->SetDelimitorMode( delim_mode );
1220
1221          // At the top of the hierarchy, stands a Document. When "set"
1222          // is a Document, then we are building the first depth level.
1223          // Hence the SeqEntry we are building simply has a depth
1224          // level of one:
1225          if (Document *dummy = dynamic_cast< Document* > ( set ) )
1226          {
1227             (void)dummy;
1228             newSeqEntry->SetDepthLevel( 1 );
1229             newSeqEntry->SetKey( newSeqEntry->GetKey() );
1230          }
1231          // But when "set" is allready a SQItem, we are building a nested
1232          // sequence, and hence the depth level of the new SeqEntry
1233          // we are building, is one level deeper:
1234          if (SQItem *parentSQItem = dynamic_cast< SQItem* > ( set ) )
1235          {
1236             newSeqEntry->SetDepthLevel( parentSQItem->GetDepthLevel() + 1 );
1237             newSeqEntry->SetKey(  parentSQItem->GetBaseTagKey()
1238                                 + newSeqEntry->GetKey() );
1239          }
1240
1241          if ( l != 0 )
1242          {  // Don't try to parse zero-length sequences
1243             ParseSQ( newSeqEntry, 
1244                      newDocEntry->GetOffset(),
1245                      l, delim_mode);
1246          }
1247          set->AddEntry( newSeqEntry );
1248          if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
1249          {
1250             break;
1251          }
1252       }
1253
1254       if(!used)
1255          delete newDocEntry;
1256    }
1257 }
1258
1259 /**
1260  * \brief   Parses a Sequence ( SeqEntry after SeqEntry)
1261  * @return  parsed length for this level
1262  */ 
1263 void Document::ParseSQ( SeqEntry *seqEntry,
1264                         long offset, long l_max, bool delim_mode)
1265 {
1266    int SQItemNumber = 0;
1267    bool dlm_mod;
1268
1269    while (true)
1270    {
1271       DocEntry *newDocEntry = ReadNextDocEntry();   
1272       if ( !newDocEntry )
1273       {
1274          // FIXME Should warn user
1275          break;
1276       }
1277       if( delim_mode )
1278       {
1279          if ( newDocEntry->IsSequenceDelimitor() )
1280          {
1281             seqEntry->SetSequenceDelimitationItem( newDocEntry ); 
1282             break;
1283          }
1284       }
1285       if ( !delim_mode && ((long)(Fp->tellg())-offset) >= l_max)
1286       {
1287          delete newDocEntry;
1288          break;
1289       }
1290
1291       SQItem *itemSQ = new SQItem( seqEntry->GetDepthLevel() );
1292       std::ostringstream newBase;
1293       newBase << seqEntry->GetKey()
1294               << "/"
1295               << SQItemNumber
1296               << "#";
1297       itemSQ->SetBaseTagKey( newBase.str() );
1298       unsigned int l = newDocEntry->GetReadLength();
1299       
1300       if ( l == 0xffffffff )
1301       {
1302          dlm_mod = true;
1303       }
1304       else
1305       {
1306          dlm_mod = false;
1307       }
1308    
1309       ParseDES(itemSQ, newDocEntry->GetOffset(), l, dlm_mod);
1310       delete newDocEntry;
1311       
1312       seqEntry->AddEntry( itemSQ, SQItemNumber ); 
1313       SQItemNumber++;
1314       if ( !delim_mode && ((long)(Fp->tellg())-offset ) >= l_max )
1315       {
1316          break;
1317       }
1318    }
1319 }
1320
1321 /**
1322  * \brief         Loads the element content if its length doesn't exceed
1323  *                the value specified with Document::SetMaxSizeLoadEntry()
1324  * @param         entry Header Entry (Dicom Element) to be dealt with
1325  */
1326 void Document::LoadDocEntry(DocEntry *entry)
1327 {
1328    uint16_t group  = entry->GetGroup();
1329    std::string  vr = entry->GetVR();
1330    uint32_t length = entry->GetLength();
1331
1332    Fp->seekg((long)entry->GetOffset(), std::ios::beg);
1333
1334    // A SeQuence "contains" a set of Elements.  
1335    //          (fffe e000) tells us an Element is beginning
1336    //          (fffe e00d) tells us an Element just ended
1337    //          (fffe e0dd) tells us the current SeQuence just ended
1338    if( group == 0xfffe )
1339    {
1340       // NO more value field for SQ !
1341       return;
1342    }
1343
1344    // When the length is zero things are easy:
1345    if ( length == 0 )
1346    {
1347       ((ValEntry *)entry)->SetValue("");
1348       return;
1349    }
1350
1351    // The elements whose length is bigger than the specified upper bound
1352    // are not loaded. Instead we leave a short notice of the offset of
1353    // the element content and it's length.
1354
1355    std::ostringstream s;
1356    if (length > MaxSizeLoadEntry)
1357    {
1358       if (BinEntry *binEntryPtr = dynamic_cast< BinEntry* >(entry) )
1359       {  
1360          //s << "gdcm::NotLoaded (BinEntry)";
1361          s << GDCM_NOTLOADED;
1362          s << " Address:" << (long)entry->GetOffset();
1363          s << " Length:"  << entry->GetLength();
1364          s << " x(" << std::hex << entry->GetLength() << ")";
1365          binEntryPtr->SetValue(s.str());
1366       }
1367       // Be carefull : a BinEntry IS_A ValEntry ... 
1368       else if (ValEntry *valEntryPtr = dynamic_cast< ValEntry* >(entry) )
1369       {
1370         // s << "gdcm::NotLoaded. (ValEntry)";
1371          s << GDCM_NOTLOADED;  
1372          s << " Address:" << (long)entry->GetOffset();
1373          s << " Length:"  << entry->GetLength();
1374          s << " x(" << std::hex << entry->GetLength() << ")";
1375          valEntryPtr->SetValue(s.str());
1376       }
1377       else
1378       {
1379          // fusible
1380          gdcmErrorMacro( "MaxSizeLoadEntry exceeded, neither a BinEntry "
1381                       << "nor a ValEntry ?! Should never print that !" );
1382       }
1383
1384       // to be sure we are at the end of the value ...
1385       Fp->seekg((long)entry->GetOffset()+(long)entry->GetLength(),
1386                 std::ios::beg);
1387       return;
1388    }
1389
1390    // When we find a BinEntry not very much can be done :
1391    if (BinEntry *binEntryPtr = dynamic_cast< BinEntry* >(entry) )
1392    {
1393       s << GDCM_BINLOADED;
1394       binEntryPtr->SetValue(s.str());
1395       LoadEntryBinArea(binEntryPtr); // last one, not to erase length !
1396       return;
1397    }
1398
1399    /// \todo Any compacter code suggested (?)
1400    if ( IsDocEntryAnInteger(entry) )
1401    {   
1402       uint32_t NewInt;
1403       int nbInt;
1404       // When short integer(s) are expected, read and convert the following 
1405       // n *two characters properly i.e. consider them as short integers as
1406       // opposed to strings.
1407       // Elements with Value Multiplicity > 1
1408       // contain a set of integers (not a single one)       
1409       if (vr == "US" || vr == "SS")
1410       {
1411          nbInt = length / 2;
1412          NewInt = ReadInt16();
1413          s << NewInt;
1414          if (nbInt > 1)
1415          {
1416             for (int i=1; i < nbInt; i++)
1417             {
1418                s << '\\';
1419                NewInt = ReadInt16();
1420                s << NewInt;
1421             }
1422          }
1423       }
1424       // See above comment on multiple integers (mutatis mutandis).
1425       else if (vr == "UL" || vr == "SL")
1426       {
1427          nbInt = length / 4;
1428          NewInt = ReadInt32();
1429          s << NewInt;
1430          if (nbInt > 1)
1431          {
1432             for (int i=1; i < nbInt; i++)
1433             {
1434                s << '\\';
1435                NewInt = ReadInt32();
1436                s << NewInt;
1437             }
1438          }
1439       }
1440 #ifdef GDCM_NO_ANSI_STRING_STREAM
1441       s << std::ends; // to avoid oddities on Solaris
1442 #endif //GDCM_NO_ANSI_STRING_STREAM
1443
1444       ((ValEntry *)entry)->SetValue(s.str());
1445       return;
1446    }
1447    
1448   // FIXME: We need an additional byte for storing \0 that is not on disk
1449    char *str = new char[length+1];
1450    Fp->read(str, (size_t)length);
1451    str[length] = '\0'; //this is only useful when length is odd
1452    // Special DicomString call to properly handle \0 and even length
1453    std::string newValue;
1454    if( length % 2 )
1455    {
1456       newValue = Util::DicomString(str, length+1);
1457       gdcmVerboseMacro("Warning: bad length: " << length <<
1458                        ",For string :" <<  newValue.c_str()); 
1459       // Since we change the length of string update it length
1460       //entry->SetReadLength(length+1);
1461    }
1462    else
1463    {
1464       newValue = Util::DicomString(str, length);
1465    }
1466    delete[] str;
1467
1468    if ( ValEntry *valEntry = dynamic_cast<ValEntry* >(entry) )
1469    {
1470       if ( Fp->fail() || Fp->eof())
1471       {
1472          gdcmVerboseMacro("Unread element value");
1473          valEntry->SetValue(GDCM_UNREAD);
1474          return;
1475       }
1476
1477       if( vr == "UI" )
1478       {
1479          // Because of correspondance with the VR dic
1480          valEntry->SetValue(newValue);
1481       }
1482       else
1483       {
1484          valEntry->SetValue(newValue);
1485       }
1486    }
1487    else
1488    {
1489       gdcmErrorMacro( "Should have a ValEntry, here !");
1490    }
1491 }
1492
1493
1494 /**
1495  * \brief  Find the value Length of the passed Header Entry
1496  * @param  entry Header Entry whose length of the value shall be loaded. 
1497  */
1498 void Document::FindDocEntryLength( DocEntry *entry )
1499    throw ( FormatError )
1500 {
1501    uint16_t element = entry->GetElement();
1502    std::string  vr  = entry->GetVR();
1503    uint16_t length16;       
1504    
1505    if ( Filetype == ExplicitVR && !entry->IsImplicitVR() ) 
1506    {
1507       if ( vr == "OB" || vr == "OW" || vr == "SQ" || vr == "UN" ) 
1508       {
1509          // The following reserved two bytes (see PS 3.5-2003, section
1510          // "7.1.2 Data element structure with explicit vr", p 27) must be
1511          // skipped before proceeding on reading the length on 4 bytes.
1512          Fp->seekg( 2L, std::ios::cur);
1513          uint32_t length32 = ReadInt32();
1514
1515          if ( (vr == "OB" || vr == "OW") && length32 == 0xffffffff ) 
1516          {
1517             uint32_t lengthOB;
1518             try 
1519             {
1520                lengthOB = FindDocEntryLengthOBOrOW();
1521             }
1522             catch ( FormatUnexpected )
1523             {
1524                // Computing the length failed (this happens with broken
1525                // files like gdcm-JPEG-LossLess3a.dcm). We still have a
1526                // chance to get the pixels by deciding the element goes
1527                // until the end of the file. Hence we artificially fix the
1528                // the length and proceed.
1529                long currentPosition = Fp->tellg();
1530                Fp->seekg(0L,std::ios::end);
1531
1532                long lengthUntilEOF = (long)(Fp->tellg())-currentPosition;
1533                Fp->seekg(currentPosition, std::ios::beg);
1534
1535                entry->SetReadLength(lengthUntilEOF);
1536                entry->SetLength(lengthUntilEOF);
1537                return;
1538             }
1539             entry->SetReadLength(lengthOB);
1540             entry->SetLength(lengthOB);
1541             return;
1542          }
1543          FixDocEntryFoundLength(entry, length32); 
1544          return;
1545       }
1546
1547       // Length is encoded on 2 bytes.
1548       length16 = ReadInt16();
1549       
1550       // We can tell the current file is encoded in big endian (like
1551       // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
1552       // and it's value is the one of the encoding of a big endian file.
1553       // In order to deal with such big endian encoded files, we have
1554       // (at least) two strategies:
1555       // * when we load the "Transfer Syntax" tag with value of big endian
1556       //   encoding, we raise the proper flags. Then we wait for the end
1557       //   of the META group (0x0002) among which is "Transfer Syntax",
1558       //   before switching the swap code to big endian. We have to postpone
1559       //   the switching of the swap code since the META group is fully encoded
1560       //   in little endian, and big endian coding only starts at the next
1561       //   group. The corresponding code can be hard to analyse and adds
1562       //   many additional unnecessary tests for regular tags.
1563       // * the second strategy consists in waiting for trouble, that shall
1564       //   appear when we find the first group with big endian encoding. This
1565       //   is easy to detect since the length of a "Group Length" tag (the
1566       //   ones with zero as element number) has to be of 4 (0x0004). When we
1567       //   encounter 1024 (0x0400) chances are the encoding changed and we
1568       //   found a group with big endian encoding.
1569       // We shall use this second strategy. In order to make sure that we
1570       // can interpret the presence of an apparently big endian encoded
1571       // length of a "Group Length" without committing a big mistake, we
1572       // add an additional check: we look in the already parsed elements
1573       // for the presence of a "Transfer Syntax" whose value has to be "big
1574       // endian encoding". When this is the case, chances are we have got our
1575       // hands on a big endian encoded file: we switch the swap code to
1576       // big endian and proceed...
1577       if ( element  == 0x0000 && length16 == 0x0400 ) 
1578       {
1579          std::string ts = GetTransferSyntax();
1580          if ( Global::GetTS()->GetSpecialTransferSyntax(ts) 
1581                 != TS::ExplicitVRBigEndian ) 
1582          {
1583             throw FormatError( "Document::FindDocEntryLength()",
1584                                " not explicit VR." );
1585             return;
1586          }
1587          length16 = 4;
1588          SwitchByteSwapCode();
1589
1590          // Restore the unproperly loaded values i.e. the group, the element
1591          // and the dictionary entry depending on them.
1592          uint16_t correctGroup = SwapShort( entry->GetGroup() );
1593          uint16_t correctElem  = SwapShort( entry->GetElement() );
1594          DictEntry *newTag = GetDictEntry( correctGroup, correctElem );
1595          if ( !newTag )
1596          {
1597             // This correct tag is not in the dictionary. Create a new one.
1598             newTag = NewVirtualDictEntry(correctGroup, correctElem);
1599          }
1600          // FIXME this can create a memory leaks on the old entry that be
1601          // left unreferenced.
1602          entry->SetDictEntry( newTag );
1603       }
1604        
1605       // Heuristic: well, some files are really ill-formed.
1606       if ( length16 == 0xffff) 
1607       {
1608          // 0xffff means that we deal with 'Unknown Length' Sequence  
1609          length16 = 0;
1610       }
1611       FixDocEntryFoundLength( entry, (uint32_t)length16 );
1612       return;
1613    }
1614    else
1615    {
1616       // Either implicit VR or a non DICOM conformal (see note below) explicit
1617       // VR that ommited the VR of (at least) this element. Farts happen.
1618       // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1619       // on Data elements "Implicit and Explicit VR Data Elements shall
1620       // not coexist in a Data Set and Data Sets nested within it".]
1621       // Length is on 4 bytes.
1622       
1623       FixDocEntryFoundLength( entry, ReadInt32() );
1624       return;
1625    }
1626 }
1627
1628 /**
1629  * \brief     Find the Value Representation of the current Dicom Element.
1630  * @return    Value Representation of the current Entry
1631  */
1632 std::string Document::FindDocEntryVR()
1633 {
1634    if ( Filetype != ExplicitVR )
1635       return GDCM_UNKNOWN;
1636
1637    long positionOnEntry = Fp->tellg();
1638    // Warning: we believe this is explicit VR (Value Representation) because
1639    // we used a heuristic that found "UL" in the first tag. Alas this
1640    // doesn't guarantee that all the tags will be in explicit VR. In some
1641    // cases (see e-film filtered files) one finds implicit VR tags mixed
1642    // within an explicit VR file. Hence we make sure the present tag
1643    // is in explicit VR and try to fix things if it happens not to be
1644    // the case.
1645
1646    char vr[3];
1647    Fp->read (vr, (size_t)2);
1648    vr[2] = 0;
1649
1650    if( !CheckDocEntryVR(vr) )
1651    {
1652       Fp->seekg(positionOnEntry, std::ios::beg);
1653       return GDCM_UNKNOWN;
1654    }
1655    return vr;
1656 }
1657
1658 /**
1659  * \brief     Check the correspondance between the VR of the header entry
1660  *            and the taken VR. If they are different, the header entry is 
1661  *            updated with the new VR.
1662  * @param     vr    Dicom Value Representation
1663  * @return    false if the VR is incorrect of if the VR isn't referenced
1664  *            otherwise, it returns true
1665 */
1666 bool Document::CheckDocEntryVR(VRKey vr)
1667 {
1668    // CLEANME searching the dicom_vr at each occurence is expensive.
1669    // PostPone this test in an optional integrity check at the end
1670    // of parsing or only in debug mode.
1671    if ( !Global::GetVR()->IsValidVR(vr) )
1672       return false;
1673
1674    return true; 
1675 }
1676
1677 /**
1678  * \brief   Get the transformed value of the header entry. The VR value 
1679  *          is used to define the transformation to operate on the value
1680  * \warning NOT end user intended method !
1681  * @param   entry entry to tranform
1682  * @return  Transformed entry value
1683  */
1684 std::string Document::GetDocEntryValue(DocEntry *entry)
1685 {
1686    if ( IsDocEntryAnInteger(entry) && entry->IsImplicitVR() )
1687    {
1688       std::string val = ((ValEntry *)entry)->GetValue();
1689       std::string vr  = entry->GetVR();
1690       uint32_t length = entry->GetLength();
1691       std::ostringstream s;
1692       int nbInt;
1693
1694       // When short integer(s) are expected, read and convert the following 
1695       // n * 2 bytes properly i.e. as a multivaluated strings
1696       // (each single value is separated fromthe next one by '\'
1697       // as usual for standard multivaluated filels
1698       // Elements with Value Multiplicity > 1
1699       // contain a set of short integers (not a single one) 
1700    
1701       if( vr == "US" || vr == "SS" )
1702       {
1703          uint16_t newInt16;
1704
1705          nbInt = length / 2;
1706          for (int i=0; i < nbInt; i++) 
1707          {
1708             if( i != 0 )
1709             {
1710                s << '\\';
1711             }
1712             newInt16 = ( val[2*i+0] & 0xFF ) + ( ( val[2*i+1] & 0xFF ) << 8);
1713             newInt16 = SwapShort( newInt16 );
1714             s << newInt16;
1715          }
1716       }
1717
1718       // When integer(s) are expected, read and convert the following 
1719       // n * 4 bytes properly i.e. as a multivaluated strings
1720       // (each single value is separated fromthe next one by '\'
1721       // as usual for standard multivaluated filels
1722       // Elements with Value Multiplicity > 1
1723       // contain a set of integers (not a single one) 
1724       else if( vr == "UL" || vr == "SL" )
1725       {
1726          uint32_t newInt32;
1727
1728          nbInt = length / 4;
1729          for (int i=0; i < nbInt; i++) 
1730          {
1731             if( i != 0)
1732             {
1733                s << '\\';
1734             }
1735             newInt32 = ( val[4*i+0] & 0xFF )
1736                     + (( val[4*i+1] & 0xFF ) <<  8 )
1737                     + (( val[4*i+2] & 0xFF ) << 16 )
1738                     + (( val[4*i+3] & 0xFF ) << 24 );
1739             newInt32 = SwapLong( newInt32 );
1740             s << newInt32;
1741          }
1742       }
1743 #ifdef GDCM_NO_ANSI_STRING_STREAM
1744       s << std::ends; // to avoid oddities on Solaris
1745 #endif //GDCM_NO_ANSI_STRING_STREAM
1746       return s.str();
1747    }
1748
1749    return ((ValEntry *)entry)->GetValue();
1750 }
1751
1752 /**
1753  * \brief   Get the reverse transformed value of the header entry. The VR 
1754  *          value is used to define the reverse transformation to operate on
1755  *          the value
1756  * \warning NOT end user intended method !
1757  * @param   entry Entry to reverse transform
1758  * @return  Reverse transformed entry value
1759  */
1760 std::string Document::GetDocEntryUnvalue(DocEntry *entry)
1761 {
1762    if ( IsDocEntryAnInteger(entry) && entry->IsImplicitVR() )
1763    {
1764       std::string vr = entry->GetVR();
1765       std::vector<std::string> tokens;
1766       std::ostringstream s;
1767
1768       if ( vr == "US" || vr == "SS" ) 
1769       {
1770          uint16_t newInt16;
1771
1772          tokens.erase( tokens.begin(), tokens.end()); // clean any previous value
1773          Util::Tokenize (((ValEntry *)entry)->GetValue(), tokens, "\\");
1774          for (unsigned int i=0; i<tokens.size(); i++) 
1775          {
1776             newInt16 = atoi(tokens[i].c_str());
1777             s << (  newInt16        & 0xFF ) 
1778               << (( newInt16 >> 8 ) & 0xFF );
1779          }
1780          tokens.clear();
1781       }
1782       if ( vr == "UL" || vr == "SL")
1783       {
1784          uint32_t newInt32;
1785
1786          tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1787          Util::Tokenize (((ValEntry *)entry)->GetValue(), tokens, "\\");
1788          for (unsigned int i=0; i<tokens.size();i++) 
1789          {
1790             newInt32 = atoi(tokens[i].c_str());
1791             s << (char)(  newInt32         & 0xFF ) 
1792               << (char)(( newInt32 >>  8 ) & 0xFF )
1793               << (char)(( newInt32 >> 16 ) & 0xFF )
1794               << (char)(( newInt32 >> 24 ) & 0xFF );
1795          }
1796          tokens.clear();
1797       }
1798
1799 #ifdef GDCM_NO_ANSI_STRING_STREAM
1800       s << std::ends; // to avoid oddities on Solaris
1801 #endif //GDCM_NO_ANSI_STRING_STREAM
1802       return s.str();
1803    }
1804
1805    return ((ValEntry *)entry)->GetValue();
1806 }
1807
1808 /**
1809  * \brief   Skip a given Header Entry 
1810  * \warning NOT end user intended method !
1811  * @param   entry entry to skip
1812  */
1813 void Document::SkipDocEntry(DocEntry *entry) 
1814 {
1815    SkipBytes(entry->GetLength());
1816 }
1817
1818 /**
1819  * \brief   Skips to the begining of the next Header Entry 
1820  * \warning NOT end user intended method !
1821  * @param   currentDocEntry entry to skip
1822  */
1823 void Document::SkipToNextDocEntry(DocEntry *currentDocEntry) 
1824 {
1825    Fp->seekg((long)(currentDocEntry->GetOffset()),     std::ios::beg);
1826    Fp->seekg( (long)(currentDocEntry->GetReadLength()),std::ios::cur);
1827 }
1828
1829 /**
1830  * \brief   When the length of an element value is obviously wrong (because
1831  *          the parser went Jabberwocky) one can hope improving things by
1832  *          applying some heuristics.
1833  * @param   entry entry to check
1834  * @param   foundLength first assumption about length    
1835  */
1836 void Document::FixDocEntryFoundLength(DocEntry *entry,
1837                                       uint32_t foundLength)
1838 {
1839    entry->SetReadLength( foundLength ); // will be updated only if a bug is found        
1840    if ( foundLength == 0xffffffff)
1841    {
1842       foundLength = 0;
1843    }
1844    
1845    uint16_t gr   = entry->GetGroup();
1846    uint16_t elem = entry->GetElement(); 
1847      
1848    if ( foundLength % 2)
1849    {
1850       gdcmVerboseMacro( "Warning : Tag with uneven length " << foundLength 
1851         <<  " in x(" << std::hex << gr << "," << elem <<")");
1852    }
1853       
1854    //////// Fix for some naughty General Electric images.
1855    // Allthough not recent many such GE corrupted images are still present
1856    // on Creatis hard disks. Hence this fix shall remain when such images
1857    // are no longer in use (we are talking a few years, here)...
1858    // Note: XMedCom probably uses such a trick since it is able to read
1859    //       those pesky GE images ...
1860    if ( foundLength == 13)
1861    {
1862       // Only happens for this length !
1863       if ( gr != 0x0008 || ( elem != 0x0070 && elem != 0x0080 ) )
1864       {
1865          foundLength = 10;
1866          entry->SetReadLength(10); /// \todo a bug is to be fixed !?
1867       }
1868    }
1869
1870    //////// Fix for some brain-dead 'Leonardo' Siemens images.
1871    // Occurence of such images is quite low (unless one leaves close to a
1872    // 'Leonardo' source. Hence, one might consider commenting out the
1873    // following fix on efficiency reasons.
1874    else if ( gr   == 0x0009 && ( elem == 0x1113 || elem == 0x1114 ) )
1875    {
1876       foundLength = 4;
1877       entry->SetReadLength(4); /// \todo a bug is to be fixed !?
1878    } 
1879  
1880    else if ( entry->GetVR() == "SQ" )
1881    {
1882       foundLength = 0;      // ReadLength is unchanged 
1883    } 
1884     
1885    //////// We encountered a 'delimiter' element i.e. a tag of the form 
1886    // "fffe|xxxx" which is just a marker. Delimiters length should not be
1887    // taken into account.
1888    else if( gr == 0xfffe )
1889    {    
1890      // According to the norm, fffe|0000 shouldn't exist. BUT the Philips
1891      // image gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm happens to
1892      // causes extra troubles...
1893      if( entry->GetElement() != 0x0000 )
1894      {
1895         foundLength = 0;
1896      }
1897    } 
1898            
1899    entry->SetLength(foundLength);
1900 }
1901
1902 /**
1903  * \brief   Apply some heuristics to predict whether the considered 
1904  *          element value contains/represents an integer or not.
1905  * @param   entry The element value on which to apply the predicate.
1906  * @return  The result of the heuristical predicate.
1907  */
1908 bool Document::IsDocEntryAnInteger(DocEntry *entry)
1909 {
1910    uint16_t elem    = entry->GetElement();
1911    uint16_t group   = entry->GetGroup();
1912    const std::string &vr  = entry->GetVR();
1913    uint32_t length  = entry->GetLength();
1914
1915    // When we have some semantics on the element we just read, and if we
1916    // a priori know we are dealing with an integer, then we shall be
1917    // able to swap it's element value properly.
1918    if ( elem == 0 )  // This is the group length of the group
1919    {  
1920       if ( length == 4 )
1921       {
1922          return true;
1923       }
1924       else 
1925       {
1926          // Allthough this should never happen, still some images have a
1927          // corrupted group length [e.g. have a glance at offset x(8336) of
1928          // gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm].
1929          // Since for dicom compliant and well behaved headers, the present
1930          // test is useless (and might even look a bit paranoid), when we
1931          // encounter such an ill-formed image, we simply display a warning
1932          // message and proceed on parsing (while crossing fingers).
1933          long filePosition = Fp->tellg();
1934          gdcmVerboseMacro( "Erroneous Group Length element length  on : (" 
1935            << std::hex << group << " , " << elem
1936            << ") -before- position x(" << filePosition << ")"
1937            << "lgt : " << length );
1938       }
1939    }
1940
1941    if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
1942    {
1943       return true;
1944    }   
1945    return false;
1946 }
1947
1948 /**
1949  * \brief  Find the Length till the next sequence delimiter
1950  * \warning NOT end user intended method !
1951  * @return 
1952  */
1953
1954 uint32_t Document::FindDocEntryLengthOBOrOW()
1955    throw( FormatUnexpected )
1956 {
1957    // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
1958    long positionOnEntry = Fp->tellg();
1959    bool foundSequenceDelimiter = false;
1960    uint32_t totalLength = 0;
1961
1962    while ( !foundSequenceDelimiter )
1963    {
1964       uint16_t group;
1965       uint16_t elem;
1966       try
1967       {
1968          group = ReadInt16();
1969          elem  = ReadInt16();   
1970       }
1971       catch ( FormatError )
1972       {
1973          throw FormatError("Unexpected end of file encountered during ",
1974                            "Document::FindDocEntryLengthOBOrOW()");
1975       }
1976
1977       // We have to decount the group and element we just read
1978       totalLength += 4;
1979      
1980       if ( group != 0xfffe || ( ( elem != 0xe0dd ) && ( elem != 0xe000 ) ) )
1981       {
1982          long filePosition = Fp->tellg();
1983          gdcmVerboseMacro( "Neither an Item tag nor a Sequence delimiter tag on :" 
1984            << std::hex << group << " , " << elem 
1985            << ") -before- position x(" << filePosition << ")" );
1986   
1987          Fp->seekg(positionOnEntry, std::ios::beg);
1988          throw FormatUnexpected( "Neither an Item tag nor a Sequence delimiter tag.");
1989       }
1990
1991       if ( elem == 0xe0dd )
1992       {
1993          foundSequenceDelimiter = true;
1994       }
1995
1996       uint32_t itemLength = ReadInt32();
1997       // We add 4 bytes since we just read the ItemLength with ReadInt32
1998       totalLength += itemLength + 4;
1999       SkipBytes(itemLength);
2000       
2001       if ( foundSequenceDelimiter )
2002       {
2003          break;
2004       }
2005    }
2006    Fp->seekg( positionOnEntry, std::ios::beg);
2007    return totalLength;
2008 }
2009
2010 /**
2011  * \brief Reads a supposed to be 16 Bits integer
2012  *       (swaps it depending on processor endianity) 
2013  * @return read value
2014  */
2015 uint16_t Document::ReadInt16()
2016    throw( FormatError )
2017 {
2018    uint16_t g;
2019    Fp->read ((char*)&g, (size_t)2);
2020    if ( Fp->fail() )
2021    {
2022       throw FormatError( "Document::ReadInt16()", " file error." );
2023    }
2024    if( Fp->eof() )
2025    {
2026       throw FormatError( "Document::ReadInt16()", "EOF." );
2027    }
2028    g = SwapShort(g); 
2029    return g;
2030 }
2031
2032 /**
2033  * \brief  Reads a supposed to be 32 Bits integer
2034  *         (swaps it depending on processor endianity)  
2035  * @return read value
2036  */
2037 uint32_t Document::ReadInt32()
2038    throw( FormatError )
2039 {
2040    uint32_t g;
2041    Fp->read ((char*)&g, (size_t)4);
2042    if ( Fp->fail() )
2043    {
2044       throw FormatError( "Document::ReadInt32()", " file error." );
2045    }
2046    if( Fp->eof() )
2047    {
2048       throw FormatError( "Document::ReadInt32()", "EOF." );
2049    }
2050    g = SwapLong(g);
2051    return g;
2052 }
2053
2054 /**
2055  * \brief skips bytes inside the source file 
2056  * \warning NOT end user intended method !
2057  * @return 
2058  */
2059 void Document::SkipBytes(uint32_t nBytes)
2060 {
2061    //FIXME don't dump the returned value
2062    Fp->seekg((long)nBytes, std::ios::cur);
2063 }
2064
2065 /**
2066  * \brief Loads all the needed Dictionaries
2067  * \warning NOT end user intended method !   
2068  */
2069 void Document::Initialise() 
2070 {
2071    RefPubDict = Global::GetDicts()->GetDefaultPubDict();
2072    RefShaDict = NULL;
2073    RLEInfo  = new RLEFramesInfo;
2074    JPEGInfo = new JPEGFragmentsInfo;
2075    Filetype = Unknown;
2076 }
2077
2078 /**
2079  * \brief   Discover what the swap code is (among little endian, big endian,
2080  *          bad little endian, bad big endian).
2081  *          sw is set
2082  * @return false when we are absolutely sure 
2083  *               it's neither ACR-NEMA nor DICOM
2084  *         true  when we hope ours assuptions are OK
2085  */
2086 bool Document::CheckSwap()
2087 {
2088    // The only guaranted way of finding the swap code is to find a
2089    // group tag since we know it's length has to be of four bytes i.e.
2090    // 0x00000004. Finding the swap code in then straigthforward. Trouble
2091    // occurs when we can't find such group...
2092    
2093    uint32_t  x = 4;  // x : for ntohs
2094    bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
2095    uint32_t  s32;
2096    uint16_t  s16;
2097        
2098    char deb[256];
2099     
2100    // First, compare HostByteOrder and NetworkByteOrder in order to
2101    // determine if we shall need to swap bytes (i.e. the Endian type).
2102    if ( x == ntohs(x) )
2103    {
2104       net2host = true;
2105    }
2106    else
2107    {
2108       net2host = false;
2109    }
2110          
2111    // The easiest case is the one of a DICOM header, since it possesses a
2112    // file preamble where it suffice to look for the string "DICM".
2113    Fp->read(deb, 256);
2114    
2115    char *entCur = deb + 128;
2116    if( memcmp(entCur, "DICM", (size_t)4) == 0 )
2117    {
2118       gdcmVerboseMacro( "Looks like DICOM Version3" );
2119       
2120       // Next, determine the value representation (VR). Let's skip to the
2121       // first element (0002, 0000) and check there if we find "UL" 
2122       // - or "OB" if the 1st one is (0002,0001) -,
2123       // in which case we (almost) know it is explicit VR.
2124       // WARNING: if it happens to be implicit VR then what we will read
2125       // is the length of the group. If this ascii representation of this
2126       // length happens to be "UL" then we shall believe it is explicit VR.
2127       // FIXME: in order to fix the above warning, we could read the next
2128       // element value (or a couple of elements values) in order to make
2129       // sure we are not commiting a big mistake.
2130       // We need to skip :
2131       // * the 128 bytes of File Preamble (often padded with zeroes),
2132       // * the 4 bytes of "DICM" string,
2133       // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
2134       // i.e. a total of  136 bytes.
2135       entCur = deb + 136;
2136      
2137       // FIXME : FIXME:
2138       // Sometimes (see : gdcmData/icone.dcm) group 0x0002 *is* Explicit VR,
2139       // but elem 0002,0010 (Transfer Syntax) tells us the file is
2140       // *Implicit* VR.  -and it is !- 
2141       
2142       if( memcmp(entCur, "UL", (size_t)2) == 0 ||
2143           memcmp(entCur, "OB", (size_t)2) == 0 ||
2144           memcmp(entCur, "UI", (size_t)2) == 0 ||
2145           memcmp(entCur, "CS", (size_t)2) == 0 )  // CS, to remove later
2146                                                     // when Write DCM *adds*
2147       // FIXME
2148       // Use Document::dicom_vr to test all the possibilities
2149       // instead of just checking for UL, OB and UI !? group 0000 
2150       {
2151          Filetype = ExplicitVR;
2152          gdcmVerboseMacro( "Explicit Value Representation");
2153       } 
2154       else 
2155       {
2156          Filetype = ImplicitVR;
2157          gdcmVerboseMacro( "Not an explicit Value Representation");
2158       }
2159       
2160       if ( net2host )
2161       {
2162          SwapCode = 4321;
2163          gdcmVerboseMacro( "HostByteOrder != NetworkByteOrder");
2164       }
2165       else 
2166       {
2167          SwapCode = 1234;
2168          gdcmVerboseMacro( "HostByteOrder = NetworkByteOrder");
2169       }
2170       
2171       // Position the file position indicator at first tag (i.e.
2172       // after the file preamble and the "DICM" string).
2173       Fp->seekg(0, std::ios::beg);
2174       Fp->seekg ( 132L, std::ios::beg);
2175       return true;
2176    } // End of DicomV3
2177
2178    // Alas, this is not a DicomV3 file and whatever happens there is no file
2179    // preamble. We can reset the file position indicator to where the data
2180    // is (i.e. the beginning of the file).
2181    gdcmVerboseMacro( "Not a DICOM Version3 file");
2182    Fp->seekg(0, std::ios::beg);
2183
2184    // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
2185    // By clean we mean that the length of the first tag is written down.
2186    // If this is the case and since the length of the first group HAS to be
2187    // four (bytes), then determining the proper swap code is straightforward.
2188
2189    entCur = deb + 4;
2190    // We assume the array of char we are considering contains the binary
2191    // representation of a 32 bits integer. Hence the following dirty
2192    // trick :
2193    s32 = *((uint32_t *)(entCur));
2194
2195    switch( s32 )
2196    {
2197       case 0x00040000 :
2198          SwapCode = 3412;
2199          Filetype = ACR;
2200          return true;
2201       case 0x04000000 :
2202          SwapCode = 4321;
2203          Filetype = ACR;
2204          return true;
2205       case 0x00000400 :
2206          SwapCode = 2143;
2207          Filetype = ACR;
2208          return true;
2209       case 0x00000004 :
2210          SwapCode = 1234;
2211          Filetype = ACR;
2212          return true;
2213       default :
2214          // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
2215          // It is time for despaired wild guesses. 
2216          // So, let's check if this file wouldn't happen to be 'dirty' ACR/NEMA,
2217          //  i.e. the 'group length' element is not present :     
2218          
2219          //  check the supposed-to-be 'group number'
2220          //  in ( 0x0001 .. 0x0008 )
2221          //  to determine ' SwapCode' value .
2222          //  Only 0 or 4321 will be possible 
2223          //  (no oportunity to check for the formerly well known
2224          //  ACR-NEMA 'Bad Big Endian' or 'Bad Little Endian' 
2225          //  if unsuccessfull (i.e. neither 0x0002 nor 0x0200 etc -3, 4, ..., 8-) 
2226          //  the file IS NOT ACR-NEMA nor DICOM V3
2227          //  Find a trick to tell it the caller...
2228       
2229          s16 = *((uint16_t *)(deb));
2230       
2231          switch ( s16 )
2232          {
2233             case 0x0001 :
2234             case 0x0002 :
2235             case 0x0003 :
2236             case 0x0004 :
2237             case 0x0005 :
2238             case 0x0006 :
2239             case 0x0007 :
2240             case 0x0008 :
2241                SwapCode = 1234;
2242                Filetype = ACR;
2243                return true;
2244             case 0x0100 :
2245             case 0x0200 :
2246             case 0x0300 :
2247             case 0x0400 :
2248             case 0x0500 :
2249             case 0x0600 :
2250             case 0x0700 :
2251             case 0x0800 :
2252                SwapCode = 4321;
2253                Filetype = ACR;
2254                return true;
2255             default :
2256                gdcmVerboseMacro( "ACR/NEMA unfound swap info (Really hopeless !)");
2257                Filetype = Unknown;
2258                return false;
2259          }
2260          // Then the only info we have is the net2host one.
2261          //if (! net2host )
2262          //   SwapCode = 1234;
2263          //else
2264          //  SwapCode = 4321;
2265          //return;
2266    }
2267 }
2268
2269
2270
2271 /**
2272  * \brief Change the Byte Swap code. 
2273  */
2274 void Document::SwitchByteSwapCode() 
2275 {
2276    gdcmVerboseMacro( "Switching Byte Swap code.");
2277    if ( SwapCode == 1234 ) 
2278    {
2279       SwapCode = 4321;
2280    }
2281    else if ( SwapCode == 4321 ) 
2282    {
2283       SwapCode = 1234;
2284    }
2285    else if ( SwapCode == 3412 ) 
2286    {
2287       SwapCode = 2143;
2288    }
2289    else if ( SwapCode == 2143 )
2290    {
2291       SwapCode = 3412;
2292    }
2293 }
2294
2295 /**
2296  * \brief  during parsing, Header Elements too long are not loaded in memory 
2297  * @param newSize
2298  */
2299 void Document::SetMaxSizeLoadEntry(long newSize) 
2300 {
2301    if ( newSize < 0 )
2302    {
2303       return;
2304    }
2305    if ((uint32_t)newSize >= (uint32_t)0xffffffff )
2306    {
2307       MaxSizeLoadEntry = 0xffffffff;
2308       return;
2309    }
2310    MaxSizeLoadEntry = newSize;
2311 }
2312
2313
2314 /**
2315  * \brief Header Elements too long will not be printed
2316  * \todo  See comments of \ref Document::MAX_SIZE_PRINT_ELEMENT_VALUE 
2317  * @param newSize
2318  */
2319 void Document::SetMaxSizePrintEntry(long newSize) 
2320 {
2321    //DOH !! This is exactly SetMaxSizeLoadEntry FIXME FIXME
2322    if ( newSize < 0 )
2323    {
2324       return;
2325    }
2326    if ((uint32_t)newSize >= (uint32_t)0xffffffff )
2327    {
2328       MaxSizePrintEntry = 0xffffffff;
2329       return;
2330    }
2331    MaxSizePrintEntry = newSize;
2332 }
2333
2334
2335
2336 /**
2337  * \brief   Handle broken private tag from Philips NTSCAN
2338  *          where the endianess is being switch to BigEndian for no
2339  *          apparent reason
2340  * @return  no return
2341  */
2342 void Document::HandleBrokenEndian(uint16_t &group, uint16_t &elem)
2343 {
2344    // Endian reversion. Some files contain groups of tags with reversed endianess.
2345    static int reversedEndian = 0;
2346    // try to fix endian switching in the middle of headers
2347    if ((group == 0xfeff) && (elem == 0x00e0))
2348    {
2349      // start endian swap mark for group found
2350      reversedEndian++;
2351      SwitchByteSwapCode();
2352      // fix the tag
2353      group = 0xfffe;
2354      elem = 0xe000;
2355    } 
2356    else if (group == 0xfffe && elem == 0xe00d && reversedEndian) 
2357    {
2358      // end of reversed endian group
2359      reversedEndian--;
2360      SwitchByteSwapCode();
2361    }
2362 }
2363
2364 /**
2365  * \brief   Group 0002 is always coded Little Endian
2366  *          whatever Transfer Syntax is
2367  * @return  no return
2368  */
2369 void Document::HandleOutOfGroup0002(uint16_t group)
2370 {
2371    // Endian reversion. Some files contain groups of tags with reversed endianess.
2372    if ( !Group0002Parsed && group != 0x0002)
2373    {
2374       Group0002Parsed = true;
2375      // we just came out of group 0002
2376      // if Transfer syntax is Big Endian we have to change CheckSwap
2377
2378       TagKey key = DictEntry::TranslateToKey(0x0002, 0x0010);
2379       if ( !TagHT.count(key))
2380       {
2381          gdcmVerboseMacro("True DICOM File, with NO Tansfer Syntax ?!?");
2382          return;
2383       }
2384
2385    // FIXME Strangely, this works with 
2386    //'Implicit VR Transfer Syntax (GE Private)
2387
2388        if ( ((ValEntry *)TagHT.find(key)->second)->GetValue()
2389                == "Explicit VR - Big Endian" )
2390        {
2391           gdcmVerboseMacro("Tansfer Syntax = Explicit VR - Big Endian");
2392           SwitchByteSwapCode();
2393         }
2394    }
2395 }
2396
2397 /**
2398  * \brief   Read the next tag but WITHOUT loading it's value
2399  *          (read the 'Group Number', the 'Element Number',
2400  *          gets the Dict Entry
2401  *          gets the VR, gets the length, gets the offset value)
2402  * @return  On succes the newly created DocEntry, NULL on failure.      
2403  */
2404 DocEntry *Document::ReadNextDocEntry()
2405 {
2406    uint16_t group;
2407    uint16_t elem;
2408
2409    try
2410    {
2411       group = ReadInt16();
2412       elem  = ReadInt16();
2413    }
2414    catch ( FormatError e )
2415    {
2416       // We reached the EOF (or an error occured) therefore 
2417       // header parsing has to be considered as finished.
2418       //std::cout << e;
2419       return 0;
2420    }
2421
2422    // Sometimes file contains groups of tags with reversed endianess.
2423    HandleBrokenEndian(group, elem);
2424
2425 // In 'true DICOM' files Group 0002 is allways little endian
2426    if ( HasDCMPreamble )
2427       HandleOutOfGroup0002(group);
2428  
2429    std::string vr = FindDocEntryVR();
2430    std::string realVR = vr;
2431
2432    if( vr == GDCM_UNKNOWN)
2433    {
2434       DictEntry *dictEntry = GetDictEntry(group,elem);
2435       if( dictEntry )
2436          realVR = dictEntry->GetVR();
2437    }
2438
2439    DocEntry *newEntry;
2440    if( Global::GetVR()->IsVROfSequence(realVR) )
2441       newEntry = NewSeqEntry(group, elem);
2442    else if( Global::GetVR()->IsVROfStringRepresentable(realVR) )
2443       newEntry = NewValEntry(group, elem,vr);
2444    else
2445       newEntry = NewBinEntry(group, elem,vr);
2446
2447    if( vr == GDCM_UNKNOWN )
2448    {
2449       if( Filetype == ExplicitVR )
2450       {
2451          // We thought this was explicit VR, but we end up with an
2452          // implicit VR tag. Let's backtrack.   
2453          std::string msg;
2454          msg = Util::Format("Falsely explicit vr file (%04x,%04x)\n", 
2455                        newEntry->GetGroup(), newEntry->GetElement());
2456          gdcmVerboseMacro( msg.c_str() );
2457       }
2458       newEntry->SetImplicitVR();
2459    }
2460
2461    try
2462    {
2463       FindDocEntryLength(newEntry);
2464    }
2465    catch ( FormatError e )
2466    {
2467       // Call it quits
2468       //std::cout << e;
2469       delete newEntry;
2470       return 0;
2471    }
2472
2473    newEntry->SetOffset(Fp->tellg());  
2474
2475    return newEntry;
2476 }
2477
2478
2479 /**
2480  * \brief   Generate a free TagKey i.e. a TagKey that is not present
2481  *          in the TagHt dictionary.
2482  * @param   group The generated tag must belong to this group.  
2483  * @return  The element of tag with given group which is fee.
2484  */
2485 uint32_t Document::GenerateFreeTagKeyInGroup(uint16_t group) 
2486 {
2487    for (uint32_t elem = 0; elem < UINT32_MAX; elem++) 
2488    {
2489       TagKey key = DictEntry::TranslateToKey(group, elem);
2490       if (TagHT.count(key) == 0)
2491       {
2492          return elem;
2493       }
2494    }
2495    return UINT32_MAX;
2496 }
2497
2498 /**
2499  * \brief   Assuming the internal file pointer \ref Document::Fp 
2500  *          is placed at the beginning of a tag check whether this
2501  *          tag is (TestGroup, TestElement).
2502  * \warning On success the internal file pointer \ref Document::Fp
2503  *          is modified to point after the tag.
2504  *          On failure (i.e. when the tag wasn't the expected tag
2505  *          (TestGroup, TestElement) the internal file pointer
2506  *          \ref Document::Fp is restored to it's original position.
2507  * @param   testGroup   The expected group of the tag.
2508  * @param   testElement The expected Element of the tag.
2509  * @return  True on success, false otherwise.
2510  */
2511 bool Document::ReadTag(uint16_t testGroup, uint16_t testElement)
2512 {
2513    long positionOnEntry = Fp->tellg();
2514    long currentPosition = Fp->tellg();          // On debugging purposes
2515
2516    //// Read the Item Tag group and element, and make
2517    // sure they are what we expected:
2518    uint16_t itemTagGroup;
2519    uint16_t itemTagElement;
2520    try
2521    {
2522       itemTagGroup   = ReadInt16();
2523       itemTagElement = ReadInt16();
2524    }
2525    catch ( FormatError e )
2526    {
2527       //std::cerr << e << std::endl;
2528       return false;
2529    }
2530    if ( itemTagGroup != testGroup || itemTagElement != testElement )
2531    {
2532       gdcmVerboseMacro( "Wrong Item Tag found:"
2533        << "   We should have found tag ("
2534        << std::hex << testGroup << "," << testElement << ")" << std::endl
2535        << "   but instead we encountered tag ("
2536        << std::hex << itemTagGroup << "," << itemTagElement << ")"
2537        << "  at address: " << "  0x(" << (unsigned int)currentPosition  << ")" 
2538        ) ;
2539       Fp->seekg(positionOnEntry, std::ios::beg);
2540
2541       return false;
2542    }
2543    return true;
2544 }
2545
2546 /**
2547  * \brief   Assuming the internal file pointer \ref Document::Fp 
2548  *          is placed at the beginning of a tag (TestGroup, TestElement),
2549  *          read the length associated to the Tag.
2550  * \warning On success the internal file pointer \ref Document::Fp
2551  *          is modified to point after the tag and it's length.
2552  *          On failure (i.e. when the tag wasn't the expected tag
2553  *          (TestGroup, TestElement) the internal file pointer
2554  *          \ref Document::Fp is restored to it's original position.
2555  * @param   testGroup   The expected group of the tag.
2556  * @param   testElement The expected Element of the tag.
2557  * @return  On success returns the length associated to the tag. On failure
2558  *          returns 0.
2559  */
2560 uint32_t Document::ReadTagLength(uint16_t testGroup, uint16_t testElement)
2561 {
2562    long positionOnEntry = Fp->tellg();
2563    (void)positionOnEntry;
2564
2565    if ( !ReadTag(testGroup, testElement) )
2566    {
2567       return 0;
2568    }
2569                                                                                 
2570    //// Then read the associated Item Length
2571    long currentPosition = Fp->tellg();
2572    uint32_t itemLength  = ReadInt32();
2573    {
2574       gdcmVerboseMacro( "Basic Item Length is: "
2575         << itemLength << std::endl
2576         << "  at address: " << std::hex << (unsigned int)currentPosition);
2577    }
2578    return itemLength;
2579 }
2580
2581 /**
2582  * \brief When parsing the Pixel Data of an encapsulated file, read
2583  *        the basic offset table (when present, and BTW dump it).
2584  */
2585 void Document::ReadAndSkipEncapsulatedBasicOffsetTable()
2586 {
2587    //// Read the Basic Offset Table Item Tag length...
2588    uint32_t itemLength = ReadTagLength(0xfffe, 0xe000);
2589
2590    // When present, read the basic offset table itself.
2591    // Notes: - since the presence of this basic offset table is optional
2592    //          we can't rely on it for the implementation, and we will simply
2593    //          trash it's content (when present).
2594    //        - still, when present, we could add some further checks on the
2595    //          lengths, but we won't bother with such fuses for the time being.
2596    if ( itemLength != 0 )
2597    {
2598       char *basicOffsetTableItemValue = new char[itemLength + 1];
2599       Fp->read(basicOffsetTableItemValue, itemLength);
2600
2601 #ifdef GDCM_DEBUG
2602       for (unsigned int i=0; i < itemLength; i += 4 )
2603       {
2604          uint32_t individualLength = str2num( &basicOffsetTableItemValue[i],
2605                                               uint32_t);
2606          gdcmVerboseMacro( "Read one length: " << 
2607                           std::hex << individualLength );
2608       }
2609 #endif //GDCM_DEBUG
2610
2611       delete[] basicOffsetTableItemValue;
2612    }
2613 }
2614
2615 /**
2616  * \brief Parse pixel data from disk of [multi-]fragment RLE encoding.
2617  *        Compute the RLE extra information and store it in \ref RLEInfo
2618  *        for later pixel retrieval usage.
2619  */
2620 void Document::ComputeRLEInfo()
2621 {
2622    std::string ts = GetTransferSyntax();
2623    if ( !Global::GetTS()->IsRLELossless(ts) ) 
2624    {
2625       return;
2626    }
2627
2628    // Encoded pixel data: for the time being we are only concerned with
2629    // Jpeg or RLE Pixel data encodings.
2630    // As stated in PS 3.5-2003, section 8.2 p44:
2631    // "If sent in Encapsulated Format (i.e. other than the Native Format) the
2632    //  value representation OB is used".
2633    // Hence we expect an OB value representation. Concerning OB VR,
2634    // the section PS 3.5-2003, section A.4.c p 58-59, states:
2635    // "For the Value Representations OB and OW, the encoding shall meet the
2636    //   following specifications depending on the Data element tag:"
2637    //   [...snip...]
2638    //    - the first item in the sequence of items before the encoded pixel
2639    //      data stream shall be basic offset table item. The basic offset table
2640    //      item value, however, is not required to be present"
2641
2642    ReadAndSkipEncapsulatedBasicOffsetTable();
2643
2644    // Encapsulated RLE Compressed Images (see PS 3.5-2003, Annex G)
2645    // Loop on the individual frame[s] and store the information
2646    // on the RLE fragments in a RLEFramesInfo.
2647    // Note: - when only a single frame is present, this is a
2648    //         classical image.
2649    //       - when more than one frame are present, then we are in 
2650    //         the case of a multi-frame image.
2651    long frameLength;
2652    while ( (frameLength = ReadTagLength(0xfffe, 0xe000)) )
2653    { 
2654       // Parse the RLE Header and store the corresponding RLE Segment
2655       // Offset Table information on fragments of this current Frame.
2656       // Note that the fragment pixels themselves are not loaded
2657       // (but just skipped).
2658       long frameOffset = Fp->tellg();
2659
2660       uint32_t nbRleSegments = ReadInt32();
2661       if ( nbRleSegments > 16 )
2662       {
2663          // There should be at most 15 segments (refer to RLEFrame class)
2664          gdcmVerboseMacro( "Too many segments.");
2665       }
2666  
2667       uint32_t rleSegmentOffsetTable[16];
2668       for( int k = 1; k <= 15; k++ )
2669       {
2670          rleSegmentOffsetTable[k] = ReadInt32();
2671       }
2672
2673       // Deduce from both the RLE Header and the frameLength the
2674       // fragment length, and again store this info in a
2675       // RLEFramesInfo.
2676       long rleSegmentLength[15];
2677       // skipping (not reading) RLE Segments
2678       if ( nbRleSegments > 1)
2679       {
2680          for(unsigned int k = 1; k <= nbRleSegments-1; k++)
2681          {
2682              rleSegmentLength[k] =  rleSegmentOffsetTable[k+1]
2683                                   - rleSegmentOffsetTable[k];
2684              SkipBytes(rleSegmentLength[k]);
2685           }
2686        }
2687
2688        rleSegmentLength[nbRleSegments] = frameLength 
2689                                       - rleSegmentOffsetTable[nbRleSegments];
2690        SkipBytes(rleSegmentLength[nbRleSegments]);
2691
2692        // Store the collected info
2693        RLEFrame *newFrameInfo = new RLEFrame;
2694        newFrameInfo->NumberFragments = nbRleSegments;
2695        for( unsigned int uk = 1; uk <= nbRleSegments; uk++ )
2696        {
2697           newFrameInfo->Offset[uk] = frameOffset + rleSegmentOffsetTable[uk];
2698           newFrameInfo->Length[uk] = rleSegmentLength[uk];
2699        }
2700        RLEInfo->Frames.push_back( newFrameInfo );
2701    }
2702
2703    // Make sure that at the end of the item we encounter a 'Sequence
2704    // Delimiter Item':
2705    if ( !ReadTag(0xfffe, 0xe0dd) )
2706    {
2707       gdcmVerboseMacro( "No sequence delimiter item at end of RLE item sequence");
2708    }
2709 }
2710
2711 /**
2712  * \brief Parse pixel data from disk of [multi-]fragment Jpeg encoding.
2713  *        Compute the jpeg extra information (fragment[s] offset[s] and
2714  *        length) and store it[them] in \ref JPEGInfo for later pixel
2715  *        retrieval usage.
2716  */
2717 void Document::ComputeJPEGFragmentInfo()
2718 {
2719    // If you need to, look for comments of ComputeRLEInfo().
2720    std::string ts = GetTransferSyntax();
2721    if ( ! Global::GetTS()->IsJPEG(ts) )
2722    {
2723       return;
2724    }
2725
2726    ReadAndSkipEncapsulatedBasicOffsetTable();
2727
2728    // Loop on the fragments[s] and store the parsed information in a
2729    // JPEGInfo.
2730    long fragmentLength;
2731    while ( (fragmentLength = ReadTagLength(0xfffe, 0xe000)) )
2732    { 
2733       long fragmentOffset = Fp->tellg();
2734
2735        // Store the collected info
2736        JPEGFragment *newFragment = new JPEGFragment;
2737        newFragment->Offset = fragmentOffset;
2738        newFragment->Length = fragmentLength;
2739        JPEGInfo->Fragments.push_back( newFragment );
2740
2741        SkipBytes( fragmentLength );
2742    }
2743
2744    // Make sure that at the end of the item we encounter a 'Sequence
2745    // Delimiter Item':
2746    if ( !ReadTag(0xfffe, 0xe0dd) )
2747    {
2748       gdcmVerboseMacro( "No sequence delimiter item at end of JPEG item sequence");
2749    }
2750 }
2751
2752 /**
2753  * \brief Walk recursively the given \ref DocEntrySet, and feed
2754  *        the given hash table (\ref TagDocEntryHT) with all the
2755  *        \ref DocEntry (Dicom entries) encountered.
2756  *        This method does the job for \ref BuildFlatHashTable.
2757  * @param builtHT Where to collect all the \ref DocEntry encountered
2758  *        when recursively walking the given set.
2759  * @param set The structure to be traversed (recursively).
2760  */
2761 void Document::BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
2762                                           DocEntrySet *set )
2763
2764    if (ElementSet *elementSet = dynamic_cast< ElementSet* > ( set ) )
2765    {
2766       TagDocEntryHT const &currentHT = elementSet->GetTagHT();
2767       for( TagDocEntryHT::const_iterator i  = currentHT.begin();
2768                                          i != currentHT.end();
2769                                        ++i)
2770       {
2771          DocEntry *entry = i->second;
2772          if ( SeqEntry *seqEntry = dynamic_cast<SeqEntry*>(entry) )
2773          {
2774             const ListSQItem& items = seqEntry->GetSQItems();
2775             for( ListSQItem::const_iterator item  = items.begin();
2776                                             item != items.end();
2777                                           ++item)
2778             {
2779                BuildFlatHashTableRecurse( builtHT, *item );
2780             }
2781             continue;
2782          }
2783          builtHT[entry->GetKey()] = entry;
2784       }
2785       return;
2786     }
2787
2788    if (SQItem *SQItemSet = dynamic_cast< SQItem* > ( set ) )
2789    {
2790       const ListDocEntry& currentList = SQItemSet->GetDocEntries();
2791       for (ListDocEntry::const_iterator i  = currentList.begin();
2792                                         i != currentList.end();
2793                                       ++i)
2794       {
2795          DocEntry *entry = *i;
2796          if ( SeqEntry *seqEntry = dynamic_cast<SeqEntry*>(entry) )
2797          {
2798             const ListSQItem& items = seqEntry->GetSQItems();
2799             for( ListSQItem::const_iterator item  = items.begin();
2800                                             item != items.end();
2801                                           ++item)
2802             {
2803                BuildFlatHashTableRecurse( builtHT, *item );
2804             }
2805             continue;
2806          }
2807          builtHT[entry->GetKey()] = entry;
2808       }
2809
2810    }
2811 }
2812
2813 /**
2814  * \brief Build a \ref TagDocEntryHT (i.e. a std::map<>) from the current
2815  *        Document.
2816  *
2817  *        The structure used by a Document (through \ref ElementSet),
2818  *        in order to hold the parsed entries of a Dicom header, is a recursive
2819  *        one. This is due to the fact that the sequences (when present)
2820  *        can be nested. Additionaly, the sequence items (represented in
2821  *        gdcm as \ref SQItem) add an extra complexity to the data
2822  *        structure. Hence, a gdcm user whishing to visit all the entries of
2823  *        a Dicom header will need to dig in the gdcm internals (which
2824  *        implies exposing all the internal data structures to the API).
2825  *        In order to avoid this burden to the user, \ref BuildFlatHashTable
2826  *        recursively builds a temporary hash table, which holds all the
2827  *        Dicom entries in a flat structure (a \ref TagDocEntryHT i.e. a
2828  *        std::map<>).
2829  * \warning Of course there is NO integrity constrain between the 
2830  *        returned \ref TagDocEntryHT and the \ref ElementSet used
2831  *        to build it. Hence if the underlying \ref ElementSet is
2832  *        altered, then it is the caller responsability to invoke 
2833  *        \ref BuildFlatHashTable again...
2834  * @return The flat std::map<> we juste build.
2835  */
2836 TagDocEntryHT *Document::BuildFlatHashTable()
2837 {
2838    TagDocEntryHT *FlatHT = new TagDocEntryHT;
2839    BuildFlatHashTableRecurse( *FlatHT, this );
2840    return FlatHT;
2841 }
2842
2843
2844
2845 /**
2846  * \brief   Compares two documents, according to \ref DicomDir rules
2847  * \warning Does NOT work with ACR-NEMA files
2848  * \todo    Find a trick to solve the pb (use RET fields ?)
2849  * @param   document
2850  * @return  true if 'smaller'
2851  */
2852 bool Document::operator<(Document &document)
2853 {
2854    // Patient Name
2855    std::string s1 = GetEntry(0x0010,0x0010);
2856    std::string s2 = document.GetEntry(0x0010,0x0010);
2857    if(s1 < s2)
2858    {
2859       return true;
2860    }
2861    else if( s1 > s2 )
2862    {
2863       return false;
2864    }
2865    else
2866    {
2867       // Patient ID
2868       s1 = GetEntry(0x0010,0x0020);
2869       s2 = document.GetEntry(0x0010,0x0020);
2870       if ( s1 < s2 )
2871       {
2872          return true;
2873       }
2874       else if ( s1 > s2 )
2875       {
2876          return false;
2877       }
2878       else
2879       {
2880          // Study Instance UID
2881          s1 = GetEntry(0x0020,0x000d);
2882          s2 = document.GetEntry(0x0020,0x000d);
2883          if ( s1 < s2 )
2884          {
2885             return true;
2886          }
2887          else if( s1 > s2 )
2888          {
2889             return false;
2890          }
2891          else
2892          {
2893             // Serie Instance UID
2894             s1 = GetEntry(0x0020,0x000e);
2895             s2 = document.GetEntry(0x0020,0x000e);    
2896             if ( s1 < s2 )
2897             {
2898                return true;
2899             }
2900             else if( s1 > s2 )
2901             {
2902                return false;
2903             }
2904          }
2905       }
2906    }
2907    return false;
2908 }
2909
2910
2911 /**
2912  * \brief   Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
2913  * @param filetype Type of the File to be written 
2914  */
2915 int Document::ComputeGroup0002Length( FileType filetype ) 
2916 {
2917    uint16_t gr, el;
2918    std::string vr;
2919    
2920    int groupLength = 0;
2921    bool found0002 = false;   
2922   
2923    // for each zero-level Tag in the DCM Header
2924    DocEntry *entry;
2925
2926    Initialize();
2927    entry = GetNextEntry();
2928    while(entry)
2929    {
2930       gr = entry->GetGroup();
2931
2932       if (gr == 0x0002)
2933       {
2934          found0002 = true;
2935
2936          el = entry->GetElement();
2937          vr = entry->GetVR();            
2938  
2939          if (filetype == ExplicitVR) 
2940          {
2941             if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) 
2942             {
2943                groupLength +=  4; // explicit VR AND OB, OW, SQ : 4 more bytes
2944             }
2945          }
2946          groupLength += 2 + 2 + 4 + entry->GetLength();   
2947       }
2948       else if (found0002 )
2949          break;
2950
2951       entry = GetNextEntry();
2952    }
2953    return groupLength; 
2954 }
2955
2956 } // end namespace gdcm
2957
2958 //-----------------------------------------------------------------------------