title: Write to memory buffer instead of file with libjpeg?
body: <p>I have found this function which uses libjpeg to write to a file:</p>
  
  <pre><code>int write_jpeg_file( char *filename )
  {
      struct jpeg_compress_struct cinfo;
      struct jpeg_error_mgr jerr;
  
      /* this is a pointer to one row of image data */
      JSAMPROW row_pointer[1];
      FILE *outfile = fopen( filename, "wb" );
  
      if ( !outfile )
      {
          printf("Error opening output jpeg file %s\n!", filename );
          return -1;
      }
      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 = width;  
      cinfo.image_height = height;
      cinfo.input_components = bytes_per_pixel;
      cinfo.in_color_space = color_space;
      /* default compression parameters, we shouldn't be worried about these */
      jpeg_set_defaults( &amp;cinfo );
      /* Now do the compression .. */
      jpeg_start_compress( &amp;cinfo, TRUE );
      /* like reading a file, this time write one row at a time */
      while( cinfo.next_scanline &lt; cinfo.image_height )
      {
          row_pointer[0] = &amp;raw_image[ cinfo.next_scanline * cinfo.image_width *  cinfo.input_components];
          jpeg_write_scanlines( &amp;cinfo, row_pointer, 1 );
      }
      /* similar to read file, clean up after we're done compressing */
      jpeg_finish_compress( &amp;cinfo );
      jpeg_destroy_compress( &amp;cinfo );
      fclose( outfile );
      /* success code is 1! */
      return 1;
  }
  </code></pre>
  
  <p>I would actually need to write the jpeg compressed image just to memory buffer, without saving it to a file, to save time. Could somebody give me an example how to do it?</p>
  
  <p>I have been searching the web for a while but the documentation is very rare if any and examples are also difficult to come by.</p>