
% crude example of image compression using averages over boxes

function coeff=image2coeff(file)
   tic
   close all
   clear coeff
   imshow(file)
   % put image into a square array whose side 
   % length is a power of 2
   [L,K]=size(file);
   N=max(ceil(log2(L)),ceil(log2(K)));
   % make arrays to hold image
   % if input is not a square array with a 
   % power of two side, image will be larger
   image=zeros(2^N);
   image(1:L,1:K)=file;
   % make array to hold coefficients
   coeff=zeros(1,(4^(N+1)-1)/3);
   count=1;
   % k enumerates layers 
   for k=0:N
      M=2^(N-k);
      num=M^2;
      % j and k enumerate boxes in a single layer
      for j=0:(2^k)-1
	 % a,b give horizontal side of one box
	 a=1+j*M;
	 b=a+M-1;
	 for m=0:(2^k)-1
            % c,d give vertical side of one box
	    c=1+m*M;
	    d=c+M-1;
	    % take average over the box
	    block_ave=sum(sum(image(a:b,c:d)))/num;
	    coeff(count)=block_ave;
	    % subtract average from image
	    image(a:b,c:d) =image(a:b,c:d) -block_ave*ones(M);
	    % increment number of cofficients created
	    count=count+1;
	 end % for m
      end % for j
      [k,toc]
   end % for k
return
