Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image.toarray change rgb to bgr on the Linux #191

Open
Polyisoprene opened this issue Jun 10, 2023 · 5 comments
Open

Image.toarray change rgb to bgr on the Linux #191

Polyisoprene opened this issue Jun 10, 2023 · 5 comments

Comments

@Polyisoprene
Copy link

Describe the bug
On Linux platform use Image.toarray convert image to array ,then use Image.fromarray convert array to image, R and B chennel of the image will switch

To Reproduce
Steps to reproduce the behavior:

import httpx
import skia
url = "https://pic3.zhimg.com/v2-2f57af939aef71f510c35928600d79f2_b.jpg"
response = httpx.get(url)
img1 = skia.Image.MakeFromEncoded(response.content)
img1.save("1.png")

img2 = skia.Image.fromarray(img1.toarray())
img2.save("2.png")

Expected behavior
R chennel and B chennel will not switch

Desktop (please complete the following information):

  • OS: 22.04.1-Ubuntu
  • Python: 3.10.6
  • skia-python version: 87.5

Additional context
Add any other context about the problem here.

@Polyisoprene Polyisoprene reopened this Jun 10, 2023
@Polyisoprene Polyisoprene changed the title Image.fromarray change rgb to bgr on the Linux Image.toarray change rgb to bgr on the Linux Jun 10, 2023
@Polyisoprene
Copy link
Author

This problem can be solved by passing the "colorType=kia. ColorType. kRGBA_8888_ColorType" parameter when calling toarray

@HinTak
Copy link
Collaborator

HinTak commented Jul 12, 2023

that's interesting. I just encounted a somewhat similar issue - pretty sure internally skia does kN32_8888 by default. FreeType internally does kBGRA actually, so I have to specify kBGRA to render directly from Skia into a freetype bitmap based memory canvas.

You are basically saying that conversion to/from, one of them is not N32.

@HinTak
Copy link
Collaborator

HinTak commented Jul 14, 2023

It is actually documented in one of skin's design document that libjpeg and libpng does RGBA and platform neutral, while skia's internal image format is platform dependent, and on intel/small-endian, it is happens to be BGRA . There are different way of getting at it, and some will invoke auto-swap, some won't. So this behaviour is apparently documented.

I found something similar in my situation. I have a buffer maintained by freetype and I wanted skia to fill it. The default doesn't work, and I have to explicitly tell skia that freetype wants BGRA and so please do it as such.

I was doing a SkBitmap canvas with a freetype buffer backing and just ask Skia use the SkBitmap as a Canvas. I guess in my case, because jpeg/png buffers are RGBA, so SkBitmap probably has a default of being RGBA.

@HinTak
Copy link
Collaborator

HinTak commented Aug 19, 2023

@kyamagu Hmm, looking at this again, why does toarray() and fromarray() have different defaults?
fromarray has a default of py::arg("colorType") = kN32_SkColorType, while toarray has py::arg("colorType") = kUnknown_SkColorType. I think Unknown probably will end up being N32 anyway, but somehow obviously hasn't happened. Assuming the code honours those colortypes correctly.

@kyamagu
Copy link
Owner

kyamagu commented Aug 21, 2023

@HinTak Don't remember why they are different. As kN32_SkColorType seems to exist only for historical reasons, all defaults should be kUnknown_SkColorType.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants