comparison Core/DicomParsing/DcmtkTranscoder.cpp @ 3893:7a5fa8f307e9 transcoding

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 06 May 2020 12:48:28 +0200
parents
children 8f7ad4989fec
comparison
equal deleted inserted replaced
3891:5571082a9df6 3893:7a5fa8f307e9
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeaders.h"
35 #include "DcmtkTranscoder.h"
36
37
38 #if !defined(ORTHANC_ENABLE_DCMTK_JPEG)
39 # error Macro ORTHANC_ENABLE_DCMTK_JPEG must be defined
40 #endif
41
42 #if !defined(ORTHANC_ENABLE_DCMTK_JPEG_LOSSLESS)
43 # error Macro ORTHANC_ENABLE_DCMTK_JPEG_LOSSLESS must be defined
44 #endif
45
46
47 #include "FromDcmtkBridge.h"
48 #include "../OrthancException.h"
49
50 #include <dcmtk/dcmdata/dcdeftag.h>
51 #include <dcmtk/dcmjpeg/djrploss.h> // for DJ_RPLossy
52 #include <dcmtk/dcmjpeg/djrplol.h> // for DJ_RPLossless
53 #include <dcmtk/dcmjpls/djrparam.h> // for DJLSRepresentationParameter
54
55
56 namespace Orthanc
57 {
58 static uint16_t GetBitsStored(DcmDataset& dataset)
59 {
60 uint16_t bitsStored;
61 if (dataset.findAndGetUint16(DCM_BitsStored, bitsStored).good())
62 {
63 return bitsStored;
64 }
65 else
66 {
67 throw OrthancException(ErrorCode_BadFileFormat,
68 "Missing \"Bits Stored\" tag in DICOM instance");
69 }
70 }
71
72
73 static std::string GetSopInstanceUid(DcmDataset& dataset)
74 {
75 const char* v = NULL;
76
77 if (dataset.findAndGetString(DCM_SOPInstanceUID, v).good() &&
78 v != NULL)
79 {
80 return std::string(v);
81 }
82 else
83 {
84 throw OrthancException(ErrorCode_BadFileFormat, "File without SOP instance UID");
85 }
86 }
87
88
89 static void CheckSopInstanceUid(DcmFileFormat& dicom,
90 const std::string& sopInstanceUid,
91 bool mustEqual)
92 {
93 if (dicom.getDataset() == NULL)
94 {
95 throw OrthancException(ErrorCode_InternalError);
96 }
97
98 bool ok;
99
100 if (mustEqual)
101 {
102 ok = (GetSopInstanceUid(*dicom.getDataset()) == sopInstanceUid);
103 }
104 else
105 {
106 ok = (GetSopInstanceUid(*dicom.getDataset()) != sopInstanceUid);
107 }
108
109 if (!ok)
110 {
111 throw OrthancException(ErrorCode_InternalError,
112 mustEqual ? "The SOP instance UID has changed unexpectedly during transcoding" :
113 "The SOP instance UID has not changed as expected during transcoding");
114 }
115 }
116
117
118 void DcmtkTranscoder::SetLossyQuality(unsigned int quality)
119 {
120 if (quality <= 0 ||
121 quality > 100)
122 {
123 throw OrthancException(ErrorCode_ParameterOutOfRange);
124 }
125 else
126 {
127 lossyQuality_ = quality;
128 }
129 }
130
131
132 DcmFileFormat* DcmtkTranscoder::TranscodeToParsed(const void* buffer,
133 size_t size,
134 const std::set<DicomTransferSyntax>& allowedSyntaxes,
135 bool allowNewSopInstanceUid)
136 {
137 std::unique_ptr<DcmFileFormat> dicom(FromDcmtkBridge::LoadFromMemoryBuffer(buffer, size));
138
139 if (dicom.get() == NULL)
140 {
141 throw OrthancException(ErrorCode_InternalError);
142 }
143
144 if (InplaceTranscode(*dicom, allowedSyntaxes, allowNewSopInstanceUid))
145 {
146 return dicom.release();
147 }
148 else
149 {
150 return NULL;
151 }
152 }
153
154
155 bool DcmtkTranscoder::InplaceTranscode(DcmFileFormat& dicom,
156 const std::set<DicomTransferSyntax>& allowedSyntaxes,
157 bool allowNewSopInstanceUid)
158 {
159 if (dicom.getDataset() == NULL)
160 {
161 throw OrthancException(ErrorCode_InternalError);
162 }
163
164 DicomTransferSyntax syntax;
165 if (!FromDcmtkBridge::LookupOrthancTransferSyntax(syntax, dicom))
166 {
167 throw OrthancException(ErrorCode_BadFileFormat,
168 "Cannot determine the transfer syntax");
169 }
170
171 const uint16_t bitsStored = GetBitsStored(*dicom.getDataset());
172 std::string sourceSopInstanceUid = GetSopInstanceUid(*dicom.getDataset());
173
174 if (allowedSyntaxes.find(syntax) != allowedSyntaxes.end())
175 {
176 // No transcoding is needed
177 return true;
178 }
179
180 if (allowedSyntaxes.find(DicomTransferSyntax_LittleEndianImplicit) != allowedSyntaxes.end() &&
181 FromDcmtkBridge::Transcode(dicom, DicomTransferSyntax_LittleEndianImplicit, NULL))
182 {
183 CheckSopInstanceUid(dicom, sourceSopInstanceUid, true);
184 return true;
185 }
186
187 if (allowedSyntaxes.find(DicomTransferSyntax_LittleEndianExplicit) != allowedSyntaxes.end() &&
188 FromDcmtkBridge::Transcode(dicom, DicomTransferSyntax_LittleEndianExplicit, NULL))
189 {
190 CheckSopInstanceUid(dicom, sourceSopInstanceUid, true);
191 return true;
192 }
193
194 if (allowedSyntaxes.find(DicomTransferSyntax_BigEndianExplicit) != allowedSyntaxes.end() &&
195 FromDcmtkBridge::Transcode(dicom, DicomTransferSyntax_BigEndianExplicit, NULL))
196 {
197 CheckSopInstanceUid(dicom, sourceSopInstanceUid, true);
198 return true;
199 }
200
201 if (allowedSyntaxes.find(DicomTransferSyntax_DeflatedLittleEndianExplicit) != allowedSyntaxes.end() &&
202 FromDcmtkBridge::Transcode(dicom, DicomTransferSyntax_DeflatedLittleEndianExplicit, NULL))
203 {
204 CheckSopInstanceUid(dicom, sourceSopInstanceUid, true);
205 return true;
206 }
207
208 #if ORTHANC_ENABLE_DCMTK_JPEG == 1
209 if (allowedSyntaxes.find(DicomTransferSyntax_JPEGProcess1) != allowedSyntaxes.end() &&
210 allowNewSopInstanceUid &&
211 bitsStored == 8)
212 {
213 // Check out "dcmjpeg/apps/dcmcjpeg.cc"
214 DJ_RPLossy parameters(lossyQuality_);
215
216 if (FromDcmtkBridge::Transcode(dicom, DicomTransferSyntax_JPEGProcess1, &parameters))
217 {
218 CheckSopInstanceUid(dicom, sourceSopInstanceUid, false);
219 return true;
220 }
221 }
222 #endif
223
224 #if ORTHANC_ENABLE_DCMTK_JPEG == 1
225 if (allowedSyntaxes.find(DicomTransferSyntax_JPEGProcess2_4) != allowedSyntaxes.end() &&
226 allowNewSopInstanceUid &&
227 bitsStored <= 12)
228 {
229 // Check out "dcmjpeg/apps/dcmcjpeg.cc"
230 DJ_RPLossy parameters(lossyQuality_);
231 if (FromDcmtkBridge::Transcode(dicom, DicomTransferSyntax_JPEGProcess2_4, &parameters))
232 {
233 CheckSopInstanceUid(dicom, sourceSopInstanceUid, false);
234 return true;
235 }
236 }
237 #endif
238
239 #if ORTHANC_ENABLE_DCMTK_JPEG == 1
240 if (allowedSyntaxes.find(DicomTransferSyntax_JPEGProcess14SV1) != allowedSyntaxes.end())
241 {
242 // Check out "dcmjpeg/apps/dcmcjpeg.cc"
243 DJ_RPLossless parameters(6 /* opt_selection_value */,
244 0 /* opt_point_transform */);
245 if (FromDcmtkBridge::Transcode(dicom, DicomTransferSyntax_JPEGProcess14SV1, &parameters))
246 {
247 CheckSopInstanceUid(dicom, sourceSopInstanceUid, true);
248 return true;
249 }
250 }
251 #endif
252
253 #if ORTHANC_ENABLE_DCMTK_JPEG_LOSSLESS == 1
254 if (allowedSyntaxes.find(DicomTransferSyntax_JPEGLSLossless) != allowedSyntaxes.end())
255 {
256 // Check out "dcmjpls/apps/dcmcjpls.cc"
257 DJLSRepresentationParameter parameters(2 /* opt_nearlossless_deviation */,
258 OFTrue /* opt_useLosslessProcess */);
259
260 /**
261 * WARNING: This call results in a segmentation fault if using
262 * the DCMTK package 3.6.2 from Ubuntu 18.04.
263 **/
264 if (FromDcmtkBridge::Transcode(dicom, DicomTransferSyntax_JPEGLSLossless, &parameters))
265 {
266 CheckSopInstanceUid(dicom, sourceSopInstanceUid, true);
267 return true;
268 }
269 }
270 #endif
271
272 #if ORTHANC_ENABLE_DCMTK_JPEG_LOSSLESS == 1
273 if (allowNewSopInstanceUid &&
274 allowedSyntaxes.find(DicomTransferSyntax_JPEGLSLossy) != allowedSyntaxes.end())
275 {
276 // Check out "dcmjpls/apps/dcmcjpls.cc"
277 DJLSRepresentationParameter parameters(2 /* opt_nearlossless_deviation */,
278 OFFalse /* opt_useLosslessProcess */);
279
280 /**
281 * WARNING: This call results in a segmentation fault if using
282 * the DCMTK package 3.6.2 from Ubuntu 18.04.
283 **/
284 if (FromDcmtkBridge::Transcode(dicom, DicomTransferSyntax_JPEGLSLossy, &parameters))
285 {
286 CheckSopInstanceUid(dicom, sourceSopInstanceUid, false);
287 return true;
288 }
289 }
290 #endif
291
292 return false;
293 }
294
295
296 bool DcmtkTranscoder::TranscodeToBuffer(std::string& target,
297 const void* buffer,
298 size_t size,
299 const std::set<DicomTransferSyntax>& allowedSyntaxes,
300 bool allowNewSopInstanceUid)
301 {
302 std::unique_ptr<DcmFileFormat> transcoded(
303 TranscodeToParsed(buffer, size, allowedSyntaxes, allowNewSopInstanceUid));
304
305 if (transcoded.get() == NULL)
306 {
307 return false;
308 }
309 else
310 {
311 if (transcoded->getDataset() == NULL)
312 {
313 throw OrthancException(ErrorCode_InternalError);
314 }
315
316 FromDcmtkBridge::SaveToMemoryBuffer(target, *transcoded->getDataset());
317 return true;
318 }
319 }
320 }