加载中…
个人资料
飘落1990
飘落1990
  • 博客等级:
  • 博客积分:0
  • 博客访问:4,422
  • 关注人气:0
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
正文 字体大小:

bmp格式图像转换成YUV格式的图片

(2014-07-02 23:13:12)
标签:

bmp

图像格式转换

#include
#include  
#include 

 
#define TMAPFILEHEADERLENGTH 14 // The bmp FileHeader length is 14
#define BM 19778 // The ASCII code for BM

 
static float RGBYUV02990[256], RGBYUV05870[256], RGBYUV01140[256];
static float RGBYUV01684[256], RGBYUV03316[256];
static float RGBYUV04187[256], RGBYUV00813[256];

 
void InitLookupTable();

 
int RGBtoYUV (int x_dim,int y_dim,void *bmp, void *y_out, void *u_out, void *v_out)
{
long i, j,size;
unsigned char *r, *g, *b;
unsigned char *y, *u, *v;
unsigned char *pu1, *pv1, *psu, *psv;
unsigned char *y_buffer, *u_buffer, *v_buffer;
unsigned char *sub_u_buf, *sub_v_buf;
InitLookupTable();
size=x_dim*y_dim;
y_buffer = (unsigned char *)y_out;
sub_u_buf = (unsigned char *)u_out;
sub_v_buf = (unsigned char *)v_out;
u_buffer = (unsigned char *)malloc(size * sizeof(unsigned char));
v_buffer = (unsigned char *)malloc(size * sizeof(unsigned char));
if (!(u_buffer && v_buffer))
{
if (u_buffer) free(u_buffer);
if (v_buffer) free(v_buffer);
return 2;
}

 
b = (unsigned char *)bmp;
y = y_buffer;
u = u_buffer;
pu1 =u_buffer;
v = v_buffer;
pv1= v_buffer;
psu = sub_u_buf;
psv = sub_v_buf;

 
for (j = 0; j <</SPAN> y_dim; j++){
for (i = 0; i <</SPAN> x_dim; i++) 
{
g = b + 1;
r = b + 2;
*y = (unsigned char)( RGBYUV02990[*r] + RGBYUV05870[*g] + RGBYUV01140[*b]);
*u = (unsigned char)(- RGBYUV01684[*r] - RGBYUV03316[*g] + (*b)/2 + 128);
*v = (unsigned char)( (*r)/2 - RGBYUV04187[*g] - RGBYUV00813[*b] + 128);
b += 3;
y ++;
u ++;
v ++;
}
}

 
for (j=0; j<</SPAN>y_dim/2; j++)
{
for (i=0; i<</SPAN>x_dim/2; i++)
{
*psu=*pu1;
*psv=*pv1;
psu++;
psv++;
pv1+=2;
pu1+=2;
}
pu1+=x_dim;
pv1+=x_dim;
}
free(u_buffer);
free(v_buffer);
}

 
void InitLookupTable( )
{
int i;
for (i = 0; i <</SPAN> 256; i++) RGBYUV02990[i] = (float)0.2990 * i;
for (i = 0; i <</SPAN> 256; i++) RGBYUV05870[i] = (float)0.5870 * i;
for (i = 0; i <</SPAN> 256; i++) RGBYUV01140[i] = (float)0.1140 * i;
for (i = 0; i <</SPAN> 256; i++) RGBYUV01684[i] = (float)0.1684 * i;
for (i = 0; i <</SPAN> 256; i++) RGBYUV03316[i] = (float)0.3316 * i;
for (i = 0; i <</SPAN> 256; i++) RGBYUV04187[i] = (float)0.4187 * i;
for (i = 0; i <</SPAN> 256; i++) RGBYUV00813[i] = (float)0.0813 * i;
}

 
void bmp_file_test(FILE* fpbmp)
{ 
unsigned short bfType = 0; 
fseek(fpbmp, 0L, SEEK_SET);
fread(&bfType, sizeof(char), 2, fpbmp);
if (BM != bfType)
{
printf("This file is not bmp file.!!!\n");
exit(1);
}
}

 
int main(int argc,char **argv)
{
if(argc != 3){
perror("./a.out rgb_name yuv_name");
exit(0);
}
unsigned int width, height, temp;
int i;

 
FILE* bmpfile =NULL;
FILE* rgbtmp = NULL;
FILE* yuvfile =NULL;
unsigned char* rgbbuf =NULL;
unsigned char* ybuf =NULL;
unsigned char* ubuf =NULL;
unsigned char* vbuf =NULL;

 
if((bmpfile = fopen(argv[1], "rb+"))==NULL)
{
printf("cannot find rgb file\n");
exit(1);
}
if((rgbtmp = fopen("temp.rgb", "wb+"))==NULL)
{
printf("cannot find rgb file\n");
exit(1);
}
if((yuvfile = fopen(argv[2],"wb")) == NULL)
{
printf("cannot find yuv file\n");
exit(1);
}

 
bmp_file_test(bmpfile);
fseek(bmpfile, 18L, SEEK_SET);
fread(&temp, sizeof(unsigned int), 1, bmpfile);
width = temp;
fseek(bmpfile, 22L, SEEK_SET);
fread(&temp, sizeof(unsigned int), 1, bmpfile);
height = temp;
printf("width = %d , height = %d\n", width, height);

rgbbuf = (unsigned char*)malloc(width *height*3 );
ybuf = (unsigned char*)malloc(width * height);
ubuf = (unsigned char*)malloc((width * height) / 4);
vbuf = (unsigned char*)malloc((width * height) / 4);
if (rgbbuf == NULL || ybuf == NULL || ubuf == NULL || vbuf == NULL)
{
printf("no enought memory\n");
exit(1);
}
fseek(bmpfile, 54L, SEEK_SET);
fread (rgbbuf, 1, width * height * 3, bmpfile);
fwrite(rgbbuf, 1, width * height * 3, rgbtmp);
fclose(bmpfile);
 
fread(rgbbuf, 1, width * height * 3, rgbtmp);
RGBtoYUV(width, height, rgbbuf, ybuf, ubuf, vbuf);
 
fwrite(ybuf, 1, width*height, yuvfile);
fwrite(ubuf, 1, (width*height) / 4, yuvfile);
fwrite(vbuf, 1, (width*height) / 4, yuvfile);
 
fclose(rgbtmp);
fclose(yuvfile);
free(rgbbuf);
free(ybuf);
free(ubuf);
free(vbuf);
return(0);
}

 

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有