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