帮我写一个bgr888纯数据文件转bmp文件的脚本
时间: 2023-03-10 09:46:33 浏览: 150
数据转换成bmp图像
5星 · 资源好评率100%
我可以为你提供一个示例脚本:#include <stdio.h>
int main()
{
FILE *bgr888_file;
FILE *bmp_file;
int width, height;
unsigned char *image; bgr888_file=fopen("input_file.bgr888", "rb");
bmp_file=fopen("output_file.bmp", "wb");
fread(&width, sizeof(int), 1, bgr888_file);
fread(&height, sizeof(int), 1, bgr888_file);
image=(unsigned char*)malloc(width*height*3);
fread(image, width*height*3, 1, bgr888_file);
fwrite("BM", sizeof(char), 2, bmp_file);
int filesize=54+3*width*height;
fwrite(&filesize, sizeof(int), 1, bmp_file);
int pading=0;
fwrite(&pading, sizeof(int), 1, bmp_file);
int offset=54;
fwrite(&offset, sizeof(int), 1, bmp_file);
int header=40;
fwrite(&header, sizeof(int), 1, bmp_file);
fwrite(&width, sizeof(int), 1, bmp_file);
fwrite(&height, sizeof(int), 1, bmp_file);
int planes=1;
fwrite(&planes, sizeof(short), 1, bmp_file);
int bits=24;
fwrite(&bits, sizeof(short), 1, bmp_file);
int compr=0;
fwrite(&compr, sizeof(int), 1, bmp_file);
int imageSize=3*width*height;
fwrite(&imageSize, sizeof(int), 1, bmp_file);
int xRes=0;
fwrite(&xRes, sizeof(int), 1, bmp_file);
int yRes=0;
fwrite(&yRes, sizeof(int), 1, bmp_file);
int colors=0;
fwrite(&colors, sizeof(int), 1, bmp_file);
int impColors=0;
fwrite(&impColors, sizeof(int), 1, bmp_file);
for (int i=0; i<height; i++)
{
fwrite(image+(width*(height-i-1)*3), 3, width, bmp_file);
fwrite(0, sizeof(char), (4-(width*3)%4)%4, bmp_file);
}
fclose(bgr888_file);
fclose(bmp_file);
free(image);
return 0;
}
阅读全文