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