function hout = subimage(arg1,arg2,arg3,arg4,arg5)
%SUBIMAGE Display multiple images.
%	SUBIMAGE is used to create figures with multiple images.  It
%	works by appending the colormaps of the images together.
%
%	SUBIMAGE(X,MAP) displays the image X with colormap MAP in the
%	current axis.  
%
%	SUBIMAGE(I,N) displays the intensity image I with a gray
%	colormap of length N.  
%
%	SUBIMAGE(R,G,B) displays the RGB image in the intensity
%	matrices R,G,B with an approximate colormap based on
%	uniform quantization.
%
%	In all cases, colormap is appended to the colormap
%	in the current figure and the values in image are adjusted
%	appropriately.  The total number of colors from all the
%	subimages must not exceed 256.  If it does, use IMAPPROX
%	to create a common colormap with fewer than 256 colors
%	and create the figure using IMSHOW.
%
%	SUBIMAGE(x,y,A,...) can be used to define non-default axes.
%	H = SUBIMAGE(...) returns a handle to an image object.
%
%	Example:
%	  load clown
%	  subplot(2,2,1), imshow(X,map)
%	  [r,g,b] = ind2rgb(X,map);
%	  ramp = (0:49)'/49;
%	  subplot(2,2,2), subimage(gray2ind(r,50),ramp*[1 0 0])
%	  subplot(2,2,3), subimage(gray2ind(g,50),ramp*[0 1 0])
%	  subplot(2,2,4), subimage(gray2ind(b,50),ramp*[0 0 1])
%
%	See also IMAPPROX, IMSHOW.

%	Clay M. Thompson 2-22-93
%	Revised 5-13-93 by CMT
%	Copyright (c) 1993 by The MathWorks, Inc.
%	$Revision: 1.15 $  $Date: 1993/09/09 21:30:52 $

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

cm = colormap;
if nargin==1, % subimage(a)
  if isind(arg1),
    X = arg1;
    map = [];
  elseif isgray(arg1),
    X = round(arg1*(size(cm,1)-1))+1;
  else
    cmin = min(min(arg1)); cmax = max(max(arg1));
    X = min(size(cm,1),round((size(cm,1)-1)*(arg1-cmin)/(cmax-cmin))+1);
  end

elseif nargin==2, % subimage(a,cm) or subimage(I,n)
  if prod(size(arg2))==1,
    X = round(arg1*(arg2-1))+1;
    map = gray(arg2);
  else
    X = arg1; map = arg2;
  end

elseif nargin==3, % subimage(r,g,b)
  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
  [X,map] = rgb2ind(arg1,arg2,arg3,.2); % Uniform quantization, max. 216 colors
  [X,map] = cmunique(X,map); % Use the smallest number of colors possible.

elseif nargin==4, % subimage(x,y,a,cm) or subimage(x,y,I,n)
  if prod(size(arg4))==1,
    X = round(arg3*(arg4-1))+1; map = gray(arg4);
  else
    X = arg3; map = arg4;
  end
  
elseif nargin==5, % subimage(x,y,r,g,b)
  if any(size(arg3)~=size(arg4)) | any(size(arg3)~=size(arg5)),
    error('The R,G, and B matrices must all be the same size.');
  end
  [X,map] = rgb2ind(arg3,arg4,arg5,.2); % Uniform quantization, max. 216 colors
  [X,map] = cmunique(X,map); % Use the smallest number of colors possible.
end

ax = newplot;

% Check for an existing image
g = get(gcf,'ch');
hasimage = 0;
for i=1:length(g),
  h = get(g(i),'children'); 
  for i=1:length(h),
    if strcmp(get(h(i),'Type'),'image'),
      hasimage = 1;
    elseif strcmp(get(h(i),'Type'),'surface'),
      if strcmp(get(h(i),'FaceColor'),'texturemap'),
        hasimage = 2;
      end
    end
  end
end

if ~hasimage,
  if nargin<4, 
    h = imshow(X); 
  else
    x = [min(min(arg1)) max(max(arg1))];
    y = [min(min(arg2)) max(max(arg2))];
    h = imshow(x,y,X); 
  end
  colormap(map)
else
  if size(cm,1)+size(map,1)>256,
    error('Too many colors in current colormap to create subimage.')
  end
  if nargin<4, 
    h = imshow(X+size(cm,1));
  else 
    x = [min(min(arg1)) max(max(arg1))];
    y = [min(min(arg2)) max(max(arg2))];
    h = imshow(x,y,X+size(cm,1));
  end
  colormap([cm;map])
end

if nargout==1,
  hout = h;
end
