

function output = decode_data(decoded_data,layer_header, r, r_modified, c, c_modified, k, number_of_blocks, disparity, output_location)

global size_of_largest_block;

%Initially decode the main data

    number_of_layers = length(layer_header(:,1))-1;

    info_layer = layer_header(number_of_layers+1,:);
    %Initialize main layer
    length_rows = info_layer(2)-info_layer(1) + 1;
    length_columns = info_layer(4) - info_layer(3) + 1;
    spatial_decompositions_low = info_layer(5);
    spatial_decompositions_high = info_layer(6);
    
    %temporal_out_main = zeros(length_rows, length_columns, k);
    combined_output = zeros(length_rows, length_columns, k);
    
    total_number_of_blocks_decoded = 1;
    number_of_blocks_decoded_in_each_subband = 1;
    
    load('operation/low_pass_main/combined_output_label.mat');
    for l = 1:k
        block_buffer = [];
        while (number_of_blocks_decoded_in_each_subband-1) ~= number_of_blocks(end, 1)
            
            block_buffer{number_of_blocks_decoded_in_each_subband} = decoded_data{total_number_of_blocks_decoded};
            total_number_of_blocks_decoded = total_number_of_blocks_decoded + 1;
            number_of_blocks_decoded_in_each_subband = number_of_blocks_decoded_in_each_subband + 1;
            
        end
        
        buffer_output_label = combined_output_label(:,:,l);
        buffer_output_label(buffer_output_label>0)=1;
        
        combined_output(:,:,l) = inverse_spatial_shape_adaptive_DWT(block_buffer, 255*buffer_output_label, spatial_decompositions_low, size_of_largest_block);
        
        number_of_blocks_decoded_in_each_subband = 1;
        clear block_buffer;
        
    end
    
    %load combined output label
    %combined_output = inverse_shape_adapative_residual_coding_combined_output(block_buffer, combined_output_label, spatial_decompositions_low, size_of_largest_block);
    
    
    w = cd;
    cd('operation/low_pass_main_reconstructed');
    save combined_output combined_output;
    clear combined_output;
    cd(w);
    %Decode the Residual of each signal
    %First layer will contain no residuals
    for l = 1:number_of_layers
        
        info_layer = layer_header(l,:);
        
        if info_layer(1) ~= 0
            %Initialize main layer
            length_rows = info_layer(2)-info_layer(1) + 1;
            length_columns = info_layer(4) - info_layer(3) + 1;
            
            
            residual = zeros(length_rows, length_columns, k);
            
            %Load Residual label
            w = cd;
            cd('operation/residual');
            string_label = ['residual_label', num2str(l)];
            load(string_label);
            cd(w);
            
            residual_label = residual_label(info_layer(1):info_layer(2), info_layer(3):info_layer(4),:);
            %Decode each subband
            for i = 1:k
                
                while (number_of_blocks_decoded_in_each_subband-1) ~= number_of_blocks(l, i)
                    block_buffer{number_of_blocks_decoded_in_each_subband} = decoded_data{total_number_of_blocks_decoded};
                    total_number_of_blocks_decoded = total_number_of_blocks_decoded + 1;
                    number_of_blocks_decoded_in_each_subband = number_of_blocks_decoded_in_each_subband + 1;
                end
                
                if (i == 1)
                    residual(:,:,i) = inverse_spatial_shape_adaptive_DWT(block_buffer, residual_label(:,:,i), info_layer(end-2), size_of_largest_block);
                    number_of_blocks_decoded_in_each_subband = 1;
                    clear block_buffer;
                else
                    residual(:,:,i) = inverse_spatial_shape_adaptive_DWT(block_buffer, residual_label(:,:,i), info_layer(end-1), size_of_largest_block);
                    number_of_blocks_decoded_in_each_subband = 1;
                    clear block_buffer;
                end
            end
            w = cd;
            cd('operation/residual_reconstructed');
            string = ['residual', num2str(l)];
            save(string, 'residual');
            cd(w);
        end
    end
    
    i = 1;
    
    %Reconstruct each layer
    
    for i = 1:number_of_layers
        
        info_main_layer = layer_header(number_of_layers+1,:);
        info_residual_layer = layer_header(i,:);
        
        load('operation/low_pass_main_reconstructed/combined_output');
        
        if (info_residual_layer(1) ~= 0)
            string = ['operation/residual_reconstructed/residual', num2str(i)];
            load(string);
        end
        %Load the label of the layer in the combined output
        string_label = ['operation/recombined/recombined_label', num2str(i)];
        %string_label = ['operation/low_pass_main/low_pass_main_label', num2str(i)];
        load(string_label);
        
        
        
        
        %Initiliaze output
        layer_output = zeros(r_modified, c_modified, k);
        
        recombined_label = recombined_label(info_main_layer(1):info_main_layer(2), info_main_layer(3):info_main_layer(4), :);
        %Place the low pass in the correct location of the layer
        combined_output = combined_output.*recombined_label;
        layer_output(info_main_layer(1):info_main_layer(2), info_main_layer(3):info_main_layer(4),:) = combined_output;
        if i == 7
            stop_here = 1;
        end
        clear combined_output;
        
        %Add the residual signal back to the output layer
        %if i~=1
        if (info_residual_layer(1)~=0)
            layer_output(info_residual_layer(1):info_residual_layer(2), info_residual_layer(3):info_residual_layer(4),:) = residual...
                +layer_output(info_residual_layer(1):info_residual_layer(2), info_residual_layer(3):info_residual_layer(4),:);
        end
        
        layer_output = change_view_location(layer_output, disparity(:,i), [1:k], output_location);
        
        string = ['operation/extracted_layer_label/original_label', num2str(i)];
        load(string);
        
        load('D.mat');
        
        load disparity_data_set.mat;
        disparity_vec = disparity(:,i);
        save disparity_vec disparity_vec;
        
        w = cd;
        cd('original_label');
        string = ['lifting_params', num2str(i)];
        load(string);
        cd(w);
        
        cd('Inter view lifting DWT');
        layer_output = inverse_lifting_haar_transform(layer_output, double(input_label), D(i), 'ceil', uncovered_pixels, intra_pixels, motion_Vect);
        cd('..');
        
        d = (c_modified -c)/2;
        e = (r_modified-r)/2;
        
        layer_output  = uint8(round(layer_output(e+1:end-e,d+1:end-d,:)));
        
        w = cd;
        cd('operation/reconstructed_layers');
        string = ['layer', num2str(i)];
        save(string, 'layer_output');
        cd(w)
        
    end
    
    load label.mat;
    
    label = label(e+1:end-e,d+1:end-d,:);
    output = recombine_layers_using_saved_data(label, 1);
    clear temporal_out
    clear label;



end
