function y = imhistc(a,n,isGray)
%IMHISTC Image histogram (MEX file).
%	IMHISTC implements the histogram calculation for the
%	function IMHIST.  
%
%	Y = IMHISTC(X,N,GRAY_FLAG) returns the number of pixels with
%	indices from 1 to N.  GRAY_FLAG indicates whether X is an 
%	intensity image or not.  If it is, X is scaled up by (N-1) 
%	to create an indexed image into a GRAY(N) colormap.
%
%	Don't use this function directly, use IMHIST instead.

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

%	This m-file will only be called if the MEX function is
%	not available.

if isGray, a = floor(a*(n-1))+1; end % Scale if necessary
if max(max(a))>n,
  error('X contains values outside the colormap range.')
end
M = prod(size(a));
ind = 1:M;
y = full(sum(sparse(ind,a(ind),1,M,n)))';
