comparison base/ZoomLevel.h @ 1532:5360f7aba189 zoom

Pull out ZoomLevel, add inexact frame/pixel conversion functions and streaming
author Chris Cannam
date Wed, 19 Sep 2018 15:41:44 +0100
parents
children 53073777e591
comparison
equal deleted inserted replaced
1531:2f3a77472c8c 1532:5360f7aba189
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #ifndef SV_ZOOM_LEVEL_H
16 #define SV_ZOOM_LEVEL_H
17
18 #include <ostream>
19
20 /** Display zoom level. Can be an integer number of samples per pixel,
21 * or an integer number of pixels per sample.
22 */
23 struct ZoomLevel {
24
25 enum Zone {
26 FramesPerPixel, // zoomed out (as in classic SV)
27 PixelsPerFrame // zoomed in beyond 1-1 (interpolating the waveform)
28 };
29 Zone zone;
30 int level;
31
32 ZoomLevel() : zone(FramesPerPixel), level(1) { }
33 ZoomLevel(Zone z, int lev) : zone(z), level(lev) { }
34
35 bool operator<(const ZoomLevel &other) const {
36 if (zone == FramesPerPixel) {
37 if (other.zone == zone) {
38 return level < other.level;
39 } else {
40 return false;
41 }
42 } else {
43 if (other.zone == zone) {
44 return level > other.level;
45 } else {
46 return false;
47 }
48 }
49 }
50
51 bool operator==(const ZoomLevel &other) const {
52 return (zone == other.zone && level == other.level);
53 }
54
55 ZoomLevel incremented() const {
56 if (zone == FramesPerPixel) {
57 return { zone, level + 1 };
58 } else if (level == 1) {
59 return { FramesPerPixel, 2 };
60 } else if (level == 2) {
61 return { FramesPerPixel, 1 };
62 } else {
63 return { zone, level - 1 };
64 }
65 }
66
67 ZoomLevel decremented() const {
68 if (zone == PixelsPerFrame) {
69 return { zone, level + 1 };
70 } else if (level == 1) {
71 return { PixelsPerFrame, 2 };
72 } else {
73 return { zone, level - 1 };
74 }
75 }
76
77 /// Inexact
78 double framesToPixels(double frames) const {
79 if (zone == PixelsPerFrame) {
80 return frames * level;
81 } else {
82 return frames / level;
83 }
84 }
85
86 /// Inexact
87 double pixelsToFrames(double pixels) const {
88 if (zone == PixelsPerFrame) {
89 return pixels / level;
90 } else {
91 return pixels * level;
92 }
93 }
94 };
95
96 std::ostream &operator<<(std::ostream &s, const ZoomLevel &z);
97
98 #endif