% Modify (lower) the bit depth of an image.
% 
% Arguments:
% A    matrix representing a monochrome image
% B    desired bit depth: 2^B graylevels in the image
% Samuli Siltanen November 2014

function result = MyBitDepth(A,B)

% Convert A into double
A = double(A);

% Normalize the image between zero and one
A = A-min(min(A(:)));
A = A/max(A(:));

% Spread the image range to full bit depth domain
A = A*(2^B-1);

% Discretize the image
A = round(A);

% Normalize the image between zero and one
result = A/max(A(:));

