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"
23 #include "pixbuf-renderer.h"
24 #include "pixbuf_util.h"
25 #include "histogram.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
83 static void image_osd_timer_schedule(OverlayStateData *osd);
86 void set_default_image_overlay_template_string(ConfOptions *options)
88 if (options->image_overlay.common.template_string) g_free(options->image_overlay.common.template_string);
89 options->image_overlay.common.template_string = g_strdup(DEFAULT_OVERLAY_INFO);
92 static OverlayStateData *image_get_osd_data(ImageWindow *imd)
94 OverlayStateData *osd;
96 if (!imd) return NULL;
100 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
104 static void image_set_osd_data(ImageWindow *imd, OverlayStateData *osd)
108 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
112 *----------------------------------------------------------------------------
114 *----------------------------------------------------------------------------
118 void image_osd_histogram_chan_toggle(ImageWindow *imd)
120 OverlayStateData *osd = image_get_osd_data(imd);
122 if (!osd || !osd->histogram) return;
124 histogram_set_channel(osd->histogram, (histogram_get_channel(osd->histogram) +1)%HCHAN_COUNT);
125 image_osd_update(imd);
128 void image_osd_histogram_log_toggle(ImageWindow *imd)
130 OverlayStateData *osd = image_get_osd_data(imd);
132 if (!osd || !osd->histogram) return;
134 histogram_set_mode(osd->histogram, !histogram_get_mode(osd->histogram));
135 image_osd_update(imd);
138 void image_osd_toggle(ImageWindow *imd)
140 OverlayStateData *osd;
144 osd = image_get_osd_data(imd);
147 image_osd_set(imd, OSD_SHOW_INFO | OSD_SHOW_STATUS);
151 if (osd->show != OSD_SHOW_NOTHING)
153 if (osd->show & OSD_SHOW_HISTOGRAM)
155 image_osd_set(imd, OSD_SHOW_NOTHING);
159 image_osd_set(imd, osd->show | OSD_SHOW_HISTOGRAM);
164 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
166 gchar delim = '%', imp = '|', sep[] = " - ";
175 if (!str || !*str) return g_strdup("");
177 new = g_string_new(str);
179 exif = exif_read_fd(imd->image_fd);
190 start = strchr(new->str, delim);
193 end = strchr(start+1, delim);
197 for (p = end; p > start; p--)
199 if (*p == ':' && was_digit)
204 was_digit = g_ascii_isdigit(*p);
207 if (trunc) limit = atoi(trunc+1);
209 name = g_strndup(start+1, ((limit > 0) ? trunc : end)-start-1);
211 pos = start-new->str;
212 data = g_strdup(g_hash_table_lookup(vars, name));
213 if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
215 data = exif_get_data_as_text(exif, name);
216 if (data && *data && limit > 0 && strlen(data) > limit + 3)
218 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
224 /* Since we use pango markup to display, we need to escape here */
225 gchar *escaped = g_markup_escape_text(data, -1);
229 g_string_erase(new, pos, end-start+1);
231 g_string_insert(new, pos, data);
232 if (pos-prev == 2 && new->str[pos-1] == imp)
234 g_string_erase(new, --pos, 1);
237 g_string_insert(new, pos, sep);
242 prev = data ? pos+strlen(data)-1 : pos-1;
243 last = data ? TRUE : last;
249 /* search and destroy empty lines */
251 while ((start = strchr(end, '\n')))
254 while (*++(end) == '\n')
256 g_string_erase(new, start-new->str, end-start-1);
259 g_strchomp(new->str);
262 g_string_free(new, FALSE);
267 static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
269 GdkPixbuf *pixbuf = NULL;
279 GdkPixbuf *imgpixbuf = NULL;
280 LayoutWindow *lw = NULL;
285 ImageWindow *imd = osd->imd;
287 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
289 name = image_get_name(imd);
292 name_escaped = g_markup_escape_text(name, -1);
299 cd = image_get_collection(imd, &info);
304 t = g_list_length(cd->list);
305 n = g_list_index(cd->list, info) + 1;
306 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
307 ct = g_strdup_printf("<i>%s</i>\n", buf);
312 lw = layout_find_by_image(imd);
317 n = g_list_length(lw->slideshow->list_done);
318 t = n + g_list_length(lw->slideshow->list);
323 t = layout_list_count(lw, NULL);
324 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
327 else if (view_window_find_image(imd, &n, &t))
343 size = text_from_size_abrev(imd->size);
346 if (imd->delay_flip &&
347 imd->il && imd->il->pixbuf &&
348 image_get_pixbuf(imd) != imd->il->pixbuf)
350 w = gdk_pixbuf_get_width(imd->il->pixbuf);
351 h = gdk_pixbuf_get_height(imd->il->pixbuf);
352 imgpixbuf = imd->il->pixbuf;
356 image_get_image_size(imd, &w, &h);
357 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
360 if (imgpixbuf && (osd->show & OSD_SHOW_HISTOGRAM) && osd->histogram
361 && (!imd->il || imd->il->done))
364 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
365 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
366 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
369 g_hash_table_insert(vars, "collection", g_strdup(ct));
370 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
371 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
372 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
373 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
374 g_hash_table_insert(vars, "size", g_strdup(size));
375 g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
379 text = g_strdup_printf(_("Untitled"));
383 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
388 g_free(name_escaped);
389 g_hash_table_destroy(vars);
392 FileData *fd = image_get_fd(imd);
394 if (fd) /* fd may be null after file deletion */
396 gint active_marks = 0;
400 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
402 active_marks += fd->marks[mark];
405 if (active_marks > 0)
407 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
409 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
411 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
415 text2 = g_strdup_printf("%s\n%s", text, buf->str);
417 text2 = g_strdup(buf->str);
418 g_string_free(buf, TRUE);
425 gchar *escaped_histogram_label = g_markup_escape_text(histogram_label(osd->histogram), -1);
427 text2 = g_strdup_printf("%s\n%s", text, escaped_histogram_label);
429 text2 = g_strdup(escaped_histogram_label);
430 g_free(escaped_histogram_label);
437 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
438 pango_layout_set_markup(layout, text, -1);
441 pango_layout_get_pixel_size(layout, &width, &height);
442 /* with empty text width is set to 0, but not height) */
445 else if (height == 0)
447 if (width > 0) width += 10;
448 if (height > 0) height += 10;
452 histogram_read(osd->histogram, imgpixbuf);
453 if (width < 266) width = 266;
454 height += HISTOGRAM_HEIGHT + 5;
457 if (width > 0 && height > 0)
459 /* TODO: make osd color configurable --Zas */
460 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
461 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
462 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
463 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
464 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
465 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
466 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
467 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
468 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
471 histogram_draw(osd->histogram, pixbuf, 5, height - HISTOGRAM_HEIGHT - 5 , width - 10, HISTOGRAM_HEIGHT);
473 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
476 g_object_unref(G_OBJECT(layout));
481 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
483 static GdkPixbuf **icons = NULL;
484 GdkPixbuf *icon = NULL;
486 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
487 if (icons[flag]) return icons[flag];
489 if (osd_icons[flag].key)
491 icon = pixbuf_inline(osd_icons[flag].key);
496 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
497 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
498 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
501 case IMAGE_OSD_ROTATE_AUTO:
502 pixbuf_set_rect(icon, 3, 8, 11, 12,
505 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
506 20, 9, 14, 15, 14, 3,
509 case IMAGE_OSD_ROTATE_USER:
511 case IMAGE_OSD_COLOR:
512 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
513 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
514 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
516 case IMAGE_OSD_FIRST:
517 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
518 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
519 12, 5, 18, 11, 6, 11,
523 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
524 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
525 12, 18, 6, 12, 18, 12,
529 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
530 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
542 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
546 if (osd->icon_id[flag]) return;
548 pixbuf = image_osd_icon_pixbuf(flag);
551 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
552 osd_icons[flag].x, osd_icons[flag].y,
556 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
558 if (osd->icon_id[flag])
560 image_overlay_remove(osd->imd, osd->icon_id[flag]);
561 osd->icon_id[flag] = 0;
565 static gint image_osd_update_cb(gpointer data)
567 OverlayStateData *osd = data;
569 osd->imd->overlay_show_zoom = FALSE;
571 if (osd->show & OSD_SHOW_INFO)
573 if (osd->changed_states & IMAGE_STATE_IMAGE)
577 pixbuf = image_osd_info_render(osd);
580 if (osd->ovl_info == 0)
582 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
583 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
587 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
589 g_object_unref(pixbuf);
591 else if (osd->ovl_info)
593 image_overlay_remove(osd->imd, osd->ovl_info);
602 image_overlay_remove(osd->imd, osd->ovl_info);
607 if (osd->show & OSD_SHOW_STATUS)
611 if (osd->changed_states & IMAGE_STATE_IMAGE)
613 for (i = 0; i < IMAGE_OSD_COUNT; i++)
615 if (osd_icons[i].reset) osd->icon_time[i] = 0;
619 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
621 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
622 image_osd_timer_schedule(osd);
625 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
629 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
632 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
635 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
636 image_osd_timer_schedule(osd);
639 for (i = 0; i < IMAGE_OSD_COUNT; i++)
641 if (osd->icon_time[i] > 0)
643 image_osd_icon_show(osd, i);
647 image_osd_icon_hide(osd, i);
655 for (i = 0; i < IMAGE_OSD_COUNT; i++)
657 image_osd_icon_hide(osd, i);
661 if (osd->imd->il && osd->imd->il->done)
662 osd->changed_states = IMAGE_STATE_NONE;
667 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
669 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
671 if (osd->idle_id == -1)
673 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
677 void image_osd_update(ImageWindow *imd)
679 OverlayStateData *osd = image_get_osd_data(imd);
683 image_osd_update_schedule(osd, TRUE);
686 static gint image_osd_timer_cb(gpointer data)
688 OverlayStateData *osd = data;
690 gint changed = FALSE;
693 for (i = 0; i < IMAGE_OSD_COUNT; i++)
695 if (osd->icon_time[i] > 1)
698 if (osd->icon_time[i] < 2)
700 osd->icon_time[i] = 0;
710 if (changed) image_osd_update_schedule(osd, FALSE);
721 static void image_osd_timer_schedule(OverlayStateData *osd)
723 if (osd->timer_id == -1)
725 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
729 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
731 OverlayStateData *osd = data;
733 osd->changed_states |= state;
734 image_osd_update_schedule(osd, FALSE);
737 static void image_osd_free(OverlayStateData *osd)
741 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
742 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
748 image_set_osd_data(osd->imd, NULL);
749 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
751 image_set_state_func(osd->imd, NULL, NULL);
752 image_overlay_remove(osd->imd, osd->ovl_info);
754 for (i = 0; i < IMAGE_OSD_COUNT; i++)
756 image_osd_icon_hide(osd, i);
760 if (osd->histogram) histogram_free(osd->histogram);
765 static void image_osd_remove(ImageWindow *imd)
767 OverlayStateData *osd = image_get_osd_data(imd);
769 if (osd) image_osd_free(osd);
772 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
774 OverlayStateData *osd = data;
780 static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
782 OverlayStateData *osd = image_get_osd_data(imd);
786 osd = g_new0(OverlayStateData, 1);
790 osd->show = OSD_SHOW_NOTHING;
791 osd->histogram = NULL;
793 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
794 G_CALLBACK(image_osd_destroy_cb), osd);
795 image_set_osd_data(imd, osd);
797 image_set_state_func(osd->imd, image_osd_state_cb, osd);
800 if (show & OSD_SHOW_HISTOGRAM)
801 osd->histogram = histogram_new();
802 else if (osd->histogram)
804 histogram_free(osd->histogram);
805 osd->histogram = NULL;
808 if (show & OSD_SHOW_STATUS)
809 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
811 if (show != osd->show)
812 image_osd_update_schedule(osd, TRUE);
817 void image_osd_set(ImageWindow *imd, OsdShowFlags show)
821 if (show == OSD_SHOW_NOTHING)
823 image_osd_remove(imd);
827 image_osd_enable(imd, show);
830 gint image_osd_get(ImageWindow *imd, OsdShowFlags *show)
832 OverlayStateData *osd = image_get_osd_data(imd);
834 if (!osd) return FALSE;
836 if (show) *show = osd->show;
844 2+ = show for duration tenths of a second
845 -1 = use default duration
847 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
849 OverlayStateData *osd = image_get_osd_data(imd);
853 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
854 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
855 if (duration > 1) duration += 1;
857 osd->icon_time[flag] = duration;
859 image_osd_update_schedule(osd, FALSE);
860 image_osd_timer_schedule(osd);