Mercurial > hg > orthanc
annotate OrthancServer/Internals/DicomImageDecoder.cpp @ 853:839be3022203 jpeg
DicomImageInformation
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 06 Jun 2014 11:45:16 +0200 |
parents | 5944b8b80842 |
children | ff530685e46a |
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 | |
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. | |
49 | |
50 Redistribution and use in source and binary forms, with or without | |
51 modification, are permitted provided that the following conditions are met: | |
52 | |
53 * Redistributions of source code must retain the above copyright notice, | |
54 this list of conditions and the following disclaimer. | |
55 | |
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. | |
59 | |
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. | |
64 | |
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. | |
75 | |
76 =========================================================================*/ | |
77 | |
78 | |
79 | |
845 | 80 #include "../../Core/OrthancException.h" |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
81 #include "../../Core/DicomFormat/DicomIntegerPixelAccessor.h" |
845 | 82 #include "../ToDcmtkBridge.h" |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
83 #include "../FromDcmtkBridge.h" |
845 | 84 |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
85 #include <glog/logging.h> |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
86 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
87 #include <boost/lexical_cast.hpp> |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
88 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
89 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1 |
845 | 90 #include <dcmtk/dcmjpls/djcodecd.h> |
91 #include <dcmtk/dcmjpls/djcparam.h> | |
92 #include <dcmtk/dcmjpeg/djrplol.h> | |
93 #endif | |
94 | |
95 | |
96 namespace Orthanc | |
97 { | |
852 | 98 class DicomImageDecoder::ImageSource |
99 { | |
100 private: | |
101 std::string psmct_; | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
102 std::auto_ptr<DicomIntegerPixelAccessor> slowAccessor_; |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
103 std::auto_ptr<ImageAccessor> fastAccessor_; |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
104 |
852 | 105 public: |
106 void Setup(DcmDataset& dataset, | |
107 unsigned int frame) | |
108 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
109 psmct_.clear(); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
110 slowAccessor_.reset(NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
111 fastAccessor_.reset(NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
112 |
852 | 113 // See also: http://support.dcmtk.org/wiki/dcmtk/howto/accessing-compressed-data |
114 | |
115 DicomMap m; | |
116 FromDcmtkBridge::Convert(m, dataset); | |
117 | |
118 /** | |
119 * Create an accessor to the raw values of the DICOM image. | |
120 **/ | |
121 | |
122 DcmElement* e; | |
123 if (dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_DATA), e).good() && | |
124 e != NULL) | |
125 { | |
126 Uint8* pixData = NULL; | |
127 if (e->getUint8Array(pixData) == EC_Normal) | |
128 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
129 slowAccessor_.reset(new DicomIntegerPixelAccessor(m, pixData, e->getLength())); |
852 | 130 } |
131 } | |
132 else if (DicomImageDecoder::DecodePsmctRle1(psmct_, dataset)) | |
133 { | |
134 LOG(INFO) << "The PMSCT_RLE1 decoding has succeeded"; | |
135 Uint8* pixData = NULL; | |
136 if (psmct_.size() > 0) | |
137 { | |
138 pixData = reinterpret_cast<Uint8*>(&psmct_[0]); | |
139 } | |
140 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
141 slowAccessor_.reset(new DicomIntegerPixelAccessor(m, pixData, psmct_.size())); |
852 | 142 } |
143 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
144 if (slowAccessor_.get() == NULL) |
852 | 145 { |
146 throw OrthancException(ErrorCode_BadFileFormat); | |
147 } | |
148 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
149 slowAccessor_->SetCurrentFrame(frame); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
150 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
151 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
152 /** |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
153 * If possible, create a fast ImageAccessor to the image buffer. |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
154 **/ |
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 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
159 unsigned int GetWidth() const |
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 assert(slowAccessor_.get() != NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
162 return slowAccessor_->GetInformation().GetWidth(); |
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 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
165 unsigned int GetHeight() const |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
166 { |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
167 assert(slowAccessor_.get() != NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
168 return slowAccessor_->GetInformation().GetHeight(); |
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 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
171 unsigned int GetBytesPerPixel() const |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
172 { |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
173 assert(slowAccessor_.get() != NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
174 return slowAccessor_->GetInformation().GetBytesPerPixel(); |
852 | 175 } |
176 | |
177 unsigned int GetChannelCount() const | |
178 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
179 assert(slowAccessor_.get() != NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
180 return slowAccessor_->GetInformation().GetChannelCount(); |
852 | 181 } |
182 | |
183 const DicomIntegerPixelAccessor GetAccessor() const | |
184 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
185 assert(slowAccessor_.get() != NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
186 return *slowAccessor_; |
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 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
189 bool HasFastAccessor() const |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
190 { |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
191 return fastAccessor_.get() != NULL; |
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 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
194 const ImageAccessor& GetFastAccessor() const |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
195 { |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
196 assert(HasFastAccessor()); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
197 return *fastAccessor_; |
852 | 198 } |
199 }; | |
200 | |
201 | |
846 | 202 static const DicomTag DICOM_TAG_CONTENT(0x07a1, 0x100a); |
203 static const DicomTag DICOM_TAG_COMPRESSION_TYPE(0x07a1, 0x1011); | |
204 | |
205 bool DicomImageDecoder::IsPsmctRle1(DcmDataset& dataset) | |
206 { | |
207 DcmElement* e; | |
208 char* c; | |
209 | |
210 // Check whether the DICOM instance contains an image encoded with | |
211 // the PMSCT_RLE1 scheme. | |
212 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_COMPRESSION_TYPE), e).good() || | |
213 e == NULL || | |
214 !e->isaString() || | |
215 !e->getString(c).good() || | |
216 c == NULL || | |
217 strcmp("PMSCT_RLE1", c)) | |
218 { | |
219 return false; | |
220 } | |
221 else | |
222 { | |
223 return true; | |
224 } | |
225 } | |
226 | |
227 | |
228 bool DicomImageDecoder::DecodePsmctRle1(std::string& output, | |
229 DcmDataset& dataset) | |
230 { | |
231 // Check whether the DICOM instance contains an image encoded with | |
232 // the PMSCT_RLE1 scheme. | |
233 if (!IsPsmctRle1(dataset)) | |
234 { | |
235 return false; | |
236 } | |
237 | |
238 // OK, this is a custom RLE encoding from Philips. Get the pixel | |
239 // data from the appropriate private DICOM tag. | |
240 Uint8* pixData = NULL; | |
241 DcmElement* e; | |
242 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_CONTENT), e).good() || | |
243 e == NULL || | |
244 e->getUint8Array(pixData) != EC_Normal) | |
245 { | |
246 return false; | |
247 } | |
248 | |
249 // The "unsigned" below IS VERY IMPORTANT | |
250 const uint8_t* inbuffer = reinterpret_cast<const uint8_t*>(pixData); | |
251 const size_t length = e->getLength(); | |
252 | |
253 /** | |
254 * The code below is an adaptation of a sample code for GDCM by | |
255 * Mathieu Malaterre (under a BSD license). | |
256 * http://gdcm.sourceforge.net/html/rle2img_8cxx-example.html | |
257 **/ | |
258 | |
259 // RLE pass | |
260 std::vector<uint8_t> temp; | |
261 temp.reserve(length); | |
262 for (size_t i = 0; i < length; i++) | |
263 { | |
264 if (inbuffer[i] == 0xa5) | |
265 { | |
266 temp.push_back(inbuffer[i+2]); | |
267 for (uint8_t repeat = inbuffer[i + 1]; repeat != 0; repeat--) | |
268 { | |
269 temp.push_back(inbuffer[i+2]); | |
270 } | |
271 i += 2; | |
272 } | |
273 else | |
274 { | |
275 temp.push_back(inbuffer[i]); | |
276 } | |
277 } | |
278 | |
279 // Delta encoding pass | |
280 uint16_t delta = 0; | |
281 output.clear(); | |
282 output.reserve(temp.size()); | |
283 for (size_t i = 0; i < temp.size(); i++) | |
284 { | |
285 uint16_t value; | |
286 | |
287 if (temp[i] == 0x5a) | |
288 { | |
289 uint16_t v1 = temp[i + 1]; | |
290 uint16_t v2 = temp[i + 2]; | |
291 value = (v2 << 8) + v1; | |
292 i += 2; | |
293 } | |
294 else | |
295 { | |
296 value = delta + (int8_t) temp[i]; | |
297 } | |
298 | |
299 output.push_back(value & 0xff); | |
300 output.push_back(value >> 8); | |
301 delta = value; | |
302 } | |
303 | |
304 if (output.size() % 2) | |
305 { | |
306 output.resize(output.size() - 1); | |
307 } | |
308 | |
309 return true; | |
310 } | |
311 | |
312 | |
845 | 313 void DicomImageDecoder::SetupImageBuffer(ImageBuffer& target, |
314 DcmDataset& dataset) | |
315 { | |
316 OFString value; | |
317 | |
318 if (!dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_COLUMNS), value).good()) | |
319 { | |
320 throw OrthancException(ErrorCode_BadFileFormat); | |
321 } | |
322 | |
323 unsigned int width = boost::lexical_cast<unsigned int>(value.c_str()); | |
324 | |
325 if (!dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_ROWS), value).good()) | |
326 { | |
327 throw OrthancException(ErrorCode_BadFileFormat); | |
328 } | |
329 | |
330 unsigned int height = boost::lexical_cast<unsigned int>(value.c_str()); | |
331 | |
332 if (!dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_BITS_STORED), value).good()) | |
333 { | |
334 throw OrthancException(ErrorCode_BadFileFormat); | |
335 } | |
336 | |
337 unsigned int bitsStored = boost::lexical_cast<unsigned int>(value.c_str()); | |
338 | |
339 if (!dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_REPRESENTATION), value).good()) | |
340 { | |
341 throw OrthancException(ErrorCode_BadFileFormat); | |
342 } | |
343 | |
344 bool isSigned = (boost::lexical_cast<unsigned int>(value.c_str()) != 0); | |
345 | |
346 unsigned int samplesPerPixel = 1; // By default | |
347 if (dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_SAMPLES_PER_PIXEL), value).good()) | |
348 { | |
349 samplesPerPixel = boost::lexical_cast<unsigned int>(value.c_str()); | |
350 } | |
351 | |
352 target.SetHeight(height); | |
353 target.SetWidth(width); | |
354 | |
355 if (bitsStored == 8 && samplesPerPixel == 1 && !isSigned) | |
356 { | |
357 target.SetFormat(PixelFormat_Grayscale8); | |
358 } | |
359 else if (bitsStored == 8 && samplesPerPixel == 3 && !isSigned) | |
360 { | |
361 target.SetFormat(PixelFormat_RGB24); | |
362 } | |
850
9ee2e7a5efaf
support of JPEG images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
847
diff
changeset
|
363 else if (bitsStored >= 9 && bitsStored <= 16 && samplesPerPixel == 1 && !isSigned) |
845 | 364 { |
365 target.SetFormat(PixelFormat_Grayscale16); | |
366 } | |
850
9ee2e7a5efaf
support of JPEG images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
847
diff
changeset
|
367 else if (bitsStored >= 9 && bitsStored <= 16 && samplesPerPixel == 1 && isSigned) |
845 | 368 { |
369 target.SetFormat(PixelFormat_SignedGrayscale16); | |
370 } | |
371 else | |
372 { | |
850
9ee2e7a5efaf
support of JPEG images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
847
diff
changeset
|
373 LOG(WARNING) << "Unsupported DICOM image: " << bitsStored << "bpp, " |
9ee2e7a5efaf
support of JPEG images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
847
diff
changeset
|
374 << samplesPerPixel << " channels, " << (isSigned ? "signed" : "unsigned"); |
845 | 375 throw OrthancException(ErrorCode_NotImplemented); |
376 } | |
377 } | |
378 | |
379 | |
380 bool DicomImageDecoder::IsJpegLossless(const DcmDataset& dataset) | |
381 { | |
851 | 382 // http://support.dcmtk.org/docs/dcxfer_8h-source.html |
845 | 383 return (dataset.getOriginalXfer() == EXS_JPEGLSLossless || |
384 dataset.getOriginalXfer() == EXS_JPEGLSLossy); | |
385 } | |
386 | |
387 | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
388 bool DicomImageDecoder::IsUncompressedImage(const DcmDataset& dataset) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
389 { |
851 | 390 // 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
|
391 return (dataset.getOriginalXfer() == EXS_Unknown || |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
392 dataset.getOriginalXfer() == EXS_LittleEndianImplicit || |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
393 dataset.getOriginalXfer() == EXS_BigEndianImplicit || |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
394 dataset.getOriginalXfer() == EXS_LittleEndianExplicit || |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
395 dataset.getOriginalXfer() == EXS_BigEndianExplicit); |
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 |
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 template <typename PixelType> |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
400 static void CopyPixels(ImageAccessor& target, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
401 const DicomIntegerPixelAccessor& source) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
402 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
403 const PixelType minValue = std::numeric_limits<PixelType>::min(); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
404 const PixelType maxValue = std::numeric_limits<PixelType>::max(); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
405 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
406 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
|
407 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
408 PixelType* pixel = reinterpret_cast<PixelType*>(target.GetRow(y)); |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
409 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
|
410 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
411 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
|
412 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
413 int32_t v = source.GetValue(x, y, c); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
414 if (v < static_cast<int32_t>(minValue)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
415 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
416 *pixel = minValue; |
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 else if (v > static_cast<int32_t>(maxValue)) |
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 *pixel = maxValue; |
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 else |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
423 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
424 *pixel = static_cast<PixelType>(v); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
425 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
426 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
427 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
428 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
429 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
430 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
431 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
432 void DicomImageDecoder::DecodeUncompressedImage(ImageBuffer& target, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
433 DcmDataset& dataset, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
434 unsigned int frame) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
435 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
436 if (!IsUncompressedImage(dataset)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
437 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
438 throw OrthancException(ErrorCode_BadParameterType); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
439 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
440 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
441 DecodeUncompressedImageInternal(target, dataset, frame); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
442 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
443 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
444 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
445 void DicomImageDecoder::DecodeUncompressedImageInternal(ImageBuffer& target, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
446 DcmDataset& dataset, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
447 unsigned int frame) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
448 { |
852 | 449 ImageSource source; |
450 source.Setup(dataset, frame); | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
451 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
452 /** |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
453 * Resize the target image, with some sanity checks. |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
454 **/ |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
455 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
456 SetupImageBuffer(target, dataset); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
457 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
458 if (target.GetWidth() != target.GetWidth() || |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
459 target.GetHeight() != target.GetHeight()) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
460 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
461 throw OrthancException(ErrorCode_InternalError); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
462 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
463 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
464 bool ok; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
465 switch (target.GetFormat()) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
466 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
467 case PixelFormat_RGB24: |
852 | 468 ok = source.GetChannelCount() == 3; |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
469 break; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
470 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
471 case PixelFormat_RGBA32: |
852 | 472 ok = source.GetChannelCount() == 4; |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
473 break; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
474 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
475 case PixelFormat_Grayscale8: |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
476 case PixelFormat_Grayscale16: |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
477 case PixelFormat_SignedGrayscale16: |
852 | 478 ok = source.GetChannelCount() == 1; |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
479 break; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
480 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
481 default: |
852 | 482 ok = false; |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
483 break; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
484 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
485 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
486 if (!ok) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
487 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
488 throw OrthancException(ErrorCode_InternalError); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
489 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
490 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
491 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
492 /** |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
493 * Loop over the DICOM buffer, storing its value into the target |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
494 * image. |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
495 **/ |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
496 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
497 ImageAccessor accessor(target.GetAccessor()); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
498 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
499 switch (target.GetFormat()) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
500 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
501 case PixelFormat_RGB24: |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
502 case PixelFormat_RGBA32: |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
503 case PixelFormat_Grayscale8: |
852 | 504 CopyPixels<uint8_t>(accessor, source.GetAccessor()); |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
505 break; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
506 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
507 case PixelFormat_Grayscale16: |
852 | 508 CopyPixels<uint16_t>(accessor, source.GetAccessor()); |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
509 break; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
510 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
511 case PixelFormat_SignedGrayscale16: |
852 | 512 CopyPixels<int16_t>(accessor, source.GetAccessor()); |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
513 break; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
514 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
515 default: |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
516 throw OrthancException(ErrorCode_InternalError); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
517 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
518 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
519 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
520 |
845 | 521 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1 |
522 void DicomImageDecoder::DecodeJpegLossless(ImageBuffer& target, | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
523 DcmDataset& dataset, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
524 unsigned int frame) |
845 | 525 { |
526 if (!IsJpegLossless(dataset)) | |
527 { | |
528 throw OrthancException(ErrorCode_BadParameterType); | |
529 } | |
530 | |
531 DcmElement *element = NULL; | |
532 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_DATA), element).good()) | |
533 { | |
534 throw OrthancException(ErrorCode_BadFileFormat); | |
535 } | |
536 | |
537 DcmPixelData& pixelData = dynamic_cast<DcmPixelData&>(*element); | |
538 DcmPixelSequence* pixelSequence = NULL; | |
539 if (!pixelData.getEncapsulatedRepresentation | |
540 (dataset.getOriginalXfer(), NULL, pixelSequence).good()) | |
541 { | |
542 throw OrthancException(ErrorCode_BadFileFormat); | |
543 } | |
544 | |
545 SetupImageBuffer(target, dataset); | |
546 | |
547 ImageAccessor accessor(target.GetAccessor()); | |
548 | |
549 /** | |
550 * The "DJLSLosslessDecoder" and "DJLSNearLosslessDecoder" in DCMTK | |
551 * are exactly the same, except for the "supportedTransferSyntax()" | |
552 * virtual function. | |
553 * http://support.dcmtk.org/docs/classDJLSDecoderBase.html | |
554 **/ | |
555 | |
556 DJLSLosslessDecoder decoder; DJLSCodecParameter parameters; | |
557 //DJLSNearLosslessDecoder decoder; DJLSCodecParameter parameters; | |
558 | |
559 Uint32 startFragment = 0; // Default | |
560 OFString decompressedColorModel; // Out | |
561 DJ_RPLossless representationParameter; | |
562 OFCondition c = decoder.decodeFrame(&representationParameter, pixelSequence, ¶meters, | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
563 &dataset, frame, startFragment, accessor.GetBuffer(), |
845 | 564 accessor.GetSize(), decompressedColorModel); |
565 | |
566 if (!c.good()) | |
567 { | |
568 throw OrthancException(ErrorCode_InternalError); | |
569 } | |
570 } | |
571 #endif | |
846 | 572 |
573 | |
847
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 bool DicomImageDecoder::Decode(ImageBuffer& target, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
576 DcmDataset& dataset, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
577 unsigned int frame) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
578 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
579 if (IsUncompressedImage(dataset)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
580 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
581 DecodeUncompressedImage(target, dataset, frame); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
582 return true; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
583 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
584 |
852 | 585 |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
586 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
587 if (IsJpegLossless(dataset)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
588 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
589 LOG(INFO) << "Decoding a JPEG-LS image"; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
590 DecodeJpegLossless(target, dataset, frame); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
591 return true; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
592 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
593 #endif |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
594 |
852 | 595 |
596 #if ORTHANC_JPEG_ENABLED == 1 | |
597 // TODO Implement this part to speed up JPEG decompression | |
598 #endif | |
599 | |
600 | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
601 /** |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
602 * 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
|
603 * 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
|
604 * 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
|
605 * actually the second example of the following page: |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
606 * 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
|
607 **/ |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
608 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
609 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
610 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
|
611 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
612 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
|
613 converted->chooseRepresentation(EXS_LittleEndianExplicit, NULL); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
614 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
615 if (converted->canWriteXfer(EXS_LittleEndianExplicit)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
616 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
617 DecodeUncompressedImageInternal(target, *converted, frame); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
618 return true; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
619 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
620 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
621 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
622 return false; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
623 } |
845 | 624 } |