好久不写代码,一段将bmp写到mif的小程序花了我好几个小时。以后要多练习啊。
1 #include <string.h> 2 #include <stdio.h> 3 #include <math.h> 4 5 6 void main(){ 7 char str[3]; 8 char pos_str[5]; 9 10 FILE *fp_read; 11 fp_read = fopen("pattern_mem.bmp","r"); 12 13 FILE *fp_write; 14 fp_write = fopen("pattern_rom.mif","w"); 15 16 if(fp_write == NULL || fp_read == NULL) 17 { 18 printf("can't open files!"); 19 return ; 20 } 21 22 char bmp_head[54]; 23 //读出BMP头,抛弃不要 24 fread(bmp_head,sizeof(char)*54,1,fp_read); 25 //读出RGB信号,按顺序存入mif文件 26 for(int i = 0 ; i< 28800; i++) 27 { 28 fread(str, 3, 1, fp_read); 29 fprintf(fp_write,"%x:",i); 30 31 //这里可能大小端有问题 32 int result[24]={0}; 33 for(int j = 0 ; j < 8; j++) 34 result[j] = (str[0] & (0x01 << j))?1:0; 35 for(int m = 0 ; m < 8; m ++) 36 result[8+m] = (str[1] & (0x01 << m))?1:0; 37 for(int n = 0; n < 8 ; n++) 38 result[16+n] = (str[2] & (0x01 << n))?1:0; 39 40 for(int k = 0; k<24; k++) 41 fprintf(fp_write, "%d",result[k]); 42 43 fprintf(fp_write,"\n"); 44 45 } 46 fclose(fp_read); 47 fclose(fp_write); 48 49 return ; 50 }