嵌入式3-29

今日作业:用fwrite 和 fseek功能,将一张bmp格式的图片更改成 德国国旗
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
typedef unsigned char bgr[3];
int main(int argc, const char *argv[])
{
FILE* fp = fopen("./photo.bmp","r");
fseek(fp,2,SEEK_SET);
int bmp_size = 0;
fread(&bmp_size,4,1,fp);
printf("图片大小为:%d\nkb",bmp_size);
int bmp_w = 0;
int bmp_h = 0;
//从头往后偏移18个字节,定位存储宽度的的内存上
fseek(fp,18,SEEK_SET);
fread(&bmp_w,4,1,fp);
fread(&bmp_h,4,1,fp);
printf("图像分辨率为:%d*%d\n",bmp_w,bmp_h);
fclose(fp);
fp = fopen("./photo.bmp","r+");
//准备一个3字节内存,用来表示bgr像素点信息
bgr c1={0,0,0};
bgr c2={0,0,230};
bgr c3={0,200,230};
//偏移54个字节,让光标定位在存放像素点的首地址上
fseek(fp,54,SEEK_SET);
// int count=bmp_w*bmp_h/3;
// fwrite(c1,3,count,fp);
// fwrite(c2,3,bmp_w * bmp_h/3,fp);
// fwrite(c3,3,bmp_w * bmp_h/3,fp);
for(int i=0;i<bmp_w;i++)
{
for(int j=0;j<=bmp_h/3;j++){
fwrite(c3,3,1,fp);
}
}
for(int i=0;i<bmp_w;i++)
{
for(int j=0;j<=bmp_h/3;j++){
fwrite(c2,3,1,fp);
}
}
for(int i=0;i<=bmp_w;i++)
{
for(int j=0;j<bmp_h/3;j++){
fwrite(c1,3,1,fp);
}
}
fclose(fp);
return 0;
}

相关推荐
MwEUwQ3Gx3 分钟前
常见Linux权限提升笔记
linux·运维·笔记
ken223243 分钟前
安装问题@ ubuntu 24.04 :efi 磁盘分区,挂载
linux·运维·ubuntu
不爱吃炸鸡柳1 小时前
[特殊字符]C/C++内存管理深度解剖:从内存布局到new/delete底层,吃透面试必考核心
c语言·c++·面试
数据知道1 小时前
claw-code 源码详细分析:`reference_data` JSON 快照——大型移植里「对照底稿」该怎么治理与演进?
linux·python·ubuntu·json·claude code
kvo7f2JTy1 小时前
吃透Linux/C++系统编程:文件与I/O操作从入门到避坑
java·linux·c++
Deitymoon1 小时前
linux——守护进程
linux
Lzh编程小栈1 小时前
数据结构与算法之队列深度解析:循环队列+C 语言硬核实现 + 面试考点全梳理
c语言·开发语言·汇编·数据结构·后端·算法·面试
Vect__2 小时前
深刻理解虚拟内存机制
linux
maosheng11462 小时前
Linux的第二次作业
linux·运维·服务器
maosheng11462 小时前
Linux
linux·运维·服务器