2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "image-overlay.h"
26 #include "histogram.h"
31 #include "pixbuf-renderer.h"
32 #include "pixbuf-util.h"
33 #include "ui-fileops.h"
34 #include "image-load.h"
37 *----------------------------------------------------------------------------
39 *----------------------------------------------------------------------------
43 struct OverlayStateData {
45 ImageState changed_states;
51 OverlayRendererFlags origin;
58 gint icon_time[IMAGE_OSD_COUNT];
59 gint icon_id[IMAGE_OSD_COUNT];
61 guint idle_id; /* event source id */
62 guint timer_id; /* event source id */
68 gboolean reset; /* reset on new image */
69 gint x; /* x, y offset */
71 gchar *key; /* inline pixbuf */
74 static OSDIcon osd_icons[] = {
75 { TRUE, 0, 0, nullptr }, /* none */
76 { TRUE, -10, -10, nullptr }, /* auto rotated */
77 { TRUE, -10, -10, nullptr }, /* user rotated */
78 { TRUE, -40, -10, nullptr }, /* color embedded */
79 { TRUE, -70, -10, nullptr }, /* first image */
80 { TRUE, -70, -10, nullptr }, /* last image */
81 { FALSE, -70, -10, nullptr }, /* osd enabled */
82 { FALSE, 0, 0, nullptr }
85 #define OSD_DATA "overlay-data"
87 #define IMAGE_OSD_DEFAULT_DURATION 30
89 #define HISTOGRAM_HEIGHT 140
90 #define HISTOGRAM_WIDTH 256
92 static void image_osd_timer_schedule(OverlayStateData *osd);
94 void set_image_overlay_template_string(gchar **template_string, const gchar *value)
96 g_assert(template_string);
98 g_free(*template_string);
99 *template_string = g_strdup(value);
103 void set_default_image_overlay_template_string(gchar **template_string)
105 set_image_overlay_template_string(template_string, DEFAULT_OVERLAY_INFO);
108 void set_image_overlay_font_string(gchar **font_string, const gchar *value)
110 g_assert(font_string);
112 g_free(*font_string);
113 *font_string = g_strdup(value);
116 static OverlayStateData *image_get_osd_data(ImageWindow *imd)
118 OverlayStateData *osd;
120 if (!imd) return nullptr;
124 osd = static_cast<OverlayStateData *>(g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA"));
128 static void image_set_osd_data(ImageWindow *imd, OverlayStateData *osd)
132 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
136 *----------------------------------------------------------------------------
138 *----------------------------------------------------------------------------
142 void image_osd_histogram_toggle_channel(ImageWindow *imd)
144 OverlayStateData *osd = image_get_osd_data(imd);
146 if (!osd || !osd->histogram) return;
148 histogram_toggle_channel(osd->histogram);
149 image_osd_update(imd);
152 void image_osd_histogram_toggle_mode(ImageWindow *imd)
154 OverlayStateData *osd = image_get_osd_data(imd);
156 if (!osd || !osd->histogram) return;
158 histogram_toggle_mode(osd->histogram);
159 image_osd_update(imd);
162 void image_osd_histogram_set_channel(ImageWindow *imd, gint chan)
164 OverlayStateData *osd = image_get_osd_data(imd);
166 if (!osd || !osd->histogram) return;
168 histogram_set_channel(osd->histogram, chan);
169 image_osd_update(imd);
172 void image_osd_histogram_set_mode(ImageWindow *imd, gint mode)
174 OverlayStateData *osd = image_get_osd_data(imd);
176 if (!osd || !osd->histogram) return;
178 histogram_set_mode(osd->histogram, mode);
179 image_osd_update(imd);
182 gint image_osd_histogram_get_channel(ImageWindow *imd)
184 OverlayStateData *osd = image_get_osd_data(imd);
186 if (!osd || !osd->histogram) return HCHAN_DEFAULT;
188 return histogram_get_channel(osd->histogram);
191 gint image_osd_histogram_get_mode(ImageWindow *imd)
193 OverlayStateData *osd = image_get_osd_data(imd);
195 if (!osd || !osd->histogram) return 0;
197 return histogram_get_mode(osd->histogram);
200 void image_osd_toggle(ImageWindow *imd)
205 show = image_osd_get(imd);
206 if (show == OSD_SHOW_NOTHING)
208 image_osd_set(imd, static_cast<OsdShowFlags>(OSD_SHOW_INFO | OSD_SHOW_STATUS));
213 if (show & OSD_SHOW_HISTOGRAM)
215 image_osd_set(imd, OSD_SHOW_NOTHING);
219 image_osd_set(imd, static_cast<OsdShowFlags>(show | OSD_SHOW_HISTOGRAM));
224 static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
226 GdkPixbuf *pixbuf = nullptr;
232 const HistMap *histmap = nullptr;
233 ImageWindow *imd = osd->imd;
234 FileData *fd = image_get_fd(imd);
235 PangoFontDescription *font_desc;
237 if (!fd) return nullptr;
239 name = image_get_name(imd);
247 vars = g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, g_free);
249 cd = image_get_collection(imd, &info);
252 t = g_list_length(cd->list);
253 n = g_list_index(cd->list, info) + 1;
256 if (file_extension_match(cd->name, GQ_COLLECTION_EXT))
257 osd_template_insert(vars, "collection", remove_extension_from_path(cd->name), OSDT_FREE);
259 osd_template_insert(vars, "collection", cd->name, OSDT_NONE);
263 osd_template_insert(vars, "collection", _("Untitled"), OSDT_NONE);
268 LayoutWindow *lw = layout_find_by_image(imd);
273 n = g_list_length(lw->slideshow->list_done);
274 t = n + g_list_length(lw->slideshow->list);
279 t = layout_list_count(lw, nullptr);
280 n = layout_list_get_index(lw, image_get_fd(lw->image)) + 1;
283 else if (view_window_find_image(imd, &n, &t))
296 osd_template_insert(vars, "collection", nullptr, OSDT_NONE);
299 osd_template_insert(vars, "number", g_strdup_printf("%d", n), OSDT_NO_DUP);
300 osd_template_insert(vars, "total", g_strdup_printf("%d", t), OSDT_NO_DUP);
301 osd_template_insert(vars, "name", const_cast<gchar *>(name), OSDT_NONE);
302 osd_template_insert(vars, "path", const_cast<gchar *>(image_get_path(imd)), OSDT_NONE);
303 osd_template_insert(vars, "date", imd->image_fd ? (const_cast<gchar *>(text_from_time(imd->image_fd->date))) : "", OSDT_NONE);
304 osd_template_insert(vars, "size", imd->image_fd ? (text_from_size_abrev(imd->image_fd->size)) : g_strdup(""), OSDT_FREE);
305 osd_template_insert(vars, "zoom", image_zoom_get_as_text(imd), OSDT_FREE);
310 GdkPixbuf *load_pixbuf = image_loader_get_pixbuf(imd->il);
312 if (imd->delay_flip &&
313 imd->il && load_pixbuf &&
314 image_get_pixbuf(imd) != load_pixbuf)
316 w = gdk_pixbuf_get_width(load_pixbuf);
317 h = gdk_pixbuf_get_height(load_pixbuf);
321 image_get_image_size(imd, &w, &h);
325 osd_template_insert(vars, "width", g_strdup_printf("%d", w), OSDT_NO_DUP);
326 osd_template_insert(vars, "height", g_strdup_printf("%d", h), OSDT_NO_DUP);
327 osd_template_insert(vars, "res", g_strdup_printf("%d × %d", w, h), OSDT_FREE);
331 osd_template_insert(vars, "width", nullptr, OSDT_NONE);
332 osd_template_insert(vars, "height", nullptr, OSDT_NONE);
333 osd_template_insert(vars, "res", nullptr, OSDT_NONE);
336 text = image_osd_mkinfo(options->image_overlay.template_string, imd->image_fd, vars);
337 g_hash_table_destroy(vars);
340 /* When does this occur ?? */
341 text = g_markup_escape_text(_("Untitled"), -1);
344 with_hist = ((osd->show & OSD_SHOW_HISTOGRAM) && osd->histogram);
347 histmap = histmap_get(imd->image_fd);
350 histmap_start_idle(imd->image_fd);
357 gint active_marks = 0;
361 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
363 active_marks += file_data_get_mark(fd, mark);
366 if (active_marks > 0)
368 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
370 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
372 g_string_append_printf(buf, file_data_get_mark(fd, mark) ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + (mark < 9 ? mark : -1) );
376 text2 = g_strdup_printf("%s\n%s", text, buf->str);
378 text2 = g_strdup(buf->str);
379 g_string_free(buf, TRUE);
386 gchar *escaped_histogram_label = g_markup_escape_text(histogram_label(osd->histogram), -1);
388 text2 = g_strdup_printf("%s\n%s", text, escaped_histogram_label);
390 text2 = g_strdup(escaped_histogram_label);
391 g_free(escaped_histogram_label);
397 font_desc = pango_font_description_from_string(options->image_overlay.font);
398 layout = gtk_widget_create_pango_layout(imd->pr, nullptr);
399 pango_layout_set_font_description(layout, font_desc);
401 pango_layout_set_markup(layout, text, -1);
404 pango_layout_get_pixel_size(layout, &width, &height);
405 /* with empty text width is set to 0, but not height) */
408 else if (height == 0)
410 if (width > 0) width += 10;
411 if (height > 0) height += 10;
415 if (width < HISTOGRAM_WIDTH + 10) width = HISTOGRAM_WIDTH + 10;
416 height += HISTOGRAM_HEIGHT + 5;
419 if (width > 0 && height > 0)
421 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
422 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, options->image_overlay.background_red, options->image_overlay.background_green,
423 options->image_overlay.background_blue, options->image_overlay.background_alpha);
424 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
425 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
426 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
427 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
428 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
429 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
430 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
435 gint y = height - HISTOGRAM_HEIGHT - 5;
438 pixbuf_set_rect_fill(pixbuf, x, y, w, HISTOGRAM_HEIGHT, 220, 220, 220, 210);
439 histogram_draw(osd->histogram, histmap, pixbuf, x, y, w, HISTOGRAM_HEIGHT);
441 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, options->image_overlay.text_red, options->image_overlay.text_green,
442 options->image_overlay.text_blue, options->image_overlay.text_alpha);
445 g_object_unref(G_OBJECT(layout));
450 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
452 static GdkPixbuf **icons = nullptr;
453 GdkPixbuf *icon = nullptr;
455 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
456 if (icons[flag]) return icons[flag];
458 if (osd_icons[flag].key)
460 icon = pixbuf_inline(osd_icons[flag].key);
465 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
466 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
467 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
470 case IMAGE_OSD_ROTATE_AUTO:
471 pixbuf_set_rect(icon, 3, 8, 11, 12,
474 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
475 20, 9, 14, 15, 14, 3,
478 case IMAGE_OSD_ROTATE_USER:
480 case IMAGE_OSD_COLOR:
481 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
482 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
483 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
485 case IMAGE_OSD_FIRST:
486 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
487 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
488 12, 5, 18, 11, 6, 11,
492 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
493 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
494 12, 18, 6, 12, 18, 12,
498 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
499 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
511 static gint image_overlay_add(ImageWindow *imd, GdkPixbuf *pixbuf, gint x, gint y,
512 OverlayRendererFlags flags)
514 return pixbuf_renderer_overlay_add(reinterpret_cast<PixbufRenderer *>(imd->pr), pixbuf, x, y, flags);
517 static void image_overlay_set(ImageWindow *imd, gint id, GdkPixbuf *pixbuf, gint x, gint y)
519 pixbuf_renderer_overlay_set(reinterpret_cast<PixbufRenderer *>(imd->pr), id, pixbuf, x, y);
522 static void image_overlay_remove(ImageWindow *imd, gint id)
524 pixbuf_renderer_overlay_remove(reinterpret_cast<PixbufRenderer *>(imd->pr), id);
527 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
531 if (osd->icon_id[flag]) return;
533 pixbuf = image_osd_icon_pixbuf(flag);
536 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
537 osd_icons[flag].x, osd_icons[flag].y,
541 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
543 if (osd->icon_id[flag])
545 image_overlay_remove(osd->imd, osd->icon_id[flag]);
546 osd->icon_id[flag] = 0;
550 static void image_osd_icons_reset_time(OverlayStateData *osd)
554 for (i = 0; i < IMAGE_OSD_COUNT; i++)
556 if (osd_icons[i].reset)
558 osd->icon_time[i] = 0;
563 static void image_osd_icons_update(OverlayStateData *osd)
567 for (i = 0; i < IMAGE_OSD_COUNT; i++)
569 if (osd->icon_time[i] > 0)
571 image_osd_icon_show(osd, static_cast<ImageOSDFlag>(i));
575 image_osd_icon_hide(osd, static_cast<ImageOSDFlag>(i));
580 static void image_osd_icons_hide(OverlayStateData *osd)
584 for (i = 0; i < IMAGE_OSD_COUNT; i++)
586 image_osd_icon_hide(osd, static_cast<ImageOSDFlag>(i));
590 static void image_osd_info_show(OverlayStateData *osd, GdkPixbuf *pixbuf)
592 if (osd->ovl_info == 0)
594 osd->ovl_info = image_overlay_add(osd->imd, pixbuf, osd->x, osd->y, osd->origin);
598 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, osd->x, osd->y);
602 static void image_osd_info_hide(OverlayStateData *osd)
604 if (osd->ovl_info == 0) return;
606 image_overlay_remove(osd->imd, osd->ovl_info);
610 static gboolean image_osd_update_cb(gpointer data)
612 auto osd = static_cast<OverlayStateData *>(data);
614 if (osd->show & OSD_SHOW_INFO)
616 /* redraw when the image was changed,
617 with histogram we have to redraw also when loading is finished */
618 if (osd->changed_states & IMAGE_STATE_IMAGE ||
619 (osd->changed_states & IMAGE_STATE_LOADING && osd->show & OSD_SHOW_HISTOGRAM) ||
620 osd->notify & NOTIFY_HISTMAP)
624 pixbuf = image_osd_info_render(osd);
627 image_osd_info_show(osd, pixbuf);
628 g_object_unref(pixbuf);
632 image_osd_info_hide(osd);
638 image_osd_info_hide(osd);
641 if (osd->show & OSD_SHOW_STATUS)
643 if (osd->changed_states & IMAGE_STATE_IMAGE)
644 image_osd_icons_reset_time(osd);
646 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
648 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
649 image_osd_timer_schedule(osd);
652 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
656 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
659 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
662 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
663 image_osd_timer_schedule(osd);
666 image_osd_icons_update(osd);
670 image_osd_icons_hide(osd);
673 osd->changed_states = IMAGE_STATE_NONE;
674 osd->notify = static_cast<NotifyType>(0);
676 return G_SOURCE_REMOVE;
679 static void image_osd_update_schedule(OverlayStateData *osd, gboolean force)
681 if (force) osd->changed_states = static_cast<ImageState>(osd->changed_states | IMAGE_STATE_IMAGE);
685 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, nullptr);
689 void image_osd_update(ImageWindow *imd)
691 OverlayStateData *osd = image_get_osd_data(imd);
695 image_osd_update_schedule(osd, TRUE);
698 static gboolean image_osd_timer_cb(gpointer data)
700 auto osd = static_cast<OverlayStateData *>(data);
701 gboolean done = TRUE;
702 gboolean changed = FALSE;
705 for (i = 0; i < IMAGE_OSD_COUNT; i++)
707 if (osd->icon_time[i] > 1)
710 if (osd->icon_time[i] < 2)
712 osd->icon_time[i] = 0;
722 if (changed) image_osd_update_schedule(osd, FALSE);
733 static void image_osd_timer_schedule(OverlayStateData *osd)
737 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
741 static void image_osd_state_cb(ImageWindow *, ImageState state, gpointer data)
743 auto osd = static_cast<OverlayStateData *>(data);
745 osd->changed_states = static_cast<ImageState>(osd->changed_states | state);
746 image_osd_update_schedule(osd, FALSE);
749 static void image_osd_notify_cb(FileData *fd, NotifyType type, gpointer data)
751 auto osd = static_cast<OverlayStateData *>(data);
753 if ((type & (NOTIFY_HISTMAP)) && osd->imd && fd == osd->imd->image_fd)
755 DEBUG_1("Notify osd: %s %04x", fd->path, type);
756 osd->notify = static_cast<NotifyType>(osd->notify | type);
757 image_osd_update_schedule(osd, FALSE);
762 static void image_osd_free(OverlayStateData *osd)
766 if (osd->idle_id) g_source_remove(osd->idle_id);
767 if (osd->timer_id) g_source_remove(osd->timer_id);
769 file_data_unregister_notify_func(image_osd_notify_cb, osd);
773 image_set_osd_data(osd->imd, nullptr);
774 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
776 image_set_state_func(osd->imd, nullptr, nullptr);
778 image_osd_info_hide(osd);
779 image_osd_icons_hide(osd);
782 if (osd->histogram) histogram_free(osd->histogram);
787 static void image_osd_destroy_cb(GtkWidget *, gpointer data)
789 auto osd = static_cast<OverlayStateData *>(data);
795 static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
797 OverlayStateData *osd = image_get_osd_data(imd);
801 osd = g_new0(OverlayStateData, 1);
803 osd->show = OSD_SHOW_NOTHING;
804 osd->x = options->image_overlay.x;
805 osd->y = options->image_overlay.y;
806 osd->origin = OVL_RELATIVE;
808 osd->histogram = histogram_new();
810 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
811 G_CALLBACK(image_osd_destroy_cb), osd);
812 image_set_osd_data(imd, osd);
814 image_set_state_func(osd->imd, image_osd_state_cb, osd);
815 file_data_register_notify_func(image_osd_notify_cb, osd, NOTIFY_PRIORITY_LOW);
818 if (show & OSD_SHOW_STATUS)
819 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
821 if (show != osd->show)
822 image_osd_update_schedule(osd, TRUE);
827 void image_osd_set(ImageWindow *imd, OsdShowFlags show)
831 image_osd_enable(imd, show);
834 OsdShowFlags image_osd_get(ImageWindow *imd)
836 OverlayStateData *osd = image_get_osd_data(imd);
838 return osd ? osd->show : OSD_SHOW_NOTHING;
841 Histogram *image_osd_get_histogram(ImageWindow *imd)
843 OverlayStateData *osd = image_get_osd_data(imd);
845 return osd ? osd->histogram : nullptr;
848 void image_osd_copy_status(ImageWindow *src, ImageWindow *dest)
850 Histogram *h_src, *h_dest;
851 image_osd_set(dest, image_osd_get(src));
853 h_src = image_osd_get_histogram(src);
854 h_dest = image_osd_get_histogram(dest);
856 h_dest->histogram_mode = h_src->histogram_mode;
857 h_dest->histogram_channel = h_src->histogram_channel;
864 2+ = show for duration tenths of a second
865 -1 = use default duration
867 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
869 OverlayStateData *osd = image_get_osd_data(imd);
873 if (flag >= IMAGE_OSD_COUNT) return;
874 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
875 if (duration > 1) duration += 1;
877 osd->icon_time[flag] = duration;
879 image_osd_update_schedule(osd, FALSE);
880 image_osd_timer_schedule(osd);
882 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */