1 // $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.68 2003/05/28 19:36:21 frog Exp $
9 #include <netinet/in.h>
11 #include <cctype> // for isalpha
14 #include "gdcmHeader.h"
17 // Refer to gdcmHeader::CheckSwap()
18 #define HEADER_LENGTH_TO_READ 256
19 // Refer to gdcmHeader::SetMaxSizeLoadElementValue()
20 #define _MaxSizeLoadElementValue_ 1024
22 void gdcmHeader::Initialise(void) {
23 dicom_vr = gdcmGlobal::GetVR();
24 Dicts = gdcmGlobal::GetDicts();
25 RefPubDict = Dicts->GetDefaultPubDict();
26 RefShaDict = (gdcmDict*)0;
29 gdcmHeader::gdcmHeader(const char *InFilename, bool exception_on_error) {
30 SetMaxSizeLoadElementValue(_MaxSizeLoadElementValue_);
31 filename = InFilename;
33 if ( !OpenFile(exception_on_error))
40 bool gdcmHeader::OpenFile(bool exception_on_error)
41 throw(gdcmFileError) {
42 fp=fopen(filename.c_str(),"rb");
43 if(exception_on_error) {
45 throw gdcmFileError("gdcmHeader::gdcmHeader(const char *, bool)");
49 dbg.Verbose(0, "gdcmHeader::gdcmHeader cannot open file", filename.c_str());
53 bool gdcmHeader::CloseFile(void) {
54 int closed = fclose(fp);
61 gdcmHeader::~gdcmHeader (void) {
62 dicom_vr = (gdcmVR*)0;
63 Dicts = (gdcmDictSet*)0;
64 RefPubDict = (gdcmDict*)0;
65 RefShaDict = (gdcmDict*)0;
71 // META Meta Information
83 // NMI Nuclear Medicine
85 // BFS Basic Film Session
87 // BIB Basic Image Box
102 * \ingroup gdcmHeader
103 * \brief Discover what the swap code is (among little endian, big endian,
104 * bad little endian, bad big endian).
107 void gdcmHeader::CheckSwap()
109 // The only guaranted way of finding the swap code is to find a
110 // group tag since we know it's length has to be of four bytes i.e.
111 // 0x00000004. Finding the swap code in then straigthforward. Trouble
112 // occurs when we can't find such group...
114 guint32 x=4; // x : pour ntohs
115 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
119 char deb[HEADER_LENGTH_TO_READ];
121 // First, compare HostByteOrder and NetworkByteOrder in order to
122 // determine if we shall need to swap bytes (i.e. the Endian type).
128 // The easiest case is the one of a DICOM header, since it possesses a
129 // file preamble where it suffice to look for the string "DICM".
130 lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
133 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
134 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "looks like DICOM Version3");
135 // Next, determine the value representation (VR). Let's skip to the
136 // first element (0002, 0000) and check there if we find "UL"
137 // - or "OB" if the 1st one is (0002,0001) -,
138 // in which case we (almost) know it is explicit VR.
139 // WARNING: if it happens to be implicit VR then what we will read
140 // is the length of the group. If this ascii representation of this
141 // length happens to be "UL" then we shall believe it is explicit VR.
142 // FIXME: in order to fix the above warning, we could read the next
143 // element value (or a couple of elements values) in order to make
144 // sure we are not commiting a big mistake.
146 // * the 128 bytes of File Preamble (often padded with zeroes),
147 // * the 4 bytes of "DICM" string,
148 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
149 // i.e. a total of 136 bytes.
152 // Use gdcmHeader::dicom_vr to test all the possibilities
153 // instead of just checking for UL, OB and UI !?
154 if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
155 (memcmp(entCur, "OB", (size_t)2) == 0) ||
156 (memcmp(entCur, "UI", (size_t)2) == 0) )
158 filetype = ExplicitVR;
159 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
160 "explicit Value Representation");
162 filetype = ImplicitVR;
163 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
164 "not an explicit Value Representation");
169 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
170 "HostByteOrder != NetworkByteOrder");
173 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
174 "HostByteOrder = NetworkByteOrder");
177 // Position the file position indicator at first tag (i.e.
178 // after the file preamble and the "DICM" string).
180 fseek (fp, 132L, SEEK_SET);
184 // Alas, this is not a DicomV3 file and whatever happens there is no file
185 // preamble. We can reset the file position indicator to where the data
186 // is (i.e. the beginning of the file).
187 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "not a DICOM Version3 file");
190 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
191 // By clean we mean that the length of the first tag is written down.
192 // If this is the case and since the length of the first group HAS to be
193 // four (bytes), then determining the proper swap code is straightforward.
196 // We assume the array of char we are considering contains the binary
197 // representation of a 32 bits integer. Hence the following dirty
199 s = *((guint32 *)(entCur));
219 dbg.Verbose(0, "gdcmHeader::CheckSwap:",
220 "ACR/NEMA unfound swap info (time to raise bets)");
223 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
224 // It is time for despaired wild guesses. So, let's assume this file
225 // happens to be 'dirty' ACR/NEMA, i.e. the length of the group is
226 // not present. Then the only info we have is the net2host one.
235 void gdcmHeader::SwitchSwapToBigEndian(void) {
236 dbg.Verbose(1, "gdcmHeader::SwitchSwapToBigEndian",
237 "Switching to BigEndian mode.");
255 * \ingroup gdcmHeader
256 * \brief Find the value representation of the current tag.
258 void gdcmHeader::FindVR( gdcmElValue *ElVal) {
259 if (filetype != ExplicitVR)
265 long PositionOnEntry = ftell(fp);
266 // Warning: we believe this is explicit VR (Value Representation) because
267 // we used a heuristic that found "UL" in the first tag. Alas this
268 // doesn't guarantee that all the tags will be in explicit VR. In some
269 // cases (see e-film filtered files) one finds implicit VR tags mixed
270 // within an explicit VR file. Hence we make sure the present tag
271 // is in explicit VR and try to fix things if it happens not to be
273 bool RealExplicit = true;
275 lgrLue=fread (&VR, (size_t)2,(size_t)1, fp);
279 // Assume we are reading a falsely explicit VR file i.e. we reached
280 // a tag where we expect reading a VR but are in fact we read the
281 // first to bytes of the length. Then we will interogate (through find)
282 // the dicom_vr dictionary with oddities like "\004\0" which crashes
283 // both GCC and VC++ implementations of the STL map. Hence when the
284 // expected VR read happens to be non-ascii characters we consider
285 // we hit falsely explicit VR tag.
287 if ( (!isalpha(VR[0])) && (!isalpha(VR[1])) )
288 RealExplicit = false;
290 // CLEANME searching the dicom_vr at each occurence is expensive.
291 // PostPone this test in an optional integrity check at the end
292 // of parsing or only in debug mode.
293 if ( RealExplicit && !dicom_vr->Count(vr) )
296 if ( RealExplicit ) {
297 if ( ElVal->IsVrUnknown() ) {
298 // When not a dictionary entry, we can safely overwrite the vr.
302 if ( ElVal->GetVR() == vr ) {
303 // The vr we just read and the dictionary agree. Nothing to do.
306 // The vr present in the file and the dictionary disagree. We assume
307 // the file writer knew best and use the vr of the file. Since it would
308 // be unwise to overwrite the vr of a dictionary (since it would
309 // compromise it's next user), we need to clone the actual DictEntry
310 // and change the vr for the read one.
311 gdcmDictEntry* NewTag = new gdcmDictEntry(ElVal->GetGroup(),
316 ElVal->SetDictEntry(NewTag);
320 // We thought this was explicit VR, but we end up with an
321 // implicit VR tag. Let's backtrack.
322 dbg.Verbose(1, "gdcmHeader::FindVR:", "Falsely explicit vr file");
323 fseek(fp, PositionOnEntry, SEEK_SET);
324 // When this element is known in the dictionary we shall use, e.g. for
325 // the semantics (see the usage of IsAnInteger), the vr proposed by the
326 // dictionary entry. Still we have to flag the element as implicit since
327 // we know now our assumption on expliciteness is not furfilled.
329 if ( ElVal->IsVrUnknown() )
330 ElVal->SetVR("Implicit");
331 ElVal->SetImplicitVr();
335 * \ingroup gdcmHeader
336 * \brief Determines if the Transfer Syntax was allready encountered
337 * and if it corresponds to a ImplicitVRLittleEndian one.
339 * @return True when ImplicitVRLittleEndian found. False in all other cases.
341 bool gdcmHeader::IsImplicitVRLittleEndianTransferSyntax(void) {
342 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
345 LoadElementValueSafe(Element);
346 string Transfer = Element->GetValue();
347 if ( Transfer == "1.2.840.10008.1.2" )
353 * \ingroup gdcmHeader
354 * \brief Determines if the Transfer Syntax was allready encountered
355 * and if it corresponds to a ExplicitVRLittleEndian one.
357 * @return True when ExplicitVRLittleEndian found. False in all other cases.
359 bool gdcmHeader::IsExplicitVRLittleEndianTransferSyntax(void) {
360 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
363 LoadElementValueSafe(Element);
364 string Transfer = Element->GetValue();
365 if ( Transfer == "1.2.840.10008.1.2.1" )
371 * \ingroup gdcmHeader
372 * \brief Determines if the Transfer Syntax was allready encountered
373 * and if it corresponds to a DeflatedExplicitVRLittleEndian one.
375 * @return True when DeflatedExplicitVRLittleEndian found. False in all other cases.
377 bool gdcmHeader::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
378 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
381 LoadElementValueSafe(Element);
382 string Transfer = Element->GetValue();
383 if ( Transfer == "1.2.840.10008.1.2.1.99" )
389 * \ingroup gdcmHeader
390 * \brief Determines if the Transfer Syntax was allready encountered
391 * and if it corresponds to a Explicit VR Big Endian one.
393 * @return True when big endian found. False in all other cases.
395 bool gdcmHeader::IsExplicitVRBigEndianTransferSyntax(void) {
396 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
399 LoadElementValueSafe(Element);
400 string Transfer = Element->GetValue();
401 if ( Transfer == "1.2.840.10008.1.2.2" ) //1.2.2 ??? A verifier !
407 * \ingroup gdcmHeader
408 * \brief Determines if the Transfer Syntax was allready encountered
409 * and if it corresponds to a JPEGBaseLineProcess1 one.
411 * @return True when JPEGBaseLineProcess1found. False in all other cases.
413 bool gdcmHeader::IsJPEGBaseLineProcess1TransferSyntax(void) {
414 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
417 LoadElementValueSafe(Element);
418 string Transfer = Element->GetValue();
419 if ( Transfer == "1.2.840.10008.1.2.4.50" )
424 // faire qq chose d'intelligent a la place de ça
426 bool gdcmHeader::IsJPEGLossless(void) {
427 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
430 LoadElementValueSafe(Element);
431 const char * Transfert = Element->GetValue().c_str();
432 printf("TransfertSyntx %s\n",Transfert);
433 if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true;
434 if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true;
440 * \ingroup gdcmHeader
441 * \brief Determines if the Transfer Syntax was allready encountered
442 * and if it corresponds to a JPEGExtendedProcess2-4 one.
444 * @return True when JPEGExtendedProcess2-4 found. False in all other cases.
446 bool gdcmHeader::IsJPEGExtendedProcess2_4TransferSyntax(void) {
447 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
450 LoadElementValueSafe(Element);
451 string Transfer = Element->GetValue();
452 if ( Transfer == "1.2.840.10008.1.2.4.51" )
458 * \ingroup gdcmHeader
459 * \brief Determines if the Transfer Syntax was allready encountered
460 * and if it corresponds to a JPEGExtendeProcess3-5 one.
462 * @return True when JPEGExtendedProcess3-5 found. False in all other cases.
464 bool gdcmHeader::IsJPEGExtendedProcess3_5TransferSyntax(void) {
465 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
468 LoadElementValueSafe(Element);
469 string Transfer = Element->GetValue();
470 if ( Transfer == "1.2.840.10008.1.2.4.52" )
476 * \ingroup gdcmHeader
477 * \brief Determines if the Transfer Syntax was allready encountered
478 * and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
480 * @return True when JPEGSpectralSelectionProcess6-8 found. False in all
483 bool gdcmHeader::IsJPEGSpectralSelectionProcess6_8TransferSyntax(void) {
484 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
487 LoadElementValueSafe(Element);
488 string Transfer = Element->GetValue();
489 if ( Transfer == "1.2.840.10008.1.2.4.53" )
494 * \ingroup gdcmHeader
495 * \brief Predicate for dicom version 3 file.
496 * @return True when the file is a dicom version 3.
498 bool gdcmHeader::IsDicomV3(void) {
499 if ( (filetype == ExplicitVR)
500 || (filetype == ImplicitVR) )
506 * \ingroup gdcmHeader
507 * \brief When the length of an element value is obviously wrong (because
508 * the parser went Jabberwocky) one can hope improving things by
509 * applying this heuristic.
511 void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
512 if ( FoundLength == 0xffffffff)
514 ElVal->SetLength(FoundLength);
517 guint32 gdcmHeader::FindLengthOB(void) {
518 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
521 long PositionOnEntry = ftell(fp);
522 bool FoundSequenceDelimiter = false;
523 guint32 TotalLength = 0;
526 while ( ! FoundSequenceDelimiter) {
531 TotalLength += 4; // We even have to decount the group and element
533 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",
534 "wrong group for an item sequence.");
539 FoundSequenceDelimiter = true;
540 else if ( n != 0xe000) {
541 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",
542 "wrong element for an item sequence.");
546 ItemLength = ReadInt32();
547 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
548 // the ItemLength with ReadInt32
549 SkipBytes(ItemLength);
551 fseek(fp, PositionOnEntry, SEEK_SET);
555 void gdcmHeader::FindLength(gdcmElValue * ElVal) {
556 guint16 element = ElVal->GetElement();
557 string vr = ElVal->GetVR();
560 if ( (filetype == ExplicitVR) && ! ElVal->IsImplicitVr() ) {
562 if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") ) {
563 // The following reserved two bytes (see PS 3.5-2001, section
564 // 7.1.2 Data element structure with explicit vr p27) must be
565 // skipped before proceeding on reading the length on 4 bytes.
566 fseek(fp, 2L, SEEK_CUR);
567 guint32 length32 = ReadInt32();
568 if ( (vr == "OB") && (length32 == 0xffffffff) ) {
569 ElVal->SetLength(FindLengthOB());
572 FixFoundLength(ElVal, length32);
576 // Length is encoded on 2 bytes.
577 length16 = ReadInt16();
579 // We can tell the current file is encoded in big endian (like
580 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
581 // and it's value is the one of the encoding of a big endian file.
582 // In order to deal with such big endian encoded files, we have
583 // (at least) two strategies:
584 // * when we load the "Transfer Syntax" tag with value of big endian
585 // encoding, we raise the proper flags. Then we wait for the end
586 // of the META group (0x0002) among which is "Transfer Syntax",
587 // before switching the swap code to big endian. We have to postpone
588 // the switching of the swap code since the META group is fully encoded
589 // in little endian, and big endian coding only starts at the next
590 // group. The corresponding code can be hard to analyse and adds
591 // many additional unnecessary tests for regular tags.
592 // * the second strategy consists in waiting for trouble, that shall
593 // appear when we find the first group with big endian encoding. This
594 // is easy to detect since the length of a "Group Length" tag (the
595 // ones with zero as element number) has to be of 4 (0x0004). When we
596 // encouter 1024 (0x0400) chances are the encoding changed and we
597 // found a group with big endian encoding.
598 // We shall use this second strategy. In order to make sure that we
599 // can interpret the presence of an apparently big endian encoded
600 // length of a "Group Length" without committing a big mistake, we
601 // add an additional check: we look in the allready parsed elements
602 // for the presence of a "Transfer Syntax" whose value has to be "big
603 // endian encoding". When this is the case, chances are we have got our
604 // hands on a big endian encoded file: we switch the swap code to
605 // big endian and proceed...
606 if ( (element == 0x000) && (length16 == 0x0400) ) {
607 if ( ! IsExplicitVRBigEndianTransferSyntax() ) {
608 dbg.Verbose(0, "gdcmHeader::FindLength", "not explicit VR");
613 SwitchSwapToBigEndian();
614 // Restore the unproperly loaded values i.e. the group, the element
615 // and the dictionary entry depending on them.
616 guint16 CorrectGroup = SwapShort(ElVal->GetGroup());
617 guint16 CorrectElem = SwapShort(ElVal->GetElement());
618 gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
621 // This correct tag is not in the dictionary. Create a new one.
622 NewTag = new gdcmDictEntry(CorrectGroup, CorrectElem);
624 // FIXME this can create a memory leaks on the old entry that be
625 // left unreferenced.
626 ElVal->SetDictEntry(NewTag);
629 // Heuristic: well some files are really ill-formed.
630 if ( length16 == 0xffff) {
632 dbg.Verbose(0, "gdcmHeader::FindLength",
633 "Erroneous element length fixed.");
635 FixFoundLength(ElVal, (guint32)length16);
639 // Either implicit VR or a non DICOM conformal (see not below) explicit
640 // VR that ommited the VR of (at least) this element. Farts happen.
641 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
642 // on Data elements "Implicit and Explicit VR Data Elements shall
643 // not coexist in a Data Set and Data Sets nested within it".]
644 // Length is on 4 bytes.
645 FixFoundLength(ElVal, ReadInt32());
649 * \ingroup gdcmHeader
650 * \brief Swaps back the bytes of 4-byte long integer accordingly to
653 * @return The suggested integer.
655 guint32 gdcmHeader::SwapLong(guint32 a) {
660 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
661 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
665 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
669 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
672 dbg.Error(" gdcmHeader::SwapLong : unset swap code");
679 * \ingroup gdcmHeader
680 * \brief Swaps the bytes so they agree with the processor order
681 * @return The properly swaped 16 bits integer.
683 guint16 gdcmHeader::SwapShort(guint16 a) {
684 if ( (sw==4321) || (sw==2143) )
685 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
689 void gdcmHeader::SkipBytes(guint32 NBytes) {
690 //FIXME don't dump the returned value
691 (void)fseek(fp, (long)NBytes, SEEK_CUR);
694 void gdcmHeader::SkipElementValue(gdcmElValue * ElVal) {
695 SkipBytes(ElVal->GetLength());
698 void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) {
701 if ((guint32)NewSize >= (guint32)0xffffffff) {
702 MaxSizeLoadElementValue = 0xffffffff;
705 MaxSizeLoadElementValue = NewSize;
709 * \ingroup gdcmHeader
710 * \brief Loads the element content if it's length is not bigger
711 * than the value specified with
712 * gdcmHeader::SetMaxSizeLoadElementValue()
714 void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) {
716 guint16 group = ElVal->GetGroup();
717 string vr = ElVal->GetVR();
718 guint32 length = ElVal->GetLength();
719 bool SkipLoad = false;
721 fseek(fp, (long)ElVal->GetOffset(), SEEK_SET);
723 // FIXME Sequences not treated yet !
725 // Ne faudrait-il pas au contraire trouver immediatement
726 // une maniere 'propre' de traiter les sequences (vr = SQ)
727 // car commencer par les ignorer risque de conduire a qq chose
728 // qui pourrait ne pas etre generalisable
729 // Well, I'm expecting your code !!!
734 // Heuristic : a sequence "contains" a set of tags (called items). It looks
735 // like the last tag of a sequence (the one that terminates the sequence)
736 // has a group of 0xfffe (with a dummy length).
737 if( group == 0xfffe )
742 ElVal->SetValue("gdcm::Skipped");
746 // When the length is zero things are easy:
752 // The elements whose length is bigger than the specified upper bound
753 // are not loaded. Instead we leave a short notice of the offset of
754 // the element content and it's length.
755 if (length > MaxSizeLoadElementValue) {
757 s << "gdcm::NotLoaded.";
758 s << " Address:" << (long)ElVal->GetOffset();
759 s << " Length:" << ElVal->GetLength();
760 ElVal->SetValue(s.str());
764 // When an integer is expected, read and convert the following two or
765 // four bytes properly i.e. as an integer as opposed to a string.
767 // pour les elements de Value Multiplicity > 1
768 // on aura en fait une serie d'entiers
770 // on devrait pouvoir faire + compact (?)
772 if ( IsAnInteger(ElVal) ) {
776 if (vr == "US" || vr == "SS") {
778 NewInt = ReadInt16();
781 for (int i=1; i < nbInt; i++) {
783 NewInt = ReadInt16();
788 } else if (vr == "UL" || vr == "SL") {
790 NewInt = ReadInt32();
793 for (int i=1; i < nbInt; i++) {
795 NewInt = ReadInt32();
800 ElVal->SetValue(s.str());
804 // We need an additional byte for storing \0 that is not on disk
805 char* NewValue = (char*)malloc(length+1);
807 dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
812 item_read = fread(NewValue, (size_t)length, (size_t)1, fp);
813 if ( item_read != 1 ) {
815 dbg.Verbose(1, "gdcmHeader::LoadElementValue","unread element value");
816 ElVal->SetValue("gdcm::UnRead");
819 ElVal->SetValue(NewValue);
824 * \ingroup gdcmHeader
825 * \brief Loads the element while preserving the current
826 * underlying file position indicator as opposed to
827 * to LoadElementValue that modifies it.
828 * @param ElVal Element whose value shall be loaded.
831 void gdcmHeader::LoadElementValueSafe(gdcmElValue * ElVal) {
832 long PositionOnEntry = ftell(fp);
833 LoadElementValue(ElVal);
834 fseek(fp, PositionOnEntry, SEEK_SET);
838 guint16 gdcmHeader::ReadInt16(void) {
841 item_read = fread (&g, (size_t)2,(size_t)1, fp);
843 if ( item_read != 1 ) {
844 dbg.Verbose(1, "gdcmHeader::ReadInt16", " File read error");
852 guint32 gdcmHeader::ReadInt32(void) {
855 item_read = fread (&g, (size_t)4,(size_t)1, fp);
857 if ( item_read != 1 ) {
858 dbg.Verbose(1, "gdcmHeader::ReadInt32", " File read error");
867 gdcmElValue* gdcmHeader::GetElValueByNumber(guint16 Group, guint16 Elem) {
869 gdcmElValue* elValue = PubElValSet.GetElementByNumber(Group, Elem);
871 dbg.Verbose(1, "gdcmHeader::GetElValueByNumber",
872 "failed to Locate gdcmElValue");
873 return (gdcmElValue*)0;
879 * \ingroup gdcmHeader
880 * \brief Build a new Element Value from all the low level arguments.
881 * Check for existence of dictionary entry, and build
882 * a default one when absent.
883 * @param Group group of the underlying DictEntry
884 * @param Elem element of the underlying DictEntry
886 gdcmElValue* gdcmHeader::NewElValueByNumber(guint16 Group, guint16 Elem) {
887 // Find out if the tag we encountered is in the dictionaries:
888 gdcmDictEntry * NewTag = GetDictEntryByNumber(Group, Elem);
890 NewTag = new gdcmDictEntry(Group, Elem);
892 gdcmElValue* NewElVal = new gdcmElValue(NewTag);
894 dbg.Verbose(1, "gdcmHeader::NewElValueByNumber",
895 "failed to allocate gdcmElValue");
896 return (gdcmElValue*)0;
902 * \ingroup gdcmHeader
906 int gdcmHeader::ReplaceOrCreateByNumber(string Value, guint16 Group, guint16 Elem ) {
908 gdcmElValue* nvElValue=NewElValueByNumber(Group, Elem);
909 PubElValSet.Add(nvElValue);
910 PubElValSet.SetElValueByNumber(Value, Group, Elem);
916 * \ingroup gdcmHeader
917 * \brief Build a new Element Value from all the low level arguments.
918 * Check for existence of dictionary entry, and build
919 * a default one when absent.
920 * @param Name Name of the underlying DictEntry
922 gdcmElValue* gdcmHeader::NewElValueByName(string Name) {
924 gdcmDictEntry * NewTag = GetDictEntryByName(Name);
926 NewTag = new gdcmDictEntry(0xffff, 0xffff, "LO", "Unknown", Name);
928 gdcmElValue* NewElVal = new gdcmElValue(NewTag);
930 dbg.Verbose(1, "gdcmHeader::ObtainElValueByName",
931 "failed to allocate gdcmElValue");
932 return (gdcmElValue*)0;
938 * \ingroup gdcmHeader
939 * \brief Read the next tag but WITHOUT loading it's value
940 * @return On succes the newly created ElValue, NULL on failure.
942 gdcmElValue * gdcmHeader::ReadNextElement(void) {
945 gdcmElValue * NewElVal;
950 // We reached the EOF (or an error occured) and header parsing
951 // has to be considered as finished.
952 return (gdcmElValue *)0;
954 NewElVal = NewElValueByNumber(g, n);
956 FindLength(NewElVal);
959 return (gdcmElValue *)0;
960 NewElVal->SetOffset(ftell(fp));
965 * \ingroup gdcmHeader
966 * \brief Apply some heuristics to predict wether the considered
967 * element value contains/represents an integer or not.
968 * @param ElVal The element value on which to apply the predicate.
969 * @return The result of the heuristical predicate.
971 bool gdcmHeader::IsAnInteger(gdcmElValue * ElVal) {
972 guint16 group = ElVal->GetGroup();
973 guint16 element = ElVal->GetElement();
974 string vr = ElVal->GetVR();
975 guint32 length = ElVal->GetLength();
977 // When we have some semantics on the element we just read, and if we
978 // a priori know we are dealing with an integer, then we shall be
979 // able to swap it's element value properly.
980 if ( element == 0 ) { // This is the group length of the group
984 printf("Erroneous Group Length element length (%04x , %04x) : %d\n",
985 group, element,length);
987 dbg.Error("gdcmHeader::IsAnInteger",
988 "Erroneous Group Length element length.");
992 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
999 * \ingroup gdcmHeader
1000 * \brief Recover the offset (from the beginning of the file) of the pixels.
1002 size_t gdcmHeader::GetPixelOffset(void) {
1003 // If this file complies with the norm we should encounter the
1004 // "Image Location" tag (0x0028, 0x0200). This tag contains the
1005 // the group that contains the pixel data (hence the "Pixel Data"
1006 // is found by indirection through the "Image Location").
1007 // Inside the group pointed by "Image Location" the searched element
1008 // is conventionally the element 0x0010 (when the norm is respected).
1009 // When the "Image Location" is absent we default to group 0x7fe0.
1012 string ImageLocation = GetPubElValByName("Image Location");
1013 if ( ImageLocation == "gdcm::Unfound" ) {
1016 grPixel = (guint16) atoi( ImageLocation.c_str() );
1018 if (grPixel != 0x7fe0)
1019 // This is a kludge for old dirty Philips imager.
1023 gdcmElValue* PixelElement = PubElValSet.GetElementByNumber(grPixel,
1026 return PixelElement->GetOffset();
1032 * \ingroup gdcmHeader
1033 * \brief Searches both the public and the shadow dictionary (when they
1034 * exist) for the presence of the DictEntry with given
1035 * group and element. The public dictionary has precedence on the
1037 * @param group group of the searched DictEntry
1038 * @param element element of the searched DictEntry
1039 * @return Corresponding DictEntry when it exists, NULL otherwise.
1041 gdcmDictEntry * gdcmHeader::GetDictEntryByNumber(guint16 group,
1043 gdcmDictEntry * found = (gdcmDictEntry*)0;
1044 if (!RefPubDict && !RefShaDict) {
1045 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
1046 "we SHOULD have a default dictionary");
1049 found = RefPubDict->GetTagByNumber(group, element);
1054 found = RefShaDict->GetTagByNumber(group, element);
1062 * \ingroup gdcmHeader
1063 * \brief Searches both the public and the shadow dictionary (when they
1064 * exist) for the presence of the DictEntry with given name.
1065 * The public dictionary has precedence on the shadow one.
1066 * @param Name name of the searched DictEntry
1067 * @return Corresponding DictEntry when it exists, NULL otherwise.
1069 gdcmDictEntry * gdcmHeader::GetDictEntryByName(string Name) {
1070 gdcmDictEntry * found = (gdcmDictEntry*)0;
1071 if (!RefPubDict && !RefShaDict) {
1072 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
1073 "we SHOULD have a default dictionary");
1076 found = RefPubDict->GetTagByName(Name);
1081 found = RefShaDict->GetTagByName(Name);
1089 * \ingroup gdcmHeader
1090 * \brief Searches within the public dictionary for element value of
1092 * @param group Group of the researched tag.
1093 * @param element Element of the researched tag.
1094 * @return Corresponding element value when it exists, and the string
1095 * "gdcm::Unfound" otherwise.
1097 string gdcmHeader::GetPubElValByNumber(guint16 group, guint16 element) {
1098 return PubElValSet.GetElValueByNumber(group, element);
1102 * \ingroup gdcmHeader
1103 * \brief Searches within the public dictionary for element value
1104 * representation of a given tag.
1106 * Obtaining the VR (Value Representation) might be needed by caller
1107 * to convert the string typed content to caller's native type
1108 * (think of C++ vs Python). The VR is actually of a higher level
1109 * of semantics than just the native C++ type.
1110 * @param group Group of the researched tag.
1111 * @param element Element of the researched tag.
1112 * @return Corresponding element value representation when it exists,
1113 * and the string "gdcm::Unfound" otherwise.
1115 string gdcmHeader::GetPubElValRepByNumber(guint16 group, guint16 element) {
1116 gdcmElValue* elem = PubElValSet.GetElementByNumber(group, element);
1118 return "gdcm::Unfound";
1119 return elem->GetVR();
1123 * \ingroup gdcmHeader
1124 * \brief Searches within the public dictionary for element value of
1126 * @param TagName name of the researched element.
1127 * @return Corresponding element value when it exists, and the string
1128 * "gdcm::Unfound" otherwise.
1130 string gdcmHeader::GetPubElValByName(string TagName) {
1131 return PubElValSet.GetElValueByName(TagName);
1135 * \ingroup gdcmHeader
1136 * \brief Searches within the elements parsed with the public dictionary for
1137 * the element value representation of a given tag.
1139 * Obtaining the VR (Value Representation) might be needed by caller
1140 * to convert the string typed content to caller's native type
1141 * (think of C++ vs Python). The VR is actually of a higher level
1142 * of semantics than just the native C++ type.
1143 * @param TagName name of the researched element.
1144 * @return Corresponding element value representation when it exists,
1145 * and the string "gdcm::Unfound" otherwise.
1147 string gdcmHeader::GetPubElValRepByName(string TagName) {
1148 gdcmElValue* elem = PubElValSet.GetElementByName(TagName);
1150 return "gdcm::Unfound";
1151 return elem->GetVR();
1155 * \ingroup gdcmHeader
1156 * \brief Searches within elements parsed with the SHADOW dictionary
1157 * for the element value of a given tag.
1158 * @param group Group of the researched tag.
1159 * @param element Element of the researched tag.
1160 * @return Corresponding element value representation when it exists,
1161 * and the string "gdcm::Unfound" otherwise.
1163 string gdcmHeader::GetShaElValByNumber(guint16 group, guint16 element) {
1164 return ShaElValSet.GetElValueByNumber(group, element);
1168 * \ingroup gdcmHeader
1169 * \brief Searches within the elements parsed with the SHADOW dictionary
1170 * for the element value representation of a given tag.
1172 * Obtaining the VR (Value Representation) might be needed by caller
1173 * to convert the string typed content to caller's native type
1174 * (think of C++ vs Python). The VR is actually of a higher level
1175 * of semantics than just the native C++ type.
1176 * @param group Group of the researched tag.
1177 * @param element Element of the researched tag.
1178 * @return Corresponding element value representation when it exists,
1179 * and the string "gdcm::Unfound" otherwise.
1181 string gdcmHeader::GetShaElValRepByNumber(guint16 group, guint16 element) {
1182 gdcmElValue* elem = ShaElValSet.GetElementByNumber(group, element);
1184 return "gdcm::Unfound";
1185 return elem->GetVR();
1189 * \ingroup gdcmHeader
1190 * \brief Searches within the elements parsed with the shadow dictionary
1191 * for an element value of given tag.
1192 * @param TagName name of the researched element.
1193 * @return Corresponding element value when it exists, and the string
1194 * "gdcm::Unfound" otherwise.
1196 string gdcmHeader::GetShaElValByName(string TagName) {
1197 return ShaElValSet.GetElValueByName(TagName);
1201 * \ingroup gdcmHeader
1202 * \brief Searches within the elements parsed with the shadow dictionary for
1203 * the element value representation of a given tag.
1205 * Obtaining the VR (Value Representation) might be needed by caller
1206 * to convert the string typed content to caller's native type
1207 * (think of C++ vs Python). The VR is actually of a higher level
1208 * of semantics than just the native C++ type.
1209 * @param TagName name of the researched element.
1210 * @return Corresponding element value representation when it exists,
1211 * and the string "gdcm::Unfound" otherwise.
1213 string gdcmHeader::GetShaElValRepByName(string TagName) {
1214 gdcmElValue* elem = ShaElValSet.GetElementByName(TagName);
1216 return "gdcm::Unfound";
1217 return elem->GetVR();
1221 * \ingroup gdcmHeader
1222 * \brief Searches within elements parsed with the public dictionary
1223 * and then within the elements parsed with the shadow dictionary
1224 * for the element value of a given tag.
1225 * @param group Group of the researched tag.
1226 * @param element Element of the researched tag.
1227 * @return Corresponding element value representation when it exists,
1228 * and the string "gdcm::Unfound" otherwise.
1230 string gdcmHeader::GetElValByNumber(guint16 group, guint16 element) {
1231 string pub = GetPubElValByNumber(group, element);
1234 return GetShaElValByNumber(group, element);
1238 * \ingroup gdcmHeader
1239 * \brief Searches within elements parsed with the public dictionary
1240 * and then within the elements parsed with the shadow dictionary
1241 * for the element value representation of a given tag.
1243 * Obtaining the VR (Value Representation) might be needed by caller
1244 * to convert the string typed content to caller's native type
1245 * (think of C++ vs Python). The VR is actually of a higher level
1246 * of semantics than just the native C++ type.
1247 * @param group Group of the researched tag.
1248 * @param element Element of the researched tag.
1249 * @return Corresponding element value representation when it exists,
1250 * and the string "gdcm::Unfound" otherwise.
1252 string gdcmHeader::GetElValRepByNumber(guint16 group, guint16 element) {
1253 string pub = GetPubElValRepByNumber(group, element);
1256 return GetShaElValRepByNumber(group, element);
1260 * \ingroup gdcmHeader
1261 * \brief Searches within elements parsed with the public dictionary
1262 * and then within the elements parsed with the shadow dictionary
1263 * for the element value of a given tag.
1264 * @param TagName name of the researched element.
1265 * @return Corresponding element value when it exists,
1266 * and the string "gdcm::Unfound" otherwise.
1268 string gdcmHeader::GetElValByName(string TagName) {
1269 string pub = GetPubElValByName(TagName);
1272 return GetShaElValByName(TagName);
1276 * \ingroup gdcmHeader
1277 * \brief Searches within elements parsed with the public dictionary
1278 * and then within the elements parsed with the shadow dictionary
1279 * for the element value representation of a given tag.
1281 * Obtaining the VR (Value Representation) might be needed by caller
1282 * to convert the string typed content to caller's native type
1283 * (think of C++ vs Python). The VR is actually of a higher level
1284 * of semantics than just the native C++ type.
1285 * @param TagName name of the researched element.
1286 * @return Corresponding element value representation when it exists,
1287 * and the string "gdcm::Unfound" otherwise.
1289 string gdcmHeader::GetElValRepByName(string TagName) {
1290 string pub = GetPubElValRepByName(TagName);
1293 return GetShaElValRepByName(TagName);
1297 * \ingroup gdcmHeader
1298 * \brief Accesses an existing gdcmElValue in the PubElValSet of this instance
1299 * through it's (group, element) and modifies it's content with
1301 * @param content new value to substitute with
1302 * @param group group of the ElVal to modify
1303 * @param element element of the ElVal to modify
1305 int gdcmHeader::SetPubElValByNumber(string content, guint16 group,
1308 //TODO : homogeneiser les noms : SetPubElValByNumber qui appelle PubElValSet.SetElValueByNumber
1309 // pourquoi pas SetPubElValueByNumber ??
1312 return ( PubElValSet.SetElValueByNumber (content, group, element) );
1316 * \ingroup gdcmHeader
1317 * \brief Accesses an existing gdcmElValue in the PubElValSet of this instance
1318 * through tag name and modifies it's content with the given value.
1319 * @param content new value to substitute with
1320 * @param TagName name of the tag to be modified
1322 int gdcmHeader::SetPubElValByName(string content, string TagName) {
1323 return ( PubElValSet.SetElValueByName (content, TagName) );
1327 * \ingroup gdcmHeader
1328 * \brief Accesses an existing gdcmElValue in the PubElValSet of this instance
1329 * through it's (group, element) and modifies it's length with
1331 * \warning Use with extreme caution.
1332 * @param length new length to substitute with
1333 * @param group group of the ElVal to modify
1334 * @param element element of the ElVal to modify
1335 * @return 1 on success, 0 otherwise.
1338 int gdcmHeader::SetPubElValLengthByNumber(guint32 length, guint16 group,
1340 return ( PubElValSet.SetElValueLengthByNumber (length, group, element) );
1344 * \ingroup gdcmHeader
1345 * \brief Accesses an existing gdcmElValue in the ShaElValSet of this instance
1346 * through it's (group, element) and modifies it's content with
1348 * @param content new value to substitute with
1349 * @param group group of the ElVal to modify
1350 * @param element element of the ElVal to modify
1351 * @return 1 on success, 0 otherwise.
1353 int gdcmHeader::SetShaElValByNumber(string content,
1354 guint16 group, guint16 element) {
1355 return ( ShaElValSet.SetElValueByNumber (content, group, element) );
1359 * \ingroup gdcmHeader
1360 * \brief Accesses an existing gdcmElValue in the ShaElValSet of this instance
1361 * through tag name and modifies it's content with the given value.
1362 * @param content new value to substitute with
1363 * @param TagName name of the tag to be modified
1365 int gdcmHeader::SetShaElValByName(string content, string TagName) {
1366 return ( ShaElValSet.SetElValueByName (content, TagName) );
1370 * \ingroup gdcmHeader
1371 * \brief Parses the header of the file but WITHOUT loading element values.
1373 void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
1374 gdcmElValue * newElValue = (gdcmElValue *)0;
1378 while ( (newElValue = ReadNextElement()) ) {
1379 SkipElementValue(newElValue);
1380 PubElValSet.Add(newElValue);
1385 * \ingroup gdcmHeader
1386 * \brief Retrieve the number of columns of image.
1387 * @return The encountered size when found, 0 by default.
1389 int gdcmHeader::GetXSize(void) {
1390 // We cannot check for "Columns" because the "Columns" tag is present
1391 // both in IMG (0028,0011) and OLY (6000,0011) sections of the dictionary.
1392 string StrSize = GetPubElValByNumber(0x0028,0x0011);
1393 if (StrSize == "gdcm::Unfound")
1395 return atoi(StrSize.c_str());
1399 * \ingroup gdcmHeader
1400 * \brief Retrieve the number of lines of image.
1401 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
1402 * @return The encountered size when found, 1 by default.
1404 int gdcmHeader::GetYSize(void) {
1405 // We cannot check for "Rows" because the "Rows" tag is present
1406 // both in IMG (0028,0010) and OLY (6000,0010) sections of the dictionary.
1407 string StrSize = GetPubElValByNumber(0x0028,0x0010);
1408 if (StrSize != "gdcm::Unfound")
1409 return atoi(StrSize.c_str());
1413 // The Rows (0028,0010) entry is optional for ACR/NEMA. It might
1414 // hence be a signal (1d image). So we default to 1:
1419 * \ingroup gdcmHeader
1420 * \brief Retrieve the number of planes of volume or the number
1421 * of frames of a multiframe.
1422 * \warning When present we consider the "Number of Frames" as the third
1423 * dimension. When absent we consider the third dimension as
1424 * being the "Planes" tag content.
1425 * @return The encountered size when found, 1 by default.
1427 int gdcmHeader::GetZSize(void) {
1428 // Both in DicomV3 and ACR/Nema the consider the "Number of Frames"
1429 // as the third dimension.
1430 string StrSize = GetPubElValByNumber(0x0028,0x0008);
1431 if (StrSize != "gdcm::Unfound")
1432 return atoi(StrSize.c_str());
1434 // We then consider the "Planes" entry as the third dimension [we
1435 // cannot retrieve by name since "Planes tag is present both in
1436 // IMG (0028,0012) and OLY (6000,0012) sections of the dictionary].
1437 StrSize = GetPubElValByNumber(0x0028,0x0012);
1438 if (StrSize != "gdcm::Unfound")
1439 return atoi(StrSize.c_str());
1444 * \ingroup gdcmHeader
1445 * \brief Return the size (in bytes) of a single pixel of data.
1446 * @return The size in bytes of a single pixel of data.
1449 int gdcmHeader::GetPixelSize(void) {
1450 string PixelType = GetPixelType();
1451 if (PixelType == "8U" || PixelType == "8S")
1453 if (PixelType == "16U" || PixelType == "16S")
1455 if (PixelType == "32U" || PixelType == "32S")
1457 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
1462 * \ingroup gdcmHeader
1463 * \brief Build the Pixel Type of the image.
1464 * Possible values are:
1465 * - 8U unsigned 8 bit,
1466 * - 8S signed 8 bit,
1467 * - 16U unsigned 16 bit,
1468 * - 16S signed 16 bit,
1469 * - 32U unsigned 32 bit,
1470 * - 32S signed 32 bit,
1471 * \warning 12 bit images appear as 16 bit.
1474 string gdcmHeader::GetPixelType(void) {
1476 BitsAlloc = GetElValByName("Bits Allocated");
1477 if (BitsAlloc == "gdcm::Unfound") {
1478 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
1479 BitsAlloc = string("16");
1481 if (BitsAlloc == "12")
1482 BitsAlloc = string("16");
1485 Signed = GetElValByName("Pixel Representation");
1486 if (Signed == "gdcm::Unfound") {
1487 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
1488 BitsAlloc = string("0");
1491 Signed = string("U");
1493 Signed = string("S");
1495 return( BitsAlloc + Signed);
1499 * \ingroup gdcmHeader
1500 * \brief This predicate, based on hopefully reasonnable heuristics,
1501 * decides whether or not the current gdcmHeader was properly parsed
1502 * and contains the mandatory information for being considered as
1503 * a well formed and usable image.
1504 * @return true when gdcmHeader is the one of a reasonable Dicom file,
1507 bool gdcmHeader::IsReadable(void) {
1508 if ( GetElValByName("Image Dimensions") != "gdcm::Unfound"
1509 && atoi(GetElValByName("Image Dimensions").c_str()) > 4 ) {
1512 if ( GetElValByName("Bits Allocated") == "gdcm::Unfound" )
1514 if ( GetElValByName("Bits Stored") == "gdcm::Unfound" )
1516 if ( GetElValByName("High Bit") == "gdcm::Unfound" )
1518 if ( GetElValByName("Pixel Representation") == "gdcm::Unfound" )
1524 * \ingroup gdcmHeader
1525 * \brief Small utility function that creates a new manually crafted
1526 * (as opposed as read from the file) gdcmElValue with user
1527 * specified name and adds it to the public tag hash table.
1528 * \note A fake TagKey is generated so the PubDict can keep it's coherence.
1529 * @param NewTagName The name to be given to this new tag.
1530 * @param VR The Value Representation to be given to this new tag.
1531 * @ return The newly hand crafted Element Value.
1533 gdcmElValue* gdcmHeader::NewManualElValToPubDict(string NewTagName, string VR) {
1534 gdcmElValue* NewElVal = (gdcmElValue*)0;
1535 guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
1536 guint32 FreeElem = 0;
1537 gdcmDictEntry* NewEntry = (gdcmDictEntry*)0;
1539 FreeElem = PubElValSet.GenerateFreeTagKeyInGroup(StuffGroup);
1540 if (FreeElem == UINT32_MAX) {
1541 dbg.Verbose(1, "gdcmHeader::NewManualElValToPubDict",
1542 "Group 0xffff in Public Dict is full");
1543 return (gdcmElValue*)0;
1545 NewEntry = new gdcmDictEntry(StuffGroup, FreeElem,
1546 VR, "GDCM", NewTagName);
1547 NewElVal = new gdcmElValue(NewEntry);
1548 PubElValSet.Add(NewElVal);
1554 * \ingroup gdcmHeader
1555 * \brief Loads the element values of all the elements present in the
1556 * public tag based hash table.
1558 void gdcmHeader::LoadElements(void) {
1560 TagElValueHT ht = PubElValSet.GetTagHt();
1561 for (TagElValueHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) {
1562 LoadElementValue(tag->second);
1566 void gdcmHeader::PrintPubElVal(std::ostream & os) {
1567 PubElValSet.Print(os);
1570 void gdcmHeader::PrintPubDict(std::ostream & os) {
1571 RefPubDict->Print(os);
1574 int gdcmHeader::Write(FILE * fp, FileType type) {
1575 return PubElValSet.Write(fp, type);