]> Creatis software - gdcm.git/blob - src/gdcmDocument.cxx
ENH: Untangle the transfer syntax from the Document. The Document can only read a...
[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 16:44:43 $
7   Version:   $Revision: 1.174 $
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 = 0;
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    0 :
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) != TS::ExplicitVRBigEndian ) 
1581          {
1582             throw FormatError( "Document::FindDocEntryLength()",
1583                                " not explicit VR." );
1584             return;
1585          }
1586          length16 = 4;
1587          SwitchByteSwapCode();
1588
1589          // Restore the unproperly loaded values i.e. the group, the element
1590          // and the dictionary entry depending on them.
1591          uint16_t correctGroup = SwapShort( entry->GetGroup() );
1592          uint16_t correctElem  = SwapShort( entry->GetElement() );
1593          DictEntry *newTag = GetDictEntry( correctGroup, correctElem );
1594          if ( !newTag )
1595          {
1596             // This correct tag is not in the dictionary. Create a new one.
1597             newTag = NewVirtualDictEntry(correctGroup, correctElem);
1598          }
1599          // FIXME this can create a memory leaks on the old entry that be
1600          // left unreferenced.
1601          entry->SetDictEntry( newTag );
1602       }
1603        
1604       // Heuristic: well, some files are really ill-formed.
1605       if ( length16 == 0xffff) 
1606       {
1607          // 0xffff means that we deal with 'Unknown Length' Sequence  
1608          length16 = 0;
1609       }
1610       FixDocEntryFoundLength( entry, (uint32_t)length16 );
1611       return;
1612    }
1613    else
1614    {
1615       // Either implicit VR or a non DICOM conformal (see note below) explicit
1616       // VR that ommited the VR of (at least) this element. Farts happen.
1617       // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1618       // on Data elements "Implicit and Explicit VR Data Elements shall
1619       // not coexist in a Data Set and Data Sets nested within it".]
1620       // Length is on 4 bytes.
1621       
1622       FixDocEntryFoundLength( entry, ReadInt32() );
1623       return;
1624    }
1625 }
1626
1627 /**
1628  * \brief     Find the Value Representation of the current Dicom Element.
1629  * @return    Value Representation of the current Entry
1630  */
1631 std::string Document::FindDocEntryVR()
1632 {
1633    if ( Filetype != ExplicitVR )
1634       return GDCM_UNKNOWN;
1635
1636    long positionOnEntry = Fp->tellg();
1637    // Warning: we believe this is explicit VR (Value Representation) because
1638    // we used a heuristic that found "UL" in the first tag. Alas this
1639    // doesn't guarantee that all the tags will be in explicit VR. In some
1640    // cases (see e-film filtered files) one finds implicit VR tags mixed
1641    // within an explicit VR file. Hence we make sure the present tag
1642    // is in explicit VR and try to fix things if it happens not to be
1643    // the case.
1644
1645    char vr[3];
1646    Fp->read (vr, (size_t)2);
1647    vr[2] = 0;
1648
1649    if( !CheckDocEntryVR(vr) )
1650    {
1651       Fp->seekg(positionOnEntry, std::ios::beg);
1652       return GDCM_UNKNOWN;
1653    }
1654    return vr;
1655 }
1656
1657 /**
1658  * \brief     Check the correspondance between the VR of the header entry
1659  *            and the taken VR. If they are different, the header entry is 
1660  *            updated with the new VR.
1661  * @param     vr    Dicom Value Representation
1662  * @return    false if the VR is incorrect of if the VR isn't referenced
1663  *            otherwise, it returns true
1664 */
1665 bool Document::CheckDocEntryVR(VRKey vr)
1666 {
1667    // CLEANME searching the dicom_vr at each occurence is expensive.
1668    // PostPone this test in an optional integrity check at the end
1669    // of parsing or only in debug mode.
1670    if ( !Global::GetVR()->IsValidVR(vr) )
1671       return false;
1672
1673    return true; 
1674 }
1675
1676 /**
1677  * \brief   Get the transformed value of the header entry. The VR value 
1678  *          is used to define the transformation to operate on the value
1679  * \warning NOT end user intended method !
1680  * @param   entry entry to tranform
1681  * @return  Transformed entry value
1682  */
1683 std::string Document::GetDocEntryValue(DocEntry *entry)
1684 {
1685    if ( IsDocEntryAnInteger(entry) && entry->IsImplicitVR() )
1686    {
1687       std::string val = ((ValEntry *)entry)->GetValue();
1688       std::string vr  = entry->GetVR();
1689       uint32_t length = entry->GetLength();
1690       std::ostringstream s;
1691       int nbInt;
1692
1693       // When short integer(s) are expected, read and convert the following 
1694       // n * 2 bytes properly i.e. as a multivaluated strings
1695       // (each single value is separated fromthe next one by '\'
1696       // as usual for standard multivaluated filels
1697       // Elements with Value Multiplicity > 1
1698       // contain a set of short integers (not a single one) 
1699    
1700       if( vr == "US" || vr == "SS" )
1701       {
1702          uint16_t newInt16;
1703
1704          nbInt = length / 2;
1705          for (int i=0; i < nbInt; i++) 
1706          {
1707             if( i != 0 )
1708             {
1709                s << '\\';
1710             }
1711             newInt16 = ( val[2*i+0] & 0xFF ) + ( ( val[2*i+1] & 0xFF ) << 8);
1712             newInt16 = SwapShort( newInt16 );
1713             s << newInt16;
1714          }
1715       }
1716
1717       // When integer(s) are expected, read and convert the following 
1718       // n * 4 bytes properly i.e. as a multivaluated strings
1719       // (each single value is separated fromthe next one by '\'
1720       // as usual for standard multivaluated filels
1721       // Elements with Value Multiplicity > 1
1722       // contain a set of integers (not a single one) 
1723       else if( vr == "UL" || vr == "SL" )
1724       {
1725          uint32_t newInt32;
1726
1727          nbInt = length / 4;
1728          for (int i=0; i < nbInt; i++) 
1729          {
1730             if( i != 0)
1731             {
1732                s << '\\';
1733             }
1734             newInt32 = ( val[4*i+0] & 0xFF )
1735                     + (( val[4*i+1] & 0xFF ) <<  8 )
1736                     + (( val[4*i+2] & 0xFF ) << 16 )
1737                     + (( val[4*i+3] & 0xFF ) << 24 );
1738             newInt32 = SwapLong( newInt32 );
1739             s << newInt32;
1740          }
1741       }
1742 #ifdef GDCM_NO_ANSI_STRING_STREAM
1743       s << std::ends; // to avoid oddities on Solaris
1744 #endif //GDCM_NO_ANSI_STRING_STREAM
1745       return s.str();
1746    }
1747
1748    return ((ValEntry *)entry)->GetValue();
1749 }
1750
1751 /**
1752  * \brief   Get the reverse transformed value of the header entry. The VR 
1753  *          value is used to define the reverse transformation to operate on
1754  *          the value
1755  * \warning NOT end user intended method !
1756  * @param   entry Entry to reverse transform
1757  * @return  Reverse transformed entry value
1758  */
1759 std::string Document::GetDocEntryUnvalue(DocEntry *entry)
1760 {
1761    if ( IsDocEntryAnInteger(entry) && entry->IsImplicitVR() )
1762    {
1763       std::string vr = entry->GetVR();
1764       std::vector<std::string> tokens;
1765       std::ostringstream s;
1766
1767       if ( vr == "US" || vr == "SS" ) 
1768       {
1769          uint16_t newInt16;
1770
1771          tokens.erase( tokens.begin(), tokens.end()); // clean any previous value
1772          Util::Tokenize (((ValEntry *)entry)->GetValue(), tokens, "\\");
1773          for (unsigned int i=0; i<tokens.size(); i++) 
1774          {
1775             newInt16 = atoi(tokens[i].c_str());
1776             s << (  newInt16        & 0xFF ) 
1777               << (( newInt16 >> 8 ) & 0xFF );
1778          }
1779          tokens.clear();
1780       }
1781       if ( vr == "UL" || vr == "SL")
1782       {
1783          uint32_t newInt32;
1784
1785          tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1786          Util::Tokenize (((ValEntry *)entry)->GetValue(), tokens, "\\");
1787          for (unsigned int i=0; i<tokens.size();i++) 
1788          {
1789             newInt32 = atoi(tokens[i].c_str());
1790             s << (char)(  newInt32         & 0xFF ) 
1791               << (char)(( newInt32 >>  8 ) & 0xFF )
1792               << (char)(( newInt32 >> 16 ) & 0xFF )
1793               << (char)(( newInt32 >> 24 ) & 0xFF );
1794          }
1795          tokens.clear();
1796       }
1797
1798 #ifdef GDCM_NO_ANSI_STRING_STREAM
1799       s << std::ends; // to avoid oddities on Solaris
1800 #endif //GDCM_NO_ANSI_STRING_STREAM
1801       return s.str();
1802    }
1803
1804    return ((ValEntry *)entry)->GetValue();
1805 }
1806
1807 /**
1808  * \brief   Skip a given Header Entry 
1809  * \warning NOT end user intended method !
1810  * @param   entry entry to skip
1811  */
1812 void Document::SkipDocEntry(DocEntry *entry) 
1813 {
1814    SkipBytes(entry->GetLength());
1815 }
1816
1817 /**
1818  * \brief   Skips to the begining of the next Header Entry 
1819  * \warning NOT end user intended method !
1820  * @param   currentDocEntry entry to skip
1821  */
1822 void Document::SkipToNextDocEntry(DocEntry *currentDocEntry) 
1823 {
1824    Fp->seekg((long)(currentDocEntry->GetOffset()),     std::ios::beg);
1825    Fp->seekg( (long)(currentDocEntry->GetReadLength()),std::ios::cur);
1826 }
1827
1828 /**
1829  * \brief   When the length of an element value is obviously wrong (because
1830  *          the parser went Jabberwocky) one can hope improving things by
1831  *          applying some heuristics.
1832  * @param   entry entry to check
1833  * @param   foundLength first assumption about length    
1834  */
1835 void Document::FixDocEntryFoundLength(DocEntry *entry,
1836                                       uint32_t foundLength)
1837 {
1838    entry->SetReadLength( foundLength ); // will be updated only if a bug is found        
1839    if ( foundLength == 0xffffffff)
1840    {
1841       foundLength = 0;
1842    }
1843    
1844    uint16_t gr   = entry->GetGroup();
1845    uint16_t elem = entry->GetElement(); 
1846      
1847    if ( foundLength % 2)
1848    {
1849       gdcmVerboseMacro( "Warning : Tag with uneven length " << foundLength 
1850         <<  " in x(" << std::hex << gr << "," << elem <<")");
1851    }
1852       
1853    //////// Fix for some naughty General Electric images.
1854    // Allthough not recent many such GE corrupted images are still present
1855    // on Creatis hard disks. Hence this fix shall remain when such images
1856    // are no longer in use (we are talking a few years, here)...
1857    // Note: XMedCom probably uses such a trick since it is able to read
1858    //       those pesky GE images ...
1859    if ( foundLength == 13)
1860    {
1861       // Only happens for this length !
1862       if ( gr != 0x0008 || ( elem != 0x0070 && elem != 0x0080 ) )
1863       {
1864          foundLength = 10;
1865          entry->SetReadLength(10); /// \todo a bug is to be fixed !?
1866       }
1867    }
1868
1869    //////// Fix for some brain-dead 'Leonardo' Siemens images.
1870    // Occurence of such images is quite low (unless one leaves close to a
1871    // 'Leonardo' source. Hence, one might consider commenting out the
1872    // following fix on efficiency reasons.
1873    else if ( gr   == 0x0009 && ( elem == 0x1113 || elem == 0x1114 ) )
1874    {
1875       foundLength = 4;
1876       entry->SetReadLength(4); /// \todo a bug is to be fixed !?
1877    } 
1878  
1879    else if ( entry->GetVR() == "SQ" )
1880    {
1881       foundLength = 0;      // ReadLength is unchanged 
1882    } 
1883     
1884    //////// We encountered a 'delimiter' element i.e. a tag of the form 
1885    // "fffe|xxxx" which is just a marker. Delimiters length should not be
1886    // taken into account.
1887    else if( gr == 0xfffe )
1888    {    
1889      // According to the norm, fffe|0000 shouldn't exist. BUT the Philips
1890      // image gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm happens to
1891      // causes extra troubles...
1892      if( entry->GetElement() != 0x0000 )
1893      {
1894         foundLength = 0;
1895      }
1896    } 
1897            
1898    entry->SetLength(foundLength);
1899 }
1900
1901 /**
1902  * \brief   Apply some heuristics to predict whether the considered 
1903  *          element value contains/represents an integer or not.
1904  * @param   entry The element value on which to apply the predicate.
1905  * @return  The result of the heuristical predicate.
1906  */
1907 bool Document::IsDocEntryAnInteger(DocEntry *entry)
1908 {
1909    uint16_t elem    = entry->GetElement();
1910    uint16_t group   = entry->GetGroup();
1911    const std::string &vr  = entry->GetVR();
1912    uint32_t length  = entry->GetLength();
1913
1914    // When we have some semantics on the element we just read, and if we
1915    // a priori know we are dealing with an integer, then we shall be
1916    // able to swap it's element value properly.
1917    if ( elem == 0 )  // This is the group length of the group
1918    {  
1919       if ( length == 4 )
1920       {
1921          return true;
1922       }
1923       else 
1924       {
1925          // Allthough this should never happen, still some images have a
1926          // corrupted group length [e.g. have a glance at offset x(8336) of
1927          // gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm].
1928          // Since for dicom compliant and well behaved headers, the present
1929          // test is useless (and might even look a bit paranoid), when we
1930          // encounter such an ill-formed image, we simply display a warning
1931          // message and proceed on parsing (while crossing fingers).
1932          long filePosition = Fp->tellg();
1933          gdcmVerboseMacro( "Erroneous Group Length element length  on : (" 
1934            << std::hex << group << " , " << elem
1935            << ") -before- position x(" << filePosition << ")"
1936            << "lgt : " << length );
1937       }
1938    }
1939
1940    if ( vr == "UL" || vr == "US" || vr == "SL" || vr == "SS" )
1941    {
1942       return true;
1943    }   
1944    return false;
1945 }
1946
1947 /**
1948  * \brief  Find the Length till the next sequence delimiter
1949  * \warning NOT end user intended method !
1950  * @return 
1951  */
1952
1953 uint32_t Document::FindDocEntryLengthOBOrOW()
1954    throw( FormatUnexpected )
1955 {
1956    // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
1957    long positionOnEntry = Fp->tellg();
1958    bool foundSequenceDelimiter = false;
1959    uint32_t totalLength = 0;
1960
1961    while ( !foundSequenceDelimiter )
1962    {
1963       uint16_t group;
1964       uint16_t elem;
1965       try
1966       {
1967          group = ReadInt16();
1968          elem  = ReadInt16();   
1969       }
1970       catch ( FormatError )
1971       {
1972          throw FormatError("Unexpected end of file encountered during ",
1973                            "Document::FindDocEntryLengthOBOrOW()");
1974       }
1975
1976       // We have to decount the group and element we just read
1977       totalLength += 4;
1978      
1979       if ( group != 0xfffe || ( ( elem != 0xe0dd ) && ( elem != 0xe000 ) ) )
1980       {
1981          long filePosition = Fp->tellg();
1982          gdcmVerboseMacro( "Neither an Item tag nor a Sequence delimiter tag on :" 
1983            << std::hex << group << " , " << elem 
1984            << ") -before- position x(" << filePosition << ")" );
1985   
1986          Fp->seekg(positionOnEntry, std::ios::beg);
1987          throw FormatUnexpected( "Neither an Item tag nor a Sequence delimiter tag.");
1988       }
1989
1990       if ( elem == 0xe0dd )
1991       {
1992          foundSequenceDelimiter = true;
1993       }
1994
1995       uint32_t itemLength = ReadInt32();
1996       // We add 4 bytes since we just read the ItemLength with ReadInt32
1997       totalLength += itemLength + 4;
1998       SkipBytes(itemLength);
1999       
2000       if ( foundSequenceDelimiter )
2001       {
2002          break;
2003       }
2004    }
2005    Fp->seekg( positionOnEntry, std::ios::beg);
2006    return totalLength;
2007 }
2008
2009 /**
2010  * \brief Reads a supposed to be 16 Bits integer
2011  *       (swaps it depending on processor endianity) 
2012  * @return read value
2013  */
2014 uint16_t Document::ReadInt16()
2015    throw( FormatError )
2016 {
2017    uint16_t g;
2018    Fp->read ((char*)&g, (size_t)2);
2019    if ( Fp->fail() )
2020    {
2021       throw FormatError( "Document::ReadInt16()", " file error." );
2022    }
2023    if( Fp->eof() )
2024    {
2025       throw FormatError( "Document::ReadInt16()", "EOF." );
2026    }
2027    g = SwapShort(g); 
2028    return g;
2029 }
2030
2031 /**
2032  * \brief  Reads a supposed to be 32 Bits integer
2033  *         (swaps it depending on processor endianity)  
2034  * @return read value
2035  */
2036 uint32_t Document::ReadInt32()
2037    throw( FormatError )
2038 {
2039    uint32_t g;
2040    Fp->read ((char*)&g, (size_t)4);
2041    if ( Fp->fail() )
2042    {
2043       throw FormatError( "Document::ReadInt32()", " file error." );
2044    }
2045    if( Fp->eof() )
2046    {
2047       throw FormatError( "Document::ReadInt32()", "EOF." );
2048    }
2049    g = SwapLong(g);
2050    return g;
2051 }
2052
2053 /**
2054  * \brief skips bytes inside the source file 
2055  * \warning NOT end user intended method !
2056  * @return 
2057  */
2058 void Document::SkipBytes(uint32_t nBytes)
2059 {
2060    //FIXME don't dump the returned value
2061    Fp->seekg((long)nBytes, std::ios::cur);
2062 }
2063
2064 /**
2065  * \brief Loads all the needed Dictionaries
2066  * \warning NOT end user intended method !   
2067  */
2068 void Document::Initialise() 
2069 {
2070    RefPubDict = Global::GetDicts()->GetDefaultPubDict();
2071    RefShaDict = NULL;
2072    RLEInfo  = new RLEFramesInfo;
2073    JPEGInfo = new JPEGFragmentsInfo;
2074    Filetype = Unknown;
2075 }
2076
2077 /**
2078  * \brief   Discover what the swap code is (among little endian, big endian,
2079  *          bad little endian, bad big endian).
2080  *          sw is set
2081  * @return false when we are absolutely sure 
2082  *               it's neither ACR-NEMA nor DICOM
2083  *         true  when we hope ours assuptions are OK
2084  */
2085 bool Document::CheckSwap()
2086 {
2087    // The only guaranted way of finding the swap code is to find a
2088    // group tag since we know it's length has to be of four bytes i.e.
2089    // 0x00000004. Finding the swap code in then straigthforward. Trouble
2090    // occurs when we can't find such group...
2091    
2092    uint32_t  x = 4;  // x : for ntohs
2093    bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
2094    uint32_t  s32;
2095    uint16_t  s16;
2096        
2097    char deb[256];
2098     
2099    // First, compare HostByteOrder and NetworkByteOrder in order to
2100    // determine if we shall need to swap bytes (i.e. the Endian type).
2101    if ( x == ntohs(x) )
2102    {
2103       net2host = true;
2104    }
2105    else
2106    {
2107       net2host = false;
2108    }
2109          
2110    // The easiest case is the one of a DICOM header, since it possesses a
2111    // file preamble where it suffice to look for the string "DICM".
2112    Fp->read(deb, 256);
2113    
2114    char *entCur = deb + 128;
2115    if( memcmp(entCur, "DICM", (size_t)4) == 0 )
2116    {
2117       gdcmVerboseMacro( "Looks like DICOM Version3" );
2118       
2119       // Next, determine the value representation (VR). Let's skip to the
2120       // first element (0002, 0000) and check there if we find "UL" 
2121       // - or "OB" if the 1st one is (0002,0001) -,
2122       // in which case we (almost) know it is explicit VR.
2123       // WARNING: if it happens to be implicit VR then what we will read
2124       // is the length of the group. If this ascii representation of this
2125       // length happens to be "UL" then we shall believe it is explicit VR.
2126       // FIXME: in order to fix the above warning, we could read the next
2127       // element value (or a couple of elements values) in order to make
2128       // sure we are not commiting a big mistake.
2129       // We need to skip :
2130       // * the 128 bytes of File Preamble (often padded with zeroes),
2131       // * the 4 bytes of "DICM" string,
2132       // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
2133       // i.e. a total of  136 bytes.
2134       entCur = deb + 136;
2135      
2136       // FIXME : FIXME:
2137       // Sometimes (see : gdcmData/icone.dcm) group 0x0002 *is* Explicit VR,
2138       // but elem 0002,0010 (Transfer Syntax) tells us the file is
2139       // *Implicit* VR.  -and it is !- 
2140       
2141       if( memcmp(entCur, "UL", (size_t)2) == 0 ||
2142           memcmp(entCur, "OB", (size_t)2) == 0 ||
2143           memcmp(entCur, "UI", (size_t)2) == 0 ||
2144           memcmp(entCur, "CS", (size_t)2) == 0 )  // CS, to remove later
2145                                                     // when Write DCM *adds*
2146       // FIXME
2147       // Use Document::dicom_vr to test all the possibilities
2148       // instead of just checking for UL, OB and UI !? group 0000 
2149       {
2150          Filetype = ExplicitVR;
2151          gdcmVerboseMacro( "Explicit Value Representation");
2152       } 
2153       else 
2154       {
2155          Filetype = ImplicitVR;
2156          gdcmVerboseMacro( "Not an explicit Value Representation");
2157       }
2158       
2159       if ( net2host )
2160       {
2161          SwapCode = 4321;
2162          gdcmVerboseMacro( "HostByteOrder != NetworkByteOrder");
2163       }
2164       else 
2165       {
2166          SwapCode = 0;
2167          gdcmVerboseMacro( "HostByteOrder = NetworkByteOrder");
2168       }
2169       
2170       // Position the file position indicator at first tag (i.e.
2171       // after the file preamble and the "DICM" string).
2172       Fp->seekg(0, std::ios::beg);
2173       Fp->seekg ( 132L, std::ios::beg);
2174       return true;
2175    } // End of DicomV3
2176
2177    // Alas, this is not a DicomV3 file and whatever happens there is no file
2178    // preamble. We can reset the file position indicator to where the data
2179    // is (i.e. the beginning of the file).
2180    gdcmVerboseMacro( "Not a DICOM Version3 file");
2181    Fp->seekg(0, std::ios::beg);
2182
2183    // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
2184    // By clean we mean that the length of the first tag is written down.
2185    // If this is the case and since the length of the first group HAS to be
2186    // four (bytes), then determining the proper swap code is straightforward.
2187
2188    entCur = deb + 4;
2189    // We assume the array of char we are considering contains the binary
2190    // representation of a 32 bits integer. Hence the following dirty
2191    // trick :
2192    s32 = *((uint32_t *)(entCur));
2193
2194    switch( s32 )
2195    {
2196       case 0x00040000 :
2197          SwapCode = 3412;
2198          Filetype = ACR;
2199          return true;
2200       case 0x04000000 :
2201          SwapCode = 4321;
2202          Filetype = ACR;
2203          return true;
2204       case 0x00000400 :
2205          SwapCode = 2143;
2206          Filetype = ACR;
2207          return true;
2208       case 0x00000004 :
2209          SwapCode = 0;
2210          Filetype = ACR;
2211          return true;
2212       default :
2213          // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
2214          // It is time for despaired wild guesses. 
2215          // So, let's check if this file wouldn't happen to be 'dirty' ACR/NEMA,
2216          //  i.e. the 'group length' element is not present :     
2217          
2218          //  check the supposed-to-be 'group number'
2219          //  in ( 0x0001 .. 0x0008 )
2220          //  to determine ' SwapCode' value .
2221          //  Only 0 or 4321 will be possible 
2222          //  (no oportunity to check for the formerly well known
2223          //  ACR-NEMA 'Bad Big Endian' or 'Bad Little Endian' 
2224          //  if unsuccessfull (i.e. neither 0x0002 nor 0x0200 etc -3, 4, ..., 8-) 
2225          //  the file IS NOT ACR-NEMA nor DICOM V3
2226          //  Find a trick to tell it the caller...
2227       
2228          s16 = *((uint16_t *)(deb));
2229       
2230          switch ( s16 )
2231          {
2232             case 0x0001 :
2233             case 0x0002 :
2234             case 0x0003 :
2235             case 0x0004 :
2236             case 0x0005 :
2237             case 0x0006 :
2238             case 0x0007 :
2239             case 0x0008 :
2240                SwapCode = 0;
2241                Filetype = ACR;
2242                return true;
2243             case 0x0100 :
2244             case 0x0200 :
2245             case 0x0300 :
2246             case 0x0400 :
2247             case 0x0500 :
2248             case 0x0600 :
2249             case 0x0700 :
2250             case 0x0800 :
2251                SwapCode = 4321;
2252                Filetype = ACR;
2253                return true;
2254             default :
2255                gdcmVerboseMacro( "ACR/NEMA unfound swap info (Really hopeless !)");
2256                Filetype = Unknown;
2257                return false;
2258          }
2259          // Then the only info we have is the net2host one.
2260          //if (! net2host )
2261          //   SwapCode = 0;
2262          //else
2263          //  SwapCode = 4321;
2264          //return;
2265    }
2266 }
2267
2268
2269
2270 /**
2271  * \brief Change the Byte Swap code. 
2272  */
2273 void Document::SwitchByteSwapCode() 
2274 {
2275    gdcmVerboseMacro( "Switching Byte Swap code.");
2276    if ( SwapCode == 0    ) 
2277    {
2278       SwapCode = 4321;
2279    }
2280    else if ( SwapCode == 4321 ) 
2281    {
2282       SwapCode = 0;
2283    }
2284    else if ( SwapCode == 3412 ) 
2285    {
2286       SwapCode = 2143;
2287    }
2288    else if ( SwapCode == 2143 )
2289    {
2290       SwapCode = 3412;
2291    }
2292 }
2293
2294 /**
2295  * \brief  during parsing, Header Elements too long are not loaded in memory 
2296  * @param newSize
2297  */
2298 void Document::SetMaxSizeLoadEntry(long newSize) 
2299 {
2300    if ( newSize < 0 )
2301    {
2302       return;
2303    }
2304    if ((uint32_t)newSize >= (uint32_t)0xffffffff )
2305    {
2306       MaxSizeLoadEntry = 0xffffffff;
2307       return;
2308    }
2309    MaxSizeLoadEntry = newSize;
2310 }
2311
2312
2313 /**
2314  * \brief Header Elements too long will not be printed
2315  * \todo  See comments of \ref Document::MAX_SIZE_PRINT_ELEMENT_VALUE 
2316  * @param newSize
2317  */
2318 void Document::SetMaxSizePrintEntry(long newSize) 
2319 {
2320    //DOH !! This is exactly SetMaxSizeLoadEntry FIXME FIXME
2321    if ( newSize < 0 )
2322    {
2323       return;
2324    }
2325    if ((uint32_t)newSize >= (uint32_t)0xffffffff )
2326    {
2327       MaxSizePrintEntry = 0xffffffff;
2328       return;
2329    }
2330    MaxSizePrintEntry = newSize;
2331 }
2332
2333
2334
2335 /**
2336  * \brief   Handle broken private tag from Philips NTSCAN
2337  *          where the endianess is being switch to BigEndian for no
2338  *          apparent reason
2339  * @return  no return
2340  */
2341 void Document::HandleBrokenEndian(uint16_t group, uint16_t elem)
2342 {
2343    // Endian reversion. Some files contain groups of tags with reversed endianess.
2344    static int reversedEndian = 0;
2345    // try to fix endian switching in the middle of headers
2346    if ((group == 0xfeff) && (elem == 0x00e0))
2347    {
2348      // start endian swap mark for group found
2349      reversedEndian++;
2350      SwitchByteSwapCode();
2351      // fix the tag
2352      group = 0xfffe;
2353      elem = 0xe000;
2354    } 
2355    else if (group == 0xfffe && elem == 0xe00d && reversedEndian) 
2356    {
2357      // end of reversed endian group
2358      reversedEndian--;
2359      SwitchByteSwapCode();
2360    }
2361 }
2362
2363 /**
2364  * \brief   Group 0002 is always coded Little Endian
2365  *          whatever Transfer Syntax is
2366  * @return  no return
2367  */
2368 void Document::HandleOutOfGroup0002(uint16_t group)
2369 {
2370    // Endian reversion. Some files contain groups of tags with reversed endianess.
2371    if ( !Group0002Parsed && group != 0x0002)
2372    {
2373       Group0002Parsed = true;
2374      // we just came out of group 0002
2375      // if Transfer syntax is Big Endian we have to change CheckSwap
2376
2377       TagKey key = DictEntry::TranslateToKey(0x0002, 0x0010);
2378       if ( !TagHT.count(key))
2379       {
2380          gdcmVerboseMacro("True DICOM File, with NO Tansfer Syntax ?!?");
2381          return;
2382       }
2383
2384    // FIXME Strangely, this works with 
2385    //'Implicit VR Transfer Syntax (GE Private)
2386
2387        if ( ((ValEntry *)TagHT.find(key)->second)->GetValue()
2388                == "Explicit VR - Big Endian" )
2389        {
2390           gdcmVerboseMacro("Tansfer Syntax = Explicit VR - Big Endian");
2391           SwitchByteSwapCode();
2392         }
2393    }
2394 }
2395
2396 /**
2397  * \brief   Read the next tag but WITHOUT loading it's value
2398  *          (read the 'Group Number', the 'Element Number',
2399  *          gets the Dict Entry
2400  *          gets the VR, gets the length, gets the offset value)
2401  * @return  On succes the newly created DocEntry, NULL on failure.      
2402  */
2403 DocEntry *Document::ReadNextDocEntry()
2404 {
2405    uint16_t group;
2406    uint16_t elem;
2407
2408    try
2409    {
2410       group = ReadInt16();
2411       elem  = ReadInt16();
2412    }
2413    catch ( FormatError e )
2414    {
2415       // We reached the EOF (or an error occured) therefore 
2416       // header parsing has to be considered as finished.
2417       //std::cout << e;
2418       return 0;
2419    }
2420
2421    // Sometimes file contains groups of tags with reversed endianess.
2422    HandleBrokenEndian(group, elem);
2423
2424 // In 'true DICOM' files Group 0002 is allways little endian
2425    if ( HasDCMPreamble )
2426       HandleOutOfGroup0002(group);
2427  
2428    std::string vr = FindDocEntryVR();
2429    std::string realVR = vr;
2430
2431    if( vr == GDCM_UNKNOWN)
2432    {
2433       DictEntry *dictEntry = GetDictEntry(group,elem);
2434       if( dictEntry )
2435          realVR = dictEntry->GetVR();
2436    }
2437
2438    DocEntry *newEntry;
2439    if( Global::GetVR()->IsVROfSequence(realVR) )
2440       newEntry = NewSeqEntry(group, elem);
2441    else if( Global::GetVR()->IsVROfStringRepresentable(realVR) )
2442       newEntry = NewValEntry(group, elem,vr);
2443    else
2444       newEntry = NewBinEntry(group, elem,vr);
2445
2446    if( vr == GDCM_UNKNOWN )
2447    {
2448       if( Filetype == ExplicitVR )
2449       {
2450          // We thought this was explicit VR, but we end up with an
2451          // implicit VR tag. Let's backtrack.   
2452          std::string msg;
2453          msg = Util::Format("Falsely explicit vr file (%04x,%04x)\n", 
2454                        newEntry->GetGroup(), newEntry->GetElement());
2455          gdcmVerboseMacro( msg.c_str() );
2456       }
2457       newEntry->SetImplicitVR();
2458    }
2459
2460    try
2461    {
2462       FindDocEntryLength(newEntry);
2463    }
2464    catch ( FormatError e )
2465    {
2466       // Call it quits
2467       //std::cout << e;
2468       delete newEntry;
2469       return 0;
2470    }
2471
2472    newEntry->SetOffset(Fp->tellg());  
2473
2474    return newEntry;
2475 }
2476
2477
2478 /**
2479  * \brief   Generate a free TagKey i.e. a TagKey that is not present
2480  *          in the TagHt dictionary.
2481  * @param   group The generated tag must belong to this group.  
2482  * @return  The element of tag with given group which is fee.
2483  */
2484 uint32_t Document::GenerateFreeTagKeyInGroup(uint16_t group) 
2485 {
2486    for (uint32_t elem = 0; elem < UINT32_MAX; elem++) 
2487    {
2488       TagKey key = DictEntry::TranslateToKey(group, elem);
2489       if (TagHT.count(key) == 0)
2490       {
2491          return elem;
2492       }
2493    }
2494    return UINT32_MAX;
2495 }
2496
2497 /**
2498  * \brief   Assuming the internal file pointer \ref Document::Fp 
2499  *          is placed at the beginning of a tag check whether this
2500  *          tag is (TestGroup, TestElement).
2501  * \warning On success the internal file pointer \ref Document::Fp
2502  *          is modified to point after the tag.
2503  *          On failure (i.e. when the tag wasn't the expected tag
2504  *          (TestGroup, TestElement) the internal file pointer
2505  *          \ref Document::Fp is restored to it's original position.
2506  * @param   testGroup   The expected group of the tag.
2507  * @param   testElement The expected Element of the tag.
2508  * @return  True on success, false otherwise.
2509  */
2510 bool Document::ReadTag(uint16_t testGroup, uint16_t testElement)
2511 {
2512    long positionOnEntry = Fp->tellg();
2513    long currentPosition = Fp->tellg();          // On debugging purposes
2514
2515    //// Read the Item Tag group and element, and make
2516    // sure they are what we expected:
2517    uint16_t itemTagGroup;
2518    uint16_t itemTagElement;
2519    try
2520    {
2521       itemTagGroup   = ReadInt16();
2522       itemTagElement = ReadInt16();
2523    }
2524    catch ( FormatError e )
2525    {
2526       //std::cerr << e << std::endl;
2527       return false;
2528    }
2529    if ( itemTagGroup != testGroup || itemTagElement != testElement )
2530    {
2531       gdcmVerboseMacro( "Wrong Item Tag found:"
2532        << "   We should have found tag ("
2533        << std::hex << testGroup << "," << testElement << ")" << std::endl
2534        << "   but instead we encountered tag ("
2535        << std::hex << itemTagGroup << "," << itemTagElement << ")"
2536        << std::dec
2537        << "  at address: " << (unsigned int)currentPosition 
2538        << std::hex 
2539        << "  0x(" << (unsigned int)currentPosition  << ")" 
2540        ) ;
2541       Fp->seekg(positionOnEntry, std::ios::beg);
2542
2543       return false;
2544    }
2545    return true;
2546 }
2547
2548 /**
2549  * \brief   Assuming the internal file pointer \ref Document::Fp 
2550  *          is placed at the beginning of a tag (TestGroup, TestElement),
2551  *          read the length associated to the Tag.
2552  * \warning On success the internal file pointer \ref Document::Fp
2553  *          is modified to point after the tag and it's length.
2554  *          On failure (i.e. when the tag wasn't the expected tag
2555  *          (TestGroup, TestElement) the internal file pointer
2556  *          \ref Document::Fp is restored to it's original position.
2557  * @param   testGroup   The expected group of the tag.
2558  * @param   testElement The expected Element of the tag.
2559  * @return  On success returns the length associated to the tag. On failure
2560  *          returns 0.
2561  */
2562 uint32_t Document::ReadTagLength(uint16_t testGroup, uint16_t testElement)
2563 {
2564    long positionOnEntry = Fp->tellg();
2565    (void)positionOnEntry;
2566
2567    if ( !ReadTag(testGroup, testElement) )
2568    {
2569       return 0;
2570    }
2571                                                                                 
2572    //// Then read the associated Item Length
2573    long currentPosition = Fp->tellg();
2574    uint32_t itemLength  = ReadInt32();
2575    {
2576       gdcmVerboseMacro( "Basic Item Length is: "
2577         << itemLength << std::endl
2578         << "  at address: " << (unsigned int)currentPosition);
2579    }
2580    return itemLength;
2581 }
2582
2583 /**
2584  * \brief When parsing the Pixel Data of an encapsulated file, read
2585  *        the basic offset table (when present, and BTW dump it).
2586  */
2587 void Document::ReadAndSkipEncapsulatedBasicOffsetTable()
2588 {
2589    //// Read the Basic Offset Table Item Tag length...
2590    uint32_t itemLength = ReadTagLength(0xfffe, 0xe000);
2591
2592    // When present, read the basic offset table itself.
2593    // Notes: - since the presence of this basic offset table is optional
2594    //          we can't rely on it for the implementation, and we will simply
2595    //          trash it's content (when present).
2596    //        - still, when present, we could add some further checks on the
2597    //          lengths, but we won't bother with such fuses for the time being.
2598    if ( itemLength != 0 )
2599    {
2600       char *basicOffsetTableItemValue = new char[itemLength + 1];
2601       Fp->read(basicOffsetTableItemValue, itemLength);
2602
2603 #ifdef GDCM_DEBUG
2604       for (unsigned int i=0; i < itemLength; i += 4 )
2605       {
2606          uint32_t individualLength = str2num( &basicOffsetTableItemValue[i],
2607                                               uint32_t);
2608          gdcmVerboseMacro( "Read one length: " << 
2609                           std::hex << individualLength );
2610       }
2611 #endif //GDCM_DEBUG
2612
2613       delete[] basicOffsetTableItemValue;
2614    }
2615 }
2616
2617 /**
2618  * \brief Parse pixel data from disk of [multi-]fragment RLE encoding.
2619  *        Compute the RLE extra information and store it in \ref RLEInfo
2620  *        for later pixel retrieval usage.
2621  */
2622 void Document::ComputeRLEInfo()
2623 {
2624    std::string ts = GetTransferSyntax();
2625    if ( Global::GetTS()->IsRLELossless(ts) ) 
2626    {
2627       return;
2628    }
2629
2630    // Encoded pixel data: for the time being we are only concerned with
2631    // Jpeg or RLE Pixel data encodings.
2632    // As stated in PS 3.5-2003, section 8.2 p44:
2633    // "If sent in Encapsulated Format (i.e. other than the Native Format) the
2634    //  value representation OB is used".
2635    // Hence we expect an OB value representation. Concerning OB VR,
2636    // the section PS 3.5-2003, section A.4.c p 58-59, states:
2637    // "For the Value Representations OB and OW, the encoding shall meet the
2638    //   following specifications depending on the Data element tag:"
2639    //   [...snip...]
2640    //    - the first item in the sequence of items before the encoded pixel
2641    //      data stream shall be basic offset table item. The basic offset table
2642    //      item value, however, is not required to be present"
2643
2644    ReadAndSkipEncapsulatedBasicOffsetTable();
2645
2646    // Encapsulated RLE Compressed Images (see PS 3.5-2003, Annex G)
2647    // Loop on the individual frame[s] and store the information
2648    // on the RLE fragments in a RLEFramesInfo.
2649    // Note: - when only a single frame is present, this is a
2650    //         classical image.
2651    //       - when more than one frame are present, then we are in 
2652    //         the case of a multi-frame image.
2653    long frameLength;
2654    while ( (frameLength = ReadTagLength(0xfffe, 0xe000)) )
2655    { 
2656       // Parse the RLE Header and store the corresponding RLE Segment
2657       // Offset Table information on fragments of this current Frame.
2658       // Note that the fragment pixels themselves are not loaded
2659       // (but just skipped).
2660       long frameOffset = Fp->tellg();
2661
2662       uint32_t nbRleSegments = ReadInt32();
2663       if ( nbRleSegments > 16 )
2664       {
2665          // There should be at most 15 segments (refer to RLEFrame class)
2666          gdcmVerboseMacro( "Too many segments.");
2667       }
2668  
2669       uint32_t rleSegmentOffsetTable[16];
2670       for( int k = 1; k <= 15; k++ )
2671       {
2672          rleSegmentOffsetTable[k] = ReadInt32();
2673       }
2674
2675       // Deduce from both the RLE Header and the frameLength the
2676       // fragment length, and again store this info in a
2677       // RLEFramesInfo.
2678       long rleSegmentLength[15];
2679       // skipping (not reading) RLE Segments
2680       if ( nbRleSegments > 1)
2681       {
2682          for(unsigned int k = 1; k <= nbRleSegments-1; k++)
2683          {
2684              rleSegmentLength[k] =  rleSegmentOffsetTable[k+1]
2685                                   - rleSegmentOffsetTable[k];
2686              SkipBytes(rleSegmentLength[k]);
2687           }
2688        }
2689
2690        rleSegmentLength[nbRleSegments] = frameLength 
2691                                       - rleSegmentOffsetTable[nbRleSegments];
2692        SkipBytes(rleSegmentLength[nbRleSegments]);
2693
2694        // Store the collected info
2695        RLEFrame *newFrameInfo = new RLEFrame;
2696        newFrameInfo->NumberFragments = nbRleSegments;
2697        for( unsigned int uk = 1; uk <= nbRleSegments; uk++ )
2698        {
2699           newFrameInfo->Offset[uk] = frameOffset + rleSegmentOffsetTable[uk];
2700           newFrameInfo->Length[uk] = rleSegmentLength[uk];
2701        }
2702        RLEInfo->Frames.push_back( newFrameInfo );
2703    }
2704
2705    // Make sure that at the end of the item we encounter a 'Sequence
2706    // Delimiter Item':
2707    if ( !ReadTag(0xfffe, 0xe0dd) )
2708    {
2709       gdcmVerboseMacro( "No sequence delimiter item at end of RLE item sequence");
2710    }
2711 }
2712
2713 /**
2714  * \brief Parse pixel data from disk of [multi-]fragment Jpeg encoding.
2715  *        Compute the jpeg extra information (fragment[s] offset[s] and
2716  *        length) and store it[them] in \ref JPEGInfo for later pixel
2717  *        retrieval usage.
2718  */
2719 void Document::ComputeJPEGFragmentInfo()
2720 {
2721    // If you need to, look for comments of ComputeRLEInfo().
2722    std::string ts = GetTransferSyntax();
2723    if ( ! Global::GetTS()->IsJPEG(ts) )
2724    {
2725       return;
2726    }
2727
2728    ReadAndSkipEncapsulatedBasicOffsetTable();
2729
2730    // Loop on the fragments[s] and store the parsed information in a
2731    // JPEGInfo.
2732    long fragmentLength;
2733    while ( (fragmentLength = ReadTagLength(0xfffe, 0xe000)) )
2734    { 
2735       long fragmentOffset = Fp->tellg();
2736
2737        // Store the collected info
2738        JPEGFragment *newFragment = new JPEGFragment;
2739        newFragment->Offset = fragmentOffset;
2740        newFragment->Length = fragmentLength;
2741        JPEGInfo->Fragments.push_back( newFragment );
2742
2743        SkipBytes( fragmentLength );
2744    }
2745
2746    // Make sure that at the end of the item we encounter a 'Sequence
2747    // Delimiter Item':
2748    if ( !ReadTag(0xfffe, 0xe0dd) )
2749    {
2750       gdcmVerboseMacro( "No sequence delimiter item at end of JPEG item sequence");
2751    }
2752 }
2753
2754 /**
2755  * \brief Walk recursively the given \ref DocEntrySet, and feed
2756  *        the given hash table (\ref TagDocEntryHT) with all the
2757  *        \ref DocEntry (Dicom entries) encountered.
2758  *        This method does the job for \ref BuildFlatHashTable.
2759  * @param builtHT Where to collect all the \ref DocEntry encountered
2760  *        when recursively walking the given set.
2761  * @param set The structure to be traversed (recursively).
2762  */
2763 void Document::BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
2764                                           DocEntrySet *set )
2765
2766    if (ElementSet *elementSet = dynamic_cast< ElementSet* > ( set ) )
2767    {
2768       TagDocEntryHT const &currentHT = elementSet->GetTagHT();
2769       for( TagDocEntryHT::const_iterator i  = currentHT.begin();
2770                                          i != currentHT.end();
2771                                        ++i)
2772       {
2773          DocEntry *entry = i->second;
2774          if ( SeqEntry *seqEntry = dynamic_cast<SeqEntry*>(entry) )
2775          {
2776             const ListSQItem& items = seqEntry->GetSQItems();
2777             for( ListSQItem::const_iterator item  = items.begin();
2778                                             item != items.end();
2779                                           ++item)
2780             {
2781                BuildFlatHashTableRecurse( builtHT, *item );
2782             }
2783             continue;
2784          }
2785          builtHT[entry->GetKey()] = entry;
2786       }
2787       return;
2788     }
2789
2790    if (SQItem *SQItemSet = dynamic_cast< SQItem* > ( set ) )
2791    {
2792       const ListDocEntry& currentList = SQItemSet->GetDocEntries();
2793       for (ListDocEntry::const_iterator i  = currentList.begin();
2794                                         i != currentList.end();
2795                                       ++i)
2796       {
2797          DocEntry *entry = *i;
2798          if ( SeqEntry *seqEntry = dynamic_cast<SeqEntry*>(entry) )
2799          {
2800             const ListSQItem& items = seqEntry->GetSQItems();
2801             for( ListSQItem::const_iterator item  = items.begin();
2802                                             item != items.end();
2803                                           ++item)
2804             {
2805                BuildFlatHashTableRecurse( builtHT, *item );
2806             }
2807             continue;
2808          }
2809          builtHT[entry->GetKey()] = entry;
2810       }
2811
2812    }
2813 }
2814
2815 /**
2816  * \brief Build a \ref TagDocEntryHT (i.e. a std::map<>) from the current
2817  *        Document.
2818  *
2819  *        The structure used by a Document (through \ref ElementSet),
2820  *        in order to hold the parsed entries of a Dicom header, is a recursive
2821  *        one. This is due to the fact that the sequences (when present)
2822  *        can be nested. Additionaly, the sequence items (represented in
2823  *        gdcm as \ref SQItem) add an extra complexity to the data
2824  *        structure. Hence, a gdcm user whishing to visit all the entries of
2825  *        a Dicom header will need to dig in the gdcm internals (which
2826  *        implies exposing all the internal data structures to the API).
2827  *        In order to avoid this burden to the user, \ref BuildFlatHashTable
2828  *        recursively builds a temporary hash table, which holds all the
2829  *        Dicom entries in a flat structure (a \ref TagDocEntryHT i.e. a
2830  *        std::map<>).
2831  * \warning Of course there is NO integrity constrain between the 
2832  *        returned \ref TagDocEntryHT and the \ref ElementSet used
2833  *        to build it. Hence if the underlying \ref ElementSet is
2834  *        altered, then it is the caller responsability to invoke 
2835  *        \ref BuildFlatHashTable again...
2836  * @return The flat std::map<> we juste build.
2837  */
2838 TagDocEntryHT *Document::BuildFlatHashTable()
2839 {
2840    TagDocEntryHT *FlatHT = new TagDocEntryHT;
2841    BuildFlatHashTableRecurse( *FlatHT, this );
2842    return FlatHT;
2843 }
2844
2845
2846
2847 /**
2848  * \brief   Compares two documents, according to \ref DicomDir rules
2849  * \warning Does NOT work with ACR-NEMA files
2850  * \todo    Find a trick to solve the pb (use RET fields ?)
2851  * @param   document
2852  * @return  true if 'smaller'
2853  */
2854 bool Document::operator<(Document &document)
2855 {
2856    // Patient Name
2857    std::string s1 = GetEntry(0x0010,0x0010);
2858    std::string s2 = document.GetEntry(0x0010,0x0010);
2859    if(s1 < s2)
2860    {
2861       return true;
2862    }
2863    else if( s1 > s2 )
2864    {
2865       return false;
2866    }
2867    else
2868    {
2869       // Patient ID
2870       s1 = GetEntry(0x0010,0x0020);
2871       s2 = document.GetEntry(0x0010,0x0020);
2872       if ( s1 < s2 )
2873       {
2874          return true;
2875       }
2876       else if ( s1 > s2 )
2877       {
2878          return false;
2879       }
2880       else
2881       {
2882          // Study Instance UID
2883          s1 = GetEntry(0x0020,0x000d);
2884          s2 = document.GetEntry(0x0020,0x000d);
2885          if ( s1 < s2 )
2886          {
2887             return true;
2888          }
2889          else if( s1 > s2 )
2890          {
2891             return false;
2892          }
2893          else
2894          {
2895             // Serie Instance UID
2896             s1 = GetEntry(0x0020,0x000e);
2897             s2 = document.GetEntry(0x0020,0x000e);    
2898             if ( s1 < s2 )
2899             {
2900                return true;
2901             }
2902             else if( s1 > s2 )
2903             {
2904                return false;
2905             }
2906          }
2907       }
2908    }
2909    return false;
2910 }
2911
2912
2913 /**
2914  * \brief   Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
2915  * @param filetype Type of the File to be written 
2916  */
2917 int Document::ComputeGroup0002Length( FileType filetype ) 
2918 {
2919    uint16_t gr, el;
2920    std::string vr;
2921    
2922    int groupLength = 0;
2923    bool found0002 = false;   
2924   
2925    // for each zero-level Tag in the DCM Header
2926    DocEntry *entry;
2927
2928    Initialize();
2929    entry = GetNextEntry();
2930    while(entry)
2931    {
2932       gr = entry->GetGroup();
2933
2934       if (gr == 0x0002)
2935       {
2936          found0002 = true;
2937
2938          el = entry->GetElement();
2939          vr = entry->GetVR();            
2940  
2941          if (filetype == ExplicitVR) 
2942          {
2943             if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) 
2944             {
2945                groupLength +=  4; // explicit VR AND OB, OW, SQ : 4 more bytes
2946             }
2947          }
2948          groupLength += 2 + 2 + 4 + entry->GetLength();   
2949       }
2950       else if (found0002 )
2951          break;
2952
2953       entry = GetNextEntry();
2954    }
2955    return groupLength; 
2956 }
2957
2958 } // end namespace gdcm
2959
2960 //-----------------------------------------------------------------------------