annotate layer/ScrollableImageCache.cpp @ 1054:5e5873c24142 3.0-integration

Make peak cache divisor a member
author Chris Cannam
date Mon, 13 Jun 2016 11:42:51 +0100
parents 25b035362c44
children 7ebfb61b1701
rev   line source
Chris@1035 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1035 2
Chris@1035 3 /*
Chris@1035 4 Sonic Visualiser
Chris@1035 5 An audio file viewer and annotation editor.
Chris@1035 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1035 7
Chris@1035 8 This program is free software; you can redistribute it and/or
Chris@1035 9 modify it under the terms of the GNU General Public License as
Chris@1035 10 published by the Free Software Foundation; either version 2 of the
Chris@1035 11 License, or (at your option) any later version. See the file
Chris@1035 12 COPYING included with this distribution for more information.
Chris@1035 13 */
Chris@1035 14
Chris@1035 15 #include "ScrollableImageCache.h"
Chris@1035 16
Chris@1035 17 #include <iostream>
Chris@1035 18 using namespace std;
Chris@1035 19
Chris@1040 20 //#define DEBUG_SCROLLABLE_IMAGE_CACHE 1
Chris@1035 21
Chris@1035 22 void
Chris@1035 23 ScrollableImageCache::scrollTo(sv_frame_t newStartFrame)
Chris@1035 24 {
Chris@1035 25 if (!m_v) throw std::logic_error("ScrollableImageCache: not associated with a LayerGeometryProvider");
Chris@1035 26
Chris@1035 27 int dx = (m_v->getXForFrame(m_startFrame) -
Chris@1035 28 m_v->getXForFrame(newStartFrame));
Chris@1035 29
Chris@1035 30 #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE
Chris@1035 31 cerr << "ScrollableImageCache::scrollTo: start frame " << m_startFrame
Chris@1035 32 << " -> " << newStartFrame << ", dx = " << dx << endl;
Chris@1035 33 #endif
Chris@1035 34
Chris@1035 35 m_startFrame = newStartFrame;
Chris@1035 36
Chris@1035 37 if (!isValid()) {
Chris@1035 38 return;
Chris@1035 39 }
Chris@1035 40
Chris@1035 41 int w = m_image.width();
Chris@1035 42
Chris@1035 43 if (dx == 0) {
Chris@1035 44 // haven't moved
Chris@1035 45 return;
Chris@1035 46 }
Chris@1035 47
Chris@1035 48 if (dx <= -w || dx >= w) {
Chris@1035 49 // scrolled entirely off
Chris@1035 50 invalidate();
Chris@1035 51 return;
Chris@1035 52 }
Chris@1035 53
Chris@1035 54 // dx is in range, cache is scrollable
Chris@1035 55
Chris@1035 56 int dxp = dx;
Chris@1035 57 if (dxp < 0) dxp = -dxp;
Chris@1035 58
Chris@1035 59 int copylen = (w - dxp) * int(sizeof(QRgb));
Chris@1035 60 for (int y = 0; y < m_image.height(); ++y) {
Chris@1035 61 QRgb *line = (QRgb *)m_image.scanLine(y);
Chris@1035 62 if (dx < 0) {
Chris@1035 63 memmove(line, line + dxp, copylen);
Chris@1035 64 } else {
Chris@1035 65 memmove(line + dxp, line, copylen);
Chris@1035 66 }
Chris@1035 67 }
Chris@1035 68
Chris@1035 69 // update valid area
Chris@1035 70
Chris@1035 71 int px = m_left;
Chris@1035 72 int pw = m_width;
Chris@1035 73
Chris@1035 74 px += dx;
Chris@1035 75
Chris@1035 76 if (dx < 0) {
Chris@1035 77 // we scrolled left
Chris@1035 78 if (px < 0) {
Chris@1035 79 pw += px;
Chris@1035 80 px = 0;
Chris@1035 81 if (pw < 0) {
Chris@1035 82 pw = 0;
Chris@1035 83 }
Chris@1035 84 }
Chris@1035 85 } else {
Chris@1035 86 // we scrolled right
Chris@1035 87 if (px + pw > w) {
Chris@1035 88 pw = w - px;
Chris@1035 89 if (pw < 0) {
Chris@1035 90 pw = 0;
Chris@1035 91 }
Chris@1035 92 }
Chris@1035 93 }
Chris@1035 94
Chris@1035 95 m_left = px;
Chris@1035 96 m_width = pw;
Chris@1035 97 }
Chris@1035 98
Chris@1035 99 void
Chris@1035 100 ScrollableImageCache::adjustToTouchValidArea(int &left, int &width,
Chris@1035 101 bool &isLeftOfValidArea) const
Chris@1035 102 {
Chris@1036 103 #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE
Chris@1036 104 cerr << "ScrollableImageCache::adjustToTouchValidArea: left " << left
Chris@1036 105 << ", width " << width << endl;
Chris@1036 106 cerr << "ScrollableImageCache: my left " << m_left
Chris@1036 107 << ", width " << m_width << " so right " << (m_left + m_width) << endl;
Chris@1036 108 #endif
Chris@1035 109 if (left < m_left) {
Chris@1035 110 isLeftOfValidArea = true;
Chris@1036 111 if (left + width <= m_left + m_width) {
Chris@1035 112 width = m_left - left;
Chris@1035 113 }
Chris@1036 114 #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE
Chris@1036 115 cerr << "ScrollableImageCache: we're left of valid area, adjusted width to " << width << endl;
Chris@1036 116 #endif
Chris@1035 117 } else {
Chris@1035 118 isLeftOfValidArea = false;
Chris@1035 119 width = left + width - (m_left + m_width);
Chris@1035 120 left = m_left + m_width;
Chris@1035 121 if (width < 0) width = 0;
Chris@1036 122 #ifdef DEBUG_SCROLLABLE_IMAGE_CACHE
Chris@1036 123 cerr << "ScrollableImageCache: we're right of valid area, adjusted left to " << left << ", width to " << width << endl;
Chris@1036 124 #endif
Chris@1035 125 }
Chris@1035 126 }
Chris@1035 127
Chris@1035 128 void
Chris@1035 129 ScrollableImageCache::drawImage(int left,
Chris@1035 130 int width,
Chris@1035 131 QImage image,
Chris@1035 132 int imageLeft,
Chris@1035 133 int imageWidth)
Chris@1035 134 {
Chris@1035 135 if (image.height() != m_image.height()) {
Chris@1035 136 cerr << "ScrollableImageCache::drawImage: ERROR: Supplied image height "
Chris@1035 137 << image.height() << " does not match cache height "
Chris@1035 138 << m_image.height() << endl;
Chris@1035 139 throw std::logic_error("Image height must match cache height in ScrollableImageCache::drawImage");
Chris@1035 140 }
Chris@1035 141 if (left < 0 || width < 0 || left + width > m_image.width()) {
Chris@1035 142 cerr << "ScrollableImageCache::drawImage: ERROR: Target area (left = "
Chris@1040 143 << left << ", width = " << width << ", so right = " << left + width
Chris@1040 144 << ") out of bounds for cache of width " << m_image.width() << endl;
Chris@1035 145 throw std::logic_error("Target area out of bounds in ScrollableImageCache::drawImage");
Chris@1035 146 }
Chris@1035 147 if (imageLeft < 0 || imageWidth < 0 ||
Chris@1035 148 imageLeft + imageWidth > image.width()) {
Chris@1035 149 cerr << "ScrollableImageCache::drawImage: ERROR: Source area (left = "
Chris@1040 150 << imageLeft << ", width = " << imageWidth << ", so right = "
Chris@1040 151 << imageLeft + imageWidth << ") out of bounds for image of "
Chris@1035 152 << "width " << image.width() << endl;
Chris@1035 153 throw std::logic_error("Source area out of bounds in ScrollableImageCache::drawImage");
Chris@1035 154 }
Chris@1035 155
Chris@1035 156 QPainter painter(&m_image);
Chris@1035 157 painter.drawImage(QRect(left, 0, width, m_image.height()),
Chris@1035 158 image,
Chris@1035 159 QRect(imageLeft, 0, imageWidth, image.height()));
Chris@1035 160 painter.end();
Chris@1035 161
Chris@1035 162 if (!isValid()) {
Chris@1035 163 m_left = left;
Chris@1035 164 m_width = width;
Chris@1035 165 return;
Chris@1035 166 }
Chris@1035 167
Chris@1035 168 if (left < m_left) {
Chris@1035 169 if (left + width > m_left + m_width) {
Chris@1035 170 // new image completely contains the old valid area --
Chris@1035 171 // use the new area as is
Chris@1035 172 m_left = left;
Chris@1035 173 m_width = width;
Chris@1035 174 } else if (left + width < m_left) {
Chris@1035 175 // new image completely off left of old valid area --
Chris@1035 176 // we can't extend the valid area because the bit in
Chris@1035 177 // between is not valid, so must use the new area only
Chris@1035 178 m_left = left;
Chris@1035 179 m_width = width;
Chris@1035 180 } else {
Chris@1035 181 // new image overlaps old valid area on left side --
Chris@1035 182 // use new left edge, and extend width to existing
Chris@1035 183 // right edge
Chris@1035 184 m_width = (m_left + m_width) - left;
Chris@1035 185 m_left = left;
Chris@1035 186 }
Chris@1035 187 } else {
Chris@1035 188 if (left > m_left + m_width) {
Chris@1035 189 // new image completely off right of old valid area --
Chris@1035 190 // we can't extend the valid area because the bit in
Chris@1035 191 // between is not valid, so must use the new area only
Chris@1035 192 m_left = left;
Chris@1035 193 m_width = width;
Chris@1035 194 } else if (left + width > m_left + m_width) {
Chris@1035 195 // new image overlaps old valid area on right side --
Chris@1035 196 // use existing left edge, and extend width to new
Chris@1035 197 // right edge
Chris@1035 198 m_width = (left + width) - m_left;
Chris@1035 199 // (m_left unchanged)
Chris@1035 200 } else {
Chris@1035 201 // new image completely contained within old valid
Chris@1035 202 // area -- leave the old area unchanged
Chris@1035 203 }
Chris@1035 204 }
Chris@1035 205 }
Chris@1035 206