function [epyr, recImage] = errorPyramid(image, levels, open, method, param1)

% recursive error pyramid generation 
% pyramid coder framework
% returns an error pyramid and the reconstructed image

% levels is the number of levels to be computed
% open = 'o' is selecting the open pyramid coder. (otherwise closed)
% method is a string specifying the method to use
% param1 is an optional parameter for the method selected 

% AVAILABLE METHODS
%  'haar' Haar linear filter
%  'burt' Burt&Adelson linear pyramid, takes a filter kernel W
%  'opt_lin' Optimal Linear pyramid takes a kernel size
%  'opt_cub'  "      Cubic     "
%  'spline' B-spline pyramid, takes a B-spline order
%  'ideal' ideal low-pass filter pyramid
%  'opt_NL' approx optimal non-linear, takes a coefficient table NLcoeffs
%  'multi' hybrid method, takes a cell -> best method table

% no quantization, only a rounding of the error images

disp(sprintf('error pyramid called, level %d, method %s', levels, method));

if(nargin <= 4)
   param1 = '';
end


if (levels == 0) 
   epyr = addLevel(image);
   recImage = image;
else
   sImage = reduceFunc(image, method, param1);
   
   [sepyr, srecImage] = errorPyramid(sImage, levels-1, open, method, param1);
   
   if(open == 'o')
      recImage = expandFunc(srecImage, method, param1);
   else
      recImage = expandFunc(sImage, method, param1);
   end
   
   errorImage = image-recImage;
   %round(errorImage);
   
   recImage = recImage + errorImage;
   
   epyr = addLevel(errorImage, sepyr);
end




