64
64
bpp = surface.get_bytesize ()
65
65
if bpp <= 0 or bpp > 4:
66
raise ValueError, "unsupported bit depth for 2D array"
66
raise ValueError("unsupported bit depth for 2D array")
68
68
# Taken from Alex Holkner's pygame-ctypes package. Thanks a lot.
69
69
data = surface.get_buffer ().raw
89
typecode = (numpy.uint8, numpy.uint16, None, numpy.uint32)[bpp - 1]
89
typecode = (numpy.uint8, numpy.uint16, None, numpy.int32)[bpp - 1]
90
90
array = numpy.fromstring (data, typecode)
91
91
array.shape = (surface.get_height (), width)
92
92
array = numpy.transpose (array)
112
112
bpp = surface.get_bytesize ()
113
113
if bpp == 3 or bpp < 1 or bpp > 4:
114
raise ValueError, "unsupported bit depth for 2D reference array"
114
raise ValueError("unsupported bit depth for 2D reference array")
116
typecode = (numpy.uint8, numpy.uint16, None, numpy.uint32)[bpp - 1]
116
typecode = (numpy.uint8, numpy.uint16, None, numpy.int32)[bpp - 1]
117
117
array = numpy.frombuffer (surface.get_buffer (), typecode)
118
118
array.shape = surface.get_height (), surface.get_pitch () / bpp
149
149
planes = [numpy.choose (array, pal_r),
150
150
numpy.choose (array, pal_g),
151
151
numpy.choose (array, pal_b)]
152
array = numpy.array (planes)
152
array = numpy.array (planes, numpy.uint8)
153
153
array = numpy.transpose (array, (1, 2, 0))
156
# Taken from SDL_GetRGBA.
157
masks = surface.get_masks ()
158
shifts = surface.get_shifts ()
159
losses = surface.get_losses ()
160
vr = (array & masks[0]) >> shifts[0]
161
vg = (array & masks[1]) >> shifts[1]
162
vb = (array & masks[2]) >> shifts[2]
163
planes = [(vr << losses[0]) + (vr >> (8 - (losses[0] << 1))),
164
(vg << losses[1]) + (vg >> (8 - (losses[1] << 1))),
165
(vb << losses[2]) + (vb >> (8 - (losses[2] << 1)))]
166
array = numpy.array (planes, numpy.uint8)
167
return numpy.transpose (array, (1, 2, 0))
156
169
masks = surface.get_masks ()
157
170
shifts = surface.get_shifts ()
158
171
losses = surface.get_losses ()
159
planes = [((array & masks[0]) >> shifts[0]) << losses[0],
160
((array & masks[1]) >> shifts[1]) << losses[1],
161
((array & masks[2]) >> shifts[2]) << losses[2]]
162
array = numpy.array (planes)
172
planes = [((array & masks[0]) >> shifts[0]), # << losses[0], Assume 0
173
((array & masks[1]) >> shifts[1]), # << losses[1],
174
((array & masks[2]) >> shifts[2])] # << losses[2]]
175
array = numpy.array (planes, numpy.uint8)
163
176
return numpy.transpose (array, (1, 2, 0))
165
178
def pixels3d (surface):
230
243
(see the Surface.lock - lock the Surface memory for pixel access
233
if surface.get_bytesize () == 1 or not surface.get_alpha ():
234
# 1 bpp surfaces and surfaces without alpha are always fully
246
if (surface.get_bytesize () == 1 or
247
surface.get_alpha () is None or
248
surface.get_masks ()[3] == 0):
249
# 1 bpp surfaces and surfaces without per-pixel alpha are always
236
251
array = numpy.empty (surface.get_width () * surface.get_height (),
238
253
array.fill (0xff)
242
257
array = array2d (surface)
243
# Those shifts match the results from the old numeric system
245
array = array >> surface.get_shifts ()[3] << surface.get_losses ()[3]
258
if surface.get_bytesize () == 2:
259
# Taken from SDL_GetRGBA.
260
va = (array & surface.get_masks ()[3]) >> surface.get_shifts ()[3]
261
array = ((va << surface.get_losses ()[3]) +
262
(va >> (8 - (surface.get_losses ()[3] << 1))))
264
# Taken from _numericsurfarray.c.
265
array = array >> surface.get_shifts ()[3] << surface.get_losses ()[3]
246
266
array = array.astype (numpy.uint8)
274
294
elif surface.get_shifts ()[3] == 0 and not lilendian:
277
raise ValueError, "unsupported colormasks for alpha reference array"
297
raise ValueError("unsupported colormasks for alpha reference array")
279
299
array = numpy.ndarray \
280
300
(shape=(surface.get_width (), surface.get_height ()),
345
raise ValueError, "must be a valid 2d or 3d array"
365
raise ValueError("must be a valid 2d or 3d array")
347
367
surface = pygame.Surface ((shape[0], shape[1]), 0, bpp, (r, g, b, 0))
348
368
blit_array (surface, array)
383
403
elif len (shape) == 2:
384
404
array = numpy.transpose (array)
386
raise ValueError, "must be a valid 2d or 3d array"
406
raise ValueError("must be a valid 2d or 3d array")
388
408
if width != shape[0] or surface.get_height () != shape[1]:
389
raise ValueError, "array must match the surface dimensions"
409
raise ValueError("array must match the surface dimensions")
391
411
itemsize = array.itemsize
392
412
data = array.tostring ()
433
453
# Taken from from Alex Holkner's pygame-ctypes package. Thanks a
435
455
bpp = surface.get_bytesize ()
436
if bpp <= 0 or bpp > 4:
437
raise ValueError, "unsupported bit depth for surface array"
456
if bpp <= 1 or bpp > 4:
457
raise ValueError("unsupported bit depth for surface array")
439
459
shape = array.shape
440
460
if shape[-1] != 3:
441
raise ValueError, "array must be a 3d array of 3-value color data"
461
raise ValueError("array must be a 3d array of 3-value color data")
443
463
shifts = surface.get_shifts ()
444
464
losses = surface.get_losses ()
445
array = (array[:,:,::3] >> losses[0] << shifts[0]) | \
446
(array[:,:,1::3] >> losses[1] << shifts[1]) | \
447
(array[:,:,2::3] >> losses[2] << shifts[2])
465
if array.dtype != numpy.int32:
466
array = array.astype(numpy.int32)
467
out = array[...,0] >> losses[0] << shifts[0]
468
out[...] |= array[...,1] >> losses[1] << shifts[1]
469
out[...] |= array[...,2] >> losses[2] << shifts[2]
470
if surface.get_flags() & pygame.SRCALPHA:
471
out[...] |= numpy.int32(255) >> losses[3] << shifts[3]