Пример #1
0
 def get_raw(self):
     """ get_raw() -> bytes
     return a bytestring copy of the Sound samples.
     """
     check_mixer()
     return ffi.buffer(ffi.cast('char*', self.chunk.abuf),
                       self.chunk.alen)[:]
Пример #2
0
 def get_raw(self):
     """ get_raw() -> bytes
     return a bytestring copy of the Sound samples.
     """
     check_mixer()
     return ffi.buffer(ffi.cast('char*', self.chunk.abuf),
                       self.chunk.alen)[:]
Пример #3
0
def obj_write(context, input, size, maxnum):
    fileobj = ffi.from_handle(context.hidden.unknown.data1)
    if not hasattr(fileobj, 'write'):
        return -1
    data = ffi.buffer(input, size * maxnum)
    try:
        fileobj.write(data)
    except IOError:
        return -1
    return size * maxnum
Пример #4
0
def obj_write(context, input, size, maxnum):
    fileobj = ffi.from_handle(context.hidden.unknown.data1)
    if not hasattr(fileobj, 'write'):
        return -1
    data = ffi.buffer(input, size*maxnum)
    try:
        fileobj.write(data)
    except IOError:
        return -1
    return size*maxnum
Пример #5
0
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):
            string = ffi.buffer(ffi.cast('char*', surf.pixels))[:]
    else:
        _tostring = globals().get('_tostring_%s' % format, None)
        if _tostring is None:
            raise ValueError("Unrecognized type of format")
        with locked(surf):
            string = _tostring(surf, flipped)
    return string
Пример #6
0
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):
            string = ffi.buffer(ffi.cast('char*', surf.pixels))[:]
    else:
        _tostring = globals().get('_tostring_%s' % format, None)
        if _tostring is None:
            raise ValueError("Unrecognized type of format")
        with locked(surf):
            string = _tostring(surf, flipped)
    return string
