Mercurial > hg > orthanc
comparison OrthancFramework/Sources/Images/ImageAccessor.cpp @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Core/Images/ImageAccessor.cpp@94f4a18a79cc |
children | bf7b9edf6b81 |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
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 "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 throw OrthancException(ErrorCode_ReadOnly, | |
111 "Trying to write to a read-only image"); | |
112 } | |
113 | |
114 return buffer_; | |
115 } | |
116 | |
117 | |
118 const void* ImageAccessor::GetConstRow(unsigned int y) const | |
119 { | |
120 if (buffer_ != NULL) | |
121 { | |
122 return buffer_ + y * pitch_; | |
123 } | |
124 else | |
125 { | |
126 return NULL; | |
127 } | |
128 } | |
129 | |
130 | |
131 void* ImageAccessor::GetRow(unsigned int y) const | |
132 { | |
133 if (readOnly_) | |
134 { | |
135 throw OrthancException(ErrorCode_ReadOnly, | |
136 "Trying to write to a read-only image"); | |
137 } | |
138 | |
139 if (buffer_ != NULL) | |
140 { | |
141 return buffer_ + y * pitch_; | |
142 } | |
143 else | |
144 { | |
145 return NULL; | |
146 } | |
147 } | |
148 | |
149 | |
150 void ImageAccessor::AssignEmpty(PixelFormat format) | |
151 { | |
152 readOnly_ = false; | |
153 format_ = format; | |
154 width_ = 0; | |
155 height_ = 0; | |
156 pitch_ = 0; | |
157 buffer_ = NULL; | |
158 } | |
159 | |
160 | |
161 void ImageAccessor::AssignReadOnly(PixelFormat format, | |
162 unsigned int width, | |
163 unsigned int height, | |
164 unsigned int pitch, | |
165 const void *buffer) | |
166 { | |
167 readOnly_ = true; | |
168 format_ = format; | |
169 width_ = width; | |
170 height_ = height; | |
171 pitch_ = pitch; | |
172 buffer_ = reinterpret_cast<uint8_t*>(const_cast<void*>(buffer)); | |
173 | |
174 if (GetBytesPerPixel() * width_ > pitch_) | |
175 { | |
176 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
177 } | |
178 } | |
179 | |
180 | |
181 void ImageAccessor::AssignWritable(PixelFormat format, | |
182 unsigned int width, | |
183 unsigned int height, | |
184 unsigned int pitch, | |
185 void *buffer) | |
186 { | |
187 readOnly_ = false; | |
188 format_ = format; | |
189 width_ = width; | |
190 height_ = height; | |
191 pitch_ = pitch; | |
192 buffer_ = reinterpret_cast<uint8_t*>(buffer); | |
193 | |
194 if (GetBytesPerPixel() * width_ > pitch_) | |
195 { | |
196 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
197 } | |
198 } | |
199 | |
200 | |
201 void ImageAccessor::GetWriteableAccessor(ImageAccessor& target) const | |
202 { | |
203 if (readOnly_) | |
204 { | |
205 throw OrthancException(ErrorCode_ReadOnly); | |
206 } | |
207 else | |
208 { | |
209 target.AssignWritable(format_, width_, height_, pitch_, buffer_); | |
210 } | |
211 } | |
212 | |
213 | |
214 void ImageAccessor::ToMatlabString(std::string& target) const | |
215 { | |
216 ChunkedBuffer buffer; | |
217 | |
218 switch (GetFormat()) | |
219 { | |
220 case PixelFormat_Grayscale8: | |
221 ToMatlabStringInternal<uint8_t>(buffer, *this); | |
222 break; | |
223 | |
224 case PixelFormat_Grayscale16: | |
225 ToMatlabStringInternal<uint16_t>(buffer, *this); | |
226 break; | |
227 | |
228 case PixelFormat_Grayscale32: | |
229 ToMatlabStringInternal<uint32_t>(buffer, *this); | |
230 break; | |
231 | |
232 case PixelFormat_Grayscale64: | |
233 ToMatlabStringInternal<uint64_t>(buffer, *this); | |
234 break; | |
235 | |
236 case PixelFormat_SignedGrayscale16: | |
237 ToMatlabStringInternal<int16_t>(buffer, *this); | |
238 break; | |
239 | |
240 case PixelFormat_Float32: | |
241 ToMatlabStringInternal<float>(buffer, *this); | |
242 break; | |
243 | |
244 case PixelFormat_RGB24: | |
245 RGB24ToMatlabString(buffer, *this); | |
246 break; | |
247 | |
248 default: | |
249 throw OrthancException(ErrorCode_NotImplemented); | |
250 } | |
251 | |
252 buffer.Flatten(target); | |
253 } | |
254 | |
255 | |
256 | |
257 void ImageAccessor::GetRegion(ImageAccessor& accessor, | |
258 unsigned int x, | |
259 unsigned int y, | |
260 unsigned int width, | |
261 unsigned int height) const | |
262 { | |
263 if (x + width > width_ || | |
264 y + height > height_) | |
265 { | |
266 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
267 } | |
268 | |
269 if (width == 0 || | |
270 height == 0) | |
271 { | |
272 accessor.AssignWritable(format_, 0, 0, 0, NULL); | |
273 } | |
274 else | |
275 { | |
276 uint8_t* p = (buffer_ + | |
277 y * pitch_ + | |
278 x * GetBytesPerPixel()); | |
279 | |
280 if (readOnly_) | |
281 { | |
282 accessor.AssignReadOnly(format_, width, height, pitch_, p); | |
283 } | |
284 else | |
285 { | |
286 accessor.AssignWritable(format_, width, height, pitch_, p); | |
287 } | |
288 } | |
289 } | |
290 | |
291 | |
292 void ImageAccessor::SetFormat(PixelFormat format) | |
293 { | |
294 if (readOnly_) | |
295 { | |
296 throw OrthancException(ErrorCode_ReadOnly, | |
297 "Trying to modify the format of a read-only image"); | |
298 } | |
299 | |
300 if (::Orthanc::GetBytesPerPixel(format) != ::Orthanc::GetBytesPerPixel(format_)) | |
301 { | |
302 throw OrthancException(ErrorCode_IncompatibleImageFormat); | |
303 } | |
304 | |
305 format_ = format; | |
306 } | |
307 } |