]> Creatis software - gdcm.git/blob - src/gdcmDocEntrySet.cxx
forget this one
[gdcm.git] / src / gdcmDocEntrySet.cxx
1 // gdcmDocEntrySet.cxx
2 //-----------------------------------------------------------------------------
3 //
4 #include "gdcmDocEntrySet.h"
5 #include "gdcmException.h"
6
7 //-----------------------------------------------------------------------------
8 // Constructor / Destructor
9 /**
10  * \ingroup gdcmDocEntrySEt
11  * \brief   Constructor from a given gdcmDocEntrySet
12  */
13 gdcmDocEntrySet::gdcmDocEntrySet() {
14 }
15 /**
16  * \brief   Canonical destructor.
17  */
18 gdcmDocEntrySet::~gdcmDocEntrySet(){
19 }
20 //-----------------------------------------------------------------------------
21 // Print
22 /*
23  * \ingroup gdcmDocEntrySet
24  * \brief   canonical Printer
25  */
26
27
28 //-----------------------------------------------------------------------------
29 // Public
30
31
32 //-----------------------------------------------------------------------------
33 // Protected
34
35 //-----------------------------------------------------------------------------
36 // Private
37
38 /**
39  * \brief   Parses an EntrySet (Document header or SQ Item )
40  * \       and load element values (a voir !)
41  * @return  false anything wrong happens 
42  */
43 bool gdcmDocEntrySet::LoadDocEntrySet(bool exception_on_error) 
44                    throw(gdcmFormatError) {
45    (void)exception_on_error;
46
47    gdcmDocEntry  *newDocEntry = (gdcmDocEntry *)0;     
48    gdcmValEntry  *newValEntry = (gdcmValEntry *)0; 
49    gdcmBinEntry  *newBinEntry = (gdcmBinEntry *)0; 
50    gdcmSeqEntry  *newSeqEntry = (gdcmSeqEntry *)0;  
51    //gdcmDictEntry *NewTag      = (gdcmDictEntry *)0;
52
53    while (newDocEntry = ReadNextDocEntry())) { 
54    // TODO (?) : liberation du DocEntry ainsi cree, 
55    // apres copie dans un ValEntry, SeqEntry, BinEntry   
56       vr = newDocEntry->getVR();
57          
58       if (vr == "SQ" ) {
59       // --- SeqEntry
60       
61          newSeqEntry = (gdcmSeqEntry *)0;
62          if (!NewSeqEntry) {
63             dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
64                            "failed to allocate gdcmSeqEntry");
65             return false;                                
66          }       
67          newSeqEntry->Copy(newDocEntry);            
68       // TODO
69       // SEQUENCE; appel 'récursif' de ??? pour charger la 'valeur'
70       //           (ensemble d' ITEMs, en fait, 
71       //            chaque ITEM etant chargé avec LoadDocEntrySet)
72             
73          SkipDocEntry(newSeqEntry); // voir ce qu'on fait pour une SeQuence
74          AddDocEntry(newSeqEntry); 
75          
76       } else  if (vr == "AE" || vr == "AS" || vr == "DA" || vr == "PN" || 
77                   vr == "UI" || vr == "TM" ) {
78       // --- ValEntry             
79                   
80          newValEntry = (gdcmValEntry *)0;
81          if (!NewValEntry) {
82             dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
83                            "failed to allocate gdcmValEntry");
84             return false;                                
85          }       
86          newValEntry->Copy(newDocEntry);
87          SkipDocEntry(newValEntry); 
88          AddDocEntry(newValEntry); 
89                                                   
90       } else {
91       // --- BinEntry
92       
93          NewBinEntry = new gdcmBinEntry(DictEntry);     
94          if (!NewValEntry) {
95             dbg.Verbose(1, "gdcmDocEntrySet::LoadDocEntrySet",
96                            "failed to allocate gdcmBinEntry");
97             return false;                                
98          }      
99          newBinEntry->Copy(newDocEntry);
100          SkipDocEntry(newBinEntry); 
101          AddDocEntry(newBinEntry);            
102       }                      
103    }   
104    rewind(fp);
105
106
107    // TODO : il n'y a plus de Chained List qui contient toutes les Entries 
108    //        Le chargement des valeurs devra se faire à la volée  
109    // Be carefull : merging this two loops may cause troubles ...
110    for (ListTag::iterator i = GetListEntry().begin();                           
111         i != GetListEntry().end();                                                
112         ++i)                                                                      
113    {                                                                            
114       LoadDocEntry(*i);                                                      
115    }                                                                            
116    rewind(fp);
117     
118    // --------------------------------------------------------------
119    // Special Patch to allow gdcm to read ACR-LibIDO formated images
120    //
121    // if recognition code tells us we deal with a LibIDO image
122    // we switch lineNumber and columnNumber
123    //
124    std::string RecCode;
125    RecCode = GetEntryByNumber(0x0008, 0x0010); // recognition code
126    if (RecCode == "ACRNEMA_LIBIDO_1.1" ||
127        RecCode == "CANRME_AILIBOD1_1." )  // for brain-damaged softwares
128                                           // with "little-endian strings"
129    {
130          filetype = ACR_LIBIDO; 
131          std::string rows    = GetEntryByNumber(0x0028, 0x0010);
132          std::string columns = GetEntryByNumber(0x0028, 0x0011);
133          SetEntryByNumber(columns, 0x0028, 0x0010);
134          SetEntryByNumber(rows   , 0x0028, 0x0011);
135    }
136    // ----------------- End of Special Patch ----------------   
137    return true;
138 }
139
140
141
142 /**
143  * \brief     Check the correspondance between the VR of the header entry
144  *            and the taken VR. If they are different, the header entry is 
145  *            updated with the new VR.
146  * @param     Entry Header Entry to check
147  * @param     vr    Dicom Value Representation
148  * @return    false if the VR is incorrect of if the VR isn't referenced
149  *            otherwise, it returns true
150 */
151
152 // NE MARCHE PAS EN L'ETAT :
153 // On a besoin de VR pour 'fabriquer', au choix ValEntry, BinEntry, ou SeqEntry.
154 //
155
156 bool gdcmDocEntrySet::CheckEntryVR(gdcmHeaderEntry *Entry, VRKey vr)
157 {
158    char msg[100]; // for sprintf
159    bool RealExplicit = true;
160
161    // Assume we are reading a falsely explicit VR file i.e. we reached
162    // a tag where we expect reading a VR but are in fact we read the
163    // first to bytes of the length. Then we will interogate (through find)
164    // the dicom_vr dictionary with oddities like "\004\0" which crashes
165    // both GCC and VC++ implementations of the STL map. Hence when the
166    // expected VR read happens to be non-ascii characters we consider
167    // we hit falsely explicit VR tag.
168
169    if ( (!isalpha(vr[0])) && (!isalpha(vr[1])) )
170       RealExplicit = false;
171
172    // CLEANME searching the dicom_vr at each occurence is expensive.
173    // PostPone this test in an optional integrity check at the end
174    // of parsing or only in debug mode.
175    if ( RealExplicit && !gdcmGlobal::GetVR()->Count(vr) )
176       RealExplicit= false;
177
178    if ( !RealExplicit ) 
179    {
180       // We thought this was explicit VR, but we end up with an
181       // implicit VR tag. Let's backtrack.   
182       sprintf(msg,"Falsely explicit vr file (%04x,%04x)\n", 
183                    Entry->GetGroup(),Entry->GetElement());
184       dbg.Verbose(1, "gdcmParser::FindVR: ",msg);
185       if (Entry->GetGroup()%2 && Entry->GetElement() == 0x0000) { // Group length is UL !
186          gdcmDictEntry* NewEntry = NewVirtualDictEntry(
187                                    Entry->GetGroup(),Entry->GetElement(),
188                                    "UL","FIXME","Group Length");
189          Entry->SetDictEntry(NewEntry);     
190       }
191       return(false);
192    }
193
194    if ( Entry->IsVRUnknown() ) 
195    {
196       // When not a dictionary entry, we can safely overwrite the VR.
197       if (Entry->GetElement() == 0x0000) { // Group length is UL !
198          Entry->SetVR("UL");
199       } else {
200          Entry->SetVR(vr);
201       }
202    }
203    else if ( Entry->GetVR() != vr ) 
204    {
205       // The VR present in the file and the dictionary disagree. We assume
206       // the file writer knew best and use the VR of the file. Since it would
207       // be unwise to overwrite the VR of a dictionary (since it would
208       // compromise it's next user), we need to clone the actual DictEntry
209       // and change the VR for the read one.
210       gdcmDictEntry* NewEntry = NewVirtualDictEntry(
211                                  Entry->GetGroup(),Entry->GetElement(),
212                                  vr,"FIXME",Entry->GetName());
213       Entry->SetDictEntry(NewEntry);
214    }
215    return(true); 
216 }
217
218
219
220
221 //-----------------------------------------------------------------------------