function bout=grayslice(a,n)
%GRAYSLICE Density (intensity) slicing.
%	X=GRAYSLICE(I,N) converts an intensity image I to a pseudo-color
%	(indexed) image X with N colors.  The values in the matrix I
%	should range from 0 (black) to 1 (white).  The image matrix
%	X will contain indices into a colormap of length N (e.g. JET(N)).
%	The (N-1) slicing planes will be equally spaced between 0 and 1.
%	The default value for N is 10.
%
%	X=GRAYSLICE(A,V) converts the matrix A to a pseudo-color
%	image with N=LENGTH(V)+1 colors.  The vector V contains the 
%	intensity values of the slicing planes that separate the colors.
%	For example, X=GRAYSLICE(A,[.34 .7]) returns an indexed image X
%	with three colors such that the pixels with values in the ranges
%	A<0.34, 0.34<A<0.7, and 0.7<A will be mapped to different colors.
%
%	The density sliced image can be viewed using IMSHOW(X,MAP) with
%	any colormap MAP of length N.  GRAYSLICE(A,N) or GRAYSLICE(A,V)
%	displays the density sliced image using the colormap JET.

%	Clay M. Thompson 1-28-93
%	Copyright (c) 1993 by The MathWorks, Inc.
%	$Revision: 1.10 $  $Date: 1993/09/09 17:41:38 $


error(nargchk(1,2,nargin));

if nargin<2, n = 10; end

if round(n)==n | n>1, % Integer number of equally spaced levels.
  b = round(a*(n-1))+1;
else
  z = n; n = length(z)+1;
  zmin = min(0,min(min(a))); zmax = max(1,max(max(a)));
  z = [zmin,max(zmin,min(zmax,sort(z(:)')))]; % sort and threshold z
  b = zeros(size(a));
  for i=1:length(z)-1,
    d = find(a>=z(i) & a<z(i+1));
    if ~isempty(d), b(d) = i*ones(size(d)); end
  end
  d = find(a>z(length(z)));
  if ~isempty(d), b(d) = length(z)*ones(size(d)); end
end

if nargout==0,
  imshow(b,jet(n))
  return
end
bout = b;
  


