%kkshad 'Compute the object surface shading giving a 3D cue '
% This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros kshad.pane file
%
% Parameters: 
% InputFile: i1 'Z-buffer', required: 'Distance between view plane and object surface'
% OutputFile: o 'Shading', required: 'Shading information using Phong s model'
% Integer: background 'background', default: 255: 'Image background color (gray level)'
% Integer: Imin ' minimum ', default: 100: 'Minimum intensity of light that reaches the object surface '
% Integer: Imax ' maximum ', default: 244: 'Maximum intensity of light that reaches the object surface '
% InputFile: i2 'Normal Vectors', optional: 'Surface normal vectors'
% Double: Ka 'ambient ', default: 20: 'Coeficient of reflection for the ambient light'
% Double: Kd 'difuse ', default: 80: 'Coeficient of reflection for the difuse light'
% Double: Ks 'specular', default: 20: 'Coficient of reflection for the specular light'
% Integer: n 'n factor', default: 7: 'Falling factor for the specular light'
% Double: alpha 'alpha', default: 0: 'View plane rotation angle around Z axis'
% Double: beta 'beta ', default: 0: 'View plane rotation angle around X axis'
% InputFile: i3 '"Texture"', optional: '"Texture" information to be incorporated on shading'
% Double: Kt '"texture"', default: 0: 'Coficient of texture influence'
%
% Example: o = kkshad({i1, i2, i3}, {'i1','';'o','';'background',255;'Imin',100;'Imax',244;'i2','';'Ka',20;'Kd',80;'Ks',20;'n',7;'alpha',0;'beta',0;'i3','';'Kt',0})
%
% Khoros helpfile follows below:
%
%  PROGRAM
% kshad - Compute shading
%
%  DESCRIPTION
% Create a shaded image file based on z-buffer and/or normal vectors.
% 
% The operator "kshad"
% creates a shaded image file of type KUBYTE from the z-buffer of a volume and 
% optionally, also from the normal vectors of the volume.
% 
% There are two 
% shading possibilities: depth shading and gradient shading. The depth shading
% uses only the z-buffer information from the input file. The gradient shading 
% uses the z-buffer and the angles between the normals and the light source 
% direction (given by angles alpha and beta) to create the shaded image.  
% 
% The normal vectors can be computed in three different ways: in the image
% space, in the object space and in the voxel space. Each one will possibly
% give a different shaded image. These vectors must have their directions 
% inverted (pointing towards the interior of the object).
% 
% The input parameters Imax, Imin, Ka, Kd, Ks, and n, are chosen to 
% adjust the gradient shading quality according to the Phong equation (see
% equation below).
% However, the depth shading only needs the Imax and Imin parameters. The 
% image background color (gray level) also can be chosen by the user. If the
% texture information is to be added, then the texture file as well as the
% texture coeficient Kt must be given.
% 
% Angles alpha and beta estabilish rotations of the light source around z and x
% axes respectively (see figure in man page of kzbuff), and they are 
% recommended to be 
% the same used to generate the z-buffer image (so that observer and light 
% source have the same position), avoiding problems with shadowing, not 
% implemented. Different values of alpha and beta can be used without 
% restrictions when there is only one object and its visible surface is
% convex. The rotations are made firstly around x and then around z.
% 
% Depth Shading Intensity (Id):
% Id(u,v) = [(Imax-Imin)/(dmin-dmax)] * [d(u,v)-dmax] + Imin
% where 
% d corresponds to the distance in the z-buffer image
% 
% Gradient Shading Intensity (Ig):
% Ig(u,v) = Imax*Ka + Id(u,v)*{kd*cos[theta(u,v)] + Ks*[cos(2*theta(u,v))^n] +
% Kt*T(u,v)}
% where 
% theta is the angle between the surface nomal and the ray of light;
% Ka is the coeficient of reflection for the ambient light;
% Kd is the coeficient of diffuse reflection;
% Ks is the coeficient of specular reflection;
% Kt is the coeficient of texture;
% T is the optional texture information;
%
%  
%
%  EXAMPLES
% kshad -i1 zbuffer.viff -i2 normals.viff -o image.viff -Ka 30 -Kd 80 -Ks 20 
%       -n 7 -Imax 220 -Imin 100 -background 255 -alpha 0 -beta 0
% 
% The program will create from the zbuffer.viff and normals.viff files an image 
% represented by the 
% image.viff file using the gradient shading. The gradient 
% shading will use 30% (Ka) of reflection coeficient for the ambient light, 
% 80% (Kd) of difuse reflection coeficient and 20% (Ks) of specular reflection 
% coeficient for the light source. The maximum (Imax) and minimum (Imin) 
% intensity of the light source reflected on the object surface will be 
% respectively 220 and 100. The falling factor (n) for the specular reflection 
% light component will be 7 and the image background color (gray level) will 
% be 255. The zbuffer.viff image was generated by a view plane rotated zero 
% degree around x axis and zero degree around z axis, therefore the position 
% of the light source is given by setting angles alpha and beta to zero.
%
%  "SEE ALSO"
% kzbuff, kisnorm, kvsnorm, ktextu, kvoxext
%
%  RESTRICTIONS 
% The input objects must have only the value segment. 
% 
% The z-buffer and texture
% input objects can not have dimention e > 1. The input object normal must have
% dimention e=3.
% 
% The input objects must match in their dimentions w and h.
% 
% The input objects can not be of data types KBIT and KCOMPLEX.
% 
% In case of t > 1 in the input objects, the operator will be applied to the time
% t=0 only.
% 
% None of the input and output objects are referenced, therefore some attributes
% may change, as the VALUE_POSITION, for example.
%
%  REFERENCES 
%
%  COPYRIGHT
% Copyright (C) 1993, 1994, 1995 UNICAMP, R A Lotufo,  All rights reserved.
% 


function varargout = kkshad(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,..] = kkshad(Inputs,arglist).');
end
if size(arglist,2)~=2
  error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
 end
narglist={'i1', '__input';'o', '__output';'background', 255;'Imin', 100;'Imax', 244;'i2', '__input';'Ka', 20;'Kd', 80;'Ks', 20;'n', 7;'alpha', 0;'beta', 0;'i3', '__input';'Kt', 0};
maxval={0,0,255,255,255,1,100,100,100,200,0,0,1,100};
minval={0,0,0,0,0,1,0,0,0,0,0,0,1,0};
istoggle=[0,0,1,1,1,1,1,1,1,1,1,1,1,1];
was_set=istoggle * 0;
paramtype={'InputFile','OutputFile','Integer','Integer','Integer','InputFile','Double','Double','Double','Integer','Double','Double','InputFile','Double'};
% 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 'kshad"  '],Inputs,narglist);
