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"
21 #include "histogram.h"
25 #include "pixbuf-renderer.h"
26 #include "pixbuf_util.h"
27 #include "ui_fileops.h"
30 *----------------------------------------------------------------------------
32 *----------------------------------------------------------------------------
36 typedef struct _OverlayStateData OverlayStateData;
37 struct _OverlayStateData {
39 ImageState changed_states;
47 gint icon_time[IMAGE_OSD_COUNT];
48 gint icon_id[IMAGE_OSD_COUNT];
56 typedef struct _OSDIcon OSDIcon;
58 gint reset; /* reset on new image */
59 gint x; /* x, y offset */
61 gchar *key; /* inline pixbuf */
64 static OSDIcon osd_icons[] = {
65 { TRUE, 0, 0, NULL }, /* none */
66 { TRUE, -10, -10, NULL }, /* auto rotated */
67 { TRUE, -10, -10, NULL }, /* user rotated */
68 { TRUE, -40, -10, NULL }, /* color embedded */
69 { TRUE, -70, -10, NULL }, /* first image */
70 { TRUE, -70, -10, NULL }, /* last image */
71 { FALSE, -70, -10, NULL }, /* osd enabled */
75 #define OSD_DATA "overlay-data"
78 #define OSD_INFO_Y -10
80 #define IMAGE_OSD_DEFAULT_DURATION 30
82 #define HISTOGRAM_HEIGHT 140
83 #define HISTOGRAM_WIDTH 256
85 static void image_osd_timer_schedule(OverlayStateData *osd);
88 void set_default_image_overlay_template_string(ConfOptions *options)
90 if (options->image_overlay.common.template_string) g_free(options->image_overlay.common.template_string);
91 options->image_overlay.common.template_string = g_strdup(DEFAULT_OVERLAY_INFO);
94 static OverlayStateData *image_get_osd_data(ImageWindow *imd)
96 OverlayStateData *osd;
98 if (!imd) return NULL;
102 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
106 static void image_set_osd_data(ImageWindow *imd, OverlayStateData *osd)
110 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
114 *----------------------------------------------------------------------------
116 *----------------------------------------------------------------------------
120 void image_osd_histogram_chan_toggle(ImageWindow *imd)
122 OverlayStateData *osd = image_get_osd_data(imd);
124 if (!osd || !osd->histogram) return;
126 histogram_set_channel(osd->histogram, (histogram_get_channel(osd->histogram) +1)%HCHAN_COUNT);
127 image_osd_update(imd);
130 void image_osd_histogram_log_toggle(ImageWindow *imd)
132 OverlayStateData *osd = image_get_osd_data(imd);
134 if (!osd || !osd->histogram) return;
136 histogram_set_mode(osd->histogram, !histogram_get_mode(osd->histogram));
137 image_osd_update(imd);
140 void image_osd_toggle(ImageWindow *imd)
142 OverlayStateData *osd;
146 osd = image_get_osd_data(imd);
149 image_osd_set(imd, OSD_SHOW_INFO | OSD_SHOW_STATUS);
153 if (osd->show != OSD_SHOW_NOTHING)
155 if (osd->show & OSD_SHOW_HISTOGRAM)
157 image_osd_set(imd, OSD_SHOW_NOTHING);
161 image_osd_set(imd, osd->show | OSD_SHOW_HISTOGRAM);
166 static gchar *keywords_to_string(FileData *fd)
169 GString *kwstr = NULL;
174 if (comment_read(fd, &keywords, NULL))
176 GList *work = keywords;
180 gchar *kw = work->data;
185 kwstr = g_string_new("");
187 g_string_append(kwstr, ", ");
189 g_string_append(kwstr, kw);
196 g_string_free(kwstr, FALSE);
202 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
204 gchar delim = '%', imp = '|', sep[] = " - ";
213 if (!str || !*str) return g_strdup("");
215 new = g_string_new(str);
217 exif = exif_read_fd(imd->image_fd);
225 gchar *limpos = NULL;
227 gchar *extrapos = NULL;
230 start = strchr(new->str, delim);
233 end = strchr(start+1, delim);
237 /* Search for optionnal modifiers
238 * %name:99:extra% -> name = "name", limit=99, extra = "extra"
240 for (p = start + 1; p < end; p++)
244 if (g_ascii_isdigit(p[1]) && !limpos)
247 if (!trunc) trunc = p;
252 if (!trunc) trunc = p;
259 limit = atoi(limpos);
262 extra = g_strndup(extrapos, end - extrapos);
264 name = g_strndup(start+1, (trunc ? trunc : end)-start-1);
265 pos = start - new->str;
268 if (strcmp(name, "keywords") == 0)
270 data = keywords_to_string(imd->image_fd);
272 else if (strcmp(name, "comment") == 0)
274 comment_read(imd->image_fd, NULL, &data);
278 data = g_strdup(g_hash_table_lookup(vars, name));
279 if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
281 data = exif_get_data_as_text(exif, name);
283 if (data && *data && limit > 0 && strlen(data) > limit + 3)
285 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
292 /* Since we use pango markup to display, we need to escape here */
293 gchar *escaped = g_markup_escape_text(data, -1);
302 /* Display data between left and right parts of extra string
303 * the data is expressed by a '*' character.
304 * If no "*" is present, the extra string is just appended to data string.
305 * Pango mark up is accepted in left and right parts.
306 * Any \n is replaced by a newline
308 * "<i>*</i>\n" -> data is displayed in italics ended with a newline
309 * "\n" -> ended with newline
310 * "ISO *" -> prefix data with "ISO " (ie. "ISO 100")
311 * "Collection <b>*</b>\n" -> display data in bold prefixed by "Collection " and a newline is appended
313 * FIXME: using background / foreground colors lead to weird results.
317 gchar *right = extra;
319 gint len = strlen(extra);
321 /* Search and replace "\n" by a newline character */
322 for (p = extra; *p; p++, len--)
323 if (p[0] == '\\' && p[1] == 'n')
325 memmove(p+1, p+2, --len);
329 /* Search for left and right parts */
330 for (p = extra; *p; p++)
340 new_data = g_strdup_printf("%s%s%s", left ? left : "", data, right);
347 g_string_erase(new, pos, end-start+1);
349 g_string_insert(new, pos, data);
351 if (pos-prev == 2 && new->str[pos-1] == imp)
353 g_string_erase(new, --pos, 1);
356 g_string_insert(new, pos, sep);
361 prev = data ? pos+strlen(data)-1 : pos-1;
362 last = data ? TRUE : last;
368 /* search and destroy empty lines */
370 while ((start = strchr(end, '\n')))
373 while (*++(end) == '\n')
375 g_string_erase(new, start-new->str, end-start-1);
378 g_strchomp(new->str);
381 g_string_free(new, FALSE);
386 static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
388 GdkPixbuf *pixbuf = NULL;
398 GdkPixbuf *imgpixbuf = NULL;
399 LayoutWindow *lw = NULL;
404 ImageWindow *imd = osd->imd;
405 FileData *fd = image_get_fd(imd);
407 if (!fd) return NULL;
409 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
411 name = image_get_name(imd);
414 name_escaped = g_markup_escape_text(name, -1);
421 cd = image_get_collection(imd, &info);
424 gchar *collection_name;
426 t = g_list_length(cd->list);
427 n = g_list_index(cd->list, info) + 1;
430 if (file_extension_match(cd->name, ".gqv"))
431 collection_name = remove_extension_from_path(cd->name);
433 collection_name = g_strdup(cd->name);
437 collection_name = g_strdup(_("Untitled"));
440 ct = g_markup_escape_text(collection_name, -1);
441 g_free(collection_name);
445 lw = layout_find_by_image(imd);
450 n = g_list_length(lw->slideshow->list_done);
451 t = n + g_list_length(lw->slideshow->list);
456 t = layout_list_count(lw, NULL);
457 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
460 else if (view_window_find_image(imd, &n, &t))
476 size = text_from_size_abrev(imd->size);
479 if (imd->delay_flip &&
480 imd->il && imd->il->pixbuf &&
481 image_get_pixbuf(imd) != imd->il->pixbuf)
483 w = gdk_pixbuf_get_width(imd->il->pixbuf);
484 h = gdk_pixbuf_get_height(imd->il->pixbuf);
485 imgpixbuf = imd->il->pixbuf;
489 image_get_image_size(imd, &w, &h);
490 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
493 if (imgpixbuf && (osd->show & OSD_SHOW_HISTOGRAM) && osd->histogram
494 && (!imd->il || imd->il->done))
497 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
498 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
499 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
502 g_hash_table_insert(vars, "collection", g_strdup(ct));
503 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
504 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
505 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
506 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
507 g_hash_table_insert(vars, "size", g_strdup(size));
508 g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
512 text = g_strdup_printf(_("Untitled"));
516 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
521 g_free(name_escaped);
522 g_hash_table_destroy(vars);
525 gint active_marks = 0;
529 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
531 active_marks += fd->marks[mark];
534 if (active_marks > 0)
536 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
538 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
540 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
544 text2 = g_strdup_printf("%s\n%s", text, buf->str);
546 text2 = g_strdup(buf->str);
547 g_string_free(buf, TRUE);
554 gchar *escaped_histogram_label = g_markup_escape_text(histogram_label(osd->histogram), -1);
556 text2 = g_strdup_printf("%s\n%s", text, escaped_histogram_label);
558 text2 = g_strdup(escaped_histogram_label);
559 g_free(escaped_histogram_label);
565 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
566 pango_layout_set_markup(layout, text, -1);
569 pango_layout_get_pixel_size(layout, &width, &height);
570 /* with empty text width is set to 0, but not height) */
573 else if (height == 0)
575 if (width > 0) width += 10;
576 if (height > 0) height += 10;
580 histogram_read(osd->histogram, imgpixbuf);
581 if (width < HISTOGRAM_WIDTH + 10) width = HISTOGRAM_WIDTH + 10;
582 height += HISTOGRAM_HEIGHT + 5;
585 if (width > 0 && height > 0)
587 /* TODO: make osd color configurable --Zas */
588 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
589 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
590 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
591 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
592 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
593 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
594 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
595 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
596 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
601 gint y = height - HISTOGRAM_HEIGHT - 5;
609 for (i = 0; i < subdiv; i++)
611 gint d = (i > 0 ? 0 : 1);
612 gint div_width = d + w / subdiv;
614 pixbuf_set_rect(pixbuf, x + xoffset, y, div_width, HISTOGRAM_HEIGHT, c, c, c, alpha, d, 1, 1, 1);
615 xoffset += div_width;
618 histogram_draw(osd->histogram, pixbuf, x, y, w, HISTOGRAM_HEIGHT);
620 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
623 g_object_unref(G_OBJECT(layout));
628 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
630 static GdkPixbuf **icons = NULL;
631 GdkPixbuf *icon = NULL;
633 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
634 if (icons[flag]) return icons[flag];
636 if (osd_icons[flag].key)
638 icon = pixbuf_inline(osd_icons[flag].key);
643 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
644 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
645 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
648 case IMAGE_OSD_ROTATE_AUTO:
649 pixbuf_set_rect(icon, 3, 8, 11, 12,
652 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
653 20, 9, 14, 15, 14, 3,
656 case IMAGE_OSD_ROTATE_USER:
658 case IMAGE_OSD_COLOR:
659 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
660 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
661 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
663 case IMAGE_OSD_FIRST:
664 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
665 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
666 12, 5, 18, 11, 6, 11,
670 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
671 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
672 12, 18, 6, 12, 18, 12,
676 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
677 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
689 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
693 if (osd->icon_id[flag]) return;
695 pixbuf = image_osd_icon_pixbuf(flag);
698 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
699 osd_icons[flag].x, osd_icons[flag].y,
703 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
705 if (osd->icon_id[flag])
707 image_overlay_remove(osd->imd, osd->icon_id[flag]);
708 osd->icon_id[flag] = 0;
712 static gint image_osd_update_cb(gpointer data)
714 OverlayStateData *osd = data;
716 osd->imd->overlay_show_zoom = FALSE;
718 if (osd->show & OSD_SHOW_INFO)
720 if (osd->changed_states & IMAGE_STATE_IMAGE)
724 pixbuf = image_osd_info_render(osd);
727 if (osd->ovl_info == 0)
729 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
730 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
734 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
736 g_object_unref(pixbuf);
738 else if (osd->ovl_info)
740 image_overlay_remove(osd->imd, osd->ovl_info);
749 image_overlay_remove(osd->imd, osd->ovl_info);
754 if (osd->show & OSD_SHOW_STATUS)
758 if (osd->changed_states & IMAGE_STATE_IMAGE)
760 for (i = 0; i < IMAGE_OSD_COUNT; i++)
762 if (osd_icons[i].reset) osd->icon_time[i] = 0;
766 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
768 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
769 image_osd_timer_schedule(osd);
772 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
776 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
779 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
782 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
783 image_osd_timer_schedule(osd);
786 for (i = 0; i < IMAGE_OSD_COUNT; i++)
788 if (osd->icon_time[i] > 0)
790 image_osd_icon_show(osd, i);
794 image_osd_icon_hide(osd, i);
802 for (i = 0; i < IMAGE_OSD_COUNT; i++)
804 image_osd_icon_hide(osd, i);
808 if (osd->imd->il && osd->imd->il->done)
809 osd->changed_states = IMAGE_STATE_NONE;
814 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
816 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
818 if (osd->idle_id == -1)
820 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
824 void image_osd_update(ImageWindow *imd)
826 OverlayStateData *osd = image_get_osd_data(imd);
830 image_osd_update_schedule(osd, TRUE);
833 static gint image_osd_timer_cb(gpointer data)
835 OverlayStateData *osd = data;
837 gint changed = FALSE;
840 for (i = 0; i < IMAGE_OSD_COUNT; i++)
842 if (osd->icon_time[i] > 1)
845 if (osd->icon_time[i] < 2)
847 osd->icon_time[i] = 0;
857 if (changed) image_osd_update_schedule(osd, FALSE);
868 static void image_osd_timer_schedule(OverlayStateData *osd)
870 if (osd->timer_id == -1)
872 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
876 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
878 OverlayStateData *osd = data;
880 osd->changed_states |= state;
881 image_osd_update_schedule(osd, FALSE);
884 static void image_osd_free(OverlayStateData *osd)
888 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
889 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
895 image_set_osd_data(osd->imd, NULL);
896 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
898 image_set_state_func(osd->imd, NULL, NULL);
899 image_overlay_remove(osd->imd, osd->ovl_info);
901 for (i = 0; i < IMAGE_OSD_COUNT; i++)
903 image_osd_icon_hide(osd, i);
907 if (osd->histogram) histogram_free(osd->histogram);
912 static void image_osd_remove(ImageWindow *imd)
914 OverlayStateData *osd = image_get_osd_data(imd);
916 if (osd) image_osd_free(osd);
919 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
921 OverlayStateData *osd = data;
927 static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
929 OverlayStateData *osd = image_get_osd_data(imd);
933 osd = g_new0(OverlayStateData, 1);
937 osd->show = OSD_SHOW_NOTHING;
938 osd->histogram = NULL;
940 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
941 G_CALLBACK(image_osd_destroy_cb), osd);
942 image_set_osd_data(imd, osd);
944 image_set_state_func(osd->imd, image_osd_state_cb, osd);
947 if (show & OSD_SHOW_HISTOGRAM)
948 osd->histogram = histogram_new();
949 else if (osd->histogram)
951 histogram_free(osd->histogram);
952 osd->histogram = NULL;
955 if (show & OSD_SHOW_STATUS)
956 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
958 if (show != osd->show)
959 image_osd_update_schedule(osd, TRUE);
964 void image_osd_set(ImageWindow *imd, OsdShowFlags show)
968 if (show == OSD_SHOW_NOTHING)
970 image_osd_remove(imd);
974 image_osd_enable(imd, show);
977 OsdShowFlags image_osd_get(ImageWindow *imd)
979 OverlayStateData *osd = image_get_osd_data(imd);
981 return osd ? osd->show : OSD_SHOW_NOTHING;
987 2+ = show for duration tenths of a second
988 -1 = use default duration
990 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
992 OverlayStateData *osd = image_get_osd_data(imd);
996 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
997 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
998 if (duration > 1) duration += 1;
1000 osd->icon_time[flag] = duration;
1002 image_osd_update_schedule(osd, FALSE);
1003 image_osd_timer_schedule(osd);