Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
chap08_01.cpp 1.79 KB
Copy Edit Raw Blame History
Admin authored 2022-05-30 16:18 . 重命名 ch08_01.cpp 为 chap08_01.cpp
#include <iostream>
#include <fstream>
using namespace std;
#pragma pack(1)
struct FILEHEADER
{
char dummy1[18]; // 无用信息
int width; // 图像宽
int height; // 图像高
char dummy2[28]; //无用信息
};
struct RGB
{
unsigned char b; // 蓝
unsigned char g; // 绿
unsigned char r; // 红
};
int main()
{
FILEHEADER imgHead1, imgHead2;
RGB pixel1, pixel2, pixel;
float blendRatio = 0.25;
fstream imgFile1, imgFile2, imgFileResult;
imgFile1.open("lighthouse.bmp", ios::in|ios::binary);
imgFile2.open("penguins.bmp", ios::in|ios::binary);
imgFileResult.open("result.bmp", ios::out|ios::binary);
if(!imgFile1 || !imgFile2 || !imgFileResult)
{
cerr<<"File open or create error!"<<endl;
return 0;
}
imgFile1.read((char *)(&imgHead1), sizeof(FILEHEADER));
cout << imgHead1.width << "," << imgHead1.height << endl;
imgFile2.read((char *)(&imgHead2), sizeof(FILEHEADER));
cout << imgHead2.width << "," << imgHead2.height << endl;
if(imgHead1.width!=imgHead2.width || imgHead1.height!=imgHead2.height || imgHead1.width%4!=0) return 0;
imgFileResult.write((char *)(&imgHead1), sizeof(FILEHEADER));
for(int i=0; i<(imgHead1.width*imgHead1.height); i++)
{
imgFile1.read((char *)(&pixel1), sizeof(RGB));
imgFile2.read((char *)(&pixel2), sizeof(RGB));
pixel.b = pixel1.b*blendRatio + pixel2.b*(1-blendRatio);
pixel.g = pixel1.g*blendRatio + pixel2.g*(1-blendRatio);
pixel.r = pixel1.r*blendRatio + pixel2.r*(1-blendRatio);
imgFileResult.write((char *)(&pixel), sizeof(RGB));
}
imgFile1.close();
imgFile2.close();
imgFileResult.close();
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化