%PARAMTYPE   Parameter-type-specific code for DIPIMAGE and GETPARAMS.
%   The first argument is a command, and the second one is a PARAM
%   structure containing:
%      PARAM.NAME         String containing the variable's name.
%      PARAM.DESCRIPTION  String containing a description to show the user.
%      PARAM.TYPE         String containing the data type.
%      PARAM.DIM_CHECK    Dimensionality check.
%      PARAM.RANGE_CHECK  Range check.
%      PARAM.REQUIRED     Boolean value, whether this is an optional or
%                         required argument.
%      PARAM.DEFAULT      Default value used if none specified.
%   All these entries can be interpreted differently for the various
%   parameter types.
%
%   HANDLES = PARAMTYPE('control_create',PARAM,FIG,INDEX)
%      Create a UI control in DIPimage GUI, return the list of handles.
%      The first handle is the edit control, and if there's another
%      handle it'll be for a push button to the right.
%      FIG is the figure handle. INDEX is the control's index number.
%
%   [VALUE,STRING] = PARAMTYPE('control_value',PARAM,CONTROL)
%      Evaluate the input in a UI control in DIPimage GUI.
%      CONTROL is the handle of the control containing the values.
%
%   VALUE = PARAMTYPE('default_value',PARAM)
%      Returns the default value.
%
%   MSG = PARAMTYPE('definition_test',PARAM,PARAMS)
%      Test the PARAM structure entry, returns an error message or ''.
%
%   [MSG,VALUE] = PARAMTYPE('value_test',PARAM,VALUE,OUTARGS)
%   MSG = PARAMTYPE('value_test',PARAM,VALUE)
%      [Second syntax only for '?' input argument parsing, to be removed.]
%      Test the value against the specification in the PARAM structure.
%      OUTARGS is a cell array with all the arguments, in case this
%      argument needs to be formatted to fit another argument.
%      Warning: assumes the PARAM struct is correct.
%
%   The actual code is in sub-functions called 'PARAMTYPE_xxx.M'. To
%   add a parameter type, just copy one of these over, change the name
%   and modify the code to do what you need it to do. It will be
%   automatically registered. However, for the new type to work with
%   the MATLAB compiler, you need to add it to the #function pragma
%   in this file.
%   If the new type becomes part of DIPimage, please edit the help in
%   GETPARAMS and the User Guide.

% (C) Copyright 1999-2009               Pattern Recognition Group
%     All rights reserved               Faculty of Applied Physics
%                                       Delft University of Technology
%                                       Lorentzweg 1
%                                       2628 CJ Delft
%                                       The Netherlands
%
% Cris Luengo, 16, 18 September 2007.
% Built with code extracted from DIPIMAGE.M and GETPARAMS.M
% 17 December 2009: Using new function MATLABVER_GE.

function varargout = paramtype(command,param,varargin)

%#function paramtype_anytypearray
%#function paramtype_array
%#function paramtype_boolean
%#function paramtype_cellarray
%#function paramtype_dataset
%#function paramtype_handle
%#function paramtype_image
%#function paramtype_indir
%#function paramtype_infile
%#function paramtype_measureid
%#function paramtype_measurement
%#function paramtype_option
%#function paramtype_optionarray
%#function paramtype_outfile
%#function paramtype_string

% Compile registry, only the first time this function is called.
persistent registry;
if ~iscell(registry)
   % The first time this function is called, registry==[].
   if matlabver_ge([6,5])
      p = fileparts(mfilename('fullpath'));
   else
      p = fileparts(which(mfilename));
   end
   p = dir(fullfile(p,'paramtype_*.m'));
   registry = {p.name};
   for ii=1:length(registry)
      registry{ii} = registry{ii}(11:end-2); % remove the 'paramtype_' and '.m' parts.
   end
end

% Call the relevant sub-function
if ~any(strcmp(param.type,registry))
   error('Unknown parameter type');
end
varargout = cell(1,nargout);
[varargout{:}] = feval(['paramtype_',param.type],command,param,varargin{:});
