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@1531
|
782 QImage target(targetWidth, targetHeight,
|
Chris@1531
|
783 QImage::Format_ARGB32_Premultiplied);
|
Chris@1167
|
784
|
Chris@1167
|
785 for (int y = 0; y < targetHeight; ++y) {
|
Chris@1167
|
786
|
Chris@1167
|
787 QRgb *targetLine = reinterpret_cast<QRgb *>(target.scanLine(y));
|
Chris@1167
|
788
|
Chris@1167
|
789 int sy = int((uint64_t(y) * sourceHeight) / targetHeight);
|
Chris@1167
|
790 if (sy == sourceHeight) --sy;
|
Chris@1167
|
791
|
Chris@1531
|
792 // The source image is 8-bit indexed
|
Chris@1531
|
793 const uchar *sourceLine = image.constScanLine(sy);
|
Chris@1532
|
794
|
Chris@1532
|
795 int psx = -1;
|
Chris@1532
|
796 QRgb colour = {};
|
Chris@1531
|
797
|
Chris@1167
|
798 for (int x = 0; x < targetWidth; ++x) {
|
Chris@1167
|
799
|
Chris@1167
|
800 int sx = int((uint64_t(x) * sourceWidth) / targetWidth);
|
Chris@1167
|
801 if (sx == sourceWidth) --sx;
|
Chris@1531
|
802
|
Chris@1532
|
803 if (sx > psx) {
|
Chris@1532
|
804 colour = image.color(sourceLine[sx]);
|
Chris@1532
|
805 }
|
Chris@1532
|
806
|
Chris@1532
|
807 targetLine[x] = colour;
|
Chris@1532
|
808 psx = sx;
|
Chris@1167
|
809 }
|
Chris@1167
|
810 }
|
Chris@1167
|
811
|
Chris@1167
|
812 return target;
|
Chris@1167
|
813 }
|
Chris@1167
|
814
|
Chris@1094
|
815 void
|
Chris@1113
|
816 Colour3DPlotRenderer::renderToCacheBinResolution(const LayerGeometryProvider *v,
|
Chris@1094
|
817 int x0, int repaintWidth)
|
Chris@1094
|
818 {
|
Chris@1117
|
819 Profiler profiler("Colour3DPlotRenderer::renderToCacheBinResolution");
|
Chris@1143
|
820 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1499
|
821 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
822 << ": [BIN] renderToCacheBinResolution" << endl;
|
Chris@1143
|
823 #endif
|
Chris@1094
|
824
|
Chris@1094
|
825 // Draw to the draw buffer, and then scale-copy from there. Draw
|
Chris@1094
|
826 // buffer is at bin resolution, i.e. buffer x == source column
|
Chris@1094
|
827 // number. We use toolkit smooth scaling for interpolation.
|
Chris@1084
|
828
|
Chris@1469
|
829 auto model = ModelById::getAs<DenseThreeDimensionalModel>(m_sources.source);
|
Chris@1469
|
830 if (!model) return;
|
Chris@1094
|
831
|
Chris@1094
|
832 // The draw buffer will contain a fragment at bin resolution. We
|
Chris@1094
|
833 // need to ensure that it starts and ends at points where a
|
Chris@1094
|
834 // time-bin boundary occurs at an exact pixel boundary, and with a
|
Chris@1094
|
835 // certain amount of overlap across existing pixels so that we can
|
Chris@1094
|
836 // scale and draw from it without smoothing errors at the edges.
|
Chris@1094
|
837
|
Chris@1094
|
838 // If (getFrameForX(x) / increment) * increment ==
|
Chris@1094
|
839 // getFrameForX(x), then x is a time-bin boundary. We want two
|
Chris@1094
|
840 // such boundaries at either side of the draw buffer -- one which
|
Chris@1094
|
841 // we draw up to, and one which we subsequently crop at.
|
Chris@1094
|
842
|
Chris@1094
|
843 sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1;
|
Chris@1094
|
844 sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1;
|
Chris@1094
|
845
|
Chris@1094
|
846 int drawBufferWidth;
|
Chris@1094
|
847 int binResolution = model->getResolution();
|
Chris@1094
|
848
|
Chris@1330
|
849 // These loops should eventually terminate provided that
|
Chris@1330
|
850 // getFrameForX always returns a multiple of the zoom level,
|
Chris@1330
|
851 // i.e. there is some x for which getFrameForX(x) == 0 and
|
Chris@1330
|
852 // subsequent return values are equally spaced
|
Chris@1329
|
853
|
Chris@1330
|
854 for (int x = x0; ; --x) {
|
Chris@1094
|
855 sv_frame_t f = v->getFrameForX(x);
|
Chris@1330
|
856 if ((f / binResolution) * binResolution == f) {
|
Chris@1094
|
857 if (leftCropFrame == -1) leftCropFrame = f;
|
Chris@1094
|
858 else if (x < x0 - 2) {
|
Chris@1094
|
859 leftBoundaryFrame = f;
|
Chris@1094
|
860 break;
|
Chris@1094
|
861 }
|
Chris@1094
|
862 }
|
Chris@1094
|
863 }
|
Chris@1329
|
864
|
Chris@1330
|
865 for (int x = x0 + repaintWidth; ; ++x) {
|
Chris@1094
|
866 sv_frame_t f = v->getFrameForX(x);
|
Chris@1330
|
867 if ((f / binResolution) * binResolution == f) {
|
Chris@1494
|
868 if (v->getXForFrame(f) < x0 + repaintWidth) {
|
Chris@1494
|
869 continue;
|
Chris@1494
|
870 }
|
Chris@1094
|
871 if (rightCropFrame == -1) rightCropFrame = f;
|
Chris@1094
|
872 else if (x > x0 + repaintWidth + 2) {
|
Chris@1094
|
873 rightBoundaryFrame = f;
|
Chris@1094
|
874 break;
|
Chris@1094
|
875 }
|
Chris@1094
|
876 }
|
Chris@1094
|
877 }
|
Chris@1329
|
878
|
Chris@1094
|
879 drawBufferWidth = int
|
Chris@1094
|
880 ((rightBoundaryFrame - leftBoundaryFrame) / binResolution);
|
Chris@1094
|
881
|
Chris@1094
|
882 int h = v->getPaintHeight();
|
Chris@1094
|
883
|
Chris@1095
|
884 // For our purposes here, the draw buffer needs to be exactly our
|
Chris@1095
|
885 // target size (so we recreate always rather than just clear it)
|
Chris@1095
|
886
|
Chris@1095
|
887 recreateDrawBuffer(drawBufferWidth, h);
|
Chris@1094
|
888
|
Chris@1094
|
889 vector<int> binforx(drawBufferWidth);
|
Chris@1094
|
890 vector<double> binfory(h);
|
Chris@1094
|
891
|
Chris@1094
|
892 for (int x = 0; x < drawBufferWidth; ++x) {
|
Chris@1094
|
893 binforx[x] = int(leftBoundaryFrame / binResolution) + x;
|
Chris@1094
|
894 }
|
Chris@1094
|
895
|
Chris@1214
|
896 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
897 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
898 << ": binResolution " << binResolution << endl;
|
Chris@1214
|
899 #endif
|
Chris@1094
|
900
|
Chris@1094
|
901 for (int y = 0; y < h; ++y) {
|
Chris@1094
|
902 binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1);
|
Chris@1094
|
903 }
|
Chris@1094
|
904
|
Chris@1094
|
905 int attainedWidth = renderDrawBuffer(drawBufferWidth,
|
Chris@1094
|
906 h,
|
Chris@1094
|
907 binforx,
|
Chris@1094
|
908 binfory,
|
Chris@1212
|
909 -1,
|
Chris@1094
|
910 false,
|
Chris@1094
|
911 false);
|
Chris@1094
|
912
|
Chris@1094
|
913 if (attainedWidth == 0) return;
|
Chris@1094
|
914
|
Chris@1094
|
915 int scaledLeft = v->getXForFrame(leftBoundaryFrame);
|
Chris@1094
|
916 int scaledRight = v->getXForFrame(rightBoundaryFrame);
|
Chris@1095
|
917
|
Chris@1143
|
918 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
919 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
920 << ": scaling draw buffer from width " << m_drawBuffer.width()
|
Chris@1494
|
921 << " to " << (scaledRight - scaledLeft)
|
Chris@1494
|
922 << " (nb drawBufferWidth = "
|
Chris@1494
|
923 << drawBufferWidth << ", attainedWidth = "
|
Chris@1494
|
924 << attainedWidth << ")" << endl;
|
Chris@1143
|
925 #endif
|
Chris@1167
|
926
|
Chris@1167
|
927 QImage scaled = scaleDrawBufferImage
|
Chris@1167
|
928 (m_drawBuffer, scaledRight - scaledLeft, h);
|
Chris@1084
|
929
|
Chris@1094
|
930 int scaledLeftCrop = v->getXForFrame(leftCropFrame);
|
Chris@1094
|
931 int scaledRightCrop = v->getXForFrame(rightCropFrame);
|
Chris@1094
|
932
|
Chris@1094
|
933 int targetLeft = scaledLeftCrop;
|
Chris@1094
|
934 if (targetLeft < 0) {
|
Chris@1094
|
935 targetLeft = 0;
|
Chris@1094
|
936 }
|
Chris@1094
|
937
|
Chris@1094
|
938 int targetWidth = scaledRightCrop - targetLeft;
|
Chris@1094
|
939 if (targetLeft + targetWidth > m_cache.getSize().width()) {
|
Chris@1094
|
940 targetWidth = m_cache.getSize().width() - targetLeft;
|
Chris@1094
|
941 }
|
Chris@1094
|
942
|
Chris@1094
|
943 int sourceLeft = targetLeft - scaledLeft;
|
Chris@1094
|
944 if (sourceLeft < 0) {
|
Chris@1094
|
945 sourceLeft = 0;
|
Chris@1094
|
946 }
|
Chris@1494
|
947
|
Chris@1494
|
948 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
949 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
950 << ": leftBoundaryFrame = " << leftBoundaryFrame
|
Chris@1494
|
951 << ", leftCropFrame = " << leftCropFrame
|
Chris@1494
|
952 << ", scaledLeft = " << scaledLeft
|
Chris@1494
|
953 << ", scaledLeftCrop = " << scaledLeftCrop
|
Chris@1494
|
954 << endl;
|
Chris@1500
|
955 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
956 << ": rightBoundaryFrame = " << rightBoundaryFrame
|
Chris@1494
|
957 << ", rightCropFrame = " << rightCropFrame
|
Chris@1494
|
958 << ", scaledRight = " << scaledRight
|
Chris@1494
|
959 << ", scaledRightCrop = " << scaledRightCrop
|
Chris@1494
|
960 << endl;
|
Chris@1494
|
961 #endif
|
Chris@1094
|
962
|
Chris@1143
|
963 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
964 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
965 << ": x0 = " << x0
|
Chris@1494
|
966 << ", repaintWidth = " << repaintWidth
|
Chris@1494
|
967 << ", targetLeft = " << targetLeft
|
Chris@1214
|
968 << ", targetWidth = " << targetWidth << endl;
|
Chris@1143
|
969 #endif
|
Chris@1094
|
970
|
Chris@1094
|
971 if (targetWidth > 0) {
|
Chris@1136
|
972 // we are copying from an image that has already been scaled,
|
Chris@1136
|
973 // hence using the same width in both geometries
|
Chris@1094
|
974 m_cache.drawImage(targetLeft, targetWidth,
|
Chris@1094
|
975 scaled,
|
Chris@1136
|
976 sourceLeft, targetWidth);
|
Chris@1084
|
977 }
|
Chris@1121
|
978
|
Chris@1121
|
979 for (int i = 0; i < targetWidth; ++i) {
|
Chris@1136
|
980 // but the mag range vector has not been scaled
|
Chris@1136
|
981 int sourceIx = int((double(i + sourceLeft) / scaled.width())
|
Chris@1136
|
982 * int(m_magRanges.size()));
|
Chris@1121
|
983 if (in_range_for(m_magRanges, sourceIx)) {
|
Chris@1121
|
984 m_magCache.sampleColumn(i, m_magRanges.at(sourceIx));
|
Chris@1121
|
985 }
|
Chris@1121
|
986 }
|
Chris@1079
|
987 }
|
Chris@1083
|
988
|
Chris@1083
|
989 int
|
Chris@1083
|
990 Colour3DPlotRenderer::renderDrawBuffer(int w, int h,
|
Chris@1083
|
991 const vector<int> &binforx,
|
Chris@1083
|
992 const vector<double> &binfory,
|
Chris@1212
|
993 int peakCacheIndex,
|
Chris@1083
|
994 bool rightToLeft,
|
Chris@1083
|
995 bool timeConstrained)
|
Chris@1083
|
996 {
|
Chris@1083
|
997 // Callers must have checked that the appropriate subset of
|
Chris@1083
|
998 // Sources data members are set for the supplied flags (e.g. that
|
Chris@1212
|
999 // peakCache corresponding to peakCacheIndex exists)
|
Chris@1083
|
1000
|
Chris@1083
|
1001 RenderTimer timer(timeConstrained ?
|
Chris@1083
|
1002 RenderTimer::FastRender :
|
Chris@1083
|
1003 RenderTimer::NoTimeout);
|
Chris@1083
|
1004
|
Chris@1164
|
1005 Profiler profiler("Colour3DPlotRenderer::renderDrawBuffer");
|
Chris@1164
|
1006
|
Chris@1083
|
1007 int divisor = 1;
|
Chris@1473
|
1008
|
Chris@1473
|
1009 std::shared_ptr<DenseThreeDimensionalModel> sourceModel;
|
Chris@1473
|
1010
|
Chris@1473
|
1011 if (peakCacheIndex >= 0) {
|
Chris@1473
|
1012 auto peakCache = ModelById::getAs<Dense3DModelPeakCache>
|
Chris@1473
|
1013 (m_sources.peakCaches[peakCacheIndex]);
|
Chris@1473
|
1014 if (peakCache) {
|
Chris@1473
|
1015 divisor = peakCache->getColumnsPerPeak();
|
Chris@1473
|
1016 sourceModel = peakCache;
|
Chris@1473
|
1017 }
|
Chris@1473
|
1018 }
|
Chris@1473
|
1019
|
Chris@1473
|
1020 if (!sourceModel) {
|
Chris@1473
|
1021 sourceModel = ModelById::getAs<DenseThreeDimensionalModel>
|
Chris@1473
|
1022 (m_sources.source);
|
Chris@1473
|
1023 }
|
Chris@1469
|
1024
|
Chris@1473
|
1025 if (!sourceModel) return 0;
|
Chris@1083
|
1026
|
Chris@1214
|
1027 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1028 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1029 << ": renderDrawBuffer: w = " << w << ", h = " << h
|
Chris@1212
|
1030 << ", peakCacheIndex = " << peakCacheIndex << " (divisor = "
|
cannam@1171
|
1031 << divisor << "), rightToLeft = " << rightToLeft
|
cannam@1171
|
1032 << ", timeConstrained = " << timeConstrained << endl;
|
Chris@1500
|
1033 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1034 << ": renderDrawBuffer: normalization = " << int(m_params.normalization)
|
cannam@1171
|
1035 << ", binDisplay = " << int(m_params.binDisplay)
|
cannam@1171
|
1036 << ", binScale = " << int(m_params.binScale)
|
cannam@1171
|
1037 << ", alwaysOpaque = " << m_params.alwaysOpaque
|
cannam@1171
|
1038 << ", interpolate = " << m_params.interpolate << endl;
|
Chris@1214
|
1039 #endif
|
cannam@1171
|
1040
|
Chris@1135
|
1041 int sh = sourceModel->getHeight();
|
Chris@1135
|
1042
|
Chris@1135
|
1043 int minbin = int(binfory[0] + 0.0001);
|
Chris@1135
|
1044 if (minbin >= sh) minbin = sh - 1;
|
Chris@1135
|
1045 if (minbin < 0) minbin = 0;
|
Chris@1135
|
1046
|
Chris@1170
|
1047 int nbins = int(binfory[h-1] + 0.0001) - minbin + 1;
|
Chris@1135
|
1048 if (minbin + nbins > sh) nbins = sh - minbin;
|
Chris@1162
|
1049
|
Chris@1162
|
1050 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1051 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1052 << ": minbin = " << minbin << ", nbins = " << nbins
|
Chris@1500
|
1053 << ", last binfory = " << binfory[h-1]
|
Chris@1500
|
1054 << " (rounds to " << int(binfory[h-1])
|
Chris@1500
|
1055 << ") (model height " << sh << ")" << endl;
|
Chris@1162
|
1056 #endif
|
Chris@1135
|
1057
|
Chris@1083
|
1058 int psx = -1;
|
Chris@1083
|
1059
|
Chris@1083
|
1060 int start = 0;
|
Chris@1083
|
1061 int finish = w;
|
Chris@1083
|
1062 int step = 1;
|
Chris@1083
|
1063
|
Chris@1083
|
1064 if (rightToLeft) {
|
Chris@1083
|
1065 start = w-1;
|
Chris@1083
|
1066 finish = -1;
|
Chris@1083
|
1067 step = -1;
|
Chris@1083
|
1068 }
|
Chris@1083
|
1069
|
Chris@1221
|
1070 int xPixelCount = 0;
|
Chris@1083
|
1071
|
Chris@1083
|
1072 vector<float> preparedColumn;
|
Chris@1094
|
1073
|
Chris@1094
|
1074 int modelWidth = sourceModel->getWidth();
|
Chris@1121
|
1075
|
Chris@1143
|
1076 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1077 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1078 << ": modelWidth " << modelWidth << ", divisor " << divisor << endl;
|
Chris@1500
|
1079 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1080 << ": start = " << start << ", finish = " << finish << ", step = " << step << endl;
|
Chris@1143
|
1081 #endif
|
Chris@1143
|
1082
|
Chris@1083
|
1083 for (int x = start; x != finish; x += step) {
|
Chris@1083
|
1084
|
Chris@1083
|
1085 // x is the on-canvas pixel coord; sx (later) will be the
|
Chris@1083
|
1086 // source column index
|
Chris@1083
|
1087
|
Chris@1221
|
1088 ++xPixelCount;
|
Chris@1083
|
1089
|
Chris@1083
|
1090 if (binforx[x] < 0) continue;
|
Chris@1083
|
1091
|
Chris@1083
|
1092 int sx0 = binforx[x] / divisor;
|
Chris@1083
|
1093 int sx1 = sx0;
|
Chris@1083
|
1094 if (x+1 < w) sx1 = binforx[x+1] / divisor;
|
Chris@1083
|
1095 if (sx0 < 0) sx0 = sx1 - 1;
|
Chris@1083
|
1096 if (sx0 < 0) continue;
|
Chris@1083
|
1097 if (sx1 <= sx0) sx1 = sx0 + 1;
|
Chris@1083
|
1098
|
Chris@1161
|
1099 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1214
|
1100 // SVDEBUG << "x = " << x << ", binforx[x] = " << binforx[x] << ", sx range " << sx0 << " -> " << sx1 << endl;
|
Chris@1161
|
1101 #endif
|
Chris@1161
|
1102
|
Chris@1083
|
1103 vector<float> pixelPeakColumn;
|
Chris@1121
|
1104 MagnitudeRange magRange;
|
Chris@1083
|
1105
|
Chris@1083
|
1106 for (int sx = sx0; sx < sx1; ++sx) {
|
Chris@1083
|
1107
|
Chris@1094
|
1108 if (sx < 0 || sx >= modelWidth) {
|
Chris@1083
|
1109 continue;
|
Chris@1083
|
1110 }
|
Chris@1083
|
1111
|
Chris@1083
|
1112 if (sx != psx) {
|
Chris@1138
|
1113
|
Chris@1138
|
1114 // order:
|
Chris@1138
|
1115 // get column -> scale -> normalise -> record extents ->
|
Chris@1138
|
1116 // peak pick -> distribute/interpolate -> apply display gain
|
Chris@1083
|
1117
|
Chris@1138
|
1118 // this does the first three:
|
Chris@1161
|
1119 ColumnOp::Column column = getColumn(sx, minbin, nbins,
|
Chris@1502
|
1120 sourceModel);
|
Chris@1083
|
1121
|
Chris@1131
|
1122 magRange.sample(column);
|
Chris@1162
|
1123
|
Chris@1103
|
1124 if (m_params.binDisplay == BinDisplay::PeakBins) {
|
Chris@1083
|
1125 column = ColumnOp::peakPick(column);
|
Chris@1083
|
1126 }
|
Chris@1083
|
1127
|
Chris@1083
|
1128 preparedColumn =
|
Chris@1124
|
1129 ColumnOp::distribute(column,
|
Chris@1083
|
1130 h,
|
Chris@1083
|
1131 binfory,
|
Chris@1083
|
1132 minbin,
|
Chris@1083
|
1133 m_params.interpolate);
|
Chris@1124
|
1134
|
Chris@1124
|
1135 // Display gain belongs to the colour scale and is
|
Chris@1124
|
1136 // applied by the colour scale object when mapping it
|
Chris@1083
|
1137
|
Chris@1083
|
1138 psx = sx;
|
Chris@1083
|
1139 }
|
Chris@1083
|
1140
|
Chris@1083
|
1141 if (sx == sx0) {
|
Chris@1083
|
1142 pixelPeakColumn = preparedColumn;
|
Chris@1083
|
1143 } else {
|
Chris@1083
|
1144 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
|
Chris@1083
|
1145 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
|
Chris@1083
|
1146 preparedColumn[i]);
|
Chris@1083
|
1147 }
|
Chris@1083
|
1148 }
|
Chris@1083
|
1149 }
|
Chris@1083
|
1150
|
Chris@1083
|
1151 if (!pixelPeakColumn.empty()) {
|
Chris@1121
|
1152
|
Chris@1083
|
1153 for (int y = 0; y < h; ++y) {
|
Chris@1116
|
1154 int py;
|
Chris@1116
|
1155 if (m_params.invertVertical) {
|
Chris@1116
|
1156 py = y;
|
Chris@1116
|
1157 } else {
|
Chris@1116
|
1158 py = h - y - 1;
|
Chris@1116
|
1159 }
|
Chris@1083
|
1160 m_drawBuffer.setPixel
|
Chris@1083
|
1161 (x,
|
Chris@1116
|
1162 py,
|
Chris@1083
|
1163 m_params.colourScale.getPixel(pixelPeakColumn[y]));
|
Chris@1083
|
1164 }
|
Chris@1121
|
1165
|
Chris@1121
|
1166 m_magRanges.push_back(magRange);
|
Chris@1083
|
1167 }
|
Chris@1083
|
1168
|
Chris@1221
|
1169 double fractionComplete = double(xPixelCount) / double(w);
|
Chris@1083
|
1170 if (timer.outOfTime(fractionComplete)) {
|
Chris@1214
|
1171 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1172 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1173 << ": out of time with xPixelCount = " << xPixelCount << endl;
|
Chris@1214
|
1174 #endif
|
Chris@1221
|
1175 updateTimings(timer, xPixelCount);
|
Chris@1221
|
1176 return xPixelCount;
|
Chris@1083
|
1177 }
|
Chris@1083
|
1178 }
|
Chris@1083
|
1179
|
Chris@1221
|
1180 updateTimings(timer, xPixelCount);
|
Chris@1451
|
1181
|
Chris@1451
|
1182 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1183 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1184 << ": completed with xPixelCount = " << xPixelCount << endl;
|
Chris@1451
|
1185 #endif
|
Chris@1221
|
1186 return xPixelCount;
|
Chris@1083
|
1187 }
|
Chris@1083
|
1188
|
Chris@1097
|
1189 int
|
Chris@1113
|
1190 Colour3DPlotRenderer::renderDrawBufferPeakFrequencies(const LayerGeometryProvider *v,
|
Chris@1097
|
1191 int w, int h,
|
Chris@1097
|
1192 const vector<int> &binforx,
|
Chris@1097
|
1193 const vector<double> &binfory,
|
Chris@1097
|
1194 bool rightToLeft,
|
Chris@1097
|
1195 bool timeConstrained)
|
Chris@1097
|
1196 {
|
Chris@1500
|
1197 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1198 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1199 << ": [PEAK] renderDrawBufferPeakFrequencies" << endl;
|
Chris@1500
|
1200 #endif
|
Chris@1500
|
1201
|
Chris@1097
|
1202 // Callers must have checked that the appropriate subset of
|
Chris@1097
|
1203 // Sources data members are set for the supplied flags (e.g. that
|
Chris@1097
|
1204 // fft model exists)
|
Chris@1097
|
1205
|
Chris@1097
|
1206 RenderTimer timer(timeConstrained ?
|
Chris@1221
|
1207 RenderTimer::SlowRender :
|
Chris@1097
|
1208 RenderTimer::NoTimeout);
|
Chris@1097
|
1209
|
Chris@1469
|
1210 auto fft = ModelById::getAs<FFTModel>(m_sources.fft);
|
Chris@1469
|
1211 if (!fft) return 0;
|
Chris@1135
|
1212
|
Chris@1135
|
1213 int sh = fft->getHeight();
|
Chris@1135
|
1214
|
Chris@1097
|
1215 int minbin = int(binfory[0] + 0.0001);
|
Chris@1135
|
1216 if (minbin >= sh) minbin = sh - 1;
|
Chris@1097
|
1217 if (minbin < 0) minbin = 0;
|
Chris@1097
|
1218
|
Chris@1135
|
1219 int nbins = int(binfory[h-1]) - minbin + 1;
|
Chris@1135
|
1220 if (minbin + nbins > sh) nbins = sh - minbin;
|
Chris@1097
|
1221
|
Chris@1097
|
1222 FFTModel::PeakSet peakfreqs;
|
Chris@1097
|
1223
|
Chris@1097
|
1224 int psx = -1;
|
Chris@1097
|
1225
|
Chris@1097
|
1226 int start = 0;
|
Chris@1097
|
1227 int finish = w;
|
Chris@1097
|
1228 int step = 1;
|
Chris@1097
|
1229
|
Chris@1097
|
1230 if (rightToLeft) {
|
Chris@1097
|
1231 start = w-1;
|
Chris@1097
|
1232 finish = -1;
|
Chris@1097
|
1233 step = -1;
|
Chris@1097
|
1234 }
|
Chris@1097
|
1235
|
Chris@1221
|
1236 int xPixelCount = 0;
|
Chris@1097
|
1237
|
Chris@1097
|
1238 vector<float> preparedColumn;
|
Chris@1097
|
1239
|
Chris@1097
|
1240 int modelWidth = fft->getWidth();
|
Chris@1143
|
1241 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1242 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1243 << ": modelWidth " << modelWidth << endl;
|
Chris@1143
|
1244 #endif
|
Chris@1143
|
1245
|
Chris@1135
|
1246 double minFreq =
|
Chris@1135
|
1247 (double(minbin) * fft->getSampleRate()) / fft->getFFTSize();
|
Chris@1135
|
1248 double maxFreq =
|
Chris@1135
|
1249 (double(minbin + nbins - 1) * fft->getSampleRate()) / fft->getFFTSize();
|
Chris@1097
|
1250
|
Chris@1103
|
1251 bool logarithmic = (m_params.binScale == BinScale::Log);
|
Chris@1217
|
1252
|
Chris@1218
|
1253 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1254 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1255 << ": start = " << start << ", finish = " << finish
|
Chris@1218
|
1256 << ", step = " << step << endl;
|
Chris@1218
|
1257 #endif
|
Chris@1218
|
1258
|
Chris@1097
|
1259 for (int x = start; x != finish; x += step) {
|
Chris@1097
|
1260
|
Chris@1097
|
1261 // x is the on-canvas pixel coord; sx (later) will be the
|
Chris@1097
|
1262 // source column index
|
Chris@1097
|
1263
|
Chris@1221
|
1264 ++xPixelCount;
|
Chris@1097
|
1265
|
Chris@1097
|
1266 if (binforx[x] < 0) continue;
|
Chris@1097
|
1267
|
Chris@1097
|
1268 int sx0 = binforx[x];
|
Chris@1097
|
1269 int sx1 = sx0;
|
Chris@1097
|
1270 if (x+1 < w) sx1 = binforx[x+1];
|
Chris@1097
|
1271 if (sx0 < 0) sx0 = sx1 - 1;
|
Chris@1097
|
1272 if (sx0 < 0) continue;
|
Chris@1097
|
1273 if (sx1 <= sx0) sx1 = sx0 + 1;
|
Chris@1097
|
1274
|
Chris@1097
|
1275 vector<float> pixelPeakColumn;
|
Chris@1121
|
1276 MagnitudeRange magRange;
|
Chris@1097
|
1277
|
Chris@1097
|
1278 for (int sx = sx0; sx < sx1; ++sx) {
|
Chris@1097
|
1279
|
Chris@1097
|
1280 if (sx < 0 || sx >= modelWidth) {
|
Chris@1097
|
1281 continue;
|
Chris@1097
|
1282 }
|
Chris@1097
|
1283
|
Chris@1097
|
1284 if (sx != psx) {
|
Chris@1502
|
1285 preparedColumn = getColumn(sx, minbin, nbins, fft);
|
Chris@1131
|
1286 magRange.sample(preparedColumn);
|
Chris@1097
|
1287 psx = sx;
|
Chris@1097
|
1288 }
|
Chris@1097
|
1289
|
Chris@1097
|
1290 if (sx == sx0) {
|
Chris@1097
|
1291 pixelPeakColumn = preparedColumn;
|
Chris@1097
|
1292 peakfreqs = fft->getPeakFrequencies(FFTModel::AllPeaks, sx,
|
Chris@1135
|
1293 minbin, minbin + nbins - 1);
|
Chris@1097
|
1294 } else {
|
Chris@1097
|
1295 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
|
Chris@1097
|
1296 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
|
Chris@1097
|
1297 preparedColumn[i]);
|
Chris@1097
|
1298 }
|
Chris@1097
|
1299 }
|
Chris@1097
|
1300 }
|
Chris@1097
|
1301
|
Chris@1097
|
1302 if (!pixelPeakColumn.empty()) {
|
Chris@1164
|
1303
|
Chris@1218
|
1304 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1221
|
1305 // SVDEBUG << "found " << peakfreqs.size() << " peak freqs at column "
|
Chris@1221
|
1306 // << sx0 << endl;
|
Chris@1218
|
1307 #endif
|
Chris@1218
|
1308
|
Chris@1097
|
1309 for (FFTModel::PeakSet::const_iterator pi = peakfreqs.begin();
|
Chris@1097
|
1310 pi != peakfreqs.end(); ++pi) {
|
Chris@1097
|
1311
|
Chris@1097
|
1312 int bin = pi->first;
|
Chris@1097
|
1313 double freq = pi->second;
|
Chris@1097
|
1314
|
Chris@1097
|
1315 if (bin < minbin) continue;
|
Chris@1135
|
1316 if (bin >= minbin + nbins) break;
|
Chris@1097
|
1317
|
Chris@1097
|
1318 double value = pixelPeakColumn[bin - minbin];
|
Chris@1097
|
1319
|
Chris@1097
|
1320 double y = v->getYForFrequency
|
Chris@1097
|
1321 (freq, minFreq, maxFreq, logarithmic);
|
Chris@1097
|
1322
|
Chris@1097
|
1323 int iy = int(y + 0.5);
|
Chris@1097
|
1324 if (iy < 0 || iy >= h) continue;
|
Chris@1097
|
1325
|
Chris@1219
|
1326 auto pixel = m_params.colourScale.getPixel(value);
|
Chris@1219
|
1327
|
Chris@1219
|
1328 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1219
|
1329 // SVDEBUG << "frequency " << freq << " for bin " << bin
|
Chris@1219
|
1330 // << " -> y = " << y << ", iy = " << iy << ", value = "
|
Chris@1219
|
1331 // << value << ", pixel " << pixel << "\n";
|
Chris@1219
|
1332 #endif
|
Chris@1219
|
1333
|
Chris@1219
|
1334 m_drawBuffer.setPixel(x, iy, pixel);
|
Chris@1097
|
1335 }
|
Chris@1121
|
1336
|
Chris@1121
|
1337 m_magRanges.push_back(magRange);
|
Chris@1218
|
1338
|
Chris@1218
|
1339 } else {
|
Chris@1218
|
1340 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1341 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1342 << ": pixel peak column for range " << sx0 << " to " << sx1
|
Chris@1218
|
1343 << " is empty" << endl;
|
Chris@1218
|
1344 #endif
|
Chris@1097
|
1345 }
|
Chris@1097
|
1346
|
Chris@1221
|
1347 double fractionComplete = double(xPixelCount) / double(w);
|
Chris@1097
|
1348 if (timer.outOfTime(fractionComplete)) {
|
Chris@1218
|
1349 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1350 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1351 << ": out of time" << endl;
|
Chris@1218
|
1352 #endif
|
Chris@1221
|
1353 updateTimings(timer, xPixelCount);
|
Chris@1221
|
1354 return xPixelCount;
|
Chris@1097
|
1355 }
|
Chris@1097
|
1356 }
|
Chris@1097
|
1357
|
Chris@1221
|
1358 updateTimings(timer, xPixelCount);
|
Chris@1221
|
1359 return xPixelCount;
|
Chris@1221
|
1360 }
|
Chris@1221
|
1361
|
Chris@1221
|
1362 void
|
Chris@1221
|
1363 Colour3DPlotRenderer::updateTimings(const RenderTimer &timer, int xPixelCount)
|
Chris@1221
|
1364 {
|
Chris@1237
|
1365 double secondsPerXPixel = timer.secondsPerItem(xPixelCount);
|
Chris@1221
|
1366
|
Chris@1237
|
1367 // valid if we have enough data points, or if the overall time is
|
Chris@1237
|
1368 // massively slow anyway (as we definitely need to warn about that)
|
Chris@1237
|
1369 bool valid = (xPixelCount > 20 || secondsPerXPixel > 0.01);
|
Chris@1237
|
1370
|
Chris@1237
|
1371 if (valid) {
|
Chris@1237
|
1372 m_secondsPerXPixel = secondsPerXPixel;
|
Chris@1237
|
1373 m_secondsPerXPixelValid = true;
|
Chris@1237
|
1374
|
Chris@1221
|
1375 #ifdef DEBUG_COLOUR_PLOT_REPAINT
|
Chris@1500
|
1376 SVDEBUG << "render " << m_sources.source
|
Chris@1500
|
1377 << ": across " << xPixelCount
|
Chris@1500
|
1378 << " x-pixels, seconds per x-pixel = "
|
Chris@1450
|
1379 << m_secondsPerXPixel << " (total = "
|
Chris@1499
|
1380 << (xPixelCount * m_secondsPerXPixel) << ")" << endl;
|
Chris@1221
|
1381 #endif
|
Chris@1237
|
1382 }
|
Chris@1097
|
1383 }
|
Chris@1097
|
1384
|
Chris@1079
|
1385 void
|
Chris@1095
|
1386 Colour3DPlotRenderer::recreateDrawBuffer(int w, int h)
|
Chris@1079
|
1387 {
|
Chris@1095
|
1388 m_drawBuffer = QImage(w, h, QImage::Format_Indexed8);
|
Chris@1079
|
1389
|
Chris@1095
|
1390 for (int pixel = 0; pixel < 256; ++pixel) {
|
Chris@1095
|
1391 m_drawBuffer.setColor
|
Chris@1095
|
1392 ((unsigned char)pixel,
|
Chris@1112
|
1393 m_params.colourScale.getColourForPixel
|
Chris@1112
|
1394 (pixel, m_params.colourRotation).rgb());
|
Chris@1079
|
1395 }
|
Chris@1079
|
1396
|
Chris@1079
|
1397 m_drawBuffer.fill(0);
|
Chris@1121
|
1398 m_magRanges.clear();
|
Chris@1079
|
1399 }
|
Chris@1079
|
1400
|
Chris@1095
|
1401 void
|
Chris@1095
|
1402 Colour3DPlotRenderer::clearDrawBuffer(int w, int h)
|
Chris@1095
|
1403 {
|
Chris@1095
|
1404 if (m_drawBuffer.width() < w || m_drawBuffer.height() != h) {
|
Chris@1095
|
1405 recreateDrawBuffer(w, h);
|
Chris@1095
|
1406 } else {
|
Chris@1095
|
1407 m_drawBuffer.fill(0);
|
Chris@1121
|
1408 m_magRanges.clear();
|
Chris@1095
|
1409 }
|
Chris@1095
|
1410 }
|
Chris@1079
|
1411
|
Chris@1139
|
1412 QRect
|
Chris@1139
|
1413 Colour3DPlotRenderer::findSimilarRegionExtents(QPoint p) const
|
Chris@1139
|
1414 {
|
Chris@1139
|
1415 QImage image = m_cache.getImage();
|
Chris@1139
|
1416 ImageRegionFinder finder;
|
Chris@1139
|
1417 QRect rect = finder.findRegionExtents(&image, p);
|
Chris@1139
|
1418 return rect;
|
Chris@1139
|
1419 }
|