Chris@1071: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1071: Chris@1071: /* Chris@1071: Sonic Visualiser Chris@1071: An audio file viewer and annotation editor. Chris@1071: Centre for Digital Music, Queen Mary, University of London. Chris@1071: This file copyright 2006-2016 Chris Cannam and QMUL. Chris@1071: Chris@1071: This program is free software; you can redistribute it and/or Chris@1071: modify it under the terms of the GNU General Public License as Chris@1071: published by the Free Software Foundation; either version 2 of the Chris@1071: License, or (at your option) any later version. See the file Chris@1071: COPYING included with this distribution for more information. Chris@1071: */ Chris@1071: Chris@1071: #include "Colour3DPlotRenderer.h" Chris@1074: #include "RenderTimer.h" Chris@1071: Chris@1117: #include "base/Profiler.h" Chris@1117: Chris@1075: #include "data/model/DenseThreeDimensionalModel.h" Chris@1075: #include "data/model/Dense3DModelPeakCache.h" Chris@1075: #include "data/model/FFTModel.h" Chris@1075: Chris@1077: #include "LayerGeometryProvider.h" Chris@1082: #include "VerticalBinLayer.h" Chris@1109: #include "PaintAssistant.h" Chris@1109: Chris@1109: #include "view/ViewManager.h" // for main model sample rate. Pity Chris@1075: Chris@1079: #include Chris@1079: Chris@1131: //#define DEBUG_COLOUR_PLOT_REPAINT 1 Chris@1094: Chris@1079: using namespace std; Chris@1079: Chris@1073: Colour3DPlotRenderer::RenderResult Chris@1113: Colour3DPlotRenderer::render(const LayerGeometryProvider *v, QPainter &paint, QRect rect) Chris@1076: { Chris@1090: return render(v, paint, rect, false); Chris@1076: } Chris@1076: Chris@1076: Colour3DPlotRenderer::RenderResult Chris@1113: Colour3DPlotRenderer::renderTimeConstrained(const LayerGeometryProvider *v, Chris@1090: QPainter &paint, QRect rect) Chris@1076: { Chris@1090: return render(v, paint, rect, true); Chris@1076: } Chris@1076: Chris@1096: QRect Chris@1121: Colour3DPlotRenderer::getLargestUncachedRect(const LayerGeometryProvider *v) Chris@1096: { Chris@1121: RenderType renderType = decideRenderType(v); Chris@1121: Chris@1121: if (renderType == DirectTranslucent) { Chris@1121: return QRect(); // never cached Chris@1121: } Chris@1121: Chris@1096: int h = m_cache.getSize().height(); Chris@1096: Chris@1096: QRect areaLeft(0, 0, m_cache.getValidLeft(), h); Chris@1096: QRect areaRight(m_cache.getValidRight(), 0, Chris@1096: m_cache.getSize().width() - m_cache.getValidRight(), h); Chris@1096: Chris@1096: if (areaRight.width() > areaLeft.width()) { Chris@1096: return areaRight; Chris@1096: } else { Chris@1096: return areaLeft; Chris@1096: } Chris@1096: } Chris@1096: Chris@1122: bool Chris@1122: Colour3DPlotRenderer::geometryChanged(const LayerGeometryProvider *v) Chris@1122: { Chris@1122: RenderType renderType = decideRenderType(v); Chris@1122: Chris@1122: if (renderType == DirectTranslucent) { Chris@1122: return true; // never cached Chris@1122: } Chris@1122: Chris@1122: if (m_cache.getSize() == v->getPaintSize() && Chris@1122: m_cache.getZoomLevel() == v->getZoomLevel() && Chris@1122: m_cache.getStartFrame() == v->getStartFrame()) { Chris@1122: return false; Chris@1122: } else { Chris@1122: return true; Chris@1122: } Chris@1122: } Chris@1122: Chris@1076: Colour3DPlotRenderer::RenderResult Chris@1113: Colour3DPlotRenderer::render(const LayerGeometryProvider *v, Chris@1090: QPainter &paint, QRect rect, bool timeConstrained) Chris@1073: { Chris@1109: RenderType renderType = decideRenderType(v); Chris@1109: Chris@1109: if (renderType != DrawBufferPixelResolution) { Chris@1109: // Rendering should be fast in bin-resolution and direct draw Chris@1109: // cases because we are quite well zoomed-in, and the sums are Chris@1109: // easier this way. Calculating boundaries later will be Chris@1109: // fiddly for partial paints otherwise. Chris@1109: timeConstrained = false; Chris@1109: } Chris@1109: Chris@1079: int x0 = v->getXForViewX(rect.x()); Chris@1079: int x1 = v->getXForViewX(rect.x() + rect.width()); Chris@1079: if (x0 < 0) x0 = 0; Chris@1079: if (x1 > v->getPaintWidth()) x1 = v->getPaintWidth(); Chris@1079: Chris@1120: sv_frame_t startFrame = v->getStartFrame(); Chris@1120: Chris@1079: m_cache.resize(v->getPaintSize()); Chris@1079: m_cache.setZoomLevel(v->getZoomLevel()); Chris@1079: Chris@1119: m_magCache.resize(v->getPaintSize().width()); Chris@1119: m_magCache.setZoomLevel(v->getZoomLevel()); Chris@1119: Chris@1120: if (renderType == DirectTranslucent) { Chris@1121: MagnitudeRange range = renderDirectTranslucent(v, paint, rect); Chris@1121: return { rect, range }; Chris@1120: } Chris@1120: Chris@1123: #ifdef DEBUG_COLOUR_PLOT_REPAINT Chris@1090: cerr << "cache start " << m_cache.getStartFrame() Chris@1090: << " valid left " << m_cache.getValidLeft() Chris@1090: << " valid right " << m_cache.getValidRight() Chris@1094: << endl; Chris@1094: cerr << " view start " << startFrame Chris@1090: << " x0 " << x0 Chris@1090: << " x1 " << x1 Chris@1090: << endl; Chris@1123: #endif Chris@1090: Chris@1079: if (m_cache.isValid()) { // some part of the cache is valid Chris@1079: Chris@1079: if (v->getXForFrame(m_cache.getStartFrame()) == Chris@1079: v->getXForFrame(startFrame) && Chris@1079: m_cache.getValidLeft() <= x0 && Chris@1079: m_cache.getValidRight() >= x1) { Chris@1090: Chris@1123: #ifdef DEBUG_COLOUR_PLOT_REPAINT Chris@1090: cerr << "cache hit" << endl; Chris@1123: #endif Chris@1090: Chris@1079: // cache is valid for the complete requested area Chris@1079: paint.drawImage(rect, m_cache.getImage(), rect); Chris@1119: Chris@1122: MagnitudeRange range = m_magCache.getRange(x0, x1 - x0); Chris@1119: Chris@1119: return { rect, range }; Chris@1079: Chris@1079: } else { Chris@1123: #ifdef DEBUG_COLOUR_PLOT_REPAINT Chris@1090: cerr << "cache partial hit" << endl; Chris@1123: #endif Chris@1090: Chris@1079: // cache doesn't begin at the right frame or doesn't Chris@1079: // contain the complete view, but might be scrollable or Chris@1079: // partially usable Chris@1090: m_cache.scrollTo(v, startFrame); Chris@1119: m_magCache.scrollTo(v, startFrame); Chris@1079: Chris@1079: // if we are not time-constrained, then we want to paint Chris@1081: // the whole area in one go; we don't return a partial Chris@1081: // paint. To avoid providing the more complex logic to Chris@1081: // handle painting discontiguous areas, if the only valid Chris@1079: // part of cache is in the middle, just make the whole Chris@1079: // thing invalid and start again. Chris@1079: if (!timeConstrained) { Chris@1079: if (m_cache.getValidLeft() > x0 && Chris@1079: m_cache.getValidRight() < x1) { Chris@1079: m_cache.invalidate(); Chris@1079: } Chris@1079: } Chris@1079: } Chris@1090: } else { Chris@1118: // cache is completely invalid Chris@1090: m_cache.setStartFrame(startFrame); Chris@1119: m_magCache.setStartFrame(startFrame); Chris@1075: } Chris@1075: Chris@1079: bool rightToLeft = false; Chris@1079: Chris@1122: int reqx0 = x0; Chris@1122: int reqx1 = x1; Chris@1122: Chris@1079: if (!m_cache.isValid() && timeConstrained) { Chris@1081: // When rendering the whole area, in a context where we might Chris@1081: // not be able to complete the work, start from somewhere near Chris@1081: // the middle so that the region of interest appears first Chris@1079: Chris@1079: //!!! (perhaps we should avoid doing this if past repaints Chris@1079: //!!! have been fast enough to do the whole in one shot) Chris@1079: if (x0 == 0 && x1 == v->getPaintWidth()) { Chris@1079: x0 = int(x1 * 0.3); Chris@1079: } Chris@1079: } Chris@1079: Chris@1079: if (m_cache.isValid()) { Chris@1090: Chris@1079: // When rendering only a part of the cache, we need to make Chris@1079: // sure that the part we're rendering is adjacent to (or Chris@1079: // overlapping) a valid area of cache, if we have one. The Chris@1079: // alternative is to ditch the valid area of cache and render Chris@1079: // only the requested area, but that's risky because this can Chris@1079: // happen when just waving the pointer over a small part of Chris@1079: // the view -- if we lose the partly-built cache every time Chris@1079: // the user does that, we'll never finish building it. Chris@1079: int left = x0; Chris@1079: int width = x1 - x0; Chris@1079: bool isLeftOfValidArea = false; Chris@1079: m_cache.adjustToTouchValidArea(left, width, isLeftOfValidArea); Chris@1079: x0 = left; Chris@1079: x1 = x0 + width; Chris@1079: Chris@1079: // That call also told us whether we should be painting Chris@1079: // sub-regions of our target region in right-to-left order in Chris@1079: // order to ensure contiguity Chris@1079: rightToLeft = isLeftOfValidArea; Chris@1079: } Chris@1075: Chris@1109: // Note, we always paint the full height to cache. We want to Chris@1109: // ensure the cache is coherent without having to worry about Chris@1109: // vertical matching of required and valid areas as well as Chris@1109: // horizontal. Chris@1094: Chris@1109: if (renderType == DrawBufferBinResolution) { Chris@1109: Chris@1094: renderToCacheBinResolution(v, x0, x1 - x0); Chris@1109: Chris@1109: } else { // must be DrawBufferPixelResolution, handled DirectTranslucent earlier Chris@1109: Chris@1094: renderToCachePixelResolution(v, x0, x1 - x0, rightToLeft, timeConstrained); Chris@1094: } Chris@1079: Chris@1079: QRect pr = rect & m_cache.getValidArea(); Chris@1079: paint.drawImage(pr.x(), pr.y(), m_cache.getImage(), Chris@1079: pr.x(), pr.y(), pr.width(), pr.height()); Chris@1079: Chris@1079: if (!timeConstrained && (pr != rect)) { Chris@1079: //!!! on a first cut, there is a risk that this will happen Chris@1079: //!!! when we are at start/end of model -- trap, report, and Chris@1079: //!!! then fix Chris@1079: throw std::logic_error("internal error: failed to render entire requested rect even when not time-constrained"); Chris@1079: } Chris@1120: Chris@1122: MagnitudeRange range = m_magCache.getRange(reqx0, reqx1 - reqx0); Chris@1120: Chris@1120: return { pr, range }; Chris@1079: Chris@1079: //!!! todo, here or in caller: illuminateLocalFeatures Chris@1079: Chris@1123: //!!! todo: handle vertical range other than full range of column Chris@1110: Chris@1079: //!!! fft model scaling? Chris@1073: } Chris@1073: Chris@1109: Colour3DPlotRenderer::RenderType Chris@1113: Colour3DPlotRenderer::decideRenderType(const LayerGeometryProvider *v) const Chris@1094: { Chris@1100: const DenseThreeDimensionalModel *model = m_sources.source; Chris@1109: if (!model || !v || !(v->getViewManager())) { Chris@1109: return DrawBufferPixelResolution; // or anything Chris@1109: } Chris@1109: Chris@1094: int binResolution = model->getResolution(); Chris@1094: int zoomLevel = v->getZoomLevel(); Chris@1109: sv_samplerate_t modelRate = model->getSampleRate(); Chris@1109: Chris@1109: double rateRatio = v->getViewManager()->getMainModelSampleRate() / modelRate; Chris@1109: double relativeBinResolution = binResolution * rateRatio; Chris@1109: Chris@1109: if (m_params.binDisplay == BinDisplay::PeakFrequencies) { Chris@1109: // no alternative works here Chris@1109: return DrawBufferPixelResolution; Chris@1109: } Chris@1109: Chris@1109: if (!m_params.alwaysOpaque && !m_params.interpolate) { Chris@1109: Chris@1109: // consider translucent option -- only if not smoothing & not Chris@1109: // explicitly requested opaque & sufficiently zoomed-in Chris@1109: Chris@1117: if (model->getHeight() * 3 < v->getPaintHeight() && Chris@1117: relativeBinResolution >= 3 * zoomLevel) { Chris@1109: return DirectTranslucent; Chris@1109: } Chris@1109: } Chris@1109: Chris@1109: if (relativeBinResolution > zoomLevel) { Chris@1109: return DrawBufferBinResolution; Chris@1109: } else { Chris@1109: return DrawBufferPixelResolution; Chris@1109: } Chris@1109: } Chris@1109: Chris@1121: MagnitudeRange Chris@1113: Colour3DPlotRenderer::renderDirectTranslucent(const LayerGeometryProvider *v, Chris@1109: QPainter &paint, Chris@1109: QRect rect) Chris@1109: { Chris@1117: Profiler profiler("Colour3DPlotRenderer::renderDirectTranslucent"); Chris@1117: Chris@1121: MagnitudeRange magRange; Chris@1121: Chris@1115: QPoint illuminatePos; Chris@1115: bool illuminate = v->shouldIlluminateLocalFeatures Chris@1115: (m_sources.verticalBinLayer, illuminatePos); Chris@1109: Chris@1109: const DenseThreeDimensionalModel *model = m_sources.source; Chris@1109: Chris@1109: int x0 = rect.left(); Chris@1109: int x1 = rect.right() + 1; Chris@1109: Chris@1109: int h = v->getPaintHeight(); Chris@1109: Chris@1109: sv_frame_t modelStart = model->getStartFrame(); Chris@1109: sv_frame_t modelEnd = model->getEndFrame(); Chris@1109: int modelResolution = model->getResolution(); Chris@1109: Chris@1109: double rateRatio = Chris@1109: v->getViewManager()->getMainModelSampleRate() / model->getSampleRate(); Chris@1109: Chris@1109: // the s-prefix values are source, i.e. model, column and bin numbers Chris@1109: int sx0 = int((double(v->getFrameForX(x0)) / rateRatio - double(modelStart)) Chris@1109: / modelResolution); Chris@1109: int sx1 = int((double(v->getFrameForX(x1)) / rateRatio - double(modelStart)) Chris@1109: / modelResolution); Chris@1109: Chris@1109: int sh = model->getHeight(); Chris@1109: Chris@1109: const int buflen = 40; Chris@1109: char labelbuf[buflen]; Chris@1109: Chris@1133: int minbin = m_sources.verticalBinLayer->getIBinForY(v, h); Chris@1135: if (minbin >= sh) minbin = sh - 1; Chris@1135: if (minbin < 0) minbin = 0; Chris@1135: Chris@1135: int nbins = m_sources.verticalBinLayer->getIBinForY(v, 0) - minbin + 1; Chris@1135: if (minbin + nbins > sh) nbins = sh - minbin; Chris@1133: Chris@1109: int psx = -1; Chris@1109: Chris@1109: vector preparedColumn; Chris@1109: Chris@1109: int modelWidth = model->getWidth(); Chris@1109: Chris@1109: for (int sx = sx0; sx <= sx1; ++sx) { Chris@1109: Chris@1109: if (sx < 0 || sx >= modelWidth) { Chris@1109: continue; Chris@1109: } Chris@1109: Chris@1109: if (sx != psx) { Chris@1109: Chris@1109: //!!! this is in common with renderDrawBuffer - pull it out Chris@1109: Chris@1109: // order: Chris@1109: // get column -> scale -> record extents -> Chris@1124: // normalise -> peak pick -> apply display gain Chris@1124: Chris@1109: ColumnOp::Column fullColumn = model->getColumn(sx); Chris@1109: Chris@1109: ColumnOp::Column column = Chris@1109: vector(fullColumn.data() + minbin, Chris@1135: fullColumn.data() + minbin + nbins); Chris@1109: Chris@1125: column = ColumnOp::applyGain(column, m_params.scaleFactor); Chris@1121: Chris@1109: // if (m_colourScale != ColourScaleType::Phase) { Chris@1115: preparedColumn = ColumnOp::normalize(column, m_params.normalization); Chris@1109: // } Chris@1131: Chris@1131: magRange.sample(preparedColumn); Chris@1109: Chris@1109: if (m_params.binDisplay == BinDisplay::PeakBins) { Chris@1115: preparedColumn = ColumnOp::peakPick(preparedColumn); Chris@1109: } Chris@1109: Chris@1124: // Display gain belongs to the colour scale and is Chris@1124: // applied by the colour scale object when mapping it Chris@1124: Chris@1109: psx = sx; Chris@1109: } Chris@1109: Chris@1109: sv_frame_t fx = sx * modelResolution + modelStart; Chris@1109: Chris@1109: if (fx + modelResolution <= modelStart || fx > modelEnd) continue; Chris@1109: Chris@1109: int rx0 = v->getXForFrame(int(double(fx) * rateRatio)); Chris@1109: int rx1 = v->getXForFrame(int(double(fx + modelResolution + 1) * rateRatio)); Chris@1109: Chris@1109: int rw = rx1 - rx0; Chris@1109: if (rw < 1) rw = 1; Chris@1109: Chris@1109: bool showLabel = (rw > 10 && Chris@1109: paint.fontMetrics().width("0.000000") < rw - 3 && Chris@1109: paint.fontMetrics().height() < (h / sh)); Chris@1109: Chris@1135: for (int sy = minbin; sy < minbin + nbins; ++sy) { Chris@1109: Chris@1109: int ry0 = m_sources.verticalBinLayer->getIYForBin(v, sy); Chris@1109: int ry1 = m_sources.verticalBinLayer->getIYForBin(v, sy + 1); Chris@1116: Chris@1116: if (m_params.invertVertical) { Chris@1116: ry0 = h - ry0 - 1; Chris@1116: ry1 = h - ry1 - 1; Chris@1116: } Chris@1116: Chris@1109: QRect r(rx0, ry1, rw, ry0 - ry1); Chris@1109: Chris@1109: float value = preparedColumn[sy - minbin]; Chris@1112: QColor colour = m_params.colourScale.getColour(value, Chris@1112: m_params.colourRotation); Chris@1109: Chris@1109: if (rw == 1) { Chris@1109: paint.setPen(colour); Chris@1109: paint.setBrush(Qt::NoBrush); Chris@1109: paint.drawLine(r.x(), r.y(), r.x(), r.y() + r.height() - 1); Chris@1109: continue; Chris@1109: } Chris@1109: Chris@1109: QColor pen(255, 255, 255, 80); Chris@1109: QColor brush(colour); Chris@1109: Chris@1109: if (rw > 3 && r.height() > 3) { Chris@1109: brush.setAlpha(160); Chris@1109: } Chris@1109: Chris@1109: paint.setPen(Qt::NoPen); Chris@1109: paint.setBrush(brush); Chris@1109: Chris@1115: if (illuminate) { Chris@1115: if (r.contains(illuminatePos)) { Chris@1115: paint.setPen(v->getForeground()); Chris@1115: } Chris@1115: } Chris@1109: Chris@1109: #ifdef DEBUG_COLOUR_3D_PLOT_LAYER_PAINT Chris@1109: // cerr << "rect " << r.x() << "," << r.y() << " " Chris@1109: // << r.width() << "x" << r.height() << endl; Chris@1109: #endif Chris@1109: Chris@1109: paint.drawRect(r); Chris@1109: Chris@1109: if (showLabel) { Chris@1109: double value = model->getValueAt(sx, sy); Chris@1109: snprintf(labelbuf, buflen, "%06f", value); Chris@1109: QString text(labelbuf); Chris@1109: PaintAssistant::drawVisibleText Chris@1109: (v, Chris@1109: paint, Chris@1109: rx0 + 2, Chris@1109: ry0 - h / sh - 1 + 2 + paint.fontMetrics().ascent(), Chris@1109: text, Chris@1109: PaintAssistant::OutlinedText); Chris@1109: } Chris@1109: } Chris@1109: } Chris@1121: Chris@1121: return magRange; Chris@1094: } Chris@1094: Chris@1080: void Chris@1113: Colour3DPlotRenderer::renderToCachePixelResolution(const LayerGeometryProvider *v, Chris@1094: int x0, int repaintWidth, Chris@1094: bool rightToLeft, Chris@1094: bool timeConstrained) Chris@1079: { Chris@1117: Profiler profiler("Colour3DPlotRenderer::renderToCachePixelResolution"); Chris@1094: cerr << "renderToCachePixelResolution" << endl; Chris@1094: Chris@1094: // Draw to the draw buffer, and then copy from there. The draw Chris@1094: // buffer is at the same resolution as the target in the cache, so Chris@1094: // no extra scaling needed. Chris@1079: Chris@1100: const DenseThreeDimensionalModel *model = m_sources.source; Chris@1079: if (!model || !model->isOK() || !model->isReady()) { Chris@1079: throw std::logic_error("no source model provided, or model not ready"); Chris@1079: } Chris@1079: Chris@1079: int h = v->getPaintHeight(); Chris@1079: Chris@1094: clearDrawBuffer(repaintWidth, h); Chris@1079: Chris@1094: vector binforx(repaintWidth); Chris@1079: vector binfory(h); Chris@1079: Chris@1079: bool usePeaksCache = false; Chris@1079: int binsPerPeak = 1; Chris@1094: int zoomLevel = v->getZoomLevel(); Chris@1094: int binResolution = model->getResolution(); Chris@1079: Chris@1094: for (int x = 0; x < repaintWidth; ++x) { Chris@1094: sv_frame_t f0 = v->getFrameForX(x0 + x); Chris@1094: double s0 = double(f0 - model->getStartFrame()) / binResolution; Chris@1094: binforx[x] = int(s0 + 0.0001); Chris@1094: } Chris@1080: Chris@1094: if (m_sources.peaks) { // peaks cache exists Chris@1080: Chris@1094: binsPerPeak = m_sources.peaks->getColumnsPerPeak(); Chris@1094: usePeaksCache = (binResolution * binsPerPeak) < zoomLevel; Chris@1094: Chris@1094: if (m_params.colourScale.getScale() == Chris@1105: ColourScaleType::Phase) { Chris@1094: usePeaksCache = false; Chris@1079: } Chris@1079: } Chris@1082: Chris@1094: cerr << "[PIX] zoomLevel = " << zoomLevel Chris@1094: << ", binResolution " << binResolution Chris@1094: << ", binsPerPeak " << binsPerPeak Chris@1094: << ", peak cache " << m_sources.peaks Chris@1094: << ", usePeaksCache = " << usePeaksCache Chris@1094: << endl; Chris@1094: Chris@1080: for (int y = 0; y < h; ++y) { Chris@1090: binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1); Chris@1080: } Chris@1079: Chris@1097: int attainedWidth; Chris@1097: Chris@1103: if (m_params.binDisplay == BinDisplay::PeakFrequencies) { Chris@1097: attainedWidth = renderDrawBufferPeakFrequencies(v, Chris@1097: repaintWidth, Chris@1097: h, Chris@1097: binforx, Chris@1097: binfory, Chris@1097: rightToLeft, Chris@1097: timeConstrained); Chris@1097: Chris@1097: } else { Chris@1097: attainedWidth = renderDrawBuffer(repaintWidth, Chris@1080: h, Chris@1080: binforx, Chris@1080: binfory, Chris@1080: usePeaksCache, Chris@1080: rightToLeft, Chris@1080: timeConstrained); Chris@1097: } Chris@1083: Chris@1094: if (attainedWidth == 0) return; Chris@1084: Chris@1094: // draw buffer is pixel resolution, no scaling factors or padding involved Chris@1084: Chris@1084: int paintedLeft = x0; Chris@1084: if (rightToLeft) { Chris@1084: paintedLeft += (repaintWidth - attainedWidth); Chris@1084: } Chris@1084: Chris@1094: m_cache.drawImage(paintedLeft, attainedWidth, Chris@1094: m_drawBuffer, Chris@1094: paintedLeft - x0, attainedWidth); Chris@1121: Chris@1121: for (int i = 0; in_range_for(m_magRanges, i); ++i) { Chris@1121: m_magCache.sampleColumn(i, m_magRanges.at(i)); Chris@1121: } Chris@1094: } Chris@1084: Chris@1094: void Chris@1113: Colour3DPlotRenderer::renderToCacheBinResolution(const LayerGeometryProvider *v, Chris@1094: int x0, int repaintWidth) Chris@1094: { Chris@1117: Profiler profiler("Colour3DPlotRenderer::renderToCacheBinResolution"); Chris@1094: cerr << "renderToCacheBinResolution" << endl; Chris@1094: Chris@1094: // Draw to the draw buffer, and then scale-copy from there. Draw Chris@1094: // buffer is at bin resolution, i.e. buffer x == source column Chris@1094: // number. We use toolkit smooth scaling for interpolation. Chris@1084: Chris@1100: const DenseThreeDimensionalModel *model = m_sources.source; Chris@1094: if (!model || !model->isOK() || !model->isReady()) { Chris@1094: throw std::logic_error("no source model provided, or model not ready"); Chris@1094: } Chris@1094: Chris@1094: // The draw buffer will contain a fragment at bin resolution. We Chris@1094: // need to ensure that it starts and ends at points where a Chris@1094: // time-bin boundary occurs at an exact pixel boundary, and with a Chris@1094: // certain amount of overlap across existing pixels so that we can Chris@1094: // scale and draw from it without smoothing errors at the edges. Chris@1094: Chris@1094: // If (getFrameForX(x) / increment) * increment == Chris@1094: // getFrameForX(x), then x is a time-bin boundary. We want two Chris@1094: // such boundaries at either side of the draw buffer -- one which Chris@1094: // we draw up to, and one which we subsequently crop at. Chris@1094: Chris@1094: sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1; Chris@1094: sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1; Chris@1094: Chris@1094: int drawBufferWidth; Chris@1094: int binResolution = model->getResolution(); Chris@1094: Chris@1094: for (int x = x0; ; --x) { Chris@1094: sv_frame_t f = v->getFrameForX(x); Chris@1094: if ((f / binResolution) * binResolution == f) { Chris@1094: if (leftCropFrame == -1) leftCropFrame = f; Chris@1094: else if (x < x0 - 2) { Chris@1094: leftBoundaryFrame = f; Chris@1094: break; Chris@1094: } Chris@1094: } Chris@1094: } Chris@1094: for (int x = x0 + repaintWidth; ; ++x) { Chris@1094: sv_frame_t f = v->getFrameForX(x); Chris@1094: if ((f / binResolution) * binResolution == f) { Chris@1094: if (rightCropFrame == -1) rightCropFrame = f; Chris@1094: else if (x > x0 + repaintWidth + 2) { Chris@1094: rightBoundaryFrame = f; Chris@1094: break; Chris@1094: } Chris@1094: } Chris@1094: } Chris@1094: drawBufferWidth = int Chris@1094: ((rightBoundaryFrame - leftBoundaryFrame) / binResolution); Chris@1094: Chris@1094: int h = v->getPaintHeight(); Chris@1094: Chris@1095: // For our purposes here, the draw buffer needs to be exactly our Chris@1095: // target size (so we recreate always rather than just clear it) Chris@1095: Chris@1095: recreateDrawBuffer(drawBufferWidth, h); Chris@1094: Chris@1094: vector binforx(drawBufferWidth); Chris@1094: vector binfory(h); Chris@1094: Chris@1094: for (int x = 0; x < drawBufferWidth; ++x) { Chris@1094: binforx[x] = int(leftBoundaryFrame / binResolution) + x; Chris@1094: } Chris@1094: Chris@1094: cerr << "[BIN] binResolution " << binResolution Chris@1094: << endl; Chris@1094: Chris@1094: for (int y = 0; y < h; ++y) { Chris@1094: binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1); Chris@1094: } Chris@1094: Chris@1094: int attainedWidth = renderDrawBuffer(drawBufferWidth, Chris@1094: h, Chris@1094: binforx, Chris@1094: binfory, Chris@1094: false, Chris@1094: false, Chris@1094: false); Chris@1094: Chris@1094: if (attainedWidth == 0) return; Chris@1094: Chris@1094: int scaledLeft = v->getXForFrame(leftBoundaryFrame); Chris@1094: int scaledRight = v->getXForFrame(rightBoundaryFrame); Chris@1095: Chris@1095: cerr << "scaling draw buffer from width " << m_drawBuffer.width() Chris@1095: << " to " << (scaledRight - scaledLeft) << " (nb drawBufferWidth = " Chris@1095: << drawBufferWidth << ")" << endl; Chris@1094: Chris@1094: QImage scaled = m_drawBuffer.scaled Chris@1094: (scaledRight - scaledLeft, h, Chris@1094: Qt::IgnoreAspectRatio, (m_params.interpolate ? Chris@1094: Qt::SmoothTransformation : Chris@1094: Qt::FastTransformation)); Chris@1084: Chris@1094: int scaledLeftCrop = v->getXForFrame(leftCropFrame); Chris@1094: int scaledRightCrop = v->getXForFrame(rightCropFrame); Chris@1094: Chris@1094: int targetLeft = scaledLeftCrop; Chris@1094: if (targetLeft < 0) { Chris@1094: targetLeft = 0; Chris@1094: } Chris@1094: Chris@1094: int targetWidth = scaledRightCrop - targetLeft; Chris@1094: if (targetLeft + targetWidth > m_cache.getSize().width()) { Chris@1094: targetWidth = m_cache.getSize().width() - targetLeft; Chris@1094: } Chris@1094: Chris@1094: int sourceLeft = targetLeft - scaledLeft; Chris@1094: if (sourceLeft < 0) { Chris@1094: sourceLeft = 0; Chris@1094: } Chris@1094: Chris@1094: cerr << "repaintWidth = " << repaintWidth Chris@1094: << ", targetWidth = " << targetWidth << endl; Chris@1094: Chris@1094: if (targetWidth > 0) { Chris@1136: // we are copying from an image that has already been scaled, Chris@1136: // hence using the same width in both geometries Chris@1094: m_cache.drawImage(targetLeft, targetWidth, Chris@1094: scaled, Chris@1136: sourceLeft, targetWidth); Chris@1084: } Chris@1121: Chris@1121: for (int i = 0; i < targetWidth; ++i) { Chris@1136: // but the mag range vector has not been scaled Chris@1136: int sourceIx = int((double(i + sourceLeft) / scaled.width()) Chris@1136: * int(m_magRanges.size())); Chris@1136: // int sourceIx = int((double(i) / targetWidth) * sourceWidth); Chris@1136: cerr << "mag range target ix = " << i << ", source ix = " Chris@1136: << sourceIx << ", of " << m_magRanges.size() << endl; Chris@1121: if (in_range_for(m_magRanges, sourceIx)) { Chris@1121: m_magCache.sampleColumn(i, m_magRanges.at(sourceIx)); Chris@1121: } Chris@1121: } Chris@1079: } Chris@1083: Chris@1083: int Chris@1083: Colour3DPlotRenderer::renderDrawBuffer(int w, int h, Chris@1083: const vector &binforx, Chris@1083: const vector &binfory, Chris@1083: bool usePeaksCache, Chris@1083: bool rightToLeft, Chris@1083: bool timeConstrained) Chris@1083: { Chris@1083: // Callers must have checked that the appropriate subset of Chris@1083: // Sources data members are set for the supplied flags (e.g. that Chris@1083: // peaks model exists if usePeaksCache) Chris@1083: Chris@1083: RenderTimer timer(timeConstrained ? Chris@1083: RenderTimer::FastRender : Chris@1083: RenderTimer::NoTimeout); Chris@1083: Chris@1083: int divisor = 1; Chris@1100: const DenseThreeDimensionalModel *sourceModel = m_sources.source; Chris@1083: if (usePeaksCache) { Chris@1083: divisor = m_sources.peaks->getColumnsPerPeak(); Chris@1083: sourceModel = m_sources.peaks; Chris@1083: } Chris@1083: Chris@1135: int sh = sourceModel->getHeight(); Chris@1135: Chris@1135: int minbin = int(binfory[0] + 0.0001); Chris@1135: if (minbin >= sh) minbin = sh - 1; Chris@1135: if (minbin < 0) minbin = 0; Chris@1135: Chris@1135: int nbins = int(binfory[h-1]) - minbin + 1; Chris@1135: if (minbin + nbins > sh) nbins = sh - minbin; Chris@1135: Chris@1083: int psx = -1; Chris@1083: Chris@1083: int start = 0; Chris@1083: int finish = w; Chris@1083: int step = 1; Chris@1083: Chris@1083: if (rightToLeft) { Chris@1083: start = w-1; Chris@1083: finish = -1; Chris@1083: step = -1; Chris@1083: } Chris@1083: Chris@1083: int columnCount = 0; Chris@1083: Chris@1083: vector preparedColumn; Chris@1094: Chris@1094: int modelWidth = sourceModel->getWidth(); Chris@1121: Chris@1121: cerr << "modelWidth " << modelWidth << ", divisor " << divisor << endl; Chris@1121: Chris@1083: for (int x = start; x != finish; x += step) { Chris@1083: Chris@1083: // x is the on-canvas pixel coord; sx (later) will be the Chris@1083: // source column index Chris@1083: Chris@1083: ++columnCount; Chris@1120: Chris@1123: #ifdef DEBUG_COLOUR_PLOT_REPAINT Chris@1121: cerr << "x = " << x << ", binforx[x] = " << binforx[x] << endl; Chris@1120: #endif Chris@1083: Chris@1083: if (binforx[x] < 0) continue; Chris@1083: Chris@1083: int sx0 = binforx[x] / divisor; Chris@1083: int sx1 = sx0; Chris@1083: if (x+1 < w) sx1 = binforx[x+1] / divisor; Chris@1083: if (sx0 < 0) sx0 = sx1 - 1; Chris@1083: if (sx0 < 0) continue; Chris@1083: if (sx1 <= sx0) sx1 = sx0 + 1; Chris@1083: Chris@1083: vector pixelPeakColumn; Chris@1121: MagnitudeRange magRange; Chris@1083: Chris@1083: for (int sx = sx0; sx < sx1; ++sx) { Chris@1083: Chris@1123: #ifdef DEBUG_COLOUR_PLOT_REPAINT Chris@1094: cerr << "sx = " << sx << endl; Chris@1083: #endif Chris@1083: Chris@1094: if (sx < 0 || sx >= modelWidth) { Chris@1083: continue; Chris@1083: } Chris@1083: Chris@1083: if (sx != psx) { Chris@1083: Chris@1083: // order: Chris@1083: // get column -> scale -> record extents -> Chris@1124: // normalise -> peak pick -> distribute/interpolate -> Chris@1124: // apply display gain Chris@1083: Chris@1083: ColumnOp::Column fullColumn = sourceModel->getColumn(sx); Chris@1090: Chris@1136: // cerr << "x " << x << ", sx " << sx << ", col height " << fullColumn.size() Chris@1136: // << ", minbin " << minbin << ", nbins " << nbins << endl; Chris@1090: Chris@1083: ColumnOp::Column column = Chris@1083: vector(fullColumn.data() + minbin, Chris@1135: fullColumn.data() + minbin + nbins); Chris@1083: Chris@1125: column = ColumnOp::applyGain(column, m_params.scaleFactor); Chris@1125: Chris@1105: // if (m_colourScale != ColourScaleType::Phase) { Chris@1083: column = ColumnOp::normalize(column, m_params.normalization); Chris@1083: // } Chris@1083: Chris@1131: magRange.sample(column); Chris@1131: Chris@1103: if (m_params.binDisplay == BinDisplay::PeakBins) { Chris@1083: column = ColumnOp::peakPick(column); Chris@1083: } Chris@1083: Chris@1083: preparedColumn = Chris@1124: ColumnOp::distribute(column, Chris@1083: h, Chris@1083: binfory, Chris@1083: minbin, Chris@1083: m_params.interpolate); Chris@1124: Chris@1124: // Display gain belongs to the colour scale and is Chris@1124: // applied by the colour scale object when mapping it Chris@1083: Chris@1083: psx = sx; Chris@1083: } Chris@1083: Chris@1083: if (sx == sx0) { Chris@1083: pixelPeakColumn = preparedColumn; Chris@1083: } else { Chris@1083: for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) { Chris@1083: pixelPeakColumn[i] = std::max(pixelPeakColumn[i], Chris@1083: preparedColumn[i]); Chris@1083: } Chris@1083: } Chris@1083: } Chris@1083: Chris@1083: if (!pixelPeakColumn.empty()) { Chris@1121: Chris@1083: for (int y = 0; y < h; ++y) { Chris@1116: int py; Chris@1116: if (m_params.invertVertical) { Chris@1116: py = y; Chris@1116: } else { Chris@1116: py = h - y - 1; Chris@1116: } Chris@1083: m_drawBuffer.setPixel Chris@1083: (x, Chris@1116: py, Chris@1083: m_params.colourScale.getPixel(pixelPeakColumn[y])); Chris@1083: } Chris@1121: Chris@1121: m_magRanges.push_back(magRange); Chris@1083: } Chris@1083: Chris@1083: double fractionComplete = double(columnCount) / double(w); Chris@1083: if (timer.outOfTime(fractionComplete)) { Chris@1121: cerr << "out of time" << endl; Chris@1083: return columnCount; Chris@1083: } Chris@1083: } Chris@1083: Chris@1083: return columnCount; Chris@1083: } Chris@1083: Chris@1097: int Chris@1113: Colour3DPlotRenderer::renderDrawBufferPeakFrequencies(const LayerGeometryProvider *v, Chris@1097: int w, int h, Chris@1097: const vector &binforx, Chris@1097: const vector &binfory, Chris@1097: bool rightToLeft, Chris@1097: bool timeConstrained) Chris@1097: { Chris@1097: // Callers must have checked that the appropriate subset of Chris@1097: // Sources data members are set for the supplied flags (e.g. that Chris@1097: // fft model exists) Chris@1097: Chris@1097: RenderTimer timer(timeConstrained ? Chris@1097: RenderTimer::FastRender : Chris@1097: RenderTimer::NoTimeout); Chris@1097: Chris@1135: const FFTModel *fft = m_sources.fft; Chris@1135: Chris@1135: int sh = fft->getHeight(); Chris@1135: Chris@1097: int minbin = int(binfory[0] + 0.0001); Chris@1135: if (minbin >= sh) minbin = sh - 1; Chris@1097: if (minbin < 0) minbin = 0; Chris@1097: Chris@1135: int nbins = int(binfory[h-1]) - minbin + 1; Chris@1135: if (minbin + nbins > sh) nbins = sh - minbin; Chris@1097: Chris@1097: FFTModel::PeakSet peakfreqs; Chris@1097: Chris@1097: int psx = -1; Chris@1097: Chris@1097: int start = 0; Chris@1097: int finish = w; Chris@1097: int step = 1; Chris@1097: Chris@1097: if (rightToLeft) { Chris@1097: start = w-1; Chris@1097: finish = -1; Chris@1097: step = -1; Chris@1097: } Chris@1097: Chris@1097: int columnCount = 0; Chris@1097: Chris@1097: vector preparedColumn; Chris@1097: Chris@1097: int modelWidth = fft->getWidth(); Chris@1097: cerr << "modelWidth " << modelWidth << endl; Chris@1097: Chris@1135: double minFreq = Chris@1135: (double(minbin) * fft->getSampleRate()) / fft->getFFTSize(); Chris@1135: double maxFreq = Chris@1135: (double(minbin + nbins - 1) * fft->getSampleRate()) / fft->getFFTSize(); Chris@1097: Chris@1103: bool logarithmic = (m_params.binScale == BinScale::Log); Chris@1097: Chris@1097: for (int x = start; x != finish; x += step) { Chris@1097: Chris@1097: // x is the on-canvas pixel coord; sx (later) will be the Chris@1097: // source column index Chris@1097: Chris@1097: ++columnCount; Chris@1097: Chris@1097: if (binforx[x] < 0) continue; Chris@1097: Chris@1097: int sx0 = binforx[x]; Chris@1097: int sx1 = sx0; Chris@1097: if (x+1 < w) sx1 = binforx[x+1]; Chris@1097: if (sx0 < 0) sx0 = sx1 - 1; Chris@1097: if (sx0 < 0) continue; Chris@1097: if (sx1 <= sx0) sx1 = sx0 + 1; Chris@1097: Chris@1097: vector pixelPeakColumn; Chris@1121: MagnitudeRange magRange; Chris@1097: Chris@1097: for (int sx = sx0; sx < sx1; ++sx) { Chris@1097: Chris@1097: if (sx < 0 || sx >= modelWidth) { Chris@1097: continue; Chris@1097: } Chris@1097: Chris@1097: if (sx != psx) { Chris@1097: Chris@1097: ColumnOp::Column fullColumn = fft->getColumn(sx); Chris@1097: Chris@1097: ColumnOp::Column column = Chris@1097: vector(fullColumn.data() + minbin, Chris@1135: fullColumn.data() + minbin + nbins + 1); Chris@1097: Chris@1125: column = ColumnOp::applyGain(column, m_params.scaleFactor); Chris@1121: Chris@1105: //!!! if (m_colourScale != ColourScaleType::Phase) { Chris@1124: preparedColumn = ColumnOp::normalize Chris@1124: (column, m_params.normalization); Chris@1097: //!!! } Chris@1097: Chris@1131: magRange.sample(preparedColumn); Chris@1131: Chris@1097: psx = sx; Chris@1097: } Chris@1097: Chris@1097: if (sx == sx0) { Chris@1097: pixelPeakColumn = preparedColumn; Chris@1097: peakfreqs = fft->getPeakFrequencies(FFTModel::AllPeaks, sx, Chris@1135: minbin, minbin + nbins - 1); Chris@1097: } else { Chris@1097: for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) { Chris@1097: pixelPeakColumn[i] = std::max(pixelPeakColumn[i], Chris@1097: preparedColumn[i]); Chris@1097: } Chris@1097: } Chris@1097: } Chris@1097: Chris@1097: if (!pixelPeakColumn.empty()) { Chris@1121: Chris@1097: for (FFTModel::PeakSet::const_iterator pi = peakfreqs.begin(); Chris@1097: pi != peakfreqs.end(); ++pi) { Chris@1097: Chris@1097: int bin = pi->first; Chris@1097: double freq = pi->second; Chris@1097: Chris@1097: if (bin < minbin) continue; Chris@1135: if (bin >= minbin + nbins) break; Chris@1097: Chris@1097: double value = pixelPeakColumn[bin - minbin]; Chris@1097: Chris@1097: double y = v->getYForFrequency Chris@1097: (freq, minFreq, maxFreq, logarithmic); Chris@1097: Chris@1097: int iy = int(y + 0.5); Chris@1097: if (iy < 0 || iy >= h) continue; Chris@1097: Chris@1097: m_drawBuffer.setPixel Chris@1097: (x, Chris@1097: iy, Chris@1097: m_params.colourScale.getPixel(value)); Chris@1097: } Chris@1121: Chris@1121: m_magRanges.push_back(magRange); Chris@1097: } Chris@1097: Chris@1097: double fractionComplete = double(columnCount) / double(w); Chris@1097: if (timer.outOfTime(fractionComplete)) { Chris@1097: return columnCount; Chris@1097: } Chris@1097: } Chris@1097: Chris@1097: return columnCount; Chris@1097: } Chris@1097: Chris@1079: void Chris@1095: Colour3DPlotRenderer::recreateDrawBuffer(int w, int h) Chris@1079: { Chris@1095: m_drawBuffer = QImage(w, h, QImage::Format_Indexed8); Chris@1079: Chris@1095: for (int pixel = 0; pixel < 256; ++pixel) { Chris@1095: m_drawBuffer.setColor Chris@1095: ((unsigned char)pixel, Chris@1112: m_params.colourScale.getColourForPixel Chris@1112: (pixel, m_params.colourRotation).rgb()); Chris@1079: } Chris@1079: Chris@1079: m_drawBuffer.fill(0); Chris@1121: m_magRanges.clear(); Chris@1079: } Chris@1079: Chris@1095: void Chris@1095: Colour3DPlotRenderer::clearDrawBuffer(int w, int h) Chris@1095: { Chris@1095: if (m_drawBuffer.width() < w || m_drawBuffer.height() != h) { Chris@1095: recreateDrawBuffer(w, h); Chris@1095: } else { Chris@1095: m_drawBuffer.fill(0); Chris@1121: m_magRanges.clear(); Chris@1095: } Chris@1095: } Chris@1079: Chris@1095: