⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmp128_64.cpp

📁 使用PIC18F系列配合128*64 LCM,PC端辅助软体,用C++写成,将bmp档转成二进制档
💻 CPP
字号:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

long filelength(FILE *);
int main(int argc,char **argv)
{
    FILE *bmp,*w_file,*w_file1;
    long size,start_addr,x,y,temp;
    int i,j,q,t,k;
    unsigned char uchar_temp1;
    //int buf[40][8];
    union bit_var
    {
     struct
     {
      unsigned bit0:1;
      unsigned bit1:1;
      unsigned bit2:1;
      unsigned bit3:1;
      unsigned bit4:1;
      unsigned bit5:1;
      unsigned bit6:1;
      unsigned bit7:1;
     };
     unsigned char uchar;
    };
    bit_var uchar_temp,uchar_array_temp[8],buf[40][8];
    if(argc<2)
    {
     printf("Usage : BMPINFO filename\n");
     return(1);
    }
    if((bmp=fopen(argv[1],"rb")) == NULL)
    {
     printf("Cannot open file !\n");
     return(2);
    }
    else
    {
     printf("Open file success!\n");
        if(argc==3)
            w_file=fopen(argv[2],"w");
        else
            w_file=fopen("temp","w");
    }
    size=filelength(bmp);
    printf("%s=%d bytes\n",argv[1],size);
    if(fseek(bmp,10,SEEK_SET) == 0)
    {
     printf("image start address=%d\n",start_addr=getc(bmp));   //image address of file
//     fseek(bmp,start_addr,SEEK_SET);
    }
    x=0;
    y=0;
    for(i=4;i>0;i--)
    {
     if(fseek(bmp,0x11+i,SEEK_SET) == 0)
     {
      temp=getc(bmp);
      x=x + temp<<((i-1)*8);
//      printf("%d %d\n",temp,x);
     }
     if(fseek(bmp,0x15+i,SEEK_SET) == 0)
     {
      temp=getc(bmp);
      y=y + temp<<((i-1)*8);
//      printf("%d %d\n",temp,y);
     }
    }
    printf("image width=%d\n",x);
    printf("image height=%d\n",y);
    t=x/8;
    if(x%8!=0)
        t++;
    q=(x/32)*4;
    if(x%32!=0)
        q=q+4;
    printf("{");
    fprintf(w_file,"{");
    k=0;
    w_file1=fopen("temp128_64","w");
    for(i=y;i>0;i--)    //240
    {
        for(j=0;j<t;j++) //40
        {
            if(fseek(bmp,q*(i-1) + start_addr + j,SEEK_SET) == 0)
            {
                temp=getc(bmp);
                uchar_temp1=(unsigned char)temp;
                uchar_temp1=0xff-uchar_temp1;
                if(j==t-1 && i==1)
                {
                    printf("0x%02x}",uchar_temp1);    //getc(bmp));
                    fprintf(w_file,"0x%02x}",uchar_temp1);//getc(bmp));
                }
                else
                {
                    printf("0x%02x,",uchar_temp1);    //getc(bmp));
                    fprintf(w_file,"0x%02x,",uchar_temp1);//getc(bmp));
                }
                buf[j][k].uchar=uchar_temp1;//(unsigned char)temp;
            }
        }
        k++;
        if(k==8)
        {
         for(j=0;j<t;j++)
         {
          uchar_array_temp[0]=buf[j][0];
          uchar_array_temp[1]=buf[j][1];
          uchar_array_temp[2]=buf[j][2];
          uchar_array_temp[3]=buf[j][3];
          uchar_array_temp[4]=buf[j][4];
          uchar_array_temp[5]=buf[j][5];
          uchar_array_temp[6]=buf[j][6];
          uchar_array_temp[7]=buf[j][7];
          uchar_temp.bit0=uchar_array_temp[0].bit7;
          uchar_temp.bit1=uchar_array_temp[1].bit7;
          uchar_temp.bit2=uchar_array_temp[2].bit7;
          uchar_temp.bit3=uchar_array_temp[3].bit7;
          uchar_temp.bit4=uchar_array_temp[4].bit7;
          uchar_temp.bit5=uchar_array_temp[5].bit7;
          uchar_temp.bit6=uchar_array_temp[6].bit7;
          uchar_temp.bit7=uchar_array_temp[7].bit7;
          fprintf(w_file1,"0x%02x,",uchar_temp.uchar);
          uchar_temp.bit0=uchar_array_temp[0].bit6;
          uchar_temp.bit1=uchar_array_temp[1].bit6;
          uchar_temp.bit2=uchar_array_temp[2].bit6;
          uchar_temp.bit3=uchar_array_temp[3].bit6;
          uchar_temp.bit4=uchar_array_temp[4].bit6;
          uchar_temp.bit5=uchar_array_temp[5].bit6;
          uchar_temp.bit6=uchar_array_temp[6].bit6;
          uchar_temp.bit7=uchar_array_temp[7].bit6;
          fprintf(w_file1,"0x%02x,",uchar_temp.uchar);
          uchar_temp.bit0=uchar_array_temp[0].bit5;
          uchar_temp.bit1=uchar_array_temp[1].bit5;
          uchar_temp.bit2=uchar_array_temp[2].bit5;
          uchar_temp.bit3=uchar_array_temp[3].bit5;
          uchar_temp.bit4=uchar_array_temp[4].bit5;
          uchar_temp.bit5=uchar_array_temp[5].bit5;
          uchar_temp.bit6=uchar_array_temp[6].bit5;
          uchar_temp.bit7=uchar_array_temp[7].bit5;
          fprintf(w_file1,"0x%02x,",uchar_temp.uchar);
          uchar_temp.bit0=uchar_array_temp[0].bit4;
          uchar_temp.bit1=uchar_array_temp[1].bit4;
          uchar_temp.bit2=uchar_array_temp[2].bit4;
          uchar_temp.bit3=uchar_array_temp[3].bit4;
          uchar_temp.bit4=uchar_array_temp[4].bit4;
          uchar_temp.bit5=uchar_array_temp[5].bit4;
          uchar_temp.bit6=uchar_array_temp[6].bit4;
          uchar_temp.bit7=uchar_array_temp[7].bit4;
          fprintf(w_file1,"0x%02x,",uchar_temp.uchar);
          uchar_temp.bit0=uchar_array_temp[0].bit3;
          uchar_temp.bit1=uchar_array_temp[1].bit3;
          uchar_temp.bit2=uchar_array_temp[2].bit3;
          uchar_temp.bit3=uchar_array_temp[3].bit3;
          uchar_temp.bit4=uchar_array_temp[4].bit3;
          uchar_temp.bit5=uchar_array_temp[5].bit3;
          uchar_temp.bit6=uchar_array_temp[6].bit3;
          uchar_temp.bit7=uchar_array_temp[7].bit3;
          fprintf(w_file1,"0x%02x,",uchar_temp.uchar);
          uchar_temp.bit0=uchar_array_temp[0].bit2;
          uchar_temp.bit1=uchar_array_temp[1].bit2;
          uchar_temp.bit2=uchar_array_temp[2].bit2;
          uchar_temp.bit3=uchar_array_temp[3].bit2;
          uchar_temp.bit4=uchar_array_temp[4].bit2;
          uchar_temp.bit5=uchar_array_temp[5].bit2;
          uchar_temp.bit6=uchar_array_temp[6].bit2;
          uchar_temp.bit7=uchar_array_temp[7].bit2;
          fprintf(w_file1,"0x%02x,",uchar_temp.uchar);
          uchar_temp.bit0=uchar_array_temp[0].bit1;
          uchar_temp.bit1=uchar_array_temp[1].bit1;
          uchar_temp.bit2=uchar_array_temp[2].bit1;
          uchar_temp.bit3=uchar_array_temp[3].bit1;
          uchar_temp.bit4=uchar_array_temp[4].bit1;
          uchar_temp.bit5=uchar_array_temp[5].bit1;
          uchar_temp.bit6=uchar_array_temp[6].bit1;
          uchar_temp.bit7=uchar_array_temp[7].bit1;
          fprintf(w_file1,"0x%02x,",uchar_temp.uchar);
          uchar_temp.bit0=uchar_array_temp[0].bit0;
          uchar_temp.bit1=uchar_array_temp[1].bit0;
          uchar_temp.bit2=uchar_array_temp[2].bit0;
          uchar_temp.bit3=uchar_array_temp[3].bit0;
          uchar_temp.bit4=uchar_array_temp[4].bit0;
          uchar_temp.bit5=uchar_array_temp[5].bit0;
          uchar_temp.bit6=uchar_array_temp[6].bit0;
          uchar_temp.bit7=uchar_array_temp[7].bit0;
          fprintf(w_file1,"0x%02x,\n",uchar_temp.uchar);
         }
         k=0;
        }
        printf("\n");
        fprintf(w_file,"\n");
    }
    //printf("}");
    //fprintf(w_file,"}");
    fclose(bmp);
    fclose(w_file);
    fclose(w_file1);
//      system("PAUSE");
      return 0;
}
long filelength(FILE *stream)
{
 long cur,length;
 cur=ftell(stream);
 fseek(stream,0,SEEK_END);
 length=ftell(stream);
 fseek(stream,cur,SEEK_SET);
 return(length);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -