C语言实现BMP图像的读写

C语言实现BMP图像的读写

对于刚接触数字图像的同学,应该都有一个疑问,如何把一个BMP格式的图像用纯C语言读入呢,我相信这也是数字图像处理的第一步,如果有幸看到这篇文档,我就有幸的成为你数字图像处理路上的第一盏明灯!

了解BMP的构成

在这里插入图片描述

    这就是BMP图像的理论知识,有个大概的了解就行,最主要的是从理论到实践!!!

废话不多说,直接上干货。

代码

定义头文件为“bmp.h”,定义read_bmp函数为读函数,write_bmp函数为写函数
读bmp图

#include <stdlib.h>
#include <math.h>
#include <Windows.h>
#include "bmp.h"
/*存储原图的像素宽度高度和位图深度*/
FILE* fpbmp;
FILE* fpout;
unsigned char* fpBmpHeader;   //位图头
unsigned char* fpFileHeader;  //位图信息
RGBQUAD* pColorTable;         //BMP 调色板
int read_bmp(const char* path, unsigned char *pBmpBuf,int *Width,int *Height,int * bitCount)
{
	fpbmp = fopen(path, "rb");//path为图像路径
	unsigned short s;
	fread(&s, 1, 2, fpbmp);
	//判断读入的图像是否为BMP图  字符串"BM"=19778
	if (s == 19778)
	{
		printf("Open bmp success!!!\n");
	}
	else
	{
		printf("Open bmp fail!!!\n");
		return -1;
	}
	fseek(fpbmp, 0, SEEK_SET);
	BITMAPFILEHEADER fileHead;
	fread(&fileHead, sizeof(BITMAPFILEHEADER), 1, fpbmp);

	BITMAPINFOHEADER infoHead;
	fread(&infoHead, sizeof(BITMAPINFOHEADER), 1, fpbmp);

	*Width = infoHead.biWidth;//图像的宽
	*Height = infoHead.biHeight;//图像的高
	*bitCount = infoHead.biBitCount;
	int lineByte = (*Width * *bitCount / 8 + 3) / 4 * 4;
	fseek(fpbmp, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) ,SEEK_SET);
	fread(pBmpBuf, lineByte * *Height, 1, fpbmp);//pBmpBuf为图像的RGB数据,也是我们将要处理的数据
	return 0;
}

写BMP图

int write_bmp(unsigned char* img, int* Width, int* Height, int* bitCount)
{
	fpout = fopen("out.bmp", "wb+");
	if (fpbmp == NULL)
	{
		printf("read bmp failed!!!\n");
		return -1;
	}
	int lineByte = (*Width * *bitCount / 8 + 3) / 4 * 4;
	if (lineByte == 0)
	{
		printf("err");
		return -1;
	}
	fpFileHeader = new unsigned char[(sizeof(BITMAPFILEHEADER))];
	fseek(fpbmp, 0, SEEK_SET);  //定位原图 偏移位置
	fseek(fpout, 0, SEEK_SET);  //定位新图 偏移位置
	fread(fpFileHeader, 1, sizeof(BITMAPFILEHEADER), fpbmp);
	fwrite(fpFileHeader, 1, sizeof(BITMAPFILEHEADER), fpout);


	/*复制原图中 位图 信息到新图像*/
	fpBmpHeader = new unsigned char[(sizeof(BITMAPINFOHEADER))];
	fseek(fpbmp, sizeof(BITMAPFILEHEADER), SEEK_SET);
	fseek(fpout, sizeof(BITMAPFILEHEADER), SEEK_SET);
	fread(fpBmpHeader, 1, sizeof(BITMAPINFOHEADER), fpbmp);
	fwrite(fpBmpHeader, 1, sizeof(BITMAPINFOHEADER), fpout);
	
	fseek(fpout, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) , SEEK_SET);
	fwrite(img, lineByte * *Height, sizeof(char), fpout);
	fclose(fpout);
	fclose(fpbmp);
	return 0;
}

main函数调用

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <Windows.h>
#include "bmp.h"

int main()
{
	int width, height, bitCount = 0;
	unsigned char* pBmpBuf = (unsigned char*)malloc(1000 * 1000 * 3);//申请空间
	const char* path = "D:\\test\\read_bmp_image\\1-B.bmp";//图的路径
	read_bmp(path, pBmpBuf, &width, &height, &bitCount);
	write_bmp(pBmpBuf, &width, &height, &bitCount);
}

总结,将read_bmp函数返回的pBmpBuf参数,赋值给write_bmp函数的img参数,就实现了BMP图从读到写的全部过程,有兴趣的同学动手实践下,会有意向不到的收获。
注:在线转换BMP图的网址,可以将任何格式的照片转换为BMP格式。亲测好用。链接奉上BMP图像转换网址

  • 13
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
以下是使用STM32F103C8T6控制LCD屏幕显示图片的代码和bmp.h文件。这个例子使用了TFT库和一个bmp文件作为示例。 bmp.h文件: ``` #ifndef __BMP_H #define __BMP_H #include <stdint.h> #pragma pack(push, 1) typedef struct { uint16_t bfType; uint32_t bfSize; uint16_t bfReserved1; uint16_t bfReserved2; uint32_t bfOffBits; } BMPFileHeader; typedef struct { uint32_t biSize; int32_t biWidth; int32_t biHeight; uint16_t biPlanes; uint16_t biBitCount; uint32_t biCompression; uint32_t biSizeImage; int32_t biXPelsPerMeter; int32_t biYPelsPerMeter; uint32_t biClrUsed; uint32_t biClrImportant; } BMPInfoHeader; #pragma pack(pop) void BMP_Draw(char *filename, uint16_t x, uint16_t y); #endif /* __BMP_H */ ``` 代码: ``` #include "stm32f10x.h" #include "tft.h" #include "bmp.h" int main(void) { TFT_Init(); BMP_Draw("image.bmp", 0, 0); while(1); } void BMP_Draw(char *filename, uint16_t x, uint16_t y) { uint8_t header[54]; uint32_t dataOffset, width, height, imageSize; uint16_t bpp; uint8_t *data; uint32_t row, col, pos = 0; FILE *file = fopen(filename, "rb"); if (!file) { return; } fread(header, sizeof(uint8_t), 54, file); dataOffset = header[10] + (header[11] << 8) + (header[12] << 16) + (header[13] << 24); width = header[18] + (header[19] << 8) + (header[20] << 16) + (header[21] << 24); height = header[22] + (header[23] << 8) + (header[24] << 16) + (header[25] << 24); bpp = header[28] + (header[29] << 8); imageSize = header[34] + (header[35] << 8) + (header[36] << 16) + (header[37] << 24); if (bpp != 24 || width > TFT_WIDTH || height > TFT_HEIGHT || !imageSize) { fclose(file); return; } data = (uint8_t*)malloc(imageSize); fseek(file, dataOffset, SEEK_SET); fread(data, sizeof(uint8_t), imageSize, file); fclose(file); for (row = 0; row < height; row++) { for (col = 0; col < width; col++) { pos = (row * width + col) * 3; TFT_DrawPixel(x + col, y + height - row - 1, RGB(data[pos + 2], data[pos + 1], data[pos])); } } free(data); } ``` 这个例子中,我们使用了TFT_Init()函数初始化TFT屏幕,然后使用BMP_Draw()函数从文件中取图片并将其绘制在TFT屏幕上。你需要将一个名为"image.bmp"的BMP文件放在你的工程目录中,然后编译并下载到STM32F103C8T6芯片上。 需要注意的是,这个例子只支持24位BMP文件,如果你需要支持其他格式的图片文件,你需要修改BMP_Draw()函数的代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值