BMP图像直方图均衡化算法程序为什么出来的图片无法显示呀
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdint.h>
typedef unsigned char BYTE; // 定义BYTE为一个字节的类型
typedef unsigned short WORD; // 定义WORD为两个字节的类型
typedef unsigned int DWORD; // 定义DWORD为四个字节的类型
typedef struct PerPixelgbr
{
BYTE b;
BYTE g;
BYTE r;
}BGR;
//14 Bytes 文件头信息块
typedef struct tBITMAPFILEHEADER
{
DWORD bfSize; //图片大小
DWORD bfReserved; //保留字;
DWORD bfOffBits; //文件头到像素数据偏移量
}TBH;
//40 Bytes 图像描述信息块
typedef struct tBITMAPINFOHEADER
{
DWORD biSize; //此结构体大小,换成kb除以1024
DWORD biWidth; //宽
DWORD biHeight; //高
WORD biPlanes; //平面显示属,一般显示器只有一个平面所以为1
WORD biBitCount; //一个像素所占的位数,一般为24,带A通道的有32
DWORD biCompression; //图像数据压缩的类,0为不压缩
DWORD biSizeImage; //像素数据所占的大小 =bfSize-bfOffBits
DWORD biXPelsPerMeter;//水平分辨率
DWORD biYPelsPerMeter;//垂直分辨率
DWORD biClrUsed; //位图实际使用的彩表的彩色索引数,为0证明全部使用
DWORD biClrImportant; //说明对图象显示有重要影响的颜色索引数,如果是0,表示无差别
}TBI;
int main()
{
FILE* in;
FILE* out;
TBH* BITMAPFILEHEADER;
TBI* BITMAPINFOHEADER;
BGR* duqu;
BITMAPFILEHEADER = (TBH*)malloc(sizeof(TBH));
BITMAPINFOHEADER = (TBI*)malloc(sizeof(TBI));
duqu = (BGR*)malloc(sizeof(BGR));
fopen_s(&in, "E:\\121.bmp", "rb");
WORD fileType;
fread(&fileType, sizeof(unsigned short), 1, in);
if(fileType == 0x4d42)
{
printf("文件类型标识正确\n");
}//判断是不是bmp文件
else{}
fread(BITMAPFILEHEADER, sizeof(TBH), 1, in);//读取 14 Bytes 文件头信息块
fread(BITMAPINFOHEADER, sizeof(TBI), 1, in);//读取 40 Bytes 图像描述信息块
int W, H;
W = (BITMAPINFOHEADER->biWidth);//得到宽度
H = (BITMAPINFOHEADER->biHeight);//得到长度
uint32_t B_ZFT[256] = { 0 }, G_ZFT[256] = { 0 }, R_ZFT[256] = { 0 };
uint32_t B_CS[256], G_CS[256], R_CS[256];
double B_GL[256], G_GL[256], R_GL[256];
double gailvhe = 0;
//fseek(in, 18, SEEK_SET);
//fread(&W, 4, 1, in);
//fseek(in, 22, SEEK_SET);
//fread(&H, 4, 1, in);
//fseek(in, 54, SEEK_SET);
for (int i = W * H; i > 0; i--)
{
fread(duqu, sizeof(BGR), 1, in);
B_ZFT[(duqu->b)]++;
G_ZFT[(duqu->g)]++;
R_ZFT[(duqu->r)]++;
}
free(duqu);
for (int i = 0; i < 256; i++)
{
gailvhe += (double)B_ZFT[i] / ((double)W * (double)H);
B_GL[i] = gailvhe;
}
printf("%lf", gailvhe);
gailvhe = 0;
for (int i = 0; i < 256; i++)
{
gailvhe += (double)G_ZFT[i] / ((double)W * (double)H);
G_GL[i] = gailvhe;
}
printf("%lf", gailvhe);
gailvhe = 0;
for (int i = 0; i < 256; i++)
{
gailvhe += (double)R_ZFT[i] / ((double)W * (double)H);
R_GL[i] = gailvhe;
}
printf("%lf", gailvhe);
for (int i = 0; i < 256; i++)
{
B_CS[i] = (uint32_t)(B_GL[i] * 255);
G_CS[i] = (uint32_t)(G_GL[i] * 255);
R_CS[i] = (uint32_t)(R_GL[i] * 255);
}
BGR* xielu = (BGR*)malloc(sizeof(BGR));
duqu = (BGR*)malloc(sizeof(BGR));
fseek(in, 54, SEEK_SET);
fopen_s(&out, "E:\\12121.bmp", "wb");
fwrite(&fileType, 2, 1, out);
fwrite(&BITMAPFILEHEADER, sizeof(TBH), 1, out);
fwrite(&BITMAPINFOHEADER, sizeof(TBI), 1, out);
//fseek(out, 54, SEEK_SET);
for (int i = W * H; i > 0; i--)
{
fread(duqu, sizeof(BGR), 1, in);
(xielu->b) = B_CS[(duqu->b)];
(xielu->g) = G_CS[(duqu->g)];
(xielu->r) = R_CS[(duqu->r)];
fwrite(&xielu, sizeof(BGR), 1, out);
}
free(duqu);
free(xielu);
fclose(in);
fclose(out);
return 0;
}