1
|
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 *
|
|
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 "../PrecompiledHeaders.h"
|
|
34 #include "ImageAccessor.h"
|
|
35
|
|
36 #include "../Logging.h"
|
|
37 #include "../OrthancException.h"
|
|
38 #include "../ChunkedBuffer.h"
|
|
39
|
|
40 #include <stdint.h>
|
|
41 #include <cassert>
|
|
42 #include <boost/lexical_cast.hpp>
|
|
43
|
|
44
|
|
45
|
|
46 namespace Orthanc
|
|
47 {
|
|
48 template <typename PixelType>
|
|
49 static void ToMatlabStringInternal(ChunkedBuffer& target,
|
|
50 const ImageAccessor& source)
|
|
51 {
|
|
52 target.AddChunk("double([ ");
|
|
53
|
|
54 for (unsigned int y = 0; y < source.GetHeight(); y++)
|
|
55 {
|
|
56 const PixelType* p = reinterpret_cast<const PixelType*>(source.GetConstRow(y));
|
|
57
|
|
58 std::string s;
|
|
59 if (y > 0)
|
|
60 {
|
|
61 s = "; ";
|
|
62 }
|
|
63
|
|
64 s.reserve(source.GetWidth() * 8);
|
|
65
|
|
66 for (unsigned int x = 0; x < source.GetWidth(); x++, p++)
|
|
67 {
|
|
68 s += boost::lexical_cast<std::string>(static_cast<double>(*p)) + " ";
|
|
69 }
|
|
70
|
|
71 target.AddChunk(s);
|
|
72 }
|
|
73
|
|
74 target.AddChunk("])");
|
|
75 }
|
|
76
|
|
77
|
|
78 static void RGB24ToMatlabString(ChunkedBuffer& target,
|
|
79 const ImageAccessor& source)
|
|
80 {
|
|
81 assert(source.GetFormat() == PixelFormat_RGB24);
|
|
82
|
|
83 target.AddChunk("double(permute(reshape([ ");
|
|
84
|
|
85 for (unsigned int y = 0; y < source.GetHeight(); y++)
|
|
86 {
|
|
87 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
|
|
88
|
|
89 std::string s;
|
|
90 s.reserve(source.GetWidth() * 3 * 8);
|
|
91
|
|
92 for (unsigned int x = 0; x < 3 * source.GetWidth(); x++, p++)
|
|
93 {
|
|
94 s += boost::lexical_cast<std::string>(static_cast<int>(*p)) + " ";
|
|
95 }
|
|
96
|
|
97 target.AddChunk(s);
|
|
98 }
|
|
99
|
|
100 target.AddChunk("], [ 3 " + boost::lexical_cast<std::string>(source.GetHeight()) +
|
|
101 " " + boost::lexical_cast<std::string>(source.GetWidth()) + " ]), [ 3 2 1 ]))");
|
|
102 }
|
|
103
|
|
104
|
|
105 void* ImageAccessor::GetBuffer() const
|
|
106 {
|
|
107 if (readOnly_)
|
|
108 {
|
|
109 #if ORTHANC_ENABLE_LOGGING == 1
|
|
110 LOG(ERROR) << "Trying to write on a read-only image";
|
|
111 #endif
|
|
112
|
|
113 throw OrthancException(ErrorCode_ReadOnly);
|
|
114 }
|
|
115
|
|
116 return buffer_;
|
|
117 }
|
|
118
|
|
119
|
|
120 const void* ImageAccessor::GetConstRow(unsigned int y) const
|
|
121 {
|
|
122 if (buffer_ != NULL)
|
|
123 {
|
|
124 return buffer_ + y * pitch_;
|
|
125 }
|
|
126 else
|
|
127 {
|
|
128 return NULL;
|
|
129 }
|
|
130 }
|
|
131
|
|
132
|
|
133 void* ImageAccessor::GetRow(unsigned int y) const
|
|
134 {
|
|
135 if (readOnly_)
|
|
136 {
|
|
137 #if ORTHANC_ENABLE_LOGGING == 1
|
|
138 LOG(ERROR) << "Trying to write on a read-only image";
|
|
139 #endif
|
|
140
|
|
141 throw OrthancException(ErrorCode_ReadOnly);
|
|
142 }
|
|
143
|
|
144 if (buffer_ != NULL)
|
|
145 {
|
|
146 return buffer_ + y * pitch_;
|
|
147 }
|
|
148 else
|
|
149 {
|
|
150 return NULL;
|
|
151 }
|
|
152 }
|
|
153
|
|
154
|
|
155 void ImageAccessor::AssignEmpty(PixelFormat format)
|
|
156 {
|
|
157 readOnly_ = false;
|
|
158 format_ = format;
|
|
159 width_ = 0;
|
|
160 height_ = 0;
|
|
161 pitch_ = 0;
|
|
162 buffer_ = NULL;
|
|
163 }
|
|
164
|
|
165
|
|
166 void ImageAccessor::AssignReadOnly(PixelFormat format,
|
|
167 unsigned int width,
|
|
168 unsigned int height,
|
|
169 unsigned int pitch,
|
|
170 const void *buffer)
|
|
171 {
|
|
172 readOnly_ = true;
|
|
173 format_ = format;
|
|
174 width_ = width;
|
|
175 height_ = height;
|
|
176 pitch_ = pitch;
|
|
177 buffer_ = reinterpret_cast<uint8_t*>(const_cast<void*>(buffer));
|
|
178
|
|
179 if (GetBytesPerPixel() * width_ > pitch_)
|
|
180 {
|
|
181 throw OrthancException(ErrorCode_ParameterOutOfRange);
|
|
182 }
|
|
183 }
|
|
184
|
|
185
|
|
186 void ImageAccessor::AssignWritable(PixelFormat format,
|
|
187 unsigned int width,
|
|
188 unsigned int height,
|
|
189 unsigned int pitch,
|
|
190 void *buffer)
|
|
191 {
|
|
192 readOnly_ = false;
|
|
193 format_ = format;
|
|
194 width_ = width;
|
|
195 height_ = height;
|
|
196 pitch_ = pitch;
|
|
197 buffer_ = reinterpret_cast<uint8_t*>(buffer);
|
|
198
|
|
199 if (GetBytesPerPixel() * width_ > pitch_)
|
|
200 {
|
|
201 throw OrthancException(ErrorCode_ParameterOutOfRange);
|
|
202 }
|
|
203 }
|
|
204
|
|
205
|
|
206 void ImageAccessor::ToMatlabString(std::string& target) const
|
|
207 {
|
|
208 ChunkedBuffer buffer;
|
|
209
|
|
210 switch (GetFormat())
|
|
211 {
|
|
212 case PixelFormat_Grayscale8:
|
|
213 ToMatlabStringInternal<uint8_t>(buffer, *this);
|
|
214 break;
|
|
215
|
|
216 case PixelFormat_Grayscale16:
|
|
217 ToMatlabStringInternal<uint16_t>(buffer, *this);
|
|
218 break;
|
|
219
|
|
220 case PixelFormat_SignedGrayscale16:
|
|
221 ToMatlabStringInternal<int16_t>(buffer, *this);
|
|
222 break;
|
|
223
|
|
224 case PixelFormat_Float32:
|
|
225 ToMatlabStringInternal<float>(buffer, *this);
|
|
226 break;
|
|
227
|
|
228 case PixelFormat_RGB24:
|
|
229 RGB24ToMatlabString(buffer, *this);
|
|
230 break;
|
|
231
|
|
232 default:
|
|
233 throw OrthancException(ErrorCode_NotImplemented);
|
|
234 }
|
|
235
|
|
236 buffer.Flatten(target);
|
|
237 }
|
|
238
|
|
239
|
|
240
|
|
241 ImageAccessor ImageAccessor::GetRegion(unsigned int x,
|
|
242 unsigned int y,
|
|
243 unsigned int width,
|
|
244 unsigned int height) const
|
|
245 {
|
|
246 if (x + width > width_ ||
|
|
247 y + height > height_)
|
|
248 {
|
|
249 throw OrthancException(ErrorCode_ParameterOutOfRange);
|
|
250 }
|
|
251
|
|
252 ImageAccessor result;
|
|
253
|
|
254 if (width == 0 ||
|
|
255 height == 0)
|
|
256 {
|
|
257 result.AssignWritable(format_, 0, 0, 0, NULL);
|
|
258 }
|
|
259 else
|
|
260 {
|
|
261 uint8_t* p = (buffer_ +
|
|
262 y * pitch_ +
|
|
263 x * GetBytesPerPixel());
|
|
264
|
|
265 if (readOnly_)
|
|
266 {
|
|
267 result.AssignReadOnly(format_, width, height, pitch_, p);
|
|
268 }
|
|
269 else
|
|
270 {
|
|
271 result.AssignWritable(format_, width, height, pitch_, p);
|
|
272 }
|
|
273 }
|
|
274
|
|
275 return result;
|
|
276 }
|
|
277
|
|
278
|
|
279 void ImageAccessor::SetFormat(PixelFormat format)
|
|
280 {
|
|
281 if (readOnly_)
|
|
282 {
|
|
283 #if ORTHANC_ENABLE_LOGGING == 1
|
|
284 LOG(ERROR) << "Trying to modify the format of a read-only image";
|
|
285 #endif
|
|
286 throw OrthancException(ErrorCode_ReadOnly);
|
|
287 }
|
|
288
|
|
289 if (::Orthanc::GetBytesPerPixel(format) != ::Orthanc::GetBytesPerPixel(format_))
|
|
290 {
|
|
291 throw OrthancException(ErrorCode_IncompatibleImageFormat);
|
|
292 }
|
|
293
|
|
294 format_ = format;
|
|
295 }
|
|
296 }
|