Kozea / Kozea/CairoSVG

Wrong Transparency in ImageSurface.get_data

Open
#357 3 comments 0 reactions 0 assignees View on GitHub
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:

![1_write_to_png](https://user-images.githubusercontent.com/235346/155555305-b50ba225-fc4e-459f-8268-d04d336ef0a6.png) ![2_get_data](https://user-images.githubusercontent.com/235346/155555313-30a3dd70-2f26-4757-a70a-d3ea4d37c09f.png)

The problem is visible when they are viewed on a white background. Zooming in reveals:

![image](https://user-images.githubusercontent.com/235346/155555860-c3f0c005-53c1-404e-bdc7-bd4174a867f7.png)

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:

![image](https://user-images.githubusercontent.com/235346/155558647-81c524cb-2438-4b27-9366-7a54e6a647ab.png)

And the color channels (without alpha):

![image](https://user-images.githubusercontent.com/235346/155558773-bc5b76a4-9038-4b1f-9281-af5a055b9c24.png)

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.