changeset 4278:9279de56a405

avoid multiple calls to GetWidth() and GetHeight() on pixel loops
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 03 Nov 2020 20:05:55 +0100
parents c5ca798b158a
children ab4d015af660
files OrthancFramework/Sources/DicomFormat/DicomIntegerPixelAccessor.cpp OrthancFramework/Sources/DicomParsing/Internals/DicomImageDecoder.cpp OrthancFramework/Sources/DicomParsing/ParsedDicomFile.cpp OrthancFramework/Sources/Images/ImageAccessor.cpp OrthancFramework/Sources/Images/ImageProcessing.cpp OrthancFramework/Sources/Images/PamReader.cpp
diffstat 6 files changed, 45 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- 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<int32_t>::max();
     max = std::numeric_limits<int32_t>::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)
--- 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<PixelType>::min();
     const PixelType maxValue = std::numeric_limits<PixelType>::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<PixelType*>(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<int32_t>(minValue))
@@ -437,12 +441,14 @@
         }
 
         const uint8_t* source = reinterpret_cast<const uint8_t*>(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<uint8_t*>(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<const uint16_t*>(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<uint16_t*>(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];
--- 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<const Uint8*>(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];
--- 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<const PixelType*>(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<std::string>(static_cast<double>(*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<const uint8_t*>(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<std::string>(static_cast<int>(*p)) + " ";
       }
@@ -87,8 +93,8 @@
       target.AddChunk(s);
     }
 
-    target.AddChunk("], [ 3 " + boost::lexical_cast<std::string>(source.GetHeight()) +
-                    " " + boost::lexical_cast<std::string>(source.GetWidth()) + " ]), [ 3 2 1 ]))");
+    target.AddChunk("], [ 3 " + boost::lexical_cast<std::string>(height) +
+                    " " + boost::lexical_cast<std::string>(width) + " ]), [ 3 2 1 ]))");
   }
   
 
--- 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);
     }
--- 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<uint16_t*>(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
           /*