I am trying to save an rgb bytearray into a jpg file.
I followed instructions in this page and here is my code
//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 );
I am getting a segmentation fault, and when trying buffer = rgb->image data i got a libjpeg error that too much scanlines and nothing is written into the file, what is wrong with my code ??
I can find a bunch of libjpeg examples online but I can't find memory to memory (char* to char*) example.
I have a secondary question: I have a very optimized YUV to rgb function , is it good to convert YUV(more compressed) image into RGB (not compressed) and then convert RGB to jpeg ?? or only use YUV to Jpeg in libjpeg ??