📄 bmp2c.c
字号:
/*
BMP2C Version 1.0
2001 08 24 (Happy Birthday, Alexander! ^-^)
--------
Isaac Rounds
admin@isaac.cxfs
www.gbadev.org/cabbage
Edited by Sean Tasker
slip@ice-d.com
www.ice-d.com
--------
Usage: bmp2c input.bmp output.c
Note that the output file will be overwritten
by default. I will probably hack up something
to fix that in the next release (assuming
that there will be =D)
--------
Greets bounce out to Jeremy Penner and SimonB!
*/
#include <stdio.h>
int main(int argv, char *argc[])
{
FILE *fin;
FILE *fout;
unsigned int i,j,width,height;
unsigned char dataV;
if(argv<3) {
printf("Usage: %s input.bmp output.c\n",argc[0]);
return;
}
if((fin=fopen(argc[1],"r"))==NULL) {
printf("Error loading input file.\n");
return;
}
fseek(fin,0x1c,SEEK_SET);
if(fgetc(fin)!=8) {
printf("Bitmap must have 256 colours.\n");
fclose(fin);
return;
}
if((fout=fopen(argc[2],"w"))==NULL) {
printf("Output file not found. Creating it now...");
fclose(fin);
return;
}
fprintf(fout,"/*\n BMP2C Version 1.0a\n 2001 08 24 (Happy Birthday, Alexander! ^-^)\n --------\n Isaac Rounds\n admin@isaac.cx\n www.gbadev.org/cabbage\n\n Edited by Sean Tasker\n slip@ice-d.com\n www.ice-d.com\n --------\n Usage: bmp2c input.bmp output.c\n Note that the output file will be overwritten\n by default. I will probably hack up something\n to fix that in the next release (assuming\n that there will be =D)\n --------\n Greets bounce out to Jeremy Penner and SimonB!\n*/\n\n");
fprintf(fout,"unsigned short %s[] = {\n",strtok(argc[2],"."));
fseek(fin,0x12,SEEK_SET);
fread(&width,4,1,fin);
fseek(fin,0x16,SEEK_SET);
fread(&height,4,1,fin);
fseek(fin,0x436,SEEK_SET);
for(j=0; j<height; j++) {
fprintf(fout," ");
for(i=0; i<width; i++)
{
//The problem ocoured here. The next line was changed to the following three.
//There were errors in the output file. After a certain number of entries the
//output was at a max value. For example 0xFFFFFFF.
//fprintf(fout,"%#.2x, ",fgetc(fin));
fread(&dataV,2,1,fin);
fseek(fin,0x436+i+(j*width),SEEK_SET);
fprintf(fout,"%#.2x, ",dataV);
//it seems as though fgetc has a problem accessing data after a certain position.
}
fprintf(fout,"\n");
}
fprintf(fout,"};");
fclose(fin);
fclose(fout);
return 0;
}
/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -