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 *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
167 gchar delim = '%', imp = '|', sep[] = " - ";
176 if (!str || !*str) return g_strdup("");
178 new = g_string_new(str);
180 exif = exif_read_fd(imd->image_fd);
188 gchar *limpos = NULL;
190 gchar *extrapos = NULL;
193 start = strchr(new->str, delim);
196 end = strchr(start+1, delim);
200 /* Search for optionnal modifiers
201 * %name:99:extra% -> name = "name", limit=99, extra = "extra"
203 for (p = start + 1; p < end; p++)
207 if (g_ascii_isdigit(p[1]) && !limpos)
210 if (!trunc) trunc = p;
215 if (!trunc) trunc = p;
222 limit = atoi(limpos);
225 extra = g_strndup(extrapos, end - extrapos);
227 name = g_strndup(start+1, (trunc ? trunc : end)-start-1);
229 pos = start-new->str;
230 data = g_strdup(g_hash_table_lookup(vars, name));
231 if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
233 data = exif_get_data_as_text(exif, name);
234 if (data && *data && limit > 0 && strlen(data) > limit + 3)
236 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
243 /* Since we use pango markup to display, we need to escape here */
244 gchar *escaped = g_markup_escape_text(data, -1);
253 /* Display data between left and right parts of extra string
254 * the data is expressed by a '*' character.
255 * If no "*" is present, the extra string is just appended to data string.
256 * Pango mark up is accepted in left and right parts.
257 * Any \n is replaced by a newline
259 * "<i>*</i>\n" -> data is displayed in italics ended with a newline
260 * "\n" -> ended with newline
261 * "ISO *" -> prefix data with "ISO " (ie. "ISO 100")
262 * "Collection <b>*</b>\n" -> display data in bold prefixed by "Collection " and a newline is appended
264 * FIXME: using background / foreground colors lead to weird results.
268 gchar *right = extra;
270 gint len = strlen(extra);
272 /* Search and replace "\n" by a newline character */
273 for (p = extra; *p; p++, len--)
274 if (p[0] == '\\' && p[1] == 'n')
276 memmove(p+1, p+2, --len);
280 /* Search for left and right parts */
281 for (p = extra; *p; p++)
291 new_data = g_strdup_printf("%s%s%s", left ? left : "", data, right);
298 g_string_erase(new, pos, end-start+1);
300 g_string_insert(new, pos, data);
302 if (pos-prev == 2 && new->str[pos-1] == imp)
304 g_string_erase(new, --pos, 1);
307 g_string_insert(new, pos, sep);
312 prev = data ? pos+strlen(data)-1 : pos-1;
313 last = data ? TRUE : last;
319 /* search and destroy empty lines */
321 while ((start = strchr(end, '\n')))
324 while (*++(end) == '\n')
326 g_string_erase(new, start-new->str, end-start-1);
329 g_strchomp(new->str);
332 g_string_free(new, FALSE);
337 static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
339 GdkPixbuf *pixbuf = NULL;
349 GdkPixbuf *imgpixbuf = NULL;
350 LayoutWindow *lw = NULL;
355 ImageWindow *imd = osd->imd;
356 FileData *fd = image_get_fd(imd);
358 if (!fd) return NULL;
360 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
362 name = image_get_name(imd);
365 name_escaped = g_markup_escape_text(name, -1);
372 cd = image_get_collection(imd, &info);
375 gchar *collection_name;
377 t = g_list_length(cd->list);
378 n = g_list_index(cd->list, info) + 1;
381 if (file_extension_match(cd->name, ".gqv"))
382 collection_name = remove_extension_from_path(cd->name);
384 collection_name = g_strdup(cd->name);
388 collection_name = g_strdup(_("Untitled"));
391 ct = g_markup_escape_text(collection_name, -1);
392 g_free(collection_name);
396 lw = layout_find_by_image(imd);
401 n = g_list_length(lw->slideshow->list_done);
402 t = n + g_list_length(lw->slideshow->list);
407 t = layout_list_count(lw, NULL);
408 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
411 else if (view_window_find_image(imd, &n, &t))
427 size = text_from_size_abrev(imd->size);
430 if (imd->delay_flip &&
431 imd->il && imd->il->pixbuf &&
432 image_get_pixbuf(imd) != imd->il->pixbuf)
434 w = gdk_pixbuf_get_width(imd->il->pixbuf);
435 h = gdk_pixbuf_get_height(imd->il->pixbuf);
436 imgpixbuf = imd->il->pixbuf;
440 image_get_image_size(imd, &w, &h);
441 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
444 if (imgpixbuf && (osd->show & OSD_SHOW_HISTOGRAM) && osd->histogram
445 && (!imd->il || imd->il->done))
448 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
449 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
450 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
453 g_hash_table_insert(vars, "collection", g_strdup(ct));
454 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
455 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
456 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
457 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
458 g_hash_table_insert(vars, "size", g_strdup(size));
459 g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
463 text = g_strdup_printf(_("Untitled"));
467 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
472 g_free(name_escaped);
473 g_hash_table_destroy(vars);
476 gint active_marks = 0;
480 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
482 active_marks += fd->marks[mark];
485 if (active_marks > 0)
487 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
489 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
491 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
495 text2 = g_strdup_printf("%s\n%s", text, buf->str);
497 text2 = g_strdup(buf->str);
498 g_string_free(buf, TRUE);
505 gchar *escaped_histogram_label = g_markup_escape_text(histogram_label(osd->histogram), -1);
507 text2 = g_strdup_printf("%s\n%s", text, escaped_histogram_label);
509 text2 = g_strdup(escaped_histogram_label);
510 g_free(escaped_histogram_label);
516 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
517 pango_layout_set_markup(layout, text, -1);
520 pango_layout_get_pixel_size(layout, &width, &height);
521 /* with empty text width is set to 0, but not height) */
524 else if (height == 0)
526 if (width > 0) width += 10;
527 if (height > 0) height += 10;
531 histogram_read(osd->histogram, imgpixbuf);
532 if (width < HISTOGRAM_WIDTH + 10) width = HISTOGRAM_WIDTH + 10;
533 height += HISTOGRAM_HEIGHT + 5;
536 if (width > 0 && height > 0)
538 /* TODO: make osd color configurable --Zas */
539 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
540 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
541 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
542 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
543 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
544 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
545 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
546 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
547 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
552 gint y = height - HISTOGRAM_HEIGHT - 5;
560 for (i = 0; i < subdiv; i++)
562 gint d = (i > 0 ? 0 : 1);
563 gint div_width = d + w / subdiv;
565 pixbuf_set_rect(pixbuf, x + xoffset, y, div_width, HISTOGRAM_HEIGHT, c, c, c, alpha, d, 1, 1, 1);
566 xoffset += div_width;
569 histogram_draw(osd->histogram, pixbuf, x, y, w, HISTOGRAM_HEIGHT);
571 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
574 g_object_unref(G_OBJECT(layout));
579 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
581 static GdkPixbuf **icons = NULL;
582 GdkPixbuf *icon = NULL;
584 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
585 if (icons[flag]) return icons[flag];
587 if (osd_icons[flag].key)
589 icon = pixbuf_inline(osd_icons[flag].key);
594 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
595 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
596 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
599 case IMAGE_OSD_ROTATE_AUTO:
600 pixbuf_set_rect(icon, 3, 8, 11, 12,
603 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
604 20, 9, 14, 15, 14, 3,
607 case IMAGE_OSD_ROTATE_USER:
609 case IMAGE_OSD_COLOR:
610 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
611 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
612 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
614 case IMAGE_OSD_FIRST:
615 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
616 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
617 12, 5, 18, 11, 6, 11,
621 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
622 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
623 12, 18, 6, 12, 18, 12,
627 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
628 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
640 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
644 if (osd->icon_id[flag]) return;
646 pixbuf = image_osd_icon_pixbuf(flag);
649 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
650 osd_icons[flag].x, osd_icons[flag].y,
654 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
656 if (osd->icon_id[flag])
658 image_overlay_remove(osd->imd, osd->icon_id[flag]);
659 osd->icon_id[flag] = 0;
663 static gint image_osd_update_cb(gpointer data)
665 OverlayStateData *osd = data;
667 osd->imd->overlay_show_zoom = FALSE;
669 if (osd->show & OSD_SHOW_INFO)
671 if (osd->changed_states & IMAGE_STATE_IMAGE)
675 pixbuf = image_osd_info_render(osd);
678 if (osd->ovl_info == 0)
680 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
681 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
685 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
687 g_object_unref(pixbuf);
689 else if (osd->ovl_info)
691 image_overlay_remove(osd->imd, osd->ovl_info);
700 image_overlay_remove(osd->imd, osd->ovl_info);
705 if (osd->show & OSD_SHOW_STATUS)
709 if (osd->changed_states & IMAGE_STATE_IMAGE)
711 for (i = 0; i < IMAGE_OSD_COUNT; i++)
713 if (osd_icons[i].reset) osd->icon_time[i] = 0;
717 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
719 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
720 image_osd_timer_schedule(osd);
723 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
727 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
730 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
733 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
734 image_osd_timer_schedule(osd);
737 for (i = 0; i < IMAGE_OSD_COUNT; i++)
739 if (osd->icon_time[i] > 0)
741 image_osd_icon_show(osd, i);
745 image_osd_icon_hide(osd, i);
753 for (i = 0; i < IMAGE_OSD_COUNT; i++)
755 image_osd_icon_hide(osd, i);
759 if (osd->imd->il && osd->imd->il->done)
760 osd->changed_states = IMAGE_STATE_NONE;
765 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
767 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
769 if (osd->idle_id == -1)
771 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
775 void image_osd_update(ImageWindow *imd)
777 OverlayStateData *osd = image_get_osd_data(imd);
781 image_osd_update_schedule(osd, TRUE);
784 static gint image_osd_timer_cb(gpointer data)
786 OverlayStateData *osd = data;
788 gint changed = FALSE;
791 for (i = 0; i < IMAGE_OSD_COUNT; i++)
793 if (osd->icon_time[i] > 1)
796 if (osd->icon_time[i] < 2)
798 osd->icon_time[i] = 0;
808 if (changed) image_osd_update_schedule(osd, FALSE);
819 static void image_osd_timer_schedule(OverlayStateData *osd)
821 if (osd->timer_id == -1)
823 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
827 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
829 OverlayStateData *osd = data;
831 osd->changed_states |= state;
832 image_osd_update_schedule(osd, FALSE);
835 static void image_osd_free(OverlayStateData *osd)
839 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
840 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
846 image_set_osd_data(osd->imd, NULL);
847 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
849 image_set_state_func(osd->imd, NULL, NULL);
850 image_overlay_remove(osd->imd, osd->ovl_info);
852 for (i = 0; i < IMAGE_OSD_COUNT; i++)
854 image_osd_icon_hide(osd, i);
858 if (osd->histogram) histogram_free(osd->histogram);
863 static void image_osd_remove(ImageWindow *imd)
865 OverlayStateData *osd = image_get_osd_data(imd);
867 if (osd) image_osd_free(osd);
870 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
872 OverlayStateData *osd = data;
878 static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
880 OverlayStateData *osd = image_get_osd_data(imd);
884 osd = g_new0(OverlayStateData, 1);
888 osd->show = OSD_SHOW_NOTHING;
889 osd->histogram = NULL;
891 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
892 G_CALLBACK(image_osd_destroy_cb), osd);
893 image_set_osd_data(imd, osd);
895 image_set_state_func(osd->imd, image_osd_state_cb, osd);
898 if (show & OSD_SHOW_HISTOGRAM)
899 osd->histogram = histogram_new();
900 else if (osd->histogram)
902 histogram_free(osd->histogram);
903 osd->histogram = NULL;
906 if (show & OSD_SHOW_STATUS)
907 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
909 if (show != osd->show)
910 image_osd_update_schedule(osd, TRUE);
915 void image_osd_set(ImageWindow *imd, OsdShowFlags show)
919 if (show == OSD_SHOW_NOTHING)
921 image_osd_remove(imd);
925 image_osd_enable(imd, show);
928 OsdShowFlags image_osd_get(ImageWindow *imd)
930 OverlayStateData *osd = image_get_osd_data(imd);
932 return osd ? osd->show : OSD_SHOW_NOTHING;
938 2+ = show for duration tenths of a second
939 -1 = use default duration
941 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
943 OverlayStateData *osd = image_get_osd_data(imd);
947 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
948 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
949 if (duration > 1) duration += 1;
951 osd->icon_time[flag] = duration;
953 image_osd_update_schedule(osd, FALSE);
954 image_osd_timer_schedule(osd);