Example #1
0
File: gl.py Project: lxnt/fgtestbed
 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)
Example #2
0
 def test_create_rgb_surface_from(self):
     for buf, bpp, pitch, masks, fmt in rgba_pixelations_16x16:
         arflag = None
         if bpp == 32:
             arflag = "I"
         elif bpp == 16:
             arflag = "H"
         bytebuf = pgarray.CTypesView(array.array(arflag, buf))
         sf = surface.create_rgb_surface_from(bytebuf.to_bytes(), 16, 16,
                                              bpp, pitch, masks[0],
                                              masks[1], masks[2], masks[3])
         self.assertIsInstance(sf, surface.SDL_Surface)
         surface.free_surface(sf)
Example #3
0
def load_image(fname, enforce=None):
    """Creates a SDL_Surface from an image file.

    If assurface is True, a SDL_Surface will be returned instead of a
    Sprite or SoftSprite object. If renderer is set to a SDL_Renderer, a
    Sprite will be returned.

    This function makes use of the Python Imaging Library, if it is available
    on the target execution environment. The function will try to load the
    file via pygame2.sdlimage first. If the file could not be loaded, it will
    try to load it via PIL.

    You can force the function to use only one of them, by passing the enforce
    as either "PIL" or "SDL".

    Note: This will call pygame2.sdlimage.init() implicitly with the
    default arguments, if the module is available.
    """
    if enforce is not None and enforce not in ("PIL", "SDL"):
        raise ValueError("enforce must be either 'PIL' or 'SDL', if set")

    if enforce == "PIL" and not _HASPIL:
        raise UnsupportedError("PIL loading")
    if enforce == "SDL" and not _HASSDLIMAGE:
        raise UnsupportedError("SDL loading")

    surface = None
    if enforce != "PIL" and _HASSDLIMAGE:
        try:
            sdlimage.init()
            surface = sdlimage.load(fname)
        except:
            # An error occured - if we do not try PIL, break out now
            if not _HASPIL or enforce == "SDL":
                raise

    if enforce != "SDL" and _HASPIL and surface is None:
        image = Image.open(fname)
        mode = image.mode
        width, height = image.size
        rmask = gmask = bmask = amask = 0
        if mode in ("1", "L", "P"):
            # 1 = B/W, 1 bit per byte
            # "L" = greyscale, 8-bit
            # "P" = palette-based, 8-bit
            pitch = width
            depth = 8
        elif mode == "RGB":
            # 3x8-bit, 24bpp
            if SDL_BYTEORDER == SDL_LIL_ENDIAN:
                rmask = 0x0000FF
                gmask = 0x00FF00
                bmask = 0xFF0000
            else:
                rmask = 0xFF0000
                gmask = 0x00FF00
                bmask = 0x0000FF
            depth = 24
            pitch = width * 3
        elif mode in ("RGBA", "RGBX"):
            # RGBX: 4x8-bit, no alpha
            # RGBA: 4x8-bit, alpha
            if SDL_BYTEORDER == SDL_LIL_ENDIAN:
                rmask = 0x000000FF
                gmask = 0x0000FF00
                bmask = 0x00FF0000
                if mode == "RGBA":
                    amask = 0xFF000000
            else:
                rmask = 0xFF000000
                gmask = 0x00FF0000
                bmask = 0x0000FF00
                if mode == "RGBA":
                    amask = 0x000000FF
            depth = 32
            pitch = width * 4
        else:
            # We do not support CMYK or YCbCr for now
            raise TypeError("unsupported image format")

        pxbuf = image.tostring()
        surface = sdlsurface.create_rgb_surface_from(pxbuf, width, height,
                                                     depth, pitch, rmask,
                                                     gmask, bmask, amask)

        if mode == "P":
            # Create a SDL_Palette for the SDL_Surface
            def _chunk(seq, size):
                for x in range(0, len(seq), size):
                    yield seq[x:x + size]

            rgbcolors = image.getpalette()
            sdlpalette = sdlpixels.alloc_palette(len(rgbcolors) // 3)
            SDL_Color = sdlpixels.SDL_Color
            for idx, (r, g, b) in enumerate(_chunk(rgbcolors, 3)):
                sdlpalette.colors[idx] = SDL_Color(r, g, b)

            try:
                sdlsurface.set_surface_palette(surface, sdlpalette)
            except:
                sdlsurface.free_surface(surface)
                raise
            finally:
                # This will decrease the refcount on the palette, so it gets
                # freed properly on releasing the SDL_Surface.
                sdlpixels.free_palette(sdlpalette)
    return surface