%EUSIPCO 2012
%Imperial College London 
%Andriy Gelman, Jon Onativia and Pier Luigi Dragotti 

function [in] = inverse_move_pixels_into_different_frames(intra, uncovered, D, iteration, number_of_images_original, round_param, label, in)

[r,c,number_of_images] = size(label);

number_of_layers = length(D);

if strcmp(round_param, 'ceil') == 1
    disparity = ceil(([0:1: number_of_images_original-1])'*D');
else
    disparity = round(([0:1: number_of_images_original-1])'*D');
end

for l = 1:iteration -1
    disparity = downsample(disparity,2);
end


for i = 1:number_of_images/2

    for l = 1:number_of_layers
   
        low_pass_frame_label = label(:,:,2*i-1);
        high_pass_frame_label = label(:,:,2*i);
    
        low_pass_frame_data = in(:,:,2*i-1);
        high_pass_frame_data = in(:,:,2*i);
    
        displaced_pixels_frame_1 = zeros(size(label(:,:,1)));
        
        buffer_intra = intra(:,:,i);
        buffer_intra(buffer_intra ~= l) = 0;
        buffer_intra(buffer_intra >0) = 1;
    
        %disparity compensate the intra frame onto the low pass image;
        buffer_intra = integer_shift(buffer_intra, -(disparity(2*i,l)-disparity(2*i-1,l)));
    
        displaced_pixels_frame_1(buffer_intra > 0 & low_pass_frame_label == 0) = 1;
        displaced_pixels_frame_2 = integer_shift(displaced_pixels_frame_1, (disparity(2*i,l)-disparity(2*i-1,l)) );
    
        low_pass_frame_label(displaced_pixels_frame_1 > 0) = l;
        high_pass_frame_label(displaced_pixels_frame_2 > 0) = 0;
    
        if sum(sum(displaced_pixels_frame_2)) >0
        high_pass_frame_data(displaced_pixels_frame_2 >0) = low_pass_frame_data(displaced_pixels_frame_1>0);
        low_pass_frame_data(displaced_pixels_frame_1 > 0)=0;
        end

        label(:,:,2*i-1) = low_pass_frame_label;
        label(:,:,2*i) = high_pass_frame_label;
    
        in(:,:,2*i-1) = low_pass_frame_data;
        in(:,:,2*i) = high_pass_frame_data;
        
        buffer_intra = intra(:,:,i);
        buffer_intra(displaced_pixels_frame_2 >0) = 0;
        intra(:,:,i) = buffer_intra;
        
        uncovered_buffer = uncovered(:,:,i);
        uncovered_buffer(displaced_pixels_frame_2>0) = 0;
        uncovered(:,:,i) = uncovered_buffer;
    end

end
end



function [connected_pixels, intra_pixels] = evaluate_uncovered_pixels(label, layer_val, disparity)

    %layer = extract_layer(label, label, layer_val);
    layer = label;
    
    
    layer(layer>0) = 1;
    layer_frame_1 = layer(:,:,1);
    layer_frame_2 = layer(:,:,2);
    
    %disparity compensate frame 1 onto frame 2
    layer_frame_1 = integer_shift(layer_frame_1, disparity);
    
    intra_pixels = layer_frame_2 - layer_frame_1;
    
    intra_pixels((intra_pixels)>0)=1;
    intra_pixels(intra_pixels<0) = 0;
    
    connected_pixels = layer_frame_2;
    connected_pixels(intra_pixels == 1) =0;

end



