7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
13 #include "image-overlay.h"
21 #include "pixbuf-renderer.h"
22 #include "pixbuf_util.h"
23 #include "histogram.h"
27 *----------------------------------------------------------------------------
29 *----------------------------------------------------------------------------
32 typedef struct _OverlayStateData OverlayStateData;
33 struct _OverlayStateData {
35 ImageState changed_states;
42 gint icon_time[IMAGE_OSD_COUNT];
43 gint icon_id[IMAGE_OSD_COUNT];
51 typedef struct _OSDIcon OSDIcon;
53 gint reset; /* reset on new image */
54 gint x; /* x, y offset */
56 gchar *key; /* inline pixbuf */
59 static OSDIcon osd_icons[] = {
60 { TRUE, 0, 0, NULL }, /* none */
61 { TRUE, -10, -10, NULL }, /* auto rotated */
62 { TRUE, -10, -10, NULL }, /* user rotated */
63 { TRUE, -40, -10, NULL }, /* color embedded */
64 { TRUE, -70, -10, NULL }, /* first image */
65 { TRUE, -70, -10, NULL }, /* last image */
66 { FALSE, -70, -10, NULL }, /* osd enabled */
70 #define OSD_DATA "overlay-data"
73 #define OSD_INFO_Y -10
75 #define IMAGE_OSD_DEFAULT_DURATION 30
77 #define HISTOGRAM_HEIGHT 140
79 void set_default_image_overlay_template_string(ConfOptions *options)
81 if (options->image_overlay.common.template_string) g_free(options->image_overlay.common.template_string);
82 options->image_overlay.common.template_string = g_strdup(DEFAULT_OVERLAY_INFO);
86 *----------------------------------------------------------------------------
88 *----------------------------------------------------------------------------
92 #define HIST_PREPARE(imd, lw) \
93 LayoutWindow *lw = NULL; \
95 lw = layout_find_by_image(imd);
97 void image_osd_histogram_onoff_toggle(ImageWindow *imd, gint x)
102 lw->histogram_enabled = !!(x);
103 if (lw->histogram_enabled && !lw->histogram)
104 lw->histogram = histogram_new();
108 gint image_osd_histogram_onoff_status(ImageWindow *imd)
110 HIST_PREPARE(imd, lw)
111 return lw ? lw->histogram_enabled : FALSE;
114 void image_osd_histogram_chan_toggle(ImageWindow *imd)
116 HIST_PREPARE(imd, lw)
117 if (lw && lw->histogram)
118 histogram_set_channel(lw->histogram, (histogram_get_channel(lw->histogram) +1)%HCHAN_COUNT);
121 void image_osd_histogram_log_toggle(ImageWindow *imd)
124 if (lw && lw->histogram)
125 histogram_set_mode(lw->histogram, !histogram_get_mode(lw->histogram));
130 static void image_osd_timer_schedule(OverlayStateData *osd);
132 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
134 gchar delim = '%', imp = '|', sep[] = " - ";
143 if (!str || !*str) return g_strdup("");
145 new = g_string_new(str);
147 exif = exif_read_fd(imd->image_fd);
158 start = strchr(new->str, delim);
161 end = strchr(start+1, delim);
165 for (p = end; p > start; p--)
167 if (*p == ':' && was_digit)
172 was_digit = g_ascii_isdigit(*p);
175 if (trunc) limit = atoi(trunc+1);
177 name = g_strndup(start+1, ((limit > 0) ? trunc : end)-start-1);
179 pos = start-new->str;
180 data = g_strdup(g_hash_table_lookup(vars, name));
181 if (data && strcmp(name, "zoom") == 0) imd->overlay_show_zoom = TRUE;
183 data = exif_get_data_as_text(exif, name);
184 if (data && *data && limit > 0 && strlen(data) > limit + 3)
186 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
191 g_string_erase(new, pos, end-start+1);
193 g_string_insert(new, pos, data);
194 if (pos-prev == 2 && new->str[pos-1] == imp)
196 g_string_erase(new, --pos, 1);
199 g_string_insert(new, pos, sep);
204 prev = data ? pos+strlen(data)-1 : pos-1;
205 last = data ? TRUE : last;
210 /* search and destroy empty lines */
212 while ((start = strchr(end, '\n')))
215 while (*++(end) == '\n')
217 g_string_erase(new, start-new->str, end-start-1);
220 g_strchomp(new->str);
223 g_string_free(new, FALSE);
228 static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
230 GdkPixbuf *pixbuf = NULL;
240 GdkPixbuf *imgpixbuf = NULL;
241 LayoutWindow *lw = NULL;
247 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
249 name = image_get_name(imd);
252 name_escaped = g_markup_escape_text(name, -1);
259 cd = image_get_collection(imd, &info);
264 t = g_list_length(cd->list);
265 n = g_list_index(cd->list, info) + 1;
266 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
267 ct = g_strdup_printf("<i>%s</i>\n", buf);
272 lw = layout_find_by_image(imd);
277 n = g_list_length(lw->slideshow->list_done);
278 t = n + g_list_length(lw->slideshow->list);
283 t = layout_list_count(lw, NULL);
284 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
287 else if (view_window_find_image(imd, &n, &t))
303 size = text_from_size_abrev(imd->size);
306 if (imd->delay_flip &&
307 imd->il && imd->il->pixbuf &&
308 image_get_pixbuf(imd) != imd->il->pixbuf)
310 w = gdk_pixbuf_get_width(imd->il->pixbuf);
311 h = gdk_pixbuf_get_height(imd->il->pixbuf);
312 imgpixbuf = imd->il->pixbuf;
316 pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
317 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
320 lw = layout_find_by_image(imd);
322 if (imgpixbuf && lw && lw->histogram && lw->histogram_enabled
323 && (!imd->il || imd->il->done))
326 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
327 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
328 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
331 g_hash_table_insert(vars, "collection", g_strdup(ct));
332 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
333 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
334 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
335 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
336 g_hash_table_insert(vars, "size", g_strdup(size));
337 g_hash_table_insert(vars, "zoom", image_zoom_get_as_text(imd));
341 text = g_strdup_printf(_("Untitled"));
345 text = image_osd_mkinfo(options->image_overlay.common.template_string, imd, vars);
350 g_free(name_escaped);
351 g_hash_table_destroy(vars);
354 FileData *fd = image_get_fd(imd);
356 if (fd) /* fd may be null after file deletion */
358 gint active_marks = 0;
362 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
364 active_marks += fd->marks[mark];
368 if (active_marks > 0)
370 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
372 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
374 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
378 text2 = g_strdup_printf("%s\n%s", text, buf->str);
380 text2 = g_strdup(buf->str);
381 g_string_free(buf, TRUE);
389 text2 = g_strdup_printf("%s\n%s", text, histogram_label(lw->histogram));
391 text2 = g_strdup(histogram_label(lw->histogram));
398 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
399 pango_layout_set_markup(layout, text, -1);
402 pango_layout_get_pixel_size(layout, &width, &height);
403 /* with empty text width is set to 0, but not height) */
406 else if (height == 0)
408 if (width > 0) width += 10;
409 if (height > 0) height += 10;
413 histogram_read(lw->histogram, imgpixbuf);
414 if (width < 266) width = 266;
415 height += HISTOGRAM_HEIGHT + 5;
418 if (width > 0 && height > 0)
420 /* TODO: make osd color configurable --Zas */
421 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
422 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
423 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
424 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
425 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
426 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
427 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
428 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
429 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
432 histogram_draw(lw->histogram, pixbuf, 5, height - HISTOGRAM_HEIGHT - 5 , width - 10, HISTOGRAM_HEIGHT);
434 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
437 g_object_unref(G_OBJECT(layout));
442 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
444 static GdkPixbuf **icons = NULL;
445 GdkPixbuf *icon = NULL;
447 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
448 if (icons[flag]) return icons[flag];
450 if (osd_icons[flag].key)
452 icon = pixbuf_inline(osd_icons[flag].key);
457 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
458 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
459 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
462 case IMAGE_OSD_ROTATE_AUTO:
463 pixbuf_set_rect(icon, 3, 8, 11, 12,
466 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
467 20, 9, 14, 15, 14, 3,
470 case IMAGE_OSD_ROTATE_USER:
472 case IMAGE_OSD_COLOR:
473 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
474 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
475 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
477 case IMAGE_OSD_FIRST:
478 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
479 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
480 12, 5, 18, 11, 6, 11,
484 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
485 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
486 12, 18, 6, 12, 18, 12,
490 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
491 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
503 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
507 if (osd->icon_id[flag]) return;
509 pixbuf = image_osd_icon_pixbuf(flag);
512 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
513 osd_icons[flag].x, osd_icons[flag].y,
517 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
519 if (osd->icon_id[flag])
521 image_overlay_remove(osd->imd, osd->icon_id[flag]);
522 osd->icon_id[flag] = 0;
526 static gint image_osd_update_cb(gpointer data)
528 OverlayStateData *osd = data;
530 osd->imd->overlay_show_zoom = FALSE;
534 if (osd->changed_states & IMAGE_STATE_IMAGE)
538 pixbuf = image_osd_info_render(osd->imd);
541 if (osd->ovl_info == 0)
543 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
544 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
548 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
550 g_object_unref(pixbuf);
552 else if (osd->ovl_info)
554 image_overlay_remove(osd->imd, osd->ovl_info);
563 image_overlay_remove(osd->imd, osd->ovl_info);
568 if (osd->show_status)
572 if (osd->changed_states & IMAGE_STATE_IMAGE)
574 for (i = 0; i < IMAGE_OSD_COUNT; i++)
576 if (osd_icons[i].reset) osd->icon_time[i] = 0;
580 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
582 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
583 image_osd_timer_schedule(osd);
586 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
590 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
593 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
596 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
597 image_osd_timer_schedule(osd);
600 for (i = 0; i < IMAGE_OSD_COUNT; i++)
602 if (osd->icon_time[i] > 0)
604 image_osd_icon_show(osd, i);
608 image_osd_icon_hide(osd, i);
616 for (i = 0; i < IMAGE_OSD_COUNT; i++)
618 image_osd_icon_hide(osd, i);
622 if (osd->imd->il && osd->imd->il->done)
623 osd->changed_states = IMAGE_STATE_NONE;
628 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
630 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
632 if (osd->idle_id == -1)
634 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
638 void image_osd_update(ImageWindow *imd)
640 OverlayStateData *osd;
644 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
647 image_osd_update_schedule(osd, TRUE);
650 static gint image_osd_timer_cb(gpointer data)
652 OverlayStateData *osd = data;
654 gint changed = FALSE;
657 for (i = 0; i < IMAGE_OSD_COUNT; i++)
659 if (osd->icon_time[i] > 1)
662 if (osd->icon_time[i] < 2)
664 osd->icon_time[i] = 0;
674 if (changed) image_osd_update_schedule(osd, FALSE);
685 static void image_osd_timer_schedule(OverlayStateData *osd)
687 if (osd->timer_id == -1)
689 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
693 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
695 OverlayStateData *osd = data;
697 osd->changed_states |= state;
698 image_osd_update_schedule(osd, FALSE);
701 static void image_osd_free(OverlayStateData *osd)
705 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
706 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
712 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
713 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
715 image_set_state_func(osd->imd, NULL, NULL);
716 image_overlay_remove(osd->imd, osd->ovl_info);
718 for (i = 0; i < IMAGE_OSD_COUNT; i++)
720 image_osd_icon_hide(osd, i);
727 static void image_osd_remove(ImageWindow *imd)
729 OverlayStateData *osd;
731 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
735 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
737 OverlayStateData *osd = data;
743 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
745 OverlayStateData *osd;
747 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
750 osd = g_new0(OverlayStateData, 1);
755 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
756 G_CALLBACK(image_osd_destroy_cb), osd);
757 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
759 image_set_state_func(osd->imd, image_osd_state_cb, osd);
762 if (osd->show_info != info ||
763 osd->show_status != status)
765 osd->show_info = info;
766 osd->show_status = status;
768 image_osd_update_schedule(osd, TRUE);
772 void image_osd_set(ImageWindow *imd, gint info, gint status)
776 if (!info && !status)
778 image_osd_remove(imd);
782 image_osd_enable(imd, info, status);
785 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
787 OverlayStateData *osd;
789 if (!imd) return FALSE;
791 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
792 if (!osd) return FALSE;
794 if (info) *info = osd->show_info;
795 if (status) *status = osd->show_status;
803 2+ = show for duration tenths of a second
804 -1 = use default duration
806 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
808 OverlayStateData *osd;
812 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
815 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
816 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
817 if (duration > 1) duration += 1;
819 osd->icon_time[flag] = duration;
821 image_osd_update_schedule(osd, FALSE);
822 image_osd_timer_schedule(osd);