forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
270 lines (233 loc) · 7.54 KB
/
Copy pathmain.c
File metadata and controls
270 lines (233 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
This file is part of darktable,
copyright (c) 2012 johannes hanika, tobias ellinghaus.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* TODO:
* - make --bpp work
* - add options for interpolator
* - make these settings work
* - ???
* - profit
*/
#include "common/darktable.h"
#include "common/debug.h"
#include "common/collection.h"
#include "common/points.h"
#include "common/film.h"
#include "common/image.h"
#include "common/image_cache.h"
#include "common/imageio.h"
#include "common/imageio_module.h"
#include "common/exif.h"
#include "common/history.h"
#include <sys/time.h>
#include <unistd.h>
int usleep(useconds_t usec);
#include <inttypes.h>
#include <libintl.h>
static void
usage(const char* progname)
{
fprintf(stderr, "usage: %s <input file> [<xmp file>] <output file> [--width <max width>,--height <max height>,--bpp <bpp>,--hq <0|1|true|false> --verbose]\n", progname);
}
int main(int argc, char *arg[])
{
bindtextdomain (GETTEXT_PACKAGE, DARKTABLE_LOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
gtk_init (&argc, &arg);
// parse command line arguments
char *image_filename = NULL;
char *xmp_filename = NULL;
char *output_filename = NULL;
int file_counter = 0;
int width = 0, height = 0, bpp = 0;
gboolean verbose = FALSE, high_quality = TRUE;
for(int k=1; k<argc; k++)
{
if(arg[k][0] == '-')
{
if(!strcmp(arg[k], "--help"))
{
usage(arg[0]);
exit(1);
}
else if(!strcmp(arg[k], "--version"))
{
printf("this is darktable-cli\ncopyright (c) 2012-2013 johannes hanika, tobias ellinghaus\n");
exit(1);
}
else if(!strcmp(arg[k], "--width"))
{
k++;
width = MAX(atoi(arg[k]), 0);
}
else if(!strcmp(arg[k], "--height"))
{
k++;
height = MAX(atoi(arg[k]), 0);
}
else if(!strcmp(arg[k], "--bpp"))
{
k++;
bpp = MAX(atoi(arg[k]), 0);
fprintf(stderr, "%s %d\n", _("TODO: sorry, due to api restrictions we currently cannot set the bpp to"), bpp);
}
else if(!strcmp(arg[k], "--hq"))
{
k++;
gchar *str = g_ascii_strup(arg[k], -1);
if(!g_strcmp0(str, "0") || !g_strcmp0(str, "FALSE"))
high_quality = FALSE;
else if(!g_strcmp0(str, "1") || !g_strcmp0(str, "TRUE"))
high_quality = TRUE;
else
{
fprintf(stderr, "%s: %s\n", _("Unknown option for --hq"), arg[k]);
usage(arg[0]);
exit(1);
}
g_free(str);
}
else if(!strcmp(arg[k], "-v") || !strcmp(arg[k], "--verbose"))
{
verbose = TRUE;
}
}
else
{
if(file_counter == 0)
image_filename = arg[k];
else if(file_counter == 1)
xmp_filename = arg[k];
else if(file_counter == 2)
output_filename = arg[k];
file_counter++;
}
}
if(file_counter < 2 || file_counter > 3)
{
usage(arg[0]);
exit(1);
}
else if(file_counter == 2)
{
// no xmp file given
output_filename = xmp_filename;
xmp_filename = NULL;
}
// the output file already exists, so there will be a sequence number added
if(g_file_test(output_filename, G_FILE_TEST_EXISTS))
{
fprintf(stderr, "%s\n", _("output file already exists, it will get renamed"));
}
char *m_arg[] = {"darktable-cli", "--library", ":memory:", NULL};
// init dt without gui:
if(dt_init(3, m_arg, 0,NULL)) exit(1);
dt_film_t film;
int id = 0;
int filmid = 0;
gchar *directory = g_path_get_dirname(image_filename);
filmid = dt_film_new(&film, directory);
id = dt_image_import(filmid, image_filename, TRUE);
if(!id)
{
fprintf(stderr, _("error: can't open file %s"), image_filename);
fprintf(stderr, "\n");
exit(1);
}
g_free(directory);
// attach xmp, if requested:
if(xmp_filename)
{
const dt_image_t *cimg = dt_image_cache_read_get(darktable.image_cache, id);
dt_image_t *image = dt_image_cache_write_get(darktable.image_cache, cimg);
dt_exif_xmp_read(image, xmp_filename, 1);
// don't write new xmp:
dt_image_cache_write_release(darktable.image_cache, image, DT_IMAGE_CACHE_RELAXED);
dt_image_cache_read_release(darktable.image_cache, image);
}
// print the history stack
if(verbose)
{
gchar *history = dt_history_get_items_as_string(id);
if(history)
printf("%s\n", history);
else
printf("[%s]\n", _("empty history stack"));
}
// try to find out the export format from the output_filename
char *ext = output_filename + strlen(output_filename);
while(ext > output_filename && *ext != '.') ext--;
*ext = '\0';
ext++;
if(!strcmp(ext, "jpg"))
ext = "jpeg";
// init the export data structures
int size = 0, dat_size = 0;
dt_imageio_module_format_t *format;
dt_imageio_module_storage_t *storage;
dt_imageio_module_data_t *sdata, *fdata;
storage = dt_imageio_get_storage_by_name("disk"); // only exporting to disk makes sense
if(storage == NULL)
{
fprintf(stderr, "%s\n", _("cannot find disk storage module. please check your installation, something seems to be broken."));
exit(1);
}
sdata = storage->get_params(storage, &size);
if(sdata == NULL)
{
fprintf(stderr, "%s\n", _("failed to get parameters from storage module, aborting export ..."));
exit(1);
}
// and now for the really ugly hacks. don't tell your children about this one or they won't sleep at night any longer ...
g_strlcpy((char*)sdata, output_filename, DT_MAX_PATH_LEN);
// all is good now, the last line didn't happen.
format = dt_imageio_get_format_by_name(ext);
if(format == NULL)
{
fprintf(stderr, _("unknown extension '.%s'"), ext);
fprintf(stderr, "\n");
exit(1);
}
fdata = format->get_params(format, &dat_size);
if(fdata == NULL)
{
fprintf(stderr, "%s\n", _("failed to get parameters from format module, aborting export ..."));
exit(1);
}
uint32_t w,h,fw,fh,sw,sh;
fw=fh=sw=sh=0;
storage->dimension(storage, &sw, &sh);
format->dimension(format, &fw, &fh);
if( sw==0 || fw==0) w=sw>fw?sw:fw;
else w=sw<fw?sw:fw;
if( sh==0 || fh==0) h=sh>fh?sh:fh;
else h=sh<fh?sh:fh;
fdata->max_width = width;
fdata->max_height = height;
fdata->max_width = (w!=0 && fdata->max_width >w)?w:fdata->max_width;
fdata->max_height = (h!=0 && fdata->max_height >h)?h:fdata->max_height;
fdata->style[0] = '\0';
//TODO: add a callback to set the bpp without going through the config
storage->store(storage,sdata, id, format, fdata, 1, 1, high_quality);
// cleanup time
if(storage->finalize_store) storage->finalize_store(storage, sdata);
storage->free_params(storage, sdata);
format->free_params(format, fdata);
dt_cleanup();
}
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-space on;