title: Loading JPG image using jpeglib - incorrect result, tilted image
body: <p>I'm loading JPG image using jpeglib. Sometimes images are loaded correctly and sometimes incorrectly (with no color and tilted). It depends on which image I'm loading. I am using OpenGL to render and display graphics. Below I attach example and my code:</p>
  
  <p><a href="https://i.stack.imgur.com/MIdJb.jpg" rel="nofollow noreferrer">Picture I want to load</a></p>
  
  <p><a href="https://i.stack.imgur.com/Uzh3E.png" rel="nofollow noreferrer">Incorrect result</a></p>
  
  <p>Code:</p>
  
  <pre><code>bool JPEGUtil::load(VGByteArray* source, VGByteArray *destination, int &amp;width, int &amp;height) 
  {
      source-&gt;position = 0;
  
      struct jpeg_decompress_struct cinfo;
  
      struct ErrorMgr jerr;
  
      JSAMPROW row_pointer[1] = {0};
      unsigned long location = 0;
  
      bool ret = false;
      do
      {
          cinfo.err = jpeg_std_error(&amp;jerr.pub);
          jerr.pub.error_exit = myErrorExit;
  
          if (setjmp(jerr.setjmp_buffer))
          {
              jpeg_destroy_decompress(&amp;cinfo);
              break;
          }
  
          jpeg_create_decompress( &amp;cinfo );
          jpeg_mem_src(&amp;cinfo, source-&gt;getBytes(), source-&gt;getBytesAvailable());
  
          int header = jpeg_read_header(&amp;cinfo, TRUE);
  
          if (header != 1) {
              break;//error
          }
  
          cinfo.out_color_space = JCS_RGB;
  
          /* Start decompression jpeg here */
          jpeg_start_decompress( &amp;cinfo );
  
          /* init image info */
          width  = cinfo.output_width;
          height = cinfo.output_height;
  
          unsigned char* data = destination-&gt;getBytes();
  
          /* now actually read the jpeg into the raw buffer */
          /* read one scan line at a time */
          while (cinfo.output_scanline &lt; cinfo.output_height)
          {
              row_pointer[0] = data + location;
              location += cinfo.output_width*cinfo.output_components;
              jpeg_read_scanlines(&amp;cinfo, row_pointer, 1);
          }
  
          jpeg_destroy_decompress( &amp;cinfo );
          ret = true;
      } while (0);
  
      return ret;
  }
  </code></pre>
  
  <p>Again - some pictures are loaded correctly, some as in example. Any ideas? Thanks.</p>