c - 我正在尝试使用 c/c++ 读取、写入和翻译 bmp 图像文件,窗口不支持输出 bmp 文件格式(使用 Visual Studio)
问题描述
代码从这里开始
//creat the stucture for the bitmap file, which consist the header of size 54, which contains the information about the file.
struct BMP {
int width;
int height;
unsigned char header[54];
unsigned char *pixels;
int size;
};
c/c++中bmp文件的读写功能。这里我使用了 fopen() 函数
// read the image
BMP readBMP(string filename) {
BMP image;
int i;
string fileName = filename;
FILE *f = fopen(fileName.c_str(), "rb");
fread(image.header, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
image.width = *(int *) &image.header[18];
image.height = *(int *) &image.header[22];
cout<<"inage width: "<< image.width <<", image height: "<< image.height <<endl;
image.size = 3 * image.width * image.height;
image.pixels = new unsigned char[image.size]; // allocate 3 bytes per pixel
fread(image.pixels, sizeof(unsigned char), image.size, f); // read the rest of the data at once
fclose(f);
// bmp stors data in BGR format, so changing the BGR to RGB.
for (i = 0; i < image.size; i += 3) {
unsigned char tmp = image.pixels[i];
image.pixels[i] = image.pixels[i + 2];
image.pixels[i + 2] = tmp;
}
return image;
}
// write image
void writeBMP(string filename, BMP image) {
string fileName = filename;
FILE *out = fopen(fileName.c_str(), "wb");
fwrite(image.header, sizeof(unsigned char), 54, out);
int i;
unsigned char tmp;
for (i = 0; i < image.size; i += 3) {
tmp = image.pixels[i];
image.pixels[i] = image.pixels[i + 2];
image.pixels[i + 2] = tmp;
}
fwrite(image.pixels, sizeof(unsigned char), image.size, out); // read the rest of the data at once
fclose(out);
}
图像平移功能,用于平移图像对应的 x 和 y 维度。
// image translation
BMP translation(BMP image, int tx, int ty) {
BMP newImage = image;
unsigned char *pixel = new unsigned char[image.size];
// translation
for (int x = 0; x < image.width; ++x){
for (int y = 0; y < image.height; ++y) {
int xx = x-tx;
int yy = y+ty;
if (xx >= 0 && xx < image.width && yy >= 0 && yy < image.height){
pixel[(y * image.width + x) * 3 + 0] = image.pixels[(yy * image.width + xx) * 3 + 0];
pixel[(y * image.width + x) * 3 + 1] = image.pixels[(yy * image.width + xx) * 3 + 1];
pixel[(y * image.width + x) * 3 + 2] = image.pixels[(yy * image.width + xx) * 3 + 2];
}
}
}
newImage.pixels = pixel;
return newImage;
}
执行读、写和翻译操作的主函数调用如下。
int main() {
BMP image = readBMP("sample_640×426.bmp");
BMP image_t;
// // original image
writeBMP("Output-11.bmp", image);
// image translation
int x0=100, y0=50;
image_t = translation(image, x0, y0);
writeBMP("Out_translate.bmp", image_t);
return 0;
}
windows输出图像在这里[1] [1]:https ://i.stack.imgur.com/gd83D.png
解决方案
推荐阅读
- php - Docker 数据库错误 SQLSTATE[HY000] [2002] 没有这样的文件或目录
- spring - Thymeleaf th:字段未向控制器提交任何值
- python - Python 3 中按位与和逻辑与以及相等运算符的行为
- azure - 带有 vmss 和 cloud-init 的 Azure Devops 自托管代理
- jquery - 如何使用外部 AJAX 调用检索数据表过滤器?
- laravel - 从护照中的一台设备注销用户
- python - TensorFlow 多类分类器 -
- - 为什么?
- csv - 如何从 CSV 文件中读取随机行数并将它们包含在请求中
- asp.net-mvc-4 - ASP.net MVC Application having Nonce Problem in Subdirectory hosting in IIS
- python - average parameter of sklearn.metrics.jaccard_score (python sklearn)