Mercurial > hg > svgui
changeset 1070:27163db978d8 spectrogram-minor-refactor
Use a params structure
author | Chris Cannam |
---|---|
date | Thu, 23 Jun 2016 14:51:10 +0100 |
parents | 0c4734cd33c1 |
children | 65b183494331 |
files | layer/ColourScale.cpp layer/ColourScale.h |
diffstat | 2 files changed, 50 insertions(+), 47 deletions(-) [+] |
line wrap: on
line diff
--- a/layer/ColourScale.cpp Thu Jun 23 14:43:14 2016 +0100 +++ b/layer/ColourScale.cpp Thu Jun 23 14:51:10 2016 +0100 @@ -22,36 +22,27 @@ int ColourScale::m_maxPixel = 255; -ColourScale::ColourScale(int colourMap, - Scale scale, - double minValue, - double maxValue, - double threshold, - double gain) : - m_mapper(colourMap, 1.f, double(m_maxPixel)), - m_scale(scale), - m_min(minValue), - m_max(maxValue), - m_threshold(threshold), - m_gain(gain) +ColourScale::ColourScale(Parameters parameters) : + m_params(parameters), + m_mapper(m_params.colourMap, 1.f, double(m_maxPixel)) { - if (minValue >= maxValue) { + if (m_params.minValue >= m_params.maxValue) { throw std::logic_error("maxValue must be greater than minValue"); } - m_mappedMin = m_min; - m_mappedMax = m_max; + m_mappedMin = m_params.minValue; + m_mappedMax = m_params.maxValue; - if (m_scale == LogColourScale) { + if (m_params.scale == LogColourScale) { LogRange::mapRange(m_mappedMin, m_mappedMax); - } else if (m_scale == PlusMinusOneScale) { + } else if (m_params.scale == PlusMinusOneScale) { m_mappedMin = -1.0; m_mappedMax = 1.0; - } else if (m_scale == AbsoluteScale) { + } else if (m_params.scale == AbsoluteScale) { m_mappedMin = fabs(m_mappedMin); m_mappedMax = fabs(m_mappedMax); @@ -70,23 +61,23 @@ { double maxPixF = m_maxPixel; - if (m_scale == PhaseColourScale) { + if (m_params.scale == PhaseColourScale) { double half = (maxPixF - 1.f) / 2.f; return 1 + int((value * half) / M_PI + half); } - value *= m_gain; + value *= m_params.gain; - if (value < m_threshold) return 0; + if (value < m_params.threshold) return 0; double mapped = value; - if (m_scale == LogColourScale) { + if (m_params.scale == LogColourScale) { mapped = LogRange::map(value); - } else if (m_scale == PlusMinusOneScale) { + } else if (m_params.scale == PlusMinusOneScale) { if (mapped < -1.f) mapped = -1.f; if (mapped > 1.f) mapped = 1.f; - } else if (m_scale == AbsoluteScale) { + } else if (m_params.scale == AbsoluteScale) { if (mapped < 0.f) mapped = -mapped; } @@ -101,14 +92,18 @@ int pixel = 0; - if (m_scale == MeterColourScale) { + if (m_params.scale == MeterColourScale) { pixel = AudioLevel::multiplier_to_preview(proportion, m_maxPixel-1) + 1; } else { pixel = int(proportion * maxPixF) + 1; } - if (pixel > m_maxPixel) pixel = m_maxPixel; - if (pixel < 0) pixel = 0; + if (pixel < 0) { + pixel = 0; + } + if (pixel > m_maxPixel) { + pixel = m_maxPixel; + } return pixel; }
--- a/layer/ColourScale.h Thu Jun 23 14:43:14 2016 +0100 +++ b/layer/ColourScale.h Thu Jun 23 14:51:10 2016 +0100 @@ -34,28 +34,40 @@ AbsoluteScale }; + struct Parameters { + Parameters() : colourMap(0), scale(LinearColourScale), + minValue(0.0), maxValue(1.0), + threshold(0.0), gain(1.0) { } + + /** A colour map index as used by ColourMapper */ + int colourMap; + + /** Distribution for the scale */ + Scale scale; + + /** Minimum value in source range */ + double minValue; + + /** Maximum value in source range. Must be > minValue */ + double maxValue; + + /** Threshold below which every value is mapped to background + pixel 0 */ + double threshold; + + /** Gain to apply before clamping and mapping */ + double gain; + }; + /** - * Create a ColourScale with the given parameters: - * - * @param colourMap A colour map index as used by ColourMapper - * @param scale Distribution for the scale - * @param minValue Minimum value in range - * @param maxValue Maximum value in range. Must be > minValue - * @param threshold Threshold below which every value is mapped to - * background pixel 0 - * @param gain Gain to apply before clamping and mapping, typically 1 + * Create a ColourScale with the given parameters. * * Note that some parameters may be ignored for some scale * distribution settings. For example, min and max are ignored for * PlusMinusOneScale and PhaseColourScale and threshold and gain * are ignored for PhaseColourScale. */ - ColourScale(int colourMap, - Scale scale, - double minValue, - double maxValue, - double threshold, - double gain); + ColourScale(Parameters parameters); /** * Return a pixel number (in the range 0-255 inclusive) @@ -82,14 +94,10 @@ } private: + Parameters m_params; ColourMapper m_mapper; - Scale m_scale; - double m_min; - double m_max; double m_mappedMin; double m_mappedMax; - double m_threshold; - double m_gain; static int m_maxPixel; };