Wrong Transparency in ImageSurface.get_data
- Dominant language
- Python
- Stars
- 951
- Forks
- 166
- PR merge metrics
- No merged PRs in 30d
Description
Hello,
It seems that `ImageSurface.get_data` gives different transparency values than `ImageSurface.write_to_png`. It seems as if the one from `get_data` is incorrect.
How to reproduce:
```python
import cairosvg.parser
import numpy as np
import cv2
svg_text = """
"""
tree = cairosvg.parser.Tree(bytestring=svg_text.encode('utf-8'))
surf = cairosvg.surface.PNGSurface(tree, None, dpi=96).cairo
# write to PNG
surf.write_to_png('1_write_to_png.png')
# write to RGBA buffer
data = np.frombuffer(surf.get_data(), np.uint8).reshape(surf.get_height(), surf.get_width(), 4)
cv2.imwrite("2_get_data.png", data)
```
This generates the two PNG's here:
 
The problem is visible when they are viewed on a white background. Zooming in reveals:

The left one is `write_to_png`. The right `get_data`. The black outline on the second image is not correct.
Here is the alpha channel. It looks identical:

And the color channels (without alpha):

It looks like the alpha value is pre-applied to the RGB channel.
Is this just a different convention, as we are using cairo internals and the output needs to be processed before using it as a typical RGBA channel?
I found the snipped with `get_data` in some issues here, maybe it was incomplete?
System information:
```
Ubuntu 21.10
Python 3.8.12
CairoSVG==2.5.2
cairocffi==1.3.0
libcairo2 1.16.0
```
I tested other png writers like the one in `matlab` and `PIL` and get the same output.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by running the supplied CairoSVG, cairocffi, NumPy, and OpenCV reproduction, then compare the ImageSurface.get_data and write_to_png entry points on the PNGSurface cairo object. Check the Cairo image-buffer convention and determine whether the difference is expected; done means the behavior is corrected or clearly documented, with coverage for the reproduced transparency case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- computer-graphics
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100