xpra icon
Bug tracker and wiki

This bug tracker and wiki are being discontinued
please use https://github.com/Xpra-org/xpra instead.


Ticket #463: rgb-no-PIL.patch

File rgb-no-PIL.patch, 8.4 KB (added by Antoine Martin, 7 years ago)

try to convert to RGB without PIL

  • xpra/codecs/argb/argb.pxd

     
    55# later version. See the file COPYING for details.
    66
    77
    8 cdef argbdata_to_pixdata(const unsigned long* data, int len)
     8cdef argbdata_to_pixdata(const unsigned int* data, int len)
  • xpra/codecs/argb/argb.pyx

     
    3030
    3131def argb_to_rgba(buf):
    3232    # b is a Python buffer object
    33     cdef const unsigned long * cbuf = <unsigned long *> 0
     33    cdef const unsigned int * cbuf = <unsigned int *> 0
    3434    cdef Py_ssize_t cbuf_len = 0
    3535    assert sizeof(int) == 4
    3636    assert len(buf) % 4 == 0, "invalid buffer size: %s is not a multiple of 4" % len(buf)
    3737    assert PyObject_AsReadBuffer(buf, <const void**> &cbuf, &cbuf_len)==0
    3838    return argbdata_to_pixdata(cbuf, cbuf_len)
    3939
    40 cdef argbdata_to_pixdata(const unsigned long* data, int dlen):
     40cdef argbdata_to_pixdata(const unsigned int* data, int dlen):
    4141    if dlen <= 0:
    4242        return None
    4343    assert dlen % 4 == 0, "invalid buffer size: %s is not a multiple of 4" % dlen
    4444    b = make_byte_buffer(dlen)
    4545    cdef int offset = 0
    4646    cdef int i = 0
    47     cdef unsigned long rgba
    48     cdef unsigned long argb
     47    cdef unsigned int rgba
     48    cdef unsigned int argb
    4949    cdef unsigned char b1, b2, b3, b4
    5050    while i < dlen/4:
    5151        argb = data[i] & 0xffffffff
    52         rgba = <unsigned long> ((argb << 8) | (argb >> 24)) & 0xffffffff
     52        rgba = <unsigned int> ((argb << 8) | (argb >> 24)) & 0xffffffff
    5353        b1 = (rgba >> 24) & 0xff
    5454        b2 = (rgba >> 16) & 0xff
    5555        b3 = (rgba >> 8) & 0xff
     
    6464
    6565def argb_to_rgb(buf):
    6666    # b is a Python buffer object
    67     cdef unsigned long * cbuf = <unsigned long *> 0     #@DuplicateSignature
     67    cdef unsigned int * cbuf = <unsigned int *> 0     #@DuplicateSignature
    6868    cdef Py_ssize_t cbuf_len = 0                        #@DuplicateSignature
    6969    assert sizeof(int) == 4
    7070    assert len(buf) % 4 == 0, "invalid buffer size: %s is not a multiple of 4" % len(buf)
    7171    assert PyObject_AsReadBuffer(buf, <const void**> &cbuf, &cbuf_len)==0
    7272    return argbdata_to_rgb(cbuf, cbuf_len)
    7373
    74 cdef argbdata_to_rgb(const unsigned long* data, int dlen):
     74cdef argbdata_to_rgb(const unsigned int* data, int dlen):
    7575    if dlen <= 0:
    7676        return None
    7777    assert dlen % 4 == 0, "invalid buffer size: %s is not a multiple of 4" % dlen
    7878    b = make_byte_buffer(dlen*3/4)
    7979    cdef int offset = 0                     #@DuplicateSignature
    8080    cdef int i = 0                          #@DuplicateSignature
    81     cdef unsigned long rgba                 #@DuplicateSignature
    82     cdef unsigned long argb                 #@DuplicateSignature
     81    cdef unsigned int rgba                  #@DuplicateSignature
     82    cdef unsigned int argb                  #@DuplicateSignature
    8383    cdef unsigned char b1, b2, b3           #@DuplicateSignature
    84     while i < dlen/4:
    85         argb = data[i] & 0xffffffff
    86         rgba = <unsigned long> ((argb << 8) | (argb >> 24)) & 0xffffffff
     84    cdef int mi = dlen/4
     85    while i < mi:
     86        argb = data[i]
     87        rgba = <unsigned int> ((argb << 8) | (argb >> 24)) & 0xffffffff
    8788        b1 = (rgba >> 24) & 0xff
    8889        b2 = (rgba >> 16) & 0xff
    8990        b3 = (rgba >> 8) & 0xff
     
    9596    return b
    9697
    9798
     99def bgra_to_rgb(buf):
     100    assert len(buf) % 4 == 0, "invalid buffer size: %s is not a multiple of 4" % len(buf)
     101    # buf is a Python buffer object
     102    cdef unsigned int * bgra_buf = <unsigned int *> 0 #@DuplicateSignature
     103    cdef Py_ssize_t bgra_buf_len = 0                    #@DuplicateSignature
     104    assert sizeof(int) == 4
     105    assert PyObject_AsReadBuffer(buf, <const void**> &bgra_buf, &bgra_buf_len)==0
     106    return bgradata_to_rgb(bgra_buf, bgra_buf_len)
     107
     108cdef bgradata_to_rgb(const unsigned int* data, int dlen):
     109    if dlen <= 0:
     110        return None
     111    assert dlen % 4 == 0, "invalid buffer size: %s is not a multiple of 4" % dlen
     112    buf = make_byte_buffer(dlen*3/4)
     113    cdef int bo = 0                         #@DuplicateSignature
     114    cdef int di = 0                         #@DuplicateSignature
     115    cdef unsigned int bgra                  #@DuplicateSignature
     116    cdef unsigned char r, g, b              #@DuplicateSignature
     117    cdef int mdi = dlen/4
     118    while di < mdi:
     119        bgra = data[di]
     120        b = (bgra >> 24) & 0xff
     121        g = (bgra >> 16) & 0xff
     122        r = (bgra >> 8) & 0xff
     123        buf[bo] = r
     124        buf[bo+1] = g
     125        buf[bo+2] = b
     126        bo = bo + 3
     127        di = di + 1
     128    return buf
     129
     130
    98131def premultiply_argb_in_place(buf):
    99132    # b is a Python buffer object
    100133    cdef unsigned int * cbuf = <unsigned int *> 0
  • xpra/codecs/loader.py

     
    5757        return
    5858    loaded = True
    5959    debug("loading codecs")
    60     codec_import_check("PIL", "Python Imaging Library", "PIL", "PIL", "Image")
    61     add_codec_version("PIL", "PIL.Image", "VERSION")
     60    #codec_import_check("PIL", "Python Imaging Library", "PIL", "PIL", "Image")
     61    #add_codec_version("PIL", "PIL.Image", "VERSION")
    6262
    6363    codec_import_check("enc_vpx", "vpx encoder", "xpra.codecs.vpx", "xpra.codecs.vpx.encoder", "Encoder")
    6464    codec_import_check("dec_vpx", "vpx decoder", "xpra.codecs.vpx", "xpra.codecs.vpx.decoder", "Decoder")
  • xpra/server/window_source.py

     
    4545    from xpra.codecs.xor import xor_str        #@UnresolvedImport
    4646except:
    4747    xor_str = None
     48try:
     49    from xpra.codecs.argb.argb import bgra_to_rgb, argb_to_rgb
     50except:
     51    bgra_to_rgb = None
     52    argb_to_rgb = None
    4853from xpra.os_util import StringIOClass
    4954from xpra.codecs.loader import get_codec, has_codec, NEW_ENCODING_NAMES_TO_OLD
    5055
     
    10651070    def rgb_reformat(self, image):
    10661071        #need to convert to a supported format!
    10671072        pixel_format = image.get_pixel_format()
     1073        pixels = image.get_pixels()
     1074        PIL = get_codec("PIL")
     1075        if not PIL:
     1076            #fallback to argb module if we have the pixel format:
     1077            if bgra_to_rgb and pixel_format in ("BGRX", "BGRA") and "RGB" in self.rgb_formats:
     1078                image.set_pixels(bgra_to_rgb(pixels))
     1079                image.set_pixel_format("RGB")
     1080                return True
     1081            if argb_to_rgb and pixel_format in ("XRGB", "ARGB") and "RGB" in self.rgb_formats:
     1082                image.set_pixels(argb_to_rgb(pixels))
     1083                image.set_pixel_format("RGB")
     1084                return True
     1085            self.warn_encoding_once(pixel_format+"-format-not-handled", "no PIL and no matching argb function, cannot convert %s to one of: %s" % (pixel_format, self.rgb_formats))
     1086            return False
     1087
    10681088        target_format = {
    10691089                 "XRGB"   : "RGB",
    10701090                 "BGRX"   : "RGB",
     
    10761096        start = time.time()
    10771097        w = image.get_width()
    10781098        h = image.get_height()
    1079         pixels = image.get_pixels()
    1080         PIL = get_codec("PIL")
    1081         if not PIL:
    1082             self.warn_encoding_once("no-PIL-module", "no PIL module, cannot convert %s to one of: %s" % (pixel_format, self.rgb_formats))
    1083             return False
    10841099        img = PIL.Image.frombuffer(target_format, (w, h), pixels, "raw", pixel_format, image.get_rowstride())
    10851100        rowstride = w*len(target_format)    #number of characters is number of bytes per pixel!
    10861101        data = img.tostring("raw", target_format)
  • xpra/x11/bindings/keyboard_bindings.pyx

     
    688688            if image==NULL:
    689689                return  None
    690690            l = image.width*image.height*4
    691             pixels = argbdata_to_pixdata(<const unsigned long*> image.pixels, l)
     691            pixels = argbdata_to_pixdata(<const unsigned int*> image.pixels, l)
    692692            name = str(image.name)
    693693            return [image.x, image.y, image.width, image.height, image.xhot, image.yhot,
    694694                image.cursor_serial, pixels, name]