function imout=dither( r, g, b, map, qm, qe )
%DITHER	Floyd-Steinberg image dithering.
%	X = DITHER(R,G,B,MAP,Qm,Qe) dithers the RGB image in the 
%	matrices R,G,B against the colormap MAP to create the
%	dithered indexed image X.  Qm specifies the number of
%	quantization bits to use along each color axis for the
%	inverse color map.  Qe specifies the number of quantization
%	bits to use for the color space error calculations.  Use 
%	imshow(X,MAP) to display the dithered image.  If Qe < Qm, 
%	dithering cannot be performed and an undithered indexed 
%	image is returned in X.  
%
%	X = DITHER(R,G,B,MAP) uses the default values: Qm = 5, Qe = 8.
%
%	BW = DITHER(I) converts the gray scale image I to a binary
%	image BW via dithering.
%
%	See also: RGB2IND, RGB2GRAY, IM2BW.

%	This is the wrapper function for the MEX function DITHERC.C

%	Joseph M. Winograd 7-93
%	Copyright (c) 1993 by The MathWorks, Inc.
%	$Revision: 1.7 $  $Date: 1993/10/21 12:59:32 $

%	References: 
%	R. W. Floyd and L. Steinberg, "An Adaptive Algorithm for
%	    Spatial Gray Scale," International Symposium Digest of Technical
%	    Papers, Society for Information Displays, 36.  1975.
%	Spencer W. Thomas, "Efficient Inverse Color Map Computation",
%	   Graphics Gems II, (ed. James Arvo), Academic Press: Boston.
%	   1991. (includes source code)


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

if nargin < 6
    qe = 8;	% DEFAULT: 	If you change default, change them also in 
end		%		ditherc.c MEX file.

if nargin < 5
    qm = 5;	% DEFAULT:	See note above.
end


if nargin==1
  if isgray(r),
    im = im2bw(ditherc(r,r,r,gray(2),qm,qe),gray(2));
  else
    error('Single argument isn''t an intensity image.')
  end
elseif nargin==2 | nargin==3,
  error('Wrong number of input parameters.');
else
  im = ditherc( r, g, b, map, qm, qe );
end

if nargout==0,
  if nargin==1, imshow(im,2), else, imshow(im,map), end
  return
end
imout = im;
