empira / PDFsharp

PDFsharp and MigraDoc Foundation for .NET 6 and .NET Framework
https://docs.pdfsharp.net/
Other
531 stars 132 forks source link

Certain PNG images are skewed when rendering in Core build of PDFsharp #168

Open bsouthard-jackhenry opened 2 months ago

bsouthard-jackhenry commented 2 months ago

Expected Behavior

I expect the PNG image to render correctly. The PNG in question seems to be valid and is viewable elsewhere (MS Paint, GIMP, Chrome, etc.).

Actual Behavior

The PNG file is rendered skewed. Please see the image below: image

Steps to Reproduce the Behavior

Please see the example in the attached issue submission. A PNG file that demonstrates the issue is included.

Issue.zip

Thank you, Brent Southard bsouthard@jackhenry.com

ThomasHoevel commented 2 months ago

Both the GDI and WPF builds handle the image correctly. I still have to investigate what goes wrong with the Core build. As a workaround, the Core build handles the image correctly after conversion to BMP monochrome format and this has no impact on PDF file size.

bsouthard-jackhenry commented 2 months ago

First, thanks for looking into this! I am new to PDFsharp and I have to say I'm very impressed with it.

We are specifically using the Core build to avoid GDI and WPF dependencies, unfortunately. May I ask what you used to convert the PNG to a monochrome BMP file? I tried to do that using Magick.NET and was unable to get monochrome output -- I ended up with an 8-bit image no matter what I tried. I'm limited by avoiding the System.Drawing namespace, otherwise I would have many viable options.

Brent

ThomasHoevel commented 2 months ago

I used the good old Paint mspaint.exe. Select Monochrome under "Save as". It is 1032 kB as BMP vs. 72 as PNG, but the BMP gets compressed in the PDF and final file size is the same.

bsouthard-jackhenry commented 2 months ago

Ah, I'm in need of a programmatic solution. The full story is that our application deals with a wide variety of image formats and we need to convert any that are not of a PDFsharp-supported format (JPG, PNG, BMP for Core) to a supported format for inclusion in a generated PDF. The PNG in question was actually converted from a bitonal TIFF image using ImageGear.NET (we have an ImageGear license). At first I thought the blame here was with ImageGear's conversion, but the PNG seems valid and I can open it using a variety of applications. I will go back and try converting from TIFF to BMP using ImageGear and see if the BMP file generated that way is acceptable.

By the way, were you able to reproduce the skewing issue using the submitted code (using the Core library)?

Thanks again, Brent

ThomasHoevel commented 2 months ago

By the way, were you able to reproduce the skewing issue using the submitted code (using the Core library)?

Yes. Shame on me. My monochrome test images always had widths that are a multiple of 8. 2550 is not a multiple of 8 and needs adjustment at one point in code.

So the next preview of PDFsharp will handle this particular image correctly. Cannot yet give an ETA.

bsouthard-jackhenry commented 2 months ago

That's great! I'm glad you've discovered the issue and I thought it was probably width related, based on the skewed output. I'm glad I could add a file to your image test library. :)

I think I might have a JPEG file that I had a different issue with. If I can find it I'll submit an issue for that as well. Not that I'm trying to be a troublemaker or anything...

Regards, Brent

ThomasHoevel commented 2 months ago

I just found out that the bug was not my original work, but already existed in the PNG library we use for the import. I feel a little bit better now.

JPEG files come in many different flavors and we added two more flavors in the past few months, but maybe there are more to come.

bsouthard-jackhenry commented 2 months ago

@ThomasHoevel, will your fix still work despite the bug in the PNG library (BigGustave, it appears)?

ThomasHoevel commented 2 months ago

PDFsharp includes an enhanced BigGustave library - now with the fix.