Skip to content

Commit

Permalink
#812 add PoC image overlay and image stamp to png transfers
Browse files Browse the repository at this point in the history
git-svn-id: https://xpra.org/svn/Xpra/trunk@22826 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Jun 1, 2019
1 parent 3c397c6 commit 6cd525f
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/xpra/x11/gtk_x11/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import os
import struct
from io import BytesIO

from xpra.gtk_common.error import xsync, xswallow
from xpra.gtk_common.gobject_util import one_arg_signal, n_arg_signal
Expand All @@ -27,7 +28,7 @@
X11WindowBindings, #@UnresolvedImport
)
from xpra.os_util import bytestostr
from xpra.util import csv, repr_ellipsized, first_time
from xpra.util import csv, repr_ellipsized, first_time, envbool
from xpra.log import Logger

gdk = import_gdk()
Expand Down Expand Up @@ -584,8 +585,8 @@ def do_property_notify(self, event):
return
self.cancel_incr_data_timer()
data = b"".join(self.incr_data_chunks)
self.reset_incr_data()
log("got incremental data termination, total size=%i bytes", len(data))
self.reset_incr_data()
self.got_local_contents(target, dtype, dformat, data)
return
except PropertyError:
Expand All @@ -597,6 +598,7 @@ def do_property_notify(self, event):
self.got_local_contents(target, dtype, dformat, data)

def got_local_contents(self, target, dtype=None, dformat=None, data=None):
data = self.filter_data(target, dtype, dformat, data)
target_requests = self.local_requests.pop(target, {})
for timer, got_contents, time in target_requests.values():
if log.is_debug_enabled():
Expand All @@ -605,6 +607,44 @@ def got_local_contents(self, target, dtype=None, dformat=None, data=None):
glib.source_remove(timer)
got_contents(dtype, dformat, data)

def filter_data(self, target, dtype=None, dformat=None, data=None):
log("filter_data(%s, %s, %s, ..)", target, dtype, dformat)
IMAGE_OVERLAY = os.environ.get("XPRA_CLIPBOARD_IMAGE_OVERLAY", None)
if IMAGE_OVERLAY and not os.path.exists(IMAGE_OVERLAY):
IMAGE_OVERLAY = None
IMAGE_STAMP = envbool("XPRA_CLIPBOARD_IMAGE_STAMP", True)
if dtype in ("image/png", ) and (IMAGE_STAMP or IMAGE_OVERLAY):
from xpra.codecs.pillow.decoder import open_only
img = open_only(data, ("png", ))
has_alpha = img.mode=="RGBA"
if not has_alpha and IMAGE_OVERLAY:
img = img.convert("RGBA")
w, h = img.size
from PIL import Image #@UnresolvedImport
overlay = Image.open(IMAGE_OVERLAY)
if overlay.mode!="RGBA":
log.warn("Warning: cannot use overlay image '%s'", IMAGE_OVERLAY)
log.warn(" invalid mode '%s'", overlay.mode)
else:
log("adding clipboard image overlay to %s", dtype)
overlay_resized = overlay.resize((w, h), Image.ANTIALIAS)
composite = Image.alpha_composite(img, overlay_resized)
if not has_alpha and img.mode=="RGBA":
composite = composite.convert("RGB")
img = composite
if IMAGE_STAMP:
log("adding clipboard image stamp to %s", dtype)
from datetime import datetime
from PIL import ImageDraw
img_draw = ImageDraw.Draw(img)
w, h = img.size
img_draw.text((10, max(0, h//2-16)), 'via Xpra, %s' % datetime.now().isoformat(), fill='black')
buf = BytesIO()
img.save(buf, "PNG")
data = buf.getvalue()
buf.close()
return data


def reschedule_incr_data_timer(self):
self.cancel_incr_data_timer()
Expand Down

0 comments on commit 6cd525f

Please sign in to comment.