kuuko pushed a commit to branch master.

http://git.enlightenment.org/bindings/python/python-efl.git/commit/?id=31a6f559b0f461cf64160a79d02e2454981aca87

commit 31a6f559b0f461cf64160a79d02e2454981aca87
Author: Kai Huuhko <[email protected]>
Date:   Mon Feb 16 14:54:26 2015 +0200

    Elementary.image: Add memfile support
---
 efl/elementary/image.pyx          | 51 ++++++++++++++++++++++++++--
 examples/elementary/test.py       |  1 +
 examples/elementary/test_image.py | 70 ++++++++++++++++++++++++++++++++++++---
 3 files changed, 114 insertions(+), 8 deletions(-)

diff --git a/efl/elementary/image.pyx b/efl/elementary/image.pyx
index b2eca9f..79cb7e3 100644
--- a/efl/elementary/image.pyx
+++ b/efl/elementary/image.pyx
@@ -114,6 +114,9 @@ Inheritance diagram
 from cpython cimport PyUnicode_AsUTF8String
 from libc.stdint cimport uintptr_t
 
+from cpython.buffer cimport Py_buffer, PyObject_CheckBuffer, \
+    PyObject_GetBuffer, PyBuffer_Release, PyBUF_SIMPLE
+
 from efl.eo cimport _object_mapping_register, object_from_instance
 from efl.utils.conversions cimport _ctouni
 from efl.evas cimport Object as evasObject
@@ -191,9 +194,51 @@ cdef class Image(Object):
         self._set_obj(elm_image_add(parent.obj))
         self._set_properties_from_keyword_args(kwargs)
 
-    #def memfile_set(self, img, size, format, key):
-        # NOTE: remove _cfruni if this is implemented
-        #return bool(elm_image_memfile_set(self.obj, img, size, 
_cfruni(format), _cfruni(key)))
+    def memfile_set(self, img, size, format=None, key=None):
+        """
+        Set a location in memory to be used as an image object's source
+        bitmap.
+
+        This function is handy when the contents of an image file are
+        mapped in memory, for example.
+
+        The ``format`` string should be something like ``"png"``, ``"jpg"``,
+        ``"tga"``, ``"tiff"``, ``"bmp"`` etc, when provided. This improves the 
loader performance as it tries the
+        "correct" loader first, before trying a range of other possible
+        loaders until one succeeds.
+
+        :return: (``True`` = success, ``False`` = error)
+
+        .. versionadded:: 1.14
+
+        :param img: The binary data that will be used as image source
+        :param size: The size of binary data blob ``img``
+        :param format: (Optional) expected format of ``img`` bytes
+        :param key: Optional indexing key of ``img`` to be passed to the
+            image loader (eg. if ``img`` is a memory-mapped EET file)
+        """
+        cdef Py_buffer view
+
+        if not PyObject_CheckBuffer(img):
+            raise TypeError("The provided object does not support buffer 
interface.")
+
+        if isinstance(format, unicode): format = PyUnicode_AsUTF8String(format)
+        if isinstance(key, unicode): key = PyUnicode_AsUTF8String(key)
+
+        PyObject_GetBuffer(img, &view, PyBUF_SIMPLE)
+
+        ret = bool(elm_image_memfile_set(
+            self.obj,
+            <void *>view.buf,
+            size,
+            <const char *>format if format else NULL,
+            <const char *>key if key else NULL
+            ))
+
+        PyBuffer_Release(&view)
+
+        if not ret:
+            raise RuntimeError("Setting memory file for an image failed")
 
     property file:
         """The file (and edje group) that will be used as the image's source.
diff --git a/examples/elementary/test.py b/examples/elementary/test.py
index 3bb1abb..0ce1d16 100755
--- a/examples/elementary/test.py
+++ b/examples/elementary/test.py
@@ -147,6 +147,7 @@ items = [
         ("Icon Transparent", "test_icon", "icon_transparent_clicked"),
         ("Icon Standard", "test_icon", "icon_standard_clicked"),
         ("Image", "test_image", "image_clicked"),
+        ("Image with memfile", "test_image", "image2_clicked"),
         ("Photo", "test_photo", "photo_clicked"),
         ("Photocam", "test_photocam", "photocam_clicked"),
         ("Slideshow", "test_slideshow", "slideshow_clicked"),
diff --git a/examples/elementary/test_image.py 
b/examples/elementary/test_image.py
index c4cfebf..b430369 100644
--- a/examples/elementary/test_image.py
+++ b/examples/elementary/test_image.py
@@ -13,6 +13,9 @@ from efl.elementary.image import Image, ELM_IMAGE_ROTATE_90, \
     ELM_IMAGE_FLIP_VERTICAL, ELM_IMAGE_FLIP_TRANSPOSE, 
ELM_IMAGE_FLIP_TRANSVERSE
 from efl.elementary.progressbar import Progressbar
 from efl.elementary.separator import Separator
+from efl.elementary.label import Label
+from efl.elementary.frame import Frame
+from efl.elementary.list import List
 
 
 script_path = os.path.dirname(os.path.abspath(__file__))
@@ -48,7 +51,8 @@ def _cb_im_download_error(im, info, pb):
     print("CB DOWNLOAD ERROR [status %s, open_error: %s]" % (info.status, 
info.open_error))
     pb.value = 1.0
 
-def image_clicked(obj):
+
+def image_clicked(obj, it=None):
     win = StandardWindow("image", "Image test", autodel=True, size=(320, 480))
     if obj is None:
         win.callback_delete_request_add(lambda o: elementary.exit())
@@ -57,7 +61,8 @@ def image_clicked(obj):
     win.resize_object_add(vbox)
     vbox.show()
 
-    im = Image(win, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH,
+    im = Image(
+        win, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH,
         file=os.path.join(img_path, "logo.png"))
     vbox.pack_end(im)
     im.show()
@@ -66,7 +71,8 @@ def image_clicked(obj):
     vbox.pack_end(sep)
     sep.show()
 
-    hbox = Box(win, layout=ELM_BOX_LAYOUT_FLOW_HORIZONTAL,
+    hbox = Box(
+        win, layout=ELM_BOX_LAYOUT_FLOW_HORIZONTAL,
         size_hint_align=FILL_BOTH)
     vbox.pack_end(hbox)
     hbox.show()
@@ -90,7 +96,8 @@ def image_clicked(obj):
     b.callback_clicked_add(lambda b: im.file_set(remote_url))
     b.show()
 
-    pb = Progressbar(win, size_hint_weight=EXPAND_BOTH,
+    pb = Progressbar(
+        win, size_hint_weight=EXPAND_BOTH,
         size_hint_align=FILL_BOTH)
     hbox.pack_end(pb)
     pb.show()
@@ -103,10 +110,63 @@ def image_clicked(obj):
     win.show()
 
 
+def image2_clicked(obj, it=None):
+    win = StandardWindow("image", "Image test", autodel=True, size=(320, 480))
+    if obj is None:
+        win.callback_delete_request_add(lambda o: elementary.exit())
+
+    vbox = Box(win, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH)
+    win.resize_object_add(vbox)
+
+    im = Image(
+        win, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH)
+
+    with open(os.path.join(img_path, "logo.png"), "rb") as fp:
+        image_data = fp.read()
+        im.memfile_set(image_data, len(image_data))
+    vbox.pack_end(im)
+    im.show()
+
+    im = Image(
+        win, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH)
+
+    vbox.show()
+    win.show()
+
+
 if __name__ == "__main__":
     elementary.init()
+    win = StandardWindow(
+        "test", "python-elementary test application",
+        size=(320, 520))
+    win.callback_delete_request_add(lambda o: elementary.exit())
+
+    box0 = Box(win, size_hint_weight=EXPAND_BOTH)
+    win.resize_object_add(box0)
+    box0.show()
 
-    image_clicked(None)
+    lb = Label(win)
+    lb.text_set("Please select a test from the list below<br>"
+                "by clicking the test button to show the<br>"
+                "test window.")
+    lb.show()
 
+    fr = Frame(win, text="Information", content=lb)
+    box0.pack_end(fr)
+    fr.show()
+
+    items = [("Image", image_clicked),
+             ("Image with memfile", image2_clicked)]
+
+    li = List(win, size_hint_weight=EXPAND_BOTH, size_hint_align=FILL_BOTH)
+    box0.pack_end(li)
+    li.show()
+
+    for item in items:
+        li.item_append(item[0], callback=item[1])
+
+    li.go()
+
+    win.show()
     elementary.run()
     elementary.shutdown()

-- 


Reply via email to