read


read
or learn more

C Fun: Bitmaps

Jul 6, 2004

C Fun
At one point or another I needed a method for including a bitmap into a binary in order to jump directly to said bitmap and display it during execution. Normally, one would simply create a library or two to load an external bitmap and display it. However, because of the circumstances I needed a truly lightweight implementation, and needed the bitmap to remain static (and in fact was used as a checksum). Therefore, my brilliant solution was to cruch the bitmap data into the binary, run some tests on its data (checksum), and display the bitmap only if the checksum was valid. My eventual solution was a simple snippet of C code:



#include

int main( int argc, char **argv )
{
int i;
FILE *f;
int ch;
char sep;

for (i = 1; i < argc - 1; i += 2)
{
sep = '{';

f = fopen(argv[i + 1],"r");
printf("char %s[] = ",argv[i]);

while((ch = fgetc(f)) != -1)
{
printf("%c %d",sep,ch);
sep = ',';
}

printf(" };\n");
fclose(f);
} return 0;
}


The point is that the code takes a bitmap (any binary file actually) and translates its contents into a byte array. This program’s output can then be piped into a C header file, and then included in the source to run the checksum and display the image.

-m

No Comments, Comment or Ping

Reply to “C Fun: Bitmaps”