依赖库:开源库libjpeg, http://www.ijg.org/
代码:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <linux/types.h>
#include <linux/videodev2.h>
#include <setjmp.h>
#include "jpeglib.h"
typedef int BOOL;
#define TRUE 1
#define FALSE 0
unsigned char yuv[4] = {0,};
unsigned char rgb[2432*2048*3] = {0,};
int yuv422_to_rgb24()
{
FILE* fp = fopen("422.yuv", "rb");
int ret = 0;
int r1,g1,b1,r2,g2,b2;
char* ptr = rgb;
while(ret = fread(yuv, 4,1, fp))
{
/*
r1 = (unsigned char)yuv[0] + 1.042*((unsigned char)yuv[3]-128);
g1 = (unsigned char)yuv[0] - 0.34414*((unsigned char)yuv[1]-128) - 0.71414*((unsigned char)yuv[3]-128);
b1 = (unsigned char)yuv[0] + 1.772*((unsigned char)yuv[1]-128);
r2 = (unsigned char)yuv[2] + 1.042*((unsigned char)yuv[3]-128);
g2 = (unsigned char)yuv[2] - 0.34414*((unsigned char)yuv[1]-128) - 0.71414*((unsigned char)yuv[3]-128);
b2 = (unsigned char)yuv[2] + 1.772*((unsigned char)yuv[1]-128);
*/
r1 = (unsigned char)yuv[0] + 1.042*((unsigned char)yuv[1]-128);
g1 = (unsigned char)yuv[0] - 0.34414*((unsigned char)yuv[3]-128) - 0.71414*((unsigned char)yuv[1]-128);
b1 = (unsigned char)yuv[0] + 1.772*((unsigned char)yuv[3]-128);
r2 = (unsigned char)yuv[2] + 1.042*((unsigned char)yuv[1]-128);
g2 = (unsigned char)yuv[2] - 0.34414*((unsigned char)yuv[3]-128) - 0.71414*((unsigned char)yuv[1]-128);
b2 = (unsigned char)yuv[2] + 1.772*((unsigned char)yuv[3]-128);
if(r1<0) r1 = 0; else if(r1>255) r1 = 255;
if(g1<0) g1 = 0; else if(g1>255) g1 = 255;
if(b1<0) b1 = 0; else if(b1>255) b1 = 255;
if(r2<0) r2 = 0; else if(r2>255) r2 = 255;
if(g2<0) g2 = 0; else if(g2>255) g2 = 255;
if(b2<0) b2 = 0; else if(b2>255) b2 = 255;
*ptr = (unsigned char)b1;ptr++;
*ptr = (unsigned char)g1;ptr++;
*ptr = (unsigned char)r1;ptr++;
*ptr = (unsigned char)b2;ptr++;
*ptr = (unsigned char)g2;ptr++;
*ptr = (unsigned char)r2;ptr++;
}
return 0;
}
BOOL encode_jpeg(char *lpbuf,int width,int height)
{
struct jpeg_compress_struct cinfo ;
struct jpeg_error_mgr jerr ;
JSAMPROW row_pointer[1] ;
int row_stride ;
char *buf=NULL ;
int x ;
FILE *fptr_jpg = fopen ("f.jpg","wb");//注意这里为什么用fopen而不用open
if(fptr_jpg==NULL)
{
printf("Encoder:open file failed!/n") ;
return FALSE;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, fptr_jpg);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 30,TRUE);
jpeg_start_compress(&cinfo, TRUE);
row_stride = width * 3;
buf=malloc(row_stride) ;
row_pointer[0] = buf;
while (cinfo.next_scanline < height)
{
for (x = 0; x < row_stride; x+=3)
{
buf[x] = lpbuf[x];
buf[x+1] = lpbuf[x+1];
buf[x+2] = lpbuf[x+2];
}
jpeg_write_scanlines (&cinfo, row_pointer, 1);//critical
lpbuf += row_stride;
}
jpeg_finish_compress(&cinfo);
fclose(fptr_jpg);
jpeg_destroy_compress(&cinfo);
free(buf) ;
return TRUE ;
}
int main(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
printf("-------------- now: sec %d usec %d \n", (int)tv.tv_sec, (int)tv.tv_usec);
yuv422_to_rgb24();
encode_jpeg(rgb,2432,2048); //RGB24 to Jpeg
gettimeofday(&tv, NULL);
printf("-------------- now: sec %d usec %d \n", (int)tv.tv_sec, (int)tv.tv_usec);
printf("save JPEG OK\n");
return(TRUE);
}