Mercurial > hg > orthanc
annotate OrthancServer/Internals/DicomImageDecoder.cpp @ 863:3c0d0836f704 jpeg
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 10 Jun 2014 17:20:33 +0200 |
parents | a546b05a43da |
children | 87791ebc1f50 |
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 | |
845 | 80 #include "../../Core/OrthancException.h" |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
81 #include "../../Core/ImageFormats/ImageProcessing.h" |
861 | 82 #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
|
83 #include "../../Core/DicomFormat/DicomIntegerPixelAccessor.h" |
845 | 84 #include "../ToDcmtkBridge.h" |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
85 #include "../FromDcmtkBridge.h" |
845 | 86 |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
87 #include <glog/logging.h> |
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 #include <boost/lexical_cast.hpp> |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
90 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
91 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1 |
845 | 92 #include <dcmtk/dcmjpls/djcodecd.h> |
93 #include <dcmtk/dcmjpls/djcparam.h> | |
94 #include <dcmtk/dcmjpeg/djrplol.h> | |
95 #endif | |
96 | |
97 | |
98 namespace Orthanc | |
99 { | |
852 | 100 class DicomImageDecoder::ImageSource |
101 { | |
102 private: | |
103 std::string psmct_; | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
104 std::auto_ptr<DicomIntegerPixelAccessor> slowAccessor_; |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
105 std::auto_ptr<ImageAccessor> fastAccessor_; |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
106 |
852 | 107 public: |
108 void Setup(DcmDataset& dataset, | |
109 unsigned int frame) | |
110 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
111 psmct_.clear(); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
112 slowAccessor_.reset(NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
113 fastAccessor_.reset(NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
114 |
852 | 115 // See also: http://support.dcmtk.org/wiki/dcmtk/howto/accessing-compressed-data |
116 | |
117 DicomMap m; | |
118 FromDcmtkBridge::Convert(m, dataset); | |
119 | |
120 /** | |
121 * Create an accessor to the raw values of the DICOM image. | |
122 **/ | |
123 | |
124 DcmElement* e; | |
125 if (dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_DATA), e).good() && | |
126 e != NULL) | |
127 { | |
128 Uint8* pixData = NULL; | |
129 if (e->getUint8Array(pixData) == EC_Normal) | |
130 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
131 slowAccessor_.reset(new DicomIntegerPixelAccessor(m, pixData, e->getLength())); |
852 | 132 } |
133 } | |
134 else if (DicomImageDecoder::DecodePsmctRle1(psmct_, dataset)) | |
135 { | |
136 LOG(INFO) << "The PMSCT_RLE1 decoding has succeeded"; | |
137 Uint8* pixData = NULL; | |
138 if (psmct_.size() > 0) | |
139 { | |
140 pixData = reinterpret_cast<Uint8*>(&psmct_[0]); | |
141 } | |
142 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
143 slowAccessor_.reset(new DicomIntegerPixelAccessor(m, pixData, psmct_.size())); |
852 | 144 } |
145 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
146 if (slowAccessor_.get() == NULL) |
852 | 147 { |
148 throw OrthancException(ErrorCode_BadFileFormat); | |
149 } | |
150 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
151 slowAccessor_->SetCurrentFrame(frame); |
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 |
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 * If possible, create a fast ImageAccessor to the image buffer. |
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 } |
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 unsigned int GetWidth() const |
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 assert(slowAccessor_.get() != NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
164 return slowAccessor_->GetInformation().GetWidth(); |
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 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
167 unsigned int GetHeight() const |
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 assert(slowAccessor_.get() != NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
170 return slowAccessor_->GetInformation().GetHeight(); |
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 |
852 | 173 unsigned int GetChannelCount() const |
174 { | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
175 assert(slowAccessor_.get() != NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
176 return slowAccessor_->GetInformation().GetChannelCount(); |
852 | 177 } |
178 | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
179 const DicomIntegerPixelAccessor& GetAccessor() const |
852 | 180 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
181 assert(slowAccessor_.get() != NULL); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
182 return *slowAccessor_; |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
183 } |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
184 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
185 bool HasFastAccessor() const |
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 return fastAccessor_.get() != NULL; |
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 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
190 const ImageAccessor& GetFastAccessor() const |
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 assert(HasFastAccessor()); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
193 return *fastAccessor_; |
852 | 194 } |
195 }; | |
196 | |
197 | |
846 | 198 static const DicomTag DICOM_TAG_CONTENT(0x07a1, 0x100a); |
199 static const DicomTag DICOM_TAG_COMPRESSION_TYPE(0x07a1, 0x1011); | |
200 | |
201 bool DicomImageDecoder::IsPsmctRle1(DcmDataset& dataset) | |
202 { | |
203 DcmElement* e; | |
204 char* c; | |
205 | |
206 // Check whether the DICOM instance contains an image encoded with | |
207 // the PMSCT_RLE1 scheme. | |
208 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_COMPRESSION_TYPE), e).good() || | |
209 e == NULL || | |
210 !e->isaString() || | |
211 !e->getString(c).good() || | |
212 c == NULL || | |
213 strcmp("PMSCT_RLE1", c)) | |
214 { | |
215 return false; | |
216 } | |
217 else | |
218 { | |
219 return true; | |
220 } | |
221 } | |
222 | |
223 | |
224 bool DicomImageDecoder::DecodePsmctRle1(std::string& output, | |
225 DcmDataset& dataset) | |
226 { | |
227 // Check whether the DICOM instance contains an image encoded with | |
228 // the PMSCT_RLE1 scheme. | |
229 if (!IsPsmctRle1(dataset)) | |
230 { | |
231 return false; | |
232 } | |
233 | |
234 // OK, this is a custom RLE encoding from Philips. Get the pixel | |
235 // data from the appropriate private DICOM tag. | |
236 Uint8* pixData = NULL; | |
237 DcmElement* e; | |
238 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_CONTENT), e).good() || | |
239 e == NULL || | |
240 e->getUint8Array(pixData) != EC_Normal) | |
241 { | |
242 return false; | |
243 } | |
244 | |
245 // The "unsigned" below IS VERY IMPORTANT | |
246 const uint8_t* inbuffer = reinterpret_cast<const uint8_t*>(pixData); | |
247 const size_t length = e->getLength(); | |
248 | |
249 /** | |
250 * The code below is an adaptation of a sample code for GDCM by | |
251 * Mathieu Malaterre (under a BSD license). | |
252 * http://gdcm.sourceforge.net/html/rle2img_8cxx-example.html | |
253 **/ | |
254 | |
255 // RLE pass | |
256 std::vector<uint8_t> temp; | |
257 temp.reserve(length); | |
258 for (size_t i = 0; i < length; i++) | |
259 { | |
260 if (inbuffer[i] == 0xa5) | |
261 { | |
262 temp.push_back(inbuffer[i+2]); | |
263 for (uint8_t repeat = inbuffer[i + 1]; repeat != 0; repeat--) | |
264 { | |
265 temp.push_back(inbuffer[i+2]); | |
266 } | |
267 i += 2; | |
268 } | |
269 else | |
270 { | |
271 temp.push_back(inbuffer[i]); | |
272 } | |
273 } | |
274 | |
275 // Delta encoding pass | |
276 uint16_t delta = 0; | |
277 output.clear(); | |
278 output.reserve(temp.size()); | |
279 for (size_t i = 0; i < temp.size(); i++) | |
280 { | |
281 uint16_t value; | |
282 | |
283 if (temp[i] == 0x5a) | |
284 { | |
285 uint16_t v1 = temp[i + 1]; | |
286 uint16_t v2 = temp[i + 2]; | |
287 value = (v2 << 8) + v1; | |
288 i += 2; | |
289 } | |
290 else | |
291 { | |
292 value = delta + (int8_t) temp[i]; | |
293 } | |
294 | |
295 output.push_back(value & 0xff); | |
296 output.push_back(value >> 8); | |
297 delta = value; | |
298 } | |
299 | |
300 if (output.size() % 2) | |
301 { | |
302 output.resize(output.size() - 1); | |
303 } | |
304 | |
305 return true; | |
306 } | |
307 | |
308 | |
845 | 309 void DicomImageDecoder::SetupImageBuffer(ImageBuffer& target, |
310 DcmDataset& dataset) | |
311 { | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
312 DicomMap m; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
313 FromDcmtkBridge::Convert(m, dataset); |
845 | 314 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
315 DicomImageInformation info(m); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
316 PixelFormat format; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
317 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
318 if (!info.ExtractPixelFormat(format)) |
845 | 319 { |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
320 LOG(WARNING) << "Unsupported DICOM image: " << info.GetBitsStored() |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
321 << "bpp, " << info.GetChannelCount() << " channels, " |
860 | 322 << (info.IsSigned() ? "signed" : "unsigned") |
323 << (info.IsPlanar() ? ", planar" : ", non-planar"); | |
845 | 324 throw OrthancException(ErrorCode_NotImplemented); |
325 } | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
326 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
327 target.SetHeight(info.GetHeight()); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
328 target.SetWidth(info.GetWidth()); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
329 target.SetFormat(format); |
845 | 330 } |
331 | |
332 | |
333 bool DicomImageDecoder::IsJpegLossless(const DcmDataset& dataset) | |
334 { | |
851 | 335 // http://support.dcmtk.org/docs/dcxfer_8h-source.html |
845 | 336 return (dataset.getOriginalXfer() == EXS_JPEGLSLossless || |
337 dataset.getOriginalXfer() == EXS_JPEGLSLossy); | |
338 } | |
339 | |
340 | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
341 bool DicomImageDecoder::IsUncompressedImage(const DcmDataset& dataset) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
342 { |
851 | 343 // 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
|
344 return (dataset.getOriginalXfer() == EXS_Unknown || |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
345 dataset.getOriginalXfer() == EXS_LittleEndianImplicit || |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
346 dataset.getOriginalXfer() == EXS_BigEndianImplicit || |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
347 dataset.getOriginalXfer() == EXS_LittleEndianExplicit || |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
348 dataset.getOriginalXfer() == EXS_BigEndianExplicit); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
349 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
350 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
351 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
352 template <typename PixelType> |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
353 static void CopyPixels(ImageAccessor& target, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
354 const DicomIntegerPixelAccessor& source) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
355 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
356 const PixelType minValue = std::numeric_limits<PixelType>::min(); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
357 const PixelType maxValue = std::numeric_limits<PixelType>::max(); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
358 |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
359 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
|
360 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
361 PixelType* pixel = reinterpret_cast<PixelType*>(target.GetRow(y)); |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
362 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
|
363 { |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
852
diff
changeset
|
364 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
|
365 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
366 int32_t v = source.GetValue(x, y, c); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
367 if (v < static_cast<int32_t>(minValue)) |
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 *pixel = minValue; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
370 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
371 else if (v > static_cast<int32_t>(maxValue)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
372 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
373 *pixel = maxValue; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
374 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
375 else |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
376 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
377 *pixel = static_cast<PixelType>(v); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
378 } |
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 } |
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 void DicomImageDecoder::DecodeUncompressedImage(ImageBuffer& target, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
386 DcmDataset& dataset, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
387 unsigned int frame) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
388 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
389 if (!IsUncompressedImage(dataset)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
390 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
391 throw OrthancException(ErrorCode_BadParameterType); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
392 } |
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 DecodeUncompressedImageInternal(target, dataset, frame); |
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 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
398 void DicomImageDecoder::DecodeUncompressedImageInternal(ImageBuffer& target, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
399 DcmDataset& dataset, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
400 unsigned int frame) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
401 { |
852 | 402 ImageSource source; |
403 source.Setup(dataset, frame); | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
404 |
858
ebc41566f742
removed unneeded check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
405 |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
406 /** |
858
ebc41566f742
removed unneeded check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
407 * Resize the target image. |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
408 **/ |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
409 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
410 SetupImageBuffer(target, dataset); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
411 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
412 if (source.GetWidth() != target.GetWidth() || |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
413 source.GetHeight() != target.GetHeight()) |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
414 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
415 throw OrthancException(ErrorCode_InternalError); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
416 } |
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 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
419 /** |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
420 * 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
|
421 * direct access to copy its values. |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
422 **/ |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
423 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
424 ImageAccessor targetAccessor(target.GetAccessor()); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
425 const DicomImageInformation& info = source.GetAccessor().GetInformation(); |
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 bool fastVersionSuccess = false; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
428 PixelFormat sourceFormat; |
863 | 429 if (!info.IsPlanar() && |
430 info.ExtractPixelFormat(sourceFormat)) | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
431 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
432 try |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
433 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
434 ImageAccessor sourceImage; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
435 sourceImage.AssignReadOnly(sourceFormat, |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
436 info.GetWidth(), |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
437 info.GetHeight(), |
860 | 438 info.GetWidth() * GetBytesPerPixel(sourceFormat), |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
439 source.GetAccessor().GetPixelData()); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
440 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
441 ImageProcessing::Convert(targetAccessor, sourceImage); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
442 ImageProcessing::ShiftRight(targetAccessor, info.GetShift()); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
443 fastVersionSuccess = true; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
444 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
445 catch (OrthancException&) |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
446 { |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
447 // Unsupported conversion, use the slow version |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
448 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
449 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
450 |
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 /** |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
453 * Slow version : loop over the DICOM buffer, storing its value |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
454 * into the target image. |
847
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 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
457 if (!fastVersionSuccess) |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
458 { |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
459 switch (target.GetFormat()) |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
460 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
461 case PixelFormat_RGB24: |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
462 case PixelFormat_RGBA32: |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
463 case PixelFormat_Grayscale8: |
863 | 464 CopyPixels<uint8_t>(targetAccessor, source.GetAccessor()); |
465 break; | |
860 | 466 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
467 case PixelFormat_Grayscale16: |
863 | 468 CopyPixels<uint16_t>(targetAccessor, source.GetAccessor()); |
469 break; | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
470 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
471 case PixelFormat_SignedGrayscale16: |
863 | 472 CopyPixels<int16_t>(targetAccessor, source.GetAccessor()); |
473 break; | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
474 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
475 default: |
863 | 476 throw OrthancException(ErrorCode_InternalError); |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
477 } |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
478 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
479 } |
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 |
845 | 482 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1 |
483 void DicomImageDecoder::DecodeJpegLossless(ImageBuffer& target, | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
484 DcmDataset& dataset, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
485 unsigned int frame) |
845 | 486 { |
487 if (!IsJpegLossless(dataset)) | |
488 { | |
489 throw OrthancException(ErrorCode_BadParameterType); | |
490 } | |
491 | |
492 DcmElement *element = NULL; | |
493 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_DATA), element).good()) | |
494 { | |
495 throw OrthancException(ErrorCode_BadFileFormat); | |
496 } | |
497 | |
498 DcmPixelData& pixelData = dynamic_cast<DcmPixelData&>(*element); | |
499 DcmPixelSequence* pixelSequence = NULL; | |
500 if (!pixelData.getEncapsulatedRepresentation | |
501 (dataset.getOriginalXfer(), NULL, pixelSequence).good()) | |
502 { | |
503 throw OrthancException(ErrorCode_BadFileFormat); | |
504 } | |
505 | |
506 SetupImageBuffer(target, dataset); | |
507 | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
508 ImageAccessor targetAccessor(target.GetAccessor()); |
845 | 509 |
510 /** | |
511 * The "DJLSLosslessDecoder" and "DJLSNearLosslessDecoder" in DCMTK | |
512 * are exactly the same, except for the "supportedTransferSyntax()" | |
513 * virtual function. | |
514 * http://support.dcmtk.org/docs/classDJLSDecoderBase.html | |
515 **/ | |
516 | |
517 DJLSLosslessDecoder decoder; DJLSCodecParameter parameters; | |
518 //DJLSNearLosslessDecoder decoder; DJLSCodecParameter parameters; | |
519 | |
520 Uint32 startFragment = 0; // Default | |
521 OFString decompressedColorModel; // Out | |
522 DJ_RPLossless representationParameter; | |
523 OFCondition c = decoder.decodeFrame(&representationParameter, pixelSequence, ¶meters, | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
524 &dataset, frame, startFragment, targetAccessor.GetBuffer(), |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
525 targetAccessor.GetSize(), decompressedColorModel); |
845 | 526 |
527 if (!c.good()) | |
528 { | |
529 throw OrthancException(ErrorCode_InternalError); | |
530 } | |
531 } | |
532 #endif | |
846 | 533 |
534 | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
535 |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
536 |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
537 bool DicomImageDecoder::Decode(ImageBuffer& target, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
538 DcmDataset& dataset, |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
539 unsigned int frame) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
540 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
541 if (IsUncompressedImage(dataset)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
542 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
543 DecodeUncompressedImage(target, dataset, frame); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
544 return true; |
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 |
852 | 547 |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
548 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
549 if (IsJpegLossless(dataset)) |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
550 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
551 LOG(INFO) << "Decoding a JPEG-LS image"; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
552 DecodeJpegLossless(target, dataset, frame); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
553 return true; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
554 } |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
555 #endif |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
556 |
852 | 557 |
558 #if ORTHANC_JPEG_ENABLED == 1 | |
559 // TODO Implement this part to speed up JPEG decompression | |
560 #endif | |
561 | |
562 | |
847
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
563 /** |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
564 * 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
|
565 * 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
|
566 * 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
|
567 * actually the second example of the following page: |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
568 * 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
|
569 **/ |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
570 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
571 { |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
572 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
|
573 |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
574 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
|
575 converted->chooseRepresentation(EXS_LittleEndianExplicit, NULL); |
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 if (converted->canWriteXfer(EXS_LittleEndianExplicit)) |
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 DecodeUncompressedImageInternal(target, *converted, frame); |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
580 return true; |
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 } |
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 return false; |
03ea55da7429
fully functional JPEG-LS conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
846
diff
changeset
|
585 } |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
586 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
587 |
863 | 588 bool DicomImageDecoder::DecodeAndTruncate(ImageBuffer& target, |
589 DcmDataset& dataset, | |
590 unsigned int frame, | |
591 PixelFormat format) | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
592 { |
863 | 593 // TODO Special case for uncompressed images |
594 | |
595 ImageBuffer source; | |
596 if (!Decode(source, dataset, frame)) | |
597 { | |
598 return false; | |
599 } | |
600 | |
601 if (source.GetFormat() == format) | |
602 { | |
603 // No conversion is required, return the temporary image | |
604 target.AcquireOwnership(source); | |
605 return true; | |
606 } | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
607 |
863 | 608 target.SetFormat(format); |
609 target.SetWidth(source.GetWidth()); | |
610 target.SetHeight(source.GetHeight()); | |
611 | |
612 ImageAccessor targetAccessor(target.GetAccessor()); | |
613 ImageAccessor sourceAccessor(source.GetAccessor()); | |
614 ImageProcessing::Convert(targetAccessor, sourceAccessor); | |
615 | |
616 return true; | |
617 } | |
618 | |
619 | |
620 bool DicomImageDecoder::DecodePreview(ImageBuffer& target, | |
621 DcmDataset& dataset, | |
622 unsigned int frame) | |
623 { | |
624 // TODO Special case for uncompressed images | |
625 | |
626 ImageBuffer source; | |
627 if (!Decode(source, dataset, frame)) | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
628 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
629 return false; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
630 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
631 |
863 | 632 switch (source.GetFormat()) |
861 | 633 { |
863 | 634 case PixelFormat_RGB24: |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
635 { |
863 | 636 // Directly return color images (RGB) |
637 target.AcquireOwnership(source); | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
638 return true; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
639 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
640 |
863 | 641 case PixelFormat_Grayscale8: |
642 case PixelFormat_Grayscale16: | |
643 case PixelFormat_SignedGrayscale16: | |
644 { | |
645 // Grayscale image: Stretch its dynamics to the [0,255] range | |
646 target.SetFormat(PixelFormat_Grayscale8); | |
647 target.SetWidth(source.GetWidth()); | |
648 target.SetHeight(source.GetHeight()); | |
649 | |
650 ImageAccessor targetAccessor(target.GetAccessor()); | |
651 ImageAccessor sourceAccessor(source.GetAccessor()); | |
652 | |
653 int64_t a, b; | |
654 ImageProcessing::GetMinMaxValue(a, b, sourceAccessor); | |
655 | |
656 if (a == b) | |
657 { | |
658 ImageProcessing::Set(targetAccessor, 0); | |
659 } | |
660 else | |
661 { | |
662 ImageProcessing::ShiftScale(sourceAccessor, -a, 255.0f / static_cast<float>(b - a)); | |
663 | |
664 if (source.GetFormat() == PixelFormat_Grayscale8) | |
665 { | |
666 target.AcquireOwnership(source); | |
667 } | |
668 else | |
669 { | |
670 ImageProcessing::Convert(targetAccessor, sourceAccessor); | |
671 } | |
672 } | |
673 | |
674 return true; | |
675 } | |
676 | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
677 default: |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
678 throw OrthancException(ErrorCode_NotImplemented); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
679 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
858
diff
changeset
|
680 } |
845 | 681 } |