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

function layer_stack = generate_layer_stack(label, D, zero_pad, string, stack_phi_location)

label = padarray(label, [0 zero_pad], 'both');

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

number_of_layers = length(D);

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

layer_stack = zeros(r,c, number_of_layers);

for l = 1:number_of_layers
    
    buffer_layer = extract_layer(label, label, l)/l;
    
    %map the layers to a single frame
    buffer_image = zeros(r,c);
    for i = 1:number_of_images
        buffer_image = buffer_image + integer_shift(buffer_layer(:,:,i), -(disparity(i,l) - disparity(stack_phi_location,l)));    
    end
    buffer_image(buffer_image > 0) = 1;
    layer_stack(:,:,l) = buffer_image;
end

    
end