refactor FloatImg class method to function imported in floatimg module. add tests for file dump / load : coverage of exixting python code is 100%, yeah

This commit is contained in:
John Doe
2021-05-12 08:28:06 +02:00
parent f5200d6fba
commit 1d4e2242cd
6 changed files with 212 additions and 66 deletions

View File

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

View File

@@ -2,8 +2,18 @@ import ctypes as ct
from floatimg.settings import LIB
############################################################################################################
# type constants
GRAY = 1
RGB = 3
RGBA = 4
RGBZ = 99
############################################################################################################
# Type mapping
class C_FloatImg(ct.Structure):
"""mapping to the C structure of FloatImg"""
pass
@@ -66,16 +76,31 @@ 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), ct.c_int, ct.c_int, ct.POINTER(ct.c_float * 3))
c_fimg_get_rgb.argtypes = (
ct.POINTER(C_FloatImg),
ct.c_int,
ct.c_int,
ct.POINTER(ct.c_float * 3),
)
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), ct.c_int, ct.c_int, ct.POINTER(ct.c_float * 3))
c_fimg_put_rgb.argtypes = (
ct.POINTER(C_FloatImg),
ct.c_int,
ct.c_int,
ct.POINTER(ct.c_float * 3),
)
c_fimg_put_rgb.restype = ct.c_int
c_fimg_rgb_constant = LIB.fimg_rgb_constant
c_fimg_rgb_constant.argtypes = (ct.POINTER(C_FloatImg), ct.c_float, ct.c_float, ct.c_float)
c_fimg_rgb_constant.argtypes = (
ct.POINTER(C_FloatImg),
ct.c_float,
ct.c_float,
ct.c_float,
)
c_fimg_rgb_constant.restype = ct.c_int
c_fimg_dump_to_file = LIB.fimg_dump_to_file
@@ -100,12 +125,6 @@ class FloatImg:
:attr c_img: an pointer to the c_img structure
"""
# type constants
GRAY = 1
RGB = 3
RGBA = 4
RGBZ = 99
# proxy attributes to the C_FloatImg structure
magic = property(lambda self: self.c_img.magic)
width = property(lambda self: self.c_img.width)
@@ -129,21 +148,6 @@ class FloatImg:
def __str__(self):
return f"<{self.__class__.__name__} instance {id(self)} width={self.width} height={self.height} type={self.str_type}>"
#######################################################################################################
@classmethod
def create(cls, witdh, height, type_id):
"""create an new FloatImg instance"""
assert cls.type_is_valid(type_id)
img = C_FloatImg()
assert c_fimgcreate(ct.pointer(img), witdh, height, type_id) == 0
return FloatImg(img)
#######################################################################################################
@classmethod
def create_rgb(cls, witdh, height):
"""create a new rgb instance """
return cls.create(witdh, height, cls.RGB)
#######################################################################################################
def destroy(self):
"""destroy the underlying structure. automattically called at instance destruction"""
@@ -169,12 +173,6 @@ class FloatImg:
def copy_data(self, to_img):
assert c_fimg_copy_data(self.c_img_p, to_img.c_img_p) == 0
#######################################################################################################
@staticmethod
def type_is_valid(type_id):
"""return True if type_id is a valid one"""
return c_fimg_type_is_valid(type_id) == 1
#######################################################################################################
@property
def str_type(self):
@@ -216,26 +214,49 @@ class FloatImg:
def load_from_dump(self, fname):
"""load data from a dump. size and type have to be compatible"""
assert (
c_fimg_load_from_dump(ct.c_char_p(bytes(fname, encoding="utf8")), self.c_img_p)
c_fimg_load_from_dump(
ct.c_char_p(bytes(fname, encoding="utf8")), self.c_img_p
)
== 0
)
#######################################################################################################
@classmethod
def fileinfos(cls, fname):
"""return witdh, height, img_type triplet read from a dump file"""
datas = (ct.c_int * 3)()
assert (
c_fimg_fileinfos(ct.c_char_p(bytes(fname, encoding="utf8")), ct.pointer(datas))
== 0
)
return datas[:3]
#######################################################################################################
@classmethod
def create_from_dump(cls, fname):
"""Create a new instance from a dump file"""
witdh, height, img_type = cls.fileinfos(fname)
img = cls.create(witdh, height, img_type)
img.load_from_dump(fname)
return img
###########################################################################################################
def fileinfos(fname):
"""return witdh, height, img_type triplet read from a dump file"""
datas = (ct.c_int * 3)()
assert (
c_fimg_fileinfos(ct.c_char_p(bytes(fname, encoding="utf8")), ct.pointer(datas))
== 0
)
return datas[:3]
###########################################################################################################
def create(witdh, height, type_id):
"""create an new FloatImg instance"""
assert type_is_valid(type_id)
img = C_FloatImg()
assert c_fimgcreate(ct.pointer(img), witdh, height, type_id) == 0
return FloatImg(img)
###########################################################################################################
def create_rgb(witdh, height):
"""create a new rgb instance """
return create(witdh, height, RGB)
###########################################################################################################
def create_from_dump(fname):
"""Create a new instance from a dump file"""
witdh, height, img_type = fileinfos(fname)
img = create(witdh, height, img_type)
img.load_from_dump(fname)
return img
###########################################################################################################
def type_is_valid(type_id):
"""return True if type_id is a valid one"""
return c_fimg_type_is_valid(type_id) == 1

View File

@@ -1,3 +1,5 @@
import ctypes
LIB = ctypes.cdll.LoadLibrary("../FloatImg/libfloatimg.so")
# LIB = ctypes.cdll.LoadLibrary("../FloatImg/libfloatimg.so")
LIB = ctypes.cdll.LoadLibrary("../FloatImg4PythonBinding/build/lib/libfloatimg.so")