Chris@1071
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@1071
|
2
|
Chris@1071
|
3 /*
|
Chris@1071
|
4 Sonic Visualiser
|
Chris@1071
|
5 An audio file viewer and annotation editor.
|
Chris@1071
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@1071
|
7 This file copyright 2006-2016 Chris Cannam and QMUL.
|
Chris@1071
|
8
|
Chris@1071
|
9 This program is free software; you can redistribute it and/or
|
Chris@1071
|
10 modify it under the terms of the GNU General Public License as
|
Chris@1071
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@1071
|
12 License, or (at your option) any later version. See the file
|
Chris@1071
|
13 COPYING included with this distribution for more information.
|
Chris@1071
|
14 */
|
Chris@1071
|
15
|
Chris@1071
|
16 #include "Colour3DPlotRenderer.h"
|
Chris@1074
|
17 #include "RenderTimer.h"
|
Chris@1071
|
18
|
Chris@1075
|
19 #include "data/model/DenseThreeDimensionalModel.h"
|
Chris@1075
|
20 #include "data/model/Dense3DModelPeakCache.h"
|
Chris@1075
|
21 #include "data/model/FFTModel.h"
|
Chris@1075
|
22
|
Chris@1077
|
23 #include "LayerGeometryProvider.h"
|
Chris@1082
|
24 #include "VerticalBinLayer.h"
|
Chris@1075
|
25
|
Chris@1079
|
26 #include <vector>
|
Chris@1079
|
27
|
Chris@1094
|
28 //#define DEBUG_SPECTROGRAM_REPAINT 1
|
Chris@1094
|
29
|
Chris@1079
|
30 using namespace std;
|
Chris@1079
|
31
|
Chris@1073
|
32 Colour3DPlotRenderer::RenderResult
|
Chris@1090
|
33 Colour3DPlotRenderer::render(LayerGeometryProvider *v, QPainter &paint, QRect rect)
|
Chris@1076
|
34 {
|
Chris@1090
|
35 return render(v, paint, rect, false);
|
Chris@1076
|
36 }
|
Chris@1076
|
37
|
Chris@1076
|
38 Colour3DPlotRenderer::RenderResult
|
Chris@1090
|
39 Colour3DPlotRenderer::renderTimeConstrained(LayerGeometryProvider *v,
|
Chris@1090
|
40 QPainter &paint, QRect rect)
|
Chris@1076
|
41 {
|
Chris@1090
|
42 return render(v, paint, rect, true);
|
Chris@1076
|
43 }
|
Chris@1076
|
44
|
Chris@1076
|
45 Colour3DPlotRenderer::RenderResult
|
Chris@1090
|
46 Colour3DPlotRenderer::render(LayerGeometryProvider *v,
|
Chris@1090
|
47 QPainter &paint, QRect rect, bool timeConstrained)
|
Chris@1073
|
48 {
|
Chris@1079
|
49 sv_frame_t startFrame = v->getStartFrame();
|
Chris@1079
|
50
|
Chris@1079
|
51 int x0 = v->getXForViewX(rect.x());
|
Chris@1079
|
52 int x1 = v->getXForViewX(rect.x() + rect.width());
|
Chris@1079
|
53 if (x0 < 0) x0 = 0;
|
Chris@1079
|
54 if (x1 > v->getPaintWidth()) x1 = v->getPaintWidth();
|
Chris@1079
|
55
|
Chris@1079
|
56 m_cache.resize(v->getPaintSize());
|
Chris@1079
|
57 m_cache.setZoomLevel(v->getZoomLevel());
|
Chris@1079
|
58
|
Chris@1090
|
59 cerr << "cache start " << m_cache.getStartFrame()
|
Chris@1090
|
60 << " valid left " << m_cache.getValidLeft()
|
Chris@1090
|
61 << " valid right " << m_cache.getValidRight()
|
Chris@1094
|
62 << endl;
|
Chris@1094
|
63 cerr << " view start " << startFrame
|
Chris@1090
|
64 << " x0 " << x0
|
Chris@1090
|
65 << " x1 " << x1
|
Chris@1090
|
66 << endl;
|
Chris@1090
|
67
|
Chris@1094
|
68 bool bufferIsBinResolution = useBinResolutionForDrawBuffer(v);
|
Chris@1094
|
69
|
Chris@1094
|
70 if (bufferIsBinResolution) {
|
Chris@1094
|
71 // Rendering should be fast in this situation because we are
|
Chris@1094
|
72 // quite well zoomed-in, and the sums are easier this
|
Chris@1094
|
73 // way. Calculating boundaries later will be fiddly for
|
Chris@1094
|
74 // partial paints otherwise.
|
Chris@1094
|
75 timeConstrained = false;
|
Chris@1094
|
76 }
|
Chris@1090
|
77
|
Chris@1079
|
78 if (m_cache.isValid()) { // some part of the cache is valid
|
Chris@1079
|
79
|
Chris@1079
|
80 if (v->getXForFrame(m_cache.getStartFrame()) ==
|
Chris@1079
|
81 v->getXForFrame(startFrame) &&
|
Chris@1079
|
82 m_cache.getValidLeft() <= x0 &&
|
Chris@1079
|
83 m_cache.getValidRight() >= x1) {
|
Chris@1090
|
84
|
Chris@1090
|
85 cerr << "cache hit" << endl;
|
Chris@1090
|
86
|
Chris@1079
|
87 // cache is valid for the complete requested area
|
Chris@1079
|
88 paint.drawImage(rect, m_cache.getImage(), rect);
|
Chris@1079
|
89 return { rect, {} };
|
Chris@1079
|
90
|
Chris@1079
|
91 } else {
|
Chris@1090
|
92 cerr << "cache partial hit" << endl;
|
Chris@1090
|
93
|
Chris@1079
|
94 // cache doesn't begin at the right frame or doesn't
|
Chris@1079
|
95 // contain the complete view, but might be scrollable or
|
Chris@1079
|
96 // partially usable
|
Chris@1090
|
97 m_cache.scrollTo(v, startFrame);
|
Chris@1079
|
98
|
Chris@1079
|
99 // if we are not time-constrained, then we want to paint
|
Chris@1081
|
100 // the whole area in one go; we don't return a partial
|
Chris@1081
|
101 // paint. To avoid providing the more complex logic to
|
Chris@1081
|
102 // handle painting discontiguous areas, if the only valid
|
Chris@1079
|
103 // part of cache is in the middle, just make the whole
|
Chris@1079
|
104 // thing invalid and start again.
|
Chris@1079
|
105 if (!timeConstrained) {
|
Chris@1079
|
106 if (m_cache.getValidLeft() > x0 &&
|
Chris@1079
|
107 m_cache.getValidRight() < x1) {
|
Chris@1079
|
108 m_cache.invalidate();
|
Chris@1079
|
109 }
|
Chris@1079
|
110 }
|
Chris@1079
|
111 }
|
Chris@1090
|
112 } else {
|
Chris@1090
|
113 // cache completely invalid
|
Chris@1090
|
114 m_cache.setStartFrame(startFrame);
|
Chris@1075
|
115 }
|
Chris@1075
|
116
|
Chris@1079
|
117 bool rightToLeft = false;
|
Chris@1079
|
118
|
Chris@1079
|
119 if (!m_cache.isValid() && timeConstrained) {
|
Chris@1081
|
120 // When rendering the whole area, in a context where we might
|
Chris@1081
|
121 // not be able to complete the work, start from somewhere near
|
Chris@1081
|
122 // the middle so that the region of interest appears first
|
Chris@1079
|
123
|
Chris@1079
|
124 //!!! (perhaps we should avoid doing this if past repaints
|
Chris@1079
|
125 //!!! have been fast enough to do the whole in one shot)
|
Chris@1079
|
126 if (x0 == 0 && x1 == v->getPaintWidth()) {
|
Chris@1079
|
127 x0 = int(x1 * 0.3);
|
Chris@1079
|
128 }
|
Chris@1079
|
129 }
|
Chris@1079
|
130
|
Chris@1079
|
131 if (m_cache.isValid()) {
|
Chris@1090
|
132 cerr << "cache somewhat valid" << endl;
|
Chris@1090
|
133
|
Chris@1079
|
134 // When rendering only a part of the cache, we need to make
|
Chris@1079
|
135 // sure that the part we're rendering is adjacent to (or
|
Chris@1079
|
136 // overlapping) a valid area of cache, if we have one. The
|
Chris@1079
|
137 // alternative is to ditch the valid area of cache and render
|
Chris@1079
|
138 // only the requested area, but that's risky because this can
|
Chris@1079
|
139 // happen when just waving the pointer over a small part of
|
Chris@1079
|
140 // the view -- if we lose the partly-built cache every time
|
Chris@1079
|
141 // the user does that, we'll never finish building it.
|
Chris@1079
|
142 int left = x0;
|
Chris@1079
|
143 int width = x1 - x0;
|
Chris@1079
|
144 bool isLeftOfValidArea = false;
|
Chris@1079
|
145 m_cache.adjustToTouchValidArea(left, width, isLeftOfValidArea);
|
Chris@1079
|
146 x0 = left;
|
Chris@1079
|
147 x1 = x0 + width;
|
Chris@1079
|
148
|
Chris@1079
|
149 // That call also told us whether we should be painting
|
Chris@1079
|
150 // sub-regions of our target region in right-to-left order in
|
Chris@1079
|
151 // order to ensure contiguity
|
Chris@1079
|
152 rightToLeft = isLeftOfValidArea;
|
Chris@1079
|
153 }
|
Chris@1075
|
154
|
Chris@1094
|
155 // Note, we always paint the full height. Smaller heights can be
|
Chris@1094
|
156 // used when painting direct from cache (outside this function),
|
Chris@1094
|
157 // but we want to ensure the cache is coherent without having to
|
Chris@1094
|
158 // worry about vertical matching of required and valid areas as
|
Chris@1094
|
159 // well as horizontal. That's why this function didn't take any
|
Chris@1094
|
160 // y/height parameters.
|
Chris@1094
|
161
|
Chris@1094
|
162 if (bufferIsBinResolution) {
|
Chris@1094
|
163 renderToCacheBinResolution(v, x0, x1 - x0);
|
Chris@1094
|
164 } else {
|
Chris@1094
|
165 renderToCachePixelResolution(v, x0, x1 - x0, rightToLeft, timeConstrained);
|
Chris@1094
|
166 }
|
Chris@1079
|
167
|
Chris@1079
|
168 QRect pr = rect & m_cache.getValidArea();
|
Chris@1079
|
169 paint.drawImage(pr.x(), pr.y(), m_cache.getImage(),
|
Chris@1079
|
170 pr.x(), pr.y(), pr.width(), pr.height());
|
Chris@1079
|
171
|
Chris@1079
|
172 if (!timeConstrained && (pr != rect)) {
|
Chris@1079
|
173 //!!! on a first cut, there is a risk that this will happen
|
Chris@1079
|
174 //!!! when we are at start/end of model -- trap, report, and
|
Chris@1079
|
175 //!!! then fix
|
Chris@1079
|
176 throw std::logic_error("internal error: failed to render entire requested rect even when not time-constrained");
|
Chris@1079
|
177 }
|
Chris@1079
|
178
|
Chris@1079
|
179 return { pr, {} };
|
Chris@1079
|
180
|
Chris@1073
|
181 //!!! todo: timing/incomplete paint
|
Chris@1073
|
182
|
Chris@1073
|
183 //!!! todo: peak frequency style
|
Chris@1073
|
184
|
Chris@1073
|
185 //!!! todo: transparent style from Colour3DPlot
|
Chris@1074
|
186
|
Chris@1074
|
187 //!!! todo: bin boundary alignment when in BinResolution
|
Chris@1074
|
188
|
Chris@1079
|
189 //!!! todo: view magnitudes / normalise visible area
|
Chris@1079
|
190
|
Chris@1079
|
191 //!!! todo: alter documentation for view mag stuff (cached paints
|
Chris@1079
|
192 //!!! do not update MagnitudeRange)
|
Chris@1079
|
193
|
Chris@1079
|
194 //!!! todo, here or in caller: illuminateLocalFeatures
|
Chris@1079
|
195
|
Chris@1079
|
196 //!!! fft model scaling?
|
Chris@1079
|
197
|
Chris@1079
|
198 //!!! should we own the Dense3DModelPeakCache here? or should it persist
|
Chris@1073
|
199 }
|
Chris@1073
|
200
|
Chris@1094
|
201 bool
|
Chris@1094
|
202 Colour3DPlotRenderer::useBinResolutionForDrawBuffer(LayerGeometryProvider *v) const
|
Chris@1094
|
203 {
|
Chris@1094
|
204 DenseThreeDimensionalModel *model = m_sources.source;
|
Chris@1094
|
205 if (!model) return false;
|
Chris@1094
|
206 int binResolution = model->getResolution();
|
Chris@1094
|
207 int zoomLevel = v->getZoomLevel();
|
Chris@1094
|
208 return (binResolution > zoomLevel);
|
Chris@1094
|
209 }
|
Chris@1094
|
210
|
Chris@1080
|
211 void
|
Chris@1094
|
212 Colour3DPlotRenderer::renderToCachePixelResolution(LayerGeometryProvider *v,
|
Chris@1094
|
213 int x0, int repaintWidth,
|
Chris@1094
|
214 bool rightToLeft,
|
Chris@1094
|
215 bool timeConstrained)
|
Chris@1079
|
216 {
|
Chris@1094
|
217 cerr << "renderToCachePixelResolution" << endl;
|
Chris@1094
|
218
|
Chris@1094
|
219 // Draw to the draw buffer, and then copy from there. The draw
|
Chris@1094
|
220 // buffer is at the same resolution as the target in the cache, so
|
Chris@1094
|
221 // no extra scaling needed.
|
Chris@1079
|
222
|
Chris@1079
|
223 DenseThreeDimensionalModel *model = m_sources.source;
|
Chris@1079
|
224 if (!model || !model->isOK() || !model->isReady()) {
|
Chris@1079
|
225 throw std::logic_error("no source model provided, or model not ready");
|
Chris@1079
|
226 }
|
Chris@1079
|
227
|
Chris@1079
|
228 int h = v->getPaintHeight();
|
Chris@1079
|
229
|
Chris@1094
|
230 clearDrawBuffer(repaintWidth, h);
|
Chris@1079
|
231
|
Chris@1094
|
232 vector<int> binforx(repaintWidth);
|
Chris@1079
|
233 vector<double> binfory(h);
|
Chris@1079
|
234
|
Chris@1079
|
235 bool usePeaksCache = false;
|
Chris@1079
|
236 int binsPerPeak = 1;
|
Chris@1094
|
237 int zoomLevel = v->getZoomLevel();
|
Chris@1094
|
238 int binResolution = model->getResolution();
|
Chris@1079
|
239
|
Chris@1094
|
240 for (int x = 0; x < repaintWidth; ++x) {
|
Chris@1094
|
241 sv_frame_t f0 = v->getFrameForX(x0 + x);
|
Chris@1094
|
242 double s0 = double(f0 - model->getStartFrame()) / binResolution;
|
Chris@1094
|
243 binforx[x] = int(s0 + 0.0001);
|
Chris@1094
|
244 }
|
Chris@1080
|
245
|
Chris@1094
|
246 if (m_sources.peaks) { // peaks cache exists
|
Chris@1080
|
247
|
Chris@1094
|
248 binsPerPeak = m_sources.peaks->getColumnsPerPeak();
|
Chris@1094
|
249 usePeaksCache = (binResolution * binsPerPeak) < zoomLevel;
|
Chris@1094
|
250
|
Chris@1094
|
251 if (m_params.colourScale.getScale() ==
|
Chris@1094
|
252 ColourScale::PhaseColourScale) {
|
Chris@1094
|
253 usePeaksCache = false;
|
Chris@1079
|
254 }
|
Chris@1079
|
255 }
|
Chris@1082
|
256
|
Chris@1094
|
257 cerr << "[PIX] zoomLevel = " << zoomLevel
|
Chris@1094
|
258 << ", binResolution " << binResolution
|
Chris@1094
|
259 << ", binsPerPeak " << binsPerPeak
|
Chris@1094
|
260 << ", peak cache " << m_sources.peaks
|
Chris@1094
|
261 << ", usePeaksCache = " << usePeaksCache
|
Chris@1094
|
262 << endl;
|
Chris@1094
|
263
|
Chris@1080
|
264 for (int y = 0; y < h; ++y) {
|
Chris@1090
|
265 binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1);
|
Chris@1080
|
266 }
|
Chris@1079
|
267
|
Chris@1083
|
268 int attainedWidth = renderDrawBuffer(repaintWidth,
|
Chris@1080
|
269 h,
|
Chris@1080
|
270 binforx,
|
Chris@1080
|
271 binfory,
|
Chris@1080
|
272 usePeaksCache,
|
Chris@1080
|
273 rightToLeft,
|
Chris@1080
|
274 timeConstrained);
|
Chris@1083
|
275
|
Chris@1094
|
276 if (attainedWidth == 0) return;
|
Chris@1084
|
277
|
Chris@1094
|
278 // draw buffer is pixel resolution, no scaling factors or padding involved
|
Chris@1084
|
279
|
Chris@1084
|
280 int paintedLeft = x0;
|
Chris@1084
|
281 if (rightToLeft) {
|
Chris@1084
|
282 paintedLeft += (repaintWidth - attainedWidth);
|
Chris@1084
|
283 }
|
Chris@1084
|
284
|
Chris@1094
|
285 m_cache.drawImage(paintedLeft, attainedWidth,
|
Chris@1094
|
286 m_drawBuffer,
|
Chris@1094
|
287 paintedLeft - x0, attainedWidth);
|
Chris@1094
|
288 }
|
Chris@1084
|
289
|
Chris@1094
|
290 void
|
Chris@1094
|
291 Colour3DPlotRenderer::renderToCacheBinResolution(LayerGeometryProvider *v,
|
Chris@1094
|
292 int x0, int repaintWidth)
|
Chris@1094
|
293 {
|
Chris@1094
|
294 cerr << "renderToCacheBinResolution" << endl;
|
Chris@1094
|
295
|
Chris@1094
|
296 // Draw to the draw buffer, and then scale-copy from there. Draw
|
Chris@1094
|
297 // buffer is at bin resolution, i.e. buffer x == source column
|
Chris@1094
|
298 // number. We use toolkit smooth scaling for interpolation.
|
Chris@1084
|
299
|
Chris@1094
|
300 DenseThreeDimensionalModel *model = m_sources.source;
|
Chris@1094
|
301 if (!model || !model->isOK() || !model->isReady()) {
|
Chris@1094
|
302 throw std::logic_error("no source model provided, or model not ready");
|
Chris@1094
|
303 }
|
Chris@1094
|
304
|
Chris@1094
|
305 // The draw buffer will contain a fragment at bin resolution. We
|
Chris@1094
|
306 // need to ensure that it starts and ends at points where a
|
Chris@1094
|
307 // time-bin boundary occurs at an exact pixel boundary, and with a
|
Chris@1094
|
308 // certain amount of overlap across existing pixels so that we can
|
Chris@1094
|
309 // scale and draw from it without smoothing errors at the edges.
|
Chris@1094
|
310
|
Chris@1094
|
311 // If (getFrameForX(x) / increment) * increment ==
|
Chris@1094
|
312 // getFrameForX(x), then x is a time-bin boundary. We want two
|
Chris@1094
|
313 // such boundaries at either side of the draw buffer -- one which
|
Chris@1094
|
314 // we draw up to, and one which we subsequently crop at.
|
Chris@1094
|
315
|
Chris@1094
|
316 sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1;
|
Chris@1094
|
317 sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1;
|
Chris@1094
|
318
|
Chris@1094
|
319 int drawBufferWidth;
|
Chris@1094
|
320 int binResolution = model->getResolution();
|
Chris@1094
|
321
|
Chris@1094
|
322 for (int x = x0; ; --x) {
|
Chris@1094
|
323 sv_frame_t f = v->getFrameForX(x);
|
Chris@1094
|
324 if ((f / binResolution) * binResolution == f) {
|
Chris@1094
|
325 if (leftCropFrame == -1) leftCropFrame = f;
|
Chris@1094
|
326 else if (x < x0 - 2) {
|
Chris@1094
|
327 leftBoundaryFrame = f;
|
Chris@1094
|
328 break;
|
Chris@1094
|
329 }
|
Chris@1094
|
330 }
|
Chris@1094
|
331 }
|
Chris@1094
|
332 for (int x = x0 + repaintWidth; ; ++x) {
|
Chris@1094
|
333 sv_frame_t f = v->getFrameForX(x);
|
Chris@1094
|
334 if ((f / binResolution) * binResolution == f) {
|
Chris@1094
|
335 if (rightCropFrame == -1) rightCropFrame = f;
|
Chris@1094
|
336 else if (x > x0 + repaintWidth + 2) {
|
Chris@1094
|
337 rightBoundaryFrame = f;
|
Chris@1094
|
338 break;
|
Chris@1094
|
339 }
|
Chris@1094
|
340 }
|
Chris@1094
|
341 }
|
Chris@1094
|
342 drawBufferWidth = int
|
Chris@1094
|
343 ((rightBoundaryFrame - leftBoundaryFrame) / binResolution);
|
Chris@1094
|
344
|
Chris@1094
|
345 int h = v->getPaintHeight();
|
Chris@1094
|
346
|
Chris@1095
|
347 // For our purposes here, the draw buffer needs to be exactly our
|
Chris@1095
|
348 // target size (so we recreate always rather than just clear it)
|
Chris@1095
|
349
|
Chris@1095
|
350 recreateDrawBuffer(drawBufferWidth, h);
|
Chris@1094
|
351
|
Chris@1094
|
352 vector<int> binforx(drawBufferWidth);
|
Chris@1094
|
353 vector<double> binfory(h);
|
Chris@1094
|
354
|
Chris@1094
|
355 for (int x = 0; x < drawBufferWidth; ++x) {
|
Chris@1094
|
356 binforx[x] = int(leftBoundaryFrame / binResolution) + x;
|
Chris@1094
|
357 }
|
Chris@1094
|
358
|
Chris@1094
|
359 cerr << "[BIN] binResolution " << binResolution
|
Chris@1094
|
360 << endl;
|
Chris@1094
|
361
|
Chris@1094
|
362 for (int y = 0; y < h; ++y) {
|
Chris@1094
|
363 binfory[y] = m_sources.verticalBinLayer->getBinForY(v, h - y - 1);
|
Chris@1094
|
364 }
|
Chris@1094
|
365
|
Chris@1094
|
366 int attainedWidth = renderDrawBuffer(drawBufferWidth,
|
Chris@1094
|
367 h,
|
Chris@1094
|
368 binforx,
|
Chris@1094
|
369 binfory,
|
Chris@1094
|
370 false,
|
Chris@1094
|
371 false,
|
Chris@1094
|
372 false);
|
Chris@1094
|
373
|
Chris@1094
|
374 if (attainedWidth == 0) return;
|
Chris@1094
|
375
|
Chris@1094
|
376 int scaledLeft = v->getXForFrame(leftBoundaryFrame);
|
Chris@1094
|
377 int scaledRight = v->getXForFrame(rightBoundaryFrame);
|
Chris@1095
|
378
|
Chris@1095
|
379 cerr << "scaling draw buffer from width " << m_drawBuffer.width()
|
Chris@1095
|
380 << " to " << (scaledRight - scaledLeft) << " (nb drawBufferWidth = "
|
Chris@1095
|
381 << drawBufferWidth << ")" << endl;
|
Chris@1094
|
382
|
Chris@1094
|
383 QImage scaled = m_drawBuffer.scaled
|
Chris@1094
|
384 (scaledRight - scaledLeft, h,
|
Chris@1094
|
385 Qt::IgnoreAspectRatio, (m_params.interpolate ?
|
Chris@1094
|
386 Qt::SmoothTransformation :
|
Chris@1094
|
387 Qt::FastTransformation));
|
Chris@1084
|
388
|
Chris@1094
|
389 int scaledLeftCrop = v->getXForFrame(leftCropFrame);
|
Chris@1094
|
390 int scaledRightCrop = v->getXForFrame(rightCropFrame);
|
Chris@1094
|
391
|
Chris@1094
|
392 int targetLeft = scaledLeftCrop;
|
Chris@1094
|
393 if (targetLeft < 0) {
|
Chris@1094
|
394 targetLeft = 0;
|
Chris@1094
|
395 }
|
Chris@1094
|
396
|
Chris@1094
|
397 int targetWidth = scaledRightCrop - targetLeft;
|
Chris@1094
|
398 if (targetLeft + targetWidth > m_cache.getSize().width()) {
|
Chris@1094
|
399 targetWidth = m_cache.getSize().width() - targetLeft;
|
Chris@1094
|
400 }
|
Chris@1094
|
401
|
Chris@1094
|
402 int sourceLeft = targetLeft - scaledLeft;
|
Chris@1094
|
403 if (sourceLeft < 0) {
|
Chris@1094
|
404 sourceLeft = 0;
|
Chris@1094
|
405 }
|
Chris@1094
|
406
|
Chris@1094
|
407 int sourceWidth = targetWidth;
|
Chris@1094
|
408
|
Chris@1094
|
409 cerr << "repaintWidth = " << repaintWidth
|
Chris@1094
|
410 << ", targetWidth = " << targetWidth << endl;
|
Chris@1094
|
411
|
Chris@1094
|
412 if (targetWidth > 0) {
|
Chris@1094
|
413 m_cache.drawImage(targetLeft, targetWidth,
|
Chris@1094
|
414 scaled,
|
Chris@1094
|
415 sourceLeft, sourceWidth);
|
Chris@1084
|
416 }
|
Chris@1079
|
417 }
|
Chris@1083
|
418
|
Chris@1083
|
419 int
|
Chris@1083
|
420 Colour3DPlotRenderer::renderDrawBuffer(int w, int h,
|
Chris@1083
|
421 const vector<int> &binforx,
|
Chris@1083
|
422 const vector<double> &binfory,
|
Chris@1083
|
423 bool usePeaksCache,
|
Chris@1083
|
424 bool rightToLeft,
|
Chris@1083
|
425 bool timeConstrained)
|
Chris@1083
|
426 {
|
Chris@1083
|
427 // Callers must have checked that the appropriate subset of
|
Chris@1083
|
428 // Sources data members are set for the supplied flags (e.g. that
|
Chris@1083
|
429 // peaks model exists if usePeaksCache)
|
Chris@1083
|
430
|
Chris@1083
|
431 RenderTimer timer(timeConstrained ?
|
Chris@1083
|
432 RenderTimer::FastRender :
|
Chris@1083
|
433 RenderTimer::NoTimeout);
|
Chris@1083
|
434
|
Chris@1083
|
435 int minbin = int(binfory[0] + 0.0001);
|
Chris@1083
|
436 int maxbin = int(binfory[h-1]);
|
Chris@1083
|
437 if (minbin < 0) minbin = 0;
|
Chris@1083
|
438 if (maxbin < 0) maxbin = minbin+1;
|
Chris@1083
|
439
|
Chris@1083
|
440 int divisor = 1;
|
Chris@1083
|
441 DenseThreeDimensionalModel *sourceModel = m_sources.source;
|
Chris@1083
|
442 if (usePeaksCache) {
|
Chris@1083
|
443 divisor = m_sources.peaks->getColumnsPerPeak();
|
Chris@1083
|
444 sourceModel = m_sources.peaks;
|
Chris@1083
|
445 }
|
Chris@1083
|
446
|
Chris@1083
|
447 int psx = -1;
|
Chris@1083
|
448
|
Chris@1083
|
449 int start = 0;
|
Chris@1083
|
450 int finish = w;
|
Chris@1083
|
451 int step = 1;
|
Chris@1083
|
452
|
Chris@1083
|
453 if (rightToLeft) {
|
Chris@1083
|
454 start = w-1;
|
Chris@1083
|
455 finish = -1;
|
Chris@1083
|
456 step = -1;
|
Chris@1083
|
457 }
|
Chris@1083
|
458
|
Chris@1083
|
459 int columnCount = 0;
|
Chris@1083
|
460
|
Chris@1083
|
461 vector<float> preparedColumn;
|
Chris@1094
|
462
|
Chris@1094
|
463 int modelWidth = sourceModel->getWidth();
|
Chris@1094
|
464 cerr << "modelWidth " << modelWidth << endl;
|
Chris@1083
|
465
|
Chris@1083
|
466 for (int x = start; x != finish; x += step) {
|
Chris@1083
|
467
|
Chris@1083
|
468 // x is the on-canvas pixel coord; sx (later) will be the
|
Chris@1083
|
469 // source column index
|
Chris@1083
|
470
|
Chris@1083
|
471 ++columnCount;
|
Chris@1083
|
472
|
Chris@1083
|
473 if (binforx[x] < 0) continue;
|
Chris@1083
|
474
|
Chris@1083
|
475 int sx0 = binforx[x] / divisor;
|
Chris@1083
|
476 int sx1 = sx0;
|
Chris@1083
|
477 if (x+1 < w) sx1 = binforx[x+1] / divisor;
|
Chris@1083
|
478 if (sx0 < 0) sx0 = sx1 - 1;
|
Chris@1083
|
479 if (sx0 < 0) continue;
|
Chris@1083
|
480 if (sx1 <= sx0) sx1 = sx0 + 1;
|
Chris@1083
|
481
|
Chris@1083
|
482 vector<float> pixelPeakColumn;
|
Chris@1083
|
483
|
Chris@1083
|
484 for (int sx = sx0; sx < sx1; ++sx) {
|
Chris@1083
|
485
|
Chris@1083
|
486 #ifdef DEBUG_SPECTROGRAM_REPAINT
|
Chris@1094
|
487 cerr << "sx = " << sx << endl;
|
Chris@1083
|
488 #endif
|
Chris@1083
|
489
|
Chris@1094
|
490 if (sx < 0 || sx >= modelWidth) {
|
Chris@1083
|
491 continue;
|
Chris@1083
|
492 }
|
Chris@1083
|
493
|
Chris@1083
|
494 if (sx != psx) {
|
Chris@1083
|
495
|
Chris@1083
|
496 // order:
|
Chris@1083
|
497 // get column -> scale -> record extents ->
|
Chris@1083
|
498 // normalise -> peak pick -> apply display gain ->
|
Chris@1083
|
499 // distribute/interpolate
|
Chris@1083
|
500
|
Chris@1083
|
501 ColumnOp::Column fullColumn = sourceModel->getColumn(sx);
|
Chris@1090
|
502
|
Chris@1094
|
503 // cerr << "x " << x << ", sx " << sx << ", col height " << fullColumn.size()
|
Chris@1094
|
504 // << ", minbin " << minbin << ", maxbin " << maxbin << endl;
|
Chris@1090
|
505
|
Chris@1083
|
506 ColumnOp::Column column =
|
Chris@1083
|
507 vector<float>(fullColumn.data() + minbin,
|
Chris@1083
|
508 fullColumn.data() + maxbin + 1);
|
Chris@1083
|
509
|
Chris@1083
|
510 //!!! fft scale if (m_colourScale != PhaseColourScale) {
|
Chris@1083
|
511 // column = ColumnOp::fftScale(column, m_fftSize);
|
Chris@1083
|
512 // }
|
Chris@1083
|
513
|
Chris@1083
|
514 //!!! extents recordColumnExtents(column,
|
Chris@1083
|
515 // sx,
|
Chris@1083
|
516 // overallMag,
|
Chris@1083
|
517 // overallMagChanged);
|
Chris@1083
|
518
|
Chris@1083
|
519 // if (m_colourScale != PhaseColourScale) {
|
Chris@1083
|
520 column = ColumnOp::normalize(column, m_params.normalization);
|
Chris@1083
|
521 // }
|
Chris@1083
|
522
|
Chris@1083
|
523 if (m_params.binDisplay == PeakBins) {
|
Chris@1083
|
524 column = ColumnOp::peakPick(column);
|
Chris@1083
|
525 }
|
Chris@1083
|
526
|
Chris@1083
|
527 preparedColumn =
|
Chris@1083
|
528 ColumnOp::distribute(column, //!!! gain? ColumnOp::applyGain(column, m_gain),
|
Chris@1083
|
529 h,
|
Chris@1083
|
530 binfory,
|
Chris@1083
|
531 minbin,
|
Chris@1083
|
532 m_params.interpolate);
|
Chris@1083
|
533
|
Chris@1083
|
534 psx = sx;
|
Chris@1083
|
535 }
|
Chris@1083
|
536
|
Chris@1083
|
537 if (sx == sx0) {
|
Chris@1083
|
538 pixelPeakColumn = preparedColumn;
|
Chris@1083
|
539 } else {
|
Chris@1083
|
540 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
|
Chris@1083
|
541 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
|
Chris@1083
|
542 preparedColumn[i]);
|
Chris@1083
|
543 }
|
Chris@1083
|
544 }
|
Chris@1083
|
545 }
|
Chris@1083
|
546
|
Chris@1083
|
547 if (!pixelPeakColumn.empty()) {
|
Chris@1083
|
548 for (int y = 0; y < h; ++y) {
|
Chris@1083
|
549 m_drawBuffer.setPixel
|
Chris@1083
|
550 (x,
|
Chris@1083
|
551 h-y-1,
|
Chris@1083
|
552 m_params.colourScale.getPixel(pixelPeakColumn[y]));
|
Chris@1083
|
553 }
|
Chris@1083
|
554 }
|
Chris@1083
|
555
|
Chris@1083
|
556 double fractionComplete = double(columnCount) / double(w);
|
Chris@1083
|
557 if (timer.outOfTime(fractionComplete)) {
|
Chris@1083
|
558 return columnCount;
|
Chris@1083
|
559 }
|
Chris@1083
|
560 }
|
Chris@1083
|
561
|
Chris@1083
|
562 return columnCount;
|
Chris@1083
|
563 }
|
Chris@1083
|
564
|
Chris@1079
|
565 void
|
Chris@1095
|
566 Colour3DPlotRenderer::recreateDrawBuffer(int w, int h)
|
Chris@1079
|
567 {
|
Chris@1095
|
568 m_drawBuffer = QImage(w, h, QImage::Format_Indexed8);
|
Chris@1079
|
569
|
Chris@1095
|
570 for (int pixel = 0; pixel < 256; ++pixel) {
|
Chris@1095
|
571 //!!! todo: colour rotation (here 0)
|
Chris@1095
|
572 m_drawBuffer.setColor
|
Chris@1095
|
573 ((unsigned char)pixel,
|
Chris@1095
|
574 m_params.colourScale.getColourForPixel(pixel, 0).rgb());
|
Chris@1079
|
575 }
|
Chris@1079
|
576
|
Chris@1079
|
577 m_drawBuffer.fill(0);
|
Chris@1079
|
578 }
|
Chris@1079
|
579
|
Chris@1095
|
580 void
|
Chris@1095
|
581 Colour3DPlotRenderer::clearDrawBuffer(int w, int h)
|
Chris@1095
|
582 {
|
Chris@1095
|
583 if (m_drawBuffer.width() < w || m_drawBuffer.height() != h) {
|
Chris@1095
|
584 recreateDrawBuffer(w, h);
|
Chris@1095
|
585 } else {
|
Chris@1095
|
586 m_drawBuffer.fill(0);
|
Chris@1095
|
587 }
|
Chris@1095
|
588 }
|
Chris@1079
|
589
|
Chris@1095
|
590
|