changeset 1550:1ae6a19464a7 zoom-double

Messing with non-integer zoom ratios. But I don't think this is going anywhere as it stands
author Chris Cannam
date Mon, 08 Oct 2018 13:39:40 +0100
parents 32400727bcbd
children
files base/ZoomLevel.h data/model/PowerOfSqrtTwoZoomConstraint.cpp data/model/PowerOfSqrtTwoZoomConstraint.h data/model/PowerOfTwoZoomConstraint.cpp data/model/PowerOfTwoZoomConstraint.h data/model/ReadOnlyWaveFileModel.cpp
diffstat 6 files changed, 34 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/base/ZoomLevel.h	Mon Oct 01 15:48:02 2018 +0100
+++ b/base/ZoomLevel.h	Mon Oct 08 13:39:40 2018 +0100
@@ -31,10 +31,10 @@
     };
 
     Zone zone;
-    int level;
+    double level;
 
     ZoomLevel() : zone(FramesPerPixel), level(1) { }
-    ZoomLevel(Zone z, int lev) : zone(z), level(lev) { }
+    ZoomLevel(Zone z, double lev) : zone(z), level(lev) { }
     
     bool operator<(const ZoomLevel &other) const {
         if (zone == FramesPerPixel) {
@@ -107,9 +107,9 @@
      */
     static ZoomLevel fromRatio(int pixels, sv_frame_t frames) {
         if (pixels < frames) {
-            return { FramesPerPixel, int(round(double(frames)/pixels)) };
+            return { FramesPerPixel, round(double(frames)/pixels) };
         } else {
-            int r = int(round(pixels/double(frames)));
+            double r = round(pixels/double(frames));
             if (r > 1) {
                 return { PixelsPerFrame, r };
             } else {
--- a/data/model/PowerOfSqrtTwoZoomConstraint.cpp	Mon Oct 01 15:48:02 2018 +0100
+++ b/data/model/PowerOfSqrtTwoZoomConstraint.cpp	Mon Oct 08 13:39:40 2018 +0100
@@ -26,7 +26,7 @@
                                                   RoundingDirection dir) const
 {
     int type, power;
-    int blockSize;
+    double blockSize;
 
     if (requested.zone == ZoomLevel::FramesPerPixel) {
         blockSize = getNearestBlockSize(requested.level, type, power, dir);
@@ -47,8 +47,8 @@
     }
 }
 
-int
-PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(int blockSize,
+double
+PowerOfSqrtTwoZoomConstraint::getNearestBlockSize(double blockSize,
                                                   int &type, 
                                                   int &power,
                                                   RoundingDirection dir) const
@@ -57,46 +57,47 @@
 
     int minCachePower = getMinCachePower();
 
+    double eps = 1e-8;
+    
     if (blockSize < (1 << minCachePower)) {
         type = -1;
         power = 0;
-        float val = 1.0, prevVal = 1.0;
-        while (val + 0.01 < blockSize) {
+        double val = 1.0, prevVal = 1.0;
+        while (val + eps < blockSize) {
             prevVal = val;
             val *= sqrtf(2.f);
         }
-        int rval = int(val + 0.01f);
+        double rval = val;
 //        SVCERR << "got val = " << val << ", rval = " << rval << ", prevVal = " << prevVal << endl;
         if (rval != blockSize && dir != RoundUp) {
             if (dir == RoundDown) {
-                rval = int(prevVal + 0.01f);
-            } else if (val - float(blockSize) < float(blockSize) - prevVal) {
-                rval = int(val + 0.01f);
+                rval = prevVal;
+            } else if (val - blockSize < blockSize - prevVal) {
+                rval = val;
             } else {
-                rval = int(prevVal + 0.01);
+                rval = prevVal;
             }
         }
 //        SVCERR << "returning " << rval << endl;
         return rval;
     }
 
-    int prevBase = (1 << minCachePower);
+    double prevBase = (1 << minCachePower);
     int prevPower = minCachePower;
     int prevType = 0;
 
-    int result = 0;
+    double result = 0;
 
     for (unsigned int i = 0; ; ++i) {
 
         power = minCachePower + i/2;
         type = i % 2;
 
-        int base;
+        double base;
         if (type == 0) {
-            base = (1 << power);
+            base = pow(2.0, power);
         } else {
-            base = (((unsigned int)((1 << minCachePower) * sqrt(2.) + 0.01))
-                    << (power - minCachePower));
+            base = sqrt(2.0) * pow(2.0, power);
         }
 
 //        SVCERR << "Testing base " << base << " (i = " << i << ", power = " << power << ", type = " << type << ")" << endl;
--- a/data/model/PowerOfSqrtTwoZoomConstraint.h	Mon Oct 01 15:48:02 2018 +0100
+++ b/data/model/PowerOfSqrtTwoZoomConstraint.h	Mon Oct 08 13:39:40 2018 +0100
@@ -27,10 +27,10 @@
 	
     virtual int getMinCachePower() const { return 6; }
 
-    virtual int getNearestBlockSize(int requestedBlockSize,
-                                    int &type,
-                                    int &power,
-                                    RoundingDirection dir = RoundNearest)
+    virtual double getNearestBlockSize(double requestedBlockSize,
+                                       int &type,
+                                       int &power,
+                                       RoundingDirection dir = RoundNearest)
         const;
 };
 
--- a/data/model/PowerOfTwoZoomConstraint.cpp	Mon Oct 01 15:48:02 2018 +0100
+++ b/data/model/PowerOfTwoZoomConstraint.cpp	Mon Oct 08 13:39:40 2018 +0100
@@ -19,7 +19,7 @@
 PowerOfTwoZoomConstraint::getNearestZoomLevel(ZoomLevel requested,
                                               RoundingDirection dir) const
 {
-    int blockSize;
+    double blockSize;
 
     if (requested.zone == ZoomLevel::FramesPerPixel) {
         blockSize = getNearestBlockSize(requested.level, dir);
@@ -35,7 +35,7 @@
         if (blockSize > getMinZoomLevel().level) {
             blockSize = getMinZoomLevel().level;
         }
-        if (blockSize == 1) {
+        if (blockSize == 1.0) {
             return { ZoomLevel::FramesPerPixel, 1 };
         } else {
             return { requested.zone, blockSize };
@@ -43,17 +43,17 @@
     }
 }
 
-int
-PowerOfTwoZoomConstraint::getNearestBlockSize(int req,
+double
+PowerOfTwoZoomConstraint::getNearestBlockSize(double req,
                                               RoundingDirection dir) const
 {
-    int max = getMaxZoomLevel().level;
+    double max = getMaxZoomLevel().level;
 
     if (req > max) {
         return max;
     }
 
-    for (int bs = 1; bs <= max; bs *= 2) {
+    for (double bs = 1; bs <= max; bs *= 2) {
         if (bs < req) {
             continue;
         } else if (bs == req) {
--- a/data/model/PowerOfTwoZoomConstraint.h	Mon Oct 01 15:48:02 2018 +0100
+++ b/data/model/PowerOfTwoZoomConstraint.h	Mon Oct 08 13:39:40 2018 +0100
@@ -26,8 +26,8 @@
 	const override;
 
 protected:
-    virtual int getNearestBlockSize(int requested,
-                                    RoundingDirection dir = RoundNearest)
+    virtual double getNearestBlockSize(double requested,
+                                       RoundingDirection dir = RoundNearest)
         const;
 };
 
--- a/data/model/ReadOnlyWaveFileModel.cpp	Mon Oct 01 15:48:02 2018 +0100
+++ b/data/model/ReadOnlyWaveFileModel.cpp	Mon Oct 08 13:39:40 2018 +0100
@@ -339,7 +339,7 @@
 {
     int cacheType = 0;
     int power = m_zoomConstraint.getMinCachePower();
-    int roundedBlockSize = m_zoomConstraint.getNearestBlockSize
+    double roundedBlockSize = m_zoomConstraint.getNearestBlockSize
         (desired, cacheType, power, ZoomConstraint::RoundDown);
 
     if (cacheType != 0 && cacheType != 1) {