annotate 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
rev   line source
Chris@1532 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1532 2
Chris@1532 3 /*
Chris@1532 4 Sonic Visualiser
Chris@1532 5 An audio file viewer and annotation editor.
Chris@1532 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1532 7
Chris@1532 8 This program is free software; you can redistribute it and/or
Chris@1532 9 modify it under the terms of the GNU General Public License as
Chris@1532 10 published by the Free Software Foundation; either version 2 of the
Chris@1532 11 License, or (at your option) any later version. See the file
Chris@1532 12 COPYING included with this distribution for more information.
Chris@1532 13 */
Chris@1532 14
Chris@1532 15 #ifndef SV_ZOOM_LEVEL_H
Chris@1532 16 #define SV_ZOOM_LEVEL_H
Chris@1532 17
Chris@1532 18 #include <ostream>
Chris@1533 19 #include <cmath>
Chris@1532 20
Chris@1532 21 /** Display zoom level. Can be an integer number of samples per pixel,
Chris@1532 22 * or an integer number of pixels per sample.
Chris@1532 23 */
Chris@1532 24 struct ZoomLevel {
Chris@1532 25
Chris@1532 26 enum Zone {
Chris@1532 27 FramesPerPixel, // zoomed out (as in classic SV)
Chris@1532 28 PixelsPerFrame // zoomed in beyond 1-1 (interpolating the waveform)
Chris@1532 29 };
Chris@1533 30
Chris@1532 31 Zone zone;
Chris@1532 32 int level;
Chris@1532 33
Chris@1532 34 ZoomLevel() : zone(FramesPerPixel), level(1) { }
Chris@1532 35 ZoomLevel(Zone z, int lev) : zone(z), level(lev) { }
Chris@1532 36
Chris@1532 37 bool operator<(const ZoomLevel &other) const {
Chris@1532 38 if (zone == FramesPerPixel) {
Chris@1532 39 if (other.zone == zone) {
Chris@1532 40 return level < other.level;
Chris@1532 41 } else {
Chris@1532 42 return false;
Chris@1532 43 }
Chris@1532 44 } else {
Chris@1532 45 if (other.zone == zone) {
Chris@1532 46 return level > other.level;
Chris@1532 47 } else {
Chris@1532 48 return false;
Chris@1532 49 }
Chris@1532 50 }
Chris@1532 51 }
Chris@1532 52
Chris@1532 53 bool operator==(const ZoomLevel &other) const {
Chris@1532 54 return (zone == other.zone && level == other.level);
Chris@1532 55 }
Chris@1532 56
Chris@1532 57 ZoomLevel incremented() const {
Chris@1532 58 if (zone == FramesPerPixel) {
Chris@1532 59 return { zone, level + 1 };
Chris@1532 60 } else if (level == 1) {
Chris@1532 61 return { FramesPerPixel, 2 };
Chris@1532 62 } else if (level == 2) {
Chris@1532 63 return { FramesPerPixel, 1 };
Chris@1532 64 } else {
Chris@1532 65 return { zone, level - 1 };
Chris@1532 66 }
Chris@1532 67 }
Chris@1532 68
Chris@1532 69 ZoomLevel decremented() const {
Chris@1532 70 if (zone == PixelsPerFrame) {
Chris@1532 71 return { zone, level + 1 };
Chris@1532 72 } else if (level == 1) {
Chris@1532 73 return { PixelsPerFrame, 2 };
Chris@1532 74 } else {
Chris@1532 75 return { zone, level - 1 };
Chris@1532 76 }
Chris@1532 77 }
Chris@1532 78
Chris@1533 79 /** Inexact conversion. The result is a whole number if we are
Chris@1533 80 * zoomed in enough (in PixelsPerFrame zone), a fraction
Chris@1533 81 * otherwise.
Chris@1533 82 */
Chris@1532 83 double framesToPixels(double frames) const {
Chris@1532 84 if (zone == PixelsPerFrame) {
Chris@1532 85 return frames * level;
Chris@1532 86 } else {
Chris@1532 87 return frames / level;
Chris@1532 88 }
Chris@1532 89 }
Chris@1532 90
Chris@1533 91 /** Inexact conversion. The result is a whole number if we are
Chris@1533 92 * zoomed out enough (in FramesPerPixel zone), a fraction
Chris@1533 93 * otherwise.
Chris@1533 94 */
Chris@1532 95 double pixelsToFrames(double pixels) const {
Chris@1532 96 if (zone == PixelsPerFrame) {
Chris@1532 97 return pixels / level;
Chris@1532 98 } else {
Chris@1532 99 return pixels * level;
Chris@1532 100 }
Chris@1532 101 }
Chris@1533 102
Chris@1533 103 /** Return a ZoomLevel that approximates the given ratio of pixels
Chris@1533 104 * to frames.
Chris@1533 105 */
Chris@1533 106 static ZoomLevel fromRatio(int pixels, int frames) {
Chris@1533 107 if (pixels < frames) {
Chris@1533 108 return { FramesPerPixel, int(round(double(pixels)/frames)) };
Chris@1533 109 } else {
Chris@1533 110 int r = int(round(double(frames)/pixels));
Chris@1533 111 if (r > 1) {
Chris@1533 112 return { PixelsPerFrame, r };
Chris@1533 113 } else {
Chris@1533 114 return { FramesPerPixel, 1 };
Chris@1533 115 }
Chris@1533 116 }
Chris@1533 117 }
Chris@1532 118 };
Chris@1532 119
Chris@1532 120 std::ostream &operator<<(std::ostream &s, const ZoomLevel &z);
Chris@1532 121
Chris@1532 122 #endif