% Refocus in Spatial Domain
% Review thesis, "Fourier Slice Photography"(Page 2, Formula 3)
% Photography Operator (Basis Change and Integral Projection)
% \iint LF( u*(1-1/a)+x/a, v*(1-1/a)+y/a, u, v) dudv

function IOut = RefocusINT(LF,a,b)

nAngles = size(LF, 3);   % nAnlges = 9 % The Number of Angles
cAngles = (nAngles+1)/2; % cAngles = 5 % Center of Angles

F1Y  = size(LF, 1);  % F1Y  = 175
F1X  = size(LF, 2);  % F1X  = 237
F12Y = floor(F1Y/2); % F12Y = 87
F12X = floor(F1X/2); % F12X = 118

img_out = zeros(F1Y, F1X); % Initialization of Output Image

for y=1:F1Y % 1~175 % Vertical (y)
    for x=1:F1X % 1~237 % Horizontal (x)
        for v=1:nAngles % 1~9
            for u=1:nAngles % 1~9
                idx1 = round(b*(v-cAngles)*(1-1/a)+(y-F12Y)/a+F12Y);
                if(idx1 <= 0)
                    idx1 = 1;
                elseif(idx1 > F1Y)
                    idx1 = F1Y;
                end
                idx2 = round(b*(u-cAngles)*(1-1/a)+(x-F12X)/a+F12X);
                if(idx2 <= 0)
                    idx2 = 1;
                elseif(idx2 > F1X)
                    idx2 = F1X;
                end
                img_out(y,x) = img_out(y,x) + LF(idx1,idx2,v,u);
            end
        end
    end
end

IOut = real(img_out);
