📄 bmp2c.cpp
字号:
fprintf (outfile,"#if !defined(INCLUDED_%s_H)\n",file);
fprintf (outfile,"#define INCLUDED_%s_H\n",file);
fprintf (outfile,
"#define %s_BPP 8\n"
"extern const unsigned short %s_w;\n"
"extern const unsigned short %s_h;\n"
"extern const unsigned short %s_ncolors;\n"
"extern unsigned int %s_palette[];\n"
"extern unsigned char %s[];\n",
file,
file,file,file,file,file);
if (argc == 3)
{
/* then write out mask array */
fprintf(outfile,"extern unsigned char %s_mask[];\n",file);
}
fprintf (outfile,"#endif /* !defined(INCLUDED_%s_H)*/\n",file);
fclose(outfile);
} /* end if bmi.bmiHeader.biBitCount == 8 */
/************************************/
/* start of handling 4 bit bmp file */
/************************************/
else if (bmi.bmiHeader.biBitCount == 4)
{
RGBQUAD * palette;
const RGBQUAD black = {0,0,0,0};
/* read 16-color bitmap file info*/
/* fix clrUsed field in case it is 0 (all colors) */
if (bmi.bmiHeader.biClrUsed == 0)
{
bmi.bmiHeader.biClrUsed = 1<<bmi.bmiHeader.biBitCount;
}
/* read the palette */
palette = (RGBQUAD *)malloc(bmi.bmiHeader.biClrUsed*sizeof(*palette));
if (palette == NULL)
{
fprintf(stderr,"%s: out of memory allocating palette\n",argv[0]);
fclose(infile);
free(bmp);
exit(-1);
}
if (fread(palette, sizeof(*palette), bmi.bmiHeader.biClrUsed,
infile) != bmi.bmiHeader.biClrUsed)
{
fprintf(stderr,"%s: error reading palette data\n",argv[0]);
fclose(infile);
free(bmp);
free(palette);
exit(-1);
}
bytes_read += ( bmi.bmiHeader.biClrUsed * sizeof(*palette));
if (bytes_read != bf.bfOffBits)
{
fprintf(stderr,"%s: wrong number of bytes read\n",argv[0]);
fclose(infile);
free(bmp);
free(palette);
exit(-1);
}
bytes_read = fread(bmp,sizeof(bmp[0]),bmpsize,infile);
if (bytes_read != bmpsize)
{
fprintf(stderr,"%s: cannot read bitmap data\n",argv[0]);
fclose(infile);
free(bmp);
free(palette);
exit(-1);
}
fclose(infile);
_splitpath(argv[1],drive,dir,file,ext);
strcpy(of_name,file);
strcat(of_name,".c");
outfile = fopen(of_name,"w");
if (outfile == NULL)
{
fprintf(stderr,"%s: cannot open output file %s\n",argv[0],of_name);
free(bmp);
free(palette);
exit(-1);
}
/*
compute bytes_per_line so that each line ends on an aligned,
4-byte boundary.
*/
bytes_per_line = (((DWORD)bmi.bmiHeader.biWidth *
(DWORD)bmi.bmiHeader.biBitCount + 31L) / 32L) * 4L;
fprintf (outfile,"/*\nThis file, %s, was automatically generated from"
" file\n %s \nusing utility\n %s $Revision: 1.1 $.\n"
"Please do not edit it.\n*/\n",of_name,argv[1],argv[0]);
if (argc == 2)
{
fprintf (outfile,
"\n\n/*\Below is a set of five variables: %s_w, the width,\n"
"%s_h, the height, %s_palette[], an array of palette\n"
"entries, %s_ncolors, the number of palette entries,\n"
" and the array %s[], an array of indices into the\n"
"palette array\n*/\n" ,file,file,file,file,file);
}
else
{
fprintf (outfile,
"\n\n/*\Below is a set of six variables: %s_w, the width,\n"
"%s_h, the height,"
"%s_palette[], an array of palette\n",
file,file,file);
fprintf (outfile,
"entries, %s_ncolors, the number of palette entries,\n"
"the array %s[], an array of indices into the palette array,\n"
"and %s_mask[], an array of mask bits (for sprites)\n*/\n"
,file,file,file);
}
//fprintf (outfile,"#include <ugl/ugl.h>\n");
fprintf (outfile, "const unsigned short %s_w = %d;\n",
file,bmi.bmiHeader.biWidth);
fprintf (outfile, "const unsigned short %s_h = %d;\n",
file,bmi.bmiHeader.biHeight);
fprintf (outfile,"const unsigned short %s_ncolors = %d;\n",
file,bmi.bmiHeader.biClrUsed);
fprintf (outfile, "unsigned int %s_palette[] = {",
file,bmi.bmiHeader.biClrUsed, file);
/* write out all but the last palette entry */
k = 0;
for (i = 0; i< bmi.bmiHeader.biClrUsed-1; i++)
{
if ( (k%5) == 0)
{
fprintf (outfile,"\n");
k = 1;
}
else
{
++k;
}
fprintf (outfile,"0x%08x, ",
UGL_MAKE_RGB(
palette[i].rgbRed,
palette[i].rgbGreen,
palette[i].rgbBlue) );
}
/* last line of palette*/
fprintf (outfile,"\n0x%08x\n};\n\n",
UGL_MAKE_RGB(
palette[i].rgbRed,
palette[i].rgbGreen,
palette[i].rgbBlue) );
/* write out bitmap */
fprintf (outfile,
"unsigned char %s[] = {",
file);
/* write the bitmap out; ignore padding bytes; origin upper left */
k = 0;
for (i = bmi.bmiHeader.biHeight-1; i > 0 ; i--)
{
for (j=0; j < bmi.bmiHeader.biWidth; j++)
{
if ( (k % 10) == 0)
{
k = 1;
fprintf (outfile,"\n");
}
else
{
++k;
}
fprintf (outfile,"0x%02x, ",bmp[i*bytes_per_line + j]);
}
}
/* last line */
for (j=0; j < bmi.bmiHeader.biWidth-1; j++)
{
if ( (k % 10) == 0)
{
fprintf (outfile,"\n");
}
++k;
fprintf (outfile,"0x%02x, ",bmp[i*bytes_per_line + j]);
}
/* last byte */
fprintf (outfile,"0x%02x\n};\n\n",bmp[i*bytes_per_line + j]);
if (argc == 3)
{
unsigned char mask;
int n;
/* then write out mask array */
fprintf(outfile,"unsigned char %s_mask[] = {",file);
k = 0;
for (i = bmi.bmiHeader.biHeight-1; i > 0 ; i--)
{
for (j=0; j < bmi.bmiHeader.biWidth;)
{
if ( (k % 10) == 0)
{
k = 1;
fprintf (outfile,"\n");
}
else
{
++k;
}
mask = 0;
for (n=7;n>=0 && j < bmi.bmiHeader.biWidth;n--,j++)
{
mask |= (memcmp(palette+bmp[i*bytes_per_line + j],
&black,sizeof(black) ) == 0 ? 0:1) << n;
}
fprintf (outfile,"0x%02x,", mask);
}
}
/* last line */
for (j=0; j < bmi.bmiHeader.biWidth;)
{
if ( (k % 10) == 0)
{
k = 1;
fprintf (outfile,"\n");
}
else
{
++k;
}
mask = 0;
for (n=7;n>=0 && j < bmi.bmiHeader.biWidth;n--,j++)
{
mask |= (memcmp(palette+bmp[i*bytes_per_line + j],
&black,sizeof(black) ) == 0 ? 0:1) << n;
}
if (j == bmi.bmiHeader.biWidth)
{
fprintf (outfile,"0x%02x\n};\n\n", mask);
}
else
{
fprintf (outfile,"0x%02x,", mask);
}
}
}
fclose(outfile);
free(palette);
/* write the .h file */
strcpy(of_name,file);
strcat(of_name,".h");
outfile = fopen(of_name,"w");
if (outfile == NULL)
{
fprintf(stderr,"%s: cannot open output file %s\n",argv[0],of_name);
free(bmp);
exit(-1);
}
fprintf (outfile,"/*\nThis file, %s, was automatically generated from"
" file\n %s \nusing utility\n %s $Revision: 1.1 $.\n"
"Please do not edit it.\n*/\n",of_name,argv[1],argv[0]);
fprintf (outfile,"#if !defined(INCLUDED_%s_H)\n",file);
fprintf (outfile,"#define INCLUDED_%s_H\n",file);
fprintf (outfile,
"#define %s_BPP 8\n"
"extern const unsigned short %s_w;\n"
"extern const unsigned short %s_h;\n"
"extern const unsigned short %s_ncolors;\n"
"extern unsigned int %s_palette[];\n"
"extern unsigned char %s[];\n",
file,
file,file,file,file,file);
if (argc == 3)
{
/* then write out mask array */
fprintf(outfile,"extern unsigned char %s_mask[];\n",file);
}
fprintf (outfile,"#endif /* !defined(INCLUDED_%s_H)*/\n",file);
fclose(outfile);
} /* end if bmi.bmiHeader.biBitCount == 8 */
else
{
fprintf(stderr,"%s: unrecognized bitmap format\n\tbiBitCount==%d\n",
argv[0],
bmi.bmiHeader.biBitCount);
}
/* done processing*/
free(bmp);
return 0;
} /* end if (infile) */
fprintf(stderr,"%s: UNABLE to open the input file (file must be 8.3 format) \n",file);
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -