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
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);
191 start = strchr(new->str, delim);
194 end = strchr(start+1, delim);
198 for (p = end; p > start; p--)
200 if (*p == ':' && was_digit)
205 was_digit = g_ascii_isdigit(*p);
208 if (trunc) limit = atoi(trunc+1);
210 name = g_strndup(start+1, ((limit > 0) ? trunc : end)-start-1);
212 pos = start-new->str;
213 data = g_strdup(g_hash_table_lookup(vars, name));
214 if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
216 data = exif_get_data_as_text(exif, name);
217 if (data && *data && limit > 0 && strlen(data) > limit + 3)
219 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
225 /* Since we use pango markup to display, we need to escape here */
226 gchar *escaped = g_markup_escape_text(data, -1);
230 g_string_erase(new, pos, end-start+1);
232 g_string_insert(new, pos, data);
233 if (pos-prev == 2 && new->str[pos-1] == imp)
235 g_string_erase(new, --pos, 1);
238 g_string_insert(new, pos, sep);
243 prev = data ? pos+strlen(data)-1 : pos-1;
244 last = data ? TRUE : last;
250 /* search and destroy empty lines */
252 while ((start = strchr(end, '\n')))
255 while (*++(end) == '\n')
257 g_string_erase(new, start-new->str, end-start-1);
260 g_strchomp(new->str);
263 g_string_free(new, FALSE);
268 static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
270 GdkPixbuf *pixbuf = NULL;
280 GdkPixbuf *imgpixbuf = NULL;
281 LayoutWindow *lw = NULL;
286 ImageWindow *imd = osd->imd;
287 FileData *fd = image_get_fd(imd);
289 if (!fd) return NULL;
291 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
293 name = image_get_name(imd);
296 name_escaped = g_markup_escape_text(name, -1);
303 cd = image_get_collection(imd, &info);
308 t = g_list_length(cd->list);
309 n = g_list_index(cd->list, info) + 1;
310 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
311 ct = g_strdup_printf("<i>%s</i>\n", buf);
316 lw = layout_find_by_image(imd);
321 n = g_list_length(lw->slideshow->list_done);
322 t = n + g_list_length(lw->slideshow->list);
327 t = layout_list_count(lw, NULL);
328 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
331 else if (view_window_find_image(imd, &n, &t))
347 size = text_from_size_abrev(imd->size);
350 if (imd->delay_flip &&
351 imd->il && imd->il->pixbuf &&
352 image_get_pixbuf(imd) != imd->il->pixbuf)
354 w = gdk_pixbuf_get_width(imd->il->pixbuf);
355 h = gdk_pixbuf_get_height(imd->il->pixbuf);
356 imgpixbuf = imd->il->pixbuf;
360 image_get_image_size(imd, &w, &h);
361 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
364 if (imgpixbuf && (osd->show & OSD_SHOW_HISTOGRAM) && osd->histogram
365 && (!imd->il || imd->il->done))
368 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
369 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
370 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
373 g_hash_table_insert(vars, "collection", g_strdup(ct));
374 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
375 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
376 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
377 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
378 g_hash_table_insert(vars, "size", g_strdup(size));
379 g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
383 text = g_strdup_printf(_("Untitled"));
387 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
392 g_free(name_escaped);
393 g_hash_table_destroy(vars);
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);
436 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
437 pango_layout_set_markup(layout, text, -1);
440 pango_layout_get_pixel_size(layout, &width, &height);
441 /* with empty text width is set to 0, but not height) */
444 else if (height == 0)
446 if (width > 0) width += 10;
447 if (height > 0) height += 10;
451 histogram_read(osd->histogram, imgpixbuf);
452 if (width < HISTOGRAM_WIDTH + 10) width = HISTOGRAM_WIDTH + 10;
453 height += HISTOGRAM_HEIGHT + 5;
456 if (width > 0 && height > 0)
458 /* TODO: make osd color configurable --Zas */
459 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
460 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
461 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
462 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
463 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
464 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
465 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
466 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
467 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
472 gint y = height - HISTOGRAM_HEIGHT - 5;
480 for (i = 0; i < subdiv; i++)
482 gint d = (i > 0 ? 0 : 1);
483 gint div_width = d + w / subdiv;
485 pixbuf_set_rect(pixbuf, x + xoffset, y, div_width, HISTOGRAM_HEIGHT, c, c, c, alpha, d, 1, 1, 1);
486 xoffset += div_width;
489 histogram_draw(osd->histogram, pixbuf, x, y, w, HISTOGRAM_HEIGHT);
491 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
494 g_object_unref(G_OBJECT(layout));
499 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
501 static GdkPixbuf **icons = NULL;
502 GdkPixbuf *icon = NULL;
504 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
505 if (icons[flag]) return icons[flag];
507 if (osd_icons[flag].key)
509 icon = pixbuf_inline(osd_icons[flag].key);
514 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
515 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
516 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
519 case IMAGE_OSD_ROTATE_AUTO:
520 pixbuf_set_rect(icon, 3, 8, 11, 12,
523 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
524 20, 9, 14, 15, 14, 3,
527 case IMAGE_OSD_ROTATE_USER:
529 case IMAGE_OSD_COLOR:
530 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
531 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
532 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
534 case IMAGE_OSD_FIRST:
535 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
536 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
537 12, 5, 18, 11, 6, 11,
541 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
542 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
543 12, 18, 6, 12, 18, 12,
547 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
548 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
560 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
564 if (osd->icon_id[flag]) return;
566 pixbuf = image_osd_icon_pixbuf(flag);
569 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
570 osd_icons[flag].x, osd_icons[flag].y,
574 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
576 if (osd->icon_id[flag])
578 image_overlay_remove(osd->imd, osd->icon_id[flag]);
579 osd->icon_id[flag] = 0;
583 static gint image_osd_update_cb(gpointer data)
585 OverlayStateData *osd = data;
587 osd->imd->overlay_show_zoom = FALSE;
589 if (osd->show & OSD_SHOW_INFO)
591 if (osd->changed_states & IMAGE_STATE_IMAGE)
595 pixbuf = image_osd_info_render(osd);
598 if (osd->ovl_info == 0)
600 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
601 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
605 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
607 g_object_unref(pixbuf);
609 else if (osd->ovl_info)
611 image_overlay_remove(osd->imd, osd->ovl_info);
620 image_overlay_remove(osd->imd, osd->ovl_info);
625 if (osd->show & OSD_SHOW_STATUS)
629 if (osd->changed_states & IMAGE_STATE_IMAGE)
631 for (i = 0; i < IMAGE_OSD_COUNT; i++)
633 if (osd_icons[i].reset) osd->icon_time[i] = 0;
637 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
639 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
640 image_osd_timer_schedule(osd);
643 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
647 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
650 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
653 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
654 image_osd_timer_schedule(osd);
657 for (i = 0; i < IMAGE_OSD_COUNT; i++)
659 if (osd->icon_time[i] > 0)
661 image_osd_icon_show(osd, i);
665 image_osd_icon_hide(osd, i);
673 for (i = 0; i < IMAGE_OSD_COUNT; i++)
675 image_osd_icon_hide(osd, i);
679 if (osd->imd->il && osd->imd->il->done)
680 osd->changed_states = IMAGE_STATE_NONE;
685 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
687 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
689 if (osd->idle_id == -1)
691 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
695 void image_osd_update(ImageWindow *imd)
697 OverlayStateData *osd = image_get_osd_data(imd);
701 image_osd_update_schedule(osd, TRUE);
704 static gint image_osd_timer_cb(gpointer data)
706 OverlayStateData *osd = data;
708 gint changed = FALSE;
711 for (i = 0; i < IMAGE_OSD_COUNT; i++)
713 if (osd->icon_time[i] > 1)
716 if (osd->icon_time[i] < 2)
718 osd->icon_time[i] = 0;
728 if (changed) image_osd_update_schedule(osd, FALSE);
739 static void image_osd_timer_schedule(OverlayStateData *osd)
741 if (osd->timer_id == -1)
743 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
747 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
749 OverlayStateData *osd = data;
751 osd->changed_states |= state;
752 image_osd_update_schedule(osd, FALSE);
755 static void image_osd_free(OverlayStateData *osd)
759 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
760 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
766 image_set_osd_data(osd->imd, NULL);
767 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
769 image_set_state_func(osd->imd, NULL, NULL);
770 image_overlay_remove(osd->imd, osd->ovl_info);
772 for (i = 0; i < IMAGE_OSD_COUNT; i++)
774 image_osd_icon_hide(osd, i);
778 if (osd->histogram) histogram_free(osd->histogram);
783 static void image_osd_remove(ImageWindow *imd)
785 OverlayStateData *osd = image_get_osd_data(imd);
787 if (osd) image_osd_free(osd);
790 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
792 OverlayStateData *osd = data;
798 static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
800 OverlayStateData *osd = image_get_osd_data(imd);
804 osd = g_new0(OverlayStateData, 1);
808 osd->show = OSD_SHOW_NOTHING;
809 osd->histogram = NULL;
811 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
812 G_CALLBACK(image_osd_destroy_cb), osd);
813 image_set_osd_data(imd, osd);
815 image_set_state_func(osd->imd, image_osd_state_cb, osd);
818 if (show & OSD_SHOW_HISTOGRAM)
819 osd->histogram = histogram_new();
820 else if (osd->histogram)
822 histogram_free(osd->histogram);
823 osd->histogram = NULL;
826 if (show & OSD_SHOW_STATUS)
827 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
829 if (show != osd->show)
830 image_osd_update_schedule(osd, TRUE);
835 void image_osd_set(ImageWindow *imd, OsdShowFlags show)
839 if (show == OSD_SHOW_NOTHING)
841 image_osd_remove(imd);
845 image_osd_enable(imd, show);
848 OsdShowFlags image_osd_get(ImageWindow *imd)
850 OverlayStateData *osd = image_get_osd_data(imd);
852 return osd ? osd->show : OSD_SHOW_NOTHING;
858 2+ = show for duration tenths of a second
859 -1 = use default duration
861 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
863 OverlayStateData *osd = image_get_osd_data(imd);
867 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
868 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
869 if (duration > 1) duration += 1;
871 osd->icon_time[flag] = duration;
873 image_osd_update_schedule(osd, FALSE);
874 image_osd_timer_schedule(osd);