% sur une idée de Louis-Hadrien Robert & Paul Turner

im = im2double(imread('image.jpg'));

ir = im(:,:,1);
ig = im(:,:,2);
ib = im(:,:,3);

[Ur,Sr,Vr] = svd(ir-.5);
[Ug,Sg,Vg] = svd(ig-.5);
[Ub,Sb,Vb] = svd(ib-.5);

function out = frame_channel(U,S,V,fun)

    z = zeros(size(diag(S)));

    for k = 1:size(z)(1)
        z(k)=fun(k);
    end

    D = diag(diag(S).*z);
    T = padarray( D ,size(S)-size(D),'post');

    out = U*T*V';
end

function out = frame(Ur,Sr,Vr,Ug,Sg,Vg,Ub,Sb,Vb,fun)
    ir = frame_channel(Ur,Sr,Vr,fun)+.5;
    ig = frame_channel(Ug,Sg,Vg,fun)+.5;
    ib = frame_channel(Ub,Sb,Vb,fun)+.5;
    out = cat(3, ir, ig, ib);
end

mkdir('export')
iMax = 50
parfor frameIndex = 1:iMax

    % pos = frameIndex;
    % sigma = 500;
    % f = @(k) exppdf( (k-pos)**2 ,sigma)/exppdf(0,sigma);

    % f = @(k) .5+.5*sin((k/500*10 + frameIndex/iMax)*2*pi);

    % f = @(k) .5+.5*cos((k/500*100*frameIndex/iMax)*2*pi);
    f = @(k) .5+.5*cos((k*frameIndex/iMax)*2*pi);

    imwrite(frame(Ur,Sr,Vr,Ug,Sg,Vg,Ub,Sb,Vb,f),strcat('export/',num2str(frameIndex,'%04d'),'.png'))
end
