comparison base/ZoomLevel.h @ 1533:53073777e591 zoom

More ZoomLevel updates
author Chris Cannam
date Thu, 20 Sep 2018 10:45:48 +0100
parents 5360f7aba189
children 8f9e9cff141d
comparison
equal deleted inserted replaced
1532:5360f7aba189 1533:53073777e591
14 14
15 #ifndef SV_ZOOM_LEVEL_H 15 #ifndef SV_ZOOM_LEVEL_H
16 #define SV_ZOOM_LEVEL_H 16 #define SV_ZOOM_LEVEL_H
17 17
18 #include <ostream> 18 #include <ostream>
19 #include <cmath>
19 20
20 /** Display zoom level. Can be an integer number of samples per pixel, 21 /** Display zoom level. Can be an integer number of samples per pixel,
21 * or an integer number of pixels per sample. 22 * or an integer number of pixels per sample.
22 */ 23 */
23 struct ZoomLevel { 24 struct ZoomLevel {
24 25
25 enum Zone { 26 enum Zone {
26 FramesPerPixel, // zoomed out (as in classic SV) 27 FramesPerPixel, // zoomed out (as in classic SV)
27 PixelsPerFrame // zoomed in beyond 1-1 (interpolating the waveform) 28 PixelsPerFrame // zoomed in beyond 1-1 (interpolating the waveform)
28 }; 29 };
30
29 Zone zone; 31 Zone zone;
30 int level; 32 int level;
31 33
32 ZoomLevel() : zone(FramesPerPixel), level(1) { } 34 ZoomLevel() : zone(FramesPerPixel), level(1) { }
33 ZoomLevel(Zone z, int lev) : zone(z), level(lev) { } 35 ZoomLevel(Zone z, int lev) : zone(z), level(lev) { }
72 } else { 74 } else {
73 return { zone, level - 1 }; 75 return { zone, level - 1 };
74 } 76 }
75 } 77 }
76 78
77 /// Inexact 79 /** Inexact conversion. The result is a whole number if we are
80 * zoomed in enough (in PixelsPerFrame zone), a fraction
81 * otherwise.
82 */
78 double framesToPixels(double frames) const { 83 double framesToPixels(double frames) const {
79 if (zone == PixelsPerFrame) { 84 if (zone == PixelsPerFrame) {
80 return frames * level; 85 return frames * level;
81 } else { 86 } else {
82 return frames / level; 87 return frames / level;
83 } 88 }
84 } 89 }
85 90
86 /// Inexact 91 /** Inexact conversion. The result is a whole number if we are
92 * zoomed out enough (in FramesPerPixel zone), a fraction
93 * otherwise.
94 */
87 double pixelsToFrames(double pixels) const { 95 double pixelsToFrames(double pixels) const {
88 if (zone == PixelsPerFrame) { 96 if (zone == PixelsPerFrame) {
89 return pixels / level; 97 return pixels / level;
90 } else { 98 } else {
91 return pixels * level; 99 return pixels * level;
92 } 100 }
93 } 101 }
102
103 /** Return a ZoomLevel that approximates the given ratio of pixels
104 * to frames.
105 */
106 static ZoomLevel fromRatio(int pixels, int frames) {
107 if (pixels < frames) {
108 return { FramesPerPixel, int(round(double(pixels)/frames)) };
109 } else {
110 int r = int(round(double(frames)/pixels));
111 if (r > 1) {
112 return { PixelsPerFrame, r };
113 } else {
114 return { FramesPerPixel, 1 };
115 }
116 }
117 }
94 }; 118 };
95 119
96 std::ostream &operator<<(std::ostream &s, const ZoomLevel &z); 120 std::ostream &operator<<(std::ostream &s, const ZoomLevel &z);
97 121
98 #endif 122 #endif