2 // $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.78 2003/07/03 14:38:16 jpr Exp $
10 #include <netinet/in.h>
12 #include <cctype> // for isalpha
15 #include "gdcmHeader.h"
19 // TODO : remove DEBUG
22 // Refer to gdcmHeader::CheckSwap()
23 #define HEADER_LENGTH_TO_READ 256
24 // Refer to gdcmHeader::SetMaxSizeLoadElementValue()
25 #define _MaxSizeLoadElementValue_ 1024
31 void gdcmHeader::Initialise(void) {
32 dicom_vr = gdcmGlobal::GetVR();
33 dicom_ts = gdcmGlobal::GetTS();
34 Dicts = gdcmGlobal::GetDicts();
35 RefPubDict = Dicts->GetDefaultPubDict();
36 RefShaDict = (gdcmDict*)0;
43 * @param exception_on_error
45 gdcmHeader::gdcmHeader(const char *InFilename, bool exception_on_error) {
46 SetMaxSizeLoadElementValue(_MaxSizeLoadElementValue_);
47 filename = InFilename;
49 if ( !OpenFile(exception_on_error))
59 * @param exception_on_error
61 gdcmHeader::gdcmHeader(bool exception_on_error) {
62 SetMaxSizeLoadElementValue(_MaxSizeLoadElementValue_);
69 * @param exception_on_error
72 bool gdcmHeader::OpenFile(bool exception_on_error)
73 throw(gdcmFileError) {
74 fp=fopen(filename.c_str(),"rb");
75 if(exception_on_error) {
77 throw gdcmFileError("gdcmHeader::gdcmHeader(const char *, bool)");
81 dbg.Verbose(0, "gdcmHeader::gdcmHeader cannot open file", filename.c_str());
90 bool gdcmHeader::CloseFile(void) {
91 int closed = fclose(fp);
100 * \brief Canonical destructor.
102 gdcmHeader::~gdcmHeader (void) {
103 dicom_vr = (gdcmVR*)0;
104 Dicts = (gdcmDictSet*)0;
105 RefPubDict = (gdcmDict*)0;
106 RefShaDict = (gdcmDict*)0;
112 // META Meta Information
124 // NMI Nuclear Medicine
126 // BFS Basic Film Session
127 // BFB Basic Film Box
128 // BIB Basic Image Box
143 * \ingroup gdcmHeader
144 * \brief Discover what the swap code is (among little endian, big endian,
145 * bad little endian, bad big endian).
148 void gdcmHeader::CheckSwap()
150 // The only guaranted way of finding the swap code is to find a
151 // group tag since we know it's length has to be of four bytes i.e.
152 // 0x00000004. Finding the swap code in then straigthforward. Trouble
153 // occurs when we can't find such group...
155 guint32 x=4; // x : pour ntohs
156 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
160 char deb[HEADER_LENGTH_TO_READ];
162 // First, compare HostByteOrder and NetworkByteOrder in order to
163 // determine if we shall need to swap bytes (i.e. the Endian type).
169 // The easiest case is the one of a DICOM header, since it possesses a
170 // file preamble where it suffice to look for the string "DICM".
171 lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
174 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
175 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "looks like DICOM Version3");
176 // Next, determine the value representation (VR). Let's skip to the
177 // first element (0002, 0000) and check there if we find "UL"
178 // - or "OB" if the 1st one is (0002,0001) -,
179 // in which case we (almost) know it is explicit VR.
180 // WARNING: if it happens to be implicit VR then what we will read
181 // is the length of the group. If this ascii representation of this
182 // length happens to be "UL" then we shall believe it is explicit VR.
183 // FIXME: in order to fix the above warning, we could read the next
184 // element value (or a couple of elements values) in order to make
185 // sure we are not commiting a big mistake.
187 // * the 128 bytes of File Preamble (often padded with zeroes),
188 // * the 4 bytes of "DICM" string,
189 // * the 4 bytes of the first tag (0002, 0000),or (0002, 0001)
190 // i.e. a total of 136 bytes.
193 // Use gdcmHeader::dicom_vr to test all the possibilities
194 // instead of just checking for UL, OB and UI !?
195 if( (memcmp(entCur, "UL", (size_t)2) == 0) ||
196 (memcmp(entCur, "OB", (size_t)2) == 0) ||
197 (memcmp(entCur, "UI", (size_t)2) == 0) )
199 filetype = ExplicitVR;
200 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
201 "explicit Value Representation");
203 filetype = ImplicitVR;
204 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
205 "not an explicit Value Representation");
209 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
210 "HostByteOrder != NetworkByteOrder");
213 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
214 "HostByteOrder = NetworkByteOrder");
217 // Position the file position indicator at first tag (i.e.
218 // after the file preamble and the "DICM" string).
220 fseek (fp, 132L, SEEK_SET);
224 // Alas, this is not a DicomV3 file and whatever happens there is no file
225 // preamble. We can reset the file position indicator to where the data
226 // is (i.e. the beginning of the file).
227 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "not a DICOM Version3 file");
230 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
231 // By clean we mean that the length of the first tag is written down.
232 // If this is the case and since the length of the first group HAS to be
233 // four (bytes), then determining the proper swap code is straightforward.
236 // We assume the array of char we are considering contains the binary
237 // representation of a 32 bits integer. Hence the following dirty
239 s = *((guint32 *)(entCur));
259 dbg.Verbose(0, "gdcmHeader::CheckSwap:",
260 "ACR/NEMA unfound swap info (time to raise bets)");
263 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
264 // It is time for despaired wild guesses. So, let's assume this file
265 // happens to be 'dirty' ACR/NEMA, i.e. the length of the group is
266 // not present. Then the only info we have is the net2host one.
276 * \ingroup gdcmHeader
279 void gdcmHeader::SwitchSwapToBigEndian(void) {
280 dbg.Verbose(1, "gdcmHeader::SwitchSwapToBigEndian",
281 "Switching to BigEndian mode.");
299 * \ingroup gdcmHeader
300 * \brief Find the value representation of the current tag.
303 void gdcmHeader::FindVR( gdcmElValue *ElVal) {
304 if (filetype != ExplicitVR)
310 char msg[100]; // for sprintf. Sorry
312 long PositionOnEntry = ftell(fp);
313 // Warning: we believe this is explicit VR (Value Representation) because
314 // we used a heuristic that found "UL" in the first tag. Alas this
315 // doesn't guarantee that all the tags will be in explicit VR. In some
316 // cases (see e-film filtered files) one finds implicit VR tags mixed
317 // within an explicit VR file. Hence we make sure the present tag
318 // is in explicit VR and try to fix things if it happens not to be
320 bool RealExplicit = true;
322 lgrLue=fread (&VR, (size_t)2,(size_t)1, fp);
326 // Assume we are reading a falsely explicit VR file i.e. we reached
327 // a tag where we expect reading a VR but are in fact we read the
328 // first to bytes of the length. Then we will interogate (through find)
329 // the dicom_vr dictionary with oddities like "\004\0" which crashes
330 // both GCC and VC++ implementations of the STL map. Hence when the
331 // expected VR read happens to be non-ascii characters we consider
332 // we hit falsely explicit VR tag.
334 if ( (!isalpha(VR[0])) && (!isalpha(VR[1])) )
335 RealExplicit = false;
337 // CLEANME searching the dicom_vr at each occurence is expensive.
338 // PostPone this test in an optional integrity check at the end
339 // of parsing or only in debug mode.
340 if ( RealExplicit && !dicom_vr->Count(vr) )
343 if ( RealExplicit ) {
344 if ( ElVal->IsVrUnknown() ) {
345 // When not a dictionary entry, we can safely overwrite the vr.
349 if ( ElVal->GetVR() == vr ) {
350 // The vr we just read and the dictionary agree. Nothing to do.
353 // The vr present in the file and the dictionary disagree. We assume
354 // the file writer knew best and use the vr of the file. Since it would
355 // be unwise to overwrite the vr of a dictionary (since it would
356 // compromise it's next user), we need to clone the actual DictEntry
357 // and change the vr for the read one.
358 gdcmDictEntry* NewTag = new gdcmDictEntry(ElVal->GetGroup(),
363 ElVal->SetDictEntry(NewTag);
367 // We thought this was explicit VR, but we end up with an
368 // implicit VR tag. Let's backtrack.
370 sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n", ElVal->GetGroup(),ElVal->GetElement());
371 dbg.Verbose(1, "gdcmHeader::FindVR: ",msg);
373 fseek(fp, PositionOnEntry, SEEK_SET);
374 // When this element is known in the dictionary we shall use, e.g. for
375 // the semantics (see the usage of IsAnInteger), the vr proposed by the
376 // dictionary entry. Still we have to flag the element as implicit since
377 // we know now our assumption on expliciteness is not furfilled.
379 if ( ElVal->IsVrUnknown() )
380 ElVal->SetVR("Implicit");
381 ElVal->SetImplicitVr();
385 * \ingroup gdcmHeader
386 * \brief Determines if the Transfer Syntax was already encountered
387 * and if it corresponds to a ImplicitVRLittleEndian one.
389 * @return True when ImplicitVRLittleEndian found. False in all other cases.
391 bool gdcmHeader::IsImplicitVRLittleEndianTransferSyntax(void) {
392 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
395 LoadElementValueSafe(Element);
396 string Transfer = Element->GetValue();
397 if ( Transfer == "1.2.840.10008.1.2" )
403 * \ingroup gdcmHeader
404 * \brief Determines if the Transfer Syntax was already encountered
405 * and if it corresponds to a ExplicitVRLittleEndian one.
407 * @return True when ExplicitVRLittleEndian found. False in all other cases.
409 bool gdcmHeader::IsExplicitVRLittleEndianTransferSyntax(void) {
410 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
413 LoadElementValueSafe(Element);
414 string Transfer = Element->GetValue();
415 if ( Transfer == "1.2.840.10008.1.2.1" )
421 * \ingroup gdcmHeader
422 * \brief Determines if the Transfer Syntax was already encountered
423 * and if it corresponds to a DeflatedExplicitVRLittleEndian one.
425 * @return True when DeflatedExplicitVRLittleEndian found. False in all other cases.
427 bool gdcmHeader::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
428 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
431 LoadElementValueSafe(Element);
432 string Transfer = Element->GetValue();
433 if ( Transfer == "1.2.840.10008.1.2.1.99" )
439 * \ingroup gdcmHeader
440 * \brief Determines if the Transfer Syntax was already encountered
441 * and if it corresponds to a Explicit VR Big Endian one.
443 * @return True when big endian found. False in all other cases.
445 bool gdcmHeader::IsExplicitVRBigEndianTransferSyntax(void) {
446 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
449 LoadElementValueSafe(Element);
450 string Transfer = Element->GetValue();
451 if ( Transfer == "1.2.840.10008.1.2.2" ) //1.2.2 ??? A verifier !
457 * \ingroup gdcmHeader
458 * \brief Determines if the Transfer Syntax was already encountered
459 * and if it corresponds to a JPEGBaseLineProcess1 one.
461 * @return True when JPEGBaseLineProcess1found. False in all other cases.
463 bool gdcmHeader::IsJPEGBaseLineProcess1TransferSyntax(void) {
464 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
467 LoadElementValueSafe(Element);
468 string Transfer = Element->GetValue();
469 if ( Transfer == "1.2.840.10008.1.2.4.50" )
475 * \ingroup gdcmHeader
480 bool gdcmHeader::IsJPEGLossless(void) {
481 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
482 // faire qq chose d'intelligent a la place de ça
485 LoadElementValueSafe(Element);
486 const char * Transfert = Element->GetValue().c_str();
487 if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true;
488 if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true;
494 * \ingroup gdcmHeader
495 * \brief Determines if the Transfer Syntax was already encountered
496 * and if it corresponds to a JPEGExtendedProcess2-4 one.
498 * @return True when JPEGExtendedProcess2-4 found. False in all other cases.
500 bool gdcmHeader::IsJPEGExtendedProcess2_4TransferSyntax(void) {
501 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
504 LoadElementValueSafe(Element);
505 string Transfer = Element->GetValue();
506 if ( Transfer == "1.2.840.10008.1.2.4.51" )
512 * \ingroup gdcmHeader
513 * \brief Determines if the Transfer Syntax was already encountered
514 * and if it corresponds to a JPEGExtendeProcess3-5 one.
516 * @return True when JPEGExtendedProcess3-5 found. False in all other cases.
518 bool gdcmHeader::IsJPEGExtendedProcess3_5TransferSyntax(void) {
519 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
522 LoadElementValueSafe(Element);
523 string Transfer = Element->GetValue();
524 if ( Transfer == "1.2.840.10008.1.2.4.52" )
530 * \ingroup gdcmHeader
531 * \brief Determines if the Transfer Syntax was already encountered
532 * and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
534 * @return True when JPEGSpectralSelectionProcess6-8 found. False in all
537 bool gdcmHeader::IsJPEGSpectralSelectionProcess6_8TransferSyntax(void) {
538 gdcmElValue* Element = PubElValSet.GetElementByNumber(0x0002, 0x0010);
541 LoadElementValueSafe(Element);
542 string Transfer = Element->GetValue();
543 if ( Transfer == "1.2.840.10008.1.2.4.53" )
549 * \ingroup gdcmHeader
550 * \brief Predicate for dicom version 3 file.
551 * @return True when the file is a dicom version 3.
553 bool gdcmHeader::IsDicomV3(void) {
554 if ( (filetype == ExplicitVR)
555 || (filetype == ImplicitVR) )
561 * \ingroup gdcmHeader
562 * \brief When the length of an element value is obviously wrong (because
563 * the parser went Jabberwocky) one can hope improving things by
564 * applying this heuristic.
566 void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) {
567 if ( FoundLength == 0xffffffff)
569 ElVal->SetLength(FoundLength);
573 * \ingroup gdcmHeader
578 guint32 gdcmHeader::FindLengthOB(void) {
579 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
582 long PositionOnEntry = ftell(fp);
583 bool FoundSequenceDelimiter = false;
584 guint32 TotalLength = 0;
587 while ( ! FoundSequenceDelimiter) {
595 TotalLength += 4; // We even have to decount the group and element
597 if ( g != 0xfffe && g!=0xb00c ) /*for bogus header */ {
598 char msg[100]; // for sprintf. Sorry
599 sprintf(msg,"wrong group (%04x) for an item sequence (%04x,%04x)\n",g, g,n);
600 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
605 if ( n == 0xe0dd || ( g==0xb00c && n==0x0eb6 ) ) /* for bogus header */
606 FoundSequenceDelimiter = true;
607 else if ( n != 0xe000 ){
608 char msg[100]; // for sprintf. Sorry
609 sprintf(msg,"wrong element (%04x) for an item sequence (%04x,%04x)\n",n, g,n);
610 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",msg);
614 ItemLength = ReadInt32();
615 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
616 // the ItemLength with ReadInt32
618 SkipBytes(ItemLength);
620 fseek(fp, PositionOnEntry, SEEK_SET);
625 * \ingroup gdcmHeader
630 void gdcmHeader::FindLength (gdcmElValue * ElVal) {
631 guint16 element = ElVal->GetElement();
632 guint16 group = ElVal->GetGroup();
633 string vr = ElVal->GetVR();
635 if( (element == 0x0010) && (group == 0x7fe0) ) {
637 dbg.Verbose(2, "gdcmHeader::FindLength: ",
638 "on est sur 7fe0 0010");
641 if ( (filetype == ExplicitVR) && ! ElVal->IsImplicitVr() ) {
642 if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") ) {
644 // The following reserved two bytes (see PS 3.5-2001, section
645 // 7.1.2 Data element structure with explicit vr p27) must be
646 // skipped before proceeding on reading the length on 4 bytes.
647 fseek(fp, 2L, SEEK_CUR);
649 guint32 length32 = ReadInt32();
650 if ( (vr == "OB") && (length32 == 0xffffffff) ) {
651 ElVal->SetLength(FindLengthOB());
654 FixFoundLength(ElVal, length32);
658 // Length is encoded on 2 bytes.
659 length16 = ReadInt16();
661 // We can tell the current file is encoded in big endian (like
662 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
663 // and it's value is the one of the encoding of a big endian file.
664 // In order to deal with such big endian encoded files, we have
665 // (at least) two strategies:
666 // * when we load the "Transfer Syntax" tag with value of big endian
667 // encoding, we raise the proper flags. Then we wait for the end
668 // of the META group (0x0002) among which is "Transfer Syntax",
669 // before switching the swap code to big endian. We have to postpone
670 // the switching of the swap code since the META group is fully encoded
671 // in little endian, and big endian coding only starts at the next
672 // group. The corresponding code can be hard to analyse and adds
673 // many additional unnecessary tests for regular tags.
674 // * the second strategy consists in waiting for trouble, that shall
675 // appear when we find the first group with big endian encoding. This
676 // is easy to detect since the length of a "Group Length" tag (the
677 // ones with zero as element number) has to be of 4 (0x0004). When we
678 // encouter 1024 (0x0400) chances are the encoding changed and we
679 // found a group with big endian encoding.
680 // We shall use this second strategy. In order to make sure that we
681 // can interpret the presence of an apparently big endian encoded
682 // length of a "Group Length" without committing a big mistake, we
683 // add an additional check: we look in the already parsed elements
684 // for the presence of a "Transfer Syntax" whose value has to be "big
685 // endian encoding". When this is the case, chances are we have got our
686 // hands on a big endian encoded file: we switch the swap code to
687 // big endian and proceed...
688 if ( (element == 0x0000) && (length16 == 0x0400) ) {
689 if ( ! IsExplicitVRBigEndianTransferSyntax() ) {
690 dbg.Verbose(0, "gdcmHeader::FindLength", "not explicit VR");
695 SwitchSwapToBigEndian();
696 // Restore the unproperly loaded values i.e. the group, the element
697 // and the dictionary entry depending on them.
698 guint16 CorrectGroup = SwapShort(ElVal->GetGroup());
699 guint16 CorrectElem = SwapShort(ElVal->GetElement());
700 gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
703 // This correct tag is not in the dictionary. Create a new one.
704 NewTag = new gdcmDictEntry(CorrectGroup, CorrectElem);
706 // FIXME this can create a memory leaks on the old entry that be
707 // left unreferenced.
708 ElVal->SetDictEntry(NewTag);
711 // Heuristic: well some files are really ill-formed.
712 if ( length16 == 0xffff) {
714 dbg.Verbose(0, "gdcmHeader::FindLength",
715 "Erroneous element length fixed.");
717 FixFoundLength(ElVal, (guint32)length16);
721 // Either implicit VR or a non DICOM conformal (see not below) explicit
722 // VR that ommited the VR of (at least) this element. Farts happen.
723 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
724 // on Data elements "Implicit and Explicit VR Data Elements shall
725 // not coexist in a Data Set and Data Sets nested within it".]
726 // Length is on 4 bytes.
727 FixFoundLength(ElVal, ReadInt32());
731 * \ingroup gdcmHeader
732 * \brief Swaps back the bytes of 4-byte long integer accordingly to
735 * @return The suggested integer.
737 guint32 gdcmHeader::SwapLong(guint32 a) {
742 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
743 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
747 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
751 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
754 dbg.Error(" gdcmHeader::SwapLong : unset swap code");
761 * \ingroup gdcmHeader
762 * \brief Swaps the bytes so they agree with the processor order
763 * @return The properly swaped 16 bits integer.
765 guint16 gdcmHeader::SwapShort(guint16 a) {
766 if ( (sw==4321) || (sw==2143) )
767 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
772 * \ingroup gdcmHeader
777 void gdcmHeader::SkipBytes(guint32 NBytes) {
778 //FIXME don't dump the returned value
779 (void)fseek(fp, (long)NBytes, SEEK_CUR);
783 * \ingroup gdcmHeader
788 void gdcmHeader::SkipElementValue(gdcmElValue * ElVal) {
789 SkipBytes(ElVal->GetLength());
793 * \ingroup gdcmHeader
798 void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) {
801 if ((guint32)NewSize >= (guint32)0xffffffff) {
802 MaxSizeLoadElementValue = 0xffffffff;
805 MaxSizeLoadElementValue = NewSize;
809 * \ingroup gdcmHeader
810 * \brief Loads the element content if it's length is not bigger
811 * than the value specified with
812 * gdcmHeader::SetMaxSizeLoadElementValue()
814 void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) {
816 guint16 group = ElVal->GetGroup();
817 string vr = ElVal->GetVR();
818 guint32 length = ElVal->GetLength();
819 bool SkipLoad = false;
821 fseek(fp, (long)ElVal->GetOffset(), SEEK_SET);
823 // FIXME Sequences not treated yet !
825 // Ne faudrait-il pas au contraire trouver immediatement
826 // une maniere 'propre' de traiter les sequences (vr = SQ)
827 // car commencer par les ignorer risque de conduire a qq chose
828 // qui pourrait ne pas etre generalisable
829 // Well, I'm expecting your code !!!
834 // Heuristic : a sequence "contains" a set of tags (called items). It looks
835 // like the last tag of a sequence (the one that terminates the sequence)
836 // has a group of 0xfffe (with a dummy length).
837 if( group == 0xfffe )
842 ElVal->SetValue("gdcm::Skipped");
846 // When the length is zero things are easy:
852 // The elements whose length is bigger than the specified upper bound
853 // are not loaded. Instead we leave a short notice of the offset of
854 // the element content and it's length.
855 if (length > MaxSizeLoadElementValue) {
857 s << "gdcm::NotLoaded.";
858 s << " Address:" << (long)ElVal->GetOffset();
859 s << " Length:" << ElVal->GetLength();
860 ElVal->SetValue(s.str());
864 // When an integer is expected, read and convert the following two or
865 // four bytes properly i.e. as an integer as opposed to a string.
867 // pour les elements de Value Multiplicity > 1
868 // on aura en fait une serie d'entiers
869 // on devrait pouvoir faire + compact (?)
871 if ( IsAnInteger(ElVal) ) {
875 if (vr == "US" || vr == "SS") {
877 NewInt = ReadInt16();
880 for (int i=1; i < nbInt; i++) {
882 NewInt = ReadInt16();
887 } else if (vr == "UL" || vr == "SL") {
889 NewInt = ReadInt32();
892 for (int i=1; i < nbInt; i++) {
894 NewInt = ReadInt32();
899 ElVal->SetValue(s.str());
903 // We need an additional byte for storing \0 that is not on disk
904 char* NewValue = (char*)malloc(length+1);
906 dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
911 item_read = fread(NewValue, (size_t)length, (size_t)1, fp);
912 if ( item_read != 1 ) {
914 dbg.Verbose(1, "gdcmHeader::LoadElementValue","unread element value");
915 ElVal->SetValue("gdcm::UnRead");
918 ElVal->SetValue(NewValue);
923 * \ingroup gdcmHeader
924 * \brief Loads the element while preserving the current
925 * underlying file position indicator as opposed to
926 * to LoadElementValue that modifies it.
927 * @param ElVal Element whose value shall be loaded.
930 void gdcmHeader::LoadElementValueSafe(gdcmElValue * ElVal) {
931 long PositionOnEntry = ftell(fp);
932 LoadElementValue(ElVal);
933 fseek(fp, PositionOnEntry, SEEK_SET);
937 * \ingroup gdcmHeader
942 guint16 gdcmHeader::ReadInt16(void) {
945 item_read = fread (&g, (size_t)2,(size_t)1, fp);
946 if ( item_read != 1 ) {
947 // dbg.Verbose(0, "gdcmHeader::ReadInt16", " Failed to read :");
949 // dbg.Verbose(0, "gdcmHeader::ReadInt16", " End of File encountered");
951 dbg.Verbose(0, "gdcmHeader::ReadInt16", " File Error");
961 * \ingroup gdcmHeader
966 guint32 gdcmHeader::ReadInt32(void) {
969 item_read = fread (&g, (size_t)4,(size_t)1, fp);
970 if ( item_read != 1 ) {
971 //dbg.Verbose(0, "gdcmHeader::ReadInt32", " Failed to read :");
973 // dbg.Verbose(0, "gdcmHeader::ReadInt32", " End of File encountered");
975 dbg.Verbose(0, "gdcmHeader::ReadInt32", " File Error");
985 * \ingroup gdcmHeader
990 gdcmElValue* gdcmHeader::GetElValueByNumber(guint16 Group, guint16 Elem) {
992 gdcmElValue* elValue = PubElValSet.GetElementByNumber(Group, Elem);
994 dbg.Verbose(1, "gdcmHeader::GetElValueByNumber",
995 "failed to Locate gdcmElValue");
996 return (gdcmElValue*)0;
1002 * \ingroup gdcmHeader
1003 * \brief Build a new Element Value from all the low level arguments.
1004 * Check for existence of dictionary entry, and build
1005 * a default one when absent.
1006 * @param Group group of the underlying DictEntry
1007 * @param Elem element of the underlying DictEntry
1009 gdcmElValue* gdcmHeader::NewElValueByNumber(guint16 Group, guint16 Elem) {
1010 // Find out if the tag we encountered is in the dictionaries:
1011 gdcmDictEntry * NewTag = GetDictEntryByNumber(Group, Elem);
1013 NewTag = new gdcmDictEntry(Group, Elem);
1015 gdcmElValue* NewElVal = new gdcmElValue(NewTag);
1017 dbg.Verbose(1, "gdcmHeader::NewElValueByNumber",
1018 "failed to allocate gdcmElValue");
1019 return (gdcmElValue*)0;
1025 * \ingroup gdcmHeader
1031 int gdcmHeader::ReplaceOrCreateByNumber(string Value, guint16 Group, guint16 Elem ) {
1033 // TODO : FIXME JPRx
1035 // on (je) cree une Elvalue ne contenant pas de valeur
1036 // on l'ajoute au ElValSet
1037 // on affecte une valeur a cette ElValue a l'interieur du ElValSet
1038 // --> devrait pouvoir etre fait + simplement ???
1040 gdcmElValue* nvElValue=NewElValueByNumber(Group, Elem);
1041 PubElValSet.Add(nvElValue);
1042 PubElValSet.SetElValueByNumber(Value, Group, Elem);
1048 * \ingroup gdcmHeader
1054 int gdcmHeader::ReplaceOrCreateByNumber(char* Value, guint16 Group, guint16 Elem ) {
1056 gdcmElValue* nvElValue=NewElValueByNumber(Group, Elem);
1057 PubElValSet.Add(nvElValue);
1059 PubElValSet.SetElValueByNumber(v, Group, Elem);
1064 * \ingroup gdcmHeader
1070 int gdcmHeader::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
1071 return (PubElValSet.CheckIfExistByNumber(Group, Elem));
1076 * \ingroup gdcmHeader
1077 * \brief Build a new Element Value from all the low level arguments.
1078 * Check for existence of dictionary entry, and build
1079 * a default one when absent.
1080 * @param Name Name of the underlying DictEntry
1082 gdcmElValue* gdcmHeader::NewElValueByName(string Name) {
1084 gdcmDictEntry * NewTag = GetDictEntryByName(Name);
1086 NewTag = new gdcmDictEntry(0xffff, 0xffff, "LO", "Unknown", Name);
1088 gdcmElValue* NewElVal = new gdcmElValue(NewTag);
1090 dbg.Verbose(1, "gdcmHeader::ObtainElValueByName",
1091 "failed to allocate gdcmElValue");
1092 return (gdcmElValue*)0;
1098 * \ingroup gdcmHeader
1099 * \brief Read the next tag but WITHOUT loading it's value
1100 * @return On succes the newly created ElValue, NULL on failure.
1102 gdcmElValue * gdcmHeader::ReadNextElement(void) {
1105 gdcmElValue * NewElVal;
1110 if ( (g==0x7fe0) && (n==0x0010) )
1112 printf("in gdcmHeader::ReadNextElement try to read 7fe0 0010 \n");
1115 // We reached the EOF (or an error occured) and header parsing
1116 // has to be considered as finished.
1117 return (gdcmElValue *)0;
1119 NewElVal = NewElValueByNumber(g, n);
1121 FindLength(NewElVal);
1124 if (DEBUG) printf("in gdcmHeader::ReadNextElement : g %04x n %04x errno %d\n",g, n, errno);
1125 return (gdcmElValue *)0;
1127 NewElVal->SetOffset(ftell(fp));
1128 if ( (g==0x7fe0) && (n==0x0010) )
1130 printf("sortie de gdcmHeader::ReadNextElement 7fe0 0010 \n");
1135 * \ingroup gdcmHeader
1136 * \brief Apply some heuristics to predict wether the considered
1137 * element value contains/represents an integer or not.
1138 * @param ElVal The element value on which to apply the predicate.
1139 * @return The result of the heuristical predicate.
1141 bool gdcmHeader::IsAnInteger(gdcmElValue * ElVal) {
1142 guint16 group = ElVal->GetGroup();
1143 guint16 element = ElVal->GetElement();
1144 string vr = ElVal->GetVR();
1145 guint32 length = ElVal->GetLength();
1147 // When we have some semantics on the element we just read, and if we
1148 // a priori know we are dealing with an integer, then we shall be
1149 // able to swap it's element value properly.
1150 if ( element == 0 ) { // This is the group length of the group
1154 if (DEBUG) printf("Erroneous Group Length element length (%04x , %04x) : %d\n",
1155 group, element,length);
1157 dbg.Error("gdcmHeader::IsAnInteger",
1158 "Erroneous Group Length element length.");
1161 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
1168 * \ingroup gdcmHeader
1169 * \brief Recover the offset (from the beginning of the file) of the pixels.
1171 size_t gdcmHeader::GetPixelOffset(void) {
1172 // If this file complies with the norm we should encounter the
1173 // "Image Location" tag (0x0028, 0x0200). This tag contains the
1174 // the group that contains the pixel data (hence the "Pixel Data"
1175 // is found by indirection through the "Image Location").
1176 // Inside the group pointed by "Image Location" the searched element
1177 // is conventionally the element 0x0010 (when the norm is respected).
1178 // When the "Image Location" is absent we default to group 0x7fe0.
1181 string ImageLocation = GetPubElValByName("Image Location");
1182 if ( ImageLocation == "gdcm::Unfound" ) {
1185 grPixel = (guint16) atoi( ImageLocation.c_str() );
1187 if (grPixel != 0x7fe0)
1188 // This is a kludge for old dirty Philips imager.
1193 gdcmElValue* PixelElement = PubElValSet.GetElementByNumber(grPixel,
1196 return PixelElement->GetOffset();
1202 * \ingroup gdcmHeader
1203 * \brief Searches both the public and the shadow dictionary (when they
1204 * exist) for the presence of the DictEntry with given
1205 * group and element. The public dictionary has precedence on the
1207 * @param group group of the searched DictEntry
1208 * @param element element of the searched DictEntry
1209 * @return Corresponding DictEntry when it exists, NULL otherwise.
1211 gdcmDictEntry * gdcmHeader::GetDictEntryByNumber(guint16 group,
1213 gdcmDictEntry * found = (gdcmDictEntry*)0;
1214 if (!RefPubDict && !RefShaDict) {
1215 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
1216 "we SHOULD have a default dictionary");
1219 found = RefPubDict->GetTagByNumber(group, element);
1224 found = RefShaDict->GetTagByNumber(group, element);
1232 * \ingroup gdcmHeader
1233 * \brief Searches both the public and the shadow dictionary (when they
1234 * exist) for the presence of the DictEntry with given name.
1235 * The public dictionary has precedence on the shadow one.
1236 * @param Name name of the searched DictEntry
1237 * @return Corresponding DictEntry when it exists, NULL otherwise.
1239 gdcmDictEntry * gdcmHeader::GetDictEntryByName(string Name) {
1240 gdcmDictEntry * found = (gdcmDictEntry*)0;
1241 if (!RefPubDict && !RefShaDict) {
1242 dbg.Verbose(0, "gdcmHeader::GetDictEntry",
1243 "we SHOULD have a default dictionary");
1246 found = RefPubDict->GetTagByName(Name);
1251 found = RefShaDict->GetTagByName(Name);
1259 * \ingroup gdcmHeader
1260 * \brief Searches within the public dictionary for element value of
1262 * @param group Group of the researched tag.
1263 * @param element Element of the researched tag.
1264 * @return Corresponding element value when it exists, and the string
1265 * "gdcm::Unfound" otherwise.
1267 string gdcmHeader::GetPubElValByNumber(guint16 group, guint16 element) {
1268 return PubElValSet.GetElValueByNumber(group, element);
1272 * \ingroup gdcmHeader
1273 * \brief Searches within the public dictionary for element value
1274 * representation of a given tag.
1276 * Obtaining the VR (Value Representation) might be needed by caller
1277 * to convert the string typed content to caller's native type
1278 * (think of C++ vs Python). The VR is actually of a higher level
1279 * of semantics than just the native C++ type.
1280 * @param group Group of the researched tag.
1281 * @param element Element of the researched tag.
1282 * @return Corresponding element value representation when it exists,
1283 * and the string "gdcm::Unfound" otherwise.
1285 string gdcmHeader::GetPubElValRepByNumber(guint16 group, guint16 element) {
1286 gdcmElValue* elem = PubElValSet.GetElementByNumber(group, element);
1288 return "gdcm::Unfound";
1289 return elem->GetVR();
1293 * \ingroup gdcmHeader
1294 * \brief Searches within the public dictionary for element value of
1296 * @param TagName name of the researched element.
1297 * @return Corresponding element value when it exists, and the string
1298 * "gdcm::Unfound" otherwise.
1300 string gdcmHeader::GetPubElValByName(string TagName) {
1301 return PubElValSet.GetElValueByName(TagName);
1305 * \ingroup gdcmHeader
1306 * \brief Searches within the elements parsed with the public dictionary for
1307 * the element value representation of a given tag.
1309 * Obtaining the VR (Value Representation) might be needed by caller
1310 * to convert the string typed content to caller's native type
1311 * (think of C++ vs Python). The VR is actually of a higher level
1312 * of semantics than just the native C++ type.
1313 * @param TagName name of the researched element.
1314 * @return Corresponding element value representation when it exists,
1315 * and the string "gdcm::Unfound" otherwise.
1317 string gdcmHeader::GetPubElValRepByName(string TagName) {
1318 gdcmElValue* elem = PubElValSet.GetElementByName(TagName);
1320 return "gdcm::Unfound";
1321 return elem->GetVR();
1325 * \ingroup gdcmHeader
1326 * \brief Searches within elements parsed with the SHADOW dictionary
1327 * for the element value of a given tag.
1328 * @param group Group of the researched tag.
1329 * @param element Element of the researched tag.
1330 * @return Corresponding element value representation when it exists,
1331 * and the string "gdcm::Unfound" otherwise.
1333 string gdcmHeader::GetShaElValByNumber(guint16 group, guint16 element) {
1334 return ShaElValSet.GetElValueByNumber(group, element);
1338 * \ingroup gdcmHeader
1339 * \brief Searches within the elements parsed with the SHADOW dictionary
1340 * for the element value representation of a given tag.
1342 * Obtaining the VR (Value Representation) might be needed by caller
1343 * to convert the string typed content to caller's native type
1344 * (think of C++ vs Python). The VR is actually of a higher level
1345 * of semantics than just the native C++ type.
1346 * @param group Group of the researched tag.
1347 * @param element Element of the researched tag.
1348 * @return Corresponding element value representation when it exists,
1349 * and the string "gdcm::Unfound" otherwise.
1351 string gdcmHeader::GetShaElValRepByNumber(guint16 group, guint16 element) {
1352 gdcmElValue* elem = ShaElValSet.GetElementByNumber(group, element);
1354 return "gdcm::Unfound";
1355 return elem->GetVR();
1359 * \ingroup gdcmHeader
1360 * \brief Searches within the elements parsed with the shadow dictionary
1361 * for an element value of given tag.
1362 * @param TagName name of the researched element.
1363 * @return Corresponding element value when it exists, and the string
1364 * "gdcm::Unfound" otherwise.
1366 string gdcmHeader::GetShaElValByName(string TagName) {
1367 return ShaElValSet.GetElValueByName(TagName);
1371 * \ingroup gdcmHeader
1372 * \brief Searches within the elements parsed with the shadow dictionary for
1373 * the element value representation of a given tag.
1375 * Obtaining the VR (Value Representation) might be needed by caller
1376 * to convert the string typed content to caller's native type
1377 * (think of C++ vs Python). The VR is actually of a higher level
1378 * of semantics than just the native C++ type.
1379 * @param TagName name of the researched element.
1380 * @return Corresponding element value representation when it exists,
1381 * and the string "gdcm::Unfound" otherwise.
1383 string gdcmHeader::GetShaElValRepByName(string TagName) {
1384 gdcmElValue* elem = ShaElValSet.GetElementByName(TagName);
1386 return "gdcm::Unfound";
1387 return elem->GetVR();
1391 * \ingroup gdcmHeader
1392 * \brief Searches within elements parsed with the public dictionary
1393 * and then within the elements parsed with the shadow dictionary
1394 * for the element value of a given tag.
1395 * @param group Group of the researched tag.
1396 * @param element Element of the researched tag.
1397 * @return Corresponding element value representation when it exists,
1398 * and the string "gdcm::Unfound" otherwise.
1400 string gdcmHeader::GetElValByNumber(guint16 group, guint16 element) {
1401 string pub = GetPubElValByNumber(group, element);
1404 return GetShaElValByNumber(group, element);
1408 * \ingroup gdcmHeader
1409 * \brief Searches within elements parsed with the public dictionary
1410 * and then within the elements parsed with the shadow dictionary
1411 * for the element value representation of a given tag.
1413 * Obtaining the VR (Value Representation) might be needed by caller
1414 * to convert the string typed content to caller's native type
1415 * (think of C++ vs Python). The VR is actually of a higher level
1416 * of semantics than just the native C++ type.
1417 * @param group Group of the researched tag.
1418 * @param element Element of the researched tag.
1419 * @return Corresponding element value representation when it exists,
1420 * and the string "gdcm::Unfound" otherwise.
1422 string gdcmHeader::GetElValRepByNumber(guint16 group, guint16 element) {
1423 string pub = GetPubElValRepByNumber(group, element);
1426 return GetShaElValRepByNumber(group, element);
1430 * \ingroup gdcmHeader
1431 * \brief Searches within elements parsed with the public dictionary
1432 * and then within the elements parsed with the shadow dictionary
1433 * for the element value of a given tag.
1434 * @param TagName name of the researched element.
1435 * @return Corresponding element value when it exists,
1436 * and the string "gdcm::Unfound" otherwise.
1438 string gdcmHeader::GetElValByName(string TagName) {
1439 string pub = GetPubElValByName(TagName);
1442 return GetShaElValByName(TagName);
1446 * \ingroup gdcmHeader
1447 * \brief Searches within elements parsed with the public dictionary
1448 * and then within the elements parsed with the shadow dictionary
1449 * for the element value representation of a given tag.
1451 * Obtaining the VR (Value Representation) might be needed by caller
1452 * to convert the string typed content to caller's native type
1453 * (think of C++ vs Python). The VR is actually of a higher level
1454 * of semantics than just the native C++ type.
1455 * @param TagName name of the researched element.
1456 * @return Corresponding element value representation when it exists,
1457 * and the string "gdcm::Unfound" otherwise.
1459 string gdcmHeader::GetElValRepByName(string TagName) {
1460 string pub = GetPubElValRepByName(TagName);
1463 return GetShaElValRepByName(TagName);
1467 * \ingroup gdcmHeader
1468 * \brief Accesses an existing gdcmElValue in the PubElValSet of this instance
1469 * through it's (group, element) and modifies it's content with
1471 * @param content new value to substitute with
1472 * @param group group of the ElVal to modify
1473 * @param element element of the ElVal to modify
1475 int gdcmHeader::SetPubElValByNumber(string content, guint16 group,
1478 //TODO : homogeneiser les noms : SetPubElValByNumber qui appelle PubElValSet.SetElValueByNumber
1479 // pourquoi pas SetPubElValueByNumber ??
1482 return ( PubElValSet.SetElValueByNumber (content, group, element) );
1486 * \ingroup gdcmHeader
1487 * \brief Accesses an existing gdcmElValue in the PubElValSet of this instance
1488 * through tag name and modifies it's content with the given value.
1489 * @param content new value to substitute with
1490 * @param TagName name of the tag to be modified
1492 int gdcmHeader::SetPubElValByName(string content, string TagName) {
1493 return ( PubElValSet.SetElValueByName (content, TagName) );
1497 * \ingroup gdcmHeader
1498 * \brief Accesses an existing gdcmElValue in the PubElValSet of this instance
1499 * through it's (group, element) and modifies it's length with
1501 * \warning Use with extreme caution.
1502 * @param length new length to substitute with
1503 * @param group group of the ElVal to modify
1504 * @param element element of the ElVal to modify
1505 * @return 1 on success, 0 otherwise.
1508 int gdcmHeader::SetPubElValLengthByNumber(guint32 length, guint16 group,
1510 return ( PubElValSet.SetElValueLengthByNumber (length, group, element) );
1514 * \ingroup gdcmHeader
1515 * \brief Accesses an existing gdcmElValue in the ShaElValSet of this instance
1516 * through it's (group, element) and modifies it's content with
1518 * @param content new value to substitute with
1519 * @param group group of the ElVal to modify
1520 * @param element element of the ElVal to modify
1521 * @return 1 on success, 0 otherwise.
1523 int gdcmHeader::SetShaElValByNumber(string content,
1524 guint16 group, guint16 element) {
1525 return ( ShaElValSet.SetElValueByNumber (content, group, element) );
1529 * \ingroup gdcmHeader
1530 * \brief Accesses an existing gdcmElValue in the ShaElValSet of this instance
1531 * through tag name and modifies it's content with the given value.
1532 * @param content new value to substitute with
1533 * @param ShadowTagName name of the tag to be modified
1535 int gdcmHeader::SetShaElValByName(string content, string ShadowTagName) {
1536 return ( ShaElValSet.SetElValueByName (content, ShadowTagName) );
1540 * \ingroup gdcmHeader
1541 * \brief Parses the header of the file but WITHOUT loading element values.
1543 void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
1544 gdcmElValue * newElValue = (gdcmElValue *)0;
1548 while ( (newElValue = ReadNextElement()) ) {
1549 SkipElementValue(newElValue);
1550 PubElValSet.Add(newElValue);
1555 * \ingroup gdcmHeader
1556 * \brief This predicate, based on hopefully reasonnable heuristics,
1557 * decides whether or not the current gdcmHeader was properly parsed
1558 * and contains the mandatory information for being considered as
1559 * a well formed and usable image.
1560 * @return true when gdcmHeader is the one of a reasonable Dicom file,
1563 bool gdcmHeader::IsReadable(void) {
1564 if ( GetElValByName("Image Dimensions") != "gdcm::Unfound"
1565 && atoi(GetElValByName("Image Dimensions").c_str()) > 4 ) {
1568 if ( GetElValByName("Bits Allocated") == "gdcm::Unfound" )
1570 if ( GetElValByName("Bits Stored") == "gdcm::Unfound" )
1572 if ( GetElValByName("High Bit") == "gdcm::Unfound" )
1574 if ( GetElValByName("Pixel Representation") == "gdcm::Unfound" )
1580 * \ingroup gdcmHeader
1581 * \brief Small utility function that creates a new manually crafted
1582 * (as opposed as read from the file) gdcmElValue with user
1583 * specified name and adds it to the public tag hash table.
1584 * \note A fake TagKey is generated so the PubDict can keep it's coherence.
1585 * @param NewTagName The name to be given to this new tag.
1586 * @param VR The Value Representation to be given to this new tag.
1587 * @ return The newly hand crafted Element Value.
1589 gdcmElValue* gdcmHeader::NewManualElValToPubDict(string NewTagName, string VR) {
1590 gdcmElValue* NewElVal = (gdcmElValue*)0;
1591 guint32 StuffGroup = 0xffff; // Group to be stuffed with additional info
1592 guint32 FreeElem = 0;
1593 gdcmDictEntry* NewEntry = (gdcmDictEntry*)0;
1595 FreeElem = PubElValSet.GenerateFreeTagKeyInGroup(StuffGroup);
1596 if (FreeElem == UINT32_MAX) {
1597 dbg.Verbose(1, "gdcmHeader::NewManualElValToPubDict",
1598 "Group 0xffff in Public Dict is full");
1599 return (gdcmElValue*)0;
1601 NewEntry = new gdcmDictEntry(StuffGroup, FreeElem,
1602 VR, "GDCM", NewTagName);
1603 NewElVal = new gdcmElValue(NewEntry);
1604 PubElValSet.Add(NewElVal);
1609 * \ingroup gdcmHeader
1610 * \brief Loads the element values of all the elements present in the
1611 * public tag based hash table.
1613 void gdcmHeader::LoadElements(void) {
1615 TagElValueHT ht = PubElValSet.GetTagHt();
1616 for (TagElValueHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) {
1617 LoadElementValue(tag->second);
1622 * \ingroup gdcmHeader
1626 void gdcmHeader::PrintPubElVal(std::ostream & os) {
1627 PubElValSet.Print(os);
1631 * \ingroup gdcmHeader
1635 void gdcmHeader::PrintPubDict(std::ostream & os) {
1636 RefPubDict->Print(os);
1640 * \ingroup gdcmHeader
1644 int gdcmHeader::Write(FILE * fp, FileType type) {
1645 return PubElValSet.Write(fp, type);
1649 // =============================================================================
1650 // Accesors with euristics
1651 //==============================================================================
1654 // TODO : move to an other file.
1658 * \ingroup gdcmHeader
1659 * \brief Retrieve the number of columns of image.
1660 * @return The encountered size when found, 0 by default.
1662 int gdcmHeader::GetXSize(void) {
1663 // We cannot check for "Columns" because the "Columns" tag is present
1664 // both in IMG (0028,0011) and OLY (6000,0011) sections of the dictionary.
1665 string StrSize = GetPubElValByNumber(0x0028,0x0011);
1666 if (StrSize == "gdcm::Unfound")
1668 return atoi(StrSize.c_str());
1672 * \ingroup gdcmHeader
1673 * \brief Retrieve the number of lines of image.
1674 * \warning The defaulted value is 1 as opposed to gdcmHeader::GetXSize()
1675 * @return The encountered size when found, 1 by default.
1677 int gdcmHeader::GetYSize(void) {
1678 // We cannot check for "Rows" because the "Rows" tag is present
1679 // both in IMG (0028,0010) and OLY (6000,0010) sections of the dictionary.
1680 string StrSize = GetPubElValByNumber(0x0028,0x0010);
1681 if (StrSize != "gdcm::Unfound")
1682 return atoi(StrSize.c_str());
1686 // The Rows (0028,0010) entry is optional for ACR/NEMA. It might
1687 // hence be a signal (1d image). So we default to 1:
1692 * \ingroup gdcmHeader
1693 * \brief Retrieve the number of planes of volume or the number
1694 * of frames of a multiframe.
1695 * \warning When present we consider the "Number of Frames" as the third
1696 * dimension. When absent we consider the third dimension as
1697 * being the "Planes" tag content.
1698 * @return The encountered size when found, 1 by default.
1700 int gdcmHeader::GetZSize(void) {
1701 // Both in DicomV3 and ACR/Nema the consider the "Number of Frames"
1702 // as the third dimension.
1703 string StrSize = GetPubElValByNumber(0x0028,0x0008);
1704 if (StrSize != "gdcm::Unfound")
1705 return atoi(StrSize.c_str());
1707 // We then consider the "Planes" entry as the third dimension [we
1708 // cannot retrieve by name since "Planes tag is present both in
1709 // IMG (0028,0012) and OLY (6000,0012) sections of the dictionary].
1710 StrSize = GetPubElValByNumber(0x0028,0x0012);
1711 if (StrSize != "gdcm::Unfound")
1712 return atoi(StrSize.c_str());
1717 * \ingroup gdcmHeader
1718 * \brief Retrieve the number of Bits Stored
1719 * (as opposite to number of Bits Allocated)
1721 * @return The encountered number of Bits Stored, 0 by default.
1723 int gdcmHeader::GetBitsStored(void) {
1724 string StrSize = GetPubElValByNumber(0x0028,0x0101);
1725 if (StrSize == "gdcm::Unfound")
1727 return atoi(StrSize.c_str());
1732 * \ingroup gdcmHeader
1733 * \brief Retrieve the number of Samples Per Pixel
1734 * (1 : gray level, 3 : RGB)
1736 * @return The encountered number of Samples Per Pixel, 1 by default.
1738 int gdcmHeader::GetSamplesPerPixel(void) {
1739 string StrSize = GetPubElValByNumber(0x0028,0x0002);
1740 if (StrSize == "gdcm::Unfound")
1741 return 1; // Well, it's supposed to be mandatory ...
1742 return atoi(StrSize.c_str());
1746 /* ================ COMMENT OUT after unfreeze
1748 * \ingroup gdcmHeader
1749 * \brief Retrieve the Planar Configuration for RGB images
1750 * (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
1752 * @return The encountered Planar Configuration, 0 by default.
1754 int gdcmHeader::GetPlanarConfiguration(void) {
1755 string StrSize = GetPubElValByNumber(0x0028,0x0006);
1756 if (StrSize == "gdcm::Unfound")
1758 return atoi(StrSize.c_str());
1761 ======================================= */
1764 * \ingroup gdcmHeader
1765 * \brief Return the size (in bytes) of a single pixel of data.
1766 * @return The size in bytes of a single pixel of data.
1769 int gdcmHeader::GetPixelSize(void) {
1770 string PixelType = GetPixelType();
1771 if (PixelType == "8U" || PixelType == "8S")
1773 if (PixelType == "16U" || PixelType == "16S")
1775 if (PixelType == "32U" || PixelType == "32S")
1777 dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type");
1782 * \ingroup gdcmHeader
1783 * \brief Build the Pixel Type of the image.
1784 * Possible values are:
1785 * - 8U unsigned 8 bit,
1786 * - 8S signed 8 bit,
1787 * - 16U unsigned 16 bit,
1788 * - 16S signed 16 bit,
1789 * - 32U unsigned 32 bit,
1790 * - 32S signed 32 bit,
1791 * \warning 12 bit images appear as 16 bit.
1794 string gdcmHeader::GetPixelType(void) {
1796 BitsAlloc = GetElValByName("Bits Allocated");
1797 if (BitsAlloc == "gdcm::Unfound") {
1798 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Bits Allocated");
1799 BitsAlloc = string("16");
1801 if (BitsAlloc == "12")
1802 BitsAlloc = string("16");
1805 Signed = GetElValByName("Pixel Representation");
1806 if (Signed == "gdcm::Unfound") {
1807 dbg.Verbose(0, "gdcmHeader::GetPixelType: unfound Pixel Representation");
1808 BitsAlloc = string("0");
1811 Signed = string("U");
1813 Signed = string("S");
1815 return( BitsAlloc + Signed);
1819 * \ingroup gdcmHeader
1820 * \brief gets the info from 0028,0030 : Pixel Spacing
1822 * @return X dimension of a pixel
1824 float gdcmHeader::GetXSpacing(void) {
1825 float xspacing, yspacing;
1826 string StrSpacing = GetPubElValByNumber(0x0028,0x0030);
1828 if (StrSpacing == "gdcm::Unfound") {
1829 dbg.Verbose(0, "gdcmHeader::GetXSpacing: unfound Pixel Spacing (0028,0030)");
1832 if( sscanf( StrSpacing.c_str(), "%f\\%f", &xspacing, &yspacing) != 2)
1839 * \ingroup gdcmHeader
1840 * \brief gets the info from 0028,0030 : Pixel Spacing
1842 * @return Y dimension of a pixel
1844 float gdcmHeader::GetYSpacing(void) {
1845 float xspacing, yspacing;
1846 string StrSpacing = GetPubElValByNumber(0x0028,0x0030);
1848 if (StrSpacing == "gdcm::Unfound") {
1849 dbg.Verbose(0, "gdcmHeader::GetYSpacing: unfound Pixel Spacing (0028,0030)");
1852 if( sscanf( StrSpacing.c_str(), "%f\\%f", &xspacing, &yspacing) != 2)
1854 if (yspacing == 0.) {
1855 dbg.Verbose(0, "gdcmHeader::GetYSpacing: gdcmData/CT-MONO2-8-abdo.dcm problem");
1856 // seems to be a bug in the header ...
1857 sscanf( StrSpacing.c_str(), "%f\\0\\%f", &xspacing, &yspacing);
1864 *\ingroup gdcmHeader
1865 *\brief gets the info from 0018,0088 : Space Between Slices
1866 *\ else from 0018,0050 : Slice Thickness
1868 * @return Z dimension of a voxel-to be
1870 float gdcmHeader::GetZSpacing(void) {
1871 // TODO : translate into English
1872 // Spacing Between Slices : distance entre le milieu de chaque coupe
1873 // Les coupes peuvent etre :
1874 // jointives (Spacing between Slices = Slice Thickness)
1875 // chevauchantes (Spacing between Slices < Slice Thickness)
1876 // disjointes (Spacing between Slices > Slice Thickness)
1877 // Slice Thickness : epaisseur de tissus sur laquelle est acquis le signal
1878 // ca interesse le physicien de l'IRM, pas le visualisateur de volumes ...
1879 // Si le Spacing Between Slices est absent,
1880 // on suppose que les coupes sont jointives
1882 string StrSpacingBSlices = GetPubElValByNumber(0x0018,0x0088);
1884 if (StrSpacingBSlices == "gdcm::Unfound") {
1885 dbg.Verbose(0, "gdcmHeader::GetZSpacing: unfound StrSpacingBSlices");
1886 string StrSliceThickness = GetPubElValByNumber(0x0018,0x0050);
1887 if (StrSliceThickness == "gdcm::Unfound")
1890 // if no 'Spacing Between Slices' is found,
1891 // we assume slices join together
1892 // (no overlapping, no interslice gap)
1893 // if they don't, we're fucked up
1894 return atof(StrSliceThickness.c_str());
1896 return atof(StrSpacingBSlices.c_str());
1901 // Image Position Patient (0020,0032):
1902 // If not found (ACR_NEMA) we try Image Position (0020,0030)
1903 // If not found (ACR-NEMA), we consider Slice Location (0020,1041)
1904 // or Location (0020,0050)
1905 // as the Z coordinate,
1906 // 0. for all the coordinates if nothing is found
1908 // TODO : find a way to inform the caller nothing was found
1909 // TODO : How to tell the caller a wrong number of values was found?
1912 * \ingroup gdcmHeader
1913 * \brief gets the info from 0020,0032 : Image Position Patient
1914 *\ else from 0020,0030 : Image Position (RET)
1916 * @return up-left image corner position
1918 float gdcmHeader::GetXImagePosition(void) {
1919 float xImPos, yImPos, zImPos;
1920 string StrImPos = GetPubElValByNumber(0x0020,0x0032);
1922 if (StrImPos == "gdcm::Unfound") {
1923 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position Patient (0020,0032)");
1924 StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
1925 if (StrImPos == "gdcm::Unfound") {
1926 dbg.Verbose(0, "gdcmHeader::GetXImagePosition: unfound Image Position (RET) (0020,0030)");
1927 // How to tell the caller nothing was found ?
1931 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
1937 * \ingroup gdcmHeader
1938 * \brief gets the info from 0020,0032 : Image Position Patient
1939 * \ else from 0020,0030 : Image Position (RET)
1941 * @return up-left image corner position
1943 float gdcmHeader::GetYImagePosition(void) {
1944 float xImPos, yImPos, zImPos;
1945 string StrImPos = GetPubElValByNumber(0x0020,0x0032);
1947 if (StrImPos == "gdcm::Unfound") {
1948 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position Patient (0020,0032)");
1949 StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
1950 if (StrImPos == "gdcm::Unfound") {
1951 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Image Position (RET) (0020,0030)");
1955 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3)
1961 * \ingroup gdcmHeader
1962 * \brief gets the info from 0020,0032 : Image Position Patient
1963 * \ else from 0020,0030 : Image Position (RET)
1964 * \ else from 0020,1041 : Slice Location
1965 * \ else from 0020,0050 : Location
1967 * @return up-left image corner position
1969 float gdcmHeader::GetZImagePosition(void) {
1970 float xImPos, yImPos, zImPos;
1971 string StrImPos = GetPubElValByNumber(0x0020,0x0032);
1972 if (StrImPos != "gdcm::Unfound") {
1973 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
1974 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position Patient (0020,0032)");
1975 return 0.; // bug in the element 0x0020,0x0032
1980 StrImPos = GetPubElValByNumber(0x0020,0x0030); // For ACR-NEMA images
1981 if (StrImPos != "gdcm::Unfound") {
1982 if( sscanf( StrImPos.c_str(), "%f\\%f\\%f", &xImPos, &yImPos, &zImPos) != 3) {
1983 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Image Position (RET) (0020,0030)");
1984 return 0.; // bug in the element 0x0020,0x0032
1989 string StrSliceLocation = GetPubElValByNumber(0x0020,0x1041);// for *very* old ACR-NEMA images
1990 if (StrSliceLocation != "gdcm::Unfound") {
1991 if( sscanf( StrSliceLocation.c_str(), "%f", &zImPos) !=1) {
1992 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Slice Location (0020,1041)");
1993 return 0.; // bug in the element 0x0020,0x1041
1998 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: unfound Slice Location (0020,1041)");
1999 string StrLocation = GetPubElValByNumber(0x0020,0x0050);
2000 if (StrLocation != "gdcm::Unfound") {
2001 if( sscanf( StrLocation.c_str(), "%f", &zImPos) !=1) {
2002 dbg.Verbose(0, "gdcmHeader::GetZImagePosition: wrong Location (0020,0050)");
2003 return 0.; // bug in the element 0x0020,0x0050
2008 dbg.Verbose(0, "gdcmHeader::GetYImagePosition: unfound Location (0020,0050)");
2009 return 0.; // Hopeless