Chris@1071
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@1071
|
2
|
Chris@1071
|
3 /*
|
Chris@1071
|
4 Sonic Visualiser
|
Chris@1071
|
5 An audio file viewer and annotation editor.
|
Chris@1071
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@1071
|
7 This file copyright 2006-2016 Chris Cannam and QMUL.
|
Chris@1071
|
8
|
Chris@1071
|
9 This program is free software; you can redistribute it and/or
|
Chris@1071
|
10 modify it under the terms of the GNU General Public License as
|
Chris@1071
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@1071
|
12 License, or (at your option) any later version. See the file
|
Chris@1071
|
13 COPYING included with this distribution for more information.
|
Chris@1071
|
14 */
|
Chris@1071
|
15
|
Chris@1071
|
16 #include "Colour3DPlotRenderer.h"
|
Chris@1074
|
17 #include "RenderTimer.h"
|
Chris@1071
|
18
|
Chris@1117
|
19 #include "base/Profiler.h"
|
Chris@1117
|
20
|
Chris@1075
|
21 #include "data/model/DenseThreeDimensionalModel.h"
|
Chris@1075
|
22 #include "data/model/Dense3DModelPeakCache.h"
|
Chris@1075
|
23 #include "data/model/FFTModel.h"
|
Chris@1075
|
24
|
Chris@1077
|
25 #include "LayerGeometryProvider.h"
|
Chris@1082
|
26 #include "VerticalBinLayer.h"
|
Chris@1109
|
27 #include "PaintAssistant.h"
|
Chris@1109
|
28
|
Chris@1109
|
29 #include "view/ViewManager.h" // for main model sample rate. Pity
|
Chris@1075
|
30
|
Chris@1079
|
31 #include <vector>
|
Chris@1079
|
32
|
Chris@1120
|
33 #define DEBUG_SPECTROGRAM_REPAINT 1 //!!! name
|
Chris@1094
|
34
|
Chris@1079
|
35 using namespace std;
|
Chris@1079
|
36
|
Chris@1073
|
37 Colour3DPlotRenderer::RenderResult
|
Chris@1113
|
38 Colour3DPlotRenderer::render(const LayerGeometryProvider *v, QPainter &paint, QRect rect)
|
Chris@1076
|
39 {
|
Chris@1090
|
40 return render(v, paint, rect, false);
|
Chris@1076
|
41 }
|
Chris@1076
|
42
|
Chris@1076
|
43 Colour3DPlotRenderer::RenderResult
|
Chris@1113
|
44 Colour3DPlotRenderer::renderTimeConstrained(const LayerGeometryProvider *v,
|
Chris@1090
|
45 QPainter &paint, QRect rect)
|
Chris@1076
|
46 {
|
Chris@1090
|
47 return render(v, paint, rect, true);
|
Chris@1076
|
48 }
|
Chris@1076
|
49
|
Chris@1096
|
50 QRect
|
Chris@1121
|
51 Colour3DPlotRenderer::getLargestUncachedRect(const LayerGeometryProvider *v)
|
Chris@1096
|
52 {
|
Chris@1121
|
53 RenderType renderType = decideRenderType(v);
|
Chris@1121
|
54
|
Chris@1121
|
55 if (renderType == DirectTranslucent) {
|
Chris@1121
|
56 return QRect(); // never cached
|
Chris@1121
|
57 }
|
Chris@1121
|
58
|
Chris@1096
|
59 int h = m_cache.getSize().height();
|
Chris@1096
|
60
|
Chris@1096
|
61 QRect areaLeft(0, 0, m_cache.getValidLeft(), h);
|
Chris@1096
|
62 QRect areaRight(m_cache.getValidRight(), 0,
|
Chris@1096
|
63 m_cache.getSize().width() - m_cache.getValidRight(), h);
|
Chris@1096
|
64
|
Chris@1096
|
65 if (areaRight.width() > areaLeft.width()) {
|
Chris@1096
|
66 return areaRight;
|
Chris@1096
|
67 } else {
|
Chris@1096
|
68 return areaLeft;
|
Chris@1096
|
69 }
|
Chris@1096
|
70 }
|
Chris@1096
|
71
|
Chris@1122
|
72 bool
|
Chris@1122
|
73 Colour3DPlotRenderer::geometryChanged(const LayerGeometryProvider *v)
|
Chris@1122
|
74 {
|
Chris@1122
|
75 RenderType renderType = decideRenderType(v);
|
Chris@1122
|
76
|
Chris@1122
|
77 if (renderType == DirectTranslucent) {
|
Chris@1122
|
78 return true; // never cached
|
Chris@1122
|
79 }
|
Chris@1122
|
80
|
Chris@1122
|
81 if (m_cache.getSize() == v->getPaintSize() &&
|
Chris@1122
|
82 m_cache.getZoomLevel() == v->getZoomLevel() &&
|
Chris@1122
|
83 m_cache.getStartFrame() == v->getStartFrame()) {
|
Chris@1122
|
84 return false;
|
Chris@1122
|
85 } else {
|
Chris@1122
|
86 return true;
|
Chris@1122
|
87 }
|
Chris@1122
|
88 }
|
Chris@1122
|
89
|
Chris@1076
|
90 Colour3DPlotRenderer::RenderResult
|
Chris@1113
|
91 Colour3DPlotRenderer::render(const LayerGeometryProvider *v,
|
Chris@1090
|
92 QPainter &paint, QRect rect, bool timeConstrained)
|
Chris@1073
|
93 {
|
Chris@1109
|
94 RenderType renderType = decideRenderType(v);
|
Chris@1109
|
95
|
Chris@1109
|
96 if (renderType != DrawBufferPixelResolution) {
|
Chris@1109
|
97 // Rendering should be fast in bin-resolution and direct draw
|
Chris@1109
|
98 // cases because we are quite well zoomed-in, and the sums are
|
Chris@1109
|
99 // easier this way. Calculating boundaries later will be
|
Chris@1109
|
100 // fiddly for partial paints otherwise.
|
Chris@1109
|
101 timeConstrained = false;
|
Chris@1109
|
102 }
|
Chris@1109
|
103
|
Chris@1079
|
104 int x0 = v->getXForViewX(rect.x());
|
Chris@1079
|
105 int x1 = v->getXForViewX(rect.x() + rect.width());
|
Chris@1079
|
106 if (x0 < 0) x0 = 0;
|
Chris@1079
|
107 if (x1 > v->getPaintWidth()) x1 = v->getPaintWidth();
|
Chris@1079
|
108
|
Chris@1120
|
109 sv_frame_t startFrame = v->getStartFrame();
|
Chris@1120
|
110
|
Chris@1079
|
111 m_cache.resize(v->getPaintSize());
|
Chris@1079
|
112 m_cache.setZoomLevel(v->getZoomLevel());
|
Chris@1079
|
113
|
Chris@1119
|
114 m_magCache.resize(v->getPaintSize().width());
|
Chris@1119
|
115 m_magCache.setZoomLevel(v->getZoomLevel());
|
Chris@1119
|
116
|
Chris@1120
|
117 if (renderType == DirectTranslucent) {
|
Chris@1121
|
118 MagnitudeRange range = renderDirectTranslucent(v, paint, rect);
|
Chris@1121
|
119 return { rect, range };
|
Chris@1120
|
120 }
|
Chris@1120
|
121
|
Chris@1090
|
122 cerr << "cache start " << m_cache.getStartFrame()
|
Chris@1090
|
123 << " valid left " << m_cache.getValidLeft()
|
Chris@1090
|
124 << " valid right " << m_cache.getValidRight()
|
Chris@1094
|
125 << endl;
|
Chris@1094
|
126 cerr << " view start " << startFrame
|
Chris@1090
|
127 << " x0 " << x0
|
Chris@1090
|
128 << " x1 " << x1
|
Chris@1090
|
129 << endl;
|
Chris@1090
|
130
|
Chris@1079
|
131 if (m_cache.isValid()) { // some part of the cache is valid
|
Chris@1079
|
132
|
Chris@1079
|
133 if (v->getXForFrame(m_cache.getStartFrame()) ==
|
Chris@1079
|
134 v->getXForFrame(startFrame) &&
|
Chris@1079
|
135 m_cache.getValidLeft() <= x0 &&
|
Chris@1079
|
136 m_cache.getValidRight() >= x1) {
|
Chris@1090
|
137
|
Chris@1090
|
138 cerr << "cache hit" << endl;
|
Chris@1090
|
139
|
Chris@1079
|
140 // cache is valid for the complete requested area
|
Chris@1079
|
141 paint.drawImage(rect, m_cache.getImage(), rect);
|
Chris@1119
|
142
|
Chris@1119
|
143 //!!! a dev debug check
|
Chris@1119
|
144 if (!m_magCache.areColumnsSet(x0, x1 - x0)) {
|
Chris@1121
|
145 cerr << "NB Columns (" << x0 << " -> " << x1-x0
|
Chris@1119
|
146 << ") not set in mag cache" << endl;
|
Chris@1121
|
147 // throw std::logic_error("Columns not set in mag cache");
|
Chris@1119
|
148 }
|
Chris@1119
|
149
|
Chris@1122
|
150 MagnitudeRange range = m_magCache.getRange(x0, x1 - x0);
|
Chris@1119
|
151
|
Chris@1119
|
152 return { rect, range };
|
Chris@1079
|
153
|
Chris@1079
|
154 } else {
|
Chris@1090
|
155 cerr << "cache partial hit" << endl;
|
Chris@1090
|
156
|
Chris@1079
|
157 // cache doesn't begin at the right frame or doesn't
|
Chris@1079
|
158 // contain the complete view, but might be scrollable or
|
Chris@1079
|
159 // partially usable
|
Chris@1090
|
160 m_cache.scrollTo(v, startFrame);
|
Chris@1119
|
161 m_magCache.scrollTo(v, startFrame);
|
Chris@1079
|
162
|
Chris@1079
|
163 // if we are not time-constrained, then we want to paint
|
Chris@1081
|
164 // the whole area in one go; we don't return a partial
|
Chris@1081
|
165 // paint. To avoid providing the more complex logic to
|
Chris@1081
|
166 // handle painting discontiguous areas, if the only valid
|
Chris@1079
|
167 // part of cache is in the middle, just make the whole
|
Chris@1079
|
168 // thing invalid and start again.
|
Chris@1079
|
169 if (!timeConstrained) {
|
Chris@1079
|
170 if (m_cache.getValidLeft() > x0 &&
|
Chris@1079
|
171 m_cache.getValidRight() < x1) {
|
Chris@1079
|
172 m_cache.invalidate();
|
Chris@1079
|
173 }
|
Chris@1079
|
174 }
|
Chris@1079
|
175 }
|
Chris@1090
|
176 } else {
|
Chris@1118
|
177 // cache is completely invalid
|
Chris@1090
|
178 m_cache.setStartFrame(startFrame);
|
Chris@1119
|
179 m_magCache.setStartFrame(startFrame);
|
Chris@1075
|
180 }
|
Chris@1075
|
181
|
Chris@1079
|
182 bool rightToLeft = false;
|
Chris@1079
|
183
|
Chris@1122
|
184 int reqx0 = x0;
|
Chris@1122
|
185 int reqx1 = x1;
|
Chris@1122
|
186
|
Chris@1079
|
187 if (!m_cache.isValid() && timeConstrained) {
|
Chris@1081
|
188 // When rendering the whole area, in a context where we might
|
Chris@1081
|
189 // not be able to complete the work, start from somewhere near
|
Chris@1081
|
190 // the middle so that the region of interest appears first
|
Chris@1079
|
191
|
Chris@1079
|
192 //!!! (perhaps we should avoid doing this if past repaints
|
Chris@1079
|
193 //!!! have been fast enough to do the whole in one shot)
|
Chris@1079
|
194 if (x0 == 0 && x1 == v->getPaintWidth()) {
|
Chris@1079
|
195 x0 = int(x1 * 0.3);
|
Chris@1079
|
196 }
|
Chris@1079
|
197 }
|
Chris@1079
|
198
|
Chris@1079
|
199 if (m_cache.isValid()) {
|
Chris@1090
|
200
|
Chris@1079
|
201 // When rendering only a part of the cache, we need to make
|
Chris@1079
|
202 // sure that the part we're rendering is adjacent to (or
|
Chris@1079
|
203 // overlapping) a valid area of cache, if we have one. The
|
Chris@1079
|
204 // alternative is to ditch the valid area of cache and render
|
Chris@1079
|
205 // only the requested area, but that's risky because this can
|
Chris@1079
|
206 // happen when just waving the pointer over a small part of
|
Chris@1079
|
207 // the view -- if we lose the partly-built cache every time
|
Chris@1079
|
208 // the user does that, we'll never finish building it.
|
Chris@1079
|
209 int left = x0;
|
Chris@1079
|
210 int width = x1 - x0;
|
Chris@1079
|
211 bool isLeftOfValidArea = false;
|
Chris@1079
|
212 m_cache.adjustToTouchValidArea(left, width, isLeftOfValidArea);
|
Chris@1079
|
213 x0 = left;
|
Chris@1079
|
214 x1 = x0 + width;
|
Chris@1079
|
215
|
Chris@1079
|
216 // That call also told us whether we should be painting
|
Chris@1079
|
217 // sub-regions of our target region in right-to-left order in
|
Chris@1079
|
218 // order to ensure contiguity
|
Chris@1079
|
219 rightToLeft = isLeftOfValidArea;
|
Chris@1079
|
220 }
|
Chris@1075
|
221
|
Chris@1109
|
222 // Note, we always paint the full height to cache. We want to
|
Chris@1109
|
223 // ensure the cache is coherent without having to worry about
|
Chris@1109
|
224 // vertical matching of required and valid areas as well as
|
Chris@1109
|
225 // horizontal.
|
Chris@1094
|
226
|
Chris@1109
|
227 if (renderType == DrawBufferBinResolution) {
|
Chris@1109
|
228
|
Chris@1094
|
229 renderToCacheBinResolution(v, x0, x1 - x0);
|
Chris@1109
|
230
|
Chris@1109
|
231 } else { // must be DrawBufferPixelResolution, handled DirectTranslucent earlier
|
Chris@1109
|
232
|
Chris@1094
|
233 renderToCachePixelResolution(v, x0, x1 - x0, rightToLeft, timeConstrained);
|
Chris@1094
|
234 }
|
Chris@1079
|
235
|
Chris@1079
|
236 QRect pr = rect & m_cache.getValidArea();
|
Chris@1079
|
237 paint.drawImage(pr.x(), pr.y(), m_cache.getImage(),
|
Chris@1079
|
238 pr.x(), pr.y(), pr.width(), pr.height());
|
Chris@1079
|
239
|
Chris@1079
|
240 if (!timeConstrained && (pr != rect)) {
|
Chris@1079
|
241 //!!! on a first cut, there is a risk that this will happen
|
Chris@1079
|
242 //!!! when we are at start/end of model -- trap, report, and
|
Chris@1079
|
243 //!!! then fix
|
Chris@1079
|
244 throw std::logic_error("internal error: failed to render entire requested rect even when not time-constrained");
|
Chris@1079
|
245 }
|
Chris@1120
|
246
|
Chris@1122
|
247 MagnitudeRange range = m_magCache.getRange(reqx0, reqx1 - reqx0);
|
Chris@1120
|
248
|
Chris@1120
|
249 return { pr, range };
|
Chris@1079
|
250
|
Chris@1073
|
251 //!!! todo: timing/incomplete paint
|
Chris@1073
|
252
|
Chris@1073
|
253 //!!! todo: peak frequency style
|
Chris@1073
|
254
|
Chris@1073
|
255 //!!! todo: transparent style from Colour3DPlot
|
Chris@1074
|
256
|
Chris@1079
|
257 //!!! todo: view magnitudes / normalise visible area
|
Chris@1079
|
258
|
Chris@1079
|
259 //!!! todo: alter documentation for view mag stuff (cached paints
|
Chris@1079
|
260 //!!! do not update MagnitudeRange)
|
Chris@1079
|
261
|
Chris@1079
|
262 //!!! todo, here or in caller: illuminateLocalFeatures
|
Chris@1079
|
263
|
Chris@1110
|
264 //!!! todo: colourmap rotation
|
Chris@1110
|
265
|
Chris@1079
|
266 //!!! fft model scaling?
|
Chris@1079
|
267
|
Chris@1079
|
268 //!!! should we own the Dense3DModelPeakCache here? or should it persist
|
Chris@1073
|
269 }
|
Chris@1073
|
270
|
Chris@1109
|
271 Colour3DPlotRenderer::RenderType
|
Chris@1113
|
272 Colour3DPlotRenderer::decideRenderType(const LayerGeometryProvider *v) const
|
Chris@1094
|
273 {
|
Chris@1100
|
274 const DenseThreeDimensionalModel *model = m_sources.source;
|
Chris@1109
|
275 if (!model || !v || !(v->getViewManager())) {
|
Chris@1109
|
276 return DrawBufferPixelResolution; // or anything
|
Chris@1109
|
277 }
|
Chris@1109
|
278
|
Chris@1094
|
279 int binResolution = model->getResolution();
|
Chris@1094
|
280 int zoomLevel = v->getZoomLevel();
|
Chris@1109
|
281 sv_samplerate_t modelRate = model->getSampleRate();
|
Chris@1109
|
282
|
Chris@1109
|
283 double rateRatio = v->getViewManager()->getMainModelSampleRate() / modelRate;
|
Chris@1109
|
284 double relativeBinResolution = binResolution * rateRatio;
|
Chris@1109
|
285
|
Chris@1109
|
286 if (m_params.binDisplay == BinDisplay::PeakFrequencies) {
|
Chris@1109
|
287 // no alternative works here
|
Chris@1109
|
288 return DrawBufferPixelResolution;
|
Chris@1109
|
289 }
|
Chris@1109
|
290
|
Chris@1109
|
291 if (!m_params.alwaysOpaque && !m_params.interpolate) {
|
Chris@1109
|
292
|
Chris@1109
|
293 // consider translucent option -- only if not smoothing & not
|
Chris@1109
|
294 // explicitly requested opaque & sufficiently zoomed-in
|
Chris@1109
|
295
|
Chris@1117
|
296 if (model->getHeight() * 3 < v->getPaintHeight() &&
|
Chris@1117
|
297 relativeBinResolution >= 3 * zoomLevel) {
|
Chris@1109
|
298 return DirectTranslucent;
|
Chris@1109
|
299 }
|
Chris@1109
|
300 }
|
Chris@1109
|
301
|
Chris@1109
|
302 if (relativeBinResolution > zoomLevel) {
|
Chris@1109
|
303 return DrawBufferBinResolution;
|
Chris@1109
|
304 } else {
|
Chris@1109
|
305 return DrawBufferPixelResolution;
|
Chris@1109
|
306 }
|
Chris@1109
|
307 }
|
Chris@1109
|
308
|
Chris@1121
|
309 MagnitudeRange
|
Chris@1113
|
310 Colour3DPlotRenderer::renderDirectTranslucent(const LayerGeometryProvider *v,
|
Chris@1109
|
311 QPainter &paint,
|
Chris@1109
|
312 QRect rect)
|
Chris@1109
|
313 {
|
Chris@1117
|
314 Profiler profiler("Colour3DPlotRenderer::renderDirectTranslucent");
|
Chris@1117
|
315
|
Chris@1121
|
316 MagnitudeRange magRange;
|
Chris@1121
|
317
|
Chris@1115
|
318 QPoint illuminatePos;
|
Chris@1115
|
319 bool illuminate = v->shouldIlluminateLocalFeatures
|
Chris@1115
|
320 (m_sources.verticalBinLayer, illuminatePos);
|
Chris@1109
|
321
|
Chris@1109
|
322 const DenseThreeDimensionalModel *model = m_sources.source;
|
Chris@1109
|
323
|
Chris@1109
|
324 int x0 = rect.left();
|
Chris@1109
|
325 int x1 = rect.right() + 1;
|
Chris@1109
|
326
|
Chris@1109
|
327 int h = v->getPaintHeight();
|
Chris@1109
|
328
|
Chris@1109
|
329 sv_frame_t modelStart = model->getStartFrame();
|
Chris@1109
|
330 sv_frame_t modelEnd = model->getEndFrame();
|
Chris@1109
|
331 int modelResolution = model->getResolution();
|
Chris@1109
|
332
|
Chris@1109
|
333 double rateRatio =
|
Chris@1109
|
334 v->getViewManager()->getMainModelSampleRate() / model->getSampleRate();
|
Chris@1109
|
335
|
Chris@1109
|
336 // the s-prefix values are source, i.e. model, column and bin numbers
|
Chris@1109
|
337 int sx0 = int((double(v->getFrameForX(x0)) / rateRatio - double(modelStart))
|
Chris@1109
|
338 / modelResolution);
|
Chris@1109
|
339 int sx1 = int((double(v->getFrameForX(x1)) / rateRatio - double(modelStart))
|
Chris@1109
|
340 / modelResolution);
|
Chris@1109
|
341
|
Chris@1109
|
342 int sh = model->getHeight();
|
Chris@1109
|
343
|
Chris@1109
|
344 const int buflen = 40;
|
Chris@1109
|
345 char labelbuf[buflen];
|
Chris@1109
|
346
|
Chris@1115
|
347 int minbin = 0; //!!!
|
Chris@1109
|
348 int maxbin = sh - 1; //!!!
|
Chris@1109
|
349
|
Chris@1109
|
350 int psx = -1;
|
Chris@1109
|
351
|
Chris@1109
|
352 vector<float> preparedColumn;
|
Chris@1109
|
353
|
Chris@1109
|
354 int modelWidth = model->getWidth();
|
Chris@1109
|
355
|
Chris@1109
|
356 for (int sx = sx0; sx <= sx1; ++sx) {
|
Chris@1109
|
357
|
Chris@1109
|
358 if (sx < 0 || sx >= modelWidth) {
|
Chris@1109
|
359 continue;
|
Chris@1109
|
360 }
|
Chris@1109
|
361
|
Chris@1109
|
362 if (sx != psx) {
|
Chris@1109
|
363
|
Chris@1109
|
364 //!!! this is in common with renderDrawBuffer - pull it out
|
Chris@1109
|
365
|
Chris@1109
|
366 // order:
|
Chris@1109
|
367 // get column -> scale -> record extents ->
|
Chris@1109
|
368 // normalise -> peak pick -> apply display gain ->
|
Chris@1109
|
369 // distribute/interpolate
|
Chris@1109
|
370
|
Chris@1109
|
371 ColumnOp::Column fullColumn = model->getColumn(sx);
|
Chris@1109
|
372
|
Chris@1109
|
373 // cerr << "x " << x << ", sx " << sx << ", col height " << fullColumn.size()
|
Chris@1109
|
374 // << ", minbin " << minbin << ", maxbin " << maxbin << endl;
|
Chris@1109
|
375
|
Chris@1109
|
376 ColumnOp::Column column =
|
Chris@1109
|
377 vector<float>(fullColumn.data() + minbin,
|
Chris@1109
|
378 fullColumn.data() + maxbin + 1);
|
Chris@1109
|
379
|
Chris@1121
|
380 magRange.sample(column);
|
Chris@1121
|
381
|
Chris@1109
|
382 //!!! fft scale if (m_colourScale != ColourScaleType::Phase) {
|
Chris@1109
|
383 // column = ColumnOp::fftScale(column, m_fftSize);
|
Chris@1109
|
384 // }
|
Chris@1109
|
385
|
Chris@1109
|
386 //!!! extents recordColumnExtents(column,
|
Chris@1109
|
387 // sx,
|
Chris@1109
|
388 // overallMag,
|
Chris@1109
|
389 // overallMagChanged);
|
Chris@1109
|
390
|
Chris@1109
|
391 // if (m_colourScale != ColourScaleType::Phase) {
|
Chris@1115
|
392 preparedColumn = ColumnOp::normalize(column, m_params.normalization);
|
Chris@1109
|
393 // }
|
Chris@1109
|
394
|
Chris@1109
|
395 if (m_params.binDisplay == BinDisplay::PeakBins) {
|
Chris@1115
|
396 preparedColumn = ColumnOp::peakPick(preparedColumn);
|
Chris@1109
|
397 }
|
Chris@1109
|
398
|
Chris@1109
|
399 psx = sx;
|
Chris@1109
|
400 }
|
Chris@1109
|
401
|
Chris@1109
|
402 sv_frame_t fx = sx * modelResolution + modelStart;
|
Chris@1109
|
403
|
Chris@1109
|
404 if (fx + modelResolution <= modelStart || fx > modelEnd) continue;
|
Chris@1109
|
405
|
Chris@1109
|
406 int rx0 = v->getXForFrame(int(double(fx) * rateRatio));
|
Chris@1109
|
407 int rx1 = v->getXForFrame(int(double(fx + modelResolution + 1) * rateRatio));
|
Chris@1109
|
408
|
Chris@1109
|
409 int rw = rx1 - rx0;
|
Chris@1109
|
410 if (rw < 1) rw = 1;
|
Chris@1109
|
411
|
Chris@1109
|
412 bool showLabel = (rw > 10 &&
|
Chris@1109
|
413 paint.fontMetrics().width("0.000000") < rw - 3 &&
|
Chris@1109
|
414 paint.fontMetrics().height() < (h / sh));
|
Chris@1109
|
415
|
Chris@1109
|
416 for (int sy = minbin; sy <= maxbin; ++sy) {
|
Chris@1109
|
417
|
Chris@1109
|
418 int ry0 = m_sources.verticalBinLayer->getIYForBin(v, sy);
|
Chris@1109
|
419 int ry1 = m_sources.verticalBinLayer->getIYForBin(v, sy + 1);
|
Chris@1116
|
420
|
Chris@1116
|
421 if (m_params.invertVertical) {
|
Chris@1116
|
422 ry0 = h - ry0 - 1;
|
Chris@1116
|
423 ry1 = h - ry1 - 1;
|
Chris@1116
|
424 }
|
Chris@1116
|
425
|
Chris@1109
|
426 QRect r(rx0, ry1, rw, ry0 - ry1);
|
Chris@1109
|
427
|
Chris@1109
|
428 float value = preparedColumn[sy - minbin];
|
Chris@1112
|
429 QColor colour = m_params.colourScale.getColour(value,
|
Chris@1112
|
430 m_params.colourRotation);
|
Chris@1109
|
431
|
Chris@1109
|
432 if (rw == 1) {
|
Chris@1109
|
433 paint.setPen(colour);
|
Chris@1109
|
434 paint.setBrush(Qt::NoBrush);
|
Chris@1109
|
435 paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1);
|
Chris@1109
|
436 continue;
|
Chris@1109
|
437 }
|
Chris@1109
|
438
|
Chris@1109
|
439 QColor pen(255, 255, 255, 80);
|
Chris@1109
|
440 QColor brush(colour);
|
Chris@1109
|
441
|
Chris@1109
|
442 if (rw > 3 && r.height() > 3) {
|
Chris@1109
|
443 brush.setAlpha(160);
|
Chris@1109
|
444 }
|
Chris@1109
|
445
|
Chris@1109
|
446 paint.setPen(Qt::NoPen);
|
Chris@1109
|
447 paint.setBrush(brush);
|
Chris@1109
|
448
|
Chris@1115
|
449 if (illuminate) {
|
Chris@1115
|
450 if (r.contains(illuminatePos)) {
|
Chris@1115
|
451 paint.setPen(v->getForeground());
|
Chris@1115
|
452 }
|
Chris@1115
|
453 }
|
Chris@1109
|
454
|
Chris@1109
|
455 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
|
Chris@1109
|
456 // cerr << "rect " << r.x() << "," << r.y() << " "
|
Chris@1109
|
457 // << r.width() << "x" << r.height() << endl;
|
Chris@1109
|
458 #endif
|
Chris@1109
|
459
|
Chris@1109
|
460 paint.drawRect(r);
|
Chris@1109
|
461
|
Chris@1109
|
462 if (showLabel) {
|
Chris@1109
|
463 double value = model->getValueAt(sx, sy);
|
Chris@1109
|
464 snprintf(labelbuf, buflen, "%06f", value);
|
Chris@1109
|
465 QString text(labelbuf);
|
Chris@1109
|
466 PaintAssistant::drawVisibleText
|
Chris@1109
|
467 (v,
|
Chris@1109
|
468 paint,
|
Chris@1109
|
469 rx0 + 2,
|
Chris@1109
|
470 ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(),
|
Chris@1109
|
471 text,
|
Chris@1109
|
472 PaintAssistant::OutlinedText);
|
Chris@1109
|
473 }
|
Chris@1109
|
474 }
|
Chris@1109
|
475 }
|
Chris@1121
|
476
|
Chris@1121
|
477 return magRange;
|
Chris@1094
|
478 }
|
Chris@1094
|
479
|
Chris@1080
|
480 void
|
Chris@1113
|
481 Colour3DPlotRenderer::renderToCachePixelResolution(const LayerGeometryProvider *v,
|
Chris@1094
|
482 int x0, int repaintWidth,
|
Chris@1094
|
483 bool rightToLeft,
|
Chris@1094
|
484 bool timeConstrained)
|
Chris@1079
|
485 {
|
Chris@1117
|
486 Profiler profiler("Colour3DPlotRenderer::renderToCachePixelResolution");
|
Chris@1094
|
487 cerr << "renderToCachePixelResolution" << endl;
|
Chris@1094
|
488
|
Chris@1094
|
489 // Draw to the draw buffer, and then copy from there. The draw
|
Chris@1094
|
490 // buffer is at the same resolution as the target in the cache, so
|
Chris@1094
|
491 // no extra scaling needed.
|
Chris@1079
|
492
|
Chris@1100
|
493 const DenseThreeDimensionalModel *model = m_sources.source;
|
Chris@1079
|
494 if (!model || !model->isOK() || !model->isReady()) {
|
Chris@1079
|
495 throw std::logic_error("no source model provided, or model not ready");
|
Chris@1079
|
496 }
|
Chris@1079
|
497
|
Chris@1079
|
498 int h = v->getPaintHeight();
|
Chris@1079
|
499
|
Chris@1094
|
500 clearDrawBuffer(repaintWidth, h);
|
Chris@1079
|
501
|
Chris@1094
|
502 vector<int> binforx(repaintWidth);
|
Chris@1079
|
503 vector<double> binfory(h);
|
Chris@1079
|
504
|
Chris@1079
|
505 bool usePeaksCache = false;
|
Chris@1079
|
506 int binsPerPeak = 1;
|
Chris@1094
|
507 int zoomLevel = v->getZoomLevel();
|
Chris@1094
|
508 int binResolution = model->getResolution();
|
Chris@1079
|
509
|
Chris@1094
|
510 for (int x = 0; x < repaintWidth; ++x) {
|
Chris@1094
|
511 sv_frame_t f0 = v->getFrameForX(x0 + x);
|
Chris@1094
|
512 double s0 = double(f0 - model->getStartFrame()) / binResolution;
|
Chris@1094
|
513 binforx[x] = int(s0 + 0.0001);
|
Chris@1094
|
514 }
|
Chris@1080
|
515
|
Chris@1094
|
516 if (m_sources.peaks) { // peaks cache exists
|
Chris@1080
|
517
|
Chris@1094
|
518 binsPerPeak = m_sources.peaks->getColumnsPerPeak();
|
Chris@1094
|
519 usePeaksCache = (binResolution * binsPerPeak) < zoomLevel;
|
Chris@1094
|
520
|
Chris@1094
|
521 if (m_params.colourScale.getScale() ==
|
Chris@1105
|
522 ColourScaleType::Phase) {
|
Chris@1094
|
523 usePeaksCache = false;
|
Chris@1079
|
524 }
|
Chris@1079
|
525 }
|
Chris@1082
|
526
|
Chris@1094
|
527 cerr << "[PIX] zoomLevel = " << zoomLevel
|
Chris@1094
|
528 << ", binResolution " << binResolution
|
Chris@1094
|
529 << ", binsPerPeak " << binsPerPeak
|
Chris@1094
|
530 << ", peak cache " << m_sources.peaks
|
Chris@1094
|
531 << ", usePeaksCache = " << usePeaksCache
|
Chris@1094
|
532 << endl;
|
Chris@1094
|
533
|
Chris@1080
|
534 for (int y = 0; y < h; ++y) {
|
Chris@1090
|
535 binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1);
|
Chris@1080
|
536 }
|
Chris@1079
|
537
|
Chris@1097
|
538 int attainedWidth;
|
Chris@1097
|
539
|
Chris@1103
|
540 if (m_params.binDisplay == BinDisplay::PeakFrequencies) {
|
Chris@1097
|
541 attainedWidth = renderDrawBufferPeakFrequencies(v,
|
Chris@1097
|
542 repaintWidth,
|
Chris@1097
|
543 h,
|
Chris@1097
|
544 binforx,
|
Chris@1097
|
545 binfory,
|
Chris@1097
|
546 rightToLeft,
|
Chris@1097
|
547 timeConstrained);
|
Chris@1097
|
548
|
Chris@1097
|
549 } else {
|
Chris@1097
|
550 attainedWidth = renderDrawBuffer(repaintWidth,
|
Chris@1080
|
551 h,
|
Chris@1080
|
552 binforx,
|
Chris@1080
|
553 binfory,
|
Chris@1080
|
554 usePeaksCache,
|
Chris@1080
|
555 rightToLeft,
|
Chris@1080
|
556 timeConstrained);
|
Chris@1097
|
557 }
|
Chris@1083
|
558
|
Chris@1094
|
559 if (attainedWidth == 0) return;
|
Chris@1084
|
560
|
Chris@1094
|
561 // draw buffer is pixel resolution, no scaling factors or padding involved
|
Chris@1084
|
562
|
Chris@1084
|
563 int paintedLeft = x0;
|
Chris@1084
|
564 if (rightToLeft) {
|
Chris@1084
|
565 paintedLeft += (repaintWidth - attainedWidth);
|
Chris@1084
|
566 }
|
Chris@1084
|
567
|
Chris@1094
|
568 m_cache.drawImage(paintedLeft, attainedWidth,
|
Chris@1094
|
569 m_drawBuffer,
|
Chris@1094
|
570 paintedLeft - x0, attainedWidth);
|
Chris@1121
|
571
|
Chris@1121
|
572 for (int i = 0; in_range_for(m_magRanges, i); ++i) {
|
Chris@1121
|
573 m_magCache.sampleColumn(i, m_magRanges.at(i));
|
Chris@1121
|
574 }
|
Chris@1094
|
575 }
|
Chris@1084
|
576
|
Chris@1094
|
577 void
|
Chris@1113
|
578 Colour3DPlotRenderer::renderToCacheBinResolution(const LayerGeometryProvider *v,
|
Chris@1094
|
579 int x0, int repaintWidth)
|
Chris@1094
|
580 {
|
Chris@1117
|
581 Profiler profiler("Colour3DPlotRenderer::renderToCacheBinResolution");
|
Chris@1094
|
582 cerr << "renderToCacheBinResolution" << endl;
|
Chris@1094
|
583
|
Chris@1094
|
584 // Draw to the draw buffer, and then scale-copy from there. Draw
|
Chris@1094
|
585 // buffer is at bin resolution, i.e. buffer x == source column
|
Chris@1094
|
586 // number. We use toolkit smooth scaling for interpolation.
|
Chris@1084
|
587
|
Chris@1100
|
588 const DenseThreeDimensionalModel *model = m_sources.source;
|
Chris@1094
|
589 if (!model || !model->isOK() || !model->isReady()) {
|
Chris@1094
|
590 throw std::logic_error("no source model provided, or model not ready");
|
Chris@1094
|
591 }
|
Chris@1094
|
592
|
Chris@1094
|
593 // The draw buffer will contain a fragment at bin resolution. We
|
Chris@1094
|
594 // need to ensure that it starts and ends at points where a
|
Chris@1094
|
595 // time-bin boundary occurs at an exact pixel boundary, and with a
|
Chris@1094
|
596 // certain amount of overlap across existing pixels so that we can
|
Chris@1094
|
597 // scale and draw from it without smoothing errors at the edges.
|
Chris@1094
|
598
|
Chris@1094
|
599 // If (getFrameForX(x) / increment) * increment ==
|
Chris@1094
|
600 // getFrameForX(x), then x is a time-bin boundary. We want two
|
Chris@1094
|
601 // such boundaries at either side of the draw buffer -- one which
|
Chris@1094
|
602 // we draw up to, and one which we subsequently crop at.
|
Chris@1094
|
603
|
Chris@1094
|
604 sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1;
|
Chris@1094
|
605 sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1;
|
Chris@1094
|
606
|
Chris@1094
|
607 int drawBufferWidth;
|
Chris@1094
|
608 int binResolution = model->getResolution();
|
Chris@1094
|
609
|
Chris@1094
|
610 for (int x = x0; ; --x) {
|
Chris@1094
|
611 sv_frame_t f = v->getFrameForX(x);
|
Chris@1094
|
612 if ((f / binResolution) * binResolution == f) {
|
Chris@1094
|
613 if (leftCropFrame == -1) leftCropFrame = f;
|
Chris@1094
|
614 else if (x < x0 - 2) {
|
Chris@1094
|
615 leftBoundaryFrame = f;
|
Chris@1094
|
616 break;
|
Chris@1094
|
617 }
|
Chris@1094
|
618 }
|
Chris@1094
|
619 }
|
Chris@1094
|
620 for (int x = x0 + repaintWidth; ; ++x) {
|
Chris@1094
|
621 sv_frame_t f = v->getFrameForX(x);
|
Chris@1094
|
622 if ((f / binResolution) * binResolution == f) {
|
Chris@1094
|
623 if (rightCropFrame == -1) rightCropFrame = f;
|
Chris@1094
|
624 else if (x > x0 + repaintWidth + 2) {
|
Chris@1094
|
625 rightBoundaryFrame = f;
|
Chris@1094
|
626 break;
|
Chris@1094
|
627 }
|
Chris@1094
|
628 }
|
Chris@1094
|
629 }
|
Chris@1094
|
630 drawBufferWidth = int
|
Chris@1094
|
631 ((rightBoundaryFrame - leftBoundaryFrame) / binResolution);
|
Chris@1094
|
632
|
Chris@1094
|
633 int h = v->getPaintHeight();
|
Chris@1094
|
634
|
Chris@1095
|
635 // For our purposes here, the draw buffer needs to be exactly our
|
Chris@1095
|
636 // target size (so we recreate always rather than just clear it)
|
Chris@1095
|
637
|
Chris@1095
|
638 recreateDrawBuffer(drawBufferWidth, h);
|
Chris@1094
|
639
|
Chris@1094
|
640 vector<int> binforx(drawBufferWidth);
|
Chris@1094
|
641 vector<double> binfory(h);
|
Chris@1094
|
642
|
Chris@1094
|
643 for (int x = 0; x < drawBufferWidth; ++x) {
|
Chris@1094
|
644 binforx[x] = int(leftBoundaryFrame / binResolution) + x;
|
Chris@1094
|
645 }
|
Chris@1094
|
646
|
Chris@1094
|
647 cerr << "[BIN] binResolution " << binResolution
|
Chris@1094
|
648 << endl;
|
Chris@1094
|
649
|
Chris@1094
|
650 for (int y = 0; y < h; ++y) {
|
Chris@1094
|
651 binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1);
|
Chris@1094
|
652 }
|
Chris@1094
|
653
|
Chris@1094
|
654 int attainedWidth = renderDrawBuffer(drawBufferWidth,
|
Chris@1094
|
655 h,
|
Chris@1094
|
656 binforx,
|
Chris@1094
|
657 binfory,
|
Chris@1094
|
658 false,
|
Chris@1094
|
659 false,
|
Chris@1094
|
660 false);
|
Chris@1094
|
661
|
Chris@1094
|
662 if (attainedWidth == 0) return;
|
Chris@1094
|
663
|
Chris@1094
|
664 int scaledLeft = v->getXForFrame(leftBoundaryFrame);
|
Chris@1094
|
665 int scaledRight = v->getXForFrame(rightBoundaryFrame);
|
Chris@1095
|
666
|
Chris@1095
|
667 cerr << "scaling draw buffer from width " << m_drawBuffer.width()
|
Chris@1095
|
668 << " to " << (scaledRight - scaledLeft) << " (nb drawBufferWidth = "
|
Chris@1095
|
669 << drawBufferWidth << ")" << endl;
|
Chris@1094
|
670
|
Chris@1094
|
671 QImage scaled = m_drawBuffer.scaled
|
Chris@1094
|
672 (scaledRight - scaledLeft, h,
|
Chris@1094
|
673 Qt::IgnoreAspectRatio, (m_params.interpolate ?
|
Chris@1094
|
674 Qt::SmoothTransformation :
|
Chris@1094
|
675 Qt::FastTransformation));
|
Chris@1084
|
676
|
Chris@1094
|
677 int scaledLeftCrop = v->getXForFrame(leftCropFrame);
|
Chris@1094
|
678 int scaledRightCrop = v->getXForFrame(rightCropFrame);
|
Chris@1094
|
679
|
Chris@1094
|
680 int targetLeft = scaledLeftCrop;
|
Chris@1094
|
681 if (targetLeft < 0) {
|
Chris@1094
|
682 targetLeft = 0;
|
Chris@1094
|
683 }
|
Chris@1094
|
684
|
Chris@1094
|
685 int targetWidth = scaledRightCrop - targetLeft;
|
Chris@1094
|
686 if (targetLeft + targetWidth > m_cache.getSize().width()) {
|
Chris@1094
|
687 targetWidth = m_cache.getSize().width() - targetLeft;
|
Chris@1094
|
688 }
|
Chris@1094
|
689
|
Chris@1094
|
690 int sourceLeft = targetLeft - scaledLeft;
|
Chris@1094
|
691 if (sourceLeft < 0) {
|
Chris@1094
|
692 sourceLeft = 0;
|
Chris@1094
|
693 }
|
Chris@1094
|
694
|
Chris@1094
|
695 int sourceWidth = targetWidth;
|
Chris@1094
|
696
|
Chris@1094
|
697 cerr << "repaintWidth = " << repaintWidth
|
Chris@1094
|
698 << ", targetWidth = " << targetWidth << endl;
|
Chris@1094
|
699
|
Chris@1094
|
700 if (targetWidth > 0) {
|
Chris@1094
|
701 m_cache.drawImage(targetLeft, targetWidth,
|
Chris@1094
|
702 scaled,
|
Chris@1094
|
703 sourceLeft, sourceWidth);
|
Chris@1084
|
704 }
|
Chris@1121
|
705
|
Chris@1121
|
706 for (int i = 0; i < targetWidth; ++i) {
|
Chris@1121
|
707 int sourceIx = int((double(i) / targetWidth) * sourceWidth);
|
Chris@1121
|
708 if (in_range_for(m_magRanges, sourceIx)) {
|
Chris@1121
|
709 m_magCache.sampleColumn(i, m_magRanges.at(sourceIx));
|
Chris@1121
|
710 }
|
Chris@1121
|
711 }
|
Chris@1079
|
712 }
|
Chris@1083
|
713
|
Chris@1083
|
714 int
|
Chris@1083
|
715 Colour3DPlotRenderer::renderDrawBuffer(int w, int h,
|
Chris@1083
|
716 const vector<int> &binforx,
|
Chris@1083
|
717 const vector<double> &binfory,
|
Chris@1083
|
718 bool usePeaksCache,
|
Chris@1083
|
719 bool rightToLeft,
|
Chris@1083
|
720 bool timeConstrained)
|
Chris@1083
|
721 {
|
Chris@1083
|
722 // Callers must have checked that the appropriate subset of
|
Chris@1083
|
723 // Sources data members are set for the supplied flags (e.g. that
|
Chris@1083
|
724 // peaks model exists if usePeaksCache)
|
Chris@1083
|
725
|
Chris@1083
|
726 RenderTimer timer(timeConstrained ?
|
Chris@1083
|
727 RenderTimer::FastRender :
|
Chris@1083
|
728 RenderTimer::NoTimeout);
|
Chris@1083
|
729
|
Chris@1083
|
730 int minbin = int(binfory[0] + 0.0001);
|
Chris@1083
|
731 int maxbin = int(binfory[h-1]);
|
Chris@1083
|
732 if (minbin < 0) minbin = 0;
|
Chris@1083
|
733 if (maxbin < 0) maxbin = minbin+1;
|
Chris@1083
|
734
|
Chris@1083
|
735 int divisor = 1;
|
Chris@1100
|
736 const DenseThreeDimensionalModel *sourceModel = m_sources.source;
|
Chris@1083
|
737 if (usePeaksCache) {
|
Chris@1083
|
738 divisor = m_sources.peaks->getColumnsPerPeak();
|
Chris@1083
|
739 sourceModel = m_sources.peaks;
|
Chris@1083
|
740 }
|
Chris@1083
|
741
|
Chris@1083
|
742 int psx = -1;
|
Chris@1083
|
743
|
Chris@1083
|
744 int start = 0;
|
Chris@1083
|
745 int finish = w;
|
Chris@1083
|
746 int step = 1;
|
Chris@1083
|
747
|
Chris@1083
|
748 if (rightToLeft) {
|
Chris@1083
|
749 start = w-1;
|
Chris@1083
|
750 finish = -1;
|
Chris@1083
|
751 step = -1;
|
Chris@1083
|
752 }
|
Chris@1083
|
753
|
Chris@1083
|
754 int columnCount = 0;
|
Chris@1083
|
755
|
Chris@1083
|
756 vector<float> preparedColumn;
|
Chris@1094
|
757
|
Chris@1094
|
758 int modelWidth = sourceModel->getWidth();
|
Chris@1121
|
759
|
Chris@1121
|
760 cerr << "modelWidth " << modelWidth << ", divisor " << divisor << endl;
|
Chris@1121
|
761
|
Chris@1083
|
762 for (int x = start; x != finish; x += step) {
|
Chris@1083
|
763
|
Chris@1083
|
764 // x is the on-canvas pixel coord; sx (later) will be the
|
Chris@1083
|
765 // source column index
|
Chris@1083
|
766
|
Chris@1083
|
767 ++columnCount;
|
Chris@1120
|
768
|
Chris@1120
|
769 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1121
|
770 cerr << "x = " << x << ", binforx[x] = " << binforx[x] << endl;
|
Chris@1120
|
771 #endif
|
Chris@1083
|
772
|
Chris@1083
|
773 if (binforx[x] < 0) continue;
|
Chris@1083
|
774
|
Chris@1083
|
775 int sx0 = binforx[x] / divisor;
|
Chris@1083
|
776 int sx1 = sx0;
|
Chris@1083
|
777 if (x+1 < w) sx1 = binforx[x+1] / divisor;
|
Chris@1083
|
778 if (sx0 < 0) sx0 = sx1 - 1;
|
Chris@1083
|
779 if (sx0 < 0) continue;
|
Chris@1083
|
780 if (sx1 <= sx0) sx1 = sx0 + 1;
|
Chris@1083
|
781
|
Chris@1083
|
782 vector<float> pixelPeakColumn;
|
Chris@1121
|
783 MagnitudeRange magRange;
|
Chris@1083
|
784
|
Chris@1083
|
785 for (int sx = sx0; sx < sx1; ++sx) {
|
Chris@1083
|
786
|
Chris@1083
|
787 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1094
|
788 cerr << "sx = " << sx << endl;
|
Chris@1083
|
789 #endif
|
Chris@1083
|
790
|
Chris@1094
|
791 if (sx < 0 || sx >= modelWidth) {
|
Chris@1083
|
792 continue;
|
Chris@1083
|
793 }
|
Chris@1083
|
794
|
Chris@1083
|
795 if (sx != psx) {
|
Chris@1083
|
796
|
Chris@1083
|
797 // order:
|
Chris@1083
|
798 // get column -> scale -> record extents ->
|
Chris@1083
|
799 // normalise -> peak pick -> apply display gain ->
|
Chris@1083
|
800 // distribute/interpolate
|
Chris@1083
|
801
|
Chris@1083
|
802 ColumnOp::Column fullColumn = sourceModel->getColumn(sx);
|
Chris@1090
|
803
|
Chris@1094
|
804 // cerr << "x " << x << ", sx " << sx << ", col height " << fullColumn.size()
|
Chris@1094
|
805 // << ", minbin " << minbin << ", maxbin " << maxbin << endl;
|
Chris@1090
|
806
|
Chris@1083
|
807 ColumnOp::Column column =
|
Chris@1083
|
808 vector<float>(fullColumn.data() + minbin,
|
Chris@1083
|
809 fullColumn.data() + maxbin + 1);
|
Chris@1083
|
810
|
Chris@1105
|
811 //!!! fft scale if (m_colourScale != ColourScaleType::Phase) {
|
Chris@1083
|
812 // column = ColumnOp::fftScale(column, m_fftSize);
|
Chris@1083
|
813 // }
|
Chris@1083
|
814
|
Chris@1121
|
815 magRange.sample(column);
|
Chris@1120
|
816
|
Chris@1083
|
817 //!!! extents recordColumnExtents(column,
|
Chris@1083
|
818 // sx,
|
Chris@1083
|
819 // overallMag,
|
Chris@1083
|
820 // overallMagChanged);
|
Chris@1083
|
821
|
Chris@1105
|
822 // if (m_colourScale != ColourScaleType::Phase) {
|
Chris@1083
|
823 column = ColumnOp::normalize(column, m_params.normalization);
|
Chris@1083
|
824 // }
|
Chris@1083
|
825
|
Chris@1103
|
826 if (m_params.binDisplay == BinDisplay::PeakBins) {
|
Chris@1083
|
827 column = ColumnOp::peakPick(column);
|
Chris@1083
|
828 }
|
Chris@1083
|
829
|
Chris@1083
|
830 preparedColumn =
|
Chris@1083
|
831 ColumnOp::distribute(column, //!!! gain? ColumnOp::applyGain(column, m_gain),
|
Chris@1083
|
832 h,
|
Chris@1083
|
833 binfory,
|
Chris@1083
|
834 minbin,
|
Chris@1083
|
835 m_params.interpolate);
|
Chris@1083
|
836
|
Chris@1083
|
837 psx = sx;
|
Chris@1083
|
838 }
|
Chris@1083
|
839
|
Chris@1083
|
840 if (sx == sx0) {
|
Chris@1083
|
841 pixelPeakColumn = preparedColumn;
|
Chris@1083
|
842 } else {
|
Chris@1083
|
843 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
|
Chris@1083
|
844 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
|
Chris@1083
|
845 preparedColumn[i]);
|
Chris@1083
|
846 }
|
Chris@1083
|
847 }
|
Chris@1083
|
848 }
|
Chris@1083
|
849
|
Chris@1083
|
850 if (!pixelPeakColumn.empty()) {
|
Chris@1121
|
851
|
Chris@1083
|
852 for (int y = 0; y < h; ++y) {
|
Chris@1116
|
853 int py;
|
Chris@1116
|
854 if (m_params.invertVertical) {
|
Chris@1116
|
855 py = y;
|
Chris@1116
|
856 } else {
|
Chris@1116
|
857 py = h - y - 1;
|
Chris@1116
|
858 }
|
Chris@1083
|
859 m_drawBuffer.setPixel
|
Chris@1083
|
860 (x,
|
Chris@1116
|
861 py,
|
Chris@1083
|
862 m_params.colourScale.getPixel(pixelPeakColumn[y]));
|
Chris@1083
|
863 }
|
Chris@1121
|
864
|
Chris@1121
|
865 m_magRanges.push_back(magRange);
|
Chris@1083
|
866 }
|
Chris@1083
|
867
|
Chris@1083
|
868 double fractionComplete = double(columnCount) / double(w);
|
Chris@1083
|
869 if (timer.outOfTime(fractionComplete)) {
|
Chris@1121
|
870 cerr << "out of time" << endl;
|
Chris@1083
|
871 return columnCount;
|
Chris@1083
|
872 }
|
Chris@1083
|
873 }
|
Chris@1083
|
874
|
Chris@1083
|
875 return columnCount;
|
Chris@1083
|
876 }
|
Chris@1083
|
877
|
Chris@1097
|
878 int
|
Chris@1113
|
879 Colour3DPlotRenderer::renderDrawBufferPeakFrequencies(const LayerGeometryProvider *v,
|
Chris@1097
|
880 int w, int h,
|
Chris@1097
|
881 const vector<int> &binforx,
|
Chris@1097
|
882 const vector<double> &binfory,
|
Chris@1097
|
883 bool rightToLeft,
|
Chris@1097
|
884 bool timeConstrained)
|
Chris@1097
|
885 {
|
Chris@1097
|
886 // Callers must have checked that the appropriate subset of
|
Chris@1097
|
887 // Sources data members are set for the supplied flags (e.g. that
|
Chris@1097
|
888 // fft model exists)
|
Chris@1097
|
889
|
Chris@1097
|
890 RenderTimer timer(timeConstrained ?
|
Chris@1097
|
891 RenderTimer::FastRender :
|
Chris@1097
|
892 RenderTimer::NoTimeout);
|
Chris@1097
|
893
|
Chris@1097
|
894 int minbin = int(binfory[0] + 0.0001);
|
Chris@1097
|
895 int maxbin = int(binfory[h-1]);
|
Chris@1097
|
896 if (minbin < 0) minbin = 0;
|
Chris@1097
|
897 if (maxbin < 0) maxbin = minbin+1;
|
Chris@1097
|
898
|
Chris@1100
|
899 const FFTModel *fft = m_sources.fft;
|
Chris@1097
|
900
|
Chris@1097
|
901 FFTModel::PeakSet peakfreqs;
|
Chris@1097
|
902
|
Chris@1097
|
903 int psx = -1;
|
Chris@1097
|
904
|
Chris@1097
|
905 int start = 0;
|
Chris@1097
|
906 int finish = w;
|
Chris@1097
|
907 int step = 1;
|
Chris@1097
|
908
|
Chris@1097
|
909 if (rightToLeft) {
|
Chris@1097
|
910 start = w-1;
|
Chris@1097
|
911 finish = -1;
|
Chris@1097
|
912 step = -1;
|
Chris@1097
|
913 }
|
Chris@1097
|
914
|
Chris@1097
|
915 int columnCount = 0;
|
Chris@1097
|
916
|
Chris@1097
|
917 vector<float> preparedColumn;
|
Chris@1097
|
918
|
Chris@1097
|
919 int modelWidth = fft->getWidth();
|
Chris@1097
|
920 cerr << "modelWidth " << modelWidth << endl;
|
Chris@1097
|
921
|
Chris@1097
|
922 double minFreq = (double(minbin) * fft->getSampleRate()) / fft->getFFTSize();
|
Chris@1097
|
923 double maxFreq = (double(maxbin) * fft->getSampleRate()) / fft->getFFTSize();
|
Chris@1097
|
924
|
Chris@1103
|
925 bool logarithmic = (m_params.binScale == BinScale::Log);
|
Chris@1097
|
926
|
Chris@1097
|
927 for (int x = start; x != finish; x += step) {
|
Chris@1097
|
928
|
Chris@1097
|
929 // x is the on-canvas pixel coord; sx (later) will be the
|
Chris@1097
|
930 // source column index
|
Chris@1097
|
931
|
Chris@1097
|
932 ++columnCount;
|
Chris@1097
|
933
|
Chris@1097
|
934 if (binforx[x] < 0) continue;
|
Chris@1097
|
935
|
Chris@1097
|
936 int sx0 = binforx[x];
|
Chris@1097
|
937 int sx1 = sx0;
|
Chris@1097
|
938 if (x+1 < w) sx1 = binforx[x+1];
|
Chris@1097
|
939 if (sx0 < 0) sx0 = sx1 - 1;
|
Chris@1097
|
940 if (sx0 < 0) continue;
|
Chris@1097
|
941 if (sx1 <= sx0) sx1 = sx0 + 1;
|
Chris@1097
|
942
|
Chris@1097
|
943 vector<float> pixelPeakColumn;
|
Chris@1121
|
944 MagnitudeRange magRange;
|
Chris@1097
|
945
|
Chris@1097
|
946 for (int sx = sx0; sx < sx1; ++sx) {
|
Chris@1097
|
947
|
Chris@1097
|
948 if (sx < 0 || sx >= modelWidth) {
|
Chris@1097
|
949 continue;
|
Chris@1097
|
950 }
|
Chris@1097
|
951
|
Chris@1097
|
952 if (sx != psx) {
|
Chris@1097
|
953
|
Chris@1097
|
954 ColumnOp::Column fullColumn = fft->getColumn(sx);
|
Chris@1097
|
955
|
Chris@1097
|
956 ColumnOp::Column column =
|
Chris@1097
|
957 vector<float>(fullColumn.data() + minbin,
|
Chris@1097
|
958 fullColumn.data() + maxbin + 1);
|
Chris@1097
|
959
|
Chris@1121
|
960 magRange.sample(column);
|
Chris@1121
|
961
|
Chris@1105
|
962 //!!! fft scale if (m_colourScale != ColourScaleType::Phase) {
|
Chris@1097
|
963 // column = ColumnOp::fftScale(column, getFFTSize());
|
Chris@1097
|
964 // }
|
Chris@1097
|
965
|
Chris@1097
|
966 //!!! extents recordColumnExtents(column,
|
Chris@1097
|
967 // sx,
|
Chris@1097
|
968 // overallMag,
|
Chris@1097
|
969 // overallMagChanged);
|
Chris@1097
|
970
|
Chris@1105
|
971 //!!! if (m_colourScale != ColourScaleType::Phase) {
|
Chris@1097
|
972 column = ColumnOp::normalize(column, m_params.normalization);
|
Chris@1097
|
973 //!!! }
|
Chris@1097
|
974
|
Chris@1097
|
975 preparedColumn = column;
|
Chris@1097
|
976 //!!! gain? preparedColumn = ColumnOp::applyGain(column, m_params.gain);
|
Chris@1097
|
977
|
Chris@1097
|
978 psx = sx;
|
Chris@1097
|
979 }
|
Chris@1097
|
980
|
Chris@1097
|
981 if (sx == sx0) {
|
Chris@1097
|
982 pixelPeakColumn = preparedColumn;
|
Chris@1097
|
983 peakfreqs = fft->getPeakFrequencies(FFTModel::AllPeaks, sx,
|
Chris@1097
|
984 minbin, maxbin - 1);
|
Chris@1097
|
985 } else {
|
Chris@1097
|
986 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
|
Chris@1097
|
987 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
|
Chris@1097
|
988 preparedColumn[i]);
|
Chris@1097
|
989 }
|
Chris@1097
|
990 }
|
Chris@1097
|
991 }
|
Chris@1097
|
992
|
Chris@1097
|
993 if (!pixelPeakColumn.empty()) {
|
Chris@1121
|
994
|
Chris@1097
|
995 for (FFTModel::PeakSet::const_iterator pi = peakfreqs.begin();
|
Chris@1097
|
996 pi != peakfreqs.end(); ++pi) {
|
Chris@1097
|
997
|
Chris@1097
|
998 int bin = pi->first;
|
Chris@1097
|
999 double freq = pi->second;
|
Chris@1097
|
1000
|
Chris@1097
|
1001 if (bin < minbin) continue;
|
Chris@1097
|
1002 if (bin > maxbin) break;
|
Chris@1097
|
1003
|
Chris@1097
|
1004 double value = pixelPeakColumn[bin - minbin];
|
Chris@1097
|
1005
|
Chris@1097
|
1006 double y = v->getYForFrequency
|
Chris@1097
|
1007 (freq, minFreq, maxFreq, logarithmic);
|
Chris@1097
|
1008
|
Chris@1097
|
1009 int iy = int(y + 0.5);
|
Chris@1097
|
1010 if (iy < 0 || iy >= h) continue;
|
Chris@1097
|
1011
|
Chris@1097
|
1012 m_drawBuffer.setPixel
|
Chris@1097
|
1013 (x,
|
Chris@1097
|
1014 iy,
|
Chris@1097
|
1015 m_params.colourScale.getPixel(value));
|
Chris@1097
|
1016 }
|
Chris@1121
|
1017
|
Chris@1121
|
1018 m_magRanges.push_back(magRange);
|
Chris@1097
|
1019 }
|
Chris@1097
|
1020
|
Chris@1097
|
1021 double fractionComplete = double(columnCount) / double(w);
|
Chris@1097
|
1022 if (timer.outOfTime(fractionComplete)) {
|
Chris@1097
|
1023 return columnCount;
|
Chris@1097
|
1024 }
|
Chris@1097
|
1025 }
|
Chris@1097
|
1026
|
Chris@1097
|
1027 return columnCount;
|
Chris@1097
|
1028 }
|
Chris@1097
|
1029
|
Chris@1079
|
1030 void
|
Chris@1095
|
1031 Colour3DPlotRenderer::recreateDrawBuffer(int w, int h)
|
Chris@1079
|
1032 {
|
Chris@1095
|
1033 m_drawBuffer = QImage(w, h, QImage::Format_Indexed8);
|
Chris@1079
|
1034
|
Chris@1095
|
1035 for (int pixel = 0; pixel < 256; ++pixel) {
|
Chris@1095
|
1036 m_drawBuffer.setColor
|
Chris@1095
|
1037 ((unsigned char)pixel,
|
Chris@1112
|
1038 m_params.colourScale.getColourForPixel
|
Chris@1112
|
1039 (pixel, m_params.colourRotation).rgb());
|
Chris@1079
|
1040 }
|
Chris@1079
|
1041
|
Chris@1079
|
1042 m_drawBuffer.fill(0);
|
Chris@1121
|
1043 m_magRanges.clear();
|
Chris@1079
|
1044 }
|
Chris@1079
|
1045
|
Chris@1095
|
1046 void
|
Chris@1095
|
1047 Colour3DPlotRenderer::clearDrawBuffer(int w, int h)
|
Chris@1095
|
1048 {
|
Chris@1095
|
1049 if (m_drawBuffer.width() < w || m_drawBuffer.height() != h) {
|
Chris@1095
|
1050 recreateDrawBuffer(w, h);
|
Chris@1095
|
1051 } else {
|
Chris@1095
|
1052 m_drawBuffer.fill(0);
|
Chris@1121
|
1053 m_magRanges.clear();
|
Chris@1095
|
1054 }
|
Chris@1095
|
1055 }
|
Chris@1079
|
1056
|
Chris@1095
|
1057
|