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.
22 #include "image-overlay.h"
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27 #include <glib-object.h>
29 #include <pango/pango.h>
34 #include "histogram.h"
35 #include "image-load.h"
40 #include "main-defines.h"
43 #include "pixbuf-renderer.h"
44 #include "pixbuf-util.h"
45 #include "slideshow.h"
47 #include "ui-fileops.h"
52 *----------------------------------------------------------------------------
54 *----------------------------------------------------------------------------
58 struct OverlayStateData {
60 ImageState changed_states;
66 OverlayRendererFlags origin;
73 gint icon_time[IMAGE_OSD_COUNT];
74 gint icon_id[IMAGE_OSD_COUNT];
76 guint idle_id; /* event source id */
77 guint timer_id; /* event source id */
83 gboolean reset; /* reset on new image */
84 gint x; /* x, y offset */
86 gchar *key; /* inline pixbuf */
89 static OSDIcon osd_icons[] = {
90 { TRUE, 0, 0, nullptr }, /* none */
91 { TRUE, -10, -10, nullptr }, /* auto rotated */
92 { TRUE, -10, -10, nullptr }, /* user rotated */
93 { TRUE, -40, -10, nullptr }, /* color embedded */
94 { TRUE, -70, -10, nullptr }, /* first image */
95 { TRUE, -70, -10, nullptr }, /* last image */
96 { FALSE, -70, -10, nullptr }, /* osd enabled */
97 { FALSE, 0, 0, nullptr }
100 #define OSD_DATA "overlay-data"
103 IMAGE_OSD_DEFAULT_DURATION = 30
107 HISTOGRAM_HEIGHT = 140,
108 HISTOGRAM_WIDTH = 256
111 static void image_osd_timer_schedule(OverlayStateData *osd);
113 void set_image_overlay_template_string(gchar **template_string, const gchar *value)
115 g_assert(template_string);
117 g_free(*template_string);
118 *template_string = g_strdup(value);
122 void set_default_image_overlay_template_string(gchar **template_string)
124 set_image_overlay_template_string(template_string, DEFAULT_OVERLAY_INFO);
127 void set_image_overlay_font_string(gchar **font_string, const gchar *value)
129 g_assert(font_string);
131 g_free(*font_string);
132 *font_string = g_strdup(value);
135 static OverlayStateData *image_get_osd_data(ImageWindow *imd)
137 OverlayStateData *osd;
139 if (!imd) return nullptr;
143 osd = static_cast<OverlayStateData *>(g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA"));
147 static void image_set_osd_data(ImageWindow *imd, OverlayStateData *osd)
151 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
155 *----------------------------------------------------------------------------
157 *----------------------------------------------------------------------------
161 void image_osd_histogram_toggle_channel(ImageWindow *imd)
163 OverlayStateData *osd = image_get_osd_data(imd);
165 if (!osd || !osd->histogram) return;
167 histogram_toggle_channel(osd->histogram);
168 image_osd_update(imd);
171 void image_osd_histogram_toggle_mode(ImageWindow *imd)
173 OverlayStateData *osd = image_get_osd_data(imd);
175 if (!osd || !osd->histogram) return;
177 histogram_toggle_mode(osd->histogram);
178 image_osd_update(imd);
181 void image_osd_histogram_set_channel(ImageWindow *imd, gint chan)
183 OverlayStateData *osd = image_get_osd_data(imd);
185 if (!osd || !osd->histogram) return;
187 histogram_set_channel(osd->histogram, chan);
188 image_osd_update(imd);
191 void image_osd_histogram_set_mode(ImageWindow *imd, gint mode)
193 OverlayStateData *osd = image_get_osd_data(imd);
195 if (!osd || !osd->histogram) return;
197 histogram_set_mode(osd->histogram, mode);
198 image_osd_update(imd);
201 gint image_osd_histogram_get_channel(ImageWindow *imd)
203 OverlayStateData *osd = image_get_osd_data(imd);
205 if (!osd || !osd->histogram) return HCHAN_DEFAULT;
207 return histogram_get_channel(osd->histogram);
210 gint image_osd_histogram_get_mode(ImageWindow *imd)
212 OverlayStateData *osd = image_get_osd_data(imd);
214 if (!osd || !osd->histogram) return 0;
216 return histogram_get_mode(osd->histogram);
219 void image_osd_toggle(ImageWindow *imd)
224 show = image_osd_get(imd);
225 if (show == OSD_SHOW_NOTHING)
227 image_osd_set(imd, static_cast<OsdShowFlags>(OSD_SHOW_INFO | OSD_SHOW_STATUS));
231 if (show & OSD_SHOW_HISTOGRAM)
233 image_osd_set(imd, OSD_SHOW_NOTHING);
237 image_osd_set(imd, static_cast<OsdShowFlags>(show | OSD_SHOW_HISTOGRAM));
241 static GdkPixbuf *image_osd_info_render(OverlayStateData *osd)
243 GdkPixbuf *pixbuf = nullptr;
250 const HistMap *histmap = nullptr;
251 ImageWindow *imd = osd->imd;
252 FileData *fd = image_get_fd(imd);
253 PangoFontDescription *font_desc;
255 if (!fd) return nullptr;
257 name = image_get_name(imd);
266 vars = g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, g_free);
268 cd = image_get_collection(imd, &info);
271 t = g_list_length(cd->list);
272 n = g_list_index(cd->list, info) + 1;
275 if (file_extension_match(cd->name, GQ_COLLECTION_EXT))
276 osd_template_insert(vars, "collection", remove_extension_from_path(cd->name), OSDT_FREE);
278 osd_template_insert(vars, "collection", cd->name, OSDT_NONE);
282 osd_template_insert(vars, "collection", _("Untitled"), OSDT_NONE);
287 LayoutWindow *lw = layout_find_by_image(imd);
292 n = g_list_length(lw->slideshow->list_done);
293 t = n + g_list_length(lw->slideshow->list);
298 t = layout_list_count(lw, nullptr);
299 n = layout_list_get_index(lw, image_get_fd(lw->image)) + 1;
302 else if (view_window_find_image(imd, &n, &t))
315 osd_template_insert(vars, "collection", nullptr, OSDT_NONE);
318 osd_template_insert(vars, "number", g_strdup_printf("%d", n), OSDT_NO_DUP);
319 osd_template_insert(vars, "total", g_strdup_printf("%d", t), OSDT_NO_DUP);
320 osd_template_insert(vars, "name", const_cast<gchar *>(name), OSDT_NONE);
321 osd_template_insert(vars, "path", const_cast<gchar *>(image_get_path(imd)), OSDT_NONE);
322 osd_template_insert(vars, "date", imd->image_fd ? (const_cast<gchar *>(text_from_time(imd->image_fd->date))) : "", OSDT_NONE);
323 osd_template_insert(vars, "size", imd->image_fd ? (text_from_size_abrev(imd->image_fd->size)) : g_strdup(""), OSDT_FREE);
324 osd_template_insert(vars, "zoom", image_zoom_get_as_text(imd), OSDT_FREE);
330 GdkPixbuf *load_pixbuf = image_loader_get_pixbuf(imd->il);
332 if (imd->delay_flip &&
333 imd->il && load_pixbuf &&
334 image_get_pixbuf(imd) != load_pixbuf)
336 w = gdk_pixbuf_get_width(load_pixbuf);
337 h = gdk_pixbuf_get_height(load_pixbuf);
341 image_get_image_size(imd, &w, &h);
345 osd_template_insert(vars, "width", g_strdup_printf("%d", w), OSDT_NO_DUP);
346 osd_template_insert(vars, "height", g_strdup_printf("%d", h), OSDT_NO_DUP);
347 osd_template_insert(vars, "res", g_strdup_printf("%d × %d", w, h), OSDT_FREE);
351 osd_template_insert(vars, "width", nullptr, OSDT_NONE);
352 osd_template_insert(vars, "height", nullptr, OSDT_NONE);
353 osd_template_insert(vars, "res", nullptr, OSDT_NONE);
356 text = image_osd_mkinfo(options->image_overlay.template_string, imd->image_fd, vars);
357 g_hash_table_destroy(vars);
360 /* When does this occur ?? */
361 text = g_markup_escape_text(_("Untitled"), -1);
364 with_hist = ((osd->show & OSD_SHOW_HISTOGRAM) && osd->histogram);
367 histmap = histmap_get(imd->image_fd);
370 histmap_start_idle(imd->image_fd);
377 gint active_marks = 0;
381 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
383 active_marks += file_data_get_mark(fd, mark);
386 if (active_marks > 0)
388 GString *buf = g_string_sized_new(strlen(text) + 1 + FILEDATA_MARKS_SIZE * 2);
392 g_string_append_printf(buf, "%s\n", text);
395 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
397 g_string_append_printf(buf, file_data_get_mark(fd, mark) ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + (mark < 9 ? mark : -1) );
401 text = g_string_free(buf, FALSE);
406 gchar *escaped_histogram_label = g_markup_escape_text(histogram_label(osd->histogram), -1);
408 text2 = g_strdup_printf("%s\n%s", text, escaped_histogram_label);
410 text2 = g_strdup(escaped_histogram_label);
411 g_free(escaped_histogram_label);
417 font_desc = pango_font_description_from_string(options->image_overlay.font);
418 layout = gtk_widget_create_pango_layout(imd->pr, nullptr);
419 pango_layout_set_font_description(layout, font_desc);
421 pango_layout_set_markup(layout, text, -1);
424 pango_layout_get_pixel_size(layout, &width, &height);
425 /* with empty text width is set to 0, but not height) */
428 else if (height == 0)
430 if (width > 0) width += 10;
431 if (height > 0) height += 10;
435 if (width < HISTOGRAM_WIDTH + 10) width = HISTOGRAM_WIDTH + 10;
436 height += HISTOGRAM_HEIGHT + 5;
439 if (width > 0 && height > 0)
441 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
442 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, options->image_overlay.background_red, options->image_overlay.background_green,
443 options->image_overlay.background_blue, options->image_overlay.background_alpha);
444 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
445 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
446 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
447 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
448 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
449 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
450 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
455 gint y = height - HISTOGRAM_HEIGHT - 5;
458 pixbuf_set_rect_fill(pixbuf, x, y, w, HISTOGRAM_HEIGHT, 220, 220, 220, 210);
459 histogram_draw(osd->histogram, histmap, pixbuf, x, y, w, HISTOGRAM_HEIGHT);
461 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, options->image_overlay.text_red, options->image_overlay.text_green,
462 options->image_overlay.text_blue, options->image_overlay.text_alpha);
465 g_object_unref(G_OBJECT(layout));
471 * @brief Create non-standard icons for the OSD
476 * \image html image-osd-color.png
478 * \image html image-osd-first.png
480 * \image html image-osd-icon.png
482 * \image html image-osd-last.png
483 * IMAGE_OSD_ROTATE_AUTO
484 * \image html image-osd-rotate-auto.png
487 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
489 static GdkPixbuf **icons = nullptr;
490 GdkPixbuf *icon = nullptr;
492 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
493 if (icons[flag]) return icons[flag];
495 if (osd_icons[flag].key)
497 icon = pixbuf_inline(osd_icons[flag].key);
502 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
503 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
504 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
507 case IMAGE_OSD_ROTATE_AUTO:
508 pixbuf_set_rect(icon, 3, 8, 11, 12,
511 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
512 20, 9, 14, 15, 14, 3,
515 case IMAGE_OSD_ROTATE_USER:
517 case IMAGE_OSD_COLOR:
518 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
519 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
520 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
522 case IMAGE_OSD_FIRST:
523 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
524 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
525 12, 5, 18, 11, 6, 11,
529 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
530 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
531 12, 18, 6, 12, 18, 12,
535 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
536 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
548 static gint image_overlay_add(ImageWindow *imd, GdkPixbuf *pixbuf, gint x, gint y,
549 OverlayRendererFlags flags)
551 return pixbuf_renderer_overlay_add(reinterpret_cast<PixbufRenderer *>(imd->pr), pixbuf, x, y, flags);
554 static void image_overlay_set(ImageWindow *imd, gint id, GdkPixbuf *pixbuf, gint x, gint y)
556 pixbuf_renderer_overlay_set(reinterpret_cast<PixbufRenderer *>(imd->pr), id, pixbuf, x, y);
559 static void image_overlay_remove(ImageWindow *imd, gint id)
561 pixbuf_renderer_overlay_remove(reinterpret_cast<PixbufRenderer *>(imd->pr), id);
564 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
568 if (osd->icon_id[flag]) return;
570 pixbuf = image_osd_icon_pixbuf(flag);
573 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
574 osd_icons[flag].x, osd_icons[flag].y,
578 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
580 if (osd->icon_id[flag])
582 image_overlay_remove(osd->imd, osd->icon_id[flag]);
583 osd->icon_id[flag] = 0;
587 static void image_osd_icons_reset_time(OverlayStateData *osd)
591 for (i = 0; i < IMAGE_OSD_COUNT; i++)
593 if (osd_icons[i].reset)
595 osd->icon_time[i] = 0;
600 static void image_osd_icons_update(OverlayStateData *osd)
604 for (i = 0; i < IMAGE_OSD_COUNT; i++)
606 if (osd->icon_time[i] > 0)
608 image_osd_icon_show(osd, static_cast<ImageOSDFlag>(i));
612 image_osd_icon_hide(osd, static_cast<ImageOSDFlag>(i));
617 static void image_osd_icons_hide(OverlayStateData *osd)
621 for (i = 0; i < IMAGE_OSD_COUNT; i++)
623 image_osd_icon_hide(osd, static_cast<ImageOSDFlag>(i));
627 static void image_osd_info_show(OverlayStateData *osd, GdkPixbuf *pixbuf)
629 if (osd->ovl_info == 0)
631 osd->ovl_info = image_overlay_add(osd->imd, pixbuf, osd->x, osd->y, osd->origin);
635 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, osd->x, osd->y);
639 static void image_osd_info_hide(OverlayStateData *osd)
641 if (osd->ovl_info == 0) return;
643 image_overlay_remove(osd->imd, osd->ovl_info);
647 static gboolean image_osd_update_cb(gpointer data)
649 auto osd = static_cast<OverlayStateData *>(data);
651 if (osd->show & OSD_SHOW_INFO)
653 /* redraw when the image was changed,
654 with histogram we have to redraw also when loading is finished */
655 if (osd->changed_states & IMAGE_STATE_IMAGE ||
656 (osd->changed_states & IMAGE_STATE_LOADING && osd->show & OSD_SHOW_HISTOGRAM) ||
657 osd->notify & NOTIFY_HISTMAP)
661 pixbuf = image_osd_info_render(osd);
664 image_osd_info_show(osd, pixbuf);
665 g_object_unref(pixbuf);
669 image_osd_info_hide(osd);
675 image_osd_info_hide(osd);
678 if (osd->show & OSD_SHOW_STATUS)
680 if (osd->changed_states & IMAGE_STATE_IMAGE)
681 image_osd_icons_reset_time(osd);
683 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
685 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
686 image_osd_timer_schedule(osd);
689 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
693 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
696 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
699 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
700 image_osd_timer_schedule(osd);
703 image_osd_icons_update(osd);
707 image_osd_icons_hide(osd);
710 osd->changed_states = IMAGE_STATE_NONE;
711 osd->notify = static_cast<NotifyType>(0);
713 return G_SOURCE_REMOVE;
716 static void image_osd_update_schedule(OverlayStateData *osd, gboolean force)
718 if (force) osd->changed_states = static_cast<ImageState>(osd->changed_states | IMAGE_STATE_IMAGE);
722 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, nullptr);
726 void image_osd_update(ImageWindow *imd)
728 OverlayStateData *osd = image_get_osd_data(imd);
732 image_osd_update_schedule(osd, TRUE);
735 static gboolean image_osd_timer_cb(gpointer data)
737 auto osd = static_cast<OverlayStateData *>(data);
738 gboolean done = TRUE;
739 gboolean changed = FALSE;
742 for (i = 0; i < IMAGE_OSD_COUNT; i++)
744 if (osd->icon_time[i] > 1)
747 if (osd->icon_time[i] < 2)
749 osd->icon_time[i] = 0;
759 if (changed) image_osd_update_schedule(osd, FALSE);
770 static void image_osd_timer_schedule(OverlayStateData *osd)
774 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
778 static void image_osd_state_cb(ImageWindow *, ImageState state, gpointer data)
780 auto osd = static_cast<OverlayStateData *>(data);
782 osd->changed_states = static_cast<ImageState>(osd->changed_states | state);
783 image_osd_update_schedule(osd, FALSE);
786 static void image_osd_notify_cb(FileData *fd, NotifyType type, gpointer data)
788 auto osd = static_cast<OverlayStateData *>(data);
790 if ((type & (NOTIFY_HISTMAP)) && osd->imd && fd == osd->imd->image_fd)
792 DEBUG_1("Notify osd: %s %04x", fd->path, type);
793 osd->notify = static_cast<NotifyType>(osd->notify | type);
794 image_osd_update_schedule(osd, FALSE);
799 static void image_osd_free(OverlayStateData *osd)
803 if (osd->idle_id) g_source_remove(osd->idle_id);
804 if (osd->timer_id) g_source_remove(osd->timer_id);
806 file_data_unregister_notify_func(image_osd_notify_cb, osd);
810 image_set_osd_data(osd->imd, nullptr);
811 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
813 image_set_state_func(osd->imd, nullptr, nullptr);
815 image_osd_info_hide(osd);
816 image_osd_icons_hide(osd);
819 if (osd->histogram) histogram_free(osd->histogram);
824 static void image_osd_destroy_cb(GtkWidget *, gpointer data)
826 auto osd = static_cast<OverlayStateData *>(data);
832 static void image_osd_enable(ImageWindow *imd, OsdShowFlags show)
834 OverlayStateData *osd = image_get_osd_data(imd);
838 osd = g_new0(OverlayStateData, 1);
840 osd->show = OSD_SHOW_NOTHING;
841 osd->x = options->image_overlay.x;
842 osd->y = options->image_overlay.y;
843 osd->origin = OVL_RELATIVE;
845 osd->histogram = histogram_new();
847 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
848 G_CALLBACK(image_osd_destroy_cb), osd);
849 image_set_osd_data(imd, osd);
851 image_set_state_func(osd->imd, image_osd_state_cb, osd);
852 file_data_register_notify_func(image_osd_notify_cb, osd, NOTIFY_PRIORITY_LOW);
855 if (show & OSD_SHOW_STATUS)
856 image_osd_icon(imd, IMAGE_OSD_ICON, -1);
858 if (show != osd->show)
859 image_osd_update_schedule(osd, TRUE);
864 void image_osd_set(ImageWindow *imd, OsdShowFlags show)
868 image_osd_enable(imd, show);
871 OsdShowFlags image_osd_get(ImageWindow *imd)
873 OverlayStateData *osd = image_get_osd_data(imd);
875 return osd ? osd->show : OSD_SHOW_NOTHING;
878 Histogram *image_osd_get_histogram(ImageWindow *imd)
880 OverlayStateData *osd = image_get_osd_data(imd);
882 return osd ? osd->histogram : nullptr;
885 void image_osd_copy_status(ImageWindow *src, ImageWindow *dest)
889 image_osd_set(dest, image_osd_get(src));
891 h_src = image_osd_get_histogram(src);
892 h_dest = image_osd_get_histogram(dest);
894 h_dest->histogram_mode = h_src->histogram_mode;
895 h_dest->histogram_channel = h_src->histogram_channel;
902 2+ = show for duration tenths of a second
903 -1 = use default duration
905 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
907 OverlayStateData *osd = image_get_osd_data(imd);
911 if (flag >= IMAGE_OSD_COUNT) return;
912 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
913 if (duration > 1) duration += 1;
915 osd->icon_time[flag] = duration;
917 image_osd_update_schedule(osd, FALSE);
918 image_osd_timer_schedule(osd);
920 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */