嵌入式3-29

今日作业:用fwrite 和 fseek功能,将一张bmp格式的图片更改成 德国国旗
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
typedef unsigned char bgr3;
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;
}

相关推荐
Nebula嵌入式10 小时前
【C语言】09-深入解析main函数
linux·c语言·开发语言·嵌入式
Dxy123931021611 小时前
Linux 编译安装 Python 3.12.10(多版本共存,不破坏系统Python)
linux·运维·python
天空'之城12 小时前
C 语言工业级通用组件手写 23:卡尔曼滤波(简易版)
c语言·卡尔曼滤波·嵌入式算法·工业级组件
xz驱动分享13 小时前
Linux DMA 子系统学习笔记
linux·dma·rk3588·嵌入式软件
888CC++13 小时前
C语言与C++的区别:从面向过程到面向对象
java·c语言·c++
w678200714 小时前
Prisma不能优雅的支持DTO,试试Vona ORM吧
linux·运维·ubuntu
Darkwanderor14 小时前
Linux系统编程实战项目:模拟实现shell
linux·c++
库玛西15 小时前
C++ 运行时多态 :核心总结与原理图解
c语言·c++·笔记
杨某不才15 小时前
如何能让Linux服务器对shell 终端 + sftp 文件传输长期保活
linux·运维·服务器
vance0416 小时前
免费Cloudflare隧道隐藏公网IP
linux·tcp/ip·github