Mercurial > hg > orthanc
annotate OrthancServer/ParsedDicomFile.cpp @ 910:28a52982196e plugins
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 20 Jun 2014 13:38:19 +0200 |
parents | e078ea944089 |
children | 509e146c3cb3 |
rev | line source |
---|---|
790 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2014 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 * 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. | |
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 | |
32 | |
33 | |
34 /*========================================================================= | |
35 | |
36 This file is based on portions of the following project: | |
37 | |
38 Program: GDCM (Grassroots DICOM). A DICOM library | |
39 Module: http://gdcm.sourceforge.net/Copyright.html | |
40 | |
41 Copyright (c) 2006-2011 Mathieu Malaterre | |
42 Copyright (c) 1993-2005 CREATIS | |
43 (CREATIS = Centre de Recherche et d'Applications en Traitement de l'Image) | |
44 All rights reserved. | |
45 | |
46 Redistribution and use in source and binary forms, with or without | |
47 modification, are permitted provided that the following conditions are met: | |
48 | |
49 * Redistributions of source code must retain the above copyright notice, | |
50 this list of conditions and the following disclaimer. | |
51 | |
52 * Redistributions in binary form must reproduce the above copyright notice, | |
53 this list of conditions and the following disclaimer in the documentation | |
54 and/or other materials provided with the distribution. | |
55 | |
56 * Neither name of Mathieu Malaterre, or CREATIS, nor the names of any | |
57 contributors (CNRS, INSERM, UCB, Universite Lyon I), may be used to | |
58 endorse or promote products derived from this software without specific | |
59 prior written permission. | |
60 | |
61 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' | |
62 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
63 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
64 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR | |
65 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
66 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
67 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
68 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
69 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
70 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
71 | |
72 =========================================================================*/ | |
73 | |
74 | |
831
84513f2ee1f3
pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
75 #include "PrecompiledHeadersServer.h" |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
817
diff
changeset
|
76 |
790 | 77 #ifndef NOMINMAX |
78 #define NOMINMAX | |
79 #endif | |
80 | |
81 #include "ParsedDicomFile.h" | |
82 | |
83 #include "FromDcmtkBridge.h" | |
84 #include "ToDcmtkBridge.h" | |
874
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
85 #include "Internals/DicomImageDecoder.h" |
790 | 86 #include "../Core/Toolbox.h" |
87 #include "../Core/OrthancException.h" | |
874
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
88 #include "../Core/ImageFormats/ImageBuffer.h" |
799 | 89 #include "../Core/ImageFormats/PngWriter.h" |
790 | 90 #include "../Core/Uuid.h" |
91 #include "../Core/DicomFormat/DicomString.h" | |
92 #include "../Core/DicomFormat/DicomNullValue.h" | |
93 #include "../Core/DicomFormat/DicomIntegerPixelAccessor.h" | |
800
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
94 #include "../Core/ImageFormats/PngReader.h" |
790 | 95 |
96 #include <list> | |
97 #include <limits> | |
98 | |
99 #include <boost/lexical_cast.hpp> | |
100 | |
101 #include <dcmtk/dcmdata/dcchrstr.h> | |
102 #include <dcmtk/dcmdata/dcdicent.h> | |
103 #include <dcmtk/dcmdata/dcdict.h> | |
104 #include <dcmtk/dcmdata/dcfilefo.h> | |
105 #include <dcmtk/dcmdata/dcistrmb.h> | |
106 #include <dcmtk/dcmdata/dcuid.h> | |
107 #include <dcmtk/dcmdata/dcmetinf.h> | |
108 | |
109 #include <dcmtk/dcmdata/dcvrae.h> | |
110 #include <dcmtk/dcmdata/dcvras.h> | |
111 #include <dcmtk/dcmdata/dcvrcs.h> | |
112 #include <dcmtk/dcmdata/dcvrda.h> | |
113 #include <dcmtk/dcmdata/dcvrds.h> | |
114 #include <dcmtk/dcmdata/dcvrdt.h> | |
115 #include <dcmtk/dcmdata/dcvrfd.h> | |
116 #include <dcmtk/dcmdata/dcvrfl.h> | |
117 #include <dcmtk/dcmdata/dcvris.h> | |
118 #include <dcmtk/dcmdata/dcvrlo.h> | |
119 #include <dcmtk/dcmdata/dcvrlt.h> | |
120 #include <dcmtk/dcmdata/dcvrpn.h> | |
121 #include <dcmtk/dcmdata/dcvrsh.h> | |
122 #include <dcmtk/dcmdata/dcvrsl.h> | |
123 #include <dcmtk/dcmdata/dcvrss.h> | |
124 #include <dcmtk/dcmdata/dcvrst.h> | |
125 #include <dcmtk/dcmdata/dcvrtm.h> | |
126 #include <dcmtk/dcmdata/dcvrui.h> | |
127 #include <dcmtk/dcmdata/dcvrul.h> | |
128 #include <dcmtk/dcmdata/dcvrus.h> | |
129 #include <dcmtk/dcmdata/dcvrut.h> | |
130 #include <dcmtk/dcmdata/dcpixel.h> | |
131 #include <dcmtk/dcmdata/dcpixseq.h> | |
132 #include <dcmtk/dcmdata/dcpxitem.h> | |
133 | |
134 | |
135 #include <boost/math/special_functions/round.hpp> | |
136 #include <glog/logging.h> | |
137 #include <dcmtk/dcmdata/dcostrmb.h> | |
138 | |
139 | |
140 static const char* CONTENT_TYPE_OCTET_STREAM = "application/octet-stream"; | |
141 | |
142 | |
143 | |
144 namespace Orthanc | |
145 { | |
794 | 146 struct ParsedDicomFile::PImpl |
147 { | |
148 std::auto_ptr<DcmFileFormat> file_; | |
149 }; | |
150 | |
151 | |
152 // This method can only be called from the constructors! | |
790 | 153 void ParsedDicomFile::Setup(const char* buffer, size_t size) |
154 { | |
155 DcmInputBufferStream is; | |
156 if (size > 0) | |
157 { | |
158 is.setBuffer(buffer, size); | |
159 } | |
160 is.setEos(); | |
161 | |
794 | 162 pimpl_->file_.reset(new DcmFileFormat); |
163 pimpl_->file_->transferInit(); | |
164 if (!pimpl_->file_->read(is).good()) | |
790 | 165 { |
794 | 166 delete pimpl_; // Avoid a memory leak due to exception |
167 // throwing, as we are in the constructor | |
168 | |
790 | 169 throw OrthancException(ErrorCode_BadFileFormat); |
170 } | |
794 | 171 pimpl_->file_->loadAllDataIntoMemory(); |
172 pimpl_->file_->transferEnd(); | |
790 | 173 } |
174 | |
175 | |
176 static void SendPathValueForDictionary(RestApiOutput& output, | |
177 DcmItem& dicom) | |
178 { | |
179 Json::Value v = Json::arrayValue; | |
180 | |
181 for (unsigned long i = 0; i < dicom.card(); i++) | |
182 { | |
183 DcmElement* element = dicom.getElement(i); | |
184 if (element) | |
185 { | |
186 char buf[16]; | |
187 sprintf(buf, "%04x-%04x", element->getTag().getGTag(), element->getTag().getETag()); | |
188 v.append(buf); | |
189 } | |
190 } | |
191 | |
192 output.AnswerJson(v); | |
193 } | |
194 | |
195 static inline uint16_t GetCharValue(char c) | |
196 { | |
197 if (c >= '0' && c <= '9') | |
198 return c - '0'; | |
199 else if (c >= 'a' && c <= 'f') | |
200 return c - 'a' + 10; | |
201 else if (c >= 'A' && c <= 'F') | |
202 return c - 'A' + 10; | |
203 else | |
204 return 0; | |
205 } | |
206 | |
207 static inline uint16_t GetTagValue(const char* c) | |
208 { | |
209 return ((GetCharValue(c[0]) << 12) + | |
210 (GetCharValue(c[1]) << 8) + | |
211 (GetCharValue(c[2]) << 4) + | |
212 GetCharValue(c[3])); | |
213 } | |
214 | |
215 static void ParseTagAndGroup(DcmTagKey& key, | |
216 const std::string& tag) | |
217 { | |
218 DicomTag t = FromDcmtkBridge::ParseTag(tag); | |
219 key = DcmTagKey(t.GetGroup(), t.GetElement()); | |
220 } | |
221 | |
222 | |
223 static void SendSequence(RestApiOutput& output, | |
224 DcmSequenceOfItems& sequence) | |
225 { | |
226 // This element is a sequence | |
227 Json::Value v = Json::arrayValue; | |
228 | |
229 for (unsigned long i = 0; i < sequence.card(); i++) | |
230 { | |
231 v.append(boost::lexical_cast<std::string>(i)); | |
232 } | |
233 | |
234 output.AnswerJson(v); | |
235 } | |
236 | |
237 | |
238 static unsigned int GetPixelDataBlockCount(DcmPixelData& pixelData, | |
239 E_TransferSyntax transferSyntax) | |
240 { | |
241 DcmPixelSequence* pixelSequence = NULL; | |
242 if (pixelData.getEncapsulatedRepresentation | |
243 (transferSyntax, NULL, pixelSequence).good() && pixelSequence) | |
244 { | |
245 return pixelSequence->card(); | |
246 } | |
247 else | |
248 { | |
249 return 1; | |
250 } | |
251 } | |
252 | |
253 | |
254 static void AnswerDicomField(RestApiOutput& output, | |
255 DcmElement& element, | |
256 E_TransferSyntax transferSyntax) | |
257 { | |
258 // This element is nor a sequence, neither a pixel-data | |
259 std::string buffer; | |
260 buffer.resize(65536); | |
261 Uint32 length = element.getLength(transferSyntax); | |
262 Uint32 offset = 0; | |
263 | |
264 output.GetLowLevelOutput().SendOkHeader(CONTENT_TYPE_OCTET_STREAM, true, length, NULL); | |
265 | |
266 while (offset < length) | |
267 { | |
268 Uint32 nbytes; | |
269 if (length - offset < buffer.size()) | |
270 { | |
271 nbytes = length - offset; | |
272 } | |
273 else | |
274 { | |
275 nbytes = buffer.size(); | |
276 } | |
277 | |
278 OFCondition cond = element.getPartialValue(&buffer[0], offset, nbytes); | |
279 | |
280 if (cond.good()) | |
281 { | |
908
e078ea944089
refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
874
diff
changeset
|
282 output.GetLowLevelOutput().SendBodyData(&buffer[0], nbytes); |
790 | 283 offset += nbytes; |
284 } | |
285 else | |
286 { | |
287 LOG(ERROR) << "Error while sending a DICOM field: " << cond.text(); | |
288 return; | |
289 } | |
290 } | |
291 | |
292 output.MarkLowLevelOutputDone(); | |
293 } | |
294 | |
295 | |
296 static bool AnswerPixelData(RestApiOutput& output, | |
297 DcmItem& dicom, | |
298 E_TransferSyntax transferSyntax, | |
299 const std::string* blockUri) | |
300 { | |
301 DcmTag k(DICOM_TAG_PIXEL_DATA.GetGroup(), | |
302 DICOM_TAG_PIXEL_DATA.GetElement()); | |
303 | |
304 DcmElement *element = NULL; | |
305 if (!dicom.findAndGetElement(k, element).good() || | |
306 element == NULL) | |
307 { | |
308 return false; | |
309 } | |
310 | |
311 try | |
312 { | |
313 DcmPixelData& pixelData = dynamic_cast<DcmPixelData&>(*element); | |
314 if (blockUri == NULL) | |
315 { | |
316 // The user asks how many blocks are presents in this pixel data | |
317 unsigned int blocks = GetPixelDataBlockCount(pixelData, transferSyntax); | |
318 | |
319 Json::Value result(Json::arrayValue); | |
320 for (unsigned int i = 0; i < blocks; i++) | |
321 { | |
322 result.append(boost::lexical_cast<std::string>(i)); | |
323 } | |
324 | |
325 output.AnswerJson(result); | |
326 return true; | |
327 } | |
328 | |
329 | |
330 unsigned int block = boost::lexical_cast<unsigned int>(*blockUri); | |
331 | |
332 if (block < GetPixelDataBlockCount(pixelData, transferSyntax)) | |
333 { | |
334 DcmPixelSequence* pixelSequence = NULL; | |
335 if (pixelData.getEncapsulatedRepresentation | |
336 (transferSyntax, NULL, pixelSequence).good() && pixelSequence) | |
337 { | |
338 // This is the case for JPEG transfer syntaxes | |
339 if (block < pixelSequence->card()) | |
340 { | |
341 DcmPixelItem* pixelItem = NULL; | |
342 if (pixelSequence->getItem(pixelItem, block).good() && pixelItem) | |
343 { | |
344 if (pixelItem->getLength() == 0) | |
345 { | |
346 output.AnswerBuffer(NULL, 0, CONTENT_TYPE_OCTET_STREAM); | |
347 return true; | |
348 } | |
349 | |
350 Uint8* buffer = NULL; | |
351 if (pixelItem->getUint8Array(buffer).good() && buffer) | |
352 { | |
353 output.AnswerBuffer(buffer, pixelItem->getLength(), CONTENT_TYPE_OCTET_STREAM); | |
354 return true; | |
355 } | |
356 } | |
357 } | |
358 } | |
359 else | |
360 { | |
361 // This is the case for raw, uncompressed image buffers | |
362 assert(*blockUri == "0"); | |
363 AnswerDicomField(output, *element, transferSyntax); | |
364 } | |
365 } | |
366 } | |
367 catch (boost::bad_lexical_cast&) | |
368 { | |
369 // The URI entered by the user is not a number | |
370 } | |
371 catch (std::bad_cast&) | |
372 { | |
373 // This should never happen | |
374 } | |
375 | |
376 return false; | |
377 } | |
378 | |
379 | |
380 | |
381 static void SendPathValueForLeaf(RestApiOutput& output, | |
382 const std::string& tag, | |
383 DcmItem& dicom, | |
384 E_TransferSyntax transferSyntax) | |
385 { | |
386 DcmTagKey k; | |
387 ParseTagAndGroup(k, tag); | |
388 | |
389 DcmSequenceOfItems* sequence = NULL; | |
390 if (dicom.findAndGetSequence(k, sequence).good() && | |
391 sequence != NULL && | |
392 sequence->getVR() == EVR_SQ) | |
393 { | |
394 SendSequence(output, *sequence); | |
395 return; | |
396 } | |
397 | |
398 DcmElement* element = NULL; | |
399 if (dicom.findAndGetElement(k, element).good() && | |
400 element != NULL && | |
401 //element->getVR() != EVR_UNKNOWN && // This would forbid private tags | |
402 element->getVR() != EVR_SQ) | |
403 { | |
404 AnswerDicomField(output, *element, transferSyntax); | |
405 } | |
406 } | |
407 | |
408 void ParsedDicomFile::SendPathValue(RestApiOutput& output, | |
409 const UriComponents& uri) | |
410 { | |
794 | 411 DcmItem* dicom = pimpl_->file_->getDataset(); |
412 E_TransferSyntax transferSyntax = pimpl_->file_->getDataset()->getOriginalXfer(); | |
790 | 413 |
414 // Special case: Accessing the pixel data | |
415 if (uri.size() == 1 || | |
416 uri.size() == 2) | |
417 { | |
418 DcmTagKey tag; | |
419 ParseTagAndGroup(tag, uri[0]); | |
420 | |
421 if (tag.getGroup() == DICOM_TAG_PIXEL_DATA.GetGroup() && | |
422 tag.getElement() == DICOM_TAG_PIXEL_DATA.GetElement()) | |
423 { | |
424 AnswerPixelData(output, *dicom, transferSyntax, uri.size() == 1 ? NULL : &uri[1]); | |
425 return; | |
426 } | |
427 } | |
428 | |
429 // Go down in the tag hierarchy according to the URI | |
430 for (size_t pos = 0; pos < uri.size() / 2; pos++) | |
431 { | |
432 size_t index; | |
433 try | |
434 { | |
435 index = boost::lexical_cast<size_t>(uri[2 * pos + 1]); | |
436 } | |
437 catch (boost::bad_lexical_cast&) | |
438 { | |
439 return; | |
440 } | |
441 | |
442 DcmTagKey k; | |
443 DcmItem *child = NULL; | |
444 ParseTagAndGroup(k, uri[2 * pos]); | |
445 if (!dicom->findAndGetSequenceItem(k, child, index).good() || | |
446 child == NULL) | |
447 { | |
448 return; | |
449 } | |
450 | |
451 dicom = child; | |
452 } | |
453 | |
454 // We have reached the end of the URI | |
455 if (uri.size() % 2 == 0) | |
456 { | |
457 SendPathValueForDictionary(output, *dicom); | |
458 } | |
459 else | |
460 { | |
461 SendPathValueForLeaf(output, uri.back(), *dicom, transferSyntax); | |
462 } | |
463 } | |
464 | |
465 | |
466 | |
467 | |
468 | |
469 static DcmElement* CreateElementForTag(const DicomTag& tag) | |
470 { | |
471 DcmTag key(tag.GetGroup(), tag.GetElement()); | |
472 | |
473 switch (key.getEVR()) | |
474 { | |
475 // http://support.dcmtk.org/docs/dcvr_8h-source.html | |
476 | |
477 /** | |
478 * TODO. | |
479 **/ | |
480 | |
481 case EVR_OB: // other byte | |
482 case EVR_OF: // other float | |
483 case EVR_OW: // other word | |
484 case EVR_AT: // attribute tag | |
485 throw OrthancException(ErrorCode_NotImplemented); | |
486 | |
487 case EVR_UN: // unknown value representation | |
488 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
489 | |
490 | |
491 /** | |
492 * String types. | |
493 * http://support.dcmtk.org/docs/classDcmByteString.html | |
494 **/ | |
495 | |
496 case EVR_AS: // age string | |
497 return new DcmAgeString(key); | |
498 | |
499 case EVR_AE: // application entity title | |
500 return new DcmApplicationEntity(key); | |
501 | |
502 case EVR_CS: // code string | |
503 return new DcmCodeString(key); | |
504 | |
505 case EVR_DA: // date string | |
506 return new DcmDate(key); | |
507 | |
508 case EVR_DT: // date time string | |
509 return new DcmDateTime(key); | |
510 | |
511 case EVR_DS: // decimal string | |
512 return new DcmDecimalString(key); | |
513 | |
514 case EVR_IS: // integer string | |
515 return new DcmIntegerString(key); | |
516 | |
517 case EVR_TM: // time string | |
518 return new DcmTime(key); | |
519 | |
520 case EVR_UI: // unique identifier | |
521 return new DcmUniqueIdentifier(key); | |
522 | |
523 case EVR_ST: // short text | |
524 return new DcmShortText(key); | |
525 | |
526 case EVR_LO: // long string | |
527 return new DcmLongString(key); | |
528 | |
529 case EVR_LT: // long text | |
530 return new DcmLongText(key); | |
531 | |
532 case EVR_UT: // unlimited text | |
533 return new DcmUnlimitedText(key); | |
534 | |
535 case EVR_SH: // short string | |
536 return new DcmShortString(key); | |
537 | |
538 case EVR_PN: // person name | |
539 return new DcmPersonName(key); | |
540 | |
541 | |
542 /** | |
543 * Numerical types | |
544 **/ | |
545 | |
546 case EVR_SL: // signed long | |
547 return new DcmSignedLong(key); | |
548 | |
549 case EVR_SS: // signed short | |
550 return new DcmSignedShort(key); | |
551 | |
552 case EVR_UL: // unsigned long | |
553 return new DcmUnsignedLong(key); | |
554 | |
555 case EVR_US: // unsigned short | |
556 return new DcmUnsignedShort(key); | |
557 | |
558 case EVR_FL: // float single-precision | |
559 return new DcmFloatingPointSingle(key); | |
560 | |
561 case EVR_FD: // float double-precision | |
562 return new DcmFloatingPointDouble(key); | |
563 | |
564 | |
565 /** | |
566 * Sequence types, should never occur at this point. | |
567 **/ | |
568 | |
569 case EVR_SQ: // sequence of items | |
570 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
571 | |
572 | |
573 /** | |
574 * Internal to DCMTK. | |
575 **/ | |
576 | |
577 case EVR_ox: // OB or OW depending on context | |
578 case EVR_xs: // SS or US depending on context | |
579 case EVR_lt: // US, SS or OW depending on context, used for LUT Data (thus the name) | |
580 case EVR_na: // na="not applicable", for data which has no VR | |
581 case EVR_up: // up="unsigned pointer", used internally for DICOMDIR suppor | |
582 case EVR_item: // used internally for items | |
583 case EVR_metainfo: // used internally for meta info datasets | |
584 case EVR_dataset: // used internally for datasets | |
585 case EVR_fileFormat: // used internally for DICOM files | |
586 case EVR_dicomDir: // used internally for DICOMDIR objects | |
587 case EVR_dirRecord: // used internally for DICOMDIR records | |
588 case EVR_pixelSQ: // used internally for pixel sequences in a compressed image | |
589 case EVR_pixelItem: // used internally for pixel items in a compressed image | |
590 case EVR_UNKNOWN: // used internally for elements with unknown VR (encoded with 4-byte length field in explicit VR) | |
591 case EVR_PixelData: // used internally for uncompressed pixeld data | |
592 case EVR_OverlayData: // used internally for overlay data | |
593 case EVR_UNKNOWN2B: // used internally for elements with unknown VR with 2-byte length field in explicit VR | |
594 default: | |
595 break; | |
596 } | |
597 | |
598 throw OrthancException(ErrorCode_InternalError); | |
599 } | |
600 | |
601 | |
602 | |
603 static void FillElementWithString(DcmElement& element, | |
604 const DicomTag& tag, | |
605 const std::string& value) | |
606 { | |
607 DcmTag key(tag.GetGroup(), tag.GetElement()); | |
608 bool ok = false; | |
609 | |
610 try | |
611 { | |
612 switch (key.getEVR()) | |
613 { | |
614 // http://support.dcmtk.org/docs/dcvr_8h-source.html | |
615 | |
616 /** | |
617 * TODO. | |
618 **/ | |
619 | |
620 case EVR_OB: // other byte | |
621 case EVR_OF: // other float | |
622 case EVR_OW: // other word | |
623 case EVR_AT: // attribute tag | |
624 throw OrthancException(ErrorCode_NotImplemented); | |
625 | |
626 case EVR_UN: // unknown value representation | |
627 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
628 | |
629 | |
630 /** | |
631 * String types. | |
632 **/ | |
633 | |
634 case EVR_DS: // decimal string | |
635 case EVR_IS: // integer string | |
636 case EVR_AS: // age string | |
637 case EVR_DA: // date string | |
638 case EVR_DT: // date time string | |
639 case EVR_TM: // time string | |
640 case EVR_AE: // application entity title | |
641 case EVR_CS: // code string | |
642 case EVR_SH: // short string | |
643 case EVR_LO: // long string | |
644 case EVR_ST: // short text | |
645 case EVR_LT: // long text | |
646 case EVR_UT: // unlimited text | |
647 case EVR_PN: // person name | |
648 case EVR_UI: // unique identifier | |
649 { | |
650 ok = element.putString(value.c_str()).good(); | |
651 break; | |
652 } | |
653 | |
654 | |
655 /** | |
656 * Numerical types | |
657 **/ | |
658 | |
659 case EVR_SL: // signed long | |
660 { | |
661 ok = element.putSint32(boost::lexical_cast<Sint32>(value)).good(); | |
662 break; | |
663 } | |
664 | |
665 case EVR_SS: // signed short | |
666 { | |
667 ok = element.putSint16(boost::lexical_cast<Sint16>(value)).good(); | |
668 break; | |
669 } | |
670 | |
671 case EVR_UL: // unsigned long | |
672 { | |
673 ok = element.putUint32(boost::lexical_cast<Uint32>(value)).good(); | |
674 break; | |
675 } | |
676 | |
677 case EVR_US: // unsigned short | |
678 { | |
679 ok = element.putUint16(boost::lexical_cast<Uint16>(value)).good(); | |
680 break; | |
681 } | |
682 | |
683 case EVR_FL: // float single-precision | |
684 { | |
685 ok = element.putFloat32(boost::lexical_cast<float>(value)).good(); | |
686 break; | |
687 } | |
688 | |
689 case EVR_FD: // float double-precision | |
690 { | |
691 ok = element.putFloat64(boost::lexical_cast<double>(value)).good(); | |
692 break; | |
693 } | |
694 | |
695 | |
696 /** | |
697 * Sequence types, should never occur at this point. | |
698 **/ | |
699 | |
700 case EVR_SQ: // sequence of items | |
701 { | |
702 ok = false; | |
703 break; | |
704 } | |
705 | |
706 | |
707 /** | |
708 * Internal to DCMTK. | |
709 **/ | |
710 | |
711 case EVR_ox: // OB or OW depending on context | |
712 case EVR_xs: // SS or US depending on context | |
713 case EVR_lt: // US, SS or OW depending on context, used for LUT Data (thus the name) | |
714 case EVR_na: // na="not applicable", for data which has no VR | |
715 case EVR_up: // up="unsigned pointer", used internally for DICOMDIR suppor | |
716 case EVR_item: // used internally for items | |
717 case EVR_metainfo: // used internally for meta info datasets | |
718 case EVR_dataset: // used internally for datasets | |
719 case EVR_fileFormat: // used internally for DICOM files | |
720 case EVR_dicomDir: // used internally for DICOMDIR objects | |
721 case EVR_dirRecord: // used internally for DICOMDIR records | |
722 case EVR_pixelSQ: // used internally for pixel sequences in a compressed image | |
723 case EVR_pixelItem: // used internally for pixel items in a compressed image | |
724 case EVR_UNKNOWN: // used internally for elements with unknown VR (encoded with 4-byte length field in explicit VR) | |
725 case EVR_PixelData: // used internally for uncompressed pixeld data | |
726 case EVR_OverlayData: // used internally for overlay data | |
727 case EVR_UNKNOWN2B: // used internally for elements with unknown VR with 2-byte length field in explicit VR | |
728 default: | |
729 break; | |
730 } | |
731 } | |
732 catch (boost::bad_lexical_cast&) | |
733 { | |
734 ok = false; | |
735 } | |
736 | |
737 if (!ok) | |
738 { | |
739 throw OrthancException(ErrorCode_InternalError); | |
740 } | |
741 } | |
742 | |
743 | |
744 void ParsedDicomFile::Remove(const DicomTag& tag) | |
745 { | |
746 DcmTagKey key(tag.GetGroup(), tag.GetElement()); | |
794 | 747 DcmElement* element = pimpl_->file_->getDataset()->remove(key); |
790 | 748 if (element != NULL) |
749 { | |
750 delete element; | |
751 } | |
752 } | |
753 | |
754 | |
755 | |
756 void ParsedDicomFile::RemovePrivateTags() | |
757 { | |
758 typedef std::list<DcmElement*> Tags; | |
759 | |
760 Tags privateTags; | |
761 | |
794 | 762 DcmDataset& dataset = *pimpl_->file_->getDataset(); |
790 | 763 for (unsigned long i = 0; i < dataset.card(); i++) |
764 { | |
765 DcmElement* element = dataset.getElement(i); | |
766 DcmTag tag(element->getTag()); | |
767 if (!strcmp("PrivateCreator", tag.getTagName()) || // TODO - This may change with future versions of DCMTK | |
768 tag.getPrivateCreator() != NULL) | |
769 { | |
770 privateTags.push_back(element); | |
771 } | |
772 } | |
773 | |
774 for (Tags::iterator it = privateTags.begin(); | |
775 it != privateTags.end(); ++it) | |
776 { | |
777 DcmElement* tmp = dataset.remove(*it); | |
778 if (tmp != NULL) | |
779 { | |
780 delete tmp; | |
781 } | |
782 } | |
783 } | |
784 | |
785 | |
786 | |
787 void ParsedDicomFile::Insert(const DicomTag& tag, | |
788 const std::string& value) | |
789 { | |
790 std::auto_ptr<DcmElement> element(CreateElementForTag(tag)); | |
791 FillElementWithString(*element, tag, value); | |
792 | |
794 | 793 if (!pimpl_->file_->getDataset()->insert(element.release(), false, false).good()) |
790 | 794 { |
795 // This field already exists | |
796 throw OrthancException(ErrorCode_InternalError); | |
797 } | |
798 } | |
799 | |
800 | |
801 void ParsedDicomFile::Replace(const DicomTag& tag, | |
802 const std::string& value, | |
803 DicomReplaceMode mode) | |
804 { | |
805 DcmTagKey key(tag.GetGroup(), tag.GetElement()); | |
806 DcmElement* element = NULL; | |
807 | |
794 | 808 if (!pimpl_->file_->getDataset()->findAndGetElement(key, element).good() || |
790 | 809 element == NULL) |
810 { | |
811 // This field does not exist, act wrt. the specified "mode" | |
812 switch (mode) | |
813 { | |
814 case DicomReplaceMode_InsertIfAbsent: | |
815 Insert(tag, value); | |
816 break; | |
817 | |
818 case DicomReplaceMode_ThrowIfAbsent: | |
819 throw OrthancException(ErrorCode_InexistentItem); | |
820 | |
821 case DicomReplaceMode_IgnoreIfAbsent: | |
822 return; | |
823 } | |
824 } | |
825 else | |
826 { | |
827 FillElementWithString(*element, tag, value); | |
828 } | |
829 | |
830 | |
831 /** | |
832 * dcmodify will automatically correct 'Media Storage SOP Class | |
833 * UID' and 'Media Storage SOP Instance UID' in the metaheader, if | |
834 * you make changes to the related tags in the dataset ('SOP Class | |
835 * UID' and 'SOP Instance UID') via insert or modify mode | |
836 * options. You can disable this behaviour by using the -nmu | |
837 * option. | |
838 **/ | |
839 | |
840 if (tag == DICOM_TAG_SOP_CLASS_UID) | |
841 { | |
842 Replace(DICOM_TAG_MEDIA_STORAGE_SOP_CLASS_UID, value, DicomReplaceMode_InsertIfAbsent); | |
843 } | |
844 | |
845 if (tag == DICOM_TAG_SOP_INSTANCE_UID) | |
846 { | |
847 Replace(DICOM_TAG_MEDIA_STORAGE_SOP_INSTANCE_UID, value, DicomReplaceMode_InsertIfAbsent); | |
848 } | |
849 } | |
850 | |
851 | |
852 void ParsedDicomFile::Answer(RestApiOutput& output) | |
853 { | |
854 std::string serialized; | |
794 | 855 if (FromDcmtkBridge::SaveToMemoryBuffer(serialized, pimpl_->file_->getDataset())) |
790 | 856 { |
857 output.AnswerBuffer(serialized, CONTENT_TYPE_OCTET_STREAM); | |
858 } | |
859 } | |
860 | |
861 | |
862 | |
863 bool ParsedDicomFile::GetTagValue(std::string& value, | |
864 const DicomTag& tag) | |
865 { | |
866 DcmTagKey k(tag.GetGroup(), tag.GetElement()); | |
794 | 867 DcmDataset& dataset = *pimpl_->file_->getDataset(); |
790 | 868 DcmElement* element = NULL; |
869 if (!dataset.findAndGetElement(k, element).good() || | |
870 element == NULL) | |
871 { | |
872 return false; | |
873 } | |
874 | |
875 std::auto_ptr<DicomValue> v(FromDcmtkBridge::ConvertLeafElement(*element)); | |
876 | |
877 if (v.get() == NULL) | |
878 { | |
879 value = ""; | |
880 } | |
881 else | |
882 { | |
883 value = v->AsString(); | |
884 } | |
885 | |
886 return true; | |
887 } | |
888 | |
889 | |
890 | |
891 DicomInstanceHasher ParsedDicomFile::GetHasher() | |
892 { | |
893 std::string patientId, studyUid, seriesUid, instanceUid; | |
894 | |
895 if (!GetTagValue(patientId, DICOM_TAG_PATIENT_ID) || | |
896 !GetTagValue(studyUid, DICOM_TAG_STUDY_INSTANCE_UID) || | |
897 !GetTagValue(seriesUid, DICOM_TAG_SERIES_INSTANCE_UID) || | |
898 !GetTagValue(instanceUid, DICOM_TAG_SOP_INSTANCE_UID)) | |
899 { | |
900 throw OrthancException(ErrorCode_BadFileFormat); | |
901 } | |
902 | |
903 return DicomInstanceHasher(patientId, studyUid, seriesUid, instanceUid); | |
904 } | |
905 | |
906 | |
907 static void StoreElement(Json::Value& target, | |
908 DcmElement& element, | |
909 unsigned int maxStringLength); | |
910 | |
911 static void StoreItem(Json::Value& target, | |
912 DcmItem& item, | |
913 unsigned int maxStringLength) | |
914 { | |
915 target = Json::Value(Json::objectValue); | |
916 | |
917 for (unsigned long i = 0; i < item.card(); i++) | |
918 { | |
919 DcmElement* element = item.getElement(i); | |
920 StoreElement(target, *element, maxStringLength); | |
921 } | |
922 } | |
923 | |
924 | |
925 static void StoreElement(Json::Value& target, | |
926 DcmElement& element, | |
927 unsigned int maxStringLength) | |
928 { | |
929 assert(target.type() == Json::objectValue); | |
930 | |
931 DicomTag tag(FromDcmtkBridge::GetTag(element)); | |
932 const std::string formattedTag = tag.Format(); | |
933 | |
934 #if 0 | |
935 const std::string tagName = FromDcmtkBridge::GetName(tag); | |
936 #else | |
937 // This version of the code gives access to the name of the private tags | |
938 DcmTag tagbis(element.getTag()); | |
939 const std::string tagName(tagbis.getTagName()); | |
940 #endif | |
941 | |
942 if (element.isLeaf()) | |
943 { | |
944 Json::Value value(Json::objectValue); | |
945 value["Name"] = tagName; | |
946 | |
947 if (tagbis.getPrivateCreator() != NULL) | |
948 { | |
949 value["PrivateCreator"] = tagbis.getPrivateCreator(); | |
950 } | |
951 | |
952 std::auto_ptr<DicomValue> v(FromDcmtkBridge::ConvertLeafElement(element)); | |
953 if (v->IsNull()) | |
954 { | |
955 value["Type"] = "Null"; | |
956 value["Value"] = Json::nullValue; | |
957 } | |
958 else | |
959 { | |
960 std::string s = v->AsString(); | |
961 if (maxStringLength == 0 || | |
962 s.size() <= maxStringLength) | |
963 { | |
964 value["Type"] = "String"; | |
965 value["Value"] = s; | |
966 } | |
967 else | |
968 { | |
969 value["Type"] = "TooLong"; | |
970 value["Value"] = Json::nullValue; | |
971 } | |
972 } | |
973 | |
974 target[formattedTag] = value; | |
975 } | |
976 else | |
977 { | |
978 Json::Value children(Json::arrayValue); | |
979 | |
980 // "All subclasses of DcmElement except for DcmSequenceOfItems | |
981 // are leaf nodes, while DcmSequenceOfItems, DcmItem, DcmDataset | |
982 // etc. are not." The following cast is thus OK. | |
983 DcmSequenceOfItems& sequence = dynamic_cast<DcmSequenceOfItems&>(element); | |
984 | |
985 for (unsigned long i = 0; i < sequence.card(); i++) | |
986 { | |
987 DcmItem* child = sequence.getItem(i); | |
988 Json::Value& v = children.append(Json::objectValue); | |
989 StoreItem(v, *child, maxStringLength); | |
990 } | |
991 | |
992 target[formattedTag]["Name"] = tagName; | |
993 target[formattedTag]["Type"] = "Sequence"; | |
994 target[formattedTag]["Value"] = children; | |
995 } | |
996 } | |
997 | |
998 | |
999 template <typename T> | |
1000 static void ExtractPngImageTruncate(std::string& result, | |
1001 DicomIntegerPixelAccessor& accessor, | |
1002 PixelFormat format) | |
1003 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1004 assert(accessor.GetInformation().GetChannelCount() == 1); |
790 | 1005 |
1006 PngWriter w; | |
1007 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1008 std::vector<T> image(accessor.GetInformation().GetWidth() * accessor.GetInformation().GetHeight(), 0); |
790 | 1009 T* pixel = &image[0]; |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1010 for (unsigned int y = 0; y < accessor.GetInformation().GetHeight(); y++) |
790 | 1011 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1012 for (unsigned int x = 0; x < accessor.GetInformation().GetWidth(); x++, pixel++) |
790 | 1013 { |
1014 int32_t v = accessor.GetValue(x, y); | |
1015 if (v < static_cast<int32_t>(std::numeric_limits<T>::min())) | |
1016 *pixel = std::numeric_limits<T>::min(); | |
1017 else if (v > static_cast<int32_t>(std::numeric_limits<T>::max())) | |
1018 *pixel = std::numeric_limits<T>::max(); | |
1019 else | |
1020 *pixel = static_cast<T>(v); | |
1021 } | |
1022 } | |
1023 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1024 w.WriteToMemory(result, accessor.GetInformation().GetWidth(), accessor.GetInformation().GetHeight(), |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1025 accessor.GetInformation().GetWidth() * sizeof(T), format, &image[0]); |
790 | 1026 } |
1027 | |
1028 | |
1029 void ParsedDicomFile::SaveToMemoryBuffer(std::string& buffer) | |
1030 { | |
794 | 1031 FromDcmtkBridge::SaveToMemoryBuffer(buffer, pimpl_->file_->getDataset()); |
790 | 1032 } |
1033 | |
1034 | |
1035 void ParsedDicomFile::SaveToFile(const std::string& path) | |
1036 { | |
1037 // TODO Avoid using a temporary memory buffer, write directly on disk | |
1038 std::string content; | |
1039 SaveToMemoryBuffer(content); | |
1040 Toolbox::WriteFile(content, path); | |
1041 } | |
1042 | |
1043 | |
794 | 1044 ParsedDicomFile::ParsedDicomFile() : pimpl_(new PImpl) |
790 | 1045 { |
794 | 1046 pimpl_->file_.reset(new DcmFileFormat); |
790 | 1047 Replace(DICOM_TAG_PATIENT_ID, FromDcmtkBridge::GenerateUniqueIdentifier(ResourceType_Patient)); |
1048 Replace(DICOM_TAG_STUDY_INSTANCE_UID, FromDcmtkBridge::GenerateUniqueIdentifier(ResourceType_Study)); | |
1049 Replace(DICOM_TAG_SERIES_INSTANCE_UID, FromDcmtkBridge::GenerateUniqueIdentifier(ResourceType_Series)); | |
1050 Replace(DICOM_TAG_SOP_INSTANCE_UID, FromDcmtkBridge::GenerateUniqueIdentifier(ResourceType_Instance)); | |
1051 } | |
1052 | |
791 | 1053 |
794 | 1054 ParsedDicomFile::ParsedDicomFile(const char* content, size_t size) : pimpl_(new PImpl) |
791 | 1055 { |
1056 Setup(content, size); | |
1057 } | |
1058 | |
794 | 1059 ParsedDicomFile::ParsedDicomFile(const std::string& content) : pimpl_(new PImpl) |
791 | 1060 { |
1061 if (content.size() == 0) | |
1062 { | |
1063 Setup(NULL, 0); | |
1064 } | |
1065 else | |
1066 { | |
1067 Setup(&content[0], content.size()); | |
1068 } | |
1069 } | |
792 | 1070 |
1071 | |
794 | 1072 ParsedDicomFile::ParsedDicomFile(ParsedDicomFile& other) : |
1073 pimpl_(new PImpl) | |
792 | 1074 { |
794 | 1075 pimpl_->file_.reset(dynamic_cast<DcmFileFormat*>(other.pimpl_->file_->clone())); |
792 | 1076 } |
1077 | |
1078 | |
1079 ParsedDicomFile::~ParsedDicomFile() | |
1080 { | |
794 | 1081 delete pimpl_; |
792 | 1082 } |
1083 | |
793 | 1084 |
1085 void* ParsedDicomFile::GetDcmtkObject() | |
1086 { | |
794 | 1087 return pimpl_->file_.get(); |
793 | 1088 } |
1089 | |
1090 | |
1091 ParsedDicomFile* ParsedDicomFile::Clone() | |
1092 { | |
794 | 1093 return new ParsedDicomFile(*this); |
793 | 1094 } |
800
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1095 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1096 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1097 void ParsedDicomFile::EmbedImage(const std::string& dataUriScheme) |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1098 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1099 std::string mime, content; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1100 Toolbox::DecodeDataUriScheme(mime, content, dataUriScheme); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1101 |
809
8ce2f69436ca
do not return strings with base64
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
1102 std::string decoded; |
8ce2f69436ca
do not return strings with base64
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
1103 Toolbox::DecodeBase64(decoded, content); |
800
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1104 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1105 if (mime == "image/png") |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1106 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1107 PngReader reader; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1108 reader.ReadFromMemory(decoded); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1109 EmbedImage(reader); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1110 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1111 else |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1112 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1113 throw OrthancException(ErrorCode_NotImplemented); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1114 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1115 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1116 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1117 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1118 void ParsedDicomFile::EmbedImage(const ImageAccessor& accessor) |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1119 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1120 if (accessor.GetFormat() != PixelFormat_Grayscale8 && |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1121 accessor.GetFormat() != PixelFormat_Grayscale16 && |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1122 accessor.GetFormat() != PixelFormat_RGB24 && |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1123 accessor.GetFormat() != PixelFormat_RGBA32) |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1124 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1125 throw OrthancException(ErrorCode_NotImplemented); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1126 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1127 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1128 if (accessor.GetFormat() == PixelFormat_RGBA32) |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1129 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1130 LOG(WARNING) << "Getting rid of the alpha channel when embedding a RGBA image inside DICOM"; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1131 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1132 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1133 // http://dicomiseasy.blogspot.be/2012/08/chapter-12-pixel-data.html |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1134 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1135 Remove(DICOM_TAG_PIXEL_DATA); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1136 Replace(DICOM_TAG_COLUMNS, boost::lexical_cast<std::string>(accessor.GetWidth())); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1137 Replace(DICOM_TAG_ROWS, boost::lexical_cast<std::string>(accessor.GetHeight())); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1138 Replace(DICOM_TAG_SAMPLES_PER_PIXEL, "1"); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1139 Replace(DICOM_TAG_NUMBER_OF_FRAMES, "1"); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1140 Replace(DICOM_TAG_PIXEL_REPRESENTATION, "0"); // Unsigned pixels |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1141 Replace(DICOM_TAG_PLANAR_CONFIGURATION, "0"); // Color channels are interleaved |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1142 Replace(DICOM_TAG_PHOTOMETRIC_INTERPRETATION, "MONOCHROME2"); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1143 Replace(DICOM_TAG_BITS_ALLOCATED, "8"); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1144 Replace(DICOM_TAG_BITS_STORED, "8"); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1145 Replace(DICOM_TAG_HIGH_BIT, "7"); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1146 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1147 unsigned int bytesPerPixel = 1; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1148 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1149 switch (accessor.GetFormat()) |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1150 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1151 case PixelFormat_RGB24: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1152 case PixelFormat_RGBA32: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1153 Replace(DICOM_TAG_PHOTOMETRIC_INTERPRETATION, "RGB"); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1154 Replace(DICOM_TAG_SAMPLES_PER_PIXEL, "3"); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1155 bytesPerPixel = 3; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1156 break; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1157 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1158 case PixelFormat_Grayscale8: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1159 break; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1160 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1161 case PixelFormat_Grayscale16: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1162 Replace(DICOM_TAG_BITS_ALLOCATED, "16"); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1163 Replace(DICOM_TAG_BITS_STORED, "16"); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1164 Replace(DICOM_TAG_HIGH_BIT, "15"); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1165 bytesPerPixel = 2; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1166 break; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1167 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1168 default: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1169 throw OrthancException(ErrorCode_NotImplemented); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1170 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1171 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1172 DcmTag key(DICOM_TAG_PIXEL_DATA.GetGroup(), |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1173 DICOM_TAG_PIXEL_DATA.GetElement()); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1174 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1175 std::auto_ptr<DcmPixelData> pixels(new DcmPixelData(key)); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1176 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1177 unsigned int pitch = accessor.GetWidth() * bytesPerPixel; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1178 Uint8* target = NULL; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1179 pixels->createUint8Array(accessor.GetHeight() * pitch, target); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1180 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1181 for (unsigned int y = 0; y < accessor.GetHeight(); y++) |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1182 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1183 switch (accessor.GetFormat()) |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1184 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1185 case PixelFormat_RGB24: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1186 case PixelFormat_Grayscale8: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1187 case PixelFormat_Grayscale16: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1188 case PixelFormat_SignedGrayscale16: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1189 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1190 memcpy(target, reinterpret_cast<const Uint8*>(accessor.GetConstRow(y)), pitch); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1191 target += pitch; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1192 break; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1193 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1194 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1195 case PixelFormat_RGBA32: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1196 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1197 // The alpha channel is not supported by the DICOM standard |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1198 const Uint8* source = reinterpret_cast<const Uint8*>(accessor.GetConstRow(y)); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1199 for (unsigned int x = 0; x < accessor.GetWidth(); x++, target += 3, source += 4) |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1200 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1201 target[0] = source[0]; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1202 target[1] = source[1]; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1203 target[2] = source[2]; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1204 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1205 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1206 break; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1207 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1208 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1209 default: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1210 throw OrthancException(ErrorCode_NotImplemented); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1211 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1212 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1213 |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1214 if (!pimpl_->file_->getDataset()->insert(pixels.release(), false, false).good()) |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1215 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1216 throw OrthancException(ErrorCode_InternalError); |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1217 } |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
1218 } |
874
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1219 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1220 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1221 void ParsedDicomFile::ExtractImage(ImageBuffer& result, |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1222 unsigned int frame) |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1223 { |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1224 DcmDataset& dataset = *pimpl_->file_->getDataset(); |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1225 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1226 if (!DicomImageDecoder::Decode(result, dataset, frame)) |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1227 { |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1228 throw OrthancException(ErrorCode_BadFileFormat); |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1229 } |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1230 } |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1231 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1232 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1233 void ParsedDicomFile::ExtractImage(ImageBuffer& result, |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1234 unsigned int frame, |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1235 ImageExtractionMode mode) |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1236 { |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1237 DcmDataset& dataset = *pimpl_->file_->getDataset(); |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1238 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1239 bool ok = false; |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1240 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1241 switch (mode) |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1242 { |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1243 case ImageExtractionMode_UInt8: |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1244 ok = DicomImageDecoder::DecodeAndTruncate(result, dataset, frame, PixelFormat_Grayscale8); |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1245 break; |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1246 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1247 case ImageExtractionMode_UInt16: |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1248 ok = DicomImageDecoder::DecodeAndTruncate(result, dataset, frame, PixelFormat_Grayscale16); |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1249 break; |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1250 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1251 case ImageExtractionMode_Int16: |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1252 ok = DicomImageDecoder::DecodeAndTruncate(result, dataset, frame, PixelFormat_SignedGrayscale16); |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1253 break; |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1254 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1255 case ImageExtractionMode_Preview: |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1256 ok = DicomImageDecoder::DecodePreview(result, dataset, frame); |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1257 break; |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1258 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1259 default: |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1260 throw OrthancException(ErrorCode_ParameterOutOfRange); |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1261 } |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1262 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1263 if (!ok) |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1264 { |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1265 throw OrthancException(ErrorCode_BadFileFormat); |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1266 } |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1267 } |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1268 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1269 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1270 void ParsedDicomFile::ExtractPngImage(std::string& result, |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1271 unsigned int frame, |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1272 ImageExtractionMode mode) |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1273 { |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1274 ImageBuffer buffer; |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1275 ExtractImage(buffer, frame, mode); |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1276 |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1277 ImageAccessor accessor(buffer.GetConstAccessor()); |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1278 PngWriter writer; |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1279 writer.WriteToMemory(result, accessor); |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1280 } |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
1281 |
790 | 1282 } |