comparison Core/ImageFormats/ImageProcessing.cpp @ 993:501880d76474 plugins

improvements to GDCM plugin
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 02 Jul 2014 14:41:57 +0200
parents e21d1a5f5934
children 6e7e5ed91c2d
comparison
equal deleted inserted replaced
992:af014624dac1 993:501880d76474
73 } 73 }
74 } 74 }
75 } 75 }
76 76
77 77
78 template <typename TargetType>
79 static void ConvertColorToGrayscale(ImageAccessor& target,
80 const ImageAccessor& source)
81 {
82 assert(source.GetFormat() == PixelFormat_RGB24);
83
84 const TargetType minValue = std::numeric_limits<TargetType>::min();
85 const TargetType maxValue = std::numeric_limits<TargetType>::max();
86
87 for (unsigned int y = 0; y < source.GetHeight(); y++)
88 {
89 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y));
90 const uint8_t* s = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
91
92 for (unsigned int x = 0; x < source.GetWidth(); x++, t++, s += 3)
93 {
94 // Y = 0.2126 R + 0.7152 G + 0.0722 B
95 int32_t v = (2126 * static_cast<int32_t>(s[0]) +
96 7152 * static_cast<int32_t>(s[1]) +
97 0722 * static_cast<int32_t>(s[2])) / 1000;
98
99 if (static_cast<int32_t>(v) < static_cast<int32_t>(minValue))
100 {
101 *t = minValue;
102 }
103 else if (static_cast<int32_t>(v) > static_cast<int32_t>(maxValue))
104 {
105 *t = maxValue;
106 }
107 else
108 {
109 *t = static_cast<TargetType>(v);
110 }
111 }
112 }
113 }
114
115
78 template <typename PixelType> 116 template <typename PixelType>
79 static void SetInternal(ImageAccessor& image, 117 static void SetInternal(ImageAccessor& image,
80 int64_t constant) 118 int64_t constant)
81 { 119 {
82 for (unsigned int y = 0; y < image.GetHeight(); y++) 120 for (unsigned int y = 0; y < image.GetHeight(); y++)
314 352
315 if (target.GetFormat() == PixelFormat_Grayscale16 && 353 if (target.GetFormat() == PixelFormat_Grayscale16 &&
316 source.GetFormat() == PixelFormat_SignedGrayscale16) 354 source.GetFormat() == PixelFormat_SignedGrayscale16)
317 { 355 {
318 ConvertInternal<uint16_t, int16_t>(target, source); 356 ConvertInternal<uint16_t, int16_t>(target, source);
357 return;
358 }
359
360 if (target.GetFormat() == PixelFormat_Grayscale8 &&
361 source.GetFormat() == PixelFormat_RGB24)
362 {
363 ConvertColorToGrayscale<uint8_t>(target, source);
364 return;
365 }
366
367 if (target.GetFormat() == PixelFormat_Grayscale16 &&
368 source.GetFormat() == PixelFormat_RGB24)
369 {
370 ConvertColorToGrayscale<uint16_t>(target, source);
371 return;
372 }
373
374 if (target.GetFormat() == PixelFormat_SignedGrayscale16 &&
375 source.GetFormat() == PixelFormat_RGB24)
376 {
377 ConvertColorToGrayscale<int16_t>(target, source);
319 return; 378 return;
320 } 379 }
321 380
322 throw OrthancException(ErrorCode_NotImplemented); 381 throw OrthancException(ErrorCode_NotImplemented);
323 } 382 }