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@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@1075: Chris@1079: #include Chris@1079: Chris@1079: using namespace std; Chris@1079: Chris@1073: Colour3DPlotRenderer::RenderResult Chris@1090: Colour3DPlotRenderer::render(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@1090: Colour3DPlotRenderer::renderTimeConstrained(LayerGeometryProvider *v, Chris@1090: QPainter &paint, QRect rect) Chris@1076: { Chris@1090: return render(v, paint, rect, true); Chris@1076: } Chris@1076: Chris@1076: Colour3DPlotRenderer::RenderResult Chris@1090: Colour3DPlotRenderer::render(LayerGeometryProvider *v, Chris@1090: QPainter &paint, QRect rect, bool timeConstrained) Chris@1073: { Chris@1079: sv_frame_t startFrame = v->getStartFrame(); Chris@1079: 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@1079: m_cache.resize(v->getPaintSize()); Chris@1079: m_cache.setZoomLevel(v->getZoomLevel()); Chris@1079: Chris@1090: cerr << "cache start " << m_cache.getStartFrame() Chris@1090: << " view start " << startFrame Chris@1090: << " valid left " << m_cache.getValidLeft() Chris@1090: << " valid right " << m_cache.getValidRight() Chris@1090: << " x0 " << x0 Chris@1090: << " x1 " << x1 Chris@1090: << endl; Chris@1090: 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@1090: cerr << "cache hit" << endl; Chris@1090: Chris@1079: // cache is valid for the complete requested area Chris@1079: paint.drawImage(rect, m_cache.getImage(), rect); Chris@1079: return { rect, {} }; Chris@1079: Chris@1079: } else { Chris@1090: cerr << "cache partial hit" << endl; 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@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@1090: // cache completely invalid Chris@1090: m_cache.setStartFrame(startFrame); Chris@1075: } Chris@1075: Chris@1079: bool rightToLeft = false; Chris@1079: 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: cerr << "cache somewhat valid" << endl; 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@1090: renderToCache(v, x0, x1 - x0, rightToLeft, timeConstrained); 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@1079: Chris@1079: return { pr, {} }; Chris@1079: Chris@1073: //!!! todo: timing/incomplete paint Chris@1073: Chris@1073: //!!! todo: peak frequency style Chris@1073: Chris@1073: //!!! todo: transparent style from Colour3DPlot Chris@1074: Chris@1074: //!!! todo: bin boundary alignment when in BinResolution Chris@1074: Chris@1079: //!!! todo: view magnitudes / normalise visible area Chris@1079: Chris@1079: //!!! todo: alter documentation for view mag stuff (cached paints Chris@1079: //!!! do not update MagnitudeRange) Chris@1079: Chris@1079: //!!! todo, here or in caller: illuminateLocalFeatures Chris@1079: Chris@1079: //!!! fft model scaling? Chris@1079: Chris@1079: //!!! should we own the Dense3DModelPeakCache here? or should it persist Chris@1073: } Chris@1073: Chris@1080: void Chris@1090: Colour3DPlotRenderer::renderToCache(LayerGeometryProvider *v, Chris@1090: int x0, int repaintWidth, Chris@1080: bool rightToLeft, bool timeConstrained) Chris@1079: { Chris@1079: // Draw to the draw buffer, and then scale-copy from there. Chris@1079: Chris@1079: 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: // The draw buffer contains a fragment at either our pixel Chris@1079: // resolution (if there is more than one time-bin per pixel) or Chris@1079: // time-bin resolution (if a time-bin spans more than one pixel). Chris@1079: // We need to ensure that it starts and ends at points where a Chris@1079: // time-bin boundary occurs at an exact pixel boundary, and with a Chris@1079: // certain amount of overlap across existing pixels so that we can Chris@1079: // scale and draw from it without smoothing errors at the edges. Chris@1079: Chris@1079: // If (getFrameForX(x) / increment) * increment == Chris@1079: // getFrameForX(x), then x is a time-bin boundary. We want two Chris@1079: // such boundaries at either side of the draw buffer -- one which Chris@1079: // we draw up to, and one which we subsequently crop at. Chris@1079: Chris@1079: bool bufferIsBinResolution = false; Chris@1079: int binResolution = model->getResolution(); Chris@1079: int zoomLevel = v->getZoomLevel(); Chris@1079: if (binResolution > zoomLevel) bufferIsBinResolution = true; Chris@1079: Chris@1079: sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1; Chris@1079: sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1; Chris@1079: Chris@1079: int drawWidth; Chris@1079: Chris@1079: if (bufferIsBinResolution) { Chris@1079: for (int x = x0; ; --x) { Chris@1079: sv_frame_t f = v->getFrameForX(x); Chris@1079: if ((f / binResolution) * binResolution == f) { Chris@1079: if (leftCropFrame == -1) leftCropFrame = f; Chris@1079: else if (x < x0 - 2) { Chris@1079: leftBoundaryFrame = f; Chris@1079: break; Chris@1079: } Chris@1079: } Chris@1079: } Chris@1079: for (int x = x0 + repaintWidth; ; ++x) { Chris@1079: sv_frame_t f = v->getFrameForX(x); Chris@1079: if ((f / binResolution) * binResolution == f) { Chris@1079: if (rightCropFrame == -1) rightCropFrame = f; Chris@1079: else if (x > x0 + repaintWidth + 2) { Chris@1079: rightBoundaryFrame = f; Chris@1079: break; Chris@1079: } Chris@1079: } Chris@1079: } Chris@1079: drawWidth = int((rightBoundaryFrame - leftBoundaryFrame) / binResolution); Chris@1079: } else { Chris@1079: drawWidth = repaintWidth; Chris@1079: } Chris@1079: Chris@1079: // We always paint the full height. Smaller heights can be used Chris@1079: // when painting direct from cache (outside this function), but we Chris@1079: // want to ensure the cache is coherent without having to worry Chris@1079: // about vertical matching of required and valid areas as well as Chris@1079: // horizontal. That's why this function didn't take any y/height Chris@1079: // parameters. Chris@1079: int h = v->getPaintHeight(); Chris@1079: Chris@1079: clearDrawBuffer(drawWidth, h); Chris@1079: Chris@1079: vector binforx(drawWidth); Chris@1079: vector binfory(h); Chris@1079: Chris@1079: bool usePeaksCache = false; Chris@1079: int binsPerPeak = 1; Chris@1079: Chris@1079: if (bufferIsBinResolution) { Chris@1080: Chris@1079: for (int x = 0; x < drawWidth; ++x) { Chris@1079: binforx[x] = int(leftBoundaryFrame / binResolution) + x; Chris@1079: } Chris@1080: Chris@1080: // calculating boundaries later will be too fiddly for partial Chris@1080: // paints, and painting should be fast anyway when this is the Chris@1080: // case because it means we're well zoomed in Chris@1080: timeConstrained = false; Chris@1080: Chris@1079: } else { Chris@1079: for (int x = 0; x < drawWidth; ++x) { Chris@1090: sv_frame_t f0 = v->getFrameForX(x0 + x); Chris@1079: double s0 = double(f0 - model->getStartFrame()) / binResolution; Chris@1079: binforx[x] = int(s0 + 0.0001); Chris@1079: } Chris@1079: Chris@1079: if (m_sources.peaks) { // peaks cache exists Chris@1079: Chris@1079: binsPerPeak = m_sources.peaks->getColumnsPerPeak(); Chris@1079: usePeaksCache = (binResolution * binsPerPeak) < zoomLevel; Chris@1079: Chris@1079: if (m_params.colourScale.getScale() == Chris@1079: ColourScale::PhaseColourScale) { Chris@1079: usePeaksCache = false; Chris@1079: } Chris@1079: } Chris@1079: } Chris@1082: 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@1083: int attainedWidth = renderDrawBuffer(repaintWidth, Chris@1080: h, Chris@1080: binforx, Chris@1080: binfory, Chris@1080: usePeaksCache, Chris@1080: rightToLeft, Chris@1080: timeConstrained); Chris@1083: Chris@1083: //!!! now scale-copy to cache Chris@1084: Chris@1084: if (attainedWidth == 0) return; Chris@1084: Chris@1084: int paintedLeft = x0; Chris@1084: if (rightToLeft) { Chris@1084: paintedLeft += (repaintWidth - attainedWidth); Chris@1084: } Chris@1084: Chris@1084: if (bufferIsBinResolution) { Chris@1084: Chris@1084: int scaledLeft = v->getXForFrame(leftBoundaryFrame); Chris@1084: int scaledRight = v->getXForFrame(rightBoundaryFrame); Chris@1084: Chris@1084: QImage scaled = m_drawBuffer.scaled Chris@1084: (scaledRight - scaledLeft, h, Chris@1084: Qt::IgnoreAspectRatio, (m_params.interpolate ? Chris@1084: Qt::SmoothTransformation : Chris@1084: Qt::FastTransformation)); Chris@1084: Chris@1084: int scaledLeftCrop = v->getXForFrame(leftCropFrame); Chris@1084: int scaledRightCrop = v->getXForFrame(rightCropFrame); Chris@1084: Chris@1084: int targetLeft = scaledLeftCrop; Chris@1084: if (targetLeft < 0) { Chris@1084: targetLeft = 0; Chris@1084: } Chris@1084: Chris@1084: int targetWidth = scaledRightCrop - targetLeft; Chris@1084: if (targetLeft + targetWidth > m_cache.getSize().width()) { Chris@1084: targetWidth = m_cache.getSize().width() - targetLeft; Chris@1084: } Chris@1084: Chris@1084: int sourceLeft = targetLeft - scaledLeft; Chris@1084: if (sourceLeft < 0) { Chris@1084: sourceLeft = 0; Chris@1084: } Chris@1084: Chris@1084: int sourceWidth = targetWidth; Chris@1084: Chris@1084: if (targetWidth > 0) { Chris@1084: m_cache.drawImage(targetLeft, targetWidth, Chris@1084: scaled, Chris@1084: sourceLeft, sourceWidth); Chris@1084: } Chris@1084: Chris@1084: } else { Chris@1084: Chris@1084: m_cache.drawImage(paintedLeft, attainedWidth, Chris@1084: m_drawBuffer, Chris@1084: paintedLeft - x0, attainedWidth); Chris@1084: } 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 minbin = int(binfory[0] + 0.0001); Chris@1083: int maxbin = int(binfory[h-1]); Chris@1083: if (minbin < 0) minbin = 0; Chris@1083: if (maxbin < 0) maxbin = minbin+1; Chris@1083: Chris@1083: int divisor = 1; Chris@1083: 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@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@1083: 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@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@1083: Chris@1083: for (int sx = sx0; sx < sx1; ++sx) { Chris@1083: Chris@1083: #ifdef DEBUG_SPECTROGRAM_REPAINT Chris@1083: // cerr << "sx = " << sx << endl; Chris@1083: #endif Chris@1083: Chris@1083: if (sx < 0 || sx >= sourceModel->getWidth()) { 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@1083: // normalise -> peak pick -> apply display gain -> Chris@1083: // distribute/interpolate Chris@1083: Chris@1083: ColumnOp::Column fullColumn = sourceModel->getColumn(sx); Chris@1090: Chris@1090: cerr << "x " << x << ", sx " << sx << ", col height " << fullColumn.size() Chris@1090: << ", minbin " << minbin << ", maxbin " << maxbin << endl; Chris@1090: Chris@1083: ColumnOp::Column column = Chris@1083: vector(fullColumn.data() + minbin, Chris@1083: fullColumn.data() + maxbin + 1); Chris@1083: Chris@1083: //!!! fft scale if (m_colourScale != PhaseColourScale) { Chris@1083: // column = ColumnOp::fftScale(column, m_fftSize); Chris@1083: // } Chris@1083: Chris@1083: //!!! extents recordColumnExtents(column, Chris@1083: // sx, Chris@1083: // overallMag, Chris@1083: // overallMagChanged); Chris@1083: Chris@1083: // if (m_colourScale != PhaseColourScale) { Chris@1083: column = ColumnOp::normalize(column, m_params.normalization); Chris@1083: // } Chris@1083: Chris@1083: if (m_params.binDisplay == PeakBins) { Chris@1083: column = ColumnOp::peakPick(column); Chris@1083: } Chris@1083: Chris@1083: preparedColumn = Chris@1083: ColumnOp::distribute(column, //!!! gain? ColumnOp::applyGain(column, m_gain), Chris@1083: h, Chris@1083: binfory, Chris@1083: minbin, Chris@1083: m_params.interpolate); 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@1083: for (int y = 0; y < h; ++y) { Chris@1083: m_drawBuffer.setPixel Chris@1083: (x, Chris@1083: h-y-1, Chris@1083: m_params.colourScale.getPixel(pixelPeakColumn[y])); Chris@1083: } Chris@1083: } Chris@1083: Chris@1083: double fractionComplete = double(columnCount) / double(w); Chris@1083: if (timer.outOfTime(fractionComplete)) { Chris@1083: return columnCount; Chris@1083: } Chris@1083: } Chris@1083: Chris@1083: return columnCount; Chris@1083: } Chris@1083: Chris@1079: void Chris@1079: Colour3DPlotRenderer::clearDrawBuffer(int w, int h) Chris@1079: { Chris@1079: if (m_drawBuffer.width() < w || m_drawBuffer.height() != h) { Chris@1079: Chris@1079: m_drawBuffer = QImage(w, h, QImage::Format_Indexed8); Chris@1079: Chris@1079: for (int pixel = 0; pixel < 256; ++pixel) { Chris@1079: //!!! todo: colour rotation (here 0) Chris@1079: m_drawBuffer.setColor Chris@1079: ((unsigned char)pixel, Chris@1079: m_params.colourScale.getColourForPixel(pixel, 0).rgb()); Chris@1079: } Chris@1079: } Chris@1079: Chris@1079: m_drawBuffer.fill(0); Chris@1079: } Chris@1079: Chris@1079: