annotate layer/Colour3DPlotRenderer.cpp @ 1140:ca910d8070c8 spectrogram-minor-refactor

Restore illuminateLocalFeatures in spectrogram
author Chris Cannam
date Fri, 05 Aug 2016 12:47:10 +0100
parents 2976f57164ac
children f19dcb288123
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@1131 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@1081 190 // the middle so that the region of interest appears first
Chris@1079 191
Chris@1079 192 //!!! (perhaps we should avoid doing this if past repaints
Chris@1079 193 //!!! have been fast enough to do the whole in one shot)
Chris@1079 194 if (x0 == 0 && x1 == v->getPaintWidth()) {
Chris@1079 195 x0 = int(x1 * 0.3);
Chris@1079 196 }
Chris@1079 197 }
Chris@1079 198
Chris@1079 199 if (m_cache.isValid()) {
Chris@1090 200
Chris@1079 201 // When rendering only a part of the cache, we need to make
Chris@1079 202 // sure that the part we're rendering is adjacent to (or
Chris@1079 203 // overlapping) a valid area of cache, if we have one. The
Chris@1079 204 // alternative is to ditch the valid area of cache and render
Chris@1079 205 // only the requested area, but that's risky because this can
Chris@1079 206 // happen when just waving the pointer over a small part of
Chris@1079 207 // the view -- if we lose the partly-built cache every time
Chris@1079 208 // the user does that, we'll never finish building it.
Chris@1079 209 int left = x0;
Chris@1079 210 int width = x1 - x0;
Chris@1079 211 bool isLeftOfValidArea = false;
Chris@1079 212 m_cache.adjustToTouchValidArea(left, width, isLeftOfValidArea);
Chris@1079 213 x0 = left;
Chris@1079 214 x1 = x0 + width;
Chris@1079 215
Chris@1079 216 // That call also told us whether we should be painting
Chris@1079 217 // sub-regions of our target region in right-to-left order in
Chris@1079 218 // order to ensure contiguity
Chris@1079 219 rightToLeft = isLeftOfValidArea;
Chris@1079 220 }
Chris@1075 221
Chris@1109 222 // Note, we always paint the full height to cache. We want to
Chris@1109 223 // ensure the cache is coherent without having to worry about
Chris@1109 224 // vertical matching of required and valid areas as well as
Chris@1109 225 // horizontal.
Chris@1094 226
Chris@1109 227 if (renderType == DrawBufferBinResolution) {
Chris@1109 228
Chris@1094 229 renderToCacheBinResolution(v, x0, x1 - x0);
Chris@1109 230
Chris@1109 231 } else { // must be DrawBufferPixelResolution, handled DirectTranslucent earlier
Chris@1109 232
Chris@1094 233 renderToCachePixelResolution(v, x0, x1 - x0, rightToLeft, timeConstrained);
Chris@1094 234 }
Chris@1079 235
Chris@1079 236 QRect pr = rect & m_cache.getValidArea();
Chris@1079 237 paint.drawImage(pr.x(), pr.y(), m_cache.getImage(),
Chris@1079 238 pr.x(), pr.y(), pr.width(), pr.height());
Chris@1079 239
Chris@1079 240 if (!timeConstrained && (pr != rect)) {
Chris@1079 241 //!!! on a first cut, there is a risk that this will happen
Chris@1079 242 //!!! when we are at start/end of model -- trap, report, and
Chris@1079 243 //!!! then fix
Chris@1079 244 throw std::logic_error("internal error: failed to render entire requested rect even when not time-constrained");
Chris@1079 245 }
Chris@1120 246
Chris@1122 247 MagnitudeRange range = m_magCache.getRange(reqx0, reqx1 - reqx0);
Chris@1120 248
Chris@1120 249 return { pr, range };
Chris@1079 250
Chris@1079 251 //!!! todo, here or in caller: illuminateLocalFeatures
Chris@1079 252
Chris@1123 253 //!!! todo: handle vertical range other than full range of column
Chris@1110 254
Chris@1079 255 //!!! fft model scaling?
Chris@1073 256 }
Chris@1073 257
Chris@1109 258 Colour3DPlotRenderer::RenderType
Chris@1113 259 Colour3DPlotRenderer::decideRenderType(const LayerGeometryProvider *v) const
Chris@1094 260 {
Chris@1100 261 const DenseThreeDimensionalModel *model = m_sources.source;
Chris@1109 262 if (!model || !v || !(v->getViewManager())) {
Chris@1109 263 return DrawBufferPixelResolution; // or anything
Chris@1109 264 }
Chris@1109 265
Chris@1094 266 int binResolution = model->getResolution();
Chris@1094 267 int zoomLevel = v->getZoomLevel();
Chris@1109 268 sv_samplerate_t modelRate = model->getSampleRate();
Chris@1109 269
Chris@1109 270 double rateRatio = v->getViewManager()->getMainModelSampleRate() / modelRate;
Chris@1109 271 double relativeBinResolution = binResolution * rateRatio;
Chris@1109 272
Chris@1109 273 if (m_params.binDisplay == BinDisplay::PeakFrequencies) {
Chris@1109 274 // no alternative works here
Chris@1109 275 return DrawBufferPixelResolution;
Chris@1109 276 }
Chris@1109 277
Chris@1109 278 if (!m_params.alwaysOpaque && !m_params.interpolate) {
Chris@1109 279
Chris@1109 280 // consider translucent option -- only if not smoothing & not
Chris@1109 281 // explicitly requested opaque & sufficiently zoomed-in
Chris@1109 282
Chris@1117 283 if (model->getHeight() * 3 < v->getPaintHeight() &&
Chris@1117 284 relativeBinResolution >= 3 * zoomLevel) {
Chris@1109 285 return DirectTranslucent;
Chris@1109 286 }
Chris@1109 287 }
Chris@1109 288
Chris@1109 289 if (relativeBinResolution > zoomLevel) {
Chris@1109 290 return DrawBufferBinResolution;
Chris@1109 291 } else {
Chris@1109 292 return DrawBufferPixelResolution;
Chris@1109 293 }
Chris@1109 294 }
Chris@1109 295
Chris@1138 296 ColumnOp::Column
Chris@1138 297 Colour3DPlotRenderer::getColumn(int sx, int minbin, int nbins) const
Chris@1138 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@1138 317
Chris@1138 318 ColumnOp::Column fullColumn = m_sources.source->getColumn(sx);
Chris@1138 319
Chris@1138 320 column = vector<float>(fullColumn.data() + minbin,
Chris@1138 321 fullColumn.data() + minbin + nbins);
Chris@1138 322
Chris@1138 323 column = ColumnOp::applyGain(column, m_params.scaleFactor);
Chris@1138 324
Chris@1138 325 column = ColumnOp::normalize(column, m_params.normalization);
Chris@1138 326 }
Chris@1138 327
Chris@1138 328 return column;
Chris@1138 329 }
Chris@1138 330
Chris@1121 331 MagnitudeRange
Chris@1113 332 Colour3DPlotRenderer::renderDirectTranslucent(const LayerGeometryProvider *v,
Chris@1109 333 QPainter &paint,
Chris@1109 334 QRect rect)
Chris@1109 335 {
Chris@1117 336 Profiler profiler("Colour3DPlotRenderer::renderDirectTranslucent");
Chris@1117 337
Chris@1121 338 MagnitudeRange magRange;
Chris@1121 339
Chris@1115 340 QPoint illuminatePos;
Chris@1115 341 bool illuminate = v->shouldIlluminateLocalFeatures
Chris@1115 342 (m_sources.verticalBinLayer, illuminatePos);
Chris@1109 343
Chris@1109 344 const DenseThreeDimensionalModel *model = m_sources.source;
Chris@1109 345
Chris@1109 346 int x0 = rect.left();
Chris@1109 347 int x1 = rect.right() + 1;
Chris@1109 348
Chris@1109 349 int h = v->getPaintHeight();
Chris@1109 350
Chris@1109 351 sv_frame_t modelStart = model->getStartFrame();
Chris@1109 352 sv_frame_t modelEnd = model->getEndFrame();
Chris@1109 353 int modelResolution = model->getResolution();
Chris@1109 354
Chris@1109 355 double rateRatio =
Chris@1109 356 v->getViewManager()->getMainModelSampleRate() / model->getSampleRate();
Chris@1109 357
Chris@1109 358 // the s-prefix values are source, i.e. model, column and bin numbers
Chris@1109 359 int sx0 = int((double(v->getFrameForX(x0)) / rateRatio - double(modelStart))
Chris@1109 360 / modelResolution);
Chris@1109 361 int sx1 = int((double(v->getFrameForX(x1)) / rateRatio - double(modelStart))
Chris@1109 362 / modelResolution);
Chris@1109 363
Chris@1109 364 int sh = model->getHeight();
Chris@1109 365
Chris@1109 366 const int buflen = 40;
Chris@1109 367 char labelbuf[buflen];
Chris@1109 368
Chris@1133 369 int minbin = m_sources.verticalBinLayer->getIBinForY(v, h);
Chris@1135 370 if (minbin >= sh) minbin = sh - 1;
Chris@1135 371 if (minbin < 0) minbin = 0;
Chris@1135 372
Chris@1135 373 int nbins = m_sources.verticalBinLayer->getIBinForY(v, 0) - minbin + 1;
Chris@1135 374 if (minbin + nbins > sh) nbins = sh - minbin;
Chris@1133 375
Chris@1109 376 int psx = -1;
Chris@1109 377
Chris@1109 378 vector<float> preparedColumn;
Chris@1109 379
Chris@1109 380 int modelWidth = model->getWidth();
Chris@1109 381
Chris@1109 382 for (int sx = sx0; sx <= sx1; ++sx) {
Chris@1109 383
Chris@1109 384 if (sx < 0 || sx >= modelWidth) {
Chris@1109 385 continue;
Chris@1109 386 }
Chris@1109 387
Chris@1109 388 if (sx != psx) {
Chris@1109 389
Chris@1138 390 // order:
Chris@1138 391 // get column -> scale -> normalise -> record extents ->
Chris@1138 392 // peak pick -> distribute/interpolate -> apply display gain
Chris@1109 393
Chris@1138 394 // this does the first three:
Chris@1138 395 preparedColumn = getColumn(sx, minbin, nbins);
Chris@1131 396
Chris@1131 397 magRange.sample(preparedColumn);
Chris@1109 398
Chris@1109 399 if (m_params.binDisplay == BinDisplay::PeakBins) {
Chris@1115 400 preparedColumn = ColumnOp::peakPick(preparedColumn);
Chris@1109 401 }
Chris@1109 402
Chris@1124 403 // Display gain belongs to the colour scale and is
Chris@1124 404 // applied by the colour scale object when mapping it
Chris@1124 405
Chris@1109 406 psx = sx;
Chris@1109 407 }
Chris@1109 408
Chris@1109 409 sv_frame_t fx = sx * modelResolution + modelStart;
Chris@1109 410
Chris@1109 411 if (fx + modelResolution <= modelStart || fx > modelEnd) continue;
Chris@1109 412
Chris@1109 413 int rx0 = v->getXForFrame(int(double(fx) * rateRatio));
Chris@1109 414 int rx1 = v->getXForFrame(int(double(fx + modelResolution + 1) * rateRatio));
Chris@1109 415
Chris@1109 416 int rw = rx1 - rx0;
Chris@1109 417 if (rw < 1) rw = 1;
Chris@1109 418
Chris@1109 419 bool showLabel = (rw > 10 &&
Chris@1109 420 paint.fontMetrics().width("0.000000") < rw - 3 &&
Chris@1109 421 paint.fontMetrics().height() < (h / sh));
Chris@1109 422
Chris@1135 423 for (int sy = minbin; sy < minbin + nbins; ++sy) {
Chris@1109 424
Chris@1109 425 int ry0 = m_sources.verticalBinLayer->getIYForBin(v, sy);
Chris@1109 426 int ry1 = m_sources.verticalBinLayer->getIYForBin(v, sy + 1);
Chris@1116 427
Chris@1116 428 if (m_params.invertVertical) {
Chris@1116 429 ry0 = h - ry0 - 1;
Chris@1116 430 ry1 = h - ry1 - 1;
Chris@1116 431 }
Chris@1116 432
Chris@1109 433 QRect r(rx0, ry1, rw, ry0 - ry1);
Chris@1109 434
Chris@1109 435 float value = preparedColumn[sy - minbin];
Chris@1112 436 QColor colour = m_params.colourScale.getColour(value,
Chris@1112 437 m_params.colourRotation);
Chris@1109 438
Chris@1109 439 if (rw == 1) {
Chris@1109 440 paint.setPen(colour);
Chris@1109 441 paint.setBrush(Qt::NoBrush);
Chris@1109 442 paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1);
Chris@1109 443 continue;
Chris@1109 444 }
Chris@1109 445
Chris@1109 446 QColor pen(255, 255, 255, 80);
Chris@1109 447 QColor brush(colour);
Chris@1109 448
Chris@1109 449 if (rw > 3 && r.height() > 3) {
Chris@1109 450 brush.setAlpha(160);
Chris@1109 451 }
Chris@1109 452
Chris@1109 453 paint.setPen(Qt::NoPen);
Chris@1109 454 paint.setBrush(brush);
Chris@1109 455
Chris@1115 456 if (illuminate) {
Chris@1115 457 if (r.contains(illuminatePos)) {
Chris@1115 458 paint.setPen(v->getForeground());
Chris@1115 459 }
Chris@1115 460 }
Chris@1109 461
Chris@1109 462 #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT
Chris@1109 463 // cerr << "rect " << r.x() << "," << r.y() << " "
Chris@1109 464 // << r.width() << "x" << r.height() << endl;
Chris@1109 465 #endif
Chris@1109 466
Chris@1109 467 paint.drawRect(r);
Chris@1109 468
Chris@1109 469 if (showLabel) {
Chris@1109 470 double value = model->getValueAt(sx, sy);
Chris@1109 471 snprintf(labelbuf, buflen, "%06f", value);
Chris@1109 472 QString text(labelbuf);
Chris@1109 473 PaintAssistant::drawVisibleText
Chris@1109 474 (v,
Chris@1109 475 paint,
Chris@1109 476 rx0 + 2,
Chris@1109 477 ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(),
Chris@1109 478 text,
Chris@1109 479 PaintAssistant::OutlinedText);
Chris@1109 480 }
Chris@1109 481 }
Chris@1109 482 }
Chris@1121 483
Chris@1121 484 return magRange;
Chris@1094 485 }
Chris@1094 486
Chris@1080 487 void
Chris@1113 488 Colour3DPlotRenderer::renderToCachePixelResolution(const LayerGeometryProvider *v,
Chris@1094 489 int x0, int repaintWidth,
Chris@1094 490 bool rightToLeft,
Chris@1094 491 bool timeConstrained)
Chris@1079 492 {
Chris@1117 493 Profiler profiler("Colour3DPlotRenderer::renderToCachePixelResolution");
Chris@1094 494 cerr << "renderToCachePixelResolution" << endl;
Chris@1094 495
Chris@1094 496 // Draw to the draw buffer, and then copy from there. The draw
Chris@1094 497 // buffer is at the same resolution as the target in the cache, so
Chris@1094 498 // no extra scaling needed.
Chris@1079 499
Chris@1100 500 const DenseThreeDimensionalModel *model = m_sources.source;
Chris@1079 501 if (!model || !model->isOK() || !model->isReady()) {
Chris@1079 502 throw std::logic_error("no source model provided, or model not ready");
Chris@1079 503 }
Chris@1079 504
Chris@1079 505 int h = v->getPaintHeight();
Chris@1079 506
Chris@1094 507 clearDrawBuffer(repaintWidth, h);
Chris@1079 508
Chris@1094 509 vector<int> binforx(repaintWidth);
Chris@1079 510 vector<double> binfory(h);
Chris@1079 511
Chris@1079 512 bool usePeaksCache = false;
Chris@1079 513 int binsPerPeak = 1;
Chris@1094 514 int zoomLevel = v->getZoomLevel();
Chris@1094 515 int binResolution = model->getResolution();
Chris@1079 516
Chris@1094 517 for (int x = 0; x < repaintWidth; ++x) {
Chris@1094 518 sv_frame_t f0 = v->getFrameForX(x0 + x);
Chris@1094 519 double s0 = double(f0 - model->getStartFrame()) / binResolution;
Chris@1094 520 binforx[x] = int(s0 + 0.0001);
Chris@1094 521 }
Chris@1080 522
Chris@1094 523 if (m_sources.peaks) { // peaks cache exists
Chris@1080 524
Chris@1094 525 binsPerPeak = m_sources.peaks->getColumnsPerPeak();
Chris@1094 526 usePeaksCache = (binResolution * binsPerPeak) < zoomLevel;
Chris@1094 527
Chris@1094 528 if (m_params.colourScale.getScale() ==
Chris@1105 529 ColourScaleType::Phase) {
Chris@1094 530 usePeaksCache = false;
Chris@1079 531 }
Chris@1079 532 }
Chris@1082 533
Chris@1094 534 cerr << "[PIX] zoomLevel = " << zoomLevel
Chris@1094 535 << ", binResolution " << binResolution
Chris@1094 536 << ", binsPerPeak " << binsPerPeak
Chris@1094 537 << ", peak cache " << m_sources.peaks
Chris@1094 538 << ", usePeaksCache = " << usePeaksCache
Chris@1094 539 << endl;
Chris@1094 540
Chris@1080 541 for (int y = 0; y < h; ++y) {
Chris@1090 542 binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1);
Chris@1080 543 }
Chris@1079 544
Chris@1097 545 int attainedWidth;
Chris@1097 546
Chris@1103 547 if (m_params.binDisplay == BinDisplay::PeakFrequencies) {
Chris@1097 548 attainedWidth = renderDrawBufferPeakFrequencies(v,
Chris@1097 549 repaintWidth,
Chris@1097 550 h,
Chris@1097 551 binforx,
Chris@1097 552 binfory,
Chris@1097 553 rightToLeft,
Chris@1097 554 timeConstrained);
Chris@1097 555
Chris@1097 556 } else {
Chris@1097 557 attainedWidth = renderDrawBuffer(repaintWidth,
Chris@1080 558 h,
Chris@1080 559 binforx,
Chris@1080 560 binfory,
Chris@1080 561 usePeaksCache,
Chris@1080 562 rightToLeft,
Chris@1080 563 timeConstrained);
Chris@1097 564 }
Chris@1083 565
Chris@1094 566 if (attainedWidth == 0) return;
Chris@1084 567
Chris@1094 568 // draw buffer is pixel resolution, no scaling factors or padding involved
Chris@1084 569
Chris@1084 570 int paintedLeft = x0;
Chris@1084 571 if (rightToLeft) {
Chris@1084 572 paintedLeft += (repaintWidth - attainedWidth);
Chris@1084 573 }
Chris@1084 574
Chris@1094 575 m_cache.drawImage(paintedLeft, attainedWidth,
Chris@1094 576 m_drawBuffer,
Chris@1094 577 paintedLeft - x0, attainedWidth);
Chris@1121 578
Chris@1121 579 for (int i = 0; in_range_for(m_magRanges, i); ++i) {
Chris@1121 580 m_magCache.sampleColumn(i, m_magRanges.at(i));
Chris@1121 581 }
Chris@1094 582 }
Chris@1084 583
Chris@1094 584 void
Chris@1113 585 Colour3DPlotRenderer::renderToCacheBinResolution(const LayerGeometryProvider *v,
Chris@1094 586 int x0, int repaintWidth)
Chris@1094 587 {
Chris@1117 588 Profiler profiler("Colour3DPlotRenderer::renderToCacheBinResolution");
Chris@1094 589 cerr << "renderToCacheBinResolution" << endl;
Chris@1094 590
Chris@1094 591 // Draw to the draw buffer, and then scale-copy from there. Draw
Chris@1094 592 // buffer is at bin resolution, i.e. buffer x == source column
Chris@1094 593 // number. We use toolkit smooth scaling for interpolation.
Chris@1084 594
Chris@1100 595 const DenseThreeDimensionalModel *model = m_sources.source;
Chris@1094 596 if (!model || !model->isOK() || !model->isReady()) {
Chris@1094 597 throw std::logic_error("no source model provided, or model not ready");
Chris@1094 598 }
Chris@1094 599
Chris@1094 600 // The draw buffer will contain a fragment at bin resolution. We
Chris@1094 601 // need to ensure that it starts and ends at points where a
Chris@1094 602 // time-bin boundary occurs at an exact pixel boundary, and with a
Chris@1094 603 // certain amount of overlap across existing pixels so that we can
Chris@1094 604 // scale and draw from it without smoothing errors at the edges.
Chris@1094 605
Chris@1094 606 // If (getFrameForX(x) / increment) * increment ==
Chris@1094 607 // getFrameForX(x), then x is a time-bin boundary. We want two
Chris@1094 608 // such boundaries at either side of the draw buffer -- one which
Chris@1094 609 // we draw up to, and one which we subsequently crop at.
Chris@1094 610
Chris@1094 611 sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1;
Chris@1094 612 sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1;
Chris@1094 613
Chris@1094 614 int drawBufferWidth;
Chris@1094 615 int binResolution = model->getResolution();
Chris@1094 616
Chris@1094 617 for (int x = x0; ; --x) {
Chris@1094 618 sv_frame_t f = v->getFrameForX(x);
Chris@1094 619 if ((f / binResolution) * binResolution == f) {
Chris@1094 620 if (leftCropFrame == -1) leftCropFrame = f;
Chris@1094 621 else if (x < x0 - 2) {
Chris@1094 622 leftBoundaryFrame = f;
Chris@1094 623 break;
Chris@1094 624 }
Chris@1094 625 }
Chris@1094 626 }
Chris@1094 627 for (int x = x0 + repaintWidth; ; ++x) {
Chris@1094 628 sv_frame_t f = v->getFrameForX(x);
Chris@1094 629 if ((f / binResolution) * binResolution == f) {
Chris@1094 630 if (rightCropFrame == -1) rightCropFrame = f;
Chris@1094 631 else if (x > x0 + repaintWidth + 2) {
Chris@1094 632 rightBoundaryFrame = f;
Chris@1094 633 break;
Chris@1094 634 }
Chris@1094 635 }
Chris@1094 636 }
Chris@1094 637 drawBufferWidth = int
Chris@1094 638 ((rightBoundaryFrame - leftBoundaryFrame) / binResolution);
Chris@1094 639
Chris@1094 640 int h = v->getPaintHeight();
Chris@1094 641
Chris@1095 642 // For our purposes here, the draw buffer needs to be exactly our
Chris@1095 643 // target size (so we recreate always rather than just clear it)
Chris@1095 644
Chris@1095 645 recreateDrawBuffer(drawBufferWidth, h);
Chris@1094 646
Chris@1094 647 vector<int> binforx(drawBufferWidth);
Chris@1094 648 vector<double> binfory(h);
Chris@1094 649
Chris@1094 650 for (int x = 0; x < drawBufferWidth; ++x) {
Chris@1094 651 binforx[x] = int(leftBoundaryFrame / binResolution) + x;
Chris@1094 652 }
Chris@1094 653
Chris@1094 654 cerr << "[BIN] binResolution " << binResolution
Chris@1094 655 << endl;
Chris@1094 656
Chris@1094 657 for (int y = 0; y < h; ++y) {
Chris@1094 658 binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1);
Chris@1094 659 }
Chris@1094 660
Chris@1094 661 int attainedWidth = renderDrawBuffer(drawBufferWidth,
Chris@1094 662 h,
Chris@1094 663 binforx,
Chris@1094 664 binfory,
Chris@1094 665 false,
Chris@1094 666 false,
Chris@1094 667 false);
Chris@1094 668
Chris@1094 669 if (attainedWidth == 0) return;
Chris@1094 670
Chris@1094 671 int scaledLeft = v->getXForFrame(leftBoundaryFrame);
Chris@1094 672 int scaledRight = v->getXForFrame(rightBoundaryFrame);
Chris@1095 673
Chris@1095 674 cerr << "scaling draw buffer from width " << m_drawBuffer.width()
Chris@1095 675 << " to " << (scaledRight - scaledLeft) << " (nb drawBufferWidth = "
Chris@1095 676 << drawBufferWidth << ")" << endl;
Chris@1094 677
Chris@1094 678 QImage scaled = m_drawBuffer.scaled
Chris@1094 679 (scaledRight - scaledLeft, h,
Chris@1094 680 Qt::IgnoreAspectRatio, (m_params.interpolate ?
Chris@1094 681 Qt::SmoothTransformation :
Chris@1094 682 Qt::FastTransformation));
Chris@1084 683
Chris@1094 684 int scaledLeftCrop = v->getXForFrame(leftCropFrame);
Chris@1094 685 int scaledRightCrop = v->getXForFrame(rightCropFrame);
Chris@1094 686
Chris@1094 687 int targetLeft = scaledLeftCrop;
Chris@1094 688 if (targetLeft < 0) {
Chris@1094 689 targetLeft = 0;
Chris@1094 690 }
Chris@1094 691
Chris@1094 692 int targetWidth = scaledRightCrop - targetLeft;
Chris@1094 693 if (targetLeft + targetWidth > m_cache.getSize().width()) {
Chris@1094 694 targetWidth = m_cache.getSize().width() - targetLeft;
Chris@1094 695 }
Chris@1094 696
Chris@1094 697 int sourceLeft = targetLeft - scaledLeft;
Chris@1094 698 if (sourceLeft < 0) {
Chris@1094 699 sourceLeft = 0;
Chris@1094 700 }
Chris@1094 701
Chris@1094 702 cerr << "repaintWidth = " << repaintWidth
Chris@1094 703 << ", targetWidth = " << targetWidth << endl;
Chris@1094 704
Chris@1094 705 if (targetWidth > 0) {
Chris@1136 706 // we are copying from an image that has already been scaled,
Chris@1136 707 // hence using the same width in both geometries
Chris@1094 708 m_cache.drawImage(targetLeft, targetWidth,
Chris@1094 709 scaled,
Chris@1136 710 sourceLeft, targetWidth);
Chris@1084 711 }
Chris@1121 712
Chris@1121 713 for (int i = 0; i < targetWidth; ++i) {
Chris@1136 714 // but the mag range vector has not been scaled
Chris@1136 715 int sourceIx = int((double(i + sourceLeft) / scaled.width())
Chris@1136 716 * int(m_magRanges.size()));
Chris@1136 717 // int sourceIx = int((double(i) / targetWidth) * sourceWidth);
Chris@1136 718 cerr << "mag range target ix = " << i << ", source ix = "
Chris@1136 719 << sourceIx << ", of " << m_magRanges.size() << endl;
Chris@1121 720 if (in_range_for(m_magRanges, sourceIx)) {
Chris@1121 721 m_magCache.sampleColumn(i, m_magRanges.at(sourceIx));
Chris@1121 722 }
Chris@1121 723 }
Chris@1079 724 }
Chris@1083 725
Chris@1083 726 int
Chris@1083 727 Colour3DPlotRenderer::renderDrawBuffer(int w, int h,
Chris@1083 728 const vector<int> &binforx,
Chris@1083 729 const vector<double> &binfory,
Chris@1083 730 bool usePeaksCache,
Chris@1083 731 bool rightToLeft,
Chris@1083 732 bool timeConstrained)
Chris@1083 733 {
Chris@1083 734 // Callers must have checked that the appropriate subset of
Chris@1083 735 // Sources data members are set for the supplied flags (e.g. that
Chris@1083 736 // peaks model exists if usePeaksCache)
Chris@1083 737
Chris@1083 738 RenderTimer timer(timeConstrained ?
Chris@1083 739 RenderTimer::FastRender :
Chris@1083 740 RenderTimer::NoTimeout);
Chris@1083 741
Chris@1083 742 int divisor = 1;
Chris@1100 743 const DenseThreeDimensionalModel *sourceModel = m_sources.source;
Chris@1083 744 if (usePeaksCache) {
Chris@1083 745 divisor = m_sources.peaks->getColumnsPerPeak();
Chris@1083 746 sourceModel = m_sources.peaks;
Chris@1083 747 }
Chris@1083 748
Chris@1135 749 int sh = sourceModel->getHeight();
Chris@1135 750
Chris@1135 751 int minbin = int(binfory[0] + 0.0001);
Chris@1135 752 if (minbin >= sh) minbin = sh - 1;
Chris@1135 753 if (minbin < 0) minbin = 0;
Chris@1135 754
Chris@1135 755 int nbins = int(binfory[h-1]) - minbin + 1;
Chris@1135 756 if (minbin + nbins > sh) nbins = sh - minbin;
Chris@1135 757
Chris@1083 758 int psx = -1;
Chris@1083 759
Chris@1083 760 int start = 0;
Chris@1083 761 int finish = w;
Chris@1083 762 int step = 1;
Chris@1083 763
Chris@1083 764 if (rightToLeft) {
Chris@1083 765 start = w-1;
Chris@1083 766 finish = -1;
Chris@1083 767 step = -1;
Chris@1083 768 }
Chris@1083 769
Chris@1083 770 int columnCount = 0;
Chris@1083 771
Chris@1083 772 vector<float> preparedColumn;
Chris@1094 773
Chris@1094 774 int modelWidth = sourceModel->getWidth();
Chris@1121 775
Chris@1121 776 cerr << "modelWidth " << modelWidth << ", divisor " << divisor << endl;
Chris@1121 777
Chris@1083 778 for (int x = start; x != finish; x += step) {
Chris@1083 779
Chris@1083 780 // x is the on-canvas pixel coord; sx (later) will be the
Chris@1083 781 // source column index
Chris@1083 782
Chris@1083 783 ++columnCount;
Chris@1120 784
Chris@1123 785 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1121 786 cerr << "x = " << x << ", binforx[x] = " << binforx[x] << endl;
Chris@1120 787 #endif
Chris@1083 788
Chris@1083 789 if (binforx[x] < 0) continue;
Chris@1083 790
Chris@1083 791 int sx0 = binforx[x] / divisor;
Chris@1083 792 int sx1 = sx0;
Chris@1083 793 if (x+1 < w) sx1 = binforx[x+1] / divisor;
Chris@1083 794 if (sx0 < 0) sx0 = sx1 - 1;
Chris@1083 795 if (sx0 < 0) continue;
Chris@1083 796 if (sx1 <= sx0) sx1 = sx0 + 1;
Chris@1083 797
Chris@1083 798 vector<float> pixelPeakColumn;
Chris@1121 799 MagnitudeRange magRange;
Chris@1083 800
Chris@1083 801 for (int sx = sx0; sx < sx1; ++sx) {
Chris@1083 802
Chris@1123 803 #ifdef DEBUG_COLOUR_PLOT_REPAINT
Chris@1094 804 cerr << "sx = " << sx << endl;
Chris@1083 805 #endif
Chris@1083 806
Chris@1094 807 if (sx < 0 || sx >= modelWidth) {
Chris@1083 808 continue;
Chris@1083 809 }
Chris@1083 810
Chris@1083 811 if (sx != psx) {
Chris@1138 812
Chris@1138 813 // order:
Chris@1138 814 // get column -> scale -> normalise -> record extents ->
Chris@1138 815 // peak pick -> distribute/interpolate -> apply display gain
Chris@1083 816
Chris@1138 817 // this does the first three:
Chris@1138 818 ColumnOp::Column column = getColumn(sx, minbin, nbins);
Chris@1083 819
Chris@1131 820 magRange.sample(column);
Chris@1138 821
Chris@1103 822 if (m_params.binDisplay == BinDisplay::PeakBins) {
Chris@1083 823 column = ColumnOp::peakPick(column);
Chris@1083 824 }
Chris@1083 825
Chris@1083 826 preparedColumn =
Chris@1124 827 ColumnOp::distribute(column,
Chris@1083 828 h,
Chris@1083 829 binfory,
Chris@1083 830 minbin,
Chris@1083 831 m_params.interpolate);
Chris@1124 832
Chris@1124 833 // Display gain belongs to the colour scale and is
Chris@1124 834 // applied by the colour scale object when mapping it
Chris@1083 835
Chris@1083 836 psx = sx;
Chris@1083 837 }
Chris@1083 838
Chris@1083 839 if (sx == sx0) {
Chris@1083 840 pixelPeakColumn = preparedColumn;
Chris@1083 841 } else {
Chris@1083 842 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
Chris@1083 843 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
Chris@1083 844 preparedColumn[i]);
Chris@1083 845 }
Chris@1083 846 }
Chris@1083 847 }
Chris@1083 848
Chris@1083 849 if (!pixelPeakColumn.empty()) {
Chris@1121 850
Chris@1083 851 for (int y = 0; y < h; ++y) {
Chris@1116 852 int py;
Chris@1116 853 if (m_params.invertVertical) {
Chris@1116 854 py = y;
Chris@1116 855 } else {
Chris@1116 856 py = h - y - 1;
Chris@1116 857 }
Chris@1083 858 m_drawBuffer.setPixel
Chris@1083 859 (x,
Chris@1116 860 py,
Chris@1083 861 m_params.colourScale.getPixel(pixelPeakColumn[y]));
Chris@1083 862 }
Chris@1121 863
Chris@1121 864 m_magRanges.push_back(magRange);
Chris@1083 865 }
Chris@1083 866
Chris@1083 867 double fractionComplete = double(columnCount) / double(w);
Chris@1083 868 if (timer.outOfTime(fractionComplete)) {
Chris@1121 869 cerr << "out of time" << endl;
Chris@1083 870 return columnCount;
Chris@1083 871 }
Chris@1083 872 }
Chris@1083 873
Chris@1083 874 return columnCount;
Chris@1083 875 }
Chris@1083 876
Chris@1097 877 int
Chris@1113 878 Colour3DPlotRenderer::renderDrawBufferPeakFrequencies(const LayerGeometryProvider *v,
Chris@1097 879 int w, int h,
Chris@1097 880 const vector<int> &binforx,
Chris@1097 881 const vector<double> &binfory,
Chris@1097 882 bool rightToLeft,
Chris@1097 883 bool timeConstrained)
Chris@1097 884 {
Chris@1097 885 // Callers must have checked that the appropriate subset of
Chris@1097 886 // Sources data members are set for the supplied flags (e.g. that
Chris@1097 887 // fft model exists)
Chris@1097 888
Chris@1097 889 RenderTimer timer(timeConstrained ?
Chris@1097 890 RenderTimer::FastRender :
Chris@1097 891 RenderTimer::NoTimeout);
Chris@1097 892
Chris@1135 893 const FFTModel *fft = m_sources.fft;
Chris@1135 894
Chris@1135 895 int sh = fft->getHeight();
Chris@1135 896
Chris@1097 897 int minbin = int(binfory[0] + 0.0001);
Chris@1135 898 if (minbin >= sh) minbin = sh - 1;
Chris@1097 899 if (minbin < 0) minbin = 0;
Chris@1097 900
Chris@1135 901 int nbins = int(binfory[h-1]) - minbin + 1;
Chris@1135 902 if (minbin + nbins > sh) nbins = sh - minbin;
Chris@1097 903
Chris@1097 904 FFTModel::PeakSet peakfreqs;
Chris@1097 905
Chris@1097 906 int psx = -1;
Chris@1097 907
Chris@1097 908 int start = 0;
Chris@1097 909 int finish = w;
Chris@1097 910 int step = 1;
Chris@1097 911
Chris@1097 912 if (rightToLeft) {
Chris@1097 913 start = w-1;
Chris@1097 914 finish = -1;
Chris@1097 915 step = -1;
Chris@1097 916 }
Chris@1097 917
Chris@1097 918 int columnCount = 0;
Chris@1097 919
Chris@1097 920 vector<float> preparedColumn;
Chris@1097 921
Chris@1097 922 int modelWidth = fft->getWidth();
Chris@1097 923 cerr << "modelWidth " << modelWidth << endl;
Chris@1097 924
Chris@1135 925 double minFreq =
Chris@1135 926 (double(minbin) * fft->getSampleRate()) / fft->getFFTSize();
Chris@1135 927 double maxFreq =
Chris@1135 928 (double(minbin + nbins - 1) * fft->getSampleRate()) / fft->getFFTSize();
Chris@1097 929
Chris@1103 930 bool logarithmic = (m_params.binScale == BinScale::Log);
Chris@1097 931
Chris@1097 932 for (int x = start; x != finish; x += step) {
Chris@1097 933
Chris@1097 934 // x is the on-canvas pixel coord; sx (later) will be the
Chris@1097 935 // source column index
Chris@1097 936
Chris@1097 937 ++columnCount;
Chris@1097 938
Chris@1097 939 if (binforx[x] < 0) continue;
Chris@1097 940
Chris@1097 941 int sx0 = binforx[x];
Chris@1097 942 int sx1 = sx0;
Chris@1097 943 if (x+1 < w) sx1 = binforx[x+1];
Chris@1097 944 if (sx0 < 0) sx0 = sx1 - 1;
Chris@1097 945 if (sx0 < 0) continue;
Chris@1097 946 if (sx1 <= sx0) sx1 = sx0 + 1;
Chris@1097 947
Chris@1097 948 vector<float> pixelPeakColumn;
Chris@1121 949 MagnitudeRange magRange;
Chris@1097 950
Chris@1097 951 for (int sx = sx0; sx < sx1; ++sx) {
Chris@1097 952
Chris@1097 953 if (sx < 0 || sx >= modelWidth) {
Chris@1097 954 continue;
Chris@1097 955 }
Chris@1097 956
Chris@1097 957 if (sx != psx) {
Chris@1138 958 preparedColumn = getColumn(sx, minbin, nbins);
Chris@1131 959 magRange.sample(preparedColumn);
Chris@1097 960 psx = sx;
Chris@1097 961 }
Chris@1097 962
Chris@1097 963 if (sx == sx0) {
Chris@1097 964 pixelPeakColumn = preparedColumn;
Chris@1097 965 peakfreqs = fft->getPeakFrequencies(FFTModel::AllPeaks, sx,
Chris@1135 966 minbin, minbin + nbins - 1);
Chris@1097 967 } else {
Chris@1097 968 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
Chris@1097 969 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
Chris@1097 970 preparedColumn[i]);
Chris@1097 971 }
Chris@1097 972 }
Chris@1097 973 }
Chris@1097 974
Chris@1097 975 if (!pixelPeakColumn.empty()) {
Chris@1121 976
Chris@1097 977 for (FFTModel::PeakSet::const_iterator pi = peakfreqs.begin();
Chris@1097 978 pi != peakfreqs.end(); ++pi) {
Chris@1097 979
Chris@1097 980 int bin = pi->first;
Chris@1097 981 double freq = pi->second;
Chris@1097 982
Chris@1097 983 if (bin < minbin) continue;
Chris@1135 984 if (bin >= minbin + nbins) break;
Chris@1097 985
Chris@1097 986 double value = pixelPeakColumn[bin - minbin];
Chris@1097 987
Chris@1097 988 double y = v->getYForFrequency
Chris@1097 989 (freq, minFreq, maxFreq, logarithmic);
Chris@1097 990
Chris@1097 991 int iy = int(y + 0.5);
Chris@1097 992 if (iy < 0 || iy >= h) continue;
Chris@1097 993
Chris@1097 994 m_drawBuffer.setPixel
Chris@1097 995 (x,
Chris@1097 996 iy,
Chris@1097 997 m_params.colourScale.getPixel(value));
Chris@1097 998 }
Chris@1121 999
Chris@1121 1000 m_magRanges.push_back(magRange);
Chris@1097 1001 }
Chris@1097 1002
Chris@1097 1003 double fractionComplete = double(columnCount) / double(w);
Chris@1097 1004 if (timer.outOfTime(fractionComplete)) {
Chris@1097 1005 return columnCount;
Chris@1097 1006 }
Chris@1097 1007 }
Chris@1097 1008
Chris@1097 1009 return columnCount;
Chris@1097 1010 }
Chris@1097 1011
Chris@1079 1012 void
Chris@1095 1013 Colour3DPlotRenderer::recreateDrawBuffer(int w, int h)
Chris@1079 1014 {
Chris@1095 1015 m_drawBuffer = QImage(w, h, QImage::Format_Indexed8);
Chris@1079 1016
Chris@1095 1017 for (int pixel = 0; pixel < 256; ++pixel) {
Chris@1095 1018 m_drawBuffer.setColor
Chris@1095 1019 ((unsigned char)pixel,
Chris@1112 1020 m_params.colourScale.getColourForPixel
Chris@1112 1021 (pixel, m_params.colourRotation).rgb());
Chris@1079 1022 }
Chris@1079 1023
Chris@1079 1024 m_drawBuffer.fill(0);
Chris@1121 1025 m_magRanges.clear();
Chris@1079 1026 }
Chris@1079 1027
Chris@1095 1028 void
Chris@1095 1029 Colour3DPlotRenderer::clearDrawBuffer(int w, int h)
Chris@1095 1030 {
Chris@1095 1031 if (m_drawBuffer.width() < w || m_drawBuffer.height() != h) {
Chris@1095 1032 recreateDrawBuffer(w, h);
Chris@1095 1033 } else {
Chris@1095 1034 m_drawBuffer.fill(0);
Chris@1121 1035 m_magRanges.clear();
Chris@1095 1036 }
Chris@1095 1037 }
Chris@1079 1038
Chris@1139 1039 QRect
Chris@1139 1040 Colour3DPlotRenderer::findSimilarRegionExtents(QPoint p) const
Chris@1139 1041 {
Chris@1139 1042 QImage image = m_cache.getImage();
Chris@1139 1043 ImageRegionFinder finder;
Chris@1139 1044 QRect rect = finder.findRegionExtents(&image, p);
Chris@1139 1045 return rect;
Chris@1139 1046 }