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);
86 void set_image_overlay_template_string(gchar **template_string, const gchar *value)
88 g_assert(template_string);
90 g_free(*template_string);
91 *template_string = g_strdup(value);
95 void set_default_image_overlay_template_string(gchar **template_string)
97 set_image_overlay_template_string(template_string, DEFAULT_OVERLAY_INFO);
100 static OverlayStateData *image_get_osd_data(ImageWindow *imd)
102 OverlayStateData *osd;
104 if (!imd) return NULL;
108 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
112 static void image_set_osd_data(ImageWindow *imd, OverlayStateData *osd)
116 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
120 *----------------------------------------------------------------------------
122 *----------------------------------------------------------------------------
126 void image_osd_histogram_chan_toggle(ImageWindow *imd)
128 OverlayStateData *osd = image_get_osd_data(imd);
130 if (!osd || !osd->histogram) return;
132 histogram_set_channel(osd->histogram, (histogram_get_channel(osd->histogram) +1)%HCHAN_COUNT);
133 image_osd_update(imd);
136 void image_osd_histogram_log_toggle(ImageWindow *imd)
138 OverlayStateData *osd = image_get_osd_data(imd);
140 if (!osd || !osd->histogram) return;
142 histogram_set_mode(osd->histogram, !histogram_get_mode(osd->histogram));
143 image_osd_update(imd);
146 void image_osd_toggle(ImageWindow *imd)
148 OverlayStateData *osd;
152 osd = image_get_osd_data(imd);
155 image_osd_set(imd, OSD_SHOW_INFO | OSD_SHOW_STATUS);
159 if (osd->show != OSD_SHOW_NOTHING)
161 if (osd->show & OSD_SHOW_HISTOGRAM)
163 image_osd_set(imd, OSD_SHOW_NOTHING);
167 image_osd_set(imd, osd->show | OSD_SHOW_HISTOGRAM);
172 static gchar *keywords_to_string(FileData *fd)
175 GString *kwstr = NULL;
180 if (comment_read(fd, &keywords, NULL))
182 GList *work = keywords;
186 gchar *kw = work->data;
191 kwstr = g_string_new("");
193 g_string_append(kwstr, ", ");
195 g_string_append(kwstr, kw);
202 g_string_free(kwstr, FALSE);
208 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
210 gchar delim = '%', imp = '|', sep[] = " - ";
219 if (!str || !*str) return g_strdup("");
221 new = g_string_new(str);
223 exif = exif_read_fd(imd->image_fd);
231 gchar *limpos = NULL;
233 gchar *extrapos = NULL;
236 start = strchr(new->str, delim);
239 end = strchr(start+1, delim);
243 /* Search for optionnal modifiers
244 * %name:99:extra% -> name = "name", limit=99, extra = "extra"
246 for (p = start + 1; p < end; p++)
250 if (g_ascii_isdigit(p[1]) && !limpos)
253 if (!trunc) trunc = p;
258 if (!trunc) trunc = p;
265 limit = (guint) atoi(limpos);
268 extra = g_strndup(extrapos, end - extrapos);
270 name = g_strndup(start+1, (trunc ? trunc : end)-start-1);
271 pos = start - new->str;
274 if (strcmp(name, "keywords") == 0)
276 data = keywords_to_string(imd->image_fd);
278 else if (strcmp(name, "comment") == 0)
280 comment_read(imd->image_fd, NULL, &data);
284 data = g_strdup(g_hash_table_lookup(vars, name));
285 if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
287 data = exif_get_data_as_text(exif, name);
289 if (data && *data && limit > 0 && strlen(data) > limit + 3)
291 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
298 /* Since we use pango markup to display, we need to escape here */
299 gchar *escaped = g_markup_escape_text(data, -1);
308 /* Display data between left and right parts of extra string
309 * the data is expressed by a '*' character. A '*' may be escaped
310 * by a \. You should escape all '*' characters, do not rely on the
311 * current implementation which only replaces the first unescaped '*'.
312 * If no "*" is present, the extra string is just appended to data string.
313 * Pango mark up is accepted in left and right parts.
314 * Any \n is replaced by a newline
316 * "<i>*</i>\n" -> data is displayed in italics ended with a newline
317 * "\n" -> ended with newline
318 * "ISO *" -> prefix data with "ISO " (ie. "ISO 100")
319 * "\**\*" -> prefix data with a star, and append a star (ie. "*100*")
320 * "\\*" -> prefix data with an anti slash (ie "\100")
321 * "Collection <b>*</b>\n" -> display data in bold prefixed by "Collection " and a newline is appended
323 * FIXME: using background / foreground colors lead to weird results.
327 gchar *right = extra;
329 guint len = strlen(extra);
331 /* Search for left and right parts and unescape characters */
332 for (p = extra; *p; p++, len--)
337 memmove(p+1, p+2, --len);
340 else if (p[1] != '\0')
341 memmove(p, p+1, len--); // includes \0
343 else if (p[0] == '*' && !left)
349 if (left) right[-1] = '\0';
351 new_data = g_strdup_printf("%s%s%s", left ? left : "", data, right);
358 g_string_erase(new, pos, end-start+1);
360 g_string_insert(new, pos, data);
362 if (pos-prev >= 2 && new->str[pos-1] == imp)
364 g_string_erase(new, --pos, 1);
365 if (last && data && *data)
367 g_string_insert(new, pos, sep);
372 prev = data ? pos+strlen(data)-1 : pos-1;
373 last = data ? TRUE : last;
379 /* search and destroy empty lines */
381 while ((start = strchr(end, '\n')))
384 while (*++(end) == '\n')
386 g_string_erase(new, start-new->str, end-start-1);
389 g_strchomp(new->str);
392 g_string_free(new, FALSE);
397 static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
399 GdkPixbuf *pixbuf = NULL;
409 GdkPixbuf *imgpixbuf = NULL;
410 LayoutWindow *lw = NULL;
415 ImageWindow *imd = osd->imd;
416 FileData *fd = image_get_fd(imd);
418 if (!fd) return NULL;
420 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
422 name = image_get_name(imd);
425 name_escaped = g_markup_escape_text(name, -1);
432 cd = image_get_collection(imd, &info);
435 gchar *collection_name;
437 t = g_list_length(cd->list);
438 n = g_list_index(cd->list, info) + 1;
441 if (file_extension_match(cd->name, GQ_COLLECTION_EXT))
442 collection_name = remove_extension_from_path(cd->name);
444 collection_name = g_strdup(cd->name);
448 collection_name = g_strdup(_("Untitled"));
451 ct = g_markup_escape_text(collection_name, -1);
452 g_free(collection_name);
456 lw = layout_find_by_image(imd);
461 n = g_list_length(lw->slideshow->list_done);
462 t = n + g_list_length(lw->slideshow->list);
467 t = layout_list_count(lw, NULL);
468 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
471 else if (view_window_find_image(imd, &n, &t))
487 size = text_from_size_abrev(imd->size);
490 if (imd->delay_flip &&
491 imd->il && imd->il->pixbuf &&
492 image_get_pixbuf(imd) != imd->il->pixbuf)
494 w = gdk_pixbuf_get_width(imd->il->pixbuf);
495 h = gdk_pixbuf_get_height(imd->il->pixbuf);
496 imgpixbuf = imd->il->pixbuf;
500 image_get_image_size(imd, &w, &h);
501 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
504 if (imgpixbuf && (osd->show & OSD_SHOW_HISTOGRAM) && osd->histogram
505 && (!imd->il || imd->il->done))
508 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
509 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
510 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
513 g_hash_table_insert(vars, "collection", g_strdup(ct));
514 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
515 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
516 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
517 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
518 g_hash_table_insert(vars, "size", g_strdup(size));
519 g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
523 text = g_strdup_printf(_("Untitled"));
527 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
532 g_free(name_escaped);
533 g_hash_table_destroy(vars);
536 gint active_marks = 0;
540 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
542 active_marks += fd->marks[mark];
545 if (active_marks > 0)
547 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
549 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
551 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
555 text2 = g_strdup_printf("%s\n%s", text, buf->str);
557 text2 = g_strdup(buf->str);
558 g_string_free(buf, TRUE);
565 gchar *escaped_histogram_label = g_markup_escape_text(histogram_label(osd->histogram), -1);
567 text2 = g_strdup_printf("%s\n%s", text, escaped_histogram_label);
569 text2 = g_strdup(escaped_histogram_label);
570 g_free(escaped_histogram_label);
576 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
577 pango_layout_set_markup(layout, text, -1);
580 pango_layout_get_pixel_size(layout, &width, &height);
581 /* with empty text width is set to 0, but not height) */
584 else if (height == 0)
586 if (width > 0) width += 10;
587 if (height > 0) height += 10;
591 histogram_read(osd->histogram, imgpixbuf);
592 if (width < HISTOGRAM_WIDTH + 10) width = HISTOGRAM_WIDTH + 10;
593 height += HISTOGRAM_HEIGHT + 5;
596 if (width > 0 && height > 0)
598 /* TODO: make osd color configurable --Zas */
599 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
600 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
601 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
602 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
603 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
604 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
605 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
606 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
607 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
612 gint y = height - HISTOGRAM_HEIGHT - 5;
619 float add = w / (float)subdiv;
621 for (i = 0; i < subdiv; i++)
623 gint d = (i > 0 ? 0 : 1);
625 pixbuf_set_rect(pixbuf, x + xoffset + 0.5, y, add + d + 0.5, HISTOGRAM_HEIGHT, c, c, c, alpha, d, 1, 1, 1);
629 histogram_draw(osd->histogram, pixbuf, x, y, w, HISTOGRAM_HEIGHT);
631 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
634 g_object_unref(G_OBJECT(layout));
639 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
641 static GdkPixbuf **icons = NULL;
642 GdkPixbuf *icon = NULL;
644 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
645 if (icons[flag]) return icons[flag];
647 if (osd_icons[flag].key)
649 icon = pixbuf_inline(osd_icons[flag].key);
654 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
655 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
656 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
659 case IMAGE_OSD_ROTATE_AUTO:
660 pixbuf_set_rect(icon, 3, 8, 11, 12,
663 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
664 20, 9, 14, 15, 14, 3,
667 case IMAGE_OSD_ROTATE_USER:
669 case IMAGE_OSD_COLOR:
670 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
671 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
672 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
674 case IMAGE_OSD_FIRST:
675 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
676 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
677 12, 5, 18, 11, 6, 11,
681 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
682 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
683 12, 18, 6, 12, 18, 12,
687 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
688 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
700 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
704 if (osd->icon_id[flag]) return;
706 pixbuf = image_osd_icon_pixbuf(flag);
709 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
710 osd_icons[flag].x, osd_icons[flag].y,
714 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
716 if (osd->icon_id[flag])
718 image_overlay_remove(osd->imd, osd->icon_id[flag]);
719 osd->icon_id[flag] = 0;
723 static gint image_osd_update_cb(gpointer data)
725 OverlayStateData *osd = data;
727 osd->imd->overlay_show_zoom = FALSE;
729 if (osd->show & OSD_SHOW_INFO)
731 if (osd->changed_states & IMAGE_STATE_IMAGE)
735 pixbuf = image_osd_info_render(osd);
738 if (osd->ovl_info == 0)
740 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
741 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
745 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
747 g_object_unref(pixbuf);
749 else if (osd->ovl_info)
751 image_overlay_remove(osd->imd, osd->ovl_info);
760 image_overlay_remove(osd->imd, osd->ovl_info);
765 if (osd->show & OSD_SHOW_STATUS)
769 if (osd->changed_states & IMAGE_STATE_IMAGE)
771 for (i = 0; i < IMAGE_OSD_COUNT; i++)
773 if (osd_icons[i].reset) osd->icon_time[i] = 0;
777 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
779 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
780 image_osd_timer_schedule(osd);
783 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
787 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
790 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
793 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
794 image_osd_timer_schedule(osd);
797 for (i = 0; i < IMAGE_OSD_COUNT; i++)
799 if (osd->icon_time[i] > 0)
801 image_osd_icon_show(osd, i);
805 image_osd_icon_hide(osd, i);
813 for (i = 0; i < IMAGE_OSD_COUNT; i++)
815 image_osd_icon_hide(osd, i);
819 if (osd->imd->il && osd->imd->il->done)
820 osd->changed_states = IMAGE_STATE_NONE;
825 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
827 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
829 if (osd->idle_id == -1)
831 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
835 void image_osd_update(ImageWindow *imd)
837 OverlayStateData *osd = image_get_osd_data(imd);
841 image_osd_update_schedule(osd, TRUE);
844 static gint image_osd_timer_cb(gpointer data)
846 OverlayStateData *osd = data;
848 gint changed = FALSE;
851 for (i = 0; i < IMAGE_OSD_COUNT; i++)
853 if (osd->icon_time[i] > 1)
856 if (osd->icon_time[i] < 2)
858 osd->icon_time[i] = 0;
868 if (changed) image_osd_update_schedule(osd, FALSE);
879 static void image_osd_timer_schedule(OverlayStateData *osd)
881 if (osd->timer_id == -1)
883 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
887 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
889 OverlayStateData *osd = data;
891 osd->changed_states |= state;
892 image_osd_update_schedule(osd, FALSE);
895 static void image_osd_free(OverlayStateData *osd)
899 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
900 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
906 image_set_osd_data(osd->imd, NULL);
907 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
909 image_set_state_func(osd->imd, NULL, NULL);
910 image_overlay_remove(osd->imd, osd->ovl_info);
912 for (i = 0; i < IMAGE_OSD_COUNT; i++)
914 image_osd_icon_hide(osd, i);
918 if (osd->histogram) histogram_free(osd->histogram);
923 static void image_osd_remove(ImageWindow *imd)
925 OverlayStateData *osd = image_get_osd_data(imd);
927 if (osd) image_osd_free(osd);
930 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
932 OverlayStateData *osd = data;
938 static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
940 OverlayStateData *osd = image_get_osd_data(imd);
944 osd = g_new0(OverlayStateData, 1);
948 osd->show = OSD_SHOW_NOTHING;
949 osd->histogram = NULL;
951 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
952 G_CALLBACK(image_osd_destroy_cb), osd);
953 image_set_osd_data(imd, osd);
955 image_set_state_func(osd->imd, image_osd_state_cb, osd);
958 if (show & OSD_SHOW_HISTOGRAM)
959 osd->histogram = histogram_new();
960 else if (osd->histogram)
962 histogram_free(osd->histogram);
963 osd->histogram = NULL;
966 if (show & OSD_SHOW_STATUS)
967 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
969 if (show != osd->show)
970 image_osd_update_schedule(osd, TRUE);
975 void image_osd_set(ImageWindow *imd, OsdShowFlags show)
979 if (show == OSD_SHOW_NOTHING)
981 image_osd_remove(imd);
985 image_osd_enable(imd, show);
988 OsdShowFlags image_osd_get(ImageWindow *imd)
990 OverlayStateData *osd = image_get_osd_data(imd);
992 return osd ? osd->show : OSD_SHOW_NOTHING;
998 2+ = show for duration tenths of a second
999 -1 = use default duration
1001 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
1003 OverlayStateData *osd = image_get_osd_data(imd);
1007 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
1008 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
1009 if (duration > 1) duration += 1;
1011 osd->icon_time[flag] = duration;
1013 image_osd_update_schedule(osd, FALSE);
1014 image_osd_timer_schedule(osd);