function [bitstream] = subscontour2bitstream(contours, a, b, width, height)
% -------------------------------------------------------------------------
% MSc Project - MSc in Communications and Signal Processing
% Department of Electrical and Electronic Engineering
% Imperial College London, 2011
%
% Supervisor  : Dr Pier Luigi Dragotti
% Contributor : Andriy Gelman
% Author      : Jon Onativia Bravo
%
% File        : contour2bitstream.m
% -------------------------------------------------------------------------
% 
%
% [bitstream] = subscontour2bitstream(contours, a, b, width, height)
%
% INPUT:
%  - 
%
% OUTPUT:
%  - 
%

% Dictionary 
dict = cell(8,2);
dict{1,1} = 0;      dict{1,2} = [0];
dict{2,1} = 45;     dict{2,2} = [1 0];
dict{3,1} = -45;    dict{3,2} = [1 1 0];
dict{4,1} = 90;     dict{4,2} = [1 1 1 0];
dict{5,1} = -90;    dict{5,2} = [1 1 1 1 0];
dict{6,1}= 135;     dict{6,2} = [1 1 1 1 1 0];
dict{7,1} = -135;   dict{7,2} = [1 1 1 1 1 1 0];
dict{8,1} = 180;    dict{8,2} = [1 1 1 1 1 1 1];

% Output variable
bitstream = [];

% Store the size of the images
bitstream = [bitstream, dec2bin(width, 12)];
bitstream = [bitstream, dec2bin(height, 12)];

% Obtain the number of layers
if iscell(contours)
    num_layers = length(contours);
else
    num_layers = 1;
    
    % Put the vertices into a cell so the algorithm always take cells
    verts = contours;
    contours = cell(1, 1);
    contours{1} = verts;
end

% Store the number of layers
bitstream = [bitstream, dec2bin(num_layers, 6)];

for layer = 1 : num_layers
    verts = contours{layer};
    
    % For each layer, store the a and b parameters
    bitstream = [bitstream, dec2bin(a{layer}, 4)];
    bitstream = [bitstream, dec2bin(b{layer}, 4)];
    
    % Get the different new segments
    segment_inds = find(verts(1,:) == 0);
    seg_lengths  = verts(2,segment_inds);
    num_of_segs  = length(segment_inds);
    
    % Store the number of segments
    bitstream = [bitstream, dec2bin(num_of_segs, 6)];
    
    for ith_seg = 1 : num_of_segs
        seg_len   = seg_lengths(ith_seg);
        seg_start = segment_inds(ith_seg) + 1;
        seg_end   = seg_start + seg_len - 1;
        seg       = verts(:,seg_start:seg_end);
        
        % Store the segment length and the absolute position of the first
        % vertex
        bitstream = [bitstream, dec2bin(seg_len, 12)];
        bitstream = [bitstream, dec2bin(seg(1,1), 12)];
        bitstream = [bitstream, dec2bin(seg(2,1), 12)];
        
        if seg_len > 1
            directions  = seg(:,2:end) - seg(:,1:end-1);
            angles      = angle(directions(1,:) + 1i*directions(2,:));
            angles      = round(angles * 180 / pi);
            
            bitstream   = [bitstream, huffmanenco(angles(1), dict)'];
            
            if length(angles) > 1
                angs_dif = angles(2:end) - angles(1:end-1);
                
                % Angles differences bigger than 180 or smaller than -180
                % can be brought to [-180, 180]
                angs_dif(angs_dif>180)   = angs_dif(angs_dif>180) - 360;
                angs_dif(angs_dif<=-180) = angs_dif(angs_dif<=-180) + 360;
                
                bitstream = [bitstream, huffmanenco(angs_dif, dict)];
            end
        end
    end
end

