Linux平台C语言保存RGB数据为BMP文件

前言

在图像处理领域,RGB格式是经常使用的一种格式,BMP格式文件是较常见的一种图像文件。
BMP格式文件的优缺点:

  • 优点:兼容性好,方便处理,画质无损
  • 缺点:文件体积较大

目录

  • 文件头结构体
  • 信息头结构体
  • 生成BMP文件

C代码

#include <linux/fb.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <getopt.h>
#include <strings.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>

typedef unsigned short WORD;
typedef unsigned int DWORD;

// 文件头结构体
typedef struct {
    WORD bfType;
    DWORD bfSize;
    WORD bfReserved1;
    WORD bfReserved2;
    DWORD bfOffBits;
} __attribute__((packed)) BITMAPFILEHEADER;

// 信息头结构体
typedef struct {
    DWORD biSize; /*info header size in bytes*/
    DWORD biWidth; /*widht of image*/
    DWORD biHeight;/*height of image*/
    WORD biPlanes;/*number of colour planes*/
    WORD biBitCount;/*bits per pixel*/
    DWORD biCompression;/*compression type*/
    DWORD biSizeImage;/*image size meter*/
    DWORD biXPelsPerMeter;/*pixels per meter*/
    DWORD biYPelsPerMeter;/*pexels per meter*/
    DWORD biClrUsed;/*number of colour*/
    DWORD biClrImportant;/*important colour*/
} __attribute__((packed)) BITMAPINFOHEADER;

// 生成BMP文件
int imageBmp(unsigned char* rgbBytes, int width, int height, const char *filePath)
{
    int _bmppitch = ((width * 24 + 31) >> 5) << 2;
    unsigned char *_bmpBuffer = (unsigned char *) malloc(_bmppitch);
    memset(_bmpBuffer, 0, _bmppitch);

    BITMAPFILEHEADER    bmfh;
    BITMAPINFOHEADER    bmih;

    ((char *) &bmfh.bfType)[0] = 'B';
    ((char *) &bmfh.bfType)[1] = 'M';
    bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + _bmppitch * height;
    bmfh.bfReserved1 = 0;
    bmfh.bfReserved2 = 0;
    bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

    bmih.biSize = sizeof(BITMAPINFOHEADER);
    bmih.biWidth = width;
    bmih.biHeight = height;
    bmih.biPlanes = 1;
    bmih.biBitCount = 24;
    bmih.biCompression = 0;
    bmih.biSizeImage = 0; /*说明图像的大小,以字节为单位。当用BI_RGB格式时,总设置为0*/
    bmih.biXPelsPerMeter = 0; /*缺省值*/
    bmih.biYPelsPerMeter = 0;
    bmih.biClrUsed = 0; /*说明位图实际使用的调色板索引数,0:使用所有的调色板索引*/
    bmih.biClrImportant = 0; /*说明对图像显示有重要影响的颜色索引的数目,如果是0,表示都重要*/

    FILE* image_file = fopen(filePath, "wb");
    if (NULL == image_file)
    {
        //LOGE("imageBmp(): file INVALID, FILE_NAME= %s", filePath);
        free(_bmpBuffer);
        _bmpBuffer = NULL;
        return -1;
    }

    fwrite(&bmfh, sizeof(BITMAPFILEHEADER), 1, image_file);
    fwrite(&bmih, sizeof(BITMAPINFOHEADER), 1, image_file);

    unsigned char _r, _g, _b;
    for (int y = height - 1; y >= 0; y--)
    {
        memset(_bmpBuffer, 0, _bmppitch);
        for (int x = 0; x < width; x++) {
            _bmpBuffer[x * 3 + 0] = rgbBytes[(y * width + x) * 3 + 2];
            _bmpBuffer[x * 3 + 1] = rgbBytes[(y * width + x) * 3 + 1];
            _bmpBuffer[x * 3 + 2] = rgbBytes[(y * width + x) * 3 + 0];
        }
        fwrite(_bmpBuffer, _bmppitch, 1, image_file);
    }

    fclose(image_file);

    free(_bmpBuffer);
    _bmpBuffer = NULL;

    return 0;
}

觉得有用的话,可以给我点奖励哦!(微信)
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lichaofan2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值