16 static void write_color_map(tinyxml2::XMLPrinter& printer,
const std::string& name,
const cgv::render::color_map& color_map) {
18 printer.OpenElement(
"ColorMap");
19 printer.PushAttribute(
"name", name.c_str());
21 for(
const auto& color_point : color_map.ref_color_points()) {
22 printer.OpenElement(
"ColorPoint");
23 printer.PushAttribute(
"x", color_point.first);
24 printer.PushAttribute(
"r", color_point.second.R());
25 printer.PushAttribute(
"g", color_point.second.G());
26 printer.PushAttribute(
"b", color_point.second.B());
27 printer.CloseElement();
30 for(
const auto& opacity_point : color_map.ref_opacity_points()) {
31 printer.OpenElement(
"OpacityPoint");
32 printer.PushAttribute(
"x", opacity_point.first);
33 printer.PushAttribute(
"o", opacity_point.second);
34 printer.CloseElement();
37 printer.CloseElement();
41 static void to_xml_printer(tinyxml2::XMLPrinter& printer,
const std::string& name,
const cgv::render::color_map& color_map,
bool put_parent_tag =
true) {
43 std::vector<std::string> names = {name};
44 std::vector<cgv::render::color_map> color_maps = {color_map};
45 to_xml_printer(printer, names, color_maps, put_parent_tag);
48 static void to_xml_printer(tinyxml2::XMLPrinter& printer,
const std::vector<std::string>& names,
const std::vector<cgv::render::color_map>& color_maps,
bool put_parent_tag =
true) {
51 printer.OpenElement(
"ColorMaps");
53 if (names.size() == color_maps.size()) {
54 for (
size_t i = 0; i < names.size(); ++i)
55 write_color_map(printer, names[i], color_maps[i]);
59 printer.CloseElement();
62 static std::string to_xml(
const std::string& name,
const cgv::render::color_map& color_map,
bool put_parent_tag =
true) {
64 std::vector<std::string> names = { name };
65 std::vector<cgv::render::color_map> color_maps = { color_map };
67 return to_xml(names, color_maps, put_parent_tag);
70 static std::string to_xml(
const std::vector<std::string>& names,
const std::vector<cgv::render::color_map>& color_maps,
bool put_parent_tag =
true) {
72 tinyxml2::XMLPrinter printer;
73 to_xml_printer(printer, names, color_maps, put_parent_tag);
74 std::string xml = printer.CStr();
79 static bool write_to_xml_file(
const std::string& file_name,
const std::string& name,
const cgv::render::color_map& color_map,
bool put_parent_tag =
true) {
81 std::vector<std::string> names = { name };
82 std::vector<cgv::render::color_map> color_maps = { color_map };
84 return write_to_xml_file(file_name, names, color_maps, put_parent_tag);
87 static bool write_to_xml_file(
const std::string& file_name,
const std::vector<std::string>& names,
const std::vector<cgv::render::color_map>& color_maps,
bool put_parent_tag =
true) {
89 return cgv::utils::file::write(file_name, to_xml(names, color_maps, put_parent_tag),
true);
92 static bool write_to_png_file(
const std::string& file_name,
const cgv::render::color_map& color_map,
size_t resolution) {
94 std::vector<rgba> data = color_map.interpolate(
static_cast<size_t>(resolution));
96 bool has_opacity = !color_map.ref_opacity_points().empty();
98 std::vector<uint8_t> data_8(4 * data.size());
99 for(
unsigned i = 0; i < data.size(); ++i) {
101 data_8[4 * i + 0] =
static_cast<uint8_t
>(255.0f * col.R());
102 data_8[4 * i + 1] =
static_cast<uint8_t
>(255.0f * col.G());
103 data_8[4 * i + 2] =
static_cast<uint8_t
>(255.0f * col.B());
104 data_8[4 * i + 3] = has_opacity ?
static_cast<uint8_t
>(255.0f * col.alpha()) : 255;