function y = grayscale( x, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  function y = grayscale( x[, bpp][, centered])
%
%      scales an input to the typical gray levels [0,255]
%
%      BIT_RANGE is used to define the scaling range according to 2^bit_range
%      CENTERED is used to define if the values are scaled round zero
%

%
% Copyright © 15-Oct-2007 George P. Pavlidis® (sepdek@sepdek.net, gpavlid@ceti.gr)
%
	if nargin < 2
		bit_range = 8;
		centered = false;
	elseif nargin == 2
		if islogical( varargin{ 1})
			bit_range = 8;
			centered = varargin{ 1};
		else
			bit_range = varargin{ 1};
			centered = false;
		end;
	else
		if islogical( varargin{ 1})
			centered = varargin{ 1};
			bit_range = varargin{ 2};
		else
			bit_range = varargin{ 1};
			centered = varargin{ 2};
		end;
	end;

	if centered,
		y = scale( x, [-2^(bit_range-1) 2^(bit_range-1)-1]);
	else
		y = scale( x, [0 2^bit_range-1]);
	end;
	