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