From 00589a72953dff4b907144f53eae588f27c942f0 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Sat, 30 Apr 2011 13:27:27 +0200 Subject: [PATCH] Implemented recoloring --- config.c | 5 +++++ render.c | 33 +++++++++++++++++++++++++++++++++ shortcuts.c | 5 +++++ zathura.c | 18 +++++++++++++++++- zathura.h | 21 ++++++++++++++------- 5 files changed, 74 insertions(+), 8 deletions(-) diff --git a/config.c b/config.c index 30bf0f3..cea5207 100644 --- a/config.c +++ b/config.c @@ -14,6 +14,7 @@ config_load_default(zathura_t* zathura) } int int_value = 0; + char* string_value = NULL; girara_session_t* gsession = zathura->ui.session; /* general settings */ @@ -27,6 +28,10 @@ config_load_default(zathura_t* zathura) int_value = 2; girara_setting_add(gsession, "pages-per-row", &int_value, INT, false, "Number of pages per row", NULL); + string_value = "#FFFFFF"; + girara_setting_add(gsession, "recolor-dark-color", string_value, STRING, false, "Recoloring (dark color)", NULL); + string_value = "#000000"; + girara_setting_add(gsession, "recolor-light-color", string_value, STRING, false, "Recoloring (light color)", NULL); /* define default shortcuts */ girara_shortcut_add(gsession, GDK_CONTROL_MASK, GDK_c, NULL, sc_abort, 0, 0, NULL); diff --git a/render.c b/render.c index 81695b0..16c129a 100644 --- a/render.c +++ b/render.c @@ -168,6 +168,39 @@ render(zathura_t* zathura, zathura_page_t* page) } } + /* recolor */ + if (zathura->global.recolor) { + /* recolor code based on qimageblitz library flatten() function + (http://sourceforge.net/projects/qimageblitz/) */ + + int r1 = zathura->ui.colors.recolor_dark_color.red / 257; + int g1 = zathura->ui.colors.recolor_dark_color.green / 257; + int b1 = zathura->ui.colors.recolor_dark_color.blue / 257; + int r2 = zathura->ui.colors.recolor_light_color.red / 257; + int g2 = zathura->ui.colors.recolor_light_color.green / 257; + int b2 = zathura->ui.colors.recolor_light_color.blue / 257; + + int min = 0x00; + int max = 0xFF; + int mean = 0x00; + + float sr = ((float) r2 - r1) / (max - min); + float sg = ((float) g2 - g1) / (max - min); + float sb = ((float) b2 - b1) / (max - min); + + for (unsigned int y = 0; y < page_height; y++) { + unsigned char* data = image + y * rowstride; + + for (unsigned int x = 0; x < page_width; x++) { + mean = (data[0] + data[1] + data[2]) / 3; + data[2] = sr * (mean - min) + r1 + 0.5; + data[1] = sg * (mean - min) + g1 + 0.5; + data[0] = sb * (mean - min) + b1 + 0.5; + data += 4; + } + } + } + /* draw to gtk widget */ page->surface = surface; gtk_widget_set_size_request(page->drawing_area, page_width, page_height); diff --git a/shortcuts.c b/shortcuts.c index a47f69a..d02c5ae 100644 --- a/shortcuts.c +++ b/shortcuts.c @@ -119,6 +119,11 @@ bool sc_recolor(girara_session_t* session, girara_argument_t* argument, unsigned int t) { g_return_val_if_fail(session != NULL, false); + g_return_val_if_fail(session->global.data != NULL, false); + zathura_t* zathura = session->global.data; + + zathura->global.recolor = !zathura->global.recolor; + render_all(zathura); return false; } diff --git a/zathura.c b/zathura.c index 892654e..99b389b 100644 --- a/zathura.c +++ b/zathura.c @@ -104,10 +104,13 @@ zathura_init(int argc, char* argv[]) zathura->ui.page_view = NULL; zathura->ui.index = NULL; - /* Print settings */ + /* print settings */ zathura->print.settings = NULL; zathura->print.page_setup = NULL; + /* global settings */ + zathura->global.recolor = false; + /* load plugins */ zathura_document_plugins_load(zathura); @@ -173,6 +176,19 @@ zathura_init(int argc, char* argv[]) gtk_table_set_row_spacings(GTK_TABLE(zathura->ui.page_view), zathura->global.page_padding); gtk_table_set_col_spacings(GTK_TABLE(zathura->ui.page_view), zathura->global.page_padding); + /* parse colors */ + char* string_value = girara_setting_get(zathura->ui.session, "recolor-dark-color"); + if (string_value != NULL) { + gdk_color_parse(string_value, &(zathura->ui.colors.recolor_dark_color)); + free(string_value); + } + + string_value = girara_setting_get(zathura->ui.session, "recolor-light-color"); + if (string_value != NULL) { + gdk_color_parse(string_value, &(zathura->ui.colors.recolor_light_color)); + free(string_value); + } + /* open document if passed */ if (argc > 1) { zathura_document_info_t* document_info = malloc(sizeof(zathura_document_info_t)); diff --git a/zathura.h b/zathura.h index 1421751..0ded288 100644 --- a/zathura.h +++ b/zathura.h @@ -44,6 +44,12 @@ typedef struct zathura_s girara_statusbar_item_t* page_number; /**> page number statusbar entry */ } statusbar; + struct + { + GdkColor recolor_dark_color; /**> Dark color for recoloring */ + GdkColor recolor_light_color; /**> Light color for recoloring */ + } colors; + GtkWidget *page_view; /**> Widget that contains all rendered pages */ GtkWidget *index; /**> Widget to show the index of the document */ } ui; @@ -55,25 +61,26 @@ typedef struct zathura_s struct { - girara_list_t* plugins; - girara_list_t* path; + girara_list_t* plugins; /**> List of plugins */ + girara_list_t* path; /**> List of plugin paths */ } plugins; struct { - gchar* config_dir; - gchar* data_dir; + gchar* config_dir; /**> Path to the configuration directory */ + gchar* data_dir; /**> Path to the data directory */ } config; struct { - GtkPrintSettings* settings; - GtkPageSetup* page_setup; + GtkPrintSettings* settings; /**> Print settings */ + GtkPageSetup* page_setup; /**> Saved page setup */ } print; struct { - unsigned int page_padding; + unsigned int page_padding; /**> Padding between the pages */ + bool recolor; /**> Recoloring mode switch */ } global; zathura_document_t* document; /**> The current document */