2 //-----------------------------------------------------------------------------
4 #include "gdcmDocEntrySet.h"
6 #include "gdcmGlobal.h"
9 #include <iomanip> // for std::ios::left, ...
12 //-----------------------------------------------------------------------------
13 // Constructor / Destructor
15 * \ingroup gdcmDocEntrySEt
16 * \brief Constructor from a given gdcmDocEntrySet
18 gdcmDocEntrySet::gdcmDocEntrySet() {
22 //-----------------------------------------------------------------------------
25 * \ingroup gdcmDocEntrySet
26 * \brief canonical Printer
30 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
41 * \brief Parses an EntrySet (Document header or SQ Item )
42 * \ and load element values (a voir !)
43 * @return false if file is not ACR-NEMA / PAPYRUS / DICOM
45 bool gdcmDocument::LoadDocEntrySet(bool exception_on_error) throw(gdcmFormatError) {
46 (void)exception_on_error;
52 gdcmValEntry *newValEntry = (gdcmValEntry *)0;
53 gdcmBinEntry *newBinEntry = (gdcmBinEntry *)0;
54 gdcmSeqEntry *newSeqEntry = (gdcmSeqEntry *)0;
55 gdcmDictEntry *NewTag = (gdcmDictEntry *)0;
59 // while ( (newHeaderEntry = ReadNextHeaderEntry()) ) {
62 // ----------------------- was ReadNextHeaderEntry -----------------
66 // We reached the EOF (or an error occured) therefore
67 // header parsing has to be considered as finished.
70 // Find out if the tag we encountered is in the dictionaries:
71 DictEntry = GetDictEntryByNumber(Group, Elem);
73 DictEntry = NewVirtualDictEntry(Group, Elem);
75 dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
76 "failed to allocate gdcmDictEntry");
81 vr = DictEntry->GetVR(); // Suppose we get it, suppose it's the same one
82 // that's in the Explicit VR part of the Header
84 // Resoudre pb quand inadequation entre Explicit VR et Dict
86 if (filetype != ExplicitVR) {} // jamais de risque de conflit
87 // pour les autres cas,
88 // refaire l'equiv de gdcmParser::FindHeaderEntryVR
89 // qui contient CheckHeaderEntryVR
90 // le pb, c'est qu'on a besoin de la VR pour fabriquer l'Entry
95 // SEQUENCE; appel 'récursif' de ??? pour charger la 'valeur'
96 // (ensemble d' ITEMs, en fait,
97 // chaque ITEM etant chargé avec LoadDocEntrySet)
100 } else if (vr == "AE" || vr == "AS" || vr == "DA" || vr == "PN" ||
101 vr == "UI" || vr == "TM" ) {
103 NewValEntry = new gdcmValEntry(DictEntry);
105 dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
106 "failed to allocate gdcmValEntry");
109 FindHeaderEntryVR(NewEntry);
110 FindHeaderEntryLength(NewEntry);
116 NewBinEntry = new gdcmBinEntry(DictEntry);
118 dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
119 "failed to allocate gdcmBinEntry");
126 // ------------- end of former ReadNextHeaderEntry -----------------
128 SkipHeaderEntry(newHeaderEntry);
129 if ( (ignoreShadow==0) || (newHeaderEntry->GetGroup()%2) == 0) {
130 AddHeaderEntry(newHeaderEntry);
134 // Be carefull : merging this two loops may cause troubles ...
135 for (ListTag::iterator i = GetListEntry().begin();
136 i != GetListEntry().end();
144 // --------------------------------------------------------------
145 // Special Patch to allow gdcm to read ACR-LibIDO formated images
147 // if recognition code tells us we deal with a LibIDO image
148 // we switch lineNumber and columnNumber
151 RecCode = GetEntryByNumber(0x0008, 0x0010); // recognition code
152 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
153 RecCode == "CANRME_AILIBOD1_1." ) // for brain-damaged softwares
154 // with "little-endian strings"
156 filetype = ACR_LIBIDO;
157 std::string rows = GetEntryByNumber(0x0028, 0x0010);
158 std::string columns = GetEntryByNumber(0x0028, 0x0011);
159 SetEntryByNumber(columns, 0x0028, 0x0010);
160 SetEntryByNumber(rows , 0x0028, 0x0011);
162 // ----------------- End of Special Patch ----------------
169 * \brief Check the correspondance between the VR of the header entry
170 * and the taken VR. If they are different, the header entry is
171 * updated with the new VR.
172 * @param Entry Header Entry to check
173 * @param vr Dicom Value Representation
174 * @return false if the VR is incorrect of if the VR isn't referenced
175 * otherwise, it returns true
178 // NE MARCHE PAS EN L'ETAT :
179 // On a besoin de VR pour 'fabriquer', au choix ValEntry, BinEntry, ou SeqEntry.
182 bool gdcmDocEntrySet::CheckEntryVR(gdcmHeaderEntry *Entry, VRKey vr)
184 char msg[100]; // for sprintf
185 bool RealExplicit = true;
187 // Assume we are reading a falsely explicit VR file i.e. we reached
188 // a tag where we expect reading a VR but are in fact we read the
189 // first to bytes of the length. Then we will interogate (through find)
190 // the dicom_vr dictionary with oddities like "\004\0" which crashes
191 // both GCC and VC++ implementations of the STL map. Hence when the
192 // expected VR read happens to be non-ascii characters we consider
193 // we hit falsely explicit VR tag.
195 if ( (!isalpha(vr[0])) && (!isalpha(vr[1])) )
196 RealExplicit = false;
198 // CLEANME searching the dicom_vr at each occurence is expensive.
199 // PostPone this test in an optional integrity check at the end
200 // of parsing or only in debug mode.
201 if ( RealExplicit && !gdcmGlobal::GetVR()->Count(vr) )
206 // We thought this was explicit VR, but we end up with an
207 // implicit VR tag. Let's backtrack.
208 sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n",
209 Entry->GetGroup(),Entry->GetElement());
210 dbg.Verbose(1, "gdcmParser::FindVR: ",msg);
211 if (Entry->GetGroup()%2 && Entry->GetElement() == 0x0000) { // Group length is UL !
212 gdcmDictEntry* NewEntry = NewVirtualDictEntry(
213 Entry->GetGroup(),Entry->GetElement(),
214 "UL","FIXME","Group Length");
215 Entry->SetDictEntry(NewEntry);
220 if ( Entry->IsVRUnknown() )
222 // When not a dictionary entry, we can safely overwrite the VR.
223 if (Entry->GetElement() == 0x0000) { // Group length is UL !
229 else if ( Entry->GetVR() != vr )
231 // The VR present in the file and the dictionary disagree. We assume
232 // the file writer knew best and use the VR of the file. Since it would
233 // be unwise to overwrite the VR of a dictionary (since it would
234 // compromise it's next user), we need to clone the actual DictEntry
235 // and change the VR for the read one.
236 gdcmDictEntry* NewEntry = NewVirtualDictEntry(
237 Entry->GetGroup(),Entry->GetElement(),
238 vr,"FIXME",Entry->GetName());
239 Entry->SetDictEntry(NewEntry);
247 //-----------------------------------------------------------------------------