function [ image_out ] = bmp_to_coe( filename_in, filename_out )
%Converts a 512 wide x 480 tall bmp to coe file
%   to be preloaded into block ram memory

image = imread(filename_in); % Open image

fid = fopen( filename_out, 'wt' ); % Create COE file
fprintf( fid, 'memory_initialization_radix=2;\n')
fprintf( fid, 'memory_initialization_vector=\n')


for (row = 1:480)     % Loop for each row
   for (column = 1:512)  % Loop for each column
       % In indexing memory, concatenate row and column as 
       % address = { row[8:0], column[8:0] }
       % Need to shift row left by 9 bits 
       % Subtracting 1 from row due to Matlab's 1 indexing
       %   Not Subtracting -> not writing to first row
       address = ( bitsll((row-1),9) + column );
       
       % White pixels = 255
       % Black pixels = 0
       % Gray pixels somewhere between 0 and 255
       % Want a pixel if the colors are anything but black
       if (~(image(row,column,1) == 0)) 
           pixel = 1;
           image_out(row,column) = 1;
       else 
           pixel = 0;
           image_out(row,column) = 0;
       end
       
       % Assign current pixel value to proper memory address
       memory(address) = pixel;
   end    
end

% Print out all of memory file into COE file
for address = 1:245759
   fprintf( fid, '%d,\n', memory(address) );   
end

% Last line needs a semicolon
address = 245760
fprintf( fid, '%d;\n', memory(address) );

fclose(fid)


end

