From 87fe16413ef00ed8d1a2e6c8fe01e82845f85f56 Mon Sep 17 00:00:00 2001 From: lufebe16 Date: Fri, 5 Jan 2024 17:56:57 +0100 Subject: [PATCH] Kivy/Android - refactoring --- pysollib/images.py | 7 +--- pysollib/kivy/LApp.py | 19 +++++++++ pysollib/kivy/LImage.py | 17 -------- pysollib/kivy/tkcanvas.py | 8 ++-- pysollib/kivy/tkutil.py | 81 +++++++++++++++++++++++++-------------- 5 files changed, 78 insertions(+), 54 deletions(-) diff --git a/pysollib/images.py b/pysollib/images.py index c197af89..12d568a5 100644 --- a/pysollib/images.py +++ b/pysollib/images.py @@ -91,12 +91,7 @@ class Images: except Exception: return None - if TOOLKIT == 'kivy': - w = img.texture.size[0] - h = img.texture.size[1] - else: - w, h = img.width(), img.height() - + w, h = img.width(), img.height() if self.CARDW < 0: self.CARDW, self.CARDH = w, h else: diff --git a/pysollib/kivy/LApp.py b/pysollib/kivy/LApp.py index cb3a5a1c..22708aa2 100644 --- a/pysollib/kivy/LApp.py +++ b/pysollib/kivy/LApp.py @@ -270,6 +270,25 @@ def LColorToLuminance(color): # ============================================================================= +def LTextureToLuminance(texture): + b = texture.pixels + s = 4 + n = len(b)/1000 + if n > 4: + s = n - n % 4 + n = 0 + ll = 0 + for i in range(0, len(b), int(s)): + rr = int.from_bytes(b[i:i+1], byteorder='big', signed=False) / 256.0 # noqa + gg = int.from_bytes(b[i+1:i+2], byteorder='big', signed=False) / 256.0 # noqa + bb = int.from_bytes(b[i+2:i+3], byteorder='big', signed=False) / 256.0 # noqa + ll = ll + LColorToLuminance([rr, gg, bb, 1]) + n += 1 + return (ll/n) + +# ============================================================================= + + def LColorToKivy(outline): if (outline[0] == '#'): outline = outline[1:] diff --git a/pysollib/kivy/LImage.py b/pysollib/kivy/LImage.py index 4358e195..24840969 100644 --- a/pysollib/kivy/LImage.py +++ b/pysollib/kivy/LImage.py @@ -20,7 +20,6 @@ from kivy.properties import StringProperty from kivy.uix.image import Image as KivyImage from kivy.uix.widget import Widget -from pysollib.kivy.LApp import LColorToLuminance from pysollib.kivy.LBase import LBase # ============================================================================= @@ -222,20 +221,4 @@ class LImage(Widget, LBase): def subsample(self, r): return LImage(texture=self.texture) - def luminance(self): - b = self.texture.pixels - s = 4 - n = len(b)/1000 - if n > 4: - s = n - n % 4 - n = 0 - ll = 0 - for i in range(0, len(b), int(s)): - rr = int.from_bytes(b[i:i+1], byteorder='big', signed=False) / 256.0 # noqa - gg = int.from_bytes(b[i+1:i+2], byteorder='big', signed=False) / 256.0 # noqa - bb = int.from_bytes(b[i+2:i+3], byteorder='big', signed=False) / 256.0 # noqa - ll = ll + LColorToLuminance([rr, gg, bb, 1]) - n += 1 - return (ll/n) - # ============================================================================= diff --git a/pysollib/kivy/tkcanvas.py b/pysollib/kivy/tkcanvas.py index 4981284a..9fe5c091 100644 --- a/pysollib/kivy/tkcanvas.py +++ b/pysollib/kivy/tkcanvas.py @@ -35,7 +35,6 @@ from kivy.uix.anchorlayout import AnchorLayout from pysollib.kivy.LApp import LAnimationManager from pysollib.kivy.LApp import LColorToKivy -from pysollib.kivy.LApp import LColorToLuminance from pysollib.kivy.LApp import LImageItem from pysollib.kivy.LApp import LLine from pysollib.kivy.LApp import LRectangle @@ -856,8 +855,10 @@ class MfxCanvas(LImage): # color. We do not support that. Instead of this wie examine # the background and set the color accordingly. if self._bg_img is not None: - lumi = self._bg_img.luminance() + from pysollib.kivy.LApp import LTextureToLuminance + lumi = LTextureToLuminance(self._bg_img.texture) else: + from pysollib.kivy.LApp import LColorToLuminance lumi = LColorToLuminance(self._bg_color) self._text_color = ("#000000", "#ffffff")[lumi < 0.4] @@ -868,7 +869,8 @@ class MfxCanvas(LImage): # print('setTile: %s, %s, %s' % (image, stretch, save_aspect)) if image: try: - self._bg_img = LImage(source=image) + from pysollib.kivy.tkutil import LImageInfo + self._bg_img = LImageInfo(image) self._stretch_bg_image = stretch self._save_aspect_bg_image = save_aspect self.update_widget(self.pos, self.size) diff --git a/pysollib/kivy/tkutil.py b/pysollib/kivy/tkutil.py index 0faeeee7..8c3f5b6a 100644 --- a/pysollib/kivy/tkutil.py +++ b/pysollib/kivy/tkutil.py @@ -37,7 +37,6 @@ from kivy.core.text import Label as CoreLabel from kivy.graphics.texture import Texture from pysollib.kivy.LApp import LTopLevel0 -from pysollib.kivy.LImage import LImage # ************************************************************************ # * window manager util @@ -216,39 +215,64 @@ def after_cancel(t): # ************************************************************************ # * image handling # ************************************************************************ +# Wrappers + +LCoreImage = CoreImage # noqa + +class LImageInfo(object): # noqa + def __init__(self, arg): + if type(arg) is Texture: + self.filename = None + self.source = None + self.texture = arg + self.size = self.texture.size + if type(arg) is str: + self.filename = arg + self.source = arg + self.texture = LCoreImage(arg).texture + self.size = self.texture.size + + # pysol core needs that: + + def subsample(self, image): + return self + + def width(self): + return self.size[0] + + def height(self): + return self.size[1] + + def getWidth(self): + return self.size[0] + + def getHeight(self): + return self.size[1] + +# ************************************************************************ +# Interface to core. def makeImage(file=None, data=None, dither=None, alpha=None): - kw = {} if data is None: assert file is not None - kw["source"] = file - # print('makeImage: source = %s' % file) - # if (file=='/home/lb/PRG/Python/Kivy/pysolfc/data/images/redeal.gif'): - # y = self.yy + return LImageInfo(file) else: assert data is not None - kw["texture"] = data - # ob das geht ?? - kommt das vor ? - # yy = self.yy - ''' - if 'source' in kw: - logging.info ("makeImage: " + kw["source"]) - if 'texture' in kw: - logging.info ("makeImage: " + str(kw["texture"])) - ''' + return LImageInfo(data) - return LImage(**kw) - - -loadImage = makeImage +loadImage = makeImage # noqa - sorry flake8, aber das gehört zu oben dazu! def copyImage(image, x, y, width, height): - # return Image(source=image.source) - # return Image(texture=image.texture) - return image + # wird das überhaupt aufgerufen - ja bei SubSampleImage. + # aber: wo wird das gebraucht? - oder ist es eine altlast + # welche gar keine Relevanz mehr hat ? (Kann auch None + # zurückgeben, ohne dass etwas fehlt oder abstürzt). + + tregion = image.texture.get_region(x, y, width, height) + return LImageInfo(tregion) def fillTexture(texture, fill, outline=None, owidth=1): @@ -337,14 +361,16 @@ def createImage(width, height, fill, outline=None, outwidth=1): texture = Texture.create(size=(width, height), colorfmt='rgba') fillTexture(texture, fill, outline, outwidth) - image = LImage(texture=texture) - # logging.info("createImage: LImage create %s" % image) + image = LImageInfo(texture) + # logging.info("createImage: LImageInfo create %s" % image) return image def createImagePIL(width, height, fill, outline=None, outwidth=1): # Is this needed for Kivy? createImage(width, height, fill, outline=outline, outwidth=outwidth) + # wird nur mit USE_PIL benutzt: nicht relevant. Der code wird mit + # Kivy nie durchlaufen. def shadowImage(image, color='#3896f8', factor=0.3): @@ -488,15 +514,14 @@ def createBottom(image, color='white', backfile=None): tmp0 = _createImageMask(image.texture, color) if backfile: - tmp1 = CoreImage(backfile) + tmp1 = LCoreImage(backfile) txtre = _scaleTextureToSize(tmp1.texture, image.texture.size) tmp = _pasteTextureTo(txtre, tmp0) else: tmp = tmp0 - img = LImage(texture=tmp) - img.size[0] = image.getWidth() - img.size[1] = image.getHeight() + img = LImageInfo(tmp) + img.size = (image.getWidth(), image.getHeight()) return img ''' im = image._pil_image