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 *----------------------------------------------------------------------------
81 *----------------------------------------------------------------------------
85 #define HIST_PREPARE(imd, lw) \
86 LayoutWindow *lw = NULL; \
88 lw = layout_find_by_image(imd);
90 void image_osd_histogram_onoff_toggle(ImageWindow *imd, gint x)
95 lw->histogram_enabled = !!(x);
96 if (lw->histogram_enabled && !lw->histogram)
97 lw->histogram = histogram_new();
101 gint image_osd_histogram_onoff_status(ImageWindow *imd)
103 HIST_PREPARE(imd, lw)
104 return lw ? lw->histogram_enabled : FALSE;
107 void image_osd_histogram_chan_toggle(ImageWindow *imd)
109 HIST_PREPARE(imd, lw)
110 if (lw && lw->histogram)
111 histogram_set_channel(lw->histogram, (histogram_get_channel(lw->histogram) +1)%HCHAN_COUNT);
114 void image_osd_histogram_log_toggle(ImageWindow *imd)
117 if (lw && lw->histogram)
118 histogram_set_mode(lw->histogram, !histogram_get_mode(lw->histogram));
123 static void image_osd_timer_schedule(OverlayStateData *osd);
125 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
127 gchar delim = '%', imp = '|', sep[] = " - ";
136 if (!str || !*str) return g_strdup("");
138 new = g_string_new(str);
140 exif = exif_read_fd(imd->image_fd, FALSE);
151 start = strchr(new->str, delim);
154 end = strchr(start+1, delim);
158 for (p = end; p > start; p--)
160 if (*p == ':' && was_digit)
165 was_digit = g_ascii_isdigit(*p);
168 if (trunc) limit = atoi(trunc+1);
170 name = g_strndup(start+1, ((limit > 0) ? trunc : end)-start-1);
172 pos = start-new->str;
173 data = g_strdup(g_hash_table_lookup(vars, name));
175 data = exif_get_data_as_text(exif, name);
176 if (data && *data && limit > 0 && strlen(data) > limit + 3)
178 gchar *new_data = g_strdup_printf("%-*.*s...", limit, limit, data);
183 g_string_erase(new, pos, end-start+1);
185 g_string_insert(new, pos, data);
186 if (pos-prev == 2 && new->str[pos-1] == imp)
188 g_string_erase(new, --pos, 1);
191 g_string_insert(new, pos, sep);
196 prev = data ? pos+strlen(data)-1 : pos-1;
197 last = data ? TRUE : last;
202 /* search and destroy empty lines */
204 while ((start = strchr(end, '\n')))
207 while (*++(end) == '\n')
209 g_string_erase(new, start-new->str, end-start-1);
212 g_strchomp(new->str);
215 g_string_free(new, FALSE);
220 static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
222 GdkPixbuf *pixbuf = NULL;
232 GdkPixbuf *imgpixbuf = NULL;
233 LayoutWindow *lw = NULL;
239 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
241 name = image_get_name(imd);
244 name_escaped = g_markup_escape_text(name, -1);
251 cd = image_get_collection(imd, &info);
256 t = g_list_length(cd->list);
257 n = g_list_index(cd->list, info) + 1;
258 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
259 ct = g_strdup_printf("<i>%s</i>\n", buf);
264 lw = layout_find_by_image(imd);
269 n = g_list_length(lw->slideshow->list_done);
270 t = n + g_list_length(lw->slideshow->list);
275 t = layout_list_count(lw, NULL);
276 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
279 else if (view_window_find_image(imd, &n, &t))
295 size = text_from_size_abrev(imd->size);
298 if (imd->delay_flip &&
299 imd->il && imd->il->pixbuf &&
300 image_get_pixbuf(imd) != imd->il->pixbuf)
302 w = gdk_pixbuf_get_width(imd->il->pixbuf);
303 h = gdk_pixbuf_get_height(imd->il->pixbuf);
304 imgpixbuf = imd->il->pixbuf;
308 pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
309 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
312 lw = layout_find_by_image(imd);
314 if (imgpixbuf && lw->histogram && lw->histogram_enabled
315 && (!imd->il || imd->il->done))
318 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
319 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
320 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
323 g_hash_table_insert(vars, "collection", g_strdup(ct));
324 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
325 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
326 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
327 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
328 g_hash_table_insert(vars, "size", g_strdup(size));
332 text = g_strdup_printf(_("Untitled"));
336 text = image_osd_mkinfo(options->fullscreen.info, imd, vars);
341 g_free(name_escaped);
342 g_hash_table_destroy(vars);
345 FileData *fd = image_get_fd(imd);
347 if (fd) /* fd may be null after file deletion */
349 gint active_marks = 0;
353 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
355 active_marks += fd->marks[mark];
359 if (active_marks > 0)
361 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
363 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
365 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
369 text2 = g_strdup_printf("%s\n%s", text, buf->str);
371 text2 = g_strdup(buf->str);
372 g_string_free(buf, TRUE);
380 text2 = g_strdup_printf("%s\n%s", text, histogram_label(lw->histogram));
382 text2 = g_strdup(histogram_label(lw->histogram));
389 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
390 pango_layout_set_markup(layout, text, -1);
393 pango_layout_get_pixel_size(layout, &width, &height);
394 /* with empty text width is set to 0, but not height) */
397 else if (height == 0)
399 if (width > 0) width += 10;
400 if (height > 0) height += 10;
404 histogram_read(lw->histogram, imgpixbuf);
405 if (width < 266) width = 266;
406 height += HISTOGRAM_HEIGHT + 5;
409 if (width > 0 && height > 0)
411 /* TODO: make osd color configurable --Zas */
412 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
413 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
414 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
415 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
416 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
417 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
418 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
419 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
420 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
423 histogram_draw(lw->histogram, pixbuf, 5, height - HISTOGRAM_HEIGHT - 5 , width - 10, HISTOGRAM_HEIGHT);
425 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
428 g_object_unref(G_OBJECT(layout));
433 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
435 static GdkPixbuf **icons = NULL;
436 GdkPixbuf *icon = NULL;
438 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
439 if (icons[flag]) return icons[flag];
441 if (osd_icons[flag].key)
443 icon = pixbuf_inline(osd_icons[flag].key);
448 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
449 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
450 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
453 case IMAGE_OSD_ROTATE_AUTO:
454 pixbuf_set_rect(icon, 3, 8, 11, 12,
457 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
458 20, 9, 14, 15, 14, 3,
461 case IMAGE_OSD_ROTATE_USER:
463 case IMAGE_OSD_COLOR:
464 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
465 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
466 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
468 case IMAGE_OSD_FIRST:
469 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
470 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
471 12, 5, 18, 11, 6, 11,
475 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
476 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
477 12, 18, 6, 12, 18, 12,
481 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
482 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
494 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
498 if (osd->icon_id[flag]) return;
500 pixbuf = image_osd_icon_pixbuf(flag);
503 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
504 osd_icons[flag].x, osd_icons[flag].y,
508 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
510 if (osd->icon_id[flag])
512 image_overlay_remove(osd->imd, osd->icon_id[flag]);
513 osd->icon_id[flag] = 0;
517 static gint image_osd_update_cb(gpointer data)
519 OverlayStateData *osd = data;
523 if (osd->changed_states & IMAGE_STATE_IMAGE)
527 pixbuf = image_osd_info_render(osd->imd);
530 if (osd->ovl_info == 0)
532 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
533 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
537 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
539 g_object_unref(pixbuf);
541 else if (osd->ovl_info)
543 image_overlay_remove(osd->imd, osd->ovl_info);
552 image_overlay_remove(osd->imd, osd->ovl_info);
557 if (osd->show_status)
561 if (osd->changed_states & IMAGE_STATE_IMAGE)
563 for (i = 0; i < IMAGE_OSD_COUNT; i++)
565 if (osd_icons[i].reset) osd->icon_time[i] = 0;
569 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
571 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
572 image_osd_timer_schedule(osd);
575 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
579 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
582 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
585 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
586 image_osd_timer_schedule(osd);
589 for (i = 0; i < IMAGE_OSD_COUNT; i++)
591 if (osd->icon_time[i] > 0)
593 image_osd_icon_show(osd, i);
597 image_osd_icon_hide(osd, i);
605 for (i = 0; i < IMAGE_OSD_COUNT; i++)
607 image_osd_icon_hide(osd, i);
611 if (osd->imd->il && osd->imd->il->done)
612 osd->changed_states = IMAGE_STATE_NONE;
617 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
619 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
621 if (osd->idle_id == -1)
623 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
627 void image_osd_update(ImageWindow *imd)
629 OverlayStateData *osd;
633 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
636 image_osd_update_schedule(osd, TRUE);
639 static gint image_osd_timer_cb(gpointer data)
641 OverlayStateData *osd = data;
643 gint changed = FALSE;
646 for (i = 0; i < IMAGE_OSD_COUNT; i++)
648 if (osd->icon_time[i] > 1)
651 if (osd->icon_time[i] < 2)
653 osd->icon_time[i] = 0;
663 if (changed) image_osd_update_schedule(osd, FALSE);
674 static void image_osd_timer_schedule(OverlayStateData *osd)
676 if (osd->timer_id == -1)
678 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
682 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
684 OverlayStateData *osd = data;
686 osd->changed_states |= state;
687 image_osd_update_schedule(osd, FALSE);
690 static void image_osd_free(OverlayStateData *osd)
694 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
695 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
701 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
702 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
704 image_set_state_func(osd->imd, NULL, NULL);
705 image_overlay_remove(osd->imd, osd->ovl_info);
707 for (i = 0; i < IMAGE_OSD_COUNT; i++)
709 image_osd_icon_hide(osd, i);
716 static void image_osd_remove(ImageWindow *imd)
718 OverlayStateData *osd;
720 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
724 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
726 OverlayStateData *osd = data;
732 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
734 OverlayStateData *osd;
736 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
739 osd = g_new0(OverlayStateData, 1);
744 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
745 G_CALLBACK(image_osd_destroy_cb), osd);
746 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
748 image_set_state_func(osd->imd, image_osd_state_cb, osd);
751 if (osd->show_info != info ||
752 osd->show_status != status)
754 osd->show_info = info;
755 osd->show_status = status;
757 image_osd_update_schedule(osd, TRUE);
761 void image_osd_set(ImageWindow *imd, gint info, gint status)
765 if (!info && !status)
767 image_osd_remove(imd);
771 image_osd_enable(imd, info, status);
774 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
776 OverlayStateData *osd;
778 if (!imd) return FALSE;
780 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
781 if (!osd) return FALSE;
783 if (info) *info = osd->show_info;
784 if (status) *status = osd->show_status;
792 2+ = show for duration tenths of a second
793 -1 = use default duration
795 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
797 OverlayStateData *osd;
801 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
804 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
805 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
806 if (duration > 1) duration += 1;
808 osd->icon_time[flag] = duration;
810 image_osd_update_schedule(osd, FALSE);
811 image_osd_timer_schedule(osd);