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);
391 printf("|%s|\n",text);
394 pango_layout_get_pixel_size(layout, &width, &height);
395 /* with empty text width is set to 0, but not height) */
398 else if (height == 0)
400 if (width > 0) width += 10;
401 if (height > 0) height += 10;
405 histogram_read(lw->histogram, imgpixbuf);
406 if (width < 266) width = 266;
407 height += HISTOGRAM_HEIGHT + 5;
410 if (width > 0 && height > 0)
412 /* TODO: make osd color configurable --Zas */
413 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
414 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
415 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
416 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
417 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
418 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
419 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
420 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
421 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
424 histogram_draw(lw->histogram, pixbuf, 5, height - HISTOGRAM_HEIGHT - 5 , width - 10, HISTOGRAM_HEIGHT);
426 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
429 g_object_unref(G_OBJECT(layout));
434 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
436 static GdkPixbuf **icons = NULL;
437 GdkPixbuf *icon = NULL;
439 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
440 if (icons[flag]) return icons[flag];
442 if (osd_icons[flag].key)
444 icon = pixbuf_inline(osd_icons[flag].key);
449 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
450 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
451 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
454 case IMAGE_OSD_ROTATE_AUTO:
455 pixbuf_set_rect(icon, 3, 8, 11, 12,
458 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
459 20, 9, 14, 15, 14, 3,
462 case IMAGE_OSD_ROTATE_USER:
464 case IMAGE_OSD_COLOR:
465 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
466 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
467 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
469 case IMAGE_OSD_FIRST:
470 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
471 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
472 12, 5, 18, 11, 6, 11,
476 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
477 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
478 12, 18, 6, 12, 18, 12,
482 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
483 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
495 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
499 if (osd->icon_id[flag]) return;
501 pixbuf = image_osd_icon_pixbuf(flag);
504 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
505 osd_icons[flag].x, osd_icons[flag].y,
509 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
511 if (osd->icon_id[flag])
513 image_overlay_remove(osd->imd, osd->icon_id[flag]);
514 osd->icon_id[flag] = 0;
518 static gint image_osd_update_cb(gpointer data)
520 OverlayStateData *osd = data;
524 if (osd->changed_states & IMAGE_STATE_IMAGE)
528 pixbuf = image_osd_info_render(osd->imd);
531 if (osd->ovl_info == 0)
533 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
534 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
538 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
540 g_object_unref(pixbuf);
542 else if (osd->ovl_info)
544 image_overlay_remove(osd->imd, osd->ovl_info);
553 image_overlay_remove(osd->imd, osd->ovl_info);
558 if (osd->show_status)
562 if (osd->changed_states & IMAGE_STATE_IMAGE)
564 for (i = 0; i < IMAGE_OSD_COUNT; i++)
566 if (osd_icons[i].reset) osd->icon_time[i] = 0;
570 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
572 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
573 image_osd_timer_schedule(osd);
576 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
580 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
583 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
586 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
587 image_osd_timer_schedule(osd);
590 for (i = 0; i < IMAGE_OSD_COUNT; i++)
592 if (osd->icon_time[i] > 0)
594 image_osd_icon_show(osd, i);
598 image_osd_icon_hide(osd, i);
606 for (i = 0; i < IMAGE_OSD_COUNT; i++)
608 image_osd_icon_hide(osd, i);
612 if (osd->imd->il && osd->imd->il->done)
613 osd->changed_states = IMAGE_STATE_NONE;
618 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
620 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
622 if (osd->idle_id == -1)
624 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
628 void image_osd_update(ImageWindow *imd)
630 OverlayStateData *osd;
634 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
637 image_osd_update_schedule(osd, TRUE);
640 static gint image_osd_timer_cb(gpointer data)
642 OverlayStateData *osd = data;
644 gint changed = FALSE;
647 for (i = 0; i < IMAGE_OSD_COUNT; i++)
649 if (osd->icon_time[i] > 1)
652 if (osd->icon_time[i] < 2)
654 osd->icon_time[i] = 0;
664 if (changed) image_osd_update_schedule(osd, FALSE);
675 static void image_osd_timer_schedule(OverlayStateData *osd)
677 if (osd->timer_id == -1)
679 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
683 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
685 OverlayStateData *osd = data;
687 osd->changed_states |= state;
688 image_osd_update_schedule(osd, FALSE);
691 static void image_osd_free(OverlayStateData *osd)
695 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
696 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
702 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
703 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
705 image_set_state_func(osd->imd, NULL, NULL);
706 image_overlay_remove(osd->imd, osd->ovl_info);
708 for (i = 0; i < IMAGE_OSD_COUNT; i++)
710 image_osd_icon_hide(osd, i);
717 static void image_osd_remove(ImageWindow *imd)
719 OverlayStateData *osd;
721 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
725 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
727 OverlayStateData *osd = data;
733 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
735 OverlayStateData *osd;
737 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
740 osd = g_new0(OverlayStateData, 1);
745 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
746 G_CALLBACK(image_osd_destroy_cb), osd);
747 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
749 image_set_state_func(osd->imd, image_osd_state_cb, osd);
752 if (osd->show_info != info ||
753 osd->show_status != status)
755 osd->show_info = info;
756 osd->show_status = status;
758 image_osd_update_schedule(osd, TRUE);
762 void image_osd_set(ImageWindow *imd, gint info, gint status)
766 if (!info && !status)
768 image_osd_remove(imd);
772 image_osd_enable(imd, info, status);
775 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
777 OverlayStateData *osd;
779 if (!imd) return FALSE;
781 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
782 if (!osd) return FALSE;
784 if (info) *info = osd->show_info;
785 if (status) *status = osd->show_status;
793 2+ = show for duration tenths of a second
794 -1 = use default duration
796 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
798 OverlayStateData *osd;
802 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
805 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
806 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
807 if (duration > 1) duration += 1;
809 osd->icon_time[flag] = duration;
811 image_osd_update_schedule(osd, FALSE);
812 image_osd_timer_schedule(osd);