function image2 = convert_image_to_c_file (image_file)

    output_width = 320;
    output_height = 240;
    border_color = [255,255,255];

    input_image = imread(image_file);
    output_aspect = output_width / output_height;
    
    input_width = size(input_image,2);
    input_height = size(input_image,1);
    input_aspect = input_width / input_height;

    if output_aspect > input_aspect
        % output is wider than input
        output_image=add_letterbox_and_resize(input_image,...
                                              output_width,...
                                              output_height,...
                                              border_color);
    elseif output_aspect > input_aspect
        input_image=imrotate(input_image,90);
        output_image=add_letterbox_and_resize(input_image,...
                                              output_width,...
                                              output_height,...
                                              border_color);
        
        output_image=imrotate(output_image,-90);
    end

    write_code(output_image,'myfile.c');
    
end

function output_image = add_letterbox_and_resize (input_image,...
                                                  output_width,...
                                                  output_height,...
                                                  border_color)
    input_width = size(input_image,2);
    input_height = size(input_image,1);
    input_aspect = input_width / input_height;

    output_aspect = output_width / output_height;
    
    uncorrected_width = floor(input_height * output_aspect);
    
    border_width = uncorrected_width - input_width;
    border_width_left = floor(border_width/2);
    border_width_right = ceil(border_width/2);
    
    left_border = ones(input_height,border_width_left,3);
    left_border(:,:,1) = border_color(1);
    left_border(:,:,2) = border_color(2);
    left_border(:,:,3) = border_color(3);
    
    right_border = ones(input_height,border_width_right,3);
    right_border(:,:,1) = border_color(1);
    right_border(:,:,2) = border_color(2);
    right_border(:,:,3) = border_color(3);
    
    input_image = [left_border input_image right_border];
    output_image = imresize(input_image,[output_height,...
                                         output_width]);
end

function write_code = write_code (image,filename)
    myfile=fopen(filename,'w');
    n=0;
    
    header_filename = strrep(filename,'.c','.h');

    fprintf (myfile,'#include "alt_types.h"\n#include "%s"\n\n',...
                                                        header_filename);
    %fprintf (myfile,'void set_matrix_to_image(alt_u16 *my_array) {\n');
    
    total_size = size(image,1) * size(image,2) * size(image,3);
    
    fprintf (myfile,'const alt_u8 myimage[%d] = {',total_size+1);
    count = 0;
    for i=1:size(image,1)
        for j=1:size(image,2)
            for k=1:size(image,3)
                fprintf (myfile,'%d,',image(i,j,k));
                count=count+1;
            end
        end
    end
    fprintf (myfile,'0};\n');
    fclose (myfile);
    
    myfile=fopen(header_filename,'w');
    fprintf (myfile,'#ifndef MYFILE_H\n#define MYFILE_H_\n\n#include "alt_types.h"\n\nextern const alt_u8 myimage[];\n\n#endif /* MYFILE_H_ */\n');
    fclose (myfile);
end

