]> Creatis software - gdcm.git/blob - src/gdcmDocEntrySet.cxx
The shows goes on
[gdcm.git] / src / gdcmDocEntrySet.cxx
1 // gdcmDocEntrySet.cxx
2 //-----------------------------------------------------------------------------
3 //
4 #include "gdcmDocEntrySet.h"
5 #include "gdcmTS.h"
6 #include "gdcmGlobal.h"
7 #include "gdcmUtil.h"
8
9 #include <iomanip> // for std::ios::left, ...
10
11
12 //-----------------------------------------------------------------------------
13 // Constructor / Destructor
14 /**
15  * \ingroup gdcmDocEntrySEt
16  * \brief   Constructor from a given gdcmDocEntrySet
17  */
18 gdcmDocEntrySet::gdcmDocEntrySet() {
19
20 }
21
22 //-----------------------------------------------------------------------------
23 // Print
24 /*
25  * \ingroup gdcmDocEntrySet
26  * \brief   canonical Printer
27  */
28
29
30 //-----------------------------------------------------------------------------
31 // Public
32
33
34 //-----------------------------------------------------------------------------
35 // Protected
36
37 //-----------------------------------------------------------------------------
38 // Private
39
40 /**
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 
44  */
45 bool gdcmDocument::LoadDocEntrySet(bool exception_on_error) throw(gdcmFormatError) {
46    (void)exception_on_error;
47    rewind(fp);
48    if (!CheckSwap())
49       return false;
50
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;
56
57    while (newDocEntry = ReadNextDocEntry())) { 
58    // TODO (?) : liberation du DocEntry ainsi cree, 
59    // apres copie dans un ValEntry, SeqEntry, BinEntry   
60       vr = newDocEntry->getVR();
61          
62       if (vr == "SQ" ) {
63       // --- SeqEntry
64       
65          newSeqEntry = (gdcmSeqEntry *)0;
66          if (!NewSeqEntry) {
67             dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
68                            "failed to allocate gdcmSeqEntry");
69             return false;                                
70          }       
71          newSeqEntry->Copy(newDocEntry);            
72       // TODO
73       // SEQUENCE; appel 'récursif' de ??? pour charger la 'valeur'
74       //           (ensemble d' ITEMs, en fait, 
75       //            chaque ITEM etant chargé avec LoadDocEntrySet)
76             
77          SkipDocEntry(newSeqEntry); // voir ce qu'on fait pour une SeQuence
78          AddDocEntry(newSeqEntry); 
79          
80       } else  if (vr == "AE" || vr == "AS" || vr == "DA" || vr == "PN" || 
81                   vr == "UI" || vr == "TM" ) {
82       // --- ValEntry             
83                   
84          newValEntry = (gdcmValEntry *)0;
85          if (!NewValEntry) {
86             dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
87                            "failed to allocate gdcmValEntry");
88             return false;                                
89          }       
90          newValEntry->Copy(newDocEntry);
91          SkipDocEntry(newValEntry); 
92          AddDocEntry(newValEntry); 
93                                                   
94       } else {
95       // --- BinEntry
96       
97          NewBinEntry = new gdcmBinEntry(DictEntry);     
98          if (!NewValEntry) {
99             dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
100                            "failed to allocate gdcmBinEntry");
101             return false;                                
102          }      
103          newBinEntry->Copy(newDocEntry);
104          SkipDocEntry(newBinEntry); 
105          AddDocEntry(newBinEntry);            
106       }                      
107    }   
108    rewind(fp);
109
110
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();                                                
116         ++i)                                                                      
117    {                                                                            
118       LoadDocEntry(*i);                                                      
119    }                                                                            
120    rewind(fp);
121     
122    // --------------------------------------------------------------
123    // Special Patch to allow gdcm to read ACR-LibIDO formated images
124    //
125    // if recognition code tells us we deal with a LibIDO image
126    // we switch lineNumber and columnNumber
127    //
128    std::string RecCode;
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"
133    {
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);
139    }
140    // ----------------- End of Special Patch ----------------   
141    return true;
142 }
143
144
145
146 /**
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
154 */
155
156 // NE MARCHE PAS EN L'ETAT :
157 // On a besoin de VR pour 'fabriquer', au choix ValEntry, BinEntry, ou SeqEntry.
158 //
159
160 bool gdcmDocEntrySet::CheckEntryVR(gdcmHeaderEntry *Entry, VRKey vr)
161 {
162    char msg[100]; // for sprintf
163    bool RealExplicit = true;
164
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.
172
173    if ( (!isalpha(vr[0])) && (!isalpha(vr[1])) )
174       RealExplicit = false;
175
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) )
180       RealExplicit= false;
181
182    if ( !RealExplicit ) 
183    {
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);     
194       }
195       return(false);
196    }
197
198    if ( Entry->IsVRUnknown() ) 
199    {
200       // When not a dictionary entry, we can safely overwrite the VR.
201       if (Entry->GetElement() == 0x0000) { // Group length is UL !
202          Entry->SetVR("UL");
203       } else {
204          Entry->SetVR(vr);
205       }
206    }
207    else if ( Entry->GetVR() != vr ) 
208    {
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);
218    }
219    return(true); 
220 }
221
222
223
224
225 //-----------------------------------------------------------------------------