comparison OrthancServer/Internals/DicomImageDecoder.cpp @ 845:48016722c770 jpeg

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 05 Jun 2014 15:58:07 +0200
parents
children 715ab7674993
comparison
equal deleted inserted replaced
844:502c49adb5ad 845:48016722c770
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
36 #include "../../Core/OrthancException.h"
37 #include "../ToDcmtkBridge.h"
38
39 #include <dcmtk/dcmjpls/djcodecd.h>
40 #include <dcmtk/dcmjpls/djcparam.h>
41 #include <dcmtk/dcmjpeg/djrplol.h>
42 #include <boost/lexical_cast.hpp>
43
44 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1
45 #endif
46
47
48 namespace Orthanc
49 {
50 void DicomImageDecoder::SetupImageBuffer(ImageBuffer& target,
51 DcmDataset& dataset)
52 {
53 OFString value;
54
55 if (!dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_COLUMNS), value).good())
56 {
57 throw OrthancException(ErrorCode_BadFileFormat);
58 }
59
60 unsigned int width = boost::lexical_cast<unsigned int>(value.c_str());
61
62 if (!dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_ROWS), value).good())
63 {
64 throw OrthancException(ErrorCode_BadFileFormat);
65 }
66
67 unsigned int height = boost::lexical_cast<unsigned int>(value.c_str());
68
69 if (!dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_BITS_STORED), value).good())
70 {
71 throw OrthancException(ErrorCode_BadFileFormat);
72 }
73
74 unsigned int bitsStored = boost::lexical_cast<unsigned int>(value.c_str());
75
76 if (!dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_REPRESENTATION), value).good())
77 {
78 throw OrthancException(ErrorCode_BadFileFormat);
79 }
80
81 bool isSigned = (boost::lexical_cast<unsigned int>(value.c_str()) != 0);
82
83 unsigned int samplesPerPixel = 1; // By default
84 if (dataset.findAndGetOFString(ToDcmtkBridge::Convert(DICOM_TAG_SAMPLES_PER_PIXEL), value).good())
85 {
86 samplesPerPixel = boost::lexical_cast<unsigned int>(value.c_str());
87 }
88
89 target.SetHeight(height);
90 target.SetWidth(width);
91
92 if (bitsStored == 8 && samplesPerPixel == 1 && !isSigned)
93 {
94 target.SetFormat(PixelFormat_Grayscale8);
95 }
96 else if (bitsStored == 8 && samplesPerPixel == 3 && !isSigned)
97 {
98 target.SetFormat(PixelFormat_RGB24);
99 }
100 else if (bitsStored == 16 && samplesPerPixel == 1 && !isSigned)
101 {
102 target.SetFormat(PixelFormat_Grayscale16);
103 }
104 else if (bitsStored == 16 && samplesPerPixel == 1 && isSigned)
105 {
106 target.SetFormat(PixelFormat_SignedGrayscale16);
107 }
108 else
109 {
110 throw OrthancException(ErrorCode_NotImplemented);
111 }
112 }
113
114
115 bool DicomImageDecoder::IsJpegLossless(const DcmDataset& dataset)
116 {
117 return (dataset.getOriginalXfer() == EXS_JPEGLSLossless ||
118 dataset.getOriginalXfer() == EXS_JPEGLSLossy);
119 }
120
121
122 #if ORTHANC_JPEG_LOSSLESS_ENABLED == 1
123 void DicomImageDecoder::DecodeJpegLossless(ImageBuffer& target,
124 DcmDataset& dataset)
125 {
126 if (!IsJpegLossless(dataset))
127 {
128 throw OrthancException(ErrorCode_BadParameterType);
129 }
130
131 DcmElement *element = NULL;
132 if (!dataset.findAndGetElement(ToDcmtkBridge::Convert(DICOM_TAG_PIXEL_DATA), element).good())
133 {
134 throw OrthancException(ErrorCode_BadFileFormat);
135 }
136
137 DcmPixelData& pixelData = dynamic_cast<DcmPixelData&>(*element);
138 DcmPixelSequence* pixelSequence = NULL;
139 if (!pixelData.getEncapsulatedRepresentation
140 (dataset.getOriginalXfer(), NULL, pixelSequence).good())
141 {
142 throw OrthancException(ErrorCode_BadFileFormat);
143 }
144
145 SetupImageBuffer(target, dataset);
146
147 ImageAccessor accessor(target.GetAccessor());
148
149 /**
150 * The "DJLSLosslessDecoder" and "DJLSNearLosslessDecoder" in DCMTK
151 * are exactly the same, except for the "supportedTransferSyntax()"
152 * virtual function.
153 * http://support.dcmtk.org/docs/classDJLSDecoderBase.html
154 **/
155
156 DJLSLosslessDecoder decoder; DJLSCodecParameter parameters;
157 //DJLSNearLosslessDecoder decoder; DJLSCodecParameter parameters;
158
159 Uint32 startFragment = 0; // Default
160 OFString decompressedColorModel; // Out
161 DJ_RPLossless representationParameter;
162 OFCondition c = decoder.decodeFrame(&representationParameter, pixelSequence, &parameters,
163 &dataset, 0, startFragment, accessor.GetBuffer(),
164 accessor.GetSize(), decompressedColorModel);
165
166 if (!c.good())
167 {
168 throw OrthancException(ErrorCode_InternalError);
169 }
170 }
171 #endif
172 }