From fa389483828fb561d2b15eb87bd11329ca76217d Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Mon, 27 Apr 2020 00:14:11 +0200 Subject: [PATCH] Added RBA::image::from_s for reading image back from string. Added tests. --- src/img/img/gsiDeclImg.cc | 17 +++++++++++++++++ testdata/ruby/imgObject.rb | 32 +++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/img/img/gsiDeclImg.cc b/src/img/img/gsiDeclImg.cc index 836cb8a77..174887712 100644 --- a/src/img/img/gsiDeclImg.cc +++ b/src/img/img/gsiDeclImg.cc @@ -190,6 +190,11 @@ gsi::Class decl_ImageDataMapping ("lay", "ImageDataMapping", "@brief Returns the current number of color map entries.\n" "@return The number of entries.\n" ) + + gsi::method_ext ("colormap_value", &gsi::colormap_value, gsi::arg ("n"), + "@brief Returns the vlue for a given color map entry.\n" + "@param n The index of the entry (0..\\num_colormap_entries-1)\n" + "@return The value (see \\add_colormap_entry for a description).\n" + ) + gsi::method_ext ("colormap_color", &gsi::colormap_color, gsi::arg ("n"), "@brief Returns the color for a given color map entry.\n" "@param n The index of the entry (0..\\num_colormap_entries-1)\n" @@ -401,6 +406,13 @@ private: tl::DeferredMethod dm_update_view; }; +static ImageRef *img_from_s (const std::string &s) +{ + std::auto_ptr img (new ImageRef ()); + img->from_string (s.c_str ()); + return img.release (); +} + static ImageRef *new_image () { return new ImageRef (); @@ -525,6 +537,10 @@ static std::vector get_mask_data (ImageRef *obj) gsi::Class decl_BasicImage ("lay", "BasicImage", gsi::Methods (), "@hide"); gsi::Class decl_Image (decl_BasicImage, "lay", "Image", + gsi::constructor ("from_s", &gsi::img_from_s, gsi::arg ("s"), + "@brief Creates an image from the string returned by \\to_s.\n" + "This method has been introduced in version 0.27." + ) + gsi::constructor ("new", &gsi::new_image, "@brief Create a new image with the default attributes" "\n" @@ -973,6 +989,7 @@ gsi::Class decl_Image (decl_BasicImage, "lay", "Image", ) + gsi::method ("to_s", &ImageRef::to_string, "@brief Converts the image to a string\n" + "The string returned can be used to create an image object using \\from_s.\n" "@return The string\n" ), "@brief An image to be stored as a layout annotation\n" diff --git a/testdata/ruby/imgObject.rb b/testdata/ruby/imgObject.rb index 1f35a1688..9d961fa93 100644 --- a/testdata/ruby/imgObject.rb +++ b/testdata/ruby/imgObject.rb @@ -53,14 +53,31 @@ class IMG_TestClass < TestBase assert_equal(dm.num_colormap_entries, 0) dm.add_colormap_entry(0, 0x123456); dm.add_colormap_entry(0.5, 0xff0000); + dm.add_colormap_entry(0.75, 0x123456, 0x654321); dm.add_colormap_entry(1.0, 0x00ff00); - assert_equal(dm.num_colormap_entries, 3) + assert_equal(dm.num_colormap_entries, 4) assert_equal(dm.colormap_color(0) & 0xffffff, 0x123456) assert_equal(dm.colormap_value(0), 0.0) assert_equal(dm.colormap_color(1) & 0xffffff, 0xff0000) + assert_equal(dm.colormap_lcolor(1) & 0xffffff, 0xff0000) + assert_equal(dm.colormap_rcolor(1) & 0xffffff, 0xff0000) assert_equal(dm.colormap_value(1), 0.5) - assert_equal(dm.colormap_color(2) & 0xffffff, 0x00ff00) - assert_equal(dm.colormap_value(2), 1.0) + assert_equal(dm.colormap_color(2) & 0xffffff, 0x123456) + assert_equal(dm.colormap_lcolor(2) & 0xffffff, 0x123456) + assert_equal(dm.colormap_rcolor(2) & 0xffffff, 0x654321) + assert_equal(dm.colormap_value(2), 0.75) + assert_equal(dm.colormap_color(3) & 0xffffff, 0x00ff00) + assert_equal(dm.colormap_value(3), 1.0) + + image = RBA::Image.new + image.data_mapping = dm + + dm2 = image.data_mapping + assert_equal(dm2.num_colormap_entries, 4) + + dm.clear_colormap + assert_equal(dm2.num_colormap_entries, 4) + assert_equal(dm.num_colormap_entries, 0) end @@ -74,12 +91,21 @@ class IMG_TestClass < TestBase assert_equal(image.to_s, "empty:") assert_equal(image.is_empty?, true) + image2 = RBA::Image::from_s(image.to_s) + assert_equal(image2.to_s, image.to_s) + image = RBA::Image.new(2, 3, []) assert_equal(image.to_s, "mono:matrix=(1,0,0) (0,1,0) (0,0,1);min_value=0;max_value=1;is_visible=true;z_position=0;brightness=0;contrast=0;gamma=1;red_gain=1;green_gain=1;blue_gain=1;color_mapping=[0,'#000000';1,'#ffffff';];width=2;height=3;data=[0;0;0;0;0;0;]") + image2 = RBA::Image::from_s(image.to_s) + assert_equal(image2.to_s, image.to_s) + image = RBA::Image.new(2, 3, [], [], []) assert_equal(image.to_s, "color:matrix=(1,0,0) (0,1,0) (0,0,1);min_value=0;max_value=1;is_visible=true;z_position=0;brightness=0;contrast=0;gamma=1;red_gain=1;green_gain=1;blue_gain=1;color_mapping=[0,'#000000';1,'#ffffff';];width=2;height=3;data=[0,0,0;0,0,0;0,0,0;0,0,0;0,0,0;0,0,0;]") + image2 = RBA::Image::from_s(image.to_s) + assert_equal(image2.to_s, image.to_s) + data = [0.0, 0.5, 1.5, 2.5, 10, 20] image = RBA::Image.new(2, 3, data) assert_equal(image.to_s, "mono:matrix=(1,0,0) (0,1,0) (0,0,1);min_value=0;max_value=1;is_visible=true;z_position=0;brightness=0;contrast=0;gamma=1;red_gain=1;green_gain=1;blue_gain=1;color_mapping=[0,'#000000';1,'#ffffff';];width=2;height=3;data=[0;0.5;1.5;2.5;10;20;]")