annotate layer/ColourScale.cpp @ 1070:27163db978d8 spectrogram-minor-refactor

Use a params structure
author Chris Cannam
date Thu, 23 Jun 2016 14:51:10 +0100
parents 521f7e8b0559
children 65b183494331
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@1068 59 int
Chris@1068 60 ColourScale::getPixel(double value)
Chris@1068 61 {
Chris@1068 62 double maxPixF = m_maxPixel;
Chris@1068 63
Chris@1070 64 if (m_params.scale == PhaseColourScale) {
Chris@1068 65 double half = (maxPixF - 1.f) / 2.f;
Chris@1068 66 return 1 + int((value * half) / M_PI + half);
Chris@1068 67 }
Chris@1068 68
Chris@1070 69 value *= m_params.gain;
Chris@1068 70
Chris@1070 71 if (value < m_params.threshold) return 0;
Chris@1068 72
Chris@1068 73 double mapped = value;
Chris@1068 74
Chris@1070 75 if (m_params.scale == LogColourScale) {
Chris@1068 76 mapped = LogRange::map(value);
Chris@1070 77 } else if (m_params.scale == PlusMinusOneScale) {
Chris@1068 78 if (mapped < -1.f) mapped = -1.f;
Chris@1068 79 if (mapped > 1.f) mapped = 1.f;
Chris@1070 80 } else if (m_params.scale == AbsoluteScale) {
Chris@1068 81 if (mapped < 0.f) mapped = -mapped;
Chris@1068 82 }
Chris@1068 83
Chris@1068 84 if (mapped < m_mappedMin) {
Chris@1068 85 mapped = m_mappedMin;
Chris@1068 86 }
Chris@1068 87 if (mapped > m_mappedMax) {
Chris@1068 88 mapped = m_mappedMax;
Chris@1068 89 }
Chris@1068 90
Chris@1068 91 double proportion = (mapped - m_mappedMin) / (m_mappedMax - m_mappedMin);
Chris@1068 92
Chris@1068 93 int pixel = 0;
Chris@1068 94
Chris@1070 95 if (m_params.scale == MeterColourScale) {
Chris@1068 96 pixel = AudioLevel::multiplier_to_preview(proportion, m_maxPixel-1) + 1;
Chris@1068 97 } else {
Chris@1068 98 pixel = int(proportion * maxPixF) + 1;
Chris@1068 99 }
Chris@1068 100
Chris@1070 101 if (pixel < 0) {
Chris@1070 102 pixel = 0;
Chris@1070 103 }
Chris@1070 104 if (pixel > m_maxPixel) {
Chris@1070 105 pixel = m_maxPixel;
Chris@1070 106 }
Chris@1068 107 return pixel;
Chris@1068 108 }
Chris@1068 109
Chris@1068 110 QColor
Chris@1068 111 ColourScale::getColourForPixel(int pixel, int rotation)
Chris@1068 112 {
Chris@1068 113 if (pixel < 0) {
Chris@1068 114 pixel = 0;
Chris@1068 115 }
Chris@1068 116 if (pixel > m_maxPixel) {
Chris@1068 117 pixel = m_maxPixel;
Chris@1068 118 }
Chris@1068 119 if (pixel == 0) {
Chris@1068 120 if (m_mapper.hasLightBackground()) {
Chris@1068 121 return Qt::white;
Chris@1068 122 } else {
Chris@1068 123 return Qt::black;
Chris@1068 124 }
Chris@1068 125 } else {
Chris@1068 126 int target = int(pixel) + rotation;
Chris@1068 127 while (target < 1) target += m_maxPixel;
Chris@1068 128 while (target > m_maxPixel) target -= m_maxPixel;
Chris@1068 129 return m_mapper.map(double(target));
Chris@1068 130 }
Chris@1068 131 }