将YUV或RGB的格式的数据保存为bmp文件

该博客介绍了如何将YUV或RGB格式的数据转换并保存为BMP文件,包括定义BMP文件头和信息头结构,利用ffmpeg库进行像素格式转换,并在必要时对图像进行反转操作。通过创建BMP文件头和信息头,填充数据并调整图像顺序,最终生成符合BMP格式的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#pragma pack (push ,1)


typedef struct { /* bmfh */

uint16_t bfType;  

uint32_t bfSize; 

uint16_t bfReserved1; 

uint16_t bfReserved2; 

uint32_t bfOffBits;

} BMP_FILE_HDR;


typedef struct { /* bmih */

uint32_t biSize; 

uint32_t biWidth; 

uint32_t biHeight; 

uint16_t biPlanes; 

uint16_t biBitCount; 

uint32_t biCompression; 

uint32_t biSizeImage; 

uint32_t biXPelsPerMeter; 

uint32_t biYPelsPerMeter; 

uint32_t biClrUsed; 

uint32_t biClrImportant;

} BMP_INFO_HDR;


#pragma pack (pop)


//src 原数据

//fmt 原数据格式

//len 获取返回的bmp数据大小

//reverse 是否需要将图片反转

//返回值 指向bmp数据,使用完需要free

uint8_t *create_bmp(uint8_t *src, enum PixelFormat fmt, int width, int height, int *len, bool reverse)

{

AVFrame *frame_src = avcodec_alloc_frame();

AVFrame *frame_dst = avcodec_alloc_frame();

enum PixelFormat bmp_fmt = PIX_FMT_BGR24;

int bytes = avpicture_get_size(bmp_fmt, width, height);

*len = bytes + sizeof(BMP_FILE_HDR) + sizeof(BMP_INFO_HDR);

uint8_t *dst_data = (uint8_t *)malloc(*len);

memset(dst_data, 0x0, *len);

uint8_t *img_data = dst_data + sizeof(BMP_FILE_HDR) + sizeof(BMP_INFO_HDR);

bool is_cvt = false;

if (fmt == bmp_fmt) {

memcpy(img_data, src, bytes);

is_cvt = true;

}

else {

avpicture_fill((AVPicture *)frame_dst, img_data, bmp_fmt, width, height);

avpicture_fill((AVPicture *)frame_src, src, fmt, width, height);

struct SwsContext *sws = 

sws_getContext(width, height, fmt,

  width, height, bmp_fmt, 

  SWS_FAST_BILINEAR, NULL, NULL, NULL);

if (sws) {

int h = sws_scale(sws, (const uint8_t** const)frame_src->data, frame_src->linesize

  0, height, frame_dst->data, frame_dst->linesize);

if (h == height) {

is_cvt = true;

}

sws_freeContext(sws);

}

av_free(frame_src);

av_free(frame_dst);

}

if (!is_cvt) {

free(dst_data);

return NULL;

}

if (reverse) {

int line_bytes = bytes / height;

uint8_t *buf_tmp = (uint8_t *)malloc(line_bytes);

for (int j = 0; j < height / 2; j++) {

memcpy(buf_tmp, img_data + line_bytes * j, line_bytes);

memcpy(img_data + line_bytes * j, img_data + line_bytes * 

  (height - j - 1), line_bytes);

memcpy(img_data + line_bytes * (height - j - 1), buf_tmp, line_bytes);

}

free(buf_tmp);

}

BMP_FILE_HDR *fh = (BMP_FILE_HDR *)dst_data;

BMP_INFO_HDR *bh = (BMP_INFO_HDR *)(dst_data + sizeof(BMP_FILE_HDR));

fh->bfType = 0x4D42;

fh->bfSize = sizeof(BMP_FILE_HDR) + sizeof(BMP_INFO_HDR) + bytes;

fh->bfOffBits = sizeof(BMP_FILE_HDR) + sizeof(BMP_INFO_HDR);

bh->biSize = sizeof(BMP_INFO_HDR);

bh->biWidth = width;

bh->biHeight = height;

bh->biPlanes = 1;

bh->biBitCount = 24;

bh->biCompression = 0;

bh->biSizeImage = 0;

bh->biXPelsPerMeter = 0;

bh->biYPelsPerMeter = 0;

bh->biClrUsed = 0;

bh->biClrImportant = 0;

return dst_data;

}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值