linux 565显示格式,RGB565转BMP格式 C语言程序

#include

#include

#include

#include"rgb2bmp.h"

int RGB2BMP(char *,int ,int ,FILE *);

int main(int argc,char *argv[]){

double  num_tmp = 0;

FILE *p;

/***************  input data  ***********

filename      :RGB数据文件名称

nWidth        :所生成文件的水平像素

nHeight       :所生成文件的垂直像素

newFile       :最终生成文件的名称

**********************************************/

size_t read_size;

size_t write_size;

printf("argv[1]= %s\n",argv[1]);

printf("argv[2]= %s\n",argv[2]);

char * filename = "rgb565_800_480_woman";

int nWidth = 800;

int nHeight = 480;

char *newFile = "rgb565_800_480_woman_0x7f.bmp";

if(argc!=3){

printf("Usage:./a.out \n");

return -1;

}

filename = argv[1];

newFile = argv[2];

p = fopen(filename,"rb");

if( p == NULL){

printf("open file %s error \n",filename);

return 0;

}

printf("open file %s success\n",filename);

/*************** read Image Data **********/

long nData = nWidth *nHeight;

unsigned short * rgb_buffer = malloc(nData * sizeof(short));

//read_size = fread(rgb_buffer,2,nData,p);

//printf("rgb_buffer[384000] = %d\n",*(rgb_buffer+383980));

read_size = fread(rgb_buffer,2,384000,p);

//printf("rgb_buffer[384000] = %d\n",*(rgb_buffer+383980));

printf("fread 读取到的字节数是 %ld\n",read_size);

unsigned long total = nWidth*nHeight*3;

unsigned char *pVisit = malloc(total*sizeof(char));

unsigned char *tmp = pVisit;

long i = 0;

unsigned char R,G,B;

unsigned short *free1= rgb_buffer;

unsigned char *free2= pVisit;

while(i

R = *rgb_buffer&0x1f;

G = (*rgb_buffer>>5)&0x3f;

B = (*rgb_buffer>>11)&0x1f;

/*

B<<3;

G<<2;

R<<3;

*/

num_tmp = R;

num_tmp/= 31;

R = num_tmp * 255;

num_tmp = G;

num_tmp/= 63;

G = num_tmp * 255;

num_tmp = B;

num_tmp/= 31;

B = num_tmp * 255;

*pVisit = R;pVisit++;

*pVisit = G;pVisit++;

*pVisit = B;pVisit++;

rgb_buffer++;

i++;

}

//free(free1);

printf("read file over. nData = %ld\n",nData);

fclose(p);

p = NULL;

/*************************************/

/**************** write file *********/

FILE *result = fopen(newFile,"wb");

if(result == NULL){

printf("open new file failed");

return -1;

}

RGB2BMP(tmp,nWidth,nHeight,result);

printf("total = %ld\n",total);

/*

write_size =  fwrite(((char *)pVisit),1,total/3,result);

printf("write_size = %ld \n",write_size);

write_size =  fwrite(((char *)pVisit)+total/3,1,total/3,result);

printf("write_size = %ld \n",write_size);

write_size =  fwrite(((char *)pVisit)+total/3*2,1,total/3,result);

*/

write_size =  fwrite(free2,1,1152000,result);

printf("write_size = %ld \n",write_size);

/*

write_size =  fwrite(pVisit+752000,1,500000,result);

printf("write_size = %ld \n",write_size);

*/

fclose(result);

result = NULL;

free(free2);

return 0;

}

int RGB2BMP(char *rgb_buffer,int nWidth,int nHeight,FILE *fp1){

BmpHead m_BMPHeader;

char bfType[2] = {'B','M'};

m_BMPHeader.imageSize = 3*nWidth*nHeight + 54;

m_BMPHeader.blank=0;

m_BMPHeader.startPosition=54;

//m_BMPHeader.startPosition=1078;

fwrite(bfType,sizeof(bfType),1,fp1);

fwrite(&m_BMPHeader.imageSize,sizeof(m_BMPHeader.imageSize),1,fp1);

fwrite(&m_BMPHeader.blank,sizeof(m_BMPHeader.blank),1,fp1);

fwrite(&m_BMPHeader.startPosition,sizeof(m_BMPHeader.startPosition),1,fp1);

InfoHead m_BMPInfoHeader;

m_BMPInfoHeader.Length=40;

m_BMPInfoHeader.width = nWidth;

m_BMPInfoHeader.height = nHeight;

m_BMPInfoHeader.colorPlane = 1;

m_BMPInfoHeader.bitColor = 24;

m_BMPInfoHeader.zipFormat = 0;

m_BMPInfoHeader.realSize = 3*nHeight*nWidth;

m_BMPInfoHeader.xPels=2835;

m_BMPInfoHeader.yPels=2835;

m_BMPInfoHeader.colorUse=0;

m_BMPInfoHeader.colorImportant=0;

fwrite(&m_BMPInfoHeader.Length,sizeof(m_BMPInfoHeader.Length),1,fp1);

fwrite(&m_BMPInfoHeader.width,sizeof(m_BMPInfoHeader.width),1,fp1);

fwrite(&m_BMPInfoHeader.height,sizeof(m_BMPInfoHeader.height),1,fp1);

fwrite(&m_BMPInfoHeader.colorPlane,sizeof(m_BMPInfoHeader.colorPlane),1,fp1);

fwrite(&m_BMPInfoHeader.bitColor,sizeof(m_BMPInfoHeader.bitColor),1,fp1);

fwrite(&m_BMPInfoHeader.zipFormat,sizeof(m_BMPInfoHeader.zipFormat),1,fp1);

fwrite(&m_BMPInfoHeader.realSize,sizeof(m_BMPInfoHeader.realSize),1,fp1);

fwrite(&m_BMPInfoHeader.xPels,sizeof(m_BMPInfoHeader.xPels),1,fp1);

fwrite(&m_BMPInfoHeader.yPels,sizeof(m_BMPInfoHeader.yPels),1,fp1);

fwrite(&m_BMPInfoHeader.colorUse,sizeof(m_BMPInfoHeader.colorUse),1,fp1);

fwrite(&m_BMPInfoHeader.colorImportant,sizeof(m_BMPInfoHeader.colorImportant),1,fp1);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值