function [im, map] = vmquant(r,g,b,k,Q,dither,qe )
%VMQUANT Variance Minimization Color Quantization.
%	[X, MAP] = VMQUANT(R,G,B,K,[Qr Qg Qb],DITHER,Qe) converts 
%	an arbitrary image comprised of RGB triples into an 
%	indexed image X with color map MAP.  K specifies the number 
%	of desired entries in the target color map, and [Qr Qg Qb] 
%	specifies the number of quantization bits to assign each color 
%	axis during color interpolation.  DITHER is a string ('dither' or
%	'nodither') that indicates whether or not to perform error propagation 
%	dither on the output image.  Qe specifies the number of bits of
%	quantization used in the error calculations.
%
%	K is optional and defaults to 256.
%	[Qr Qg Qb] is optional and defaults to [5 5 5].
%	DITHER is optional and defaults to 'nodither'.
%	Qe is optional and defaults to 8.
%
%	This function requires temporary memory storage proportional to
%	c1*2^(Qr+Qg+Qb) + c2*k + c3*prod(size(R)). c1, c2 and c3 are
%	machine dependant.  Memory usage on most platforms is around 800Kb
%	for a 100x100 image with the default parameters.
%
%	See also: RGB2IND, RGB2GRAY, DITHER, IND2RGB, CMUNIQUE, IMAPPROX.

%	This is the wrapper function for the MEX file VMQUANTC.C

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

%	Reference: Xiaolin Wu, "Efficient Statistical Computation for
%	Optimal Color Quantization," Graphics Gems II, (ed. James
%	Arvo).  Academic Press: Boston. 1991.

error( nargchk( 3, 7, nargin ) );

			% NOTE: If you change defaults, change them also
			%	in VMQUANTC.C and recompile the MEX function.
if nargin < 7
    qe = 8;		% DEFAULT_QE = 8
end

if nargin < 6
    dither = 'n';		% DEFAULT_DITHER = 0
end

if nargin < 5
    Q = [5 5 5];	% DEFAULT_Q = [5 5 5]
end

if nargin < 4
    k = 256;		% DEFAULT_K = 256
end
if strcmp(lower(dither(1)),'d')
   dith = 1;
else
   dith = 0;
end
   
[im map] = vmquantc( r, g, b, k, Q, dith, qe );
