def save_jpg(surf, filename): if (surf.format.BytesPerPixel == 3 and not (surf.flags & sdl.SDL_SRCALPHA) and surf.format.Rshift == 0): ss_surf = surf else: if get_sdl_byteorder() == sdl.SDL_LIL_ENDIAN: rmask, gmask, bmask, amask = 0xff, 0xff00, 0xff0000, 0xff000000 else: rmask, gmask, bmask, amask = 0xff00, 0xff, 0xff0000, 0xff000000 ss_surf = sdl.SDL_CreateRGBSurface(sdl.SDL_SWSURFACE, surf.w, surf.h, 24, rmask, gmask, bmask, amask) if not ss_surf: return -1 rect = ffi.new('SDL_Rect*') rect.w = surf.w rect.h = surf.h sdl.SDL_BlitSurface(surf, rect, ss_surf, ffi.NULL) ss_rows = ffi.new('unsigned char*[]', surf.h) ss_pixels = ffi.cast('unsigned char*', ss_surf.pixels) for i in range(surf.h): ss_rows[i] = ss_pixels + i * ss_surf.pitch err_msg = ffi.new('char**') result = jpglib.write_jpeg(filename, ss_rows, surf.w, surf.h, 85, err_msg) if ss_surf is not surf: sdl.SDL_FreeSurface(ss_surf) if result == -1: raise IOError("JPGError: %s" % ffi.string(err_msg[0])) return result
def save_png(surf, filename): alpha = bool(surf.format.Amask) if get_sdl_byteorder() == sdl.SDL_LIL_ENDIAN: rmask, gmask, bmask, amask = 0xff, 0xff00, 0xff0000, 0xff000000 else: rmask, gmask, bmask, amask = 0xff00, 0xff0000, 0xff, 0x000000ff ss_surf = sdl.SDL_CreateRGBSurface(sdl.SDL_SWSURFACE | sdl.SDL_SRCALPHA, surf.w, surf.h, 32 if alpha else 24, rmask, gmask, bmask, amask) if not ss_surf: return -1 with opaque(surf): rect = ffi.new('SDL_Rect*') rect.w = surf.w rect.h = surf.h sdl.SDL_BlitSurface(surf, rect, ss_surf, ffi.NULL) ss_rows = ffi.new('unsigned char*[]', surf.h) ss_pixels = ffi.cast('unsigned char*', ss_surf.pixels) for i in range(surf.h): ss_rows[i] = ss_pixels + i * ss_surf.pitch err_msg = ffi.new('char**') result = pnglib.write_png(filename, ss_rows, surf.w, surf.h, (pnglib.PNG_COLOR_TYPE_RGB_ALPHA if alpha else pnglib.PNG_COLOR_TYPE_RGB), 8, err_msg) sdl.SDL_FreeSurface(ss_surf) if result == -1: raise IOError("PNGError: %s" % ffi.string(err_msg[0])) return result
def _set_at(self, x, y, c_color): bpp = self._format.BytesPerPixel if bpp == 1: pixels = ffi.cast("uint8_t*", self._c_surface.pixels) pixels[y * self._c_surface.pitch // bpp + x] = c_color elif bpp == 2: pixels = ffi.cast("uint16_t*", self._c_surface.pixels) pixels[y * self._c_surface.pitch // bpp + x] = c_color elif bpp == 3: pixels = ffi.cast("uint8_t*", self._c_surface.pixels) base = y * self._c_surface.pitch + x * 3 fmt = self._format if get_sdl_byteorder() == sdl.SDL_LIL_ENDIAN: pixels[base + (fmt.Rshift >> 3)] = ffi.cast( 'uint8_t', c_color >> 16) pixels[base + (fmt.Gshift >> 3)] = ffi.cast( 'uint8_t', c_color >> 8) pixels[base + (fmt.Bshift >> 3)] = ffi.cast('uint8_t', c_color) else: pixels[base + 2 - (fmt.Rshift >> 3)] = ffi.cast( 'uint8_t', c_color >> 16) pixels[base + 2 - (fmt.Gshift >> 3)] = ffi.cast( 'uint8_t', c_color >> 8) pixels[base + 2 - (fmt.Bshift >> 3)] = ffi.cast( 'uint8_t', c_color) elif bpp == 4: pixels = ffi.cast("uint32_t*", self._c_surface.pixels) pixels[y * (self._c_surface.pitch // bpp) + x] = c_color else: raise RuntimeError("invalid color depth for surface")
def _set_at(self, x, y, c_color): bpp = self._format.BytesPerPixel if bpp == 1: pixels = ffi.cast("uint8_t*", self._c_surface.pixels) pixels[y * self._c_surface.pitch // bpp + x] = c_color elif bpp == 2: pixels = ffi.cast("uint16_t*", self._c_surface.pixels) pixels[y * self._c_surface.pitch // bpp + x] = c_color elif bpp == 3: pixels = ffi.cast("uint8_t*", self._c_surface.pixels) base = y * self._c_surface.pitch + x * 3 fmt = self._format if get_sdl_byteorder() == sdl.SDL_LIL_ENDIAN: pixels[base + (fmt.Rshift >> 3)] = ffi.cast('uint8_t', c_color >> 16) pixels[base + (fmt.Gshift >> 3)] = ffi.cast('uint8_t', c_color >> 8) pixels[base + (fmt.Bshift >> 3)] = ffi.cast('uint8_t', c_color) else: pixels[base + 2 - (fmt.Rshift >> 3)] = ffi.cast('uint8_t', c_color >> 16) pixels[base + 2 - (fmt.Gshift >> 3)] = ffi.cast('uint8_t', c_color >> 8) pixels[base + 2 - (fmt.Bshift >> 3)] = ffi.cast('uint8_t', c_color) elif bpp == 4: pixels = ffi.cast("uint32_t*", self._c_surface.pixels) pixels[y * (self._c_surface.pitch // bpp) + x] = c_color else: raise RuntimeError("invalid color depth for surface")
def fromstring(string, size, format, flipped=False): w, h = size if w < 1 or h < 1: raise ValueError("Resolution must be positive values") if format == "P": if len(string) != w * h: raise ValueError("String length does not equal format and " "resolution size") surf = sdl.SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0) if not surf: raise SDLError.from_sdl_error() with locked(surf): pixels = ffi.cast('char*', surf.pixels) for y in range(h): dest = surf.pitch * y src_start = (h - 1 - y) * w if flipped else y * w pixels[dest:dest + w] = string[src_start:src_start + w] elif format == "RGB": if len(string) != w * h * 3: raise ValueError("String length does not equal format and " "resolution size") surf = sdl.SDL_CreateRGBSurface(0, w, h, 24, 0xff, 0xff << 16, 0xff << 8, 0) if not surf: raise SDLError.from_sdl_error() with locked(surf): pixels = ffi.cast("char*", surf.pixels) for y in range(h): dest = surf.pitch * y src_start = (h - 1 - y) * w * 3 if flipped else y * w * 3 row = string[src_start:src_start + w * 3] for x in range(0, w * 3, 3): # BYTE0, BYTE1 and BYTE2 are determined by byte order pixels[dest + x + BYTE0] = row[x + BYTE0] pixels[dest + x + BYTE1] = row[x + BYTE1] pixels[dest + x + BYTE2] = row[x + BYTE2] elif format in ("RGBA", "RGBAX", "ARGB"): if len(string) != w * h * 4: raise ValueError("String length does not equal format and " "resolution size") if format == "ARGB": if get_sdl_byteorder() == sdl.SDL_LIL_ENDIAN: rmask, gmask, bmask, amask = (0xff << 8, 0xff << 16, 0xff << 24, 0xff) else: rmask, gmask, bmask, amask = (0xff << 16, 0xff << 8, 0xff, 0xff << 24) surf = sdl.SDL_CreateRGBSurface(sdl.SDL_SRCALPHA, w, h, 32, rmask, gmask, bmask, amask) else: alphamult = format == "RGBA" if get_sdl_byteorder() == sdl.SDL_LIL_ENDIAN: rmask, gmask, bmask = 0xff, 0xff << 8, 0xff << 16 amask = 0xff << 24 if alphamult else 0 else: rmask, gmask, bmask = 0xff << 24, 0xff << 16, 0xff << 8 amask = 0xff if alphamult else 0 surf = sdl.SDL_CreateRGBSurface(sdl.SDL_SRCALPHA if alphamult else 0, w, h, 32, rmask, gmask, bmask, amask) if not surf: raise SDLError.from_sdl_error() with locked(surf): pixels = ffi.cast("char*", surf.pixels) for y in range(h): dest = surf.pitch * y src_start = (h - 1 - y) * w * 4 if flipped else y * w * 4 pixels[dest:dest + w * 4] = string[src_start:src_start + w * 4] else: raise ValueError("Unrecognized type of format") return Surface._from_sdl_surface(surf)
def fromstring(string, size, format, flipped=False): w, h = size if w < 1 or h < 1: raise ValueError("Resolution must be positive values") if format == "P": if len(string) != w * h: raise ValueError("String length does not equal format and " "resolution size") surf = sdl.SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0) if not surf: raise SDLError.from_sdl_error() with locked(surf): pixels = ffi.cast('char*', surf.pixels) for y in range(h): dest = surf.pitch * y src_start = (h - 1 - y) * w if flipped else y * w pixels[dest:dest + w] = string[src_start:src_start + w] elif format == "RGB": if len(string) != w * h * 3: raise ValueError("String length does not equal format and " "resolution size") surf = sdl.SDL_CreateRGBSurface(0, w, h, 24, 0xff, 0xff << 16, 0xff << 8, 0) if not surf: raise SDLError.from_sdl_error() with locked(surf): pixels = ffi.cast("char*", surf.pixels) for y in range(h): dest = surf.pitch * y src_start = (h - 1 - y) * w * 3 if flipped else y * w * 3 row = string[src_start:src_start + w * 3] for x in range(0, w * 3, 3): # BYTE0, BYTE1 and BYTE2 are determined by byte order pixels[dest + x + BYTE0] = row[x + BYTE0] pixels[dest + x + BYTE1] = row[x + BYTE1] pixels[dest + x + BYTE2] = row[x + BYTE2] elif format in ("RGBA", "RGBAX", "ARGB"): if len(string) != w * h * 4: raise ValueError("String length does not equal format and " "resolution size") if format == "ARGB": if get_sdl_byteorder() == sdl.SDL_LIL_ENDIAN: rmask, gmask, bmask, amask = (0xff << 8, 0xff << 16, 0xff << 24, 0xff) else: rmask, gmask, bmask, amask = (0xff << 16, 0xff << 8, 0xff, 0xff << 24) surf = sdl.SDL_CreateRGBSurface(sdl.SDL_SRCALPHA, w, h, 32, rmask, gmask, bmask, amask) else: alphamult = format == "RGBA" if get_sdl_byteorder() == sdl.SDL_LIL_ENDIAN: rmask, gmask, bmask = 0xff, 0xff << 8, 0xff << 16 amask = 0xff << 24 if alphamult else 0 else: rmask, gmask, bmask = 0xff << 24, 0xff << 16, 0xff << 8 amask = 0xff if alphamult else 0 surf = sdl.SDL_CreateRGBSurface( sdl.SDL_SRCALPHA if alphamult else 0, w, h, 32, rmask, gmask, bmask, amask) if not surf: raise SDLError.from_sdl_error() with locked(surf): pixels = ffi.cast("char*", surf.pixels) for y in range(h): dest = surf.pitch * y src_start = (h - 1 - y) * w * 4 if flipped else y * w * 4 pixels[dest:dest + w * 4] = string[src_start:src_start + w * 4] else: raise ValueError("Unrecognized type of format") return Surface._from_sdl_surface(surf)
""" XXX """ from pygame._error import SDLError, unpack_rect from pygame._sdl import sdl, ffi, get_sdl_byteorder from pygame.color import create_color, Color from pygame.rect import Rect, game_rect_from_obj from pygame.surflock import locked if get_sdl_byteorder() == sdl.SDL_LIL_ENDIAN: BYTE0 = 0 BYTE1 = 1 BYTE2 = 2 else: # BIG_ENDIAN BYTE0 = 2 BYTE1 = 1 BYTE2 = 0 class SubSurfaceData(object): def __init__(self, owner, pixeloffset, xoffset, yoffset): self.owner = owner self.pixeloffset = pixeloffset self.xoffset = xoffset self.yoffset = yoffset def rect_vals_from_obj(obj): if isinstance(obj, Rect): return obj.r.x, obj.r.y, obj.r.w, obj.r.h try: if len(obj) == 1:
""" XXX """ from pygame._error import SDLError, unpack_rect from pygame._sdl import sdl, ffi, get_sdl_byteorder from pygame.color import create_color, Color from pygame.rect import Rect, game_rect_from_obj from pygame.surflock import locked if get_sdl_byteorder() == sdl.SDL_LIL_ENDIAN: BYTE0 = 0 BYTE1 = 1 BYTE2 = 2 else: # BIG_ENDIAN BYTE0 = 2 BYTE1 = 1 BYTE2 = 0 class SubSurfaceData(object): def __init__(self, owner, pixeloffset, xoffset, yoffset): self.owner = owner self.pixeloffset = pixeloffset self.xoffset = xoffset self.yoffset = yoffset def rect_vals_from_obj(obj): if isinstance(obj, Rect): return obj.r.x, obj.r.y, obj.r.w, obj.r.h try: