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

function [low_pass, high_pass, uncovered, motionVect] = haar_decomposition_two_images(image_1, image_2, label_buffer, intra, D, disparity_buffer)

global block_compensation_size;

number_of_layers = length(disparity_buffer(1,:));

[r,c] = size(image_1);

high_pass = zeros(r,c);  
low_pass = zeros(r,c);
uncovered = zeros(r,c);
covered = zeros(r,c);

%Apply prediction to the intra coded pixels - Block motion compensation 

%{
w = cd;
intra(intra>0) = 1;
cd('..\..\block matching software\BlockMatchingAlgoMPEG');
[motionVect, EScomputations] = motionEstES(image_2, image_1, intra,  block_compensation_size, 15);
imgComp = motionComp(image_1, motionVect, block_compensation_size).*intra;
high_pass = high_pass + ((image_2 - imgComp)/2).*intra;
cd(w);
%}
intra_buffer= intra;
intra_buffer(intra_buffer>0)=1;
high_pass = high_pass + image_2.*intra_buffer;
motionVect = [];

low_pass_label = label_buffer(:,:,1);
low_pass_label(low_pass_label>0)=1;

%Apply prediction high-pass step
for l = 1:number_of_layers
    %Update uncovered areas       
    [connected_pixels, new_uncovered_pixels] = evaluate_uncovered_pixels(label_buffer, l, disparity_buffer(2,l) - disparity_buffer(1,l));   
    uncovered = uncovered + new_uncovered_pixels*l;    
    %apply prediction step to the connected pixels     
    high_pass = high_pass + ((image_2 - disparity_comp(image_1, low_pass_label, D(l), 1, 2))/2).*connected_pixels;     
end

buffer = uncovered;    
high_pass_label = -(buffer-1);
high_pass_label(high_pass_label > 0) = 1;
high_pass_label(high_pass_label < 0) = 0;

buffer(buffer>0) = 1;    
buffer(intra > 0) = 0;
high_pass = high_pass + image_2.*buffer;     

    %Apply update step 
    for l = 1:number_of_layers

        [connected_pixels, new_covered_pixels] = evaluate_covered_pixels(label_buffer, l, disparity_buffer(2,l) - disparity_buffer(1,l));   
        covered = covered + new_covered_pixels;   
        low_pass = low_pass + (image_1 + disparity_comp(high_pass, high_pass_label, D(l), 2, 1)).*connected_pixels;
    
    end
    low_pass = low_pass + image_1.*covered; 
end



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

    layer = extract_layer(label, label, layer_val);
    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

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

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

    connected_pixels = layer_frame_1;
    connected_pixels(intra_pixels == 1) =0;
end
