loaders: PNG: Handle gamma on 16bpp conversion
[gfxprim.git] / demos / spiv / spiv_config.c
blob73db91bc57eb2a81f924f6d7766983d8f9f6daf2
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2014 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "cfg.h"
28 #include "image_actions.h"
29 #include "spiv_config.h"
32 * These are default config values, you can hardcompile yours here.
34 struct spiv_config config = {
35 .slideshow_delay = 0,
36 .show_info = 0,
37 .backend_init = "X11",
38 .emul_type = GP_PIXEL_UNKNOWN,
39 .zoom_strategy = ZOOM_IMAGE_DOWNSCALE,
40 .win_strategy = ZOOM_WIN_FIXED,
41 .full_screen = 0,
42 .exif_autorotate = 1,
43 .max_win_w = 1024,
44 .max_win_h = 768,
46 .font_path = NULL,
47 .font_height = 12,
50 static int set_zoom_strategy(struct cfg_opt *self, unsigned int lineno)
52 switch (self->opt) {
53 case 'w':
54 switch (self->val[0]) {
55 case 'r':
56 case 'R':
57 config.win_strategy = ZOOM_WIN_RESIZABLE;
58 return 0;
59 case 'f':
60 case 'F':
61 config.win_strategy = ZOOM_WIN_FIXED;
62 return 0;
64 break;
65 case 'z':
66 switch (self->val[0]) {
67 case 'n':
68 case 'N':
69 config.zoom_strategy = 0;
70 return 0;
71 case 'u':
72 case 'U':
73 config.zoom_strategy = ZOOM_IMAGE_UPSCALE;
74 return 0;
75 case 'd':
76 case 'D':
77 config.zoom_strategy = ZOOM_IMAGE_DOWNSCALE;
78 return 0;
79 case 'b':
80 case 'B':
81 config.zoom_strategy = ZOOM_IMAGE_UPSCALE |
82 ZOOM_IMAGE_DOWNSCALE;
83 return 0;
85 break;
88 fprintf(stderr, "ERROR: %u: Invalid zoom strategy '%s'\n",
89 lineno, self->val);
90 return 1;
93 static int set_win_max_size(struct cfg_opt *self, unsigned int lineno)
95 unsigned int w, h;
97 if (sscanf(self->val, "%ux%u", &w, &h) != 2) {
98 fprintf(stderr, "ERROR: %u: Invalid max window size '%s'\n",
99 lineno, self->val);
100 return 1;
103 config.max_win_w = w;
104 config.max_win_h = h;
105 return 0;
108 static int set_action(struct cfg_opt *self, unsigned int lineno)
110 (void) lineno;
111 image_action_set(atoi(self->key), self->val);
112 return 0;
115 static int set_opt(struct cfg_opt *self, unsigned int lineno)
117 (void) self;
118 (void) lineno;
120 switch (self->opt) {
121 case 'd':
122 config.floyd_steinberg = 1;
123 break;
124 case 'f':
125 config.full_screen = 1;
126 break;
127 case 'i':
128 config.show_info = 1;
129 break;
130 case 'p':
131 config.show_progress = 1;
132 break;
133 case 't':
134 config.timers = 1;
135 break;
136 case 'r':
137 config.exif_autorotate = 0;
138 break;
141 return 0;
144 static int set_orientation(struct cfg_opt *self, unsigned int lineno)
146 if (!strcmp("0", self->val)) {
147 config.orientation = ROTATE_0;
148 return 0;
151 if (!strcmp("90", self->val)) {
152 config.orientation = ROTATE_90;
153 return 0;
156 if (!strcmp("180", self->val)) {
157 config.orientation = ROTATE_180;
158 return 0;
161 if (!strcmp("270", self->val)) {
162 config.orientation = ROTATE_270;
163 return 0;
166 fprintf(stderr, "ERROR: %u: Invalid orientation '%s'\n",
167 lineno, self->val);
168 return 1;
171 static int set_backend_init(struct cfg_opt *self, unsigned int lineno)
173 if (strlen(self->val) + 1 >= sizeof(config.backend_init)) {
174 fprintf(stderr, "ERROR: %u: Backend init string too long\n",
175 lineno);
176 return 1;
179 strcpy(config.backend_init, self->val);
181 return 0;
184 static int set_slideshow(struct cfg_opt *self, unsigned int lineno)
186 config.slideshow_delay = atof(self->val);
188 if (config.slideshow_delay == 0) {
189 fprintf(stderr, "ERROR: %u: Invalid slideshow delay '%s'\n",
190 lineno, self->val);
191 return 1;
194 return 0;
197 static int set_emulation(struct cfg_opt *self, unsigned int lineno)
199 config.emul_type = GP_PixelTypeByName(optarg);
201 if (config.emul_type == GP_PIXEL_UNKNOWN) {
202 fprintf(stderr, "ERROR: %u: Invalid pixel type '%s'\n",
203 lineno, self->val);
204 return 1;
207 return 0;
210 int load_font_face(GP_TextStyle *style, const char *path, unsigned int height,
211 unsigned int lineno)
213 GP_FontFace *font;
214 static GP_FontFace *old_font = NULL;
216 font = GP_FontFaceLoad(path, 0, height);
218 if (!font) {
219 fprintf(stderr, "ERROR: %u: Failed to load font '%s'\n",
220 lineno, path);
221 return 1;
224 GP_FontFaceFree(old_font);
225 style->font = old_font = font;
227 return 0;
230 static int set_font(struct cfg_opt *self, unsigned int lineno)
232 static GP_TextStyle style = {NULL, 0, 0, 1, 1, 0};
234 if (load_font_face(&style, self->val, config.font_height, lineno))
235 return 1;
237 free(config.font_path);
238 config.font_path = strdup(self->val);
239 config.style = &style;
241 return 0;
244 static int set_font_height(struct cfg_opt *self, unsigned int lineno)
246 int height = atoi(self->val);
248 if (height <= 0) {
249 fprintf(stderr, "ERROR: %u: Wrong font height '%s'\n",
250 lineno, self->val);
251 return 1;
254 config.font_height = height;
256 if (config.style) {
257 return load_font_face(config.style, config.font_path,
258 config.font_height, lineno);
261 return 0;
264 static int help(struct cfg_opt *self, unsigned int lineno)
266 (void) self;
267 (void) lineno;
269 print_help();
270 exit(0);
273 static int man(struct cfg_opt *self, unsigned int lineno)
275 (void) self;
276 (void) lineno;
278 print_man();
279 exit(0);
282 struct cfg_opt spiv_opts[] = {
283 {.name_space = NULL,
284 .key = NULL,
285 .opt = 'h',
286 .opt_long = "help",
287 .opt_has_value = 0,
288 .set = help,
289 .help = "Shows this help",
292 {.name_space = "Gui",
293 .key = "ShowInfo",
294 .opt = 'i',
295 .opt_long = "show-info",
296 .opt_has_value = 0,
297 .set = set_opt,
298 .help = "Show image info such as filename, size, etc...",
300 {.name_space = "Gui",
301 .key = "FontPath",
302 .opt_long = "font-path",
303 .opt_has_value = 1,
304 .set = set_font,
305 .help = "Path to TTF font to be used in GUI",
307 {.name_space = "Gui",
308 .key = "FontHeight",
309 .opt_long = "font-height",
310 .opt_has_value = 1,
311 .set = set_font_height,
312 .help = "TTF font height in pixels",
314 {.name_space = "Gui",
315 .key = "ShowProgress",
316 .opt = 'p',
317 .opt_long = "show-progress",
318 .set = set_opt,
319 .help = "Show progress bar when loading/resampling/... images",
321 {.name_space = "Gui",
322 .key = "SlideshowDelay",
323 .opt = 's',
324 .opt_long = "slideshow-delay",
325 .opt_has_value = 1,
326 .set = set_slideshow,
327 .help = "Delay between images in seconds (float) for slideshow",
329 {.name_space = "Gui",
330 .key = "Dithering",
331 .opt = 'd',
332 .opt_long = "dithering",
333 .opt_has_value = 0,
334 .set = set_opt,
335 .help = "Turn on Floyd-Steinberg dithering",
337 {.name_space = "Gui",
338 .key = "Orientation",
339 .opt = 'o',
340 .opt_long = "orientation",
341 .opt_has_value = 1,
342 .set = set_orientation,
343 .help = "Orientation, one of 0, 90, 180, 270",
345 {.name_space = "Gui",
346 .key = "DisableExifAutorotate",
347 .opt = 'r',
348 .opt_long = "disable_exif_autorotate",
349 .opt_has_value = 0,
350 .set = set_opt,
351 .help = "Disables automatic rotation by EXIF",
353 {.name_space = "Gui",
354 .key = "FullScreen",
355 .opt = 'f',
356 .opt_long = "full-screen",
357 .opt_has_value = 0,
358 .set = set_opt,
359 .help = "Start fullscreen.",
361 {.name_space = "Gui",
362 .key = "BackendInit",
363 .opt = 'b',
364 .opt_long = "backend-init",
365 .opt_has_value = 1,
366 .set = set_backend_init,
367 .help = "Backend init string, set it to 'help' for more info",
370 {.name_space = "Zoom",
371 .key = "WindowSize",
372 .opt = 'w',
373 .opt_long = "window-size",
374 .opt_has_value = 1,
375 .set = set_zoom_strategy,
376 .help = "Window size, resizeable (-wr) or fixed (-wf)",
378 {.name_space = "Zoom",
379 .key = "ZoomStrategy",
380 .opt = 'z',
381 .opt_long = "zoom-strategy",
382 .opt_has_value = 1,
383 .set = set_zoom_strategy,
384 .help = "Zoom strategy, none (-zn), upscale (-zu), "
385 "downscale (-zd) or both (-zb)",
387 {.name_space = "Zoom",
388 .key = "MaxWinSize",
389 .opt = 'm',
390 .opt_long = "max-win-size",
391 .opt_has_value = 1,
392 .set = set_win_max_size,
393 .help = "Window maximal size, 800x600 for example",
397 {.name_space = "Actions",
398 .key = "1",
399 .opt = '1',
400 .opt_long = "action-1",
401 .opt_has_value = 1,
402 .set = set_action,
404 {.name_space = "Actions",
405 .key = "2",
406 .opt = '2',
407 .opt_long = "action-2",
408 .opt_has_value = 1,
409 .set = set_action,
411 {.name_space = "Actions",
412 .key = "3",
413 .opt = '3',
414 .opt_long = "action-3",
415 .opt_has_value = 1,
416 .set = set_action,
418 {.name_space = "Actions",
419 .key = "4",
420 .opt = '4',
421 .opt_long = "action-4",
422 .opt_has_value = 1,
423 .set = set_action,
425 {.name_space = "Actions",
426 .key = "5",
427 .opt = '5',
428 .opt_long = "action-5",
429 .opt_has_value = 1,
430 .set = set_action,
432 {.name_space = "Actions",
433 .key = "6",
434 .opt = '6',
435 .opt_long = "action-6",
436 .opt_has_value = 1,
437 .set = set_action,
439 {.name_space = "Actions",
440 .key = "7",
441 .opt = '7',
442 .opt_long = "action-7",
443 .opt_has_value = 1,
444 .set = set_action,
446 {.name_space = "Actions",
447 .key = "8",
448 .opt = '8',
449 .opt_long = "action-8",
450 .opt_has_value = 1,
451 .set = set_action,
453 {.name_space = "Actions",
454 .key = "9",
455 .opt = '9',
456 .opt_long = "action-9",
457 .opt_has_value = 1,
458 .set = set_action,
460 {.name_space = "Actions",
461 .key = "10",
462 .opt = '0',
463 .opt_long = "action-10",
464 .opt_has_value = 1,
465 .set = set_action,
466 .help = "Sets command line for action 1-10",
469 {.name_space = "Devel",
470 .key = "Timers",
471 .opt = 't',
472 .opt_long = "timers",
473 .opt_has_value = 0,
474 .set = set_opt,
475 .help = "Turns on cpu and wall clock measurement (printed to stdout)",
477 {.name_space = "Devel",
478 .key = "BackendEmulation",
479 .opt = 'e',
480 .opt_long = "backend-emulation",
481 .opt_has_value = 1,
482 .set = set_emulation,
483 .help = "Emulate different backend pixel type (G1, G2, RGB555, ...)",
485 {.name_space = "Devel",
486 .key = NULL,
487 .opt_long = "print-man",
488 .opt_has_value = 0,
489 .set = man,
490 .help = "Prints spiv man page to stdout",
493 {NULL}
496 int spiv_config_load(const char *path)
498 return cfg_load(spiv_opts, path);
501 int spiv_config_parse_args(int argc, char *argv[])
503 return cfg_getopt(spiv_opts, argc, argv);
506 void spiv_config_print_help(void)
508 cfg_print_help(spiv_opts);
511 void spiv_config_print_man(void)
513 cfg_print_man(spiv_opts);