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

Re: [pygame] Exception in _numpysurfarray.py



On, Sat Apr 12, 2008, Lorenz Quack wrote:

[...]
> 
> Hey there,
> 
> well I did mix up the strides in the last patch. so here is my most recent 
> patch. Neither me nor my associate experience any of the problems  we had 
> before with this patch. However, the issue with the alpha value ending up 
> on index 0 remains. It would be really sad if we would have to reshape the 
> array just to get the alpha value in the correct spot.

The "bpp - 1" start messes this up for me. The trick would be to do the
same the C code does (which I should have done right from the
start). Attached you'll find the version which adapts the Numeric
implementation behaviour. Once more, let me know...

Regards
Marcus
Index: lib/_numpysurfarray.py
===================================================================
--- lib/_numpysurfarray.py	(Revision 1203)
+++ lib/_numpysurfarray.py	(Arbeitskopie)
@@ -181,38 +181,39 @@
     bpp = surface.get_bytesize ()
     if bpp < 3 or bpp > 4:
         raise ValueError, "unsupported bit depth for 3D reference array"
+    lilendian = pygame.get_sdl_byteorder () == pygame.LIL_ENDIAN
 
     start = 0
     step = 0
-    end = 0
 
     # Check for RGB or BGR surface.
     shifts = surface.get_shifts ()
     if shifts[0] == 16 and shifts[1] == 8 and shifts[2] == 0:
         # RGB 
-        end = None
-        if pygame.get_sdl_byteorder () == pygame.LIL_ENDIAN:
+        if lilendian:
             start = 2
             step = -1
         else:
             start = 0
             step = 1
-    else:
+    elif shifts[2] == 16 and shifts[1] == 8 and shifts[0] == 0:
         # BGR
-        end = 3
-        if pygame.get_sdl_byteorder () == pygame.LIL_ENDIAN:
+        if lilendian:
             start = 0
             step = 1
         else:
             start = 2
             step = -1
+    else:
+        raise ValueError, "unsupport colormasks for 3D reference array"
 
-    array = numpy.frombuffer (surface.get_buffer (), numpy.uint8)
-    array.shape = surface.get_height (), surface.get_pitch ()
-    array = array[:,:surface.get_width () * bpp]
-    array = numpy.reshape (array, (surface.get_width (), surface.get_height (),
-                                   bpp))
-    array = array[:,:,start:end:step]
+    if bpp == 4 and not lilendian:
+        start += 1
+
+    array = numpy.ndarray \
+            (shape=(surface.get_width (), surface.get_height (), 3),
+             dtype=numpy.uint8, buffer=surface.get_buffer (),
+             offset=start, strides=(bpp, surface.get_pitch (),step))
     return array
 
 def array_alpha (surface):

Attachment: pgphmEnGzPh7r.pgp
Description: PGP signature