~ubuntu-branches/ubuntu/saucy/darktable/saucy

« back to all changes in this revision

Viewing changes to src/common/imageio_module.c

  • Committer: Bazaar Package Importer
  • Author(s): David Bremner
  • Date: 2011-04-14 23:42:12 UTC
  • Revision ID: james.westby@ubuntu.com-20110414234212-kuffcz5wiu18v6ra
Tags: upstream-0.8
ImportĀ upstreamĀ versionĀ 0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This file is part of darktable,
 
3
    copyright (c) 2009--2010 johannes hanika.
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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/>.
 
17
*/
 
18
 
 
19
#include "common/imageio_module.h"
 
20
#include "control/conf.h"
 
21
#include "control/control.h"
 
22
#include <stdlib.h>
 
23
 
 
24
static gint
 
25
dt_imageio_sort_modules_storage (gconstpointer a, gconstpointer b)
 
26
{
 
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());
 
30
}
 
31
 
 
32
static gint
 
33
dt_imageio_sort_modules_format (gconstpointer a, gconstpointer b)
 
34
{
 
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());
 
38
}
 
39
 
 
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)
 
42
{
 
43
  return 0;
 
44
}
 
45
 
 
46
static int
 
47
dt_imageio_load_module_format (dt_imageio_module_format_t *module, const char *libname, const char *plugin_name)
 
48
{
 
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;
 
53
  int (*version)();
 
54
  if(!g_module_symbol(module->module, "dt_module_dt_version", (gpointer)&(version))) goto error;
 
55
  if(version() != dt_version())
 
56
  {
 
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");
 
58
    goto error;
 
59
  }
 
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;
 
64
 
 
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;
 
73
 
 
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;
 
77
 
 
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;
 
80
 
 
81
  return 0;
 
82
error:
 
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);
 
85
  return 1;
 
86
}
 
87
 
 
88
 
 
89
static int
 
90
dt_imageio_load_modules_format(dt_imageio_t *iio)
 
91
{
 
92
  iio->plugins_format = NULL;
 
93
  GList *res = NULL;
 
94
  dt_imageio_module_format_t *module;
 
95
  char plugindir[1024], plugin_name[256];
 
96
  const gchar *d_name;
 
97
  dt_get_plugindir(plugindir, 1024);
 
98
  strcpy(plugindir + strlen(plugindir), "/plugins/imageio/format");
 
99
  GDir *dir = g_dir_open(plugindir, 0, NULL);
 
100
  if(!dir) return 1;
 
101
  while((d_name = g_dir_read_name(dir)))
 
102
  {
 
103
    // get lib*.so
 
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))
 
111
    {
 
112
      free(module);
 
113
      continue;
 
114
    }
 
115
    module->gui_data = NULL;
 
116
    module->gui_init(module);
 
117
    if(module->widget) gtk_widget_ref(module->widget);
 
118
    g_free(libname);
 
119
    res = g_list_insert_sorted(res, module, dt_imageio_sort_modules_format);
 
120
  }
 
121
  g_dir_close(dir);
 
122
  iio->plugins_format = res;
 
123
  return 0;
 
124
}
 
125
 
 
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)
 
128
{
 
129
  return 1;
 
130
}
 
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)
 
133
{
 
134
  return 0;
 
135
}
 
136
 
 
137
static int
 
138
dt_imageio_load_module_storage (dt_imageio_module_storage_t *module, const char *libname, const char *plugin_name)
 
139
{
 
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;
 
144
  int (*version)();
 
145
  if(!g_module_symbol(module->module, "dt_module_dt_version", (gpointer)&(version))) goto error;
 
146
  if(version() != dt_version())
 
147
  {
 
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");
 
149
    goto error;
 
150
  }
 
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;
 
155
 
 
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;
 
161
 
 
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;
 
165
 
 
166
  return 0;
 
167
error:
 
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);
 
170
  return 1;
 
171
}
 
172
 
 
173
static int
 
174
dt_imageio_load_modules_storage (dt_imageio_t *iio)
 
175
{
 
176
  iio->plugins_storage = NULL;
 
177
  GList *res = NULL;
 
178
  dt_imageio_module_storage_t *module;
 
179
  char plugindir[1024], plugin_name[256];
 
180
  const gchar *d_name;
 
181
  dt_get_plugindir(plugindir, 1024);
 
182
  strcpy(plugindir + strlen(plugindir), "/plugins/imageio/storage");
 
183
  GDir *dir = g_dir_open(plugindir, 0, NULL);
 
184
  if(!dir) return 1;
 
185
  while((d_name = g_dir_read_name(dir)))
 
186
  {
 
187
    // get lib*.so
 
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))
 
195
    {
 
196
      free(module);
 
197
      continue;
 
198
    }
 
199
    module->gui_data = NULL;
 
200
    module->gui_init(module);
 
201
    if(module->widget) gtk_widget_ref(module->widget);
 
202
    g_free(libname);
 
203
    res = g_list_insert_sorted(res, module, dt_imageio_sort_modules_storage);
 
204
  }
 
205
  g_dir_close(dir);
 
206
  iio->plugins_storage = res;
 
207
  return 0;
 
208
}
 
209
 
 
210
void
 
211
dt_imageio_init (dt_imageio_t *iio)
 
212
{
 
213
  iio->plugins_format  = NULL;
 
214
  iio->plugins_storage = NULL;
 
215
 
 
216
  dt_imageio_load_modules_format (iio);
 
217
  dt_imageio_load_modules_storage(iio);
 
218
}
 
219
 
 
220
void
 
221
dt_imageio_cleanup (dt_imageio_t *iio)
 
222
{
 
223
  while(iio->plugins_format)
 
224
  {
 
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);
 
228
    free(module);
 
229
    iio->plugins_format = g_list_delete_link(iio->plugins_format, iio->plugins_format);
 
230
  }
 
231
  while(iio->plugins_storage)
 
232
  {
 
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);
 
236
    free(module);
 
237
    iio->plugins_storage = g_list_delete_link(iio->plugins_storage, iio->plugins_storage);
 
238
  }
 
239
}
 
240
 
 
241
dt_imageio_module_format_t *dt_imageio_get_format()
 
242
{
 
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;
 
248
}
 
249
 
 
250
dt_imageio_module_storage_t *dt_imageio_get_storage()
 
251
{
 
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;
 
257
}
 
258
 
 
259
dt_imageio_module_format_t *dt_imageio_get_format_by_name(const char *name)
 
260
{
 
261
  dt_imageio_t *iio = darktable.imageio;
 
262
  GList *it = iio->plugins_format;
 
263
  while(it)
 
264
  {
 
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);
 
268
  }
 
269
  return NULL;
 
270
}
 
271
 
 
272
dt_imageio_module_storage_t *dt_imageio_get_storage_by_name(const char *name)
 
273
{
 
274
  dt_imageio_t *iio = darktable.imageio;
 
275
  GList *it = iio->plugins_storage;
 
276
  while(it)
 
277
  {
 
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);
 
281
  }
 
282
  return NULL;
 
283
}