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