# HG changeset patch # User am@osimis.io # Date 1550157772 -3600 # Node ID 53bb1f4b3844870f9ba99f5e416e34496bb89889 # Parent 45e1631febbba56f02e90a5244e63f2ea0cbd5ca added Invert for Grayscale16 diff -r 45e1631febbb -r 53bb1f4b3844 Core/Images/ImageProcessing.cpp --- a/Core/Images/ImageProcessing.cpp Thu Feb 14 15:25:41 2019 +0100 +++ b/Core/Images/ImageProcessing.cpp Thu Feb 14 16:22:52 2019 +0100 @@ -20,7 +20,7 @@ * you do not wish to do so, delete this exception statement from your * version. If you delete this exception statement from all source files * in the program, then also delete it here. - * + * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU @@ -147,8 +147,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)) { @@ -168,7 +168,7 @@ static void MemsetZeroInternal(ImageAccessor& image) - { + { const unsigned int height = image.GetHeight(); const size_t lineSize = image.GetBytesPerPixel() * image.GetWidth(); const size_t pitch = image.GetPitch(); @@ -535,8 +535,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; } } @@ -554,8 +554,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; } } @@ -788,33 +788,33 @@ { 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; - default: - throw OrthancException(ErrorCode_NotImplemented); + default: + throw OrthancException(ErrorCode_NotImplemented); } } @@ -830,32 +830,32 @@ 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(); const unsigned int height = image.GetHeight(); @@ -898,44 +898,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); } } @@ -946,18 +946,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); } } @@ -968,20 +968,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); } } @@ -992,41 +992,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); } } @@ -1038,45 +1038,75 @@ { 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); } } + void ImageProcessing::Invert(ImageAccessor& image, int64_t maxValue) + { + const unsigned int width = image.GetWidth(); + const unsigned int height = image.GetHeight(); + + switch (image.GetFormat()) + { + case PixelFormat_Grayscale16: + { + uint16_t maxValueUint16 = (uint16_t)maxValue; + + 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); + } + } + + return; + } + + default: + throw OrthancException(ErrorCode_NotImplemented); + } + + } + void ImageProcessing::Invert(ImageAccessor& image) { const unsigned int width = image.GetWidth(); @@ -1084,24 +1114,24 @@ switch (image.GetFormat()) { - case PixelFormat_Grayscale8: + case PixelFormat_Grayscale8: + { + for (unsigned int y = 0; y < height; y++) { - for (unsigned int y = 0; y < height; y++) - { - uint8_t* p = reinterpret_cast(image.GetRow(y)); + uint8_t* p = reinterpret_cast(image.GetRow(y)); - for (unsigned int x = 0; x < width; x++, p++) - { - *p = 255 - (*p); - } + for (unsigned int x = 0; x < width; x++, p++) + { + *p = 255 - (*p); } - - return; } - default: - throw OrthancException(ErrorCode_NotImplemented); - } + return; + } + + default: + throw OrthancException(ErrorCode_NotImplemented); + } } @@ -1113,7 +1143,7 @@ { private: typedef typename PixelTraits::PixelType PixelType; - + Orthanc::ImageAccessor& image_; PixelType value_; @@ -1144,7 +1174,7 @@ y = y + yi; d = d - 2 * dx; } - + d = d + 2*dy; } } @@ -1157,13 +1187,13 @@ int dx = x1 - x0; int dy = y1 - y0; int xi = 1; - + if (dx < 0) { xi = -1; dx = -dx; } - + int d = 2 * dx - dy; int x = x0; @@ -1176,7 +1206,7 @@ x = x + xi; d = d - 2 * dy; } - + d = d + 2 * dx; } } @@ -1216,7 +1246,7 @@ { // This is an implementation of Bresenham's line algorithm // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#All_cases - + if (abs(y1 - y0) < abs(x1 - x0)) { if (x0 > x1) @@ -1252,30 +1282,30 @@ int64_t value) { 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; - } - - default: - throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); + case Orthanc::PixelFormat_SignedGrayscale16: + { + BresenhamPixelWriter writer(image, value); + writer.DrawSegment(x0, y0, x1, y1); + break; + } + + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); } } @@ -1292,46 +1322,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; - } - - default: - throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); + BresenhamPixelWriter writer(image, pixel); + writer.DrawSegment(x0, y0, x1, y1); + break; + } + + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); } } } diff -r 45e1631febbb -r 53bb1f4b3844 Core/Images/ImageProcessing.h --- a/Core/Images/ImageProcessing.h Thu Feb 14 15:25:41 2019 +0100 +++ b/Core/Images/ImageProcessing.h Thu Feb 14 16:22:52 2019 +0100 @@ -83,6 +83,8 @@ void Invert(ImageAccessor& image); + void Invert(ImageAccessor& image, int64_t maxValue); + void DrawLineSegment(ImageAccessor& image, int x0, int y0,