BMP位图转变为16进制
1 单色BMP位图变为16进制
/**/
/*小于112x64的任意单色BMP位图文件转换成C代码格式
转换后的点阵排列顺序:
.............. 896
..............
113 114 115 ... 224
1 2 3 ... 112
*/
#include "stdio.h"
int main(void)
{...} {
FILE *fp,*fout;
unsigned short t,widthbmp,heightbmp; //bmp store width and height
unsigned char ch1,ch2,width,height,heightlcd;
unsigned char image[1024];
unsigned char temp[7168];
unsigned char lcd[896];
int i,j,k;
if((fp=fopen("screen.bmp","rb"))==NULL)
{
printf("Can't open screen.bmp ");
return 0;
}
if((fout=fopen("screen.h","wt"))==NULL)
{...} {
printf("Can't open screen.h ");
return 0;
}
ch1=fgetc(fp);
ch2=fgetc(fp);
if((ch1!='B')||(ch2!='M'))
{...} {
printf("Not BMP file ");
return 0;
}
fseek(fp,0x0e,0);
ch1=fgetc(fp);
if(ch1!=0x28)
{...} {
printf("Not windows BMP ");
return 0;
}
fseek(fp,0x12,0);
width=fgetc(fp);
if(width%8==0)
t=width/8;
else
t=width/8+1;
if(t%4==0)
widthbmp=t;
else
widthbmp=(t+4-(t%4));
fseek(fp,0x16,0);
height=fgetc(fp);
if(height%8==0)
{...} {
heightbmp=height;
heightlcd=height/8;
}
else
{...} {
heightbmp=(height/8+1)*8;
heightlcd=height/8+1;
}
fseek(fp,0x1c,0);
ch1=fgetc(fp);
if(ch1!=0x01)
{...} {
printf("Not a Black-White image file ");
return 0;
}
fseek(fp,0x0a,0);
ch1=fgetc(fp);
fseek(fp,ch1,0);
fread(image,1024,1,fp);
/**/ /*这是我在工程中转换为横向扫描方式的代码
for(int h=31;h>=0;h--)
for(int m=24;m>0;m--)
image1[(31-h)*24+24-m]=image[h*24+24-m];
for(int h=31;h>=0;h--)
for(int m=24;m>0;m--)
image1[(31-h)*24+24-m]=image[h*24+24-m];
for(i=0;i<7168;i++) temp[i]=1;
for(j=0;j<height;j++)
for(i=0;i<t;i++)
for(k=0;k<8;k++)
temp[8*i+k+j*t*8]=(((image1[i+j*widthbmp]<<k)&0x80)>>7);
for(i=0;i<7168;i++)
temp[i]=(temp[i]-1)&1;
int n=0;
for(j=0;j<heightbmp;j++)
for(i=0;i<widthbmp;i++)
{
lcd[j*widthbmp+i]=0;
for(k=0;k<8;k++)
lcd[j*widthbmp+i]=lcd[j*widthbmp+i]+(temp[192*j+k+8*i]<<k);
}
LED为32*192
*/
for(i=0;i<7168;i++) temp[i]=1;
for(j=0;j<height;j++)
for(i=0;i<t;i++)
for(k=0;k<8;k++)
temp[8*i+k+j*t*8]=(((image[i+j*widthbmp]<<k)&0x80)>>7);
for(i=0;i<7168;i++)
temp[i]=(temp[i]-1)&1;
for(j=0;j<heightlcd;j++)
for(i=0;i<width;i++)
{...} {
lcd[j*width+i]=0;
for(k=0;k<8;k++)
lcd[j*width+i]=lcd[j*width+i]+(temp[i+k*t*8+j*8*t*8]<<k);
}
fprintf(fout,"//width=%d t=%d widthbmp=%d heightlcd=%d ",width,t,widthbmp,heightlcd);
fprintf(fout,"const unsigned char screen[%d]= {...} {%d,%d, ",width*heightlcd+2,width,heightlcd);
//swap bit order in byte
//these code used in handset only
/**//*for(i=0;i<t*heightbmp;i++)
{
ch1=0;
for(j=0;j<8;j++)
{
ch1=ch1+((lcd[i] & 1)<<(7-j));
lcd[i]=lcd[i]>>1;
}
lcd[i]=ch1;
}*/
//end swap
for(i=0;i<(heightlcd);i++)
{
for(j=0;j<width;j++)
fprintf(fout,"0x%x,",lcd[j+i*width]);
fprintf(fout," ");
}
fprintf(fout,"}; ");
printf("ok ");
fclose(fp);
return 1;
转换后的点阵排列顺序:
.............. 896
..............
113 114 115 ... 224
1 2 3 ... 112
*/
#include "stdio.h"
int main(void)
{...} {
FILE *fp,*fout;
unsigned short t,widthbmp,heightbmp; //bmp store width and height
unsigned char ch1,ch2,width,height,heightlcd;
unsigned char image[1024];
unsigned char temp[7168];
unsigned char lcd[896];
int i,j,k;
if((fp=fopen("screen.bmp","rb"))==NULL)
{
printf("Can't open screen.bmp ");
return 0;
}
if((fout=fopen("screen.h","wt"))==NULL)
{...} {
printf("Can't open screen.h ");
return 0;
}
ch1=fgetc(fp);
ch2=fgetc(fp);
if((ch1!='B')||(ch2!='M'))
{...} {
printf("Not BMP file ");
return 0;
}
fseek(fp,0x0e,0);
ch1=fgetc(fp);
if(ch1!=0x28)
{...} {
printf("Not windows BMP ");
return 0;
}
fseek(fp,0x12,0);
width=fgetc(fp);
if(width%8==0)
t=width/8;
else
t=width/8+1;
if(t%4==0)
widthbmp=t;
else
widthbmp=(t+4-(t%4));
fseek(fp,0x16,0);
height=fgetc(fp);
if(height%8==0)
{...} {
heightbmp=height;
heightlcd=height/8;
}
else
{...} {
heightbmp=(height/8+1)*8;
heightlcd=height/8+1;
}
fseek(fp,0x1c,0);
ch1=fgetc(fp);
if(ch1!=0x01)
{...} {
printf("Not a Black-White image file ");
return 0;
}
fseek(fp,0x0a,0);
ch1=fgetc(fp);
fseek(fp,ch1,0);
fread(image,1024,1,fp);
/**/ /*这是我在工程中转换为横向扫描方式的代码
for(int h=31;h>=0;h--)
for(int m=24;m>0;m--)
image1[(31-h)*24+24-m]=image[h*24+24-m];
for(int h=31;h>=0;h--)
for(int m=24;m>0;m--)
image1[(31-h)*24+24-m]=image[h*24+24-m];
for(i=0;i<7168;i++) temp[i]=1;
for(j=0;j<height;j++)
for(i=0;i<t;i++)
for(k=0;k<8;k++)
temp[8*i+k+j*t*8]=(((image1[i+j*widthbmp]<<k)&0x80)>>7);
for(i=0;i<7168;i++)
temp[i]=(temp[i]-1)&1;
int n=0;
for(j=0;j<heightbmp;j++)
for(i=0;i<widthbmp;i++)
{
lcd[j*widthbmp+i]=0;
for(k=0;k<8;k++)
lcd[j*widthbmp+i]=lcd[j*widthbmp+i]+(temp[192*j+k+8*i]<<k);
}
LED为32*192
*/
for(i=0;i<7168;i++) temp[i]=1;
for(j=0;j<height;j++)
for(i=0;i<t;i++)
for(k=0;k<8;k++)
temp[8*i+k+j*t*8]=(((image[i+j*widthbmp]<<k)&0x80)>>7);
for(i=0;i<7168;i++)
temp[i]=(temp[i]-1)&1;
for(j=0;j<heightlcd;j++)
for(i=0;i<width;i++)
{...} {
lcd[j*width+i]=0;
for(k=0;k<8;k++)
lcd[j*width+i]=lcd[j*width+i]+(temp[i+k*t*8+j*8*t*8]<<k);
}
fprintf(fout,"//width=%d t=%d widthbmp=%d heightlcd=%d ",width,t,widthbmp,heightlcd);
fprintf(fout,"const unsigned char screen[%d]= {...} {%d,%d, ",width*heightlcd+2,width,heightlcd);
//swap bit order in byte
//these code used in handset only
/**//*for(i=0;i<t*heightbmp;i++)
{
ch1=0;
for(j=0;j<8;j++)
{
ch1=ch1+((lcd[i] & 1)<<(7-j));
lcd[i]=lcd[i]>>1;
}
lcd[i]=ch1;
}*/
//end swap
for(i=0;i<(heightlcd);i++)
{
for(j=0;j<width;j++)
fprintf(fout,"0x%x,",lcd[j+i*width]);
fprintf(fout," ");
}
fprintf(fout,"}; ");
printf("ok ");
fclose(fp);
return 1;
2. 24真彩色BMP位图变为16进制
#include "stdio.h"
int main(void)
{...} {
FILE *fp,*fout;
int i,j;
unsigned short width,height,widthbyte;
unsigned short temp0,temp1,temp2;
unsigned char ch1,ch2;
unsigned char image[116160]; /**//*the sizes of 220*176 Bytes*/
unsigned short buffer[220][172]; /**//*the lcd data*/
if((fp=fopen("tiger.bmp","rb"))==NULL) /**//*Is it exist?*/
{
printf(" Can't open BMP file!");
return 0;
}
ch1=fgetc(fp);
ch2=fgetc(fp);
if((ch1!='B')||(ch2!='M')) /**/ /*Is it BMP file?*/
{...} {
printf("It's Not BMP file! ");
return 0;
}
fseek(fp,0x1c,0);
ch1=fgetc(fp);
if(ch1!=0x18) /**/ /*Is it 24bit true color BMP?*/
{...} {
printf("It's Not 24bit true color BMP file! ");
return 0;
}
if((fout=fopen("tiger.h","wt"))==NULL) /**/ /*创建数据头文件*/
{...} {
printf("Can't create tiger.h file ");
return 0;
}
fseek(fp,0x12,0); /**/ /*get the width on 0x12*/
width=fgetc(fp);
fseek(fp,0x16,0); /**/ /*get the height on 0x16*/
height=fgetc(fp);
if((width>0xB0)||(height>0xDC)) /**/ /*Is it conform to the LCD(<=220*176)?*/
{...} {
printf("The size of BMP is too big! ");
return 0;
}
if((width*3%4)==0) /**/ /*every scan-line has N*4Bytes*/
widthbyte=width*3;
else
//widthbyte=width*3+(4-(width*3%4));
widthbyte=(width*3/4)*4+4;
fseek(fp,0x0a,0); /**/ /*get the offset of the imagedata */
ch1=fgetc(fp);
fseek(fp,ch1,0);
fread(image,1,116160,fp); /**/ /*read the imagedata to Array*/
for(i=0;i<220;i++) /**/ /*buffer initialization*/
{...} {
for(j=0;j<176;j++)
buffer[i][j]=0;
}
for(i=0;i<height;i++) /**/ /*write image data to buffer*/
{...} {
for(j=0;j<widthbyte;j+=3)
{
temp0=image[i*widthbyte+j]>>3;
temp1=image[i*widthbyte+j+1]>>2;
temp2=image[i*widthbyte+j+2]>>3;
buffer[i][j/3]=temp2;
buffer[i][j/3]+=temp1<<5;
buffer[i][j/3]+=temp0<<11;
}
}
fprintf(fout," /**/ /*The info of BMPDATA:width=%d height=%d*/ ",width,height);
fprintf(fout,"const unsigned short lcdbuffer[%d][%d]= {...} { ",width,height);
/**//*
这是我在工程中修改的代码 位图大小为32*`192 扫描方式为横向扫描
for(int m=0;m<32;m++)
{
for(int n=0;n<24;n++)
{
for(int l=0;l<8;l++)
{
buffer1[m][n*8+l]=buffer[31-m][n*8+7-l];
if(buffer1[m][n*8+l]>0) buffer1[m][n*8+l]=1;
else buffer1[m][n*8+l]=0;
}
}
}
for(j=0;j<32;j++)
for(i=0;i<24;i++)
{
lcd[j*24+i]=0;
for(int k=0;k<8;k++)
lcd[j*24+i]=lcd[j*24+i]+(buffer1[j][i*8+k]<<k);
}
*/
for(i=0;i<height;i++) /**//*putout the hex data to array*/
{
fprintf(fout,"{");
for(j=0;j<width;j++)
{
fprintf(fout,"0x%x,",buffer[i][j]);
if((j+1)%8==0)
fprintf(fout," ");
}
fprintf(fout,"} ");
}
fprintf(fout,"}; ");
fclose(fp);
fclose(fout);
return 1;
int main(void)
{...} {
FILE *fp,*fout;
int i,j;
unsigned short width,height,widthbyte;
unsigned short temp0,temp1,temp2;
unsigned char ch1,ch2;
unsigned char image[116160]; /**//*the sizes of 220*176 Bytes*/
unsigned short buffer[220][172]; /**//*the lcd data*/
if((fp=fopen("tiger.bmp","rb"))==NULL) /**//*Is it exist?*/
{
printf(" Can't open BMP file!");
return 0;
}
ch1=fgetc(fp);
ch2=fgetc(fp);
if((ch1!='B')||(ch2!='M')) /**/ /*Is it BMP file?*/
{...} {
printf("It's Not BMP file! ");
return 0;
}
fseek(fp,0x1c,0);
ch1=fgetc(fp);
if(ch1!=0x18) /**/ /*Is it 24bit true color BMP?*/
{...} {
printf("It's Not 24bit true color BMP file! ");
return 0;
}
if((fout=fopen("tiger.h","wt"))==NULL) /**/ /*创建数据头文件*/
{...} {
printf("Can't create tiger.h file ");
return 0;
}
fseek(fp,0x12,0); /**/ /*get the width on 0x12*/
width=fgetc(fp);
fseek(fp,0x16,0); /**/ /*get the height on 0x16*/
height=fgetc(fp);
if((width>0xB0)||(height>0xDC)) /**/ /*Is it conform to the LCD(<=220*176)?*/
{...} {
printf("The size of BMP is too big! ");
return 0;
}
if((width*3%4)==0) /**/ /*every scan-line has N*4Bytes*/
widthbyte=width*3;
else
//widthbyte=width*3+(4-(width*3%4));
widthbyte=(width*3/4)*4+4;
fseek(fp,0x0a,0); /**/ /*get the offset of the imagedata */
ch1=fgetc(fp);
fseek(fp,ch1,0);
fread(image,1,116160,fp); /**/ /*read the imagedata to Array*/
for(i=0;i<220;i++) /**/ /*buffer initialization*/
{...} {
for(j=0;j<176;j++)
buffer[i][j]=0;
}
for(i=0;i<height;i++) /**/ /*write image data to buffer*/
{...} {
for(j=0;j<widthbyte;j+=3)
{
temp0=image[i*widthbyte+j]>>3;
temp1=image[i*widthbyte+j+1]>>2;
temp2=image[i*widthbyte+j+2]>>3;
buffer[i][j/3]=temp2;
buffer[i][j/3]+=temp1<<5;
buffer[i][j/3]+=temp0<<11;
}
}
fprintf(fout," /**/ /*The info of BMPDATA:width=%d height=%d*/ ",width,height);
fprintf(fout,"const unsigned short lcdbuffer[%d][%d]= {...} { ",width,height);
/**//*
这是我在工程中修改的代码 位图大小为32*`192 扫描方式为横向扫描
for(int m=0;m<32;m++)
{
for(int n=0;n<24;n++)
{
for(int l=0;l<8;l++)
{
buffer1[m][n*8+l]=buffer[31-m][n*8+7-l];
if(buffer1[m][n*8+l]>0) buffer1[m][n*8+l]=1;
else buffer1[m][n*8+l]=0;
}
}
}
for(j=0;j<32;j++)
for(i=0;i<24;i++)
{
lcd[j*24+i]=0;
for(int k=0;k<8;k++)
lcd[j*24+i]=lcd[j*24+i]+(buffer1[j][i*8+k]<<k);
}
*/
for(i=0;i<height;i++) /**//*putout the hex data to array*/
{
fprintf(fout,"{");
for(j=0;j<width;j++)
{
fprintf(fout,"0x%x,",buffer[i][j]);
if((j+1)%8==0)
fprintf(fout," ");
}
fprintf(fout,"} ");
}
fprintf(fout,"}; ");
fclose(fp);
fclose(fout);
return 1;