计算机C语言编程-----C语言保存图形为16色BMP文件

计算机C语言编程-----C语言保存图形为16色BMP文件
2011年12月31日
  把下面的代码保存为文件名.C,如sv.c,存到c编译软件的include 文件夹内,这样以后要保存上面的图形模式文件,只要调用下面的函数就行了。如save_pic("my.bmp");,我的这些贴图都是用这个函数保存的。
  #include
  void save_pic(char *fn){
  typedef long LONG;
  typedef unsigned char BYTE;
  typedef unsigned long DWORD;
  typedef unsigned short WORD;
  typedef struct tagBITMAPFILEHEADER {
  WORD bfType;
  DWORD bfSize;
  WORD bfReserved1;
  WORD bfReserved2;
  DWORD bfOffBits;
  } BITMAPFILEHEADER;
  typedef struct tagBITMAPINFOHEADER{
  DWORD biSize;
  LONG biWidth;
  LONG biHeight;
  WORD biPlanes;
  WORD biBitCount;
  DWORD biCompression;
  DWORD biSizeImage;
  LONG biXPelsPerMeter;
  LONG biYPelsPerMeter;
  DWORD biClrUsed;
  DWORD biClrImportant;
  } BITMAPINFOHEADER;
  int i,j;
  FILE *fp;
  BITMAPFILEHEADER bfh;
  BITMAPINFOHEADER bif;
  int color[16]={0,4,2,6,1,5,3,7,8,12,10,14,9,13,11,15};
  unsigned char rgb[64]={0,0,0,0,0,0,128,0,0,128,0,0,0,128,128,0
  ,128,0,0,0,128,0,128,0,128,128,0,0,128,128,128,0
  ,192,192,192,0,0,0,225,0,0,255,0,0,0,255,255,0
  ,255,0,0,0,255,0,255,0,255,255,0,0,255,255,255,0};
  union
  {
  unsigned char value;
  struct
  {
  unsigned c1:4;
  unsigned c2:4;
  }color;
  }mycolor;
  bfh.bfType=0x4d42;
  bfh.bfSize=153718;
  bfh.bfReserved1=0;
  bfh.bfReserved2=0;
  bfh.bfOffBits=118;
  bif.biSize=40;
  bif.biWidth=640;
  bif.biHeight=480;
  bif.biPlanes=1;
  bif.biBitCount=4;
  bif.biCompression=0;
  bif.biSizeImage=0;
  bif.biXPelsPerMeter=0;
  bif.biYPelsPerMeter=0;
  bif.biClrUsed=0;
  bif.biClrImportant=0;
  fp=fopen(fn,"wb");
  fwrite(&bfh,sizeof(bfh),1,fp);
  fwrite(&bif,sizeof(bif),1,fp);
  fwrite(rgb,sizeof(rgb),1,fp);
  for(j=479;j>=0;j--){
  for(i=0;i<640;i+=2){
  mycolor.color.c2=color[getpixel(i,j)];
  mycolor.color.c1=color[getpixel(i+1,j)];
  putc(mycolor.value,fp);
  }
  }
  fclose(fp);
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值