# HG changeset patch # User Sebastien Jodogne # Date 1566574303 -7200 # Node ID d8f7c3970e2519d9dc620d4d15e55b0320bb064e # Parent 957e06cbe76a423c58eb7e5cec4ed1e43211e558 more tolerance in ImageProcessing::Set() diff -r 957e06cbe76a -r d8f7c3970e25 Core/Images/ImageProcessing.cpp --- a/Core/Images/ImageProcessing.cpp Thu Aug 22 15:02:19 2019 +0200 +++ b/Core/Images/ImageProcessing.cpp Fri Aug 23 17:31:43 2019 +0200 @@ -155,8 +155,8 @@ { // Y = 0.2126 R + 0.7152 G + 0.0722 B int32_t v = (2126 * static_cast(s[0]) + - 7152 * static_cast(s[1]) + - 0722 * static_cast(s[2])) / 10000; + 7152 * static_cast(s[1]) + + 0722 * static_cast(s[2])) / 10000; if (static_cast(v) < static_cast(minValue)) { @@ -543,8 +543,8 @@ for (unsigned int x = 0; x < width; x++, q++) { *q = static_cast((2126 * static_cast(p[0]) + - 7152 * static_cast(p[1]) + - 0722 * static_cast(p[2])) / 10000); + 7152 * static_cast(p[1]) + + 0722 * static_cast(p[2])) / 10000); p += 4; } } @@ -562,8 +562,8 @@ for (unsigned int x = 0; x < width; x++, q++) { *q = static_cast((2126 * static_cast(p[2]) + - 7152 * static_cast(p[1]) + - 0722 * static_cast(p[0])) / 10000); + 7152 * static_cast(p[1]) + + 0722 * static_cast(p[0])) / 10000); p += 4; } } @@ -796,33 +796,42 @@ { switch (image.GetFormat()) { - case PixelFormat_Grayscale8: - SetInternal(image, value); - return; + case PixelFormat_Grayscale8: + SetInternal(image, value); + return; + + case PixelFormat_Grayscale16: + SetInternal(image, value); + return; - case PixelFormat_Grayscale16: - SetInternal(image, value); - return; + case PixelFormat_Grayscale32: + SetInternal(image, value); + return; - case PixelFormat_Grayscale32: - SetInternal(image, value); - return; + case PixelFormat_Grayscale64: + SetInternal(image, value); + return; - case PixelFormat_Grayscale64: - SetInternal(image, value); - return; + case PixelFormat_SignedGrayscale16: + SetInternal(image, value); + return; - case PixelFormat_SignedGrayscale16: - SetInternal(image, value); - return; + case PixelFormat_Float32: + assert(sizeof(float) == 4); + SetInternal(image, value); + return; - case PixelFormat_Float32: - assert(sizeof(float) == 4); - SetInternal(image, value); - return; + case PixelFormat_RGBA32: + case PixelFormat_BGRA32: + case PixelFormat_RGB24: + { + uint8_t v = static_cast(value); + Set(image, v, v, v, v); // Use the color version + return; + } - default: - throw OrthancException(ErrorCode_NotImplemented); + default: + throw OrthancException(ErrorCode_NotImplemented); } } @@ -838,31 +847,31 @@ switch (image.GetFormat()) { - case PixelFormat_RGBA32: - p[0] = red; - p[1] = green; - p[2] = blue; - p[3] = alpha; - size = 4; - break; + case PixelFormat_RGBA32: + p[0] = red; + p[1] = green; + p[2] = blue; + p[3] = alpha; + size = 4; + break; - case PixelFormat_BGRA32: - p[0] = blue; - p[1] = green; - p[2] = red; - p[3] = alpha; - size = 4; - break; + case PixelFormat_BGRA32: + p[0] = blue; + p[1] = green; + p[2] = red; + p[3] = alpha; + size = 4; + break; - case PixelFormat_RGB24: - p[0] = red; - p[1] = green; - p[2] = blue; - size = 3; - break; + case PixelFormat_RGB24: + p[0] = red; + p[1] = green; + p[2] = blue; + size = 3; + break; - default: - throw OrthancException(ErrorCode_NotImplemented); + default: + throw OrthancException(ErrorCode_NotImplemented); } const unsigned int width = image.GetWidth(); @@ -906,44 +915,44 @@ { switch (image.GetFormat()) { - case PixelFormat_Grayscale8: - { - uint8_t a, b; - GetMinMaxValueInternal(a, b, image); - minValue = a; - maxValue = b; - break; - } + case PixelFormat_Grayscale8: + { + uint8_t a, b; + GetMinMaxValueInternal(a, b, image); + minValue = a; + maxValue = b; + break; + } - case PixelFormat_Grayscale16: - { - uint16_t a, b; - GetMinMaxValueInternal(a, b, image); - minValue = a; - maxValue = b; - break; - } + case PixelFormat_Grayscale16: + { + uint16_t a, b; + GetMinMaxValueInternal(a, b, image); + minValue = a; + maxValue = b; + break; + } - case PixelFormat_Grayscale32: - { - uint32_t a, b; - GetMinMaxValueInternal(a, b, image); - minValue = a; - maxValue = b; - break; - } + case PixelFormat_Grayscale32: + { + uint32_t a, b; + GetMinMaxValueInternal(a, b, image); + minValue = a; + maxValue = b; + break; + } - case PixelFormat_SignedGrayscale16: - { - int16_t a, b; - GetMinMaxValueInternal(a, b, image); - minValue = a; - maxValue = b; - break; - } + case PixelFormat_SignedGrayscale16: + { + int16_t a, b; + GetMinMaxValueInternal(a, b, image); + minValue = a; + maxValue = b; + break; + } - default: - throw OrthancException(ErrorCode_NotImplemented); + default: + throw OrthancException(ErrorCode_NotImplemented); } } @@ -954,18 +963,18 @@ { switch (image.GetFormat()) { - case PixelFormat_Float32: - { - assert(sizeof(float) == 4); - float a, b; - GetMinMaxValueInternal(a, b, image); - minValue = a; - maxValue = b; - break; - } + case PixelFormat_Float32: + { + assert(sizeof(float) == 4); + float a, b; + GetMinMaxValueInternal(a, b, image); + minValue = a; + maxValue = b; + break; + } - default: - throw OrthancException(ErrorCode_NotImplemented); + default: + throw OrthancException(ErrorCode_NotImplemented); } } @@ -976,20 +985,20 @@ { switch (image.GetFormat()) { - case PixelFormat_Grayscale8: - AddConstantInternal(image, value); - return; + case PixelFormat_Grayscale8: + AddConstantInternal(image, value); + return; - case PixelFormat_Grayscale16: - AddConstantInternal(image, value); - return; + case PixelFormat_Grayscale16: + AddConstantInternal(image, value); + return; - case PixelFormat_SignedGrayscale16: - AddConstantInternal(image, value); - return; + case PixelFormat_SignedGrayscale16: + AddConstantInternal(image, value); + return; - default: - throw OrthancException(ErrorCode_NotImplemented); + default: + throw OrthancException(ErrorCode_NotImplemented); } } @@ -1000,41 +1009,41 @@ { switch (image.GetFormat()) { - case PixelFormat_Grayscale8: - if (useRound) - { - MultiplyConstantInternal(image, factor); - } - else - { - MultiplyConstantInternal(image, factor); - } - return; + case PixelFormat_Grayscale8: + if (useRound) + { + MultiplyConstantInternal(image, factor); + } + else + { + MultiplyConstantInternal(image, factor); + } + return; - case PixelFormat_Grayscale16: - if (useRound) - { - MultiplyConstantInternal(image, factor); - } - else - { - MultiplyConstantInternal(image, factor); - } - return; + case PixelFormat_Grayscale16: + if (useRound) + { + MultiplyConstantInternal(image, factor); + } + else + { + MultiplyConstantInternal(image, factor); + } + return; - case PixelFormat_SignedGrayscale16: - if (useRound) - { - MultiplyConstantInternal(image, factor); - } - else - { - MultiplyConstantInternal(image, factor); - } - return; + case PixelFormat_SignedGrayscale16: + if (useRound) + { + MultiplyConstantInternal(image, factor); + } + else + { + MultiplyConstantInternal(image, factor); + } + return; - default: - throw OrthancException(ErrorCode_NotImplemented); + default: + throw OrthancException(ErrorCode_NotImplemented); } } @@ -1046,41 +1055,41 @@ { switch (image.GetFormat()) { - case PixelFormat_Grayscale8: - if (useRound) - { - ShiftScaleInternal(image, offset, scaling); - } - else - { - ShiftScaleInternal(image, offset, scaling); - } - return; + case PixelFormat_Grayscale8: + if (useRound) + { + ShiftScaleInternal(image, offset, scaling); + } + else + { + ShiftScaleInternal(image, offset, scaling); + } + return; - case PixelFormat_Grayscale16: - if (useRound) - { - ShiftScaleInternal(image, offset, scaling); - } - else - { - ShiftScaleInternal(image, offset, scaling); - } - return; + case PixelFormat_Grayscale16: + if (useRound) + { + ShiftScaleInternal(image, offset, scaling); + } + else + { + ShiftScaleInternal(image, offset, scaling); + } + return; - case PixelFormat_SignedGrayscale16: - if (useRound) - { - ShiftScaleInternal(image, offset, scaling); - } - else - { - ShiftScaleInternal(image, offset, scaling); - } - return; + case PixelFormat_SignedGrayscale16: + if (useRound) + { + ShiftScaleInternal(image, offset, scaling); + } + else + { + ShiftScaleInternal(image, offset, scaling); + } + return; - default: - throw OrthancException(ErrorCode_NotImplemented); + default: + throw OrthancException(ErrorCode_NotImplemented); } } @@ -1092,41 +1101,41 @@ switch (image.GetFormat()) { - case PixelFormat_Grayscale16: - { - uint16_t maxValueUint16 = (uint16_t)(std::min(maxValue, static_cast(std::numeric_limits::max()))); - - for (unsigned int y = 0; y < height; y++) + case PixelFormat_Grayscale16: { - uint16_t* p = reinterpret_cast(image.GetRow(y)); + uint16_t maxValueUint16 = (uint16_t)(std::min(maxValue, static_cast(std::numeric_limits::max()))); + + for (unsigned int y = 0; y < height; y++) + { + uint16_t* p = reinterpret_cast(image.GetRow(y)); + + for (unsigned int x = 0; x < width; x++, p++) + { + *p = maxValueUint16 - (*p); + } + } - for (unsigned int x = 0; x < width; x++, p++) + return; + } + case PixelFormat_Grayscale8: + { + uint8_t maxValueUint8 = (uint8_t)(std::min(maxValue, static_cast(std::numeric_limits::max()))); + + for (unsigned int y = 0; y < height; y++) { - *p = maxValueUint16 - (*p); + uint8_t* p = reinterpret_cast(image.GetRow(y)); + + for (unsigned int x = 0; x < width; x++, p++) + { + *p = maxValueUint8 - (*p); + } } + + return; } - return; - } - case PixelFormat_Grayscale8: - { - uint8_t maxValueUint8 = (uint8_t)(std::min(maxValue, static_cast(std::numeric_limits::max()))); - - for (unsigned int y = 0; y < height; y++) - { - uint8_t* p = reinterpret_cast(image.GetRow(y)); - - for (unsigned int x = 0; x < width; x++, p++) - { - *p = maxValueUint8 - (*p); - } - } - - return; - } - - default: - throw OrthancException(ErrorCode_NotImplemented); + default: + throw OrthancException(ErrorCode_NotImplemented); } } @@ -1135,10 +1144,10 @@ { switch (image.GetFormat()) { - case PixelFormat_Grayscale8: - return Invert(image, 255); - default: - throw OrthancException(ErrorCode_NotImplemented); // you should use the Invert(image, maxValue) overload + case PixelFormat_Grayscale8: + return Invert(image, 255); + default: + throw OrthancException(ErrorCode_NotImplemented); // you should use the Invert(image, maxValue) overload } } @@ -1291,29 +1300,29 @@ { switch (image.GetFormat()) { - case Orthanc::PixelFormat_Grayscale8: - { - BresenhamPixelWriter writer(image, value); - writer.DrawSegment(x0, y0, x1, y1); - break; - } + case Orthanc::PixelFormat_Grayscale8: + { + BresenhamPixelWriter writer(image, value); + writer.DrawSegment(x0, y0, x1, y1); + break; + } - case Orthanc::PixelFormat_Grayscale16: - { - BresenhamPixelWriter writer(image, value); - writer.DrawSegment(x0, y0, x1, y1); - break; - } + case Orthanc::PixelFormat_Grayscale16: + { + BresenhamPixelWriter writer(image, value); + writer.DrawSegment(x0, y0, x1, y1); + break; + } - case Orthanc::PixelFormat_SignedGrayscale16: - { - BresenhamPixelWriter writer(image, value); - writer.DrawSegment(x0, y0, x1, y1); - break; - } + case Orthanc::PixelFormat_SignedGrayscale16: + { + BresenhamPixelWriter writer(image, value); + writer.DrawSegment(x0, y0, x1, y1); + break; + } - default: - throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); } } @@ -1330,46 +1339,46 @@ { switch (image.GetFormat()) { - case Orthanc::PixelFormat_BGRA32: - { - PixelTraits::PixelType pixel; - pixel.red_ = red; - pixel.green_ = green; - pixel.blue_ = blue; - pixel.alpha_ = alpha; + case Orthanc::PixelFormat_BGRA32: + { + PixelTraits::PixelType pixel; + pixel.red_ = red; + pixel.green_ = green; + pixel.blue_ = blue; + pixel.alpha_ = alpha; - BresenhamPixelWriter writer(image, pixel); - writer.DrawSegment(x0, y0, x1, y1); - break; - } + BresenhamPixelWriter writer(image, pixel); + writer.DrawSegment(x0, y0, x1, y1); + break; + } - case Orthanc::PixelFormat_RGBA32: - { - PixelTraits::PixelType pixel; - pixel.red_ = red; - pixel.green_ = green; - pixel.blue_ = blue; - pixel.alpha_ = alpha; + case Orthanc::PixelFormat_RGBA32: + { + PixelTraits::PixelType pixel; + pixel.red_ = red; + pixel.green_ = green; + pixel.blue_ = blue; + pixel.alpha_ = alpha; - BresenhamPixelWriter writer(image, pixel); - writer.DrawSegment(x0, y0, x1, y1); - break; - } + BresenhamPixelWriter writer(image, pixel); + writer.DrawSegment(x0, y0, x1, y1); + break; + } - case Orthanc::PixelFormat_RGB24: - { - PixelTraits::PixelType pixel; - pixel.red_ = red; - pixel.green_ = green; - pixel.blue_ = blue; + case Orthanc::PixelFormat_RGB24: + { + PixelTraits::PixelType pixel; + pixel.red_ = red; + pixel.green_ = green; + pixel.blue_ = blue; - BresenhamPixelWriter writer(image, pixel); - writer.DrawSegment(x0, y0, x1, y1); - break; - } + BresenhamPixelWriter writer(image, pixel); + writer.DrawSegment(x0, y0, x1, y1); + break; + } - default: - throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); } } @@ -1392,8 +1401,8 @@ template void FillPolygon_(ImageAccessor& image, - const std::vector& points, - int64_t value_) + const std::vector& points, + int64_t value_) { typedef typename PixelTraits::PixelType TargetType; @@ -1524,24 +1533,23 @@ { switch (image.GetFormat()) { - case Orthanc::PixelFormat_Grayscale8: - { - FillPolygon_(image, points, value); - break; - } - case Orthanc::PixelFormat_Grayscale16: - { - FillPolygon_(image, points, value); - break; - } - case Orthanc::PixelFormat_SignedGrayscale16: - { - FillPolygon_(image, points, value); - break; - } - default: - throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); + case Orthanc::PixelFormat_Grayscale8: + { + FillPolygon_(image, points, value); + break; + } + case Orthanc::PixelFormat_Grayscale16: + { + FillPolygon_(image, points, value); + break; + } + case Orthanc::PixelFormat_SignedGrayscale16: + { + FillPolygon_(image, points, value); + break; + } + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); } } - }