title: Image flips, OpenGL output to JPEG using libjpeg
body: <p>The below code helps me to convert OpenGL output to JPEG image using libjpg but the resultant image is flipped vertical...</p>
  
  <p>The code works perfect but the final image is flipped I dont know why ?!</p>
  
  <pre><code>unsigned char *pdata = new unsigned char[width*height*3];
      glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pdata);
  
      FILE *outfile;
      if ((outfile = fopen("sample.jpeg", "wb")) == NULL) {
          printf("can't open %s");
          exit(1);
        }
  
      struct jpeg_compress_struct cinfo;
      struct jpeg_error_mgr       jerr;
  
      cinfo.err = jpeg_std_error(&amp;jerr);
      jpeg_create_compress(&amp;cinfo);
      jpeg_stdio_dest(&amp;cinfo, outfile);
  
      cinfo.image_width      = width;
      cinfo.image_height     = height;
      cinfo.input_components = 3;
      cinfo.in_color_space   = JCS_RGB;
  
      jpeg_set_defaults(&amp;cinfo);
      /*set the quality [0..100]  */
      jpeg_set_quality (&amp;cinfo, 100, true);
      jpeg_start_compress(&amp;cinfo, true);
  
      JSAMPROW row_pointer;
      int row_stride = width * 3;
  
      while (cinfo.next_scanline &lt; cinfo.image_height) {
      row_pointer = (JSAMPROW) &amp;pdata[cinfo.next_scanline*row_stride];
      jpeg_write_scanlines(&amp;cinfo, &amp;row_pointer, 1);
      }
  
      jpeg_finish_compress(&amp;cinfo);
  
      fclose(outfile);
  
      jpeg_destroy_compress(&amp;cinfo);
  </code></pre>