c语言读写保存图像,C语言读写BMP图像

几点说明:

1.程序需要包含头文件Windows.h,其中定义了bmp文件格式信息。

2.bmp文件每像素包含RGB三个分量,实际数据存储顺序是BGR。

3.bmp图像的左下角对应数据的起始存储位置data[0][0]。

以下示例代码读入一个bmp文件"lenna.bmp",经降采样处理后,保存成"lenna_downsample.bmp"。

#include #include 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

运行结果:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是将灰度图像存储为bmp文件的C语言代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <stdint.h> #pragma pack(push, 1) // 禁用对齐 struct bmp_file_header { uint16_t magic_num; // 文件类型标识符 uint32_t size; // 文件大小 uint16_t reserved1; // 保留字段1 uint16_t reserved2; // 保留字段2 uint32_t offset; // 数据偏移量 }; struct bmp_info_header { uint32_t header_size; // 位图信息头大小 int32_t width; // 图像宽度 int32_t height; // 图像高度 uint16_t planes; // 颜色平面数 uint16_t bit_depth; // 颜色位深度 uint32_t compression; // 压缩类型 uint32_t image_size; // 压缩图像大小 int32_t x_resolution; // 水平分辨率 int32_t y_resolution; // 垂直分辨率 uint32_t color_palette; // 调色板颜色数 uint32_t important_colors; // 重要颜色数 }; #pragma pack(pop) // 恢复对齐 int main() { // 读取图像数据 // TODO: 替换成实际的图像数据 uint8_t *data = NULL; int width = 640; int height = 480; // 打开文件,准备写入 FILE *fp = fopen("example.bmp", "wb"); if (!fp) { perror("failed to open file"); exit(1); } // 计算文件大小和数据偏移量 uint32_t image_size = width * height; uint32_t file_size = sizeof(struct bmp_file_header) + sizeof(struct bmp_info_header) + 1024 + image_size; uint32_t offset = sizeof(struct bmp_file_header) + sizeof(struct bmp_info_header) + 1024; // 写入文件头信息 struct bmp_file_header file_header = { .magic_num = 0x4D42, // BM .size = file_size, .reserved1 = 0, .reserved2 = 0, .offset = offset }; fwrite(&file_header, sizeof(file_header), 1, fp); // 写入位图信息头信息 struct bmp_info_header info_header = { .header_size = sizeof(struct bmp_info_header), .width = width, .height = height, .planes = 1, .bit_depth = 8, .compression = 0, .image_size = image_size, .x_resolution = 0, .y_resolution = 0, .color_palette = 256, .important_colors = 0 }; fwrite(&info_header, sizeof(info_header), 1, fp); // 写入调色板信息 uint8_t palette[1024] = {0}; for (int i = 0; i < 256; i++) { palette[i * 4] = i; palette[i * 4 + 1] = i; palette[i * 4 + 2] = i; } fwrite(palette, sizeof(palette), 1, fp); // 写入像素数据 fwrite(data, image_size, 1, fp); // 关闭文件 fclose(fp); // 释放内存 free(data); return 0; } ``` 这段代码使用了C语言的文件读写API和结构体来生成bmp文件头、位图信息头、调色板信息和像素数据,并将它们写入文件。其中,调色板信息使用了灰度调色板,即将256个颜色值(0-255)都设置为相同的灰度值,以便表示灰度图像。如果需要将其他类型的图像存储为bmp文件,可以相应地修改位图信息头和像素数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值