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