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