operator binding

This commit is contained in:
2021-05-14 06:40:38 +02:00
parent eaf039a83f
commit 95c1e6d9c6
5 changed files with 178 additions and 14 deletions

View File

@@ -1,6 +1,14 @@
"""Python Binding to FloatImg """
# FloatImg class and static functions
from floatimg.image import FloatImg, create, create_rgb, create_from_dump, GRAY, RGB, fileinfos
# FloatImg class, constants & static functions
from floatimg.image import (
GRAY,
RGB,
FloatImg,
create,
create_rgb,
create_from_dump,
fileinfos,
)
__version__ = "0.0.1"
__version__ = "0.0.1"

View File

@@ -122,6 +122,48 @@ c_fimg_add_rgb.argtypes = (
)
c_fimg_add_rgb.restype = ct.c_int
# operators
c_fimg_add_3 = LIB.fimg_add_3
c_fimg_add_3.argtypes = (
ct.POINTER(C_FloatImg),
ct.POINTER(C_FloatImg),
ct.POINTER(C_FloatImg),
)
c_fimg_add_3.restype = ct.c_int
c_fimg_sub_3 = LIB.fimg_sub_3
c_fimg_sub_3.argtypes = (
ct.POINTER(C_FloatImg),
ct.POINTER(C_FloatImg),
ct.POINTER(C_FloatImg),
)
c_fimg_sub_3.restype = ct.c_int
c_fimg_mul_3 = LIB.fimg_mul_3
c_fimg_mul_3.argtypes = (
ct.POINTER(C_FloatImg),
ct.POINTER(C_FloatImg),
ct.POINTER(C_FloatImg),
)
c_fimg_mul_3.restype = ct.c_int
c_fimg_minimum = LIB.fimg_minimum
c_fimg_minimum.argtypes = (
ct.POINTER(C_FloatImg),
ct.POINTER(C_FloatImg),
ct.POINTER(C_FloatImg),
)
c_fimg_minimum.restype = ct.c_int
c_fimg_maximum = LIB.fimg_maximum
c_fimg_maximum.argtypes = (
ct.POINTER(C_FloatImg),
ct.POINTER(C_FloatImg),
ct.POINTER(C_FloatImg),
)
c_fimg_maximum.restype = ct.c_int
############################################################################################################
class FloatImg:
"""
@@ -168,9 +210,9 @@ class FloatImg:
#######################################################################################################
def clone(self, copy_data=False):
"""return a clone of the current instance"""
new_pic = C_FloatImg()
assert c_fimg_clone(self.c_img_p, ct.pointer(new_pic), int(copy_data)) == 0
return FloatImg(new_pic)
c_img_p = C_FloatImg()
assert c_fimg_clone(self.c_img_p, ct.pointer(c_img_p), int(copy_data)) == 0
return FloatImg(c_img_p)
#######################################################################################################
def copy_data(self, to_img):
@@ -196,11 +238,7 @@ class FloatImg:
#######################################################################################################
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(color):
c_rgb[i] = c
c_rgb = (ct.c_float * 3)(*color)
assert c_fimg_put_rgb(self.c_img_p, x, y, ct.pointer(c_rgb)) == 0
#######################################################################################################
@@ -229,10 +267,44 @@ class FloatImg:
== 0
)
# TODO
# fimg_plot_rgb
# fimg_add_rgb
#######################################################################################################
def __add__(self, img):
""" + operator overload"""
res = self.clone(False)
assert c_fimg_add_3(self.c_img_p, img.c_img_p, res.c_img_p) == 0
return res
#######################################################################################################
def __sub__(self, img):
""" - operator overload"""
res = self.clone(False)
assert c_fimg_sub_3(self.c_img_p, img.c_img_p, res.c_img_p) == 0
return res
#######################################################################################################
def __mul__(self, img):
""" * operator overload"""
res = self.clone(False)
assert c_fimg_mul_3(self.c_img_p, img.c_img_p, res.c_img_p) == 0
return res
#######################################################################################################
def min(self, img):
"""return a new image with minimum pixel values per channel between current and parameter"""
res = self.clone(False)
# C code invert comparison
assert c_fimg_maximum(self.c_img_p, img.c_img_p, res.c_img_p) == 0
return res
#######################################################################################################
def max(self, img):
"""return a new image with maximum pixel values per channel between current and parameter"""
res = self.clone(False)
# C code invert comparison
assert c_fimg_minimum(self.c_img_p, img.c_img_p, res.c_img_p) == 0
return res
# TODO
# export(fname, flags) : depending file extension, save to the correct format

View File