% image2x1resize.m This function transform image form factor to 2x1, adding black
% bands if needed

function [ imageCorrected, imageHeight, imageWidth, resizeFactor ] = image2x1resize( image, outputWindowWidth )

imageHeight             = size( image, 1 );
imageWidth              = size( image, 2 );

% correct aspect ratio if needed
imageAspectRatio        = imageWidth / imageHeight;                         % should be 2

if imageAspectRatio ~= 2    
    if imageWidth/2 < imageHeight
        if mod( imageWidth, 2 ) == 0
            intervalBlack   = ceil( imageHeight - imageWidth/2 );
            blackBand       = zeros( imageHeight, intervalBlack, 3 );
            image  = [ blackBand, image, blackBand ];
        else
            intervalBlack   = ceil( imageHeight - imageWidth/2 );
            blackBandL      = zeros( imageHeight, intervalBlack, 3 );
            blackBandR      = zeros( imageHeight, intervalBlack-1, 3 );
            image  = [ blackBandL, image, blackBandR ];
        end
    else
        if mod( imageHeight, 2 ) == 0
            intervalBlack   = ceil( imageWidth/4 - imageHeight/2 );
            blackBand       = zeros( intervalBlack, imageWidth, 3 );
            image  = [ blackBand; image; blackBand ];
        else
            intervalBlack   = ceil( imageWidth/4 - imageHeight/2 );
            blackBandU      = zeros( intervalBlack, imageWidth, 3 );
            blackBandD      = zeros( intervalBlack-1, imageWidth, 3 );
            image  = [ blackBandU; image; blackBandD ];
        end
    end
end

imageHeight             = size( image, 1 );                                 % update values
imageWidth              = size( image, 2 );                                 % update values

% resize
resizeFactor            = outputWindowWidth / imageWidth;
imageHeight             = imageHeight * resizeFactor;                       % update value
imageWidth              = imageWidth * resizeFactor;                        % update value
imageCorrected          = imresize( image, resizeFactor );
