title: saving rgb char* into jpeg image using libjpeg
body: <p>I am trying to save an rgb bytearray into a jpg file.</p>
  
  <p>I followed instructions in this <a href="http://andrewewhite.net/wordpress/2008/09/02/very-simple-jpeg-writer-in-c-c" rel="nofollow">page</a> and here is my code</p>
  
  <pre><code>//rgb is the image
  //rgb-&gt;imageData is char* to the rgb data
  //rgb-&gt;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( &amp;jerr );
    jpeg_create_compress(&amp;cinfo);
    jpeg_stdio_dest(&amp;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( &amp;cinfo );
    /* Now do the compression .. */
    jpeg_start_compress( &amp;cinfo, TRUE );
  
    JSAMPROW buffer;
    for(int i=0;i&lt;rgb-&gt;imageSize;i+=640)
    {
        memcpy(buffer,
               (JSAMPROW)rgb-&gt;imageData+i,
               640);//segmentation fault here
        jpeg_write_scanlines( &amp;cinfo, &amp;buffer, 1 );
    }
    jpeg_finish_compress( &amp;cinfo );
    jpeg_destroy_compress( &amp;cinfo );
    fclose( outfile );
  </code></pre>
  
  <p>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 ??</p>
  
  <p>I can find a bunch of libjpeg examples online but I can't find memory to memory (char* to char*) example.</p>
  
  <p><em><strong>I have a secondary question:</em></strong>
  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 ??</p>