% edgetimer.m 
% second script, uses information from first script (shadowscan.m)
% to make image of when the pixel was first seen by the shadow via
% interpolation.

Iedgetime = double(zeros(sizey,sizex));
for i = 1:sizey
    for j = 1:sizex
        Iedgetime(i,j) = endimg+1;
    end
end

str = sprintf('output_%05d.jpg',startimg);
img = rgb2gray(imread(str));
imshad = (double(img) - Ishadow).*im2bw(1/256*Icontrast,30/256);

for x = startimg+1:endimg-1
    str = sprintf('output_%05d.jpg',x);
    img = rgb2gray(imread(str));
    oldimshad = imshad;
    imshad = (double(img) - Ishadow).*im2bw(1/256*Icontrast,30/256);
    for i = 1:sizey
        for j = 1:sizex
            if (imshad(i,j) < 0)
                if (Iedgetime(i,j) > x)
                    deltat = (-oldimshad(i,j)/(imshad(i,j)-oldimshad(i,j)));
                    if (deltat > 1)
                        deltat = 1;
                    elseif (deltat < 0)
                        deltat = 0;
                    end
                    if ((deltat + x) > endimg)
                        Iedgetime(i,j) = imgend-1;
                    elseif (deltat + x) < startimg
                        Iedgetime(i,j) = startimg+1;
                    else
                        Iedgetime(i,j) = x + deltat;              
                    end
                end
            end
        end
    end
end