Ejemplo n.º 1
0
    def __convert_PIL_to_SDL(self, image):
        # Copied from sdl2.ext.image.load_image()
        # Thanks to Marcus von. Appen!
        endian = sdl2.endian
        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 endian.SDL_BYTEORDER == endian.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 endian.SDL_BYTEORDER == endian.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.tobytes()
        surface = sdl2.SDL_CreateRGBSurfaceFrom(pxbuf, width, height, depth,
                                                pitch, rmask, gmask, bmask,
                                                amask)
        return sdl2.SDL_CreateTextureFromSurface(self._renderer, surface)
Ejemplo n.º 2
0
    def write_snapshot(self, filename):
        """Take a snapshot of the view and write it as a BMP image.

        Parameters
        ----------
        filename : str
            Destination filename.
        """
        pixel_size = 4
        pixels = self._renderer.read_pixels()
        surface = sdl2.SDL_CreateRGBSurfaceFrom(
            pixels.data, pixels.width, pixels.height,
            8 * pixel_size, pixels.width * pixel_size,
            _RGB_MASKS.red, _RGB_MASKS.green, _RGB_MASKS.blue, 0)
        sdl2.SDL_SaveBMP(surface, filename)
        sdl2.SDL_FreeSurface(surface)
Ejemplo n.º 3
0
def image_to_surface(image):
    y_size, x_size, channels = image.shape
    assert channels == N_RGBA

    data = numpy.round(image * 255.).astype(numpy.uint8).tobytes()
    surface = sdl2.SDL_CreateRGBSurfaceFrom(
        data,
        x_size,  # width
        y_size,  # height
        32,  # depth
        x_size * 4,  # pitch
        0x000000ff,  # rmask
        0x0000ff00,  # gmask
        0x00ff0000,  # gmask
        0xff000000,  # amask
    )
    assert surface
    return surface, data
Ejemplo n.º 4
0
def set_window_icon(icon: PathLike, bytes_per_pixel: int = 4):
    """
    Lets you set a custom window icon for pyxel. you can call this after pyxel.init
    """

    if not has_pil:
        RuntimeWarning('You need Pillow/PIL in order to use pyxelext.system.set_window_icon')
        return

    current_window = sdl2.SDL_GL_GetCurrentWindow()
    img = Image.open(icon)
    x, y = img.size
    rmask = 0x000000ff
    gmask = 0x0000ff00
    bmask = 0x00ff0000
    amask = 0 if bytes_per_pixel == 3 else 0xff000000
    icon_surface = sdl2.SDL_CreateRGBSurfaceFrom(img.tobytes(), x, y, bytes_per_pixel*8, bytes_per_pixel*x, rmask, gmask, bmask, amask)

    sdl2.SDL_SetWindowIcon(current_window, icon_surface)
    sdl2.SDL_FreeSurface(icon_surface)
Ejemplo n.º 5
0
def defaultfont(forergb, backrgb, cwidth=8, cheight=16):
    '''
    Given 32-bit foreground and background colors, create a SDL_Surface
    containing a default character font with 256 character cells of size
    8x16, arranged vertically (so entire surface size is 8x4096).
    Return a pointer to this SDL_Surface and a data array which must not be
    freed until the surface is freed.
    '''
    pixdata = (ctypes.c_ulong * int(1024 * cwidth * cheight))()
    charset = sdl2.SDL_CreateRGBSurfaceFrom(ctypes.byref(pixdata),
                                            cwidth, cheight << 8,
                                            32,
                                            cwidth << 2,
                                            0xff0000,
                                            0xff00,
                                            0xff,
                                            0x0)
    charsrc = array.array('B')
    charsrc.fromstring(zlib.decompress(
        'x\x9cuW\xcfk\x1bG\x14^\xc5\xb0\xa1 d\xfb\xb6\x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\xfa$\xd7\xfa4^\xebSt\xb6\xbc\xd1(\xcf\xbf*\xc7WZ\x06\xed\xaf\x8a\xcf\x1a\x7f\xb0\x96\x9ffi\xbc\x9e\x8f\xb2U\xceoy\xbd\xf4|kWx\xf2O\xe1\xa6\x84\xcd\x12\xeca\x84\xf6|6\x9b\xcdi?w`k\xcf2\xd8\xbe\x99\xe4\xd0)G\x12J\x1d\xaf\xff\x08E9\xf4\xb4\xe2|\x02@\xf3\x04\xb4\x14U\xa7\xab\x9a\xa0=\xb7r\xd3\x85\x8e&\x9a\x16qj\xcf\xea>.\x0e+4w-\xf9>\\gO\xc4\x81Z\xd8\xbb\xbc\xbc,Z\xcc\x86\x0b|\xd9U\xe6\xf6\xfc\x18\xdb{\xec\xab\xf3Y_\x00u<\xea!\xd0\xbf\x17*It}\xd6M\x8c\xae\xdb\x8a\xd7vw\xbdu\x98\xf1\xc3d]\xf3\xfe\x8eMR\x9f\xca\xa2\x1f\xaea\xfd\xdf\x1b\xf5\xb2)\xeevo\x19\x8bd<3s)\x00\xad\x99\x1f\xffe\x97O*\xb1n\x05\xf0?\x93\x8d\xb5\xab'
        ))
    for y in xrange(4096):
        #offset = y << 3
        offset = ((y >> 4) * cheight + (y & 15)) * cwidth
        #print y, offset
        pixdata[offset    ] = forergb if charsrc[y] & 128 else backrgb
        pixdata[offset + 1] = forergb if charsrc[y] &  64 else backrgb
        pixdata[offset + 2] = forergb if charsrc[y] &  32 else backrgb
        pixdata[offset + 3] = forergb if charsrc[y] &  16 else backrgb
        pixdata[offset + 4] = forergb if charsrc[y] &   8 else backrgb
        pixdata[offset + 5] = forergb if charsrc[y] &   4 else backrgb
        pixdata[offset + 6] = forergb if charsrc[y] &   2 else backrgb
        pixdata[offset + 7] = forergb if charsrc[y] &   1 else backrgb
        for oo in xrange(offset + 8, offset + cwidth):
            pixdata[oo] = pixdata[offset + 7]
        if (y & 15) == 15:
            for oo in xrange(((y >> 4) * cheight + 16) * cwidth,
                             (((y >> 4) + 1) * cheight) * cwidth):
                pixdata[oo] = pixdata[oo - cwidth]
    return charset, pixdata
