%kkpad 'Pad Data with a Constant Value'
% This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros kpad.pane file
%
% Parameters: 
% InputFile: i 'Input ', required: 'Input data file'
% Integer: wsize 'Padded Width ', default: 512: 'Size of width dimension after padding'
% Integer: hsize 'Padded Height ', default: 512: 'Size of height dimension after padding'
% Integer: dsize 'Padded Depth ', default: 1: 'Size of depth dimension after padding'
% Integer: tsize 'Padded Time Size ', default: 1: 'Size of time dimension after padding'
% Integer: esize 'Padded Element Size', default: 1: 'Size of element dimension after padding'
% Integer: woff 'Width Offset ', default: 0: 'Offset of original object in width dimension'
% Integer: hoff 'Height Offset ', default: 0: 'Offset of original object in height dimension'
% Integer: doff 'Depth Offset ', default: 0: 'Offset of original object in depth dimension'
% Integer: toff 'Time Offset ', default: 0: 'Offset of original object in time dimension'
% Integer: eoff 'Element Offset ', default: 0: 'Offset of original object in elements dimension'
% Toggle: wrap 'Fill Outside Data Regions by WRAPPING', default: 0: 'fill outside data regions by WRAPPING'
% OutputFile: o 'Output', required: 'Resulting output file'
%
% Example: o = kkpad(i, {'i','';'wsize',512;'hsize',512;'dsize',1;'tsize',1;'esize',1;'woff',0;'hoff',0;'doff',0;'toff',0;'eoff',0;'wrap',0;'o',''})
%
% Khoros helpfile follows below:
%
%  PROGRAM
% kpad - Pad Data with a Constant Value
%
%  DESCRIPTION
% "Pad Object" (kpad) performs the padding of a data object using the pad 
% values defined by the Real and Imaginary padding value options.  
% 
%  "Size"
% The output data object will be the size specified by the final
% padded size parameters, Padded Width, Padded Height, etc (wsize, 
% hsize, dsize, tsize, esize).  
% 
%  "Offset Values"
% The offset parameters (woff, hoff, doff, toff, eoff) define where the 
% origin of the source object will lie with respect to the output object.  
% Specifying an offset greater than zero can be used to generate a border 
% around the original data.  Negative offset values will result in 
% truncation of data lying in the region (0-woff, 0-hoff, 0-doff, 0-toff, 
% 0-eoff).  For example, if an offset of (-1,-1,0,0,0) is specified, the 
% object will effectively be shifted by -1 in the width and height dimensions, 
% and that data truncated in the destination object.  If the offset plus 
% the original data size is greater than the specified destination size, 
% truncation of the source data will occur.
% If the input object is larger than the final padded size specified,
% the output data object will be truncated to the specified size.  
% 
%  "Pad Values"
% The Real Pad Value (real) specifies the pad value that will be assigned 
% to the real component of a complex pair, and the Imaginary Pad Value 
% (imag) specifies the imaginary component of the pair.
% If the input data object type is not complex, the imaginary pad value 
% will be ignored.  For input data objects that are not of type double, 
% the specified pad values are cast to the same type as the input data object.  
% 
%  "Wrapping"
% The regions outside the original data bounds can be filled also by wrapping 
% data from the opposite side of the dataset by specifying the Wrap flag. 
% This will override the Pad Value functionality.
% 
%  "Mask Data & Validity Option"
% The validity mask corresponding to padded data will be zero if 
% the user specifies that padded data added by this program be marked
% as invalid (valid FALSE).  Likewise, if the user specifies that padded 
% data is valid (valid TRUE), the mask corresponding to padded data will 
% be non-zero.  In the first case (invalid), if no mask exists in the 
% source object, on will be created.  In the valid case, no mask will be
% created.  Validity mask data corresponding to the original source object 
% data is transferred to the destination unaltered.
% 
%  "Location & Time Data"
% If curvilinear or rectilinear time or location data
% exist, it is unclear how to alter the dimensionality,
% so the pad operation will fail.  If uniform location data exist
% padding occurs, but the location information is not altered in
% any way.
% 
%  "Map Data"
% The pad operator does not modify map data.
% 
%  "Failure Modes"
% The input object must contain value or mask data.
%
%  
%
%  EXAMPLES
% 
% kpad -i input_file -o output_file- Pad Data with a Constant Value
% Will create a zero padded output object, using the default
% size and zero offset.  
% 
% kpad -i input_file -o output_file -wsize 512 -hsize 256 -real 200 -imag 100- Pad Data with a Constant Value
% This padding operation will produce an output object with a
% width of 512 and height of 256.  If the input data is complex,
% padding for the real component will be 200, and 100 for the
% imaginary component.   If the data is not complex, the imaginary
% value is ignored.
%
%  "SEE ALSO"
%
%  RESTRICTIONS 
% kpad will fail if the source object contains curvilinear or rectilinear
% location or time data.  If uniform time or location data exist, it is
% transferred, but not modified.
%
%  REFERENCES 
%
%  COPYRIGHT
% Copyright (C) 1993 - 1997, Khoral Research, Inc. ("KRI")  All rights reserved.
% 


