用c语言创建bmp,C语言构建BMP文件(一)

#define false 0

#define true 0

typedef struct tagXSurface{

u32 surface;

int width,height;

int pitchWidth,pitch;

u32 *pData;

}XSurface;

XSurface pDemoSurface;

typedef struct tag_bmp_data

{

int width;

int height;

}_bmp_data;

_bmp_data p_my_bmp_data;

FILE *pWritingFile = NULL;

int WriteBmp(XSurface *pInputSurface,char *BmpFileName)

{

int i=0,k=0;

long j=0;

int get_size;

int write_cnt;

char *pTempBuffer=NULL;

int w=pInputSurface->width;

int h=pInputSurface->height;

char *pSurfaceData=(char *)(pInputSurface->pData);

int EVERY_READ_SIZE=pInputSurface->pitchWidth*3;

if((w!=p_my_bmp_data.width)||(h!=p_my_bmp_data.height))

{

JPRINTF(("Write BMP Failed, w( %d != %d ), h( %d != %d ) \n",w,p_my_bmp_data.width,h,p_my_bmp_data.height));

return -1;

}

JPRINTF(("Offset:: %d \n",ftell(pWritingFile)));

pTempBuffer=(char *)XMemAlloc(sizeof(char)*EVERY_READ_SIZE);

if(NULL==pTempBuffer)

{

JPRINTF(("Malloc Memory Failed ... \n"));

return -2;

}

get_size=EVERY_READ_SIZE;

for(k=pInputSurface->height;k>=0;--k)

{

for(i=0,j=0+k*(pInputSurface->pitchWidth)*4;i

{

pTempBuffer[i]=pSurfaceData[j];

pTempBuffer[i+1]=pSurfaceData[j+1];

pTempBuffer[i+2]=pSurfaceData[j+2];

}

write_cnt=fwrite(pTempBuffer, sizeof(char), get_size, pWritingFile);

if(write_cnt!=get_size)

JPRINTF(("### Write Error !###\n"));

}

fclose(pWritingFile);

}

int CreateBmp(long w, long h, char *BmpFileName)

{

#define BMP_Header_Length 54

long ret;

unsigned char header[BMP_Header_Length] = {

0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,

54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0

};

long file_size = (long)w * (long)h * 3 + 54;

header[2] = (unsigned char)(file_size &0x000000ff);

header[3] = (file_size >> 8) & 0x000000ff;

header[4] = (file_size >> 16) & 0x000000ff;

header[5] = (file_size >> 24) & 0x000000ff;

long width = w;

header[18] = width & 0x000000ff;

header[19] = (width >> 8) &0x000000ff;

header[20] = (width >> 16) &0x000000ff;

header[21] = (width >> 24) &0x000000ff;

long height = h;

header[22] = height &0x000000ff;

header[23] = (height >> 8) &0x000000ff;

header[24] = (height >> 16) &0x000000ff;

header[25] = (height >> 24) &0x000000ff;

//FILE *pWritingFile = NULL;

pWritingFile = fopen(BmpFileName, "wb");

if( pWritingFile == NULL )

return false;

fwrite(header, sizeof(unsigned char), 54, pWritingFile);

p_my_bmp_data.width=width;

p_my_bmp_data.height=height;

return true;

}

void creat_snap(char *BmpFileName)

{

CreateBmp(pDemoSurface->width,pDemoSurface->height,BmpFileName);

WriteBmp(pDemoSurface,BmpFileName);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值