Mercurial > hg > svcore
annotate data/model/PowerOfTwoZoomConstraint.cpp @ 1528:a7485c1bdba5
Tests and a couple of minor fixes for zoom constraints
author | Chris Cannam |
---|---|
date | Tue, 18 Sep 2018 15:04:46 +0100 |
parents | 48e9f538e6e9 |
children | c1c45c5146bb |
rev | line source |
---|---|
Chris@147 | 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ |
Chris@147 | 2 |
Chris@147 | 3 /* |
Chris@147 | 4 Sonic Visualiser |
Chris@147 | 5 An audio file viewer and annotation editor. |
Chris@147 | 6 Centre for Digital Music, Queen Mary, University of London. |
Chris@147 | 7 This file copyright 2006 Chris Cannam. |
Chris@147 | 8 |
Chris@147 | 9 This program is free software; you can redistribute it and/or |
Chris@147 | 10 modify it under the terms of the GNU General Public License as |
Chris@147 | 11 published by the Free Software Foundation; either version 2 of the |
Chris@147 | 12 License, or (at your option) any later version. See the file |
Chris@147 | 13 COPYING included with this distribution for more information. |
Chris@147 | 14 */ |
Chris@147 | 15 |
Chris@147 | 16 #include "PowerOfTwoZoomConstraint.h" |
Chris@147 | 17 |
Chris@929 | 18 int |
Chris@929 | 19 PowerOfTwoZoomConstraint::getNearestBlockSize(int req, |
Chris@1429 | 20 RoundingDirection dir) const |
Chris@147 | 21 { |
Chris@1528 | 22 int max = getMaxZoomLevel(); |
Chris@147 | 23 |
Chris@1528 | 24 if (req > max) { |
Chris@1528 | 25 return max; |
Chris@1528 | 26 } |
Chris@1528 | 27 |
Chris@1528 | 28 for (int bs = 1; bs <= max; bs *= 2) { |
Chris@1528 | 29 if (bs < req) { |
Chris@1528 | 30 continue; |
Chris@1528 | 31 } else if (bs == req) { |
Chris@1528 | 32 return bs; |
Chris@1528 | 33 } else { // bs > req |
Chris@1429 | 34 if (dir == RoundNearest) { |
Chris@1429 | 35 if (bs - req < req - bs/2) { |
Chris@1528 | 36 return bs; |
Chris@1429 | 37 } else { |
Chris@1528 | 38 return bs/2; |
Chris@1429 | 39 } |
Chris@1429 | 40 } else if (dir == RoundDown) { |
Chris@1528 | 41 return bs/2; |
Chris@1429 | 42 } else { |
Chris@1528 | 43 return bs; |
Chris@1429 | 44 } |
Chris@1429 | 45 } |
Chris@147 | 46 } |
Chris@147 | 47 |
Chris@1528 | 48 return max; |
Chris@147 | 49 } |
Chris@147 | 50 |