Commits

aholkner  committed 0b84803

pygame.transform.scale, via PIL

  • Participants
  • Parent commits bf7a40c
  • Branches ctypes-soc

Comments (0)

Files changed (3)

File pygame/surface.py

         newsurf = SDL_DisplayFormatAlpha(self._surf)
         self._unprep()
 
-        return Surface(newsurf)
+        return Surface(surf=newsurf)
         
 
     def copy(self):
         newsurf = SDL_ConvertSurface(surf, surf.format, surf.flags) 
         self._unprep()
 
-        return Surface(newsurf)
+        return Surface(surf=newsurf)
 
     def fill(self, color, rect=None):
         '''Fill surface with a solid color.
         else:
             value = 255
 
-        SDL_SetAlpha(surf, flags, alpha)
+        SDL_SetAlpha(surf, flags, value)
 
     def get_alpha(self):
         '''Get the current surface alpha value.

File pygame/transform.py

 import pygame.base
 import pygame.surface
 
+try:
+    import Image
+    _have_PIL = True
+except ImportError:
+    _have_PIL = False
+
 def _newsurf_fromsurf(surf, width, height):
     format = surf.format
     newsurf = SDL_CreateRGBSurface(surf.flags, width, height,
         for i in range(len(rows)):
             rows[i] = rows[i][:newsurf.pitch]
     elif newsurf.pitch > surf.pitch:
-        pad = '\000' * newsurf.pitch - surf.pitch
+        pad = '\000' * (newsurf.pitch - surf.pitch)
         for i in range(len(rows)):
             rows[i] = rows[i] + pad
 
 
     :rtype: `Surface`
     '''
+    if not _have_PIL:
+        raise NotImplementedError, 'Python imaging library (PIL) required.'
+    
+    # XXX: Differ from Pygame: subsurfaces permitted.
+    width, height = size
+    if width < 0 or height < 0:
+        raise ValueError, 'Cannot scale to negative size'
+
+    surf = surface._surf
+    if not dest:
+        newsurf = _newsurf_fromsurf(surf, width, height)
+    else:
+        dest._prep()
+        newsurf = dest._surf
+
+    if newsurf.w != width or newsurf.h != height:
+        raise ValueError, 'Destination surface not the given width or height.'
+
+    if newsurf.format.BytesPerPixel != surf.format.BytesPerPixel:
+        raise ValueError, \
+              'Source and destination surfaces need the same format.'
+
+    if surf.format.BitsPerPixel == 8:
+        mode = 'P'
+    elif surf.format.BitsPerPixel == 24:
+        mode = 'RGB'
+    elif surf.format.BitsPerPixel == 32:
+        mode = 'RGBA'
+    else:
+        raise ValueError, 'Unsupported pixel format' # TODO convert
+
+    if width and height:
+        surface.lock()
+        data = surf.pixels.to_string()
+        surface.unlock()
+
+        source_pitch = surf.w * surf.format.BytesPerPixel
+        if surf.pitch > source_pitch:
+            rows = re.findall('.' * surf.pitch, data, re.DOTALL)
+            for i in range(len(rows)):
+                rows[i] = rows[i][:source_pitch]
+            data = ''.join(rows)
+
+        image = Image.fromstring(mode, (surf.w, surf.h), data)
+        image = image.resize((width, height), Image.NEAREST)
+        data = image.tostring()
+
+        dest_pitch = width * newsurf.format.BytesPerPixel
+        if newsurf.pitch > dest_pitch:
+            rows = re.findall('.' * dest_pitch, data, re.DOTALL)
+            pad = '\000' * (newsurf.pitch - dest_pitch)
+            for i in range(len(rows)):
+                rows[i] = rows[i] + pad
+            data = ''.join(rows)
+
+        SDL_LockSurface(newsurf)
+        memmove(newsurf.pixels.ptr, data, len(data))
+        SDL_UnlockSurface(newsurf)
+
+    if dest:
+        dest._unprep()
+        return dest
+    else:
+        return pygame.surface.Surface(surf=newsurf)
 
 def rotate(surface, angle):
     '''Rotate an image.

File test_pygame/transform_scale.py

+#!/usr/bin/env python
+
+'''Check that scale works.
+'''
+
+__docformat__ = 'restructuredtext'
+__version__ = '$Id$'
+
+import os
+import sys
+
+import pygame
+from pygame.locals import *
+import Image
+
+def show_image(surf, message):
+    screen = pygame.display.set_mode(surf.get_size())
+    pygame.display.set_caption(message)
+
+
+
+if __name__ == '__main__':
+    pygame.init()
+
+    if len(sys.argv) > 1:
+        image_file = sys.argv[1]
+    else:
+        image_file = os.path.join(os.path.dirname(sys.argv[0]), 
+                                  '../test/sample.bmp')
+
+    image = pygame.image.load(image_file)
+    w, h = image.get_size()
+    dx, dy = 1, 1
+
+    width, height = 800, 600
+    screen = pygame.display.set_mode((width, height))
+
+    while True:
+        for event in pygame.event.get():
+            if event.type in (KEYDOWN, QUIT):
+                sys.exit(0)
+
+        w += dx
+        h += dy
+        if w >= width or w <= 0:
+            dx = -dx
+        if h >= height or h <= 0:
+            dy = -dy
+        w = max(0, w)
+        h = max(0, h)
+
+        surf = pygame.transform.scale(image, (w, h))
+
+        screen.blit(surf, (0, 0))
+        pygame.display.flip()
+