📄 makecfrombmpdlg.cpp
字号:
MessageBox("\nDestination file open error"); /*提示打开目的文件不成功*/
return;
}
//写文件说明头
fprintf (fpd,"\n\n\n// 这是个简单的将bmp格式的位图数据转换为C语言格式的程序,本来不想写的,");
fprintf (fpd,"\n//在网上找了半天,只找到个要注册的版本,用不来。没法了,逼着我这头老牛上树");
fprintf (fpd,"\n//用我那蹩脚的C配上蹩脚的e文写了这程序,希望对后来者有所帮助。");
fprintf (fpd,"\n\n//用法:");
fprintf (fpd,"\n//先用大眼睛ACDsee将图片处理成需要的长宽和颜色数,保存成 *.bmp 格式,不要压缩,");
fprintf (fpd,"\n//将bmp2code.exe 和生成的 *.bmp 拷贝到同一个目录下,在dos下执行如下命令:");
fprintf (fpd,"\n//bmp2code abc.bmp def.c 2 回车");
fprintf (fpd,"\n// abc.bmp --- 为ACDsee生成的bmp位图");
fprintf (fpd,"\n// def.c --- 为本文件");
fprintf (fpd,"\n// 2 --- 为1、2、4 时为本文件中数据的字节数,3时产生图标用数据,5时数据压缩!!其他无效");
fprintf (fpd,"\n// 如:1-- 0x3F , 2-- 0x3F3F ...等");
fprintf (fpd,"\n// 当然了,文件名 abc.bmp 和 def.c 可以更改的,我只测试过4bit 16灰,320x240。呵呵。");
fprintf (fpd,"\n//有机会大家多多交流!:hwjchina@sohu.com");
fprintf (fpd,"\n//20060923该程序被Martal用VC6改编为MFC程序,欢迎指教:martal@21cn.com");
/*位图文件(*.bmp)转换为C代码文件(*.c)*/
//判断是否为bmp图
fgets(ctmp, 3, fps); //从流中读取一字符串
if (strcmp (ctmp,"BM")) //比较字符串
{
MessageBox("该文件并非 bitmap 文件!");
exit_pro(); //退出程序的准备
MessageBox("操作失败");
return;
}
//读入文件长度
f_tmp = getw(fps); //从流中读取一个整数
if (f_tmp<0)
{
f_len=f_tmp+65536;
}
else
{
f_len=f_tmp;
}
f_tmp = getw(fps);
if ((f_tmp<0)||(f_tmp>16))
{
MessageBox("文件过大!");
exit_pro();
return;
}
f_len=f_len+f_tmp*65536;
if (f_len!=filesize(fps))
{
MessageBox("文件长度读取错误!");
exit_pro();
return;
}
//处理源文件头
//读偏址
if (fseek(fps, 0x0a, SEEK_SET)) //将文件指针定位到指定位置
{
MessageBox("设置读取文件指针失败!");
exit_pro();
return;
}
f_tmp=getw(fps);
f_pian=f_tmp;
f_len-=f_tmp; //产生位图文件实际大小。
//读图片长宽
if (fseek(fps, 0x12, SEEK_SET))
{
MessageBox("设置读取文件指针失败!");
exit_pro();
return;
}
f_tmp = getw(fps);
if (f_tmp<0)
{
MessageBox("读取图片的宽失败!");
exit_pro();
return;
}
else
{
f_kuan=f_tmp;
}
f_tmp = getw(fps);
if (f_tmp<0)
{
MessageBox("读取图片的长失败!");
exit_pro();
return;
}
else
{
f_chang=f_tmp;
}
//读颜色位数
if (fseek(fps, 0x1c, SEEK_SET))
{
MessageBox("文件颜色位数读取错误!");
exit_pro();
return;
}
f_se=fgetc(fps);
if (f_se > 8)
{
MessageBox("错误! 只能处理最大为256色的BMP图片!");
exit_pro();
return;
}
//判断数据正确性
if (f_len%unt != 0)
{
CString strtmp;
strtmp.Format ("unit: %d error!",unt);
MessageBox(strtmp);
exit_pro();
return;
}
//读调色表
if (fseek(fps, 0x32, SEEK_SET))
{
MessageBox("读调色表错误!");
exit_pro();
return;
}
f_tmp = getw(fps);
if (f_tmp == 0)
{
f_tmp = 1<<f_se;
}
if (f_tmp > 256)
{
MessageBox("错误! 只能处理最大为256色的BMP图片!");
exit_pro();
return;
}
if (fseek(fps, 0x36, SEEK_SET))
{
MessageBox("读调色表错误!");
exit_pro();
return;
}
for (i=0; i<f_tmp; i++)
{
se_tmp[0] = fgetc(fps);
se_tmp[1] = fgetc(fps);
se_tmp[2] = fgetc(fps);
se_tmp[3] = fgetc(fps);
if (f_se == 8)
{
se_list[i] = (int)((se_tmp[0]&0xff)/64 + (((se_tmp[1]&0xff)/32)<<2) + (((se_tmp[2]&0xff)/32)<<5));
if (se_list[i] == 0xf0) se_list[i] = 0xf1; //将色0xf0挪作标记用,所有0xf0色用oxf1代替。
}
if (f_se == 4)
{
se_list[i] = (se_tmp[0] + se_tmp[1] + se_tmp[2]) / 3 ;
}
}
//写C文件头
fprintf (fpd,"\n\n\n// HH HH W W W JJJJJJJJ");
fprintf (fpd,"\n// H H W W W J");
fprintf (fpd,"\n// H H W W W J");
fprintf (fpd,"\n// HHHHHHH W W W J");
fprintf (fpd,"\n// H H W W W J");
fprintf (fpd,"\n// H H W W W J");
fprintf (fpd,"\n// H H W W J J");
fprintf (fpd,"\n// HHH HHH W W JJJ\n");
fprintf (fpd,"\n\n// ----------2004.06.12 HWJ");
fprintf (fpd,"\n// ----------2006.09.23 Martal\n\n");
fprintf (fpd,"\n\n\n// picture size: 宽width X 高high = %d X %d ",f_kuan,f_chang);
fprintf (fpd,"\n// colour: %dbit",f_se);
//转换bmp数据到c文件
if (fseek(fps, f_pian, SEEK_SET))
{
printf("\n read %s error! convert fail!",s_file);
exit_pro();
return;
}
//**************************************************
unt = untbuf;
if (unt==1)
{
if (f_se <8)
{
fprintf (fpd,"\n// 整屏用数据\n");
fprintf (fpd,"\nconst %s %s[%ld] = \n {",unt_c[unt],d_file,f_len);
fprintf (fpd,"\n ");
buf_i[1]=fgetc(fps);
j=0;
k=1;
l=2;
for (i=0;i<f_len;i++)
{
if (j==24)
{
fprintf (fpd,"\n ");
j=0;
}
if (k==4)
{
for (l=4;l>0;l--)
{
if (!((i==3)&&(l==4)))
if ((l%unt)==0)
fprintf(fpd,",");
if ((l%unt)==0) fprintf(fpd,"0x");
fprintf(fpd,"%s",chr[buf_i[l]]);
}
k=0;
l=1;
}
buf_i[l]=fgetc(fps);
j++;
k++;
l++;
}
fprintf (fpd,"\n };\n");
}
else if (f_se == 8)//**********************************************************
{
fprintf (fpd,"\n// 整屏用数据\n");
fprintf (fpd,"\n#include \"def.h\"");
CString tmpFileName;
char *d_filetmp;
tmpFileName.Format ("%s",d_file);
tmpFileName = tmpFileName.Left(tmpFileName.GetLength()-2);
d_filetmp = tmpFileName.GetBuffer(tmpFileName.GetLength());
fprintf (fpd,"\nconst INT8U %s[%ld + 6] = \n {",d_filetmp,f_len);
fprintf(fpd,"//X低8位,X高8位,Y低8位,Y高8位,颜色低8位,颜色高8位\n ");
fprintf (fpd,"0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,",f_kuan&0xff,(f_kuan>>8)&0xff,f_chang&0xff,(f_chang>>8)&0xff,f_se&0xff,(f_se>>8)&0xff);
fprintf (fpd,"\n ");
buf_i[1]=fgetc(fps);
j=0;
k=1;
l=2;
for (i=0;i<f_len;i++)
{
if (j==24)
{
fprintf (fpd,"\n ");
j=0;
}
if (k==4)
{
for (l=0;l<4;l++)
{
if (!((i==3)&&(l==0)))
if ((l%unt)==0)
fprintf(fpd,",");
if ((l%unt)==0) fprintf(fpd,"0x");
fprintf(fpd,"%s",chr[se_list[buf_i[l+1]]]);
}
k=0;
l=1;
}
buf_i[l]=fgetc(fps);
j++;
k++;
l++;
}
fprintf (fpd,"\n };\n");
}
}
//************************************************************
if (unt==2)
{
fprintf(fpd,"// 暂时未写!!呵呵");
}
//************************************************************
if (unt==3)
{
if (f_se <8)
{
fprintf (fpd,"\n// 图标用数据\n");
fprintf (fpd,"\nconst %s %s[%ld] = \n {",unt_c[1],d_file,f_len*2+6);
fprintf(fpd,"//X低8位,X高8位,Y低8位,Y高8位,颜色低8位,颜色高8位\n ");
fprintf (fpd,"0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,",f_kuan&0xff,(f_kuan>>8)&0xff,f_chang&0xff,(f_chang>>8)&0xff,f_se&0xff,(f_se>>8)&0xff);
fprintf (fpd,"\n ");
buf_i[1]=fgetc(fps);
j=0;
k=1;
l=2;
for (i=0;i<f_len;i++)
{
if (j==24)
{
fprintf (fpd,"\n ");
j=0;
}
if (k==4)
{
for (l=4;l>0;l--)
{
if (!((i==3)&&(l==4)))
fprintf(fpd,",");
strcpy(tmp,chr[buf_i[5-l]]);
fprintf(fpd,"0x0%c,0x0%c",tmp[0],tmp[1]);
}
k=0;
l=1;
}
buf_i[l]=fgetc(fps);
j++;
k++;
l++;
}
fprintf (fpd,"\n };\n");
}
else if (f_se == 8)//**********************************************************
{
fprintf (fpd,"\n// 图标用数据\n");
fprintf (fpd,"\nconst %s %s[%ld] = \n {",unt_c[1],d_file,f_len*2+6);
fprintf(fpd,"//X低8位,X高8位,Y低8位,Y高8位,颜色低8位,颜色高8位\n ");
fprintf (fpd,"0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,",f_kuan&0xff,(f_kuan>>8)&0xff,f_chang&0xff,(f_chang>>8)&0xff,f_se&0xff,(f_se>>8)&0xff);
fprintf (fpd,"\n ");
buf_i[1]=fgetc(fps);
j=0;
k=1;
l=2;
for (i=0;i<f_len;i++)
{
if (j==24)
{
fprintf (fpd,"\n ");
j=0;
}
if (k==4)
{
for (l=4;l>0;l--)
{
if (!((i==3)&&(l==4)))
if ((l%unt)==0)
fprintf(fpd,",");
if ((l%unt)==0) fprintf(fpd,"0x");
fprintf(fpd,"%s",chr[se_list[buf_i[l]]]);
}
k=0;
l=1;
}
buf_i[l]=fgetc(fps);
j++;
k++;
l++;
}
fprintf (fpd,"\n };\n");
}
}
//********************************************************
if (unt==4)
{
fprintf(fpd,"// 暂时未写!!呵呵");
}
//********************************************************
if (unt==5)
{
if (f_se <8)
{
fprintf(fpd,"// 暂时未写!!呵呵");
}
else if (f_se == 8)//**********************************************************
{
fprintf (fpd,"\n// 整屏用压缩数据\n");
fprintf (fpd,"\nconst %s %s[ ] = //%ld + 6\n {",unt_c[unt],d_file,f_len);
fprintf(fpd,"//X低8位,X高8位,Y低8位,Y高8位,颜色低8位,颜色高8位\n ");
fprintf (fpd,"0x%x,0x%x,0x%x,0x%x,0x%x,0x%x",f_kuan&0xff,(f_kuan>>8)&0xff,f_chang&0xff,(f_chang>>8)&0xff,f_se&0xff,(f_se>>8)&0xff);
fprintf (fpd,"\n ");
buf_char[6]=0;
buf_CompCnt[7] = 0;
buf_CompCnt[8] = 0;
CompCnt =0;
j=0;
for (i=0;i<(f_len);i++)
{
buf_char[0]=buf_char[1];
buf_char[1]=buf_char[2];
buf_char[2]=buf_char[3];
buf_char[3]=buf_char[4];
buf_char[4]=buf_char[5];
buf_char[5]=fgetc(fps);
if (buf_char[6] <5)
{
buf_char[6]++;
continue;
}
buf_char[6] = 6;
//判断单个的重复数据
if ( (se_list[buf_char[0]]==se_list[buf_char[1]]) &&
(se_list[buf_char[1]]==se_list[buf_char[2]]) &&
(se_list[buf_char[2]]==se_list[buf_char[3]]) &&
(se_list[buf_char[3]]==se_list[buf_char[4]]) &&
(buf_CompCnt[8]==0)
)
{
buf_CompCnt[7]++;
buf_CompCnt[8] = 0;
if (buf_CompCnt[7] <246)
{
buf_CompCnt[7]++;
}
else
{
PrintSingle(); //转下一个处理
buf_CompCnt[7] =0;
buf_char[6] =0;
}
continue;
}
else
{
if (buf_CompCnt[7] != 0)
{
PrintSingle();
buf_CompCnt[7] =0;
buf_char[6] =1;
continue;
}
}
//判断二个的重复数据
if ( (se_list[buf_char[0]]==se_list[buf_char[2]]) &&
(se_list[buf_char[1]]==se_list[buf_char[3]]) &&
(se_list[buf_char[2]]==se_list[buf_char[4]]) &&
(se_list[buf_char[3]]==se_list[buf_char[5]]) &&
(buf_CompCnt[7]==0)
)
{
buf_CompCnt[8]++;
buf_CompCnt[7] = 0;
if (buf_CompCnt[8] <491)
{
buf_CompCnt[8]++;
}
else
{
PrintDual(); //转下一个处理
buf_CompCnt[8] =0;
buf_char[6] =0;
}
continue;
}
else
{
if (buf_CompCnt[8] != 0)
{
PrintDual();
buf_CompCnt[8] =0;
buf_char[6] =(char)(buf_CompCnt[8]+1)%2 +1;
continue;
}
}
//没有重复数据
if (j==24)
{
fprintf (fpd,"\n ");
j=0;
}
j++;
fprintf(fpd,",");
fprintf(fpd,"0x");
fprintf(fpd,"%s",chr[se_list[buf_char[0]]]);
CompCnt++;
k++;
l++;
}
//处理剩下的数据
for(;buf_char[6]>0;buf_char[6]--)
{
fprintf(fpd,",");
fprintf(fpd,"0x");
fprintf(fpd,"%s",chr[se_list[buf_char[6-buf_char[6]]]]);
CompCnt++;
}
fprintf (fpd,"\n }; //All data number =%ld + 6\n",CompCnt);
}
}
exit_pro();
CString Waring;
Waring = FileName;
Waring += " to ";
Waring += D_file;
Waring += " 成功转换!";
MessageBox(Waring);
}
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_ShowMsg="这是个简单的将bmp格式的位图数据转换为C语言格式的程序,本来不想写的,\r\n\
在网上找了半天,只找到个要注册的版本,用不来。没法了,逼着我这头老牛上树\r\n\
用我那蹩脚的C配上蹩脚的e文写了这程序,希望对后来者有所帮助。\r\n\
用法:\r\n\
先用大眼睛ACDsee将图片处理成需要的长宽和颜色数,保存成 *.bmp 格式,不要压缩,\r\n\
将bmp2code.exe 和生成的 *.bmp 拷贝到同一个目录下,在dos下执行如下命令:\r\n\
bmp2code abc.bmp def.c 2 回车\r\n\
abc.bmp --- 为ACDsee生成的bmp位图\r\n\
def.c --- 为本文件\r\n\
2 --- 为1、2、4 时为本文件中数据的字节数,3时产生图标用数据,5时\r\n\
数据压缩!!其他无效\r\n\
\r\n\
如:1-- 0x3F , 2-- 0x3F3F ...等\r\n\
当然了,文件名 abc.bmp 和 def.c 可以更改的,我只测试过4bit 16灰,320x240。呵呵。\r\n\
有机会大家多多交流!:hwjchina@sohu.com\r\n\
以上为 HWJ 原文\r\n\
2006年9月23日该程序被Martal用VC6改编为MFC程序,欢迎指教:martal@21cn.com";
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CMakeCFromBmpDlg::OnButton2() //说明
{
CAboutDlg dlgabout;
dlgabout.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -