首页 > 代码库 > BMP转字符
BMP转字符
转载请注明出处!!
输入一个BMP文件,输出一个文本图形,
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #pragma pack(1) 5 struct BMPFILEHEADER //BMP头文件 6 { 7 short bfType; 8 int bfSize; 9 int bfReserved; 10 int bfOffset; 11 }; 12 13 struct BMPINFOHEADER //BMP图像信息 14 { 15 int biSize; 16 int biWidth; 17 int biHeight; 18 short biPlanes; 19 short biBits; 20 int biCompression; 21 int biDataSize; 22 int biHResolution; 23 int biVResolution; 24 int biColors; 25 int biImport; 26 }; 27 #pragma pop() 28 29 int main(int argc, char * argv[]) 30 { 31 if(argc != 3) 32 { 33 printf("usage: %s <bmp file> <out file>", argv[0]); 34 return -1; 35 } 36 unsigned int w, h; 37 unsigned int i, j, k; 38 unsigned int bgr2gray; 39 FILE * p = fopen(argv[1], "rb"); 40 FILE * q = fopen(argv[2], "w"); 41 unsigned char max_gray = 0; 42 43 unsigned char bgr[1024][1024][4] = {0}; //RGB数组 44 unsigned char gray[1024][1024] = {0}; //灰度数组 45 unsigned char captain[8]; //替换字符 46 captain[0] = ‘ ‘; 47 captain[1] = ‘.‘; 48 captain[2] = ‘-‘; 49 captain[3] = ‘=‘; 50 captain[4] = ‘o‘; 51 captain[5] = ‘*‘; 52 captain[6] = ‘#‘; 53 captain[7] = ‘@‘; 54 captain[8] = ‘&‘; 55 56 BMPFILEHEADER bfh; 57 BMPINFOHEADER bih; 58 59 fread(&bfh, sizeof(BMPFILEHEADER), 1, p); 60 fread(&bih, sizeof(BMPINFOHEADER), 1, p); 61 62 63 printf("bfh: %d\t%d\t%d\t%d\n", bfh.bfType, bfh.bfSize, bfh.bfReserved, bfh.bfOffset); 64 printf("bih: %d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", bih.biSize, bih.biWidth, 65 bih.biHeight, bih.biPlanes, 66 bih.biBits, bih.biCompression, 67 bih.biDataSize, bih.biHResolution, 68 bih.biVResolution, bih.biColors, 69 bih.biImport); 70 71 if(bih.biWidth > 1024 || bih.biHeight > 1024) 72 { 73 fclose(p); 74 fclose(q); 75 printf("only support 24-bits or 32-bits & uncompress & (width height) < 1024 bmp!\n"); 76 return 0; 77 } 78 79 w = bih.biWidth; 80 h = bih.biHeight; 81 82 for(i=0; i<h; i++) 83 { 84 for(j=0; j<w; j++) 85 { 86 for(k=0; k<3; k++) 87 { 88 fread(&bgr[i][j][k], sizeof(unsigned char), 1, p); 89 } 90 if(bih.biBits == 32) 91 { 92 fread(&bgr[i][j][3], sizeof(unsigned char), 1, p); //读取透明度值 93 } 94 bgr2gray = bgr[i][j][0] * 11 + bgr[i][j][1] * 59 + bgr[i][j][2] * 30; //BGR -> Gray 95 gray[h-i-1][j] = (unsigned char)(bgr2gray/100); 96 if(gray[h-i-1][j] > max_gray) //算是归一化吧- -! 97 { 98 max_gray = gray[h-i-1][j]; 99 }100 }101 if(bih.biBits == 24) //读取非对齐行冗余符102 {103 if(bih.biWidth % 4 == 1)104 {105 fread(&bgr[i][j][3], sizeof(unsigned char), 1, p);106 }107 else if(bih.biWidth % 4 == 2)108 {109 fread(&bgr[i][j][3], sizeof(unsigned char), 1, p);110 fread(&bgr[i][j][3], sizeof(unsigned char), 1, p);111 }112 else if(bih.biWidth % 4 == 3)113 {114 fread(&bgr[i][j][3], sizeof(unsigned char), 1, p);115 fread(&bgr[i][j][3], sizeof(unsigned char), 1, p);116 fread(&bgr[i][j][3], sizeof(unsigned char), 1, p);117 }118 }119 }120 121 for(i=0; i<h; i++) //打印122 {123 for(j=0; j<w; j++)124 {125 bgr2gray = gray[i][j] * 8 / max_gray; //归一化126 fprintf(q, "%c", captain[bgr2gray]);127 }128 fprintf(q, "\n");129 }130 131 fclose(p);132 fclose(q);133 134 return 0;135 }
BMP转字符
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。