Esempio n. 1
0
 def unmap_rgb(self, mapped_int):
     """ unmap_rgb(mapped_int) -> Color
     convert a mapped integer color value into a Color
     """
     self.check_surface()
     mapped_int = ffi.cast('uint32_t', mapped_int)
     r, g, b, a = [ffi.new('uint8_t*') for i in range(4)]
     sdl.SDL_GetRGBA(mapped_int, self._format, r, g, b, a)
     return Color(r[0], g[0], b[0], a[0])
Esempio n. 2
0
 def check_alpha(x, y):
     value = self._get_at(x, y)
     sdl.SDL_GetRGBA(value, format, r, g, b, a)
     if (keyr is None and a[0] >= min_alpha) or \
        (keyr is not None and (r[0] != keyr or
                               g[0] != keyg or
                               b[0] != keyb)):
         return True
     return False
Esempio n. 3
0
 def unmap_rgb(self, mapped_int):
     """ unmap_rgb(mapped_int) -> Color
     convert a mapped integer color value into a Color
     """
     if not self._c_surface:
         raise SDLError("display Surface quit")
     mapped_int = ffi.cast('uint32_t', mapped_int)
     r, g, b, a = [ffi.new('uint8_t*') for i in range(4)]
     sdl.SDL_GetRGBA(mapped_int, self._format, r, g, b, a)
     return Color(r[0], g[0], b[0], a[0])
Esempio n. 4
0
    def get_colorkey(self):
        self.check_opengl()

        if not self._c_surface.flags & sdl.SDL_SRCCOLORKEY:
            return None
        r = ffi.new("uint8_t[1]")
        g = ffi.new("uint8_t[1]")
        b = ffi.new("uint8_t[1]")
        a = ffi.new("uint8_t[1]")
        sdl.SDL_GetRGBA(self._format.colorkey, self._format, r, g, b, a)
        return (r[0], g[0], b[0], a[0])
Esempio n. 5
0
def from_surface(surf, threshold=127):
    """from_surface(surf, threshold = 127) -> Mask

       Returns a Mask from the given surface"""
    c_surf = surf._c_surface
    output_mask = Mask((surf._w, surf._h))
    # colorkey will be None if we're not using a colorkey
    colorkey = surf.get_colorkey()
    format = surf._c_surface.format
    r, g, b, a = (ffi.new('uint8_t *'), ffi.new('uint8_t *'),
                  ffi.new('uint8_t *'), ffi.new('uint8_t *'))
    with locked(c_surf):
        for y in range(surf._h):
            for x in range(surf._w):
                sdl.SDL_GetRGBA(surf._get_at(x, y), format, r, g, b, a)
                if colorkey is None:
                    # check alpha
                    if a[0] > threshold:
                        sdl.bitmask_setbit(output_mask._mask, x, y)
                else:
                    pixel = (r[0], g[0], b[0], a[0])
                    if pixel == colorkey:
                        sdl.bitmask_setbit(output_mask._mask, x, y)
    return output_mask
Esempio n. 6
0
def uncreate_color(c_color, color_format):
    r, g, b, a = [ffi.new("uint8_t*") for _ in range(4)]
    sdl.SDL_GetRGBA(c_color, color_format, r, g, b, a)
    return Color(r[0], g[0], b[0], a[0])
Esempio n. 7
0
    def get_bounding_rect(self, min_alpha=1):
        """ get_bounding_rect(min_alpha = 1) -> Rect
        find the smallest rect containing data
        """
        self.check_surface()

        min_alpha = int(min_alpha)
        if min_alpha > 255:
            min_alpha = 255
        elif min_alpha < 0:
            min_alpha = 0

        r, g, b, a = (ffi.new('uint8_t *'), ffi.new('uint8_t *'),
                      ffi.new('uint8_t *'), ffi.new('uint8_t *'))
        format = self._c_surface.format

        if self._c_surface.flags & sdl.SDL_SRCCOLORKEY:
            keyr = ffi.new('uint8_t *')
            keyg = ffi.new('uint8_t *')
            keyb = ffi.new('uint8_t *')
            sdl.SDL_GetRGBA(self._c_surface.format.colorkey, format, keyr,
                            keyg, keyb, a)
            keyr, keyg, keyb = keyr[0], keyg[0], keyb[0]
        else:
            keyr = keyg = keyb = None

        min_x, min_y, max_x, max_y = 0, 0, self._w, self._h

        def check_alpha(x, y):
            value = self._get_at(x, y)
            sdl.SDL_GetRGBA(value, format, r, g, b, a)
            if (keyr is None and a[0] >= min_alpha) or \
               (keyr is not None and (r[0] != keyr or
                                      g[0] != keyg or
                                      b[0] != keyb)):
                return True
            return False

        with locked(self._c_surface):
            found_alpha = False
            for y in range(max_y - 1, -1, -1):
                for x in range(min_x, max_x):
                    found_alpha = check_alpha(x, y)
                    if found_alpha:
                        break
                if found_alpha:
                    break
                max_y = y

            found_alpha = False
            for x in range(max_x - 1, -1, -1):
                for y in range(min_y, max_y):
                    found_alpha = check_alpha(x, y)
                    if found_alpha:
                        break
                if found_alpha:
                    break
                max_x = x

            found_alpha = False
            for y in range(min_y, max_y):
                min_y = y
                for x in range(min_x, max_x):
                    found_alpha = check_alpha(x, y)
                    if found_alpha:
                        break
                if found_alpha:
                    break

            found_alpha = False
            for x in range(min_x, max_x):
                min_x = x
                for y in range(min_y, max_y):
                    found_alpha = check_alpha(x, y)
                    if found_alpha:
                        break
                if found_alpha:
                    break

        return Rect._from4(min_x, min_y, max_x - min_x, max_y - min_y)