# HG changeset patch # User Sebastien Jodogne # Date 1604430355 -3600 # Node ID 9279de56a4053fd64a846053cd716a5511f477a1 # Parent c5ca798b158a72202bfdc0437a2a6d74bfa71882 avoid multiple calls to GetWidth() and GetHeight() on pixel loops diff -r c5ca798b158a -r 9279de56a405 OrthancFramework/Sources/DicomFormat/DicomIntegerPixelAccessor.cpp --- a/OrthancFramework/Sources/DicomFormat/DicomIntegerPixelAccessor.cpp Tue Nov 03 18:45:32 2020 +0100 +++ b/OrthancFramework/Sources/DicomFormat/DicomIntegerPixelAccessor.cpp Tue Nov 03 20:05:55 2020 +0100 @@ -104,12 +104,16 @@ min = std::numeric_limits::max(); max = std::numeric_limits::min(); + + const unsigned int height = information_.GetHeight(); + const unsigned int width = information_.GetWidth(); + const unsigned int channels = information_.GetChannelCount(); - for (unsigned int y = 0; y < information_.GetHeight(); y++) + for (unsigned int y = 0; y < height; y++) { - for (unsigned int x = 0; x < information_.GetWidth(); x++) + for (unsigned int x = 0; x < width; x++) { - for (unsigned int c = 0; c < information_.GetChannelCount(); c++) + for (unsigned int c = 0; c < channels; c++) { int32_t v = GetValue(x, y); if (v < min) diff -r c5ca798b158a -r 9279de56a405 OrthancFramework/Sources/DicomParsing/Internals/DicomImageDecoder.cpp --- a/OrthancFramework/Sources/DicomParsing/Internals/DicomImageDecoder.cpp Tue Nov 03 18:45:32 2020 +0100 +++ b/OrthancFramework/Sources/DicomParsing/Internals/DicomImageDecoder.cpp Tue Nov 03 20:05:55 2020 +0100 @@ -354,12 +354,16 @@ const PixelType minValue = std::numeric_limits::min(); const PixelType maxValue = std::numeric_limits::max(); - for (unsigned int y = 0; y < source.GetInformation().GetHeight(); y++) + const unsigned int height = source.GetInformation().GetHeight(); + const unsigned int width = source.GetInformation().GetWidth(); + const unsigned int channels = source.GetInformation().GetChannelCount(); + + for (unsigned int y = 0; y < height; y++) { PixelType* pixel = reinterpret_cast(target.GetRow(y)); - for (unsigned int x = 0; x < source.GetInformation().GetWidth(); x++) + for (unsigned int x = 0; x < width; x++) { - for (unsigned int c = 0; c < source.GetInformation().GetChannelCount(); c++, pixel++) + for (unsigned int c = 0; c < channels; c++, pixel++) { int32_t v = source.GetValue(x, y, c); if (v < static_cast(minValue)) @@ -437,12 +441,14 @@ } const uint8_t* source = reinterpret_cast(pixelData); + const unsigned int width = target->GetWidth(); + const unsigned int height = target->GetHeight(); - for (unsigned int y = 0; y < target->GetHeight(); y++) + for (unsigned int y = 0; y < height; y++) { uint8_t* p = reinterpret_cast(target->GetRow(y)); - for (unsigned int x = 0; x < target->GetWidth(); x++) + for (unsigned int x = 0; x < width; x++) { p[0] = lutRed[*source] >> 8; p[1] = lutGreen[*source] >> 8; @@ -467,12 +473,14 @@ } const uint16_t* source = reinterpret_cast(pixelData); + const unsigned int width = target->GetWidth(); + const unsigned int height = target->GetHeight(); - for (unsigned int y = 0; y < target->GetHeight(); y++) + for (unsigned int y = 0; y < height; y++) { uint16_t* p = reinterpret_cast(target->GetRow(y)); - for (unsigned int x = 0; x < target->GetWidth(); x++) + for (unsigned int x = 0; x < width; x++) { p[0] = lutRed[*source]; p[1] = lutGreen[*source]; diff -r c5ca798b158a -r 9279de56a405 OrthancFramework/Sources/DicomParsing/ParsedDicomFile.cpp --- a/OrthancFramework/Sources/DicomParsing/ParsedDicomFile.cpp Tue Nov 03 18:45:32 2020 +0100 +++ b/OrthancFramework/Sources/DicomParsing/ParsedDicomFile.cpp Tue Nov 03 20:05:55 2020 +0100 @@ -1363,7 +1363,10 @@ Uint8* target = NULL; pixels->createUint8Array(accessor.GetHeight() * pitch, target); - for (unsigned int y = 0; y < accessor.GetHeight(); y++) + const unsigned int height = accessor.GetHeight(); + const unsigned int width = accessor.GetWidth(); + + for (unsigned int y = 0; y < height; y++) { switch (accessor.GetFormat()) { @@ -1381,7 +1384,7 @@ { // The alpha channel is not supported by the DICOM standard const Uint8* source = reinterpret_cast(accessor.GetConstRow(y)); - for (unsigned int x = 0; x < accessor.GetWidth(); x++, target += 3, source += 4) + for (unsigned int x = 0; x < width; x++, target += 3, source += 4) { target[0] = source[0]; target[1] = source[1]; diff -r c5ca798b158a -r 9279de56a405 OrthancFramework/Sources/Images/ImageAccessor.cpp --- a/OrthancFramework/Sources/Images/ImageAccessor.cpp Tue Nov 03 18:45:32 2020 +0100 +++ b/OrthancFramework/Sources/Images/ImageAccessor.cpp Tue Nov 03 20:05:55 2020 +0100 @@ -41,7 +41,10 @@ { target.AddChunk("double([ "); - for (unsigned int y = 0; y < source.GetHeight(); y++) + const unsigned int width = source.GetWidth(); + const unsigned int height = source.GetHeight(); + + for (unsigned int y = 0; y < height; y++) { const PixelType* p = reinterpret_cast(source.GetConstRow(y)); @@ -51,9 +54,9 @@ s = "; "; } - s.reserve(source.GetWidth() * 8); + s.reserve(width * 8); - for (unsigned int x = 0; x < source.GetWidth(); x++, p++) + for (unsigned int x = 0; x < width; x++, p++) { s += boost::lexical_cast(static_cast(*p)) + " "; } @@ -72,14 +75,17 @@ target.AddChunk("double(permute(reshape([ "); - for (unsigned int y = 0; y < source.GetHeight(); y++) + const unsigned int width = source.GetWidth(); + const unsigned int height = source.GetHeight(); + + for (unsigned int y = 0; y < height; y++) { const uint8_t* p = reinterpret_cast(source.GetConstRow(y)); std::string s; - s.reserve(source.GetWidth() * 3 * 8); + s.reserve(width * 3 * 8); - for (unsigned int x = 0; x < 3 * source.GetWidth(); x++, p++) + for (unsigned int x = 0; x < 3 * width; x++, p++) { s += boost::lexical_cast(static_cast(*p)) + " "; } @@ -87,8 +93,8 @@ target.AddChunk(s); } - target.AddChunk("], [ 3 " + boost::lexical_cast(source.GetHeight()) + - " " + boost::lexical_cast(source.GetWidth()) + " ]), [ 3 2 1 ]))"); + target.AddChunk("], [ 3 " + boost::lexical_cast(height) + + " " + boost::lexical_cast(width) + " ]), [ 3 2 1 ]))"); } diff -r c5ca798b158a -r 9279de56a405 OrthancFramework/Sources/Images/ImageProcessing.cpp --- a/OrthancFramework/Sources/Images/ImageProcessing.cpp Tue Nov 03 18:45:32 2020 +0100 +++ b/OrthancFramework/Sources/Images/ImageProcessing.cpp Tue Nov 03 20:05:55 2020 +0100 @@ -501,11 +501,11 @@ throw OrthancException(ErrorCode_IncompatibleImageFormat); } - unsigned int lineSize = source.GetBytesPerPixel() * source.GetWidth(); - + const unsigned int lineSize = source.GetBytesPerPixel() * source.GetWidth(); assert(source.GetPitch() >= lineSize && target.GetPitch() >= lineSize); - for (unsigned int y = 0; y < source.GetHeight(); y++) + const unsigned int height = source.GetHeight(); + for (unsigned int y = 0; y < height; y++) { memcpy(target.GetRow(y), source.GetConstRow(y), lineSize); } diff -r c5ca798b158a -r 9279de56a405 OrthancFramework/Sources/Images/PamReader.cpp --- a/OrthancFramework/Sources/Images/PamReader.cpp Tue Nov 03 18:45:32 2020 +0100 +++ b/OrthancFramework/Sources/Images/PamReader.cpp Tue Nov 03 20:05:55 2020 +0100 @@ -229,7 +229,7 @@ { uint16_t* pixel = reinterpret_cast(GetRow(h)); - for (unsigned int w = 0; w < GetWidth(); ++w, ++pixel) + for (unsigned int w = 0; w < width; ++w, ++pixel) { #if defined(__EMSCRIPTEN__) // For WebAssembly /*