]> Creatis software - gdcm.git/blob - src/gdcmParser.cxx
a29b042013d5b7f20e5c9e552323b418327094bf
[gdcm.git] / src / gdcmParser.cxx
1 // gdcmParser.cxx
2 //-----------------------------------------------------------------------------
3 //#define UINT32_MAX    (4294967295U)
4
5
6 #include "gdcmParser.h"
7 #include "gdcmGlobal.h"
8 #include "gdcmUtil.h"
9 #include "gdcmDebug.h"
10
11 #include <errno.h>
12 #include <vector>
13
14 // For nthos:
15 #ifdef _MSC_VER
16    #include <winsock.h>
17 #else
18    #include <netinet/in.h>
19 #endif
20
21 #  include <iomanip>
22
23 #define UI1_2_840_10008_1_2      "1.2.840.10008.1.2"
24 #define UI1_2_840_10008_1_2_1    "1.2.840.10008.1.2.1"
25 #define UI1_2_840_10008_1_2_2    "1.2.840.10008.1.2.2"
26 #define UI1_2_840_10008_1_2_1_99 "1.2.840.10008.1.2.1.99"
27
28 typedef struct {
29    guint32 totalSQlength;
30    guint32 alreadyParsedlength;
31 } pileElem;
32
33 //-----------------------------------------------------------------------------
34 // Refer to gdcmParser::CheckSwap()
35 const unsigned int gdcmParser::HEADER_LENGTH_TO_READ = 256;
36
37 // Refer to gdcmParser::SetMaxSizeLoadEntry()
38 const unsigned int gdcmParser::MAX_SIZE_LOAD_ELEMENT_VALUE = 4096;
39
40 // Refer to gdcmParser::SetMaxSizePrintEntry()
41 // TODO : Right now, better see "define, in gdcmHederEntry.cxx
42 const unsigned int gdcmParser::MAX_SIZE_PRINT_ELEMENT_VALUE = 64;
43
44 //-----------------------------------------------------------------------------
45 // Constructor / Destructor
46
47 /**
48  * \brief   constructor  
49  * @param   inFilename file to be opened for parsing
50  * @param   exception_on_error whether we throw an exception or not
51  * @param   enable_sequences = true to allow the header 
52  *          to be parsed *inside* the SeQuences,
53  *          when they have an actual length 
54  * \warning enable_sequences *has to be* true for reading PAPYRUS 3.0 files 
55  * @param   ignore_shadow to allow skipping the shadow elements, 
56  *          to save memory space.
57  * \warning The TRUE value for this param has to be used 
58  *          with a FALSE value for the 'enable_sequence' param.
59  *          ('public elements' may be embedded in 'shadow Sequences')
60  */
61 gdcmParser::gdcmParser(const char *inFilename, 
62                        bool exception_on_error,
63                        bool enable_sequences,
64                        bool ignore_shadow) {
65    enableSequences=enable_sequences;
66    ignoreShadow   =ignore_shadow;
67    
68    SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE); 
69    filename = inFilename;
70    Initialise();
71
72    if ( !OpenFile(exception_on_error))
73       return;
74
75    LoadHeaderEntries();   
76    CloseFile();
77
78    wasUpdated = 0;  // will be set to 1 if user adds an entry
79    printLevel = 1;  // 'Medium' print level by default
80 }
81
82 /**
83  * \ingroup gdcmParser
84  * \brief  constructor 
85  * @param   exception_on_error
86  */
87 gdcmParser::gdcmParser(bool exception_on_error) {
88         (void)exception_on_error;
89    enableSequences=0;
90
91    SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
92    Initialise();
93
94    wasUpdated = 0;  // will be set to 1 if user adds an entry
95    printLevel = 1;  // 'Medium' print level by default
96 }
97
98 /**
99  * \ingroup gdcmParser
100  * \brief   Canonical destructor.
101  */
102 gdcmParser::~gdcmParser (void) {
103    RefPubDict = NULL;
104    RefShaDict = NULL;
105 }
106
107 //-----------------------------------------------------------------------------
108 // Print
109 /**
110   * \ingroup gdcmParser
111   * \brief   Prints the Header Entries (Dicom Elements)
112   *          from the chained list
113   * @return
114   */ 
115 void gdcmParser::PrintEntry(std::ostream & os) {
116
117    for (ListTag::iterator i = listEntries.begin();  
118         i != listEntries.end();
119         ++i)
120    {
121       (*i)->SetPrintLevel(printLevel);
122       (*i)->Print(os);   
123    } 
124 }
125
126
127 /**
128   * \brief   Prints The Dict Entries of THE public Dicom Dictionary
129   * @return
130   */  
131 void gdcmParser::PrintPubDict(std::ostream & os) {
132    RefPubDict->Print(os);
133 }
134
135 /**
136   * \brief   Prints The Dict Entries of THE shadow Dicom Dictionary
137   * @return
138   */
139 void gdcmParser::PrintShaDict(std::ostream & os) {
140    RefShaDict->Print(os);
141 }
142
143 //-----------------------------------------------------------------------------
144 // Public
145 /**
146  * \brief   Get the public dictionary used
147  */
148 gdcmDict *gdcmParser::GetPubDict(void) {
149    return(RefPubDict);
150 }
151
152 /**
153  * \brief   Get the shadow dictionary used
154  */
155 gdcmDict *gdcmParser::GetShaDict(void) {
156    return(RefShaDict);
157 }
158
159 /**
160  * \brief   Set the shadow dictionary used
161  * \param   dict dictionary to use in shadow
162  */
163 bool gdcmParser::SetShaDict(gdcmDict *dict){
164    RefShaDict=dict;
165    return(!RefShaDict);
166 }
167
168 /**
169  * \brief   Set the shadow dictionary used
170  * \param   dictName name of the dictionary to use in shadow
171  */
172 bool gdcmParser::SetShaDict(DictKey dictName){
173    RefShaDict=gdcmGlobal::GetDicts()->GetDict(dictName);
174    return(!RefShaDict);
175 }
176
177 /**
178  * \brief  This predicate, based on hopefully reasonable heuristics,
179  *         decides whether or not the current gdcmParser was properly parsed
180  *         and contains the mandatory information for being considered as
181  *         a well formed and usable Dicom/Acr File.
182  * @return true when gdcmParser is the one of a reasonable Dicom/Acr file,
183  *         false otherwise. 
184  */
185 bool gdcmParser::IsReadable(void) { 
186    if(filetype==Unknown) {
187       return(false);
188    }
189    if(listEntries.size()<=0) {    
190       return(false);
191    }
192
193    return(true);
194 }
195
196 /**
197  * \brief   Determines if the Transfer Syntax was already encountered
198  *          and if it corresponds to a ImplicitVRLittleEndian one.
199  * @return  True when ImplicitVRLittleEndian found. False in all other cases.
200  */
201 bool gdcmParser::IsImplicitVRLittleEndianTransferSyntax(void) {
202    gdcmHeaderEntry *Element = GetHeaderEntryByNumber(0x0002, 0x0010);
203    if ( !Element )
204       return false;
205    LoadHeaderEntrySafe(Element);
206
207    std::string Transfer = Element->GetValue();
208    if ( Transfer == UI1_2_840_10008_1_2 )
209       return true;
210    return false;
211 }
212
213 /**
214  * \ingroup gdcmParser
215  * \brief   Determines if the Transfer Syntax was already encountered
216  *          and if it corresponds to a ExplicitVRLittleEndian one.
217  * @return  True when ExplicitVRLittleEndian found. False in all other cases.
218  */
219 bool gdcmParser::IsExplicitVRLittleEndianTransferSyntax(void) {
220    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
221    if ( !Element )
222       return false;
223    LoadHeaderEntrySafe(Element);
224
225    std::string Transfer = Element->GetValue();
226    if ( Transfer == UI1_2_840_10008_1_2_1 )
227       return true;
228    return false;
229 }
230
231 /**
232  * \brief   Determines if the Transfer Syntax was already encountered
233  *          and if it corresponds to a DeflatedExplicitVRLittleEndian one.
234  * @return  True when DeflatedExplicitVRLittleEndian found. False in all other cases.
235  */
236 bool gdcmParser::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
237    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
238    if ( !Element )
239       return false;
240    LoadHeaderEntrySafe(Element);
241
242    std::string Transfer = Element->GetValue();
243    if ( Transfer == UI1_2_840_10008_1_2_1_99 )
244       return true;
245    return false;
246 }
247
248 /**
249  * \brief   Determines if the Transfer Syntax was already encountered
250  *          and if it corresponds to a Explicit VR Big Endian one.
251  * @return  True when big endian found. False in all other cases.
252  */
253 bool gdcmParser::IsExplicitVRBigEndianTransferSyntax(void) {
254    gdcmHeaderEntry* Element = GetHeaderEntryByNumber(0x0002, 0x0010);
255    if ( !Element )
256       return false;
257    LoadHeaderEntrySafe(Element);
258
259    std::string Transfer = Element->GetValue();
260    if ( Transfer == UI1_2_840_10008_1_2_2 )  //1.2.2 ??? A verifier !
261       return true;
262    return false;
263 }
264
265 /**
266  * \brief  returns the File Type 
267  *         (ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown)
268  * @return the FileType code
269  */
270 FileType gdcmParser::GetFileType(void) {
271    return(filetype);
272 }
273
274 /**
275  * \brief   opens the file
276  * @param   exception_on_error
277  * @return  
278  */
279 FILE *gdcmParser::OpenFile(bool exception_on_error)
280   throw(gdcmFileError) 
281 {
282   fp=fopen(filename.c_str(),"rb");
283   if(exception_on_error) 
284   {
285     if(!fp)
286       throw gdcmFileError("gdcmParser::gdcmParser(const char *, bool)");
287   }
288
289   if ( fp ) 
290   {
291      guint16 zero;
292      fread(&zero,  (size_t)2, (size_t)1, fp);
293
294     //ACR -- or DICOM with no Preamble --
295     if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200)
296        return(fp);
297
298     //DICOM
299     fseek(fp, 126L, SEEK_CUR);
300     char dicm[4];
301     fread(dicm,  (size_t)4, (size_t)1, fp);
302     if( memcmp(dicm, "DICM", 4) == 0 )
303        return(fp);
304
305     fclose(fp);
306     dbg.Verbose(0, "gdcmParser::OpenFile not DICOM/ACR", filename.c_str());
307   }
308   else {
309     dbg.Verbose(0, "gdcmParser::OpenFile cannot open file", filename.c_str());
310   }
311   return(NULL);
312 }
313
314 /**
315  * \brief closes the file  
316  * @return  TRUE if the close was successfull 
317  */
318 bool gdcmParser::CloseFile(void) {
319   int closed = fclose(fp);
320   fp = (FILE *)0;
321   if (! closed)
322      return false;
323   return true;
324 }
325
326 /**
327  * \brief Writes in a file all the Header Entries (Dicom Elements) 
328  *        of the Chained List
329  * @param fp file pointer on an already open file
330  * @param type Type of the File to be written 
331  *          (ACR-NEMA, ExplicitVR, ImplicitVR)
332  * \return Always true.
333  */
334 bool gdcmParser::Write(FILE *fp, FileType type) {
335 // ==============
336 // TODO The stuff will have to be rewritten using the SeQuence based 
337 //       tree-like stucture instead  of the chained list .
338 //      (so we shall remove the GroupHT from the gdcmParser)
339 // To be checked
340 // =============
341
342    // TODO : move the following lines (and a lot of others, to be written)
343    // to a future function CheckAndCorrectHeader
344    
345    // Question :
346    // Comment pourrait-on savoir si le DcmHeader vient d'un fichier
347    // DicomV3 ou non (FileType est un champ de gdcmParser ...)
348    // WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA
349    // no way 
350    // a moins de se livrer a un tres complique ajout des champs manquants.
351    // faire un CheckAndCorrectHeader (?)  
352
353    if (type == ImplicitVR) 
354    {
355       std::string implicitVRTransfertSyntax = UI1_2_840_10008_1_2;
356       ReplaceOrCreateByNumber(implicitVRTransfertSyntax,0x0002, 0x0010);
357       
358       //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
359       //      values with a VR of UI shall be padded with a single trailing null
360       //      Dans le cas suivant on doit pader manuellement avec un 0
361       
362       SetEntryLengthByNumber(18, 0x0002, 0x0010);
363    } 
364
365    if (type == ExplicitVR) 
366    {
367       std::string explicitVRTransfertSyntax = UI1_2_840_10008_1_2_1;
368       ReplaceOrCreateByNumber(explicitVRTransfertSyntax,0x0002, 0x0010);
369       
370       //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
371       //      values with a VR of UI shall be padded with a single trailing null
372       //      Dans le cas suivant on doit pader manuellement avec un 0
373       
374       SetEntryLengthByNumber(20, 0x0002, 0x0010);
375    }
376
377 /* TODO : rewrite later, if really usefull
378
379 --> Warning : un-updated odd groups lengths can causes pb 
380 -->           (xmedcon breaks)
381 --> to be re- written with future org.
382
383    if ( (type == ImplicitVR) || (type == ExplicitVR) )
384       UpdateGroupLength(false,type);
385    if ( type == ACR)
386       UpdateGroupLength(true,ACR);
387 */
388
389    WriteEntries(fp,type);
390    return(true);
391 }
392
393 /**
394  * \brief   Modifies the value of a given Header Entry (Dicom Element)
395  *          when it exists. Create it with the given value when unexistant.
396  * \warning Adds the Header Entry to the HTable, NOT to the chained List
397  * @param   Value Value to be set
398  * @param   Group Group of the Entry 
399  * @param   Elem  Element of the Entry
400  * \return  pointer to the modified/created Header Entry (NULL when creation
401  *          failed).
402  */
403 gdcmHeaderEntry * gdcmParser::ReplaceOrCreateByNumber(
404                                          std::string Value, 
405                                          guint16 Group, 
406                                          guint16 Elem ){
407    gdcmHeaderEntry* a;
408    a = GetHeaderEntryByNumber( Group, Elem);
409    if (a == NULL) {
410       a =NewHeaderEntryByNumber(Group, Elem);
411       if (a == NULL) 
412          return NULL;
413       AddHeaderEntry(a);
414    }   
415    //CLEANME SetEntryByNumber(Value, Group, Elem);
416    a->SetValue(Value);
417    return(a);
418 }   
419
420 /**
421  * \brief Set a new value if the invoked element exists
422  *        Seems to be useless !!!
423  * @param Value new element value
424  * @param Group   group of the Entry 
425  * @param Elem element of the Entry
426  * \return  boolean 
427  */
428 bool gdcmParser::ReplaceIfExistByNumber(char* Value, guint16 Group, guint16 Elem ) 
429 {
430    std::string v = Value;
431    SetEntryByNumber(v, Group, Elem);
432    return true;
433
434
435 //-----------------------------------------------------------------------------
436 // Protected
437
438 /**
439  * \brief   Checks if a given Dicom Element exists
440  *          within the H table
441  * @param   group Group   number of the searched Dicom Element 
442  * @param   element  Element number of the searched Dicom Element 
443  * @return  number of occurences
444  */
445 int gdcmParser::CheckIfEntryExistByNumber(guint16 group, guint16 element ) {
446    std::string key = gdcmDictEntry::TranslateToKey(group, element );
447    return (tagHT.count(key));
448 }
449
450 /**
451  * \ingroup gdcmParser
452  * \brief   Searches within Header Entries (Dicom Elements) parsed with 
453  *          the public and private dictionaries 
454  *          for the element value of a given tag.
455  * \warning Don't use any longer : use GetPubEntryByName
456  * @param   tagName name of the searched element.
457  * @return  Corresponding element value when it exists,
458  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
459  */
460 std::string gdcmParser::GetEntryByName(std::string tagName) {
461    gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName); 
462    if( dictEntry == NULL)
463       return GDCM_UNFOUND;
464
465    return(GetEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));  
466 }
467
468 /**
469  * \ingroup gdcmParser
470  * \brief   Searches within Header Entries (Dicom Elements) parsed with 
471  *          the public and private dictionaries 
472  *          for the element value representation of a given tag.
473  *
474  *          Obtaining the VR (Value Representation) might be needed by caller
475  *          to convert the string typed content to caller's native type 
476  *          (think of C++ vs Python). The VR is actually of a higher level
477  *          of semantics than just the native C++ type.
478  * @param   tagName name of the searched element.
479  * @return  Corresponding element value representation when it exists,
480  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
481  */
482 std::string gdcmParser::GetEntryVRByName(std::string tagName) {
483    gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName); 
484    if( dictEntry == NULL)
485       return GDCM_UNFOUND;
486
487    gdcmHeaderEntry* elem =  GetHeaderEntryByNumber(dictEntry->GetGroup(),
488                                                    dictEntry->GetElement());
489    return elem->GetVR();
490 }
491
492
493 /**
494  * \ingroup gdcmParser
495  * \brief   Searches within Header Entries (Dicom Elements) parsed with 
496  *          the public and private dictionaries 
497  *          for the element value representation of a given tag.
498  * @param   group Group of the searched tag.
499  * @param   element Element of the searched tag.
500  * @return  Corresponding element value representation when it exists,
501  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
502  */
503 std::string gdcmParser::GetEntryByNumber(guint16 group, guint16 element){
504    TagKey key = gdcmDictEntry::TranslateToKey(group, element);
505    if ( ! tagHT.count(key))
506       return GDCM_UNFOUND;
507    return tagHT.find(key)->second->GetValue();
508 }
509
510 /**
511  * \ingroup gdcmParser
512  * \brief   Searches within Header Entries (Dicom Elements) parsed with 
513  *          the public and private dictionaries 
514  *          for the element value representation of a given tag..
515  *
516  *          Obtaining the VR (Value Representation) might be needed by caller
517  *          to convert the string typed content to caller's native type 
518  *          (think of C++ vs Python). The VR is actually of a higher level
519  *          of semantics than just the native C++ type.
520  * @param   group Group of the searched tag.
521  * @param   element Element of the searched tag.
522  * @return  Corresponding element value representation when it exists,
523  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
524  */
525 std::string gdcmParser::GetEntryVRByNumber(guint16 group, guint16 element) {
526    gdcmHeaderEntry* elem =  GetHeaderEntryByNumber(group, element);
527    if ( !elem )
528       return GDCM_UNFOUND;
529    return elem->GetVR();
530 }
531
532 /**
533  * \ingroup gdcmParser
534  * \brief   Searches within Header Entries (Dicom Elements) parsed with 
535  *          the public and private dictionaries 
536  *          for the value length of a given tag..
537  * @param   group Group of the searched tag.
538  * @param   element Element of the searched tag.
539  * @return  Corresponding element length; -2 if not found
540  */
541 int gdcmParser::GetEntryLengthByNumber(guint16 group, guint16 element) {
542    gdcmHeaderEntry* elem =  GetHeaderEntryByNumber(group, element);
543    if ( !elem )
544       return -2;
545    return elem->GetLength();
546 }
547 /**
548  * \ingroup gdcmParser
549  * \brief   Sets the value (string) of the Header Entry (Dicom Element)
550  * @param   content string value of the Dicom Element
551  * @param   tagName name of the searched Dicom Element.
552  * @return  true when found
553  */
554 bool gdcmParser::SetEntryByName(std::string content,std::string tagName) {
555    gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName); 
556    if( dictEntry == NULL)
557       return false;    
558
559    return(SetEntryByNumber(content,dictEntry->GetGroup(),
560                                    dictEntry->GetElement()));
561 }
562
563 /**
564  * \ingroup gdcmParser
565  * \brief   Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
566  *          through it's (group, element) and modifies it's content with
567  *          the given value.
568  * \warning Don't use any longer : use SetPubEntryByNumber
569  * @param   content new value to substitute with
570  * @param   group   group of the Dicom Element to modify
571  * @param   element element of the Dicom Element to modify
572  */
573 bool gdcmParser::SetEntryByNumber(std::string content, 
574                                   guint16 group,
575                                   guint16 element) 
576 {
577    TagKey key = gdcmDictEntry::TranslateToKey(group, element);
578    if ( ! tagHT.count(key))
579       return false;
580    int l = content.length();
581    if(l%2) // Non even length are padded with a space (020H).
582    {  
583       l++;
584       content = content + '\0';
585    }
586       
587    gdcmHeaderEntry * a;
588    IterHT p;
589    TagHeaderEntryHT::iterator p2;
590    // DO NOT remove the following lines : they explain the stuff   
591    //p= tagHT.equal_range(key); // get a pair of iterators first-last synonym
592    //p2=p.first;                // iterator on the first synonym 
593    //a=p2->second;              // H Table target column (2-nd col)
594     
595    // or, easier :
596    a = ((tagHT.equal_range(key)).first)->second; 
597        
598    a-> SetValue(content); 
599    
600    std::string vr = a->GetVR();
601    
602    guint32 lgr;
603    if( (vr == "US") || (vr == "SS") ) 
604       lgr = 2;
605    else if( (vr == "UL") || (vr == "SL") )
606       lgr = 4;
607    else
608       lgr = l;   
609
610    a->SetLength(lgr);   
611    return true;
612 }  
613
614 /**
615  * \ingroup gdcmParser
616  * \brief   Accesses an existing gdcmHeaderEntry (i.e. a Dicom Element)
617  *          in the PubHeaderEntrySet of this instance
618  *          through it's (group, element) and modifies it's length with
619  *          the given value.
620  * \warning Use with extreme caution.
621  * @param l new length to substitute with
622  * @param group   group of the Entry to modify
623  * @param element element of the Entry to modify
624  * @return  true on success, false otherwise.
625  */
626 bool gdcmParser::SetEntryLengthByNumber(guint32 l, 
627                                         guint16 group, 
628                                         guint16 element) 
629 {
630    TagKey key = gdcmDictEntry::TranslateToKey(group, element);
631    if ( ! tagHT.count(key))
632       return false;
633    if (l%2) l++; // length must be even
634    ( ((tagHT.equal_range(key)).first)->second )->SetLength(l); 
635
636    return true ;
637 }
638
639 /**
640  * \ingroup gdcmParser
641  * \brief   Gets (from Header) the offset  of a 'non string' element value 
642  *          (LoadElementValues has already be executed)
643  * @param Group   group of the Entry 
644  * @param Elem  element of the Entry
645  * @return File Offset of the Element Value 
646  */
647 size_t gdcmParser::GetEntryOffsetByNumber(guint16 Group, guint16 Elem) 
648 {
649    gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
650    if (!Entry) 
651    {
652       dbg.Verbose(1, "gdcmParser::GetHeaderEntryByNumber",
653                       "failed to Locate gdcmHeaderEntry");
654       return (size_t)0;
655    }
656    return Entry->GetOffset();
657 }
658
659 /**
660  * \ingroup gdcmParser
661  * \brief   Gets (from Header) a 'non string' element value 
662  *          (LoadElementValues has already be executed)  
663  * @param Group   group of the Entry 
664  * @param Elem element of the Entry
665  * @return Pointer to the 'non string' area
666  */
667 void * gdcmParser::GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem) 
668 {
669    gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
670    if (!Entry) 
671    {
672       dbg.Verbose(1, "gdcmParser::GetHeaderEntryByNumber",
673                   "failed to Locate gdcmHeaderEntry");
674       return (NULL);
675    }
676    return Entry->GetVoidArea();
677 }
678
679 /**
680  * \brief         Loads (from disk) the element content 
681  *                when a string is not suitable
682  * @param Group   group of the Entry 
683  * @param Elem element of the Entry
684  */
685 void *gdcmParser::LoadEntryVoidArea(guint16 Group, guint16 Elem) 
686 {
687    gdcmHeaderEntry * Element= GetHeaderEntryByNumber(Group, Elem);
688    if ( !Element )
689       return NULL;
690    size_t o =(size_t)Element->GetOffset();
691    fseek(fp, o, SEEK_SET);
692    size_t l=Element->GetLength();
693    char* a = new char[l];
694    if(!a) 
695       return NULL;
696
697    SetEntryVoidAreaByNumber(a, Group, Elem);
698    // TODO check the result 
699    size_t l2 = fread(a, 1, l ,fp);
700    if(l != l2) 
701    {
702       delete[] a;
703       return NULL;
704    }
705
706    return a;  
707 }
708
709 /**
710  * \ingroup gdcmParser
711  * \brief   Sets a 'non string' value to a given Dicom Element
712  * @param   area
713  * @param   group Group number of the searched Dicom Element 
714  * @param   element Element number of the searched Dicom Element 
715  * @return  
716  */
717 bool gdcmParser::SetEntryVoidAreaByNumber(void * area,
718                                           guint16 group, 
719                                           guint16 element) 
720 {
721    TagKey key = gdcmDictEntry::TranslateToKey(group, element);
722    if ( ! tagHT.count(key))
723       return false;
724    ( ((tagHT.equal_range(key)).first)->second )->SetVoidArea(area); 
725    return true;
726 }
727
728 /**
729  * \ingroup gdcmParser
730  * \brief   Update the entries with the shadow dictionary. 
731  *          Only non even entries are analyzed       
732  */
733 void gdcmParser::UpdateShaEntries(void) {
734    gdcmDictEntry *entry;
735    std::string vr;
736
737    for(ListTag::iterator it=listEntries.begin();
738        it!=listEntries.end();
739        ++it)
740    {
741       // Odd group => from public dictionary
742       if((*it)->GetGroup()%2==0)
743          continue;
744
745       // Peer group => search the corresponding dict entry
746       if(RefShaDict)
747          entry=RefShaDict->GetDictEntryByNumber((*it)->GetGroup(),(*it)->GetElement());
748       else
749          entry=NULL;
750
751       if((*it)->IsImplicitVR())
752          vr="Implicit";
753       else
754          vr=(*it)->GetVR();
755
756       (*it)->SetValue(GetHeaderEntryUnvalue(*it));
757       if(entry){
758          // Set the new entry and the new value
759          (*it)->SetDictEntry(entry);
760          CheckHeaderEntryVR(*it,vr);
761
762          (*it)->SetValue(GetHeaderEntryValue(*it));
763       }
764       else
765       {
766          // Remove precedent value transformation
767          (*it)->SetDictEntry(NewVirtualDictEntry((*it)->GetGroup(),(*it)->GetElement(),vr));
768       }
769    }
770 }
771
772 /**
773  * \ingroup gdcmParser
774  * \brief   Searches within the Header Entries for a Dicom Element of
775  *          a given tag.
776  * @param   tagName name of the searched Dicom Element.
777  * @return  Corresponding Dicom Element when it exists, and NULL
778  *          otherwise.
779  */
780  gdcmHeaderEntry *gdcmParser::GetHeaderEntryByName(std::string tagName) {
781    gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName); 
782    if( dictEntry == NULL)
783       return NULL;
784
785   return(GetHeaderEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
786 }
787
788 /**
789  * \ingroup gdcmParser
790  * \brief  retrieves a Dicom Element (the first one) using (group, element)
791  * \warning (group, element) IS NOT an identifier inside the Dicom Header
792  *           if you think it's NOT UNIQUE, check the count number
793  *           and use iterators to retrieve ALL the Dicoms Elements within
794  *           a given couple (group, element)
795  * @param   group Group number of the searched Dicom Element 
796  * @param   element Element number of the searched Dicom Element 
797  * @return  
798  */
799 gdcmHeaderEntry* gdcmParser::GetHeaderEntryByNumber(guint16 group, guint16 element) 
800 {
801    TagKey key = gdcmDictEntry::TranslateToKey(group, element);   
802    if ( ! tagHT.count(key))
803       return NULL;
804    return tagHT.find(key)->second;
805 }
806
807 /**
808  * \ingroup gdcmParser
809  * \brief   retrieves the Dicom Elements (all of them) using (group, element) 
810  * @param   group Group number of the searched Dicom Element.
811  * @param   element Element number of the searched Dicom Element.
812  * @return  a range (i.e.pair<,>) containing all elements whose key is group|element) 
813  */
814  
815 IterHT gdcmParser::GetHeaderEntrySameNumber(guint16 group, guint16 element){
816    TagKey key = gdcmDictEntry::TranslateToKey(group, element);
817    return (tagHT.equal_range(key));
818 }
819
820 /**
821  * \ingroup       gdcmParser
822  * \brief         Loads the element while preserving the current
823  *                underlying file position indicator as opposed to
824  *                to LoadHeaderEntry that modifies it.
825  * @param entry   Header Entry whose value shall be loaded. 
826  * @return  
827  */
828 void gdcmParser::LoadHeaderEntrySafe(gdcmHeaderEntry * entry) {
829    long PositionOnEntry = ftell(fp);
830    LoadHeaderEntry(entry);
831    fseek(fp, PositionOnEntry, SEEK_SET);
832 }
833
834 /**
835  * \brief   Re-computes the length of a ACR-NEMA/Dicom group from a DcmHeader
836  * \warning : to be re-written using the chained list instead of the H table.
837  * \warning : DO NOT use (doesn't work any longer because of the multimap)
838  * \todo : to be re-written using the chained list instead of the H table
839  * @param   SkipSequence TRUE if we don't want to write Sequences (ACR-NEMA Files)
840  * @param   type Type of the File (ExplicitVR,ImplicitVR, ACR, ...) 
841  */
842 void gdcmParser::UpdateGroupLength(bool SkipSequence, FileType type) {
843    guint16 gr, el;
844    std::string vr;
845    
846    gdcmHeaderEntry *elem;
847    char trash[10];
848    std::string str_trash;
849    
850    GroupKey key;
851    GroupHT groupHt;  // to hold the length of each group
852    TagKey tk;
853    // remember :
854    // typedef std::map<GroupKey, int> GroupHT;
855    
856    gdcmHeaderEntry *elemZ;
857   
858    // for each Tag in the DCM Header
859    
860    for (TagHeaderEntryHT::iterator tag2 = tagHT.begin(); 
861         tag2 != tagHT.end();
862         ++tag2)
863    {
864       elem  = tag2->second;
865       gr = elem->GetGroup();
866       el = elem->GetElement();
867       vr = elem->GetVR(); 
868                  
869       sprintf(trash, "%04x", gr);
870       key = trash;   // generate 'group tag'
871       
872       // if the caller decided not to take SEQUENCEs into account 
873       // e.g : he wants to write an ACR-NEMA File 
874                 
875       if (SkipSequence && vr == "SQ") 
876          continue;
877       
878       // Still unsolved problem :
879       // we cannot find the 'Sequence Delimitation Item'
880       // since it's at the end of the Hash Table
881       // (fffe,e0dd) 
882        
883       // there is SEQUENCE in ACR-NEMA
884       // WARNING : 
885       // --> la descente a l'interieur' des SQ 
886       // devrait etre faite avec une liste chainee, pas avec une HTable...
887             
888       if ( groupHt.count(key) == 0) // we just read the first elem of a given group
889       { 
890          if (el == 0x0000) // the first elem is 0x0000
891          {
892             groupHt[key] = 0;      // initialize group length 
893          } 
894          else 
895          {
896             groupHt[key] = 2 + 2 + 4 + elem->GetLength(); // non 0x0000 first group elem
897          } 
898       } 
899       else // any elem but the first
900       {   
901          if (type == ExplicitVR) 
902          {
903             if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) 
904             {
905                groupHt[key] +=  4; // explicit VR AND OB, OW, SQ : 4 more bytes
906             }
907          }
908          groupHt[key] += 2 + 2 + 4 + elem->GetLength(); 
909       } 
910    }
911
912    unsigned short int gr_bid;
913   
914    for (GroupHT::iterator g = groupHt.begin(); // for each group we found
915         g != groupHt.end();
916         ++g)
917    { 
918       // FIXME: g++ -Wall -Wstrict-prototypes reports on following line:
919       //        warning: unsigned int format, different type arg
920       sscanf(g->first.c_str(),"%x",&gr_bid); //FIXME
921       tk = g->first + "|0000";   // generate the element full tag
922                      
923       if ( tagHT.count(tk) == 0) // if element 0x0000 not found
924       {
925          gdcmDictEntry * tagZ = new gdcmDictEntry(gr_bid, 0x0000, "UL");       
926          elemZ = new gdcmHeaderEntry(tagZ);
927          elemZ->SetLength(4);
928          AddHeaderEntry(elemZ);   // create it
929       } 
930       else 
931       {
932          elemZ=GetHeaderEntryByNumber(gr_bid, 0x0000);
933       }     
934       sprintf(trash ,"%d",g->second);
935       str_trash=trash;
936       elemZ->SetValue(str_trash);
937    }   
938 }
939
940 /**
941  * \brief Writes in a file (according to the requested format)
942  *        the group, the element, the value representation and the length
943  *        of a single gdcmHeaderEntry passed as argument.
944  * @param tag  pointer on the gdcmHeaderEntry to be written
945  * @param _fp  already open file pointer
946  * @param type type of the File to be written 
947  */
948 void gdcmParser::WriteEntryTagVRLength(gdcmHeaderEntry *tag,
949                                        FILE *_fp,
950                                        FileType type)
951 {
952    guint16 group  = tag->GetGroup();
953    std::string vr = tag->GetVR();
954    guint16 el     = tag->GetElement();
955    guint32 lgr    = tag->GetReadLength();
956
957    if ( (group == 0xfffe) && (el == 0x0000) ) 
958      // Fix in order to make some MR PHILIPS images e-film readable
959      // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
960      // we just *always* ignore spurious fffe|0000 tag !   
961       return; 
962
963    fwrite ( &group,(size_t)2 ,(size_t)1 ,_fp);  //group
964    fwrite ( &el,(size_t)2 ,(size_t)1 ,_fp);     //element
965       
966    if ( type == ExplicitVR ) {
967
968       // Special case of delimiters:
969       if (group == 0xfffe) {
970          // Delimiters have NO Value Representation and have NO length.
971          // Hence we skip writing the VR and length and we pad by writing
972          // 0xffffffff
973
974          int ff=0xffffffff;
975          fwrite (&ff,(size_t)4 ,(size_t)1 ,_fp);
976          return;
977       }
978
979       guint16 z=0;
980       guint16 shortLgr = lgr;
981       if (vr == "unkn") {     // Unknown was 'written'
982          // deal with Little Endian            
983          fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
984          fwrite ( &z,  (size_t)2 ,(size_t)1 ,_fp);
985       } else {
986          fwrite (vr.c_str(),(size_t)2 ,(size_t)1 ,_fp);                     
987          if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") )
988          {
989             fwrite ( &z,  (size_t)2 ,(size_t)1 ,_fp);
990             fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
991          } else {
992             fwrite ( &shortLgr,(size_t)2 ,(size_t)1 ,_fp);
993          }
994       }
995    } 
996    else // IMPLICIT VR 
997    { 
998       fwrite ( &lgr,(size_t)4 ,(size_t)1 ,_fp);
999    }
1000 }
1001       
1002 /**
1003  * \brief Writes in a file (according to the requested format)
1004  *        the value of a single gdcmHeaderEntry passed as argument.
1005  * @param tag  Pointer on the gdcmHeaderEntry to be written
1006  * @param _fp  Already open file pointer
1007  * @param type type of the File to be written
1008  */
1009 void gdcmParser::WriteEntryValue(gdcmHeaderEntry *tag, FILE *_fp,FileType type)
1010 {
1011    (void)type;
1012    guint16 group  = tag->GetGroup();
1013    std::string vr = tag->GetVR();
1014    guint32 lgr    = tag->GetReadLength();
1015
1016    if (vr == "SQ")
1017       // SeQuences have no value:
1018       return;
1019    if (group == 0xfffe)
1020       // Delimiters have no associated value:
1021       return;
1022       
1023    void *voidArea;
1024    voidArea = tag->GetVoidArea();
1025    if (voidArea != NULL) 
1026    { // there is a 'non string' LUT, overlay, etc
1027       fwrite ( voidArea,(size_t)lgr ,(size_t)1 ,_fp); // Elem value
1028       return;            
1029    }
1030       
1031    if (vr == "US" || vr == "SS") 
1032    {
1033       // some 'Short integer' fields may be mulivaluated
1034       // each single value is separated from the next one by '\'
1035       // we split the string and write each value as a short int
1036       std::vector<std::string> tokens;
1037       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1038       Tokenize (tag->GetValue(), tokens, "\\");
1039       for (unsigned int i=0; i<tokens.size();i++) 
1040       {
1041          guint16 val_uint16 = atoi(tokens[i].c_str());
1042          void *ptr = &val_uint16;
1043          fwrite ( ptr,(size_t)2 ,(size_t)1 ,_fp);
1044       }
1045       tokens.clear();
1046       return;
1047    }
1048       // some 'Integer' fields may be mulivaluated
1049       // each single value is separated from the next one by '\'
1050       // we split the string and write each value as an int
1051    if (vr == "UL" || vr == "SL") 
1052    {
1053       std::vector<std::string> tokens;
1054       tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1055       Tokenize (tag->GetValue(), tokens, "\\");
1056       for (unsigned int i=0; i<tokens.size();i++) 
1057       {
1058          guint32 val_uint32 = atoi(tokens[i].c_str());
1059          void *ptr = &val_uint32;
1060          fwrite ( ptr,(size_t)4 ,(size_t)1 ,_fp);
1061       }
1062       tokens.clear();
1063       return;
1064    }           
1065    fwrite (tag->GetValue().c_str(), (size_t)lgr ,(size_t)1, _fp); // Elem value
1066 }
1067
1068 /**
1069  * \brief Writes in a file (according to the requested format)
1070  *        a single gdcmHeaderEntry passed as argument.
1071  * \sa    WriteEntryValue, WriteEntryTagVRLength.
1072  * @param tag  Pointer on the gdcmHeaderEntry to be written
1073  * @param _fp  Already open file pointer
1074  * @param type type of the File to be written
1075  */
1076
1077 bool gdcmParser::WriteEntry(gdcmHeaderEntry *tag, FILE *_fp,FileType type)
1078 {
1079    guint32 length = tag->GetLength();
1080
1081    // The value of a tag MUST (see the DICOM norm) be an odd number of
1082    // bytes. When this is not the case, pad with an additional byte:
1083    if(length%2==1)
1084    { 
1085       tag->SetValue(tag->GetValue()+"\0");
1086       tag->SetLength(tag->GetReadLength()+1);
1087    }
1088
1089    WriteEntryTagVRLength(tag, _fp, type);
1090    WriteEntryValue(tag, _fp, type);
1091    return true;
1092 }
1093
1094 /**
1095  * \brief   writes on disc according to the requested format
1096  *          (ACR-NEMA, ExplicitVR, ImplicitVR) the image
1097  *          using the Chained List
1098  * \warning does NOT add the missing elements in the header :
1099  *           it's up to the user doing it !
1100  *           (function CheckHeaderCoherence to be written)
1101  * \warning DON'T try, right now, to write a DICOM image
1102  *           from an ACR Header (meta elements will be missing!)
1103  * \sa WriteEntriesDeprecated (Special temporary method for Theralys)
1104  * @param   type type of the File to be written 
1105  *          (ACR-NEMA, ExplicitVR, ImplicitVR)
1106  * @param   _fp already open file pointer
1107  */
1108
1109 bool gdcmParser::WriteEntries(FILE *_fp,FileType type)
1110 {   
1111    // TODO (?) check write failures (after *each* fwrite)
1112      
1113    for (ListTag::iterator tag2=listEntries.begin();
1114                           tag2 != listEntries.end();
1115                           ++tag2)
1116    {
1117       if ( type == ACR ){ 
1118          if ((*tag2)->GetGroup() < 0x0008)
1119             // Ignore pure DICOM V3 groups
1120             continue;
1121          if ((*tag2)->GetElement() %2)
1122             // Ignore the "shadow" groups
1123             continue;
1124          if ((*tag2)->GetVR() == "SQ" ) // ignore Sequences
1125             continue;
1126          if ((*tag2)->GetSQDepthLevel() != 0) // Not only ignore the SQ element
1127             continue;       
1128       } 
1129       if (! WriteEntry(*tag2,_fp,type) )
1130          return false;
1131    }
1132    return true;
1133 }   
1134
1135 /**
1136  * \ingroup gdcmParser
1137  * \brief   writes on disc according to the requested format
1138  *          (ACR-NEMA, ExplicitVR, ImplicitVR) the image,
1139  *          using only the last synonym of each mutimap H Table post.
1140  * \warning Uses the H Table, instead of the Chained List
1141  *          in order to be compliant with the old way to proceed
1142  *         (added elements taken in to account)
1143  *         Only THERALYS, during a transitory phase is supposed
1144  *         to use this method !!!
1145  * \warning DON'T try, right now, to write a DICOM image
1146  *           from an ACR Header (meta elements will be missing!)
1147  * \sa WriteEntries
1148  * @param   _fp already open file pointer
1149  * @param   type type of the File to be written 
1150  *          (ACR-NEMA, ExplicitVR, ImplicitVR)
1151  */
1152 void gdcmParser::WriteEntriesDeprecated(FILE *_fp,FileType type) {
1153
1154    // restent a tester les echecs en ecriture (apres chaque fwrite)
1155
1156    for (TagHeaderEntryHT::iterator tag2=tagHT.begin();
1157         tag2 != tagHT.end();
1158         ++tag2){
1159       if ( type == ACR ){ 
1160          if ((*tag2->second).GetGroup() < 0x0008)    continue; // ignore pure DICOM V3 groups
1161          if ((*tag2->second).GetElement() %2)        continue; // ignore shadow groups
1162          if ((*tag2->second).GetVR() == "SQ" )       continue; // ignore Sequences
1163          if ((*tag2->second).GetSQDepthLevel() != 0) continue; // Not only ignore the SQ element          
1164       }
1165       if ( ! WriteEntry(tag2->second,_fp,type))
1166          break;
1167    }
1168 }
1169
1170 /**
1171  * \ingroup gdcmParser
1172  * \brief   Swaps back the bytes of 4-byte long integer accordingly to
1173  *          processor order.
1174  * @return  The properly swaped 32 bits integer.
1175  */
1176 guint32 gdcmParser::SwapLong(guint32 a) {
1177    switch (sw) {
1178       case    0 :
1179          break;
1180       case 4321 :
1181          a=( ((a<<24) & 0xff000000) | ((a<<8)  & 0x00ff0000) | 
1182              ((a>>8)  & 0x0000ff00) | ((a>>24) & 0x000000ff) );
1183          break;
1184    
1185       case 3412 :
1186          a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
1187          break;
1188    
1189       case 2143 :
1190          a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff)  );
1191          break;
1192       default :
1193          dbg.Error(" gdcmParser::SwapLong : unset swap code");
1194          a=0;
1195    }
1196    return(a);
1197 }
1198
1199 /**
1200  * \ingroup gdcmParser
1201  * \brief   Unswaps back the bytes of 4-byte long integer accordingly to
1202  *          processor order.
1203  * @return  The properly unswaped 32 bits integer.
1204  */
1205 guint32 gdcmParser::UnswapLong(guint32 a) {
1206    return (SwapLong(a));
1207 }
1208
1209 /**
1210  * \ingroup gdcmParser
1211  * \brief   Swaps the bytes so they agree with the processor order
1212  * @return  The properly swaped 16 bits integer.
1213  */
1214 guint16 gdcmParser::SwapShort(guint16 a) {
1215    if ( (sw==4321)  || (sw==2143) )
1216       a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
1217    return (a);
1218 }
1219
1220 /**
1221  * \ingroup gdcmParser
1222  * \brief   Unswaps the bytes so they agree with the processor order
1223  * @return  The properly unswaped 16 bits integer.
1224  */
1225 guint16 gdcmParser::UnswapShort(guint16 a) {
1226    return (SwapShort(a));
1227 }
1228
1229 //-----------------------------------------------------------------------------
1230 // Private
1231 /**
1232  * \ingroup gdcmParser
1233  * \brief   Parses the header of the file and load element values.
1234  * @return  false if file is not ACR-NEMA / PAPYRUS / DICOM 
1235  */
1236 bool gdcmParser::LoadHeaderEntries(bool exception_on_error) throw(gdcmFormatError) {
1237    (void)exception_on_error;
1238    rewind(fp);
1239    if (!CheckSwap())
1240       return false;
1241       
1242    gdcmHeaderEntry *newHeaderEntry = (gdcmHeaderEntry *)0;   
1243    while ( (newHeaderEntry = ReadNextHeaderEntry()) ) {
1244      SkipHeaderEntry(newHeaderEntry);
1245      if ( (ignoreShadow==0) || (newHeaderEntry->GetGroup()%2) == 0) { 
1246         AddHeaderEntry(newHeaderEntry); 
1247      }     
1248    }   
1249    rewind(fp);
1250    // Be carefull : merging this two loops may cause troubles ...
1251    for (ListTag::iterator i = GetListEntry().begin();                           
1252         i != GetListEntry().end();                                                
1253         ++i)                                                                      
1254    {                                                                            
1255       LoadHeaderEntry(*i);                                                      
1256    }                                                                            
1257    rewind(fp);                                                                     
1258    
1259    // Load 'non string' values
1260       
1261    std::string PhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);   
1262    if( PhotometricInterpretation == "PALETTE COLOR " ) {
1263       LoadEntryVoidArea(0x0028,0x1200);  // gray LUT   
1264       LoadEntryVoidArea(0x0028,0x1201);  // R    LUT
1265       LoadEntryVoidArea(0x0028,0x1202);  // G    LUT
1266       LoadEntryVoidArea(0x0028,0x1203);  // B    LUT
1267       
1268       LoadEntryVoidArea(0x0028,0x1221);  // Segmented Red   Palette Color LUT Data
1269       LoadEntryVoidArea(0x0028,0x1222);  // Segmented Green Palette Color LUT Data
1270       LoadEntryVoidArea(0x0028,0x1223);  // Segmented Blue  Palette Color LUT Data
1271    } 
1272    //FIXME later : how to use it?
1273    LoadEntryVoidArea(0x0028,0x3006);  //LUT Data (CTX dependent)     
1274    
1275    // --------------------------------------------------------------
1276    // Special Patch to allow gdcm to read ACR-LibIDO formated images
1277    //
1278    // if recognition code tells us we deal with a LibIDO image
1279    // we switch lineNumber and columnNumber
1280    //
1281    std::string RecCode;
1282    RecCode = GetEntryByNumber(0x0008, 0x0010); // recognition code
1283    if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
1284        RecCode == "CANRME_AILIBOD1_1." )  // for brain-damaged softwares
1285                                           // with "little-endian strings"
1286    {
1287          filetype = ACR_LIBIDO; 
1288          std::string rows    = GetEntryByNumber(0x0028, 0x0010);
1289          std::string columns = GetEntryByNumber(0x0028, 0x0011);
1290          SetEntryByNumber(columns, 0x0028, 0x0010);
1291          SetEntryByNumber(rows   , 0x0028, 0x0011);
1292    }
1293    // ----------------- End of Special Patch ----------------   
1294    return true;
1295 }
1296
1297 /**
1298  * \ingroup       gdcmParser
1299  * \brief         Loads the element content if its length doesn't exceed
1300  *                the value specified with gdcmParser::SetMaxSizeLoadEntry()
1301  * @param         Entry Header Entry (Dicom Element) to be dealt with
1302  */
1303 void gdcmParser::LoadHeaderEntry(gdcmHeaderEntry *Entry)  {
1304    size_t item_read;
1305    guint16 group  = Entry->GetGroup();
1306    std::string  vr= Entry->GetVR();
1307    guint32 length = Entry->GetLength();
1308
1309    fseek(fp, (long)Entry->GetOffset(), SEEK_SET);
1310
1311    // A SeQuence "contains" a set of Elements.  
1312    //          (fffe e000) tells us an Element is beginning
1313    //          (fffe e00d) tells us an Element just ended
1314    //          (fffe e0dd) tells us the current SeQuence just ended
1315    if( group == 0xfffe ) {
1316       Entry->SetValue("gdcm::Skipped");
1317       return;
1318    }
1319
1320    // When the length is zero things are easy:
1321    if ( length == 0 ) {
1322       Entry->SetValue("");
1323       return;
1324    }
1325
1326    // The elements whose length is bigger than the specified upper bound
1327    // are not loaded. Instead we leave a short notice of the offset of
1328    // the element content and it's length.
1329    if (length > MaxSizeLoadEntry) {
1330       std::ostringstream s;
1331       s << "gdcm::NotLoaded.";
1332       s << " Address:" << (long)Entry->GetOffset();
1333       s << " Length:"  << Entry->GetLength();
1334       s << " x(" << std::hex << Entry->GetLength() << ")";
1335       Entry->SetValue(s.str());
1336       return;
1337    }
1338     
1339    // Any compacter code suggested (?)
1340    if ( IsHeaderEntryAnInteger(Entry) ) {   
1341       guint32 NewInt;
1342       std::ostringstream s;
1343       int nbInt;
1344    // When short integer(s) are expected, read and convert the following 
1345    // n *two characters properly i.e. as short integers as opposed to strings.
1346    // Elements with Value Multiplicity > 1
1347    // contain a set of integers (not a single one)       
1348       if (vr == "US" || vr == "SS") {
1349          nbInt = length / 2;
1350          NewInt = ReadInt16();
1351          s << NewInt;
1352          if (nbInt > 1){
1353             for (int i=1; i < nbInt; i++) {
1354                s << '\\';
1355                NewInt = ReadInt16();
1356                s << NewInt;
1357             }
1358          }
1359       }
1360    // When integer(s) are expected, read and convert the following 
1361    // n * four characters properly i.e. as integers as opposed to strings.
1362    // Elements with Value Multiplicity > 1
1363    // contain a set of integers (not a single one)           
1364       else if (vr == "UL" || vr == "SL") {
1365          nbInt = length / 4;
1366          NewInt = ReadInt32();
1367          s << NewInt;
1368          if (nbInt > 1) {
1369             for (int i=1; i < nbInt; i++) {
1370                s << '\\';
1371                NewInt = ReadInt32();
1372                s << NewInt;
1373             }
1374          }
1375       }
1376 #ifdef GDCM_NO_ANSI_STRING_STREAM
1377       s << std::ends; // to avoid oddities on Solaris
1378 #endif //GDCM_NO_ANSI_STRING_STREAM
1379
1380       Entry->SetValue(s.str());
1381       return;
1382    }
1383    
1384    // We need an additional byte for storing \0 that is not on disk
1385    std::string NewValue(length,0);
1386    item_read = fread(&(NewValue[0]), (size_t)length, (size_t)1, fp);
1387    if ( item_read != 1 ) {
1388       dbg.Verbose(1, "gdcmParser::LoadElementValue","unread element value");
1389       Entry->SetValue("gdcm::UnRead");
1390       return;
1391    }
1392
1393    if( (vr == "UI") ) // Because of correspondance with the VR dic
1394       Entry->SetValue(NewValue.c_str());
1395    else
1396       Entry->SetValue(NewValue);
1397 }
1398
1399 /**
1400  * \ingroup gdcmParser
1401  * \brief   add a new Dicom Element pointer to 
1402  *          the H Table and at the end of the chained List
1403  * \warning push_bash in listEntries ONLY during ParseHeader
1404  * \todo    something to allow further Elements addition,
1405  *          (at their right place in the chained list)
1406  *          when position to be taken care of     
1407  * @param   newHeaderEntry
1408  */
1409 void gdcmParser::AddHeaderEntry(gdcmHeaderEntry *newHeaderEntry) {
1410    tagHT.insert( PairHT( newHeaderEntry->GetKey(),newHeaderEntry) );
1411    listEntries.push_back(newHeaderEntry); 
1412    wasUpdated = 1;
1413 }
1414
1415 /**
1416  * \ingroup gdcmParser
1417  * \brief  Find the value Length of the passed Header Entry
1418  * @param  Entry Header Entry whose length of the value shall be loaded. 
1419  */
1420  void gdcmParser::FindHeaderEntryLength (gdcmHeaderEntry *Entry) {
1421    guint16 element = Entry->GetElement();
1422    //guint16 group   = Entry->GetGroup(); //FIXME
1423    std::string  vr = Entry->GetVR();
1424    guint16 length16;
1425        
1426    
1427    if ( (filetype == ExplicitVR) && (! Entry->IsImplicitVR()) ) 
1428    {
1429       if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") ) 
1430       {
1431          // The following reserved two bytes (see PS 3.5-2001, section
1432          // 7.1.2 Data element structure with explicit vr p27) must be
1433          // skipped before proceeding on reading the length on 4 bytes.
1434          fseek(fp, 2L, SEEK_CUR);
1435          guint32 length32 = ReadInt32();
1436
1437          if ( (vr == "OB") && (length32 == 0xffffffff) ) 
1438          {
1439             Entry->SetLength(FindHeaderEntryLengthOB());
1440             return;
1441          }
1442          FixHeaderEntryFoundLength(Entry, length32); 
1443          return;
1444       }
1445
1446       // Length is encoded on 2 bytes.
1447       length16 = ReadInt16();
1448       
1449       // We can tell the current file is encoded in big endian (like
1450       // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
1451       // and it's value is the one of the encoding of a big endian file.
1452       // In order to deal with such big endian encoded files, we have
1453       // (at least) two strategies:
1454       // * when we load the "Transfer Syntax" tag with value of big endian
1455       //   encoding, we raise the proper flags. Then we wait for the end
1456       //   of the META group (0x0002) among which is "Transfer Syntax",
1457       //   before switching the swap code to big endian. We have to postpone
1458       //   the switching of the swap code since the META group is fully encoded
1459       //   in little endian, and big endian coding only starts at the next
1460       //   group. The corresponding code can be hard to analyse and adds
1461       //   many additional unnecessary tests for regular tags.
1462       // * the second strategy consists in waiting for trouble, that shall
1463       //   appear when we find the first group with big endian encoding. This
1464       //   is easy to detect since the length of a "Group Length" tag (the
1465       //   ones with zero as element number) has to be of 4 (0x0004). When we
1466       //   encounter 1024 (0x0400) chances are the encoding changed and we
1467       //   found a group with big endian encoding.
1468       // We shall use this second strategy. In order to make sure that we
1469       // can interpret the presence of an apparently big endian encoded
1470       // length of a "Group Length" without committing a big mistake, we
1471       // add an additional check: we look in the already parsed elements
1472       // for the presence of a "Transfer Syntax" whose value has to be "big
1473       // endian encoding". When this is the case, chances are we have got our
1474       // hands on a big endian encoded file: we switch the swap code to
1475       // big endian and proceed...
1476       if ( (element  == 0x0000) && (length16 == 0x0400) ) 
1477       {
1478          if ( ! IsExplicitVRBigEndianTransferSyntax() ) 
1479          {
1480             dbg.Verbose(0, "gdcmParser::FindLength", "not explicit VR");
1481             errno = 1;
1482             return;
1483          }
1484          length16 = 4;
1485          SwitchSwapToBigEndian();
1486          // Restore the unproperly loaded values i.e. the group, the element
1487          // and the dictionary entry depending on them.
1488          guint16 CorrectGroup   = SwapShort(Entry->GetGroup());
1489          guint16 CorrectElem    = SwapShort(Entry->GetElement());
1490          gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
1491                                                        CorrectElem);
1492          if (!NewTag) 
1493          {
1494             // This correct tag is not in the dictionary. Create a new one.
1495             NewTag = NewVirtualDictEntry(CorrectGroup, CorrectElem);
1496          }
1497          // FIXME this can create a memory leaks on the old entry that be
1498          // left unreferenced.
1499          Entry->SetDictEntry(NewTag);
1500       }
1501        
1502       // Heuristic: well some files are really ill-formed.
1503       if ( length16 == 0xffff) 
1504       {
1505          length16 = 0;
1506          //dbg.Verbose(0, "gdcmParser::FindLength",
1507          //            "Erroneous element length fixed.");
1508          // Actually, length= 0xffff means that we deal with
1509          // Unknown Sequence Length 
1510       }
1511       FixHeaderEntryFoundLength(Entry, (guint32)length16);
1512       return;
1513    }
1514    else
1515    {
1516       // Either implicit VR or a non DICOM conformal (see note below) explicit
1517       // VR that ommited the VR of (at least) this element. Farts happen.
1518       // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
1519       // on Data elements "Implicit and Explicit VR Data Elements shall
1520       // not coexist in a Data Set and Data Sets nested within it".]
1521       // Length is on 4 bytes.
1522       
1523       FixHeaderEntryFoundLength(Entry, ReadInt32());
1524       return;
1525    }
1526 }
1527
1528 /**
1529  * \ingroup   gdcmParser
1530  * \brief     Find the Value Representation of the current Dicom Element.
1531  * @param     Entry
1532  */
1533 void gdcmParser::FindHeaderEntryVR( gdcmHeaderEntry *Entry) 
1534 {
1535    if (filetype != ExplicitVR)
1536       return;
1537
1538    char VR[3];
1539
1540    long PositionOnEntry = ftell(fp);
1541    // Warning: we believe this is explicit VR (Value Representation) because
1542    // we used a heuristic that found "UL" in the first tag. Alas this
1543    // doesn't guarantee that all the tags will be in explicit VR. In some
1544    // cases (see e-film filtered files) one finds implicit VR tags mixed
1545    // within an explicit VR file. Hence we make sure the present tag
1546    // is in explicit VR and try to fix things if it happens not to be
1547    // the case.
1548    
1549    (void)fread (&VR, (size_t)2,(size_t)1, fp);
1550    VR[2]=0;
1551    if(!CheckHeaderEntryVR(Entry,VR))
1552    {
1553       fseek(fp, PositionOnEntry, SEEK_SET);
1554       // When this element is known in the dictionary we shall use, e.g. for
1555       // the semantics (see the usage of IsAnInteger), the VR proposed by the
1556       // dictionary entry. Still we have to flag the element as implicit since
1557       // we know now our assumption on expliciteness is not furfilled.
1558       // avoid  .
1559       if ( Entry->IsVRUnknown() )
1560          Entry->SetVR("Implicit");
1561       Entry->SetImplicitVR();
1562    }
1563 }
1564
1565 /**
1566  * \ingroup   gdcmParser
1567  * \brief     Check the correspondance between the VR of the header entry
1568  *            and the taken VR. If they are different, the header entry is 
1569  *            updated with the new VR.
1570  * @param     Entry Header Entry to check
1571  * @param     vr    Dicom Value Representation
1572  * @return    false if the VR is incorrect of if the VR isn't referenced
1573  *            otherwise, it returns true
1574 */
1575 bool gdcmParser::CheckHeaderEntryVR(gdcmHeaderEntry *Entry, VRKey vr)
1576 {
1577    char msg[100]; // for sprintf
1578    bool RealExplicit = true;
1579
1580    // Assume we are reading a falsely explicit VR file i.e. we reached
1581    // a tag where we expect reading a VR but are in fact we read the
1582    // first to bytes of the length. Then we will interogate (through find)
1583    // the dicom_vr dictionary with oddities like "\004\0" which crashes
1584    // both GCC and VC++ implementations of the STL map. Hence when the
1585    // expected VR read happens to be non-ascii characters we consider
1586    // we hit falsely explicit VR tag.
1587
1588    if ( (!isalpha(vr[0])) && (!isalpha(vr[1])) )
1589       RealExplicit = false;
1590
1591    // CLEANME searching the dicom_vr at each occurence is expensive.
1592    // PostPone this test in an optional integrity check at the end
1593    // of parsing or only in debug mode.
1594    if ( RealExplicit && !gdcmGlobal::GetVR()->Count(vr) )
1595       RealExplicit= false;
1596
1597    if ( !RealExplicit ) 
1598    {
1599       // We thought this was explicit VR, but we end up with an
1600       // implicit VR tag. Let's backtrack.   
1601       sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n", 
1602                    Entry->GetGroup(),Entry->GetElement());
1603       dbg.Verbose(1, "gdcmParser::FindVR: ",msg);
1604       if (Entry->GetGroup()%2 && Entry->GetElement() == 0x0000) { // Group length is UL !
1605          gdcmDictEntry* NewEntry = NewVirtualDictEntry(
1606                                    Entry->GetGroup(),Entry->GetElement(),
1607                                    "UL","FIXME","Group Length");
1608          Entry->SetDictEntry(NewEntry);     
1609       }
1610       return(false);
1611    }
1612
1613    if ( Entry->IsVRUnknown() ) 
1614    {
1615       // When not a dictionary entry, we can safely overwrite the VR.
1616       if (Entry->GetElement() == 0x0000) { // Group length is UL !
1617          Entry->SetVR("UL");
1618       } else {
1619          Entry->SetVR(vr);
1620       }
1621    }
1622    else if ( Entry->GetVR() != vr ) 
1623    {
1624       // The VR present in the file and the dictionary disagree. We assume
1625       // the file writer knew best and use the VR of the file. Since it would
1626       // be unwise to overwrite the VR of a dictionary (since it would
1627       // compromise it's next user), we need to clone the actual DictEntry
1628       // and change the VR for the read one.
1629       gdcmDictEntry* NewEntry = NewVirtualDictEntry(
1630                                  Entry->GetGroup(),Entry->GetElement(),
1631                                  vr,"FIXME",Entry->GetName());
1632       Entry->SetDictEntry(NewEntry);
1633    }
1634    return(true); 
1635 }
1636
1637 /**
1638  * \ingroup gdcmParser
1639  * \brief   Get the transformed value of the header entry. The VR value 
1640  *          is used to define the transformation to operate on the value
1641  * \warning NOT end user intended method !
1642  * @param   Entry 
1643  * @return  Transformed entry value
1644  */
1645 std::string gdcmParser::GetHeaderEntryValue(gdcmHeaderEntry *Entry)
1646 {
1647    if ( (IsHeaderEntryAnInteger(Entry)) && (Entry->IsImplicitVR()) )
1648    {
1649       std::string val=Entry->GetValue();
1650       std::string vr=Entry->GetVR();
1651       guint32 length = Entry->GetLength();
1652       std::ostringstream s;
1653       int nbInt;
1654
1655    // When short integer(s) are expected, read and convert the following 
1656    // n * 2 bytes properly i.e. as a multivaluated strings
1657    // (each single value is separated fromthe next one by '\'
1658    // as usual for standard multivaluated filels
1659    // Elements with Value Multiplicity > 1
1660    // contain a set of short integers (not a single one) 
1661    
1662       if (vr == "US" || vr == "SS")
1663       {
1664          guint16 NewInt16;
1665
1666          nbInt = length / 2;
1667          for (int i=0; i < nbInt; i++) 
1668          {
1669             if(i!=0)
1670                s << '\\';
1671             NewInt16 = (val[2*i+0]&0xFF)+((val[2*i+1]&0xFF)<<8);
1672             NewInt16 = SwapShort(NewInt16);
1673             s << NewInt16;
1674          }
1675       }
1676
1677    // When integer(s) are expected, read and convert the following 
1678    // n * 4 bytes properly i.e. as a multivaluated strings
1679    // (each single value is separated fromthe next one by '\'
1680    // as usual for standard multivaluated filels
1681    // Elements with Value Multiplicity > 1
1682    // contain a set of integers (not a single one) 
1683       else if (vr == "UL" || vr == "SL")
1684       {
1685          guint32 NewInt32;
1686
1687          nbInt = length / 4;
1688          for (int i=0; i < nbInt; i++) 
1689          {
1690             if(i!=0)
1691                s << '\\';
1692             NewInt32= (val[4*i+0]&0xFF)+((val[4*i+1]&0xFF)<<8)+
1693                      ((val[4*i+2]&0xFF)<<16)+((val[4*i+3]&0xFF)<<24);
1694             NewInt32=SwapLong(NewInt32);
1695             s << NewInt32;
1696          }
1697       }
1698 #ifdef GDCM_NO_ANSI_STRING_STREAM
1699       s << std::ends; // to avoid oddities on Solaris
1700 #endif //GDCM_NO_ANSI_STRING_STREAM
1701       return(s.str());
1702    }
1703
1704    return(Entry->GetValue());
1705 }
1706
1707 /**
1708  * \ingroup gdcmParser
1709  * \brief   Get the reverse transformed value of the header entry. The VR 
1710  *          value is used to define the reverse transformation to operate on
1711  *          the value
1712  * \warning NOT end user intended method !
1713  * @param   Entry 
1714  * @return  Reverse transformed entry value
1715  */
1716 std::string gdcmParser::GetHeaderEntryUnvalue(gdcmHeaderEntry *Entry)
1717 {
1718    if ( (IsHeaderEntryAnInteger(Entry)) && (Entry->IsImplicitVR()) )
1719    {
1720       std::string vr=Entry->GetVR();
1721       std::ostringstream s;
1722       std::vector<std::string> tokens;
1723
1724       if (vr == "US" || vr == "SS") 
1725       {
1726          guint16 NewInt16;
1727
1728          tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1729          Tokenize (Entry->GetValue(), tokens, "\\");
1730          for (unsigned int i=0; i<tokens.size();i++) 
1731          {
1732             NewInt16 = atoi(tokens[i].c_str());
1733             s<<(NewInt16&0xFF)<<((NewInt16>>8)&0xFF);
1734          }
1735          tokens.clear();
1736       }
1737       if (vr == "UL" || vr == "SL") 
1738       {
1739          guint32 NewInt32;
1740
1741          tokens.erase(tokens.begin(),tokens.end()); // clean any previous value
1742          Tokenize (Entry->GetValue(), tokens, "\\");
1743          for (unsigned int i=0; i<tokens.size();i++) 
1744          {
1745             NewInt32 = atoi(tokens[i].c_str());
1746             s<<(char)(NewInt32&0xFF)<<(char)((NewInt32>>8)&0xFF)
1747                <<(char)((NewInt32>>16)&0xFF)<<(char)((NewInt32>>24)&0xFF);
1748          }
1749          tokens.clear();
1750       }
1751
1752 #ifdef GDCM_NO_ANSI_STRING_STREAM
1753       s << std::ends; // to avoid oddities on Solaris
1754 #endif //GDCM_NO_ANSI_STRING_STREAM
1755       return(s.str());
1756    }
1757
1758    return(Entry->GetValue());
1759 }
1760
1761 /**
1762  * \ingroup gdcmParser
1763  * \brief   Skip a given Header Entry 
1764  * \warning NOT end user intended method !
1765  * @param   entry 
1766  */
1767 void gdcmParser::SkipHeaderEntry(gdcmHeaderEntry *entry) 
1768 {
1769     SkipBytes(entry->GetLength());
1770 }
1771
1772 /**
1773  * \ingroup gdcmParser
1774  * \brief   When the length of an element value is obviously wrong (because
1775  *          the parser went Jabberwocky) one can hope improving things by
1776  *          applying this heuristic.
1777  */
1778 void gdcmParser::FixHeaderEntryFoundLength(gdcmHeaderEntry *Entry, guint32 FoundLength) 
1779 {
1780    Entry->SetReadLength(FoundLength); // will be updated only if a bug is found        
1781    if ( FoundLength == 0xffffffff) {
1782       FoundLength = 0;
1783    }
1784    
1785    guint16 gr =Entry->GetGroup();
1786    guint16 el =Entry->GetElement(); 
1787      
1788    if (FoundLength%2) {
1789       std::ostringstream s;
1790       s << "Warning : Tag with uneven length " << FoundLength 
1791          <<  " in x(" << std::hex << gr << "," << el <<")" << std::dec;
1792       dbg.Verbose(0,s.str().c_str());
1793    }
1794       
1795    // Sorry for the patch!  
1796    // XMedCom did the trick to read some naughty GE images ...
1797    if (FoundLength == 13) {
1798       // The following 'if' will be removed when there is no more
1799       // images on Creatis HDs with a 13 length for Manufacturer...
1800       if ( (Entry->GetGroup() != 0x0008) ||  
1801            ( (Entry->GetElement() != 0x0070) && (Entry->GetElement() != 0x0080) ) ){
1802       // end of remove area
1803          FoundLength =10;
1804          Entry->SetReadLength(10); // a bug is to be fixed
1805       }
1806    }
1807
1808    // to fix some garbage 'Leonardo' Siemens images
1809    // May be commented out to avoid overhead
1810    else if ( (Entry->GetGroup() == 0x0009) &&
1811        ( (Entry->GetElement() == 0x1113) || (Entry->GetElement() == 0x1114) ) ){
1812       FoundLength =4;
1813       Entry->SetReadLength(4); // a bug is to be fixed 
1814    } 
1815    // end of fix
1816  
1817    // to try to 'go inside' SeQuences (with length), and not to skip them        
1818    else if ( Entry->GetVR() == "SQ") 
1819    { 
1820       if (enableSequences)    // only if the user does want to !
1821          FoundLength =0;      // ReadLength is unchanged 
1822    } 
1823     
1824    // we found a 'delimiter' element                                         
1825    // fffe|xxxx is just a marker, we don't take its length into account                                                   
1826    else if(Entry->GetGroup() == 0xfffe)
1827    {    
1828                                          // *normally, fffe|0000 doesn't exist ! 
1829      if( Entry->GetElement() != 0x0000 ) // gdcm-MR-PHILIPS-16-Multi-Seq.dcm
1830                                          // causes extra troubles :-(                                                                   
1831         FoundLength =0;
1832    } 
1833            
1834    Entry->SetUsableLength(FoundLength);
1835 }
1836
1837 /**
1838  * \ingroup gdcmParser
1839  * \brief   Apply some heuristics to predict whether the considered 
1840  *          element value contains/represents an integer or not.
1841  * @param   Entry The element value on which to apply the predicate.
1842  * @return  The result of the heuristical predicate.
1843  */
1844 bool gdcmParser::IsHeaderEntryAnInteger(gdcmHeaderEntry *Entry) {
1845    guint16 element = Entry->GetElement();
1846    guint16 group   = Entry->GetGroup();
1847    std::string  vr = Entry->GetVR();
1848    guint32 length  = Entry->GetLength();
1849    // When we have some semantics on the element we just read, and if we
1850    // a priori know we are dealing with an integer, then we shall be
1851    // able to swap it's element value properly.
1852    if ( element == 0 )  // This is the group length of the group
1853    {  
1854       if (length == 4)
1855          return true;
1856       else 
1857       {
1858          std::ostringstream s;
1859          int filePosition = ftell(fp);
1860          s << "Erroneous Group Length element length  on : (" \
1861            << std::hex << group << " , " << element 
1862            << ") -before- position x(" << filePosition << ")"
1863            << "lgt : " << length;
1864          // These 2 lines commented out : a *very dirty* patch
1865          // to go on PrintHeader'ing gdcm-MR-PHILIPS-16-Multi-Seq.dcm.
1866          // have a glance at offset  x(8336) ...
1867          // For *regular* headers, the test is useless..
1868          // lets's print a warning message and go on, 
1869          // instead of giving up with an error message
1870
1871          //std::cout << s.str().c_str() << std::endl;
1872          // dbg.Error("gdcmParser::IsHeaderEntryAnInteger",
1873          //           s.str().c_str());     
1874       }
1875    }
1876    if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
1877       return true;
1878    
1879    return false;
1880 }
1881 /**
1882  * \ingroup gdcmParser
1883  * \brief  Find the Length till the next sequence delimiter
1884  * \warning NOT end user intended method !
1885  * @return 
1886  */
1887
1888  guint32 gdcmParser::FindHeaderEntryLengthOB(void)  {
1889    // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
1890    guint16 g;
1891    guint16 n; 
1892    long PositionOnEntry = ftell(fp);
1893    bool FoundSequenceDelimiter = false;
1894    guint32 TotalLength = 0;
1895    guint32 ItemLength;
1896
1897    while ( ! FoundSequenceDelimiter) 
1898    {
1899       g = ReadInt16();
1900       n = ReadInt16();   
1901       if (errno == 1)
1902          return 0;
1903       TotalLength += 4;  // We even have to decount the group and element 
1904      
1905       if ( g != 0xfffe && g!=0xb00c ) //for bogus header  
1906       {
1907          char msg[100]; // for sprintf. Sorry
1908          sprintf(msg,"wrong group (%04x) for an item sequence (%04x,%04x)\n",g, g,n);
1909          dbg.Verbose(1, "gdcmParser::FindLengthOB: ",msg); 
1910          errno = 1;
1911          return 0;
1912       }
1913       if ( n == 0xe0dd || ( g==0xb00c && n==0x0eb6 ) ) // for bogus header 
1914          FoundSequenceDelimiter = true;
1915       else if ( n != 0xe000 )
1916       {
1917          char msg[100];  // for sprintf. Sorry
1918          sprintf(msg,"wrong element (%04x) for an item sequence (%04x,%04x)\n",
1919                       n, g,n);
1920          dbg.Verbose(1, "gdcmParser::FindLengthOB: ",msg);
1921          errno = 1;
1922          return 0;
1923       }
1924       ItemLength = ReadInt32();
1925       TotalLength += ItemLength + 4;  // We add 4 bytes since we just read
1926                                       // the ItemLength with ReadInt32                                     
1927       SkipBytes(ItemLength);
1928    }
1929    fseek(fp, PositionOnEntry, SEEK_SET);
1930    return TotalLength;
1931 }
1932
1933 /**
1934  * \ingroup gdcmParser
1935  * \brief Reads a supposed to be 16 Bits integer
1936  *       (swaps it depending on processor endianity) 
1937  * @return read value
1938  */
1939 guint16 gdcmParser::ReadInt16(void) {
1940    guint16 g;
1941    size_t item_read;
1942    item_read = fread (&g, (size_t)2,(size_t)1, fp);
1943    if ( item_read != 1 ) {
1944       if(ferror(fp)) 
1945          dbg.Verbose(0, "gdcmParser::ReadInt16", " File Error");
1946       errno = 1;
1947       return 0;
1948    }
1949    errno = 0;
1950    g = SwapShort(g);   
1951    return g;
1952 }
1953
1954 /**
1955  * \ingroup gdcmParser
1956  * \brief  Reads a supposed to be 32 Bits integer
1957  *         (swaps it depending on processor endianity)  
1958  * @return read value
1959  */
1960 guint32 gdcmParser::ReadInt32(void) {
1961    guint32 g;
1962    size_t item_read;
1963    item_read = fread (&g, (size_t)4,(size_t)1, fp);
1964    if ( item_read != 1 ) { 
1965      if(ferror(fp)) 
1966          dbg.Verbose(0, "gdcmParser::ReadInt32", " File Error");   
1967       errno = 1;
1968       return 0;
1969    }
1970    errno = 0;   
1971    g = SwapLong(g);
1972    return g;
1973 }
1974
1975 /**
1976  * \ingroup gdcmParser
1977  * \brief skips bytes inside the source file 
1978  * \warning NOT end user intended method !
1979  * @return 
1980  */
1981 void gdcmParser::SkipBytes(guint32 NBytes) {
1982    //FIXME don't dump the returned value
1983    (void)fseek(fp, (long)NBytes, SEEK_CUR);
1984 }
1985
1986 /**
1987  * \ingroup gdcmParser
1988  * \brief Loads all the needed Dictionaries
1989  * \warning NOT end user intended method !   
1990  */
1991 void gdcmParser::Initialise(void) 
1992 {
1993    RefPubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
1994    RefShaDict = (gdcmDict*)0;
1995 }
1996
1997 /**
1998  * \ingroup gdcmParser
1999  * \brief   Discover what the swap code is (among little endian, big endian,
2000  *          bad little endian, bad big endian).
2001  *          sw is set
2002  * @return false when we are absolutely sure 
2003  *               it's neither ACR-NEMA nor DICOM
2004  *         true  when we hope ours assuptions are OK
2005  */
2006 bool gdcmParser::CheckSwap() {
2007
2008    // The only guaranted way of finding the swap code is to find a
2009    // group tag since we know it's length has to be of four bytes i.e.
2010    // 0x00000004. Finding the swap code in then straigthforward. Trouble
2011    // occurs when we can't find such group...
2012    
2013    guint32  x=4;  // x : for ntohs
2014    bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
2015    guint32  s32;
2016    guint16  s16;
2017        
2018    int lgrLue;
2019    char *entCur;
2020    char deb[HEADER_LENGTH_TO_READ];
2021     
2022    // First, compare HostByteOrder and NetworkByteOrder in order to
2023    // determine if we shall need to swap bytes (i.e. the Endian type).
2024    if (x==ntohs(x))
2025       net2host = true;
2026    else
2027       net2host = false; 
2028          
2029    // The easiest case is the one of a DICOM header, since it possesses a
2030    // file preamble where it suffice to look for the string "DICM".
2031    lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
2032    
2033    entCur = deb + 128;
2034    if(memcmp(entCur, "DICM", (size_t)4) == 0) {
2035       dbg.Verbose(1, "gdcmParser::CheckSwap:", "looks like DICOM Version3");
2036       
2037       // Next, determine the value representation (VR). Let's skip to the
2038       // first element (0002, 0000) and check there if we find "UL" 
2039       // - or "OB" if the 1st one is (0002,0001) -,
2040       // in which case we (almost) know it is explicit VR.
2041       // WARNING: if it happens to be implicit VR then what we will read
2042       // is the length of the group. If this ascii representation of this
2043       // length happens to be "UL" then we shall believe it is explicit VR.
2044       // FIXME: in order to fix the above warning, we could read the next
2045       // element value (or a couple of elements values) in order to make
2046       // sure we are not commiting a big mistake.
2047       // We need to skip :
2048       // * the 128 bytes of File Preamble (often padded with zeroes),
2049       // * the 4 bytes of "DICM" string,
2050       // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
2051       // i.e. a total of  136 bytes.
2052       entCur = deb + 136;
2053      
2054       // FIXME : FIXME:
2055       // Sometimes (see : gdcmData/icone.dcm) group 0x0002 *is* Explicit VR,
2056       // but elem 0002,0010 (Transfert Syntax) tells us the file is
2057       // *Implicit* VR.  -and it is !- 
2058       
2059       if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
2060           (memcmp(entCur, "OB", (size_t)2) == 0) ||
2061           (memcmp(entCur, "UI", (size_t)2) == 0) ||
2062           (memcmp(entCur, "CS", (size_t)2) == 0) )  // CS, to remove later
2063                                                     // when Write DCM *adds*
2064       // FIXME
2065       // Use gdcmParser::dicom_vr to test all the possibilities
2066       // instead of just checking for UL, OB and UI !? group 0000 
2067       {
2068          filetype = ExplicitVR;
2069          dbg.Verbose(1, "gdcmParser::CheckSwap:",
2070                      "explicit Value Representation");
2071       } 
2072       else 
2073       {
2074          filetype = ImplicitVR;
2075          dbg.Verbose(1, "gdcmParser::CheckSwap:",
2076                      "not an explicit Value Representation");
2077       }
2078       
2079       if (net2host) 
2080       {
2081          sw = 4321;
2082          dbg.Verbose(1, "gdcmParser::CheckSwap:",
2083                         "HostByteOrder != NetworkByteOrder");
2084       } 
2085       else 
2086       {
2087          sw = 0;
2088          dbg.Verbose(1, "gdcmParser::CheckSwap:",
2089                         "HostByteOrder = NetworkByteOrder");
2090       }
2091       
2092       // Position the file position indicator at first tag (i.e.
2093       // after the file preamble and the "DICM" string).
2094       rewind(fp);
2095       fseek (fp, 132L, SEEK_SET);
2096       return true;
2097    } // End of DicomV3
2098
2099    // Alas, this is not a DicomV3 file and whatever happens there is no file
2100    // preamble. We can reset the file position indicator to where the data
2101    // is (i.e. the beginning of the file).
2102    dbg.Verbose(1, "gdcmParser::CheckSwap:", "not a DICOM Version3 file");
2103    rewind(fp);
2104
2105    // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
2106    // By clean we mean that the length of the first tag is written down.
2107    // If this is the case and since the length of the first group HAS to be
2108    // four (bytes), then determining the proper swap code is straightforward.
2109
2110    entCur = deb + 4;
2111    // We assume the array of char we are considering contains the binary
2112    // representation of a 32 bits integer. Hence the following dirty
2113    // trick :
2114    s32 = *((guint32 *)(entCur));
2115       
2116    switch (s32) {
2117       case 0x00040000 :
2118          sw = 3412;
2119          filetype = ACR;
2120          return true;
2121       case 0x04000000 :
2122          sw = 4321;
2123          filetype = ACR;
2124          return true;
2125       case 0x00000400 :
2126          sw = 2143;
2127          filetype = ACR;
2128          return true;
2129       case 0x00000004 :
2130          sw = 0;
2131          filetype = ACR;
2132          return true;
2133       default :
2134
2135       // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
2136       // It is time for despaired wild guesses. 
2137       // So, let's check if this file wouldn't happen to be 'dirty' ACR/NEMA,
2138       //  i.e. the 'group length' element is not present :     
2139       
2140       //  check the supposed to be 'group number'
2141       //  0x0002 or 0x0004 or 0x0008
2142       //  to determine ' sw' value .
2143       //  Only 0 or 4321 will be possible 
2144       //  (no oportunity to check for the formerly well known
2145       //  ACR-NEMA 'Bad Big Endian' or 'Bad Little Endian' 
2146       //  if unsuccessfull (i.e. neither 0x0002 nor 0x0200 etc -4, 8-) 
2147       //  the file IS NOT ACR-NEMA nor DICOM V3
2148       //  Find a trick to tell it the caller...
2149       
2150       s16 = *((guint16 *)(deb));
2151       
2152       switch (s16) {
2153       case 0x0002 :
2154       case 0x0004 :
2155       case 0x0008 :      
2156          sw = 0;
2157          filetype = ACR;
2158          return true;
2159       case 0x0200 :
2160       case 0x0400 :
2161       case 0x0800 : 
2162          sw = 4321;
2163          filetype = ACR;
2164          return true;
2165       default :
2166          dbg.Verbose(0, "gdcmParser::CheckSwap:",
2167                      "ACR/NEMA unfound swap info (Really hopeless !)"); 
2168          filetype = Unknown;     
2169          return false;
2170       }
2171       
2172       // Then the only info we have is the net2host one.
2173       //if (! net2host )
2174          //   sw = 0;
2175          //else
2176          //  sw = 4321;
2177          //return;
2178    }
2179 }
2180
2181 /**
2182  * \ingroup gdcmParser
2183  * \brief Restore the unproperly loaded values i.e. the group, the element
2184  *        and the dictionary entry depending on them. 
2185  */
2186 void gdcmParser::SwitchSwapToBigEndian(void) 
2187 {
2188    dbg.Verbose(1, "gdcmParser::SwitchSwapToBigEndian",
2189                   "Switching to BigEndian mode.");
2190    if ( sw == 0    ) 
2191    {
2192       sw = 4321;
2193       return;
2194    }
2195    if ( sw == 4321 ) 
2196    {
2197       sw = 0;
2198       return;
2199    }
2200    if ( sw == 3412 ) 
2201    {
2202       sw = 2143;
2203       return;
2204    }
2205    if ( sw == 2143 )
2206       sw = 3412;
2207 }
2208
2209 /**
2210  * \ingroup gdcmParser
2211  * \brief  during parsing, Header Elements too long are not loaded in memory 
2212  * @param NewSize
2213  */
2214 void gdcmParser::SetMaxSizeLoadEntry(long NewSize) 
2215 {
2216    if (NewSize < 0)
2217       return;
2218    if ((guint32)NewSize >= (guint32)0xffffffff) 
2219    {
2220       MaxSizeLoadEntry = 0xffffffff;
2221       return;
2222    }
2223    MaxSizeLoadEntry = NewSize;
2224 }
2225
2226
2227 /**
2228  * \ingroup gdcmParser
2229  * \brief Header Elements too long will not be printed
2230  * \warning 
2231  * \todo : not yet usable 
2232  *          (see MAX_SIZE_PRINT_ELEMENT_VALUE 
2233  *           in gdcmHeaderEntry gdcmLoadEntry)
2234  *             
2235  * @param NewSize
2236  */
2237 void gdcmParser::SetMaxSizePrintEntry(long NewSize) 
2238 {
2239    if (NewSize < 0)
2240       return;
2241    if ((guint32)NewSize >= (guint32)0xffffffff) 
2242    {
2243       MaxSizePrintEntry = 0xffffffff;
2244       return;
2245    }
2246    MaxSizePrintEntry = NewSize;
2247 }
2248
2249 /**
2250  * \ingroup gdcmParser
2251  * \brief   Searches both the public and the shadow dictionary (when they
2252  *          exist) for the presence of the DictEntry with given name.
2253  *          The public dictionary has precedence on the shadow one.
2254  * @param   Name name of the searched DictEntry
2255  * @return  Corresponding DictEntry when it exists, NULL otherwise.
2256  */
2257 gdcmDictEntry *gdcmParser::GetDictEntryByName(std::string Name) 
2258 {
2259    gdcmDictEntry *found = (gdcmDictEntry *)0;
2260    if (!RefPubDict && !RefShaDict) 
2261    {
2262       dbg.Verbose(0, "gdcmParser::GetDictEntry",
2263                      "we SHOULD have a default dictionary");
2264    }
2265    if (RefPubDict) 
2266    {
2267       found = RefPubDict->GetDictEntryByName(Name);
2268       if (found)
2269          return found;
2270    }
2271    if (RefShaDict) 
2272    {
2273       found = RefShaDict->GetDictEntryByName(Name);
2274       if (found)
2275          return found;
2276    }
2277    return found;
2278 }
2279
2280 /**
2281  * \ingroup gdcmParser
2282  * \brief   Searches both the public and the shadow dictionary (when they
2283  *          exist) for the presence of the DictEntry with given
2284  *          group and element. The public dictionary has precedence on the
2285  *          shadow one.
2286  * @param   group   group of the searched DictEntry
2287  * @param   element element of the searched DictEntry
2288  * @return  Corresponding DictEntry when it exists, NULL otherwise.
2289  */
2290 gdcmDictEntry *gdcmParser::GetDictEntryByNumber(guint16 group,guint16 element) 
2291 {
2292    gdcmDictEntry *found = (gdcmDictEntry *)0;
2293    if (!RefPubDict && !RefShaDict) 
2294    {
2295       dbg.Verbose(0, "gdcmParser::GetDictEntry",
2296                      "we SHOULD have a default dictionary");
2297    }
2298    if (RefPubDict) 
2299    {
2300       found = RefPubDict->GetDictEntryByNumber(group, element);
2301       if (found)
2302          return found;
2303    }
2304    if (RefShaDict) 
2305    {
2306       found = RefShaDict->GetDictEntryByNumber(group, element);
2307       if (found)
2308          return found;
2309    }
2310    return found;
2311 }
2312
2313 /**
2314  * \ingroup gdcmParser
2315  * \brief   Read the next tag but WITHOUT loading it's value
2316  * @return  On succes the newly created HeaderEntry, NULL on failure.      
2317  */
2318 gdcmHeaderEntry *gdcmParser::ReadNextHeaderEntry(void) {
2319    guint16 g,n;
2320    gdcmHeaderEntry *NewEntry;
2321    g = ReadInt16();
2322    n = ReadInt16();
2323       
2324    if (errno == 1)
2325       // We reached the EOF (or an error occured) therefore 
2326       // header parsing has to be considered as finished.
2327       return (gdcmHeaderEntry *)0;
2328
2329 // Pb : how to propagate the element length (used in SkipHeaderEntry)
2330 //       direct call to SkipBytes ?
2331    
2332 //   if (ignoreShadow == 1 && g%2 ==1)
2333       // if user wants to skip shadow groups
2334       // and current element *is* a shadow element
2335       // we don't create anything
2336 //      return (gdcmHeaderEntry *)1; // to tell caller it's NOT finished
2337   
2338    NewEntry = NewHeaderEntryByNumber(g, n);
2339    FindHeaderEntryVR(NewEntry);
2340    FindHeaderEntryLength(NewEntry);
2341
2342    if (errno == 1) {
2343       // Call it quits
2344       return NULL;
2345    }
2346    NewEntry->SetOffset(ftell(fp));  
2347    return NewEntry;
2348 }
2349
2350 /**
2351  * \ingroup gdcmParser
2352  * \brief   Build a new Element Value from all the low level arguments. 
2353  *          Check for existence of dictionary entry, and build
2354  *          a default one when absent.
2355  * @param   Name    Name of the underlying DictEntry
2356  */
2357 gdcmHeaderEntry *gdcmParser::NewHeaderEntryByName(std::string Name) 
2358 {
2359    gdcmDictEntry *NewTag = GetDictEntryByName(Name);
2360    if (!NewTag)
2361       NewTag = NewVirtualDictEntry(0xffff, 0xffff, "LO", "unkn", Name);
2362
2363    gdcmHeaderEntry* NewEntry = new gdcmHeaderEntry(NewTag);
2364    if (!NewEntry) 
2365    {
2366       dbg.Verbose(1, "gdcmParser::ObtainHeaderEntryByName",
2367                   "failed to allocate gdcmHeaderEntry");
2368       return (gdcmHeaderEntry *)0;
2369    }
2370    return NewEntry;
2371 }  
2372
2373 /**
2374  * \ingroup gdcmParser
2375  * \brief   Request a new virtual dict entry to the dict set
2376  * @param   group  group   of the underlying DictEntry
2377  * @param   element  element of the underlying DictEntry
2378  * @param   vr     VR of the underlying DictEntry
2379  * @param   fourth owner group
2380  * @param   name   english name
2381  */
2382 gdcmDictEntry *gdcmParser::NewVirtualDictEntry(guint16 group, guint16 element,
2383                                                std::string vr,
2384                                                std::string fourth,
2385                                                std::string name)
2386 {
2387    return gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,element,vr,fourth,name);
2388 }
2389
2390 /**
2391  * \ingroup gdcmParser
2392  * \brief   Build a new Element Value from all the low level arguments. 
2393  *          Check for existence of dictionary entry, and build
2394  *          a default one when absent.
2395  * @param   Group group   of the underlying DictEntry
2396  * @param   Elem  element of the underlying DictEntry
2397  */
2398 gdcmHeaderEntry *gdcmParser::NewHeaderEntryByNumber(guint16 Group, guint16 Elem) 
2399 {
2400    // Find out if the tag we encountered is in the dictionaries:
2401    gdcmDictEntry *DictEntry = GetDictEntryByNumber(Group, Elem);
2402    if (!DictEntry)
2403       DictEntry = NewVirtualDictEntry(Group, Elem);
2404
2405    gdcmHeaderEntry *NewEntry = new gdcmHeaderEntry(DictEntry);
2406    if (!NewEntry) 
2407    {
2408       dbg.Verbose(1, "gdcmParser::NewHeaderEntryByNumber",
2409                   "failed to allocate gdcmHeaderEntry");
2410       return NULL;
2411    }
2412    return NewEntry;
2413 }
2414
2415 // Never used; commented out, waiting for removal.
2416 /**
2417  * \ingroup gdcmParser
2418  * \brief   Small utility function that creates a new manually crafted
2419  *          (as opposed as read from the file) gdcmHeaderEntry with user
2420  *          specified name and adds it to the public tag hash table.
2421  * \note    A fake TagKey is generated so the PubDict can keep it's coherence.
2422  * @param   NewTagName The name to be given to this new tag.
2423  * @param   VR The Value Representation to be given to this new tag.
2424  * @return  The newly hand crafted Element Value.
2425  */
2426 //gdcmHeaderEntry *gdcmParser::NewManualHeaderEntryToPubDict(std::string NewTagName, 
2427 //                                                           std::string VR) 
2428 //{
2429 //   gdcmHeaderEntry *NewEntry = NULL;
2430 //   guint32 StuffGroup = 0xffff;   // Group to be stuffed with additional info
2431 //   guint32 FreeElem = 0;
2432 //   gdcmDictEntry *DictEntry = NULL;
2433 //
2434 //   FreeElem = GenerateFreeTagKeyInGroup(StuffGroup);
2435 //   if (FreeElem == UINT32_MAX) 
2436 //   {
2437 //      dbg.Verbose(1, "gdcmHeader::NewManualHeaderEntryToPubDict",
2438 //                     "Group 0xffff in Public Dict is full");
2439 //      return NULL;
2440 //   }
2441 //
2442 //   DictEntry = NewVirtualDictEntry(StuffGroup, FreeElem,
2443 //                                VR, "GDCM", NewTagName);
2444 //   NewEntry = new gdcmHeaderEntry(DictEntry);
2445 //   AddHeaderEntry(NewEntry);
2446 //   return NewEntry;
2447 //}
2448
2449 /**
2450  * \ingroup gdcmParser
2451  * \brief   Generate a free TagKey i.e. a TagKey that is not present
2452  *          in the TagHt dictionary.
2453  * @param   group The generated tag must belong to this group.  
2454  * @return  The element of tag with given group which is fee.
2455  */
2456 guint32 gdcmParser::GenerateFreeTagKeyInGroup(guint16 group) 
2457 {
2458    for (guint32 elem = 0; elem < UINT32_MAX; elem++) 
2459    {
2460       TagKey key = gdcmDictEntry::TranslateToKey(group, elem);
2461       if (tagHT.count(key) == 0)
2462          return elem;
2463    }
2464    return UINT32_MAX;
2465 }
2466
2467 //-----------------------------------------------------------------------------