annotate layer/Colour3DPlotRenderer.cpp @ 1219:c06861dec245

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