%
% GrayWorldFun.m
%
% Function to implement GrayWorld color balance algorithm
%
% Hui Li
% 3/7/2000
%	

function [outR, outG, outB] = GrayWorldFun(inR, inG, inB)

% Monitor Inverse Gamma correction already done for input images

avgR = mean(mean(inR));
avgG = mean(mean(inG));
avgB = mean(mean(inB));
avgGray = (avgR + avgG + avgB)/3;

% Average R, G, B value
if avgR == 0
	outR = inR;
else
	outR = (avgGray/avgR).*inR;
end

if avgG == 0
	outG = inG;
else
	outG = (avgGray/avgG).*inG;
end

if avgB == 0
	outB = inB;
else
	outB = (avgGray/avgB).*inB;
end


% Scale output
maxRGB = [max(max(outR)) max(max(outG)) max(max(outG))];

factor = max(maxRGB);
if  factor > 1
	outR = outR./factor;
	outG = outG./factor;
	outB = outB./factor;
end


% Camera Gamma Correction

