问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

获取位图的信息头,文件头,颜色表

发布网友 发布时间:2022-05-06 16:25

我来回答

2个回答

热心网友 时间:2023-10-11 20:44

就是几个结构体,从文件读出来,就可以了
loadbmp.h

#ifndef _LOADBMP_H_
#define _LOADBMP_H_
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;

typedef struct {
/* BITMAPFILEHEADER*/
BYTE bfType[2];
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BMPFILEHEAD;

#define FILEHEADSIZE 14

/* windows style*/
typedef struct {
/* BITMAPINFOHEADER*/
DWORD BiSize;
DWORD BiWidth;
DWORD BiHeight;
WORD BiPlanes;
WORD BiBitCount;
DWORD BiCompression;
DWORD BiSizeImage;
DWORD BiXpelsPerMeter;
DWORD BiYpelsPerMeter;
DWORD BiClrUsed;
DWORD BiClrImportant;
} BMPINFOHEAD;

#define INFOHEADSIZE 40

typedef struct _BMP {
BMPINFOHEAD info;
unsigned char *rgba;
unsigned char *yuy2;
unsigned char *yv12;
} BMP, *PBMP;

int LoadBMP(char *name, PBMP pbmp);
int ReleaseBMP(PBMP pbmp);
void rgb_to_yuv(unsigned int r, unsigned int g, unsigned int b,
unsigned int *y, unsigned int *u, unsigned int *v);
#endif/*_LOADBMP_H_*/
loadbmp.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#include "loadbmp.h"

void rgb_to_yuv(unsigned int r, unsigned int g, unsigned int b,
unsigned int *y, unsigned int *u, unsigned int *v)
{
double dy, , dv;

dy = (0.257 * (double)r) +
(0.504 * (double)g) + (0.098 * (double)b) + 16.0;
dv= (0.439 * (double)r) - (0.368 * (double)g) - (0.071 * (double)b) +
128.0;
= -(0.148 * (double)r) - (0.291 * (double)g) +
(0.439 * (double)b) + 128.0;

*y = (unsigned int)dy & 0xff;
*v = (unsigned int)dv & 0xff;
*u = (unsigned int) & 0xff;
}

static void
convert_to_yv12(PBMP pbmp)
{
int x, y;
unsigned char *in_ptr, *out_ptr_y, *out_ptr_u, *out_ptr_v;
unsigned int Y, U, V;

in_ptr = pbmp->rgba;
out_ptr_y = pbmp->yv12;
out_ptr_u = out_ptr_y + pbmp->info.BiWidth * pbmp->info.BiHeight;
out_ptr_v = out_ptr_u + pbmp->info.BiWidth * pbmp->info.BiHeight / 4;
for (y = 0; y < pbmp->info.BiHeight; y++) {
for (x = 0; x < pbmp->info.BiWidth; x++) {
rgb_to_yuv(in_ptr[0], in_ptr[1], in_ptr[2], &Y, &U, &V);
in_ptr += pbmp->info.BiBitCount / 8;
*out_ptr_y++ = Y;
if (x % 2 == 0 && y % 2 == 0) {
*out_ptr_u++ = V;
*out_ptr_v++ = U;
}
}
}
}

static void
convert_to_yuy2(PBMP pbmp)
{
int x, y;
unsigned char *in, *out;
unsigned int Y, U, V;

in = pbmp->rgba;
out = pbmp->yuy2;
for (y = 0; y < pbmp->info.BiHeight; y++) {
for (x = 0; x < pbmp->info.BiWidth; x++) {
static int cnt = 0;
rgb_to_yuv(in[0], in[1], in[2], &Y, &U, &V);
in += pbmp->info.BiBitCount / 8;
*(out) = Y;
if (cnt %2 == 0)
*(out+1) = V;
else
*(out+1) = U;
out+= 2;
cnt++;
}
}
}

int LoadBMP(char *name, PBMP pbmp)
{
int fd;
int headsize;
BMPFILEHEAD bmpf;
unsigned char headbuffer[INFOHEADSIZE];

fd = open(name, O_RDONLY);
if (fd < 0) {
perror("open");
return -1;
}

read(fd, &headbuffer, FILEHEADSIZE);
bmpf.bfType[0] = headbuffer[0];
bmpf.bfType[1] = headbuffer[1];
if (*(WORD*)&bmpf.bfType[0] != 0x4D42) /* 'BM' */
return -2; /* not bmp image*/

bmpf.bfOffBits = *((DWORD*)&headbuffer[10]);

if (read(fd,&headsize,sizeof(DWORD)) != sizeof(DWORD))
return 0; /* not bmp image*/

read(fd, &headbuffer, INFOHEADSIZE - sizeof(DWORD));
pbmp->info.BiWidth = *(DWORD*)(&headbuffer[0]);
pbmp->info.BiHeight = *(DWORD*)(&headbuffer[4]);
pbmp->info.BiBitCount = *(DWORD*)(&headbuffer[10]);
pbmp->info.BiSizeImage = *(DWORD*)(&headbuffer[16]);

pbmp->rgba = (unsigned char *)malloc(pbmp->info.BiSizeImage);
if (!pbmp->rgba) {
perror("malloc");
exit(EXIT_FAILURE);
}
// pbmp->yuy2 = (unsigned char *)malloc(pbmp->info.BiWidth * pbmp->info.BiHeight * 2);
// if (!pbmp->yuy2) {
// perror("malloc");
// exit(EXIT_FAILURE);
// }
// pbmp->yv12 = (unsigned char *)malloc(pbmp->info.BiWidth * pbmp->info.BiHeight * 3 / 2);
// if (!pbmp->yv12) {
// perror("malloc");
// exit(EXIT_FAILURE);
// }

lseek(fd, bmpf.bfOffBits, SEEK_SET);
if (read(fd, pbmp->rgba, pbmp->info.BiSizeImage) != pbmp->info.BiSizeImage) {
perror("read");
return -4;
}

// convert_to_yv12(pbmp);
// convert_to_yuy2(pbmp);
printf("LoadBMP: %s %ldx%ldx%d\n", name, pbmp->info.BiWidth, pbmp->info.BiHeight, pbmp->info.BiBitCount);
close(fd);

return 0;
}

int ReleaseBMP(PBMP pbmp)
{
if (pbmp->rgba) {
free(pbmp->rgba);
pbmp->rgba = NULL;
}
// if (pbmp->yuy2) {
// free(pbmp->yuy2);
// pbmp->yuy2 = NULL;
// }
// if (pbmp->yv12) {
// free(pbmp->yv12);
// pbmp->yv12 = NULL;
// }
return 0;
}

参考资料:hi.baidu.com/xqs83

热心网友 时间:2023-10-11 20:44

就是几个结构体,从文件读出来,就可以了
loadbmp.h

#ifndef _LOADBMP_H_
#define _LOADBMP_H_
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;

typedef struct {
/* BITMAPFILEHEADER*/
BYTE bfType[2];
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BMPFILEHEAD;

#define FILEHEADSIZE 14

/* windows style*/
typedef struct {
/* BITMAPINFOHEADER*/
DWORD BiSize;
DWORD BiWidth;
DWORD BiHeight;
WORD BiPlanes;
WORD BiBitCount;
DWORD BiCompression;
DWORD BiSizeImage;
DWORD BiXpelsPerMeter;
DWORD BiYpelsPerMeter;
DWORD BiClrUsed;
DWORD BiClrImportant;
} BMPINFOHEAD;

#define INFOHEADSIZE 40

typedef struct _BMP {
BMPINFOHEAD info;
unsigned char *rgba;
unsigned char *yuy2;
unsigned char *yv12;
} BMP, *PBMP;

int LoadBMP(char *name, PBMP pbmp);
int ReleaseBMP(PBMP pbmp);
void rgb_to_yuv(unsigned int r, unsigned int g, unsigned int b,
unsigned int *y, unsigned int *u, unsigned int *v);
#endif/*_LOADBMP_H_*/
loadbmp.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#include "loadbmp.h"

void rgb_to_yuv(unsigned int r, unsigned int g, unsigned int b,
unsigned int *y, unsigned int *u, unsigned int *v)
{
double dy, , dv;

dy = (0.257 * (double)r) +
(0.504 * (double)g) + (0.098 * (double)b) + 16.0;
dv= (0.439 * (double)r) - (0.368 * (double)g) - (0.071 * (double)b) +
128.0;
= -(0.148 * (double)r) - (0.291 * (double)g) +
(0.439 * (double)b) + 128.0;

*y = (unsigned int)dy & 0xff;
*v = (unsigned int)dv & 0xff;
*u = (unsigned int) & 0xff;
}

static void
convert_to_yv12(PBMP pbmp)
{
int x, y;
unsigned char *in_ptr, *out_ptr_y, *out_ptr_u, *out_ptr_v;
unsigned int Y, U, V;

in_ptr = pbmp->rgba;
out_ptr_y = pbmp->yv12;
out_ptr_u = out_ptr_y + pbmp->info.BiWidth * pbmp->info.BiHeight;
out_ptr_v = out_ptr_u + pbmp->info.BiWidth * pbmp->info.BiHeight / 4;
for (y = 0; y < pbmp->info.BiHeight; y++) {
for (x = 0; x < pbmp->info.BiWidth; x++) {
rgb_to_yuv(in_ptr[0], in_ptr[1], in_ptr[2], &Y, &U, &V);
in_ptr += pbmp->info.BiBitCount / 8;
*out_ptr_y++ = Y;
if (x % 2 == 0 && y % 2 == 0) {
*out_ptr_u++ = V;
*out_ptr_v++ = U;
}
}
}
}

static void
convert_to_yuy2(PBMP pbmp)
{
int x, y;
unsigned char *in, *out;
unsigned int Y, U, V;

in = pbmp->rgba;
out = pbmp->yuy2;
for (y = 0; y < pbmp->info.BiHeight; y++) {
for (x = 0; x < pbmp->info.BiWidth; x++) {
static int cnt = 0;
rgb_to_yuv(in[0], in[1], in[2], &Y, &U, &V);
in += pbmp->info.BiBitCount / 8;
*(out) = Y;
if (cnt %2 == 0)
*(out+1) = V;
else
*(out+1) = U;
out+= 2;
cnt++;
}
}
}

int LoadBMP(char *name, PBMP pbmp)
{
int fd;
int headsize;
BMPFILEHEAD bmpf;
unsigned char headbuffer[INFOHEADSIZE];

fd = open(name, O_RDONLY);
if (fd < 0) {
perror("open");
return -1;
}

read(fd, &headbuffer, FILEHEADSIZE);
bmpf.bfType[0] = headbuffer[0];
bmpf.bfType[1] = headbuffer[1];
if (*(WORD*)&bmpf.bfType[0] != 0x4D42) /* 'BM' */
return -2; /* not bmp image*/

bmpf.bfOffBits = *((DWORD*)&headbuffer[10]);

if (read(fd,&headsize,sizeof(DWORD)) != sizeof(DWORD))
return 0; /* not bmp image*/

read(fd, &headbuffer, INFOHEADSIZE - sizeof(DWORD));
pbmp->info.BiWidth = *(DWORD*)(&headbuffer[0]);
pbmp->info.BiHeight = *(DWORD*)(&headbuffer[4]);
pbmp->info.BiBitCount = *(DWORD*)(&headbuffer[10]);
pbmp->info.BiSizeImage = *(DWORD*)(&headbuffer[16]);

pbmp->rgba = (unsigned char *)malloc(pbmp->info.BiSizeImage);
if (!pbmp->rgba) {
perror("malloc");
exit(EXIT_FAILURE);
}
// pbmp->yuy2 = (unsigned char *)malloc(pbmp->info.BiWidth * pbmp->info.BiHeight * 2);
// if (!pbmp->yuy2) {
// perror("malloc");
// exit(EXIT_FAILURE);
// }
// pbmp->yv12 = (unsigned char *)malloc(pbmp->info.BiWidth * pbmp->info.BiHeight * 3 / 2);
// if (!pbmp->yv12) {
// perror("malloc");
// exit(EXIT_FAILURE);
// }

lseek(fd, bmpf.bfOffBits, SEEK_SET);
if (read(fd, pbmp->rgba, pbmp->info.BiSizeImage) != pbmp->info.BiSizeImage) {
perror("read");
return -4;
}

// convert_to_yv12(pbmp);
// convert_to_yuy2(pbmp);
printf("LoadBMP: %s %ldx%ldx%d\n", name, pbmp->info.BiWidth, pbmp->info.BiHeight, pbmp->info.BiBitCount);
close(fd);

return 0;
}

int ReleaseBMP(PBMP pbmp)
{
if (pbmp->rgba) {
free(pbmp->rgba);
pbmp->rgba = NULL;
}
// if (pbmp->yuy2) {
// free(pbmp->yuy2);
// pbmp->yuy2 = NULL;
// }
// if (pbmp->yv12) {
// free(pbmp->yv12);
// pbmp->yv12 = NULL;
// }
return 0;
}

参考资料:hi.baidu.com/xqs83

热心网友 时间:2023-10-11 20:45

如何得到?

不就就是一幅位图的头部数据嘛!

你根据位图文件的文件格式,对位图数据,进行解析一下,不就可以了啊

热心网友 时间:2023-10-11 20:45

如何得到?

不就就是一幅位图的头部数据嘛!

你根据位图文件的文件格式,对位图数据,进行解析一下,不就可以了啊
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
Linux系统安装FTP服务器 Linux系统的网络文件共享 建筑的七盏明灯的内容简介 面向对象设计七大原则 简单说 交互设计七大定律 交互设计的“根”——七大定律 交互设计原则和理论2——七大定律 七大设计原则 附近的加油站有哪些 附近的加油站有哪些地方 谁知道正规的ps在那下载的 有什么方法在详细信息头看文件夹大小 如何用vc++实现:查找A文件夹下所有*.doc文件,并将每一个*.doc文件路径显示出来? 周公解梦梦见自己家的一口屋里有很多的老鼠,本来在一个小容器里,不小心打翻变出好多好多的老鼠… MFC怎么显示bmp图像文件头和图像信息头? 晚上做梦梦到屋里跑跑老鼠是怎么回事 腾讯课堂别人可以用自己的登录上去吗 腾讯课堂别人可以用自己的登录上去吗 苹果6手机在车载充电的情况下使用导航无语音提示是什么原因?如何解决? 苹果6手机百度地图导航一插着车载usb充电就没声音了,怎么回事啊?充电线拔掉后导航声音又出来,跑长 什么音乐适合作森林防火宣传的片头 大金刚菩提子怎么盘红,我是新手,纯小白,请大神仔细的讲解,怎么保护,存放,盘红金刚子,详细一点,谢 哪里能配森林防火宣传声音? 公益短片:森林防火知识宣传 感悟青春的散文读后感700字 05或06年的青年文摘。寻一篇青春校园文章~ 求几本青春散文。 如何自己做海苔 自制海苔,5元紫菜做成50元的海苔,做法简单,再也不花钱买海苔 手工自制海苔,5块紫菜变身50块的海苔,做法简单,比买的还好吃 ...就是做一个学生信息管理系统,信息头是什么意思怎么加? 显示EXE文件的头信息 有没有一款软件可以查看各种格式的文件的文件头信息 微信升级到最新版2015新年 请教:网页文件头部有什么标签?具体功能是什么?在什么网址可找到相关信息... 关于PE文件的头部信息读取,编程实现~~? 如何读rmvb文件头部信息 php文件下载头信息需要哪些内容。 新年机上微信登陆设备管理怎么有旧手机的登陆时间与型号? 微信 系统保护状态怎么关闭 我的手机卸载了自带的微信系统,但被我卸载了一直就安装不了,提示安装版本比原版本低,怎么办? 统组词和拼音 统的拼音 夹胶钢化玻璃8+8一平方多重? 统计的统字怎么组词 12mm+8mm夹胶玻璃一平米有多重 10乘10钢化夹胶玻璃能承受多大重量? 钢化玻璃承重怎么测算 - 信息提示 梦见两只狗咬架而且贱了一身血