服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C语言实现BMP图像的读写功能

C语言实现BMP图像的读写功能

2021-11-01 14:35小丑程序员 C/C++

这篇文章主要介绍了C语言实现BMP图像的读写功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

C语言实现BMP图像的读写

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

了解BMP的构成

C语言实现BMP图像的读写功能

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

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

代码

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#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图

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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函数调用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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图像转换网址

到此这篇关于C语言实现BMP图像的读写功能的文章就介绍到这了,更多相关C语言BMP图像读写内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_47364956/article/details/115420772

延伸 · 阅读

精彩推荐