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

function [left_edge, right_edge] = compute_edge_map_horizontal(layer1)

layer1(layer1> 0) = 1;
       
[r,c,k] = size(layer1);

buffer = layer1;

%Shift buffer 1 by one pixel left and obtain pixels still in the first
%layer
%buffer = disparity_comp(buffer, [], 1, 1, 2);
buffer = integer_shift(buffer, 1);

buffer = buffer + layer1;

buffer((layer1 ~= 1)) = 0; 
buffer = mod(buffer,2);

right_edge = buffer;

%compute left edge
buffer = layer1;

%Shift buffer 1 by one pixel left and obtain pixels still in the first
%layer
%buffer = disparity_comp(buffer, [],-1, 1, 2);
buffer = integer_shift(buffer, -1);

buffer = buffer + layer1;

buffer((layer1 ~= 1)) = 0; 
buffer = mod(buffer,2);

left_edge = buffer;


%compute top edge
buffer = layer1;
%{
%Shift buffer 1 by one pixel left and obtain pixels still in the first
%layer
buffer = (disparity_comp(buffer', [], 1, 1, 2))';

buffer = buffer + layer1;

buffer((layer1 ~= 1)) = 0; 
buffer = mod(buffer,2);

top_edge = buffer;


%compute bottom edge
buffer = layer1;

%Shift buffer 1 by one pixel left and obtain pixels still in the first
%layer
buffer = (disparity_comp(buffer',[], -1, 1, 2))';

buffer = buffer + layer1;

buffer((layer1 ~= 1)) = 0; 
buffer = mod(buffer,2);

bottom_edge = buffer;

edge_map = left_edge+ right_edge+top_edge + bottom_edge;

edge_map(edge_map >0 )= 1;
%}
%edge_map = left_edge + right_edge;
%edge_map(edge_map >0 )= 1;
%edge_map = uint8(edge_map);
left_edge(left_edge >0 )= 1;
right_edge(right_edge>0) = 1;

if ((layer1(1,1) + 257) == 255)
    left_edge = uint8(left_edge);
    right_edge = uint8(right_edge);
end
end


function  out = integer_shift(input, total_shift)

[r,c,k] = size(input);
%Pad Signal

sig_pad = zeros(r,c+2,k);
sig_pad(:,2:end-1,:) = input;

sig_pad = padarray(sig_pad, [0, abs(total_shift) - 1],'both');

coords = [abs(total_shift) + 1, length(sig_pad(1,:,1)) - abs(total_shift)];

sig_pad = circshift(sig_pad, [0, -total_shift]);

out = sig_pad(:,coords(1):coords(2),:);

end