标准C读BMP图象的一个小代码

#include <stdio.h>
#include <stdlib.h>

//#define BUFFSIZE 1024
#define  WIDTH    10


unsigned char head[1078];
unsigned char pix[WIDTH][WIDTH];
int       width=WIDTH; 
int       heigh;
void          pixread(FILE *fpin);
void       headread(FILE *fpin); 
void          newbmp();


int main()
{
 //int datanum;
 //int i;
 FILE *bmpfp=NULL;
 //int width=WIDTH;

 

///mnt/win1/mywork/picture-process/32
 if((bmpfp=fopen("10x10.bmp","rb"))==NULL){
   printf("Read the bmp file has failed/n");
   exit(1);
   }
                
                 headread(bmpfp);
      pixread(bmpfp);
                        newbmp();    
   
   fclose(bmpfp);
   return 0;
}

 

     
void  headread(FILE *fpin)                               //read the bmp file head 1078 byte
{
 int num1;
 
 
  num1=fread(head,sizeof(char),1078,fpin);
  printf("You have successfully read %d data from bmp file/n",num1);
}

 

void  pixread(FILE *fpin)                                   //read the bmp file pixels data 32*32 byte
{
 int num2 ;
 int i,j;
 int count;
 unsigned char  *pixbuff=NULL;

 heigh=10;
 width=32;
 fseek(fpin,1078,0);
 if((pixbuff=(unsigned char *)malloc(sizeof(char)*320))==NULL){
  printf("Data memory allocation fail!/n");
 }
 num2=fread(pixbuff,sizeof(char),1024,fpin);
        printf("You have successfully read %d data from the bmp file/n",num2);


        for(i=(heigh-1);i>=0;i--){
  count=width-1-i;
  for(j=0;j<width;j++){
    pix[i][j]=pixbuff[count*width+j];
    }
 }
  
 free(pixbuff);
 pixbuff=NULL;

 heigh=10;
 width=32;
 printf("The data are/n");
          for(i=0;i<heigh;i++){
    for(j=0;j<width;j++){
                     printf("%d ",pix[i][j]);
                    }
        printf("/n");
       }
 //BeginWaitCursor();
   printf("Read the data end/n");
}

 


void  newbmp()                                               //write the data that has some change to new bmp file

 int             i,j;
 int             num3;
 int             num4;
 int          count;
 //unsigned char pixbuff[1024];
        FILE            *newfp;
        unsigned char  *pixbuff=NULL;

 for(i=1;i<32;i++) {
  pix[16][i]=pix[15][i]=0;                                //make a small change about old bmp file
     
 }
 
 
 newfp=fopen("c:/new1.bmp","wb");///mnt/win1/mywork/picture-process/pgm/

        num3=fwrite(head,sizeof(char),1078,newfp);
 printf("You have successfully write %d data to new bmp file/n",num3);


 if((pixbuff=(unsigned char *)malloc(sizeof(char)*width*width))==NULL){
   printf(" Data memory allocation fail!/n");
 }
 
         width=32;                                                 //because the global var has changed

 for(i=(width-1);i>=0;i--){
  count=width-1-i;
  for(j=0;j<width;j++){
   pixbuff[count*width+j]=pix[i][j];
  }
 } 
 
 //for(i=1;i<1025;i++){                                  //display the data
 // printf("%d ",pixbuf[i]);
 // if(i%32==0){
 //      putchar('/n');
 // }
 //}  
 fseek(newfp,1078,0);
 num4=fwrite(pixbuff,sizeof(char),1024,newfp);     
        printf("You have successfully write %d data to new bmp file/n",num4);
 
 free(pixbuff);
 pixbuff=NULL;
}

//根据图象的bitcounts不同,数组定义也不同,如24位BMP图象,3字节表示一像素

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值