9 #include <netinet/in.h>
11 #include <cctype> // for isalpha
16 #define HEADER_LENGTH_TO_READ 256 // on ne lit plus que le debut
21 struct FileReadError {
22 FileReadError(FILE* fp, const char* Mesg) {
24 dbg.Verbose(1, "EOF encountered :", Mesg);
26 dbg.Verbose(1, "Error on reading :", Mesg);
31 //FIXME: this looks dirty to me...
32 #define str2num(str, typeNum) *((typeNum *)(str))
34 VRHT * gdcmHeader::dicom_vr = (VRHT*)0;
35 gdcmDictSet* gdcmHeader::Dicts = new gdcmDictSet();
37 void gdcmHeader::Initialise(void) {
38 if (!gdcmHeader::dicom_vr)
40 RefPubDict = gdcmHeader::Dicts->GetDefaultPublicDict();
41 RefShaDict = (gdcmDict*)0;
44 gdcmHeader::gdcmHeader (const char* InFilename) {
45 SetMaxSizeLoadElementValue(1024);
46 filename = InFilename;
48 fp=fopen(InFilename,"rw");
49 dbg.Error(!fp, "gdcmHeader::gdcmHeader cannot open file", InFilename);
53 gdcmHeader::~gdcmHeader (void) {
58 void gdcmHeader::InitVRDict (void) {
60 dbg.Verbose(0, "gdcmHeader::InitVRDict:", "VR dictionary allready set");
64 (*vr)["AE"] = "Application Entity"; // At most 16 bytes
65 (*vr)["AS"] = "Age String"; // Exactly 4 bytes
66 (*vr)["AT"] = "Attribute Tag"; // 2 16-bit unsigned short integers
67 (*vr)["CS"] = "Code String"; // At most 16 bytes
68 (*vr)["DA"] = "Date"; // Exactly 8 bytes
69 (*vr)["DS"] = "Decimal String"; // At most 16 bytes
70 (*vr)["DT"] = "Date Time"; // At most 26 bytes
71 (*vr)["FL"] = "Floating Point Single"; // 32-bit IEEE 754:1985 float
72 (*vr)["FD"] = "Floating Point Double"; // 64-bit IEEE 754:1985 double
73 (*vr)["IS"] = "Integer String"; // At most 12 bytes
74 (*vr)["LO"] = "Long String"; // At most 64 chars
75 (*vr)["LT"] = "Long Text"; // At most 10240 chars
76 (*vr)["OB"] = "Other Byte String"; // String of bytes (vr independant)
77 (*vr)["OW"] = "Other Word String"; // String of 16-bit words (vr dep)
78 (*vr)["PN"] = "Person Name"; // At most 64 chars
79 (*vr)["SH"] = "Short String"; // At most 16 chars
80 (*vr)["SL"] = "Signed Long"; // Exactly 4 bytes
81 (*vr)["SQ"] = "Sequence of Items"; // Not Applicable
82 (*vr)["SS"] = "Signed Short"; // Exactly 2 bytes
83 (*vr)["ST"] = "Short Text"; // At most 1024 chars
84 (*vr)["TM"] = "Time"; // At most 16 bytes
85 (*vr)["UI"] = "Unique Identifier"; // At most 64 bytes
86 (*vr)["UL"] = "Unsigned Long "; // Exactly 4 bytes
87 (*vr)["UN"] = "Unknown"; // Any length of bytes
88 (*vr)["US"] = "Unsigned Short "; // Exactly 2 bytes
89 (*vr)["UT"] = "Unlimited Text"; // At most 2^32 -1 chars
95 * \brief Discover what the swap code is (among little endian, big endian,
96 * bad little endian, bad big endian).
99 void gdcmHeader::CheckSwap()
101 // The only guaranted way of finding the swap code is to find a
102 // group tag since we know it's length has to be of four bytes i.e.
103 // 0x00000004. Finding the swap code in then straigthforward. Trouble
104 // occurs when we can't find such group...
106 guint32 x=4; // x : pour ntohs
107 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
111 char deb[HEADER_LENGTH_TO_READ];
113 // First, compare HostByteOrder and NetworkByteOrder in order to
114 // determine if we shall need to swap bytes (i.e. the Endian type).
120 // The easiest case is the one of a DICOM header, since it possesses a
121 // file preamble where it suffice to look for the sting "DICM".
122 lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
125 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
126 filetype = TrueDicom;
127 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "looks like DICOM Version3");
130 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "not a DICOM Version3 file");
133 if(filetype == TrueDicom) {
134 // Next, determine the value representation (VR). Let's skip to the
135 // first element (0002, 0000) and check there if we find "UL", in
136 // which case we (almost) know it is explicit VR.
137 // WARNING: if it happens to be implicit VR then what we will read
138 // is the length of the group. If this ascii representation of this
139 // length happens to be "UL" then we shall believe it is explicit VR.
140 // FIXME: in order to fix the above warning, we could read the next
141 // element value (or a couple of elements values) in order to make
142 // sure we are not commiting a big mistake.
144 // * the 128 bytes of File Preamble (often padded with zeroes),
145 // * the 4 bytes of "DICM" string,
146 // * the 4 bytes of the first tag (0002, 0000),
147 // i.e. a total of 136 bytes.
149 if(memcmp(entCur, "UL", (size_t)2) == 0) {
150 filetype = ExplicitVR;
151 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
152 "explicit Value Representation");
154 filetype = ImplicitVR;
155 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
156 "not an explicit Value Representation");
161 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
162 "HostByteOrder != NetworkByteOrder");
165 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
166 "HostByteOrder = NetworkByteOrder");
169 // Position the file position indicator at first tag (i.e.
170 // after the file preamble and the "DICM" string).
172 fseek (fp, 132L, SEEK_SET);
174 } // End of TrueDicom
176 // Alas, this is not a DicomV3 file and whatever happens there is no file
177 // preamble. We can reset the file position indicator to where the data
178 // is (i.e. the beginning of the file).
181 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
182 // By clean we mean that the length of the first tag is written down.
183 // If this is the case and since the length of the first group HAS to be
184 // four (bytes), then determining the proper swap code is straightforward.
187 s = str2num(entCur, guint32);
207 dbg.Verbose(0, "gdcmHeader::CheckSwap:",
208 "ACR/NEMA unfound swap info (time to raise bets)");
211 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
212 // It is time for despaired wild guesses. So, let's assume this file
213 // happens to be 'dirty' ACR/NEMA, i.e. the length of the group is
214 // not present. Then the only info we have is the net2host one.
222 void gdcmHeader::SwitchSwapToBigEndian(void) {
223 dbg.Verbose(1, "gdcmHeader::SwitchSwapToBigEndian",
224 "Switching to BigEndian mode.");
241 void gdcmHeader::GetPixels(size_t lgrTotale, void* Pixels) {
243 pixelsOffset = GetPixelOffset();
244 fseek(fp, pixelsOffset, SEEK_SET);
245 fread(Pixels, 1, lgrTotale, fp);
251 * \ingroup gdcmHeader
252 * \brief Find the value representation of the current tag.
254 * @param sw code swap
255 * @param skippedLength pointeur sur nombre d'octets que l'on a saute qd
256 * la lecture est finie
257 * @param longueurLue pointeur sur longueur (en nombre d'octets)
259 * @return longueur retenue pour le champ
264 // --> C'etait la description de quoi, ca?
267 void gdcmHeader::FindVR( ElValue *ElVal) {
268 if (filetype != ExplicitVR)
274 long PositionOnEntry = ftell(fp);
275 // Warning: we believe this is explicit VR (Value Representation) because
276 // we used a heuristic that found "UL" in the first tag. Alas this
277 // doesn't guarantee that all the tags will be in explicit VR. In some
278 // cases (see e-film filtered files) one finds implicit VR tags mixed
279 // within an explicit VR file. Hence we make sure the present tag
280 // is in explicit VR and try to fix things if it happens not to be
282 bool RealExplicit = true;
284 lgrLue=fread (&VR, (size_t)2,(size_t)1, fp);
288 // Assume we are reading a falsely explicit VR file i.e. we reached
289 // a tag where we expect reading a VR but are in fact we read the
290 // first to bytes of the length. Then we will interogate (through find)
291 // the dicom_vr dictionary with oddities like "\004\0" which crashes
292 // both GCC and VC++ implementations of the STL map. Hence when the
293 // expected VR read happens to be non-ascii characters we consider
294 // we hit falsely explicit VR tag.
296 if ( (!isalpha(VR[0])) && (!isalpha(VR[1])) )
297 RealExplicit = false;
299 // CLEANME searching the dicom_vr at each occurence is expensive.
300 // PostPone this test in an optional integrity check at the end
301 // of parsing or only in debug mode.
302 if ( RealExplicit && !dicom_vr->count(vr) )
303 RealExplicit = false;
305 if ( RealExplicit ) {
306 if ( ElVal->IsVrUnknown() ) {
307 // When not a dictionary entry, we can safely overwrite the vr.
311 if ( ElVal->GetVR() == vr ) {
312 // The vr we just read and the dictionary agree. Nothing to do.
315 // The vr present in the file and the dictionary disagree. We assume
316 // the file writer knew best and use the vr of the file. Since it would
317 // be unwise to overwrite the vr of a dictionary (since it would
318 // compromise it's next user), we need to clone the actual DictEntry
319 // and change the vr for the read one.
320 gdcmDictEntry* NewTag = new gdcmDictEntry(ElVal->GetGroup(),
325 ElVal->SetDictEntry(NewTag);
329 // We thought this was explicit VR, but we end up with an
330 // implicit VR tag. Let's backtrack.
331 dbg.Verbose(1, "gdcmHeader::FindVR:", "Falsely explicit vr file");
332 fseek(fp, PositionOnEntry, SEEK_SET);
333 // When this element is known in the dictionary we shall use, e.g. for
334 // the semantics (see the usage of IsAnInteger), the vr proposed by the
335 // dictionary entry. Still we have to flag the element as implicit since
336 // we know now our assumption on expliciteness is not furfilled.
338 if ( ElVal->IsVrUnknown() )
339 ElVal->SetVR("Implicit");
340 ElVal->SetImplicitVr();
344 * \ingroup gdcmHeader
345 * \brief Determines if the Transfer Syntax was allready encountered
346 * and if it corresponds to a ImplicitVRLittleEndian one.
348 * @return True when ImplicitVRLittleEndian found. False in all other cases.
350 bool gdcmHeader::IsImplicitVRLittleEndianTransferSyntax(void) {
351 ElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
354 LoadElementValueSafe(Element);
355 string Transfer = Element->GetValue();
356 if ( Transfer == "1.2.840.10008.1.2" )
362 * \ingroup gdcmHeader
363 * \brief Determines if the Transfer Syntax was allready encountered
364 * and if it corresponds to a ExplicitVRLittleEndian one.
366 * @return True when ExplicitVRLittleEndian found. False in all other cases.
368 bool gdcmHeader::IsExplicitVRLittleEndianTransferSyntax(void) {
369 ElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
372 LoadElementValueSafe(Element);
373 string Transfer = Element->GetValue();
374 if ( Transfer == "1.2.840.10008.1.2.1" )
380 * \ingroup gdcmHeader
381 * \brief Determines if the Transfer Syntax was allready encountered
382 * and if it corresponds to a DeflatedExplicitVRLittleEndian one.
384 * @return True when DeflatedExplicitVRLittleEndian found. False in all other cases.
386 bool gdcmHeader::IsDeflatedExplicitVRLittleEndianTransferSyntax(void) {
387 ElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
390 LoadElementValueSafe(Element);
391 string Transfer = Element->GetValue();
392 if ( Transfer == "1.2.840.10008.1.2.1.99" )
399 * \ingroup gdcmHeader
400 * \brief Determines if the Transfer Syntax was allready encountered
401 * and if it corresponds to a Explicit VR Big Endian one.
403 * @return True when big endian found. False in all other cases.
405 bool gdcmHeader::IsExplicitVRBigEndianTransferSyntax(void) {
406 ElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
409 LoadElementValueSafe(Element);
410 string Transfer = Element->GetValue();
411 if ( Transfer == "1.2.840.10008.1.2.2" )
418 * \ingroup gdcmHeader
419 * \brief Determines if the Transfer Syntax was allready encountered
420 * and if it corresponds to a JPEGBaseLineProcess1 one.
422 * @return True when JPEGBaseLineProcess1found. False in all other cases.
424 bool gdcmHeader::IsJPEGBaseLineProcess1TransferSyntax(void) {
425 ElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
428 LoadElementValueSafe(Element);
429 string Transfer = Element->GetValue();
430 if ( Transfer == "1.2.840.10008.1.2.4.50" )
436 * \ingroup gdcmHeader
437 * \brief Determines if the Transfer Syntax was allready encountered
438 * and if it corresponds to a JPEGExtendedProcess2-4 one.
440 * @return True when JPEGExtendedProcess2-4 found. False in all other cases.
442 bool gdcmHeader::IsJPEGExtendedProcess2_4TransferSyntax(void) {
443 ElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
446 LoadElementValueSafe(Element);
447 string Transfer = Element->GetValue();
448 if ( Transfer == "1.2.840.10008.1.2.4.51" )
455 * \ingroup gdcmHeader
456 * \brief Determines if the Transfer Syntax was allready encountered
457 * and if it corresponds to a JPEGExtendeProcess3-5 one.
459 * @return True when JPEGExtendedProcess3-5 found. False in all other cases.
461 bool gdcmHeader::IsJPEGExtendedProcess3_5TransferSyntax(void) {
462 ElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
465 LoadElementValueSafe(Element);
466 string Transfer = Element->GetValue();
467 if ( Transfer == "1.2.840.10008.1.2.4.52" )
473 * \ingroup gdcmHeader
474 * \brief Determines if the Transfer Syntax was allready encountered
475 * and if it corresponds to a JPEGSpectralSelectionProcess6-8 one.
477 * @return True when JPEGSpectralSelectionProcess6-8 found. False in all other cases.
479 bool gdcmHeader::IsJPEGSpectralSelectionProcess6_8TransferSyntax(void) {
480 ElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
483 LoadElementValueSafe(Element);
484 string Transfer = Element->GetValue();
485 if ( Transfer == "1.2.840.10008.1.2.4.53" )
492 // Il y en a encore DIX-SEPT, comme ça.
493 // Il faudrait trouver qq chose + rusé ...
497 void gdcmHeader::FixFoundLength(ElValue * ElVal, guint32 FoundLength) {
498 // Heuristic: a final fix.
499 if ( FoundLength == 0xffffffff)
501 ElVal->SetLength(FoundLength);
504 guint32 gdcmHeader::FindLengthOB(void) {
505 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
508 long PositionOnEntry = ftell(fp);
509 bool FoundSequenceDelimiter = false;
510 guint32 TotalLength = 0;
513 while ( ! FoundSequenceDelimiter) {
516 TotalLength += 4; // We even have to decount the group and element
518 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",
519 "wrong group for an item sequence.");
520 throw Error::FileReadError(fp, "gdcmHeader::FindLengthOB");
523 FoundSequenceDelimiter = true;
524 else if ( n != 0xe000) {
525 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",
526 "wrong element for an item sequence.");
527 throw Error::FileReadError(fp, "gdcmHeader::FindLengthOB");
529 ItemLength = ReadInt32();
530 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
531 // the ItemLength with ReadInt32
532 SkipBytes(ItemLength);
534 fseek(fp, PositionOnEntry, SEEK_SET);
538 void gdcmHeader::FindLength(ElValue * ElVal) {
539 guint16 element = ElVal->GetElement();
540 string vr = ElVal->GetVR();
543 if ( (filetype == ExplicitVR) && ! ElVal->IsImplicitVr() ) {
545 if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") ) {
546 // The following reserved two bytes (see PS 3.5-2001, section
547 // 7.1.2 Data element structure with explicit vr p27) must be
548 // skipped before proceeding on reading the length on 4 bytes.
549 fseek(fp, 2L, SEEK_CUR);
550 guint32 length32 = ReadInt32();
551 if ( (vr == "OB") && (length32 == 0xffffffff) ) {
552 ElVal->SetLength(FindLengthOB());
555 FixFoundLength(ElVal, length32);
559 // Length is encoded on 2 bytes.
560 length16 = ReadInt16();
562 // We can tell the current file is encoded in big endian (like
563 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
564 // and it's value is the one of the encoding of a big endian file.
565 // In order to deal with such big endian encoded files, we have
566 // (at least) two strategies:
567 // * when we load the "Transfer Syntax" tag with value of big endian
568 // encoding, we raise the proper flags. Then we wait for the end
569 // of the META group (0x0002) among which is "Transfer Syntax",
570 // before switching the swap code to big endian. We have to postpone
571 // the switching of the swap code since the META group is fully encoded
572 // in little endian, and big endian coding only starts at the next
573 // group. The corresponding code can be hard to analyse and adds
574 // many additional unnecessary tests for regular tags.
575 // * the second strategy consist in waiting for trouble, that shall appear
576 // when we find the first group with big endian encoding. This is
577 // easy to detect since the length of a "Group Length" tag (the
578 // ones with zero as element number) has to be of 4 (0x0004). When we
579 // encouter 1024 (0x0400) chances are the encoding changed and we
580 // found a group with big endian encoding.
581 // We shall use this second strategy. In order make sure that we
582 // can interpret the presence of an apparently big endian encoded
583 // length of a "Group Length" without committing a big mistake, we
584 // add an additional check: we look in the allready parsed elements
585 // for the presence of a "Transfer Syntax" whose value has to be "big
586 // endian encoding". When this is the case, chances are we got our
587 // hands on a big endian encoded file: we switch the swap code to
588 // big endian and proceed...
589 if ( (element == 0x000) && (length16 == 0x0400) ) {
590 if ( ! IsExplicitVRBigEndianTransferSyntax() )
591 throw Error::FileReadError(fp, "gdcmHeader::FindLength");
593 SwitchSwapToBigEndian();
594 // Restore the unproperly loaded values i.e. the group, the element
595 // and the dictionary entry depending on them.
596 guint16 CorrectGroup = SwapShort(ElVal->GetGroup());
597 guint16 CorrectElem = SwapShort(ElVal->GetElement());
598 gdcmDictEntry * NewTag = IsInDicts(CorrectGroup, CorrectElem);
600 // This correct tag is not in the dictionary. Create a new one.
601 NewTag = new gdcmDictEntry(CorrectGroup, CorrectElem);
603 // FIXME this can create a memory leaks on the old entry that be
604 // left unreferenced.
605 ElVal->SetDictEntry(NewTag);
608 // Heuristic: well some files are really ill-formed.
609 if ( length16 == 0xffff) {
611 dbg.Verbose(0, "gdcmHeader::FindLength",
612 "Erroneous element length fixed.");
614 FixFoundLength(ElVal, (guint32)length16);
618 // Either implicit VR or a non DICOM conformal (see not below) explicit
619 // VR that ommited the VR of (at least) this element. Farts happen.
620 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
621 // on Data elements "Implicit and Explicit VR Data Elements shall
622 // not coexist in a Data Set and Data Sets nested within it".]
623 // Length is on 4 bytes.
624 FixFoundLength(ElVal, ReadInt32());
628 * \ingroup gdcmHeader
629 * \brief Swaps back the bytes of 4-byte long integer accordingly to
632 * @return The suggested integer.
634 guint32 gdcmHeader::SwapLong(guint32 a) {
635 // FIXME: il pourrait y avoir un pb pour les entiers negatifs ...
640 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
641 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
645 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
649 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
652 dbg.Error(" gdcmHeader::SwapLong : unset swap code");
659 * \ingroup gdcmHeader
660 * \brief Swaps the bytes so they agree with the processor order
661 * @return The properly swaped 16 bits integer.
663 guint16 gdcmHeader::SwapShort(guint16 a) {
664 if ( (sw==4321) || (sw==2143) )
665 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
669 void gdcmHeader::SkipBytes(guint32 NBytes) {
670 //FIXME don't dump the returned value
671 (void)fseek(fp, (long)NBytes, SEEK_CUR);
674 void gdcmHeader::SkipElementValue(ElValue * ElVal) {
675 SkipBytes(ElVal->GetLength());
678 void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) {
681 if ((guint32)NewSize >= (guint32)0xffffffff) {
682 MaxSizeLoadElementValue = 0xffffffff;
685 MaxSizeLoadElementValue = NewSize;
689 * \ingroup gdcmHeader
690 * \brief Loads the element if it's size is not to big.
691 * @param ElVal Element whose value shall be loaded.
692 * @param MaxSize Size treshold above which the element value is not
693 * loaded in memory. The element value is allways loaded
694 * when MaxSize is equal to UINT32_MAX.
697 void gdcmHeader::LoadElementValue(ElValue * ElVal) {
699 guint16 group = ElVal->GetGroup();
700 guint16 elem = ElVal->GetElement();
701 string vr = ElVal->GetVR();
702 guint32 length = ElVal->GetLength();
703 bool SkipLoad = false;
705 fseek(fp, (long)ElVal->GetOffset(), SEEK_SET);
707 // Sequences not treated yet !
709 // Ne faudrait-il pas au contraire trouver immediatement
710 // une maniere 'propre' de traiter les sequences (vr = SQ)
711 // car commencer par les ignorer risque de conduire a qq chose
712 // qui pourrait ne pas etre generalisable
717 // Heuristic : a sequence "contains" a set of tags (called items). It looks
718 // like the last tag of a sequence (the one that terminates the sequence)
719 // has a group of 0xfffe (with a dummy length).
720 if( group == 0xfffe )
723 // The group length doesn't represent data to be loaded in memory, since
724 // each element of the group shall be loaded individualy.
729 // FIXME the following skip is not necessary
730 SkipElementValue(ElVal);
732 ElVal->SetValue("gdcm::Skipped");
736 // When the length is zero things are easy:
742 // Values bigger than specified are not loaded.
744 // En fait, c'est les elements dont la longueur est superieure
745 // a celle fixee qui ne sont pas charges
747 if (length > MaxSizeLoadElementValue) {
749 s << "gdcm::NotLoaded.";
750 s << " Address:" << (long)ElVal->GetOffset();
751 s << " Length:" << ElVal->GetLength();
752 //mesg += " Length:" + ElVal->GetLength();
753 ElVal->SetValue(s.str());
757 // When an integer is expected, read and convert the following two or
758 // four bytes properly i.e. as an integer as opposed to a string.
759 if ( IsAnInteger(ElVal) ) {
762 NewInt = ReadInt16();
763 } else if( length == 4 ) {
764 NewInt = ReadInt32();
766 dbg.Error(true, "LoadElementValue: Inconsistency when reading Int.");
768 //FIXME: make the following an util fonction
771 ElVal->SetValue(s.str());
775 // FIXME The exact size should be length if we move to strings or whatever
778 // QUESTION : y a-t-il une raison pour ne pas utiliser g_malloc ici ?
781 char* NewValue = (char*)malloc(length+1);
783 dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
788 item_read = fread(NewValue, (size_t)length, (size_t)1, fp);
789 if ( item_read != 1 ) {
791 Error::FileReadError(fp, "gdcmHeader::LoadElementValue");
792 ElVal->SetValue("gdcm::UnRead");
795 ElVal->SetValue(NewValue);
799 * \ingroup gdcmHeader
800 * \brief Loads the element while preserving the current
801 * underlying file position indicator as opposed to
802 * to LoadElementValue that modifies it.
803 * @param ElVal Element whose value shall be loaded.
806 void gdcmHeader::LoadElementValueSafe(ElValue * ElVal) {
807 long PositionOnEntry = ftell(fp);
808 LoadElementValue(ElVal);
809 fseek(fp, PositionOnEntry, SEEK_SET);
813 guint16 gdcmHeader::ReadInt16(void) {
816 item_read = fread (&g, (size_t)2,(size_t)1, fp);
817 if ( item_read != 1 )
818 throw Error::FileReadError(fp, "gdcmHeader::ReadInt16");
823 guint32 gdcmHeader::ReadInt32(void) {
826 item_read = fread (&g, (size_t)4,(size_t)1, fp);
827 if ( item_read != 1 )
828 throw Error::FileReadError(fp, "gdcmHeader::ReadInt32");
834 * \ingroup gdcmHeader
835 * \brief Read the next tag without loading it's value
836 * @return On succes the newly created ElValue, NULL on failure.
839 ElValue * gdcmHeader::ReadNextElement(void) {
848 catch ( Error::FileReadError ) {
849 // We reached the EOF (or an error occured) and header parsing
850 // has to be considered as finished.
854 // Find out if the tag we encountered is in the dictionaries:
855 gdcmDictEntry * NewTag = IsInDicts(g, n);
857 NewTag = new gdcmDictEntry(g, n);
859 NewElVal = new ElValue(NewTag);
861 dbg.Verbose(1, "ReadNextElement: failed to allocate ElValue");
866 try { FindLength(NewElVal); }
867 catch ( Error::FileReadError ) { // Call it quits
870 NewElVal->SetOffset(ftell(fp));
874 bool gdcmHeader::IsAnInteger(ElValue * ElVal) {
875 guint16 group = ElVal->GetGroup();
876 guint16 element = ElVal->GetElement();
877 string vr = ElVal->GetVR();
878 guint32 length = ElVal->GetLength();
880 // When we have some semantics on the element we just read, and if we
881 // a priori know we are dealing with an integer, then we shall be
882 // able to swap it's element value properly.
883 if ( element == 0 ) { // This is the group length of the group
887 dbg.Error("gdcmHeader::IsAnInteger",
888 "Erroneous Group Length element length.");
891 if ( group % 2 != 0 )
892 // We only have some semantics on documented elements, which are
896 if ( (length != 4) && ( length != 2) )
897 // Swapping only make sense on integers which are 2 or 4 bytes long.
900 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
903 if ( (group == 0x0028) && (element == 0x0005) )
904 // This tag is retained from ACR/NEMA
905 // CHECKME Why should "Image Dimensions" be a single integer ?
907 // "Image Dimensions", c'est en fait le 'nombre de dimensions'
908 // de l'objet ACR-NEMA stocké
914 // DICOM V3 ne retient pas cette information
915 // Par defaut, tout est 'Image',
916 // C'est a l'utilisateur d'explorer l'ensemble des entetes
917 // pour savoir à quoi il a a faire
919 // Le Dicom Multiframe peut etre utilise pour stocker,
920 // dans un seul fichier, une serie temporelle (cardio vasculaire GE, p.ex)
921 // ou un volume (medecine Nucleaire, p.ex)
925 if ( (group == 0x0028) && (element == 0x0200) )
926 // This tag is retained from ACR/NEMA
933 * \ingroup gdcmHeader
934 * \brief Recover the offset (from the beginning of the file) of the pixels.
936 size_t gdcmHeader::GetPixelOffset(void) {
937 // If this file complies with the norm we should encounter the
938 // "Image Location" tag (0x0028, 0x0200). This tag contains the
939 // the group that contains the pixel data (hence the "Pixel Data"
940 // is found by indirection through the "Image Location").
941 // Inside the group pointed by "Image Location" the searched element
942 // is conventionally the element 0x0010 (when the norm is respected).
943 // When the "Image Location" is absent we default to group 0x7fe0.
946 string ImageLocation = GetPubElValByName("Image Location");
947 if ( ImageLocation == "UNFOUND" ) {
950 grPixel = (guint16) atoi( ImageLocation.c_str() );
952 if (grPixel != 0x7fe0)
953 // FIXME is this still necessary ?
954 // Now, this looks like an old dirty fix for Philips imager
958 ElValue* PixelElement = PubElVals.GetElementByNumber(grPixel, numPixel);
960 return PixelElement->GetOffset();
965 gdcmDictEntry * gdcmHeader::IsInDicts(guint32 group, guint32 element) {
967 // Y a-t-il une raison de lui passer des guint32
968 // alors que group et element sont des guint16?
970 gdcmDictEntry * found = (gdcmDictEntry*)0;
971 if (!RefPubDict && !RefShaDict) {
972 //FIXME build a default dictionary !
973 printf("FIXME in gdcmHeader::IsInDicts\n");
976 found = RefPubDict->GetTag(group, element);
981 found = RefShaDict->GetTag(group, element);
988 list<string> * gdcmHeader::GetPubTagNames(void) {
989 list<string> * Result = new list<string>;
990 TagHT entries = RefPubDict->GetEntries();
992 for (TagHT::iterator tag = entries.begin(); tag != entries.end(); ++tag){
993 Result->push_back( tag->second->GetName() );
998 map<string, list<string> > * gdcmHeader::GetPubTagNamesByCategory(void) {
999 map<string, list<string> > * Result = new map<string, list<string> >;
1000 TagHT entries = RefPubDict->GetEntries();
1002 for (TagHT::iterator tag = entries.begin(); tag != entries.end(); ++tag){
1003 (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
1008 string gdcmHeader::GetPubElValByNumber(guint16 group, guint16 element) {
1009 return PubElVals.GetElValueByNumber(group, element);
1012 string gdcmHeader::GetPubElValRepByNumber(guint16 group, guint16 element) {
1013 ElValue* elem = PubElVals.GetElementByNumber(group, element);
1015 return "gdcm::Unfound";
1016 return elem->GetVR();
1019 string gdcmHeader::GetPubElValByName(string TagName) {
1020 return PubElVals.GetElValueByName(TagName);
1023 string gdcmHeader::GetPubElValRepByName(string TagName) {
1024 ElValue* elem = PubElVals.GetElementByName(TagName);
1026 return "gdcm::Unfound";
1027 return elem->GetVR();
1030 string gdcmHeader::GetShaElValByNumber(guint16 group, guint16 element) {
1031 return ShaElVals.GetElValueByNumber(group, element);
1034 string gdcmHeader::GetShaElValRepByNumber(guint16 group, guint16 element) {
1035 ElValue* elem = ShaElVals.GetElementByNumber(group, element);
1037 return "gdcm::Unfound";
1038 return elem->GetVR();
1041 string gdcmHeader::GetShaElValByName(string TagName) {
1042 return ShaElVals.GetElValueByName(TagName);
1045 string gdcmHeader::GetShaElValRepByName(string TagName) {
1046 ElValue* elem = ShaElVals.GetElementByName(TagName);
1048 return "gdcm::Unfound";
1049 return elem->GetVR();
1053 string gdcmHeader::GetElValByNumber(guint16 group, guint16 element) {
1054 string pub = GetPubElValByNumber(group, element);
1057 return GetShaElValByNumber(group, element);
1060 string gdcmHeader::GetElValRepByNumber(guint16 group, guint16 element) {
1061 string pub = GetPubElValRepByNumber(group, element);
1064 return GetShaElValRepByNumber(group, element);
1067 string gdcmHeader::GetElValByName(string TagName) {
1068 string pub = GetPubElValByName(TagName);
1071 return GetShaElValByName(TagName);
1074 string gdcmHeader::GetElValRepByName(string TagName) {
1075 string pub = GetPubElValRepByName(TagName);
1078 return GetShaElValRepByName(TagName);
1082 * \ingroup gdcmHeader
1083 * \brief Parses the header of the file but does NOT load element values.
1085 void gdcmHeader::ParseHeader(void) {
1086 ElValue * newElValue = (ElValue *)0;
1090 while ( (newElValue = ReadNextElement()) ) {
1091 SkipElementValue(newElValue);
1092 PubElVals.Add(newElValue);
1097 * \ingroup gdcmHeader
1098 * \brief Loads the element values of all the elements present in the
1099 * public tag based hash table.
1101 void gdcmHeader::LoadElements(void) {
1103 if (DEBUG) printf("LoadElements : Entree\n");
1106 if (DEBUG) printf("LoadElements : rewind\n");
1108 TagElValueHT ht = PubElVals.GetTagHt();
1110 if (DEBUG) printf("LoadElements : GetTagHt\n");
1112 for (TagElValueHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) {
1113 LoadElementValue(tag->second);
1117 void gdcmHeader::PrintPubElVal(ostream & os) {
1118 PubElVals.Print(os);
1121 void gdcmHeader::PrintPubDict(ostream & os) {
1122 RefPubDict->Print(os);