annotate layer/Colour3DPlotRenderer.cpp @ 1236:784c92f93fb6

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