title: Compressing IplImage to JPEG using libjpeg in OpenCV
body: <p>So I have this problem.
  I have an IplImage that i want to compress to JPEG and do something with it. I use libjpeg.
  I found a lot of answers "read through examples and docs" and such and did that. And successfully written a function for that.</p>
  
  <pre><code>FILE* convert2jpeg(IplImage* frame) 
  {
  FILE* outstream = NULL;
  outstream=malloc(frame-&gt;imageSize*frame-&gt;nChannels*sizeof(char))
  
  unsigned char *outdata = (uchar *) frame-&gt;imageData;
  struct jpeg_error_mgr jerr;
  struct jpeg_compress_struct cinfo;
  int row_stride;
  JSAMPROW row_ptr[1];
  
  jpeg_create_compress(&amp;cinfo);
  jpeg_stdio_dest(&amp;cinfo, outstream);
  
  cinfo.image_width = frame-&gt;width;
  cinfo.image_height = frame-&gt;height;
  cinfo.input_components = frame-&gt;nChannels;
  cinfo.in_color_space = JCS_RGB;
  
  jpeg_set_defaults(&amp;cinfo);
  jpeg_start_compress(&amp;cinfo, TRUE);
  row_stride = frame-&gt;width * frame-&gt;nChannels;
  
  while (cinfo.next_scanline &lt; cinfo.image_height) {
      row_ptr[0] = &amp;outdata[cinfo.next_scanline * row_stride];
      jpeg_write_scanlines(&amp;cinfo, row_ptr, 1);
  }
  
  jpeg_finish_compress(&amp;cinfo);
  jpeg_destroy_compress(&amp;cinfo);
  
  return outstream;
  }
  </code></pre>
  
  <p>Now this function is straight from the examples (except the part of allocating memory, but i need that since i'm not writnig to a file), but it still doesn't work.
  It dies on <i>jpeg_start_compress(&amp;cinfo, TRUE);</i> part?</p>
  
  <p>Can anybody help?</p>