Inverting Red and Blue Pixels.

For some unknown evil purpose, I am decided to invert the red and blue pixels because the BGRA format annoys me.

:smiling_imp:

In that purpose I wrote a C routine using weave; though, it eats up 52.4%(measured :wink:) of the framerate. It is the best I can do and it’s not quite fast.

Can you do better?

Here’s my code:

from scipy.weave import converters
from scipy import weave
import numpy
def bgSwap(data):
    """Swaps the blue and red bytes"""
    l=len(data)
    data  = numpy.fromstring(data,dtype=numpy.uint8)
    code="""
int n=0;
uint8_t t;
for (n=0;n<=l;n=n+4)
{
    if (n+4<=l)
    {
        t=(uint8_t) data[n];
        data[n]  =(uint8_t) data[n+2];
        data[n+2]=t;
    }
}
"""
    weave.inline(code,
                 ['l','data'])
    return data.tostring()