diff data/model/PowerOfTwoZoomConstraint.cpp @ 1528:a7485c1bdba5

Tests and a couple of minor fixes for zoom constraints
author Chris Cannam
date Tue, 18 Sep 2018 15:04:46 +0100
parents 48e9f538e6e9
children c1c45c5146bb
line wrap: on
line diff
--- a/data/model/PowerOfTwoZoomConstraint.cpp	Fri Sep 14 15:32:43 2018 +0100
+++ b/data/model/PowerOfTwoZoomConstraint.cpp	Tue Sep 18 15:04:46 2018 +0100
@@ -19,29 +19,32 @@
 PowerOfTwoZoomConstraint::getNearestBlockSize(int req,
                                               RoundingDirection dir) const
 {
-    int result = 0;
+    int max = getMaxZoomLevel();
 
-    for (int bs = 1; ; bs *= 2) {
-        if (bs >= req) {
+    if (req > max) {
+        return max;
+    }
+
+    for (int bs = 1; bs <= max; bs *= 2) {
+        if (bs < req) {
+            continue;
+        } else if (bs == req) {
+            return bs;
+        } else { // bs > req
             if (dir == RoundNearest) {
                 if (bs - req < req - bs/2) {
-                    result = bs;
-                    break;
+                    return bs;
                 } else {
-                    result = bs/2;
-                    break;
+                    return bs/2;
                 }
             } else if (dir == RoundDown) {
-                result = bs/2;
-                break;
+                return bs/2;
             } else {
-                result = bs;
-                break;
+                return bs;
             }
         }
     }
 
-    if (result > getMaxZoomLevel()) result = getMaxZoomLevel();
-    return result;
+    return max;
 }