Bug 102494

Summary: image::save() generates corrupted images on Windows
Product: poppler Reporter: Jeroen Ooms <jeroen>
Component: cpp frontendAssignee: poppler-bugs <poppler-bugs>
Status: RESOLVED FIXED QA Contact:
Severity: major    
Priority: medium    
Version: unspecified   
Hardware: Other   
OS: Windows (All)   
Whiteboard:
i915 platform: i915 features:
Attachments: patch for cpp image writer

Description Jeroen Ooms 2017-08-31 14:28:10 UTC
Rendering pdf pages on Windows works great but saving the rendered image using image.save() creates corrupted files. 

I tried both saving to 'png' and 'jpg' and both images were broken. Exactly the same code works fine on MacOS and Linux. It looks like the image files are truncated on Windows. Perhaps the file doesn't get closed properly?

    page_renderer pr;
    pr.set_render_hint(page_renderer::antialiasing, true);
    pr.set_render_hint(page_renderer::text_antialiasing, true);
    image img = pr.render_page(p, dpi, dpi);
    if(!img.is_valid())
      throw std::runtime_error("PDF rendering failure.");
    if(!img.save(filename, format, dpi))
      throw std::runtime_error("Failed to save file" + filename);

I am using poppler 0.57.0 on mingw-w64/gcc 4.9.3 but I have been able to reproduce this problem with all other versions of poppler and mingw-w64.
Comment 1 Jeroen Ooms 2017-11-04 12:41:07 UTC
Created attachment 135236 [details] [review]
patch for cpp image writer
Comment 2 Jeroen Ooms 2017-11-04 12:43:35 UTC
I have finally found the problem. The file is opened in text mode "w" instead of binary mode "wb". In cpp/poppler-image.cpp on line 374 it says:

  FILE *f = fopen(file_name.c_str(), "w");

This should be:

  FILE *f = fopen(file_name.c_str(), "wb");

Attached a patch.
Comment 3 Albert Astals Cid 2017-11-11 22:49:55 UTC
Pushed

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.