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[] = " - ";
132 GString *new = g_string_new(str);
136 exif = exif_read_fd(imd->image_fd, FALSE);
142 start = strchr(new->str, delim);
145 end = strchr(start+1, delim);
149 name = g_strndup(start+1, end-start-1);
150 pos = start-new->str;
151 data = g_strdup(g_hash_table_lookup(vars, name));
153 data = exif_get_data_as_text(exif, name);
155 g_string_erase(new, pos, end-start+1);
157 g_string_insert(new, pos, data);
158 if (pos-prev == 2 && new->str[pos-1] == imp)
160 g_string_erase(new, --pos, 1);
163 g_string_insert(new, pos, sep);
168 prev = data ? pos+strlen(data)-1 : pos-1;
169 last = data ? TRUE : last;
174 /* search and destroy empty lines */
176 while ((start = strchr(end, '\n')))
179 while (*++(end) == '\n')
181 g_string_erase(new, start-new->str, end-start-1);
184 g_strchomp(new->str);
187 g_string_free(new, FALSE);
192 static GdkPixbuf *image_osd_info_render(ImageWindow *imd)
204 GdkPixbuf *imgpixbuf = NULL;
205 LayoutWindow *lw = NULL;
211 vars = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
213 name = image_get_name(imd);
216 name_escaped = g_markup_escape_text(name, -1);
223 cd = image_get_collection(imd, &info);
228 t = g_list_length(cd->list);
229 n = g_list_index(cd->list, info) + 1;
230 buf = g_markup_escape_text((cd->name) ? cd->name : _("Untitled"), -1);
231 ct = g_strdup_printf("<i>%s</i>\n", buf);
236 lw = layout_find_by_image(imd);
241 n = g_list_length(lw->slideshow->list_done);
242 t = n + g_list_length(lw->slideshow->list);
247 t = layout_list_count(lw, NULL);
248 n = layout_list_get_index(lw, image_get_path(lw->image)) + 1;
251 else if (view_window_find_image(imd, &n, &t))
267 size = text_from_size_abrev(imd->size);
270 if (imd->delay_flip &&
271 imd->il && imd->il->pixbuf &&
272 image_get_pixbuf(imd) != imd->il->pixbuf)
274 w = gdk_pixbuf_get_width(imd->il->pixbuf);
275 h = gdk_pixbuf_get_height(imd->il->pixbuf);
276 imgpixbuf = imd->il->pixbuf;
280 pixbuf_renderer_get_image_size(PIXBUF_RENDERER(imd->pr), &w, &h);
281 imgpixbuf = (PIXBUF_RENDERER(imd->pr))->pixbuf;
284 lw = layout_find_by_image(imd);
286 if (imgpixbuf && lw->histogram && lw->histogram_enabled
287 && (!imd->il || imd->il->done))
290 g_hash_table_insert(vars, "width", g_strdup_printf("%d", w));
291 g_hash_table_insert(vars, "height", g_strdup_printf("%d", h));
292 g_hash_table_insert(vars, "res", g_strdup_printf("%d × %d", w, h));
295 g_hash_table_insert(vars, "collection", g_strdup(ct));
296 g_hash_table_insert(vars, "number", g_strdup_printf("%d", n));
297 g_hash_table_insert(vars, "total", g_strdup_printf("%d", t));
298 g_hash_table_insert(vars, "name", g_strdup(name_escaped));
299 g_hash_table_insert(vars, "date", g_strdup(text_from_time(imd->mtime)));
300 g_hash_table_insert(vars, "size", g_strdup(size));
304 text = g_strdup_printf(_("Untitled"));
308 text = image_osd_mkinfo(fullscreen_info, imd, vars);
313 g_free(name_escaped);
314 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;
325 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
327 active_marks += fd->marks[mark];
331 if (active_marks > 0)
333 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
335 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
337 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
340 text2 = g_strdup_printf("%s\n%s", text, buf->str);
341 g_string_free(buf, TRUE);
348 text2 = g_strdup_printf("%s\n%s", text, histogram_label(lw->histogram));
355 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
356 pango_layout_set_markup(layout, text, -1);
359 pango_layout_get_pixel_size(layout, &width, &height);
366 histogram_read(lw->histogram, imgpixbuf);
367 if (width < 266) width = 266;
368 height += HISTOGRAM_HEIGHT + 5;
372 /* TODO: make osd color configurable --Zas */
373 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
374 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
375 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
376 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
377 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
378 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
379 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
380 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
381 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
384 histogram_draw(lw->histogram, pixbuf, 5, height - HISTOGRAM_HEIGHT - 5 , width - 10, HISTOGRAM_HEIGHT);
386 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
388 g_object_unref(G_OBJECT(layout));
393 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
395 static GdkPixbuf **icons = NULL;
396 GdkPixbuf *icon = NULL;
398 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
399 if (icons[flag]) return icons[flag];
401 if (osd_icons[flag].key)
403 icon = pixbuf_inline(osd_icons[flag].key);
408 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
409 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
410 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
413 case IMAGE_OSD_ROTATE_AUTO:
414 pixbuf_set_rect(icon, 3, 8, 11, 12,
417 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
418 20, 9, 14, 15, 14, 3,
421 case IMAGE_OSD_ROTATE_USER:
423 case IMAGE_OSD_COLOR:
424 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
425 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
426 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
428 case IMAGE_OSD_FIRST:
429 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
430 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
431 12, 5, 18, 11, 6, 11,
435 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
436 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
437 12, 18, 6, 12, 18, 12,
441 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
442 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
454 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
458 if (osd->icon_id[flag]) return;
460 pixbuf = image_osd_icon_pixbuf(flag);
463 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
464 osd_icons[flag].x, osd_icons[flag].y,
468 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
470 if (osd->icon_id[flag])
472 image_overlay_remove(osd->imd, osd->icon_id[flag]);
473 osd->icon_id[flag] = 0;
477 static gint image_osd_update_cb(gpointer data)
479 OverlayStateData *osd = data;
483 if (osd->changed_states & IMAGE_STATE_IMAGE)
487 pixbuf = image_osd_info_render(osd->imd);
488 if (osd->ovl_info == 0)
490 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
491 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
495 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
497 g_object_unref(pixbuf);
504 image_overlay_remove(osd->imd, osd->ovl_info);
509 if (osd->show_status)
513 if (osd->changed_states & IMAGE_STATE_IMAGE)
515 for (i = 0; i < IMAGE_OSD_COUNT; i++)
517 if (osd_icons[i].reset) osd->icon_time[i] = 0;
521 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
523 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
524 image_osd_timer_schedule(osd);
527 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
531 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
534 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
537 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
538 image_osd_timer_schedule(osd);
541 for (i = 0; i < IMAGE_OSD_COUNT; i++)
543 if (osd->icon_time[i] > 0)
545 image_osd_icon_show(osd, i);
549 image_osd_icon_hide(osd, i);
557 for (i = 0; i < IMAGE_OSD_COUNT; i++)
559 image_osd_icon_hide(osd, i);
563 if (osd->imd->il && osd->imd->il->done)
564 osd->changed_states = IMAGE_STATE_NONE;
569 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
571 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
573 if (osd->idle_id == -1)
575 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
579 void image_osd_update(ImageWindow *imd)
581 OverlayStateData *osd;
585 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
588 image_osd_update_schedule(osd, TRUE);
591 static gint image_osd_timer_cb(gpointer data)
593 OverlayStateData *osd = data;
595 gint changed = FALSE;
598 for (i = 0; i < IMAGE_OSD_COUNT; i++)
600 if (osd->icon_time[i] > 1)
603 if (osd->icon_time[i] < 2)
605 osd->icon_time[i] = 0;
615 if (changed) image_osd_update_schedule(osd, FALSE);
626 static void image_osd_timer_schedule(OverlayStateData *osd)
628 if (osd->timer_id == -1)
630 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
634 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
636 OverlayStateData *osd = data;
638 osd->changed_states |= state;
639 image_osd_update_schedule(osd, FALSE);
642 static void image_osd_free(OverlayStateData *osd)
646 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
647 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
653 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
654 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
656 image_set_state_func(osd->imd, NULL, NULL);
657 image_overlay_remove(osd->imd, osd->ovl_info);
659 for (i = 0; i < IMAGE_OSD_COUNT; i++)
661 image_osd_icon_hide(osd, i);
668 static void image_osd_remove(ImageWindow *imd)
670 OverlayStateData *osd;
672 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
676 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
678 OverlayStateData *osd = data;
684 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
686 OverlayStateData *osd;
688 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
691 osd = g_new0(OverlayStateData, 1);
696 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
697 G_CALLBACK(image_osd_destroy_cb), osd);
698 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
700 image_set_state_func(osd->imd, image_osd_state_cb, osd);
703 if (osd->show_info != info ||
704 osd->show_status != status)
706 osd->show_info = info;
707 osd->show_status = status;
709 image_osd_update_schedule(osd, TRUE);
713 void image_osd_set(ImageWindow *imd, gint info, gint status)
717 if (!info && !status)
719 image_osd_remove(imd);
723 image_osd_enable(imd, info, status);
726 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
728 OverlayStateData *osd;
730 if (!imd) return FALSE;
732 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
733 if (!osd) return FALSE;
735 if (info) *info = osd->show_info;
736 if (status) *status = osd->show_status;
744 2+ = show for duration tenths of a second
745 -1 = use default duration
747 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
749 OverlayStateData *osd;
753 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
756 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
757 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
758 if (duration > 1) duration += 1;
760 osd->icon_time[flag] = duration;
762 image_osd_update_schedule(osd, FALSE);
763 image_osd_timer_schedule(osd);