function y = mfilter2(x,d,FUN,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10)
%MFILTER2 Masked filter.
%	J = MFILTER2(H,I,BW) filters the data in I with the 2-D filter H.
%	The result J will contain the filtered values of I where the 
%	values of the binary mask BW are non-zero and will contain the
%	unfiltered values of I everywhere else.  The mask BW must be the
%	same size as I and contain only 1's and 0's.  FILTER2(H,I) is 
%	used to perform the filtering.
%
%	J = MFILTER2(I,BW,'fun') filters the data in I using the filter
%	function 'fun'.  'fun' should take x as a single argument and
%	return a matrix of the same size.  y = fun(x).  Up to 10
%	additional parameters can be passed to the function using the 
%	syntax, J = MFILTER2(I,BW,'fun',P1,P2,P3,...) in which case
%	'fun' is called using fun(x,P1,P2,P3,...).
%
%	See also FILTER2.

%	Clay M. Thompson 2-10-93
%	Copyright (c) 1993 by The MathWorks, Inc.
%	$Revision: 1.6 $  $Date: 1993/09/09 22:18:00 $

if nargin==3,
  if isstr(FUN),
    if ~any(FUN<48), fcall = [FUN,'(x)']; else fcall = FUN; end
  else
    b = x; x = d; d = FUN;
    fcall = 'filter2(b,x)';
  end
else
  % Form call string.
  params = [];
  for n=4:nargin
    params = [params,',P',int2str(n-3)];
  end
  if ~any(FUN<48), fcall = [FUN,'(x',params,')']; else fcall = FUN; end
end
c = eval(fcall);
if any(size(c)~=size(x)), 
  error('''fun'' must return a result that is the same size as X.');
end
y = zeros(size(x));
y(d) = c(d);
y(~d) = x(~d);


