linux 生成bmp,一个在linux下生成bmp程序

分享代码:

很多人都搞不定内存对齐的问题,最近帮老焦他们写测试程序,用到一个BMP GENERATOR,写了个比较简单的版本,仅针对24位真彩,现把代码公布

#include                 using namespace std;

typedef long BOOL;

typedef long LONG;

typedef unsigned char BYTE;

typedef unsigned long DWORD;

typedef unsigned short WORD;

typedef struct {

WORD    bfType;//2

DWORD   bfSize;//4

WORD    bfReserved1;//2

WORD    bfReserved2;//2

DWORD   bfOffBits;//4

}__attribute__((packed))FileHead;

typedef struct{

DWORD      biSize;//4

LONG       biWidth;//4

LONG       biHeight;//4

WORD       biPlanes;//2

WORD       biBitCount;//2

DWORD      biCompress;//4

DWORD      biSizeImage;//4

LONG       biXPelsPerMeter;//4

LONG       biYPelsPerMeter;//4

DWORD      biClrUsed;//4

DWORD      biClrImportant;//4

}__attribute__((packed))Infohead;

/*typedef   struct

{

unsigned   char     rgbBlue;

unsigned   char     rgbGreen;

unsigned   char     rgbRed;

unsigned   char     rgbReserved;

}RGBQuad;//it may be useless*/

typedef struct

{

BYTE b;

BYTE g;

BYTE r;

}RGB_data;//RGB TYPE

int bmp_generator(char * filename,int width,int

height,unsigned char *data)

{

FileHead bmp_head;

Infohead bmp_info;

int size = width*height*3;

//Test data

/*    RGB_Test bmp_data[width][height];

int i,j;

for (i=0;i

for (j=0;j

{

bmp_data[i][j].g=bmp_data[i][j].b=0;

bmp_data[i][j].r=0xff;

}

*/

bmp_head.bfType=0x4d42;

bmp_head.bfSize=size+sizeof(FileHead)+sizeof

(Infohead);//24+head+info no quad

bmp_head.bfReserved1=bmp_head.bfReserved2=0;

bmp_head.bfOffBits=bmp_head.bfSize-size;

//finish the initial of head

bmp_info.biSize=40;

bmp_info.biWidth=width;

bmp_info.biHeight=height;

bmp_info.biPlanes=1;

bmp_info.biBitCount = 24;

bmp_info.biCompress=0;

bmp_info.biSizeImage=size;

bmp_info.biXPelsPerMeter=0;

bmp_info.biYPelsPerMeter=0;

bmp_info.biClrUsed=0;

bmp_info.biClrImportant=0;

//finish the initial of infohead;

//copy the data

//    fstream file3(filename);

FILE *fp;

if(!(fp=fopen(filename,"wb"))) return 0;

//zhunan 2.6.00:58

fwrite(&bmp_head,1,sizeof(FileHead),fp);

fwrite(&bmp_info,1,sizeof(Infohead),fp);

fwrite(data,1,size,fp);

fclose(fp);

return 1;

}

int main(int argc,char **argv)

{

int i,j;

RGB_data buffer[512][512];

//    cout<

cout<

cout<

cout<

height,unsigned char * data);"<

memset(buffer, 0, sizeof(buffer));

//please edit width and height here

//zhunan testdata

//hard coding

for (i=0;i<256;i++)

for (j=0;j<256;j++)

{

//          buffer[i][j].g=buffer[i]

[j].b=0x00;

buffer[i][j].r=0xff;

}

bmp_generator("/home/zhunan/1234.bmp",512,512,

(BYTE*)buffer);

return 1;

}

结束语:

__attribute__((packed))的用法相当重要,它能够使结构体产生内存缩进,就是后一个定义的内存紧接着前面占用的内存。相当于内存即定义变量字节的总和。这在bmp后面写文件的时候极其重要。

24位bmp基本格式字节结构:14+40+320*240*3;

8位bmp基本格式字节分布结构:14+40+320*240*1;

如果想得到8位灰度值bmp图片:

8位bmp灰度值基本格式字节分布结构:14+40+320*240*1+data(调色板+映射);

还在研究中···

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值