Skip to content

Commit 8ebe7bb

Browse files
committed
Adding graphics functions and an example
1 parent cb3cc76 commit 8ebe7bb

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

arrayfire/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from .image import *
2222
from .features import *
2323
from .vision import *
24+
from .graphics import *
2425

2526
# do not export default modules as part of arrayfire
2627
del ct
@@ -32,6 +33,7 @@
3233
del uidx
3334
del seq
3435
del index
36+
del cell
3537

3638
#do not export internal functions
3739
del binary_func

arrayfire/graphics.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#######################################################
2+
# Copyright (c) 2015, ArrayFire
3+
# All rights reserved.
4+
#
5+
# This file is distributed under 3-clause BSD license.
6+
# The complete license agreement can be obtained at:
7+
# http://arrayfire.com/licenses/BSD-3-Clause
8+
########################################################
9+
10+
from .library import *
11+
from .array import *
12+
13+
class cell(ct.Structure):
14+
_fields_ = [("row", ct.c_int),
15+
("col", ct.c_int),
16+
("title", ct.c_char_p),
17+
("cmap", ct.c_int)]
18+
19+
def __init__(self, r, c, title, cmap):
20+
self.row = r
21+
self.col = c
22+
self.title = title if title is not None else ct.c_char_p()
23+
self.cmap = cmap
24+
25+
class window(object):
26+
27+
def __init__(self, width=None, height=None, title=None):
28+
self._r = -1
29+
self._c = -1
30+
self._wnd = ct.c_longlong(0)
31+
self._cmap = AF_COLORMAP_DEFAULT
32+
33+
_width = 1280 if width is None else width
34+
_height = 720 if height is None else height
35+
_title = "ArrayFire" if title is None else title
36+
37+
_title = _title.encode("ascii")
38+
39+
safe_call(clib.af_create_window(ct.pointer(self._wnd),\
40+
ct.c_int(_width), ct.c_int(_height), ct.c_char_p(_title)))
41+
42+
def __del__(self):
43+
safe_call(clib.af_destroy_window(self._wnd))
44+
45+
def set_pos(self, x, y):
46+
safe_call(clib.af_set_position(self._wnd, ct.c_int(x), ct.c_int(y)))
47+
48+
def set_title(self, title):
49+
safe_call(clib.af_set_title(self._wnd, title))
50+
51+
def set_colormap(self, cmap):
52+
self._cmap = cmap
53+
54+
def image(self, img, title=None):
55+
_cell = cell(self._r, self._c, title, self._cmap)
56+
safe_call(clib.af_draw_image(self._wnd, img.arr, ct.pointer(_cell)))
57+
58+
def plot(self, X, Y, title=None):
59+
_cell = cell(self._r, self._c, title, self._cmap)
60+
safe_call(clib.af_draw_plot(self._wnd, X.arr, Y.arr, ct.pointer(_cell)))
61+
62+
def hist(self, X, min_val, max_val, title=None):
63+
_cell = cell(self._r, self._c, title, self._cmap)
64+
safe_call(clib.af_draw_hist(self._wnd, X.arr, \
65+
ct.c_double(max_val), ct.c_double(min_val),\
66+
ct.pointer(_cell)))
67+
68+
def grid(rows, cols):
69+
safe_call(af_grid(self._wnd, ct.c_int(rows), ct.c_int(cols)))
70+
71+
def show(self):
72+
safe_call(clib.af_show(self._wnd))
73+
74+
def close(self):
75+
tmp = ct.c_bool(True)
76+
safe_call(clib.af_is_window_closed(ct.pointer(tmp), self._wnd))
77+
return tmp
78+
79+
def __getitem__(self, keys):
80+
if not isinstance(keys, tuple):
81+
raise IndexError("Window expects indexing along two dimensions")
82+
if len(keys) != 2:
83+
raise IndexError("Window expects indexing along two dimensions only")
84+
if not (is_number(keys[0]) and is_number(keys[1])):
85+
raise IndexError("Window expects the indices to be numbers")
86+
self._r = keys[0]
87+
self._c = keys[1]

arrayfire/image.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def gradient(image):
2121
def load_image(file_name, is_color=False):
2222
assert(os.path.isfile(file_name))
2323
image = array()
24-
safe_call(clib.af_load_image(ct.pointer(image.arr), ct.c_char_p(file_name.encode('ascii')), is_color))
24+
safe_call(clib.af_load_image(ct.pointer(image.arr), \
25+
ct.c_char_p(file_name.encode('ascii')), is_color))
2526
return image
2627

2728
def save_image(image, file_name):

examples/plot2d.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/python
2+
3+
import arrayfire as af
4+
import math
5+
6+
POINTS = 10000
7+
PRECISION = 1.0 / float(POINTS)
8+
9+
val = -math.pi
10+
X = math.pi * (2 * (af.range(POINTS) / POINTS) - 1)
11+
12+
win = af.window(512, 512, "2D Plot example using ArrayFire")
13+
sign = 1.0
14+
15+
while not win.close():
16+
Y = af.sin(X)
17+
win.plot(X, Y)
18+
19+
X += PRECISION * sign
20+
val += PRECISION * sign
21+
22+
if (val > math.pi):
23+
sign = -1.0
24+
elif (val < -math.pi):
25+
sign = 1.0

0 commit comments

Comments
 (0)