[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Numeric Question



On Sat, 2006-07-08 at 20:04 -0700, Kamilche wrote:
> Hey Pete, I have another Numeric question for you, if you've got the time.
> 
> Let's say I have a bunch of data in FFFFRGBA format, where FFFF is an 
> arbitrary floating point number, and RGBA are unsigned bytes. The 
> following routine will convert that to an RGBA picture, but it takes a 
> looooong, time.
> 
> What's the magical incantation to get Numeric to assign the data 
> directly to the pixels of the surface?

I'm not sure if that can be done actually. Numeric may have a way to do
"casting" between data types, but I'm not really sure. I was thinking
Numeric.array_constructor could bind an arbitrary sized and typed array
onto a string buffer of data. But it looks like that does not work.

You'll need a function like that in order for this to work. I know in
Python 2.5 the struct module can precompile your formatting codes, which
would speed this up. I also see that you unpack an "a" value out of the
binary data, but then throw it away.

Also, since you are doing such simple handling of the "float" data, you
could probably just treat the whole pixel as a bunch of bytes. All this
will help a bit, but won't make it orders of magnitude faster.

    xspan = range(w)
    ordfunc = ord
    setfunc = pic.set_at
    for y in range(h):
        for x in xspan:
            a = (ordfunc(data[ctr + 3]) & 0x7f < 0x46 and 255 or 0
            setfunc((x, y), (
                        ordfunc(data[ctr + 4],
                        ordfunc(data[ctr + 5],
                        ordfunc(data[ctr + 6],
                        a))
            ctr += 8


Sadly, I'm kind of wildly guessing here if that code to compare with
0x46 will match a floating point value with less than 6 digits left of
the decimal. But it works in some simplish tests.