10.40: add conversions and arithmetic operations to swilena

Index: tools/swilena/ChangeLog from Nicolas Burrus <burrus_n@lrde.epita.fr> * meta/swilena_arith.i: New. * generate_arith_instantiations.py: New. * python/tests/arith.py: New test. * meta/swilena_conversions.i: New. * generate_conversions_instantiations.py: New. * python/tests/conversions.py: New test. * generate_morpho_instantiations.py: Update. * python/tests/morpho.py: New test. * expand.sh: Adjust consequently. * meta/Makefile.am: Likewise. * python/tests/Makefile.am: Likewise. Index: tools/swilena/expand.sh --- tools/swilena/expand.sh Thu, 09 Oct 2003 16:21:55 +0200 burrus_n (oln/s/25_expand.sh 1.11 750) +++ tools/swilena/expand.sh Mon, 10 Nov 2003 14:09:27 +0100 burrus_n (oln/s/25_expand.sh 1.12 750) @@ -9,7 +9,7 @@ mkdir -p "$SWILENA/python" mkdir -p "$SWILENA/ruby" -MODULES="$MODULES ntg morpho" +MODULES="$MODULES ntg" for dim in 1 2 3; do ## {1d,2d,3d} families @@ -30,6 +30,7 @@ ## Image instances cat >>"$SWILENA/src/swilena_image${dim}d.i" <<EOF +%include swilena_ntg_macros.i %import swilena_ntg.i EOF # int_u8 int_u16 int_s8 int_s16 int_s8s int_s16s int_s32s @@ -43,9 +44,18 @@ make_image(image${dim}d_s32, $dim, ntg_int_s32, ntg_int_s32_value) make_image(image${dim}d_float, $dim, ntg_float, ntg_float_value) EOF + ## Morpho algorithms MODULES="$MODULES morpho${dim}d" - cd "$SWILENA/src" && ../generate_morpho_instantiations.py && cd .. + $SWILENA/generate_morpho_instantiations.py $SWILENA/src + + ## Arith + MODULES="$MODULES arith${dim}d" + $SWILENA/generate_arith_instantiations.py $SWILENA/src + + ## Conversions + MODULES="$MODULES conversions${dim}d" + $SWILENA/generate_conversions_instantiations.py $SWILENA/src done # for dim ... #################### Python stuff ####################### Index: tools/swilena/meta/Makefile.am --- tools/swilena/meta/Makefile.am Sat, 27 Sep 2003 19:02:23 +0200 burrus_n (oln/s/26_Makefile.a 1.2 640) +++ tools/swilena/meta/Makefile.am Mon, 10 Nov 2003 14:09:27 +0100 burrus_n (oln/s/26_Makefile.a 1.3 640) @@ -2,6 +2,7 @@ EXTRA_DIST = swilena_core.i swilena_decl.i swilena_describe.i \ swilena_exception.i swilena_image.i swilena_imagesize.i \ - swilena_morpho.i swilena_ntg.i swilena_point.i \ + swilena_morpho.i swilena_arith.i swilena_conversions.i \ + swilena_ntg.i swilena_point.i \ swilena_structelt.i \ swilena_w_win.i Index: tools/swilena/python/tests/Makefile.am --- tools/swilena/python/tests/Makefile.am Sun, 21 Sep 2003 23:13:48 +0200 burrus_n (oln/v/15_Makefile.a 1.1 600) +++ tools/swilena/python/tests/Makefile.am Mon, 10 Nov 2003 14:09:27 +0100 burrus_n (oln/v/15_Makefile.a 1.2 600) @@ -4,7 +4,7 @@ ## TESTS_ENVIRONMENT = \ - PYTHONPATH="$(PYTHONPATH):.." \ + PYTHONPATH="$(PYTHONPATH):..:$(srcdir)/.." \ IMGDIR="$(top_srcdir)/olena/img" -TESTS = simple1.py +TESTS = arith.py conversions.py simple1.py morpho.py Index: tools/swilena/generate_morpho_instantiations.py --- tools/swilena/generate_morpho_instantiations.py Sat, 08 Nov 2003 18:17:07 +0100 burrus_n (oln/v/24_generate_m 1.3 700) +++ tools/swilena/generate_morpho_instantiations.py Mon, 10 Nov 2003 14:09:27 +0100 burrus_n (oln/v/24_generate_m 1.4 700) @@ -123,13 +123,13 @@ instantiate(dim, "top_hat_contrast_op", img_type, img_type, win_type) instantiate(dim, "fast_top_hat_contrast_op", img_type, img_type, win_type) - # FIXME: watershed only works with ntg_int_u8 - if type == "ntg_int_u32": + # FIXME: fix this when available + #if type == "ntg_int_u8": # Watershed - img_ret_type = "::oln::image%(dim)sd< ntg_int_u32 >" % vars() - instantiate(dim, "watershed_seg", img_ret_type, img_type, neighb_type) - instantiate(dim, "watershed_con", img_ret_type, img_type, neighb_type) - instantiate(dim, "watershed_seg_or", img_type, img_type, img_ret_type, neighb_type) + #img_ret_type = "::oln::image%(dim)sd< ntg_int_u32 >" % vars() + #instantiate(dim, "watershed_seg", img_ret_type, img_type, neighb_type) + #instantiate(dim, "watershed_con", img_ret_type, img_type, neighb_type) + #instantiate(dim, "watershed_seg_or", img_type, img_type, img_ret_type, neighb_type) # FIXME: this is broken # instantiate(dim, "laplacian", img_type, img_type, win_type) Index: tools/swilena/python/tests/morpho.py --- tools/swilena/python/tests/morpho.py Mon, 10 Nov 2003 14:15:38 +0100 burrus_n () +++ tools/swilena/python/tests/morpho.py Mon, 10 Nov 2003 14:09:27 +0100 burrus_n (oln/v/47_morpho.py 1.1 700) @@ -0,0 +1,11 @@ +#!/usr/bin/env python + +import os +import sys +from swilena_all2d import * + +imgdir = os.environ['IMGDIR'] + +lena = image2d_u8(load(imgdir + "/lena.pgm")) +closing(lena, win_c4p()) +#watershed_seg(lena, neighb_c4()) Index: tools/swilena/meta/swilena_arith.i --- tools/swilena/meta/swilena_arith.i Mon, 10 Nov 2003 14:15:38 +0100 burrus_n () +++ tools/swilena/meta/swilena_arith.i Mon, 10 Nov 2003 14:09:27 +0100 burrus_n (oln/v/48_swilena_ar 1.1 640) @@ -0,0 +1,54 @@ +// -*- C++ -*- + +%include swilena_describe.i +%include swilena_core.i + +%define decl_arith(Dim) + +#if Dim == 1 +%{ +#include <oln/basics1d.hh> +%} +#elif Dim == 2 +%{ +#include <oln/basics2d.hh> +%} +#elif Dim == 3 +%{ +#include <oln/basics3d.hh> +%} +#endif + +%{ +#include <oln/arith/ops.hh> +%} + +%inline %{ + + template<class R, class I1, class I2> + static R my_plus(const I1& i1, const I2& i2) + { return ::oln::arith::plus<R>(i1, i2); } + + template<class R, class I1, class I2> + static R my_minus(const I1& i1, const I2& i2) + { return ::oln::arith::minus<R>(i1, i2); } + + template<class R, class I1, class I2> + static R my_times(const I1& i1, const I2& i2) + { return ::oln::arith::times<R>(i1, i2); } + + template<class R, class I1, class I2> + static R my_div(const I1& i1, const I2& i2) + { return ::oln::arith::div<R>(i1, i2); } + + template<class R, class I1, class I2> + static R my_min(const I1& i1, const I2& i2) + { return ::oln::arith::min<R>(i1, i2); } + + template<class R, class I1, class I2> + static R my_max(const I1& i1, const I2& i2) + { return ::oln::arith::max<R>(i1, i2); } + +%} + +%enddef Index: tools/swilena/meta/swilena_conversions.i --- tools/swilena/meta/swilena_conversions.i Mon, 10 Nov 2003 14:15:38 +0100 burrus_n () +++ tools/swilena/meta/swilena_conversions.i Mon, 10 Nov 2003 14:09:27 +0100 burrus_n (oln/v/49_swilena_co 1.1 600) @@ -0,0 +1,28 @@ +// -*- C++ -*- + +%include swilena_describe.i +%include swilena_core.i + +%define decl_conversions(Dim) + +%{ +#include <oln/convert/basics.hh> +%} + +// FIXME: not very elegant, but %{ %} blocks are not preprocessed. + +#if Dim == 1 +%{ +#include <oln/basics1d.hh> +%} +#elif Dim == 2 +%{ +#include <oln/basics2d.hh> +%} +#elif Dim == 3 +%{ +#include <oln/basics3d.hh> +%} +#endif + +%enddef Index: tools/swilena/generate_conversions_instantiations.py --- tools/swilena/generate_conversions_instantiations.py Mon, 10 Nov 2003 14:15:38 +0100 burrus_n () +++ tools/swilena/generate_conversions_instantiations.py Mon, 10 Nov 2003 14:09:27 +0100 burrus_n (oln/v/50_generate_c 1.1 700) @@ -0,0 +1,121 @@ +#!/usr/bin/env python + +import sys + +files = [] + +def close_files(): + for file in files: + file.close() + +def open_files(path): + for dim in range(1, 4): + files.append(open(path + "/swilena_conversions%(dim)sd.i" % vars(), 'w')) + +def write_headers(): + for dim in range(1, 4): + file = files[dim - 1] + file.write("""// Note: this file was generated by generate_conversions_instantiations.py. + +%%module swilena_conversions%(dim)sd + +%%include swilena_exception.i + +%%include swilena_ntg_macros.i +%%import swilena_image%(dim)sd.i +%%import swilena_ntg.i + +%%include swilena_conversions.i + +decl_conversions(%(dim)s) +""" % vars()) + +def instantiate(dim, algo, name, img_from, img_to): + file = files[dim - 1] + file.write(""" +// +// %(img_from)s ==> %(img_to)s +// + +%%newobject %(algo)s_to_%(name)s; + +%%inline %%{ +namespace oln { + namespace convert { + + %(img_to)s & + %(algo)s_to_%(name)s(const %(img_from)s& i) + { + typedef %(img_to)s::value_type to_value_type; + return *new %(img_to)s(oln::apply(oln::convert::%(algo)s< to_value_type >(), i)); + } + } +} +%%} +""" % vars()) + +def get_name(type): + # Remove the ntg_ prefix + return type[4:] + +def write_algorithms(): + reduce_relations = [ ("ntg_int_u32", "ntg_int_u8"), + ("ntg_int_s32", "ntg_int_s8") ] + + # FIXME: no conversions from float are available + + # Note: no conversion to bin available, one should use a threshold + # instead. + + integer_types = [ "ntg_int_u8", "ntg_int_u32", + "ntg_int_s8", "ntg_int_s32" ] + + scalar_types = integer_types + [ "ntg_float" ] + + # Algorithms with all types and all dims + for dim in range(1, 4): + # stretch, bound and force + for relation in reduce_relations: + for algo in ["stretch", "bound"]: + from_type = relation[0] + to_type = relation[1] + from_img = "::oln::image%(dim)sd< %(from_type)s >" % vars() + to_img = "::oln::image%(dim)sd< %(to_type)s >" % vars() + instantiate(dim, algo, get_name(to_type), from_img, to_img) + + for from_type in scalar_types: + for to_type in scalar_types: + if from_type != to_type: + from_img = "::oln::image%(dim)sd< %(from_type)s >" % vars() + to_img = "::oln::image%(dim)sd< %(to_type)s >" % vars() + instantiate(dim, "cast", get_name(to_type), + from_img, to_img) + + for to_type in scalar_types: + from_type = "ntg_bin" + from_img = "::oln::image%(dim)sd< %(from_type)s >" % vars() + to_img = "::oln::image%(dim)sd< %(to_type)s >" % vars() + instantiate(dim, "cast", get_name(to_type), + from_img, to_img) + + for from_type in integer_types: + to_type = "ntg_bin" + from_img = "::oln::image%(dim)sd< %(from_type)s >" % vars() + to_img = "::oln::image%(dim)sd< %(to_type)s >" % vars() + instantiate(dim, "cast", get_name(to_type), + from_img, to_img) + + +def main(): + if len(sys.argv) != 2: + sys.stderr.write ("Usage: generate_conversions_instantiations.py path\n") + sys.exit (1) + else: + path = sys.argv[1] + open_files(path) + write_headers() + write_algorithms() + close_files() + +if __name__ == "__main__": + main() Index: tools/swilena/generate_arith_instantiations.py --- tools/swilena/generate_arith_instantiations.py Mon, 10 Nov 2003 14:15:38 +0100 burrus_n () +++ tools/swilena/generate_arith_instantiations.py Mon, 10 Nov 2003 14:09:27 +0100 burrus_n (oln/v/51_generate_a 1.1 700) @@ -0,0 +1,84 @@ +#!/usr/bin/env python + +import sys + +files = [] + +def close_files(): + for file in files: + file.close() + +def open_files(path): + for dim in range(1, 4): + files.append(open(path + "/swilena_arith%(dim)sd.i" % vars(), 'w')) + +def write_headers(): + for dim in range(1, 4): + file = files[dim - 1] + file.write("""// Note: this file was generated by generate_arith.py. + +%%module swilena_arith%(dim)sd + +%%include swilena_exception.i +%%include swilena_ntg_macros.i +%%import swilena_ntg.i + +%%include swilena_arith.i + +%%import swilena_image%(dim)sd.i + +decl_arith(%(dim)s) + +""" % vars()) + +def instantiate(dim, algorithm, *args): + file = files[dim - 1] + swilena_algorithm = algorithm + params = args[0] + for param in args[1:]: + params += ", " + param + file.write( + "%%template(%(algorithm)s) my_%(swilena_algorithm)s< %(params)s >;\n" + % vars()) + +def get_bigger_type(type): + traits = { "ntg_int_u8" : "ntg_int_u32", + "ntg_int_u32" : "ntg_int_u32", + "ntg_int_s8" : "ntg_int_s32", + "ntg_int_s32" : "ntg_int_s32" } + if traits.has_key(type): + return traits[type] + return type + +def write_algorithms(): + # Algorithms with all types and all dims + for dim in range(1, 4): + for type in [ "ntg_bin", + "ntg_int_u8", "ntg_int_u32", + "ntg_int_s8", "ntg_int_s32", + "ntg_float" ]: + img_type = "::oln::image%(dim)sd< %(type)s >" % vars() + bigger_type = get_bigger_type(type) + return_img_type = "::oln::image%(dim)sd< %(bigger_type)s >" % vars() + + # FIXME: these algorithms do not work with bin + if type != "ntg_bin": + for algo in [ "plus", "minus", "times", "div" ]: + instantiate(dim, algo, return_img_type, img_type, img_type) + + instantiate(dim, "min", return_img_type, img_type, img_type) + instantiate(dim, "max", return_img_type, img_type, img_type) + +def main(): + if len(sys.argv) != 2: + sys.stderr.write ("Usage: generate_arith_instantiations.py path\n") + sys.exit (1) + else: + path = sys.argv[1] + open_files(path) + write_headers() + write_algorithms() + close_files() + +if __name__ == "__main__": + main() Index: tools/swilena/python/tests/arith.py --- tools/swilena/python/tests/arith.py Mon, 10 Nov 2003 14:15:38 +0100 burrus_n () +++ tools/swilena/python/tests/arith.py Mon, 10 Nov 2003 14:09:27 +0100 burrus_n (oln/w/0_arith.py 1.1 750) @@ -0,0 +1,23 @@ +#!/usr/bin/env python + +import os +import sys +from swilena_arith2d import * +from swilena_image2d import * + +imgdir = os.environ['IMGDIR'] + +ima1 = image2d_u8(5, 5) +ima2 = image2d_u8(5, 5) + +ima1.set(1, 1, 51) +ima1.set(2, 2, 42) + +ima2.set(0, 0, 12) +ima2.set(2, 2, 9) + +ima = plus(ima1, ima2) + +assert(ima.at(0, 0) == 12) +assert(ima.at(1, 1) == 51) +assert(ima.at(2, 2) == 51) Index: tools/swilena/python/tests/conversions.py --- tools/swilena/python/tests/conversions.py Mon, 10 Nov 2003 14:15:38 +0100 burrus_n () +++ tools/swilena/python/tests/conversions.py Mon, 10 Nov 2003 14:09:27 +0100 burrus_n (oln/w/1_conversion 1.1 750) @@ -0,0 +1,16 @@ +#!/usr/bin/env python + +import os +import sys +from swilena_all2d import * + +imgdir = os.environ['IMGDIR'] + +lena = image2d_u8(load(imgdir + "/lena.pgm")) +lena32 = cast_to_int_u32(lena) + +lena32.set(0, 0, 65536) +assert(lena32.at(0, 0) == 65536) + +lena8 = bound_to_int_u8(lena32) +assert(lena8.at(0, 0) == 255)
participants (1)
-
Nicolas Burrus