
clear

%fname = 'gray/cameraman.tif';
fname = 'lena_std_gray.tif';
fmt = 'tiff';

[RAW, MAP, ALPHA] = imread(fname,fmt);

orig = double(RAW);
A = orig;


for N = [512 256 128 64 32]
  B = A(1:N,1:N);
  
  for j=1:2
    tic
    for m = 1:N
      templ = haarlow(B(m,:));
      temph = haarhigh(B(m,:))+128;
      B(m,1:(N/2)) = downsample(templ,2);
      B(m,(N/2+1):N) = downsample(temph,2);
    end
    toc
    B = transpose(B);
  end
  A(1:N,1:N) = B;
  
  figure(1);
  clf;
  image(A);
  pause(0);
  
  imwrite(uint8(A),['lena_haar_' num2str(N/2) '.bmp'],'bmp')
end
