用C语言解析BMP文件的结构
来源:优易学  2010-1-14 10:36:50   【优易学:中国教育考试门户网】   资料下载   IT书店

 

  //byte转为BIT
  void ByteToBit(char *Out, const char *In, unsigned char bits)
  {
  unsigned char i;
  for(i=0; i<bits; i++)
  {
  Out[i] = (In[i/8]>>(i%8)) & 1;
  }
  }
  //BIT转为byte
  void BitToByte(char * Out, const char *In, unsigned bits)
  {
  unsigned char i;
  memset(Out, 0, (bits+7)/8);
  for(i=0; i<bits; i++)
  {
  Out[i/8] |= In[i]<<(i%8);
  }
  }
  //把位图转为文件,输入bmp文件名和头文件名
  int bmp2headfile(char * bmpfile, char * headfile)
  {
  int infileLen; //文件长度
  int n=0,num=1; //n 字节计数器, NUM换行指示
  unsigned char c, c_in; //C_in文件字节,C转化
  FILE *fh_in;
  FILE *fh_out;
  char com[256]={0};
  char ch[2]={0};
  long wid, hig;
  assert((bmpfile != NULL) && (headfile != NULL));
  fh_in=fopen(bmpfile,"rb");
  if (NULL==fh_in)
  {
  printf("open read file error!!");
  return 1;
  }
  fseek(fh_in,0,SEEK_END);
  infileLen=ftell(fh_in);
  fseek(fh_in,0,SEEK_SET);
  /*读取BMP文件头*/
  if (sizeof(file_head)!=fread(&file_head,1,sizeof(file_head),fh_in))
  {
  printf("read bmp file error!!");
  fclose(fh_in);
  return 1;
  }
  /*读取BMP文件信息*/
  if (sizeof(fileinfo)!=fread(&fileinfo,1,sizeof(fileinfo),fh_in))
  {
  printf("read bmp file error!!");
  fclose(fh_in);
  return 1;
  }
  /*判断是不是BMP图片*/
  if (file_head.bfType!=0x4d42)
  {
  printf("bmp file error!!");
  fclose(fh_in);
  return 1;
  }
  fseek(fh_in,file_head.bfOffBits,SEEK_SET);
  fh_out=fopen(headfile,"ab");
  if (NULL==fh_out)
  {
  printf("open write file error!!");
  return 1;
  }

上一页  [1] [2] [3] [4] 下一页

责任编辑:cyth

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训