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
78 *----------------------------------------------------------------------------
80 *----------------------------------------------------------------------------
84 #define HIST_PREPARE(imd, lw) \
85 LayoutWindow *lw = NULL; \
87 lw = layout_find_by_image(imd);
89 void image_osd_histogram_onoff_toggle(ImageWindow *imd, gint x)
94 lw->histogram_enabled = !!(x);
95 if (lw->histogram_enabled && !lw->histogram)
96 lw->histogram = histogram_new();
100 gint image_osd_histogram_onoff_status(ImageWindow *imd)
102 HIST_PREPARE(imd, lw)
103 return lw ? lw->histogram_enabled : FALSE;
106 void image_osd_histogram_chan_toggle(ImageWindow *imd)
108 HIST_PREPARE(imd, lw)
109 if (lw && lw->histogram)
110 histogram_set_channel(lw->histogram, (histogram_get_channel(lw->histogram) +1)%HCHAN_COUNT);
113 void image_osd_histogram_log_toggle(ImageWindow *imd)
116 if (lw && lw->histogram)
117 histogram_set_mode(lw->histogram, !histogram_get_mode(lw->histogram));
122 static void image_osd_timer_schedule(OverlayStateData *osd);
124 static gchar *image_osd_mkinfo(const gchar *str, ImageWindow *imd, GHashTable *vars)
126 gchar delim = '%', imp = '|', sep[] = " - ";
131 GString *new = g_string_new(str);
135 exif = exif_read_fd(imd->image_fd, FALSE);
141 start = strchr(new->str, delim);
144 end = strchr(start+1, delim);
148 name = g_strndup(start+1, end-start-1);
149 pos = start-new->str;
150 data = g_strdup(g_hash_table_lookup(vars, name));
152 data = exif_get_data_as_text(exif, name);
154 g_string_erase(new, pos, end-start+1);
156 g_string_insert(new, pos, data);
157 if (pos-prev == 2 && new->str[pos-1] == imp)
159 g_string_erase(new, --pos, 1);
162 g_string_insert(new, pos, sep);
167 prev = data ? pos+strlen(data)-1 : pos-1;
168 last = data ? TRUE : last;
173 /* search and destroy empty lines */
175 while ((start = strchr(end, '\n')))
178 while (*++(end) == '\n')
180 g_string_erase(new, start-new->str, end-start-1);
183 g_strchomp(new->str);
186 g_string_free(new, FALSE);
191 static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
203 GdkPixbuf *imgpixbuf = NULL;
204 LayoutWindow *lw = NULL;
210 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
212 name = image_get_name(imd);
215 name_escaped = g_markup_escape_text(name, -1);
222 cd = image_get_collection(imd, &info);
227 t = g_list_length(cd->list);
228 n = g_list_index(cd->list, info) + 1;
229 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
230 ct = g_strdup_printf("<i>%s</i>\n", buf);
235 lw = layout_find_by_image(imd);
240 n = g_list_length(lw->slideshow->list_done);
241 t = n + g_list_length(lw->slideshow->list);
246 t = layout_list_count(lw, NULL);
247 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
250 else if (view_window_find_image(imd, &n, &t))
266 size = text_from_size_abrev(imd->size);
269 if (imd->delay_flip &&
270 imd->il && imd->il->pixbuf &&
271 image_get_pixbuf(imd) != imd->il->pixbuf)
273 w = gdk_pixbuf_get_width(imd->il->pixbuf);
274 h = gdk_pixbuf_get_height(imd->il->pixbuf);
275 imgpixbuf = imd->il->pixbuf;
279 pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
280 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
283 lw = layout_find_by_image(imd);
285 if (imgpixbuf && lw->histogram && lw->histogram_enabled
286 && (!imd->il || imd->il->done))
289 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
290 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
291 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
294 g_hash_table_insert(vars, "collection", g_strdup(ct));
295 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
296 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
297 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
298 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
299 g_hash_table_insert(vars, "size", g_strdup(size));
303 text = g_strdup_printf(_("Untitled"));
307 text = image_osd_mkinfo(fullscreen_info, imd, vars);
312 g_free(name_escaped);
313 g_hash_table_destroy(vars);
317 FileData *fd = image_get_fd(imd);
319 if (fd) /* fd may be null after file deletion */
321 gint active_marks = 0;
324 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
326 active_marks += fd->marks[mark];
329 if (active_marks > 0)
331 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
333 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
335 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
339 text2 = g_strdup_printf("%s\n%s\n%s", text, buf->str, histogram_label(lw->histogram));
341 text2 = g_strdup_printf("%s\n%s", text, buf->str);
342 g_string_free(buf, TRUE);
348 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
349 pango_layout_set_markup(layout, text2, -1);
352 pango_layout_get_pixel_size(layout, &width, &height);
359 histogram_read(lw->histogram, imgpixbuf);
360 if (width < 266) width = 266;
365 /* TODO: make osd color configurable --Zas */
366 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
367 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
368 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
369 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
370 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
371 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
372 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
373 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
374 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
377 histogram_draw(lw->histogram, pixbuf, 0, 0, width, height);
379 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
381 g_object_unref(G_OBJECT(layout));
386 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
388 static GdkPixbuf **icons = NULL;
389 GdkPixbuf *icon = NULL;
391 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
392 if (icons[flag]) return icons[flag];
394 if (osd_icons[flag].key)
396 icon = pixbuf_inline(osd_icons[flag].key);
401 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
402 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
403 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
406 case IMAGE_OSD_ROTATE_AUTO:
407 pixbuf_set_rect(icon, 3, 8, 11, 12,
410 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
411 20, 9, 14, 15, 14, 3,
414 case IMAGE_OSD_ROTATE_USER:
416 case IMAGE_OSD_COLOR:
417 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
418 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
419 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
421 case IMAGE_OSD_FIRST:
422 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
423 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
424 12, 5, 18, 11, 6, 11,
428 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
429 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
430 12, 18, 6, 12, 18, 12,
434 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
435 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
447 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
451 if (osd->icon_id[flag]) return;
453 pixbuf = image_osd_icon_pixbuf(flag);
456 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
457 osd_icons[flag].x, osd_icons[flag].y,
461 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
463 if (osd->icon_id[flag])
465 image_overlay_remove(osd->imd, osd->icon_id[flag]);
466 osd->icon_id[flag] = 0;
470 static gint image_osd_update_cb(gpointer data)
472 OverlayStateData *osd = data;
476 if (osd->changed_states & IMAGE_STATE_IMAGE)
480 pixbuf = image_osd_info_render(osd->imd);
481 if (osd->ovl_info == 0)
483 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
484 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
488 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
490 g_object_unref(pixbuf);
497 image_overlay_remove(osd->imd, osd->ovl_info);
502 if (osd->show_status)
506 if (osd->changed_states & IMAGE_STATE_IMAGE)
508 for (i = 0; i < IMAGE_OSD_COUNT; i++)
510 if (osd_icons[i].reset) osd->icon_time[i] = 0;
514 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
516 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
517 image_osd_timer_schedule(osd);
520 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
524 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
527 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
530 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
531 image_osd_timer_schedule(osd);
534 for (i = 0; i < IMAGE_OSD_COUNT; i++)
536 if (osd->icon_time[i] > 0)
538 image_osd_icon_show(osd, i);
542 image_osd_icon_hide(osd, i);
550 for (i = 0; i < IMAGE_OSD_COUNT; i++)
552 image_osd_icon_hide(osd, i);
556 if (osd->imd->il && osd->imd->il->done)
557 osd->changed_states = IMAGE_STATE_NONE;
562 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
564 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
566 if (osd->idle_id == -1)
568 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
572 void image_osd_update(ImageWindow *imd)
574 OverlayStateData *osd;
578 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
581 image_osd_update_schedule(osd, TRUE);
584 static gint image_osd_timer_cb(gpointer data)
586 OverlayStateData *osd = data;
588 gint changed = FALSE;
591 for (i = 0; i < IMAGE_OSD_COUNT; i++)
593 if (osd->icon_time[i] > 1)
596 if (osd->icon_time[i] < 2)
598 osd->icon_time[i] = 0;
608 if (changed) image_osd_update_schedule(osd, FALSE);
619 static void image_osd_timer_schedule(OverlayStateData *osd)
621 if (osd->timer_id == -1)
623 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
627 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
629 OverlayStateData *osd = data;
631 osd->changed_states |= state;
632 image_osd_update_schedule(osd, FALSE);
635 static void image_osd_free(OverlayStateData *osd)
639 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
640 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
646 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
647 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
649 image_set_state_func(osd->imd, NULL, NULL);
650 image_overlay_remove(osd->imd, osd->ovl_info);
652 for (i = 0; i < IMAGE_OSD_COUNT; i++)
654 image_osd_icon_hide(osd, i);
661 static void image_osd_remove(ImageWindow *imd)
663 OverlayStateData *osd;
665 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
669 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
671 OverlayStateData *osd = data;
677 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
679 OverlayStateData *osd;
681 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
684 osd = g_new0(OverlayStateData, 1);
689 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
690 G_CALLBACK(image_osd_destroy_cb), osd);
691 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
693 image_set_state_func(osd->imd, image_osd_state_cb, osd);
696 if (osd->show_info != info ||
697 osd->show_status != status)
699 osd->show_info = info;
700 osd->show_status = status;
702 image_osd_update_schedule(osd, TRUE);
706 void image_osd_set(ImageWindow *imd, gint info, gint status)
710 if (!info && !status)
712 image_osd_remove(imd);
716 image_osd_enable(imd, info, status);
719 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
721 OverlayStateData *osd;
723 if (!imd) return FALSE;
725 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
726 if (!osd) return FALSE;
728 if (info) *info = osd->show_info;
729 if (status) *status = osd->show_status;
737 2+ = show for duration tenths of a second
738 -1 = use default duration
740 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
742 OverlayStateData *osd;
746 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
749 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
750 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
751 if (duration > 1) duration += 1;
753 osd->icon_time[flag] = duration;
755 image_osd_update_schedule(osd, FALSE);
756 image_osd_timer_schedule(osd);