title: libjpeg custom source manager
body: <p>So I've been reading the libjpeg documentation and it is <em>extremely</em> lackluster.</p>
  
  <p>I have been trying to figure out how to read from a custom memory buffer rather than a file and am not sure how to even test if my solution is working correctly.</p>
  
  <p>At the moment my function for loading a jpeg from memory is like so:</p>
  
  <pre><code>struct error_mgr{
      jpeg_error_mgr pub;
      std::jmp_buf buf;
  };
  
  bool load_jpeg(void *mem, size_t size, output_struct &amp;output){
      jpeg_source_mgr src;
  
      src.next_input_bytes = static_cast&lt;JOCTET*&gt;(mem)-size;
      src.bytes_in_buffer = size;
  
      src.init_source = [](j_compress_ptr){};
  
      src.fill_input_buffer = [](j_decompress_ptr cinfo) -&gt; boolean{
          // should never reach end of buffer
          throw "libjpeg tried to read past end of file";
          return true;
      };
  
      src.skip_input_data = [](j_compress_ptr cinfo, long num_bytes){
          if(num_bytes &lt; 1) return; // negative or 0 us no-op
          cinfo-&gt;src.next_input_byte+=num_bytes;
          cinfo-&gt;src.bytes_in_buffer-=num_bytes;
      };
  
      src.resync_to_restart = jpeg_resync_to_restart;
  
      src.term_source = [](j_decompress_ptr){};
  
      struct jpeg_decompress_struct cinfo;
  
      error_mgr err;
      cinfo.err = jpeg_std_error(&amp;err.pub);
      err.pub.error_exit = [](j_common_ptr cinfo){
          error_mgr ptr = reinterpret_cast&lt;error_mgr*&gt;(cinfo-&gt;err);
          std::longjmp(ptr-&gt;buf, 1);
      };
  
      if(std::setjmp(err.buf)){
          jpeg_destroy_decompress(&amp;cinfo);
          return false;
      }
  
      cinfo.src = &amp;src;
  
      jpeg_create_decompress(&amp;cinfo);
  
      (void) jpeg_read_header(&amp;cinfo, TRUE);
  
      // do the actual reading of the image
  
      return true;
  } 
  </code></pre>
  
  <p>But it never makes it past <code>jpeg_read_header</code>.</p>
  
  <p>I know that this is a jpeg file and I know that my memory is being passed correctly because I have libpng loading images with the same signature and calling function fine, so I'm sure it is how I am setting the source manager in <code>cinfo</code>.</p>
  
  <p>Anybody with more experience in libjpeg know how to do this?</p>