自写位图做纹理(原创)

读入位图做纹理的程序,搞了小一阵。犯了些不该犯的错误,比如指针的指针。

另外发现a=a-b+(b=a)可以交换两个数值不用中间变量。C做的话没问题,C++却不行。所以下面代码还是用了中间变量。

位图像素是(B,G,R)存储的,所以得用最后个一while交换下两个值。

unsigned char * loadmytex(char *bmpname,int *bmpwidth ,int *bmpheight)
{
 FILE *fp;
 unsigned char tmpswap,*bmp;
 long bmpsize;
 if((fp=fopen(bmpname,"r"))==NULL)
 {
  printf("error,no pic be loaded/n");
  getchar();
  exit(1);
 }
 fseek(fp,18,SEEK_SET);       //find the width information
 fread(bmpwidth,4,1,fp);   //read a unsigned long type (4 bytes) into bmpwidth
 fread(bmpheight,4,1,fp);  //read a unsigned long type (4 bytes) into bmpheight
 bmpsize=(*bmpwidth)*(*bmpheight)*3;
 if((bmp=(unsigned char *)malloc(bmpsize*sizeof(unsigned char)))==NULL)return 0;
 fseek(fp,54,SEEK_SET);  //find the bitmap data start offset (54)
 fread(bmp,sizeof(unsigned char),bmpsize,fp);
 fclose(fp);
 while(bmpsize-=3){tmpswap=bmp[bmpsize];bmp[bmpsize]=bmp[bmpsize+2];bmp[bmpsize+2]=tmpswap;}//swap the red and blue partition

 return bmp;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值