I420压缩为jpeg图片例子

#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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值