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