operator binding

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

View File

@ -102,6 +102,26 @@ TODO create a FileInfo named tuple.
witdh, height, img_type = floatimg.fileinfos("/tmp/image_dump") witdh, height, img_type = floatimg.fileinfos("/tmp/image_dump")
``` ```
## Operators
```python
# addition
res_img = img + img2
# substraction
res_img = img - img2
# multiplication
res_img = img * img2
# min value per color channel
res_img = img.min(img2)
# max value per color channel
res_img = img.max(img2)
```
## Development ## Development

View File

@ -1,6 +1,14 @@
"""Python Binding to FloatImg """ """Python Binding to FloatImg """
# FloatImg class and static functions # FloatImg class, constants & static functions
from floatimg.image import FloatImg, create, create_rgb, create_from_dump, GRAY, RGB, fileinfos 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 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: class FloatImg:
""" """
@ -168,9 +210,9 @@ class FloatImg:
####################################################################################################### #######################################################################################################
def clone(self, copy_data=False): def clone(self, copy_data=False):
"""return a clone of the current instance""" """return a clone of the current instance"""
new_pic = C_FloatImg() c_img_p = C_FloatImg()
assert c_fimg_clone(self.c_img_p, ct.pointer(new_pic), int(copy_data)) == 0 assert c_fimg_clone(self.c_img_p, ct.pointer(c_img_p), int(copy_data)) == 0
return FloatImg(new_pic) return FloatImg(c_img_p)
####################################################################################################### #######################################################################################################
def copy_data(self, to_img): def copy_data(self, to_img):
@ -196,11 +238,7 @@ class FloatImg:
####################################################################################################### #######################################################################################################
def put(self, x, y, color): def put(self, x, y, color):
"""put color to a pixel""" """put color to a pixel"""
# TODO may be a better way to create the array rather than iterating that may cost too much in c_rgb = (ct.c_float * 3)(*color)
# intensive usaage
c_rgb = (ct.c_float * 3)()
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 assert c_fimg_put_rgb(self.c_img_p, x, y, ct.pointer(c_rgb)) == 0
####################################################################################################### #######################################################################################################
@ -229,10 +267,44 @@ class FloatImg:
== 0 == 0
) )
# TODO #######################################################################################################
# fimg_plot_rgb def __add__(self, img):
# fimg_add_rgb """ + 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 # export(fname, flags) : depending file extension, save to the correct format

View File

View File

@ -112,3 +112,67 @@ def test_add():
img.put(0, 0, color) img.put(0, 0, color)
img.add(0, 0, color) img.add(0, 0, color)
assert img.get(0, 0) == [64.0, 64.0, 64.0] assert img.get(0, 0) == [64.0, 64.0, 64.0]
def test_op_add():
width, height = 5, 5
img = floatimg.create_rgb(width, height)
color = (32.0, 32.0, 32.0)
img.fill(color)
img2 = floatimg.create_rgb(width, height)
img2.fill(color)
img3 = img + img2
for y in range(height):
for x in range(width):
assert img3.get(x, y) == [64.0, 64.0, 64.0]
def test_op_sub():
width, height = 5, 5
img = floatimg.create_rgb(width, height)
color = (32.0, 32.0, 32.0)
img.fill(color)
img2 = floatimg.create_rgb(width, height)
img2.fill(color)
img3 = img - img2
for y in range(height):
for x in range(width):
assert img3.get(x, y) == [0.0, 0.0, 0.0]
def test_op_mul():
width, height = 5, 5
img = floatimg.create_rgb(width, height)
color = (32.0, 32.0, 32.0)
img.fill(color)
img2 = floatimg.create_rgb(width, height)
img2.fill((2.0, 2.0, 2.0))
img3 = img * img2
for y in range(height):
for x in range(width):
assert img3.get(x, y) == [64.0, 64.0, 64.0]
def test_op_min():
width, height = 5, 5
img = floatimg.create_rgb(width, height)
img.fill((32.0, 0.0, 32.0))
img2 = floatimg.create_rgb(width, height)
img2.fill((0.0, 32.0, 0.0))
img3 = img.min(img2)
for y in range(height):
for x in range(width):
assert img3.get(x, y) == [0.0, 0.0, 0.0]
def test_op_max():
width, height = 5, 5
img = floatimg.create_rgb(width, height)
img.fill((32.0, 0.0, 32.0))
img2 = floatimg.create_rgb(width, height)
img2.fill((0.0, 32.0, 0.0))
img3 = img.max(img2)
for y in range(height):
for x in range(width):
assert img3.get(x, y) == [32.0, 32.0, 32.0]