def load(filename, namehint=""): try: filename = rwops_encode_file_path(filename) c_surface = sdl.IMG_Load(filename) except SDLError: # filename is not a string, try as file object try: rwops = rwops_from_file(filename) if namehint: name, ext = path.splitext(namehint) elif hasattr(filename, 'name'): name, ext = path.splitext(filename.name) else: # No name info, so we pass an empty extension to # SDL to indicate we can only load files with # suitable magic format markers in the file. name, ext = '', '' if len(ext) == 0: ext = name ext = rwops_encode_file_path(ext) c_surface = sdl.IMG_LoadTyped_RW(rwops, 1, ext) except TypeError: raise TypeError("file argument must be a valid path " "string or file object") if not c_surface: raise SDLError(ffi.string(sdl.IMG_GetError())) return Surface._from_sdl_surface(c_surface)
def scale(surface, size, dest_surface=None): """ scale(Surface, (width, height), DestSurface = None) -> Surface resize to new resolution """ width, height = size if width < 0 or height < 0: raise ValueError("Cannot scale to negative size") c_surf = surface._c_surface 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 c_surf.format.BytesPerPixel != new_surf.format.BytesPerPixel: raise ValueError( "Source and destination surfaces need the same format.") if width and height: with locked(new_surf): with locked(c_surf): sdl.stretch(c_surf, new_surf) return Surface._from_sdl_surface(new_surf)
def render(self, text, antialias, color, background=None): """Font.render(text, antialias, color, background=None): return Surface draw text on a new Surface""" color = Color(color) fg = ffi.new("SDL_Color [1]") bg = ffi.new("SDL_Color [1]") fg[0].r = color.r fg[0].g = color.g fg[0].b = color.b if background: try: background = Color(background) bg[0].r = background.r bg[0].g = background.g bg[0].b = background.b except (TypeError, ValueError): # Same error behaviour as pygame bg[0].r = 0 bg[0].g = 0 bg[0].b = 0 else: bg[0].r = 0 bg[0].g = 0 bg[0].b = 0 if text is None or text == "": # Just return a surface of width 1 x font height height = sdl.TTF_FontHeight(self._sdl_font) surf = Surface((1, height)) if background and isinstance(background, Color): surf.fill(background) else: # clear the colorkey surf.set_colorkey(flags=sdl.SDL_SRCCOLORKEY) return surf if not isinstance(text, basestring): raise TypeError("text must be a string or unicode") if "\x00" in text: raise ValueError("A null character was found in the text") if isinstance(text, unicode): text = text.encode("utf-8", "replace") if utf_8_needs_UCS_4(text): raise UnicodeError("A Unicode character above '\\uFFFF' was found;" " not supported") if antialias: if background is None: sdl_surf = sdl.TTF_RenderUTF8_Blended(self._sdl_font, text, fg[0]) else: sdl_surf = sdl.TTF_RenderUTF8_Shaded(self._sdl_font, text, fg[0], bg[0]) else: sdl_surf = sdl.TTF_RenderUTF8_Solid(self._sdl_font, text, fg[0]) if not sdl_surf: raise SDLError(ffi.string(sdl.TTF_GetError())) surf = Surface._from_sdl_surface(sdl_surf) if not antialias and background is not None: surf.set_colorkey() surf.set_palette([(bg[0].r, bg[0].g, bg[0].b)]) return surf
def flip(surface, xaxis, yaxis): c_surf = surface._c_surface w, h = c_surf.w, c_surf.h new_surf = new_surface_from_surface(c_surf, w, h) bpp = c_surf.format.BytesPerPixel pitch = c_surf.pitch with locked(new_surf): with locked(surface._c_surface): # only have to deal with rows if not xaxis: srcpixels = ffi.cast('uint8_t*', c_surf.pixels) destpixels = ffi.cast('uint8_t*', new_surf.pixels) if not yaxis: # no changes - just copy pixels destpixels[0:h * pitch] = srcpixels[0:h * pitch] else: for y in range(h): dest_start = (h - y - 1) * pitch src_start = y * pitch destpixels[dest_start:dest_start + pitch] = \ srcpixels[src_start:src_start + pitch] # have to calculate position for individual pixels else: if not yaxis: def get_y(y): return y else: def get_y(y): return h - y - 1 if bpp in (1, 2, 4): ptr_type = 'uint%s_t*' % c_surf.format.BitsPerPixel srcpixels = ffi.cast(ptr_type, c_surf.pixels) destpixels = ffi.cast(ptr_type, new_surf.pixels) for y in range(h): dest_row_start = get_y(y) * w src_row_start = y * w for x in range(w): destpixels[dest_row_start + (w - x - 1)] = \ srcpixels[src_row_start + x] else: srcpixels = ffi.cast('uint8_t*', c_surf.pixels) destpixels = ffi.cast('uint8_t*', new_surf.pixels) for y in range(h): dest_row_start = get_y(y) * pitch src_row_start = y * pitch for x in range(0, pitch, 3): dest_pix_start = dest_row_start + (pitch - x - 3) src_pix_start = src_row_start + x destpixels[dest_pix_start:dest_pix_start + 3] = \ srcpixels[src_pix_start:src_pix_start + 3] return Surface._from_sdl_surface(new_surf)
def rotozoom(surface, angle, scale): """ rotozoom(Surface, angle, scale) -> Surface filtered scale and rotation """ c_surf = surface._c_surface if scale == 0.0: new_surf = new_surface_from_surface(c_surf, c_surf.w, c_surf.h) return Surface._from_sdl_surface(new_surf) if c_surf.format.BitsPerPixel == 32: surf32 = c_surf else: surf32 = sdl.SDL_CreateRGBSurface(sdl.SDL_SWSURFACE, surf.w, surf.h, 32, 0xff, 0xff00, 0xff0000, 0xff000000) sdl.SDL_BlitSurface(surf, ffi.NULL, surf32, ffi.NULL) new_surf = sdl.rotozoomSurface(surf32, angle, scale, 1) 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: # 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 rotate(surface, angle): c_surf = surface._c_surface # special treatment if rotating by 90 degrees if abs(angle) == 90.0: numturns = (angle / 90) % 4 if numturns < 0: numturns = 4 + numturns if numturns % 2 == 0: width = c_surf.w height = c_surf.h else: width = c_surf.h height = c_surf.w new_surf = new_surface_from_surface(c_surf, width, height) with locked(new_surf): with locked(c_surf): sdl.rotate90(c_surf, new_surf, int(angle)) else: radangle = angle * 0.01745329251994329 sangle = math.sin(radangle) cangle = math.cos(radangle) x, y = c_surf.w, c_surf.h cx, cy = cangle * x, cangle * y sx, sy = sangle * x, sangle * y nxmax = int( max(max(max(abs(cx + sy), abs(cx - sy)), abs(-cx + sy)), abs(-cx - sy))) nymax = int( max(max(max(abs(sx + cy), abs(sx - cy)), abs(-sx + cy)), abs(-sx - cy))) new_surf = new_surface_from_surface(c_surf, nxmax, nymax) if c_surf.flags & sdl.SDL_SRCCOLORKEY: bgcolor = c_surf.format.colorkey else: bgcolor = surface.get_at_mapped((0, 0)) bgcolor &= ~(c_surf.format.Amask) with locked(new_surf): with locked(c_surf): sdl.rotate(c_surf, new_surf, bgcolor, sangle, cangle) return Surface._from_sdl_surface(new_surf)
def scale2x(surface, dest_surface=None): """ scale2x(Surface, DestSurface = None) -> Surface specialized image doubler """ c_surf = surface._c_surface if dest_surface: new_surf = dest_surface._c_surface if (new_surf.w != 2 * c_surf.w) or (new_surf.h != 2 * c_surf.h): raise ValueError("Destination surface not 2x bigger") else: new_surf = new_surface_from_surface(c_surf, c_surf.w * 2, c_surf.h * 2) with locked(new_surf): with locked(c_surf): sdl.scale2x(c_surf, new_surf) if dest_surface: return dest_surface return Surface._from_sdl_surface(new_surf)
def rotate(surface, angle): c_surf = surface._c_surface # special treatment if rotating by 90 degrees if abs(angle) == 90.0 or abs(angle) == 180.0 or abs(angle) == 0.0: numturns = (angle / 90) % 4 if numturns < 0: numturns = 4 + numturns if numturns % 2 == 0: width = c_surf.w height = c_surf.h else: width = c_surf.h height = c_surf.w new_surf = new_surface_from_surface(c_surf, width, height) with locked(new_surf): with locked(c_surf): sdl.rotate90(c_surf, new_surf, int(angle)) else: radangle = angle * 0.01745329251994329 sangle = math.sin(radangle) cangle = math.cos(radangle) x, y = c_surf.w, c_surf.h cx, cy = cangle * x, cangle * y sx, sy = sangle * x, sangle * y nxmax = int(max(max(max(abs(cx + sy), abs(cx - sy)), abs(-cx + sy)), abs(-cx - sy))) nymax = int(max(max(max(abs(sx + cy), abs(sx - cy)), abs(-sx + cy)), abs(-sx - cy))) new_surf = new_surface_from_surface(c_surf, nxmax, nymax) if c_surf.flags & sdl.SDL_SRCCOLORKEY: bgcolor = c_surf.format.colorkey else: bgcolor = surface.get_at_mapped((0, 0)) bgcolor &= ~(c_surf.format.Amask) with locked(new_surf): with locked(c_surf): sdl.rotate(c_surf, new_surf, bgcolor, sangle, cangle) 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)
def load(filename, namehint=""): try: filename = rwops_encode_file_path(filename) c_surface = sdl.IMG_Load(filename) except SDLError: # filename is not a string, try as file object try: rwops = rwops_from_file(filename) if namehint: name, ext = path.splitext(namehint) else: name, ext = path.splitext(filename.name) if len(ext) == 0: ext = name c_surface = sdl.IMG_LoadTyped_RW(rwops, 1, ext) except TypeError: raise TypeError("file argument must be a valid path " "string or file object") if not c_surface: raise SDLError(ffi.string(sdl.IMG_GetError())) return Surface._from_sdl_surface(c_surface)
def render(self, text, antialias, color, background=None): """Font.render(text, antialias, color, background=None): return Surface draw text on a new Surface""" color = Color(color) fg = ffi.new("SDL_Color [1]") bg = ffi.new("SDL_Color [1]") fg[0].r = color.r fg[0].g = color.g fg[0].b = color.b if background: try: background = Color(background) bg[0].r = background.r bg[0].g = background.g bg[0].b = background.b except (TypeError, ValueError): # Same error behaviour as pygame bg[0].r = 0 bg[0].g = 0 bg[0].b = 0 else: bg[0].r = 0 bg[0].g = 0 bg[0].b = 0 if text is None or text == '': # Just return a surface of width 1 x font height height = sdl.TTF_FontHeight(self._sdl_font) surf = Surface((1, height)) if background and isinstance(background, Color): surf.fill(background) else: # clear the colorkey surf.set_colorkey(flags=sdl.SDL_SRCCOLORKEY) return surf if not isinstance(text, (bytes_, unicode_)): raise TypeError("text must be a string or unicode") if isinstance(text, unicode_): text = text.encode('utf-8', 'replace') if utf_8_needs_UCS_4(text): raise UnicodeError( "A Unicode character above '\\uFFFF' was found;" " not supported") if b'\x00' in text: raise ValueError("A null character was found in the text") if antialias: if background is None: sdl_surf = sdl.TTF_RenderUTF8_Blended(self._sdl_font, text, fg[0]) else: sdl_surf = sdl.TTF_RenderUTF8_Shaded(self._sdl_font, text, fg[0], bg[0]) else: sdl_surf = sdl.TTF_RenderUTF8_Solid(self._sdl_font, text, fg[0]) if not sdl_surf: raise SDLError(ffi.string(sdl.TTF_GetError())) surf = Surface._from_sdl_surface(sdl_surf) if not antialias and background is not None: surf.set_colorkey() surf.set_palette([(bg[0].r, bg[0].g, bg[0].b)]) return surf
def chop(surface, rect): """ chop(Surface, rect) -> Surface gets a copy of an image with an interior area removed """ rect = Rect(rect) width = rect.width height = rect.width x = rect.x y = rect.y if rect.right > surface._w: width = surface._w - rect.x if rect.height > surface._h: height = surface._h - rect.y if rect.x < 0: width -= -x x = 0 if rect.y < 0: height -= -y y = 0 c_surf = surface._c_surface new_surf = new_surface_from_surface(c_surf, surface._w, surface._h) bpp = c_surf.format.BytesPerPixel pitch = c_surf.pitch w, h = c_surf.w, c_surf.h with locked(new_surf): with locked(c_surf): if bpp in (1, 2, 4): ptr_type = 'uint%s_t*' % c_surf.format.BitsPerPixel srcpixels = ffi.cast(ptr_type, c_surf.pixels) destpixels = ffi.cast(ptr_type, new_surf.pixels) else: srcpixels = ffi.cast('uint8_t*', c_surf.pixels) destpixels = ffi.cast('uint8_t*', new_surf.pixels) dy = 0 for sy in range(0, surface._h): if sy >= y and sy < y + height: continue dx = 0 if bpp in (1, 2, 4): dest_row_start = dy * w src_row_start = sy * w else: dest_row_start = dy * pitch src_row_start = sy * pitch for sx in range(0, surface._w): if sx >= x and sx < x + width: continue if bpp in (1, 2, 4): destpixels[dest_row_start + dx] = \ srcpixels[src_row_start + sx] else: dest_pix_start = dest_row_start + dx src_pix_start = src_row_start + sx destpixels[dest_pix_start:dest_pix_start + 3] = \ srcpixels[src_pix_start:src_pix_start + 3] dx += 1 dy += 1 return Surface._from_sdl_surface(new_surf)
def chop(surface, rect): """ chop(Surface, rect) -> Surface gets a copy of an image with an interior area removed """ rect = Rect(rect) width = rect.width height = rect.width x = rect.x y = rect.y if rect.right > surface._w: width = surface._w - rect.x if rect.height > surface._h: height = surface._h - rect.y if rect.x < 0: width -= -x x = 0 if rect.y < 0: height -= -y y = 0 c_surf = surface._c_surface new_surf = new_surface_from_surface(c_surf, surface._w, surface._h) bpp = c_surf.format.BytesPerPixel src_pitch = c_surf.pitch dest_pitch = new_surf.pitch w, h = c_surf.w, c_surf.h with locked(new_surf): with locked(c_surf): if bpp in (1, 2, 4): ptr_type = 'uint%s_t*' % c_surf.format.BitsPerPixel srcpixels = ffi.cast(ptr_type, c_surf.pixels) destpixels = ffi.cast(ptr_type, new_surf.pixels) else: srcpixels = ffi.cast('uint8_t*', c_surf.pixels) destpixels = ffi.cast('uint8_t*', new_surf.pixels) dy = 0 if bpp in (1, 2, 4): dest_step = dest_pitch // bpp src_step = src_pitch // bpp for sy in range(0, surface._h): if sy >= y and sy < y + height: continue dx = 0 if bpp in (1, 2, 4): dest_row_start = dy * dest_step src_row_start = sy * src_step else: dest_row_start = dy * dest_pitch src_row_start = sy * src_pitch for sx in range(0, surface._w): if sx >= x and sx < x + width: continue if bpp in (1, 2, 4): destpixels[dest_row_start + dx] = \ srcpixels[src_row_start + sx] else: dest_pix_start = dest_row_start + dx src_pix_start = src_row_start + sx destpixels[dest_pix_start:dest_pix_start + 3] = \ srcpixels[src_pix_start:src_pix_start + 3] dx += 1 dy += 1 return Surface._from_sdl_surface(new_surf)
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 tostring(surface, format, flipped=False): """ tostring(Surface, format, flipped=False) -> string transfer image to string buffer """ surf = surface._c_surface if surf.flags & sdl.SDL_OPENGL: raise NotImplementedError() if format == "P": if surf.format.BytesPerPixel != 1: raise ValueError("Can only create \"P\" format data with " "8bit Surfaces") with locked(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)
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 c_surf.format.BytesPerPixel != new_surf.format.BytesPerPixel: raise ValueError( "Source and destination surfaces need the same format.") if width and height: with locked(new_surf): with locked(c_surf): sdl.stretch(c_surf, new_surf) return Surface._from_sdl_surface(new_surf) def rotozoom(surface, angle, scale): """ rotozoom(Surface, angle, scale) -> Surface filtered scale and rotation """ raise NotImplementedError def scale2x(surface, dest_surface=None): """ scale2x(Surface, DestSurface = None) -> Surface specialized image doubler """ c_surf = surface._c_surface if dest_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)
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 tostring(surface, format, flipped=False): """ tostring(Surface, format, flipped=False) -> string transfer image to string buffer """ surf = surface._c_surface if surf.flags & sdl.SDL_OPENGL: raise NotImplementedError() if format == "P": if surf.format.BytesPerPixel != 1: raise ValueError("Can only create \"P\" format data with " "8bit Surfaces") with locked(surf):