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@1214
|
20 #include "base/HitCount.h"
|
Chris@1117
|
21
|
Chris@1075
|
22 #include "data/model/DenseThreeDimensionalModel.h"
|
Chris@1075
|
23 #include "data/model/Dense3DModelPeakCache.h"
|
Chris@1075
|
24 #include "data/model/FFTModel.h"
|
Chris@1075
|
25
|
Chris@1077
|
26 #include "LayerGeometryProvider.h"
|
Chris@1082
|
27 #include "VerticalBinLayer.h"
|
Chris@1109
|
28 #include "PaintAssistant.h"
|
Chris@1139
|
29 #include "ImageRegionFinder.h"
|
Chris@1109
|
30
|
Chris@1109
|
31 #include "view/ViewManager.h" // for main model sample rate. Pity
|
Chris@1075
|
32
|
Chris@1079
|
33 #include <vector>
|
Chris@1079
|
34
|
Chris@1325
|
35 #include <utility>
|
Chris@1325
|
36 using namespace std::rel_ops;
|
Chris@1325
|
37
|
Chris@1362
|
38 //#define DEBUG_COLOUR_PLOT_REPAINT 1
|
Chris@1502
|
39 //#define DEBUG_COLOUR_PLOT_CACHE_SELECTION 1
|
Chris@1094
|
40
|
Chris@1079
|
41 using namespace std;
|
Chris@1079
|
42
|
Chris@1073
|
43 Colour3DPlotRenderer::RenderResult
|
Chris@1113
|
44 Colour3DPlotRenderer::render(const LayerGeometryProvider *v, QPainter &paint, QRect rect)
|
Chris@1076
|
45 {
|
Chris@1090
|
46 return render(v, paint, rect, false);
|
Chris@1076
|
47 }
|
Chris@1076
|
48
|
Chris@1076
|
49 Colour3DPlotRenderer::RenderResult
|
Chris@1113
|
50 Colour3DPlotRenderer::renderTimeConstrained(const LayerGeometryProvider *v,
|
Chris@1090
|
51 QPainter &paint, QRect rect)
|
Chris@1076
|
52 {
|
Chris@1090
|
53 return render(v, paint, rect, true);
|
Chris@1076
|
54 }
|
Chris@1076
|
55
|
Chris@1096
|
56 QRect
|
Chris@1121
|
57 Colour3DPlotRenderer::getLargestUncachedRect(const LayerGeometryProvider *v)
|
Chris@1096
|
58 {
|
Chris@1121
|
59 RenderType renderType = decideRenderType(v);
|
Chris@1121
|
60
|
Chris@1121
|
61 if (renderType == DirectTranslucent) {
|
Chris@1121
|
62 return QRect(); // never cached
|
Chris@1121
|
63 }
|
Chris@1121
|
64
|
Chris@1096
|
65 int h = m_cache.getSize().height();
|
Chris@1096
|
66
|
Chris@1096
|
67 QRect areaLeft(0, 0, m_cache.getValidLeft(), h);
|
Chris@1096
|
68 QRect areaRight(m_cache.getValidRight(), 0,
|
Chris@1096
|
69 m_cache.getSize().width() - m_cache.getValidRight(), h);
|
Chris@1096
|
70
|
Chris@1096
|
71 if (areaRight.width() > areaLeft.width()) {
|
Chris@1096
|
72 return areaRight;
|
Chris@1096
|
73 } else {
|
Chris@1096
|
74 return areaLeft;
|
Chris@1096
|
75 }
|
Chris@1096
|
76 }
|
Chris@1096
|
77
|
Chris@1122
|
78 bool
|
Chris@1122
|
79 Colour3DPlotRenderer::geometryChanged(const LayerGeometryProvider *v)
|
Chris@1122
|
80 {
|
Chris@1122
|
81 RenderType renderType = decideRenderType(v);
|
Chris@1122
|
82
|
Chris@1122
|
83 if (renderType == DirectTranslucent) {
|
Chris@1122
|
84 return true; // never cached
|
Chris@1122
|
85 }
|
Chris@1122
|
86
|
Chris@1122
|
87 if (m_cache.getSize() == v->getPaintSize() &&
|
Chris@1122
|
88 m_cache.getZoomLevel() == v->getZoomLevel() &&
|
Chris@1122
|
89 m_cache.getStartFrame() == v->getStartFrame()) {
|
Chris@1122
|
90 return false;
|
Chris@1122
|
91 } else {
|
Chris@1122
|
92 return true;
|
Chris@1122
|
93 }
|
Chris@1122
|
94 }
|
Chris@1122
|
95
|
Chris@1076
|
96 Colour3DPlotRenderer::RenderResult
|
Chris@1113
|
97 Colour3DPlotRenderer::render(const LayerGeometryProvider *v,
|
Chris@1090
|
98 QPainter &paint, QRect rect, bool timeConstrained)
|
Chris@1073
|
99 {
|
Chris@1109
|
100 RenderType renderType = decideRenderType(v);
|
Chris@1109
|
101
|
Chris@1221
|
102 if (timeConstrained) {
|
Chris@1221
|
103 if (renderType != DrawBufferPixelResolution) {
|
Chris@1221
|
104 // Rendering should be fast in bin-resolution and direct
|
Chris@1221
|
105 // draw cases because we are quite well zoomed-in, and the
|
Chris@1221
|
106 // sums are easier this way. Calculating boundaries later
|
Chris@1221
|
107 // will be fiddly for partial paints otherwise.
|
Chris@1221
|
108 timeConstrained = false;
|
Chris@1221
|
109
|
Chris@1221
|
110 } else if (m_secondsPerXPixelValid) {
|
Chris@1221
|
111 double predicted = m_secondsPerXPixel * rect.width();
|
Chris@1236
|
112 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1499
|
113 SVDEBUG << "render " << m_sources.source
|
Chris@1499
|
114 << ": Predicted time for width " << rect.width() << " = "
|
Chris@1236
|
115 << predicted << " (" << m_secondsPerXPixel << " x "
|
Chris@1236
|
116 << rect.width() << ")" << endl;
|
Chris@1221
|
117 #endif
|
Chris@1450
|
118 if (predicted < 0.175) {
|
Chris@1221
|
119 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1499
|
120 SVDEBUG << "render " << m_sources.source
|
Chris@1499
|
121 << ": Predicted time looks fast enough: no partial renders"
|
Chris@1221
|
122 << endl;
|
Chris@1221
|
123 #endif
|
Chris@1221
|
124 timeConstrained = false;
|
Chris@1221
|
125 }
|
Chris@1221
|
126 }
|
Chris@1109
|
127 }
|
Chris@1221
|
128
|
Chris@1079
|
129 int x0 = v->getXForViewX(rect.x());
|
Chris@1079
|
130 int x1 = v->getXForViewX(rect.x() + rect.width());
|
Chris@1079
|
131 if (x0 < 0) x0 = 0;
|
Chris@1079
|
132 if (x1 > v->getPaintWidth()) x1 = v->getPaintWidth();
|
Chris@1079
|
133
|
Chris@1120
|
134 sv_frame_t startFrame = v->getStartFrame();
|
Chris@1451
|
135
|
Chris@1451
|
136 bool justInvalidated =
|
Chris@1451
|
137 (m_cache.getSize() != v->getPaintSize() ||
|
Chris@1451
|
138 m_cache.getZoomLevel() != v->getZoomLevel());
|
Chris@1120
|
139
|
Chris@1079
|
140 m_cache.resize(v->getPaintSize());
|
Chris@1079
|
141 m_cache.setZoomLevel(v->getZoomLevel());
|
Chris@1079
|
142
|
Chris@1119
|
143 m_magCache.resize(v->getPaintSize().width());
|
Chris@1119
|
144 m_magCache.setZoomLevel(v->getZoomLevel());
|
Chris@1119
|
145
|
Chris@1120
|
146 if (renderType == DirectTranslucent) {
|
Chris@1121
|
147 MagnitudeRange range = renderDirectTranslucent(v, paint, rect);
|
Chris@1121
|
148 return { rect, range };
|
Chris@1120
|
149 }
|
Chris@1120
|
150
|
Chris@1123
|
151 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1499
|
152 SVDEBUG << "render " << m_sources.source
|
Chris@1499
|
153 << ": cache start " << m_cache.getStartFrame()
|
Chris@1499
|
154 << " valid left " << m_cache.getValidLeft()
|
Chris@1499
|
155 << " valid right " << m_cache.getValidRight()
|
Chris@1499
|
156 << endl;
|
Chris@1500
|
157 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
158 << ": view start " << startFrame
|
Chris@1499
|
159 << " x0 " << x0
|
Chris@1499
|
160 << " x1 " << x1
|
Chris@1499
|
161 << endl;
|
Chris@1123
|
162 #endif
|
Chris@1214
|
163
|
Chris@1214
|
164 static HitCount count("Colour3DPlotRenderer: image cache");
|
Chris@1451
|
165
|
Chris@1079
|
166 if (m_cache.isValid()) { // some part of the cache is valid
|
Chris@1079
|
167
|
Chris@1079
|
168 if (v->getXForFrame(m_cache.getStartFrame()) ==
|
Chris@1079
|
169 v->getXForFrame(startFrame) &&
|
Chris@1079
|
170 m_cache.getValidLeft() <= x0 &&
|
Chris@1079
|
171 m_cache.getValidRight() >= x1) {
|
Chris@1090
|
172
|
Chris@1123
|
173 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
174 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
175 << ": cache hit" << endl;
|
Chris@1123
|
176 #endif
|
Chris@1214
|
177 count.hit();
|
Chris@1090
|
178
|
Chris@1079
|
179 // cache is valid for the complete requested area
|
Chris@1079
|
180 paint.drawImage(rect, m_cache.getImage(), rect);
|
Chris@1119
|
181
|
Chris@1122
|
182 MagnitudeRange range = m_magCache.getRange(x0, x1 - x0);
|
Chris@1119
|
183
|
Chris@1119
|
184 return { rect, range };
|
Chris@1079
|
185
|
Chris@1079
|
186 } else {
|
Chris@1123
|
187 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
188 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
189 << ": cache partial hit" << endl;
|
Chris@1123
|
190 #endif
|
Chris@1214
|
191 count.partial();
|
Chris@1090
|
192
|
Chris@1079
|
193 // cache doesn't begin at the right frame or doesn't
|
Chris@1079
|
194 // contain the complete view, but might be scrollable or
|
Chris@1079
|
195 // partially usable
|
Chris@1090
|
196 m_cache.scrollTo(v, startFrame);
|
Chris@1119
|
197 m_magCache.scrollTo(v, startFrame);
|
Chris@1079
|
198
|
Chris@1079
|
199 // if we are not time-constrained, then we want to paint
|
Chris@1081
|
200 // the whole area in one go; we don't return a partial
|
Chris@1081
|
201 // paint. To avoid providing the more complex logic to
|
Chris@1081
|
202 // handle painting discontiguous areas, if the only valid
|
Chris@1079
|
203 // part of cache is in the middle, just make the whole
|
Chris@1079
|
204 // thing invalid and start again.
|
Chris@1079
|
205 if (!timeConstrained) {
|
Chris@1079
|
206 if (m_cache.getValidLeft() > x0 &&
|
Chris@1079
|
207 m_cache.getValidRight() < x1) {
|
Chris@1079
|
208 m_cache.invalidate();
|
Chris@1079
|
209 }
|
Chris@1079
|
210 }
|
Chris@1079
|
211 }
|
Chris@1090
|
212 } else {
|
Chris@1118
|
213 // cache is completely invalid
|
Chris@1214
|
214 count.miss();
|
Chris@1090
|
215 m_cache.setStartFrame(startFrame);
|
Chris@1119
|
216 m_magCache.setStartFrame(startFrame);
|
Chris@1075
|
217 }
|
Chris@1075
|
218
|
Chris@1079
|
219 bool rightToLeft = false;
|
Chris@1079
|
220
|
Chris@1122
|
221 int reqx0 = x0;
|
Chris@1122
|
222 int reqx1 = x1;
|
Chris@1122
|
223
|
Chris@1079
|
224 if (!m_cache.isValid() && timeConstrained) {
|
Chris@1079
|
225 if (x0 == 0 && x1 == v->getPaintWidth()) {
|
Chris@1237
|
226
|
Chris@1237
|
227 // When rendering the whole area, in a context where we
|
Chris@1237
|
228 // might not be able to complete the work, start from
|
Chris@1237
|
229 // somewhere near the middle so that the region of
|
Chris@1237
|
230 // interest appears first.
|
Chris@1237
|
231 //
|
Chris@1237
|
232 // This is very useful if we actually are slow to render,
|
Chris@1237
|
233 // but if we're not sure how fast we'll be, we should
|
Chris@1237
|
234 // prefer not to because it can be distracting to render
|
Chris@1237
|
235 // fast from the middle and then jump back to fill in the
|
Chris@1237
|
236 // start. That is:
|
Chris@1237
|
237 //
|
Chris@1237
|
238 // - if our seconds-per-x-pixel count is invalid, then we
|
Chris@1237
|
239 // don't do this: we've probably only just been created
|
Chris@1237
|
240 // and don't know how fast we'll be yet (this happens
|
Chris@1237
|
241 // often while zooming rapidly in and out). The exception
|
Chris@1237
|
242 // to the exception is if we're displaying peak
|
Chris@1237
|
243 // frequencies; this we can assume to be slow. (Note that
|
Chris@1237
|
244 // if the seconds-per-x-pixel is valid and we know we're
|
Chris@1237
|
245 // fast, then we've already set timeConstrained false
|
Chris@1237
|
246 // above so this doesn't apply)
|
Chris@1237
|
247 //
|
Chris@1237
|
248 // - if we're using a peak cache, we don't do this;
|
Chris@1237
|
249 // drawing from peak cache is often (even if not always)
|
Chris@1237
|
250 // fast.
|
Chris@1237
|
251
|
Chris@1237
|
252 bool drawFromTheMiddle = true;
|
Chris@1237
|
253
|
Chris@1237
|
254 if (!m_secondsPerXPixelValid &&
|
Chris@1237
|
255 (m_params.binDisplay != BinDisplay::PeakFrequencies)) {
|
Chris@1237
|
256 drawFromTheMiddle = false;
|
Chris@1237
|
257 } else {
|
Chris@1237
|
258 int peakCacheIndex = -1, binsPerPeak = -1;
|
Chris@1237
|
259 getPreferredPeakCache(v, peakCacheIndex, binsPerPeak);
|
Chris@1237
|
260 if (peakCacheIndex >= 0) { // have a peak cache
|
Chris@1237
|
261 drawFromTheMiddle = false;
|
Chris@1237
|
262 }
|
Chris@1237
|
263 }
|
Chris@1237
|
264
|
Chris@1237
|
265 if (drawFromTheMiddle) {
|
Chris@1222
|
266 double offset = 0.5 * (double(rand()) / double(RAND_MAX));
|
Chris@1222
|
267 x0 = int(x1 * offset);
|
Chris@1213
|
268 }
|
Chris@1079
|
269 }
|
Chris@1079
|
270 }
|
Chris@1079
|
271
|
Chris@1079
|
272 if (m_cache.isValid()) {
|
Chris@1090
|
273
|
Chris@1079
|
274 // When rendering only a part of the cache, we need to make
|
Chris@1079
|
275 // sure that the part we're rendering is adjacent to (or
|
Chris@1079
|
276 // overlapping) a valid area of cache, if we have one. The
|
Chris@1079
|
277 // alternative is to ditch the valid area of cache and render
|
Chris@1079
|
278 // only the requested area, but that's risky because this can
|
Chris@1079
|
279 // happen when just waving the pointer over a small part of
|
Chris@1079
|
280 // the view -- if we lose the partly-built cache every time
|
Chris@1079
|
281 // the user does that, we'll never finish building it.
|
Chris@1079
|
282 int left = x0;
|
Chris@1079
|
283 int width = x1 - x0;
|
Chris@1079
|
284 bool isLeftOfValidArea = false;
|
Chris@1079
|
285 m_cache.adjustToTouchValidArea(left, width, isLeftOfValidArea);
|
Chris@1079
|
286 x0 = left;
|
Chris@1079
|
287 x1 = x0 + width;
|
Chris@1079
|
288
|
Chris@1079
|
289 // That call also told us whether we should be painting
|
Chris@1079
|
290 // sub-regions of our target region in right-to-left order in
|
Chris@1079
|
291 // order to ensure contiguity
|
Chris@1079
|
292 rightToLeft = isLeftOfValidArea;
|
Chris@1079
|
293 }
|
Chris@1075
|
294
|
Chris@1109
|
295 // Note, we always paint the full height to cache. We want to
|
Chris@1109
|
296 // ensure the cache is coherent without having to worry about
|
Chris@1109
|
297 // vertical matching of required and valid areas as well as
|
Chris@1109
|
298 // horizontal.
|
Chris@1094
|
299
|
Chris@1109
|
300 if (renderType == DrawBufferBinResolution) {
|
Chris@1109
|
301
|
Chris@1094
|
302 renderToCacheBinResolution(v, x0, x1 - x0);
|
Chris@1109
|
303
|
Chris@1109
|
304 } else { // must be DrawBufferPixelResolution, handled DirectTranslucent earlier
|
Chris@1109
|
305
|
Chris@1451
|
306 if (timeConstrained && justInvalidated) {
|
Chris@1500
|
307 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
308 << ": invalidated cache in time-constrained context, that's all we're doing for now - wait for next update to start filling" << endl;
|
Chris@1451
|
309 } else {
|
Chris@1451
|
310 renderToCachePixelResolution(v, x0, x1 - x0, rightToLeft, timeConstrained);
|
Chris@1451
|
311 }
|
Chris@1094
|
312 }
|
Chris@1079
|
313
|
Chris@1079
|
314 QRect pr = rect & m_cache.getValidArea();
|
Chris@1079
|
315 paint.drawImage(pr.x(), pr.y(), m_cache.getImage(),
|
Chris@1079
|
316 pr.x(), pr.y(), pr.width(), pr.height());
|
Chris@1079
|
317
|
Chris@1079
|
318 if (!timeConstrained && (pr != rect)) {
|
Chris@1494
|
319 QRect cva = m_cache.getValidArea();
|
Chris@1265
|
320 SVCERR << "WARNING: failed to render entire requested rect "
|
Chris@1451
|
321 << "even when not time-constrained: wanted "
|
Chris@1450
|
322 << rect.x() << "," << rect.y() << " "
|
Chris@1451
|
323 << rect.width() << "x" << rect.height() << ", got "
|
Chris@1450
|
324 << pr.x() << "," << pr.y() << " "
|
Chris@1450
|
325 << pr.width() << "x" << pr.height()
|
Chris@1451
|
326 << ", after request of width " << (x1 - x0)
|
Chris@1494
|
327 << endl
|
Chris@1494
|
328 << "(cache valid area is "
|
Chris@1494
|
329 << cva.x() << "," << cva.y() << " "
|
Chris@1494
|
330 << cva.width() << "x" << cva.height() << ")"
|
Chris@1450
|
331 << endl;
|
Chris@1079
|
332 }
|
Chris@1120
|
333
|
Chris@1122
|
334 MagnitudeRange range = m_magCache.getRange(reqx0, reqx1 - reqx0);
|
Chris@1412
|
335
|
Chris@1412
|
336 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1499
|
337 SVDEBUG << "render " << m_sources.source
|
Chris@1499
|
338 << ": returning rect rendered as " << pr.x() << "," << pr.y()
|
Chris@1412
|
339 << " " << pr.width() << "x" << pr.height() << endl;
|
Chris@1499
|
340 SVDEBUG << "render " << m_sources.source
|
Chris@1499
|
341 << ": mag range from cache in x-range " << reqx0
|
Chris@1412
|
342 << " to " << reqx1 << " is " << range.getMin() << " -> "
|
Chris@1412
|
343 << range.getMax() << endl;
|
Chris@1412
|
344 #endif
|
Chris@1120
|
345
|
Chris@1120
|
346 return { pr, range };
|
Chris@1073
|
347 }
|
Chris@1073
|
348
|
Chris@1109
|
349 Colour3DPlotRenderer::RenderType
|
Chris@1113
|
350 Colour3DPlotRenderer::decideRenderType(const LayerGeometryProvider *v) const
|
Chris@1094
|
351 {
|
Chris@1469
|
352 auto model = ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
|
Chris@1109
|
353 if (!model || !v || !(v->getViewManager())) {
|
Chris@1109
|
354 return DrawBufferPixelResolution; // or anything
|
Chris@1109
|
355 }
|
Chris@1109
|
356
|
Chris@1094
|
357 int binResolution = model->getResolution();
|
Chris@1325
|
358 ZoomLevel zoomLevel = v->getZoomLevel();
|
Chris@1109
|
359 sv_samplerate_t modelRate = model->getSampleRate();
|
Chris@1109
|
360
|
Chris@1109
|
361 double rateRatio = v->getViewManager()->getMainModelSampleRate() / modelRate;
|
Chris@1109
|
362 double relativeBinResolution = binResolution * rateRatio;
|
Chris@1109
|
363
|
Chris@1109
|
364 if (m_params.binDisplay == BinDisplay::PeakFrequencies) {
|
Chris@1109
|
365 // no alternative works here
|
Chris@1109
|
366 return DrawBufferPixelResolution;
|
Chris@1109
|
367 }
|
Chris@1109
|
368
|
Chris@1109
|
369 if (!m_params.alwaysOpaque && !m_params.interpolate) {
|
Chris@1109
|
370
|
Chris@1109
|
371 // consider translucent option -- only if not smoothing & not
|
Chris@1109
|
372 // explicitly requested opaque & sufficiently zoomed-in
|
Chris@1109
|
373
|
Chris@1117
|
374 if (model->getHeight() * 3 < v->getPaintHeight() &&
|
Chris@1325
|
375 zoomLevel < ZoomLevel(ZoomLevel::FramesPerPixel,
|
Chris@1325
|
376 int(round(relativeBinResolution / 3)))) {
|
Chris@1109
|
377 return DirectTranslucent;
|
Chris@1109
|
378 }
|
Chris@1109
|
379 }
|
Chris@1109
|
380
|
Chris@1325
|
381 if (ZoomLevel(ZoomLevel::FramesPerPixel,
|
Chris@1325
|
382 int(round(relativeBinResolution))) > zoomLevel) {
|
Chris@1109
|
383 return DrawBufferBinResolution;
|
Chris@1109
|
384 } else {
|
Chris@1109
|
385 return DrawBufferPixelResolution;
|
Chris@1109
|
386 }
|
Chris@1109
|
387 }
|
Chris@1109
|
388
|
Chris@1138
|
389 ColumnOp::Column
|
Chris@1161
|
390 Colour3DPlotRenderer::getColumn(int sx, int minbin, int nbins,
|
Chris@1502
|
391 shared_ptr<DenseThreeDimensionalModel> source) const
|
Chris@1138
|
392 {
|
Chris@1138
|
393 // order:
|
Chris@1138
|
394 // get column -> scale -> normalise -> record extents ->
|
Chris@1138
|
395 // peak pick -> distribute/interpolate -> apply display gain
|
Chris@1138
|
396
|
Chris@1138
|
397 // we do the first bit here:
|
Chris@1138
|
398 // get column -> scale -> normalise
|
Chris@1138
|
399
|
Chris@1138
|
400 ColumnOp::Column column;
|
Chris@1364
|
401
|
Chris@1364
|
402 if (m_params.showDerivative && sx > 0) {
|
Chris@1364
|
403
|
Chris@1502
|
404 auto prev = getColumnRaw(sx - 1, minbin, nbins, source);
|
Chris@1502
|
405 column = getColumnRaw(sx, minbin, nbins, source);
|
Chris@1364
|
406
|
Chris@1364
|
407 for (int i = 0; i < nbins; ++i) {
|
Chris@1364
|
408 column[i] -= prev[i];
|
Chris@1364
|
409 }
|
Chris@1364
|
410
|
Chris@1364
|
411 } else {
|
Chris@1502
|
412 column = getColumnRaw(sx, minbin, nbins, source);
|
Chris@1364
|
413 }
|
Chris@1364
|
414
|
Chris@1364
|
415 if (m_params.colourScale.getScale() == ColourScaleType::Phase &&
|
Chris@1469
|
416 !m_sources.fft.isNone()) {
|
Chris@1364
|
417 return column;
|
Chris@1364
|
418 } else {
|
Chris@1364
|
419 column = ColumnOp::applyGain(column, m_params.scaleFactor);
|
Chris@1364
|
420 column = ColumnOp::normalize(column, m_params.normalization);
|
Chris@1364
|
421 return column;
|
Chris@1364
|
422 }
|
Chris@1364
|
423 }
|
Chris@1364
|
424
|
Chris@1364
|
425 ColumnOp::Column
|
Chris@1364
|
426 Colour3DPlotRenderer::getColumnRaw(int sx, int minbin, int nbins,
|
Chris@1502
|
427 shared_ptr<DenseThreeDimensionalModel> source) const
|
Chris@1364
|
428 {
|
Chris@1364
|
429 Profiler profiler("Colour3DPlotRenderer::getColumn");
|
Chris@1364
|
430
|
Chris@1364
|
431 ColumnOp::Column column;
|
Chris@1469
|
432 ColumnOp::Column fullColumn;
|
Chris@1364
|
433
|
Chris@1469
|
434 if (m_params.colourScale.getScale() == ColourScaleType::Phase) {
|
Chris@1469
|
435 auto fftModel = ModelById::getAs<FFTModel>(m_sources.fft);
|
Chris@1469
|
436 if (fftModel) {
|
Chris@1469
|
437 fullColumn = fftModel->getPhases(sx);
|
Chris@1469
|
438 }
|
Chris@1138
|
439 }
|
Chris@1138
|
440
|
Chris@1469
|
441 if (fullColumn.empty()) {
|
Chris@1502
|
442 fullColumn = source->getColumn(sx);
|
Chris@1469
|
443 }
|
Chris@1502
|
444
|
Chris@1469
|
445 column = vector<float>(fullColumn.data() + minbin,
|
Chris@1469
|
446 fullColumn.data() + minbin + nbins);
|
Chris@1138
|
447 return column;
|
Chris@1138
|
448 }
|
Chris@1138
|
449
|
Chris@1121
|
450 MagnitudeRange
|
Chris@1113
|
451 Colour3DPlotRenderer::renderDirectTranslucent(const LayerGeometryProvider *v,
|
Chris@1109
|
452 QPainter &paint,
|
Chris@1109
|
453 QRect rect)
|
Chris@1109
|
454 {
|
Chris@1117
|
455 Profiler profiler("Colour3DPlotRenderer::renderDirectTranslucent");
|
Chris@1117
|
456
|
Chris@1121
|
457 MagnitudeRange magRange;
|
Chris@1121
|
458
|
Chris@1115
|
459 QPoint illuminatePos;
|
Chris@1115
|
460 bool illuminate = v->shouldIlluminateLocalFeatures
|
Chris@1115
|
461 (m_sources.verticalBinLayer, illuminatePos);
|
Chris@1109
|
462
|
Chris@1469
|
463 auto model = ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
|
Chris@1469
|
464 if (!model) return magRange;
|
Chris@1109
|
465
|
Chris@1109
|
466 int x0 = rect.left();
|
Chris@1109
|
467 int x1 = rect.right() + 1;
|
Chris@1109
|
468
|
Chris@1109
|
469 int h = v->getPaintHeight();
|
Chris@1109
|
470
|
Chris@1109
|
471 sv_frame_t modelStart = model->getStartFrame();
|
Chris@1109
|
472 sv_frame_t modelEnd = model->getEndFrame();
|
Chris@1109
|
473 int modelResolution = model->getResolution();
|
Chris@1109
|
474
|
Chris@1109
|
475 double rateRatio =
|
Chris@1109
|
476 v->getViewManager()->getMainModelSampleRate() / model->getSampleRate();
|
Chris@1109
|
477
|
Chris@1109
|
478 // the s-prefix values are source, i.e. model, column and bin numbers
|
Chris@1109
|
479 int sx0 = int((double(v->getFrameForX(x0)) / rateRatio - double(modelStart))
|
Chris@1109
|
480 / modelResolution);
|
Chris@1109
|
481 int sx1 = int((double(v->getFrameForX(x1)) / rateRatio - double(modelStart))
|
Chris@1109
|
482 / modelResolution);
|
Chris@1109
|
483
|
Chris@1109
|
484 int sh = model->getHeight();
|
Chris@1109
|
485
|
Chris@1109
|
486 const int buflen = 40;
|
Chris@1109
|
487 char labelbuf[buflen];
|
Chris@1109
|
488
|
Chris@1133
|
489 int minbin = m_sources.verticalBinLayer->getIBinForY(v, h);
|
Chris@1135
|
490 if (minbin >= sh) minbin = sh - 1;
|
Chris@1135
|
491 if (minbin < 0) minbin = 0;
|
Chris@1135
|
492
|
Chris@1135
|
493 int nbins = m_sources.verticalBinLayer->getIBinForY(v, 0) - minbin + 1;
|
Chris@1135
|
494 if (minbin + nbins > sh) nbins = sh - minbin;
|
Chris@1133
|
495
|
Chris@1109
|
496 int psx = -1;
|
Chris@1109
|
497
|
Chris@1109
|
498 vector<float> preparedColumn;
|
Chris@1109
|
499
|
Chris@1109
|
500 int modelWidth = model->getWidth();
|
Chris@1109
|
501
|
Chris@1109
|
502 for (int sx = sx0; sx <= sx1; ++sx) {
|
Chris@1109
|
503
|
Chris@1109
|
504 if (sx < 0 || sx >= modelWidth) {
|
Chris@1109
|
505 continue;
|
Chris@1109
|
506 }
|
Chris@1109
|
507
|
Chris@1109
|
508 if (sx != psx) {
|
Chris@1109
|
509
|
Chris@1138
|
510 // order:
|
Chris@1138
|
511 // get column -> scale -> normalise -> record extents ->
|
Chris@1138
|
512 // peak pick -> distribute/interpolate -> apply display gain
|
Chris@1109
|
513
|
Chris@1138
|
514 // this does the first three:
|
Chris@1502
|
515 preparedColumn = getColumn(sx, minbin, nbins, model);
|
Chris@1131
|
516
|
Chris@1131
|
517 magRange.sample(preparedColumn);
|
Chris@1109
|
518
|
Chris@1109
|
519 if (m_params.binDisplay == BinDisplay::PeakBins) {
|
Chris@1115
|
520 preparedColumn = ColumnOp::peakPick(preparedColumn);
|
Chris@1109
|
521 }
|
Chris@1109
|
522
|
Chris@1124
|
523 // Display gain belongs to the colour scale and is
|
Chris@1124
|
524 // applied by the colour scale object when mapping it
|
Chris@1124
|
525
|
Chris@1109
|
526 psx = sx;
|
Chris@1109
|
527 }
|
Chris@1109
|
528
|
Chris@1266
|
529 sv_frame_t fx = sx * modelResolution + modelStart;
|
Chris@1109
|
530
|
Chris@1266
|
531 if (fx + modelResolution <= modelStart || fx > modelEnd) continue;
|
Chris@1109
|
532
|
Chris@1109
|
533 int rx0 = v->getXForFrame(int(double(fx) * rateRatio));
|
Chris@1266
|
534 int rx1 = v->getXForFrame(int(double(fx + modelResolution + 1) * rateRatio));
|
Chris@1109
|
535
|
Chris@1266
|
536 int rw = rx1 - rx0;
|
Chris@1266
|
537 if (rw < 1) rw = 1;
|
Chris@1109
|
538
|
Chris@1471
|
539 // Qt 5.13 deprecates QFontMetrics::width(), but its suggested
|
Chris@1471
|
540 // replacement (horizontalAdvance) was only added in Qt 5.11
|
Chris@1471
|
541 // which is too new for us
|
Chris@1471
|
542 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
Chris@1471
|
543
|
Chris@1266
|
544 bool showLabel = (rw > 10 &&
|
Chris@1266
|
545 paint.fontMetrics().width("0.000000") < rw - 3 &&
|
Chris@1266
|
546 paint.fontMetrics().height() < (h / sh));
|
Chris@1109
|
547
|
Chris@1266
|
548 for (int sy = minbin; sy < minbin + nbins; ++sy) {
|
Chris@1109
|
549
|
Chris@1109
|
550 int ry0 = m_sources.verticalBinLayer->getIYForBin(v, sy);
|
Chris@1109
|
551 int ry1 = m_sources.verticalBinLayer->getIYForBin(v, sy + 1);
|
Chris@1116
|
552
|
Chris@1116
|
553 if (m_params.invertVertical) {
|
Chris@1116
|
554 ry0 = h - ry0 - 1;
|
Chris@1116
|
555 ry1 = h - ry1 - 1;
|
Chris@1116
|
556 }
|
Chris@1116
|
557
|
Chris@1109
|
558 QRect r(rx0, ry1, rw, ry0 - ry1);
|
Chris@1109
|
559
|
Chris@1109
|
560 float value = preparedColumn[sy - minbin];
|
Chris@1112
|
561 QColor colour = m_params.colourScale.getColour(value,
|
Chris@1112
|
562 m_params.colourRotation);
|
Chris@1109
|
563
|
Chris@1109
|
564 if (rw == 1) {
|
Chris@1109
|
565 paint.setPen(colour);
|
Chris@1109
|
566 paint.setBrush(Qt::NoBrush);
|
Chris@1109
|
567 paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1);
|
Chris@1109
|
568 continue;
|
Chris@1109
|
569 }
|
Chris@1109
|
570
|
Chris@1266
|
571 QColor pen(255, 255, 255, 80);
|
Chris@1266
|
572 QColor brush(colour);
|
Chris@1109
|
573
|
Chris@1109
|
574 if (rw > 3 && r.height() > 3) {
|
Chris@1109
|
575 brush.setAlpha(160);
|
Chris@1109
|
576 }
|
Chris@1109
|
577
|
Chris@1266
|
578 paint.setPen(Qt::NoPen);
|
Chris@1266
|
579 paint.setBrush(brush);
|
Chris@1109
|
580
|
Chris@1266
|
581 if (illuminate) {
|
Chris@1266
|
582 if (r.contains(illuminatePos)) {
|
Chris@1266
|
583 paint.setPen(v->getForeground());
|
Chris@1266
|
584 }
|
Chris@1266
|
585 }
|
Chris@1109
|
586
|
Chris@1214
|
587 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1214
|
588 // SVDEBUG << "rect " << r.x() << "," << r.y() << " "
|
Chris@1109
|
589 // << r.width() << "x" << r.height() << endl;
|
Chris@1109
|
590 #endif
|
Chris@1109
|
591
|
Chris@1266
|
592 paint.drawRect(r);
|
Chris@1109
|
593
|
Chris@1266
|
594 if (showLabel) {
|
Chris@1109
|
595 double value = model->getValueAt(sx, sy);
|
Chris@1109
|
596 snprintf(labelbuf, buflen, "%06f", value);
|
Chris@1109
|
597 QString text(labelbuf);
|
Chris@1109
|
598 PaintAssistant::drawVisibleText
|
Chris@1109
|
599 (v,
|
Chris@1109
|
600 paint,
|
Chris@1109
|
601 rx0 + 2,
|
Chris@1109
|
602 ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(),
|
Chris@1109
|
603 text,
|
Chris@1109
|
604 PaintAssistant::OutlinedText);
|
Chris@1266
|
605 }
|
Chris@1266
|
606 }
|
Chris@1109
|
607 }
|
Chris@1121
|
608
|
Chris@1121
|
609 return magRange;
|
Chris@1094
|
610 }
|
Chris@1094
|
611
|
Chris@1080
|
612 void
|
Chris@1213
|
613 Colour3DPlotRenderer::getPreferredPeakCache(const LayerGeometryProvider *v,
|
Chris@1213
|
614 int &peakCacheIndex,
|
Chris@1213
|
615 int &binsPerPeak) const
|
Chris@1213
|
616 {
|
Chris@1213
|
617 peakCacheIndex = -1;
|
Chris@1213
|
618 binsPerPeak = -1;
|
Chris@1213
|
619
|
Chris@1469
|
620 auto model = ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
|
Chris@1213
|
621 if (!model) return;
|
Chris@1217
|
622 if (m_params.binDisplay == BinDisplay::PeakFrequencies) return;
|
Chris@1217
|
623 if (m_params.colourScale.getScale() == ColourScaleType::Phase) return;
|
Chris@1213
|
624
|
Chris@1325
|
625 ZoomLevel zoomLevel = v->getZoomLevel();
|
Chris@1213
|
626 int binResolution = model->getResolution();
|
Chris@1213
|
627
|
Chris@1213
|
628 for (int ix = 0; in_range_for(m_sources.peakCaches, ix); ++ix) {
|
Chris@1473
|
629 auto peakCache = ModelById::getAs<Dense3DModelPeakCache>
|
Chris@1473
|
630 (m_sources.peakCaches[ix]);
|
Chris@1473
|
631 if (!peakCache) continue;
|
Chris@1473
|
632 int bpp = peakCache->getColumnsPerPeak();
|
Chris@1325
|
633 ZoomLevel equivZoom(ZoomLevel::FramesPerPixel, binResolution * bpp);
|
Chris@1502
|
634 #ifdef DEBUG_COLOUR_PLOT_CACHE_SELECTION
|
Chris@1500
|
635 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
636 << ": getPreferredPeakCache: zoomLevel = " << zoomLevel
|
Chris@1450
|
637 << ", cache " << ix << " has bpp = " << bpp
|
Chris@1450
|
638 << " for equivZoom = " << equivZoom << endl;
|
Chris@1450
|
639 #endif
|
Chris@1213
|
640 if (zoomLevel >= equivZoom) {
|
Chris@1213
|
641 // this peak cache would work, though it might not be best
|
Chris@1213
|
642 if (bpp > binsPerPeak) {
|
Chris@1213
|
643 // ok, it's better than the best one we've found so far
|
Chris@1213
|
644 peakCacheIndex = ix;
|
Chris@1213
|
645 binsPerPeak = bpp;
|
Chris@1213
|
646 }
|
Chris@1213
|
647 }
|
Chris@1213
|
648 }
|
Chris@1213
|
649
|
Chris@1502
|
650 #ifdef DEBUG_COLOUR_PLOT_CACHE_SELECTION
|
Chris@1499
|
651 SVDEBUG << "render " << m_sources.source
|
Chris@1499
|
652 << ": getPreferredPeakCache: zoomLevel = " << zoomLevel
|
Chris@1213
|
653 << ", binResolution " << binResolution
|
Chris@1213
|
654 << ", peakCaches " << m_sources.peakCaches.size()
|
Chris@1450
|
655 << ": preferring peakCacheIndex " << peakCacheIndex
|
Chris@1450
|
656 << " for binsPerPeak " << binsPerPeak
|
Chris@1213
|
657 << endl;
|
Chris@1214
|
658 #endif
|
Chris@1213
|
659 }
|
Chris@1213
|
660
|
Chris@1213
|
661 void
|
Chris@1113
|
662 Colour3DPlotRenderer::renderToCachePixelResolution(const LayerGeometryProvider *v,
|
Chris@1094
|
663 int x0, int repaintWidth,
|
Chris@1094
|
664 bool rightToLeft,
|
Chris@1094
|
665 bool timeConstrained)
|
Chris@1079
|
666 {
|
Chris@1117
|
667 Profiler profiler("Colour3DPlotRenderer::renderToCachePixelResolution");
|
Chris@1143
|
668 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1499
|
669 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
670 << ": [PIXEL] renderToCachePixelResolution" << endl;
|
Chris@1143
|
671 #endif
|
Chris@1094
|
672
|
Chris@1094
|
673 // Draw to the draw buffer, and then copy from there. The draw
|
Chris@1094
|
674 // buffer is at the same resolution as the target in the cache, so
|
Chris@1094
|
675 // no extra scaling needed.
|
Chris@1079
|
676
|
Chris@1469
|
677 auto model = ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
|
Chris@1469
|
678 if (!model) return;
|
Chris@1079
|
679
|
Chris@1079
|
680 int h = v->getPaintHeight();
|
Chris@1079
|
681
|
Chris@1094
|
682 clearDrawBuffer(repaintWidth, h);
|
Chris@1079
|
683
|
Chris@1094
|
684 vector<int> binforx(repaintWidth);
|
Chris@1079
|
685 vector<double> binfory(h);
|
Chris@1079
|
686
|
Chris@1094
|
687 int binResolution = model->getResolution();
|
Chris@1079
|
688
|
Chris@1094
|
689 for (int x = 0; x < repaintWidth; ++x) {
|
Chris@1094
|
690 sv_frame_t f0 = v->getFrameForX(x0 + x);
|
Chris@1094
|
691 double s0 = double(f0 - model->getStartFrame()) / binResolution;
|
Chris@1094
|
692 binforx[x] = int(s0 + 0.0001);
|
Chris@1094
|
693 }
|
Chris@1080
|
694
|
Chris@1212
|
695 int peakCacheIndex = -1;
|
Chris@1212
|
696 int binsPerPeak = -1;
|
Chris@1212
|
697
|
Chris@1217
|
698 getPreferredPeakCache(v, peakCacheIndex, binsPerPeak);
|
Chris@1094
|
699
|
Chris@1080
|
700 for (int y = 0; y < h; ++y) {
|
Chris@1090
|
701 binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1);
|
Chris@1080
|
702 }
|
Chris@1079
|
703
|
Chris@1097
|
704 int attainedWidth;
|
Chris@1097
|
705
|
Chris@1103
|
706 if (m_params.binDisplay == BinDisplay::PeakFrequencies) {
|
Chris@1097
|
707 attainedWidth = renderDrawBufferPeakFrequencies(v,
|
Chris@1097
|
708 repaintWidth,
|
Chris@1097
|
709 h,
|
Chris@1097
|
710 binforx,
|
Chris@1097
|
711 binfory,
|
Chris@1097
|
712 rightToLeft,
|
Chris@1097
|
713 timeConstrained);
|
Chris@1097
|
714
|
Chris@1097
|
715 } else {
|
Chris@1097
|
716 attainedWidth = renderDrawBuffer(repaintWidth,
|
Chris@1080
|
717 h,
|
Chris@1080
|
718 binforx,
|
Chris@1080
|
719 binfory,
|
Chris@1212
|
720 peakCacheIndex,
|
Chris@1080
|
721 rightToLeft,
|
Chris@1080
|
722 timeConstrained);
|
Chris@1097
|
723 }
|
Chris@1083
|
724
|
Chris@1094
|
725 if (attainedWidth == 0) return;
|
Chris@1084
|
726
|
Chris@1094
|
727 // draw buffer is pixel resolution, no scaling factors or padding involved
|
Chris@1084
|
728
|
Chris@1084
|
729 int paintedLeft = x0;
|
Chris@1084
|
730 if (rightToLeft) {
|
Chris@1084
|
731 paintedLeft += (repaintWidth - attainedWidth);
|
Chris@1084
|
732 }
|
Chris@1084
|
733
|
Chris@1094
|
734 m_cache.drawImage(paintedLeft, attainedWidth,
|
Chris@1094
|
735 m_drawBuffer,
|
Chris@1094
|
736 paintedLeft - x0, attainedWidth);
|
Chris@1121
|
737
|
Chris@1121
|
738 for (int i = 0; in_range_for(m_magRanges, i); ++i) {
|
Chris@1121
|
739 m_magCache.sampleColumn(i, m_magRanges.at(i));
|
Chris@1121
|
740 }
|
Chris@1094
|
741 }
|
Chris@1084
|
742
|
Chris@1167
|
743 QImage
|
Chris@1167
|
744 Colour3DPlotRenderer::scaleDrawBufferImage(QImage image,
|
Chris@1167
|
745 int targetWidth,
|
Chris@1167
|
746 int targetHeight) const
|
Chris@1167
|
747 {
|
Chris@1167
|
748 int sourceWidth = image.width();
|
Chris@1167
|
749 int sourceHeight = image.height();
|
Chris@1167
|
750
|
Chris@1167
|
751 // We can only do this if we're making the image larger --
|
Chris@1167
|
752 // otherwise peaks may be lost. So this should be called only when
|
Chris@1167
|
753 // rendering in DrawBufferBinResolution mode. Whenever the bin
|
Chris@1167
|
754 // size is smaller than the pixel size, in either x or y axis, we
|
Chris@1167
|
755 // should be using DrawBufferPixelResolution mode instead
|
Chris@1167
|
756
|
Chris@1167
|
757 if (targetWidth < sourceWidth || targetHeight < sourceHeight) {
|
Chris@1167
|
758 throw std::logic_error("Colour3DPlotRenderer::scaleDrawBufferImage: Can only use this function when making the image larger; should be rendering DrawBufferPixelResolution instead");
|
Chris@1167
|
759 }
|
Chris@1167
|
760
|
Chris@1167
|
761 if (sourceWidth <= 0 || sourceHeight <= 0) {
|
Chris@1167
|
762 throw std::logic_error("Colour3DPlotRenderer::scaleDrawBufferImage: Source image is empty");
|
Chris@1167
|
763 }
|
Chris@1167
|
764
|
Chris@1167
|
765 if (targetWidth <= 0 || targetHeight <= 0) {
|
Chris@1167
|
766 throw std::logic_error("Colour3DPlotRenderer::scaleDrawBufferImage: Target image is empty");
|
Chris@1167
|
767 }
|
Chris@1167
|
768
|
Chris@1167
|
769 // This function exists because of some unpredictable behaviour
|
Chris@1167
|
770 // from Qt when scaling images with FastTransformation mode. We
|
Chris@1167
|
771 // continue to use Qt's scaler for SmoothTransformation but let's
|
Chris@1167
|
772 // bring the non-interpolated version "in-house" so we know what
|
Chris@1167
|
773 // it's really doing.
|
Chris@1167
|
774
|
Chris@1167
|
775 if (m_params.interpolate) {
|
Chris@1167
|
776 return image.scaled(targetWidth, targetHeight,
|
Chris@1167
|
777 Qt::IgnoreAspectRatio,
|
Chris@1167
|
778 Qt::SmoothTransformation);
|
Chris@1167
|
779 }
|
Chris@1167
|
780
|
Chris@1167
|
781 // Same format as the target cache
|
Chris@1167
|
782 QImage target(targetWidth, targetHeight, QImage::Format_ARGB32_Premultiplied);
|
Chris@1167
|
783
|
Chris@1167
|
784 for (int y = 0; y < targetHeight; ++y) {
|
Chris@1167
|
785
|
Chris@1167
|
786 QRgb *targetLine = reinterpret_cast<QRgb *>(target.scanLine(y));
|
Chris@1167
|
787
|
Chris@1167
|
788 int sy = int((uint64_t(y) * sourceHeight) / targetHeight);
|
Chris@1167
|
789 if (sy == sourceHeight) --sy;
|
Chris@1167
|
790
|
Chris@1167
|
791 for (int x = 0; x < targetWidth; ++x) {
|
Chris@1167
|
792
|
Chris@1167
|
793 int sx = int((uint64_t(x) * sourceWidth) / targetWidth);
|
Chris@1167
|
794 if (sx == sourceWidth) --sx;
|
Chris@1167
|
795
|
Chris@1167
|
796 targetLine[x] = image.pixel(sx, sy);
|
Chris@1167
|
797 }
|
Chris@1167
|
798 }
|
Chris@1167
|
799
|
Chris@1167
|
800 return target;
|
Chris@1167
|
801 }
|
Chris@1167
|
802
|
Chris@1094
|
803 void
|
Chris@1113
|
804 Colour3DPlotRenderer::renderToCacheBinResolution(const LayerGeometryProvider *v,
|
Chris@1094
|
805 int x0, int repaintWidth)
|
Chris@1094
|
806 {
|
Chris@1117
|
807 Profiler profiler("Colour3DPlotRenderer::renderToCacheBinResolution");
|
Chris@1143
|
808 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1499
|
809 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
810 << ": [BIN] renderToCacheBinResolution" << endl;
|
Chris@1143
|
811 #endif
|
Chris@1094
|
812
|
Chris@1094
|
813 // Draw to the draw buffer, and then scale-copy from there. Draw
|
Chris@1094
|
814 // buffer is at bin resolution, i.e. buffer x == source column
|
Chris@1094
|
815 // number. We use toolkit smooth scaling for interpolation.
|
Chris@1084
|
816
|
Chris@1469
|
817 auto model = ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
|
Chris@1469
|
818 if (!model) return;
|
Chris@1094
|
819
|
Chris@1094
|
820 // The draw buffer will contain a fragment at bin resolution. We
|
Chris@1094
|
821 // need to ensure that it starts and ends at points where a
|
Chris@1094
|
822 // time-bin boundary occurs at an exact pixel boundary, and with a
|
Chris@1094
|
823 // certain amount of overlap across existing pixels so that we can
|
Chris@1094
|
824 // scale and draw from it without smoothing errors at the edges.
|
Chris@1094
|
825
|
Chris@1094
|
826 // If (getFrameForX(x) / increment) * increment ==
|
Chris@1094
|
827 // getFrameForX(x), then x is a time-bin boundary. We want two
|
Chris@1094
|
828 // such boundaries at either side of the draw buffer -- one which
|
Chris@1094
|
829 // we draw up to, and one which we subsequently crop at.
|
Chris@1094
|
830
|
Chris@1094
|
831 sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1;
|
Chris@1094
|
832 sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1;
|
Chris@1094
|
833
|
Chris@1094
|
834 int drawBufferWidth;
|
Chris@1094
|
835 int binResolution = model->getResolution();
|
Chris@1094
|
836
|
Chris@1330
|
837 // These loops should eventually terminate provided that
|
Chris@1330
|
838 // getFrameForX always returns a multiple of the zoom level,
|
Chris@1330
|
839 // i.e. there is some x for which getFrameForX(x) == 0 and
|
Chris@1330
|
840 // subsequent return values are equally spaced
|
Chris@1329
|
841
|
Chris@1330
|
842 for (int x = x0; ; --x) {
|
Chris@1094
|
843 sv_frame_t f = v->getFrameForX(x);
|
Chris@1330
|
844 if ((f / binResolution) * binResolution == f) {
|
Chris@1094
|
845 if (leftCropFrame == -1) leftCropFrame = f;
|
Chris@1094
|
846 else if (x < x0 - 2) {
|
Chris@1094
|
847 leftBoundaryFrame = f;
|
Chris@1094
|
848 break;
|
Chris@1094
|
849 }
|
Chris@1094
|
850 }
|
Chris@1094
|
851 }
|
Chris@1329
|
852
|
Chris@1330
|
853 for (int x = x0 + repaintWidth; ; ++x) {
|
Chris@1094
|
854 sv_frame_t f = v->getFrameForX(x);
|
Chris@1330
|
855 if ((f / binResolution) * binResolution == f) {
|
Chris@1494
|
856 if (v->getXForFrame(f) < x0 + repaintWidth) {
|
Chris@1494
|
857 continue;
|
Chris@1494
|
858 }
|
Chris@1094
|
859 if (rightCropFrame == -1) rightCropFrame = f;
|
Chris@1094
|
860 else if (x > x0 + repaintWidth + 2) {
|
Chris@1094
|
861 rightBoundaryFrame = f;
|
Chris@1094
|
862 break;
|
Chris@1094
|
863 }
|
Chris@1094
|
864 }
|
Chris@1094
|
865 }
|
Chris@1329
|
866
|
Chris@1094
|
867 drawBufferWidth = int
|
Chris@1094
|
868 ((rightBoundaryFrame - leftBoundaryFrame) / binResolution);
|
Chris@1094
|
869
|
Chris@1094
|
870 int h = v->getPaintHeight();
|
Chris@1094
|
871
|
Chris@1095
|
872 // For our purposes here, the draw buffer needs to be exactly our
|
Chris@1095
|
873 // target size (so we recreate always rather than just clear it)
|
Chris@1095
|
874
|
Chris@1095
|
875 recreateDrawBuffer(drawBufferWidth, h);
|
Chris@1094
|
876
|
Chris@1094
|
877 vector<int> binforx(drawBufferWidth);
|
Chris@1094
|
878 vector<double> binfory(h);
|
Chris@1094
|
879
|
Chris@1094
|
880 for (int x = 0; x < drawBufferWidth; ++x) {
|
Chris@1094
|
881 binforx[x] = int(leftBoundaryFrame / binResolution) + x;
|
Chris@1094
|
882 }
|
Chris@1094
|
883
|
Chris@1214
|
884 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
885 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
886 << ": binResolution " << binResolution << endl;
|
Chris@1214
|
887 #endif
|
Chris@1094
|
888
|
Chris@1094
|
889 for (int y = 0; y < h; ++y) {
|
Chris@1094
|
890 binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1);
|
Chris@1094
|
891 }
|
Chris@1094
|
892
|
Chris@1094
|
893 int attainedWidth = renderDrawBuffer(drawBufferWidth,
|
Chris@1094
|
894 h,
|
Chris@1094
|
895 binforx,
|
Chris@1094
|
896 binfory,
|
Chris@1212
|
897 -1,
|
Chris@1094
|
898 false,
|
Chris@1094
|
899 false);
|
Chris@1094
|
900
|
Chris@1094
|
901 if (attainedWidth == 0) return;
|
Chris@1094
|
902
|
Chris@1094
|
903 int scaledLeft = v->getXForFrame(leftBoundaryFrame);
|
Chris@1094
|
904 int scaledRight = v->getXForFrame(rightBoundaryFrame);
|
Chris@1095
|
905
|
Chris@1143
|
906 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
907 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
908 << ": scaling draw buffer from width " << m_drawBuffer.width()
|
Chris@1494
|
909 << " to " << (scaledRight - scaledLeft)
|
Chris@1494
|
910 << " (nb drawBufferWidth = "
|
Chris@1494
|
911 << drawBufferWidth << ", attainedWidth = "
|
Chris@1494
|
912 << attainedWidth << ")" << endl;
|
Chris@1143
|
913 #endif
|
Chris@1167
|
914
|
Chris@1167
|
915 QImage scaled = scaleDrawBufferImage
|
Chris@1167
|
916 (m_drawBuffer, scaledRight - scaledLeft, h);
|
Chris@1084
|
917
|
Chris@1094
|
918 int scaledLeftCrop = v->getXForFrame(leftCropFrame);
|
Chris@1094
|
919 int scaledRightCrop = v->getXForFrame(rightCropFrame);
|
Chris@1094
|
920
|
Chris@1094
|
921 int targetLeft = scaledLeftCrop;
|
Chris@1094
|
922 if (targetLeft < 0) {
|
Chris@1094
|
923 targetLeft = 0;
|
Chris@1094
|
924 }
|
Chris@1094
|
925
|
Chris@1094
|
926 int targetWidth = scaledRightCrop - targetLeft;
|
Chris@1094
|
927 if (targetLeft + targetWidth > m_cache.getSize().width()) {
|
Chris@1094
|
928 targetWidth = m_cache.getSize().width() - targetLeft;
|
Chris@1094
|
929 }
|
Chris@1094
|
930
|
Chris@1094
|
931 int sourceLeft = targetLeft - scaledLeft;
|
Chris@1094
|
932 if (sourceLeft < 0) {
|
Chris@1094
|
933 sourceLeft = 0;
|
Chris@1094
|
934 }
|
Chris@1494
|
935
|
Chris@1494
|
936 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
937 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
938 << ": leftBoundaryFrame = " << leftBoundaryFrame
|
Chris@1494
|
939 << ", leftCropFrame = " << leftCropFrame
|
Chris@1494
|
940 << ", scaledLeft = " << scaledLeft
|
Chris@1494
|
941 << ", scaledLeftCrop = " << scaledLeftCrop
|
Chris@1494
|
942 << endl;
|
Chris@1500
|
943 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
944 << ": rightBoundaryFrame = " << rightBoundaryFrame
|
Chris@1494
|
945 << ", rightCropFrame = " << rightCropFrame
|
Chris@1494
|
946 << ", scaledRight = " << scaledRight
|
Chris@1494
|
947 << ", scaledRightCrop = " << scaledRightCrop
|
Chris@1494
|
948 << endl;
|
Chris@1494
|
949 #endif
|
Chris@1094
|
950
|
Chris@1143
|
951 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
952 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
953 << ": x0 = " << x0
|
Chris@1494
|
954 << ", repaintWidth = " << repaintWidth
|
Chris@1494
|
955 << ", targetLeft = " << targetLeft
|
Chris@1214
|
956 << ", targetWidth = " << targetWidth << endl;
|
Chris@1143
|
957 #endif
|
Chris@1094
|
958
|
Chris@1094
|
959 if (targetWidth > 0) {
|
Chris@1136
|
960 // we are copying from an image that has already been scaled,
|
Chris@1136
|
961 // hence using the same width in both geometries
|
Chris@1094
|
962 m_cache.drawImage(targetLeft, targetWidth,
|
Chris@1094
|
963 scaled,
|
Chris@1136
|
964 sourceLeft, targetWidth);
|
Chris@1084
|
965 }
|
Chris@1121
|
966
|
Chris@1121
|
967 for (int i = 0; i < targetWidth; ++i) {
|
Chris@1136
|
968 // but the mag range vector has not been scaled
|
Chris@1136
|
969 int sourceIx = int((double(i + sourceLeft) / scaled.width())
|
Chris@1136
|
970 * int(m_magRanges.size()));
|
Chris@1121
|
971 if (in_range_for(m_magRanges, sourceIx)) {
|
Chris@1121
|
972 m_magCache.sampleColumn(i, m_magRanges.at(sourceIx));
|
Chris@1121
|
973 }
|
Chris@1121
|
974 }
|
Chris@1079
|
975 }
|
Chris@1083
|
976
|
Chris@1083
|
977 int
|
Chris@1083
|
978 Colour3DPlotRenderer::renderDrawBuffer(int w, int h,
|
Chris@1083
|
979 const vector<int> &binforx,
|
Chris@1083
|
980 const vector<double> &binfory,
|
Chris@1212
|
981 int peakCacheIndex,
|
Chris@1083
|
982 bool rightToLeft,
|
Chris@1083
|
983 bool timeConstrained)
|
Chris@1083
|
984 {
|
Chris@1083
|
985 // Callers must have checked that the appropriate subset of
|
Chris@1083
|
986 // Sources data members are set for the supplied flags (e.g. that
|
Chris@1212
|
987 // peakCache corresponding to peakCacheIndex exists)
|
Chris@1083
|
988
|
Chris@1083
|
989 RenderTimer timer(timeConstrained ?
|
Chris@1083
|
990 RenderTimer::FastRender :
|
Chris@1083
|
991 RenderTimer::NoTimeout);
|
Chris@1083
|
992
|
Chris@1164
|
993 Profiler profiler("Colour3DPlotRenderer::renderDrawBuffer");
|
Chris@1164
|
994
|
Chris@1083
|
995 int divisor = 1;
|
Chris@1473
|
996
|
Chris@1473
|
997 std::shared_ptr<DenseThreeDimensionalModel> sourceModel;
|
Chris@1473
|
998
|
Chris@1473
|
999 if (peakCacheIndex >= 0) {
|
Chris@1473
|
1000 auto peakCache = ModelById::getAs<Dense3DModelPeakCache>
|
Chris@1473
|
1001 (m_sources.peakCaches[peakCacheIndex]);
|
Chris@1473
|
1002 if (peakCache) {
|
Chris@1473
|
1003 divisor = peakCache->getColumnsPerPeak();
|
Chris@1473
|
1004 sourceModel = peakCache;
|
Chris@1473
|
1005 }
|
Chris@1473
|
1006 }
|
Chris@1473
|
1007
|
Chris@1473
|
1008 if (!sourceModel) {
|
Chris@1473
|
1009 sourceModel = ModelById::getAs<DenseThreeDimensionalModel>
|
Chris@1473
|
1010 (m_sources.source);
|
Chris@1473
|
1011 }
|
Chris@1469
|
1012
|
Chris@1473
|
1013 if (!sourceModel) return 0;
|
Chris@1083
|
1014
|
Chris@1214
|
1015 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1016 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1017 << ": renderDrawBuffer: w = " << w << ", h = " << h
|
Chris@1212
|
1018 << ", peakCacheIndex = " << peakCacheIndex << " (divisor = "
|
cannam@1171
|
1019 << divisor << "), rightToLeft = " << rightToLeft
|
cannam@1171
|
1020 << ", timeConstrained = " << timeConstrained << endl;
|
Chris@1500
|
1021 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1022 << ": renderDrawBuffer: normalization = " << int(m_params.normalization)
|
cannam@1171
|
1023 << ", binDisplay = " << int(m_params.binDisplay)
|
cannam@1171
|
1024 << ", binScale = " << int(m_params.binScale)
|
cannam@1171
|
1025 << ", alwaysOpaque = " << m_params.alwaysOpaque
|
cannam@1171
|
1026 << ", interpolate = " << m_params.interpolate << endl;
|
Chris@1214
|
1027 #endif
|
cannam@1171
|
1028
|
Chris@1135
|
1029 int sh = sourceModel->getHeight();
|
Chris@1135
|
1030
|
Chris@1135
|
1031 int minbin = int(binfory[0] + 0.0001);
|
Chris@1135
|
1032 if (minbin >= sh) minbin = sh - 1;
|
Chris@1135
|
1033 if (minbin < 0) minbin = 0;
|
Chris@1135
|
1034
|
Chris@1170
|
1035 int nbins = int(binfory[h-1] + 0.0001) - minbin + 1;
|
Chris@1135
|
1036 if (minbin + nbins > sh) nbins = sh - minbin;
|
Chris@1162
|
1037
|
Chris@1162
|
1038 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1039 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1040 << ": minbin = " << minbin << ", nbins = " << nbins
|
Chris@1500
|
1041 << ", last binfory = " << binfory[h-1]
|
Chris@1500
|
1042 << " (rounds to " << int(binfory[h-1])
|
Chris@1500
|
1043 << ") (model height " << sh << ")" << endl;
|
Chris@1162
|
1044 #endif
|
Chris@1135
|
1045
|
Chris@1083
|
1046 int psx = -1;
|
Chris@1083
|
1047
|
Chris@1083
|
1048 int start = 0;
|
Chris@1083
|
1049 int finish = w;
|
Chris@1083
|
1050 int step = 1;
|
Chris@1083
|
1051
|
Chris@1083
|
1052 if (rightToLeft) {
|
Chris@1083
|
1053 start = w-1;
|
Chris@1083
|
1054 finish = -1;
|
Chris@1083
|
1055 step = -1;
|
Chris@1083
|
1056 }
|
Chris@1083
|
1057
|
Chris@1221
|
1058 int xPixelCount = 0;
|
Chris@1083
|
1059
|
Chris@1083
|
1060 vector<float> preparedColumn;
|
Chris@1094
|
1061
|
Chris@1094
|
1062 int modelWidth = sourceModel->getWidth();
|
Chris@1121
|
1063
|
Chris@1143
|
1064 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1065 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1066 << ": modelWidth " << modelWidth << ", divisor " << divisor << endl;
|
Chris@1500
|
1067 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1068 << ": start = " << start << ", finish = " << finish << ", step = " << step << endl;
|
Chris@1143
|
1069 #endif
|
Chris@1143
|
1070
|
Chris@1083
|
1071 for (int x = start; x != finish; x += step) {
|
Chris@1083
|
1072
|
Chris@1083
|
1073 // x is the on-canvas pixel coord; sx (later) will be the
|
Chris@1083
|
1074 // source column index
|
Chris@1083
|
1075
|
Chris@1221
|
1076 ++xPixelCount;
|
Chris@1083
|
1077
|
Chris@1083
|
1078 if (binforx[x] < 0) continue;
|
Chris@1083
|
1079
|
Chris@1083
|
1080 int sx0 = binforx[x] / divisor;
|
Chris@1083
|
1081 int sx1 = sx0;
|
Chris@1083
|
1082 if (x+1 < w) sx1 = binforx[x+1] / divisor;
|
Chris@1083
|
1083 if (sx0 < 0) sx0 = sx1 - 1;
|
Chris@1083
|
1084 if (sx0 < 0) continue;
|
Chris@1083
|
1085 if (sx1 <= sx0) sx1 = sx0 + 1;
|
Chris@1083
|
1086
|
Chris@1161
|
1087 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1214
|
1088 // SVDEBUG << "x = " << x << ", binforx[x] = " << binforx[x] << ", sx range " << sx0 << " -> " << sx1 << endl;
|
Chris@1161
|
1089 #endif
|
Chris@1161
|
1090
|
Chris@1083
|
1091 vector<float> pixelPeakColumn;
|
Chris@1121
|
1092 MagnitudeRange magRange;
|
Chris@1083
|
1093
|
Chris@1083
|
1094 for (int sx = sx0; sx < sx1; ++sx) {
|
Chris@1083
|
1095
|
Chris@1094
|
1096 if (sx < 0 || sx >= modelWidth) {
|
Chris@1083
|
1097 continue;
|
Chris@1083
|
1098 }
|
Chris@1083
|
1099
|
Chris@1083
|
1100 if (sx != psx) {
|
Chris@1138
|
1101
|
Chris@1138
|
1102 // order:
|
Chris@1138
|
1103 // get column -> scale -> normalise -> record extents ->
|
Chris@1138
|
1104 // peak pick -> distribute/interpolate -> apply display gain
|
Chris@1083
|
1105
|
Chris@1138
|
1106 // this does the first three:
|
Chris@1161
|
1107 ColumnOp::Column column = getColumn(sx, minbin, nbins,
|
Chris@1502
|
1108 sourceModel);
|
Chris@1083
|
1109
|
Chris@1131
|
1110 magRange.sample(column);
|
Chris@1162
|
1111
|
Chris@1103
|
1112 if (m_params.binDisplay == BinDisplay::PeakBins) {
|
Chris@1083
|
1113 column = ColumnOp::peakPick(column);
|
Chris@1083
|
1114 }
|
Chris@1083
|
1115
|
Chris@1083
|
1116 preparedColumn =
|
Chris@1124
|
1117 ColumnOp::distribute(column,
|
Chris@1083
|
1118 h,
|
Chris@1083
|
1119 binfory,
|
Chris@1083
|
1120 minbin,
|
Chris@1083
|
1121 m_params.interpolate);
|
Chris@1124
|
1122
|
Chris@1124
|
1123 // Display gain belongs to the colour scale and is
|
Chris@1124
|
1124 // applied by the colour scale object when mapping it
|
Chris@1083
|
1125
|
Chris@1083
|
1126 psx = sx;
|
Chris@1083
|
1127 }
|
Chris@1083
|
1128
|
Chris@1083
|
1129 if (sx == sx0) {
|
Chris@1083
|
1130 pixelPeakColumn = preparedColumn;
|
Chris@1083
|
1131 } else {
|
Chris@1083
|
1132 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
|
Chris@1083
|
1133 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
|
Chris@1083
|
1134 preparedColumn[i]);
|
Chris@1083
|
1135 }
|
Chris@1083
|
1136 }
|
Chris@1083
|
1137 }
|
Chris@1083
|
1138
|
Chris@1083
|
1139 if (!pixelPeakColumn.empty()) {
|
Chris@1121
|
1140
|
Chris@1083
|
1141 for (int y = 0; y < h; ++y) {
|
Chris@1116
|
1142 int py;
|
Chris@1116
|
1143 if (m_params.invertVertical) {
|
Chris@1116
|
1144 py = y;
|
Chris@1116
|
1145 } else {
|
Chris@1116
|
1146 py = h - y - 1;
|
Chris@1116
|
1147 }
|
Chris@1083
|
1148 m_drawBuffer.setPixel
|
Chris@1083
|
1149 (x,
|
Chris@1116
|
1150 py,
|
Chris@1083
|
1151 m_params.colourScale.getPixel(pixelPeakColumn[y]));
|
Chris@1083
|
1152 }
|
Chris@1121
|
1153
|
Chris@1121
|
1154 m_magRanges.push_back(magRange);
|
Chris@1083
|
1155 }
|
Chris@1083
|
1156
|
Chris@1221
|
1157 double fractionComplete = double(xPixelCount) / double(w);
|
Chris@1083
|
1158 if (timer.outOfTime(fractionComplete)) {
|
Chris@1214
|
1159 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1160 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1161 << ": out of time with xPixelCount = " << xPixelCount << endl;
|
Chris@1214
|
1162 #endif
|
Chris@1221
|
1163 updateTimings(timer, xPixelCount);
|
Chris@1221
|
1164 return xPixelCount;
|
Chris@1083
|
1165 }
|
Chris@1083
|
1166 }
|
Chris@1083
|
1167
|
Chris@1221
|
1168 updateTimings(timer, xPixelCount);
|
Chris@1451
|
1169
|
Chris@1451
|
1170 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1171 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1172 << ": completed with xPixelCount = " << xPixelCount << endl;
|
Chris@1451
|
1173 #endif
|
Chris@1221
|
1174 return xPixelCount;
|
Chris@1083
|
1175 }
|
Chris@1083
|
1176
|
Chris@1097
|
1177 int
|
Chris@1113
|
1178 Colour3DPlotRenderer::renderDrawBufferPeakFrequencies(const LayerGeometryProvider *v,
|
Chris@1097
|
1179 int w, int h,
|
Chris@1097
|
1180 const vector<int> &binforx,
|
Chris@1097
|
1181 const vector<double> &binfory,
|
Chris@1097
|
1182 bool rightToLeft,
|
Chris@1097
|
1183 bool timeConstrained)
|
Chris@1097
|
1184 {
|
Chris@1500
|
1185 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1186 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1187 << ": [PEAK] renderDrawBufferPeakFrequencies" << endl;
|
Chris@1500
|
1188 #endif
|
Chris@1500
|
1189
|
Chris@1097
|
1190 // Callers must have checked that the appropriate subset of
|
Chris@1097
|
1191 // Sources data members are set for the supplied flags (e.g. that
|
Chris@1097
|
1192 // fft model exists)
|
Chris@1097
|
1193
|
Chris@1097
|
1194 RenderTimer timer(timeConstrained ?
|
Chris@1221
|
1195 RenderTimer::SlowRender :
|
Chris@1097
|
1196 RenderTimer::NoTimeout);
|
Chris@1097
|
1197
|
Chris@1469
|
1198 auto fft = ModelById::getAs<FFTModel>(m_sources.fft);
|
Chris@1469
|
1199 if (!fft) return 0;
|
Chris@1135
|
1200
|
Chris@1135
|
1201 int sh = fft->getHeight();
|
Chris@1135
|
1202
|
Chris@1097
|
1203 int minbin = int(binfory[0] + 0.0001);
|
Chris@1135
|
1204 if (minbin >= sh) minbin = sh - 1;
|
Chris@1097
|
1205 if (minbin < 0) minbin = 0;
|
Chris@1097
|
1206
|
Chris@1135
|
1207 int nbins = int(binfory[h-1]) - minbin + 1;
|
Chris@1135
|
1208 if (minbin + nbins > sh) nbins = sh - minbin;
|
Chris@1097
|
1209
|
Chris@1097
|
1210 FFTModel::PeakSet peakfreqs;
|
Chris@1097
|
1211
|
Chris@1097
|
1212 int psx = -1;
|
Chris@1097
|
1213
|
Chris@1097
|
1214 int start = 0;
|
Chris@1097
|
1215 int finish = w;
|
Chris@1097
|
1216 int step = 1;
|
Chris@1097
|
1217
|
Chris@1097
|
1218 if (rightToLeft) {
|
Chris@1097
|
1219 start = w-1;
|
Chris@1097
|
1220 finish = -1;
|
Chris@1097
|
1221 step = -1;
|
Chris@1097
|
1222 }
|
Chris@1097
|
1223
|
Chris@1221
|
1224 int xPixelCount = 0;
|
Chris@1097
|
1225
|
Chris@1097
|
1226 vector<float> preparedColumn;
|
Chris@1097
|
1227
|
Chris@1097
|
1228 int modelWidth = fft->getWidth();
|
Chris@1143
|
1229 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1230 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1231 << ": modelWidth " << modelWidth << endl;
|
Chris@1143
|
1232 #endif
|
Chris@1143
|
1233
|
Chris@1135
|
1234 double minFreq =
|
Chris@1135
|
1235 (double(minbin) * fft->getSampleRate()) / fft->getFFTSize();
|
Chris@1135
|
1236 double maxFreq =
|
Chris@1135
|
1237 (double(minbin + nbins - 1) * fft->getSampleRate()) / fft->getFFTSize();
|
Chris@1097
|
1238
|
Chris@1103
|
1239 bool logarithmic = (m_params.binScale == BinScale::Log);
|
Chris@1217
|
1240
|
Chris@1218
|
1241 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1242 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1243 << ": start = " << start << ", finish = " << finish
|
Chris@1218
|
1244 << ", step = " << step << endl;
|
Chris@1218
|
1245 #endif
|
Chris@1218
|
1246
|
Chris@1097
|
1247 for (int x = start; x != finish; x += step) {
|
Chris@1097
|
1248
|
Chris@1097
|
1249 // x is the on-canvas pixel coord; sx (later) will be the
|
Chris@1097
|
1250 // source column index
|
Chris@1097
|
1251
|
Chris@1221
|
1252 ++xPixelCount;
|
Chris@1097
|
1253
|
Chris@1097
|
1254 if (binforx[x] < 0) continue;
|
Chris@1097
|
1255
|
Chris@1097
|
1256 int sx0 = binforx[x];
|
Chris@1097
|
1257 int sx1 = sx0;
|
Chris@1097
|
1258 if (x+1 < w) sx1 = binforx[x+1];
|
Chris@1097
|
1259 if (sx0 < 0) sx0 = sx1 - 1;
|
Chris@1097
|
1260 if (sx0 < 0) continue;
|
Chris@1097
|
1261 if (sx1 <= sx0) sx1 = sx0 + 1;
|
Chris@1097
|
1262
|
Chris@1097
|
1263 vector<float> pixelPeakColumn;
|
Chris@1121
|
1264 MagnitudeRange magRange;
|
Chris@1097
|
1265
|
Chris@1097
|
1266 for (int sx = sx0; sx < sx1; ++sx) {
|
Chris@1097
|
1267
|
Chris@1097
|
1268 if (sx < 0 || sx >= modelWidth) {
|
Chris@1097
|
1269 continue;
|
Chris@1097
|
1270 }
|
Chris@1097
|
1271
|
Chris@1097
|
1272 if (sx != psx) {
|
Chris@1502
|
1273 preparedColumn = getColumn(sx, minbin, nbins, fft);
|
Chris@1131
|
1274 magRange.sample(preparedColumn);
|
Chris@1097
|
1275 psx = sx;
|
Chris@1097
|
1276 }
|
Chris@1097
|
1277
|
Chris@1097
|
1278 if (sx == sx0) {
|
Chris@1097
|
1279 pixelPeakColumn = preparedColumn;
|
Chris@1097
|
1280 peakfreqs = fft->getPeakFrequencies(FFTModel::AllPeaks, sx,
|
Chris@1135
|
1281 minbin, minbin + nbins - 1);
|
Chris@1097
|
1282 } else {
|
Chris@1097
|
1283 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
|
Chris@1097
|
1284 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
|
Chris@1097
|
1285 preparedColumn[i]);
|
Chris@1097
|
1286 }
|
Chris@1097
|
1287 }
|
Chris@1097
|
1288 }
|
Chris@1097
|
1289
|
Chris@1097
|
1290 if (!pixelPeakColumn.empty()) {
|
Chris@1164
|
1291
|
Chris@1218
|
1292 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1221
|
1293 // SVDEBUG << "found " << peakfreqs.size() << " peak freqs at column "
|
Chris@1221
|
1294 // << sx0 << endl;
|
Chris@1218
|
1295 #endif
|
Chris@1218
|
1296
|
Chris@1097
|
1297 for (FFTModel::PeakSet::const_iterator pi = peakfreqs.begin();
|
Chris@1097
|
1298 pi != peakfreqs.end(); ++pi) {
|
Chris@1097
|
1299
|
Chris@1097
|
1300 int bin = pi->first;
|
Chris@1097
|
1301 double freq = pi->second;
|
Chris@1097
|
1302
|
Chris@1097
|
1303 if (bin < minbin) continue;
|
Chris@1135
|
1304 if (bin >= minbin + nbins) break;
|
Chris@1097
|
1305
|
Chris@1097
|
1306 double value = pixelPeakColumn[bin - minbin];
|
Chris@1097
|
1307
|
Chris@1097
|
1308 double y = v->getYForFrequency
|
Chris@1097
|
1309 (freq, minFreq, maxFreq, logarithmic);
|
Chris@1097
|
1310
|
Chris@1097
|
1311 int iy = int(y + 0.5);
|
Chris@1097
|
1312 if (iy < 0 || iy >= h) continue;
|
Chris@1097
|
1313
|
Chris@1219
|
1314 auto pixel = m_params.colourScale.getPixel(value);
|
Chris@1219
|
1315
|
Chris@1219
|
1316 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1219
|
1317 // SVDEBUG << "frequency " << freq << " for bin " << bin
|
Chris@1219
|
1318 // << " -> y = " << y << ", iy = " << iy << ", value = "
|
Chris@1219
|
1319 // << value << ", pixel " << pixel << "\n";
|
Chris@1219
|
1320 #endif
|
Chris@1219
|
1321
|
Chris@1219
|
1322 m_drawBuffer.setPixel(x, iy, pixel);
|
Chris@1097
|
1323 }
|
Chris@1121
|
1324
|
Chris@1121
|
1325 m_magRanges.push_back(magRange);
|
Chris@1218
|
1326
|
Chris@1218
|
1327 } else {
|
Chris@1218
|
1328 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1329 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1330 << ": pixel peak column for range " << sx0 << " to " << sx1
|
Chris@1218
|
1331 << " is empty" << endl;
|
Chris@1218
|
1332 #endif
|
Chris@1097
|
1333 }
|
Chris@1097
|
1334
|
Chris@1221
|
1335 double fractionComplete = double(xPixelCount) / double(w);
|
Chris@1097
|
1336 if (timer.outOfTime(fractionComplete)) {
|
Chris@1218
|
1337 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1338 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1339 << ": out of time" << endl;
|
Chris@1218
|
1340 #endif
|
Chris@1221
|
1341 updateTimings(timer, xPixelCount);
|
Chris@1221
|
1342 return xPixelCount;
|
Chris@1097
|
1343 }
|
Chris@1097
|
1344 }
|
Chris@1097
|
1345
|
Chris@1221
|
1346 updateTimings(timer, xPixelCount);
|
Chris@1221
|
1347 return xPixelCount;
|
Chris@1221
|
1348 }
|
Chris@1221
|
1349
|
Chris@1221
|
1350 void
|
Chris@1221
|
1351 Colour3DPlotRenderer::updateTimings(const RenderTimer &timer, int xPixelCount)
|
Chris@1221
|
1352 {
|
Chris@1237
|
1353 double secondsPerXPixel = timer.secondsPerItem(xPixelCount);
|
Chris@1221
|
1354
|
Chris@1237
|
1355 // valid if we have enough data points, or if the overall time is
|
Chris@1237
|
1356 // massively slow anyway (as we definitely need to warn about that)
|
Chris@1237
|
1357 bool valid = (xPixelCount > 20 || secondsPerXPixel > 0.01);
|
Chris@1237
|
1358
|
Chris@1237
|
1359 if (valid) {
|
Chris@1237
|
1360 m_secondsPerXPixel = secondsPerXPixel;
|
Chris@1237
|
1361 m_secondsPerXPixelValid = true;
|
Chris@1237
|
1362
|
Chris@1221
|
1363 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1364 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1365 << ": across " << xPixelCount
|
Chris@1500
|
1366 << " x-pixels, seconds per x-pixel = "
|
Chris@1450
|
1367 << m_secondsPerXPixel << " (total = "
|
Chris@1499
|
1368 << (xPixelCount * m_secondsPerXPixel) << ")" << endl;
|
Chris@1221
|
1369 #endif
|
Chris@1237
|
1370 }
|
Chris@1097
|
1371 }
|
Chris@1097
|
1372
|
Chris@1079
|
1373 void
|
Chris@1095
|
1374 Colour3DPlotRenderer::recreateDrawBuffer(int w, int h)
|
Chris@1079
|
1375 {
|
Chris@1095
|
1376 m_drawBuffer = QImage(w, h, QImage::Format_Indexed8);
|
Chris@1079
|
1377
|
Chris@1095
|
1378 for (int pixel = 0; pixel < 256; ++pixel) {
|
Chris@1095
|
1379 m_drawBuffer.setColor
|
Chris@1095
|
1380 ((unsigned char)pixel,
|
Chris@1112
|
1381 m_params.colourScale.getColourForPixel
|
Chris@1112
|
1382 (pixel, m_params.colourRotation).rgb());
|
Chris@1079
|
1383 }
|
Chris@1079
|
1384
|
Chris@1079
|
1385 m_drawBuffer.fill(0);
|
Chris@1121
|
1386 m_magRanges.clear();
|
Chris@1079
|
1387 }
|
Chris@1079
|
1388
|
Chris@1095
|
1389 void
|
Chris@1095
|
1390 Colour3DPlotRenderer::clearDrawBuffer(int w, int h)
|
Chris@1095
|
1391 {
|
Chris@1095
|
1392 if (m_drawBuffer.width() < w || m_drawBuffer.height() != h) {
|
Chris@1095
|
1393 recreateDrawBuffer(w, h);
|
Chris@1095
|
1394 } else {
|
Chris@1095
|
1395 m_drawBuffer.fill(0);
|
Chris@1121
|
1396 m_magRanges.clear();
|
Chris@1095
|
1397 }
|
Chris@1095
|
1398 }
|
Chris@1079
|
1399
|
Chris@1139
|
1400 QRect
|
Chris@1139
|
1401 Colour3DPlotRenderer::findSimilarRegionExtents(QPoint p) const
|
Chris@1139
|
1402 {
|
Chris@1139
|
1403 QImage image = m_cache.getImage();
|
Chris@1139
|
1404 ImageRegionFinder finder;
|
Chris@1139
|
1405 QRect rect = finder.findRegionExtents(&image, p);
|
Chris@1139
|
1406 return rect;
|
Chris@1139
|
1407 }
|