9 #include <netinet/in.h>
11 #include <cctype> // for isalpha
14 #include "gdcmHeader.h"
16 #include <iddcmjpeg.h>
18 // Refer to gdcmHeader::CheckSwap()
19 #define HEADER_LENGTH_TO_READ 256
20 // Refer to gdcmHeader::SetMaxSizeLoadElementValue()
21 #define _MaxSizeLoadElementValue_ 1024
23 VRHT * gdcmHeader::dicom_vr = (VRHT*)0;
25 void gdcmHeader::Initialise(void) {
26 if (!gdcmHeader::dicom_vr)
28 Dicts = new gdcmDictSet();
29 RefPubDict = Dicts->GetDefaultPubDict();
30 RefShaDict = (gdcmDict*)0;
33 gdcmHeader::gdcmHeader(const char *InFilename, bool exception_on_error)
34 throw(gdcmFileError) {
35 SetMaxSizeLoadElementValue(_MaxSizeLoadElementValue_);
36 filename = InFilename;
38 fp=fopen(InFilename,"rb");
39 if(exception_on_error) {
41 throw gdcmFileError("gdcmHeader::gdcmHeader(const char *, bool)");
44 dbg.Error(!fp, "gdcmHeader::gdcmHeader cannot open file", InFilename);
46 AddAndDefaultElements();
50 gdcmHeader::~gdcmHeader (void) {
51 //FIXME obviously there is much to be done here !
56 void gdcmHeader::InitVRDict (void) {
58 dbg.Verbose(0, "gdcmHeader::InitVRDict:", "VR dictionary allready set");
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
93 * \brief Discover what the swap code is (among little endian, big endian,
94 * bad little endian, bad big endian).
97 void gdcmHeader::CheckSwap()
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...
104 guint32 x=4; // x : pour ntohs
105 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
109 char deb[HEADER_LENGTH_TO_READ];
111 // First, compare HostByteOrder and NetworkByteOrder in order to
112 // determine if we shall need to swap bytes (i.e. the Endian type).
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);
123 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
124 filetype = TrueDicom;
125 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "looks like DICOM Version3");
128 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "not a DICOM Version3 file");
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.
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.
148 if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
149 (memcmp(entCur, "OB", (size_t)2) == 0) )
151 filetype = ExplicitVR;
152 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
153 "explicit Value Representation");
155 filetype = ImplicitVR;
156 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
157 "not an explicit Value Representation");
162 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
163 "HostByteOrder != NetworkByteOrder");
166 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
167 "HostByteOrder = NetworkByteOrder");
170 // Position the file position indicator at first tag (i.e.
171 // after the file preamble and the "DICM" string).
173 fseek (fp, 132L, SEEK_SET);
175 } // End of TrueDicom
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).
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.
188 // We assume the array of char we are considering contains the binary
189 // representation of a 32 bits integer. Hence the following dirty
191 s = *((guint32 *)(entCur));
211 dbg.Verbose(0, "gdcmHeader::CheckSwap:",
212 "ACR/NEMA unfound swap info (time to raise bets)");
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.
226 void gdcmHeader::SwitchSwapToBigEndian(void) {
227 dbg.Verbose(1, "gdcmHeader::SwitchSwapToBigEndian",
228 "Switching to BigEndian mode.");
245 void gdcmHeader::GetPixels(size_t lgrTotale, void* _Pixels) {
247 pixelsOffset = GetPixelOffset();
248 fseek(fp, pixelsOffset, SEEK_SET);
249 if (IsJPEGLossless()) {
250 _Pixels=_IdDcmJpegRead(fp);
252 fread(_Pixels, 1, lgrTotale, fp);
259 * \ingroup gdcmHeader
260 * \brief Find the value representation of the current tag.
262 void gdcmHeader::FindVR( gdcmElValue *ElVal) {
263 if (filetype != ExplicitVR)
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
277 bool RealExplicit = true;
279 lgrLue=fread (&VR, (size_t)2,(size_t)1, fp);
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.
291 if ( (!isalpha(VR[0])) && (!isalpha(VR[1])) )
292 RealExplicit = false;
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) )
300 if ( RealExplicit ) {
301 if ( ElVal->IsVrUnknown() ) {
302 // When not a dictionary entry, we can safely overwrite the vr.
306 if ( ElVal->GetVR() == vr ) {
307 // The vr we just read and the dictionary agree. Nothing to do.
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(),
320 ElVal->SetDictEntry(NewTag);
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.
333 if ( ElVal->IsVrUnknown() )
334 ElVal->SetVR("Implicit");
335 ElVal->SetImplicitVr();
339 * \ingroup gdcmHeader
340 * \brief Determines if the Transfer Syntax was allready encountered
341 * and if it corresponds to a ImplicitVRLittleEndian one.
343 * @return True when ImplicitVRLittleEndian found. False in all other cases.
345 bool gdcmHeader::IsImplicitVRLittleEndianTransferSyntax(void) {
346 gdcmElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
349 LoadElementValueSafe(Element);
350 string Transfer = Element->GetValue();
351 if ( Transfer == "1.2.840.10008.1.2" )
357 * \ingroup gdcmHeader
358 * \brief Determines if the Transfer Syntax was allready encountered
359 * and if it corresponds to a ExplicitVRLittleEndian one.
361 * @return True when ExplicitVRLittleEndian found. False in all other cases.
363 bool gdcmHeader::IsExplicitVRLittleEndianTransferSyntax(void) {
364 gdcmElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
367 LoadElementValueSafe(Element);
368 string Transfer = Element->GetValue();
369 if ( Transfer == "1.2.840.10008.1.2.1" )
375 * \ingroup gdcmHeader
376 * \brief Determines if the Transfer Syntax was allready encountered
377 * and if it corresponds to a DeflatedExplicitVRLittleEndian one.
379 * @return True when DeflatedExplicitVRLittleEndian found. False in all other cases.
381 bool gdcmHeader::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
382 gdcmElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
385 LoadElementValueSafe(Element);
386 string Transfer = Element->GetValue();
387 if ( Transfer == "1.2.840.10008.1.2.1.99" )
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.
397 * @return True when big endian found. False in all other cases.
399 bool gdcmHeader::IsExplicitVRBigEndianTransferSyntax(void) {
400 gdcmElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
403 LoadElementValueSafe(Element);
404 string Transfer = Element->GetValue();
405 if ( Transfer == "1.2.840.10008.1.2.2" ) //1.2.2 ??? A verifier !
411 * \ingroup gdcmHeader
412 * \brief Determines if the Transfer Syntax was allready encountered
413 * and if it corresponds to a JPEGBaseLineProcess1 one.
415 * @return True when JPEGBaseLineProcess1found. False in all other cases.
417 bool gdcmHeader::IsJPEGBaseLineProcess1TransferSyntax(void) {
418 gdcmElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
421 LoadElementValueSafe(Element);
422 string Transfer = Element->GetValue();
423 if ( Transfer == "1.2.840.10008.1.2.4.50" )
428 // faire qq chose d'intelligent a la place de ça
430 bool gdcmHeader::IsJPEGLossless(void) {
431 gdcmElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
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;
443 * \ingroup gdcmHeader
444 * \brief Determines if the Transfer Syntax was allready encountered
445 * and if it corresponds to a JPEGExtendedProcess2-4 one.
447 * @return True when JPEGExtendedProcess2-4 found. False in all other cases.
449 bool gdcmHeader::IsJPEGExtendedProcess2_4TransferSyntax(void) {
450 gdcmElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
453 LoadElementValueSafe(Element);
454 string Transfer = Element->GetValue();
455 if ( Transfer == "1.2.840.10008.1.2.4.51" )
461 * \ingroup gdcmHeader
462 * \brief Determines if the Transfer Syntax was allready encountered
463 * and if it corresponds to a JPEGExtendeProcess3-5 one.
465 * @return True when JPEGExtendedProcess3-5 found. False in all other cases.
467 bool gdcmHeader::IsJPEGExtendedProcess3_5TransferSyntax(void) {
468 gdcmElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
471 LoadElementValueSafe(Element);
472 string Transfer = Element->GetValue();
473 if ( Transfer == "1.2.840.10008.1.2.4.52" )
479 * \ingroup gdcmHeader
480 * \brief Determines if the Transfer Syntax was allready encountered
481 * and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
483 * @return True when JPEGSpectralSelectionProcess6-8 found. False in all
486 bool gdcmHeader::IsJPEGSpectralSelectionProcess6_8TransferSyntax(void) {
487 gdcmElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
490 LoadElementValueSafe(Element);
491 string Transfer = Element->GetValue();
492 if ( Transfer == "1.2.840.10008.1.2.4.53" )
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.
503 void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
504 if ( FoundLength == 0xffffffff)
506 ElVal->SetLength(FoundLength);
509 guint32 gdcmHeader::FindLengthOB(void) {
510 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
513 long PositionOnEntry = ftell(fp);
514 bool FoundSequenceDelimiter = false;
515 guint32 TotalLength = 0;
518 while ( ! FoundSequenceDelimiter) {
523 TotalLength += 4; // We even have to decount the group and element
525 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",
526 "wrong group for an item sequence.");
531 FoundSequenceDelimiter = true;
532 else if ( n != 0xe000) {
533 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",
534 "wrong element for an item sequence.");
538 ItemLength = ReadInt32();
539 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
540 // the ItemLength with ReadInt32
541 SkipBytes(ItemLength);
543 fseek(fp, PositionOnEntry, SEEK_SET);
547 void gdcmHeader::FindLength(gdcmElValue * ElVal) {
548 guint16 element = ElVal->GetElement();
549 string vr = ElVal->GetVR();
552 if ( (filetype == ExplicitVR) && ! ElVal->IsImplicitVr() ) {
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());
564 FixFoundLength(ElVal, length32);
568 // Length is encoded on 2 bytes.
569 length16 = ReadInt16();
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");
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);
612 // This correct tag is not in the dictionary. Create a new one.
613 NewTag = new gdcmDictEntry(CorrectGroup, CorrectElem);
615 // FIXME this can create a memory leaks on the old entry that be
616 // left unreferenced.
617 ElVal->SetDictEntry(NewTag);
620 // Heuristic: well some files are really ill-formed.
621 if ( length16 == 0xffff) {
623 dbg.Verbose(0, "gdcmHeader::FindLength",
624 "Erroneous element length fixed.");
626 FixFoundLength(ElVal, (guint32)length16);
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());
640 * \ingroup gdcmHeader
641 * \brief Swaps back the bytes of 4-byte long integer accordingly to
644 * @return The suggested integer.
646 guint32 gdcmHeader::SwapLong(guint32 a) {
647 // FIXME: il pourrait y avoir un pb pour les entiers negatifs ...
652 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
653 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
657 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
661 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
664 dbg.Error(" gdcmHeader::SwapLong : unset swap code");
671 * \ingroup gdcmHeader
672 * \brief Swaps the bytes so they agree with the processor order
673 * @return The properly swaped 16 bits integer.
675 guint16 gdcmHeader::SwapShort(guint16 a) {
676 if ( (sw==4321) || (sw==2143) )
677 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
681 void gdcmHeader::SkipBytes(guint32 NBytes) {
682 //FIXME don't dump the returned value
683 (void)fseek(fp, (long)NBytes, SEEK_CUR);
686 void gdcmHeader::SkipElementValue(gdcmElValue * ElVal) {
687 SkipBytes(ElVal->GetLength());
690 void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) {
693 if ((guint32)NewSize >= (guint32)0xffffffff) {
694 MaxSizeLoadElementValue = 0xffffffff;
697 MaxSizeLoadElementValue = NewSize;
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()
706 void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) {
708 guint16 group = ElVal->GetGroup();
709 guint16 elem = ElVal->GetElement();
710 string vr = ElVal->GetVR();
711 guint32 length = ElVal->GetLength();
712 bool SkipLoad = false;
714 fseek(fp, (long)ElVal->GetOffset(), SEEK_SET);
716 // Sequences not treated yet !
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
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 )
732 // The group length doesn't represent data to be loaded in memory, since
733 // each element of the group shall be loaded individualy.
735 //SkipLoad = true; // modif sauvage JPR
736 // On charge la longueur du groupe
737 // quand l'element 0x0000 est présent !
740 // FIXME the following skip is not necessary
741 SkipElementValue(ElVal);
743 ElVal->SetValue("gdcm::Skipped");
747 // When the length is zero things are easy:
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) {
758 s << "gdcm::NotLoaded.";
759 s << " Address:" << (long)ElVal->GetOffset();
760 s << " Length:" << ElVal->GetLength();
761 ElVal->SetValue(s.str());
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.
768 // pour les elements de Value Multiplicity > 1
769 // on aura en fait une serie d'entiers
773 //if ( IsAnInteger(ElVal) ) {
775 // if( length == 2 ) {
776 // NewInt = ReadInt16();
777 // } else if( length == 4 ) {
779 // NewInt = ReadInt32();
781 // dbg.Error(true, "LoadElementValue: Inconsistency when reading Int.");
783 // //FIXME: make the following an util fonction
786 // ElVal->SetValue(s.str());
790 // modif proposee. JPR
791 // on devrait pouvoir faire + compact (?)
793 if ( IsAnInteger(ElVal) ) {
797 if (vr == "US" || vr == "SS") {
799 NewInt = ReadInt16();
802 for (int i=1; i < nbInt; i++) {
804 NewInt = ReadInt16();
806 //printf("%s\n", s.str().c_str());
810 } else if (vr == "UL" || vr == "SL") {
812 NewInt = ReadInt32();
815 for (int i=1; i < nbInt; i++) {
817 NewInt = ReadInt32();
822 ElVal->SetValue(s.str());
828 // FIXME The exact size should be length if we move to strings or whatever
829 char* NewValue = (char*)malloc(length+1);
831 dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
836 item_read = fread(NewValue, (size_t)length, (size_t)1, fp);
837 if ( item_read != 1 ) {
839 dbg.Verbose(1, "gdcmHeader::LoadElementValue","unread element value");
840 ElVal->SetValue("gdcm::UnRead");
843 ElVal->SetValue(NewValue);
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.
854 void gdcmHeader::LoadElementValueSafe(gdcmElValue * ElVal) {
855 long PositionOnEntry = ftell(fp);
856 LoadElementValue(ElVal);
857 fseek(fp, PositionOnEntry, SEEK_SET);
861 guint16 gdcmHeader::ReadInt16(void) {
864 item_read = fread (&g, (size_t)2,(size_t)1, fp);
866 if ( item_read != 1 ) {
867 dbg.Verbose(1, "gdcmHeader::ReadInt16", " File read error");
875 guint32 gdcmHeader::ReadInt32(void) {
878 item_read = fread (&g, (size_t)4,(size_t)1, fp);
880 if ( item_read != 1 ) {
881 dbg.Verbose(1, "gdcmHeader::ReadInt32", " File read error");
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
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);
901 NewTag = new gdcmDictEntry(Group, Elem);
903 gdcmElValue* NewElVal = new gdcmElValue(NewTag);
905 dbg.Verbose(1, "gdcmHeader::NewElValueByKey",
906 "failed to allocate gdcmElValue");
907 return (gdcmElValue*)0;
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
919 gdcmElValue* gdcmHeader::NewElValueByName(string Name) {
921 gdcmDictEntry * NewTag = GetDictEntryByName(Name);
923 NewTag = new gdcmDictEntry(0xffff, 0xffff, "LO", "Unknown", Name);
925 gdcmElValue* NewElVal = new gdcmElValue(NewTag);
927 dbg.Verbose(1, "gdcmHeader::ObtainElValueByName",
928 "failed to allocate gdcmElValue");
929 return (gdcmElValue*)0;
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.
939 gdcmElValue * gdcmHeader::ReadNextElement(void) {
942 gdcmElValue * NewElVal;
947 // We reached the EOF (or an error occured) and header parsing
948 // has to be considered as finished.
949 return (gdcmElValue *)0;
951 NewElVal = NewElValueByKey(g, n);
953 FindLength(NewElVal);
956 return (gdcmElValue *)0;
957 NewElVal->SetOffset(ftell(fp));
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.
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();
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
981 dbg.Error("gdcmHeader::IsAnInteger",
982 "Erroneous Group Length element length.");
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
995 if ( (length != 4) && ( length != 2) )
996 // Swapping only make sense on integers which are 2 or 4 bytes long.
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.
1004 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
1008 // est-ce encore utile?
1009 // mieux vaut modifier le source du Dicom Dictionnaty
1010 // et remplacer pour ces 2 cas RET par US
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).
1018 if ( (group == 0x0028) && (element == 0x0200) )
1019 // This tag is retained from ACR/NEMA
1026 * \ingroup gdcmHeader
1027 * \brief Recover the offset (from the beginning of the file) of the pixels.
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.
1039 string ImageLocation = GetPubElValByName("Image Location");
1040 if ( ImageLocation == "gdcm::Unfound" ) {
1043 grPixel = (guint16) atoi( ImageLocation.c_str() );
1045 if (grPixel != 0x7fe0)
1046 // FIXME is this still necessary ?
1047 // Now, this looks like an old dirty fix for Philips imager
1051 gdcmElValue* PixelElement = PubElVals.GetElementByNumber(grPixel, numPixel);
1053 return PixelElement->GetOffset();
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
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.
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");
1075 found = RefPubDict->GetTagByKey(group, element);
1080 found = RefShaDict->GetTagByKey(group, element);
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.
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");
1102 found = RefPubDict->GetTagByName(Name);
1107 found = RefShaDict->GetTagByName(Name);
1115 * \ingroup gdcmHeader
1116 * \brief Searches within the public dictionary for element value of
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.
1123 string gdcmHeader::GetPubElValByNumber(guint16 group, guint16 element) {
1124 return PubElVals.GetElValueByNumber(group, element);
1128 * \ingroup gdcmHeader
1129 * \brief Searches within the public dictionary for element value
1130 * representation of a given tag.
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.
1141 string gdcmHeader::GetPubElValRepByNumber(guint16 group, guint16 element) {
1142 gdcmElValue* elem = PubElVals.GetElementByNumber(group, element);
1144 return "gdcm::Unfound";
1145 return elem->GetVR();
1149 * \ingroup gdcmHeader
1150 * \brief Searches within the public dictionary for element value of
1152 * @param TagName name of the researched element.
1153 * @return Corresponding element value when it exists, and the string
1154 * "gdcm::Unfound" otherwise.
1156 string gdcmHeader::GetPubElValByName(string TagName) {
1157 return PubElVals.GetElValueByName(TagName);
1161 * \ingroup gdcmHeader
1162 * \brief Searches within the elements parsed with the public dictionary for
1163 * the element value representation of a given tag.
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.
1173 string gdcmHeader::GetPubElValRepByName(string TagName) {
1174 gdcmElValue* elem = PubElVals.GetElementByName(TagName);
1176 return "gdcm::Unfound";
1177 return elem->GetVR();
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.
1189 string gdcmHeader::GetShaElValByNumber(guint16 group, guint16 element) {
1190 return ShaElVals.GetElValueByNumber(group, element);
1194 * \ingroup gdcmHeader
1195 * \brief Searches within the elements parsed with the SHADOW dictionary
1196 * for the element value representation of a given tag.
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.
1207 string gdcmHeader::GetShaElValRepByNumber(guint16 group, guint16 element) {
1208 gdcmElValue* elem = ShaElVals.GetElementByNumber(group, element);
1210 return "gdcm::Unfound";
1211 return elem->GetVR();
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.
1222 string gdcmHeader::GetShaElValByName(string TagName) {
1223 return ShaElVals.GetElValueByName(TagName);
1227 * \ingroup gdcmHeader
1228 * \brief Searches within the elements parsed with the shadow dictionary for
1229 * the element value representation of a given tag.
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.
1239 string gdcmHeader::GetShaElValRepByName(string TagName) {
1240 gdcmElValue* elem = ShaElVals.GetElementByName(TagName);
1242 return "gdcm::Unfound";
1243 return elem->GetVR();
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.
1256 string gdcmHeader::GetElValByNumber(guint16 group, guint16 element) {
1257 string pub = GetPubElValByNumber(group, element);
1260 return GetShaElValByNumber(group, element);
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.
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.
1278 string gdcmHeader::GetElValRepByNumber(guint16 group, guint16 element) {
1279 string pub = GetPubElValRepByNumber(group, element);
1282 return GetShaElValRepByNumber(group, element);
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.
1294 string gdcmHeader::GetElValByName(string TagName) {
1295 string pub = GetPubElValByName(TagName);
1298 return GetShaElValByName(TagName);
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.
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.
1315 string gdcmHeader::GetElValRepByName(string TagName) {
1316 string pub = GetPubElValRepByName(TagName);
1319 return GetShaElValRepByName(TagName);
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
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
1331 int gdcmHeader::SetPubElValByNumber(string content, guint16 group,
1334 return ( PubElVals.SetElValueByNumber (content, group, element) );
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
1344 int gdcmHeader::SetPubElValByName(string content, string TagName) {
1345 return ( PubElVals.SetElValueByName (content, TagName) );
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
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
1358 int gdcmHeader::SetPubElValLengthByNumber(guint32 lgr, guint16 group,
1361 return ( PubElVals.SetElValueLengthByNumber (lgr, group, element) );
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
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
1373 int gdcmHeader::SetShaElValByNumber(string content,
1374 guint16 group, guint16 element)
1376 return ( ShaElVals.SetElValueByNumber (content, group, element) );
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
1386 int gdcmHeader::SetShaElValByName(string content, string TagName) {
1387 return ( ShaElVals.SetElValueByName (content, TagName) );
1391 * \ingroup gdcmHeader
1392 * \brief Parses the header of the file but WITHOUT loading element values.
1394 void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
1395 gdcmElValue * newElValue = (gdcmElValue *)0;
1399 while ( (newElValue = ReadNextElement()) ) {
1400 SkipElementValue(newElValue);
1401 PubElVals.Add(newElValue);
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.
1417 void gdcmHeader::AddAndDefaultElements(void) {
1418 gdcmElValue* NewEntry = (gdcmElValue*)0;
1420 NewEntry = NewElValueByName("gdcmXSize");
1421 NewEntry->SetValue(GetElValByName("Rows"));
1422 PubElVals.Add(NewEntry);
1424 NewEntry = NewElValueByName("gdcmYSize");
1425 NewEntry->SetValue(GetElValByName("Columns"));
1426 PubElVals.Add(NewEntry);
1428 NewEntry = NewElValueByName("gdcmZSize");
1429 NewEntry->SetValue(GetElValByName("Planes"));
1430 PubElVals.Add(NewEntry);
1434 * \ingroup gdcmHeader
1435 * \brief Loads the element values of all the elements present in the
1436 * public tag based hash table.
1438 void gdcmHeader::LoadElements(void) {
1440 TagElValueHT ht = PubElVals.GetTagHt();
1441 for (TagElValueHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) {
1442 LoadElementValue(tag->second);
1446 void gdcmHeader::PrintPubElVal(ostream & os) {
1447 PubElVals.Print(os);
1450 void gdcmHeader::PrintPubDict(ostream & os) {
1451 RefPubDict->Print(os);