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