annotate layer/ColourScale.cpp @ 1102:36a981a0fa31 spectrogram-minor-refactor

Delete renderers
author Chris Cannam
date Wed, 13 Jul 2016 13:44:11 +0100
parents 7ebfb61b1701
children ea5ae9dd10ba
rev   line source
Chris@1068 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1068 2
Chris@1068 3 /*
Chris@1068 4 Sonic Visualiser
Chris@1068 5 An audio file viewer and annotation editor.
Chris@1068 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1068 7 This file copyright 2006-2016 Chris Cannam and QMUL.
Chris@1068 8
Chris@1068 9 This program is free software; you can redistribute it and/or
Chris@1068 10 modify it under the terms of the GNU General Public License as
Chris@1068 11 published by the Free Software Foundation; either version 2 of the
Chris@1068 12 License, or (at your option) any later version. See the file
Chris@1068 13 COPYING included with this distribution for more information.
Chris@1068 14 */
Chris@1068 15
Chris@1068 16 #include "ColourScale.h"
Chris@1068 17
Chris@1068 18 #include "base/AudioLevel.h"
Chris@1068 19 #include "base/LogRange.h"
Chris@1068 20
Chris@1068 21 #include <cmath>
Chris@1068 22
Chris@1068 23 int ColourScale::m_maxPixel = 255;
Chris@1068 24
Chris@1070 25 ColourScale::ColourScale(Parameters parameters) :
Chris@1070 26 m_params(parameters),
Chris@1070 27 m_mapper(m_params.colourMap, 1.f, double(m_maxPixel))
Chris@1068 28 {
Chris@1070 29 if (m_params.minValue >= m_params.maxValue) {
Chris@1068 30 throw std::logic_error("maxValue must be greater than minValue");
Chris@1068 31 }
Chris@1068 32
Chris@1070 33 m_mappedMin = m_params.minValue;
Chris@1070 34 m_mappedMax = m_params.maxValue;
Chris@1068 35
Chris@1070 36 if (m_params.scale == LogColourScale) {
Chris@1068 37
Chris@1068 38 LogRange::mapRange(m_mappedMin, m_mappedMax);
Chris@1068 39
Chris@1070 40 } else if (m_params.scale == PlusMinusOneScale) {
Chris@1068 41
Chris@1068 42 m_mappedMin = -1.0;
Chris@1068 43 m_mappedMax = 1.0;
Chris@1068 44
Chris@1070 45 } else if (m_params.scale == AbsoluteScale) {
Chris@1068 46
Chris@1068 47 m_mappedMin = fabs(m_mappedMin);
Chris@1068 48 m_mappedMax = fabs(m_mappedMax);
Chris@1068 49 if (m_mappedMin >= m_mappedMax) {
Chris@1068 50 std::swap(m_mappedMin, m_mappedMax);
Chris@1068 51 }
Chris@1068 52 }
Chris@1068 53
Chris@1068 54 if (m_mappedMin >= m_mappedMax) {
Chris@1068 55 throw std::logic_error("maxValue must be greater than minValue [after mapping]");
Chris@1068 56 }
Chris@1068 57 }
Chris@1068 58
Chris@1071 59 ColourScale::~ColourScale()
Chris@1071 60 {
Chris@1071 61 }
Chris@1071 62
Chris@1079 63 ColourScale::Scale
Chris@1079 64 ColourScale::getScale() const
Chris@1079 65 {
Chris@1079 66 return m_params.scale;
Chris@1079 67 }
Chris@1079 68
Chris@1068 69 int
Chris@1079 70 ColourScale::getPixel(double value) const
Chris@1068 71 {
Chris@1068 72 double maxPixF = m_maxPixel;
Chris@1068 73
Chris@1070 74 if (m_params.scale == PhaseColourScale) {
Chris@1068 75 double half = (maxPixF - 1.f) / 2.f;
Chris@1068 76 return 1 + int((value * half) / M_PI + half);
Chris@1068 77 }
Chris@1068 78
Chris@1070 79 value *= m_params.gain;
Chris@1068 80
Chris@1070 81 if (value < m_params.threshold) return 0;
Chris@1068 82
Chris@1068 83 double mapped = value;
Chris@1068 84
Chris@1070 85 if (m_params.scale == LogColourScale) {
Chris@1068 86 mapped = LogRange::map(value);
Chris@1070 87 } else if (m_params.scale == PlusMinusOneScale) {
Chris@1068 88 if (mapped < -1.f) mapped = -1.f;
Chris@1068 89 if (mapped > 1.f) mapped = 1.f;
Chris@1070 90 } else if (m_params.scale == AbsoluteScale) {
Chris@1068 91 if (mapped < 0.f) mapped = -mapped;
Chris@1068 92 }
Chris@1068 93
Chris@1068 94 if (mapped < m_mappedMin) {
Chris@1068 95 mapped = m_mappedMin;
Chris@1068 96 }
Chris@1068 97 if (mapped > m_mappedMax) {
Chris@1068 98 mapped = m_mappedMax;
Chris@1068 99 }
Chris@1068 100
Chris@1068 101 double proportion = (mapped - m_mappedMin) / (m_mappedMax - m_mappedMin);
Chris@1068 102
Chris@1068 103 int pixel = 0;
Chris@1068 104
Chris@1070 105 if (m_params.scale == MeterColourScale) {
Chris@1068 106 pixel = AudioLevel::multiplier_to_preview(proportion, m_maxPixel-1) + 1;
Chris@1068 107 } else {
Chris@1068 108 pixel = int(proportion * maxPixF) + 1;
Chris@1068 109 }
Chris@1068 110
Chris@1070 111 if (pixel < 0) {
Chris@1070 112 pixel = 0;
Chris@1070 113 }
Chris@1070 114 if (pixel > m_maxPixel) {
Chris@1070 115 pixel = m_maxPixel;
Chris@1070 116 }
Chris@1068 117 return pixel;
Chris@1068 118 }
Chris@1068 119
Chris@1068 120 QColor
Chris@1079 121 ColourScale::getColourForPixel(int pixel, int rotation) const
Chris@1068 122 {
Chris@1068 123 if (pixel < 0) {
Chris@1068 124 pixel = 0;
Chris@1068 125 }
Chris@1068 126 if (pixel > m_maxPixel) {
Chris@1068 127 pixel = m_maxPixel;
Chris@1068 128 }
Chris@1068 129 if (pixel == 0) {
Chris@1068 130 if (m_mapper.hasLightBackground()) {
Chris@1068 131 return Qt::white;
Chris@1068 132 } else {
Chris@1068 133 return Qt::black;
Chris@1068 134 }
Chris@1068 135 } else {
Chris@1068 136 int target = int(pixel) + rotation;
Chris@1068 137 while (target < 1) target += m_maxPixel;
Chris@1068 138 while (target > m_maxPixel) target -= m_maxPixel;
Chris@1068 139 return m_mapper.map(double(target));
Chris@1068 140 }
Chris@1068 141 }