+std::string gdcmHeader::GetEntryVRByNumber(guint16 group, guint16 element) {
+ return GetPubEntryVRByNumber(group, element);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Sets the value (string) of the target Dicom Element
+ * @param content string value of the Dicom Element
+ * @param tagName name of the searched Dicom Element.
+ * @return true when found
+ */
+bool gdcmHeader::SetEntryByName(std::string content,std::string tagName) {
+ gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
+ if( dictEntry == NULL)
+ return false;
+
+
+ TagKey key = gdcmDictEntry::TranslateToKey(dictEntry->GetGroup(),
+ dictEntry->GetElement());
+ if ( PubEntrySet.GetTagHT().count(key) == 0 )
+ return false;
+ int l = content.length();
+ if(l%2) { // Odd length are padded with a space (020H).
+ l++;
+ content = content + '\0';
+ }
+
+ //tagHt[key]->SetValue(content);
+ gdcmHeaderEntry * a;
+ IterHT p;
+ TagHeaderEntryHT::iterator p2;
+ // DO NOT remove the following lines : they explain the stuff
+ //p= tagHt.equal_range(key); // get a pair of iterators first-last synonym
+ //p2=p.first; // iterator on the first synonym
+ //a=p2->second; // H Table target column (2-nd col)
+
+ // or, easier :
+ a = ((PubEntrySet.GetTagHT().equal_range(key)).first)->second;
+
+ a-> SetValue(content);
+
+ //std::string vr = tagHt[key]->GetVR();
+ std::string vr = a->GetVR();
+
+ guint32 lgr;
+ if( (vr == "US") || (vr == "SS") )
+ lgr = 2;
+ else if( (vr == "UL") || (vr == "SL") )
+ lgr = 4;
+ else
+ lgr = l;
+ //tagHt[key]->SetLength(lgr);
+ a->SetLength(lgr);
+ return true;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief
+ * @param exception_on_error
+ * @return
+ */
+FILE *gdcmHeader::OpenFile(bool exception_on_error)
+ throw(gdcmFileError) {
+ fp=fopen(filename.c_str(),"rb");
+ if(exception_on_error) {
+ if(!fp)
+ throw gdcmFileError("gdcmHeader::gdcmHeader(const char *, bool)");
+ }
+
+ if ( fp ) {
+ guint16 zero;
+ fread(&zero, (size_t)2, (size_t)1, fp);
+
+ //ACR -- or DICOM with no Preamble
+ if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200)
+ return(fp);
+
+ //DICOM
+ fseek(fp, 126L, SEEK_CUR);
+ char dicm[4];
+ fread(dicm, (size_t)4, (size_t)1, fp);
+ if( memcmp(dicm, "DICM", 4) == 0 )
+ return(fp);
+
+ fclose(fp);
+ dbg.Verbose(0, "gdcmHeader::gdcmHeader not DICOM/ACR", filename.c_str());
+ }
+ else {
+ dbg.Verbose(0, "gdcmHeader::gdcmHeader cannot open file", filename.c_str());
+ }
+ return(NULL);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief
+ * @return TRUE if the close was successfull
+ */
+bool gdcmHeader::CloseFile(void) {
+ int closed = fclose(fp);
+ fp = (FILE *)0;
+ if (! closed)
+ return false;
+ return true;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Parses the header of the file but WITHOUT loading element values.
+ */
+void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) {
+ gdcmHeaderEntry * newHeaderEntry = (gdcmHeaderEntry *)0;
+
+ rewind(fp);
+ CheckSwap();
+ while ( (newHeaderEntry = ReadNextHeaderEntry()) ) {
+ SkipHeaderEntry(newHeaderEntry);
+ PubEntrySet.Add(newHeaderEntry);
+ }
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief
+ * @return integer, acts as a Boolean
+ */
+bool gdcmHeader::Write(FILE * fp, FileType type) {
+
+ // TODO : move the following lines (and a lot of others, to be written)
+ // to a future function CheckAndCorrectHeader
+
+ if (type == ImplicitVR) {
+ std::string implicitVRTransfertSyntax = "1.2.840.10008.1.2";
+ ReplaceOrCreateByNumber(implicitVRTransfertSyntax,0x0002, 0x0010);
+
+ //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
+ // values with a VR of UI shall be padded with a single trailing null
+ // Dans le cas suivant on doit pader manuellement avec un 0
+
+ PubEntrySet.SetEntryLengthByNumber(18, 0x0002, 0x0010);
+ }
+
+ if (type == ExplicitVR) {
+ std::string explicitVRTransfertSyntax = "1.2.840.10008.1.2.1";
+ ReplaceOrCreateByNumber(explicitVRTransfertSyntax,0x0002, 0x0010);
+
+ //FIXME Refer to standards on page 21, chapter 6.2 "Value representation":
+ // values with a VR of UI shall be padded with a single trailing null
+ // Dans le cas suivant on doit pader manuellement avec un 0
+
+ PubEntrySet.SetEntryLengthByNumber(20, 0x0002, 0x0010);
+ }
+
+ return PubEntrySet.Write(fp, type);
+}
+
+/**
+ * \ingroup gdcmFile
+ * \brief Sets the Pixel Area size in the Header
+ * --> not-for-rats function
+ *
+ * \warning WARNING doit-etre etre publique ?
+ * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions
+ *
+ * @param ImageDataSize new Pixel Area Size
+ * warning : nothing else is checked
+ */
+void gdcmHeader::SetImageDataSize(size_t ImageDataSize) {
+ std::string content1;
+ char car[20];
+ // Assumes HeaderEntry (0x7fe0, 0x0010) exists ...
+ sprintf(car,"%d",ImageDataSize);
+
+ gdcmHeaderEntry *a = GetHeaderEntryByNumber(0x7fe0, 0x0010);
+ a->SetLength(ImageDataSize);
+
+ ImageDataSize+=8;
+ sprintf(car,"%d",ImageDataSize);
+ content1=car;
+ SetPubEntryByNumber(content1, 0x7fe0, 0x0000);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief TODO
+ * @param Value
+ * @param Group
+ * @param Elem
+ * \return integer acts as a boolean
+ */
+bool gdcmHeader::ReplaceOrCreateByNumber(std::string Value,
+ guint16 Group, guint16 Elem ) {
+ // TODO : FIXME JPRx
+ // curieux, non ?
+ // on (je) cree une HeaderEntry ne contenant pas de valeur
+ // on l'ajoute au HeaderEntrySet
+ // on affecte une valeur a cette HeaderEntry a l'interieur du HeaderEntrySet
+ // --> devrait pouvoir etre fait + simplement ???
+ if (CheckIfExistByNumber(Group, Elem) == 0) {
+ gdcmHeaderEntry* a =NewHeaderEntryByNumber(Group, Elem);
+ if (a == NULL)
+ return false;
+ PubEntrySet.Add(a);
+ }
+ PubEntrySet.SetEntryByNumber(Value, Group, Elem);
+ return(true);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Modify (or Creates if not found) an element
+ * @param Value new value
+ * @param Group
+ * @param Elem
+ * \return integer acts as a boolean
+ *
+ */
+bool gdcmHeader::ReplaceOrCreateByNumber(char* Value, guint16 Group, guint16 Elem ) {
+
+ gdcmHeaderEntry* nvHeaderEntry=NewHeaderEntryByNumber(Group, Elem);
+ // TODO : check if fails
+ PubEntrySet.Add(nvHeaderEntry);
+ std::string v = Value;
+ PubEntrySet.SetEntryByNumber(v, Group, Elem);
+ return(true);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Set a new value if the invoked element exists
+ * Seems to be useless !!!
+ * @param Value
+ * @param Group
+ * @param Elem
+ * \return integer acts as a boolean
+ */
+bool gdcmHeader::ReplaceIfExistByNumber(char* Value, guint16 Group, guint16 Elem ) {
+
+ //gdcmHeaderEntry* HeaderEntry = PubHeaderEntrySet.GetElementByNumber(Group, Elem);
+ std::string v = Value;
+ PubEntrySet.SetEntryByNumber(v, Group, Elem);
+ return true;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Swaps back the bytes of 4-byte long integer accordingly to
+ * processor order.
+ * @return The properly swaped 32 bits integer.
+ */
+guint32 gdcmHeader::SwapLong(guint32 a) {
+ switch (sw) {
+ case 0 :
+ break;
+ case 4321 :
+ a=( ((a<<24) & 0xff000000) | ((a<<8) & 0x00ff0000) |
+ ((a>>8) & 0x0000ff00) | ((a>>24) & 0x000000ff) );
+ break;
+
+ case 3412 :
+ a=( ((a<<16) & 0xffff0000) | ((a>>16) & 0x0000ffff) );
+ break;
+
+ case 2143 :
+ a=( ((a<<8) & 0xff00ff00) | ((a>>8) & 0x00ff00ff) );
+ break;
+ default :
+ dbg.Error(" gdcmHeader::SwapLong : unset swap code");
+ a=0;
+ }
+ return(a);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Swaps the bytes so they agree with the processor order
+ * @return The properly swaped 16 bits integer.
+ */
+guint16 gdcmHeader::SwapShort(guint16 a) {
+ if ( (sw==4321) || (sw==2143) )
+ a =(((a<<8) & 0x0ff00) | ((a>>8)&0x00ff));
+ return (a);
+}
+
+//-----------------------------------------------------------------------------
+// Protected
+/**
+ * \ingroup gdcmHeader
+ * \brief
+ *
+ * @return
+ */
+gdcmHeaderEntry *gdcmHeader::GetHeaderEntryByNumber(guint16 Group, guint16 Elem) {
+ gdcmHeaderEntry *HeaderEntry = PubEntrySet.GetHeaderEntryByNumber(Group, Elem);
+ if (!HeaderEntry) {
+ dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
+ "failed to Locate gdcmHeaderEntry");
+ return NULL;
+ }
+ return HeaderEntry;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Searches within the public dictionary for a Dicom Element of
+ * a given tag.
+ * @param tagName name of the searched Dicom Element.
+ * @return Corresponding Dicom Element when it exists, and NULL
+ * otherwise.
+ */
+ gdcmHeaderEntry *gdcmHeader::GetHeaderEntryByName(std::string tagName) {
+ gdcmDictEntry *dictEntry = RefPubDict->GetTagByName(tagName);
+ if( dictEntry == NULL)
+ return NULL;
+
+ return(GetHeaderEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()));
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Checks if a given HeaderEntry (group,number)
+ * \ exists in the Public HeaderEntrySet
+ * @param Group
+ * @param Elem
+ * @return integer acts as a boolean
+ */
+bool gdcmHeader::CheckIfExistByNumber(guint16 Group, guint16 Elem ) {
+ return (PubEntrySet.CheckIfExistByNumber(Group, Elem)>0);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Gets (from Header) the offset of a 'non string' element value
+ * \ (LoadElementValue has already be executed)
+ * @param Group
+ * @param Elem
+ * @return File Offset of the Element Value
+ */
+size_t gdcmHeader::GetPubEntryOffsetByNumber(guint16 Group, guint16 Elem) {
+ gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
+ if (!Entry) {
+ dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
+ "failed to Locate gdcmHeaderEntry");
+ return (size_t)0;
+ }
+ return Entry->GetOffset();
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Gets (from Header) a 'non string' element value
+ * \ (LoadElementValue has already be executed)
+ * @param Group
+ * @param Elem
+ * @return Pointer to the 'non string' area
+ */
+void * gdcmHeader::GetPubEntryVoidAreaByNumber(guint16 Group, guint16 Elem) {
+ gdcmHeaderEntry* Entry = GetHeaderEntryByNumber(Group, Elem);
+ if (!Entry) {
+ dbg.Verbose(1, "gdcmHeader::GetHeaderEntryByNumber",
+ "failed to Locate gdcmHeaderEntry");
+ return (NULL);
+ }
+ return Entry->GetVoidArea();
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Loads (from disk) the element content
+ * when a string is not suitable
+ */
+void * gdcmHeader::LoadEntryVoidArea(guint16 Group, guint16 Elem) {
+ gdcmHeaderEntry * Element= GetHeaderEntryByNumber(Group, Elem);
+ if ( !Element )
+ return NULL;
+ size_t o =(size_t)Element->GetOffset();
+ fseek(fp, o, SEEK_SET);
+ int l=Element->GetLength();
+ void * a = malloc(l);
+ if(!a) {
+ return NULL;
+ }
+
+ PubEntrySet.SetVoidAreaByNumber(a, Group, Elem);
+ // TODO check the result
+ size_t l2 = fread(a, 1, l ,fp);
+ if(l != l2) {
+ free(a);
+ return NULL;
+ }
+ return a;
+}
+
+//-----------------------------------------------------------------------------
+// Private
+/**
+ * \ingroup gdcmHeader
+ * \brief Loads the element values of all the elements present in the
+ * public tag based hash table.
+ */
+void gdcmHeader::LoadHeaderEntries(void) {
+ rewind(fp);
+
+ // We don't use any longer the HashTable, since a lot a stuff is missing
+ // when SeQuences were encountered
+ //
+ //TagHeaderEntryHT ht = PubHeaderEntrySet.GetTagHT();
+ //for (TagHeaderEntryHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) {
+ // LoadElementValue(tag->second);
+ //}
+
+ for (ListTag::iterator i = GetPubListEntry().begin();
+ i != GetPubListEntry().end();
+ ++i){
+ LoadHeaderEntry(*i);
+ }
+
+ rewind(fp);
+
+ // Load 'non string' values
+ std::string PhotometricInterpretation = GetPubEntryByNumber(0x0028,0x0004);
+ if( PhotometricInterpretation == "PALETTE COLOR " ){
+ LoadEntryVoidArea(0x0028,0x1200); // gray LUT
+ LoadEntryVoidArea(0x0028,0x1201); // R LUT
+ LoadEntryVoidArea(0x0028,0x1202); // G LUT
+ LoadEntryVoidArea(0x0028,0x1203); // B LUT
+
+ LoadEntryVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data
+ LoadEntryVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data
+ LoadEntryVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data
+ }
+
+ // --------------------------------------------------------------
+ // Special Patch to allow gdcm to read ACR-LibIDO formated images
+ //
+ // if recognition code tells us we deal with a LibIDO image
+ // we switch lineNumber and columnNumber
+ //
+ std::string RecCode;
+ RecCode = GetPubEntryByNumber(0x0008, 0x0010);
+ if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
+ RecCode == "CANRME_AILIBOD1_1." ) {
+ filetype = ACR_LIBIDO;
+ std::string rows = GetPubEntryByNumber(0x0028, 0x0010);
+ std::string columns = GetPubEntryByNumber(0x0028, 0x0011);
+ SetPubEntryByNumber(columns, 0x0028, 0x0010);
+ SetPubEntryByNumber(rows , 0x0028, 0x0011);
+ }
+ // ----------------- End of Special Patch ----------------
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Loads the element content if it's length is not bigger
+ * than the value specified with
+ * gdcmHeader::SetMaxSizeLoadElementValue()
+ * @param ElVal string value of the Dicom Element
+ */
+void gdcmHeader::LoadHeaderEntry(gdcmHeaderEntry * ElVal) {
+ size_t item_read;
+ guint16 group = ElVal->GetGroup();
+ std::string vr= ElVal->GetVR();
+ guint32 length = ElVal->GetLength();
+ bool SkipLoad = false;
+
+ fseek(fp, (long)ElVal->GetOffset(), SEEK_SET);
+
+ // the test was commented out to 'go inside' the SeQuences
+ // we don't any longer skip them !
+
+ // if( vr == "SQ" )
+ // SkipLoad = true;
+
+ // A SeQuence "contains" a set of Elements.
+ // (fffe e000) tells us an Element is beginning
+ // (fffe e00d) tells us an Element just ended
+ // (fffe e0dd) tells us the current SeQuence just ended
+ if( group == 0xfffe )
+ SkipLoad = true;
+
+ if ( SkipLoad ) {
+ ElVal->SetLength(0);
+ ElVal->SetValue("gdcm::Skipped");
+ return;
+ }
+
+ // When the length is zero things are easy:
+ if ( length == 0 ) {
+ ElVal->SetValue("");
+ return;
+ }
+
+ // The elements whose length is bigger than the specified upper bound
+ // are not loaded. Instead we leave a short notice of the offset of
+ // the element content and it's length.
+ if (length > MaxSizeLoadElementValue) {
+ std::ostringstream s;
+ s << "gdcm::NotLoaded.";
+ s << " Address:" << (long)ElVal->GetOffset();
+ s << " Length:" << ElVal->GetLength();
+ s << " x(" << std::hex << ElVal->GetLength() << ")";
+ ElVal->SetValue(s.str());
+ return;
+ }
+
+ // When an integer is expected, read and convert the following two or
+ // four bytes properly i.e. as an integer as opposed to a string.
+
+ // Actually, elements with Value Multiplicity > 1
+ // contain a set of integers (not a single one)
+ // Any compacter code suggested (?)
+ if ( IsHeaderEntryAnInteger(ElVal) ) {
+ guint32 NewInt;
+ std::ostringstream s;
+ int nbInt;
+ if (vr == "US" || vr == "SS") {
+ nbInt = length / 2;
+ NewInt = ReadInt16();
+ s << NewInt;
+ if (nbInt > 1) {
+ for (int i=1; i < nbInt; i++) {
+ s << '\\';
+ NewInt = ReadInt16();
+ s << NewInt;
+ }
+ }
+
+ } else if (vr == "UL" || vr == "SL") {
+ nbInt = length / 4;
+ NewInt = ReadInt32();
+ s << NewInt;
+ if (nbInt > 1) {
+ for (int i=1; i < nbInt; i++) {
+ s << '\\';
+ NewInt = ReadInt32();
+ s << NewInt;
+ }
+ }
+ }
+#ifdef GDCM_NO_ANSI_STRING_STREAM
+ s << std::ends; // to avoid oddities on Solaris
+#endif //GDCM_NO_ANSI_STRING_STREAM
+ ElVal->SetValue(s.str());
+ return;
+ }
+
+ // We need an additional byte for storing \0 that is not on disk
+ char* NewValue = (char*)malloc(length+1);
+ if( !NewValue) {
+ dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue");
+ return;
+ }
+ NewValue[length]= 0;
+
+ item_read = fread(NewValue, (size_t)length, (size_t)1, fp);
+ if ( item_read != 1 ) {
+ free(NewValue);
+ dbg.Verbose(1, "gdcmHeader::LoadElementValue","unread element value");
+ ElVal->SetValue("gdcm::UnRead");
+ return;
+ }
+ ElVal->SetValue(NewValue);
+ free(NewValue);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Loads the element while preserving the current
+ * underlying file position indicator as opposed to
+ * to LoadElementValue that modifies it.
+ * @param ElVal Element whose value shall be loaded.
+ * @return
+ */
+void gdcmHeader::LoadHeaderEntrySafe(gdcmHeaderEntry * ElVal) {
+ long PositionOnEntry = ftell(fp);
+ LoadHeaderEntry(ElVal);
+ fseek(fp, PositionOnEntry, SEEK_SET);
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief
+ *
+ * @return
+ */
+ void gdcmHeader::FindHeaderEntryLength (gdcmHeaderEntry * ElVal) {
+ guint16 element = ElVal->GetElement();
+ guint16 group = ElVal->GetGroup();
+ std::string vr = ElVal->GetVR();
+ guint16 length16;
+ if( (element == 0x0010) && (group == 0x7fe0) ) {
+ dbg.SetDebug(-1);
+ dbg.Verbose(2, "gdcmHeader::FindLength: ",
+ "we reached 7fe0 0010");
+ }
+
+ if ( (filetype == ExplicitVR) && ! ElVal->IsImplicitVr() ) {
+ if ( (vr=="OB") || (vr=="OW") || (vr=="SQ") || (vr=="UN") ) {
+
+ // The following reserved two bytes (see PS 3.5-2001, section
+ // 7.1.2 Data element structure with explicit vr p27) must be
+ // skipped before proceeding on reading the length on 4 bytes.
+ fseek(fp, 2L, SEEK_CUR);
+
+ guint32 length32 = ReadInt32();
+
+ if ( (vr == "OB") && (length32 == 0xffffffff) ) {
+ ElVal->SetLength(FindHeaderEntryLengthOB());
+ return;
+ }
+ FixHeaderEntryFoundLength(ElVal, length32);
+ return;
+ }
+
+ // Length is encoded on 2 bytes.
+ length16 = ReadInt16();
+
+ // We can tell the current file is encoded in big endian (like
+ // Data/US-RGB-8-epicard) when we find the "Transfer Syntax" tag
+ // and it's value is the one of the encoding of a big endian file.
+ // In order to deal with such big endian encoded files, we have
+ // (at least) two strategies:
+ // * when we load the "Transfer Syntax" tag with value of big endian
+ // encoding, we raise the proper flags. Then we wait for the end
+ // of the META group (0x0002) among which is "Transfer Syntax",
+ // before switching the swap code to big endian. We have to postpone
+ // the switching of the swap code since the META group is fully encoded
+ // in little endian, and big endian coding only starts at the next
+ // group. The corresponding code can be hard to analyse and adds
+ // many additional unnecessary tests for regular tags.
+ // * the second strategy consists in waiting for trouble, that shall
+ // appear when we find the first group with big endian encoding. This
+ // is easy to detect since the length of a "Group Length" tag (the
+ // ones with zero as element number) has to be of 4 (0x0004). When we
+ // encounter 1024 (0x0400) chances are the encoding changed and we
+ // found a group with big endian encoding.
+ // We shall use this second strategy. In order to make sure that we
+ // can interpret the presence of an apparently big endian encoded
+ // length of a "Group Length" without committing a big mistake, we
+ // add an additional check: we look in the already parsed elements
+ // for the presence of a "Transfer Syntax" whose value has to be "big
+ // endian encoding". When this is the case, chances are we have got our
+ // hands on a big endian encoded file: we switch the swap code to
+ // big endian and proceed...
+ if ( (element == 0x0000) && (length16 == 0x0400) ) {
+ if ( ! IsExplicitVRBigEndianTransferSyntax() ) {
+ dbg.Verbose(0, "gdcmHeader::FindLength", "not explicit VR");
+ errno = 1;
+ return;
+ }
+ length16 = 4;
+ SwitchSwapToBigEndian();
+ // Restore the unproperly loaded values i.e. the group, the element
+ // and the dictionary entry depending on them.
+ guint16 CorrectGroup = SwapShort(ElVal->GetGroup());
+ guint16 CorrectElem = SwapShort(ElVal->GetElement());
+ gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup,
+ CorrectElem);
+ if (!NewTag) {
+ // This correct tag is not in the dictionary. Create a new one.
+ NewTag = new gdcmDictEntry(CorrectGroup, CorrectElem);
+ }
+ // FIXME this can create a memory leaks on the old entry that be
+ // left unreferenced.
+ ElVal->SetDictEntry(NewTag);
+ }
+
+ // Heuristic: well some files are really ill-formed.
+ if ( length16 == 0xffff) {
+ length16 = 0;
+ //dbg.Verbose(0, "gdcmHeader::FindLength",
+ // "Erroneous element length fixed.");
+ // Actually, length= 0xffff means that we deal with
+ // Unknown Sequence Length
+ }
+
+ FixHeaderEntryFoundLength(ElVal, (guint32)length16);
+ return;
+ }
+
+ // Either implicit VR or a non DICOM conformal (see not below) explicit
+ // VR that ommited the VR of (at least) this element. Farts happen.
+ // [Note: according to the part 5, PS 3.5-2001, section 7.1 p25
+ // on Data elements "Implicit and Explicit VR Data Elements shall
+ // not coexist in a Data Set and Data Sets nested within it".]
+ // Length is on 4 bytes.
+ FixHeaderEntryFoundLength(ElVal, ReadInt32());
+ return;
+}
+
+/**
+ * \ingroup gdcmHeader
+ * \brief Find the value representation of the current tag.
+ * @param ElVal
+ */
+void gdcmHeader::FindHeaderEntryVR( gdcmHeaderEntry *ElVal) {
+ if (filetype != ExplicitVR)
+ return;
+
+ char VR[3];
+ std::string vr;
+ int lgrLue;
+ char msg[100]; // for sprintf. Sorry
+
+ long PositionOnEntry = ftell(fp);
+ // Warning: we believe this is explicit VR (Value Representation) because
+ // we used a heuristic that found "UL" in the first tag. Alas this
+ // doesn't guarantee that all the tags will be in explicit VR. In some
+ // cases (see e-film filtered files) one finds implicit VR tags mixed
+ // within an explicit VR file. Hence we make sure the present tag
+ // is in explicit VR and try to fix things if it happens not to be
+ // the case.
+ bool RealExplicit = true;
+
+ lgrLue=fread (&VR, (size_t)2,(size_t)1, fp);
+ VR[2]=0;
+ vr = std::string(VR);
+
+ // Assume we are reading a falsely explicit VR file i.e. we reached
+ // a tag where we expect reading a VR but are in fact we read the
+ // first to bytes of the length. Then we will interogate (through find)
+ // the dicom_vr dictionary with oddities like "\004\0" which crashes
+ // both GCC and VC++ implementations of the STL map. Hence when the
+ // expected VR read happens to be non-ascii characters we consider
+ // we hit falsely explicit VR tag.
+
+ if ( (!isalpha(VR[0])) && (!isalpha(VR[1])) )
+ RealExplicit = false;
+
+ // CLEANME searching the dicom_vr at each occurence is expensive.
+ // PostPone this test in an optional integrity check at the end
+ // of parsing or only in debug mode.
+ if ( RealExplicit && !dicom_vr->Count(vr) )
+ RealExplicit= false;
+
+ if ( RealExplicit ) {
+ if ( ElVal->IsVRUnknown() ) {
+ // When not a dictionary entry, we can safely overwrite the vr.
+ ElVal->SetVR(vr);
+ return;
+ }
+ if ( ElVal->GetVR() == vr ) {
+ // The vr we just read and the dictionary agree. Nothing to do.
+ return;
+ }
+ // The vr present in the file and the dictionary disagree. We assume
+ // the file writer knew best and use the vr of the file. Since it would
+ // be unwise to overwrite the vr of a dictionary (since it would
+ // compromise it's next user), we need to clone the actual DictEntry
+ // and change the vr for the read one.
+ gdcmDictEntry* NewTag = new gdcmDictEntry(ElVal->GetGroup(),
+ ElVal->GetElement(),
+ vr,
+ "FIXME",
+ ElVal->GetName());
+ ElVal->SetDictEntry(NewTag);
+ return;
+ }
+
+ // We thought this was explicit VR, but we end up with an
+ // implicit VR tag. Let's backtrack.
+
+ sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n",
+ ElVal->GetGroup(),ElVal->GetElement());
+ dbg.Verbose(1, "gdcmHeader::FindVR: ",msg);
+
+ fseek(fp, PositionOnEntry, SEEK_SET);
+ // When this element is known in the dictionary we shall use, e.g. for
+ // the semantics (see the usage of IsAnInteger), the vr proposed by the
+ // dictionary entry. Still we have to flag the element as implicit since
+ // we know now our assumption on expliciteness is not furfilled.
+ // avoid .
+ if ( ElVal->IsVRUnknown() )
+ ElVal->SetVR("Implicit");
+ ElVal->SetImplicitVr();