
%takes a directory, loads all images in that directory, outputs a c header file
function success = imgs2progmem(directory)

height = 39;
width = 60;


maybefiles = ls(directory);

fileCount = 0;
for i = 1:size(maybefiles,1)
	if( maybefiles(i,1) ~= '.')
		fileCount = fileCount +1;
		files(fileCount,:) = maybefiles(i,:);
	end
end

images = false(height*width,size(files,1));

for i = 1:size(files,1)
	%read in an image
	filename = strcat(directory,'/',files(i,:));
	image = double(imread(filename));
	for y = 1:height
		for x = 1:width
			images((y-1)*width+x,i) = image(y,x) > 0;
		end
	end
end

%compress image data
imgSize = ceil(height*width/8); %8 is sizeof char
data = zeros(imgSize,size(files,1));
for i = 1:size(files,1)
	count = 0;
	for j = 1:height*width
		data(floor(count/8+1),i) = data(floor(count/8+1),i) + images(j,i) * 2^mod(count,8);
		count = count + 1;
	end
end

%output a c header file with all the images
fid = fopen('src/Shapes.h','wt');

fprintf(fid,'//<INSERT aip kaf HEADER>\n //Auto-Generated File \n');
fprintf(fid,'#include <avr/pgmspace.h>\n');

fprintf(fid,' uint8_t Shapes[%u][%u] PROGMEM = {',size(files,1),imgSize);

for i = 1:size(files,1)
	fprintf(fid,'\n /* %s */ \n',files(i,:));
	%loop through image
	fprintf(fid,'{');
	for j = 1:imgSize
		fprintf(fid,' 0x%0.2X', data(j,i));
		if(j ~=imgSize)
			fprintf(fid,',');
		end
	end
	if(i ~= size(files,1))
		fprintf(fid,'},\n');
	else
		fprintf(fid,'}\n };\n');
	end
	
end

fprintf(fid,'\n');
fclose(fid);

success = 'success';
end
