[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[pygame] surfarray & alpha



Hi,

It seems like the alpha support in surfarray is missing the inverse to
surfarray.pixels_alpha() and surfarray.array_alpha().  Or am I missing
something obvious?

If I grab the alpha mask with pixels_alpha() I can only write to the
resulting array one pixel at a time, because it's non-contiguous (put,
putmask & flat seem to require contiguous arrays).  So, I figured,
I'll just build my alpha mask into a temp array and then copy it to
the alpha layer, but couldn't find the right way to do this -- best I
could come up with is a pixel by pixel copy in python.

Here's an example -- I know it's ugly, but it illustrates the problem
(it's a direct port of an old c-library) Goal is to generate an
trapezoidal alpha mask -- flat opaque top, linear ramp down to full
transparent from r1-r2:

    def alpha_gradient(self, r1, r2, x=0, y=0):
	x0 = arange(0, self.w)-(self.w/2)-x
	y0 = arange(0, self.h)-(self.h/2)+y
	x = x0[NewAxis,:]+(0*y0[:,NewAxis])
	y = (0*x0[NewAxis,:])-y0[:,NewAxis]
	d = ((x**2)+(y**2))**0.5
	d = (255 * (d - r1) / (r2 - r1)).astype(Int)
	d = where(less(d, 0), 0, d)
	d = where(greater(d, 255), 255, d)
	d = 255 - d
		
	a0 = pygame.surfarray.pixels_alpha(self.im)
	for _x in range(0, a0.shape[0]):
		for _y in range(0, a0.shape[1]):
			a0[_x, _y] = d[_y, _x]

Seems like there should be a C-level function to quickly
copy an array like d back into the alpha layer, no?

Thanks,
/jamie

-- 
office: 510-643-3573                mail: 3210 Tolman Hall #1650
   lab: 510-642-1950                      U.C. Berkeley
   fax: 801-697-4179			  Berkeley, CA 94720-1650
 email: mazer@socrates.berkeley.edu
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org