Пример #7
0
def _tostring_RGB(surf, flipped):
    rmask, gmask, bmask, amask = (surf.format.Rmask,
                                  surf.format.Gmask,
                                  surf.format.Bmask,
                                  surf.format.Amask)
    rshift, gshift, bshift, ashift = (surf.format.Rshift,
                                      surf.format.Gshift,
                                      surf.format.Bshift,
                                      surf.format.Ashift)
    rloss, gloss, bloss, aloss = (surf.format.Rloss,
                                  surf.format.Gloss,
                                  surf.format.Bloss,
                                  surf.format.Aloss)
    bpp = surf.format.BytesPerPixel
    h, w = surf.h, surf.w

    data = ffi.new('char[]', w * h * 3)
    if bpp == 1:
        pixels = ffi.cast('uint8_t*', surf.pixels)
        colors = surf.format.palette.colors
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 3 * (y * w + x)
                color = pixels[src_start + x]
                data[dest] = chr_(colors[color].r)
                data[dest + 1] = chr_(colors[color].g)
                data[dest + 2] = chr_(colors[color].b)
    elif bpp == 2:
        pixels = ffi.cast('uint16_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 3 * (y * w + x)
                color = pixels[src_start + x]
                data[dest] = chr_(((color & rmask) >> rshift) << rloss)
                data[dest + 1] = chr_(((color & gmask) >> gshift) << gloss)
                data[dest + 2] = chr_(((color & bmask) >> bshift) << bloss)
    elif bpp == 3:
        pixels = ffi.cast('uint8_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * surf.pitch if flipped \
                        else y * surf.pitch
            for x in range(w):
                dest = 3 * (y * w + x)
                color = (pixels[src_start + x * 3 + BYTE0] +
                         (pixels[src_start + x * 3 + BYTE1] << 8) +
                         (pixels[src_start + x * 3 + BYTE2] << 16))
                data[dest] = chr_(((color & rmask) >> rshift) << rloss)
                data[dest + 1] = chr_(((color & gmask) >> gshift) << gloss)
                data[dest + 2] = chr_(((color & bmask) >> bshift) << bloss)
    elif bpp == 4:
        pixels = ffi.cast('uint32_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 3 * (y * w + x)
                color = pixels[src_start + x]
                data[dest] = chr_(((color & rmask) >> rshift) << rloss)
                data[dest + 1] = chr_(((color & gmask) >> gshift) << gloss)
                data[dest + 2] = chr_(((color & bmask) >> bshift) << bloss)
    else:
        raise ValueError("invalid color depth")
    return ffi.buffer(data)[:]
Пример #8
0
def _tostring_RGBA_PREMULT(surf, flipped, argb=False):
    if surf.format.BytesPerPixel == 1 or surf.format.Amask == 0:
        raise ValueError("Can only create pre-multiplied alpha strings if "
                         "the surface has per-pixel alpha")

    rmask, gmask, bmask, amask = (surf.format.Rmask,
                                  surf.format.Gmask,
                                  surf.format.Bmask,
                                  surf.format.Amask)
    rshift, gshift, bshift, ashift = (surf.format.Rshift,
                                      surf.format.Gshift,
                                      surf.format.Bshift,
                                      surf.format.Ashift)
    rloss, gloss, bloss, aloss = (surf.format.Rloss,
                                  surf.format.Gloss,
                                  surf.format.Bloss,
                                  surf.format.Aloss)
    bpp = surf.format.BytesPerPixel
    h, w = surf.h, surf.w
    if argb:
        ri, gi, bi, ai = 1, 2, 3, 0
    else:
        ri, gi, bi, ai = 0, 1, 2, 3

    data = ffi.new('char[]', w * h * 4)
    if bpp == 2:
        pixels = ffi.cast('uint16_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 4 * (y * w + x)
                color = pixels[src_start + x]
                alpha = ((color & amask) >> ashift) << aloss
                data[dest + ri] = chr_((((color & rmask) >> rshift) << rloss)
                                       * alpha // 255)
                data[dest + gi] = chr_((((color & gmask) >> gshift) << gloss)
                                       * alpha // 255)
                data[dest + bi] = chr_((((color & bmask) >> bshift) << bloss)
                                       * alpha // 255)
                data[dest + ai] = chr_(alpha)
    elif bpp == 3:
        pixels = ffi.cast('uint8_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * surf.pitch if flipped \
                        else y * surf.pitch
            for x in range(w):
                dest = 4 * (y * w + x)
                color = (pixels[src_start + x * 3 + BYTE0] +
                         (pixels[src_start + x * 3 + BYTE1] << 8) +
                         (pixels[src_start + x * 3 + BYTE2] << 16))
                alpha = ((color & amask) >> ashift) << aloss
                data[dest + ri] = chr_((((color & rmask) >> rshift) << rloss)
                                       * alpha // 255)
                data[dest + gi] = chr_((((color & gmask) >> gshift) << gloss)
                                       * alpha // 255)
                data[dest + bi] = chr_((((color & bmask) >> bshift) << bloss)
                                       * alpha // 255)
                data[dest + ai] = chr_(alpha)
    elif bpp == 4:
        pixels = ffi.cast('uint32_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 4 * (y * w + x)
                color = pixels[src_start + x]
                alpha = ((color & amask) >> ashift) << aloss
                if alpha == 0:
                    data[dest + ri] = data[dest + gi] = data[dest + bi] \
                        = chr_(0)
                else:
                    data[dest + ri] = chr_(
                        (((color & rmask) >> rshift) << rloss) * alpha // 255)
                    data[dest + gi] = chr_(
                        (((color & gmask) >> gshift) << gloss) * alpha // 255)
                    data[dest + bi] = chr_(
                        (((color & bmask) >> bshift) << bloss) * alpha // 255)
                data[dest + ai] = chr_(alpha)
    else:
        raise ValueError("invalid color depth")
    return ffi.buffer(data)[:]
Пример #9
0
def _tostring_RGBA(surf, flipped, has_colorkey=True, argb=False):
    rmask, gmask, bmask, amask = (surf.format.Rmask,
                                  surf.format.Gmask,
                                  surf.format.Bmask,
                                  surf.format.Amask)
    rshift, gshift, bshift, ashift = (surf.format.Rshift,
                                      surf.format.Gshift,
                                      surf.format.Bshift,
                                      surf.format.Ashift)
    rloss, gloss, bloss, aloss = (surf.format.Rloss,
                                  surf.format.Gloss,
                                  surf.format.Bloss,
                                  surf.format.Aloss)
    bpp = surf.format.BytesPerPixel
    h, w = surf.h, surf.w
    colorkey = surf.format.colorkey
    if argb:
        has_colorkey = False
        ri, gi, bi, ai = 1, 2, 3, 0
    else:
        has_colorkey = (has_colorkey and surf.flags & sdl.SDL_SRCCOLORKEY
                        and not amask)
        ri, gi, bi, ai = 0, 1, 2, 3

    data = ffi.new('char[]', w * h * 4)
    if bpp == 1:
        pixels = ffi.cast('uint8_t*', surf.pixels)
        colors = surf.format.palette.colors
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 4 * (y * w + x)
                color = pixels[src_start + x]
                data[dest + ri] = chr_(colors[color].r)
                data[dest + gi] = chr_(colors[color].g)
                data[dest + bi] = chr_(colors[color].b)
                if has_colorkey:
                    data[dest + ai] = chr_(
                        ffi.cast('char', color != colorkey) * 255)
                else:
                    data[dest + ai] = chr_(255)
    elif bpp == 2:
        pixels = ffi.cast('uint16_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 4 * (y * w + x)
                color = pixels[src_start + x]
                data[dest + ri] = chr_(((color & rmask) >> rshift) << rloss)
                data[dest + gi] = chr_(((color & gmask) >> gshift) << gloss)
                data[dest + bi] = chr_(((color & bmask) >> bshift) << bloss)
                if has_colorkey:
                    data[dest + ai] = chr_(
                        ffi.cast('char', color != colorkey) * 255)
                else:
                    data[dest + ai] = chr_(
                        (((color & amask) >> ashift) << aloss)
                        if amask else 255)
    elif bpp == 3:
        pixels = ffi.cast('uint8_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * surf.pitch if flipped \
                        else y * surf.pitch
            for x in range(w):
                dest = 4 * (y * w + x)
                color = (pixels[src_start + x * 3 + BYTE0] +
                         (pixels[src_start + x * 3 + BYTE1] << 8) +
                         (pixels[src_start + x * 3 + BYTE2] << 16))
                data[dest + ri] = chr_(((color & rmask) >> rshift) << rloss)
                data[dest + gi] = chr_(((color & gmask) >> gshift) << gloss)
                data[dest + bi] = chr_(((color & bmask) >> bshift) << bloss)
                if has_colorkey:
                    data[dest + ai] = chr_(
                        ffi.cast('char', color != colorkey) * 255)
                else:
                    data[dest + ai] = chr_(
                        (((color & amask) >> ashift) << aloss)
                        if amask else 255)
    elif bpp == 4:
        pixels = ffi.cast('uint32_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 4 * (y * w + x)
                color = pixels[src_start + x]
                data[dest + ri] = chr_(((color & rmask) >> rshift) << rloss)
                data[dest + gi] = chr_(((color & gmask) >> gshift) << gloss)
                data[dest + bi] = chr_(((color & bmask) >> bshift) << bloss)
                if has_colorkey:
                    data[dest + ai] = chr_(
                        ffi.cast('char', color != colorkey) * 255)
                else:
                    data[dest + ai] = chr_(
                        (((color & amask) >> ashift) << aloss)
                        if amask else 255)
    else:
        raise ValueError("invalid color depth")
    return ffi.buffer(data)[:]
Пример #10
0
def _tostring_RGB(surf, flipped):
    rmask, gmask, bmask, amask = (surf.format.Rmask, surf.format.Gmask,
                                  surf.format.Bmask, surf.format.Amask)
    rshift, gshift, bshift, ashift = (surf.format.Rshift, surf.format.Gshift,
                                      surf.format.Bshift, surf.format.Ashift)
    rloss, gloss, bloss, aloss = (surf.format.Rloss, surf.format.Gloss,
                                  surf.format.Bloss, surf.format.Aloss)
    bpp = surf.format.BytesPerPixel
    h, w = surf.h, surf.w

    data = ffi.new('char[]', w * h * 3)
    if bpp == 1:
        pixels = ffi.cast('uint8_t*', surf.pixels)
        colors = surf.format.palette.colors
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 3 * (y * w + x)
                color = pixels[src_start + x]
                data[dest] = chr_(colors[color].r)
                data[dest + 1] = chr_(colors[color].g)
                data[dest + 2] = chr_(colors[color].b)
    elif bpp == 2:
        pixels = ffi.cast('uint16_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 3 * (y * w + x)
                color = pixels[src_start + x]
                data[dest] = chr_(((color & rmask) >> rshift) << rloss)
                data[dest + 1] = chr_(((color & gmask) >> gshift) << gloss)
                data[dest + 2] = chr_(((color & bmask) >> bshift) << bloss)
    elif bpp == 3:
        pixels = ffi.cast('uint8_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * surf.pitch if flipped \
                        else y * surf.pitch
            for x in range(w):
                dest = 3 * (y * w + x)
                color = (pixels[src_start + x * 3 + BYTE0] +
                         (pixels[src_start + x * 3 + BYTE1] << 8) +
                         (pixels[src_start + x * 3 + BYTE2] << 16))
                data[dest] = chr_(((color & rmask) >> rshift) << rloss)
                data[dest + 1] = chr_(((color & gmask) >> gshift) << gloss)
                data[dest + 2] = chr_(((color & bmask) >> bshift) << bloss)
    elif bpp == 4:
        pixels = ffi.cast('uint32_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 3 * (y * w + x)
                color = pixels[src_start + x]
                data[dest] = chr_(((color & rmask) >> rshift) << rloss)
                data[dest + 1] = chr_(((color & gmask) >> gshift) << gloss)
                data[dest + 2] = chr_(((color & bmask) >> bshift) << bloss)
    else:
        raise ValueError("invalid color depth")
    return ffi.buffer(data)[:]
Пример #11
0
def _tostring_RGBA_PREMULT(surf, flipped, argb=False):
    if surf.format.BytesPerPixel == 1 or surf.format.Amask == 0:
        raise ValueError("Can only create pre-multiplied alpha strings if "
                         "the surface has per-pixel alpha")

    rmask, gmask, bmask, amask = (surf.format.Rmask, surf.format.Gmask,
                                  surf.format.Bmask, surf.format.Amask)
    rshift, gshift, bshift, ashift = (surf.format.Rshift, surf.format.Gshift,
                                      surf.format.Bshift, surf.format.Ashift)
    rloss, gloss, bloss, aloss = (surf.format.Rloss, surf.format.Gloss,
                                  surf.format.Bloss, surf.format.Aloss)
    bpp = surf.format.BytesPerPixel
    h, w = surf.h, surf.w
    if argb:
        ri, gi, bi, ai = 1, 2, 3, 0
    else:
        ri, gi, bi, ai = 0, 1, 2, 3

    data = ffi.new('char[]', w * h * 4)
    if bpp == 2:
        pixels = ffi.cast('uint16_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 4 * (y * w + x)
                color = pixels[src_start + x]
                alpha = ((color & amask) >> ashift) << aloss
                data[dest + ri] = chr_(
                    (((color & rmask) >> rshift) << rloss) * alpha // 255)
                data[dest + gi] = chr_(
                    (((color & gmask) >> gshift) << gloss) * alpha // 255)
                data[dest + bi] = chr_(
                    (((color & bmask) >> bshift) << bloss) * alpha // 255)
                data[dest + ai] = chr_(alpha)
    elif bpp == 3:
        pixels = ffi.cast('uint8_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * surf.pitch if flipped \
                        else y * surf.pitch
            for x in range(w):
                dest = 4 * (y * w + x)
                color = (pixels[src_start + x * 3 + BYTE0] +
                         (pixels[src_start + x * 3 + BYTE1] << 8) +
                         (pixels[src_start + x * 3 + BYTE2] << 16))
                alpha = ((color & amask) >> ashift) << aloss
                data[dest + ri] = chr_(
                    (((color & rmask) >> rshift) << rloss) * alpha // 255)
                data[dest + gi] = chr_(
                    (((color & gmask) >> gshift) << gloss) * alpha // 255)
                data[dest + bi] = chr_(
                    (((color & bmask) >> bshift) << bloss) * alpha // 255)
                data[dest + ai] = chr_(alpha)
    elif bpp == 4:
        pixels = ffi.cast('uint32_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 4 * (y * w + x)
                color = pixels[src_start + x]
                alpha = ((color & amask) >> ashift) << aloss
                if alpha == 0:
                    data[dest + ri] = data[dest + gi] = data[dest + bi] \
                        = chr_(0)
                else:
                    data[dest + ri] = chr_(
                        (((color & rmask) >> rshift) << rloss) * alpha // 255)
                    data[dest + gi] = chr_(
                        (((color & gmask) >> gshift) << gloss) * alpha // 255)
                    data[dest + bi] = chr_(
                        (((color & bmask) >> bshift) << bloss) * alpha // 255)
                data[dest + ai] = chr_(alpha)
    else:
        raise ValueError("invalid color depth")
    return ffi.buffer(data)[:]
Пример #12
0
def _tostring_RGBA(surf, flipped, has_colorkey=True, argb=False):
    rmask, gmask, bmask, amask = (surf.format.Rmask, surf.format.Gmask,
                                  surf.format.Bmask, surf.format.Amask)
    rshift, gshift, bshift, ashift = (surf.format.Rshift, surf.format.Gshift,
                                      surf.format.Bshift, surf.format.Ashift)
    rloss, gloss, bloss, aloss = (surf.format.Rloss, surf.format.Gloss,
                                  surf.format.Bloss, surf.format.Aloss)
    bpp = surf.format.BytesPerPixel
    h, w = surf.h, surf.w
    colorkey = surf.format.colorkey
    if argb:
        has_colorkey = False
        ri, gi, bi, ai = 1, 2, 3, 0
    else:
        has_colorkey = (has_colorkey and surf.flags & sdl.SDL_SRCCOLORKEY
                        and not amask)
        ri, gi, bi, ai = 0, 1, 2, 3

    data = ffi.new('char[]', w * h * 4)
    if bpp == 1:
        pixels = ffi.cast('uint8_t*', surf.pixels)
        colors = surf.format.palette.colors
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 4 * (y * w + x)
                color = pixels[src_start + x]
                data[dest + ri] = chr_(colors[color].r)
                data[dest + gi] = chr_(colors[color].g)
                data[dest + bi] = chr_(colors[color].b)
                if has_colorkey:
                    data[dest + ai] = chr_(
                        ffi.cast('char', color != colorkey) * 255)
                else:
                    data[dest + ai] = chr_(255)
    elif bpp == 2:
        pixels = ffi.cast('uint16_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 4 * (y * w + x)
                color = pixels[src_start + x]
                data[dest + ri] = chr_(((color & rmask) >> rshift) << rloss)
                data[dest + gi] = chr_(((color & gmask) >> gshift) << gloss)
                data[dest + bi] = chr_(((color & bmask) >> bshift) << bloss)
                if has_colorkey:
                    data[dest + ai] = chr_(
                        ffi.cast('char', color != colorkey) * 255)
                else:
                    data[dest + ai] = chr_(((
                        (color & amask) >> ashift) << aloss) if amask else 255)
    elif bpp == 3:
        pixels = ffi.cast('uint8_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * surf.pitch if flipped \
                        else y * surf.pitch
            for x in range(w):
                dest = 4 * (y * w + x)
                color = (pixels[src_start + x * 3 + BYTE0] +
                         (pixels[src_start + x * 3 + BYTE1] << 8) +
                         (pixels[src_start + x * 3 + BYTE2] << 16))
                data[dest + ri] = chr_(((color & rmask) >> rshift) << rloss)
                data[dest + gi] = chr_(((color & gmask) >> gshift) << gloss)
                data[dest + bi] = chr_(((color & bmask) >> bshift) << bloss)
                if has_colorkey:
                    data[dest + ai] = chr_(
                        ffi.cast('char', color != colorkey) * 255)
                else:
                    data[dest + ai] = chr_(((
                        (color & amask) >> ashift) << aloss) if amask else 255)
    elif bpp == 4:
        pixels = ffi.cast('uint32_t*', surf.pixels)
        for y in range(h):
            src_start = (h - 1 - y) * w if flipped \
                        else y * w
            for x in range(w):
                dest = 4 * (y * w + x)
                color = pixels[src_start + x]
                data[dest + ri] = chr_(((color & rmask) >> rshift) << rloss)
                data[dest + gi] = chr_(((color & gmask) >> gshift) << gloss)
                data[dest + bi] = chr_(((color & bmask) >> bshift) << bloss)
                if has_colorkey:
                    data[dest + ai] = chr_(
                        ffi.cast('char', color != colorkey) * 255)
                else:
                    data[dest + ai] = chr_(((
                        (color & amask) >> ashift) << aloss) if amask else 255)
    else:
        raise ValueError("invalid color depth")
    return ffi.buffer(data)[:]