用C或C++编程 对大小为256×256大小 24 位真彩的bmp格式的图像进行相应操作并且读写要求用到fread 、fwrite 函数等要求如下:对图像1bmp用I=R+G+B3得到灰度图像将得到的灰度图像输出为2bmp
#include <stdio.h>
#include <stdlib.h>
#pragma pack(2) // BMP文件头结构体和位图信息头结构体按2字节对齐
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;
// BMP文件头结构体
typedef struct BMPFileHeader {
WORD bfType; // 文件类型,必须为0x4D42
DWORD bfSize; // 文件大小(字节)
WORD bfReserved1; // 保留,必须为0
WORD bfReserved2; // 保留,必须为0
DWORD bfOffBits; // 从文件头到像素数据的偏移量(字节)
} BMPFileHeader;
// 位图信息头结构体
typedef struct BMPInfoHeader {
DWORD biSize; // 信息头大小(字节)
int biWidth; // 图像宽度(像素)
int biHeight; // 图像高度(像素)
WORD biPlanes; // 颜色平面数,必须为1
WORD biBitCount; // 每个像素的位数,常用值为1、4、8、16、24、32
DWORD biCompression; // 压缩类型,0表示不压缩
DWORD biSizeImage; // 像素数据大小(字节)
int biXPelsPerMeter; // 水平分辨率(像素/米)
int biYPelsPerMeter; // 垂直分辨率(像素/米)
DWORD biClrUsed; // 颜色表中实际使用的颜色数,0表示使用所有颜色
DWORD biClrImportant;// 重要的颜色数,0表示所有颜色都重要
} BMPInfoHeader;
// RGB颜色结构体
typedef struct RGBColor {
BYTE R; // 红色分量
BYTE G; // 绿色分量
BYTE B; // 蓝色分量
} RGBColor;
// 灰度图像结构体
typedef struct GrayImage {
int width; // 图像宽度(像素)
int height; // 图像高度(像素)
BYTE* data; // 像素数据
} GrayImage;
// 读取24位真彩BMP文件
bool ReadBMP24(const char* filename, RGBColor* &pColorData, BMPFileHeader &fileHeader, BMPInfoHeader &infoHeader) {
FILE* fp = fopen(filename, "rb");
if (!fp) {
printf("Failed to open file %s!\n", filename);
return false;
}
// 读取文件头
fread(&fileHeader, sizeof(BMPFileHeader), 1, fp);
if (fileHeader.bfType != 0x4D42) { // 文件类型必须为0x4D42
printf("Invalid BMP file!\n");
fclose(fp);
return false;
}
// 读取位图信息头
fread(&infoHeader, sizeof(BMPInfoHeader), 1, fp);
if (infoHeader.biBitCount != 24) { // 只支持24位真彩图像
printf("Only support 24-bit true color BMP file!\n");
fclose(fp);
return false;
}
// 读取调色板
if (infoHeader.biClrUsed > 0) {
if (infoHeader.biClrUsed > 256) {
printf("Invalid color table!\n");
fclose(fp);
return false;
}
RGBColor colorTable[256];
fread(colorTable, sizeof(RGBColor), infoHeader.biClrUsed, fp);
}
// 读取像素数据
int rowSize = (infoHeader.biWidth * 3 + 3) & ~3; // 每行像素数据的字节数(按4字节对齐)
int dataSize = rowSize * infoHeader.biHeight; // 所有像素数据的字节数
pColorData = new RGBColor[infoHeader.biWidth * infoHeader.biHeight];
BYTE* pDataBuf = new BYTE[dataSize];
fread(pDataBuf, sizeof(BYTE), dataSize, fp);
for (int i = 0; i < infoHeader.biHeight; i++) {
for (int j = 0; j < infoHeader.biWidth; j++) {
pColorData[i * infoHeader.biWidth + j].B = pDataBuf[i * rowSize + j * 3];
pColorData[i * infoHeader.biWidth + j].G = pDataBuf[i * rowSize + j * 3 + 1];
pColorData[i * infoHeader.biWidth + j].R = pDataBuf[i * rowSize + j * 3 + 2];
}
}
delete[] pDataBuf;
fclose(fp);
return true;
}
// 写入24位真彩BMP文件
bool WriteBMP24(const char* filename, RGBColor* pColorData, BMPFileHeader fileHeader, BMPInfoHeader infoHeader) {
FILE* fp = fopen(filename, "wb");
if (!fp) {
printf("Failed to open file %s!\n", filename);
return false;
}
// 写入文件头
fwrite(&fileHeader, sizeof(BMPFileHeader), 1, fp);
// 写入位图信息头
fwrite(&infoHeader, sizeof(BMPInfoHeader), 1, fp);
// 写入像素数据
int rowSize = (infoHeader.biWidth * 3 + 3) & ~3; // 每行像素数据的字节数(按4字节对齐)
int dataSize = rowSize * infoHeader.biHeight; // 所有像素数据的字节数
BYTE* pDataBuf = new BYTE[dataSize];
for (int i = 0; i < infoHeader.biHeight; i++) {
for (int j = 0; j < infoHeader.biWidth; j++) {
pDataBuf[i * rowSize + j * 3] = pColorData[i * infoHeader.biWidth + j].B;
pDataBuf[i * rowSize + j * 3 + 1] = pColorData[i * infoHeader.biWidth + j].G;
pDataBuf[i * rowSize + j * 3 + 2] = pColorData[i * infoHeader.biWidth + j].R;
}
}
fwrite(pDataBuf, sizeof(BYTE), dataSize, fp);
delete[] pDataBuf;
fclose(fp);
return true;
}
// 生成灰度图像
void ConvertToGray(RGBColor* pColorData, GrayImage &grayImage, BMPInfoHeader infoHeader) {
grayImage.width = infoHeader.biWidth;
grayImage.height = infoHeader.biHeight;
grayImage.data = new BYTE[infoHeader.biWidth * infoHeader.biHeight];
for (int i = 0; i < infoHeader.biHeight; i++) {
for (int j = 0; j < infoHeader.biWidth; j++) {
BYTE R = pColorData[i * infoHeader.biWidth + j].R;
BYTE G = pColorData[i * infoHeader.biWidth + j].G;
BYTE B = pColorData[i * infoHeader.biWidth + j].B;
grayImage.data[i * infoHeader.biWidth + j] = (R + G + B) / 3;
}
}
}
int main() {
// 读取24位真彩BMP文件
BMPFileHeader fileHeader;
BMPInfoHeader infoHeader;
RGBColor* pColorData = NULL;
if (!ReadBMP24("1.bmp", pColorData, fileHeader, infoHeader)) {
return -1;
}
// 生成灰度图像
GrayImage grayImage;
ConvertToGray(pColorData, grayImage, infoHeader);
// 写入灰度图像
BMPFileHeader grayFileHeader = fileHeader;
BMPInfoHeader grayInfoHeader = infoHeader;
grayInfoHeader.biBitCount = 8; // 8位灰度图像
grayInfoHeader.biSizeImage = grayInfoHeader.biWidth * grayInfoHeader.biHeight; // 每个像素占1字节
grayInfoHeader.biClrUsed = 256; // 颜色表中有256个颜色
grayInfoHeader.biClrImportant = 0;
BYTE* grayColorTable = new BYTE[1024];
for (int i = 0; i < 256; i++) {
grayColorTable[i * 4] = grayColorTable[i * 4 + 1] = grayColorTable[i * 4 + 2] = i;
grayColorTable[i * 4 + 3] = 0;
}
grayFileHeader.bfSize = grayFileHeader.bfOffBits + grayInfoHeader.biSizeImage + 1024; // 文件大小
grayFileHeader.bfOffBits += 1024; // 像素数据偏移量
FILE* fp = fopen("2.bmp", "wb");
if (!fp) {
printf("Failed to create file 2.bmp!\n");
return -1;
}
fwrite(&grayFileHeader, sizeof(BMPFileHeader), 1, fp); // 写入文件头
fwrite(&grayInfoHeader, sizeof(BMPInfoHeader), 1, fp); // 写入位图信息头
fwrite(grayColorTable, sizeof(BYTE), 1024, fp); // 写入颜色表
fwrite(grayImage.data, sizeof(BYTE), grayInfoHeader.biSizeImage, fp); // 写入像素数据
fclose(fp);
// 释放内存
delete[] pColorData;
delete[] grayImage.data;
delete[] grayColorTable;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/b25J 著作权归作者所有。请勿转载和采集!