# HG changeset patch # User Sebastien Jodogne # Date 1519892946 -3600 # Node ID 509041cb57db4332af5b3ec84410b6c9fd9aee2e # Parent 8f3c2017b0e33cf8341c8d1887054fbfe8253a8f speedup by truncating instead of rounding diff -r 8f3c2017b0e3 -r 509041cb57db Core/Images/ImageProcessing.cpp --- a/Core/Images/ImageProcessing.cpp Tue Feb 20 11:16:49 2018 +0100 +++ b/Core/Images/ImageProcessing.cpp Thu Mar 01 09:29:06 2018 +0100 @@ -166,11 +166,13 @@ minValue = std::numeric_limits::max(); maxValue = std::numeric_limits::min(); + const unsigned int width = source.GetWidth(); + for (unsigned int y = 0; y < source.GetHeight(); y++) { const PixelType* p = reinterpret_cast(source.GetConstRow(y)); - for (unsigned int x = 0; x < source.GetWidth(); x++, p++) + for (unsigned int x = 0; x < width; x++, p++) { if (*p < minValue) { @@ -236,14 +238,19 @@ const int64_t minValue = std::numeric_limits::min(); const int64_t maxValue = std::numeric_limits::max(); + const unsigned int width = image.GetWidth(); for (unsigned int y = 0; y < image.GetHeight(); y++) { PixelType* p = reinterpret_cast(image.GetRow(y)); - for (unsigned int x = 0; x < image.GetWidth(); x++, p++) + for (unsigned int x = 0; x < width; x++, p++) { - int64_t v = boost::math::llround(static_cast(*p) * factor); + // The "round" operation is extremely costly. We use + // truncation instead since Orthanc 1.3.2. + + //int64_t v = boost::math::llround(static_cast(*p) * factor); + int64_t v = static_cast(static_cast(*p) * factor); if (v > maxValue) { @@ -267,28 +274,37 @@ float offset, float scaling) { - const float minValue = static_cast(std::numeric_limits::min()); - const float maxValue = static_cast(std::numeric_limits::max()); + const float minFloatValue = static_cast(std::numeric_limits::min()); + const float maxFloatValue = static_cast(std::numeric_limits::max()); + const PixelType minPixelValue = std::numeric_limits::min(); + const PixelType maxPixelValue = std::numeric_limits::max(); - for (unsigned int y = 0; y < image.GetHeight(); y++) + const unsigned int height = image.GetHeight(); + const unsigned int width = image.GetWidth(); + + for (unsigned int y = 0; y < height; y++) { PixelType* p = reinterpret_cast(image.GetRow(y)); - for (unsigned int x = 0; x < image.GetWidth(); x++, p++) + for (unsigned int x = 0; x < width; x++, p++) { float v = (static_cast(*p) + offset) * scaling; - if (v > maxValue) + if (v > maxFloatValue) { - *p = std::numeric_limits::max(); + *p = maxPixelValue; } - else if (v < minValue) + else if (v < minFloatValue) { - *p = std::numeric_limits::min(); + *p = minPixelValue; } else { - *p = static_cast(boost::math::iround(v)); + // The "round" operation is extremely costly. We use + // truncation instead since Orthanc 1.3.2. + + //*p = static_cast(boost::math::iround(v)); + *p = static_cast(v); } } }