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);
316 FileData *fd = image_get_fd(imd);
318 if (fd) /* fd may be null after file deletion */
320 gint active_marks = 0;
324 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
326 active_marks += fd->marks[mark];
331 text2 = g_strdup_printf("%s\n%s", text, histogram_label(lw->histogram));
336 if (active_marks > 0)
338 GString *buf = g_string_sized_new(FILEDATA_MARKS_SIZE * 2);
340 for (mark = 0; mark < FILEDATA_MARKS_SIZE; mark++)
342 g_string_append_printf(buf, fd->marks[mark] ? " <span background='#FF00FF'>%c</span>" : " %c", '1' + mark);
345 text2 = g_strdup_printf("%s\n%s", text, buf->str);
346 g_string_free(buf, TRUE);
354 layout = gtk_widget_create_pango_layout(imd->pr, NULL);
355 pango_layout_set_markup(layout, text, -1);
358 pango_layout_get_pixel_size(layout, &width, &height);
365 histogram_read(lw->histogram, imgpixbuf);
366 if (width < 266) width = 266;
371 /* TODO: make osd color configurable --Zas */
372 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
373 pixbuf_set_rect_fill(pixbuf, 3, 3, width-6, height-6, 240, 240, 240, 210);
374 pixbuf_set_rect(pixbuf, 0, 0, width, height, 240, 240, 240, 80, 1, 1, 1, 1);
375 pixbuf_set_rect(pixbuf, 1, 1, width-2, height-2, 240, 240, 240, 130, 1, 1, 1, 1);
376 pixbuf_set_rect(pixbuf, 2, 2, width-4, height-4, 240, 240, 240, 180, 1, 1, 1, 1);
377 pixbuf_pixel_set(pixbuf, 0, 0, 0, 0, 0, 0);
378 pixbuf_pixel_set(pixbuf, width - 1, 0, 0, 0, 0, 0);
379 pixbuf_pixel_set(pixbuf, 0, height - 1, 0, 0, 0, 0);
380 pixbuf_pixel_set(pixbuf, width - 1, height - 1, 0, 0, 0, 0);
383 histogram_draw(lw->histogram, pixbuf, 0, 0, width, height);
385 pixbuf_draw_layout(pixbuf, layout, imd->pr, 5, 5, 0, 0, 0, 255);
387 g_object_unref(G_OBJECT(layout));
392 static GdkPixbuf *image_osd_icon_pixbuf(ImageOSDFlag flag)
394 static GdkPixbuf **icons = NULL;
395 GdkPixbuf *icon = NULL;
397 if (!icons) icons = g_new0(GdkPixbuf *, IMAGE_OSD_COUNT);
398 if (icons[flag]) return icons[flag];
400 if (osd_icons[flag].key)
402 icon = pixbuf_inline(osd_icons[flag].key);
407 icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 24, 24);
408 pixbuf_set_rect_fill(icon, 1, 1, 22, 22, 255, 255, 255, 200);
409 pixbuf_set_rect(icon, 0, 0, 24, 24, 0, 0, 0, 128, 1, 1, 1, 1);
412 case IMAGE_OSD_ROTATE_AUTO:
413 pixbuf_set_rect(icon, 3, 8, 11, 12,
416 pixbuf_draw_triangle(icon, 14, 3, 6, 12,
417 20, 9, 14, 15, 14, 3,
420 case IMAGE_OSD_ROTATE_USER:
422 case IMAGE_OSD_COLOR:
423 pixbuf_set_rect_fill(icon, 3, 3, 18, 6, 200, 0, 0, 255);
424 pixbuf_set_rect_fill(icon, 3, 9, 18, 6, 0, 200, 0, 255);
425 pixbuf_set_rect_fill(icon, 3, 15, 18, 6, 0, 0, 200, 255);
427 case IMAGE_OSD_FIRST:
428 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 3, 0);
429 pixbuf_draw_triangle(icon, 6, 5, 12, 6,
430 12, 5, 18, 11, 6, 11,
434 pixbuf_set_rect(icon, 3, 3, 18, 18, 0, 0, 0, 200, 3, 3, 0, 3);
435 pixbuf_draw_triangle(icon, 6, 12, 12, 6,
436 12, 18, 6, 12, 18, 12,
440 pixbuf_set_rect_fill(icon, 11, 3, 3, 12, 0, 0, 0, 255);
441 pixbuf_set_rect_fill(icon, 11, 17, 3, 3, 0, 0, 0, 255);
453 static void image_osd_icon_show(OverlayStateData *osd, ImageOSDFlag flag)
457 if (osd->icon_id[flag]) return;
459 pixbuf = image_osd_icon_pixbuf(flag);
462 osd->icon_id[flag] = image_overlay_add(osd->imd, pixbuf,
463 osd_icons[flag].x, osd_icons[flag].y,
467 static void image_osd_icon_hide(OverlayStateData *osd, ImageOSDFlag flag)
469 if (osd->icon_id[flag])
471 image_overlay_remove(osd->imd, osd->icon_id[flag]);
472 osd->icon_id[flag] = 0;
476 static gint image_osd_update_cb(gpointer data)
478 OverlayStateData *osd = data;
482 if (osd->changed_states & IMAGE_STATE_IMAGE)
486 pixbuf = image_osd_info_render(osd->imd);
487 if (osd->ovl_info == 0)
489 osd->ovl_info = image_overlay_add(osd->imd, pixbuf,
490 OSD_INFO_X, OSD_INFO_Y, TRUE, FALSE);
494 image_overlay_set(osd->imd, osd->ovl_info, pixbuf, OSD_INFO_X, OSD_INFO_Y);
496 g_object_unref(pixbuf);
503 image_overlay_remove(osd->imd, osd->ovl_info);
508 if (osd->show_status)
512 if (osd->changed_states & IMAGE_STATE_IMAGE)
514 for (i = 0; i < IMAGE_OSD_COUNT; i++)
516 if (osd_icons[i].reset) osd->icon_time[i] = 0;
520 if (osd->changed_states & IMAGE_STATE_COLOR_ADJ)
522 osd->icon_time[IMAGE_OSD_COLOR] = IMAGE_OSD_DEFAULT_DURATION + 1;
523 image_osd_timer_schedule(osd);
526 if (osd->changed_states & IMAGE_STATE_ROTATE_AUTO)
530 if (osd->imd->state & IMAGE_STATE_ROTATE_AUTO)
533 if (!osd->imd->cm) n += IMAGE_OSD_DEFAULT_DURATION;
536 osd->icon_time[IMAGE_OSD_ROTATE_AUTO] = n;
537 image_osd_timer_schedule(osd);
540 for (i = 0; i < IMAGE_OSD_COUNT; i++)
542 if (osd->icon_time[i] > 0)
544 image_osd_icon_show(osd, i);
548 image_osd_icon_hide(osd, i);
556 for (i = 0; i < IMAGE_OSD_COUNT; i++)
558 image_osd_icon_hide(osd, i);
562 if (osd->imd->il && osd->imd->il->done)
563 osd->changed_states = IMAGE_STATE_NONE;
568 static void image_osd_update_schedule(OverlayStateData *osd, gint force)
570 if (force) osd->changed_states |= IMAGE_STATE_IMAGE;
572 if (osd->idle_id == -1)
574 osd->idle_id = g_idle_add_full(G_PRIORITY_HIGH, image_osd_update_cb, osd, NULL);
578 void image_osd_update(ImageWindow *imd)
580 OverlayStateData *osd;
584 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
587 image_osd_update_schedule(osd, TRUE);
590 static gint image_osd_timer_cb(gpointer data)
592 OverlayStateData *osd = data;
594 gint changed = FALSE;
597 for (i = 0; i < IMAGE_OSD_COUNT; i++)
599 if (osd->icon_time[i] > 1)
602 if (osd->icon_time[i] < 2)
604 osd->icon_time[i] = 0;
614 if (changed) image_osd_update_schedule(osd, FALSE);
625 static void image_osd_timer_schedule(OverlayStateData *osd)
627 if (osd->timer_id == -1)
629 osd->timer_id = g_timeout_add(100, image_osd_timer_cb, osd);
633 static void image_osd_state_cb(ImageWindow *imd, ImageState state, gpointer data)
635 OverlayStateData *osd = data;
637 osd->changed_states |= state;
638 image_osd_update_schedule(osd, FALSE);
641 static void image_osd_free(OverlayStateData *osd)
645 if (osd->idle_id != -1) g_source_remove(osd->idle_id);
646 if (osd->timer_id != -1) g_source_remove(osd->timer_id);
652 g_object_set_data(G_OBJECT(osd->imd->pr), "IMAGE_OVERLAY_DATA", NULL);
653 g_signal_handler_disconnect(osd->imd->pr, osd->destroy_id);
655 image_set_state_func(osd->imd, NULL, NULL);
656 image_overlay_remove(osd->imd, osd->ovl_info);
658 for (i = 0; i < IMAGE_OSD_COUNT; i++)
660 image_osd_icon_hide(osd, i);
667 static void image_osd_remove(ImageWindow *imd)
669 OverlayStateData *osd;
671 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
675 static void image_osd_destroy_cb(GtkWidget *widget, gpointer data)
677 OverlayStateData *osd = data;
683 static void image_osd_enable(ImageWindow *imd, gint info, gint status)
685 OverlayStateData *osd;
687 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
690 osd = g_new0(OverlayStateData, 1);
695 osd->destroy_id = g_signal_connect(G_OBJECT(imd->pr), "destroy",
696 G_CALLBACK(image_osd_destroy_cb), osd);
697 g_object_set_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA", osd);
699 image_set_state_func(osd->imd, image_osd_state_cb, osd);
702 if (osd->show_info != info ||
703 osd->show_status != status)
705 osd->show_info = info;
706 osd->show_status = status;
708 image_osd_update_schedule(osd, TRUE);
712 void image_osd_set(ImageWindow *imd, gint info, gint status)
716 if (!info && !status)
718 image_osd_remove(imd);
722 image_osd_enable(imd, info, status);
725 gint image_osd_get(ImageWindow *imd, gint *info, gint *status)
727 OverlayStateData *osd;
729 if (!imd) return FALSE;
731 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
732 if (!osd) return FALSE;
734 if (info) *info = osd->show_info;
735 if (status) *status = osd->show_status;
743 2+ = show for duration tenths of a second
744 -1 = use default duration
746 void image_osd_icon(ImageWindow *imd, ImageOSDFlag flag, gint duration)
748 OverlayStateData *osd;
752 osd = g_object_get_data(G_OBJECT(imd->pr), "IMAGE_OVERLAY_DATA");
755 if (flag < IMAGE_OSD_NONE || flag >= IMAGE_OSD_COUNT) return;
756 if (duration < 0) duration = IMAGE_OSD_DEFAULT_DURATION;
757 if (duration > 1) duration += 1;
759 osd->icon_time[flag] = duration;
761 image_osd_update_schedule(osd, FALSE);
762 image_osd_timer_schedule(osd);