comparison Framework/ImageToolbox.cpp @ 87:3d83d34cd4db

sync, optimizations
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 16 Dec 2016 16:44:14 +0100
parents 7a3853d51c45
children ff0ef01c332c
comparison
equal deleted inserted replaced
86:319b8c45c231 87:3d83d34cd4db
219 static uint8_t GetPixelValue(const Orthanc::ImageAccessor& source, 219 static uint8_t GetPixelValue(const Orthanc::ImageAccessor& source,
220 unsigned int x, 220 unsigned int x,
221 unsigned int y, 221 unsigned int y,
222 unsigned int channel, 222 unsigned int channel,
223 int offsetX, 223 int offsetX,
224 int offsetY) 224 int offsetY,
225 { 225 unsigned int bytesPerPixel)
226 assert(channel < source.GetBytesPerPixel()); 226 {
227 assert(bytesPerPixel == source.GetBytesPerPixel());
228 assert(channel < bytesPerPixel);
227 assert(source.GetFormat() == Orthanc::PixelFormat_Grayscale8 || 229 assert(source.GetFormat() == Orthanc::PixelFormat_Grayscale8 ||
228 source.GetFormat() == Orthanc::PixelFormat_RGB24 || 230 source.GetFormat() == Orthanc::PixelFormat_RGB24 ||
229 source.GetFormat() == Orthanc::PixelFormat_RGBA32); // 16bpp is unsupported 231 source.GetFormat() == Orthanc::PixelFormat_RGBA32); // 16bpp is unsupported
230 232
231 if (static_cast<int>(x) + offsetX < 0) 233 if (static_cast<int>(x) + offsetX < 0)
253 y = source.GetHeight() - 1; 255 y = source.GetHeight() - 1;
254 } 256 }
255 } 257 }
256 258
257 return *(reinterpret_cast<const uint8_t*>(source.GetConstBuffer()) + 259 return *(reinterpret_cast<const uint8_t*>(source.GetConstBuffer()) +
258 y * source.GetPitch() + x * source.GetBytesPerPixel() + channel); 260 y * source.GetPitch() + x * bytesPerPixel + channel);
259 } 261 }
260 262
261 263
262 static uint8_t SmoothPixelValue(const Orthanc::ImageAccessor& source, 264 static uint8_t SmoothPixelValue(const Orthanc::ImageAccessor& source,
263 unsigned int x, 265 unsigned int x,
264 unsigned int y, 266 unsigned int y,
265 unsigned int channel) 267 unsigned int channel,
268 unsigned int bytesPerPixel)
266 { 269 {
267 static const uint32_t kernel[5] = { 1, 4, 6, 4, 1 }; 270 static const uint32_t kernel[5] = { 1, 4, 6, 4, 1 };
268 static const uint32_t normalization = 2 * (1 + 4 + 6 + 4 + 1); 271 static const uint32_t normalization = 2 * (1 + 4 + 6 + 4 + 1);
269 272
270 uint32_t accumulator = 0; 273 uint32_t accumulator = 0;
271 274
272 // Horizontal smoothing 275 // Horizontal smoothing
273 for (int offset = -2; offset <= 2; offset++) 276 for (int offset = -2; offset <= 2; offset++)
274 { 277 {
275 accumulator += kernel[offset + 2] * GetPixelValue(source, x, y, channel, offset, 0); 278 accumulator += kernel[offset + 2] * GetPixelValue(source, x, y, channel, offset, 0, bytesPerPixel);
276 } 279 }
277 280
278 // Vertical smoothing 281 // Vertical smoothing
279 for (int offset = -2; offset <= 2; offset++) 282 for (int offset = -2; offset <= 2; offset++)
280 { 283 {
281 accumulator += kernel[offset + 2] * GetPixelValue(source, x, y, channel, 0, offset); 284 accumulator += kernel[offset + 2] * GetPixelValue(source, x, y, channel, 0, offset, bytesPerPixel);
282 } 285 }
283 286
284 return static_cast<uint8_t>(accumulator / normalization); 287 return static_cast<uint8_t>(accumulator / normalization);
285 } 288 }
286 289
304 unsigned int channelsCount = source.GetBytesPerPixel(); // OK tx (*) 307 unsigned int channelsCount = source.GetBytesPerPixel(); // OK tx (*)
305 308
306 std::auto_ptr<Orthanc::ImageAccessor> result(Allocate(source.GetFormat(), 309 std::auto_ptr<Orthanc::ImageAccessor> result(Allocate(source.GetFormat(),
307 source.GetWidth() / 2, 310 source.GetWidth() / 2,
308 source.GetHeight() / 2)); 311 source.GetHeight() / 2));
312
313 unsigned int bytesPerPixel = source.GetBytesPerPixel();
309 314
310 for (unsigned int y = 0; y < source.GetHeight() / 2; y++) 315 for (unsigned int y = 0; y < source.GetHeight() / 2; y++)
311 { 316 {
312 uint8_t* q = reinterpret_cast<uint8_t*>(result->GetRow(y)); 317 uint8_t* q = reinterpret_cast<uint8_t*>(result->GetRow(y));
313 318
315 { 320 {
316 for (unsigned int c = 0; c < channelsCount; c++) 321 for (unsigned int c = 0; c < channelsCount; c++)
317 { 322 {
318 if (smooth) 323 if (smooth)
319 { 324 {
320 q[c] = SmoothPixelValue(source, 2 * x, 2 * y, c); 325 q[c] = SmoothPixelValue(source, 2 * x, 2 * y, c, bytesPerPixel);
321 } 326 }
322 else 327 else
323 { 328 {
324 q[c] = GetPixelValue(source, 2 * x, 2 * y, c, 0, 0); 329 q[c] = GetPixelValue(source, 2 * x, 2 * y, c, 0, 0, bytesPerPixel);
325 } 330 }
326 } 331 }
327 } 332 }
328 } 333 }
329 334