cgv
Loading...
Searching...
No Matches
color_map_writer.h
1#pragma once
2
3#include <string>
4
5#include <cgv/media/image/image_writer.h>
6#include <cgv/render/color_map.h>
7#include <cgv/utils/file.h>
8
9#include <tinyxml2/tinyxml2.h>
10
11namespace cgv {
12namespace app {
13
15private:
16 static void write_color_map(tinyxml2::XMLPrinter& printer, const std::string& name, const cgv::render::color_map& color_map) {
17
18 printer.OpenElement("ColorMap");
19 printer.PushAttribute("name", name.c_str());
20
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();
28 }
29
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();
35 }
36
37 printer.CloseElement();
38 }
39
40public:
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) {
42
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);
46 }
47
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) {
49
50 if (put_parent_tag)
51 printer.OpenElement("ColorMaps");
52
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]);
56 }
57
58 if (put_parent_tag)
59 printer.CloseElement();
60 }
61
62 static std::string to_xml(const std::string& name, const cgv::render::color_map& color_map, bool put_parent_tag = true) {
63
64 std::vector<std::string> names = { name };
65 std::vector<cgv::render::color_map> color_maps = { color_map };
66
67 return to_xml(names, color_maps, put_parent_tag);
68 }
69
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) {
71
72 tinyxml2::XMLPrinter printer;
73 to_xml_printer(printer, names, color_maps, put_parent_tag);
74 std::string xml = printer.CStr();
75
76 return xml;
77 }
78
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) {
80
81 std::vector<std::string> names = { name };
82 std::vector<cgv::render::color_map> color_maps = { color_map };
83
84 return write_to_xml_file(file_name, names, color_maps, put_parent_tag);
85 }
86
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) {
88
89 return cgv::utils::file::write(file_name, to_xml(names, color_maps, put_parent_tag), true);
90 }
91
92 static bool write_to_png_file(const std::string& file_name, const cgv::render::color_map& color_map, size_t resolution) {
93
94 std::vector<rgba> data = color_map.interpolate(static_cast<size_t>(resolution));
95
96 bool has_opacity = !color_map.ref_opacity_points().empty();
97
98 std::vector<uint8_t> data_8(4 * data.size());
99 for(unsigned i = 0; i < data.size(); ++i) {
100 rgba col = data[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;
105 }
106
108
109 cgv::media::image::image_writer writer(file_name);
110
111 return writer.write_image(dv);
112 }
113};
114
115}
116}
A data_format describes a multidimensional data block of data entries.
Definition data_format.h:17
the data view gives access to a data array of one, two, three or four dimensions.
Definition data_view.h:153
the image writer chooses a specific writer automatically based on the extension of the given file nam...
bool write_image(const cgv::data::const_data_view &dv, const std::vector< cgv::data::const_data_view > *palettes=0, double duration=0)
write the data stored in the data view to a file with the file name given in the constructor.
@ CF_RGBA
color format with components R, G and B
@ TI_UINT8
signed integer stored in 64 bits
Definition type_id.h:23
the cgv namespace
Definition print.h:11