annotate layer/Colour3DPlotRenderer.cpp @ 1213:34df6ff25472 3.0-integration

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