C语言实现BMP格式图片转化为灰度
程序员文章站
2022-06-24 09:45:02
本文实例为大家分享了c语言将bmp格式图片转化为灰度的具体代码,供大家参考,具体内容如下代码如下:#include#include#inc...
本文实例为大家分享了c语言将bmp格式图片转化为灰度的具体代码,供大家参考,具体内容如下
代码如下:
#include<stdio.h> #include<malloc.h> #include<stdlib.h> #pragma pack(1) typedef struct tagbitmapfileheader { unsigned char bftype[2];//文件格式 unsigned long bfsize;//文件大小 unsigned short bfreserved1;//保留 unsigned short bfreserved2; unsigned long bfoffbits; //dib数据在文件中的偏移量 }fileheader; #pragma pack() /* 位图数据信息结构 */ #pragma pack(1) typedef struct tagbitmapinfoheader { unsigned long bisize;//该结构的大小 long biwidth;//文件宽度 long biheight;//文件高度 unsigned short biplanes;//平面数 unsigned short bibitcount;//颜色位数 unsigned long bicompression;//压缩类型 unsigned long bisizeimage;//dib数据区大小 long bixpixpermeter; long biypixpermeter; unsigned long biclrused;//多少颜色索引表 unsigned long biclrimporant;//多少重要颜色 }fileinfo; #pragma pack() /* 调色板结构 */ #pragma pack(1) typedef struct tagrgbquad { unsigned char rgbblue; //蓝色分量亮度 unsigned char rgbgreen;//绿色分量亮度 unsigned char rgbred;//红色分量亮度 unsigned char rgbreserved; }rgbq; #pragma pack() int main() { file *fp1 = fopen("c:\\users\\administrator\\desktop\\data\\bmp\\image.bmp", "rb+"); if (fp1 == null) { printf("打开文件fp1失败"); exit(0); } file *fp2 = fopen("c:\\users\\administrator\\desktop\\data\\bmp\\imagegray.bmp", "wb"); if (fp1 == null) { printf("打开文件fp2失败"); exit(0); } fileheader * fh; fileinfo * fi; fh = (fileheader *)malloc(sizeof(fileheader)); fi = (fileinfo *)malloc(sizeof(fileinfo)); //读取位图头结构和信息头 fread(fh, sizeof(fileheader), 1, fp1); fread(fi, sizeof(fileinfo), 1, fp1); printf("\\\\\\\\\\\\\\\\\\\\原始图片信息\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); printf("bmp文件头:\n"); printf("bfsize:%d\n", fh->bfsize); printf("bfoffbits:%d\n", fh->bfoffbits); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); printf("bmp信息头\n"); printf("结构体长度:%d \n", fi->bisize); printf("位图宽度:%d \n", fi->biwidth); printf("位图高度:%d \n", fi->biheight); printf("位图平面数:%d \n", fi->biplanes); printf("颜色位数:%d \n", fi->bibitcount); printf("压缩方式:%d \n", fi->bicompression); printf("实际位图数据占用的字节数:%d \n", fi->bisizeimage); printf("x方向分辨率:%d \n", fi->bixpixpermeter); printf("y方向分辨率:%d \n", fi->biypixpermeter); printf("使用的颜色数:%d \n", fi->biclrused); printf("重要颜色数:%d \n", fi->biclrimporant); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); //修改信息头 fi->bibitcount = 8; //fi->bisizeimage = ((fi->biwidth * 3 + 3) / 4) * 4 * fi->biheight; fi->bisizeimage = fi->biheight*fi->biwidth; //修改文件头 fh->bfoffbits = sizeof(fileheader) + sizeof(fileinfo) + 256 * sizeof(rgbq); fh->bfsize = fh->bfoffbits + fi->bisizeimage; printf("\\\\\\\\\\\\\\\\\\\\修改后的图片信息\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); printf("bmp文件头:\n"); printf("bfsize:%d\n", fh->bfsize); printf("bfoffbits:%d\n", fh->bfoffbits); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); printf("bmp信息头\n"); printf("结构体长度:%d \n", fi->bisize); printf("位图宽度:%d \n", fi->biwidth); printf("位图高度:%d \n", fi->biheight); printf("位图平面数:%d \n", fi->biplanes); printf("颜色位数:%d \n", fi->bibitcount); printf("压缩方式:%d \n", fi->bicompression); printf("实际位图数据占用的字节数:%d \n", fi->bisizeimage); printf("x方向分辨率:%d \n", fi->bixpixpermeter); printf("y方向分辨率:%d \n", fi->biypixpermeter); printf("使用的颜色数:%d \n", fi->biclrused); printf("重要颜色数:%d \n", fi->biclrimporant); printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"); //创建调色板 int i,j,k=0; rgbq *fq = (rgbq *)malloc(256 * sizeof(rgbq)); for (i = 0; i<256; i++) { fq[i].rgbblue = fq[i].rgbgreen = fq[i].rgbred = i; } //写入文件头、信息头、调色板 fwrite(fh, sizeof(fileheader), 1, fp2); fwrite(fi, sizeof(fileinfo), 1, fp2); fwrite(fq, sizeof(rgbq), 256, fp2); //将位图信息转为灰度 //存储bmp一行的像素点 //unsigned char imgdata[900][3]; unsigned char imgdata[3000][3]; //将灰度图像存到一维数组中 //unsigned char graydata2[900]; unsigned char imgdata2[3000]; /* //错误的算法 for (i = 0; i < fi->biheight; i++) { for (j = 0; j < (fi->biwidth * 3 + 3) / 4 * 4; j++) { for (k = 0; k < 3; k++) { fread(&imgdata[j][k], 1, 1, fp1); } } for (j = 0; j < (fi->biwidth + 3) / 4 * 4; j++) { imgdata2[j] = int((float)imgdata[j][0] * 0.114 + (float)imgdata[j][1] * 0.587 + (float)imgdata[j][2] * 0.299); } //将灰度图信息写入 fwrite(imgdata2, j, 1, fp2); } */ /* //正确的算法(1) for (i = 0; i<fi->biheight; i++) { for (j = 0; j<(fi->biwidth + 3) / 4 * 4; j++) { for (k = 0; k<3; k++) fread(&imgdata[j][k], 1, 1, fp1); } for (j = 0; j<(fi->biwidth + 3) / 4 * 4; j++) { imgdata2[j] = int((float)imgdata[j][0] * 0.114 + (float)imgdata[j][1] * 0.587 + (float)imgdata[j][2] * 0.299); } //将灰度图信息写入 fwrite(imgdata2, j, 1, fp2); } */ //正确算法(2) unsigned char * * bmp_data; bmp_data = new unsigned char*[fi->biheight]; //声明一个指针数组 unsigned char *data288 = new unsigned char[fi->biheight*fi->biwidth]; for (i = 0; i<fi->biheight; i++) bmp_data[i] = new unsigned char[(fi->biwidth * 3 + 3) / 4 * 4]; //每个数组元素也是一个指针数组 for (i = 0; i<fi->biheight; i++) for (j = 0; j<(fi->biwidth * 3 + 3) / 4 * 4; j++) fread(&bmp_data[i][j], 1, 1, fp1);//每次只读取一个字节,存入数组 for (i = 0; i<fi->biheight; i++)//将24位真彩色转换成灰度图 for (j = 0; j<fi->biwidth; j++){ data288[fi->biwidth*i + j] = ((unsigned char)((float)bmp_data[i][3 * j] * 0.114 + (float)bmp_data[i][3 * j + 1] * 0.587 + (float)bmp_data[i][3 * j + 2] * 0.299)); } fwrite(data288, fi->bisizeimage, 1, fp2); free(fh); free(fi); free(fq); fclose(fp1); fclose(fp2); printf("success\n"); return 0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 浅谈Vue3的几个优势
下一篇: JavaScript 原型与原型链详情