Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Volumes/ImageBuffer3D.cpp @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | Framework/Volumes/ImageBuffer3D.cpp@30deba7bc8e2 |
children | e731e62692a9 |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
1 /** | |
2 * Stone of Orthanc | |
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 Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "ImageBuffer3D.h" | |
23 | |
24 #include <Images/ImageProcessing.h> | |
25 #include <Logging.h> | |
26 #include <OrthancException.h> | |
27 | |
28 #include <string.h> | |
29 | |
30 namespace OrthancStone | |
31 { | |
32 void ImageBuffer3D::GetAxialSliceAccessor(Orthanc::ImageAccessor& target, | |
33 unsigned int slice, | |
34 bool readOnly) const | |
35 { | |
36 if (slice >= depth_) | |
37 { | |
38 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
39 } | |
40 | |
41 if (readOnly) | |
42 { | |
43 target.AssignReadOnly(format_, width_, height_, image_.GetPitch(), | |
44 image_.GetConstRow(height_ * (depth_ - 1 - slice))); | |
45 } | |
46 else | |
47 { | |
48 target.AssignWritable(format_, width_, height_, image_.GetPitch(), | |
49 image_.GetRow(height_ * (depth_ - 1 - slice))); | |
50 } | |
51 } | |
52 | |
53 | |
54 void ImageBuffer3D::GetCoronalSliceAccessor(Orthanc::ImageAccessor& target, | |
55 unsigned int slice, | |
56 bool readOnly) const | |
57 { | |
58 if (slice >= height_) | |
59 { | |
60 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
61 } | |
62 | |
63 if (readOnly) | |
64 { | |
65 target.AssignReadOnly(format_, width_, depth_, image_.GetPitch() * height_, | |
66 image_.GetConstRow(slice)); | |
67 } | |
68 else | |
69 { | |
70 target.AssignWritable(format_, width_, depth_, image_.GetPitch() * height_, | |
71 image_.GetRow(slice)); | |
72 } | |
73 } | |
74 | |
75 | |
76 Orthanc::Image* ImageBuffer3D::ExtractSagittalSlice(unsigned int slice) const | |
77 { | |
78 //LOG(TRACE) << "ImageBuffer3D::ExtractSagittalSlice this= " << std::hex << this << std::dec << " width_ = " << width_ << " height_ = " << height_ << " depth_ = " << depth_ << " slice = " << slice; | |
79 if (slice >= width_) | |
80 { | |
81 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
82 } | |
83 | |
84 std::unique_ptr<Orthanc::Image> result(new Orthanc::Image(format_, height_, depth_, false)); | |
85 //LOG(TRACE) << "ImageBuffer3D::ExtractSagittalSlice result will be an image of WIDTH = " << height_ << " and HEIGHT = " << depth_; | |
86 | |
87 unsigned int bytesPerPixel = Orthanc::GetBytesPerPixel(format_); | |
88 | |
89 for (unsigned int z = 0; z < depth_; z++) | |
90 { | |
91 //uint8_t* target = reinterpret_cast<uint8_t*>(result->GetRow(depth_ - 1 - z)); | |
92 uint8_t* target = reinterpret_cast<uint8_t*>(result->GetRow(z)); | |
93 | |
94 for (unsigned int y = 0; y < height_; y++) | |
95 { | |
96 const void* source = (reinterpret_cast<const uint8_t*>(image_.GetConstRow(y + z * height_)) + bytesPerPixel * slice); | |
97 const uint8_t* byteSrc = reinterpret_cast<const uint8_t*>(source); | |
98 for (size_t byte = 0; byte < bytesPerPixel; ++byte) | |
99 target[byte] = byteSrc[byte]; | |
100 target += bytesPerPixel; | |
101 } | |
102 } | |
103 | |
104 return result.release(); | |
105 } | |
106 | |
107 | |
108 ImageBuffer3D::ImageBuffer3D(Orthanc::PixelFormat format, | |
109 unsigned int width, | |
110 unsigned int height, | |
111 unsigned int depth, | |
112 bool computeRange) : | |
113 image_(format, width, height * depth, false), | |
114 format_(format), | |
115 width_(width), | |
116 height_(height), | |
117 depth_(depth), | |
118 computeRange_(computeRange), | |
119 hasRange_(false) | |
120 { | |
121 LOG(TRACE) << "Created a 3D image of size " << width << "x" << height | |
122 << "x" << depth << " in " << Orthanc::EnumerationToString(format) | |
123 << " (" << (GetEstimatedMemorySize() / (1024ll * 1024ll)) << "MB)"; | |
124 } | |
125 | |
126 | |
127 void ImageBuffer3D::Clear() | |
128 { | |
129 memset(image_.GetBuffer(), 0, image_.GetHeight() * image_.GetPitch()); | |
130 } | |
131 | |
132 | |
133 uint64_t ImageBuffer3D::GetEstimatedMemorySize() const | |
134 { | |
135 return image_.GetPitch() * image_.GetHeight() * Orthanc::GetBytesPerPixel(format_); | |
136 } | |
137 | |
138 | |
139 void ImageBuffer3D::ExtendImageRange(const Orthanc::ImageAccessor& slice) | |
140 { | |
141 if (!computeRange_ || | |
142 slice.GetWidth() == 0 || | |
143 slice.GetHeight() == 0) | |
144 { | |
145 return; | |
146 } | |
147 | |
148 float sliceMin, sliceMax; | |
149 | |
150 switch (slice.GetFormat()) | |
151 { | |
152 case Orthanc::PixelFormat_Grayscale8: | |
153 case Orthanc::PixelFormat_Grayscale16: | |
154 case Orthanc::PixelFormat_Grayscale32: | |
155 case Orthanc::PixelFormat_SignedGrayscale16: | |
156 { | |
157 int64_t a, b; | |
158 Orthanc::ImageProcessing::GetMinMaxIntegerValue(a, b, slice); | |
159 sliceMin = static_cast<float>(a); | |
160 sliceMax = static_cast<float>(b); | |
161 break; | |
162 } | |
163 | |
164 case Orthanc::PixelFormat_Float32: | |
165 Orthanc::ImageProcessing::GetMinMaxFloatValue(sliceMin, sliceMax, slice); | |
166 break; | |
167 | |
168 default: | |
169 return; | |
170 } | |
171 | |
172 if (hasRange_) | |
173 { | |
174 minValue_ = std::min(minValue_, sliceMin); | |
175 maxValue_ = std::max(maxValue_, sliceMax); | |
176 } | |
177 else | |
178 { | |
179 hasRange_ = true; | |
180 minValue_ = sliceMin; | |
181 maxValue_ = sliceMax; | |
182 } | |
183 } | |
184 | |
185 | |
186 bool ImageBuffer3D::GetRange(float& minValue, | |
187 float& maxValue) const | |
188 { | |
189 if (hasRange_) | |
190 { | |
191 minValue = minValue_; | |
192 maxValue = maxValue_; | |
193 return true; | |
194 } | |
195 else | |
196 { | |
197 return false; | |
198 } | |
199 } | |
200 | |
201 | |
202 ImageBuffer3D::SliceReader::SliceReader(const ImageBuffer3D& that, | |
203 VolumeProjection projection, | |
204 unsigned int slice) | |
205 { | |
206 switch (projection) | |
207 { | |
208 case VolumeProjection_Axial: | |
209 that.GetAxialSliceAccessor(accessor_, slice, true); | |
210 break; | |
211 | |
212 case VolumeProjection_Coronal: | |
213 that.GetCoronalSliceAccessor(accessor_, slice, true); | |
214 break; | |
215 | |
216 case VolumeProjection_Sagittal: | |
217 sagittal_.reset(that.ExtractSagittalSlice(slice)); | |
218 sagittal_->GetReadOnlyAccessor(accessor_); | |
219 break; | |
220 | |
221 default: | |
222 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
223 } | |
224 } | |
225 | |
226 | |
227 void ImageBuffer3D::SliceWriter::Flush() | |
228 { | |
229 if (modified_) | |
230 { | |
231 if (sagittal_.get() != NULL) | |
232 { | |
233 // TODO | |
234 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
235 } | |
236 | |
237 // Update the dynamic range of the underlying image, if | |
238 // "computeRange_" is set to true | |
239 that_.ExtendImageRange(accessor_); | |
240 } | |
241 } | |
242 | |
243 | |
244 ImageBuffer3D::SliceWriter::SliceWriter(ImageBuffer3D& that, | |
245 VolumeProjection projection, | |
246 unsigned int slice) : | |
247 that_(that), | |
248 modified_(false) | |
249 { | |
250 switch (projection) | |
251 { | |
252 case VolumeProjection_Axial: | |
253 that.GetAxialSliceAccessor(accessor_, slice, false); | |
254 break; | |
255 | |
256 case VolumeProjection_Coronal: | |
257 that.GetCoronalSliceAccessor(accessor_, slice, false); | |
258 break; | |
259 | |
260 case VolumeProjection_Sagittal: | |
261 sagittal_.reset(that.ExtractSagittalSlice(slice)); | |
262 sagittal_->GetWriteableAccessor(accessor_); | |
263 break; | |
264 | |
265 default: | |
266 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
267 } | |
268 } | |
269 | |
270 | |
271 uint8_t ImageBuffer3D::GetVoxelGrayscale8(unsigned int x, | |
272 unsigned int y, | |
273 unsigned int z) const | |
274 { | |
275 if (format_ != Orthanc::PixelFormat_Grayscale8) | |
276 { | |
277 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
278 } | |
279 | |
280 if (x >= width_ || | |
281 y >= height_ || | |
282 z >= depth_) | |
283 { | |
284 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
285 } | |
286 | |
287 const void* p = image_.GetConstRow(y + height_ * (depth_ - 1 - z)); | |
288 return reinterpret_cast<const uint8_t*>(p) [x]; | |
289 } | |
290 | |
291 | |
292 uint16_t ImageBuffer3D::GetVoxelGrayscale16(unsigned int x, | |
293 unsigned int y, | |
294 unsigned int z) const | |
295 { | |
296 if (format_ != Orthanc::PixelFormat_Grayscale16) | |
297 { | |
298 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
299 } | |
300 | |
301 if (x >= width_ || | |
302 y >= height_ || | |
303 z >= depth_) | |
304 { | |
305 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
306 } | |
307 | |
308 const void* p = image_.GetConstRow(y + height_ * (depth_ - 1 - z)); | |
309 return reinterpret_cast<const uint16_t*>(p) [x]; | |
310 } | |
311 } |