[python-arrayfire] 41/250: Adding graphics functions and an example

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Mar 28 22:59:29 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/master
in repository python-arrayfire.

commit 8ebe7bb43d1dadfd12226428247bb8e198b6c83f
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Thu Jul 16 17:28:44 2015 -0400

    Adding graphics functions and an example
---
 arrayfire/__init__.py |  2 ++
 arrayfire/graphics.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++
 arrayfire/image.py    |  3 +-
 examples/plot2d.py    | 25 +++++++++++++++
 4 files changed, 116 insertions(+), 1 deletion(-)

diff --git a/arrayfire/__init__.py b/arrayfire/__init__.py
index 28c2ffc..326234a 100644
--- a/arrayfire/__init__.py
+++ b/arrayfire/__init__.py
@@ -21,6 +21,7 @@ from .signal     import *
 from .image      import *
 from .features   import *
 from .vision     import *
+from .graphics   import *
 
 # do not export default modules as part of arrayfire
 del ct
@@ -32,6 +33,7 @@ del os
 del uidx
 del seq
 del index
+del cell
 
 #do not export internal functions
 del binary_func
diff --git a/arrayfire/graphics.py b/arrayfire/graphics.py
new file mode 100644
index 0000000..392cbd0
--- /dev/null
+++ b/arrayfire/graphics.py
@@ -0,0 +1,87 @@
+#######################################################
+# Copyright (c) 2015, ArrayFire
+# All rights reserved.
+#
+# This file is distributed under 3-clause BSD license.
+# The complete license agreement can be obtained at:
+# http://arrayfire.com/licenses/BSD-3-Clause
+########################################################
+
+from .library import *
+from .array import *
+
+class cell(ct.Structure):
+    _fields_ = [("row", ct.c_int),
+                ("col", ct.c_int),
+                ("title", ct.c_char_p),
+                ("cmap", ct.c_int)]
+
+    def __init__(self, r, c, title, cmap):
+        self.row = r
+        self.col = c
+        self.title = title if title is not None else ct.c_char_p()
+        self.cmap = cmap
+
+class window(object):
+
+    def __init__(self, width=None, height=None, title=None):
+        self._r = -1
+        self._c = -1
+        self._wnd = ct.c_longlong(0)
+        self._cmap = AF_COLORMAP_DEFAULT
+
+        _width  = 1280 if  width is None else  width
+        _height =  720 if height is None else height
+        _title  = "ArrayFire" if title is None else title
+
+        _title = _title.encode("ascii")
+
+        safe_call(clib.af_create_window(ct.pointer(self._wnd),\
+                                        ct.c_int(_width), ct.c_int(_height), ct.c_char_p(_title)))
+
+    def __del__(self):
+        safe_call(clib.af_destroy_window(self._wnd))
+
+    def set_pos(self, x, y):
+        safe_call(clib.af_set_position(self._wnd, ct.c_int(x), ct.c_int(y)))
+
+    def set_title(self, title):
+        safe_call(clib.af_set_title(self._wnd, title))
+
+    def set_colormap(self, cmap):
+        self._cmap = cmap
+
+    def image(self, img, title=None):
+        _cell = cell(self._r, self._c, title, self._cmap)
+        safe_call(clib.af_draw_image(self._wnd, img.arr, ct.pointer(_cell)))
+
+    def plot(self, X, Y, title=None):
+        _cell = cell(self._r, self._c, title, self._cmap)
+        safe_call(clib.af_draw_plot(self._wnd, X.arr, Y.arr, ct.pointer(_cell)))
+
+    def hist(self, X, min_val, max_val, title=None):
+        _cell = cell(self._r, self._c, title, self._cmap)
+        safe_call(clib.af_draw_hist(self._wnd, X.arr, \
+                                    ct.c_double(max_val), ct.c_double(min_val),\
+                                    ct.pointer(_cell)))
+
+    def grid(rows, cols):
+        safe_call(af_grid(self._wnd, ct.c_int(rows), ct.c_int(cols)))
+
+    def show(self):
+        safe_call(clib.af_show(self._wnd))
+
+    def close(self):
+        tmp = ct.c_bool(True)
+        safe_call(clib.af_is_window_closed(ct.pointer(tmp), self._wnd))
+        return tmp
+
+    def __getitem__(self, keys):
+        if not isinstance(keys, tuple):
+            raise IndexError("Window expects indexing along two dimensions")
+        if len(keys) != 2:
+            raise IndexError("Window expects indexing along two dimensions only")
+        if not (is_number(keys[0]) and is_number(keys[1])):
+            raise IndexError("Window expects the indices to be numbers")
+        self._r = keys[0]
+        self._c = keys[1]
diff --git a/arrayfire/image.py b/arrayfire/image.py
index 3fc7622..227a895 100644
--- a/arrayfire/image.py
+++ b/arrayfire/image.py
@@ -21,7 +21,8 @@ def gradient(image):
 def load_image(file_name, is_color=False):
     assert(os.path.isfile(file_name))
     image = array()
-    safe_call(clib.af_load_image(ct.pointer(image.arr), ct.c_char_p(file_name.encode('ascii')), is_color))
+    safe_call(clib.af_load_image(ct.pointer(image.arr), \
+                                 ct.c_char_p(file_name.encode('ascii')), is_color))
     return image
 
 def save_image(image, file_name):
diff --git a/examples/plot2d.py b/examples/plot2d.py
new file mode 100755
index 0000000..08b3777
--- /dev/null
+++ b/examples/plot2d.py
@@ -0,0 +1,25 @@
+#!/usr/bin/python
+
+import arrayfire as af
+import math
+
+POINTS = 10000
+PRECISION = 1.0 / float(POINTS)
+
+val = -math.pi
+X = math.pi * (2 * (af.range(POINTS) / POINTS) - 1)
+
+win = af.window(512, 512, "2D Plot example using ArrayFire")
+sign = 1.0
+
+while not win.close():
+    Y = af.sin(X)
+    win.plot(X, Y)
+
+    X += PRECISION * sign
+    val += PRECISION * sign
+
+    if (val > math.pi):
+        sign = -1.0
+    elif (val < -math.pi):
+        sign = 1.0

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-arrayfire.git



More information about the debian-science-commits mailing list