Skip to content

Add BGRa for compatibility with cairo - #2053

Closed
arjennienhuis wants to merge 5 commits into
python-pillow:masterfrom
arjennienhuis:master
Closed

arjennienhuis wants to merge 5 commits into
python-pillow:masterfrom
arjennienhuis:master

Conversation

@arjennienhuis

Copy link
Copy Markdown
Contributor

Cairo uses BGRa format as its CAIRO_FORMAT_ARGB32

each pixel is a 32-bit quantity, with alpha in the upper 8 bits, then red, then green, then blue. The 32-bit quantities are stored native-endian. Pre-multiplied alpha is used. (That is, 50% transparent red is 0x80800000, not 0x80ff0000.)

This patch makes it possible to this:

Image.frombuffer("RGBA", (im.width, im.height), data, "raw", "BGRa", im.stride, 1)

instead of this:

im = Image.frombuffer("RGBA", (im.width, im.height), data, "raw", "RGBa", im.stride, 1)
r, g, b, a = im.split()
im = Image.merge("RGBA", (b, g, r, a))

@wiredfool

Copy link
Copy Markdown
Member

Question: Since the 32 bit quantities are native endian, are there any modes that would be required for similar streamlining on a bigendian machine?

@arjennienhuis

Copy link
Copy Markdown
Contributor Author

Yes, this needs tests. Can you give me a pointer how I do that? Can I just test the high level interfaces like Image.frombuffer() and tobytes()?

@arjennienhuis

Copy link
Copy Markdown
Contributor Author

For bigendian it would be "aRGB" I guess. I have no machine to test this on though. What uses bigendian that I could find?

@homm

homm commented Aug 7, 2016

Copy link
Copy Markdown
Member

I'm against adding new modes just for compatibility with something. This is just extra code to support, no one will do this. I'm closing in favor of #2055.

@homm homm closed this Aug 7, 2016
@homm

homm commented Aug 7, 2016

Copy link
Copy Markdown
Member

By the way, even in the current versions you can do the followings:

Image.frombuffer("RGBa", (im.width, im.height), data, "raw", "BGRa", im.stride, 1).convert('RGBA')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants