Chris@147: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@147: 
Chris@147: /*
Chris@147:     Sonic Visualiser
Chris@147:     An audio file viewer and annotation editor.
Chris@147:     Centre for Digital Music, Queen Mary, University of London.
Chris@147:     This file copyright 2006 Chris Cannam.
Chris@147:     
Chris@147:     This program is free software; you can redistribute it and/or
Chris@147:     modify it under the terms of the GNU General Public License as
Chris@147:     published by the Free Software Foundation; either version 2 of the
Chris@147:     License, or (at your option) any later version.  See the file
Chris@147:     COPYING included with this distribution for more information.
Chris@147: */
Chris@147: 
Chris@147: #include "PowerOfTwoZoomConstraint.h"
Chris@147: 
Chris@147: size_t
Chris@147: PowerOfTwoZoomConstraint::getNearestBlockSize(size_t req,
Chris@147: 					      RoundingDirection dir) const
Chris@147: {
Chris@147:     size_t result = 0;
Chris@147: 
Chris@147:     for (size_t bs = 1; ; bs *= 2) {
Chris@147: 	if (bs >= req) {
Chris@147: 	    if (dir == RoundNearest) {
Chris@147: 		if (bs - req < req - bs/2) {
Chris@147: 		    result = bs;
Chris@147: 		    break;
Chris@147: 		} else {
Chris@147: 		    result = bs/2;
Chris@147: 		    break;
Chris@147: 		}
Chris@147: 	    } else if (dir == RoundDown) {
Chris@147: 		result = bs/2;
Chris@147: 		break;
Chris@147: 	    } else {
Chris@147: 		result = bs;
Chris@147: 		break;
Chris@147: 	    }
Chris@147: 	}
Chris@147:     }
Chris@147: 
Chris@147:     if (result > getMaxZoomLevel()) result = getMaxZoomLevel();
Chris@147:     return result;
Chris@147: }
Chris@147: