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.
22 #include "pixbuf-util.h"
32 #include <glib-object.h>
33 #include <pango/pangocairo.h>
40 #include "main-defines.h"
42 #include "ui-fileops.h"
46 *-----------------------------------------------------------------------------
48 *-----------------------------------------------------------------------------
51 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
53 GError *error = nullptr;
56 if (!pixbuf || !filename) return FALSE;
58 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
59 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
63 log_printf("Error saving png file: %s\n", error->message);
71 *-----------------------------------------------------------------------------
73 *-----------------------------------------------------------------------------
76 #pragma GCC diagnostic push
77 #pragma GCC diagnostic ignored "-Wunused-function"
78 gboolean pixbuf_to_file_as_jpg_unused(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
80 GError *error = nullptr;
84 if (!pixbuf || !filename) return FALSE;
86 if (quality == -1) quality = 75;
87 if (quality < 1 || quality > 100)
89 log_printf("Jpeg not saved, invalid quality %d\n", quality);
93 qbuf = g_strdup_printf("%d", quality);
94 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
99 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
105 #pragma GCC diagnostic pop
108 *-----------------------------------------------------------------------------
110 *-----------------------------------------------------------------------------
119 static PixbufInline inline_pixbuf_data[] = {
120 { PIXBUF_INLINE_ARCHIVE, "gq-icon-archive-file" },
121 { PIXBUF_INLINE_BROKEN, "gq-icon-broken" },
122 { PIXBUF_INLINE_COLLECTION, "gq-icon-collection" },
123 { PIXBUF_INLINE_ICON_180, "gq-icon-rotate-180" },
124 { PIXBUF_INLINE_ICON_BOOK, "gq-icon-book" },
125 { PIXBUF_INLINE_ICON_CONFIG, "gq-icon-config" },
126 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, "gq-icon-draw-rectangle" },
127 { PIXBUF_INLINE_ICON_EXIF, "gq-icon-exif" },
128 { PIXBUF_INLINE_ICON_EXPOSURE, "gq-icon-exposure" },
129 { PIXBUF_INLINE_ICON_FLOAT, "gq-icon-float" },
130 { PIXBUF_INLINE_ICON, "gqview-icon" },
131 { PIXBUF_INLINE_ICON_GRAYSCALE, "gq-icon-grayscale" },
132 { PIXBUF_INLINE_ICON_HEIF, "gq-icon-heic" },
133 { PIXBUF_INLINE_ICON_HIDETOOLS, "gq-icon-hidetools" },
134 { PIXBUF_INLINE_ICON_MAINTENANCE, "gq-icon-maintenance" },
135 { PIXBUF_INLINE_ICON_MARKS, "gq-icon-marks" },
136 { PIXBUF_INLINE_ICON_MOVE, "gq-icon-move" },
137 { PIXBUF_INLINE_ICON_ORIGINAL, "gq-icon-original" },
138 { PIXBUF_INLINE_ICON_PANORAMA, "gq-icon-panorama" },
139 { PIXBUF_INLINE_ICON_PDF, "gq-icon-pdf" },
140 { PIXBUF_INLINE_ICON_PROPERTIES, "gq-icon-properties" },
141 { PIXBUF_INLINE_ICON_RENAME, "gq-icon-rename" },
142 { PIXBUF_INLINE_ICON_SELECT_ALL, "gq-icon-select-all" },
143 { PIXBUF_INLINE_ICON_SELECT_INVERT, "gq-icon-select-invert" },
144 { PIXBUF_INLINE_ICON_SELECT_NONE, "gq-icon-select-none" },
145 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, "gq-icon-select-rectangle" },
146 { PIXBUF_INLINE_ICON_SORT, "gq-icon-sort" },
147 { PIXBUF_INLINE_ICON_THUMB, "gq-icon-thumb" },
148 { PIXBUF_INLINE_ICON_TOOLS, "gq-icon-tools" },
149 { PIXBUF_INLINE_ICON_VIEW, "gq-icon-view" },
150 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, "gq-icon-zoomfillhor" },
151 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, "gq-icon-zoomfillvert" },
152 { PIXBUF_INLINE_LOGO, "geeqie-logo" },
153 { PIXBUF_INLINE_METADATA, "gq-icon-metadata" },
154 { PIXBUF_INLINE_SCROLLER, "gq-scroller" },
155 { PIXBUF_INLINE_SPLIT_PANE_SYNC, "gq-icon-split-pane-sync" },
156 { PIXBUF_INLINE_UNKNOWN, "gq-icon-unknown" },
157 { PIXBUF_INLINE_VIDEO, "gq-icon-video" },
161 GdkPixbuf *pixbuf_inline(const gchar *key)
163 gboolean dark = FALSE;
164 gchar *file_name = nullptr;
167 GdkPixbuf *icon_pixbuf;
168 GError *error = nullptr;
169 GInputStream *in_stream;
171 GtkSettings *settings;
173 if (!key) return nullptr;
175 settings = gtk_settings_get_default();
176 g_object_get(settings, "gtk-theme-name", &theme_name, nullptr);
177 dark = g_str_has_suffix(theme_name, "dark");
181 while (inline_pixbuf_data[i].key)
183 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
185 file_name = g_strconcat(inline_pixbuf_data[i].data, dark ? "-dark" : "", ".png", nullptr);
186 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
189 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
197 file_name = g_strconcat(inline_pixbuf_data[i].data, ".png", nullptr);
198 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
201 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
207 log_printf("warning: inline pixbuf error: %s", error->message);
209 g_object_unref(in_stream);
213 icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, nullptr, &error);
214 g_object_unref(in_stream);
218 log_printf("warning: inline pixbuf error: %s", error->message);
228 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
233 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
235 static GtkIconFactory *icon_factory = nullptr;
236 GtkIconSet *icon_set;
240 icon_factory = gtk_icon_factory_new();
241 gtk_icon_factory_add_default(icon_factory);
244 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
245 gtk_icon_factory_add(icon_factory, key, icon_set);
249 void pixbuf_inline_register_stock_icons()
254 while (inline_pixbuf_data[i].key)
256 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
261 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
263 GtkIconTheme *icon_theme;
265 GError *error = nullptr;
267 icon_theme = gtk_icon_theme_get_default();
269 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
271 pixbuf = gtk_icon_theme_load_icon(icon_theme,
272 icon, /* icon name */
274 static_cast<GtkIconLookupFlags>(0), /* flags */
280 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
285 if (strchr(icon, '.'))
287 /* try again without extension */
288 gchar *icon2 = remove_extension_from_path(icon);
289 pixbuf = gtk_icon_theme_load_icon(icon_theme,
290 icon2, /* icon name */
292 static_cast<GtkIconLookupFlags>(0), /* flags */
296 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
300 /* try as an absolute path */
301 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
304 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
312 if (!pixbuf) return FALSE;
314 register_stock_icon(key, pixbuf);
318 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
319 gint old_w, gint old_h,
320 gint *new_w, gint *new_h)
322 if ((static_cast<gdouble>(req_w) / old_w) < (static_cast<gdouble>(req_h) / old_h))
325 *new_h = static_cast<gdouble>(*new_w) / old_w * old_h;
326 if (*new_h < 1) *new_h = 1;
331 *new_w = static_cast<gdouble>(*new_h) / old_h * old_w;
332 if (*new_w < 1) *new_w = 1;
335 return (*new_w != old_w || *new_h != old_h);
338 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
342 switch (fd->format_class)
344 case FORMAT_CLASS_UNKNOWN:
345 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
347 case FORMAT_CLASS_META:
348 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
350 case FORMAT_CLASS_VIDEO:
351 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
353 case FORMAT_CLASS_COLLECTION:
354 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
356 case FORMAT_CLASS_DOCUMENT:
357 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
359 case FORMAT_CLASS_ARCHIVE:
360 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
363 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
366 if (requested_width && requested_height)
368 gint w = gdk_pixbuf_get_width(pixbuf);
369 gint h = gdk_pixbuf_get_height(pixbuf);
371 if (w > requested_width || h > requested_height)
376 if (pixbuf_scale_aspect(requested_width, requested_height,
382 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
383 g_object_unref(G_OBJECT(tmp));
392 *-----------------------------------------------------------------------------
394 *-----------------------------------------------------------------------------
397 gboolean util_clip_region(gint x, gint y, gint w, gint h,
398 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
399 gint &rx, gint &ry, gint &rw, gint &rh)
401 GdkRectangle main{x, y, w, h};
402 GdkRectangle clip{clip_x, clip_y, clip_w, clip_h};
405 const gboolean rectangles_intersect = gdk_rectangle_intersect(&main, &clip, &r);
406 if (rectangles_intersect)
414 return rectangles_intersect;
418 *-----------------------------------------------------------------------------
420 *-----------------------------------------------------------------------------
423 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
424 guchar *dest, gint dest_row_stride, gint w, gint h,
425 gint bytes_per_pixel, gboolean counter_clockwise)
432 for (i = 0; i < h; i++)
434 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
435 for (j = 0; j < w; j++)
437 if (counter_clockwise)
439 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
443 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
445 *(dp++) = *(sp++); /* r */
446 *(dp++) = *(sp++); /* g */
447 *(dp++) = *(sp++); /* b */
448 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
454 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
455 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
461 for (i = 0; i < h; i++)
463 sp = src + (i * src_row_stride);
464 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
465 memcpy(dp, sp, w * bytes_per_pixel);
470 ROTATE_BUFFER_WIDTH = 48,
471 ROTATE_BUFFER_HEIGHT = 48
475 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
478 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
499 if (!src) return nullptr;
501 sw = gdk_pixbuf_get_width(src);
502 sh = gdk_pixbuf_get_height(src);
503 has_alpha = gdk_pixbuf_get_has_alpha(src);
504 srs = gdk_pixbuf_get_rowstride(src);
505 s_pix = gdk_pixbuf_get_pixels(src);
509 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
510 drs = gdk_pixbuf_get_rowstride(dest);
511 d_pix = gdk_pixbuf_get_pixels(dest);
513 a = (has_alpha ? 4 : 3);
515 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
516 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
517 b_pix = gdk_pixbuf_get_pixels(buffer);
518 brs = gdk_pixbuf_get_rowstride(buffer);
520 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
522 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
523 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
528 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
529 pixbuf_copy_block_rotate(s_pix, srs, j, i,
531 a, counter_clockwise);
533 if (counter_clockwise)
543 pixbuf_copy_block(b_pix, brs, w, h,
544 d_pix, drs, x, y, a);
548 g_object_unref(buffer);
551 /* this is the simple version of rotation (roughly 2-4x slower) */
553 for (i = 0; i < sh; i++)
555 sp = s_pix + (i * srs);
556 for (j = 0; j < sw; j++)
558 if (counter_clockwise)
560 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
564 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
567 *(dp++) = *(sp++); /* r */
568 *(dp++) = *(sp++); /* g */
569 *(dp++) = *(sp++); /* b */
570 if (has_alpha) *(dp) = *(sp++); /* a */
579 * Returns a copy of pixbuf mirrored and or flipped.
580 * TO do a 180 degree rotations set both mirror and flipped TRUE
581 * if mirror and flip are FALSE, result is a simple copy.
583 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
599 if (!src) return nullptr;
601 w = gdk_pixbuf_get_width(src);
602 h = gdk_pixbuf_get_height(src);
603 has_alpha = gdk_pixbuf_get_has_alpha(src);
604 srs = gdk_pixbuf_get_rowstride(src);
605 s_pix = gdk_pixbuf_get_pixels(src);
607 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
608 drs = gdk_pixbuf_get_rowstride(dest);
609 d_pix = gdk_pixbuf_get_pixels(dest);
611 a = has_alpha ? 4 : 3;
613 for (i = 0; i < h; i++)
615 sp = s_pix + (i * srs);
618 dp = d_pix + ((h - i - 1) * drs);
622 dp = d_pix + (i * drs);
627 for (j = 0; j < w; j++)
629 *(dp++) = *(sp++); /* r */
630 *(dp++) = *(sp++); /* g */
631 *(dp++) = *(sp++); /* b */
632 if (has_alpha) *(dp) = *(sp++); /* a */
638 for (j = 0; j < w; j++)
640 *(dp++) = *(sp++); /* r */
641 *(dp++) = *(sp++); /* g */
642 *(dp++) = *(sp++); /* b */
643 if (has_alpha) *(dp++) = *(sp++); /* a */
651 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
654 GdkPixbuf *tmp = nullptr;
658 case EXIF_ORIENTATION_TOP_LEFT:
659 dest = gdk_pixbuf_copy(pixbuf);
661 case EXIF_ORIENTATION_TOP_RIGHT:
663 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
665 case EXIF_ORIENTATION_BOTTOM_RIGHT:
667 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
669 case EXIF_ORIENTATION_BOTTOM_LEFT:
671 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
673 case EXIF_ORIENTATION_LEFT_TOP:
674 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
675 dest = pixbuf_copy_rotate_90(tmp, FALSE);
677 case EXIF_ORIENTATION_RIGHT_TOP:
678 /* rotated -90 (270) */
679 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
681 case EXIF_ORIENTATION_RIGHT_BOTTOM:
682 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
683 dest = pixbuf_copy_rotate_90(tmp, TRUE);
685 case EXIF_ORIENTATION_LEFT_BOTTOM:
687 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
690 dest = gdk_pixbuf_copy(pixbuf);
693 if (tmp) g_object_unref(tmp);
700 *-----------------------------------------------------------------------------
701 * pixbuf drawing (rectangles)
702 *-----------------------------------------------------------------------------
706 * Fills region of pixbuf at x,y over w,h
707 * with colors red (r), green (g), blue (b)
708 * applying alpha (a), use a=255 for solid.
710 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
711 gint x, gint y, gint w, gint h,
712 gint r, gint g, gint b, gint a)
725 pw = gdk_pixbuf_get_width(pb);
726 ph = gdk_pixbuf_get_height(pb);
728 if (x < 0 || x + w > pw) return;
729 if (y < 0 || y + h > ph) return;
731 has_alpha = gdk_pixbuf_get_has_alpha(pb);
732 prs = gdk_pixbuf_get_rowstride(pb);
733 p_pix = gdk_pixbuf_get_pixels(pb);
735 const gint p_step = has_alpha ? 4 : 3;
737 for (i = 0; i < h; i++)
739 pp = p_pix + (y + i) * prs + (x * p_step);
740 for (j = 0; j < w; j++)
742 pp[0] = (r * a + pp[0] * (256-a)) >> 8;
743 pp[1] = (g * a + pp[1] * (256-a)) >> 8;
744 pp[2] = (b * a + pp[2] * (256-a)) >> 8;
745 // TODO(xsdg): Should we do anything about a potential
746 // existing alpha value here?
752 #pragma GCC diagnostic push
753 #pragma GCC diagnostic ignored "-Wunused-function"
754 void pixbuf_draw_rect_unused(GdkPixbuf *pb,
755 gint x, gint y, gint w, gint h,
756 gint r, gint g, gint b, gint a,
757 gint left, gint right, gint top, gint bottom)
759 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
761 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
763 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
765 pixbuf_draw_rect_fill(pb, x, y, left, h,
768 #pragma GCC diagnostic pop
770 void pixbuf_set_rect_fill(GdkPixbuf *pb,
771 gint x, gint y, gint w, gint h,
772 gint r, gint g, gint b, gint a)
785 pw = gdk_pixbuf_get_width(pb);
786 ph = gdk_pixbuf_get_height(pb);
788 if (x < 0 || x + w > pw) return;
789 if (y < 0 || y + h > ph) return;
791 has_alpha = gdk_pixbuf_get_has_alpha(pb);
792 prs = gdk_pixbuf_get_rowstride(pb);
793 p_pix = gdk_pixbuf_get_pixels(pb);
795 const gint p_step = has_alpha ? 4 : 3;
797 for (i = 0; i < h; i++)
799 pp = p_pix + (y + i) * prs + (x * p_step);
800 for (j = 0; j < w; j++)
805 if (has_alpha) { *pp = a; pp++; }
810 void pixbuf_set_rect(GdkPixbuf *pb,
811 gint x, gint y, gint w, gint h,
812 gint r, gint g, gint b, gint a,
813 gint left_width, gint right_width, gint top_width, gint bottom_width)
815 // TODO(xsdg): This function has multiple off-by-one errors. Would be
816 // much easier to read (and implement correctly) with temporaries to
817 // translate from (x, y, w, h) coordinates to (x1, y1, x2, y2).
818 pixbuf_set_rect_fill(pb,
819 x + left_width, y, w - left_width - right_width, top_width,
821 pixbuf_set_rect_fill(pb,
822 x + w - right_width, y, right_width, h,
824 pixbuf_set_rect_fill(pb,
825 x + left_width, y + h - bottom_width, w - left_width - right_width, bottom_width,
827 pixbuf_set_rect_fill(pb,
832 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
839 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
840 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
842 buf = gdk_pixbuf_get_pixels(pb);
843 has_alpha = gdk_pixbuf_get_has_alpha(pb);
844 rowstride = gdk_pixbuf_get_rowstride(pb);
846 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
850 if (has_alpha) *p = a;
855 *-----------------------------------------------------------------------------
856 * pixbuf text rendering
857 *-----------------------------------------------------------------------------
860 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
861 GdkPixbuf *dest, gint dx, gint dy,
863 guint8 r, guint8 g, guint8 b, guint8 a)
883 if (!src || !dest) return;
885 sw = gdk_pixbuf_get_width(src);
886 sh = gdk_pixbuf_get_height(src);
888 if (sx < 0 || sx + w > sw) return;
889 if (sy < 0 || sy + h > sh) return;
891 dw = gdk_pixbuf_get_width(dest);
892 dh = gdk_pixbuf_get_height(dest);
894 if (dx < 0 || dx + w > dw) return;
895 if (dy < 0 || dy + h > dh) return;
897 s_alpha = gdk_pixbuf_get_has_alpha(src);
898 d_alpha = gdk_pixbuf_get_has_alpha(dest);
899 srs = gdk_pixbuf_get_rowstride(src);
900 drs = gdk_pixbuf_get_rowstride(dest);
901 s_pix = gdk_pixbuf_get_pixels(src);
902 d_pix = gdk_pixbuf_get_pixels(dest);
904 s_step = (s_alpha) ? 4 : 3;
905 d_step = (d_alpha) ? 4 : 3;
907 for (i = 0; i < h; i++)
909 sp = s_pix + (sy + i) * srs + sx * s_step;
910 dp = d_pix + (dy + i) * drs + dx * d_step;
911 for (j = 0; j < w; j++)
917 asub = a * sp[0] / 255;
918 *dp = (r * asub + *dp * (256-asub)) >> 8;
920 asub = a * sp[1] / 255;
921 *dp = (g * asub + *dp * (256-asub)) >> 8;
923 asub = a * sp[2] / 255;
924 *dp = (b * asub + *dp * (256-asub)) >> 8;
929 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
943 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *,
945 guint8 r, guint8 g, guint8 b, guint8 a)
954 cairo_surface_t *source;
957 pango_layout_get_pixel_size(layout, &w, &h);
958 if (w < 1 || h < 1) return;
960 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
962 cr = cairo_create (source);
963 cairo_set_source_rgb(cr, 0, 0, 0);
964 cairo_rectangle (cr, 0, 0, w, h);
966 cairo_set_source_rgb(cr, 1, 1, 1);
967 pango_cairo_show_layout (cr, layout);
970 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
972 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
974 cairo_image_surface_get_width (source),
975 cairo_image_surface_get_height (source),
976 cairo_image_surface_get_stride (source),
982 dw = gdk_pixbuf_get_width(pixbuf);
983 dh = gdk_pixbuf_get_height(pixbuf);
999 if (x + w > dw) w = dw - x;
1000 if (y + h > dh) h = dh - y;
1002 pixbuf_copy_font(buffer, sx, sy,
1006 g_object_unref(buffer);
1007 cairo_surface_destroy(source);
1011 *-----------------------------------------------------------------------------
1012 * pixbuf drawing (triangle)
1013 *-----------------------------------------------------------------------------
1016 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
1017 gint &rx, gint &ry, gint &rw, gint &rh)
1019 rx = std::min({x1, x2, x3});
1020 ry = std::min({y1, y2, y3});
1021 rw = std::max({abs(x1 - x2), abs(x2 - x3), abs(x3 - x1)});
1022 rh = std::max({abs(y1 - y2), abs(y2 - y3), abs(y3 - y1)});
1025 void pixbuf_draw_triangle(GdkPixbuf *pb,
1026 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1027 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
1028 guint8 r, guint8 g, guint8 b, guint8 a)
1056 gboolean middle = FALSE;
1060 pw = gdk_pixbuf_get_width(pb);
1061 ph = gdk_pixbuf_get_height(pb);
1063 // Intersects the clip region with the pixbuf. r{x,y,w,h} is that
1064 // intersecting region.
1065 if (!util_clip_region(0, 0, pw, ph,
1066 clip_x, clip_y, clip_w, clip_h,
1067 rx, ry, rw, rh)) return;
1069 // Determine the bounding box for the triangle.
1070 util_clip_triangle(x1, y1, x2, y2, x3, y3,
1073 // And now clip the triangle bounding box to the pixbuf clipping region.
1074 if (!util_clip_region(rx, ry, rw, rh,
1076 fx1, fy1, fw, fh)) return;
1080 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1081 prs = gdk_pixbuf_get_rowstride(pb);
1082 p_pix = gdk_pixbuf_get_pixels(pb);
1084 p_step = (has_alpha) ? 4 : 3;
1086 // Ensure that points are ordered by increasing y coordinate.
1103 // TODO(xsdg): Drop these explicit casts. Int will always promote to
1104 // double without issue.
1105 slope1 = static_cast<gdouble>(y2 - y1);
1106 if (slope1) slope1 = static_cast<gdouble>(x2 - x1) / slope1;
1109 slope2 = static_cast<gdouble>(y3 - y1);
1110 if (slope2) slope2 = static_cast<gdouble>(x3 - x1) / slope2;
1112 for (y = fy1; y < fy2; y++)
1117 if (!middle && y > y2)
1119 slope1 = static_cast<gdouble>(y3 - y2);
1120 if (slope1) slope1 = static_cast<gdouble>(x3 - x2) / slope1;
1127 xa = slope1_x + (slope1 * (y - slope1_y) + 0.5);
1128 xb = x1 + (slope2 * (y - y1) + 0.5);
1135 xa = CLAMP(xa, fx1, fx2);
1136 xb = CLAMP(xb, fx1, fx2);
1138 pp = p_pix + y * prs + xa * p_step;
1142 pp[0] = (r * a + pp[0] * (256-a)) >> 8;
1143 pp[1] = (g * a + pp[1] * (256-a)) >> 8;
1144 pp[2] = (b * a + pp[2] * (256-a)) >> 8;
1153 *-----------------------------------------------------------------------------
1154 * pixbuf drawing (line)
1155 *-----------------------------------------------------------------------------
1159 * @brief Clips the specified line segment to the specified clipping region.
1160 * @param[in] clip_x,clip_y Coordinates of the top-left corner of the clipping region.
1161 * @param[in] clip_w,clip_h Extent of the clipping region.
1162 * @param[in] x1,y1 Coordinates of the first point of the line segment.
1163 * @param[in] x2,y2 Coordinates of the second point of the line segment.
1164 * @param[out] rx1,ry1 Computed coordinates of the first point of the clipped line segment.
1165 * @param[out] rx2,ry2 Computed coordinates of the second point of the clipped line segment.
1166 * @retval FALSE The line segment lies outside of the clipping region.
1167 * @retval TRUE The clip operation was performed, and the output params were set.
1169 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1170 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1171 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1173 gboolean flip = FALSE;
1176 // Normalize: Line endpoint 1 must be farther left.
1184 // Ensure the line horizontally overlaps with the clip region.
1185 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1187 // Ensure the line vertically overlaps with the clip region.
1188 // Note that a line can both horizontally and vertically overlap with
1189 // clipping region, while still being outside of the clipping region. That
1190 // case is detected further below.
1193 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1197 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1201 // TODO(xsdg): Either use ints here, or define a reasonable epsilon to do the
1202 // right thing if -epsilon < d < 0. We already guaranteed above that x2 >= x1.
1207 slope = (y2 - y1) / d;
1208 // If needed, project (x1, y1) to be horizontally within the clip
1209 // region, while maintaining the line's slope and y-offset.
1212 y1 = y1 + slope * (clip_x - x1);
1215 // Likewise with (x2, y2).
1216 if (x2 > clip_x + clip_w)
1218 y2 = y2 + slope * (clip_x + clip_w - x2);
1219 x2 = clip_x + clip_w;
1223 // Check that any horizontal projections didn't cause the line segment to
1224 // no longer vertically overlap with the clip region.
1227 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1231 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1233 // Re-normalize: line endpoint 1 must be farther up.
1244 slope = (x2 - x1) / d;
1245 // If needed, project (x1, y1) to be vertically within the clip
1246 // region, while maintaining the line's slope and x-offset.
1249 x1 = x1 + slope * (clip_y - y1);
1252 // Likewise with (x2, y2).
1253 if (y2 > clip_y + clip_h)
1255 x2 = x2 + slope * (clip_y + clip_h - y2);
1256 y2 = clip_y + clip_h;
1260 // Set the output params, accounting for any flips that might have
1261 // happened during normalization.
1280 void pixbuf_draw_line(GdkPixbuf *pb,
1281 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1282 gint x1, gint y1, gint x2, gint y2,
1283 guint8 r, guint8 g, guint8 b, guint8 a)
1312 pw = gdk_pixbuf_get_width(pb);
1313 ph = gdk_pixbuf_get_height(pb);
1315 // Intersects the clip region with the pixbuf. r{x,y,w,h} is that
1316 // intersecting region.
1317 if (!util_clip_region(0, 0, pw, ph,
1318 clip_x, clip_y, clip_w, clip_h,
1319 rx, ry, rw, rh)) return;
1320 // TODO(xsdg): These explicit casts are unnecessary and harm readability.
1321 // Clips the specified line segment to the intersecting region from above.
1322 if (!util_clip_line(static_cast<gdouble>(rx), static_cast<gdouble>(ry), static_cast<gdouble>(rw), static_cast<gdouble>(rh),
1323 static_cast<gdouble>(x1), static_cast<gdouble>(y1), static_cast<gdouble>(x2), static_cast<gdouble>(y2),
1324 &rx1, &ry1, &rx2, &ry2)) return;
1331 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1332 prs = gdk_pixbuf_get_rowstride(pb);
1333 p_pix = gdk_pixbuf_get_pixels(pb);
1335 p_step = (has_alpha) ? 4 : 3;
1337 // We draw the clipped line segment along the longer axis first, and
1338 // allow the shorter axis to follow. This is because our raster line segment
1339 // will contain max(rx2-rx1, ry2-ry1) pixels, and the pixels along the
1340 // shorter axis may not advance for each cycle (the line is not anti-aliased).
1341 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1345 std::swap(rx1, rx2);
1346 std::swap(ry1, ry2);
1350 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1351 for (x = rx1; x < rx2; x += 1.0)
1353 px = static_cast<gint>(x + 0.5);
1354 py = static_cast<gint>(ry1 + (x - rx1) * slope + 0.5);
1356 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1358 pp = p_pix + py * prs + px * p_step;
1359 *pp = (r * a + *pp * (256-a)) >> 8;
1361 *pp = (g * a + *pp * (256-a)) >> 8;
1363 *pp = (b * a + *pp * (256-a)) >> 8;
1371 std::swap(rx1, rx2);
1372 std::swap(ry1, ry2);
1376 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1377 for (y = ry1; y < ry2; y += 1.0)
1379 px = static_cast<gint>(rx1 + (y - ry1) * slope + 0.5);
1380 py = static_cast<gint>(y + 0.5);
1382 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1384 pp = p_pix + py * prs + px * p_step;
1385 *pp = (r * a + *pp * (256-a)) >> 8;
1387 *pp = (g * a + *pp * (256-a)) >> 8;
1389 *pp = (b * a + *pp * (256-a)) >> 8;
1396 *-----------------------------------------------------------------------------
1397 * pixbuf drawing (fades and shadows)
1398 *-----------------------------------------------------------------------------
1402 * @brief Composites a horizontal or vertical linear gradient into the rectangular
1403 * region defined by corners `(x1, y1)` and `(x2, y2)`. Note that the
1404 * current implementation breaks if the max distance between `s` and
1405 * `x1/x2/y1/y2` is greater than `border`.
1406 * @param p_pix The pixel buffer to paint into.
1407 * @param prs The pixel row stride (how many pixels per row of the buffer).
1408 * @param has_alpha TRUE if the p_pix representation is rgba. FALSE if just rgb.
1409 * @param s The "center" of the gradient, along the axis defined by `vertical`.
1410 * Note that if the center is not along an edge, the gradient will be
1411 * symmetric about the center.
1412 * @param vertical When `TRUE`, the gradient color will vary vertically. When `FALSE`,
1414 * @param border The maximum extent of the gradient, in pixels.
1415 * @param x1,y1 Coordinates of the first corner of the region.
1416 * @param x2,y2 Coordinates of the second corner of the region.
1417 * @param r,g,b Base color of the gradient.
1418 * @param a The peak alpha value when compositing the gradient. The alpha varies
1419 * from this value down to 0 (fully transparent). Note that any alpha
1420 * value associated with the original pixel is unmodified.
1422 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1423 gint s, gboolean vertical, gint border,
1424 gint x1, gint y1, gint x2, gint y2,
1425 guint8 r, guint8 g, guint8 b, guint8 a)
1433 p_step = (has_alpha) ? 4 : 3;
1434 for (j = y1; j < y2; j++)
1436 pp = p_pix + j * prs + x1 * p_step;
1437 if (!vertical) n = a - a * abs(j - s) / border;
1438 for (i = x1; i < x2; i++)
1440 if (vertical) n = a - a * abs(i - s) / border;
1441 pp[0] = (r * n + pp[0] * (256-n)) >> 8;
1442 pp[1] = (g * n + pp[1] * (256-n)) >> 8;
1443 pp[2] = (b * n + pp[2] * (256-n)) >> 8;
1450 * @brief Composites a radial gradient into the rectangular region defined by
1451 * corners `(x1, y1)` and `(x2, y2)`.
1452 * @param p_pix The pixel buffer to paint into.
1453 * @param prs The pixel row stride (how many pixels per row of the buffer).
1454 * @param has_alpha TRUE if the p_pix representation is rgba. FALSE if just rgb.
1455 * @param sx,sy The coordinates of the center of the gradient.
1456 * @param border The max radius, in pixels, of the gradient. Pixels farther away
1457 * from the center than this will be unaffected.
1458 * @param x1,y1 Coordinates of the first corner of the region.
1459 * @param x2,y2 Coordinates of the second corner of the region.
1460 * @param r,g,b Base color of the gradient.
1461 * @param a The peak alpha value when compositing the gradient. The alpha varies
1462 * from this value down to 0 (fully transparent). Note that any alpha
1463 * value associated with the original pixel is unmodified.
1465 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1466 gint sx, gint sy, gint border,
1467 gint x1, gint y1, gint x2, gint y2,
1468 guint8, guint8 g, guint8 b, guint8 a)
1470 // TODO(xsdg): r (red) was shadowed by r (radius), and was removed from
1471 // the params list by an automated cleanup. Fix this and distinguish the
1472 // red param from the radius temporary variable.
1478 p_step = (has_alpha) ? 4 : 3;
1479 for (j = y1; j < y2; j++)
1481 pp = p_pix + j * prs + x1 * p_step;
1482 for (i = x1; i < x2; i++)
1487 r = MIN(border, (gint)hypot(i - sx, j - sy));
1488 n = a - a * r / border;
1489 pp[0] = (r * n + pp[0] * (256-n)) >> 8;
1490 pp[1] = (g * n + pp[1] * (256-n)) >> 8;
1491 pp[2] = (b * n + pp[2] * (256-n)) >> 8;
1497 void pixbuf_draw_shadow(GdkPixbuf *pb,
1498 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1499 gint x, gint y, gint w, gint h, gint border,
1500 guint8 r, guint8 g, guint8 b, guint8 a)
1518 pw = gdk_pixbuf_get_width(pb);
1519 ph = gdk_pixbuf_get_height(pb);
1521 // Intersects the clip region with the pixbuf. r{x,y,w,h} is that
1522 // intersecting region.
1523 if (!util_clip_region(0, 0, pw, ph,
1524 clip_x, clip_y, clip_w, clip_h,
1525 rx, ry, rw, rh)) return;
1527 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1528 prs = gdk_pixbuf_get_rowstride(pb);
1529 p_pix = gdk_pixbuf_get_pixels(pb);
1531 // Composites the specified color into the rectangle specified by x, y, w, h,
1532 // as contracted by `border` pixels, with a composition fraction that's defined
1533 // by the supplied `a` parameter.
1534 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1538 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1541 if (border < 1) return;
1543 // Draws linear gradients along each of the 4 edges.
1544 if (util_clip_region(x, y + border, border, h - border * 2,
1548 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1549 x + border, TRUE, border,
1550 fx, fy, fx + fw, fy + fh,
1553 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1557 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1558 x + w - border, TRUE, border,
1559 fx, fy, fx + fw, fy + fh,
1562 if (util_clip_region(x + border, y, w - border * 2, border,
1566 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1567 y + border, FALSE, border,
1568 fx, fy, fx + fw, fy + fh,
1571 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1575 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1576 y + h - border, FALSE, border,
1577 fx, fy, fx + fw, fy + fh,
1580 // Draws radial gradients at each of the 4 corners.
1581 if (util_clip_region(x, y, border, border,
1585 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1586 x + border, y + border, border,
1587 fx, fy, fx + fw, fy + fh,
1590 if (util_clip_region(x + w - border, y, border, border,
1594 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1595 x + w - border, y + border, border,
1596 fx, fy, fx + fw, fy + fh,
1599 if (util_clip_region(x, y + h - border, border, border,
1603 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1604 x + border, y + h - border, border,
1605 fx, fy, fx + fw, fy + fh,
1608 if (util_clip_region(x + w - border, y + h - border, border, border,
1612 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1613 x + w - border, y + h - border, border,
1614 fx, fy, fx + fw, fy + fh,
1621 *-----------------------------------------------------------------------------
1622 * pixbuf color alterations
1623 *-----------------------------------------------------------------------------
1626 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1627 gint x, gint y, gint w, gint h)
1640 pw = gdk_pixbuf_get_width(pb);
1641 ph = gdk_pixbuf_get_height(pb);
1643 if (x < 0 || x + w > pw) return;
1644 if (y < 0 || y + h > ph) return;
1646 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1647 prs = gdk_pixbuf_get_rowstride(pb);
1648 p_pix = gdk_pixbuf_get_pixels(pb);
1650 const gint p_step = has_alpha ? 4 : 3;
1652 for (i = 0; i < h; i++)
1654 pp = p_pix + (y + i) * prs + (x * p_step);
1655 for (j = 0; j < w; j++)
1659 grey = (pp[0] + pp[1] + pp[2]) / 3;
1669 *-----------------------------------------------------------------------------
1670 * pixbuf highlight under/over exposure
1671 *-----------------------------------------------------------------------------
1673 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1686 pw = gdk_pixbuf_get_width(pb);
1687 ph = gdk_pixbuf_get_height(pb);
1689 if (x < 0 || x + w > pw) return;
1690 if (y < 0 || y + h > ph) return;
1692 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1693 prs = gdk_pixbuf_get_rowstride(pb);
1694 p_pix = gdk_pixbuf_get_pixels(pb);
1696 const gint p_step = has_alpha ? 4 : 3;
1698 for (i = 0; i < h; i++)
1700 pp = p_pix + (y + i) * prs + (x * p_step);
1701 for (j = 0; j < w; j++)
1703 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1715 *-----------------------------------------------------------------------------
1716 * pixbuf ignore alpha
1717 *-----------------------------------------------------------------------------
1719 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1720 gint x, gint y, gint w, gint h)
1733 pw = gdk_pixbuf_get_width(pb);
1734 ph = gdk_pixbuf_get_height(pb);
1736 if (x < 0 || x + w > pw) return;
1737 if (y < 0 || y + h > ph) return;
1739 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1740 if (!has_alpha) return;
1742 prs = gdk_pixbuf_get_rowstride(pb);
1743 p_pix = gdk_pixbuf_get_pixels(pb);
1745 for (i = 0; i < h; i++)
1747 pp = p_pix + (y + i) * prs + (x * 4 );
1748 for (j = 0; j < w; j++)
1755 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */