Moved color model settings and optical flow settings to a single *_settings file
[colorize.git] / src / settings.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "mark.h"
4 #include "opticalflow.h"
5 #include "yuv.h"
6 #include "settings.h"
7
8 enum setting_type {
9         SETTING_DOUBLE,
10         SETTING_INT,
11 };
12
13 struct settings {
14         const char *keyword;
15         enum setting_type type;
16         void *variable;
17 };
18
19 static struct settings settings[] = {
20         { "blacklevel", SETTING_DOUBLE, &black_level },
21         { "whitelevel", SETTING_DOUBLE, &white_level },
22         { "fadelevel", SETTING_DOUBLE, &fade_level },
23         { "yuv_mod", SETTING_INT, &yuv_mod },
24 #ifdef WITH_OPENCV
25         { "flow_enable", SETTING_INT, &flow_enable },
26         { "flow_window", SETTING_INT, &flow_window },
27         { "flow_view_vector", SETTING_INT, &flow_view_vector },
28         { "flow_view_uv", SETTING_INT, &flow_view_uv },
29 #endif
30         { NULL, 0, NULL }
31 };
32
33 /* save settings */
34 void save_settings(const char *filename)
35 {
36         char name[256];
37         FILE *fp;
38         int i;
39
40         sprintf(name, "%s_settings", filename);
41         fp = fopen(name, "w");
42         if (!fp) {
43                 printf("failed to save settings '%s'\n", name);
44                 return;
45         }
46         for (i = 0; settings[i].keyword; i++) {
47                 switch (settings[i].type) {
48                 case SETTING_DOUBLE:
49                         fprintf(fp, "%s %f\n", settings[i].keyword, *((double *)settings[i].variable));
50                         break;
51                 case SETTING_INT:
52                         fprintf(fp, "%s %d\n", settings[i].keyword, *((int *)settings[i].variable));
53                         break;
54                 }
55         }
56         fclose(fp);
57 }
58
59 /* load settings */
60 int _load_settings(const char *filename, const char *suffix)
61 {
62         char name[256];
63         char line[256], *p;
64         FILE *fp;
65         int i;
66         int rc = 0;
67
68         sprintf(name, "%s_%s", filename, suffix);
69         fp = fopen(name, "r");
70         if (!fp) {
71                 return -1;
72         }
73         while (fgets(line, sizeof(line), fp)) {
74                 line[sizeof(line)-1] = '\0';
75                 if (line[0]) line[strlen(line)-1] = '\0';
76                 if (line[0] && line[strlen(line)-1] == '\r') line[strlen(line)-1] = '\0';
77
78                 for (i = 0; settings[i].keyword; i++) {
79                         if (!!strncmp(line, settings[i].keyword, strlen(settings[i].keyword)))
80                                 continue;
81                         if (line[strlen(settings[i].keyword)] != ' ')
82                                 continue;
83                         p = line + strlen(settings[i].keyword) + 1;
84                         switch (settings[i].type) {
85                         case SETTING_DOUBLE:
86                                 sscanf(p, "%lf", (double *)settings[i].variable);
87                                 rc = 1;
88                                 break;
89                         case SETTING_INT:
90                                 sscanf(p, "%d", (int *)settings[i].variable);
91                                 rc = 1;
92                                 break;
93                         }
94                 }
95         }
96         fclose(fp);
97
98         return rc;
99 }
100
101 int load_settings(const char *filename)
102 {
103         int rc;
104
105         rc = _load_settings(filename, "palette");
106         if (rc > 0) {
107                 printf("Keywords in file '%s_palette' have moved. After saving, they are moved to *_settings.\n", filename);
108         }
109 #ifdef WITH_OPENCV
110         rc = _load_settings(filename, "opticalflow");
111         if (rc >= 0) {
112                 printf("File '%s_opticalflow' is obsolete. After saving, keywords are moved to *_settings and you can remove it.\n", filename);
113         }
114 #endif
115         rc = _load_settings(filename, "settings");
116
117         return rc;
118 }