% Project 1
% Andy Doran
%
% This programs quantizes and plots "in" to "X" bits
% assuming uniform distribution

function [Lmax,Lmin,out] = quant4(in, bits)

% Get size of input image
[x y] = size(in);
pix = x*y;

% Get L quantization levels

L = 2^bits;

% Find endpoints of distribution
Lmin = floor(min(min(in))) - 1;
Lmax = floor(max(max(in))) + 1;

% Make sure Lmax isn't too big
if (Lmax == 128);
  Lmax = 127;
end

% Make sure Lmin isn't too big
if (Lmin == -128);
  Lmin = -127;
end

% size(tk) = L+1, with points evenly spaced between endpoints
tk = linspace(Lmin,Lmax,L+1);

% put 'in' into a column vector, counting off by rows
in = in';
in = in(:);

for i = 1:L
  % Find indices where "in" is between quantization levels
  index = find((in>tk(i)) & (in<=tk(i+1)));
  out(index) = i-1;
end


out=reshape(out,x,y)';
