我正在尝试将rgb bytearray保存到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-&gt;图像数据时,我得到一个libjpeg错误,扫描线太多而没有写入文件,我的代码出了什么问题?
我可以在网上找到一堆libjpeg示例,但我找不到内存到内存(char *到char *)的例子。
我有一个次要问题: 我有一个非常优化的YUV到rgb函数,将YUV(更多压缩)图像转换为RGB(未压缩)然后将RGB转换为jpeg?或者只在libjpeg中使用YUV到Jpeg?
答案 0 :(得分:0)
JSAMPROW类型是如何声明的?试试这个:
memcpy(&buffer, (JSAMPROW)rgb->imageData+i, 640);
答案 1 :(得分:0)
试试这个而不是for循环:
while (cinfo.next_scanline < cinfo.image_height) {
JSAMPLE *row = rgb->imageData + 3 * cinfo.image_width * cinfo.next_scanline;
jpeg_write_scanlines(&cinfo, &row, 1);
}