Mercurial > hg > svcore
comparison data/model/PowerOfSqrtTwoZoomConstraint.cpp @ 1527:710e6250a401 zoom
Merge from default branch
author | Chris Cannam |
---|---|
date | Mon, 17 Sep 2018 13:51:14 +0100 |
parents | d4a28d1479a8 48e9f538e6e9 |
children | c1c45c5146bb |
comparison
equal
deleted
inserted
replaced
1324:d4a28d1479a8 | 1527:710e6250a401 |
---|---|
19 #include <cmath> | 19 #include <cmath> |
20 | 20 |
21 | 21 |
22 ZoomLevel | 22 ZoomLevel |
23 PowerOfSqrtTwoZoomConstraint::getNearestZoomLevel(ZoomLevel requested, | 23 PowerOfSqrtTwoZoomConstraint::getNearestZoomLevel(ZoomLevel requested, |
24 RoundingDirection dir) const | 24 RoundingDirection dir) const |
25 { | 25 { |
26 int type, power; | 26 int type, power; |
27 int blockSize; | 27 int blockSize; |
28 | 28 |
29 if (requested.zone == ZoomLevel::FramesPerPixel) { | 29 if (requested.zone == ZoomLevel::FramesPerPixel) { |
45 } | 45 } |
46 } | 46 } |
47 | 47 |
48 int | 48 int |
49 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize, | 49 PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize, |
50 int &type, | 50 int &type, |
51 int &power, | 51 int &power, |
52 RoundingDirection dir) const | 52 RoundingDirection dir) const |
53 { | 53 { |
54 // cerr << "given " << blockSize << endl; | 54 // cerr << "given " << blockSize << endl; |
55 | 55 |
56 int minCachePower = getMinCachePower(); | 56 int minCachePower = getMinCachePower(); |
57 | 57 |
58 if (blockSize < (1 << minCachePower)) { | 58 if (blockSize < (1 << minCachePower)) { |
59 type = -1; | 59 type = -1; |
60 power = 0; | 60 power = 0; |
61 float val = 1.0, prevVal = 1.0; | 61 float val = 1.0, prevVal = 1.0; |
62 while (val + 0.01 < blockSize) { | 62 while (val + 0.01 < blockSize) { |
63 prevVal = val; | 63 prevVal = val; |
64 val *= sqrtf(2.f); | 64 val *= sqrtf(2.f); |
65 } | 65 } |
66 int rval; | 66 int rval; |
67 if (dir == RoundUp) rval = int(val + 0.01f); | 67 if (dir == RoundUp) rval = int(val + 0.01f); |
68 else if (dir == RoundDown) rval = int(prevVal + 0.01f); | 68 else if (dir == RoundDown) rval = int(prevVal + 0.01f); |
69 else if (val - float(blockSize) < | 69 else if (val - float(blockSize) < |
70 float(blockSize) - prevVal) rval = int(val + 0.01f); | 70 float(blockSize) - prevVal) rval = int(val + 0.01f); |
71 else rval = int(prevVal + 0.01); | 71 else rval = int(prevVal + 0.01); |
72 // SVDEBUG << "returning " << rval << endl; | 72 // SVDEBUG << "returning " << rval << endl; |
73 return rval; | 73 return rval; |
74 } | 74 } |
75 | 75 |
76 int prevBase = (1 << minCachePower); | 76 int prevBase = (1 << minCachePower); |
77 int prevPower = minCachePower; | 77 int prevPower = minCachePower; |
78 int prevType = 0; | 78 int prevType = 0; |
79 | 79 |
80 int result = 0; | 80 int result = 0; |
81 | 81 |
82 for (unsigned int i = 0; ; ++i) { | 82 for (unsigned int i = 0; ; ++i) { |
83 | 83 |
84 power = minCachePower + i/2; | 84 power = minCachePower + i/2; |
85 type = i % 2; | 85 type = i % 2; |
86 | 86 |
87 int base; | 87 int base; |
88 if (type == 0) { | 88 if (type == 0) { |
89 base = (1 << power); | 89 base = (1 << power); |
90 } else { | 90 } else { |
91 base = (((unsigned int)((1 << minCachePower) * sqrt(2.) + 0.01)) | 91 base = (((unsigned int)((1 << minCachePower) * sqrt(2.) + 0.01)) |
92 << (power - minCachePower)); | 92 << (power - minCachePower)); |
93 } | 93 } |
94 | 94 |
95 // SVDEBUG << "Testing base " << base << endl; | 95 // SVDEBUG << "Testing base " << base << endl; |
96 | 96 |
97 if (base == blockSize) { | 97 if (base == blockSize) { |
98 result = base; | 98 result = base; |
99 break; | 99 break; |
100 } | 100 } |
101 | 101 |
102 if (base > blockSize) { | 102 if (base > blockSize) { |
103 if (dir == RoundNearest) { | 103 if (dir == RoundNearest) { |
104 if (base - blockSize < blockSize - prevBase) { | 104 if (base - blockSize < blockSize - prevBase) { |
105 dir = RoundUp; | 105 dir = RoundUp; |
106 } else { | 106 } else { |
107 dir = RoundDown; | 107 dir = RoundDown; |
108 } | 108 } |
109 } | 109 } |
110 if (dir == RoundUp) { | 110 if (dir == RoundUp) { |
111 result = base; | 111 result = base; |
112 break; | 112 break; |
113 } else { | 113 } else { |
114 type = prevType; | 114 type = prevType; |
115 power = prevPower; | 115 power = prevPower; |
116 result = prevBase; | 116 result = prevBase; |
117 break; | 117 break; |
118 } | 118 } |
119 } | 119 } |
120 | 120 |
121 prevType = type; | 121 prevType = type; |
122 prevPower = power; | 122 prevPower = power; |
123 prevBase = base; | 123 prevBase = base; |
124 } | 124 } |
125 | 125 |
126 if (result > getMaxZoomLevel().level) { | 126 if (result > getMaxZoomLevel().level) { |
127 result = getMaxZoomLevel().level; | 127 result = getMaxZoomLevel().level; |
128 } | 128 } |