title: Runtime error on Windows when trying to load image with libpng
body: <p>I am using <a href="http://phash.org/">pHash</a> and that library uses <a href="http://www.libpng.org/pub/png/libpng.html">libpng</a>. I am having issues running my program because libpng fails loading a PNG file.</p>
  
  <ul>
  <li>Version of libpng: <a href="https://sourceforge.net/projects/libpng/files/libpng14/1.4.19/">1.4.19</a></li>
  <li>Platform: Windows 10</li>
  <li>Environment: Visual Studio 2015</li>
  </ul>
  
  <h3>Trivial</h3>
  
  <p>Just if you came up with the following questions...</p>
  
  <ol>
  <li>Is the path to image correct? Yes</li>
  <li>Is the image a valid PNG file? Yes</li>
  </ol>
  
  <h2>Code details</h2>
  
  <p>Library pHash uses <a href="https://github.com/dtschump/CImg">CImg</a>, the version of CImg they are using is a bit old I think:</p>
  
  <pre><code>#define cimg_version 148 // In CImg.h
  </code></pre>
  
  <p>I have debugged the library and the problems occurs in <code>CImg.h</code> (contained in the pHash VC++ project):</p>
  
  <pre><code>CImg&lt;T&gt;&amp; _load_png(std::FILE *const file, const char *const filename) {
        if (!file &amp;&amp; !filename)
          throw CImgArgumentException(_cimg_instance
                                      "load_png() : Specified filename is (null).",
                                      cimg_instance);
        // Open file and check for PNG validity
        if (Buffer) strcat(Buffer, "Checking PNG availability\r\n");
        const char *volatile nfilename = filename; // two 'volatile' here to remove a g++ warning due to 'setjmp'.
        std::FILE *volatile nfile = file?file:cimg::fopen(nfilename,"rb");
  
        unsigned char pngCheck[8] = { 0 };
        cimg::fread(pngCheck,8,(std::FILE*)nfile);
        if (png_sig_cmp(pngCheck,0,8)) {
          if (!file) cimg::fclose(nfile);
          throw CImgIOException(_cimg_instance
                                "load_png() : Invalid PNG file '%s'.",
                                cimg_instance,
                                nfilename?nfilename:"(FILE*)");
        }
  
        // Setup PNG structures for read
        png_voidp user_error_ptr = 0;
        png_error_ptr user_error_fn = 0, user_warning_fn = 0;
        png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,user_error_ptr,user_error_fn,user_warning_fn);
  
        if (!png_ptr) { // &lt;-- PROBLEM HERE
          if (!file) cimg::fclose(nfile);
          throw CImgIOException(_cimg_instance
                                "load_png() : Failed to initialize 'png_ptr' structure for file '%s'.",
                                cimg_instance,
                                nfilename?nfilename:"(FILE*)");
  ...
  }
  </code></pre>
  
  <p>The snippet shows the first part of <code>CImg&lt;T&gt;&amp; _load_png(std::FILE *const file, const char *const filename)</code> which is called by the CImg library used by pHash.</p>
  
  <h2>Runtime issue</h2>
  
  <p>The code compiles fine but I get this error at runtime which I can see in the debugger:</p>
  
  <blockquote>
    <p>CImgIOException: Failed to initialize 'png_ptr'...</p>
  </blockquote>
  
  <p>In the point indicated in the code. I don't know why, it fails loading the image. The failure occurs when calling <code>png_create_read_struct</code> in <code>CImg.h</code>. That code is a bit obscure as defined through preprocessor directives. It is not clear why it is failing.</p>
  
  <p>Any ideas?</p>