function varargout = kkpad(varargin)
if nargin ==0
  Inputs={};arglist={'',''};
elseif nargin ==1
  Inputs=varargin{1};arglist={'',''};
elseif nargin ==2
  Inputs=varargin{1}; arglist=varargin{2};
else error('Usage: [out1,..] = kkpad(Inputs,arglist).');
end
if size(arglist,2)~=2
  error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
 end
narglist={'i', '__input';'wsize', 512;'hsize', 512;'dsize', 1;'tsize', 1;'esize', 1;'woff', 0;'hoff', 0;'doff', 0;'toff', 0;'eoff', 0;'wrap', 0;'o', '__output'};
maxval={0,2,2,2,2,2,0,0,0,0,0,0,0};
minval={0,2,2,2,2,2,0,0,0,0,0,0,0};
istoggle=[0,1,1,1,1,1,1,1,1,1,1,1,0];
was_set=istoggle * 0;
paramtype={'InputFile','Integer','Integer','Integer','Integer','Integer','Integer','Integer','Integer','Integer','Integer','Toggle','OutputFile'};
% identify the input arrays and assign them to the arguments as stated by the user
if ~iscell(Inputs)
Inputs = {Inputs};
end
NumReqOutputs=1; nextinput=1; nextoutput=1;
  for ii=1:size(arglist,1)
  wasmatched=0;
  for jj=1:size(narglist,1)
   if strcmp(arglist{ii,1},narglist{jj,1})  % a given argument was matched to the possible arguments
     wasmatched = 1;
     was_set(jj) = 1;
     if strcmp(narglist{jj,2}, '__input')
      if (nextinput > length(Inputs)) 
        error(['Input ' narglist{jj,1} ' has no corresponding input!']); 
      end
      narglist{jj,2} = 'OK_in';
      nextinput = nextinput + 1;
     elseif strcmp(narglist{jj,2}, '__output')
      if (nextoutput > nargout) 
        error(['Output nr. ' narglist{jj,1} ' is not present in the assignment list of outputs !']); 
      end
      if (isempty(arglist{ii,2}))
        narglist{jj,2} = 'OK_out';
      else
        narglist{jj,2} = arglist{ii,2};
      end

      nextoutput = nextoutput + 1;
      if (minval{jj} == 0)  
         NumReqOutputs = NumReqOutputs - 1;
      end
     elseif isstr(arglist{ii,2})
      narglist{jj,2} = arglist{ii,2};
     else
        if strcmp(paramtype{jj}, 'Integer') & (round(arglist{ii,2}) ~= arglist{ii,2})
            error(['Argument ' arglist{ii,1} ' is of integer type but non-integer number ' arglist{ii,2} ' was supplied']);
        end
        if (minval{jj} ~= 0 | maxval{jj} ~= 0)
          if (minval{jj} == 1 & maxval{jj} == 1 & arglist{ii,2} < 0)
            error(['Argument ' arglist{ii,1} ' must be bigger or equal to zero!']);
          elseif (minval{jj} == -1 & maxval{jj} == -1 & arglist{ii,2} > 0)
            error(['Argument ' arglist{ii,1} ' must be smaller or equal to zero!']);
          elseif (minval{jj} == 2 & maxval{jj} == 2 & arglist{ii,2} <= 0)
            error(['Argument ' arglist{ii,1} ' must be bigger than zero!']);
          elseif (minval{jj} == -2 & maxval{jj} == -2 & arglist{ii,2} >= 0)
            error(['Argument ' arglist{ii,1} ' must be smaller than zero!']);
          elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} < minval{jj})
            error(['Argument ' arglist{ii,1} ' must be bigger than ' num2str(minval{jj})]);
          elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} > maxval{jj})
            error(['Argument ' arglist{ii,1} ' must be smaller than ' num2str(maxval{jj})]);
          end
        end
     end
     if ~strcmp(narglist{jj,2},'OK_out') &  ~strcmp(narglist{jj,2},'OK_in') 
       narglist{jj,2} = arglist{ii,2};
     end
   end
   end
   if (wasmatched == 0 & ~strcmp(arglist{ii,1},''))
        error(['Argument ' arglist{ii,1} ' is not a valid argument for this function']);
   end
