diff view/View.cpp @ 1375:694004228ab7 zoom

Fix incorrect start/end overlay drawing when zoomed far in
author Chris Cannam
date Tue, 06 Nov 2018 10:51:46 +0000
parents bbeffb29bf09
children 28075cc658c9
line wrap: on
line diff
--- a/view/View.cpp	Tue Nov 06 08:59:03 2018 +0000
+++ b/view/View.cpp	Tue Nov 06 10:51:46 2018 +0000
@@ -395,18 +395,50 @@
     
     sv_frame_t level = m_zoomLevel.level;
     sv_frame_t fdiff = frame - getCentreFrame();
-    int diff, result;
-
+    int result = 0;
+
+    bool inRange = false;
     if (m_zoomLevel.zone == ZoomLevel::FramesPerPixel) {
-        diff = int(fdiff / level);
-        if ((fdiff < 0) && ((fdiff % level) != 0)) {
-            --diff; // round to the left
+        inRange = ((fdiff / level) < sv_frame_t(INT_MAX) &&
+                   (fdiff / level) > sv_frame_t(INT_MIN));
+    } else {
+        inRange = (fdiff < sv_frame_t(INT_MAX) / level &&
+                   fdiff > sv_frame_t(INT_MIN) / level);
+    }
+
+    if (inRange) {
+        
+        sv_frame_t adjusted;
+
+        if (m_zoomLevel.zone == ZoomLevel::FramesPerPixel) {
+            adjusted = fdiff / level;
+            if ((fdiff < 0) && ((fdiff % level) != 0)) {
+                --adjusted; // round to the left
+            }
+        } else {
+            adjusted = fdiff * level;
         }
-    } else {
-        diff = int(fdiff * level);
+
+        adjusted = adjusted + (width()/2);
+
+        if (adjusted > INT_MAX || adjusted < INT_MIN) {
+            inRange = false;
+        } else {
+            result = int(adjusted);
+        }
     }
 
-    result = int(diff + (width()/2));
+    if (!inRange) {
+        SVCERR << "ERROR: Frame " << frame
+               << " is out of range in View::getXForFrame" << endl;
+        SVCERR << "ERROR: (centre frame = " << getCentreFrame() << ", fdiff = "
+               << fdiff << ", zoom level = " << m_zoomLevel << ")" << endl;
+        SVCERR << "ERROR: This is a logic error: getXForFrame should not be "
+               << "called for locations unadjacent to the current view"
+               << endl;
+        return 0;
+    }
+
     return result;
 }