%kkimportraw 'Low level import of raw (binary) data'
% This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros kimportraw.pane file
%
% Parameters: 
% InputFile: i1 'Raw (binary) input', required: 'raw (binary) input'
% InputFile: i2 'Add-to object', optional: 'Object to which the new data should be added'
% Integer: wsize 'Width ', default: 1: 'Size of width dimension'
% Integer: hsize 'Height ', default: 1: 'Size of height dimension'
% Integer: dsize 'Depth ', default: 1: 'Size of depth dimension'
% Integer: tsize 'Time ', default: 1: 'Size of time dimension'
% Integer: esize 'Elements ', default: 1: 'Size of elements dimension'
% Integer: skip 'Offset or skip (bytes)', default: 0: 'skip this number of bytes before reading'
% OutputFile: o 'Output object', required: 'output object'
%
% Example: o = kkimportraw({i1, i2}, {'i1','';'i2','';'wsize',1;'hsize',1;'dsize',1;'tsize',1;'esize',1;'skip',0;'o',''})
%
% Khoros helpfile follows below:
%
%  PROGRAM
% kimportraw - Low level import of raw (binary) data
%
%  DESCRIPTION
% .I kimportraw
% can be used to read raw binary data and import it to a data segment.  If 
% the second input file is supplied, "Add-to Object", the new data
% will be appended onto that object.  Otherwise, an new object is created.
% The data is stored in the "Destination segment" specified.
% 
% The source architecture, data type information, and data dimensions and
% structure  must be known to permit the data to be imported correctly.
% 
% The -skip argument can be used to cause the specified number of bytes to be
% skipped before reading the data that will eventually end up in the output
% object. This is handy for skipping over a header or other information in
% a raw data file to get to the actual data.
% 
% The index order of the data is assumed to be WHDTE.
% 
% Access to the raw input is done using a special input mode (obtained
% by setting the KOBJ_RAW flag when the input file is opened) that forces
% the data to be interpreted in raw mode. This means that even if the
% data is actually in one of the supported formats, that fact will be
% ignored. This potentially lets you read the headers and other goodies
% and store them as data in the output object.
% 
% If the destination of the raw data is specified to be the comment attribute
% of the output object, the raw data will be accessed as KUBYTE regardless of
% the setting of the -type argument. The number of characters given by the
% product of the sizes of the dimensions will be read in and appended to the
% comment attribute in the output object. Only those characters up to
% the first NULL in the data will actually end up in the comment
% since the comment is required to be a NULL terminated string.
%
%  
%
%  EXAMPLES
% The following example shows how one might read a raw data file generated by a
% frame grabber. Here, we will assume that the data is a 512 col by 480 row
% unsigned byte color (RGB) image, with a 1024 byte header. We will assume that
% the color data is stored with the whole R plane, then the whole G plane, then
% the whole B plane.
% .DS
%   % kimportraw -o khoros.rgb -i1 framegrab.rgb -type 3 -wsize 512 -hsize 480 -esize 3 -segment 1 -skip 1024 -color RGB
% .DE
%
%  "SEE ALSO"
% kimportasc(1), kformats(1), kcomment(1), kcolorspace(1)
%
%  RESTRICTIONS 
% The data is assumed to be stored in the raw file with a WHDTE index order.
% It would be nice to be able to directly handle mulitply interleaved data,
% but specifiying how that is to be done in 5D is a mess.
%
%  REFERENCES 
%
%  COPYRIGHT
% Copyright (C) 1993 - 1997, Khoral Research, Inc. ("KRI")  All rights reserved.
% 


function varargout = kkimportraw(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,..] = kkimportraw(Inputs,arglist).');
end
if size(arglist,2)~=2
  error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
 end
narglist={'i1', '__input';'i2', '__input';'wsize', 1;'hsize', 1;'dsize', 1;'tsize', 1;'esize', 1;'skip', 0;'o', '__output'};
maxval={0,1,2,2,2,2,2,1,0};
minval={0,1,2,2,2,2,2,1,0};
istoggle=[0,1,1,1,1,1,1,1,0];
was_set=istoggle * 0;
paramtype={'InputFile','InputFile','Integer','Integer','Integer','Integer','Integer','Integer','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 'kimportraw"  '],Inputs,narglist);
