def cairo_paint_pointer_overlay(context, cursor_data, px: int, py: int, start_time): if not cursor_data: return elapsed = max(0, monotonic_time() - start_time) if elapsed > 6: return cw = cursor_data[3] ch = cursor_data[4] xhot = cursor_data[5] yhot = cursor_data[6] pixels = cursor_data[8] x = px - xhot y = py - yhot alpha = max(0, (5.0 - elapsed) / 5.0) log("cairo_paint_pointer_overlay%s drawing pointer with cairo, alpha=%s", (context, x, y, start_time), alpha) context.translate(x, y) context.rectangle(0, 0, cw, ch) argb = unpremultiply_argb(pixels) img_data = memoryview_to_bytes(argb) pixbuf = get_pixbuf_from_data(img_data, True, cw, ch, cw * 4) context.set_operator(cairo.OPERATOR_OVER) Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0) context.paint()
def update_icon(self, width, height, coding, data): log("%s.update_icon(%s, %s, %s, %s bytes)", self, width, height, coding, len(data)) if coding == "premult_argb32": if unpremultiply_argb is not None: #we usually cannot do in-place and this is not performance critical data = byte_buffer_to_buffer(unpremultiply_argb(data)) pixbuf = gdk.pixbuf_new_from_data(data, gtk.gdk.COLORSPACE_RGB, True, 8, width, height, width * 4) else: # slower fallback: we round-trip through PNG. # This is ridiculous, but faster than doing a bunch of alpha # un-premultiplying and byte-swapping by hand in Python cairo_surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) cairo_surf.get_data()[:] = data loader = gdk.PixbufLoader() cairo_surf.write_to_png(loader) loader.close() pixbuf = loader.get_pixbuf() else: loader = gdk.PixbufLoader(coding) loader.write(data, len(data)) loader.close() pixbuf = loader.get_pixbuf() self.set_icon(pixbuf)
def unpremultiply(self, img_data): if type(img_data) not in (str, _buffer): try: unpremultiply_argb_in_place(img_data) return img_data except: log.warn("failed to unpremultiply %s (len=%s)" % (type(img_data), len(img_data))) return unpremultiply_argb(img_data)
def unpremultiply(self, img_data): from xpra.codecs.argb.argb import unpremultiply_argb, unpremultiply_argb_in_place #@UnresolvedImport if type(img_data) not in (str, _buffer): try: unpremultiply_argb_in_place(img_data) return img_data except: log.warn("failed to unpremultiply %s (len=%s)" % (type(img_data), len(img_data))) return unpremultiply_argb(img_data)
def unpremultiply(self, img_data): if type(img_data) == str: #cannot do in-place: assert unpremultiply_argb is not None, "missing argb.unpremultiply_argb" return byte_buffer_to_buffer(unpremultiply_argb(img_data)) #assume this is a writeable buffer (ie: ctypes from mmap): assert unpremultiply_argb_in_place is not None, "missing argb.unpremultiply_argb_in_place" unpremultiply_argb_in_place(img_data) return img_data
def unpremultiply(self, img_data): if type(img_data)==str: #cannot do in-place: assert unpremultiply_argb is not None, "missing argb.unpremultiply_argb" return byte_buffer_to_buffer(unpremultiply_argb(img_data)) #assume this is a writeable buffer (ie: ctypes from mmap): assert unpremultiply_argb is not None, "missing argb.unpremultiply_argb_in_place" unpremultiply_argb_in_place(img_data) return img_data
def update_icon(self, width, height, coding, data): log("%s.update_icon(%s, %s, %s, %s bytes)", self, width, height, coding, len(data)) coding = bytestostr(coding) if coding == "premult_argb32": if unpremultiply_argb is None: #we could use PIL here with mode 'RGBa' log.warn("cannot process premult_argb32 icon without the argb module") return #we usually cannot do in-place and this is not performance critical data = byte_buffer_to_buffer(unpremultiply_argb(data)) pixbuf = pixbuf_new_from_data(data, COLORSPACE_RGB, True, 8, width, height, width*4) else: loader = PixbufLoader() loader.write(data) loader.close() pixbuf = loader.get_pixbuf() log("%s.set_icon(%s)", self, pixbuf) self.set_icon(pixbuf)
def update_icon(self, width, height, coding, data): coding = bytestostr(coding) iconlog("%s.update_icon(%s, %s, %s, %s bytes)", self, width, height, coding, len(data)) if PYTHON3 and WIN32: iconlog("not setting icon to prevent crashes..") return if coding == "premult_argb32": #we usually cannot do in-place and this is not performance critical data = unpremultiply_argb(data) rgba = memoryview_to_bytes(bgra_to_rgba(data)) pixbuf = get_pixbuf_from_data(rgba, True, width, height, width*4) else: loader = PixbufLoader() loader.write(data) loader.close() pixbuf = loader.get_pixbuf() #for debugging, save to a file so we can see it: #pixbuf.save("C-%s-%s.png" % (self._id, int(time.time())), "png") iconlog("%s.set_icon(%s)", self, pixbuf) self.set_icon(pixbuf)
def _do_paint_rgb32(self, img_data, x, y, width, height, rowstride, options, callbacks): #log.debug("do_paint_rgb32(%s bytes, %s, %s, %s, %s, %s, %s, %s) backing depth=%s", len(img_data), x, y, width, height, rowstride, options, callbacks, self._backing.get_depth()) #log.info("data head=%s", [hex(ord(v))[2:] for v in list(img_data[:500])]) if self._backing is None: return False from xpra.codecs.argb.argb import unpremultiply_argb, unpremultiply_argb_in_place, byte_buffer_to_buffer #@UnresolvedImport if type(img_data)==str or not hasattr(img_data, "raw"): #cannot do in-place: img_data = byte_buffer_to_buffer(unpremultiply_argb(img_data)) else: #assume this is a writeable buffer (ie: ctypes from mmap): unpremultiply_argb_in_place(img_data) pixbuf = gdk.pixbuf_new_from_data(img_data, gtk.gdk.COLORSPACE_RGB, True, 8, width, height, rowstride) cr = self._backing.cairo_create() cr.rectangle(x, y, width, height) cr.set_source_pixbuf(pixbuf, x, y) cr.set_operator(cairo.OPERATOR_SOURCE) cr.paint() return True
def update_icon(self, width, height, coding, data): coding = bytestostr(coding) iconlog("%s.update_icon(%s, %s, %s, %s bytes)", self, width, height, coding, len(data)) if PYTHON3 and WIN32: iconlog("not setting icon to prevent crashes..") return if coding == "premult_argb32": #we usually cannot do in-place and this is not performance critical data = unpremultiply_argb(data) rgba = memoryview_to_bytes(bgra_to_rgba(data)) pixbuf = get_pixbuf_from_data(rgba, True, width, height, width * 4) else: loader = PixbufLoader() loader.write(data) loader.close() pixbuf = loader.get_pixbuf() #for debugging, save to a file so we can see it: #pixbuf.save("C-%s-%s.png" % (self._id, int(time.time())), "png") iconlog("%s.set_icon(%s)", self, pixbuf) self.set_icon(pixbuf)
def _do_paint_rgb32(self, img_data, x, y, width, height, rowstride, options, callbacks): #log.debug("do_paint_rgb32(%s bytes, %s, %s, %s, %s, %s, %s, %s) backing depth=%s", len(img_data), x, y, width, height, rowstride, options, callbacks, self._backing.get_depth()) #log.info("data head=%s", [hex(ord(v))[2:] for v in list(img_data[:500])]) if self._backing is None: return False from xpra.codecs.argb.argb import unpremultiply_argb, unpremultiply_argb_in_place, byte_buffer_to_buffer #@UnresolvedImport if type(img_data) == str or not hasattr(img_data, "raw"): #cannot do in-place: img_data = byte_buffer_to_buffer(unpremultiply_argb(img_data)) else: #assume this is a writeable buffer (ie: ctypes from mmap): unpremultiply_argb_in_place(img_data) pixbuf = gdk.pixbuf_new_from_data(img_data, gtk.gdk.COLORSPACE_RGB, True, 8, width, height, rowstride) cr = self._backing.cairo_create() cr.rectangle(x, y, width, height) cr.set_source_pixbuf(pixbuf, x, y) cr.set_operator(cairo.OPERATOR_SOURCE) cr.paint() return True
def update_icon(self, width, height, coding, data): log("%s.update_icon(%s, %s, %s, %s bytes)", self, width, height, coding, len(data)) coding = bytestostr(coding) if coding == "premult_argb32": if unpremultiply_argb is None: #we could use PIL here with mode 'RGBa' log.warn( "cannot process premult_argb32 icon without the argb module" ) return #we usually cannot do in-place and this is not performance critical data = unpremultiply_argb(data) rgba = byte_buffer_to_buffer(bgra_to_rgba(data)) pixbuf = get_pixbuf_from_data(rgba, True, width, height, width * 4) else: loader = PixbufLoader() loader.write(data) loader.close() pixbuf = loader.get_pixbuf() log("%s.set_icon(%s)", self, pixbuf) self.set_icon(pixbuf)
def update_icon(self, width, height, coding, data): self.debug("update_icon(%s, %s, %s, %s bytes)", width, height, coding, len(data)) if coding == "premult_argb32": if unpremultiply_argb is not None: # we usually cannot do in-place and this is not performance critical data = byte_buffer_to_buffer(unpremultiply_argb(data)) pixbuf = gdk.pixbuf_new_from_data(data, gtk.gdk.COLORSPACE_RGB, True, 8, width, height, width * 4) else: # slower fallback: we round-trip through PNG. # This is ridiculous, but faster than doing a bunch of alpha # un-premultiplying and byte-swapping by hand in Python cairo_surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) cairo_surf.get_data()[:] = data loader = gdk.PixbufLoader() cairo_surf.write_to_png(loader) loader.close() pixbuf = loader.get_pixbuf() else: loader = gdk.PixbufLoader(coding) loader.write(data, len(data)) loader.close() pixbuf = loader.get_pixbuf() self.set_icon(pixbuf)
def unpremultiply(self, img_data): try: unpremultiply_argb_in_place(img_data) return img_data except: return byte_buffer_to_buffer(unpremultiply_argb(img_data))