[cairo] using ARGB32 surfaces with pygame

Bill Spitzak spitzak at thefoundry.co.uk
Fri Feb 20 10:14:09 PST 2009


That message from me is quite out of date.

Due to Windows using the ARGB32 format, that is what all hardware 
supports best, and therefore is the correct format for Cairo to use by 
default.

Also it appears everybody has agreed that adding "32" to the end of a 
name means that the name indicates the order of the bytes in a word, an 
if there is no suffix it indicates the order of the bytes in memory. 
This new usage seems to be taking over from the previous X mistakes.

It does sound like your library wants RGBA and (on a little-endian 
machine) you convert ARGB32 to it by swapping the bytes at 0 and 2.

Jayesh Salvi wrote:
> Hi,
> 
> I am creating FORMAT_ARGB32 surfaces using pycairo and while rendering 
> them as pygame images, I found them to have different colors. It looked 
> like there was some incompatibility in RGB order between pycairo and 
> pygame. After spending some time, I found following code segment 
> somewhere which fixed the colors.
> 
> 
>         buf = surface.get_data()
>         a = numpy.frombuffer(buf,numpy.uint8)
>         a.shape = (surface.get_width(),
>                     surface.get_height(),4)
>         tmp = copy(a[:,:,0])
>         a[:,:,0] = a[:,:,2]
>         a[:,:,2] = tmp
>         return a
> 
> It looks like two colors are swapped (I suppose R and B, but I am not 
> sure). But it works.
> 
> My question is can I avoid doing this? In my app this operation will 
> occur many times and hence will affect the performance of GUI. Also it 
> adds a dependency on numpy.
> 
> I found an old discussion on the mailing list that mentions "ABGR" color 
> format (which I suppose is what pygame is expecting above)
> 
> http://lists.cairographics.org/archives/cairo/2005-January/002637.html
> 
> But at present I didn't find any such format option in pycairo. All 
> FORMATs that are there today are:
> ['FORMAT_A1', 'FORMAT_A8', 'FORMAT_ARGB32', 'FORMAT_RGB16_565', 
> 'FORMAT_RGB24']
> 
> Can somebody shed some light?
> 
> Thanks,


-- 
Bill Spitzak, Senior Software Engineer
The Foundry, 618 Hampton Drive, Venice, CA, 90291, USA
Tel: +1 310 399-4555 * Fax: +1 310 450-4516 * Web: www.thefoundry.co.uk
The Foundry Visionmongers Ltd * Registered in England and Wales No: 4642027


More information about the cairo mailing list