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;
51 gdcmDocEntry *newDocEntry = (gdcmDocEntry *)0;
52 gdcmValEntry *newValEntry = (gdcmValEntry *)0;
53 gdcmBinEntry *newBinEntry = (gdcmBinEntry *)0;
54 gdcmSeqEntry *newSeqEntry = (gdcmSeqEntry *)0;
55 //gdcmDictEntry *NewTag = (gdcmDictEntry *)0;
57 while (newDocEntry = ReadNextDocEntry())) {
58 // TODO (?) : liberation du DocEntry ainsi cree,
59 // apres copie dans un ValEntry, SeqEntry, BinEntry
60 vr = newDocEntry->getVR();
65 newSeqEntry = (gdcmSeqEntry *)0;
67 dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
68 "failed to allocate gdcmSeqEntry");
71 newSeqEntry->Copy(newDocEntry);
73 // SEQUENCE; appel 'récursif' de ??? pour charger la 'valeur'
74 // (ensemble d' ITEMs, en fait,
75 // chaque ITEM etant chargé avec LoadDocEntrySet)
77 SkipDocEntry(newSeqEntry); // voir ce qu'on fait pour une SeQuence
78 AddDocEntry(newSeqEntry);
80 } else if (vr == "AE" || vr == "AS" || vr == "DA" || vr == "PN" ||
81 vr == "UI" || vr == "TM" ) {
84 newValEntry = (gdcmValEntry *)0;
86 dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
87 "failed to allocate gdcmValEntry");
90 newValEntry->Copy(newDocEntry);
91 SkipDocEntry(newValEntry);
92 AddDocEntry(newValEntry);
97 NewBinEntry = new gdcmBinEntry(DictEntry);
99 dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
100 "failed to allocate gdcmBinEntry");
103 newBinEntry->Copy(newDocEntry);
104 SkipDocEntry(newBinEntry);
105 AddDocEntry(newBinEntry);
111 // TODO : il n'y a plus de Chained List qui contient toutes les Entries
112 // Le chargement des valeurs devra se faire à la volée
113 // Be carefull : merging this two loops may cause troubles ...
114 for (ListTag::iterator i = GetListEntry().begin();
115 i != GetListEntry().end();
122 // --------------------------------------------------------------
123 // Special Patch to allow gdcm to read ACR-LibIDO formated images
125 // if recognition code tells us we deal with a LibIDO image
126 // we switch lineNumber and columnNumber
129 RecCode = GetEntryByNumber(0x0008, 0x0010); // recognition code
130 if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
131 RecCode == "CANRME_AILIBOD1_1." ) // for brain-damaged softwares
132 // with "little-endian strings"
134 filetype = ACR_LIBIDO;
135 std::string rows = GetEntryByNumber(0x0028, 0x0010);
136 std::string columns = GetEntryByNumber(0x0028, 0x0011);
137 SetEntryByNumber(columns, 0x0028, 0x0010);
138 SetEntryByNumber(rows , 0x0028, 0x0011);
140 // ----------------- End of Special Patch ----------------
147 * \brief Check the correspondance between the VR of the header entry
148 * and the taken VR. If they are different, the header entry is
149 * updated with the new VR.
150 * @param Entry Header Entry to check
151 * @param vr Dicom Value Representation
152 * @return false if the VR is incorrect of if the VR isn't referenced
153 * otherwise, it returns true
156 // NE MARCHE PAS EN L'ETAT :
157 // On a besoin de VR pour 'fabriquer', au choix ValEntry, BinEntry, ou SeqEntry.
160 bool gdcmDocEntrySet::CheckEntryVR(gdcmHeaderEntry *Entry, VRKey vr)
162 char msg[100]; // for sprintf
163 bool RealExplicit = true;
165 // Assume we are reading a falsely explicit VR file i.e. we reached
166 // a tag where we expect reading a VR but are in fact we read the
167 // first to bytes of the length. Then we will interogate (through find)
168 // the dicom_vr dictionary with oddities like "\004\0" which crashes
169 // both GCC and VC++ implementations of the STL map. Hence when the
170 // expected VR read happens to be non-ascii characters we consider
171 // we hit falsely explicit VR tag.
173 if ( (!isalpha(vr[0])) && (!isalpha(vr[1])) )
174 RealExplicit = false;
176 // CLEANME searching the dicom_vr at each occurence is expensive.
177 // PostPone this test in an optional integrity check at the end
178 // of parsing or only in debug mode.
179 if ( RealExplicit && !gdcmGlobal::GetVR()->Count(vr) )
184 // We thought this was explicit VR, but we end up with an
185 // implicit VR tag. Let's backtrack.
186 sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n",
187 Entry->GetGroup(),Entry->GetElement());
188 dbg.Verbose(1, "gdcmParser::FindVR: ",msg);
189 if (Entry->GetGroup()%2 && Entry->GetElement() == 0x0000) { // Group length is UL !
190 gdcmDictEntry* NewEntry = NewVirtualDictEntry(
191 Entry->GetGroup(),Entry->GetElement(),
192 "UL","FIXME","Group Length");
193 Entry->SetDictEntry(NewEntry);
198 if ( Entry->IsVRUnknown() )
200 // When not a dictionary entry, we can safely overwrite the VR.
201 if (Entry->GetElement() == 0x0000) { // Group length is UL !
207 else if ( Entry->GetVR() != vr )
209 // The VR present in the file and the dictionary disagree. We assume
210 // the file writer knew best and use the VR of the file. Since it would
211 // be unwise to overwrite the VR of a dictionary (since it would
212 // compromise it's next user), we need to clone the actual DictEntry
213 // and change the VR for the read one.
214 gdcmDictEntry* NewEntry = NewVirtualDictEntry(
215 Entry->GetGroup(),Entry->GetElement(),
216 vr,"FIXME",Entry->GetName());
217 Entry->SetDictEntry(NewEntry);
225 //-----------------------------------------------------------------------------