function [interpH, interpV] = LiRandhawa_Interp(filename)
% [interpH, interpV] = LiRandhawa_Interp(filename)
%
% Construct horizontally and vertically interpolated images
% according to the Li & Randhawa Cubic-Spline Interpolation Method.
% Written by Matt Golub for Psych 221, Winter Quarter, 2008.
%
% INPUTS:   filename - The filename (including extension) of the unfiltered image
%                      being mosaicked/demosaicked.
%
% OUTPUTS:  interpH -  An RGB image computed using cubic-spline interpolations.
%                      The green channel is constructed from interpolating
%                      across rows only.  The red and blue channels are 
%                      constructed from first interpolating across rows, 
%                      and then interpolating down columns.
%           interpV -  An RGB image computed using cubic-spline interpolations.
%                      The green channel is constructed from interpolating
%                      down columns only.  The red and blue channels are 
%                      constructed from first interpolating across rows, 
%                      and then interpolating down columns.
%           
%                      Red and blue channels  of interpH and interpV are identical:
%                      interpH(:,:,1) = interpV(:,:,1)
%                      interpH(:,:,3) = interpV(:,:,3)  
                    
% Read in an image and process it with a simulated Color Filter Array
Xcfa=bayerCFA(filename);
X=double(imread(filename))/255;

[height width depth]=size(X);


%%Horizontal cubic spline interpolation over green channel
disp('Creating horizontal cubic spline interpolation over G channel');
for n=1:2:height-1
    GoutH(n,:) = csi1D(Xcfa(n,:,2),Xcfa(n,:,1));       %Interpolate G using R
    GoutH(n+1,:) = csi1D(Xcfa(n+1,:,2),Xcfa(n+1,:,3)); %Interpolate G using B
end

%Vertical cubic spline interpolation over green channel
disp('Creating vertical cubic spline interpolation over G channel');
for n=1:2:width-1
    GoutV(:,n) = csi1D(Xcfa(:,n,2)',Xcfa(:,n,1)');
    GoutV(:,n+1) = csi1D(Xcfa(:,n+1,2)',Xcfa(:,n+1,3)');
end

%%Red and blue channel cubic-spline interpolation
disp('Creating horizontal cubic-spline interpolation over R & B channels');
for n=1:2:height-1
    Rout(n,:) = csi1D(Xcfa(n,:,1), Xcfa(n,:,2));  %Interpolate across red horizontal vectors
    Bout(n+1,:) = csi1D(Xcfa(n+1,:,3), Xcfa(n+1,:,2));   %Interpolate across blue horizontal vectors
end
Rout(height,:) = zeros(1,width);
disp('Creating vertical cubic-spline interpolation over R & B channels');
for n=1:width
    Rout(:,n) = csi1D(Rout(:,n)', Xcfa(:,n,2)');       %Interpolate red down vertical columns
    Bout(:,n) = csi1D(Bout(:,n)', Xcfa(:,n,2)');       %Interpolate blue down vertical columns
end;

% Reform images from the interpolated channels
interpH(:,:,1) = Rout;
interpH(:,:,2) = GoutH;  % This image uses the horizontal green interpolation
interpH(:,:,3) = Bout;

interpV(:,:,1) = Rout;
interpV(:,:,2) = GoutV;  % This image uses the vertical green interpolation
interpV(:,:,3) = Bout;

end