【发布时间】:2013-05-24 17:04:00
【问题描述】:
我正在尝试将 rgb 字节数组保存到 jpg 文件中。
我按照page 中的说明进行操作,这是我的代码
//rgb is the image
//rgb->imageData is char* to the rgb data
//rgb->imageSize is the image size which is 640*480*3
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
/* this is a pointer to one row of image data */
FILE *outfile = fopen( "file.jpeg", "wb" );
cinfo.err = jpeg_std_error( &jerr );
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
/* Setting the parameters of the output file here */
cinfo.image_width = 640;//width;
cinfo.image_height = 480;//height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults( &cinfo );
/* Now do the compression .. */
jpeg_start_compress( &cinfo, TRUE );
JSAMPROW buffer;
for(int i=0;i<rgb->imageSize;i+=640)
{
memcpy(buffer,
(JSAMPROW)rgb->imageData+i,
640);//segmentation fault here
jpeg_write_scanlines( &cinfo, &buffer, 1 );
}
jpeg_finish_compress( &cinfo );
jpeg_destroy_compress( &cinfo );
fclose( outfile );
我遇到分段错误,在尝试 buffer = rgb->image data 时遇到 libjpeg 错误,即扫描线过多且没有任何内容写入文件,我的代码有什么问题??
我可以在网上找到一堆 libjpeg 示例,但我找不到内存到内存(char* 到 char*)的示例。
我还有一个问题: 我有一个非常优化的 YUV 到 rgb 功能,将 YUV(更压缩)图像转换为 RGB(未压缩)然后将 RGB 转换为 jpeg 是否很好?还是只在 libjpeg 中使用 YUV 转 Jpeg ??
【问题讨论】: