cgv
Loading...
Searching...
No Matches
image_writer.cxx
1#include "image_writer.h"
2#include <cgv/base/register.h>
3#include <cgv/utils/scan.h>
4#include <vector>
5
6using namespace cgv::base;
7using namespace cgv::data;
8using namespace cgv::utils;
9
10namespace cgv {
11 namespace media {
12 namespace image {
13
16{
17 static std::vector<base_ptr>& ref_writers()
18 {
19 static std::vector<base_ptr> writers;
20 return writers;
21 }
22 std::string get_type_name() const
23 {
24 return "image_writer_listener";
25 }
26 void register_object(base_ptr object, const std::string&)
27 {
28 if (object->get_interface<abst_image_writer>())
29 ref_writers().push_back(object);
30 }
31 void unregister_object(base_ptr object, const std::string&)
32 {
33 for (unsigned int i=0; i<ref_writers().size(); ++i) {
34 if (object == ref_writers()[i]) {
35 ref_writers().erase(ref_writers().begin()+i);
36 ++i;
37 }
38 }
39 }
40};
41
44{
45 return false;
46}
47
49image_writer::image_writer(const std::string& _file_name) : file_name(_file_name)
50{
51 is_opened = false;
52 std::string::size_type pos = file_name.find_last_of('.');
53 if (pos == std::string::npos) {
54 last_error = "file name without extension specified";
55 return;
56 }
57 std::string ext = to_lower(file_name.substr(pos+1));
58 std::vector<base_ptr>& writers = writer_listener::ref_writers();
59 for (unsigned int i=0; i<writers.size(); ++i) {
60 abst_image_writer* aiw = writers[i]->get_interface<abst_image_writer>();
61 if (cgv::utils::is_element(ext, aiw->get_supported_extensions())) {
62 wr = aiw->clone();
63 return;
64 }
65 }
66 wr = 0;
67 last_error = "no image image_writer found for extension: ";
68 last_error += ext;
69}
70
73{
74 static std::string exts;
75 exts.clear();
76 std::vector<base_ptr>& writers = writer_listener::ref_writers();
77 for (unsigned int i=0; i<writers.size(); ++i) {
78 if (!exts.empty())
79 exts += ';';
81 }
82 if (sep != ';')
84 return exts;
85}
86
89{
90 std::string text = "Image Files (";
91 std::string exts = "*.";
92 text += get_supported_extensions(',');
93 text += "):";
95 replace(exts,",",";*.");
96 return text+exts;
97}
98
100bool image_writer::is_format_supported(const component_format& cf, const std::vector<component_format>* palette_formats) const
101{
102 if (wr)
103 return wr->is_format_supported(cf, palette_formats);
104 return false;
105}
106
108const std::string& image_writer::get_last_error() const
109{
110 if (!wr)
111 return last_error;
112 return wr->get_last_error();
113}
114
120
123{
124 bool res = false;
125 if (is_opened && wr)
126 res = wr->close();
127 is_opened = false;
128 return res;
129}
130
132bool image_writer::write_image(const const_data_view& dv, const std::vector<const_data_view>* palettes, double duration)
133{
134 if (wr) {
135 if (!is_opened) {
136 if (!wr->open(file_name))
137 return false;
138 is_opened = true;
139 }
140 return wr->write_image(dv, palettes, duration);
141 }
142 return false;
143}
144
145
148{
149 if (wr)
151 return false;
152}
153
156{
157 if (wr)
158 return wr->get_type_name();
159 return "cmi::image_writer";
160}
168
170bool image_writer::set_void(const std::string& property, const std::string& type, const void* value)
171{
172 if (wr)
173 return wr->set_void(property, type, value);
174 return cgv::base::base::set_void(property, type, value);
175}
177bool image_writer::get_void(const std::string& property, const std::string& type, void* value)
178{
179 if (wr)
180 return wr->get_void(property, type, value);
181 return cgv::base::base::get_void(property, type, value);
182}
183
184
185object_registration<writer_listener> wlr("register image writer listener");
186
187 }
188 }
189}
base class for all classes that can be registered with support for dynamic properties (see also secti...
Definition base.h:75
virtual std::string get_property_declarations()
return a semicolon separated list of property declarations
Definition base.cxx:264
virtual bool set_void(const std::string &property, const std::string &value_type, const void *value_ptr)
abstract interface for the setter of a dynamic property.
Definition base.cxx:181
virtual std::string get_type_name() const
overload to return the type name of this object. By default the type interface is queried over get_ty...
Definition base.cxx:241
virtual bool get_void(const std::string &property, const std::string &value_type, void *value_ptr)
abstract interface for the getter of a dynamic property.
Definition base.cxx:215
complete implementation of method actions that only call one method when entering a node
Definition action.h:113
the component format inherits the information of a packing_info and adds information on the component...
The const_data_view has the functionality of the data_view but uses a const pointer and therefore doe...
Definition data_view.h:221
abstract interface for image readers
virtual bool close()=0
close image [stream]
virtual const std::string & get_last_error() const =0
return a reference to the last error message
virtual bool open(const std::string &file_name)=0
open image file to write
virtual bool is_format_supported(const cgv::data::component_format &cf, const std::vector< cgv::data::component_format > *palette_formats) const =0
check if the chosen writer supports the given component format
virtual bool supports_multiple_images() const
return whether multiple images are supported, default implementation returns false
virtual bool write_image(const cgv::data::const_data_view &dv, const std::vector< cgv::data::const_data_view > *palettes, double duration)=0
write one image
bool is_format_supported(const cgv::data::component_format &cf, const std::vector< cgv::data::component_format > *palette_formats=0) const
check if the chosen writer supports the given component format
image_writer(const std::string &file_name)
construct an image writer from a file name and choose a writer from the extension
bool set_void(const std::string &property, const std::string &type, const void *value)
abstract interface for the setter, by default it simply returns false
bool is_opened
whether file has been opened
std::string get_property_declarations()
return a semicolon separated list of property declarations of the form "name:type",...
std::string last_error
store the last error not resulting from writer implementations
std::string file_name
store the file name
abst_image_writer * wr
store a pointer to the chosen reader
bool get_void(const std::string &property, const std::string &type, void *value)
abstract interface for the getter, by default it simply returns false
const std::string & get_last_error() const
return a reference to the last error message
~image_writer()
close file on destruction
bool close()
close image file;
bool supports_multiple_images() const
return whether multiple images are supported, default implementation returns false
static std::string construct_filter_string()
use this to generate a file_open_dialog or file_save_dialog
std::string get_type_name() const
overload to return the type name of this object
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.
static const std::string & get_supported_extensions(char sep=';')
return a string with a list of supported extensions, where the list entries are separated with the pa...
the base namespace holds the base hierarchy, support for plugin registration and signals
Definition action.cxx:4
namespace for data management components
namespace that holds tools that dont fit any other namespace
unsigned int replace(std::string &s, char c1, char c2)
replace char c1 with c2 in the given string _s and return number of replacements
Definition scan.cxx:159
char to_lower(char c)
convert char to lower case
Definition scan.cxx:39
bool is_element(char c, const std::string &s)
check if char c arises in string s
Definition scan.cxx:291
the cgv namespace
Definition print.h:11
Helper functions to process strings.
interfaces that allows to listen to registration events.
Definition register.h:218
interfaces that add provides very basic functionality.
Definition register.h:203
interfaces that allows to listen to registration events
std::string get_type_name() const
overload to return the type name of this object. By default the type interface is queried over get_ty...
void unregister_object(base_ptr object, const std::string &)
overload to handle unregistration events
void register_object(base_ptr object, const std::string &)
overload to handle registration events