Krolyxon
Commit 0353e6e

Branches

Refs →

nsxiv0353e6e

Add support for changing the gamma value

Author
András Mohari
Date
2013-11-13 20:54 +0100
Parent
450c1ed
Commit
0353e6eea265f5f6fa53eb75300529d9ffa6d4fa

+91 −3

9 files changed

  1. M commands.c +16 −0
  2. M commands.h +1 −0
  3. M config.def.h +11 −0
  4. M image.c +30 −0
  5. M image.h +5 −0
  6. M main.c +2 −0
  7. M options.c +12 −3
  8. M options.h +1 −0
  9. M sxiv.1 +13 −0
M commands.c View
@@ -474,6 +474,22 @@ bool i_toggle_antialias(arg_t a)
474474 }
475475 }
476476
477+/* a < 0: decrease gamma
478+ * a == 0: reset gamma
479+ * a > 0: increase gamma
480+ */
481+bool i_change_gamma(arg_t a)
482+{
483+ if (mode == MODE_IMAGE) {
484+ long val = (long) a;
485+ int delta = val > 0 ? 1 : (val < 0 ? -1 : -img.gamma);
486+ img_set_gamma(&img, img.gamma + delta);
487+ return true;
488+ } else {
489+ return false;
490+ }
491+}
492+
477493 bool it_toggle_alpha(arg_t a)
478494 {
479495 img.alpha = tns.alpha = !img.alpha;
M commands.h View
@@ -67,6 +67,7 @@ bool i_fit_to_img(arg_t);
6767 bool i_rotate(arg_t);
6868 bool i_flip(arg_t);
6969 bool i_toggle_antialias(arg_t);
70+bool i_change_gamma(arg_t);
7071 bool it_toggle_alpha(arg_t);
7172 bool it_open_with(arg_t);
7273 bool it_shell_cmd(arg_t);
M config.def.h View
@@ -46,6 +46,12 @@ enum {
4646 GIF_LOOP = 0 /* endless loop [0/1] */
4747 };
4848
49+/* gamma correction: the user-visible ranges [-GAMMA_RANGE, 0] and
50+ * (0, GAMMA_RANGE] are mapped to the ranges [0, 1], and (1, GAMMA_MAX].
51+ * */
52+static const double GAMMA_MAX = 10.0;
53+static const int GAMMA_RANGE = 32;
54+
4955 #endif
5056 #ifdef _THUMBS_CONFIG
5157
@@ -141,6 +147,11 @@ static const keymap_t keys[] = {
141147 { false, XK_a, i_toggle_antialias, (arg_t) None },
142148 { false, XK_A, it_toggle_alpha, (arg_t) None },
143149
150+ /* decrease/increase/reset gamma */
151+ { false, XK_braceleft, i_change_gamma, (arg_t) -1 },
152+ { false, XK_braceright, i_change_gamma, (arg_t) +1 },
153+ { true, XK_G, i_change_gamma, (arg_t) 0 },
154+
144155 /* open current image with given program: */
145156 { true, XK_g, it_open_with, (arg_t) "gimp" },
146157
M image.c View
@@ -67,6 +67,9 @@ void img_init(img_t *img, win_t *win)
6767 img->alpha = !RENDER_WHITE_ALPHA;
6868 img->multi.cap = img->multi.cnt = 0;
6969 img->multi.animate = false;
70+
71+ img->cmod = imlib_create_color_modifier();
72+ img_set_gamma(img, options->gamma);
7073 }
7174
7275 void exif_auto_orientate(const fileinfo_t *file)
@@ -303,6 +306,8 @@ bool img_load(img_t *img, const fileinfo_t *file)
303306 img_load_gif(img, file);
304307 #endif
305308
309+ img_set_gamma(img, img->gamma);
310+
306311 img->w = imlib_image_get_width();
307312 img->h = imlib_image_get_height();
308313 img->scalemode = options->scalemode;
@@ -335,6 +340,9 @@ void img_close(img_t *img, bool decache)
335340 imlib_free_image();
336341 img->im = NULL;
337342 }
343+
344+ if (img->cmod)
345+ imlib_context_set_color_modifier(NULL);
338346 }
339347
340348 void img_check_pan(img_t *img, bool moved)
@@ -706,6 +714,28 @@ void img_toggle_antialias(img_t *img)
706714 img->dirty = true;
707715 }
708716
717+void img_set_gamma(img_t *img, int gamma)
718+{
719+ if (img == NULL)
720+ return;
721+
722+ img->gamma = MIN(MAX(gamma, -GAMMA_RANGE), GAMMA_RANGE);
723+
724+ if (img->im && img->cmod) {
725+ if (img->gamma == 0) {
726+ imlib_context_set_color_modifier(NULL);
727+ } else {
728+ double range = img->gamma <= 0 ? 1.0 : GAMMA_MAX - 1.0;
729+ imlib_context_set_color_modifier(img->cmod);
730+ imlib_reset_color_modifier();
731+ imlib_modify_color_modifier_gamma(
732+ 1.0 + (double) img->gamma
733+ * (range / (double) GAMMA_RANGE));
734+ }
735+ img->dirty = true;
736+ }
737+}
738+
709739 bool img_frame_goto(img_t *img, int n)
710740 {
711741 if (img == NULL || img->im == NULL)
M image.h View
@@ -55,6 +55,9 @@ typedef struct {
5555 bool aa;
5656 bool alpha;
5757
58+ Imlib_Color_Modifier cmod;
59+ int gamma;
60+
5861 multi_img_t multi;
5962 } img_t;
6063
@@ -81,6 +84,8 @@ void img_flip(img_t*, flipdir_t);
8184
8285 void img_toggle_antialias(img_t*);
8386
87+void img_set_gamma(img_t*, int);
88+
8489 bool img_frame_navigate(img_t*, int);
8590 bool img_frame_animate(img_t*, bool);
8691
M main.c View
@@ -352,6 +352,8 @@ void update_info(void)
352352 n += snprintf(rt + n, rlen - n, "%0*d/%d | ",
353353 fn, img.multi.sel + 1, img.multi.cnt);
354354 }
355+ if (img.gamma != 0)
356+ n += snprintf(rt + n, rlen - n, "g%d | ", img.gamma);
355357 n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, sel + 1, filecnt);
356358 ow_info = info.script == NULL;
357359 }
M options.c View
@@ -33,7 +33,7 @@ const options_t *options = (const options_t*) &_options;
3333
3434 void print_usage(void)
3535 {
36− printf("usage: sxiv [-bcdFfhioqrstvZ] [-g GEOMETRY] [-n NUM] "
36+ printf("usage: sxiv [-bcdFfhioqrstvZ] [-g GEOMETRY] [-G GAMMA] [-n NUM] "
3737 "[-N name] [-z ZOOM] FILES...\n");
3838 }
3939
@@ -44,7 +44,7 @@ void print_version(void)
4444
4545 void parse_options(int argc, char **argv)
4646 {
47− int opt, t;
47+ int opt, t, gamma;
4848
4949 _options.from_stdin = false;
5050 _options.to_stdout = false;
@@ -53,6 +53,7 @@ void parse_options(int argc, char **argv)
5353
5454 _options.scalemode = SCALE_MODE;
5555 _options.zoom = 1.0;
56+ _options.gamma = 0;
5657
5758 _options.fixed_win = false;
5859 _options.fullscreen = false;
@@ -64,7 +65,7 @@ void parse_options(int argc, char **argv)
6465 _options.thumb_mode = false;
6566 _options.clean_cache = false;
6667
67− while ((opt = getopt(argc, argv, "bcdFfg:hin:N:oqrstvZz:")) != -1) {
68+ while ((opt = getopt(argc, argv, "bcdFfg:G:hin:N:oqrstvZz:")) != -1) {
6869 switch (opt) {
6970 case '?':
7071 print_usage();
@@ -87,6 +88,14 @@ void parse_options(int argc, char **argv)
8788 case 'g':
8889 _options.geometry = optarg;
8990 break;
91+ case 'G':
92+ if (sscanf(optarg, "%d", &gamma) <= 0) {
93+ fprintf(stderr, "sxiv: invalid argument for option -G: %s\n",
94+ optarg);
95+ exit(EXIT_FAILURE);
96+ }
97+ _options.gamma = gamma;
98+ break;
9099 case 'h':
91100 print_usage();
92101 exit(EXIT_SUCCESS);
M options.h View
@@ -34,6 +34,7 @@ typedef struct {
3434 /* image: */
3535 scalemode_t scalemode;
3636 float zoom;
37+ int gamma;
3738
3839 /* window: */
3940 bool fixed_win;
M sxiv.1 View
@@ -49,6 +49,9 @@ Set window position and size. See section GEOMETRY SPECIFICATIONS of X(7) for
4949 more information on
5050 .IR GEOMETRY .
5151 .TP
52+.BI "\-G " GAMMA
53+Set gamma to GAMMA (-32..32).
54+.TP
5255 .BI "\-n " NUM
5356 Start at picture number NUM.
5457 .TP
@@ -282,6 +285,16 @@ Flip image horizontally.
282285 .TP
283286 .B _
284287 Flip image vertically.
288+.SS Gamma Correction
289+.TP
290+.B {
291+Decrease gamma.
292+.TP
293+.B }
294+Increase gamma.
295+.TP
296+.B Ctrl-G
297+Reset gamma.
285298 .SS Miscellaneous
286299 .TP
287300 .B a