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