altmany / export_fig

A MATLAB toolbox for exporting publication quality figures
BSD 3-Clause "New" or "Revised" License
1.27k stars 365 forks source link

eps2pdf ignores -r and downsamples images above 450dpi #368

Closed scholnik closed 1 year ago

scholnik commented 1 year ago

I discovered by trial and error that when exporting to pdf using -opengl to force a bitmap that my actual output resolution and file size didn't seem to change between -r300 and -r600, and even odder was that -r400 generated a larger, higher-res file than -600. I verified that the .eps file written by matlab is scaling as requested, and tracked the problem down to eps2pdf, and more specifically what appear to be the ghostscript defaults.

A description of the ghostscript downsampling parameters can be found here: https://www.ghostscript.com/blog/optimizing-pdfs.html The quick version is that color (gray, mono) images are controlled by 3 controls:

DownsampleColorImages (default appears to be true) ColorImageResolution (default appears to be ~300) ColorImageDownsampleThreshold (default appears to be 1.5)

What this does is downsample any images with resolution greater than 1.5*300=450 to 300. When the -q parameter is >100, there is code in eps2pdf that sets ColorImageDownsampleThreshold=10, which effectively disables downsampling for anything below 3000 dpi, but in all other cases the default downsampling is used.

This behavior is easy to "fix", assuming this isn't the intended behavior. Adding -dDownsampleColorImages=false -dDownsampleGrayImages=false -dDownsampleMonoImages=false to the initial options string will disable downsampling. I've attached eps2pdf.m so modified. Adding those same flags via -d arguments to export_fig does not appear to work, as such options are added at the end of the ghostscript options string after the distiller parameters are set.

eps2pdf.zip

altmany commented 1 year ago

Your second version looks good, I've integrated it into the main version with some modifications and extra checks (take a look at the latest commit https://github.com/altmany/export_fig/commit/af5e0eb63d367428cafe15ad66f1c63c94aacff4).

Thank you for your support!