- // FIXME this can create a memory leaks on the old entry that be
- // left unreferenced.
- entry->SetDictEntry( newTag );
- }
-
- // Heuristic: well, some files are really ill-formed.
- if ( length16 == 0xffff)
- {
- // 0xffff means that we deal with 'Unknown Length' Sequence
- length16 = 0;
- }
- FixDocEntryFoundLength( entry, (uint32_t)length16 );
- return;
- }
- else
- {
- // Either implicit VR or a non DICOM conformal (see note 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.
-
- FixDocEntryFoundLength( entry, ReadInt32() );
- return;
- }
-}
-
-/**
- * \brief Find the Value Representation of the current Dicom Element.
- * @param entry
- */
-void gdcmDocument::FindDocEntryVR( gdcmDocEntry *entry )
-{
- if ( Filetype != gdcmExplicitVR )
- {
- return;
- }
-
- char vr[3];
-
- 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.
-
- fread (vr, (size_t)2,(size_t)1, Fp);
- vr[2] = 0;
-
- if( !CheckDocEntryVR(entry, vr) )
- {
- 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 ( entry->IsVRUnknown() )
- {
- entry->SetVR("Implicit");
- }
- entry->SetImplicitVR();
- }
-}
-
-/**
- * \brief Check the correspondance between the VR of the header entry
- * and the taken VR. If they are different, the header entry is
- * updated with the new VR.
- * @param entry Header Entry to check
- * @param vr Dicom Value Representation
- * @return false if the VR is incorrect of if the VR isn't referenced
- * otherwise, it returns true
-*/
-bool gdcmDocument::CheckDocEntryVR(gdcmDocEntry *entry, gdcmVRKey vr)
-{
- char msg[100]; // for sprintf
- bool realExplicit = true;
-
- // 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 && !gdcmGlobal::GetVR()->Count(vr) )
- {
- realExplicit = false;
- }
-
- if ( !realExplicit )
- {
- // 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",
- entry->GetGroup(), entry->GetElement());
- dbg.Verbose(1, "gdcmDocument::FindVR: ",msg);
-
- if( entry->GetGroup() % 2 && entry->GetElement() == 0x0000)
- {
- // Group length is UL !
- gdcmDictEntry* newEntry = NewVirtualDictEntry(
- entry->GetGroup(), entry->GetElement(),
- "UL", "FIXME", "Group Length");
- entry->SetDictEntry( newEntry );
- }
- return false;
- }
-
- if ( entry->IsVRUnknown() )
- {
- // When not a dictionary entry, we can safely overwrite the VR.
- if( entry->GetElement() == 0x0000 )
- {
- // Group length is UL !
- entry->SetVR("UL");
- }
- else
- {
- entry->SetVR(vr);
- }
- }
- else if ( entry->GetVR() != vr )
- {
- // 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* newEntry = NewVirtualDictEntry(
- entry->GetGroup(), entry->GetElement(),
- vr, "FIXME", entry->GetName());
- entry->SetDictEntry(newEntry);
- }
-
- return true;
-}
-
-/**
- * \brief Get the transformed value of the header entry. The VR value
- * is used to define the transformation to operate on the value
- * \warning NOT end user intended method !
- * @param entry entry to tranform
- * @return Transformed entry value
- */
-std::string gdcmDocument::GetDocEntryValue(gdcmDocEntry *entry)
-{
- if ( IsDocEntryAnInteger(entry) && entry->IsImplicitVR() )
- {
- std::string val = ((gdcmValEntry *)entry)->GetValue();
- std::string vr = entry->GetVR();
- uint32_t length = entry->GetLength();
- std::ostringstream s;
- int nbInt;
-
- // When short integer(s) are expected, read and convert the following
- // n * 2 bytes properly i.e. as a multivaluated strings
- // (each single value is separated fromthe next one by '\'
- // as usual for standard multivaluated filels
- // Elements with Value Multiplicity > 1
- // contain a set of short integers (not a single one)
-
- if( vr == "US" || vr == "SS" )
- {
- uint16_t newInt16;