function [y,top,bot,lft,rgt]=symmext(x,top,bot,lft,rgt)
%SYMMEXT
%	Symmetric extension, [y,top,bot,lft,rgt]=symmext(x,top,bot,lft,rgt).
%	y=symmext(x): The input image, x, is symmetric extended to the next 
%	power-2 size above its current size.
%	y=symmext(x,top,bot,lft,rgt): The input image, x, is symmetric
%	extended by the specified borders.


[M,N]=size(x);

if nargin==1,
  m=floor(log(M)/log(2));
  n=floor(log(N)/log(2));
  
  deltaM=2^(m+1) - M;
  deltaN=2^(n+1) - N;

  top=floor(deltaM/2);
  bot=ceil(deltaM/2);
  lft=floor(deltaN/2);
  rgt=ceil(deltaN/2);
elseif nargin~=5,
  error('Illegal number of parameters');
end

y=x;
y=[y(top:-1:1,:);y;y(M:-1:M-bot+1,:)];
y=[y(:,lft:-1:1) y y(:,N:-1:N-rgt+1)];
