function [albedo, imNorm]= getOneNorm(lightDir, testImage, imageMask)

% input: the light direction and test image. test image will be a cell
% array.

%output: albedo: a MxNx3 matrix. imNorm: a MxNx3 matrix.

imageMask = im2double(imageMask);
[M, N] = size(imageMask);


K = length(testImage);
grayImage = zeros(M, N, K);
imageR = zeros(M,N, K);
imageG = zeros(M,N, K);
imageB = zeros(M,N, K);
albedoR = zeros(M,N);
albedoG = zeros(M,N);
albedoB = zeros(M,N);

imNorm = zeros(M,N,3);

for k = 1:K
    grayImage(:,:,k) = im2double(RGB2gray(testImage{k}));
    imageR(:,:,k) = im2double(testImage{k}(:,:,1));
    imageG(:,:,k) = im2double(testImage{k}(:,:,2));
    imageB(:,:,k) = im2double(testImage{k}(:,:,3));
end;

I = zeros(1:K);
for i = 1:M
    for j = 1:N
        if imageMask(i,j) == 1
            I = reshape(grayImage(i,j,:),10,1);
            Li = zeros(K, 3);
            for k = 1:K
                Li(k,:) = lightDir{k};
            end;
%             
%             % weight pixel values by multiplying I
%             I = I .* I;
%             Li = 
            g = Li\I;
            imNorm(i,j,:) = g/norm(g);
            if isnan(imNorm(i,j,:))
                imNorm(i,j,:) = 0;
            end;
            
            J = Li * reshape(imNorm(i,j,:),3,1);
            I_R = reshape(imageR(i,j,:),10,1);
            I_G = reshape(imageG(i,j,:),10,1);
            I_B = reshape(imageB(i,j,:),10,1);
            JJ = dot(J,J);
            albedoR(i,j) = dot(I_R,J)/JJ;
            albedoG(i,j) = dot(I_G,J)/JJ;
            albedoB(i,j) = dot(I_B,J)/JJ;
        end;
    end;
end;

albedo = zeros(M,N,3);
albedo(:,:,1) = albedoR./max(max(albedoR));
albedo(:,:,2) = albedoG./max(max(albedoG));
albedo(:,:,3) = albedoB./max(max(albedoB));
imwrite(albedo, 'imageAlbedo.tiff');

            
            
            



