]> Creatis software - gdcm.git/blob - src/gdcmDocEntrySet.cxx
Taking into account the 'Dicom Sequences' leads up to introduce new concepts
[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    guint16 g, n;      
52    gdcmValEntry  *newValEntry = (gdcmValEntry *)0; 
53    gdcmBinEntry  *newBinEntry = (gdcmBinEntry *)0; 
54    gdcmSeqEntry  *newSeqEntry = (gdcmSeqEntry *)0;  
55    gdcmDictEntry *NewTag      = (gdcmDictEntry *)0;
56    char VR[3];
57    long PositionOnEntry;
58      
59 //   while ( (newHeaderEntry = ReadNextHeaderEntry()) ) {
60    while (1) {
61    
62    // ----------------------- was ReadNextHeaderEntry -----------------
63       g = ReadInt16();
64       n = ReadInt16();   
65       if (errno == 1)
66       // We reached the EOF (or an error occured) therefore 
67       // header parsing has to be considered as finished.
68          break;
69          
70    // Find out if the tag we encountered is in the dictionaries:
71       DictEntry = GetDictEntryByNumber(Group, Elem);
72       if (!DictEntry)
73          DictEntry = NewVirtualDictEntry(Group, Elem);
74          if (!DictEntry) {
75             dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
76                            "failed to allocate gdcmDictEntry");
77             return false;                                
78          }                         
79          
80                                // Right now,     
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
83                                // TODO
84                                // Resoudre pb quand inadequation entre Explicit VR et Dict 
85                                
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    
91                                              
92       if (vr == "SQ" ) {
93        // --- SeqEntry      
94       // TODO
95       // SEQUENCE; appel 'rĂ©cursif' de ??? pour charger la 'valeur'
96       //           (ensemble d' ITEMs, en fait, 
97       //            chaque ITEM etant chargĂ© avec LoadDocEntrySet)
98       
99       
100       } else  if (vr == "AE" || vr == "AS" || vr == "DA" || vr == "PN" || 
101           vr == "UI" || vr == "TM" ) {  
102       // --- ValEntry 
103          NewValEntry = new gdcmValEntry(DictEntry);
104          if (!NewValEntry) {
105             dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
106                            "failed to allocate gdcmValEntry");
107             return false;                                
108          }
109          FindHeaderEntryVR(NewEntry);
110          FindHeaderEntryLength(NewEntry);                   
111       } 
112       
113       
114       else {
115       // --- BinEntry
116          NewBinEntry = new gdcmBinEntry(DictEntry);     
117          if (!NewValEntry) {
118             dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
119                            "failed to allocate gdcmBinEntry");
120             return false;                                
121          }
122     }
123
124
125
126 // ------------- end of former ReadNextHeaderEntry -----------------
127                       
128      SkipHeaderEntry(newHeaderEntry);
129      if ( (ignoreShadow==0) || (newHeaderEntry->GetGroup()%2) == 0) { 
130         AddHeaderEntry(newHeaderEntry); 
131      }     
132    }   
133    rewind(fp);
134    // Be carefull : merging this two loops may cause troubles ...
135    for (ListTag::iterator i = GetListEntry().begin();                           
136         i != GetListEntry().end();                                                
137         ++i)                                                                      
138    {                                                                            
139       LoadHeaderEntry(*i);                                                      
140    }                                                                            
141    rewind(fp);
142  
143    
144    // --------------------------------------------------------------
145    // Special Patch to allow gdcm to read ACR-LibIDO formated images
146    //
147    // if recognition code tells us we deal with a LibIDO image
148    // we switch lineNumber and columnNumber
149    //
150    std::string RecCode;
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"
155    {
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);
161    }
162    // ----------------- End of Special Patch ----------------   
163    return true;
164 }
165
166
167
168 /**
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
176 */
177
178 // NE MARCHE PAS EN L'ETAT :
179 // On a besoin de VR pour 'fabriquer', au choix ValEntry, BinEntry, ou SeqEntry.
180 //
181
182 bool gdcmDocEntrySet::CheckEntryVR(gdcmHeaderEntry *Entry, VRKey vr)
183 {
184    char msg[100]; // for sprintf
185    bool RealExplicit = true;
186
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.
194
195    if ( (!isalpha(vr[0])) && (!isalpha(vr[1])) )
196       RealExplicit = false;
197
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) )
202       RealExplicit= false;
203
204    if ( !RealExplicit ) 
205    {
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);     
216       }
217       return(false);
218    }
219
220    if ( Entry->IsVRUnknown() ) 
221    {
222       // When not a dictionary entry, we can safely overwrite the VR.
223       if (Entry->GetElement() == 0x0000) { // Group length is UL !
224          Entry->SetVR("UL");
225       } else {
226          Entry->SetVR(vr);
227       }
228    }
229    else if ( Entry->GetVR() != vr ) 
230    {
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);
240    }
241    return(true); 
242 }
243
244
245
246
247 //-----------------------------------------------------------------------------