Mercurial > hg > orthanc
annotate OrthancServer/FromDcmtkBridge.cpp @ 257:098661837a96 Orthanc-0.3.1
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sun, 09 Dec 2012 12:14:31 +0100 |
parents | 81f11fb357f2 |
children | 4031f73fe0e4 |
rev | line source |
---|---|
0 | 1 /** |
62 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
0 | 3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
136 | 10 * |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
0 | 22 * |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
71 | 32 #ifndef NOMINMAX |
33 #define NOMINMAX | |
34 #endif | |
0 | 35 |
36 #include "FromDcmtkBridge.h" | |
37 | |
38 #include "ToDcmtkBridge.h" | |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
106
diff
changeset
|
39 #include "../Core/Toolbox.h" |
62 | 40 #include "../Core/OrthancException.h" |
0 | 41 #include "../Core/PngWriter.h" |
42 #include "../Core/DicomFormat/DicomString.h" | |
43 #include "../Core/DicomFormat/DicomNullValue.h" | |
79 | 44 #include "../Core/DicomFormat/DicomIntegerPixelAccessor.h" |
0 | 45 |
71 | 46 #include <limits> |
47 | |
0 | 48 #include <boost/lexical_cast.hpp> |
49 | |
50 #include <dcmtk/dcmdata/dcdicent.h> | |
51 #include <dcmtk/dcmdata/dcdict.h> | |
52 #include <dcmtk/dcmdata/dcfilefo.h> | |
53 #include <dcmtk/dcmdata/dcistrmb.h> | |
54 #include <dcmtk/dcmdata/dcvrfd.h> | |
55 #include <dcmtk/dcmdata/dcvrfl.h> | |
56 #include <dcmtk/dcmdata/dcvrsl.h> | |
57 #include <dcmtk/dcmdata/dcvrss.h> | |
58 #include <dcmtk/dcmdata/dcvrul.h> | |
59 #include <dcmtk/dcmdata/dcvrus.h> | |
176 | 60 #include <dcmtk/dcmdata/dcuid.h> |
0 | 61 |
2 | 62 #include <boost/math/special_functions/round.hpp> |
0 | 63 |
62 | 64 namespace Orthanc |
0 | 65 { |
66 void FromDcmtkBridge::Convert(DicomMap& target, DcmDataset& dataset) | |
67 { | |
68 target.Clear(); | |
69 for (unsigned long i = 0; i < dataset.card(); i++) | |
70 { | |
71 DcmElement* element = dataset.getElement(i); | |
72 if (element && element->isLeaf()) | |
73 { | |
74 target.SetValue(element->getTag().getGTag(), | |
75 element->getTag().getETag(), | |
76 ConvertLeafElement(*element)); | |
77 } | |
78 } | |
79 } | |
80 | |
81 | |
82 DicomTag FromDcmtkBridge::GetTag(const DcmElement& element) | |
83 { | |
84 return DicomTag(element.getGTag(), element.getETag()); | |
85 } | |
86 | |
87 | |
88 DicomValue* FromDcmtkBridge::ConvertLeafElement(DcmElement& element) | |
89 { | |
90 if (!element.isLeaf()) | |
91 { | |
62 | 92 throw OrthancException("Only applicable to leaf elements"); |
0 | 93 } |
94 | |
95 if (element.isaString()) | |
96 { | |
97 char *c; | |
98 if (element.getString(c).good() && | |
99 c != NULL) | |
100 { | |
101 std::string s(c); | |
107
3b45473c0a73
replace boost::locale with iconv for debian
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
106
diff
changeset
|
102 std::string utf8 = Toolbox::ConvertToUtf8(s, "ISO-8859-1"); // TODO Parameter? |
0 | 103 return new DicomString(utf8); |
104 } | |
105 else | |
106 { | |
107 return new DicomNullValue; | |
108 } | |
109 } | |
110 | |
111 try | |
112 { | |
113 // http://support.dcmtk.org/docs/dcvr_8h-source.html | |
114 switch (element.getVR()) | |
115 { | |
116 | |
117 /** | |
118 * TODO. | |
119 **/ | |
120 | |
121 case EVR_DS: // decimal string | |
122 case EVR_IS: // integer string | |
123 case EVR_OB: // other byte | |
124 case EVR_OF: // other float | |
125 case EVR_OW: // other word | |
126 case EVR_AS: // age string | |
127 case EVR_AT: // attribute tag | |
128 case EVR_DA: // date string | |
129 case EVR_DT: // date time string | |
130 case EVR_TM: // time string | |
131 case EVR_UN: // unknown value representation | |
132 return new DicomNullValue(); | |
133 | |
134 | |
135 /** | |
136 * String types, should never happen at this point because of | |
137 * "element.isaString()". | |
138 **/ | |
139 | |
140 case EVR_AE: // application entity title | |
141 case EVR_CS: // code string | |
142 case EVR_SH: // short string | |
143 case EVR_LO: // long string | |
144 case EVR_ST: // short text | |
145 case EVR_LT: // long text | |
146 case EVR_UT: // unlimited text | |
147 case EVR_PN: // person name | |
148 case EVR_UI: // unique identifier | |
149 return new DicomNullValue(); | |
150 | |
151 | |
152 /** | |
153 * Numerical types | |
154 **/ | |
155 | |
156 case EVR_SL: // signed long | |
157 { | |
158 Sint32 f; | |
159 if (dynamic_cast<DcmSignedLong&>(element).getSint32(f).good()) | |
160 { | |
161 return new DicomString(boost::lexical_cast<std::string>(f)); | |
162 } | |
163 else | |
164 { | |
165 return new DicomNullValue(); | |
166 } | |
167 } | |
168 | |
169 case EVR_SS: // signed short | |
170 { | |
171 Sint16 f; | |
172 if (dynamic_cast<DcmSignedShort&>(element).getSint16(f).good()) | |
173 { | |
174 return new DicomString(boost::lexical_cast<std::string>(f)); | |
175 } | |
176 else | |
177 { | |
178 return new DicomNullValue(); | |
179 } | |
180 } | |
181 | |
182 case EVR_UL: // unsigned long | |
183 { | |
184 Uint32 f; | |
185 if (dynamic_cast<DcmUnsignedLong&>(element).getUint32(f).good()) | |
186 { | |
187 return new DicomString(boost::lexical_cast<std::string>(f)); | |
188 } | |
189 else | |
190 { | |
191 return new DicomNullValue(); | |
192 } | |
193 } | |
194 | |
195 case EVR_US: // unsigned short | |
196 { | |
197 Uint16 f; | |
198 if (dynamic_cast<DcmUnsignedShort&>(element).getUint16(f).good()) | |
199 { | |
200 return new DicomString(boost::lexical_cast<std::string>(f)); | |
201 } | |
202 else | |
203 { | |
204 return new DicomNullValue(); | |
205 } | |
206 } | |
207 | |
208 case EVR_FL: // float single-precision | |
209 { | |
210 Float32 f; | |
211 if (dynamic_cast<DcmFloatingPointSingle&>(element).getFloat32(f).good()) | |
212 { | |
213 return new DicomString(boost::lexical_cast<std::string>(f)); | |
214 } | |
215 else | |
216 { | |
217 return new DicomNullValue(); | |
218 } | |
219 } | |
220 | |
221 case EVR_FD: // float double-precision | |
222 { | |
223 Float64 f; | |
224 if (dynamic_cast<DcmFloatingPointDouble&>(element).getFloat64(f).good()) | |
225 { | |
226 return new DicomString(boost::lexical_cast<std::string>(f)); | |
227 } | |
228 else | |
229 { | |
230 return new DicomNullValue(); | |
231 } | |
232 } | |
233 | |
234 | |
235 /** | |
236 * Sequence types, should never occur at this point because of | |
237 * "element.isLeaf()". | |
238 **/ | |
239 | |
240 case EVR_SQ: // sequence of items | |
241 return new DicomNullValue; | |
242 | |
243 | |
244 /** | |
245 * Internal to DCMTK. | |
246 **/ | |
247 | |
248 case EVR_ox: // OB or OW depending on context | |
249 case EVR_xs: // SS or US depending on context | |
250 case EVR_lt: // US, SS or OW depending on context, used for LUT Data (thus the name) | |
251 case EVR_na: // na="not applicable", for data which has no VR | |
252 case EVR_up: // up="unsigned pointer", used internally for DICOMDIR suppor | |
253 case EVR_item: // used internally for items | |
254 case EVR_metainfo: // used internally for meta info datasets | |
255 case EVR_dataset: // used internally for datasets | |
256 case EVR_fileFormat: // used internally for DICOM files | |
257 case EVR_dicomDir: // used internally for DICOMDIR objects | |
258 case EVR_dirRecord: // used internally for DICOMDIR records | |
259 case EVR_pixelSQ: // used internally for pixel sequences in a compressed image | |
260 case EVR_pixelItem: // used internally for pixel items in a compressed image | |
261 case EVR_UNKNOWN: // used internally for elements with unknown VR (encoded with 4-byte length field in explicit VR) | |
262 case EVR_PixelData: // used internally for uncompressed pixeld data | |
263 case EVR_OverlayData: // used internally for overlay data | |
264 case EVR_UNKNOWN2B: // used internally for elements with unknown VR with 2-byte length field in explicit VR | |
265 return new DicomNullValue; | |
266 | |
267 | |
268 /** | |
269 * Default case. | |
270 **/ | |
271 | |
272 default: | |
273 return new DicomNullValue; | |
274 } | |
275 } | |
276 catch (boost::bad_lexical_cast) | |
277 { | |
278 return new DicomNullValue; | |
279 } | |
280 } | |
281 | |
282 | |
283 static void StoreElement(Json::Value& target, | |
284 DcmElement& element, | |
285 unsigned int maxStringLength); | |
286 | |
287 static void StoreItem(Json::Value& target, | |
288 DcmItem& item, | |
289 unsigned int maxStringLength) | |
290 { | |
291 target = Json::Value(Json::objectValue); | |
292 | |
293 for (unsigned long i = 0; i < item.card(); i++) | |
294 { | |
295 DcmElement* element = item.getElement(i); | |
296 StoreElement(target, *element, maxStringLength); | |
297 } | |
298 } | |
299 | |
300 | |
301 static void StoreElement(Json::Value& target, | |
302 DcmElement& element, | |
303 unsigned int maxStringLength) | |
304 { | |
305 assert(target.type() == Json::objectValue); | |
306 | |
35
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
307 DicomTag tag(FromDcmtkBridge::GetTag(element)); |
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
308 const std::string tagName = FromDcmtkBridge::GetName(tag); |
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
309 const std::string formattedTag = tag.Format(); |
0 | 310 |
311 if (element.isLeaf()) | |
312 { | |
35
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
313 Json::Value value(Json::objectValue); |
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
314 value["Name"] = tagName; |
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
315 |
0 | 316 std::auto_ptr<DicomValue> v(FromDcmtkBridge::ConvertLeafElement(element)); |
317 if (v->IsNull()) | |
318 { | |
35
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
319 value["Type"] = "Null"; |
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
320 value["Value"] = Json::nullValue; |
0 | 321 } |
322 else | |
323 { | |
324 std::string s = v->AsString(); | |
325 if (maxStringLength == 0 || | |
326 s.size() <= maxStringLength) | |
327 { | |
35
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
328 value["Type"] = "String"; |
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
329 value["Value"] = s; |
0 | 330 } |
331 else | |
332 { | |
35
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
333 value["Type"] = "TooLong"; |
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
334 value["Value"] = Json::nullValue; |
0 | 335 } |
336 } | |
35
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
337 |
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
338 target[formattedTag] = value; |
0 | 339 } |
340 else | |
341 { | |
35
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
342 Json::Value children(Json::arrayValue); |
0 | 343 |
344 // "All subclasses of DcmElement except for DcmSequenceOfItems | |
345 // are leaf nodes, while DcmSequenceOfItems, DcmItem, DcmDataset | |
346 // etc. are not." The following cast is thus OK. | |
347 DcmSequenceOfItems& sequence = dynamic_cast<DcmSequenceOfItems&>(element); | |
348 | |
349 for (unsigned long i = 0; i < sequence.card(); i++) | |
350 { | |
351 DcmItem* child = sequence.getItem(i); | |
35
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
352 Json::Value& v = children.append(Json::objectValue); |
0 | 353 StoreItem(v, *child, maxStringLength); |
354 } | |
35
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
355 |
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
356 target[formattedTag]["Name"] = tagName; |
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
357 target[formattedTag]["Type"] = "Sequence"; |
f6d12037f886
full json vs. simplified json
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2
diff
changeset
|
358 target[formattedTag]["Value"] = children; |
0 | 359 } |
360 } | |
361 | |
362 | |
363 void FromDcmtkBridge::ToJson(Json::Value& root, | |
364 DcmDataset& dataset, | |
365 unsigned int maxStringLength) | |
366 { | |
367 StoreItem(root, dataset, maxStringLength); | |
368 } | |
369 | |
370 | |
371 | |
372 void FromDcmtkBridge::ToJson(Json::Value& target, | |
373 const std::string& path, | |
374 unsigned int maxStringLength) | |
375 { | |
376 DcmFileFormat dicom; | |
377 if (!dicom.loadFile(path.c_str()).good()) | |
378 { | |
62 | 379 throw OrthancException(ErrorCode_BadFileFormat); |
0 | 380 } |
381 else | |
382 { | |
383 FromDcmtkBridge::ToJson(target, *dicom.getDataset(), maxStringLength); | |
384 } | |
385 } | |
386 | |
387 | |
42
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
388 static void ExtractPngImagePreview(std::string& result, |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
389 DicomIntegerPixelAccessor& accessor) |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
390 { |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
391 PngWriter w; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
392 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
393 int32_t min, max; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
394 accessor.GetExtremeValues(min, max); |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
395 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
396 std::vector<uint8_t> image(accessor.GetWidth() * accessor.GetHeight(), 0); |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
397 if (min != max) |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
398 { |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
399 uint8_t* pixel = &image[0]; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
400 for (unsigned int y = 0; y < accessor.GetHeight(); y++) |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
401 { |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
402 for (unsigned int x = 0; x < accessor.GetWidth(); x++, pixel++) |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
403 { |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
404 int32_t v = accessor.GetValue(x, y); |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
405 *pixel = static_cast<uint8_t>( |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
406 boost::math::lround(static_cast<float>(v - min) / |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
407 static_cast<float>(max - min) * 255.0f)); |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
408 } |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
409 } |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
410 } |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
411 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
412 w.WriteToMemory(result, accessor.GetWidth(), accessor.GetHeight(), |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
413 accessor.GetWidth(), PixelFormat_Grayscale8, &image[0]); |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
414 } |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
415 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
416 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
417 template <typename T> |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
418 static void ExtractPngImageTruncate(std::string& result, |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
419 DicomIntegerPixelAccessor& accessor, |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
420 PixelFormat format) |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
421 { |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
422 PngWriter w; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
423 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
424 std::vector<T> image(accessor.GetWidth() * accessor.GetHeight(), 0); |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
425 T* pixel = &image[0]; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
426 for (unsigned int y = 0; y < accessor.GetHeight(); y++) |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
427 { |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
428 for (unsigned int x = 0; x < accessor.GetWidth(); x++, pixel++) |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
429 { |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
430 int32_t v = accessor.GetValue(x, y); |
73 | 431 if (v < std::numeric_limits<T>::min()) |
42
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
432 *pixel = std::numeric_limits<T>::min(); |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
433 else if (v > std::numeric_limits<T>::max()) |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
434 *pixel = std::numeric_limits<T>::max(); |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
435 else |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
436 *pixel = static_cast<T>(v); |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
437 } |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
438 } |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
439 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
440 w.WriteToMemory(result, accessor.GetWidth(), accessor.GetHeight(), |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
441 accessor.GetWidth() * sizeof(T), format, &image[0]); |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
442 } |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
443 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
444 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
445 void FromDcmtkBridge::ExtractPngImage(std::string& result, |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
446 DcmDataset& dataset, |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
447 unsigned int frame, |
42
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
448 ImageExtractionMode mode) |
0 | 449 { |
450 // See also: http://support.dcmtk.org/wiki/dcmtk/howto/accessing-compressed-data | |
451 | |
452 std::auto_ptr<DicomIntegerPixelAccessor> accessor; | |
453 | |
454 DicomMap m; | |
455 FromDcmtkBridge::Convert(m, dataset); | |
456 | |
457 DcmElement* e; | |
80 | 458 if (dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_DATA), e).good() && |
0 | 459 e != NULL) |
460 { | |
461 Uint8* pixData = NULL; | |
462 if (e->getUint8Array(pixData) == EC_Normal) | |
463 { | |
464 accessor.reset(new DicomIntegerPixelAccessor(m, pixData, e->getLength())); | |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
465 accessor->SetCurrentFrame(frame); |
0 | 466 } |
467 } | |
468 | |
42
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
469 PixelFormat format; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
470 switch (mode) |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
471 { |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
472 case ImageExtractionMode_Preview: |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
473 case ImageExtractionMode_UInt8: |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
474 format = PixelFormat_Grayscale8; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
475 break; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
476 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
477 case ImageExtractionMode_UInt16: |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
478 format = PixelFormat_Grayscale16; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
479 break; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
480 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
481 default: |
62 | 482 throw OrthancException(ErrorCode_NotImplemented); |
42
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
483 } |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
484 |
0 | 485 if (accessor.get() == NULL || |
486 accessor->GetWidth() == 0 || | |
487 accessor->GetHeight() == 0) | |
488 { | |
42
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
489 PngWriter w; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
490 w.WriteToMemory(result, 0, 0, 0, format, NULL); |
0 | 491 } |
492 else | |
493 { | |
42
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
494 switch (mode) |
0 | 495 { |
42
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
496 case ImageExtractionMode_Preview: |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
497 ExtractPngImagePreview(result, *accessor); |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
498 break; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
499 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
500 case ImageExtractionMode_UInt8: |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
501 ExtractPngImageTruncate<uint8_t>(result, *accessor, format); |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
502 break; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
503 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
504 case ImageExtractionMode_UInt16: |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
505 ExtractPngImageTruncate<uint16_t>(result, *accessor, format); |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
506 break; |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
507 |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
508 default: |
62 | 509 throw OrthancException(ErrorCode_NotImplemented); |
0 | 510 } |
511 } | |
512 } | |
513 | |
514 | |
42
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
515 void FromDcmtkBridge::ExtractPngImage(std::string& result, |
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
516 const std::string& dicomContent, |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
517 unsigned int frame, |
42
ea48f38afe5f
access to raw images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
41
diff
changeset
|
518 ImageExtractionMode mode) |
0 | 519 { |
520 DcmInputBufferStream is; | |
521 if (dicomContent.size() > 0) | |
522 { | |
523 is.setBuffer(&dicomContent[0], dicomContent.size()); | |
524 } | |
525 is.setEos(); | |
526 | |
527 DcmFileFormat dicom; | |
528 if (dicom.read(is).good()) | |
529 { | |
53
293038baf8f1
access to multi-frame images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
50
diff
changeset
|
530 ExtractPngImage(result, *dicom.getDataset(), frame, mode); |
0 | 531 } |
532 else | |
533 { | |
62 | 534 throw OrthancException(ErrorCode_BadFileFormat); |
0 | 535 } |
536 } | |
537 | |
538 | |
539 | |
540 std::string FromDcmtkBridge::GetName(const DicomTag& t) | |
541 { | |
106 | 542 // Some patches for important tags because of different DICOM |
543 // dictionaries between DCMTK versions | |
544 if (t == DICOM_TAG_PATIENT_NAME) | |
545 return "PatientName"; | |
546 | |
547 if (t == DicomTag(0x0010, 0x0030)) | |
548 return "PatientBirthDate"; | |
549 | |
550 if (t == DicomTag(0x0010, 0x0040)) | |
551 return "PatientSex"; | |
552 | |
553 // End of patches | |
554 | |
0 | 555 DcmTagKey tag(t.GetGroup(), t.GetElement()); |
556 const DcmDataDictionary& dict = dcmDataDict.rdlock(); | |
557 const DcmDictEntry* entry = dict.findEntry(tag, NULL); | |
558 | |
559 std::string s("Unknown"); | |
560 if (entry != NULL) | |
561 { | |
562 s = std::string(entry->getTagName()); | |
563 } | |
564 | |
565 dcmDataDict.unlock(); | |
566 return s; | |
567 } | |
568 | |
569 | |
570 DicomTag FromDcmtkBridge::FindTag(const char* name) | |
571 { | |
572 const DcmDataDictionary& dict = dcmDataDict.rdlock(); | |
573 const DcmDictEntry* entry = dict.findEntry(name); | |
574 | |
575 if (entry == NULL) | |
576 { | |
577 dcmDataDict.unlock(); | |
62 | 578 throw OrthancException("Unknown DICOM tag"); |
0 | 579 } |
580 else | |
581 { | |
582 DcmTagKey key = entry->getKey(); | |
583 DicomTag tag(key.getGroup(), key.getElement()); | |
584 dcmDataDict.unlock(); | |
585 return tag; | |
586 } | |
587 } | |
588 | |
589 | |
590 void FromDcmtkBridge::Print(FILE* fp, const DicomMap& m) | |
591 { | |
592 for (DicomMap::Map::const_iterator | |
593 it = m.map_.begin(); it != m.map_.end(); it++) | |
594 { | |
595 DicomTag t = it->first; | |
596 std::string s = it->second->AsString(); | |
597 printf("0x%04x 0x%04x (%s) [%s]\n", t.GetGroup(), t.GetElement(), GetName(t).c_str(), s.c_str()); | |
598 } | |
599 } | |
600 | |
601 | |
602 void FromDcmtkBridge::ToJson(Json::Value& result, | |
603 const DicomMap& values) | |
604 { | |
605 if (result.type() != Json::objectValue) | |
606 { | |
62 | 607 throw OrthancException(ErrorCode_BadParameterType); |
0 | 608 } |
609 | |
610 result.clear(); | |
611 | |
612 for (DicomMap::Map::const_iterator | |
613 it = values.map_.begin(); it != values.map_.end(); it++) | |
614 { | |
615 result[GetName(it->first)] = it->second->AsString(); | |
616 } | |
617 } | |
176 | 618 |
619 | |
620 std::string FromDcmtkBridge::GenerateUniqueIdentifier(DicomRootLevel level) | |
621 { | |
622 char uid[100]; | |
623 | |
624 switch (level) | |
625 { | |
626 case DicomRootLevel_Instance: | |
627 return dcmGenerateUniqueIdentifier(uid, SITE_INSTANCE_UID_ROOT); | |
628 | |
629 case DicomRootLevel_Series: | |
630 return dcmGenerateUniqueIdentifier(uid, SITE_SERIES_UID_ROOT); | |
631 | |
632 case DicomRootLevel_Study: | |
633 return dcmGenerateUniqueIdentifier(uid, SITE_STUDY_UID_ROOT); | |
634 | |
635 default: | |
636 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
637 } | |
638 } | |
639 | |
0 | 640 } |