end
% match the remaining inputs/outputs to the unused arguments and test for missing required inputs
 for jj=1:size(narglist,1)
     if  strcmp(paramtype{jj}, 'Toggle')
        if (narglist{jj,2} ==0)
          narglist{jj,1} = ''; 
        end;
        narglist{jj,2} = ''; 
     end;
     if  ~strcmp(narglist{jj,2},'__input') && ~strcmp(narglist{jj,2},'__output') && istoggle(jj) && ~ was_set(jj)
          narglist{jj,1} = ''; 
          narglist{jj,2} = ''; 
     end;
     if strcmp(narglist{jj,2}, '__input')
      if (minval{jj} == 0)  % meaning this input is required
        if (nextinput > size(Inputs)) 
           error(['Required input ' narglist{jj,1} ' has no corresponding input in the list!']); 
        else
          narglist{jj,2} = 'OK_in';
          nextinput = nextinput + 1;
        end
      else  % this is an optional input
        if (nextinput <= length(Inputs)) 
          narglist{jj,2} = 'OK_in';
          nextinput = nextinput + 1;
        else 
          narglist{jj,1} = '';
          narglist{jj,2} = '';
        end;
      end;
     else 
     if strcmp(narglist{jj,2}, '__output')
      if (minval{jj} == 0) % this is a required output
        if (nextoutput > nargout & nargout > 1) 
           error(['Required output ' narglist{jj,1} ' is not stated in the assignment list!']); 
        else
          narglist{jj,2} = 'OK_out';
          nextoutput = nextoutput + 1;
          NumReqOutputs = NumReqOutputs-1;
        end
      else % this is an optional output
        if (nargout - nextoutput >= NumReqOutputs) 
          narglist{jj,2} = 'OK_out';
          nextoutput = nextoutput + 1;
        else 
          narglist{jj,1} = '';
          narglist{jj,2} = '';
        end;
      end
     end
  end
end
if nargout
   varargout = cell(1,nargout);
else
  varargout = cell(1,1);
end
global KhorosRoot
if exist('KhorosRoot') && ~isempty(KhorosRoot)
w=['"' KhorosRoot];
else
if ispc
  w='"C:\Program Files\dip\khorosBin\';
else
[s,w] = system('which cantata');
w=['"' w(1:end-8)];
end
end
[varargout{:}]=callKhoros([w 'kpad"  '],Inputs,narglist);
