comparison Resources/Orthanc/Core/Images/ImageAccessor.cpp @ 200:03afbee0cc7b

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