annotate layer/Colour3DPlotRenderer.cpp @ 1116:e7a07cd63d21 spectrogram-minor-refactor

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