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

PDF files corrupted in 3.24 (3.15 or higher) #351

Closed claudiowilmanns closed 2 years ago

claudiowilmanns commented 2 years ago

Matlab 2021b and 2022a on macOS 11.6.4 and export_fig version 3.24 create corrupted PDF files, while PNGs are fine.

For example: plot(cos(linspace(0, 7, 1000))); export_fig test2.pdf export_fig test2.png

PDF (3.24): test2.pdf Preview of corrupt PDF:

Screen Shot 2022-03-15 at 13 19 48

PNG (3.24): test2

When I switch to the older export_fig version 3.05 the PDFs are generated properly. PDF (3.05): test2_3.05.pdf

The macOS system has the latest Ghostscript (9.55) installed. I also tried different JDK versions, but the issue is the same.

I noticed this issue (corrupted PDFs) already with a version newer than 3.05 but older than 3.24, but I can't remember which version it was. I just switch back to 3.05. Further testing (see below) revealed that the issue seems to begin with 3.15, up to version 3.14 its OK.

While I can export most of my plots with the old version I'd like to use the latest export_fig version for its improvements.

Thanks, Claudio

altmany commented 2 years ago

There were numerous changes between v3.05 and v3.24. Try to narrow it down by installing intermediate versions (for example, 3.15, then either 3.10 or 3.20 depending on the result). You can download such versions in Github by directly downloading the code from the version page (for example, https://github.com/altmany/export_fig/tree/v3.15 for v3.15), or by interactively selecting the relevant tag version and then downloading the corresponding code from the Code button: image

You can also download these older versions in the Matlab File Exchange by going to https://www.mathworks.com/matlabcentral/fileexchange/23629-export_fig, clicking the "View Version History" link, and selecting the Download button for the relevant version: image

Once you narrow down the problematic version where the problem first occurred, I assume that I will be able to resolve it. But until then I am more-or-less blind and cannot assist you.

claudiowilmanns commented 2 years ago

Thanks for your reply. Will do! Claudio

claudiowilmanns commented 2 years ago

I tracked it down and I think the issue starts with version 3.15. Creating with 3.14 or earlier (down to 3.05) it's fine. My test is as follows:

claudiowilmanns commented 2 years ago

I got a little further.

Using export_fig 3.15 and repeating the command I can see that the output is sometimes good (as expected) and sometimes bad (as shown). Interestingly the temporary EPS files are also different.

It seems that for some reason inside print2eps.m the bounding box of the temporary EPS file is set wrong.

Attached two temporary EPS files.

  1. original EPS (zipped) with bad bounding box (%%BoundingBox: 557 -3 563 3) tp512ffe54_5d37_4dcb_a2cf_05219e2c4b4a_BBmanually_corrected.eps.zip

  2. manually corrected EPS file with good bounding box (%%BoundingBox: 0 0 560 420) tp512ffe54_5d37_4dcb_a2cf_05219e2c4b4a_BBmanually_corrected.eps 2.zip

It seems that the bounding box gets randomly filled with different values.

The reason is that sometimes crop_borders.m returns bad bb_rel values which debugging revealed: [aa, aa, aa, bb_rel] = crop_borders(A, bcol, bb_padding, crop_amounts); %#ok

BAD: bb_rel: 0.99821 -0.002381 1.0018 0.002381 pagebb_matlab: 0 0 560 420 bb_matlab: 0 0 560 420 bb_new: 559 -1 561 1 bb_offset: 557 -3 3 -417

GOOD: bb_rel: 0.091071 0.071429 0.92679 0.93333 pagebb_matlab: 0 0 560 420 bb_matlab: 0 0 560 420 bb_new: 51 30 519 392 bb_offset: 49 28 -39 -26

The error happens inside crop_borders.m line 152. bb_rel = [l-1 h-b-1 r+1 h-t+1]./[w h w h];

If l = w = r and b = h = t then it goes bad: In my case: l: 560 w: 560 r: 560 and b: 420 h: 420 t: 420 bbrel: 0.99821 -0.002381 1.0018 0.002381

It is OK if in my case it is: l: 52 w: 560 r: 518 and b: 389 h: 420 t: 29: bbrel: 0.0911 0.0714 0.9268 0.9333

The question is, why does it sometimes go BAD and sometimes it is OK?

claudiowilmanns commented 2 years ago

Here's a guess. Is it possible, that some process, e.g. the print command inside print2eps.m, opens a temporary window (as a "canvas") that has wrong position onscreen. It may be partially located outside the screen and then the borders are calculated not right?

claudiowilmanns commented 2 years ago

OK, I think I found a potential fix. In print2eps.m I changed the following line drawnow; pause(0.02); % avoid unintended cropping (issue #318) to drawnow; pause(0.1); % avoid unintended cropping (issue #318)

Matlab 2022a: A pause of 0.08 was also OK, but with 0.07 I faced the same issue again when I looped the export_fig command for 100 iterations. To be on the safe side, I chose a pause value of 0.1 seconds.

I know that this is a performance degrade, but what's worth having corrupted PDF files either?

Edit: For Matlab 2021b I had to increase the value again to have the right cropping. Seems like a performance issue in general. Maybe JDK specific (Matlab 2021b runs Temurin JDK on my system). Maybe also screen resolution specific?

altmany commented 2 years ago

does the increased pause(0.1) also fix the problem in the latest version of export_fig (v3.24)? (print2eps.m line 544)

claudiowilmanns commented 2 years ago

Yes, in Matlab 2022a 0.1 was fine when using v3.24. In Matlab 2021b I had to increase it. I used 0.2 just out of the blue which worked but did not try if a value between 0.1 and 0.2 works as well.

I have the impression my system has/had some performance issues, that’s why maybe the drawnow command takes longer than expected. I need to test again with all apps closed, etc.

BTW, the change between v3.14 and v3.15 was to add the line drawnow; pause(0.02); Correct?

Best regards Claudio

From: Yair Altman @.> Date: Wednesday, 16. March 2022 at 20:35 To: altmany/export_fig @.> Cc: Claudio Wilmanns @.>, Author @.> Subject: Re: [altmany/export_fig] PDF files corrupted in 3.24 (3.15 or higher) (Issue #351) [EXTERNAL]

does the increased pause(0.1) also fix the problem in the latest version of export_fig (v3.24)? (print2eps.m line 544)

— Reply to this email directly, view it on GitHubhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Faltmany%2Fexport_fig%2Fissues%2F351%23issuecomment-1069540349&data=04%7C01%7Cclaudio.wilmanns%40efi.com%7C6c6d6c9d4fbc4cce024b08da07841f85%7C3fe4532499b245c397517034bae71475%7C0%7C0%7C637830561322554286%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=E3zL0d%2Bh3xSxZw1%2BIMoG17F28fAU%2Bh4AlMgKGHl6tKw%3D&reserved=0, or unsubscribehttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAYHO4MUJVGBNGQIW5S3DKMTVAIZX3ANCNFSM5QYQEKUQ&data=04%7C01%7Cclaudio.wilmanns%40efi.com%7C6c6d6c9d4fbc4cce024b08da07841f85%7C3fe4532499b245c397517034bae71475%7C0%7C0%7C637830561322554286%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=QXmmDm5K8284gYhVPrJJoPJo6TAlaDQLf9iezzue%2Fag%3D&reserved=0. Triage notifications on the go with GitHub Mobile for iOShttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapps.apple.com%2Fapp%2Fapple-store%2Fid1477376905%3Fct%3Dnotification-email%26mt%3D8%26pt%3D524675&data=04%7C01%7Cclaudio.wilmanns%40efi.com%7C6c6d6c9d4fbc4cce024b08da07841f85%7C3fe4532499b245c397517034bae71475%7C0%7C0%7C637830561322554286%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=m2H%2BSM9nLSOB0%2FcgwoPAVHaRtzjkwNdXfkkefjC9Oh0%3D&reserved=0 or Androidhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.github.android%26referrer%3Dutm_campaign%253Dnotification-email%2526utm_medium%253Demail%2526utm_source%253Dgithub&data=04%7C01%7Cclaudio.wilmanns%40efi.com%7C6c6d6c9d4fbc4cce024b08da07841f85%7C3fe4532499b245c397517034bae71475%7C0%7C0%7C637830561322554286%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=ZbCyzminrH2oBO7jVJrgqV4E7CxbXpoEQ1XLHe6nTnI%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.***>

Confidentiality notice: This message may contain confidential information. It is intended only for the person to whom it is addressed. If you are not that person, you should not use this message. We request that you notify us by replying to this message, and then delete all copies including any contained in your reply. Thank you.

altmany commented 2 years ago

I uploaded v3.25 that hopefully resolves the problem on all systems/releases, without a large performance penalty in the common case. I'm closing the issue, but if you find that v3.25 still does not solve the issue then let me know and I'll reopen it.

claudiowilmanns commented 2 years ago

Hi Yair,

I downloaded 3.25 and it seems to work fine in Matlab R2021b. All my files I had problems before are OK now. Thank you very much.

I had a look at your fix, which is OK, I think. But I noticed these two lines in print2eps.m after the critical step: bb_rel(bb_rel>1) = 1; % ignore invalid values bb_rel(bb_rel<=0) = 1; % ignore invalid values The second line caught my attention. I’m wondering if it should not be bb_rel(bb_rel<=0) = 0; % ignore invalid values ? Or is it OK to set values below 0 to 1?

Best regards Claudio

From: Yair Altman @.> Date: Wednesday, 16. March 2022 at 21:46 To: altmany/export_fig @.> Cc: Claudio Wilmanns @.>, Author @.> Subject: Re: [altmany/export_fig] PDF files corrupted in 3.24 (3.15 or higher) (Issue #351) [EXTERNAL]

I uploaded v3.25 that hopefully resolves the problem on all systems/releases, without a large performance penalty in the common case. I'm closing the issue, but if you find that v3.25 still does not solve the issue then let me know and I'll reopen it.

— Reply to this email directly, view it on GitHubhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Faltmany%2Fexport_fig%2Fissues%2F351%23issuecomment-1069615932&data=04%7C01%7Cclaudio.wilmanns%40efi.com%7C56158ad044a44e77092208da078e19de%7C3fe4532499b245c397517034bae71475%7C0%7C0%7C637830604157374333%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=NqsbgAsnwiVoWRKzY%2FHCudLgeOk3Clkq9qZFNy1FJDw%3D&reserved=0, or unsubscribehttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAYHO4MQST7J3BKOAASNZXX3VAJCDZANCNFSM5QYQEKUQ&data=04%7C01%7Cclaudio.wilmanns%40efi.com%7C56158ad044a44e77092208da078e19de%7C3fe4532499b245c397517034bae71475%7C0%7C0%7C637830604157374333%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=s260BAcolsVZkoQV0PRPBtWSpH9myGjufQomAwpHWb0%3D&reserved=0. Triage notifications on the go with GitHub Mobile for iOShttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapps.apple.com%2Fapp%2Fapple-store%2Fid1477376905%3Fct%3Dnotification-email%26mt%3D8%26pt%3D524675&data=04%7C01%7Cclaudio.wilmanns%40efi.com%7C56158ad044a44e77092208da078e19de%7C3fe4532499b245c397517034bae71475%7C0%7C0%7C637830604157374333%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=lpTJORJ3v98kIWFtmuIrkCNjK2pnDm86X%2FB9fb9nGU4%3D&reserved=0 or Androidhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.github.android%26referrer%3Dutm_campaign%253Dnotification-email%2526utm_medium%253Demail%2526utm_source%253Dgithub&data=04%7C01%7Cclaudio.wilmanns%40efi.com%7C56158ad044a44e77092208da078e19de%7C3fe4532499b245c397517034bae71475%7C0%7C0%7C637830604157374333%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=8KzamYUkoXfsjIw8sFKIJF0j%2BaCC%2ByZio7sVhnFMPv4%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.***>

Confidentiality notice: This message may contain confidential information. It is intended only for the person to whom it is addressed. If you are not that person, you should not use this message. We request that you notify us by replying to this message, and then delete all copies including any contained in your reply. Thank you.

altmany commented 2 years ago

it's ok because bb_rel is used as a multiplier (line 562) so setting to 1 means leaving the original value unchanged (uncropped).