comparison 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
comparison
equal deleted inserted replaced
1526:8988b27ebf38 1528:a7485c1bdba5
17 17
18 int 18 int
19 PowerOfTwoZoomConstraint::getNearestBlockSize(int req, 19 PowerOfTwoZoomConstraint::getNearestBlockSize(int req,
20 RoundingDirection dir) const 20 RoundingDirection dir) const
21 { 21 {
22 int result = 0; 22 int max = getMaxZoomLevel();
23 23
24 for (int bs = 1; ; bs *= 2) { 24 if (req > max) {
25 if (bs >= req) { 25 return max;
26 }
27
28 for (int bs = 1; bs <= max; bs *= 2) {
29 if (bs < req) {
30 continue;
31 } else if (bs == req) {
32 return bs;
33 } else { // bs > req
26 if (dir == RoundNearest) { 34 if (dir == RoundNearest) {
27 if (bs - req < req - bs/2) { 35 if (bs - req < req - bs/2) {
28 result = bs; 36 return bs;
29 break;
30 } else { 37 } else {
31 result = bs/2; 38 return bs/2;
32 break;
33 } 39 }
34 } else if (dir == RoundDown) { 40 } else if (dir == RoundDown) {
35 result = bs/2; 41 return bs/2;
36 break;
37 } else { 42 } else {
38 result = bs; 43 return bs;
39 break;
40 } 44 }
41 } 45 }
42 } 46 }
43 47
44 if (result > getMaxZoomLevel()) result = getMaxZoomLevel(); 48 return max;
45 return result;
46 } 49 }
47 50