sungaila / PDFtoImage

A .NET library to render PDF files into images.
https://www.sungaila.de/PDFtoImage/
MIT License
152 stars 15 forks source link

Fix color issue on MacOS and Android by specifying SKColorType #30

Closed Tadelsucht closed 11 months ago

Tadelsucht commented 11 months ago

This pull request addresses a color inconsistency issue observed on MacOS and Android platforms when rendering PDF files into images. The problem manifested as an incorrect color interpretation, where, for example, red was displayed as blue. (With Windows and Linux (Ubuntu) the colors were correct.) The following old issue should be the same problem: https://github.com/sungaila/PDFtoImage/issues/17

Changes: SKBitmap Creation: Updated the creation of SKBitmap in the Render method of PdfDocument.cs by specifying the SKColorType as Bgra8888 and SKAlphaType as Premul.

Rationale: SKColorType.Bgra8888: Ensures that the color channels are interpreted in the order Blue, Green, Red, Alpha, matching the expected color layout. SKAlphaType.Premul: Sets the alpha type to premultiplied, aligning with the expected transparency handling.

Testing: The change has been tested on Windows, Linux, MacOS and Android (.NET 7) and the color rendering is now consistent across all platforms.

Since for FPDFBitmap_CreateEx the FXDIBFormats is set to 4, which is "4 bytes per pixel, byte order: blue, green, red, alpha", setting the bitmap to Bgra8888 should not cause any problems. ( https://pdfium.patagames.com/help/html/T_Patagames_Pdf_Enums_FXDIBFormats.htm ) Since it was already the expected format.

Impact: This fix ensures that the PDF-to-image conversion provides consistent color rendering across different platforms, improving the reliability and visual accuracy of the library.

sonarcloud[bot] commented 11 months ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

sungaila commented 11 months ago

Hi @Tadelsucht,

thanks for your contribution and in-depth analysis of this color space issue!

Before building a new release I will add regression tests for Linux and macOS. Right now the existing comparision unit tests run on Windows only and I wouldn't notice if the colors break again.

sungaila commented 11 months ago

@Tadelsucht PDFtoImage 2.3.2 just released with your bugfix (thanks again)!

I added unit tests for macOS so future color issues won't go unnoticed. Truth be told, I don't own a Mac to test this locally. Android needs additional work to setup automatic unit tests though.