#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
#define SURFACE (1920 * 1080 * 3 / 2)
#define WIDTH 1920
#define HEIGHT 1080
void write_jpeg_file(unsigned char* yuv_buf)
{
printf(“entry write_jpeg_file funtion\n”);
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *jpegfile;
JSAMPROW row_pointer[1];
int y_stride, u_stride, v_stride;
int y_len, uv_len;
int uv_height;
unsigned char *y_buf, *u_buf, *v_buf;
unsigned char yuv_line[WIDTH * 3];
printf("entry jpeg_std_error\n");
cinfo.err = jpeg_std_error(&jerr);
printf("entry jpeg_create_compress\n");
jpeg_create_compress(&cinfo);
printf("jpeg_create_compress end\n");
if(NULL == (jpegfile = fopen("new.jpeg", "wb")))
{
printf("fopen new.jpeg failed\n");
return;
}
printf("entry jpeg_stdio_dest\n");;
jpeg_stdio_dest(&cinfo, jpegfile);
cinfo.image_width = WIDTH;
cinfo.image_height = HEIGHT;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_YCbCr;
cinfo.dct_method = JDCT_ISLOW;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 50, TRUE);
jpeg_start_compress(&cinfo, TRUE);
y_stride = WIDTH;
u_stride = WIDTH >> 1;
v_stride = WIDTH >> 1;
uv_height = HEIGHT >> 1;
y_len = y_stride * HEIGHT;
uv_len = u_stride * uv_height;
y_buf = yuv_buf;
u_buf = yuv_buf + y_stride * HEIGHT;
v_buf = yuv_buf + y_stride * HEIGHT + u_stride * uv_height;
printf("entry scan function\n");
for(int j = 0; cinfo.next_scanline < HEIGHT; j++)
{
u_buf = yuv_buf + y_stride * HEIGHT + u_stride * (j>>1);
v_buf = yuv_buf + y_stride * HEIGHT + u_stride * uv_height + u_stride * (j>>1);
for(int i = 0; i < y_stride; i++)
{
if((i%2) == 0)
{
yuv_line[i*3] = *y_buf++;
yuv_line[i*3 + 1] = *u_buf;
yuv_line[i*3 + 2] = *v_buf;
}
else
{
yuv_line[i*3] = *y_buf++;
yuv_line[i*3 + 1] = *u_buf++;
yuv_line[i*3 + 2] = *v_buf++;
}
}
row_pointer[0] = yuv_line;
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
printf("scan end\n");
jpeg_finish_compress(&cinfo);
fclose(jpegfile);
jpeg_destroy_compress(&cinfo);
}