9 #include <netinet/in.h>
11 #include <cctype> // for isalpha
16 #define HEADER_LENGTH_TO_READ 256 // on ne lit plus que le debut
19 struct FileReadError {
20 FileReadError(FILE* fp, const char* Mesg) {
22 dbg.Verbose(1, "EOF encountered :", Mesg);
24 dbg.Verbose(1, "Error on reading :", Mesg);
29 //FIXME: this looks dirty to me...
30 #define str2num(str, typeNum) *((typeNum *)(str))
32 VRHT * gdcmHeader::dicom_vr = (VRHT*)0;
33 gdcmDictSet* gdcmHeader::Dicts = new gdcmDictSet();
35 void gdcmHeader::Initialise(void) {
36 if (!gdcmHeader::dicom_vr)
38 RefPubDict = gdcmHeader::Dicts->GetDefaultPublicDict();
39 RefShaDict = (gdcmDict*)0;
42 gdcmHeader::gdcmHeader (const char* InFilename) {
43 SetMaxSizeLoadElementValue(1024);
44 filename = InFilename;
46 fp=fopen(InFilename,"rw");
47 dbg.Error(!fp, "gdcmHeader::gdcmHeader cannot open file", InFilename);
51 gdcmHeader::~gdcmHeader (void) {
56 void gdcmHeader::InitVRDict (void) {
58 dbg.Verbose(0, "gdcmHeader::InitVRDict:", "VR dictionary allready set");
62 (*vr)["AE"] = "Application Entity"; // At most 16 bytes
63 (*vr)["AS"] = "Age String"; // Exactly 4 bytes
64 (*vr)["AT"] = "Attribute Tag"; // 2 16-bit unsigned short integers
65 (*vr)["CS"] = "Code String"; // At most 16 bytes
66 (*vr)["DA"] = "Date"; // Exactly 8 bytes
67 (*vr)["DS"] = "Decimal String"; // At most 16 bytes
68 (*vr)["DT"] = "Date Time"; // At most 26 bytes
69 (*vr)["FL"] = "Floating Point Single"; // 32-bit IEEE 754:1985 float
70 (*vr)["FD"] = "Floating Point Double"; // 64-bit IEEE 754:1985 double
71 (*vr)["IS"] = "Integer String"; // At most 12 bytes
72 (*vr)["LO"] = "Long String"; // At most 64 chars
73 (*vr)["LT"] = "Long Text"; // At most 10240 chars
74 (*vr)["OB"] = "Other Byte String"; // String of bytes (vr independant)
75 (*vr)["OW"] = "Other Word String"; // String of 16-bit words (vr dep)
76 (*vr)["PN"] = "Person Name"; // At most 64 chars
77 (*vr)["SH"] = "Short String"; // At most 16 chars
78 (*vr)["SL"] = "Signed Long"; // Exactly 4 bytes
79 (*vr)["SQ"] = "Sequence of Items"; // Not Applicable
80 (*vr)["SS"] = "Signed Short"; // Exactly 2 bytes
81 (*vr)["ST"] = "Short Text"; // At most 1024 chars
82 (*vr)["TM"] = "Time"; // At most 16 bytes
83 (*vr)["UI"] = "Unique Identifier"; // At most 64 bytes
84 (*vr)["UL"] = "Unsigned Long "; // Exactly 4 bytes
85 (*vr)["UN"] = "Unknown"; // Any length of bytes
86 (*vr)["US"] = "Unsigned Short "; // Exactly 2 bytes
87 (*vr)["UT"] = "Unlimited Text"; // At most 2^32 -1 chars
93 * \brief Discover what the swap code is (among little endian, big endian,
94 * bad little endian, bad big endian).
97 void gdcmHeader::CheckSwap()
99 // The only guaranted way of finding the swap code is to find a
100 // group tag since we know it's length has to be of four bytes i.e.
101 // 0x00000004. Finding the swap code in then straigthforward. Trouble
102 // occurs when we can't find such group...
104 guint32 x=4; // x : pour ntohs
105 bool net2host; // true when HostByteOrder is the same as NetworkByteOrder
109 char deb[HEADER_LENGTH_TO_READ];
111 // First, compare HostByteOrder and NetworkByteOrder in order to
112 // determine if we shall need to swap bytes (i.e. the Endian type).
118 // The easiest case is the one of a DICOM header, since it possesses a
119 // file preamble where it suffice to look for the sting "DICM".
120 lgrLue = fread(deb, 1, HEADER_LENGTH_TO_READ, fp);
123 if(memcmp(entCur, "DICM", (size_t)4) == 0) {
124 filetype = TrueDicom;
125 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "looks like DICOM Version3");
128 dbg.Verbose(1, "gdcmHeader::CheckSwap:", "not a DICOM Version3 file");
131 if(filetype == TrueDicom) {
132 // Next, determine the value representation (VR). Let's skip to the
133 // first element (0002, 0000) and check there if we find "UL", in
134 // which case we (almost) know it is explicit VR.
135 // WARNING: if it happens to be implicit VR then what we will read
136 // is the length of the group. If this ascii representation of this
137 // length happens to be "UL" then we shall believe it is explicit VR.
138 // FIXME: in order to fix the above warning, we could read the next
139 // element value (or a couple of elements values) in order to make
140 // sure we are not commiting a big mistake.
142 // * the 128 bytes of File Preamble (often padded with zeroes),
143 // * the 4 bytes of "DICM" string,
144 // * the 4 bytes of the first tag (0002, 0000),
145 // i.e. a total of 136 bytes.
147 if(memcmp(entCur, "UL", (size_t)2) == 0) {
148 filetype = ExplicitVR;
149 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
150 "explicit Value Representation");
152 filetype = ImplicitVR;
153 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
154 "not an explicit Value Representation");
159 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
160 "HostByteOrder != NetworkByteOrder");
163 dbg.Verbose(1, "gdcmHeader::CheckSwap:",
164 "HostByteOrder = NetworkByteOrder");
167 // Position the file position indicator at first tag (i.e.
168 // after the file preamble and the "DICM" string).
170 fseek (fp, 132L, SEEK_SET);
172 } // End of TrueDicom
174 // Alas, this is not a DicomV3 file and whatever happens there is no file
175 // preamble. We can reset the file position indicator to where the data
176 // is (i.e. the beginning of the file).
179 // Our next best chance would be to be considering a 'clean' ACR/NEMA file.
180 // By clean we mean that the length of the first tag is written down.
181 // If this is the case and since the length of the first group HAS to be
182 // four (bytes), then determining the proper swap code is straightforward.
185 s = str2num(entCur, guint32);
205 dbg.Verbose(0, "gdcmHeader::CheckSwap:",
206 "ACR/NEMA unfound swap info (time to raise bets)");
209 // We are out of luck. It is not a DicomV3 nor a 'clean' ACR/NEMA file.
210 // It is time for despaired wild guesses. So, let's assume this file
211 // happens to be 'dirty' ACR/NEMA, i.e. the length of the group is
212 // not present. Then the only info we have is the net2host one.
220 void gdcmHeader::SwitchSwapToBigEndian(void) {
221 dbg.Verbose(1, "gdcmHeader::SwitchSwapToBigEndian",
222 "Switching to BigEndian mode.");
239 void gdcmHeader::GetPixels(size_t lgrTotale, void* Pixels) {
241 pixelsOffset = GetPixelOffset();
242 fseek(fp, pixelsOffset, SEEK_SET);
243 fread(Pixels, 1, lgrTotale, fp);
247 * \ingroup gdcmHeader
248 * \brief Find the value representation of the current tag.
250 * @param sw code swap
251 * @param skippedLength pointeur sur nombre d'octets que l'on a saute qd
252 * la lecture est finie
253 * @param longueurLue pointeur sur longueur (en nombre d'octets)
255 * @return longueur retenue pour le champ
258 void gdcmHeader::FindVR( ElValue *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) )
294 RealExplicit = false;
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 Big Endian one.
339 * @return True when big endian found. False in all other cases.
341 bool gdcmHeader::IsBigEndianTransferSyntax(void) {
342 ElValue* Element = PubElVals.GetElementByNumber(0x0002, 0x0010);
345 LoadElementValueSafe(Element);
346 string Transfer = Element->GetValue();
347 if ( Transfer == "1.2.840.10008.1.2.2" )
352 void gdcmHeader::FixFoundLength(ElValue * ElVal, guint32 FoudLength) {
353 // Heuristic: a final fix.
354 if ( FoudLength == 0xffffffff)
356 ElVal->SetLength(FoudLength);
359 guint32 gdcmHeader::FindLengthOB(void) {
360 // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data.
363 long PositionOnEntry = ftell(fp);
364 bool FoundSequenceDelimiter = false;
365 guint32 TotalLength = 0;
368 while ( ! FoundSequenceDelimiter) {
371 TotalLength += 4; // We even have to decount the group and element
373 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",
374 "wrong group for an item sequence.");
375 throw Error::FileReadError(fp, "gdcmHeader::FindLengthOB");
378 FoundSequenceDelimiter = true;
379 else if ( n != 0xe000) {
380 dbg.Verbose(1, "gdcmHeader::FindLengthOB: ",
381 "wrong element for an item sequence.");
382 throw Error::FileReadError(fp, "gdcmHeader::FindLengthOB");
384 ItemLength = ReadInt32();
385 TotalLength += ItemLength + 4; // We add 4 bytes since we just read
386 // the ItemLength with ReadInt32
387 SkipBytes(ItemLength);
389 fseek(fp, PositionOnEntry, SEEK_SET);
393 void gdcmHeader::FindLength(ElValue * ElVal) {
394 guint16 element = ElVal->GetElement();
395 string vr = ElVal->GetVR();
398 if ( (filetype == ExplicitVR) && ! ElVal->IsImplicitVr() ) {
400 if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") ) {
401 // The following reserved two bytes (see PS 3.5-2001, section
402 // 7.1.2 Data element structure with explicit vr p27) must be
403 // skipped before proceeding on reading the length on 4 bytes.
404 fseek(fp, 2L, SEEK_CUR);
405 guint32 length32 = ReadInt32();
406 if ( (vr == "OB") && (length32 == 0xffffffff) ) {
407 ElVal->SetLength(FindLengthOB());
410 FixFoundLength(ElVal, length32);
414 // Length is encoded on 2 bytes.
415 length16 = ReadInt16();
417 // We can tell the current file is encoded in big endian (like
418 // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
419 // and it's value is the one of the encoding of a big endian file.
420 // In order to deal with such big endian encoded files, we have
421 // (at least) two strategies:
422 // * when we load the "Transfer Syntax" tag with value of big endian
423 // encoding, we raise the proper flags. Then we wait for the end
424 // of the META group (0x0002) among which is "Transfer Syntax",
425 // before switching the swap code to big endian. We have to postpone
426 // the switching of the swap code since the META group is fully encoded
427 // in little endian, and big endian coding only starts at the next
428 // group. The corresponding code can be hard to analyse and adds
429 // many additional unnecessary tests for regular tags.
430 // * the second strategy consist in waiting for trouble, that shall appear
431 // when we find the first group with big endian encoding. This is
432 // easy to detect since the length of a "Group Length" tag (the
433 // ones with zero as element number) has to be of 4 (0x0004). When we
434 // encouter 1024 (0x0400) chances are the encoding changed and we
435 // found a group with big endian encoding.
436 // We shall use this second strategy. In order make sure that we
437 // can interpret the presence of an apparently big endian encoded
438 // length of a "Group Length" without committing a big mistake, we
439 // add an additional check: we look in the allready parsed elements
440 // for the presence of a "Transfer Syntax" whose value has to be "big
441 // endian encoding". When this is the case, chances are we got our
442 // hands on a big endian encoded file: we switch the swap code to
443 // big endian and proceed...
444 if ( (element == 0x000) && (length16 == 0x0400) ) {
445 if ( ! IsBigEndianTransferSyntax() )
446 throw Error::FileReadError(fp, "gdcmHeader::FindLength");
448 SwitchSwapToBigEndian();
449 // Restore the unproperly loaded values i.e. the group, the element
450 // and the dictionary entry depending on them.
451 guint16 CorrectGroup = SwapShort(ElVal->GetGroup());
452 guint16 CorrectElem = SwapShort(ElVal->GetElement());
453 gdcmDictEntry * NewTag = IsInDicts(CorrectGroup, CorrectElem);
455 // This correct tag is not in the dictionary. Create a new one.
456 NewTag = new gdcmDictEntry(CorrectGroup, CorrectElem);
458 // FIXME this can create a memory leaks on the old entry that be
459 // left unreferenced.
460 ElVal->SetDictEntry(NewTag);
463 // Heuristic: well some files are really ill-formed.
464 if ( length16 == 0xffff) {
466 dbg.Verbose(0, "gdcmHeader::FindLength",
467 "Erroneous element length fixed.");
469 FixFoundLength(ElVal, (guint32)length16);
473 // Either implicit VR or a non DICOM conformal (see not below) explicit
474 // VR that ommited the VR of (at least) this element. Farts happen.
475 // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
476 // on Data elements "Implicit and Explicit VR Data Elements shall
477 // not coexist in a Data Set and Data Sets nested within it".]
478 // Length is on 4 bytes.
479 FixFoundLength(ElVal, ReadInt32());
483 * \ingroup gdcmHeader
484 * \brief Swaps back the bytes of 4-byte long integer accordingly to
487 * @return The suggested integer.
489 guint32 gdcmHeader::SwapLong(guint32 a) {
490 // FIXME: il pourrait y avoir un pb pour les entiers negatifs ...
495 a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
496 ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
500 a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
504 a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
507 dbg.Error(" gdcmHeader::SwapLong : unset swap code");
514 * \ingroup gdcmHeader
515 * \brief Swaps the bytes so they agree with the processor order
516 * @return The properly swaped 16 bits integer.
518 guint16 gdcmHeader::SwapShort(guint16 a) {
519 if ( (sw==4321) || (sw==2143) )
520 a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
524 void gdcmHeader::SkipBytes(guint32 NBytes) {
525 //FIXME don't dump the returned value
526 (void)fseek(fp, (long)NBytes, SEEK_CUR);
529 void gdcmHeader::SkipElementValue(ElValue * ElVal) {
530 SkipBytes(ElVal->GetLength());
533 void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) {
536 if ((guint32)NewSize >= (guint32)0xffffffff) {
537 MaxSizeLoadElementValue = 0xffffffff;
540 MaxSizeLoadElementValue = NewSize;
544 * \ingroup gdcmHeader
545 * \brief Loads the element if it's size is not to big.
546 * @param ElVal Element whose value shall be loaded.
547 * @param MaxSize Size treshold above which the element value is not
548 * loaded in memory. The element value is allways loaded
549 * when MaxSize is equal to UINT32_MAX.
552 void gdcmHeader::LoadElementValue(ElValue * ElVal) {
554 guint16 group = ElVal->GetGroup();
555 guint16 elem = ElVal->GetElement();
556 string vr = ElVal->GetVR();
557 guint32 length = ElVal->GetLength();
558 bool SkipLoad = false;
560 fseek(fp, (long)ElVal->GetOffset(), SEEK_SET);
562 // Sequences not treated yet !
564 // Ne faudrait-il pas au contraire trouver immediatement
565 // une maniere 'propre' de traiter les sequences (vr = SQ)
566 // car commencer par les ignorer risque de conduire a qq chose
567 // qui pourrait ne pas etre generalisable
572 // Heuristic : a sequence "contains" a set of tags (called items). It looks
573 // like the last tag of a sequence (the one that terminates the sequence)
574 // has a group of 0xfffe (with a dummy length).
575 if( group == 0xfffe )
578 // The group length doesn't represent data to be loaded in memory, since
579 // each element of the group shall be loaded individualy.
584 // FIXME the following skip is not necessary
585 SkipElementValue(ElVal);
587 ElVal->SetValue("gdcm::Skipped");
591 // When the length is zero things are easy:
597 // Values bigger than specified are not loaded.
599 // En fait, c'est les elements dont la longueur est superieure
600 // a celle fixee qui ne sont pas charges
602 if (length > MaxSizeLoadElementValue) {
604 s << "gdcm::NotLoaded.";
605 s << " Address:" << (long)ElVal->GetOffset();
606 s << " Length:" << ElVal->GetLength();
607 //mesg += " Length:" + ElVal->GetLength();
608 ElVal->SetValue(s.str());
612 // When an integer is expected, read and convert the following two or
613 // four bytes properly i.e. as an integer as opposed to a string.
614 if ( IsAnInteger(ElVal) ) {
617 NewInt = ReadInt16();
618 } else if( length == 4 ) {
619 NewInt = ReadInt32();
621 dbg.Error(true, "LoadElementValue: Inconsistency when reading Int.");
623 //FIXME: make the following an util fonction
626 ElVal->SetValue(s.str());
630 // FIXME The exact size should be length if we move to strings or whatever
631 char* NewValue = (char*)malloc(length+1);
633 dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
638 item_read = fread(NewValue, (size_t)length, (size_t)1, fp);
639 if ( item_read != 1 ) {
641 Error::FileReadError(fp, "gdcmHeader::LoadElementValue");
642 ElVal->SetValue("gdcm::UnRead");
645 ElVal->SetValue(NewValue);
649 * \ingroup gdcmHeader
650 * \brief Loads the element while preserving the current
651 * underlying file position indicator as opposed to
652 * to LoadElementValue that modifies it.
653 * @param ElVal Element whose value shall be loaded.
656 void gdcmHeader::LoadElementValueSafe(ElValue * ElVal) {
657 long PositionOnEntry = ftell(fp);
658 LoadElementValue(ElVal);
659 fseek(fp, PositionOnEntry, SEEK_SET);
663 guint16 gdcmHeader::ReadInt16(void) {
666 item_read = fread (&g, (size_t)2,(size_t)1, fp);
667 if ( item_read != 1 )
668 throw Error::FileReadError(fp, "gdcmHeader::ReadInt16");
673 guint32 gdcmHeader::ReadInt32(void) {
676 item_read = fread (&g, (size_t)4,(size_t)1, fp);
677 if ( item_read != 1 )
678 throw Error::FileReadError(fp, "gdcmHeader::ReadInt32");
684 * \ingroup gdcmHeader
685 * \brief Read the next tag without loading it's value
686 * @return On succes the newly created ElValue, NULL on failure.
689 ElValue * gdcmHeader::ReadNextElement(void) {
698 catch ( Error::FileReadError ) {
699 // We reached the EOF (or an error occured) and header parsing
700 // has to be considered as finished.
704 // Find out if the tag we encountered is in the dictionaries:
705 gdcmDictEntry * NewTag = IsInDicts(g, n);
707 NewTag = new gdcmDictEntry(g, n);
709 NewElVal = new ElValue(NewTag);
711 dbg.Verbose(1, "ReadNextElement: failed to allocate ElValue");
716 try { FindLength(NewElVal); }
717 catch ( Error::FileReadError ) { // Call it quits
720 NewElVal->SetOffset(ftell(fp));
724 bool gdcmHeader::IsAnInteger(ElValue * ElVal) {
725 guint16 group = ElVal->GetGroup();
726 guint16 element = ElVal->GetElement();
727 string vr = ElVal->GetVR();
728 guint32 length = ElVal->GetLength();
730 // When we have some semantics on the element we just read, and if we
731 // a priori know we are dealing with an integer, then we shall be
732 // able to swap it's element value properly.
733 if ( element == 0 ) { // This is the group length of the group
737 dbg.Error("gdcmHeader::IsAnInteger",
738 "Erroneous Group Length element length.");
741 if ( group % 2 != 0 )
742 // We only have some semantics on documented elements, which are
746 if ( (length != 4) && ( length != 2) )
747 // Swapping only make sense on integers which are 2 or 4 bytes long.
750 if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") )
753 if ( (group == 0x0028) && (element == 0x0005) )
754 // This tag is retained from ACR/NEMA
755 // CHECKME Why should "Image Dimensions" be a single integer ?
757 // "Image Dimensions", c'est en fait le 'nombre de dimensions'
758 // de l'objet ACR-NEMA stocké
764 // DICOM V3 ne retient pas cette information
765 // Par defaut, tout est 'Image',
766 // C'est a l'utilisateur d'explorer l'ensemble des entetes
767 // pour savoir à quoi il a a faire
769 // Le Dicom Multiframe peut etre utilise pour stocker,
770 // dans un seul fichier, une serie temporelle (cardio vasculaire GE, p.ex)
771 // ou un volume (medecine Nucleaire, p.ex)
775 if ( (group == 0x0028) && (element == 0x0200) )
776 // This tag is retained from ACR/NEMA
783 * \ingroup gdcmHeader
784 * \brief Recover the offset (from the beginning of the file) of the pixels.
786 size_t gdcmHeader::GetPixelOffset(void) {
787 // If this file complies with the norm we should encounter the
788 // "Image Location" tag (0x0028, 0x0200). This tag contains the
789 // the group that contains the pixel data (hence the "Pixel Data"
790 // is found by indirection through the "Image Location").
791 // Inside the group pointed by "Image Location" the searched element
792 // is conventionally the element 0x0010 (when the norm is respected).
793 // When the "Image Location" is absent we default to group 0x7fe0.
796 string ImageLocation = GetPubElValByName("Image Location");
797 if ( ImageLocation == "UNFOUND" ) {
800 grPixel = (guint16) atoi( ImageLocation.c_str() );
802 if (grPixel != 0x7fe0)
803 // FIXME is this still necessary ?
804 // Now, this looks like an old dirty fix for Philips imager
808 ElValue* PixelElement = PubElVals.GetElementByNumber(grPixel, numPixel);
810 return PixelElement->GetOffset();
815 gdcmDictEntry * gdcmHeader::IsInDicts(guint32 group, guint32 element) {
817 // Y a-t-il une raison de lui passer des guint32
818 // alors que group et element sont des guint16?
820 gdcmDictEntry * found = (gdcmDictEntry*)0;
821 if (!RefPubDict && !RefShaDict) {
822 //FIXME build a default dictionary !
823 printf("FIXME in gdcmHeader::IsInDicts\n");
826 found = RefPubDict->GetTag(group, element);
831 found = RefShaDict->GetTag(group, element);
838 list<string> * gdcmHeader::GetPubTagNames(void) {
839 list<string> * Result = new list<string>;
840 TagHT entries = RefPubDict->GetEntries();
842 for (TagHT::iterator tag = entries.begin(); tag != entries.end(); ++tag){
843 Result->push_back( tag->second->GetName() );
848 map<string, list<string> > * gdcmHeader::GetPubTagNamesByCategory(void) {
849 map<string, list<string> > * Result = new map<string, list<string> >;
850 TagHT entries = RefPubDict->GetEntries();
852 for (TagHT::iterator tag = entries.begin(); tag != entries.end(); ++tag){
853 (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
858 string gdcmHeader::GetPubElValByNumber(guint16 group, guint16 element) {
859 return PubElVals.GetElValueByNumber(group, element);
862 string gdcmHeader::GetPubElValRepByNumber(guint16 group, guint16 element) {
863 ElValue* elem = PubElVals.GetElementByNumber(group, element);
865 return "gdcm::Unfound";
866 return elem->GetVR();
869 string gdcmHeader::GetPubElValByName(string TagName) {
870 return PubElVals.GetElValueByName(TagName);
873 string gdcmHeader::GetPubElValRepByName(string TagName) {
874 ElValue* elem = PubElVals.GetElementByName(TagName);
876 return "gdcm::Unfound";
877 return elem->GetVR();
880 string gdcmHeader::GetShaElValByNumber(guint16 group, guint16 element) {
881 return ShaElVals.GetElValueByNumber(group, element);
884 string gdcmHeader::GetShaElValRepByNumber(guint16 group, guint16 element) {
885 ElValue* elem = ShaElVals.GetElementByNumber(group, element);
887 return "gdcm::Unfound";
888 return elem->GetVR();
891 string gdcmHeader::GetShaElValByName(string TagName) {
892 return ShaElVals.GetElValueByName(TagName);
895 string gdcmHeader::GetShaElValRepByName(string TagName) {
896 ElValue* elem = ShaElVals.GetElementByName(TagName);
898 return "gdcm::Unfound";
899 return elem->GetVR();
903 string gdcmHeader::GetElValByNumber(guint16 group, guint16 element) {
904 string pub = GetPubElValByNumber(group, element);
907 return GetShaElValByNumber(group, element);
910 string gdcmHeader::GetElValRepByNumber(guint16 group, guint16 element) {
911 string pub = GetPubElValRepByNumber(group, element);
914 return GetShaElValRepByNumber(group, element);
917 string gdcmHeader::GetElValByName(string TagName) {
918 string pub = GetPubElValByName(TagName);
921 return GetShaElValByName(TagName);
924 string gdcmHeader::GetElValRepByName(string TagName) {
925 string pub = GetPubElValRepByName(TagName);
928 return GetShaElValRepByName(TagName);
932 * \ingroup gdcmHeader
933 * \brief Parses the header of the file but does NOT load element values.
935 void gdcmHeader::ParseHeader(void) {
936 ElValue * newElValue = (ElValue *)0;
940 while ( (newElValue = ReadNextElement()) ) {
941 SkipElementValue(newElValue);
942 PubElVals.Add(newElValue);
947 * \ingroup gdcmHeader
948 * \brief Loads the element values of all the elements present in the
949 * public tag based hash table.
951 void gdcmHeader::LoadElements(void) {
953 TagElValueHT ht = PubElVals.GetTagHt();
954 for (TagElValueHT::iterator tag = ht.begin(); tag != ht.end(); ++tag)
955 LoadElementValue(tag->second);
958 void gdcmHeader::PrintPubElVal(ostream & os) {
962 void gdcmHeader::PrintPubDict(ostream & os) {
963 RefPubDict->Print(os);