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