

%  recovers image from stored averages over boxes
%  the length of coeff should be a sum of powers of 
%  4, i.e, 1+4+16+...+4^N = (4^(N+1)-1)/(4-1)

function  newimage=coeff2image(coeff,r)
   tic
   %close all
   L=length(coeff)
   % compute number of layers of boxes
   N=ceil(log2(3*L+1)/2)-1
   count=1;
   % count number of coefficients used
   used=0;
   newimage=zeros(2^N);
   % k denotes the layer 
   for k=0:N
      M=2^(N-k);
      % j and l enumerate boxes in each layer
      for j=0:(2^k)-1
	 % a and b give horizontal box edge
	 a=1+j*M;
	 b=a+M-1;
	 for m=0:(2^k)-1
            if abs(coeff(count))>r
	       % c, d give vertical box edge
	       c=1+m*M;
	       d=c+M-1;
	       newimage(a:b,c:d) =newimage(a:b,c:d) +coeff(count)*ones(M);
	       used=used+1;
            end % if
	    count=count+1;
	 end % for m
      end % for j
      % show the image created from the coefficients
    %  figure
    %  imshow(uint8(newimage))
      coeffs_used(k+1)=used;
      coeffs(k+1)=count-1;
      percent(k+1)= 100*coeffs_used(k+1)/coeffs(k+1);
      [k,toc]
      %input('Hit return to proceed');
   end % for k
     figure
     imshow(uint8(newimage))

   % print number and percent of coefficients used
   L=length(coeff);
   [[0:N]',coeffs',coeffs_used']

return
