4 * Copyright (C) 2008 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
14 #include "image-overlay.h"
20 #include "histogram.h"
24 #include "pixbuf-renderer.h"
25 #include "pixbuf_util.h"
26 #include "ui_fileops.h"
29 *----------------------------------------------------------------------------
31 *----------------------------------------------------------------------------
35 typedef struct _OverlayStateData OverlayStateData;
36 struct _OverlayStateData {
38 ImageState changed_states;
46 gint icon_time[IMAGE_OSD_COUNT];
47 gint icon_id[IMAGE_OSD_COUNT];
55 typedef struct _OSDIcon OSDIcon;
57 gint reset; /* reset on new image */
58 gint x; /* x, y offset */
60 gchar *key; /* inline pixbuf */
63 static OSDIcon osd_icons[] = {
64 { TRUE, 0, 0, NULL }, /* none */
65 { TRUE, -10, -10, NULL }, /* auto rotated */
66 { TRUE, -10, -10, NULL }, /* user rotated */
67 { TRUE, -40, -10, NULL }, /* color embedded */
68 { TRUE, -70, -10, NULL }, /* first image */
69 { TRUE, -70, -10, NULL }, /* last image */
70 { FALSE, -70, -10, NULL }, /* osd enabled */
74 #define OSD_DATA "overlay-data"
77 #define OSD_INFO_Y -10
79 #define IMAGE_OSD_DEFAULT_DURATION 30
81 #define HISTOGRAM_HEIGHT 140
82 #define HISTOGRAM_WIDTH 256
84 static void image_osd_timer_schedule(OverlayStateData *osd);
87 void set_default_image_overlay_template_string(ConfOptions *options)
89 if (options->image_overlay.common.template_string) g_free(options->image_overlay.common.template_string);
90 options->image_overlay.common.template_string = g_strdup(DEFAULT_OVERLAY_INFO);
93 static OverlayStateData *image_get_osd_data(ImageWindow *imd)
95 OverlayStateData *osd;
97 if (!imd) return NULL;
101 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
105 static void image_set_osd_data(ImageWindow *imd, OverlayStateData *osd)
109 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
113 *----------------------------------------------------------------------------
115 *----------------------------------------------------------------------------
119 void image_osd_histogram_chan_toggle(ImageWindow *imd)
121 OverlayStateData *osd = image_get_osd_data(imd);
123 if (!osd || !osd->histogram) return;
125 histogram_set_channel(osd->histogram, (histogram_get_channel(osd->histogram) +1)%HCHAN_COUNT);
126 image_osd_update(imd);
129 void image_osd_histogram_log_toggle(ImageWindow *imd)
131 OverlayStateData *osd = image_get_osd_data(imd);
133 if (!osd || !osd->histogram) return;
135 histogram_set_mode(osd->histogram, !histogram_get_mode(osd->histogram));
136 image_osd_update(imd);
139 void image_osd_toggle(ImageWindow *imd)
141 OverlayStateData *osd;
145 osd = image_get_osd_data(imd);
148 image_osd_set(imd, OSD_SHOW_INFO | OSD_SHOW_STATUS);
152 if (osd->show != OSD_SHOW_NOTHING)
154 if (osd->show & OSD_SHOW_HISTOGRAM)
156 image_osd_set(imd, OSD_SHOW_NOTHING);
160 image_osd_set(imd, osd->show | OSD_SHOW_HISTOGRAM);
165 static gchar *keywords_to_string(FileData *fd)
168 GString *kwstr = NULL;
173 if (comment_read(fd, &keywords, NULL))
175 GList *work = keywords;
179 gchar *kw = work->data;
184 kwstr = g_string_new("");
186 g_string_append(kwstr, ", ");
188 g_string_append(kwstr, kw);
195 g_string_free(kwstr, FALSE);
201 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
203 gchar delim = '%', imp = '|', sep[] = " - ";
212 if (!str || !*str) return g_strdup("");
214 new = g_string_new(str);
216 exif = exif_read_fd(imd->image_fd);
224 gchar *limpos = NULL;
226 gchar *extrapos = NULL;
229 start = strchr(new->str, delim);
232 end = strchr(start+1, delim);
236 /* Search for optionnal modifiers
237 * %name:99:extra% -> name = "name", limit=99, extra = "extra"
239 for (p = start + 1; p < end; p++)
243 if (g_ascii_isdigit(p[1]) && !limpos)
246 if (!trunc) trunc = p;
251 if (!trunc) trunc = p;
258 limit = atoi(limpos);
261 extra = g_strndup(extrapos, end - extrapos);
263 name = g_strndup(start+1, (trunc ? trunc : end)-start-1);
264 pos = start - new->str;
267 if (strcmp(name, "keywords") == 0)
269 data = keywords_to_string(imd->image_fd);
271 else if (strcmp(name, "comment") == 0)
273 comment_read(imd->image_fd, NULL, &data);
277 data = g_strdup(g_hash_table_lookup(vars, name));
278 if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
280 data = exif_get_data_as_text(exif, name);
282 if (data && *data && limit > 0 && strlen(data) > limit + 3)
284 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
291 /* Since we use pango markup to display, we need to escape here */
292 gchar *escaped = g_markup_escape_text(data, -1);
301 /* Display data between left and right parts of extra string
302 * the data is expressed by a '*' character.
303 * If no "*" is present, the extra string is just appended to data string.
304 * Pango mark up is accepted in left and right parts.
305 * Any \n is replaced by a newline
307 * "<i>*</i>\n" -> data is displayed in italics ended with a newline
308 * "\n" -> ended with newline
309 * "ISO *" -> prefix data with "ISO " (ie. "ISO 100")
310 * "Collection <b>*</b>\n" -> display data in bold prefixed by "Collection " and a newline is appended
312 * FIXME: using background / foreground colors lead to weird results.
316 gchar *right = extra;
318 gint len = strlen(extra);
320 /* Search and replace "\n" by a newline character */
321 for (p = extra; *p; p++, len--)
322 if (p[0] == '\\' && p[1] == 'n')
324 memmove(p+1, p+2, --len);
328 /* Search for left and right parts */
329 for (p = extra; *p; p++)
339 new_data = g_strdup_printf("%s%s%s", left ? left : "", data, right);
346 g_string_erase(new, pos, end-start+1);
348 g_string_insert(new, pos, data);
350 if (pos-prev == 2 && new->str[pos-1] == imp)
352 g_string_erase(new, --pos, 1);
355 g_string_insert(new, pos, sep);
360 prev = data ? pos+strlen(data)-1 : pos-1;
361 last = data ? TRUE : last;
367 /* search and destroy empty lines */
369 while ((start = strchr(end, '\n')))
372 while (*++(end) == '\n')
374 g_string_erase(new, start-new->str, end-start-1);
377 g_strchomp(new->str);
380 g_string_free(new, FALSE);
385 static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
387 GdkPixbuf *pixbuf = NULL;
397 GdkPixbuf *imgpixbuf = NULL;
398 LayoutWindow *lw = NULL;
403 ImageWindow *imd = osd->imd;
404 FileData *fd = image_get_fd(imd);
406 if (!fd) return NULL;
408 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
410 name = image_get_name(imd);
413 name_escaped = g_markup_escape_text(name, -1);
420 cd = image_get_collection(imd, &info);
423 gchar *collection_name;
425 t = g_list_length(cd->list);
426 n = g_list_index(cd->list, info) + 1;
429 if (file_extension_match(cd->name, ".gqv"))
430 collection_name = remove_extension_from_path(cd->name);
432 collection_name = g_strdup(cd->name);
436 collection_name = g_strdup(_("Untitled"));
439 ct = g_markup_escape_text(collection_name, -1);
440 g_free(collection_name);
444 lw = layout_find_by_image(imd);
449 n = g_list_length(lw->slideshow->list_done);
450 t = n + g_list_length(lw->slideshow->list);
455 t = layout_list_count(lw, NULL);
456 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
459 else if (view_window_find_image(imd, &n, &t))
475 size = text_from_size_abrev(imd->size);
478 if (imd->delay_flip &&
479 imd->il && imd->il->pixbuf &&
480 image_get_pixbuf(imd) != imd->il->pixbuf)
482 w = gdk_pixbuf_get_width(imd->il->pixbuf);
483 h = gdk_pixbuf_get_height(imd->il->pixbuf);
484 imgpixbuf = imd->il->pixbuf;
488 image_get_image_size(imd, &w, &h);
489 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
492 if (imgpixbuf && (osd->show & OSD_SHOW_HISTOGRAM) && osd->histogram
493 && (!imd->il || imd->il->done))
496 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
497 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
498 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
501 g_hash_table_insert(vars, "collection", g_strdup(ct));
502 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
503 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
504 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
505 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
506 g_hash_table_insert(vars, "size", g_strdup(size));
507 g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
511 text = g_strdup_printf(_("Untitled"));
515 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
520 g_free(name_escaped);
521 g_hash_table_destroy(vars);
524 gint active_marks = 0;
528 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
530 active_marks += fd->marks[mark];
533 if (active_marks > 0)
535 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
537 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
539 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
543 text2 = g_strdup_printf("%s\n%s", text, buf->str);
545 text2 = g_strdup(buf->str);
546 g_string_free(buf, TRUE);
553 gchar *escaped_histogram_label = g_markup_escape_text(histogram_label(osd->histogram), -1);
555 text2 = g_strdup_printf("%s\n%s", text, escaped_histogram_label);
557 text2 = g_strdup(escaped_histogram_label);
558 g_free(escaped_histogram_label);
564 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
565 pango_layout_set_markup(layout, text, -1);
568 pango_layout_get_pixel_size(layout, &width, &height);
569 /* with empty text width is set to 0, but not height) */
572 else if (height == 0)
574 if (width > 0) width += 10;
575 if (height > 0) height += 10;
579 histogram_read(osd->histogram, imgpixbuf);
580 if (width < HISTOGRAM_WIDTH + 10) width = HISTOGRAM_WIDTH + 10;
581 height += HISTOGRAM_HEIGHT + 5;
584 if (width > 0 && height > 0)
586 /* TODO: make osd color configurable --Zas */
587 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
588 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
589 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
590 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
591 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
592 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
593 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
594 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
595 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
600 gint y = height - HISTOGRAM_HEIGHT - 5;
608 for (i = 0; i < subdiv; i++)
610 gint d = (i > 0 ? 0 : 1);
611 gint div_width = d + w / subdiv;
613 pixbuf_set_rect(pixbuf, x + xoffset, y, div_width, HISTOGRAM_HEIGHT, c, c, c, alpha, d, 1, 1, 1);
614 xoffset += div_width;
617 histogram_draw(osd->histogram, pixbuf, x, y, w, HISTOGRAM_HEIGHT);
619 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
622 g_object_unref(G_OBJECT(layout));
627 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
629 static GdkPixbuf **icons = NULL;
630 GdkPixbuf *icon = NULL;
632 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
633 if (icons[flag]) return icons[flag];
635 if (osd_icons[flag].key)
637 icon = pixbuf_inline(osd_icons[flag].key);
642 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
643 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
644 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
647 case IMAGE_OSD_ROTATE_AUTO:
648 pixbuf_set_rect(icon, 3, 8, 11, 12,
651 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
652 20, 9, 14, 15, 14, 3,
655 case IMAGE_OSD_ROTATE_USER:
657 case IMAGE_OSD_COLOR:
658 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
659 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
660 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
662 case IMAGE_OSD_FIRST:
663 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
664 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
665 12, 5, 18, 11, 6, 11,
669 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
670 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
671 12, 18, 6, 12, 18, 12,
675 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
676 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
688 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
692 if (osd->icon_id[flag]) return;
694 pixbuf = image_osd_icon_pixbuf(flag);
697 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
698 osd_icons[flag].x, osd_icons[flag].y,
702 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
704 if (osd->icon_id[flag])
706 image_overlay_remove(osd->imd, osd->icon_id[flag]);
707 osd->icon_id[flag] = 0;
711 static gint image_osd_update_cb(gpointer data)
713 OverlayStateData *osd = data;
715 osd->imd->overlay_show_zoom = FALSE;
717 if (osd->show & OSD_SHOW_INFO)
719 if (osd->changed_states & IMAGE_STATE_IMAGE)
723 pixbuf = image_osd_info_render(osd);
726 if (osd->ovl_info == 0)
728 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
729 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
733 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
735 g_object_unref(pixbuf);
737 else if (osd->ovl_info)
739 image_overlay_remove(osd->imd, osd->ovl_info);
748 image_overlay_remove(osd->imd, osd->ovl_info);
753 if (osd->show & OSD_SHOW_STATUS)
757 if (osd->changed_states & IMAGE_STATE_IMAGE)
759 for (i = 0; i < IMAGE_OSD_COUNT; i++)
761 if (osd_icons[i].reset) osd->icon_time[i] = 0;
765 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
767 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
768 image_osd_timer_schedule(osd);
771 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
775 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
778 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
781 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
782 image_osd_timer_schedule(osd);
785 for (i = 0; i < IMAGE_OSD_COUNT; i++)
787 if (osd->icon_time[i] > 0)
789 image_osd_icon_show(osd, i);
793 image_osd_icon_hide(osd, i);
801 for (i = 0; i < IMAGE_OSD_COUNT; i++)
803 image_osd_icon_hide(osd, i);
807 if (osd->imd->il && osd->imd->il->done)
808 osd->changed_states = IMAGE_STATE_NONE;
813 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
815 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
817 if (osd->idle_id == -1)
819 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
823 void image_osd_update(ImageWindow *imd)
825 OverlayStateData *osd = image_get_osd_data(imd);
829 image_osd_update_schedule(osd, TRUE);
832 static gint image_osd_timer_cb(gpointer data)
834 OverlayStateData *osd = data;
836 gint changed = FALSE;
839 for (i = 0; i < IMAGE_OSD_COUNT; i++)
841 if (osd->icon_time[i] > 1)
844 if (osd->icon_time[i] < 2)
846 osd->icon_time[i] = 0;
856 if (changed) image_osd_update_schedule(osd, FALSE);
867 static void image_osd_timer_schedule(OverlayStateData *osd)
869 if (osd->timer_id == -1)
871 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
875 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
877 OverlayStateData *osd = data;
879 osd->changed_states |= state;
880 image_osd_update_schedule(osd, FALSE);
883 static void image_osd_free(OverlayStateData *osd)
887 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
888 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
894 image_set_osd_data(osd->imd, NULL);
895 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
897 image_set_state_func(osd->imd, NULL, NULL);
898 image_overlay_remove(osd->imd, osd->ovl_info);
900 for (i = 0; i < IMAGE_OSD_COUNT; i++)
902 image_osd_icon_hide(osd, i);
906 if (osd->histogram) histogram_free(osd->histogram);
911 static void image_osd_remove(ImageWindow *imd)
913 OverlayStateData *osd = image_get_osd_data(imd);
915 if (osd) image_osd_free(osd);
918 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
920 OverlayStateData *osd = data;
926 static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
928 OverlayStateData *osd = image_get_osd_data(imd);
932 osd = g_new0(OverlayStateData, 1);
936 osd->show = OSD_SHOW_NOTHING;
937 osd->histogram = NULL;
939 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
940 G_CALLBACK(image_osd_destroy_cb), osd);
941 image_set_osd_data(imd, osd);
943 image_set_state_func(osd->imd, image_osd_state_cb, osd);
946 if (show & OSD_SHOW_HISTOGRAM)
947 osd->histogram = histogram_new();
948 else if (osd->histogram)
950 histogram_free(osd->histogram);
951 osd->histogram = NULL;
954 if (show & OSD_SHOW_STATUS)
955 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
957 if (show != osd->show)
958 image_osd_update_schedule(osd, TRUE);
963 void image_osd_set(ImageWindow *imd, OsdShowFlags show)
967 if (show == OSD_SHOW_NOTHING)
969 image_osd_remove(imd);
973 image_osd_enable(imd, show);
976 OsdShowFlags image_osd_get(ImageWindow *imd)
978 OverlayStateData *osd = image_get_osd_data(imd);
980 return osd ? osd->show : OSD_SHOW_NOTHING;
986 2+ = show for duration tenths of a second
987 -1 = use default duration
989 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
991 OverlayStateData *osd = image_get_osd_data(imd);
995 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
996 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
997 if (duration > 1) duration += 1;
999 osd->icon_time[flag] = duration;
1001 image_osd_update_schedule(osd, FALSE);
1002 image_osd_timer_schedule(osd);