2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "pixbuf-util.h"
27 #include "ui-fileops.h"
33 *-----------------------------------------------------------------------------
35 *-----------------------------------------------------------------------------
38 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
40 GError *error = nullptr;
43 if (!pixbuf || !filename) return FALSE;
45 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
46 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
50 log_printf("Error saving png file: %s\n", error->message);
58 *-----------------------------------------------------------------------------
60 *-----------------------------------------------------------------------------
63 #pragma GCC diagnostic push
64 #pragma GCC diagnostic ignored "-Wunused-function"
65 gboolean pixbuf_to_file_as_jpg_unused(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
67 GError *error = nullptr;
71 if (!pixbuf || !filename) return FALSE;
73 if (quality == -1) quality = 75;
74 if (quality < 1 || quality > 100)
76 log_printf("Jpeg not saved, invalid quality %d\n", quality);
80 qbuf = g_strdup_printf("%d", quality);
81 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
86 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
92 #pragma GCC diagnostic pop
95 *-----------------------------------------------------------------------------
97 *-----------------------------------------------------------------------------
106 static PixbufInline inline_pixbuf_data[] = {
107 { PIXBUF_INLINE_ARCHIVE, "gq-icon-archive-file" },
108 { PIXBUF_INLINE_BROKEN, "gq-icon-broken" },
109 { PIXBUF_INLINE_COLLECTION, "gq-icon-collection" },
110 { PIXBUF_INLINE_ICON_180, "gq-icon-rotate-180" },
111 { PIXBUF_INLINE_ICON_BOOK, "gq-icon-book" },
112 { PIXBUF_INLINE_ICON_CONFIG, "gq-icon-config" },
113 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, "gq-icon-draw-rectangle" },
114 { PIXBUF_INLINE_ICON_EXIF, "gq-icon-exif" },
115 { PIXBUF_INLINE_ICON_EXPOSURE, "gq-icon-exposure" },
116 { PIXBUF_INLINE_ICON_FLOAT, "gq-icon-float" },
117 { PIXBUF_INLINE_ICON, "gqview-icon" },
118 { PIXBUF_INLINE_ICON_GRAYSCALE, "gq-icon-grayscale" },
119 { PIXBUF_INLINE_ICON_HEIF, "gq-icon-heic" },
120 { PIXBUF_INLINE_ICON_HIDETOOLS, "gq-icon-hidetools" },
121 { PIXBUF_INLINE_ICON_MAINTENANCE, "gq-icon-maintenance" },
122 { PIXBUF_INLINE_ICON_MARKS, "gq-icon-marks" },
123 { PIXBUF_INLINE_ICON_MOVE, "gq-icon-move" },
124 { PIXBUF_INLINE_ICON_ORIGINAL, "gq-icon-original" },
125 { PIXBUF_INLINE_ICON_PANORAMA, "gq-icon-panorama" },
126 { PIXBUF_INLINE_ICON_PDF, "gq-icon-pdf" },
127 { PIXBUF_INLINE_ICON_PROPERTIES, "gq-icon-properties" },
128 { PIXBUF_INLINE_ICON_RENAME, "gq-icon-rename" },
129 { PIXBUF_INLINE_ICON_SELECT_ALL, "gq-icon-select-all" },
130 { PIXBUF_INLINE_ICON_SELECT_INVERT, "gq-icon-select-invert" },
131 { PIXBUF_INLINE_ICON_SELECT_NONE, "gq-icon-select-none" },
132 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, "gq-icon-select-rectangle" },
133 { PIXBUF_INLINE_ICON_SORT, "gq-icon-sort" },
134 { PIXBUF_INLINE_ICON_THUMB, "gq-icon-thumb" },
135 { PIXBUF_INLINE_ICON_TOOLS, "gq-icon-tools" },
136 { PIXBUF_INLINE_ICON_VIEW, "gq-icon-view" },
137 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, "gq-icon-zoomfillhor" },
138 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, "gq-icon-zoomfillvert" },
139 { PIXBUF_INLINE_LOGO, "geeqie-logo" },
140 { PIXBUF_INLINE_METADATA, "gq-icon-metadata" },
141 { PIXBUF_INLINE_SCROLLER, "gq-scroller" },
142 { PIXBUF_INLINE_SPLIT_PANE_SYNC, "gq-icon-split-pane-sync" },
143 { PIXBUF_INLINE_UNKNOWN, "gq-icon-unknown" },
144 { PIXBUF_INLINE_VIDEO, "gq-icon-video" },
148 GdkPixbuf *pixbuf_inline(const gchar *key)
150 gboolean dark = FALSE;
151 gchar *file_name = nullptr;
154 GdkPixbuf *icon_pixbuf;
155 GError *error = nullptr;
156 GInputStream *in_stream;
158 GtkSettings *settings;
160 if (!key) return nullptr;
162 settings = gtk_settings_get_default();
163 g_object_get(settings, "gtk-theme-name", &theme_name, nullptr);
164 dark = g_str_has_suffix(theme_name, "dark");
168 while (inline_pixbuf_data[i].key)
170 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
172 file_name = g_strconcat(inline_pixbuf_data[i].data, dark ? "-dark" : "", ".png", nullptr);
173 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
176 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
184 file_name = g_strconcat(inline_pixbuf_data[i].data, ".png", nullptr);
185 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
188 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
194 log_printf("warning: inline pixbuf error: %s", error->message);
196 g_object_unref(in_stream);
200 icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, nullptr, &error);
201 g_object_unref(in_stream);
205 log_printf("warning: inline pixbuf error: %s", error->message);
215 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
220 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
222 static GtkIconFactory *icon_factory = nullptr;
223 GtkIconSet *icon_set;
227 icon_factory = gtk_icon_factory_new();
228 gtk_icon_factory_add_default(icon_factory);
231 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
232 gtk_icon_factory_add(icon_factory, key, icon_set);
236 void pixbuf_inline_register_stock_icons()
241 while (inline_pixbuf_data[i].key)
243 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
248 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
250 GtkIconTheme *icon_theme;
252 GError *error = nullptr;
254 icon_theme = gtk_icon_theme_get_default();
256 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
258 pixbuf = gtk_icon_theme_load_icon(icon_theme,
259 icon, /* icon name */
261 static_cast<GtkIconLookupFlags>(0), /* flags */
267 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
272 if (strchr(icon, '.'))
274 /* try again without extension */
275 gchar *icon2 = remove_extension_from_path(icon);
276 pixbuf = gtk_icon_theme_load_icon(icon_theme,
277 icon2, /* icon name */
279 static_cast<GtkIconLookupFlags>(0), /* flags */
283 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
287 /* try as an absolute path */
288 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
291 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
299 if (!pixbuf) return FALSE;
301 register_stock_icon(key, pixbuf);
305 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
306 gint old_w, gint old_h,
307 gint *new_w, gint *new_h)
309 if ((static_cast<gdouble>(req_w) / old_w) < (static_cast<gdouble>(req_h) / old_h))
312 *new_h = static_cast<gdouble>(*new_w) / old_w * old_h;
313 if (*new_h < 1) *new_h = 1;
318 *new_w = static_cast<gdouble>(*new_h) / old_h * old_w;
319 if (*new_w < 1) *new_w = 1;
322 return (*new_w != old_w || *new_h != old_h);
325 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
329 switch (fd->format_class)
331 case FORMAT_CLASS_UNKNOWN:
332 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
334 case FORMAT_CLASS_META:
335 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
337 case FORMAT_CLASS_VIDEO:
338 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
340 case FORMAT_CLASS_COLLECTION:
341 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
343 case FORMAT_CLASS_DOCUMENT:
344 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
346 case FORMAT_CLASS_ARCHIVE:
347 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
350 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
353 if (requested_width && requested_height)
355 gint w = gdk_pixbuf_get_width(pixbuf);
356 gint h = gdk_pixbuf_get_height(pixbuf);
358 if (w > requested_width || h > requested_height)
363 if (pixbuf_scale_aspect(requested_width, requested_height,
369 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
370 g_object_unref(G_OBJECT(tmp));
379 *-----------------------------------------------------------------------------
381 *-----------------------------------------------------------------------------
384 gboolean util_clip_region(gint x, gint y, gint w, gint h,
385 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
386 gint *rx, gint *ry, gint *rw, gint *rh)
388 if (clip_x + clip_w <= x ||
390 clip_y + clip_h <= y ||
396 *rx = MAX(x, clip_x);
397 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
399 *ry = MAX(y, clip_y);
400 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
406 *-----------------------------------------------------------------------------
408 *-----------------------------------------------------------------------------
411 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
412 guchar *dest, gint dest_row_stride, gint w, gint h,
413 gint bytes_per_pixel, gboolean counter_clockwise)
420 for (i = 0; i < h; i++)
422 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
423 for (j = 0; j < w; j++)
425 if (counter_clockwise)
427 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
431 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
433 *(dp++) = *(sp++); /* r */
434 *(dp++) = *(sp++); /* g */
435 *(dp++) = *(sp++); /* b */
436 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
442 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
443 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
449 for (i = 0; i < h; i++)
451 sp = src + (i * src_row_stride);
452 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
453 memcpy(dp, sp, w * bytes_per_pixel);
458 ROTATE_BUFFER_WIDTH = 48,
459 ROTATE_BUFFER_HEIGHT = 48
463 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
466 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
487 if (!src) return nullptr;
489 sw = gdk_pixbuf_get_width(src);
490 sh = gdk_pixbuf_get_height(src);
491 has_alpha = gdk_pixbuf_get_has_alpha(src);
492 srs = gdk_pixbuf_get_rowstride(src);
493 s_pix = gdk_pixbuf_get_pixels(src);
497 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
498 drs = gdk_pixbuf_get_rowstride(dest);
499 d_pix = gdk_pixbuf_get_pixels(dest);
501 a = (has_alpha ? 4 : 3);
503 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
504 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
505 b_pix = gdk_pixbuf_get_pixels(buffer);
506 brs = gdk_pixbuf_get_rowstride(buffer);
508 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
510 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
511 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
516 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
517 pixbuf_copy_block_rotate(s_pix, srs, j, i,
519 a, counter_clockwise);
521 if (counter_clockwise)
531 pixbuf_copy_block(b_pix, brs, w, h,
532 d_pix, drs, x, y, a);
536 g_object_unref(buffer);
539 /* this is the simple version of rotation (roughly 2-4x slower) */
541 for (i = 0; i < sh; i++)
543 sp = s_pix + (i * srs);
544 for (j = 0; j < sw; j++)
546 if (counter_clockwise)
548 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
552 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
555 *(dp++) = *(sp++); /* r */
556 *(dp++) = *(sp++); /* g */
557 *(dp++) = *(sp++); /* b */
558 if (has_alpha) *(dp) = *(sp++); /* a */
567 * Returns a copy of pixbuf mirrored and or flipped.
568 * TO do a 180 degree rotations set both mirror and flipped TRUE
569 * if mirror and flip are FALSE, result is a simple copy.
571 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
587 if (!src) return nullptr;
589 w = gdk_pixbuf_get_width(src);
590 h = gdk_pixbuf_get_height(src);
591 has_alpha = gdk_pixbuf_get_has_alpha(src);
592 srs = gdk_pixbuf_get_rowstride(src);
593 s_pix = gdk_pixbuf_get_pixels(src);
595 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
596 drs = gdk_pixbuf_get_rowstride(dest);
597 d_pix = gdk_pixbuf_get_pixels(dest);
599 a = has_alpha ? 4 : 3;
601 for (i = 0; i < h; i++)
603 sp = s_pix + (i * srs);
606 dp = d_pix + ((h - i - 1) * drs);
610 dp = d_pix + (i * drs);
615 for (j = 0; j < w; j++)
617 *(dp++) = *(sp++); /* r */
618 *(dp++) = *(sp++); /* g */
619 *(dp++) = *(sp++); /* b */
620 if (has_alpha) *(dp) = *(sp++); /* a */
626 for (j = 0; j < w; j++)
628 *(dp++) = *(sp++); /* r */
629 *(dp++) = *(sp++); /* g */
630 *(dp++) = *(sp++); /* b */
631 if (has_alpha) *(dp++) = *(sp++); /* a */
639 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
642 GdkPixbuf *tmp = nullptr;
646 case EXIF_ORIENTATION_TOP_LEFT:
647 dest = gdk_pixbuf_copy(pixbuf);
649 case EXIF_ORIENTATION_TOP_RIGHT:
651 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
653 case EXIF_ORIENTATION_BOTTOM_RIGHT:
655 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
657 case EXIF_ORIENTATION_BOTTOM_LEFT:
659 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
661 case EXIF_ORIENTATION_LEFT_TOP:
662 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
663 dest = pixbuf_copy_rotate_90(tmp, FALSE);
665 case EXIF_ORIENTATION_RIGHT_TOP:
666 /* rotated -90 (270) */
667 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
669 case EXIF_ORIENTATION_RIGHT_BOTTOM:
670 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
671 dest = pixbuf_copy_rotate_90(tmp, TRUE);
673 case EXIF_ORIENTATION_LEFT_BOTTOM:
675 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
678 dest = gdk_pixbuf_copy(pixbuf);
681 if (tmp) g_object_unref(tmp);
688 *-----------------------------------------------------------------------------
689 * pixbuf drawing (rectangles)
690 *-----------------------------------------------------------------------------
694 * Fills region of pixbuf at x,y over w,h
695 * with colors red (r), green (g), blue (b)
696 * applying alpha (a), use a=255 for solid.
698 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
699 gint x, gint y, gint w, gint h,
700 gint r, gint g, gint b, gint a)
713 pw = gdk_pixbuf_get_width(pb);
714 ph = gdk_pixbuf_get_height(pb);
716 if (x < 0 || x + w > pw) return;
717 if (y < 0 || y + h > ph) return;
719 has_alpha = gdk_pixbuf_get_has_alpha(pb);
720 prs = gdk_pixbuf_get_rowstride(pb);
721 p_pix = gdk_pixbuf_get_pixels(pb);
723 for (i = 0; i < h; i++)
725 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
726 for (j = 0; j < w; j++)
728 *pp = (r * a + *pp * (256-a)) >> 8;
730 *pp = (g * a + *pp * (256-a)) >> 8;
732 *pp = (b * a + *pp * (256-a)) >> 8;
739 #pragma GCC diagnostic push
740 #pragma GCC diagnostic ignored "-Wunused-function"
741 void pixbuf_draw_rect_unused(GdkPixbuf *pb,
742 gint x, gint y, gint w, gint h,
743 gint r, gint g, gint b, gint a,
744 gint left, gint right, gint top, gint bottom)
746 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
748 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
750 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
752 pixbuf_draw_rect_fill(pb, x, y, left, h,
755 #pragma GCC diagnostic pop
757 void pixbuf_set_rect_fill(GdkPixbuf *pb,
758 gint x, gint y, gint w, gint h,
759 gint r, gint g, gint b, gint a)
772 pw = gdk_pixbuf_get_width(pb);
773 ph = gdk_pixbuf_get_height(pb);
775 if (x < 0 || x + w > pw) return;
776 if (y < 0 || y + h > ph) return;
778 has_alpha = gdk_pixbuf_get_has_alpha(pb);
779 prs = gdk_pixbuf_get_rowstride(pb);
780 p_pix = gdk_pixbuf_get_pixels(pb);
782 for (i = 0; i < h; i++)
784 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
785 for (j = 0; j < w; j++)
790 if (has_alpha) { *pp = a; pp++; }
795 void pixbuf_set_rect(GdkPixbuf *pb,
796 gint x, gint y, gint w, gint h,
797 gint r, gint g, gint b, gint a,
798 gint left, gint right, gint top, gint bottom)
800 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
802 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
804 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
806 pixbuf_set_rect_fill(pb, x, y, left, h,
810 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
817 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
818 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
820 buf = gdk_pixbuf_get_pixels(pb);
821 has_alpha = gdk_pixbuf_get_has_alpha(pb);
822 rowstride = gdk_pixbuf_get_rowstride(pb);
824 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
828 if (has_alpha) *p = a;
833 *-----------------------------------------------------------------------------
834 * pixbuf text rendering
835 *-----------------------------------------------------------------------------
838 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
839 GdkPixbuf *dest, gint dx, gint dy,
841 guint8 r, guint8 g, guint8 b, guint8 a)
861 if (!src || !dest) return;
863 sw = gdk_pixbuf_get_width(src);
864 sh = gdk_pixbuf_get_height(src);
866 if (sx < 0 || sx + w > sw) return;
867 if (sy < 0 || sy + h > sh) return;
869 dw = gdk_pixbuf_get_width(dest);
870 dh = gdk_pixbuf_get_height(dest);
872 if (dx < 0 || dx + w > dw) return;
873 if (dy < 0 || dy + h > dh) return;
875 s_alpha = gdk_pixbuf_get_has_alpha(src);
876 d_alpha = gdk_pixbuf_get_has_alpha(dest);
877 srs = gdk_pixbuf_get_rowstride(src);
878 drs = gdk_pixbuf_get_rowstride(dest);
879 s_pix = gdk_pixbuf_get_pixels(src);
880 d_pix = gdk_pixbuf_get_pixels(dest);
882 s_step = (s_alpha) ? 4 : 3;
883 d_step = (d_alpha) ? 4 : 3;
885 for (i = 0; i < h; i++)
887 sp = s_pix + (sy + i) * srs + sx * s_step;
888 dp = d_pix + (dy + i) * drs + dx * d_step;
889 for (j = 0; j < w; j++)
895 asub = a * sp[0] / 255;
896 *dp = (r * asub + *dp * (256-asub)) >> 8;
898 asub = a * sp[1] / 255;
899 *dp = (g * asub + *dp * (256-asub)) >> 8;
901 asub = a * sp[2] / 255;
902 *dp = (b * asub + *dp * (256-asub)) >> 8;
907 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
921 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *,
923 guint8 r, guint8 g, guint8 b, guint8 a)
932 cairo_surface_t *source;
935 pango_layout_get_pixel_size(layout, &w, &h);
936 if (w < 1 || h < 1) return;
938 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
940 cr = cairo_create (source);
941 cairo_set_source_rgb(cr, 0, 0, 0);
942 cairo_rectangle (cr, 0, 0, w, h);
944 cairo_set_source_rgb(cr, 1, 1, 1);
945 pango_cairo_show_layout (cr, layout);
948 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
950 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
952 cairo_image_surface_get_width (source),
953 cairo_image_surface_get_height (source),
954 cairo_image_surface_get_stride (source),
960 dw = gdk_pixbuf_get_width(pixbuf);
961 dh = gdk_pixbuf_get_height(pixbuf);
977 if (x + w > dw) w = dw - x;
978 if (y + h > dh) h = dh - y;
980 pixbuf_copy_font(buffer, sx, sy,
984 g_object_unref(buffer);
985 cairo_surface_destroy(source);
989 *-----------------------------------------------------------------------------
990 * pixbuf drawing (triangle)
991 *-----------------------------------------------------------------------------
994 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
995 gint *rx, gint *ry, gint *rw, gint *rh)
1006 tw = MAX(abs(x1 - x2), abs(x2 - x3));
1007 tw = MAX(tw, abs(x3 - x1));
1008 th = MAX(abs(y1 - y2), abs(y2 - y3));
1009 th = MAX(th, abs(y3 - y1));
1017 void pixbuf_draw_triangle(GdkPixbuf *pb,
1018 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1019 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
1020 guint8 r, guint8 g, guint8 b, guint8 a)
1049 gboolean middle = FALSE;
1053 pw = gdk_pixbuf_get_width(pb);
1054 ph = gdk_pixbuf_get_height(pb);
1056 if (!util_clip_region(0, 0, pw, ph,
1057 clip_x, clip_y, clip_w, clip_h,
1058 &rx, &ry, &rw, &rh)) return;
1060 util_clip_triangle(x1, y1, x2, y2, x3, y3,
1061 &tx, &ty, &tw, &th);
1063 if (!util_clip_region(rx, ry, rw, rh,
1065 &fx1, &fy1, &fw, &fh)) return;
1069 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1070 prs = gdk_pixbuf_get_rowstride(pb);
1071 p_pix = gdk_pixbuf_get_pixels(pb);
1073 p_step = (has_alpha) ? 4 : 3;
1077 t = x1; x1 = x2; x2 = t;
1078 t = y1; y1 = y2; y2 = t;
1082 t = x2; x2 = x3; x3 = t;
1083 t = y2; y2 = y3; y3 = t;
1087 t = x1; x1 = x2; x2 = t;
1088 t = y1; y1 = y2; y2 = t;
1091 slope1 = static_cast<gdouble>(y2 - y1);
1092 if (slope1) slope1 = static_cast<gdouble>(x2 - x1) / slope1;
1095 slope2 = static_cast<gdouble>(y3 - y1);
1096 if (slope2) slope2 = static_cast<gdouble>(x3 - x1) / slope2;
1098 for (y = fy1; y < fy2; y++)
1103 if (!middle && y > y2)
1105 slope1 = static_cast<gdouble>(y3 - y2);
1106 if (slope1) slope1 = static_cast<gdouble>(x3 - x2) / slope1;
1113 xa = slope1_x + (slope1 * (y - slope1_y) + 0.5);
1114 xb = x1 + (slope2 * (y - y1) + 0.5);
1118 t = xa; xa = xb; xb = t;
1121 xa = CLAMP(xa, fx1, fx2);
1122 xb = CLAMP(xb, fx1, fx2);
1124 pp = p_pix + y * prs + xa * p_step;
1128 *pp = (r * a + *pp * (256-a)) >> 8;
1130 *pp = (g * a + *pp * (256-a)) >> 8;
1132 *pp = (b * a + *pp * (256-a)) >> 8;
1134 if (has_alpha) pp++;
1142 *-----------------------------------------------------------------------------
1143 * pixbuf drawing (line)
1144 *-----------------------------------------------------------------------------
1147 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1148 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1149 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1151 gboolean flip = FALSE;
1158 t = x1; x1 = x2; x2 = t;
1159 t = y1; y1 = y2; y2 = t;
1163 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1167 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1171 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1179 slope = (y2 - y1) / d;
1182 y1 = y1 + slope * (clip_x - x1);
1185 if (x2 > clip_x + clip_w)
1187 y2 = y2 + slope * (clip_x + clip_w - x2);
1188 x2 = clip_x + clip_w;
1194 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1200 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1202 t = x1; x1 = x2; x2 = t;
1203 t = y1; y1 = y2; y2 = t;
1212 slope = (x2 - x1) / d;
1215 x1 = x1 + slope * (clip_y - y1);
1218 if (y2 > clip_y + clip_h)
1220 x2 = x2 + slope * (clip_y + clip_h - y2);
1221 y2 = clip_y + clip_h;
1243 void pixbuf_draw_line(GdkPixbuf *pb,
1244 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1245 gint x1, gint y1, gint x2, gint y2,
1246 guint8 r, guint8 g, guint8 b, guint8 a)
1275 pw = gdk_pixbuf_get_width(pb);
1276 ph = gdk_pixbuf_get_height(pb);
1278 if (!util_clip_region(0, 0, pw, ph,
1279 clip_x, clip_y, clip_w, clip_h,
1280 &rx, &ry, &rw, &rh)) return;
1281 if (!util_clip_line(static_cast<gdouble>(rx), static_cast<gdouble>(ry), static_cast<gdouble>(rw), static_cast<gdouble>(rh),
1282 static_cast<gdouble>(x1), static_cast<gdouble>(y1), static_cast<gdouble>(x2), static_cast<gdouble>(y2),
1283 &rx1, &ry1, &rx2, &ry2)) return;
1290 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1291 prs = gdk_pixbuf_get_rowstride(pb);
1292 p_pix = gdk_pixbuf_get_pixels(pb);
1294 p_step = (has_alpha) ? 4 : 3;
1296 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1301 t = rx1; rx1 = rx2; rx2 = t;
1302 t = ry1; ry1 = ry2; ry2 = t;
1306 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1307 for (x = rx1; x < rx2; x += 1.0)
1309 px = static_cast<gint>(x + 0.5);
1310 py = static_cast<gint>(ry1 + (x - rx1) * slope + 0.5);
1312 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1314 pp = p_pix + py * prs + px * p_step;
1315 *pp = (r * a + *pp * (256-a)) >> 8;
1317 *pp = (g * a + *pp * (256-a)) >> 8;
1319 *pp = (b * a + *pp * (256-a)) >> 8;
1328 t = rx1; rx1 = rx2; rx2 = t;
1329 t = ry1; ry1 = ry2; ry2 = t;
1333 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1334 for (y = ry1; y < ry2; y += 1.0)
1336 px = static_cast<gint>(rx1 + (y - ry1) * slope + 0.5);
1337 py = static_cast<gint>(y + 0.5);
1339 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1341 pp = p_pix + py * prs + px * p_step;
1342 *pp = (r * a + *pp * (256-a)) >> 8;
1344 *pp = (g * a + *pp * (256-a)) >> 8;
1346 *pp = (b * a + *pp * (256-a)) >> 8;
1353 *-----------------------------------------------------------------------------
1354 * pixbuf drawing (fades and shadows)
1355 *-----------------------------------------------------------------------------
1358 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1359 gint s, gboolean vertical, gint border,
1360 gint x1, gint y1, gint x2, gint y2,
1361 guint8 r, guint8 g, guint8 b, guint8 a)
1369 p_step = (has_alpha) ? 4 : 3;
1370 for (j = y1; j < y2; j++)
1372 pp = p_pix + j * prs + x1 * p_step;
1373 if (!vertical) n = a - a * abs(j - s) / border;
1374 for (i = x1; i < x2; i++)
1376 if (vertical) n = a - a * abs(i - s) / border;
1377 *pp = (r * n + *pp * (256-n)) >> 8;
1379 *pp = (g * n + *pp * (256-n)) >> 8;
1381 *pp = (b * n + *pp * (256-n)) >> 8;
1383 if (has_alpha) pp++;
1388 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1389 gint sx, gint sy, gint border,
1390 gint x1, gint y1, gint x2, gint y2,
1391 guint8, guint8 g, guint8 b, guint8 a)
1398 p_step = (has_alpha) ? 4 : 3;
1399 for (j = y1; j < y2; j++)
1401 pp = p_pix + j * prs + x1 * p_step;
1402 for (i = x1; i < x2; i++)
1407 r = MIN(border, (gint)hypot(i - sx, j - sy));
1408 n = a - a * r / border;
1409 *pp = (r * n + *pp * (256-n)) >> 8;
1411 *pp = (g * n + *pp * (256-n)) >> 8;
1413 *pp = (b * n + *pp * (256-n)) >> 8;
1415 if (has_alpha) pp++;
1420 void pixbuf_draw_shadow(GdkPixbuf *pb,
1421 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1422 gint x, gint y, gint w, gint h, gint border,
1423 guint8 r, guint8 g, guint8 b, guint8 a)
1441 pw = gdk_pixbuf_get_width(pb);
1442 ph = gdk_pixbuf_get_height(pb);
1444 if (!util_clip_region(0, 0, pw, ph,
1445 clip_x, clip_y, clip_w, clip_h,
1446 &rx, &ry, &rw, &rh)) return;
1448 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1449 prs = gdk_pixbuf_get_rowstride(pb);
1450 p_pix = gdk_pixbuf_get_pixels(pb);
1452 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1454 &fx, &fy, &fw, &fh))
1456 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1459 if (border < 1) return;
1461 if (util_clip_region(x, y + border, border, h - border * 2,
1463 &fx, &fy, &fw, &fh))
1465 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1466 x + border, TRUE, border,
1467 fx, fy, fx + fw, fy + fh,
1470 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1472 &fx, &fy, &fw, &fh))
1474 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1475 x + w - border, TRUE, border,
1476 fx, fy, fx + fw, fy + fh,
1479 if (util_clip_region(x + border, y, w - border * 2, border,
1481 &fx, &fy, &fw, &fh))
1483 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1484 y + border, FALSE, border,
1485 fx, fy, fx + fw, fy + fh,
1488 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1490 &fx, &fy, &fw, &fh))
1492 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1493 y + h - border, FALSE, border,
1494 fx, fy, fx + fw, fy + fh,
1497 if (util_clip_region(x, y, border, border,
1499 &fx, &fy, &fw, &fh))
1501 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1502 x + border, y + border, border,
1503 fx, fy, fx + fw, fy + fh,
1506 if (util_clip_region(x + w - border, y, border, border,
1508 &fx, &fy, &fw, &fh))
1510 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1511 x + w - border, y + border, border,
1512 fx, fy, fx + fw, fy + fh,
1515 if (util_clip_region(x, y + h - border, border, border,
1517 &fx, &fy, &fw, &fh))
1519 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1520 x + border, y + h - border, border,
1521 fx, fy, fx + fw, fy + fh,
1524 if (util_clip_region(x + w - border, y + h - border, border, border,
1526 &fx, &fy, &fw, &fh))
1528 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1529 x + w - border, y + h - border, border,
1530 fx, fy, fx + fw, fy + fh,
1537 *-----------------------------------------------------------------------------
1538 * pixbuf color alterations
1539 *-----------------------------------------------------------------------------
1542 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1543 gint x, gint y, gint w, gint h)
1556 pw = gdk_pixbuf_get_width(pb);
1557 ph = gdk_pixbuf_get_height(pb);
1559 if (x < 0 || x + w > pw) return;
1560 if (y < 0 || y + h > ph) return;
1562 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1563 prs = gdk_pixbuf_get_rowstride(pb);
1564 p_pix = gdk_pixbuf_get_pixels(pb);
1566 for (i = 0; i < h; i++)
1568 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1569 for (j = 0; j < w; j++)
1573 grey = (pp[0] + pp[1] + pp[2]) / 3;
1580 if (has_alpha) pp++;
1586 *-----------------------------------------------------------------------------
1587 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1589 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1602 pw = gdk_pixbuf_get_width(pb);
1603 ph = gdk_pixbuf_get_height(pb);
1605 if (x < 0 || x + w > pw) return;
1606 if (y < 0 || y + h > ph) return;
1608 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1609 prs = gdk_pixbuf_get_rowstride(pb);
1610 p_pix = gdk_pixbuf_get_pixels(pb);
1612 for (i = 0; i < h; i++)
1614 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1615 for (j = 0; j < w; j++)
1617 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1625 if (has_alpha) pp++;
1630 if (has_alpha) pp++;
1637 *-----------------------------------------------------------------------------
1638 * pixbuf ignore alpha
1639 *-----------------------------------------------------------------------------
1641 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1642 gint x, gint y, gint w, gint h)
1655 pw = gdk_pixbuf_get_width(pb);
1656 ph = gdk_pixbuf_get_height(pb);
1658 if (x < 0 || x + w > pw) return;
1659 if (y < 0 || y + h > ph) return;
1661 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1662 if (!has_alpha) return;
1664 prs = gdk_pixbuf_get_rowstride(pb);
1665 p_pix = gdk_pixbuf_get_pixels(pb);
1667 for (i = 0; i < h; i++)
1669 pp = p_pix + (y + i) * prs + (x * 4 );
1670 for (j = 0; j < w; j++)
1677 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */