Added RBA::image::from_s for reading image back from string. Added tests.

This commit is contained in:
Matthias Koefferlein 2020-04-27 00:14:11 +02:00
parent 37299a8edf
commit fa38948382
2 changed files with 46 additions and 3 deletions

View File

@ -190,6 +190,11 @@ gsi::Class<img::DataMapping> 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<ImageRef> dm_update_view;
};
static ImageRef *img_from_s (const std::string &s)
{
std::auto_ptr<ImageRef> 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<bool> get_mask_data (ImageRef *obj)
gsi::Class<img::Object> decl_BasicImage ("lay", "BasicImage", gsi::Methods (), "@hide");
gsi::Class<ImageRef> 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<ImageRef> 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"

View File

@ -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;]")