annotate 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
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@1532 19
Chris@1532 20 /** Display zoom level. Can be an integer number of samples per pixel,
Chris@1532 21 * or an integer number of pixels per sample.
Chris@1532 22 */
Chris@1532 23 struct ZoomLevel {
Chris@1532 24
Chris@1532 25 enum Zone {
Chris@1532 26 FramesPerPixel, // zoomed out (as in classic SV)
Chris@1532 27 PixelsPerFrame // zoomed in beyond 1-1 (interpolating the waveform)
Chris@1532 28 };
Chris@1532 29 Zone zone;
Chris@1532 30 int level;
Chris@1532 31
Chris@1532 32 ZoomLevel() : zone(FramesPerPixel), level(1) { }
Chris@1532 33 ZoomLevel(Zone z, int lev) : zone(z), level(lev) { }
Chris@1532 34
Chris@1532 35 bool operator<(const ZoomLevel &other) const {
Chris@1532 36 if (zone == FramesPerPixel) {
Chris@1532 37 if (other.zone == zone) {
Chris@1532 38 return level < other.level;
Chris@1532 39 } else {
Chris@1532 40 return false;
Chris@1532 41 }
Chris@1532 42 } else {
Chris@1532 43 if (other.zone == zone) {
Chris@1532 44 return level > other.level;
Chris@1532 45 } else {
Chris@1532 46 return false;
Chris@1532 47 }
Chris@1532 48 }
Chris@1532 49 }
Chris@1532 50
Chris@1532 51 bool operator==(const ZoomLevel &other) const {
Chris@1532 52 return (zone == other.zone && level == other.level);
Chris@1532 53 }
Chris@1532 54
Chris@1532 55 ZoomLevel incremented() const {
Chris@1532 56 if (zone == FramesPerPixel) {
Chris@1532 57 return { zone, level + 1 };
Chris@1532 58 } else if (level == 1) {
Chris@1532 59 return { FramesPerPixel, 2 };
Chris@1532 60 } else if (level == 2) {
Chris@1532 61 return { FramesPerPixel, 1 };
Chris@1532 62 } else {
Chris@1532 63 return { zone, level - 1 };
Chris@1532 64 }
Chris@1532 65 }
Chris@1532 66
Chris@1532 67 ZoomLevel decremented() const {
Chris@1532 68 if (zone == PixelsPerFrame) {
Chris@1532 69 return { zone, level + 1 };
Chris@1532 70 } else if (level == 1) {
Chris@1532 71 return { PixelsPerFrame, 2 };
Chris@1532 72 } else {
Chris@1532 73 return { zone, level - 1 };
Chris@1532 74 }
Chris@1532 75 }
Chris@1532 76
Chris@1532 77 /// Inexact
Chris@1532 78 double framesToPixels(double frames) const {
Chris@1532 79 if (zone == PixelsPerFrame) {
Chris@1532 80 return frames * level;
Chris@1532 81 } else {
Chris@1532 82 return frames / level;
Chris@1532 83 }
Chris@1532 84 }
Chris@1532 85
Chris@1532 86 /// Inexact
Chris@1532 87 double pixelsToFrames(double pixels) const {
Chris@1532 88 if (zone == PixelsPerFrame) {
Chris@1532 89 return pixels / level;
Chris@1532 90 } else {
Chris@1532 91 return pixels * level;
Chris@1532 92 }
Chris@1532 93 }
Chris@1532 94 };
Chris@1532 95
Chris@1532 96 std::ostream &operator<<(std::ostream &s, const ZoomLevel &z);
Chris@1532 97
Chris@1532 98 #endif