def __init__(self, w = None, h = None, data_ptr = None, surface = None, filename = None, filedata = None): self.do_free = True if filedata is not None: if type(filedata) is bytes: imagedata = bytearray(filedata) if type(filedata) is bytearray: rwops = sdlrwops.rw_from_mem(bar2voidp(filedata).value, len(filedata)) elif hasattr(filedata, "seek"): rwops = sdlrwops.rw_from_object(filedata) else: raise TypeError("unusable data type {}", str(type(filedata))) self._surf = sdlimage.load_rw(rwops, 1) elif isinstance(filename, str): self._surf = sdlimage.load(filename) elif isinstance(w, int) and isinstance(h, int): masks = list(sdlpixels.pixelformat_enum_to_masks(self.sdl_pixel_fmt)) bpp = masks.pop(0) if data_ptr is None: self._surf = sdlsurface.create_rgb_surface(w, h, bpp, *masks) else: # data_ptr == ABGR8888 self._surf = sdlsurface.create_rgb_surface_from(data_ptr, w, h, bpp, w*4, *masks) elif isinstance(surface, SDL_Surface): self.do_free = False self._surf = surface else: raise TypeError("Crazy shit in parameters: ({} {} {} {} {})".format(type(w), type(h), type(data_ptr), type(surface), type(filename))) sdlsurface.set_surface_blend_mode(self._surf, sdlvideo.SDL_BLENDMODE_NONE)
def test_load_rw(self): fname = "surfacetest.%s" for fmt in formats: if fmt == "tga": # SDL_image does not support loading TGA via IMG_Load_RW() continue fp = RESOURCES.get(fname % fmt) sf = sdlimage.load_rw(rwops.rw_from_object(fp), False) self.assertIsInstance(sf, surface.SDL_Surface) surface.free_surface(sf)