Ejemplo n.º 6
0
    def OnPaint(self, element_type, paint_buffer, **_):
        """
        Using the pixel data from CEF's offscreen rendering
        the data is converted by PIL into a SDL2 surface
        which can then be rendered as a SDL2 texture.
        """
        if element_type == cef.PET_VIEW:
            image = Image.frombuffer(
                'RGBA',
                (self.__width, self.__height),
                paint_buffer.GetString(mode="rgba", origin="top-left"),
                'raw',
                'BGRA'
            )
            # Following PIL to SDL2 surface code from pysdl2 source.
            mode = image.mode
            rmask = gmask = bmask = amask = 0
            depth = None
            pitch = None
            if mode == "RGB":
                # 3x8-bit, 24bpp
                if sdl2.endian.SDL_BYTEORDER == sdl2.endian.SDL_LIL_ENDIAN:
                    rmask = 0x0000FF
                    gmask = 0x00FF00
                    bmask = 0xFF0000
                else:
                    rmask = 0xFF0000
                    gmask = 0x00FF00
                    bmask = 0x0000FF
                depth = 24
                pitch = self.__width * 3
            elif mode in ("RGBA", "RGBX"):
                # RGBX: 4x8-bit, no alpha
                # RGBA: 4x8-bit, alpha
                if sdl2.endian.SDL_BYTEORDER == sdl2.endian.SDL_LIL_ENDIAN:
                    rmask = 0x00000000
                    gmask = 0x0000FF00
                    bmask = 0x00FF0000
                    if mode == "RGBA":
                        amask = 0xFF000000
                else:
                    rmask = 0xFF000000
                    gmask = 0x00FF0000
                    bmask = 0x0000FF00
                    if mode == "RGBA":
                        amask = 0x000000FF
                depth = 32
                pitch = self.__width * 4
            else:
                logging.error("ERROR: Unsupported mode: %s" % mode)
                exit_app()

            pxbuf = image.tobytes()
            # Create surface
            surface = sdl2.SDL_CreateRGBSurfaceFrom(
                pxbuf,
                self.__width,
                self.__height,
                depth,
                pitch,
                rmask,
                gmask,
                bmask,
                amask
            )
            if self.texture:
                # free memory used by previous texture
                sdl2.SDL_DestroyTexture(self.texture)
            # Create texture
            self.texture = sdl2.SDL_CreateTextureFromSurface(self.__renderer,
                                                             surface)
            # Free the surface
            sdl2.SDL_FreeSurface(surface)
        else:
            logging.warning("Unsupport element_type in OnPaint")
Ejemplo n.º 7
0
            # test only
            if options.copyPacket:
                pkt2 = copy.copy(pkt) 
            else:
                pkt2 = pkt

            pkt2.decode()
            if pkt2.decoded:

                decodedCount += 1
                print('decoded frame %d' % decodedCount)

                if decodedCount >= options.offset:
                    print('saving frame...')
                    
                    buf = pkt2.swsFrame.contents.data[0]
                    
                    # convert buffer to a SDL Surface
                    surface = sdl2.SDL_CreateRGBSurfaceFrom(buf, 
                            size[0], size[1], 24, 
                            size[0] * 3,
                            rmask, gmask,
                            bmask, amask)
                    # save then free surface
                    sdl2.SDL_SaveBMP(surface, 'frame.%d.bmp' % decodedCount)
                    sdl2.SDL_FreeSurface(surface)

                if decodedCount >= options.offset + options.frameCount:
                    break 

