fimg-core.c binding completed

This commit is contained in:
Mutah 2021-05-12 19:50:27 +02:00
parent e57d63f562
commit eaf039a83f
2 changed files with 37 additions and 26 deletions

View File

@ -6,8 +6,8 @@ from floatimg.settings import LIB
# type constants
GRAY = 1
RGB = 3
RGBA = 4
RGBZ = 99
RGBA = 4 # may not be used
RGBZ = 99 # may not be used
############################################################################################################
# Type mapping
@ -32,7 +32,7 @@ C_FloatImg._fields_ = [
]
############################################################################################################
# declaration of input / output types
# declaration of input / output types for C binding of core functions
c_fimgcreate = LIB.fimg_create
c_fimgcreate.argtypes = (ct.POINTER(C_FloatImg), ct.c_int, ct.c_int, ct.c_int)
c_fimgcreate.restype = ct.c_int
@ -61,7 +61,6 @@ c_fimg_clear = LIB.fimg_clear
c_fimg_clear.argtypes = (ct.POINTER(C_FloatImg),)
c_fimg_clear.restype = ct.c_int
c_fimg_plot_rgb = LIB.fimg_plot_rgb
c_fimg_plot_rgb.argtypes = (
ct.POINTER(C_FloatImg),
@ -73,8 +72,6 @@ c_fimg_plot_rgb.argtypes = (
)
c_fimg_plot_rgb.restype = ct.c_int
# int fimg_get_rgb(FloatImg *head, int x, int y, float *rgb);
c_fimg_get_rgb = LIB.fimg_get_rgb
c_fimg_get_rgb.argtypes = (
ct.POINTER(C_FloatImg),
@ -84,7 +81,6 @@ c_fimg_get_rgb.argtypes = (
)
c_fimg_get_rgb.restype = ct.c_int
# int fimg_put_rgb(FloatImg *head, int x, int y, float *rgb);
c_fimg_put_rgb = LIB.fimg_put_rgb
c_fimg_put_rgb.argtypes = (
ct.POINTER(C_FloatImg),
@ -115,6 +111,17 @@ c_fimg_fileinfos = LIB.fimg_fileinfos
c_fimg_fileinfos.argtypes = (ct.c_char_p, ct.POINTER(ct.c_int * 3))
c_fimg_fileinfos.restype = ct.c_int
c_fimg_add_rgb = LIB.fimg_add_rgb
c_fimg_add_rgb.argtypes = (
ct.POINTER(C_FloatImg),
ct.c_int,
ct.c_int,
ct.c_float,
ct.c_float,
ct.c_float,
)
c_fimg_add_rgb.restype = ct.c_int
############################################################################################################
class FloatImg:
"""
@ -131,12 +138,6 @@ class FloatImg:
fval = property(lambda self: self.c_img.fval)
count = property(lambda self: self.c_img.count)
# oh yeah, really really sluggish access to pixels img.R[0].contents
# however, pixel data are not designed to be accessed this way
# R = property(
# lambda self: pointer(self.c_img.contents.R)[: self.width * self.height]
# )
#######################################################################################################
def __init__(self, c_img):
self.c_img = c_img
@ -167,12 +168,8 @@ class FloatImg:
#######################################################################################################
def clone(self, copy_data=False):
"""return a clone of the current instance"""
if copy_data:
flags = 1
else:
flags = 0
new_pic = C_FloatImg()
assert c_fimg_clone(self.c_img_p, ct.pointer(new_pic), flags) == 0
assert c_fimg_clone(self.c_img_p, ct.pointer(new_pic), int(copy_data)) == 0
return FloatImg(new_pic)
#######################################################################################################
@ -192,19 +189,25 @@ class FloatImg:
#######################################################################################################
def get(self, x, y):
"""get r,g,b triplet from a pixel"""
rgb = (ct.c_float * 3)()
assert c_fimg_get_rgb(self.c_img_p, x, y, ct.pointer(rgb)) == 0
return rgb[:3]
color = (ct.c_float * 3)()
assert c_fimg_get_rgb(self.c_img_p, x, y, ct.pointer(color)) == 0
return color[:3]
#######################################################################################################
def put(self, x, y, rgb):
"""put r,g,b triplet to a pixel"""
# TODO may be a better way to create the array rather than iterating
def put(self, x, y, color):
"""put color to a pixel"""
# TODO may be a better way to create the array rather than iterating that may cost too much in
# intensive usaage
c_rgb = (ct.c_float * 3)()
for i, c in enumerate(rgb):
for i, c in enumerate(color):
c_rgb[i] = c
assert c_fimg_put_rgb(self.c_img_p, x, y, ct.pointer(c_rgb)) == 0
#######################################################################################################
def add(self, x, y, color):
"""add color to a pixel """
assert c_fimg_add_rgb(self.c_img_p, x, y, *color) == 0
#######################################################################################################
def dump(self, fname):
"""save data to a dump file"""

View File

@ -54,7 +54,6 @@ def test_rgb_constant():
assert img.get(x, y) == color
def test_copy_data():
width = 5
height = 5
@ -104,3 +103,12 @@ def test_put_rgb():
assert img.get(0, 0) == [0.0, 0.0, 0.0]
img.put(0, 0, (127.0, 127.0, 127.0))
assert img.get(0, 0) == [127.0, 127.0, 127.0]
def test_add():
width, height = 5, 5
img = floatimg.create_rgb(width, height)
color = (32.0, 32.0, 32.0)
img.put(0, 0, color)
img.add(0, 0, color)
assert img.get(0, 0) == [64.0, 64.0, 64.0]