2
This file is part of darktable,
3
copyright (c) 2009--2010 johannes hanika.
5
darktable is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
10
darktable is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with darktable. If not, see <http://www.gnu.org/licenses/>.
19
#include "common/imageio_module.h"
20
#include "control/conf.h"
21
#include "control/control.h"
25
dt_imageio_sort_modules_storage (gconstpointer a, gconstpointer b)
27
const dt_imageio_module_storage_t *am = (const dt_imageio_module_storage_t *)a;
28
const dt_imageio_module_storage_t *bm = (const dt_imageio_module_storage_t *)b;
29
return strcmp(am->name(), bm->name());
33
dt_imageio_sort_modules_format (gconstpointer a, gconstpointer b)
35
const dt_imageio_module_format_t *am = (const dt_imageio_module_format_t *)a;
36
const dt_imageio_module_format_t *bm = (const dt_imageio_module_format_t *)b;
37
return strcmp(am->name(), bm->name());
40
/** Default implementation of dimension module function, used if format modules does not implements dimension() */
41
int _default_format_dimension(dt_imageio_module_format_t *module, uint32_t *width, uint32_t *height)
47
dt_imageio_load_module_format (dt_imageio_module_format_t *module, const char *libname, const char *plugin_name)
49
module->widget = NULL;
50
strncpy(module->plugin_name, plugin_name, 20);
51
module->module = g_module_open(libname, G_MODULE_BIND_LAZY);
52
if(!module->module) goto error;
54
if(!g_module_symbol(module->module, "dt_module_dt_version", (gpointer)&(version))) goto error;
55
if(version() != dt_version())
57
fprintf(stderr, "[imageio_load_module] `%s' is compiled for another version of dt (module %d (%s) != dt %d (%s)) !\n", libname, abs(version()), version() < 0 ? "debug" : "opt", abs(dt_version()), dt_version() < 0 ? "debug" : "opt");
60
if(!g_module_symbol(module->module, "name", (gpointer)&(module->name))) goto error;
61
if(!g_module_symbol(module->module, "gui_reset", (gpointer)&(module->gui_reset))) goto error;
62
if(!g_module_symbol(module->module, "gui_init", (gpointer)&(module->gui_init))) goto error;
63
if(!g_module_symbol(module->module, "gui_cleanup", (gpointer)&(module->gui_cleanup))) goto error;
65
if(!g_module_symbol(module->module, "mime", (gpointer)&(module->mime))) goto error;
66
if(!g_module_symbol(module->module, "extension", (gpointer)&(module->extension))) goto error;
67
if(!g_module_symbol(module->module, "dimension", (gpointer)&(module->dimension))) module->dimension = _default_format_dimension;
68
if(!g_module_symbol(module->module, "get_params", (gpointer)&(module->get_params))) goto error;
69
if(!g_module_symbol(module->module, "free_params", (gpointer)&(module->free_params))) goto error;
70
if(!g_module_symbol(module->module, "set_params", (gpointer)&(module->set_params))) goto error;
71
if(!g_module_symbol(module->module, "write_image", (gpointer)&(module->write_image))) goto error;
72
if(!g_module_symbol(module->module, "bpp", (gpointer)&(module->bpp))) goto error;
74
if(!g_module_symbol(module->module, "decompress_header", (gpointer)&(module->decompress_header))) module->decompress_header = NULL;
75
if(!g_module_symbol(module->module, "decompress", (gpointer)&(module->decompress))) module->decompress = NULL;
76
if(!g_module_symbol(module->module, "compress", (gpointer)&(module->compress))) module->compress = NULL;
78
if(!g_module_symbol(module->module, "read_header", (gpointer)&(module->read_header))) module->read_header = NULL;
79
if(!g_module_symbol(module->module, "read_image", (gpointer)&(module->read_image))) module->read_image = NULL;
83
fprintf(stderr, "[imageio_load_module] failed to open format `%s': %s\n", plugin_name, g_module_error());
84
if(module->module) g_module_close(module->module);
90
dt_imageio_load_modules_format(dt_imageio_t *iio)
92
iio->plugins_format = NULL;
94
dt_imageio_module_format_t *module;
95
char plugindir[1024], plugin_name[256];
97
dt_get_plugindir(plugindir, 1024);
98
strcpy(plugindir + strlen(plugindir), "/plugins/imageio/format");
99
GDir *dir = g_dir_open(plugindir, 0, NULL);
101
while((d_name = g_dir_read_name(dir)))
104
if(strncmp(d_name, "lib", 3)) continue;
105
if(strncmp(d_name + strlen(d_name) - 3, ".so", 3)) continue;
106
strncpy(plugin_name, d_name+3, strlen(d_name)-6);
107
plugin_name[strlen(d_name)-6] = '\0';
108
module = (dt_imageio_module_format_t *)malloc(sizeof(dt_imageio_module_format_t));
109
gchar *libname = g_module_build_path(plugindir, (const gchar *)plugin_name);
110
if(dt_imageio_load_module_format(module, libname, plugin_name))
115
module->gui_data = NULL;
116
module->gui_init(module);
117
if(module->widget) gtk_widget_ref(module->widget);
119
res = g_list_insert_sorted(res, module, dt_imageio_sort_modules_format);
122
iio->plugins_format = res;
126
/** Default implementation of supported function, used if storage modules not implements supported() */
127
int _default_supported(struct dt_imageio_module_storage_t *self, struct dt_imageio_module_format_t *format)
131
/** Default implementation of dimension module function, used if storage modules does not implements dimension() */
132
int _default_storage_dimension(struct dt_imageio_module_storage_t *self,uint32_t *width, uint32_t *height)
138
dt_imageio_load_module_storage (dt_imageio_module_storage_t *module, const char *libname, const char *plugin_name)
140
module->widget = NULL;
141
strncpy(module->plugin_name, plugin_name, 20);
142
module->module = g_module_open(libname, G_MODULE_BIND_LAZY);
143
if(!module->module) goto error;
145
if(!g_module_symbol(module->module, "dt_module_dt_version", (gpointer)&(version))) goto error;
146
if(version() != dt_version())
148
fprintf(stderr, "[imageio_load_module] `%s' is compiled for another version of dt (module %d (%s) != dt %d (%s)) !\n", libname, abs(version()), version() < 0 ? "debug" : "opt", abs(dt_version()), dt_version() < 0 ? "debug" : "opt");
151
if(!g_module_symbol(module->module, "name", (gpointer)&(module->name))) goto error;
152
if(!g_module_symbol(module->module, "gui_reset", (gpointer)&(module->gui_reset))) goto error;
153
if(!g_module_symbol(module->module, "gui_init", (gpointer)&(module->gui_init))) goto error;
154
if(!g_module_symbol(module->module, "gui_cleanup", (gpointer)&(module->gui_cleanup))) goto error;
156
if(!g_module_symbol(module->module, "store", (gpointer)&(module->store))) goto error;
157
if(!g_module_symbol(module->module, "get_params", (gpointer)&(module->get_params))) goto error;
158
if(!g_module_symbol(module->module, "free_params", (gpointer)&(module->free_params))) goto error;
159
if(!g_module_symbol(module->module, "finalize_store", (gpointer)&(module->finalize_store))) module->finalize_store = NULL;
160
if(!g_module_symbol(module->module, "set_params", (gpointer)&(module->set_params))) goto error;
162
if(!g_module_symbol(module->module, "supported", (gpointer)&(module->supported))) module->supported = _default_supported;
163
if(!g_module_symbol(module->module, "dimension", (gpointer)&(module->dimension))) module->dimension = _default_storage_dimension;
164
if(!g_module_symbol(module->module, "recommended_dimension", (gpointer)&(module->recommended_dimension))) module->recommended_dimension = _default_storage_dimension;
168
fprintf(stderr, "[imageio_load_module] failed to open storage `%s': %s\n", plugin_name, g_module_error());
169
if(module->module) g_module_close(module->module);
174
dt_imageio_load_modules_storage (dt_imageio_t *iio)
176
iio->plugins_storage = NULL;
178
dt_imageio_module_storage_t *module;
179
char plugindir[1024], plugin_name[256];
181
dt_get_plugindir(plugindir, 1024);
182
strcpy(plugindir + strlen(plugindir), "/plugins/imageio/storage");
183
GDir *dir = g_dir_open(plugindir, 0, NULL);
185
while((d_name = g_dir_read_name(dir)))
188
if(strncmp(d_name, "lib", 3)) continue;
189
if(strncmp(d_name + strlen(d_name) - 3, ".so", 3)) continue;
190
strncpy(plugin_name, d_name+3, strlen(d_name)-6);
191
plugin_name[strlen(d_name)-6] = '\0';
192
module = (dt_imageio_module_storage_t *)malloc(sizeof(dt_imageio_module_storage_t));
193
gchar *libname = g_module_build_path(plugindir, (const gchar *)plugin_name);
194
if(dt_imageio_load_module_storage(module, libname, plugin_name))
199
module->gui_data = NULL;
200
module->gui_init(module);
201
if(module->widget) gtk_widget_ref(module->widget);
203
res = g_list_insert_sorted(res, module, dt_imageio_sort_modules_storage);
206
iio->plugins_storage = res;
211
dt_imageio_init (dt_imageio_t *iio)
213
iio->plugins_format = NULL;
214
iio->plugins_storage = NULL;
216
dt_imageio_load_modules_format (iio);
217
dt_imageio_load_modules_storage(iio);
221
dt_imageio_cleanup (dt_imageio_t *iio)
223
while(iio->plugins_format)
225
dt_imageio_module_format_t *module = (dt_imageio_module_format_t *)(iio->plugins_format->data);
226
if(module->widget) gtk_widget_unref(module->widget);
227
if(module->module) g_module_close(module->module);
229
iio->plugins_format = g_list_delete_link(iio->plugins_format, iio->plugins_format);
231
while(iio->plugins_storage)
233
dt_imageio_module_storage_t *module = (dt_imageio_module_storage_t *)(iio->plugins_storage->data);
234
if(module->widget) gtk_widget_unref(module->widget);
235
if(module->module) g_module_close(module->module);
237
iio->plugins_storage = g_list_delete_link(iio->plugins_storage, iio->plugins_storage);
241
dt_imageio_module_format_t *dt_imageio_get_format()
243
dt_imageio_t *iio = darktable.imageio;
244
int k = dt_conf_get_int ("plugins/lighttable/export/format");
245
GList *it = g_list_nth(iio->plugins_format, k);
246
if(!it) it = iio->plugins_format;
247
return (dt_imageio_module_format_t *)it->data;
250
dt_imageio_module_storage_t *dt_imageio_get_storage()
252
dt_imageio_t *iio = darktable.imageio;
253
int k = dt_conf_get_int ("plugins/lighttable/export/storage");
254
GList *it = g_list_nth(iio->plugins_storage, k);
255
if(!it) it = iio->plugins_storage;
256
return (dt_imageio_module_storage_t *)it->data;
259
dt_imageio_module_format_t *dt_imageio_get_format_by_name(const char *name)
261
dt_imageio_t *iio = darktable.imageio;
262
GList *it = iio->plugins_format;
265
dt_imageio_module_format_t *module = (dt_imageio_module_format_t *)it->data;
266
if(!strcmp(module->plugin_name, name)) return module;
267
it = g_list_next(it);
272
dt_imageio_module_storage_t *dt_imageio_get_storage_by_name(const char *name)
274
dt_imageio_t *iio = darktable.imageio;
275
GList *it = iio->plugins_storage;
278
dt_imageio_module_storage_t *module = (dt_imageio_module_storage_t *)it->data;
279
if(!strcmp(module->plugin_name, name)) return module;
280
it = g_list_next(it);