From e6977b6001b8be1c8a3e1b4e8c3684b215ceda69 Mon Sep 17 00:00:00 2001
From: Guillaume Pasero <guillaume.pasero@c-s.fr>
Date: Thu, 26 Jan 2017 11:55:57 +0100
Subject: [PATCH] BUG: Mantis-1345: number of overview computed based on the
 max of the dimensions

---
 .../IO/IOGDAL/src/otbGDALOverviewsBuilder.cxx |  2 +-
 .../src/mvdImportImagesDialog.cxx             | 25 ++++++++++++-------
 2 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/Modules/IO/IOGDAL/src/otbGDALOverviewsBuilder.cxx b/Modules/IO/IOGDAL/src/otbGDALOverviewsBuilder.cxx
index ecfdfcbeb1..5e15ca4373 100644
--- a/Modules/IO/IOGDAL/src/otbGDALOverviewsBuilder.cxx
+++ b/Modules/IO/IOGDAL/src/otbGDALOverviewsBuilder.cxx
@@ -200,7 +200,7 @@ GDALOverviewsBuilder
   unsigned int n =
     std::min(
       count,
-      CountResolutions(	factor )
+      CountResolutions( factor , 1)
     );
 
   for( unsigned int i=0; i<n; ++i )
diff --git a/Modules/Visualization/MonteverdiGui/src/mvdImportImagesDialog.cxx b/Modules/Visualization/MonteverdiGui/src/mvdImportImagesDialog.cxx
index 15b403ce5b..76614c96af 100644
--- a/Modules/Visualization/MonteverdiGui/src/mvdImportImagesDialog.cxx
+++ b/Modules/Visualization/MonteverdiGui/src/mvdImportImagesDialog.cxx
@@ -222,14 +222,22 @@ ImportImagesDialog
 	      I18nCoreApplication::SETTINGS_KEY_OVERVIEWS_SIZE
 	    )
 	  );
-
-	  count =
-	    builder->CountResolutions(
-	      2,
-	      value.isValid()
-	      ? value.toInt()
-	      : OVERVIEWS_SIZE_DEFAULT
-	    );
+    unsigned int threshold =
+      value.isValid() ? value.toInt() : OVERVIEWS_SIZE_DEFAULT;
+
+    otb::GDALOverviewsBuilder::SizeVector ovrSizes;
+    builder->SetResolutionFactor(2);
+    // Count all resolution with minimum size 1 (the high requested count is
+    // clamped by the builder)
+    builder->ListResolutions(ovrSizes, 2, 9999);
+    // Count the number of levels with a least 1 size larger than the default size
+    for (unsigned int k=0 ; k<ovrSizes.size() ; k++)
+      {
+      if (std::max(ovrSizes[k][0],ovrSizes[k][1]) >= threshold)
+        count++;
+      else
+        break;
+      }
 	}
 
 	if( builder->GetOverviewsCount()>0 )
@@ -247,7 +255,6 @@ ImportImagesDialog
     builder->SetBypassEnabled(true);
     }
 
-	builder->SetResolutionFactor( 2 );
 	builder->SetNbResolutions( count );
 	builder->SetResamplingMethod( otb::GDAL_RESAMPLING_AVERAGE );
 	builder->SetCompressionMethod( otb::GDAL_COMPRESSION_NONE );
-- 
GitLab