Commits

Anonymous committed 8170a53

Palette surface support in array3d

  • Participants
  • Parent commits 4b53fb6
  • Branches ctypes-soc

Comments (0)

Files changed (2)

File examples_pygame/arraydemo.py

 import os
 try:
     import pygame
-    import Numeric as N
+    
+    # These should all work identically
+    #import numpy as N
+    import numarray as N
+    #import Numeric as N
+
     from pygame.locals import *
     surfarray = pygame.surfarray
     if not surfarray: raise ImportError
 
 def surfdemo_show(array_img, name):
     "displays a surface, waits for user to continue"
-    screen = pygame.display.set_mode(array_img.shape[:2], 0, 32)
+    screen = pygame.display.set_mode(array_img.shape[:2])
     surfarray.blit_array(screen, array_img)
-    print len(array_img.tostring())
     pygame.display.flip()
     pygame.display.set_caption(name)
     while 1:
         e = pygame.event.wait()
-        if e.type == MOUSEBUTTONDOWN: break
+        if e.type == MOUSEBUTTONDOWN: 
+            break
         elif e.type == KEYDOWN and e.key == K_s:
-            #pygame.image.save(screen, name+'.bmp')
-            #s = pygame.Surface(screen.get_size(), 0, 32)
-            #s = s.convert_alpha()
-            #s.fill((0,0,0,255))
-            #s.blit(screen, (0,0))
-            #s.fill((222,0,0,50), (0,0,40,40))
-            #pygame.image.save_extended(s, name+'.png')
-            #pygame.image.save(s, name+'.png')
-            #pygame.image.save(screen, name+'_screen.png')
-            #pygame.image.save(s, name+'.tga')
             pygame.image.save(screen, name+'.bmp')
-        elif e.type == QUIT: raise SystemExit
+        elif e.type == QUIT: 
+            raise SystemExit
 
+# Only required if N is not Numeric.
+pygame.surfarray.set_array_module(N)
 
+imagename = os.path.join('data', 'arraydemo.bmp')
+imgsurface = pygame.image.load(imagename)
 
-
-'''
 #allblack
 allblack = N.zeros((128, 128))
 surfdemo_show(allblack, 'allblack')
 
-
 #striped
 striped = N.zeros((128, 128, 3))
 striped[:] = (255, 0, 0)
 striped[:,::3] = (0, 255, 255)
 surfdemo_show(striped, 'striped')
-'''
-import numpy
-pygame.surfarray.set_array_module(numpy)
 
 #imgarray
-imagename = os.path.join('data', 'arraydemo.bmp')
-imgsurface = pygame.image.load(imagename)
 imgarray = surfarray.array2d(imgsurface)
-#imgarray[:,::3] = 0
 surfdemo_show(imgarray, 'imgarray')
 
-
 #flipped
 flipped = imgarray[:,::-1]
 surfdemo_show(flipped, 'flipped')
 scaledown = imgarray[::2,::2]
 surfdemo_show(scaledown, 'scaledown')
 
-'''
 #scaleup
 size = N.array(imgarray.shape)*2
-scaleup = N.zeros(size)
+if N.__name__ == 'numpy':
+    scaleup = N.zeros(size, dtype=imgarray.dtype)
+else:
+    scaleup = N.zeros(size, imgarray.typecode())
 scaleup[::2,::2] = imgarray
 scaleup[1::2,::2] = imgarray
 scaleup[:,1::2] = scaleup[:,::2]
+if N.__name__ == 'numpy':
+    # Workaround bug #1524509 at sourceforge.net/projects/numpy
+    scaleup = scaleup.transpose().copy().transpose()
 surfdemo_show(scaleup, 'scaleup')
 
-
 #redimg
 rgbarray = surfarray.array3d(imgsurface)
 redimg = N.array(rgbarray)
 redimg[:,:,1:] = 0
 surfdemo_show(redimg, 'redimg')
 
-
 #soften
 soften = N.array(rgbarray)*1
 soften[1:,:]  += rgbarray[:-1,:]*8
 soften /= 33
 surfdemo_show(soften, 'soften')
 
-
 #crossfade (50%)
 src = N.array(rgbarray)
 dest = N.zeros(rgbarray.shape)
 xfade = src + diff.astype(N.Int)
 surfdemo_show(xfade, 'xfade')
 
-'''
-
 #alldone

File pygame/surfarray.py

     This function will temporarily lock the Surface as pixels are copied (see
     the Surface.lock() method).
 
+    :note: This function requires Numeric or numpy if a palettized surface
+        is used; numarray will fail.
+
     :Parameters:
          `surface` : `Surface`
             Surface to copy.
     bpp = format.BytesPerPixel
 
     if format.BytesPerPixel == 1:
-        raise NotImplementedException, 'TODO: palette lookup'
+        # XXX Fails in numarray:
+        pal_r = _array.array([c.r for c in surf.format.palette.colors])
+        pal_g = _array.array([c.g for c in surf.format.palette.colors])
+        pal_b = _array.array([c.b for c in surf.format.palette.colors])
+        # (ValueError: _operator_compute: too many inputs + outputs
+        planes = [_array.choose(array, pal_r),
+                  _array.choose(array, pal_g),
+                  _array.choose(array, pal_b)]
+        array = _array.array(planes)
+        array = _array.transpose(array, (1, 2, 0))
     else:
         planes = [((array & format.Rmask) >> format.Rshift) << format.Rloss,
                   ((array & format.Gmask) >> format.Gshift) << format.Gloss,
 
     data = array.tostring()
 
-    print itemsize, bpp
     if itemsize > bpp:
-        print 'a'
         # Trim bytes from each element, keep least significant byte(s)
         if SDL_BYTEORDER == SDL_LIL_ENDIAN:
             pattern = '(%s)%s' % ('.' * bpp, '.' * (itemsize - bpp))
             pattern = '%s(%s)' % ('.' * (itemsize - bpp), '.' * bpp)
         data = ''.join(re.compile(pattern, flags=re.DOTALL).findall(data))
     elif itemsize < bpp:
-        print 'b'
         # Add pad bytes to each element, at most significant end
         pad = '\0' * (bpp - itemsize)
         pixels = re.compile('.' * itemsize, flags=re.DOTALL).findall(data)
     # Add zeros pad for pitch correction
     pitchdiff = surf.pitch - surf.w * bpp 
     if pitchdiff > 0:
-        print 'c'
         pad = '\0' * pitchdiff
         rows = re.compile('.' * surf.w * bpp, flags=re.DOTALL).findall(data)
         data = pad.join(rows) + pad