function handle = warp(arg1,arg2,arg3,arg4,arg5,arg6)
%WARP	Warp an image onto a surface.
%	WARP(X,MAP) displays the indexed image X with colormap
%	MAP as a texturemap on a simple rectangular surface.
%	WARP(I,N) displays the intensity image I with a gray
%	scale colormap of length N as a texturemapped surface.
%	WARP(R,G,B) displays the RGB image in the matrices R,G,B
%	as a texturemapped surface.
%
%	WARP(Z,X,MAP), WARP(Z,I,N) or WARP(Z,R,G,B) displays 
%	the image on the surface Z.  WARP(x,y,z,...) displays
%	the image on the surface (x,y,z).
%
%	H = WARP(...) returns a handle to a SURFACE object.
%
%	Note: Texture-mapped surfaces render slower than images.
%
%	See also SURF, IMSHOW, IMAGE.

%	Clay M. Thompson 3-5-93
%	Copyright (c) 1993 by The MathWorks, Inc.
%	$Revision: 1.11 $  $Date: 1993/09/10 00:10:43 $

if nargin<1, error('Not enough input arguments.'); end

cm = colormap;
x = []; y = []; z = []; % Defaults
likeimage = 0; user = [];

if nargin==1, % warp(a)
  if isind(arg1),
    A = arg1;
  elseif isgray(arg1),
    A = round(arg1*(size(cm,1)-1))+1; user = [0 1];
  else
    cmin = min(min(arg1)); cmax = max(max(arg1)); user = [cmin cmax];
    A = min(size(cm,1),round((size(cm,1)-1)*(arg1-cmin)/(cmax-cmin))+1);
  end
  likeimage = 1; map = [];

elseif nargin==2, % warp(a,cm) or warp(I,n) or warp(z,a)
  if prod(size(arg2))==1,
    A = round(arg1*(arg2-1))+1;
    map = gray(arg2); user = [0 1];
    likeimage = 1;
  elseif size(arg2,2)==3,
    A = arg1; map = arg2;
    likeimage = 1;
  else
    z = arg1;
    if isind(arg2),
      A = arg2;
    elseif isgray(arg2),
      A = round(arg2*(size(cm,1)-1))+1; user = [0 1];
    else
      cmin = min(min(arg2)); cmax = max(max(arg2)); user = [cmin cmax];
      A = min(size(cm,1),round((size(cm,1)-1)*(arg2-cmin)/(cmax-cmin))+1);
    end
    map = [];
  end

elseif nargin==3, % warp(r,g,b) or warp(z,a,cm) or warp(z,I,n)
  if prod(size(arg3))==1,
    z = arg1;
    A = round(arg2*(arg3-1))+1; user = [0 1];
    map = gray(arg3);
  elseif size(arg3,2)==3,
    z = arg1; A = arg2; map = arg3;
  else
    if any(size(arg1)~=size(arg2)) | any(size(arg1)~=size(arg3)),
      error('The R,G, and B matrices must all be the same size.');
    end
    [A,map] = rgb2ind(arg1,arg2,arg3,.2); % Uniform quantization, max. 216 colors
    likeimage = 1;
  end

elseif nargin==4, % warp(z,r,g,b)
  if any(size(arg2)~=size(arg3)) | any(size(arg2)~=size(arg4)),
    error('The R,G, and B matrices must all be the same size.');
  end
  z = arg1;
  [A,map] = rgb2ind(arg2,arg3,arg4,.2); % Uniform quantization, max. 216 colors
  
elseif nargin==5, % warp(x,y,z,a,cm) or warp(x,y,z,I,n)
  x = arg1; y = arg2; z = arg3;
  if prod(size(arg5))==1,
    A = round(arg4*(arg5-1))+1; map = gray(arg5); user = [0 1];
  else
    A = arg4; map = arg5;
  end
  
elseif nargin==6, % warp(x,y,z,r,g,b)
  x = arg1; y = arg2; z = arg3;
  if any(size(arg4)~=size(arg5)) | any(size(arg4)~=size(arg6)),
    error('The R,G, and B matrices must all be the same size.');
  end
  [A,map] = rgb2ind(arg4,arg5,arg6,.2); % Uniform quantization, max. 216 colors

end

[mA,nA] = size(A);
if isempty(z), z = zeros(mA,nA); x = [1:nA]; y = [1:mA]; end
if isempty(x), [x,y] = meshgrid(1:size(z,2),1:size(z,1)); end
if length(x) == 2 & length(y) == 2
   xstep = (x(2)-x(1))/(mA-1);
   x = x(1):xstep:x(2);
   ystep = (y(2)-y(1))/(nA-1);
   y = (y(1):ystep:y(2))';
   z = interp2([x(1) x(mA)],[y(1) y(nA)],z,x,y);
end

cax = newplot;
h = surface(x,y,z,flipud(A),'EdgeColor','none','FaceColor','texturemap');
if ~isempty(map), colormap(map), end
set(h,'UserData',user)

if ~ishold & likeimage,
  view(2)
  axis([min(min(x)) max(max(x)) min(min(y)) max(max(y))])
else
  view(3)
end

if nargout, handle = h; end
