From b66f395c86e039f8fb249083c0429fced7724509 Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Thu, 11 Jun 2015 17:00:25 +0200 Subject: [PATCH] Moved color model settings and optical flow settings to a single *_settings file --- gui/Makefile.am | 3 +- gui/{level.c => diag_level.c} | 2 +- gui/{level.h => diag_level.h} | 0 gui/main.c | 2 +- gui/menu.c | 3 +- gui/timeline.c | 9 ++-- gui/toolbar.c | 2 +- src/Makefile.am | 1 + src/colorize.c | 9 +++- src/mark.c | 26 ---------- src/opticalflow.c | 65 ----------------------- src/opticalflow.h | 2 - src/settings.c | 118 ++++++++++++++++++++++++++++++++++++++++++ src/settings.h | 3 ++ 14 files changed, 139 insertions(+), 106 deletions(-) rename gui/{level.c => diag_level.c} (99%) rename gui/{level.h => diag_level.h} (100%) create mode 100644 src/settings.c create mode 100644 src/settings.h diff --git a/gui/Makefile.am b/gui/Makefile.am index 096e80d..bf053c7 100644 --- a/gui/Makefile.am +++ b/gui/Makefile.am @@ -22,12 +22,13 @@ colorize_gtk_SOURCES = \ ../src/yuv.c \ ../src/mark.c \ ../src/process.c \ + ../src/settings.c \ image.c \ palette.c \ timeline.c \ colorize.c \ diag_bc.c \ - level.c \ + diag_level.c \ fill.c \ buttons/b_size_1.c buttons/b_size_3.c buttons/b_size_5.c buttons/b_size_9.c \ buttons/b_size_11.c buttons/b_size_19.c buttons/b_move.c buttons/b_pick.c \ diff --git a/gui/level.c b/gui/diag_level.c similarity index 99% rename from gui/level.c rename to gui/diag_level.c index 29135ce..22b4389 100644 --- a/gui/level.c +++ b/gui/diag_level.c @@ -5,7 +5,7 @@ #include #include #include "main.h" -#include "level.h" +#include "diag_level.h" #include "../src/mark.h" #include "../src/yuv.h" #include "palette.h" diff --git a/gui/level.h b/gui/diag_level.h similarity index 100% rename from gui/level.h rename to gui/diag_level.h diff --git a/gui/main.c b/gui/main.c index 1602d24..4a04a8e 100644 --- a/gui/main.c +++ b/gui/main.c @@ -15,7 +15,7 @@ #include "timeline.h" #include "colorize.h" #include "diag_bc.h" -#include "level.h" +#include "diag_level.h" #include "fill.h" #ifdef WITH_OPENCV #include "diag_flow.h" diff --git a/gui/menu.c b/gui/menu.c index c946f74..41944ab 100644 --- a/gui/menu.c +++ b/gui/menu.c @@ -15,7 +15,7 @@ #include "timeline.h" #include "colorize.h" #include "diag_bc.h" -#include "level.h" +#include "diag_level.h" #include "fill.h" #ifdef WITH_OPENCV #include "diag_flow.h" @@ -71,6 +71,7 @@ static void open_event(gpointer priv) static void save_event(gpointer priv) { + anything_modified = 1; timeline_select_and_save(timeline_selected, timeline_selected); } diff --git a/gui/timeline.c b/gui/timeline.c index 1431182..c6590f0 100644 --- a/gui/timeline.c +++ b/gui/timeline.c @@ -8,6 +8,7 @@ #include "image.h" #include "main.h" #include "palette.h" +#include "../src/settings.h" #include "../src/dir_seperator.h" #ifdef WITH_OPENCV #include "../src/opticalflow.h" @@ -206,12 +207,10 @@ single_file: if (frame_list) { int i; + load_settings(frame_list[0].filename); load_palette(frame_list[0].filename); for (i = 0; i < 255; i++) update_color(i); -#ifdef WITH_OPENCV - load_flow(frame_list[0].filename); -#endif } } @@ -279,11 +278,9 @@ int timeline_select_and_save(int old_frame, int new_frame) if (frame_list) { save_palette(frame_list[0].filename); -#ifdef WITH_OPENCV if (timeline_frames > 1) - save_flow(frame_list[0].filename); + save_settings(frame_list[0].filename); save_sequence(); -#endif } } diff --git a/gui/toolbar.c b/gui/toolbar.c index c991e58..6cd8c23 100644 --- a/gui/toolbar.c +++ b/gui/toolbar.c @@ -15,7 +15,7 @@ #include "timeline.h" #include "colorize.h" #include "diag_bc.h" -#include "level.h" +#include "diag_level.h" #include "fill.h" #ifdef WITH_OPENCV #include "diag_flow.h" diff --git a/src/Makefile.am b/src/Makefile.am index 15d80af..12828c1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,6 +20,7 @@ bin_PROGRAMS = \ colorize_SOURCES = \ colorize.c \ + settings.c \ process.c \ img.c \ yuv.c \ diff --git a/src/colorize.c b/src/colorize.c index 6198e1d..4190854 100644 --- a/src/colorize.c +++ b/src/colorize.c @@ -11,6 +11,7 @@ #include "yuv.h" #include "mark.h" #include "process.h" +#include "settings.h" #include "dir_seperator.h" #ifdef WITH_OPENCV #include "opticalflow.h" @@ -425,9 +426,13 @@ next_sequence: } #ifdef WITH_OPENCV // load flow settings - if (sequence && optical_flow) { + if (sequence && optical_flow) flow_default(); - load_flow(first_filename); +#endif + load_settings(first_filename); +#ifdef WITH_OPENCV + // load flow settings + if (sequence && optical_flow) { if (flow_enable == 0 && (test == FLOW_NEXT || test == FLOW_PREV)) { fprintf(stderr, "Cannot test optical flow, because it is not enabled by GUI.\n"); exit (0); diff --git a/src/mark.c b/src/mark.c index a5a91fa..687837a 100644 --- a/src/mark.c +++ b/src/mark.c @@ -27,10 +27,6 @@ void save_palette(const char *filename) } for (i = 0; i < 255; i++) fprintf(fp, "%03d %03d %03d \"%s\" %f %f %f\n", mark_palette[i].r, mark_palette[i].g, mark_palette[i].b, mark_palette[i].name, mark_palette[i].bright, mark_palette[i].contrast, mark_palette[i].alpha); - fprintf(fp, "blacklevel %f\n", black_level); - fprintf(fp, "whitelevel %f\n", white_level); - fprintf(fp, "fadelevel %f\n", fade_level); - fprintf(fp, "yuv_mod %d\n", yuv_mod); fclose(fp); } @@ -58,28 +54,6 @@ int load_palette(const char *filename) if (line[0]) line[strlen(line)-1] = '\0'; if (line[0] && line[strlen(line)-1] == '\r') line[strlen(line)-1] = '\0'; - /* check for special attrs */ - if (!strncmp(line, "blacklevel ", 11)) { - p = line + 11; - sscanf(p, "%lf", &black_level); - continue; - } - if (!strncmp(line, "whitelevel ", 11)) { - p = line + 11; - sscanf(p, "%lf", &white_level); - continue; - } - if (!strncmp(line, "fadelevel ", 10)) { - p = line + 10; - sscanf(p, "%lf", &fade_level); - continue; - } - if (!strncmp(line, "yuv_mod ", 8)) { - p = line + 8; - sscanf(p, "%d", &yuv_mod); - continue; - } - /* read palette */ if (i == 256) break; diff --git a/src/opticalflow.c b/src/opticalflow.c index 9a9b29d..72f9559 100644 --- a/src/opticalflow.c +++ b/src/opticalflow.c @@ -16,71 +16,6 @@ void flow_default(void) flow_view_uv = 0; /* disabled */ } -/* save flow setting */ -void save_flow(const char *filename) -{ - char name[256]; - FILE *fp; - - sprintf(name, "%s_opticalflow", filename); - fp = fopen(name, "w"); - if (!fp) { - printf("failed to save flow setting '%s'\n", name); - return; - } - fprintf(fp, "flow_enable %d\n", flow_enable); - fprintf(fp, "flow_window %d\n", flow_window); - fprintf(fp, "flow_view_vector %d\n", flow_view_vector); - fprintf(fp, "flow_view_uv %d\n", flow_view_uv); - fclose(fp); -} - -/* load flow setting */ -int load_flow(const char *filename) -{ - char name[256]; - char line[256],*p; - FILE *fp; - int i; - - sprintf(name, "%s_opticalflow", filename); - fp = fopen(name, "r"); - if (!fp) { - printf("flow setting '%s' does not (yet) exists\n", name); - return -1; - } - for (i = 0; ; i++) { - if (!fgets(line, sizeof(line), fp)) - break; - line[sizeof(line)-1] = '\0'; - if (line[0]) line[strlen(line)-1] = '\0'; - if (line[0] && line[strlen(line)-1] == '\r') line[strlen(line)-1] = '\0'; - - if (!strncmp(line, "flow_enable ", 12)) { - p = line + 12; - sscanf(p, "%d", &flow_enable); - continue; - } - if (!strncmp(line, "flow_window ", 12)) { - p = line + 12; - sscanf(p, "%d", &flow_window); - continue; - } - if (!strncmp(line, "flow_view_vector ", 17)) { - p = line + 17; - sscanf(p, "%d", &flow_view_vector); - continue; - } - if (!strncmp(line, "flow_view_uv ", 13)) { - p = line + 13; - sscanf(p, "%d", &flow_view_uv); - continue; - } - } - fclose(fp); - return 0; -} - /* draw flow matrix as lines */ static void draw_lines_from_matrix(CvMat *flow, int width, int height, int steps, int color, IplImage *image) { diff --git a/src/opticalflow.h b/src/opticalflow.h index 59be9eb..338c8a4 100644 --- a/src/opticalflow.h +++ b/src/opticalflow.h @@ -1,8 +1,6 @@ extern int flow_enable, flow_window, flow_levels, flow_iter, flow_view_vector, flow_view_uv; void flow_default(void); -void save_flow(const char *filename); -int load_flow(const char *filename); void *create_flow_maps(const double *img_prev_buffer, const double *img_next_buffer, const double *img_buffer, int width, int height, int win_size, int steps, double *flow_map_x_prev, double *flow_map_y_prev, double *flow_map_x_next, double *flow_map_y_next, void *_image_preview); diff --git a/src/settings.c b/src/settings.c new file mode 100644 index 0000000..afa0155 --- /dev/null +++ b/src/settings.c @@ -0,0 +1,118 @@ +#include +#include +#include "mark.h" +#include "opticalflow.h" +#include "yuv.h" +#include "settings.h" + +enum setting_type { + SETTING_DOUBLE, + SETTING_INT, +}; + +struct settings { + const char *keyword; + enum setting_type type; + void *variable; +}; + +static struct settings settings[] = { + { "blacklevel", SETTING_DOUBLE, &black_level }, + { "whitelevel", SETTING_DOUBLE, &white_level }, + { "fadelevel", SETTING_DOUBLE, &fade_level }, + { "yuv_mod", SETTING_INT, &yuv_mod }, +#ifdef WITH_OPENCV + { "flow_enable", SETTING_INT, &flow_enable }, + { "flow_window", SETTING_INT, &flow_window }, + { "flow_view_vector", SETTING_INT, &flow_view_vector }, + { "flow_view_uv", SETTING_INT, &flow_view_uv }, +#endif + { NULL, 0, NULL } +}; + +/* save settings */ +void save_settings(const char *filename) +{ + char name[256]; + FILE *fp; + int i; + + sprintf(name, "%s_settings", filename); + fp = fopen(name, "w"); + if (!fp) { + printf("failed to save settings '%s'\n", name); + return; + } + for (i = 0; settings[i].keyword; i++) { + switch (settings[i].type) { + case SETTING_DOUBLE: + fprintf(fp, "%s %f\n", settings[i].keyword, *((double *)settings[i].variable)); + break; + case SETTING_INT: + fprintf(fp, "%s %d\n", settings[i].keyword, *((int *)settings[i].variable)); + break; + } + } + fclose(fp); +} + +/* load settings */ +int _load_settings(const char *filename, const char *suffix) +{ + char name[256]; + char line[256], *p; + FILE *fp; + int i; + int rc = 0; + + sprintf(name, "%s_%s", filename, suffix); + fp = fopen(name, "r"); + if (!fp) { + return -1; + } + while (fgets(line, sizeof(line), fp)) { + line[sizeof(line)-1] = '\0'; + if (line[0]) line[strlen(line)-1] = '\0'; + if (line[0] && line[strlen(line)-1] == '\r') line[strlen(line)-1] = '\0'; + + for (i = 0; settings[i].keyword; i++) { + if (!!strncmp(line, settings[i].keyword, strlen(settings[i].keyword))) + continue; + if (line[strlen(settings[i].keyword)] != ' ') + continue; + p = line + strlen(settings[i].keyword) + 1; + switch (settings[i].type) { + case SETTING_DOUBLE: + sscanf(p, "%lf", (double *)settings[i].variable); + rc = 1; + break; + case SETTING_INT: + sscanf(p, "%d", (int *)settings[i].variable); + rc = 1; + break; + } + } + } + fclose(fp); + + return rc; +} + +int load_settings(const char *filename) +{ + int rc; + + rc = _load_settings(filename, "palette"); + if (rc > 0) { + printf("Keywords in file '%s_palette' have moved. After saving, they are moved to *_settings.\n", filename); + } +#ifdef WITH_OPENCV + rc = _load_settings(filename, "opticalflow"); + if (rc >= 0) { + printf("File '%s_opticalflow' is obsolete. After saving, keywords are moved to *_settings and you can remove it.\n", filename); + } +#endif + rc = _load_settings(filename, "settings"); + + return rc; +} diff --git a/src/settings.h b/src/settings.h new file mode 100644 index 0000000..cd6c478 --- /dev/null +++ b/src/settings.h @@ -0,0 +1,3 @@ +void save_settings(const char *filename); +int load_settings(const char *filename); + -- 2.13.6