]> Creatis software - gdcm.git/blob - src/gdcmHeader.cxx
* src/*.[h] all occurences of stl classes are now prefixed with
[gdcm.git] / src / gdcmHeader.cxx
1 // $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.67 2003/05/21 16:26:28 regrain Exp $
2
3 #include <stdio.h>
4 #include <cerrno>
5 // For nthos:
6 #ifdef _MSC_VER
7 #include <winsock.h>
8 #else
9 #include <netinet/in.h>
10 #endif
11 #include <cctype>    // for isalpha
12 #include <sstream>
13 #include "gdcmUtil.h"
14 #include "gdcmHeader.h"
15 using namespace std;
16
17 // Refer to gdcmHeader::CheckSwap()
18 #define HEADER_LENGTH_TO_READ       256
19 // Refer to gdcmHeader::SetMaxSizeLoadElementValue()
20 #define _MaxSizeLoadElementValue_   1024
21
22 void gdcmHeader::Initialise(void) {
23    dicom_vr = gdcmGlobal::GetVR();
24    Dicts = gdcmGlobal::GetDicts();
25    RefPubDict = Dicts->GetDefaultPubDict();
26    RefShaDict = (gdcmDict*)0;
27 }
28
29 gdcmHeader::gdcmHeader(const char *InFilename, bool exception_on_error) {
30   SetMaxSizeLoadElementValue(_MaxSizeLoadElementValue_);
31   filename = InFilename;
32   Initialise();
33   if ( !OpenFile(exception_on_error))
34      return;
35   ParseHeader();
36   LoadElements();
37   CloseFile();
38 }
39
40 bool gdcmHeader::OpenFile(bool exception_on_error)
41   throw(gdcmFileError) {
42   fp=fopen(filename.c_str(),"rb");
43   if(exception_on_error) {
44     if(!fp)
45       throw gdcmFileError("gdcmHeader::gdcmHeader(const char *, bool)");
46   }
47   if ( fp )
48      return true;
49   dbg.Verbose(0, "gdcmHeader::gdcmHeader cannot open file", filename.c_str());
50   return false;
51 }
52
53 bool gdcmHeader::CloseFile(void) {
54   int closed = fclose(fp);
55   fp = (FILE *)0;
56   if (! closed)
57      return false;
58   return true;
59 }
60
61 gdcmHeader::~gdcmHeader (void) {
62    dicom_vr = (gdcmVR*)0;
63    Dicts    = (gdcmDictSet*)0;
64    RefPubDict = (gdcmDict*)0;
65    RefShaDict = (gdcmDict*)0;
66    return;
67 }
68
69 // Fourth semantics:
70 // CMD      Command        
71 // META     Meta Information 
72 // DIR      Directory
73 // ID
74 // PAT      Patient
75 // ACQ      Acquisition
76 // REL      Related
77 // IMG      Image
78 // SDY      Study
79 // VIS      Visit 
80 // WAV      Waveform
81 // PRC
82 // DEV      Device
83 // NMI      Nuclear Medicine
84 // MED
85 // BFS      Basic Film Session
86 // BFB      Basic Film Box
87 // BIB      Basic Image Box
88 // BAB
89 // IOB
90 // PJ
91 // PRINTER
92 // RT       Radio Therapy
93 // DVH   
94 // SSET
95 // RES      Results
96 // CRV      Curve
97 // OLY      Overlays
98 // PXL      Pixels
99 //
100
101 /**
102  * \ingroup gdcmHeader
103  * \brief   Discover what the swap code is (among little endian, big endian,
104  *          bad little endian, bad big endian).
105  *
106  */
107 void gdcmHeader::CheckSwap()
108 {
109    // The only guaranted way of finding the swap code is to find a
110    // group tag since we know it's length has to be of four bytes i.e.
111    // 0x00000004. Finding the swap code in then straigthforward. Trouble
112    // occurs when we can't find such group...
113    guint32  s;
114    guint32  x=4;  // x : pour ntohs
115    bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
116     
117    int lgrLue;
118    char * entCur;
119    char deb[HEADER_LENGTH_TO_READ];
120     
121    // First, compare HostByteOrder and NetworkByteOrder in order to
122    // determine if we shall need to swap bytes (i.e. the Endian type).
123    if (x==ntohs(x))
124       net2host = true;
125    else
126       net2host = false;
127    
128    // The easiest case is the one of a DICOM header, since it possesses a
129    // file preamble where it suffice to look for the string "DICM".
130    lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
131    
132    entCur = deb + 128;
133    if(memcmp(entCur, "DICM", (size_t)4) == 0) {
134       dbg.Verbose(1, "gdcmHeader::CheckSwap:", "looks like DICOM Version3");
135       // Next, determine the value representation (VR). Let's skip to the
136       // first element (0002, 0000) and check there if we find "UL" 
137       // - or "OB" if the 1st one is (0002,0001) -,
138       // in which case we (almost) know it is explicit VR.
139       // WARNING: if it happens to be implicit VR then what we will read
140       // is the length of the group. If this ascii representation of this
141       // length happens to be "UL" then we shall believe it is explicit VR.
142       // FIXME: in order to fix the above warning, we could read the next
143       // element value (or a couple of elements values) in order to make
144       // sure we are not commiting a big mistake.
145       // We need to skip :
146       // * the 128 bytes of File Preamble (often padded with zeroes),
147       // * the 4 bytes of "DICM" string,
148       // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
149       // i.e. a total of  136 bytes.
150       entCur = deb + 136;
151       // FIXME
152       // Use gdcmHeader::dicom_vr to test all the possibilities
153       // instead of just checking for UL, OB and UI !?
154       if(  (memcmp(entCur, "UL", (size_t)2) == 0) ||
155           (memcmp(entCur, "OB", (size_t)2) == 0) ||
156           (memcmp(entCur, "UI", (size_t)2) == 0) )
157         {
158          filetype = ExplicitVR;
159          dbg.Verbose(1, "gdcmHeader::CheckSwap:",
160                      "explicit Value Representation");
161       } else {
162          filetype = ImplicitVR;
163          dbg.Verbose(1, "gdcmHeader::CheckSwap:",
164                      "not an explicit Value Representation");
165       }
166
167       if (net2host) {
168          sw = 4321;
169          dbg.Verbose(1, "gdcmHeader::CheckSwap:",
170                         "HostByteOrder != NetworkByteOrder");
171       } else {
172          sw = 0;
173          dbg.Verbose(1, "gdcmHeader::CheckSwap:",
174                         "HostByteOrder = NetworkByteOrder");
175       }
176       
177       // Position the file position indicator at first tag (i.e.
178       // after the file preamble and the "DICM" string).
179       rewind(fp);
180       fseek (fp, 132L, SEEK_SET);
181       return;
182    } // End of DicomV3
183
184    // Alas, this is not a DicomV3 file and whatever happens there is no file
185    // preamble. We can reset the file position indicator to where the data
186    // is (i.e. the beginning of the file).
187     dbg.Verbose(1, "gdcmHeader::CheckSwap:", "not a DICOM Version3 file");
188    rewind(fp);
189
190    // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
191    // By clean we mean that the length of the first tag is written down.
192    // If this is the case and since the length of the first group HAS to be
193    // four (bytes), then determining the proper swap code is straightforward.
194
195    entCur = deb + 4;
196    // We assume the array of char we are considering contains the binary
197    // representation of a 32 bits integer. Hence the following dirty
198    // trick :
199    s = *((guint32 *)(entCur));
200    
201    switch (s) {
202    case 0x00040000 :
203       sw = 3412;
204       filetype = ACR;
205       return;
206    case 0x04000000 :
207       sw = 4321;
208       filetype = ACR;
209       return;
210    case 0x00000400 :
211       sw = 2143;
212       filetype = ACR;
213       return;
214    case 0x00000004 :
215       sw = 0;
216       filetype = ACR;
217       return;
218    default :
219       dbg.Verbose(0, "gdcmHeader::CheckSwap:",
220                      "ACR/NEMA unfound swap info (time to raise bets)");
221    }
222
223    // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
224    // It is time for despaired wild guesses. So, let's assume this file
225    // happens to be 'dirty' ACR/NEMA, i.e. the length of the group is
226    // not present. Then the only info we have is the net2host one.
227    filetype = Unknown;
228    if (! net2host )
229       sw = 0;
230    else
231       sw = 4321;
232    return;
233 }
234
235 void gdcmHeader::SwitchSwapToBigEndian(void) {
236    dbg.Verbose(1, "gdcmHeader::SwitchSwapToBigEndian",
237                   "Switching to BigEndian mode.");
238    if ( sw == 0    ) {
239       sw = 4321;
240       return;
241    }
242    if ( sw == 4321 ) {
243       sw = 0;
244       return;
245    }
246    if ( sw == 3412 ) {
247       sw = 2143;
248       return;
249    }
250    if ( sw == 2143 )
251       sw = 3412;
252 }
253
254 /**
255  * \ingroup   gdcmHeader
256  * \brief     Find the value representation of the current tag.
257  */
258 void gdcmHeader::FindVR( gdcmElValue *ElVal) {
259    if (filetype != ExplicitVR)
260       return;
261
262    char VR[3];
263    string vr;
264    int lgrLue;
265    long PositionOnEntry = ftell(fp);
266    // Warning: we believe this is explicit VR (Value Representation) because
267    // we used a heuristic that found "UL" in the first tag. Alas this
268    // doesn't guarantee that all the tags will be in explicit VR. In some
269    // cases (see e-film filtered files) one finds implicit VR tags mixed
270    // within an explicit VR file. Hence we make sure the present tag
271    // is in explicit VR and try to fix things if it happens not to be
272    // the case.
273    bool RealExplicit = true;
274    
275    lgrLue=fread (&VR, (size_t)2,(size_t)1, fp);
276    VR[2]=0;
277    vr = string(VR);
278       
279    // Assume we are reading a falsely explicit VR file i.e. we reached
280    // a tag where we expect reading a VR but are in fact we read the
281    // first to bytes of the length. Then we will interogate (through find)
282    // the dicom_vr dictionary with oddities like "\004\0" which crashes
283    // both GCC and VC++ implementations of the STL map. Hence when the
284    // expected VR read happens to be non-ascii characters we consider
285    // we hit falsely explicit VR tag.
286
287    if ( (!isalpha(VR[0])) && (!isalpha(VR[1])) )
288       RealExplicit = false;
289
290    // CLEANME searching the dicom_vr at each occurence is expensive.
291    // PostPone this test in an optional integrity check at the end
292    // of parsing or only in debug mode.
293    if ( RealExplicit && !dicom_vr->Count(vr) )
294       RealExplicit= false;
295
296    if ( RealExplicit ) {
297       if ( ElVal->IsVrUnknown() ) {
298          // When not a dictionary entry, we can safely overwrite the vr.
299          ElVal->SetVR(vr);
300          return; 
301       }
302       if ( ElVal->GetVR() == vr ) {
303          // The vr we just read and the dictionary agree. Nothing to do.
304          return;
305       }
306       // The vr present in the file and the dictionary disagree. We assume
307       // the file writer knew best and use the vr of the file. Since it would
308       // be unwise to overwrite the vr of a dictionary (since it would
309       // compromise it's next user), we need to clone the actual DictEntry
310       // and change the vr for the read one.
311       gdcmDictEntry* NewTag = new gdcmDictEntry(ElVal->GetGroup(),
312                                  ElVal->GetElement(),
313                                  vr,
314                                  "FIXME",
315                                  ElVal->GetName());
316       ElVal->SetDictEntry(NewTag);
317       return; 
318    }
319    
320    // We thought this was explicit VR, but we end up with an
321    // implicit VR tag. Let's backtrack.
322    dbg.Verbose(1, "gdcmHeader::FindVR:", "Falsely explicit vr file");
323    fseek(fp, PositionOnEntry, SEEK_SET);
324    // When this element is known in the dictionary we shall use, e.g. for
325    // the semantics (see  the usage of IsAnInteger), the vr proposed by the
326    // dictionary entry. Still we have to flag the element as implicit since
327    // we know now our assumption on expliciteness is not furfilled.
328    // avoid  .
329    if ( ElVal->IsVrUnknown() )
330       ElVal->SetVR("Implicit");
331    ElVal->SetImplicitVr();
332 }
333
334 /**
335  * \ingroup gdcmHeader
336  * \brief   Determines if the Transfer Syntax was allready encountered
337  *          and if it corresponds to a ImplicitVRLittleEndian one.
338  *
339  * @return  True when ImplicitVRLittleEndian found. False in all other cases.
340  */
341 bool gdcmHeader::IsImplicitVRLittleEndianTransferSyntax(void) {
342    gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
343    if ( !Element )
344       return false;
345    LoadElementValueSafe(Element);
346    string Transfer = Element->GetValue();
347    if ( Transfer == "1.2.840.10008.1.2" )
348       return true;
349    return false;
350 }
351
352 /**
353  * \ingroup gdcmHeader
354  * \brief   Determines if the Transfer Syntax was allready encountered
355  *          and if it corresponds to a ExplicitVRLittleEndian one.
356  *
357  * @return  True when ExplicitVRLittleEndian found. False in all other cases.
358  */
359 bool gdcmHeader::IsExplicitVRLittleEndianTransferSyntax(void) {
360    gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
361    if ( !Element )
362       return false;
363    LoadElementValueSafe(Element);
364    string Transfer = Element->GetValue();
365    if ( Transfer == "1.2.840.10008.1.2.1" )
366       return true;
367    return false;
368 }
369
370 /**
371  * \ingroup gdcmHeader
372  * \brief   Determines if the Transfer Syntax was allready encountered
373  *          and if it corresponds to a DeflatedExplicitVRLittleEndian one.
374  *
375  * @return  True when DeflatedExplicitVRLittleEndian found. False in all other cases.
376  */
377 bool gdcmHeader::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
378    gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
379    if ( !Element )
380       return false;
381    LoadElementValueSafe(Element);
382    string Transfer = Element->GetValue();
383    if ( Transfer == "1.2.840.10008.1.2.1.99" )
384       return true;
385    return false;
386 }
387
388 /**
389  * \ingroup gdcmHeader
390  * \brief   Determines if the Transfer Syntax was allready encountered
391  *          and if it corresponds to a Explicit VR Big Endian one.
392  *
393  * @return  True when big endian found. False in all other cases.
394  */
395 bool gdcmHeader::IsExplicitVRBigEndianTransferSyntax(void) {
396    gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
397    if ( !Element )
398       return false;
399    LoadElementValueSafe(Element);
400    string Transfer = Element->GetValue();
401    if ( Transfer == "1.2.840.10008.1.2.2" )  //1.2.2 ??? A verifier !
402       return true;
403    return false;
404 }
405
406 /**
407  * \ingroup gdcmHeader
408  * \brief   Determines if the Transfer Syntax was allready encountered
409  *          and if it corresponds to a JPEGBaseLineProcess1 one.
410  *
411  * @return  True when JPEGBaseLineProcess1found. False in all other cases.
412  */
413 bool gdcmHeader::IsJPEGBaseLineProcess1TransferSyntax(void) {
414    gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
415    if ( !Element )
416       return false;
417    LoadElementValueSafe(Element);
418    string Transfer = Element->GetValue();
419    if ( Transfer == "1.2.840.10008.1.2.4.50" )
420       return true;
421    return false;
422 }
423
424 // faire qq chose d'intelligent a la place de Ã§a
425
426 bool gdcmHeader::IsJPEGLossless(void) {
427    gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
428    if ( !Element )
429       return false;
430    LoadElementValueSafe(Element);
431    const char * Transfert = Element->GetValue().c_str();
432    printf("TransfertSyntx %s\n",Transfert);
433    if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true;
434    if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true;
435    return false;
436 }
437
438
439 /**
440  * \ingroup gdcmHeader
441  * \brief   Determines if the Transfer Syntax was allready encountered
442  *          and if it corresponds to a JPEGExtendedProcess2-4 one.
443  *
444  * @return  True when JPEGExtendedProcess2-4 found. False in all other cases.
445  */
446 bool gdcmHeader::IsJPEGExtendedProcess2_4TransferSyntax(void) {
447    gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
448    if ( !Element )
449       return false;
450    LoadElementValueSafe(Element);
451    string Transfer = Element->GetValue();
452    if ( Transfer == "1.2.840.10008.1.2.4.51" )
453       return true;
454    return false;
455 }
456
457 /**
458  * \ingroup gdcmHeader
459  * \brief   Determines if the Transfer Syntax was allready encountered
460  *          and if it corresponds to a JPEGExtendeProcess3-5 one.
461  *
462  * @return  True when JPEGExtendedProcess3-5 found. False in all other cases.
463  */
464 bool gdcmHeader::IsJPEGExtendedProcess3_5TransferSyntax(void) {
465    gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
466    if ( !Element )
467       return false;
468    LoadElementValueSafe(Element);
469    string Transfer = Element->GetValue();
470    if ( Transfer == "1.2.840.10008.1.2.4.52" )
471       return true;
472    return false;
473 }
474
475 /**
476  * \ingroup gdcmHeader
477  * \brief   Determines if the Transfer Syntax was allready encountered
478  *          and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
479  *
480  * @return  True when JPEGSpectralSelectionProcess6-8 found. False in all
481  *          other cases.
482  */
483 bool gdcmHeader::IsJPEGSpectralSelectionProcess6_8TransferSyntax(void) {
484    gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
485    if ( !Element )
486       return false;
487    LoadElementValueSafe(Element);
488    string Transfer = Element->GetValue();
489    if ( Transfer == "1.2.840.10008.1.2.4.53" )
490       return true;
491    return false;
492 }
493 /**
494  * \ingroup gdcmHeader
495  * \brief   Predicate for dicom version 3 file.
496  * @return  True when the file is a dicom version 3.
497  */
498 bool gdcmHeader::IsDicomV3(void) {
499    if (   (filetype == ExplicitVR)
500        || (filetype == ImplicitVR) )
501       return true;
502    return false;
503 }
504
505 /**
506  * \ingroup gdcmHeader
507  * \brief   When the length of an element value is obviously wrong (because
508  *          the parser went Jabberwocky) one can hope improving things by
509  *          applying this heuristic.
510  */
511 void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
512    if ( FoundLength == 0xffffffff)
513       FoundLength = 0;
514    ElVal->SetLength(FoundLength);
515 }
516
517 guint32 gdcmHeader::FindLengthOB(void) {
518    // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
519    guint16 g;
520    guint16 n; 
521    long PositionOnEntry = ftell(fp);
522    bool FoundSequenceDelimiter = false;
523    guint32 TotalLength = 0;
524    guint32 ItemLength;
525
526    while ( ! FoundSequenceDelimiter) {
527       g = ReadInt16();
528       n = ReadInt16();
529       if (errno == 1)
530          return 0;
531       TotalLength += 4;  // We even have to decount the group and element 
532       if ( g != 0xfffe ) {
533          dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",
534                      "wrong group for an item sequence.");
535          errno = 1;
536          return 0;
537       }
538       if ( n == 0xe0dd )
539          FoundSequenceDelimiter = true;
540       else if ( n != 0xe000) {
541          dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",
542                      "wrong element for an item sequence.");
543          errno = 1;
544          return 0;
545       }
546       ItemLength = ReadInt32();
547       TotalLength += ItemLength + 4;  // We add 4 bytes since we just read
548                                       // the ItemLength with ReadInt32
549       SkipBytes(ItemLength);
550    }
551    fseek(fp, PositionOnEntry, SEEK_SET);
552    return TotalLength;
553 }
554
555 void gdcmHeader::FindLength(gdcmElValue * ElVal) {
556    guint16 element = ElVal->GetElement();
557    string  vr      = ElVal->GetVR();
558    guint16 length16;
559    
560    if ( (filetype == ExplicitVR) && ! ElVal->IsImplicitVr() ) {
561
562       if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") ) {
563          // The following reserved two bytes (see PS 3.5-2001, section
564          // 7.1.2 Data element structure with explicit vr p27) must be
565          // skipped before proceeding on reading the length on 4 bytes.
566          fseek(fp, 2L, SEEK_CUR);
567          guint32 length32 = ReadInt32();
568          if ( (vr == "OB") && (length32 == 0xffffffff) ) {
569             ElVal->SetLength(FindLengthOB());
570             return;
571          }
572          FixFoundLength(ElVal, length32);
573          return;
574       }
575
576       // Length is encoded on 2 bytes.
577       length16 = ReadInt16();
578       
579       // We can tell the current file is encoded in big endian (like
580       // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
581       // and it's value is the one of the encoding of a big endian file.
582       // In order to deal with such big endian encoded files, we have
583       // (at least) two strategies:
584       // * when we load the "Transfer Syntax" tag with value of big endian
585       //   encoding, we raise the proper flags. Then we wait for the end
586       //   of the META group (0x0002) among which is "Transfer Syntax",
587       //   before switching the swap code to big endian. We have to postpone
588       //   the switching of the swap code since the META group is fully encoded
589       //   in little endian, and big endian coding only starts at the next
590       //   group. The corresponding code can be hard to analyse and adds
591       //   many additional unnecessary tests for regular tags.
592       // * the second strategy consists in waiting for trouble, that shall
593       //   appear when we find the first group with big endian encoding. This
594       //   is easy to detect since the length of a "Group Length" tag (the
595       //   ones with zero as element number) has to be of 4 (0x0004). When we
596       //   encouter 1024 (0x0400) chances are the encoding changed and we
597       //   found a group with big endian encoding.
598       // We shall use this second strategy. In order to make sure that we
599       // can interpret the presence of an apparently big endian encoded
600       // length of a "Group Length" without committing a big mistake, we
601       // add an additional check: we look in the allready parsed elements
602       // for the presence of a "Transfer Syntax" whose value has to be "big
603       // endian encoding". When this is the case, chances are we have got our
604       // hands on a big endian encoded file: we switch the swap code to
605       // big endian and proceed...
606       if ( (element  == 0x000) && (length16 == 0x0400) ) {
607          if ( ! IsExplicitVRBigEndianTransferSyntax() ) {
608             dbg.Verbose(0, "gdcmHeader::FindLength", "not explicit VR");
609             errno = 1;
610             return;
611          }
612          length16 = 4;
613          SwitchSwapToBigEndian();
614          // Restore the unproperly loaded values i.e. the group, the element
615          // and the dictionary entry depending on them.
616          guint16 CorrectGroup   = SwapShort(ElVal->GetGroup());
617          guint16 CorrectElem    = SwapShort(ElVal->GetElement());
618          gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
619                                                        CorrectElem);
620          if (!NewTag) {
621             // This correct tag is not in the dictionary. Create a new one.
622             NewTag = new gdcmDictEntry(CorrectGroup, CorrectElem);
623          }
624          // FIXME this can create a memory leaks on the old entry that be
625          // left unreferenced.
626          ElVal->SetDictEntry(NewTag);
627       }
628        
629       // Heuristic: well some files are really ill-formed.
630       if ( length16 == 0xffff) {
631          length16 = 0;
632          dbg.Verbose(0, "gdcmHeader::FindLength",
633                      "Erroneous element length fixed.");
634       }
635       FixFoundLength(ElVal, (guint32)length16);
636       return;
637    }
638
639    // Either implicit VR or a non DICOM conformal (see not below) explicit
640    // VR that ommited the VR of (at least) this element. Farts happen.
641    // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
642    // on Data elements "Implicit and Explicit VR Data Elements shall
643    // not coexist in a Data Set and Data Sets nested within it".]
644    // Length is on 4 bytes.
645    FixFoundLength(ElVal, ReadInt32());
646 }
647
648 /**
649  * \ingroup gdcmHeader
650  * \brief   Swaps back the bytes of 4-byte long integer accordingly to
651  *          processor order.
652  *
653  * @return  The suggested integer.
654  */
655 guint32 gdcmHeader::SwapLong(guint32 a) {
656    switch (sw) {
657    case    0 :
658       break;
659    case 4321 :
660       a=(   ((a<<24) & 0xff000000) | ((a<<8)  & 0x00ff0000)    | 
661             ((a>>8)  & 0x0000ff00) | ((a>>24) & 0x000000ff) );
662       break;
663    
664    case 3412 :
665       a=(   ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
666       break;
667    
668    case 2143 :
669       a=(    ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff)  );
670       break;
671    default :
672       dbg.Error(" gdcmHeader::SwapLong : unset swap code");
673       a=0;
674    }
675    return(a);
676 }
677
678 /**
679  * \ingroup gdcmHeader
680  * \brief   Swaps the bytes so they agree with the processor order
681  * @return  The properly swaped 16 bits integer.
682  */
683 guint16 gdcmHeader::SwapShort(guint16 a) {
684    if ( (sw==4321)  || (sw==2143) )
685       a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
686    return (a);
687 }
688
689 void gdcmHeader::SkipBytes(guint32 NBytes) {
690    //FIXME don't dump the returned value
691    (void)fseek(fp, (long)NBytes, SEEK_CUR);
692 }
693
694 void gdcmHeader::SkipElementValue(gdcmElValue * ElVal) {
695    SkipBytes(ElVal->GetLength());
696 }
697
698 void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) {
699    if (NewSize < 0)
700       return;
701    if ((guint32)NewSize >= (guint32)0xffffffff) {
702       MaxSizeLoadElementValue = 0xffffffff;
703       return;
704    }
705    MaxSizeLoadElementValue = NewSize;
706 }
707
708 /**
709  * \ingroup       gdcmHeader
710  * \brief         Loads the element content if it's length is not bigger
711  *                than the value specified with
712  *                gdcmHeader::SetMaxSizeLoadElementValue()
713  */
714 void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) {
715    size_t item_read;
716    guint16 group  = ElVal->GetGroup();
717    guint16 elem   = ElVal->GetElement();
718    string  vr     = ElVal->GetVR();
719    guint32 length = ElVal->GetLength();
720    bool SkipLoad  = false;
721
722    fseek(fp, (long)ElVal->GetOffset(), SEEK_SET);
723    
724    // FIXME Sequences not treated yet !
725    //
726    // Ne faudrait-il pas au contraire trouver immediatement
727    // une maniere 'propre' de traiter les sequences (vr = SQ)
728    // car commencer par les ignorer risque de conduire a qq chose
729    // qui pourrait ne pas etre generalisable
730    // Well, I'm expecting your code !!!
731     
732    if( vr == "SQ" )
733       SkipLoad = true;
734
735    // Heuristic : a sequence "contains" a set of tags (called items). It looks
736    // like the last tag of a sequence (the one that terminates the sequence)
737    // has a group of 0xfffe (with a dummy length).
738    if( group == 0xfffe )
739       SkipLoad = true;
740
741    if ( SkipLoad ) {
742       ElVal->SetLength(0);
743       ElVal->SetValue("gdcm::Skipped");
744       return;
745    }
746
747    // When the length is zero things are easy:
748    if ( length == 0 ) {
749       ElVal->SetValue("");
750       return;
751    }
752
753    // The elements whose length is bigger than the specified upper bound
754    // are not loaded. Instead we leave a short notice of the offset of
755    // the element content and it's length.
756    if (length > MaxSizeLoadElementValue) {
757       ostringstream s;
758       s << "gdcm::NotLoaded.";
759       s << " Address:" << (long)ElVal->GetOffset();
760       s << " Length:"  << ElVal->GetLength();
761       ElVal->SetValue(s.str());
762       return;
763    }
764    
765    // When an integer is expected, read and convert the following two or
766    // four bytes properly i.e. as an integer as opposed to a string.
767         
768         // pour les elements de Value Multiplicity > 1
769         // on aura en fait une serie d'entiers
770         
771         // on devrait pouvoir faire + compact (?)
772                 
773         if ( IsAnInteger(ElVal) ) {
774                 guint32 NewInt;
775                 ostringstream s;
776                 int nbInt;
777                 if (vr == "US" || vr == "SS") {
778                         nbInt = length / 2;
779                         NewInt = ReadInt16();
780                         s << NewInt;
781                         if (nbInt > 1) {
782                                 for (int i=1; i < nbInt; i++) {
783                                         s << '\\';
784                                         NewInt = ReadInt16();
785                                         s << NewInt;
786                                 }
787                         }
788                         
789                 } else if (vr == "UL" || vr == "SL") {
790                         nbInt = length / 4;
791                         NewInt = ReadInt32();
792                         s << NewInt;
793                         if (nbInt > 1) {
794                                 for (int i=1; i < nbInt; i++) {
795                                         s << '\\';
796                                         NewInt = ReadInt32();
797                                         s << NewInt;
798                                 }
799                         }
800                 }                                       
801                 ElVal->SetValue(s.str());
802                 return; 
803         }
804    
805    // We need an additional byte for storing \0 that is not on disk
806    char* NewValue = (char*)malloc(length+1);
807    if( !NewValue) {
808       dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
809       return;
810    }
811    NewValue[length]= 0;
812    
813    item_read = fread(NewValue, (size_t)length, (size_t)1, fp);
814    if ( item_read != 1 ) {
815       free(NewValue);
816       dbg.Verbose(1, "gdcmHeader::LoadElementValue","unread element value");
817       ElVal->SetValue("gdcm::UnRead");
818       return;
819    }
820    ElVal->SetValue(NewValue);
821    free(NewValue);
822 }
823
824 /**
825  * \ingroup       gdcmHeader
826  * \brief         Loads the element while preserving the current
827  *                underlying file position indicator as opposed to
828  *                to LoadElementValue that modifies it.
829  * @param ElVal   Element whose value shall be loaded. 
830  * @return  
831  */
832 void gdcmHeader::LoadElementValueSafe(gdcmElValue * ElVal) {
833    long PositionOnEntry = ftell(fp);
834    LoadElementValue(ElVal);
835    fseek(fp, PositionOnEntry, SEEK_SET);
836 }
837
838
839 guint16 gdcmHeader::ReadInt16(void) {
840    guint16 g;
841    size_t item_read;
842    item_read = fread (&g, (size_t)2,(size_t)1, fp);
843    errno = 0;
844    if ( item_read != 1 ) {
845       dbg.Verbose(1, "gdcmHeader::ReadInt16", " File read error");
846       errno = 1;
847       return 0;
848    }
849    g = SwapShort(g);
850    return g;
851 }
852
853 guint32 gdcmHeader::ReadInt32(void) {
854    guint32 g;
855    size_t item_read;
856    item_read = fread (&g, (size_t)4,(size_t)1, fp);
857    errno = 0;
858    if ( item_read != 1 ) {
859       dbg.Verbose(1, "gdcmHeader::ReadInt32", " File read error");
860       errno = 1;
861       return 0;
862    }
863    g = SwapLong(g);
864    return g;
865 }
866
867
868 gdcmElValue* gdcmHeader::GetElValueByNumber(guint16 Group, guint16 Elem) {
869
870    gdcmElValue* elValue = PubElValSet.GetElementByNumber(Group, Elem);   
871    if (!elValue) {
872       dbg.Verbose(1, "gdcmHeader::GetElValueByNumber",
873                   "failed to Locate gdcmElValue");
874       return (gdcmElValue*)0;
875    }
876    return elValue;
877 }
878
879 /**
880  * \ingroup gdcmHeader
881  * \brief   Build a new Element Value from all the low level arguments. 
882  *          Check for existence of dictionary entry, and build
883  *          a default one when absent.
884  * @param   Group group   of the underlying DictEntry
885  * @param   Elem  element of the underlying DictEntry
886  */
887 gdcmElValue* gdcmHeader::NewElValueByNumber(guint16 Group, guint16 Elem) {
888    // Find out if the tag we encountered is in the dictionaries:
889    gdcmDictEntry * NewTag = GetDictEntryByNumber(Group, Elem);
890    if (!NewTag)
891       NewTag = new gdcmDictEntry(Group, Elem);
892
893    gdcmElValue* NewElVal = new gdcmElValue(NewTag);
894    if (!NewElVal) {
895       dbg.Verbose(1, "gdcmHeader::NewElValueByNumber",
896                   "failed to allocate gdcmElValue");
897       return (gdcmElValue*)0;
898    }
899    return NewElVal;
900 }
901
902 /**
903  * \ingroup gdcmHeader
904  * \brief   TODO
905  * @param   
906  */
907 int gdcmHeader::ReplaceOrCreateByNumber(string Value, guint16 Group, guint16 Elem ) {
908
909         gdcmElValue* nvElValue=NewElValueByNumber(Group, Elem);
910         PubElValSet.Add(nvElValue);     
911         PubElValSet.SetElValueByNumber(Value, Group, Elem);
912         return(1);
913 }   
914
915
916 /**
917  * \ingroup gdcmHeader
918  * \brief   Build a new Element Value from all the low level arguments. 
919  *          Check for existence of dictionary entry, and build
920  *          a default one when absent.
921  * @param   Name    Name of the underlying DictEntry
922  */
923 gdcmElValue* gdcmHeader::NewElValueByName(string Name) {
924
925    gdcmDictEntry * NewTag = GetDictEntryByName(Name);
926    if (!NewTag)
927       NewTag = new gdcmDictEntry(0xffff, 0xffff, "LO", "Unknown", Name);
928
929    gdcmElValue* NewElVal = new gdcmElValue(NewTag);
930    if (!NewElVal) {
931       dbg.Verbose(1, "gdcmHeader::ObtainElValueByName",
932                   "failed to allocate gdcmElValue");
933       return (gdcmElValue*)0;
934    }
935    return NewElVal;
936 }  
937
938 /**
939  * \ingroup gdcmHeader
940  * \brief   Read the next tag but WITHOUT loading it's value
941  * @return  On succes the newly created ElValue, NULL on failure.      
942  */
943 gdcmElValue * gdcmHeader::ReadNextElement(void) {
944   
945    guint16 g,n;
946    gdcmElValue * NewElVal;
947    
948    g = ReadInt16();
949    n = ReadInt16();
950    if (errno == 1)
951       // We reached the EOF (or an error occured) and header parsing
952       // has to be considered as finished.
953       return (gdcmElValue *)0;
954    
955    NewElVal = NewElValueByNumber(g, n);
956    FindVR(NewElVal);
957    FindLength(NewElVal);
958    if (errno == 1)
959       // Call it quits
960       return (gdcmElValue *)0;
961    NewElVal->SetOffset(ftell(fp));
962    return NewElVal;
963 }
964
965 /**
966  * \ingroup gdcmHeader
967  * \brief   Apply some heuristics to predict wether the considered 
968  *          element value contains/represents an integer or not.
969  * @param   ElVal The element value on which to apply the predicate.
970  * @return  The result of the heuristical predicate.
971  */
972 bool gdcmHeader::IsAnInteger(gdcmElValue * ElVal) {
973    guint16 group   = ElVal->GetGroup();
974    guint16 element = ElVal->GetElement();
975    string  vr      = ElVal->GetVR();
976    guint32 length  = ElVal->GetLength();
977
978    // When we have some semantics on the element we just read, and if we
979    // a priori know we are dealing with an integer, then we shall be
980    // able to swap it's element value properly.
981    if ( element == 0 )  {  // This is the group length of the group
982       if (length == 4)
983          return true;
984       else {
985          printf("Erroneous Group Length element length (%04x , %04x) : %d\n",
986             group, element,length);
987                     
988          dbg.Error("gdcmHeader::IsAnInteger",
989             "Erroneous Group Length element length.");     
990       }
991    }
992  
993    if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
994       return true;
995    
996    return false;
997 }
998
999 /**
1000  * \ingroup gdcmHeader
1001  * \brief   Recover the offset (from the beginning of the file) of the pixels.
1002  */
1003 size_t gdcmHeader::GetPixelOffset(void) {
1004    // If this file complies with the norm we should encounter the
1005    // "Image Location" tag (0x0028,  0x0200). This tag contains the
1006    // the group that contains the pixel data (hence the "Pixel Data"
1007    // is found by indirection through the "Image Location").
1008    // Inside the group pointed by "Image Location" the searched element
1009    // is conventionally the element 0x0010 (when the norm is respected).
1010    // When the "Image Location" is absent we default to group 0x7fe0.
1011    guint16 grPixel;
1012    guint16 numPixel;
1013    string ImageLocation = GetPubElValByName("Image Location");
1014    if ( ImageLocation == "gdcm::Unfound" ) {
1015       grPixel = 0x7fe0;
1016    } else {
1017       grPixel = (guint16) atoi( ImageLocation.c_str() );
1018    }
1019    if (grPixel != 0x7fe0)
1020       // This is a kludge for old dirty Philips imager.
1021       numPixel = 0x1010;
1022    else
1023       numPixel = 0x0010;
1024    gdcmElValue* PixelElement = PubElValSet.GetElementByNumber(grPixel,
1025                                                               numPixel);
1026    if (PixelElement)
1027       return PixelElement->GetOffset();
1028    else
1029       return 0;
1030 }
1031
1032 /**
1033  * \ingroup gdcmHeader
1034  * \brief   Searches both the public and the shadow dictionary (when they
1035  *          exist) for the presence of the DictEntry with given
1036  *          group and element. The public dictionary has precedence on the
1037  *          shadow one.
1038  * @param   group   group of the searched DictEntry
1039  * @param   element element of the searched DictEntry
1040  * @return  Corresponding DictEntry when it exists, NULL otherwise.
1041  */
1042 gdcmDictEntry * gdcmHeader::GetDictEntryByNumber(guint16 group,
1043                                                  guint16 element) {
1044    gdcmDictEntry * found = (gdcmDictEntry*)0;
1045    if (!RefPubDict && !RefShaDict) {
1046       dbg.Verbose(0, "gdcmHeader::GetDictEntry",
1047                      "we SHOULD have a default dictionary");
1048    }
1049    if (RefPubDict) {
1050       found = RefPubDict->GetTagByNumber(group, element);
1051       if (found)
1052          return found;
1053    }
1054    if (RefShaDict) {
1055       found = RefShaDict->GetTagByNumber(group, element);
1056       if (found)
1057          return found;
1058    }
1059    return found;
1060 }
1061
1062 /**
1063  * \ingroup gdcmHeader
1064  * \brief   Searches both the public and the shadow dictionary (when they
1065  *          exist) for the presence of the DictEntry with given name.
1066  *          The public dictionary has precedence on the shadow one.
1067  * @param   Name name of the searched DictEntry
1068  * @return  Corresponding DictEntry when it exists, NULL otherwise.
1069  */
1070 gdcmDictEntry * gdcmHeader::GetDictEntryByName(string Name) {
1071    gdcmDictEntry * found = (gdcmDictEntry*)0;
1072    if (!RefPubDict && !RefShaDict) {
1073       dbg.Verbose(0, "gdcmHeader::GetDictEntry",
1074                      "we SHOULD have a default dictionary");
1075    }
1076    if (RefPubDict) {
1077       found = RefPubDict->GetTagByName(Name);
1078       if (found)
1079          return found;
1080    }
1081    if (RefShaDict) {
1082       found = RefShaDict->GetTagByName(Name);
1083       if (found)
1084          return found;
1085    }
1086    return found;
1087 }
1088
1089 /**
1090  * \ingroup gdcmHeader
1091  * \brief   Searches within the public dictionary for element value of
1092  *          a given tag.
1093  * @param   group Group of the researched tag.
1094  * @param   element Element of the researched tag.
1095  * @return  Corresponding element value when it exists, and the string
1096  *          "gdcm::Unfound" otherwise.
1097  */
1098 string gdcmHeader::GetPubElValByNumber(guint16 group, guint16 element) {
1099    return PubElValSet.GetElValueByNumber(group, element);
1100 }
1101
1102 /**
1103  * \ingroup gdcmHeader
1104  * \brief   Searches within the public dictionary for element value
1105  *          representation of a given tag.
1106  *
1107  *          Obtaining the VR (Value Representation) might be needed by caller
1108  *          to convert the string typed content to caller's native type 
1109  *          (think of C++ vs Python). The VR is actually of a higher level
1110  *          of semantics than just the native C++ type.
1111  * @param   group Group of the researched tag.
1112  * @param   element Element of the researched tag.
1113  * @return  Corresponding element value representation when it exists,
1114  *          and the string "gdcm::Unfound" otherwise.
1115  */
1116 string gdcmHeader::GetPubElValRepByNumber(guint16 group, guint16 element) {
1117    gdcmElValue* elem =  PubElValSet.GetElementByNumber(group, element);
1118    if ( !elem )
1119       return "gdcm::Unfound";
1120    return elem->GetVR();
1121 }
1122
1123 /**
1124  * \ingroup gdcmHeader
1125  * \brief   Searches within the public dictionary for element value of
1126  *          a given tag.
1127  * @param   TagName name of the researched element.
1128  * @return  Corresponding element value when it exists, and the string
1129  *          "gdcm::Unfound" otherwise.
1130  */
1131 string gdcmHeader::GetPubElValByName(string TagName) {
1132    return PubElValSet.GetElValueByName(TagName);
1133 }
1134
1135 /**
1136  * \ingroup gdcmHeader
1137  * \brief   Searches within the elements parsed with the public dictionary for
1138  *          the element value representation of a given tag.
1139  *
1140  *          Obtaining the VR (Value Representation) might be needed by caller
1141  *          to convert the string typed content to caller's native type 
1142  *          (think of C++ vs Python). The VR is actually of a higher level
1143  *          of semantics than just the native C++ type.
1144  * @param   TagName name of the researched element.
1145  * @return  Corresponding element value representation when it exists,
1146  *          and the string "gdcm::Unfound" otherwise.
1147  */
1148 string gdcmHeader::GetPubElValRepByName(string TagName) {
1149    gdcmElValue* elem =  PubElValSet.GetElementByName(TagName);
1150    if ( !elem )
1151       return "gdcm::Unfound";
1152    return elem->GetVR();
1153 }
1154
1155 /**
1156  * \ingroup gdcmHeader
1157  * \brief   Searches within elements parsed with the SHADOW dictionary 
1158  *          for the element value of a given tag.
1159  * @param   group Group of the researched tag.
1160  * @param   element Element of the researched tag.
1161  * @return  Corresponding element value representation when it exists,
1162  *          and the string "gdcm::Unfound" otherwise.
1163  */
1164 string gdcmHeader::GetShaElValByNumber(guint16 group, guint16 element) {
1165    return ShaElValSet.GetElValueByNumber(group, element);
1166 }
1167
1168 /**
1169  * \ingroup gdcmHeader
1170  * \brief   Searches within the elements parsed with the SHADOW dictionary
1171  *          for the element value representation of a given tag.
1172  *
1173  *          Obtaining the VR (Value Representation) might be needed by caller
1174  *          to convert the string typed content to caller's native type 
1175  *          (think of C++ vs Python). The VR is actually of a higher level
1176  *          of semantics than just the native C++ type.
1177  * @param   group Group of the researched tag.
1178  * @param   element Element of the researched tag.
1179  * @return  Corresponding element value representation when it exists,
1180  *          and the string "gdcm::Unfound" otherwise.
1181  */
1182 string gdcmHeader::GetShaElValRepByNumber(guint16 group, guint16 element) {
1183    gdcmElValue* elem =  ShaElValSet.GetElementByNumber(group, element);
1184    if ( !elem )
1185       return "gdcm::Unfound";
1186    return elem->GetVR();
1187 }
1188
1189 /**
1190  * \ingroup gdcmHeader
1191  * \brief   Searches within the elements parsed with the shadow dictionary
1192  *          for an element value of given tag.
1193  * @param   TagName name of the researched element.
1194  * @return  Corresponding element value when it exists, and the string
1195  *          "gdcm::Unfound" otherwise.
1196  */
1197 string gdcmHeader::GetShaElValByName(string TagName) {
1198    return ShaElValSet.GetElValueByName(TagName);
1199 }
1200
1201 /**
1202  * \ingroup gdcmHeader
1203  * \brief   Searches within the elements parsed with the shadow dictionary for
1204  *          the element value representation of a given tag.
1205  *
1206  *          Obtaining the VR (Value Representation) might be needed by caller
1207  *          to convert the string typed content to caller's native type 
1208  *          (think of C++ vs Python). The VR is actually of a higher level
1209  *          of semantics than just the native C++ type.
1210  * @param   TagName name of the researched element.
1211  * @return  Corresponding element value representation when it exists,
1212  *          and the string "gdcm::Unfound" otherwise.
1213  */
1214 string gdcmHeader::GetShaElValRepByName(string TagName) {
1215    gdcmElValue* elem =  ShaElValSet.GetElementByName(TagName);
1216    if ( !elem )
1217       return "gdcm::Unfound";
1218    return elem->GetVR();
1219 }
1220
1221 /**
1222  * \ingroup gdcmHeader
1223  * \brief   Searches within elements parsed with the public dictionary 
1224  *          and then within the elements parsed with the shadow dictionary
1225  *          for the element value of a given tag.
1226  * @param   group Group of the researched tag.
1227  * @param   element Element of the researched tag.
1228  * @return  Corresponding element value representation when it exists,
1229  *          and the string "gdcm::Unfound" otherwise.
1230  */
1231 string gdcmHeader::GetElValByNumber(guint16 group, guint16 element) {
1232    string pub = GetPubElValByNumber(group, element);
1233    if (pub.length())
1234       return pub;
1235    return GetShaElValByNumber(group, element);
1236 }
1237
1238 /**
1239  * \ingroup gdcmHeader
1240  * \brief   Searches within elements parsed with the public dictionary 
1241  *          and then within the elements parsed with the shadow dictionary
1242  *          for the element value representation of a given tag.
1243  *
1244  *          Obtaining the VR (Value Representation) might be needed by caller
1245  *          to convert the string typed content to caller's native type 
1246  *          (think of C++ vs Python). The VR is actually of a higher level
1247  *          of semantics than just the native C++ type.
1248  * @param   group Group of the researched tag.
1249  * @param   element Element of the researched tag.
1250  * @return  Corresponding element value representation when it exists,
1251  *          and the string "gdcm::Unfound" otherwise.
1252  */
1253 string gdcmHeader::GetElValRepByNumber(guint16 group, guint16 element) {
1254    string pub = GetPubElValRepByNumber(group, element);
1255    if (pub.length())
1256       return pub;
1257    return GetShaElValRepByNumber(group, element);
1258 }
1259
1260 /**
1261  * \ingroup gdcmHeader
1262  * \brief   Searches within elements parsed with the public dictionary 
1263  *          and then within the elements parsed with the shadow dictionary
1264  *          for the element value of a given tag.
1265  * @param   TagName name of the researched element.
1266  * @return  Corresponding element value when it exists,
1267  *          and the string "gdcm::Unfound" otherwise.
1268  */
1269 string gdcmHeader::GetElValByName(string TagName) {
1270    string pub = GetPubElValByName(TagName);
1271    if (pub.length())
1272       return pub;
1273    return GetShaElValByName(TagName);
1274 }
1275
1276 /**
1277  * \ingroup gdcmHeader
1278  * \brief   Searches within elements parsed with the public dictionary 
1279  *          and then within the elements parsed with the shadow dictionary
1280  *          for the element value representation of a given tag.
1281  *
1282  *          Obtaining the VR (Value Representation) might be needed by caller
1283  *          to convert the string typed content to caller's native type 
1284  *          (think of C++ vs Python). The VR is actually of a higher level
1285  *          of semantics than just the native C++ type.
1286  * @param   TagName name of the researched element.
1287  * @return  Corresponding element value representation when it exists,
1288  *          and the string "gdcm::Unfound" otherwise.
1289  */
1290 string gdcmHeader::GetElValRepByName(string TagName) {
1291    string pub = GetPubElValRepByName(TagName);
1292    if (pub.length())
1293       return pub;
1294    return GetShaElValRepByName(TagName);
1295 }
1296
1297 /**
1298  * \ingroup gdcmHeader
1299  * \brief   Accesses an existing gdcmElValue in the PubElValSet of this instance
1300  *          through it's (group, element) and modifies it's content with
1301  *          the given value.
1302  * @param   content new value to substitute with
1303  * @param   group   group of the ElVal to modify
1304  * @param   element element of the ElVal to modify
1305  */
1306 int gdcmHeader::SetPubElValByNumber(string content, guint16 group,
1307                                     guint16 element)
1308                                     
1309 //TODO  : homogeneiser les noms : SetPubElValByNumber   qui appelle PubElValSet.SetElValueByNumber 
1310 //        pourquoi pas            SetPubElValueByNumber ??
1311 {
1312
1313    return (  PubElValSet.SetElValueByNumber (content, group, element) );
1314 }
1315
1316 /**
1317  * \ingroup gdcmHeader
1318  * \brief   Accesses an existing gdcmElValue in the PubElValSet of this instance
1319  *          through tag name and modifies it's content with the given value.
1320  * @param   content new value to substitute with
1321  * @param   TagName name of the tag to be modified
1322  */
1323 int gdcmHeader::SetPubElValByName(string content, string TagName) {
1324    return (  PubElValSet.SetElValueByName (content, TagName) );
1325 }
1326
1327 /**
1328  * \ingroup gdcmHeader
1329  * \brief   Accesses an existing gdcmElValue in the PubElValSet of this instance
1330  *          through it's (group, element) and modifies it's length with
1331  *          the given value.
1332  * \warning Use with extreme caution.
1333  * @param   length new length to substitute with
1334  * @param   group   group of the ElVal to modify
1335  * @param   element element of the ElVal to modify
1336  * @return  1 on success, 0 otherwise.
1337  */
1338
1339 int gdcmHeader::SetPubElValLengthByNumber(guint32 length, guint16 group,
1340                                     guint16 element) {
1341         return (  PubElValSet.SetElValueLengthByNumber (length, group, element) );
1342 }
1343
1344 /**
1345  * \ingroup gdcmHeader
1346  * \brief   Accesses an existing gdcmElValue in the ShaElValSet of this instance
1347  *          through it's (group, element) and modifies it's content with
1348  *          the given value.
1349  * @param   content new value to substitute with
1350  * @param   group   group of the ElVal to modify
1351  * @param   element element of the ElVal to modify
1352  * @return  1 on success, 0 otherwise.
1353  */
1354 int gdcmHeader::SetShaElValByNumber(string content,
1355                                     guint16 group, guint16 element) {
1356    return (  ShaElValSet.SetElValueByNumber (content, group, element) );
1357 }
1358
1359 /**
1360  * \ingroup gdcmHeader
1361  * \brief   Accesses an existing gdcmElValue in the ShaElValSet of this instance
1362  *          through tag name and modifies it's content with the given value.
1363  * @param   content new value to substitute with
1364  * @param   TagName name of the tag to be modified
1365  */
1366 int gdcmHeader::SetShaElValByName(string content, string TagName) {
1367    return (  ShaElValSet.SetElValueByName (content, TagName) );
1368 }
1369
1370 /**
1371  * \ingroup gdcmHeader
1372  * \brief   Parses the header of the file but WITHOUT loading element values.
1373  */
1374 void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
1375    gdcmElValue * newElValue = (gdcmElValue *)0;
1376    
1377    rewind(fp);
1378    CheckSwap();
1379    while ( (newElValue = ReadNextElement()) ) {
1380       SkipElementValue(newElValue);
1381       PubElValSet.Add(newElValue);
1382    }
1383 }
1384
1385 /**
1386  * \ingroup gdcmHeader
1387  * \brief   Retrieve the number of columns of image.
1388  * @return  The encountered size when found, 0 by default.
1389  */
1390 int gdcmHeader::GetXSize(void) {
1391    // We cannot check for "Columns" because the "Columns" tag is present
1392    // both in IMG (0028,0011) and OLY (6000,0011) sections of the dictionary.
1393    string StrSize = GetPubElValByNumber(0x0028,0x0011);
1394    if (StrSize == "gdcm::Unfound")
1395       return 0;
1396    return atoi(StrSize.c_str());
1397 }
1398
1399 /**
1400  * \ingroup gdcmHeader
1401  * \brief   Retrieve the number of lines of image.
1402  * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
1403  * @return  The encountered size when found, 1 by default.
1404  */
1405 int gdcmHeader::GetYSize(void) {
1406    // We cannot check for "Rows" because the "Rows" tag is present
1407    // both in IMG (0028,0010) and OLY (6000,0010) sections of the dictionary.
1408    string StrSize = GetPubElValByNumber(0x0028,0x0010);
1409    if (StrSize != "gdcm::Unfound")
1410       return atoi(StrSize.c_str());
1411    if ( IsDicomV3() )
1412       return 0;
1413    else
1414       // The Rows (0028,0010) entry is optional for ACR/NEMA. It might
1415       // hence be a signal (1d image). So we default to 1:
1416       return 1;
1417 }
1418
1419 /**
1420  * \ingroup gdcmHeader
1421  * \brief   Retrieve the number of planes of volume or the number
1422  *          of frames of a multiframe.
1423  * \warning When present we consider the "Number of Frames" as the third
1424  *          dimension. When absent we consider the third dimension as
1425  *          being the "Planes" tag content.
1426  * @return  The encountered size when found, 1 by default.
1427  */
1428 int gdcmHeader::GetZSize(void) {
1429    // Both in DicomV3 and ACR/Nema the consider the "Number of Frames"
1430    // as the third dimension.
1431    string StrSize = GetPubElValByNumber(0x0028,0x0008);
1432    if (StrSize != "gdcm::Unfound")
1433       return atoi(StrSize.c_str());
1434
1435    // We then consider the "Planes" entry as the third dimension [we
1436    // cannot retrieve by name since "Planes tag is present both in
1437    // IMG (0028,0012) and OLY (6000,0012) sections of the dictionary]. 
1438    StrSize = GetPubElValByNumber(0x0028,0x0012);
1439    if (StrSize != "gdcm::Unfound")
1440       return atoi(StrSize.c_str());
1441    return 1;
1442 }
1443
1444 /**
1445  * \ingroup gdcmHeader
1446  * \brief   Return the size (in bytes) of a single pixel of data.
1447  * @return  The size in bytes of a single pixel of data.
1448  *
1449  */
1450 int gdcmHeader::GetPixelSize(void) {
1451    string PixelType = GetPixelType();
1452    if (PixelType == "8U" || PixelType == "8S")
1453       return 1;
1454    if (PixelType == "16U" || PixelType == "16S")
1455       return 2;
1456    if (PixelType == "32U" || PixelType == "32S")
1457       return 4;
1458    dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
1459    return 0;
1460 }
1461
1462 /**
1463  * \ingroup gdcmHeader
1464  * \brief   Build the Pixel Type of the image.
1465  *          Possible values are:
1466  *          - 8U  unsigned  8 bit,
1467  *          - 8S    signed  8 bit,
1468  *          - 16U unsigned 16 bit,
1469  *          - 16S   signed 16 bit,
1470  *          - 32U unsigned 32 bit,
1471  *          - 32S   signed 32 bit,
1472  * \warning 12 bit images appear as 16 bit.
1473  * @return  
1474  */
1475 string gdcmHeader::GetPixelType(void) {
1476    string BitsAlloc;
1477    BitsAlloc = GetElValByName("Bits Allocated");
1478    if (BitsAlloc == "gdcm::Unfound") {
1479       dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
1480       BitsAlloc = string("16");
1481    }
1482    if (BitsAlloc == "12")
1483       BitsAlloc = string("16");
1484
1485    string Signed;
1486    Signed = GetElValByName("Pixel Representation");
1487    if (Signed == "gdcm::Unfound") {
1488       dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
1489       BitsAlloc = string("0");
1490    }
1491    if (Signed == "0")
1492       Signed = string("U");
1493    else
1494       Signed = string("S");
1495
1496    return( BitsAlloc + Signed);
1497 }
1498
1499 /**
1500  * \ingroup gdcmHeader
1501  * \brief  This predicate, based on hopefully reasonnable heuristics,
1502  *         decides whether or not the current gdcmHeader was properly parsed
1503  *         and contains the mandatory information for being considered as
1504  *         a well formed and usable image.
1505  * @return true when gdcmHeader is the one of a reasonable Dicom file,
1506  *         false otherwise. 
1507  */
1508 bool gdcmHeader::IsReadable(void) {
1509    if (   GetElValByName("Image Dimensions") != "gdcm::Unfound"
1510       && atoi(GetElValByName("Image Dimensions").c_str()) > 4 ) {
1511       return false;
1512    }
1513    if (  GetElValByName("Bits Allocated") == "gdcm::Unfound" )
1514       return false;
1515    if (  GetElValByName("Bits Stored") == "gdcm::Unfound" )
1516       return false;
1517    if (  GetElValByName("High Bit") == "gdcm::Unfound" )
1518       return false;
1519    if (  GetElValByName("Pixel Representation") == "gdcm::Unfound" )
1520       return false;
1521    return true;
1522 }
1523
1524 /**
1525  * \ingroup gdcmHeader
1526  * \brief   Small utility function that creates a new manually crafted
1527  *          (as opposed as read from the file) gdcmElValue with user
1528  *          specified name and adds it to the public tag hash table.
1529  * \note    A fake TagKey is generated so the PubDict can keep it's coherence.
1530  * @param   NewTagName The name to be given to this new tag.
1531  * @param   VR The Value Representation to be given to this new tag.
1532  * @ return The newly hand crafted Element Value.
1533  */
1534 gdcmElValue* gdcmHeader::NewManualElValToPubDict(string NewTagName, string VR) {
1535    gdcmElValue* NewElVal = (gdcmElValue*)0;
1536    guint32 StuffGroup = 0xffff;   // Group to be stuffed with additional info
1537    guint32 FreeElem = 0;
1538    gdcmDictEntry* NewEntry = (gdcmDictEntry*)0;
1539
1540    FreeElem = PubElValSet.GenerateFreeTagKeyInGroup(StuffGroup);
1541    if (FreeElem == UINT32_MAX) {
1542       dbg.Verbose(1, "gdcmHeader::NewManualElValToPubDict",
1543                      "Group 0xffff in Public Dict is full");
1544       return (gdcmElValue*)0;
1545    }
1546    NewEntry = new gdcmDictEntry(StuffGroup, FreeElem,
1547                                 VR, "GDCM", NewTagName);
1548    NewElVal = new gdcmElValue(NewEntry);
1549    PubElValSet.Add(NewElVal);
1550    return NewElVal;
1551
1552 }
1553
1554 /**
1555  * \ingroup gdcmHeader
1556  * \brief   Loads the element values of all the elements present in the
1557  *          public tag based hash table.
1558  */
1559 void gdcmHeader::LoadElements(void) {
1560    rewind(fp);   
1561    TagElValueHT ht = PubElValSet.GetTagHt();
1562    for (TagElValueHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) {
1563       LoadElementValue(tag->second);
1564       }
1565 }
1566
1567 void gdcmHeader::PrintPubElVal(std::ostream & os) {
1568    PubElValSet.Print(os);
1569 }
1570
1571 void gdcmHeader::PrintPubDict(std::ostream & os) {
1572    RefPubDict->Print(os);
1573 }
1574
1575 int gdcmHeader::Write(FILE * fp, FileType type) {
1576    return PubElValSet.Write(fp, type);
1577 }