%assignment 1
% 1.1 Bayer Pattern demosaicing
close all
gt    = imread('./crayons.jpg');
bayer = imread('./crayons_mosaic.bmp');

 r = zeros(size(bayer));
 g = zeros(size(bayer));
 b = zeros(size(bayer));
 
 r(1:2:end,1:2:end)=bayer(1:2:end,1:2:end);
 g(1:2:end,2:2:end)=bayer(1:2:end,2:2:end);
 b(2:2:end,2:2:end)=bayer(2:2:end,2:2:end);
 g(2:2:end,1:2:end)=bayer(2:2:end,1:2:end);

 
 
 redBlueFilt= 0.25 * [ 1, 2, 1;
                       2, 4, 2;
                       1, 2, 1;];
               
               
 greenFilt= 0.25 * [ 0, 1, 0;
                     1, 4, 1;
                     0, 1, 0;];
               
 
    R=uint8(imfilter(r,redBlueFilt,'circular'));
    G=uint8(imfilter(g,greenFilt,'circular'));
    B=uint8(imfilter(b,redBlueFilt,'circular'));
    
reconstructed= cat(3,R,G,B);
diffMap =abs(reconstructed-gt);
SDMap= sum(diffMap.^2,3);
figure,imagesc(SDMap)
title('Heat map of squared differences (summed over color channels)')
colormap(copper(5))
colorbar
avErr=mean(diffMap(:)) %the mean of absolute difference
maxErr=max(diffMap(:)) %maximum of absolute difference

