comparison OrthancServer/FromDcmtkBridge.cpp @ 1656:d3ba98d6b6e9

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 29 Sep 2015 15:13:34 +0200
parents e40fd0d925c5
children 5360cdba70d8
comparison
equal deleted inserted replaced
1655:e40fd0d925c5 1656:d3ba98d6b6e9
53 53
54 #include <list> 54 #include <list>
55 #include <limits> 55 #include <limits>
56 56
57 #include <boost/lexical_cast.hpp> 57 #include <boost/lexical_cast.hpp>
58 #include <boost/filesystem.hpp>
58 59
59 #include <dcmtk/dcmdata/dcchrstr.h> 60 #include <dcmtk/dcmdata/dcchrstr.h>
60 #include <dcmtk/dcmdata/dcdicent.h> 61 #include <dcmtk/dcmdata/dcdicent.h>
61 #include <dcmtk/dcmdata/dcdict.h> 62 #include <dcmtk/dcmdata/dcdict.h>
62 #include <dcmtk/dcmdata/dcfilefo.h> 63 #include <dcmtk/dcmdata/dcfilefo.h>
89 #include <dcmtk/dcmdata/dcpixel.h> 90 #include <dcmtk/dcmdata/dcpixel.h>
90 #include <dcmtk/dcmdata/dcpixseq.h> 91 #include <dcmtk/dcmdata/dcpixseq.h>
91 #include <dcmtk/dcmdata/dcpxitem.h> 92 #include <dcmtk/dcmdata/dcpxitem.h>
92 #include <dcmtk/dcmdata/dcvrat.h> 93 #include <dcmtk/dcmdata/dcvrat.h>
93 94
95 #include <dcmtk/dcmnet/dul.h>
94 96
95 #include <boost/math/special_functions/round.hpp> 97 #include <boost/math/special_functions/round.hpp>
96 #include <dcmtk/dcmdata/dcostrmb.h> 98 #include <dcmtk/dcmdata/dcostrmb.h>
97 99
98 100
114 { 116 {
115 return ((GetCharValue(c[0]) << 12) + 117 return ((GetCharValue(c[0]) << 12) +
116 (GetCharValue(c[1]) << 8) + 118 (GetCharValue(c[1]) << 8) +
117 (GetCharValue(c[2]) << 4) + 119 (GetCharValue(c[2]) << 4) +
118 GetCharValue(c[3])); 120 GetCharValue(c[3]));
121 }
122
123
124 #if DCMTK_USE_EMBEDDED_DICTIONARIES == 1
125 static void LoadEmbeddedDictionary(DcmDataDictionary& dictionary,
126 EmbeddedResources::FileResourceId resource)
127 {
128 Toolbox::TemporaryFile tmp;
129
130 FILE* fp = fopen(tmp.GetPath().c_str(), "wb");
131 fwrite(EmbeddedResources::GetFileResourceBuffer(resource),
132 EmbeddedResources::GetFileResourceSize(resource), 1, fp);
133 fclose(fp);
134
135 if (!dictionary.loadDictionary(tmp.GetPath().c_str()))
136 {
137 throw OrthancException(ErrorCode_InternalError);
138 }
139 }
140
141 #else
142 static void LoadExternalDictionary(DcmDataDictionary& dictionary,
143 const std::string& directory,
144 const std::string& filename)
145 {
146 boost::filesystem::path p = directory;
147 p = p / filename;
148
149 LOG(WARNING) << "Loading the external DICOM dictionary " << p;
150
151 if (!dictionary.loadDictionary(p.string().c_str()))
152 {
153 throw OrthancException(ErrorCode_InternalError);
154 }
155 }
156
157 #endif
158
159
160 void FromDcmtkBridge::InitializeDictionary()
161 {
162 /* Disable "gethostbyaddr" (which results in memory leaks) and use raw IP addresses */
163 dcmDisableGethostbyaddr.set(OFTrue);
164
165 dcmDataDict.clear();
166 DcmDataDictionary& d = dcmDataDict.wrlock();
167
168 #if DCMTK_USE_EMBEDDED_DICTIONARIES == 1
169 LOG(WARNING) << "Loading the embedded dictionaries";
170 /**
171 * Do not load DICONDE dictionary, it breaks the other tags. The
172 * command "strace storescu 2>&1 |grep dic" shows that DICONDE
173 * dictionary is not loaded by storescu.
174 **/
175 //LoadEmbeddedDictionary(d, EmbeddedResources::DICTIONARY_DICONDE);
176
177 LoadEmbeddedDictionary(d, EmbeddedResources::DICTIONARY_DICOM);
178 LoadEmbeddedDictionary(d, EmbeddedResources::DICTIONARY_PRIVATE);
179
180 #elif defined(__linux) || defined(__FreeBSD_kernel__)
181 std::string path = DCMTK_DICTIONARY_DIR;
182
183 const char* env = std::getenv(DCM_DICT_ENVIRONMENT_VARIABLE);
184 if (env != NULL)
185 {
186 path = std::string(env);
187 }
188
189 LoadExternalDictionary(d, path, "dicom.dic");
190 LoadExternalDictionary(d, path, "private.dic");
191
192 #else
193 #error Support your platform here
194 #endif
195
196 dcmDataDict.unlock();
197
198 /* make sure data dictionary is loaded */
199 if (!dcmDataDict.isDictionaryLoaded())
200 {
201 LOG(ERROR) << "No DICOM dictionary loaded, check environment variable: " << DCM_DICT_ENVIRONMENT_VARIABLE;
202 throw OrthancException(ErrorCode_InternalError);
203 }
204
205 {
206 // Test the dictionary with a simple DICOM tag
207 DcmTag key(0x0010, 0x1030); // This is PatientWeight
208 if (key.getEVR() != EVR_DS)
209 {
210 LOG(ERROR) << "The DICOM dictionary has not been correctly read";
211 throw OrthancException(ErrorCode_InternalError);
212 }
213 }
119 } 214 }
120 215
121 216
122 Encoding FromDcmtkBridge::DetectEncoding(DcmDataset& dataset) 217 Encoding FromDcmtkBridge::DetectEncoding(DcmDataset& dataset)
123 { 218 {