Mercurial > hg > orthanc
annotate OrthancServer/Internals/DicomImageDecoder.cpp @ 907:9b8298234254 plugins
documentation
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 19 Jun 2014 14:28:43 +0200 |
parents | e21d1a5f5934 |
children | b3f6fb1130cd |
rev | line source |
---|---|
845 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, | |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "../PrecompiledHeadersServer.h" | |
34 #include "DicomImageDecoder.h" | |
35 | |
846 | 36 |
37 /*========================================================================= | |
38 | |
39 This file is based on portions of the following project | |
40 (cf. function "DecodePsmctRle1()"): | |
41 | |
42 Program: GDCM (Grassroots DICOM). A DICOM library | |
43 Module: http://gdcm.sourceforge.net/Copyright.html | |
44 | |
863 | 45 Copyright (c) 2006-2011 Mathieu Malaterre |
46 Copyright (c) 1993-2005 CREATIS | |
47 (CREATIS = Centre de Recherche et d'Applications en Traitement de l'Image) | |
48 All rights reserved. | |
846 | 49 |
863 | 50 Redistribution and use in source and binary forms, with or without |
51 modification, are permitted provided that the following conditions are met: | |
846 | 52 |
863 | 53 * Redistributions of source code must retain the above copyright notice, |
54 this list of conditions and the following disclaimer. | |
846 | 55 |
863 | 56 * Redistributions in binary form must reproduce the above copyright notice, |
57 this list of conditions and the following disclaimer in the documentation | |
58 and/or other materials provided with the distribution. | |
846 | 59 |
863 | 60 * Neither name of Mathieu Malaterre, or CREATIS, nor the names of any |
61 contributors (CNRS, INSERM, UCB, Universite Lyon I), may be used to | |
62 endorse or promote products derived from this software without specific | |
63 prior written permission. | |
846 | 64 |
863 | 65 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' |
66 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
67 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
68 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR | |
69 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
70 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
71 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
72 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
73 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
74 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
846 | 75 |
863 | 76 =========================================================================*/ |
846 | 77 |
78 | |
79 | |
874
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
863
diff
changeset
|
80 #include "../PrecompiledHeadersServer.h" |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
863
diff
changeset
|
81 #include "DicomImageDecoder.h" |
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
863
diff
changeset
|
82 |
845 | 83 #include "../../Core/OrthancException.h" |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
84 #include "../../Core/ImageFormats/ImageProcessing.h" |
861 | 85 #include "../../Core/ImageFormats/PngWriter.h" // TODO REMOVE THIS |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
86 #include "../../Core/DicomFormat/DicomIntegerPixelAccessor.h" |
845 | 87 #include "../ToDcmtkBridge.h" |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
88 #include "../FromDcmtkBridge.h" |
845 | 89 |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
90 #include <glog/logging.h> |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
91 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
92 #include <boost/lexical_cast.hpp> |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
93 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
94 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1 |
845 | 95 #include <dcmtk/dcmjpls/djcodecd.h> |
96 #include <dcmtk/dcmjpls/djcparam.h> | |
97 #include <dcmtk/dcmjpeg/djrplol.h> | |
98 #endif | |
99 | |
100 | |
101 namespace Orthanc | |
102 { | |
852 | 103 class DicomImageDecoder::ImageSource |
104 { | |
105 private: | |
106 std::string psmct_; | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
107 std::auto_ptr<DicomIntegerPixelAccessor> slowAccessor_; |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
108 std::auto_ptr<ImageAccessor> fastAccessor_; |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
109 |
852 | 110 public: |
111 void Setup(DcmDataset& dataset, | |
112 unsigned int frame) | |
113 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
114 psmct_.clear(); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
115 slowAccessor_.reset(NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
116 fastAccessor_.reset(NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
117 |
852 | 118 // See also: http://support.dcmtk.org/wiki/dcmtk/howto/accessing-compressed-data |
119 | |
120 DicomMap m; | |
121 FromDcmtkBridge::Convert(m, dataset); | |
122 | |
123 /** | |
124 * Create an accessor to the raw values of the DICOM image. | |
125 **/ | |
126 | |
127 DcmElement* e; | |
128 if (dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_DATA), e).good() && | |
129 e != NULL) | |
130 { | |
131 Uint8* pixData = NULL; | |
132 if (e->getUint8Array(pixData) == EC_Normal) | |
133 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
134 slowAccessor_.reset(new DicomIntegerPixelAccessor(m, pixData, e->getLength())); |
852 | 135 } |
136 } | |
137 else if (DicomImageDecoder::DecodePsmctRle1(psmct_, dataset)) | |
138 { | |
139 LOG(INFO) << "The PMSCT_RLE1 decoding has succeeded"; | |
140 Uint8* pixData = NULL; | |
141 if (psmct_.size() > 0) | |
142 { | |
143 pixData = reinterpret_cast<Uint8*>(&psmct_[0]); | |
144 } | |
145 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
146 slowAccessor_.reset(new DicomIntegerPixelAccessor(m, pixData, psmct_.size())); |
852 | 147 } |
148 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
149 if (slowAccessor_.get() == NULL) |
852 | 150 { |
151 throw OrthancException(ErrorCode_BadFileFormat); | |
152 } | |
153 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
154 slowAccessor_->SetCurrentFrame(frame); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
155 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
156 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
157 /** |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
158 * If possible, create a fast ImageAccessor to the image buffer. |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
159 **/ |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
160 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
161 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
162 } |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
163 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
164 unsigned int GetWidth() const |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
165 { |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
166 assert(slowAccessor_.get() != NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
167 return slowAccessor_->GetInformation().GetWidth(); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
168 } |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
169 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
170 unsigned int GetHeight() const |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
171 { |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
172 assert(slowAccessor_.get() != NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
173 return slowAccessor_->GetInformation().GetHeight(); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
174 } |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
175 |
852 | 176 unsigned int GetChannelCount() const |
177 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
178 assert(slowAccessor_.get() != NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
179 return slowAccessor_->GetInformation().GetChannelCount(); |
852 | 180 } |
181 | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
182 const DicomIntegerPixelAccessor& GetAccessor() const |
852 | 183 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
184 assert(slowAccessor_.get() != NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
185 return *slowAccessor_; |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
186 } |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
187 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
188 bool HasFastAccessor() const |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
189 { |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
190 return fastAccessor_.get() != NULL; |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
191 } |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
192 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
193 const ImageAccessor& GetFastAccessor() const |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
194 { |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
195 assert(HasFastAccessor()); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
196 return *fastAccessor_; |
852 | 197 } |
198 }; | |
199 | |
200 | |
846 | 201 static const DicomTag DICOM_TAG_CONTENT(0x07a1, 0x100a); |
202 static const DicomTag DICOM_TAG_COMPRESSION_TYPE(0x07a1, 0x1011); | |
203 | |
204 bool DicomImageDecoder::IsPsmctRle1(DcmDataset& dataset) | |
205 { | |
206 DcmElement* e; | |
207 char* c; | |
208 | |
209 // Check whether the DICOM instance contains an image encoded with | |
210 // the PMSCT_RLE1 scheme. | |
211 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_COMPRESSION_TYPE), e).good() || | |
212 e == NULL || | |
213 !e->isaString() || | |
214 !e->getString(c).good() || | |
215 c == NULL || | |
216 strcmp("PMSCT_RLE1", c)) | |
217 { | |
218 return false; | |
219 } | |
220 else | |
221 { | |
222 return true; | |
223 } | |
224 } | |
225 | |
226 | |
227 bool DicomImageDecoder::DecodePsmctRle1(std::string& output, | |
228 DcmDataset& dataset) | |
229 { | |
230 // Check whether the DICOM instance contains an image encoded with | |
231 // the PMSCT_RLE1 scheme. | |
232 if (!IsPsmctRle1(dataset)) | |
233 { | |
234 return false; | |
235 } | |
236 | |
237 // OK, this is a custom RLE encoding from Philips. Get the pixel | |
238 // data from the appropriate private DICOM tag. | |
239 Uint8* pixData = NULL; | |
240 DcmElement* e; | |
241 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_CONTENT), e).good() || | |
242 e == NULL || | |
243 e->getUint8Array(pixData) != EC_Normal) | |
244 { | |
245 return false; | |
246 } | |
247 | |
248 // The "unsigned" below IS VERY IMPORTANT | |
249 const uint8_t* inbuffer = reinterpret_cast<const uint8_t*>(pixData); | |
250 const size_t length = e->getLength(); | |
251 | |
252 /** | |
253 * The code below is an adaptation of a sample code for GDCM by | |
254 * Mathieu Malaterre (under a BSD license). | |
255 * http://gdcm.sourceforge.net/html/rle2img_8cxx-example.html | |
256 **/ | |
257 | |
258 // RLE pass | |
259 std::vector<uint8_t> temp; | |
260 temp.reserve(length); | |
261 for (size_t i = 0; i < length; i++) | |
262 { | |
263 if (inbuffer[i] == 0xa5) | |
264 { | |
265 temp.push_back(inbuffer[i+2]); | |
266 for (uint8_t repeat = inbuffer[i + 1]; repeat != 0; repeat--) | |
267 { | |
268 temp.push_back(inbuffer[i+2]); | |
269 } | |
270 i += 2; | |
271 } | |
272 else | |
273 { | |
274 temp.push_back(inbuffer[i]); | |
275 } | |
276 } | |
277 | |
278 // Delta encoding pass | |
279 uint16_t delta = 0; | |
280 output.clear(); | |
281 output.reserve(temp.size()); | |
282 for (size_t i = 0; i < temp.size(); i++) | |
283 { | |
284 uint16_t value; | |
285 | |
286 if (temp[i] == 0x5a) | |
287 { | |
288 uint16_t v1 = temp[i + 1]; | |
289 uint16_t v2 = temp[i + 2]; | |
290 value = (v2 << 8) + v1; | |
291 i += 2; | |
292 } | |
293 else | |
294 { | |
295 value = delta + (int8_t) temp[i]; | |
296 } | |
297 | |
298 output.push_back(value & 0xff); | |
299 output.push_back(value >> 8); | |
300 delta = value; | |
301 } | |
302 | |
303 if (output.size() % 2) | |
304 { | |
305 output.resize(output.size() - 1); | |
306 } | |
307 | |
308 return true; | |
309 } | |
310 | |
311 | |
845 | 312 void DicomImageDecoder::SetupImageBuffer(ImageBuffer& target, |
313 DcmDataset& dataset) | |
314 { | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
315 DicomMap m; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
316 FromDcmtkBridge::Convert(m, dataset); |
845 | 317 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
318 DicomImageInformation info(m); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
319 PixelFormat format; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
320 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
321 if (!info.ExtractPixelFormat(format)) |
845 | 322 { |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
323 LOG(WARNING) << "Unsupported DICOM image: " << info.GetBitsStored() |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
324 << "bpp, " << info.GetChannelCount() << " channels, " |
860 | 325 << (info.IsSigned() ? "signed" : "unsigned") |
326 << (info.IsPlanar() ? ", planar" : ", non-planar"); | |
845 | 327 throw OrthancException(ErrorCode_NotImplemented); |
328 } | |
874
87791ebc1f50
download matlab images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
863
diff
changeset
|
329 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
330 target.SetHeight(info.GetHeight()); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
331 target.SetWidth(info.GetWidth()); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
332 target.SetFormat(format); |
845 | 333 } |
334 | |
335 | |
336 bool DicomImageDecoder::IsJpegLossless(const DcmDataset& dataset) | |
337 { | |
851 | 338 // http://support.dcmtk.org/docs/dcxfer_8h-source.html |
845 | 339 return (dataset.getOriginalXfer() == EXS_JPEGLSLossless || |
340 dataset.getOriginalXfer() == EXS_JPEGLSLossy); | |
341 } | |
342 | |
343 | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
344 bool DicomImageDecoder::IsUncompressedImage(const DcmDataset& dataset) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
345 { |
851 | 346 // http://support.dcmtk.org/docs/dcxfer_8h-source.html |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
347 return (dataset.getOriginalXfer() == EXS_Unknown || |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
348 dataset.getOriginalXfer() == EXS_LittleEndianImplicit || |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
349 dataset.getOriginalXfer() == EXS_BigEndianImplicit || |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
350 dataset.getOriginalXfer() == EXS_LittleEndianExplicit || |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
351 dataset.getOriginalXfer() == EXS_BigEndianExplicit); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
352 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
353 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
354 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
355 template <typename PixelType> |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
356 static void CopyPixels(ImageAccessor& target, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
357 const DicomIntegerPixelAccessor& source) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
358 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
359 const PixelType minValue = std::numeric_limits<PixelType>::min(); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
360 const PixelType maxValue = std::numeric_limits<PixelType>::max(); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
361 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
362 for (unsigned int y = 0; y < source.GetInformation().GetHeight(); y++) |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
363 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
364 PixelType* pixel = reinterpret_cast<PixelType*>(target.GetRow(y)); |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
365 for (unsigned int x = 0; x < source.GetInformation().GetWidth(); x++) |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
366 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
367 for (unsigned int c = 0; c < source.GetInformation().GetChannelCount(); c++, pixel++) |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
368 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
369 int32_t v = source.GetValue(x, y, c); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
370 if (v < static_cast<int32_t>(minValue)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
371 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
372 *pixel = minValue; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
373 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
374 else if (v > static_cast<int32_t>(maxValue)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
375 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
376 *pixel = maxValue; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
377 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
378 else |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
379 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
380 *pixel = static_cast<PixelType>(v); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
381 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
382 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
383 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
384 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
385 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
386 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
387 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
388 void DicomImageDecoder::DecodeUncompressedImage(ImageBuffer& target, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
389 DcmDataset& dataset, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
390 unsigned int frame) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
391 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
392 if (!IsUncompressedImage(dataset)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
393 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
394 throw OrthancException(ErrorCode_BadParameterType); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
395 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
396 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
397 DecodeUncompressedImageInternal(target, dataset, frame); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
398 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
399 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
400 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
401 void DicomImageDecoder::DecodeUncompressedImageInternal(ImageBuffer& target, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
402 DcmDataset& dataset, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
403 unsigned int frame) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
404 { |
852 | 405 ImageSource source; |
406 source.Setup(dataset, frame); | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
407 |
858
ebc41566f742
removed unneeded check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
408 |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
409 /** |
858
ebc41566f742
removed unneeded check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
410 * Resize the target image. |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
411 **/ |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
412 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
413 SetupImageBuffer(target, dataset); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
414 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
415 if (source.GetWidth() != target.GetWidth() || |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
416 source.GetHeight() != target.GetHeight()) |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
417 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
418 throw OrthancException(ErrorCode_InternalError); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
419 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
420 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
421 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
422 /** |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
423 * If the format of the DICOM buffer is natively supported, use a |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
424 * direct access to copy its values. |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
425 **/ |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
426 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
427 ImageAccessor targetAccessor(target.GetAccessor()); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
428 const DicomImageInformation& info = source.GetAccessor().GetInformation(); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
429 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
430 bool fastVersionSuccess = false; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
431 PixelFormat sourceFormat; |
863 | 432 if (!info.IsPlanar() && |
433 info.ExtractPixelFormat(sourceFormat)) | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
434 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
435 try |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
436 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
437 ImageAccessor sourceImage; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
438 sourceImage.AssignReadOnly(sourceFormat, |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
439 info.GetWidth(), |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
440 info.GetHeight(), |
860 | 441 info.GetWidth() * GetBytesPerPixel(sourceFormat), |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
442 source.GetAccessor().GetPixelData()); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
443 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
444 ImageProcessing::Convert(targetAccessor, sourceImage); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
445 ImageProcessing::ShiftRight(targetAccessor, info.GetShift()); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
446 fastVersionSuccess = true; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
447 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
448 catch (OrthancException&) |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
449 { |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
450 // Unsupported conversion, use the slow version |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
451 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
452 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
453 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
454 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
455 /** |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
456 * Slow version : loop over the DICOM buffer, storing its value |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
457 * into the target image. |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
458 **/ |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
459 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
460 if (!fastVersionSuccess) |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
461 { |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
462 switch (target.GetFormat()) |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
463 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
464 case PixelFormat_RGB24: |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
465 case PixelFormat_RGBA32: |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
466 case PixelFormat_Grayscale8: |
863 | 467 CopyPixels<uint8_t>(targetAccessor, source.GetAccessor()); |
468 break; | |
860 | 469 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
470 case PixelFormat_Grayscale16: |
863 | 471 CopyPixels<uint16_t>(targetAccessor, source.GetAccessor()); |
472 break; | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
473 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
474 case PixelFormat_SignedGrayscale16: |
863 | 475 CopyPixels<int16_t>(targetAccessor, source.GetAccessor()); |
476 break; | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
477 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
478 default: |
863 | 479 throw OrthancException(ErrorCode_InternalError); |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
480 } |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
481 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
482 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
483 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
484 |
845 | 485 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1 |
486 void DicomImageDecoder::DecodeJpegLossless(ImageBuffer& target, | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
487 DcmDataset& dataset, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
488 unsigned int frame) |
845 | 489 { |
490 if (!IsJpegLossless(dataset)) | |
491 { | |
492 throw OrthancException(ErrorCode_BadParameterType); | |
493 } | |
494 | |
495 DcmElement *element = NULL; | |
496 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_DATA), element).good()) | |
497 { | |
498 throw OrthancException(ErrorCode_BadFileFormat); | |
499 } | |
500 | |
501 DcmPixelData& pixelData = dynamic_cast<DcmPixelData&>(*element); | |
502 DcmPixelSequence* pixelSequence = NULL; | |
503 if (!pixelData.getEncapsulatedRepresentation | |
504 (dataset.getOriginalXfer(), NULL, pixelSequence).good()) | |
505 { | |
506 throw OrthancException(ErrorCode_BadFileFormat); | |
507 } | |
508 | |
509 SetupImageBuffer(target, dataset); | |
510 | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
511 ImageAccessor targetAccessor(target.GetAccessor()); |
845 | 512 |
513 /** | |
514 * The "DJLSLosslessDecoder" and "DJLSNearLosslessDecoder" in DCMTK | |
515 * are exactly the same, except for the "supportedTransferSyntax()" | |
516 * virtual function. | |
517 * http://support.dcmtk.org/docs/classDJLSDecoderBase.html | |
518 **/ | |
519 | |
520 DJLSLosslessDecoder decoder; DJLSCodecParameter parameters; | |
521 //DJLSNearLosslessDecoder decoder; DJLSCodecParameter parameters; | |
522 | |
523 Uint32 startFragment = 0; // Default | |
524 OFString decompressedColorModel; // Out | |
525 DJ_RPLossless representationParameter; | |
526 OFCondition c = decoder.decodeFrame(&representationParameter, pixelSequence, ¶meters, | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
527 &dataset, frame, startFragment, targetAccessor.GetBuffer(), |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
528 targetAccessor.GetSize(), decompressedColorModel); |
845 | 529 |
530 if (!c.good()) | |
531 { | |
532 throw OrthancException(ErrorCode_InternalError); | |
533 } | |
534 } | |
535 #endif | |
846 | 536 |
537 | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
538 |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
539 |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
540 bool DicomImageDecoder::Decode(ImageBuffer& target, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
541 DcmDataset& dataset, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
542 unsigned int frame) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
543 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
544 if (IsUncompressedImage(dataset)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
545 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
546 DecodeUncompressedImage(target, dataset, frame); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
547 return true; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
548 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
549 |
852 | 550 |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
551 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
552 if (IsJpegLossless(dataset)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
553 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
554 LOG(INFO) << "Decoding a JPEG-LS image"; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
555 DecodeJpegLossless(target, dataset, frame); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
556 return true; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
557 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
558 #endif |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
559 |
852 | 560 |
561 #if ORTHANC_JPEG_ENABLED == 1 | |
562 // TODO Implement this part to speed up JPEG decompression | |
563 #endif | |
564 | |
565 | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
566 /** |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
567 * This DICOM image format is not natively supported by |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
568 * Orthanc. As a last resort, try and decode it through |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
569 * DCMTK. This will result in higher memory consumption. This is |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
570 * actually the second example of the following page: |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
571 * http://support.dcmtk.org/docs/mod_dcmjpeg.html#Examples |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
572 **/ |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
573 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
574 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
575 LOG(INFO) << "Using DCMTK to decode a compressed image"; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
576 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
577 std::auto_ptr<DcmDataset> converted(dynamic_cast<DcmDataset*>(dataset.clone())); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
578 converted->chooseRepresentation(EXS_LittleEndianExplicit, NULL); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
579 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
580 if (converted->canWriteXfer(EXS_LittleEndianExplicit)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
581 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
582 DecodeUncompressedImageInternal(target, *converted, frame); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
583 return true; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
584 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
585 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
586 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
587 return false; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
588 } |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
589 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
590 |
863 | 591 bool DicomImageDecoder::DecodeAndTruncate(ImageBuffer& target, |
592 DcmDataset& dataset, | |
593 unsigned int frame, | |
594 PixelFormat format) | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
595 { |
863 | 596 // TODO Special case for uncompressed images |
597 | |
598 ImageBuffer source; | |
599 if (!Decode(source, dataset, frame)) | |
600 { | |
601 return false; | |
602 } | |
603 | |
604 if (source.GetFormat() == format) | |
605 { | |
606 // No conversion is required, return the temporary image | |
607 target.AcquireOwnership(source); | |
608 return true; | |
609 } | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
610 |
863 | 611 target.SetFormat(format); |
612 target.SetWidth(source.GetWidth()); | |
613 target.SetHeight(source.GetHeight()); | |
614 | |
615 ImageAccessor targetAccessor(target.GetAccessor()); | |
616 ImageAccessor sourceAccessor(source.GetAccessor()); | |
617 ImageProcessing::Convert(targetAccessor, sourceAccessor); | |
618 | |
619 return true; | |
620 } | |
621 | |
622 | |
623 bool DicomImageDecoder::DecodePreview(ImageBuffer& target, | |
624 DcmDataset& dataset, | |
625 unsigned int frame) | |
626 { | |
627 // TODO Special case for uncompressed images | |
628 | |
629 ImageBuffer source; | |
630 if (!Decode(source, dataset, frame)) | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
631 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
632 return false; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
633 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
634 |
863 | 635 switch (source.GetFormat()) |
861 | 636 { |
863 | 637 case PixelFormat_RGB24: |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
638 { |
863 | 639 // Directly return color images (RGB) |
640 target.AcquireOwnership(source); | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
641 return true; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
642 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
643 |
863 | 644 case PixelFormat_Grayscale8: |
645 case PixelFormat_Grayscale16: | |
646 case PixelFormat_SignedGrayscale16: | |
647 { | |
648 // Grayscale image: Stretch its dynamics to the [0,255] range | |
649 target.SetFormat(PixelFormat_Grayscale8); | |
650 target.SetWidth(source.GetWidth()); | |
651 target.SetHeight(source.GetHeight()); | |
652 | |
653 ImageAccessor targetAccessor(target.GetAccessor()); | |
654 ImageAccessor sourceAccessor(source.GetAccessor()); | |
655 | |
656 int64_t a, b; | |
657 ImageProcessing::GetMinMaxValue(a, b, sourceAccessor); | |
658 | |
659 if (a == b) | |
660 { | |
661 ImageProcessing::Set(targetAccessor, 0); | |
662 } | |
663 else | |
664 { | |
876 | 665 ImageProcessing::ShiftScale(sourceAccessor, static_cast<float>(-a), 255.0f / static_cast<float>(b - a)); |
863 | 666 |
667 if (source.GetFormat() == PixelFormat_Grayscale8) | |
668 { | |
669 target.AcquireOwnership(source); | |
670 } | |
671 else | |
672 { | |
673 ImageProcessing::Convert(targetAccessor, sourceAccessor); | |
674 } | |
675 } | |
676 | |
677 return true; | |
678 } | |
679 | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
680 default: |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
681 throw OrthancException(ErrorCode_NotImplemented); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
682 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
683 } |
845 | 684 } |