~ubuntu-branches/ubuntu/maverick/pygame/maverick

« back to all changes in this revision

Viewing changes to lib/_numpysurfarray.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-01-14 17:02:11 UTC
  • mfrom: (1.3.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100114170211-21eop2ja7mr9vdcr
Tags: 1.9.1release-0ubuntu1
* New upstream version (lp: #433304)
* debian/control:
  - build-depends on libportmidi-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
    """
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")
67
67
 
68
68
    # Taken from Alex Holkner's pygame-ctypes package. Thanks a lot.
69
69
    data = surface.get_buffer ().raw
86
86
            data = '\0' + data
87
87
        bpp = 4
88
88
 
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)
111
111
    """
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")
115
115
 
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
119
119
 
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))
154
154
        return array
 
155
    elif bpp == 2:
 
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))
155
168
    else:
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))
164
177
 
165
178
def pixels3d (surface):
180
193
    """
181
194
    bpp = surface.get_bytesize ()
182
195
    if bpp < 3 or bpp > 4:
183
 
        raise ValueError, "unsupported bit depth for 3D reference array"
 
196
        raise ValueError("unsupported bit depth for 3D reference array")
184
197
    lilendian = pygame.get_sdl_byteorder () == pygame.LIL_ENDIAN
185
198
 
186
199
    start = 0
205
218
            start = 2
206
219
            step = -1
207
220
    else:
208
 
        raise ValueError, "unsupported colormasks for 3D reference array"
 
221
        raise ValueError("unsupported colormasks for 3D reference array")
209
222
 
210
223
    if bpp == 4 and not lilendian:
211
224
        start += 1
230
243
    (see the Surface.lock - lock the Surface memory for pixel access
231
244
    method).
232
245
    """
233
 
    if surface.get_bytesize () == 1 or not surface.get_alpha ():
234
 
        # 1 bpp surfaces and surfaces without alpha are always fully
235
 
        # opaque.
 
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
 
250
        # fully opaque.
236
251
        array = numpy.empty (surface.get_width () * surface.get_height (),
237
252
                             numpy.uint8)
238
253
        array.fill (0xff)
240
255
        return array
241
256
 
242
257
    array = array2d (surface)
243
 
    # Those shifts match the results from the old numeric system
244
 
    # exactly.
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))))
 
263
    else:
 
264
        # Taken from _numericsurfarray.c.
 
265
        array = array >> surface.get_shifts ()[3] << surface.get_losses ()[3]
246
266
    array = array.astype (numpy.uint8)
247
267
    return array
248
268
 
262
282
    lifetime of the array.
263
283
    """
264
284
    if surface.get_bytesize () != 4:
265
 
        raise ValueError, "unsupported bit depth for alpha reference array"
 
285
        raise ValueError("unsupported bit depth for alpha reference array")
266
286
    lilendian = pygame.get_sdl_byteorder () == pygame.LIL_ENDIAN
267
287
 
268
288
    # ARGB surface.
274
294
    elif surface.get_shifts ()[3] == 0 and not lilendian:
275
295
        start = 3
276
296
    else:
277
 
        raise ValueError, "unsupported colormasks for alpha reference array"
 
297
        raise ValueError("unsupported colormasks for alpha reference array")
278
298
 
279
299
    array = numpy.ndarray \
280
300
            (shape=(surface.get_width (), surface.get_height ()),
342
362
        g = 0xff << 8
343
363
        b = 0xff
344
364
    else:
345
 
        raise ValueError, "must be a valid 2d or 3d array"
 
365
        raise ValueError("must be a valid 2d or 3d array")
346
366
 
347
367
    surface = pygame.Surface ((shape[0], shape[1]), 0, bpp, (r, g, b, 0))
348
368
    blit_array (surface, array)
363
383
    """
364
384
    bpp = surface.get_bytesize ()
365
385
    if bpp <= 0 or bpp > 4:
366
 
        raise ValueError, "unsupported bit depth for surface"
 
386
        raise ValueError("unsupported bit depth for surface")
367
387
    
368
388
    shape = array.shape
369
389
    width = surface.get_width ()
383
403
    elif len (shape) == 2:
384
404
        array = numpy.transpose (array)
385
405
    else:
386
 
        raise ValueError, "must be a valid 2d or 3d array"
 
406
        raise ValueError("must be a valid 2d or 3d array")
387
407
 
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")
390
410
 
391
411
    itemsize = array.itemsize
392
412
    data = array.tostring ()
433
453
    # Taken from from Alex Holkner's pygame-ctypes package. Thanks a
434
454
    # lot.
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")
438
458
 
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")
442
462
 
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])
448
 
    return array
 
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]
 
472
    return out