几点说明:
1.程序需要包含头文件Windows.h,其中定义了bmp文件格式信息。
2.bmp文件每像素包含RGB三个分量,实际数据存储顺序是BGR。
3.bmp图像的左下角对应数据的起始存储位置data[0][0]。
以下示例代码读入一个bmp文件"lenna.bmp",经降采样处理后,保存成"lenna_downsample.bmp"。
#include <Windows.h>
#include <iostream>
using namespace std;
void main()
{
char* filename = "lenna.bmp";
//--------------bmp 文件读入-------------------------------------
BITMAPFILEHEADER head;//图像的文件头信息
BITMAPINFOHEADER info;
FILE *fp;
if( (fp=fopen(filename,"rb"))==NULL) //打开文件
{
printf("cannot open %s \n", filename);
exit(0);
}
fread(&head, 1, sizeof(BITMAPFILEHEADER), fp);
fread(&info, 1, sizeof(BITMAPINFOHEADER), fp);
int width = info.biWidth;
int height = info.biHeight;
int lineByte = (width*3+3)/4*4; //图像每行的字节数(行字节数必须是4对齐)
int size = lineByte*height;
unsigned char* data = new unsigned char[size];
fread(data, 1, size, fp); //数据读取, origin在图像的左下角
fclose(fp);
//-----------------对读入的bmp文件降采样处理,并保存成新的bmp文件
int width2 = width*0.5;
int height2 = height*0.5;
int lineByte2 = (width2*3+3)/4*4; //图像每行的字节数(行字节数必须是4对齐)
int size2 = lineByte2*height2;
unsigned char* data2 = new unsigned char[size2];
memset(data2, 0, sizeof(unsigned char)*size2);
for (int y=0; y<height2; y++)
{
int y2 = 2*y;
for (int x=0; x<width2; x++) //降采样
{
int x2 = 2*x;
data2[y*lineByte2+3*x] = data[y2*lineByte+3*x2];
data2[y*lineByte2+3*x+1] = data[y2*lineByte+3*x2+1];
data2[y*lineByte2+3*x+2] = data[y2*lineByte+3*x2+2];
}
}
//---save bmp file
if((fp=fopen("lenna_downsample.bmp","wb"))==NULL) //以二进制只读方式打开文件
{
printf("cannot write file\n");
exit(0);
}
//定义文件头
BITMAPFILEHEADER head2;
BITMAPINFOHEADER info2;
head2.bfType=0x4d42;
head2.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ size2;
head2.bfReserved1=0;
head2.bfReserved2=0;
head2.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
info2.biSize=sizeof(BITMAPINFOHEADER);
info2.biWidth=width2;
info2.biHeight=height2;
info2.biPlanes=1;
info2.biBitCount=24;
info2.biCompression=0;
info2.biSizeImage=0;
info2.biXPelsPerMeter=0;
info2.biYPelsPerMeter=0;
info2.biClrUsed=0;
info2.biClrImportant=0;
fwrite(&head2,sizeof(BITMAPFILEHEADER),1,fp); // 成块写入文件
fwrite(&info2,sizeof(BITMAPINFOHEADER),1,fp); // 成块写入文件
fwrite(data2, size2, 1, fp); // 成块写入文件,origin在图像的左下角
fclose(fp);
}
运行结果: