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.
310 * If no "*" is present, the extra string is just appended to data string.
311 * Pango mark up is accepted in left and right parts.
312 * Any \n is replaced by a newline
314 * "<i>*</i>\n" -> data is displayed in italics ended with a newline
315 * "\n" -> ended with newline
316 * "ISO *" -> prefix data with "ISO " (ie. "ISO 100")
317 * "Collection <b>*</b>\n" -> display data in bold prefixed by "Collection " and a newline is appended
319 * FIXME: using background / foreground colors lead to weird results.
323 gchar *right = extra;
325 guint len = strlen(extra);
327 /* Search and replace "\n" by a newline character */
328 for (p = extra; *p; p++, len--)
329 if (p[0] == '\\' && p[1] == 'n')
331 memmove(p+1, p+2, --len);
335 /* Search for left and right parts */
336 for (p = extra; *p; p++)
346 new_data = g_strdup_printf("%s%s%s", left ? left : "", data, right);
353 g_string_erase(new, pos, end-start+1);
355 g_string_insert(new, pos, data);
357 if (pos-prev == 2 && new->str[pos-1] == imp)
359 g_string_erase(new, --pos, 1);
362 g_string_insert(new, pos, sep);
367 prev = data ? pos+strlen(data)-1 : pos-1;
368 last = data ? TRUE : last;
374 /* search and destroy empty lines */
376 while ((start = strchr(end, '\n')))
379 while (*++(end) == '\n')
381 g_string_erase(new, start-new->str, end-start-1);
384 g_strchomp(new->str);
387 g_string_free(new, FALSE);
392 static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
394 GdkPixbuf *pixbuf = NULL;
404 GdkPixbuf *imgpixbuf = NULL;
405 LayoutWindow *lw = NULL;
410 ImageWindow *imd = osd->imd;
411 FileData *fd = image_get_fd(imd);
413 if (!fd) return NULL;
415 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
417 name = image_get_name(imd);
420 name_escaped = g_markup_escape_text(name, -1);
427 cd = image_get_collection(imd, &info);
430 gchar *collection_name;
432 t = g_list_length(cd->list);
433 n = g_list_index(cd->list, info) + 1;
436 if (file_extension_match(cd->name, ".gqv"))
437 collection_name = remove_extension_from_path(cd->name);
439 collection_name = g_strdup(cd->name);
443 collection_name = g_strdup(_("Untitled"));
446 ct = g_markup_escape_text(collection_name, -1);
447 g_free(collection_name);
451 lw = layout_find_by_image(imd);
456 n = g_list_length(lw->slideshow->list_done);
457 t = n + g_list_length(lw->slideshow->list);
462 t = layout_list_count(lw, NULL);
463 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
466 else if (view_window_find_image(imd, &n, &t))
482 size = text_from_size_abrev(imd->size);
485 if (imd->delay_flip &&
486 imd->il && imd->il->pixbuf &&
487 image_get_pixbuf(imd) != imd->il->pixbuf)
489 w = gdk_pixbuf_get_width(imd->il->pixbuf);
490 h = gdk_pixbuf_get_height(imd->il->pixbuf);
491 imgpixbuf = imd->il->pixbuf;
495 image_get_image_size(imd, &w, &h);
496 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
499 if (imgpixbuf && (osd->show & OSD_SHOW_HISTOGRAM) && osd->histogram
500 && (!imd->il || imd->il->done))
503 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
504 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
505 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
508 g_hash_table_insert(vars, "collection", g_strdup(ct));
509 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
510 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
511 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
512 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
513 g_hash_table_insert(vars, "size", g_strdup(size));
514 g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
518 text = g_strdup_printf(_("Untitled"));
522 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
527 g_free(name_escaped);
528 g_hash_table_destroy(vars);
531 gint active_marks = 0;
535 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
537 active_marks += fd->marks[mark];
540 if (active_marks > 0)
542 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
544 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
546 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
550 text2 = g_strdup_printf("%s\n%s", text, buf->str);
552 text2 = g_strdup(buf->str);
553 g_string_free(buf, TRUE);
560 gchar *escaped_histogram_label = g_markup_escape_text(histogram_label(osd->histogram), -1);
562 text2 = g_strdup_printf("%s\n%s", text, escaped_histogram_label);
564 text2 = g_strdup(escaped_histogram_label);
565 g_free(escaped_histogram_label);
571 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
572 pango_layout_set_markup(layout, text, -1);
575 pango_layout_get_pixel_size(layout, &width, &height);
576 /* with empty text width is set to 0, but not height) */
579 else if (height == 0)
581 if (width > 0) width += 10;
582 if (height > 0) height += 10;
586 histogram_read(osd->histogram, imgpixbuf);
587 if (width < HISTOGRAM_WIDTH + 10) width = HISTOGRAM_WIDTH + 10;
588 height += HISTOGRAM_HEIGHT + 5;
591 if (width > 0 && height > 0)
593 /* TODO: make osd color configurable --Zas */
594 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
595 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
596 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
597 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
598 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
599 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
600 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
601 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
602 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
607 gint y = height - HISTOGRAM_HEIGHT - 5;
615 for (i = 0; i < subdiv; i++)
617 gint d = (i > 0 ? 0 : 1);
618 gint div_width = d + w / subdiv;
620 pixbuf_set_rect(pixbuf, x + xoffset, y, div_width, HISTOGRAM_HEIGHT, c, c, c, alpha, d, 1, 1, 1);
621 xoffset += div_width;
624 histogram_draw(osd->histogram, pixbuf, x, y, w, HISTOGRAM_HEIGHT);
626 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
629 g_object_unref(G_OBJECT(layout));
634 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
636 static GdkPixbuf **icons = NULL;
637 GdkPixbuf *icon = NULL;
639 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
640 if (icons[flag]) return icons[flag];
642 if (osd_icons[flag].key)
644 icon = pixbuf_inline(osd_icons[flag].key);
649 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
650 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
651 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
654 case IMAGE_OSD_ROTATE_AUTO:
655 pixbuf_set_rect(icon, 3, 8, 11, 12,
658 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
659 20, 9, 14, 15, 14, 3,
662 case IMAGE_OSD_ROTATE_USER:
664 case IMAGE_OSD_COLOR:
665 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
666 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
667 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
669 case IMAGE_OSD_FIRST:
670 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
671 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
672 12, 5, 18, 11, 6, 11,
676 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
677 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
678 12, 18, 6, 12, 18, 12,
682 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
683 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
695 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
699 if (osd->icon_id[flag]) return;
701 pixbuf = image_osd_icon_pixbuf(flag);
704 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
705 osd_icons[flag].x, osd_icons[flag].y,
709 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
711 if (osd->icon_id[flag])
713 image_overlay_remove(osd->imd, osd->icon_id[flag]);
714 osd->icon_id[flag] = 0;
718 static gint image_osd_update_cb(gpointer data)
720 OverlayStateData *osd = data;
722 osd->imd->overlay_show_zoom = FALSE;
724 if (osd->show & OSD_SHOW_INFO)
726 if (osd->changed_states & IMAGE_STATE_IMAGE)
730 pixbuf = image_osd_info_render(osd);
733 if (osd->ovl_info == 0)
735 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
736 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
740 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
742 g_object_unref(pixbuf);
744 else if (osd->ovl_info)
746 image_overlay_remove(osd->imd, osd->ovl_info);
755 image_overlay_remove(osd->imd, osd->ovl_info);
760 if (osd->show & OSD_SHOW_STATUS)
764 if (osd->changed_states & IMAGE_STATE_IMAGE)
766 for (i = 0; i < IMAGE_OSD_COUNT; i++)
768 if (osd_icons[i].reset) osd->icon_time[i] = 0;
772 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
774 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
775 image_osd_timer_schedule(osd);
778 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
782 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
785 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
788 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
789 image_osd_timer_schedule(osd);
792 for (i = 0; i < IMAGE_OSD_COUNT; i++)
794 if (osd->icon_time[i] > 0)
796 image_osd_icon_show(osd, i);
800 image_osd_icon_hide(osd, i);
808 for (i = 0; i < IMAGE_OSD_COUNT; i++)
810 image_osd_icon_hide(osd, i);
814 if (osd->imd->il && osd->imd->il->done)
815 osd->changed_states = IMAGE_STATE_NONE;
820 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
822 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
824 if (osd->idle_id == -1)
826 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
830 void image_osd_update(ImageWindow *imd)
832 OverlayStateData *osd = image_get_osd_data(imd);
836 image_osd_update_schedule(osd, TRUE);
839 static gint image_osd_timer_cb(gpointer data)
841 OverlayStateData *osd = data;
843 gint changed = FALSE;
846 for (i = 0; i < IMAGE_OSD_COUNT; i++)
848 if (osd->icon_time[i] > 1)
851 if (osd->icon_time[i] < 2)
853 osd->icon_time[i] = 0;
863 if (changed) image_osd_update_schedule(osd, FALSE);
874 static void image_osd_timer_schedule(OverlayStateData *osd)
876 if (osd->timer_id == -1)
878 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
882 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
884 OverlayStateData *osd = data;
886 osd->changed_states |= state;
887 image_osd_update_schedule(osd, FALSE);
890 static void image_osd_free(OverlayStateData *osd)
894 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
895 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
901 image_set_osd_data(osd->imd, NULL);
902 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
904 image_set_state_func(osd->imd, NULL, NULL);
905 image_overlay_remove(osd->imd, osd->ovl_info);
907 for (i = 0; i < IMAGE_OSD_COUNT; i++)
909 image_osd_icon_hide(osd, i);
913 if (osd->histogram) histogram_free(osd->histogram);
918 static void image_osd_remove(ImageWindow *imd)
920 OverlayStateData *osd = image_get_osd_data(imd);
922 if (osd) image_osd_free(osd);
925 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
927 OverlayStateData *osd = data;
933 static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
935 OverlayStateData *osd = image_get_osd_data(imd);
939 osd = g_new0(OverlayStateData, 1);
943 osd->show = OSD_SHOW_NOTHING;
944 osd->histogram = NULL;
946 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
947 G_CALLBACK(image_osd_destroy_cb), osd);
948 image_set_osd_data(imd, osd);
950 image_set_state_func(osd->imd, image_osd_state_cb, osd);
953 if (show & OSD_SHOW_HISTOGRAM)
954 osd->histogram = histogram_new();
955 else if (osd->histogram)
957 histogram_free(osd->histogram);
958 osd->histogram = NULL;
961 if (show & OSD_SHOW_STATUS)
962 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
964 if (show != osd->show)
965 image_osd_update_schedule(osd, TRUE);
970 void image_osd_set(ImageWindow *imd, OsdShowFlags show)
974 if (show == OSD_SHOW_NOTHING)
976 image_osd_remove(imd);
980 image_osd_enable(imd, show);
983 OsdShowFlags image_osd_get(ImageWindow *imd)
985 OverlayStateData *osd = image_get_osd_data(imd);
987 return osd ? osd->show : OSD_SHOW_NOTHING;
993 2+ = show for duration tenths of a second
994 -1 = use default duration
996 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
998 OverlayStateData *osd = image_get_osd_data(imd);
1002 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
1003 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
1004 if (duration > 1) duration += 1;
1006 osd->icon_time[flag] = duration;
1008 image_osd_update_schedule(osd, FALSE);
1009 image_osd_timer_schedule(osd);