def smoothscale(surface, size, dest_surface=None): """ smoothscale(Surface, (width, height), DestSurface = None) -> Surface scale a surface to an arbitrary size smoothly """ width, height = size if width < 0 or height < 0: raise ValueError("Cannot scale to negative size") c_surf = surface._c_surface bpp = c_surf.format.BytesPerPixel if bpp < 3 or bpp > 4: raise ValueError("Only 24-bit or 32-bit surfaces can be" " smoothly scaled") if dest_surface is None: new_surf = new_surface_from_surface(c_surf, width, height) else: new_surf = dest_surface._c_surface if new_surf.w != width or new_surf.h != height: raise ValueError("Destination surface not the given width or height.") if (width * bpp + 3) // 4 > new_surf.pitch: raise ValueError("SDL Error: destination surface pitch not" " 4-byte aligned.") if width and height: with locked(new_surf): with locked(c_surf): if c_surf.w == width and c_surf.h == height: # Non-scaling case, so just copy the correct pixels c_pitch = c_surf.pitch n_pitch = new_surf.pitch srcpixels = ffi.cast('uint8_t*', c_surf.pixels) destpixels = ffi.cast('uint8_t*', new_surf.pixels) step = width * bpp for y in range(0, height): offset_n = y * n_pitch offset_c = y * c_pitch destpixels[offset_n:offset_n + step] = srcpixels[offset_c:offset_c + step] else: sdl.scalesmooth(c_surf, new_surf) if dest_surface: return dest_surface return Surface._from_sdl_surface(new_surf)
def smoothscale(surface, size, dest_surface=None): """ smoothscale(Surface, (width, height), DestSurface = None) -> Surface scale a surface to an arbitrary size smoothly """ width, height = size if width < 0 or height < 0: raise ValueError("Cannot scale to negative size") c_surf = surface._c_surface bpp = c_surf.format.BytesPerPixel if bpp < 3 or bpp > 4: raise ValueError("Only 24-bit or 32-bit surfaces can be" " smoothly scaled") if dest_surface is None: new_surf = new_surface_from_surface(c_surf, width, height) else: new_surf = dest_surface._c_surface if new_surf.w != width or new_surf.h != height: raise ValueError("Destination surface not the given width or height.") if (width * bpp + 3) // 4 > new_surf.pitch: raise ValueError("SDL Error: destination surface pitch not" " 4-byte aligned.") if width and height: with locked(new_surf): with locked(c_surf): if c_surf.w == width and c_surf.h == height: pitch = c_surf.pitch # Trivial case srcpixels = ffi.cast('uint8_t*', c_surf.pixels) destpixels = ffi.cast('uint8_t*', new_surf.pixels) destpixels[0:height * pitch] = srcpixels[0:height * pitch] else: sdl.scalesmooth(c_surf, new_surf) if dest_surface: return dest_surface return Surface._from_sdl_surface(new_surf)