%COMP 256 Assignment 2
%Adrian Ilie

function [r1,r2]=lfrrfl(im1,im2,di1,di2);
%compute "swapped" views

[h,w,d]=size(im1);
[h2,w2]=size(di1);
hoffset=floor((h-h2)/2);
woffset=floor((w-w2)/2);

r1=zeros(h,w,d);
r1=uint8(r1);
r2=zeros(h,w,d);
r2=uint8(r2);
for i=h2:-1:1
    for j=w2:-1:1
        if ((di1(i,j)~=0)&&(j+woffset+di2(i,j)<w))
            r1(i+hoffset,j+woffset,:)=im1(i+hoffset,j+woffset+di2(i,j),:);
        end
        if ((di2(i,j)~=0)&&(j+woffset-di1(i,j)>0))
            r2(i+hoffset,j+woffset,:)=im2(i+hoffset,j+woffset-di1(i,j),:);
        end
    end
end