Mercurial > hg > orthanc-stone
annotate Resources/Orthanc/Core/Images/ImageAccessor.cpp @ 54:01aa453d4d5b wasm
IWidget::HasRenderMouseOver
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 27 Apr 2017 17:49:29 +0200 |
parents | 7207a407bcd8 |
children |
rev | line source |
---|---|
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 | |
40
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
1 | 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_SignedGrayscale16: | |
222 ToMatlabStringInternal<int16_t>(buffer, *this); | |
223 break; | |
224 | |
225 case PixelFormat_Float32: | |
226 ToMatlabStringInternal<float>(buffer, *this); | |
227 break; | |
228 | |
229 case PixelFormat_RGB24: | |
230 RGB24ToMatlabString(buffer, *this); | |
231 break; | |
232 | |
233 default: | |
234 throw OrthancException(ErrorCode_NotImplemented); | |
235 } | |
236 | |
237 buffer.Flatten(target); | |
238 } | |
239 | |
240 | |
241 | |
242 ImageAccessor ImageAccessor::GetRegion(unsigned int x, | |
243 unsigned int y, | |
244 unsigned int width, | |
245 unsigned int height) const | |
246 { | |
247 if (x + width > width_ || | |
248 y + height > height_) | |
249 { | |
250 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
251 } | |
252 | |
253 ImageAccessor result; | |
254 | |
255 if (width == 0 || | |
256 height == 0) | |
257 { | |
258 result.AssignWritable(format_, 0, 0, 0, NULL); | |
259 } | |
260 else | |
261 { | |
262 uint8_t* p = (buffer_ + | |
263 y * pitch_ + | |
264 x * GetBytesPerPixel()); | |
265 | |
266 if (readOnly_) | |
267 { | |
268 result.AssignReadOnly(format_, width, height, pitch_, p); | |
269 } | |
270 else | |
271 { | |
272 result.AssignWritable(format_, width, height, pitch_, p); | |
273 } | |
274 } | |
275 | |
276 return result; | |
277 } | |
278 | |
279 | |
280 void ImageAccessor::SetFormat(PixelFormat format) | |
281 { | |
282 if (readOnly_) | |
283 { | |
284 #if ORTHANC_ENABLE_LOGGING == 1 | |
285 LOG(ERROR) << "Trying to modify the format of a read-only image"; | |
286 #endif | |
287 throw OrthancException(ErrorCode_ReadOnly); | |
288 } | |
289 | |
290 if (::Orthanc::GetBytesPerPixel(format) != ::Orthanc::GetBytesPerPixel(format_)) | |
291 { | |
292 throw OrthancException(ErrorCode_IncompatibleImageFormat); | |
293 } | |
294 | |
295 format_ = format; | |
296 } | |
297 } |