svcore  1.9
PowerOfTwoZoomConstraint.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2006 Chris Cannam.
00008     
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "PowerOfTwoZoomConstraint.h"
00017 
00018 int
00019 PowerOfTwoZoomConstraint::getNearestBlockSize(int req,
00020                                               RoundingDirection dir) const
00021 {
00022     int result = 0;
00023 
00024     for (int bs = 1; ; bs *= 2) {
00025         if (bs >= req) {
00026             if (dir == RoundNearest) {
00027                 if (bs - req < req - bs/2) {
00028                     result = bs;
00029                     break;
00030                 } else {
00031                     result = bs/2;
00032                     break;
00033                 }
00034             } else if (dir == RoundDown) {
00035                 result = bs/2;
00036                 break;
00037             } else {
00038                 result = bs;
00039                 break;
00040             }
00041         }
00042     }
00043 
00044     if (result > getMaxZoomLevel()) result = getMaxZoomLevel();
00045     return result;
00046 }
00047