Ejemplo n.º 8
0
def loadfont(infilename=None, sets=None):
    global screen
    global loadedfontpack
    if infilename is False and loadedfontpack is not None:
        fdata = loadedfontpack
    else:
        if not isinstance(infilename, (str, unicode)):
            infilename = prefs['font.file']
        with open(infilename, 'rb') as f:
            fdata = f.read(32)
            #insert newer version read blocks here
            if fdata.startswith('SRCF binary v1860\r\n'):
                fdata = fdata[19:] + f.read()
                try:
                    fdata = zlib.decompress(fdata)
                except zlib.error:
                    print 'Decompression error reading {:s}'.format(infilename)
                    return
                try:
                    fdata = pickle.loads(fdata)
                except Exception:
                    print 'Error interpreting {:s}'.format(infilename)
                    return
                #perform transformation to newer version srcf data here
            else:
                print 'Signature check failed reading {:s}'.format(infilename)
                return
        #assume fdata is in up-to-data srcf format
        loadedfontpack = fdata
    rootdict, basesets, extsets, gpages = fdata
    del fdata
    #print rootdict
    if sets is None or sets == '(default)':
        sets = rootdict[0][6]
        if isinstance(sets, unicode):
            try:
                sets = rootdict[0][5][sets]
            except KeyError:
                for k in rootdict[0][5]:
                    sets = rootdict[0][5][k]
                    break
    elif isinstance(sets, (str, unicode)):
        sets = unicode(sets).split(u' ')
    #populate data with base set
    baseset = sets[0]
    sets = sets[1:]
    try:
        baseset = basesets[baseset]
    except KeyError:
        print 'First element in charset selection is not a baseset'
        return
    cflags = baseset[0][4]
    cucp = baseset[0][3]
    if baseset[0][5] not in gpages:
        print 'Missing graphics set from font file'
        return
    gsrc = [baseset[0][5]] * 256
    #add in selected extension sets
    #TODO
    #save codepoint table
    screen.encoding = [unichr(c) for c in cucp]
    #begin drawing characters
    fcw = rootdict[0][3]
    fch = rootdict[0][4]
    pagesize = 1024 * fcw * fch
    #allocate font memory area and create surface objects if necessary
    freesurf = []
    freedsurfrepr = set()
    for a in cellcolors:
        if a not in fontmemory:
            fontmemory[a] = (ctypes.c_ulong * pagesize)()
            if screen.charsurf[a] is not None:
                freesurf.append(screen.charsurf[a])
            screen.charsurf[a] = sdl2.SDL_CreateRGBSurfaceFrom(
                ctypes.byref(fontmemory[a]), fcw, fch << 8, 32, fcw << 2,
                0xff0000, 0xff00, 0xff, 0x0)
    for s in freesurf:
        r = repr(s)
        if r in freedsurfrepr:
            continue
        sdl2.SDL_FreeSurface(s)
        freedsurfrepr.add(r)
    del freesurf
    del freedsurfrepr
    for a in xrange(256):
        if a not in cellcolors:
            screen.charsurf[a] = screen.charsurf[attalias[a]]
    if prefs['font.subpixel'] == 'rgb':
        offsetr = 0
        offsetg = 1
        offsetb = 2
    elif prefs['font.subpixel'] == 'bgr':
        offsetr = 2
        offsetg = 1
        offsetb = 0
    else:
        offsetr = 1
        offsetg = 1
        offsetb = 1
    for c in xrange(256):
        colorclass = cflags[c] & 7
        gp = gpages[gsrc[c]][c]
        for a in cellcolors:
            try:
                backcolor = ibmcolors[cellcolors[a][0]]
            except IndexError:
                backcolor = 0x00
            try:
                forecolor = ibmcolors[cellcolors[a][1][colorclass]]
            except IndexError:
                forecolor = 0x2a
            backr = (backcolor & 0x30) >> 4
            backg = (backcolor & 0xc) >> 2
            backb = backcolor & 0x3
            forer = (forecolor & 0x30) >> 4
            foreg = (forecolor & 0xc) >> 2
            foreb = forecolor & 0x3
            aptr = 0
            mptr = fcw * fch * c
            for y in xrange(fch):
                for x in xrange(fcw):
                    try:
                        r = colormix[backr][forer][gp[aptr + offsetr]]
                        g = colormix[backg][foreg][gp[aptr + offsetg]]
                        b = colormix[backb][foreb][gp[aptr + offsetb]]
                    except IndexError:
                        r = colormix[backr][forer][9]
                        g = colormix[backg][foreg][9]
                        b = colormix[backb][foreb][9]
                    fontmemory[a][mptr] = r << 16 | g << 8 | b
                    aptr += 3
                    mptr += 1
        yield None
    screen.cellwidth = fcw
    screen.cellheight = fch
    screen.refresh()