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 if (clip_x + clip_w <= x ||
403 clip_y + clip_h <= y ||
409 *rx = MAX(x, clip_x);
410 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
412 *ry = MAX(y, clip_y);
413 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
419 *-----------------------------------------------------------------------------
421 *-----------------------------------------------------------------------------
424 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
425 guchar *dest, gint dest_row_stride, gint w, gint h,
426 gint bytes_per_pixel, gboolean counter_clockwise)
433 for (i = 0; i < h; i++)
435 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
436 for (j = 0; j < w; j++)
438 if (counter_clockwise)
440 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
444 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
446 *(dp++) = *(sp++); /* r */
447 *(dp++) = *(sp++); /* g */
448 *(dp++) = *(sp++); /* b */
449 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
455 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
456 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
462 for (i = 0; i < h; i++)
464 sp = src + (i * src_row_stride);
465 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
466 memcpy(dp, sp, w * bytes_per_pixel);
471 ROTATE_BUFFER_WIDTH = 48,
472 ROTATE_BUFFER_HEIGHT = 48
476 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
479 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
500 if (!src) return nullptr;
502 sw = gdk_pixbuf_get_width(src);
503 sh = gdk_pixbuf_get_height(src);
504 has_alpha = gdk_pixbuf_get_has_alpha(src);
505 srs = gdk_pixbuf_get_rowstride(src);
506 s_pix = gdk_pixbuf_get_pixels(src);
510 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
511 drs = gdk_pixbuf_get_rowstride(dest);
512 d_pix = gdk_pixbuf_get_pixels(dest);
514 a = (has_alpha ? 4 : 3);
516 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
517 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
518 b_pix = gdk_pixbuf_get_pixels(buffer);
519 brs = gdk_pixbuf_get_rowstride(buffer);
521 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
523 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
524 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
529 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
530 pixbuf_copy_block_rotate(s_pix, srs, j, i,
532 a, counter_clockwise);
534 if (counter_clockwise)
544 pixbuf_copy_block(b_pix, brs, w, h,
545 d_pix, drs, x, y, a);
549 g_object_unref(buffer);
552 /* this is the simple version of rotation (roughly 2-4x slower) */
554 for (i = 0; i < sh; i++)
556 sp = s_pix + (i * srs);
557 for (j = 0; j < sw; j++)
559 if (counter_clockwise)
561 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
565 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
568 *(dp++) = *(sp++); /* r */
569 *(dp++) = *(sp++); /* g */
570 *(dp++) = *(sp++); /* b */
571 if (has_alpha) *(dp) = *(sp++); /* a */
580 * Returns a copy of pixbuf mirrored and or flipped.
581 * TO do a 180 degree rotations set both mirror and flipped TRUE
582 * if mirror and flip are FALSE, result is a simple copy.
584 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
600 if (!src) return nullptr;
602 w = gdk_pixbuf_get_width(src);
603 h = gdk_pixbuf_get_height(src);
604 has_alpha = gdk_pixbuf_get_has_alpha(src);
605 srs = gdk_pixbuf_get_rowstride(src);
606 s_pix = gdk_pixbuf_get_pixels(src);
608 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
609 drs = gdk_pixbuf_get_rowstride(dest);
610 d_pix = gdk_pixbuf_get_pixels(dest);
612 a = has_alpha ? 4 : 3;
614 for (i = 0; i < h; i++)
616 sp = s_pix + (i * srs);
619 dp = d_pix + ((h - i - 1) * drs);
623 dp = d_pix + (i * drs);
628 for (j = 0; j < w; j++)
630 *(dp++) = *(sp++); /* r */
631 *(dp++) = *(sp++); /* g */
632 *(dp++) = *(sp++); /* b */
633 if (has_alpha) *(dp) = *(sp++); /* a */
639 for (j = 0; j < w; j++)
641 *(dp++) = *(sp++); /* r */
642 *(dp++) = *(sp++); /* g */
643 *(dp++) = *(sp++); /* b */
644 if (has_alpha) *(dp++) = *(sp++); /* a */
652 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
655 GdkPixbuf *tmp = nullptr;
659 case EXIF_ORIENTATION_TOP_LEFT:
660 dest = gdk_pixbuf_copy(pixbuf);
662 case EXIF_ORIENTATION_TOP_RIGHT:
664 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
666 case EXIF_ORIENTATION_BOTTOM_RIGHT:
668 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
670 case EXIF_ORIENTATION_BOTTOM_LEFT:
672 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
674 case EXIF_ORIENTATION_LEFT_TOP:
675 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
676 dest = pixbuf_copy_rotate_90(tmp, FALSE);
678 case EXIF_ORIENTATION_RIGHT_TOP:
679 /* rotated -90 (270) */
680 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
682 case EXIF_ORIENTATION_RIGHT_BOTTOM:
683 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
684 dest = pixbuf_copy_rotate_90(tmp, TRUE);
686 case EXIF_ORIENTATION_LEFT_BOTTOM:
688 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
691 dest = gdk_pixbuf_copy(pixbuf);
694 if (tmp) g_object_unref(tmp);
701 *-----------------------------------------------------------------------------
702 * pixbuf drawing (rectangles)
703 *-----------------------------------------------------------------------------
707 * Fills region of pixbuf at x,y over w,h
708 * with colors red (r), green (g), blue (b)
709 * applying alpha (a), use a=255 for solid.
711 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
712 gint x, gint y, gint w, gint h,
713 gint r, gint g, gint b, gint a)
726 pw = gdk_pixbuf_get_width(pb);
727 ph = gdk_pixbuf_get_height(pb);
729 if (x < 0 || x + w > pw) return;
730 if (y < 0 || y + h > ph) return;
732 has_alpha = gdk_pixbuf_get_has_alpha(pb);
733 prs = gdk_pixbuf_get_rowstride(pb);
734 p_pix = gdk_pixbuf_get_pixels(pb);
736 const gint p_step = has_alpha ? 4 : 3;
738 for (i = 0; i < h; i++)
740 pp = p_pix + (y + i) * prs + (x * p_step);
741 for (j = 0; j < w; j++)
743 pp[0] = (r * a + pp[0] * (256-a)) >> 8;
744 pp[1] = (g * a + pp[1] * (256-a)) >> 8;
745 pp[2] = (b * a + pp[2] * (256-a)) >> 8;
751 #pragma GCC diagnostic push
752 #pragma GCC diagnostic ignored "-Wunused-function"
753 void pixbuf_draw_rect_unused(GdkPixbuf *pb,
754 gint x, gint y, gint w, gint h,
755 gint r, gint g, gint b, gint a,
756 gint left, gint right, gint top, gint bottom)
758 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
760 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
762 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
764 pixbuf_draw_rect_fill(pb, x, y, left, h,
767 #pragma GCC diagnostic pop
769 void pixbuf_set_rect_fill(GdkPixbuf *pb,
770 gint x, gint y, gint w, gint h,
771 gint r, gint g, gint b, gint a)
784 pw = gdk_pixbuf_get_width(pb);
785 ph = gdk_pixbuf_get_height(pb);
787 if (x < 0 || x + w > pw) return;
788 if (y < 0 || y + h > ph) return;
790 has_alpha = gdk_pixbuf_get_has_alpha(pb);
791 prs = gdk_pixbuf_get_rowstride(pb);
792 p_pix = gdk_pixbuf_get_pixels(pb);
794 const gint p_step = has_alpha ? 4 : 3;
796 for (i = 0; i < h; i++)
798 pp = p_pix + (y + i) * prs + (x * p_step);
799 for (j = 0; j < w; j++)
804 if (has_alpha) { *pp = a; pp++; }
809 void pixbuf_set_rect(GdkPixbuf *pb,
810 gint x, gint y, gint w, gint h,
811 gint r, gint g, gint b, gint a,
812 gint left, gint right, gint top, gint bottom)
814 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
816 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
818 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
820 pixbuf_set_rect_fill(pb, x, y, left, h,
824 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
831 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
832 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
834 buf = gdk_pixbuf_get_pixels(pb);
835 has_alpha = gdk_pixbuf_get_has_alpha(pb);
836 rowstride = gdk_pixbuf_get_rowstride(pb);
838 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
842 if (has_alpha) *p = a;
847 *-----------------------------------------------------------------------------
848 * pixbuf text rendering
849 *-----------------------------------------------------------------------------
852 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
853 GdkPixbuf *dest, gint dx, gint dy,
855 guint8 r, guint8 g, guint8 b, guint8 a)
875 if (!src || !dest) return;
877 sw = gdk_pixbuf_get_width(src);
878 sh = gdk_pixbuf_get_height(src);
880 if (sx < 0 || sx + w > sw) return;
881 if (sy < 0 || sy + h > sh) return;
883 dw = gdk_pixbuf_get_width(dest);
884 dh = gdk_pixbuf_get_height(dest);
886 if (dx < 0 || dx + w > dw) return;
887 if (dy < 0 || dy + h > dh) return;
889 s_alpha = gdk_pixbuf_get_has_alpha(src);
890 d_alpha = gdk_pixbuf_get_has_alpha(dest);
891 srs = gdk_pixbuf_get_rowstride(src);
892 drs = gdk_pixbuf_get_rowstride(dest);
893 s_pix = gdk_pixbuf_get_pixels(src);
894 d_pix = gdk_pixbuf_get_pixels(dest);
896 s_step = (s_alpha) ? 4 : 3;
897 d_step = (d_alpha) ? 4 : 3;
899 for (i = 0; i < h; i++)
901 sp = s_pix + (sy + i) * srs + sx * s_step;
902 dp = d_pix + (dy + i) * drs + dx * d_step;
903 for (j = 0; j < w; j++)
909 asub = a * sp[0] / 255;
910 *dp = (r * asub + *dp * (256-asub)) >> 8;
912 asub = a * sp[1] / 255;
913 *dp = (g * asub + *dp * (256-asub)) >> 8;
915 asub = a * sp[2] / 255;
916 *dp = (b * asub + *dp * (256-asub)) >> 8;
921 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
935 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *,
937 guint8 r, guint8 g, guint8 b, guint8 a)
946 cairo_surface_t *source;
949 pango_layout_get_pixel_size(layout, &w, &h);
950 if (w < 1 || h < 1) return;
952 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
954 cr = cairo_create (source);
955 cairo_set_source_rgb(cr, 0, 0, 0);
956 cairo_rectangle (cr, 0, 0, w, h);
958 cairo_set_source_rgb(cr, 1, 1, 1);
959 pango_cairo_show_layout (cr, layout);
962 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
964 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
966 cairo_image_surface_get_width (source),
967 cairo_image_surface_get_height (source),
968 cairo_image_surface_get_stride (source),
974 dw = gdk_pixbuf_get_width(pixbuf);
975 dh = gdk_pixbuf_get_height(pixbuf);
991 if (x + w > dw) w = dw - x;
992 if (y + h > dh) h = dh - y;
994 pixbuf_copy_font(buffer, sx, sy,
998 g_object_unref(buffer);
999 cairo_surface_destroy(source);
1003 *-----------------------------------------------------------------------------
1004 * pixbuf drawing (triangle)
1005 *-----------------------------------------------------------------------------
1008 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
1009 gint &rx, gint &ry, gint &rw, gint &rh)
1011 rx = std::min({x1, x2, x3});
1012 ry = std::min({y1, y2, y3});
1013 rw = std::max({abs(x1 - x2), abs(x2 - x3), abs(x3 - x1)});
1014 rh = std::max({abs(y1 - y2), abs(y2 - y3), 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)
1048 gboolean middle = FALSE;
1052 pw = gdk_pixbuf_get_width(pb);
1053 ph = gdk_pixbuf_get_height(pb);
1055 if (!util_clip_region(0, 0, pw, ph,
1056 clip_x, clip_y, clip_w, clip_h,
1057 &rx, &ry, &rw, &rh)) return;
1059 util_clip_triangle(x1, y1, x2, y2, x3, y3,
1062 if (!util_clip_region(rx, ry, rw, rh,
1064 &fx1, &fy1, &fw, &fh)) return;
1068 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1069 prs = gdk_pixbuf_get_rowstride(pb);
1070 p_pix = gdk_pixbuf_get_pixels(pb);
1072 p_step = (has_alpha) ? 4 : 3;
1090 slope1 = static_cast<gdouble>(y2 - y1);
1091 if (slope1) slope1 = static_cast<gdouble>(x2 - x1) / slope1;
1094 slope2 = static_cast<gdouble>(y3 - y1);
1095 if (slope2) slope2 = static_cast<gdouble>(x3 - x1) / slope2;
1097 for (y = fy1; y < fy2; y++)
1102 if (!middle && y > y2)
1104 slope1 = static_cast<gdouble>(y3 - y2);
1105 if (slope1) slope1 = static_cast<gdouble>(x3 - x2) / slope1;
1112 xa = slope1_x + (slope1 * (y - slope1_y) + 0.5);
1113 xb = x1 + (slope2 * (y - y1) + 0.5);
1120 xa = CLAMP(xa, fx1, fx2);
1121 xb = CLAMP(xb, fx1, fx2);
1123 pp = p_pix + y * prs + xa * p_step;
1127 pp[0] = (r * a + pp[0] * (256-a)) >> 8;
1128 pp[1] = (g * a + pp[1] * (256-a)) >> 8;
1129 pp[2] = (b * a + pp[2] * (256-a)) >> 8;
1138 *-----------------------------------------------------------------------------
1139 * pixbuf drawing (line)
1140 *-----------------------------------------------------------------------------
1143 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1144 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1145 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1147 gboolean flip = FALSE;
1157 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1161 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1165 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1173 slope = (y2 - y1) / d;
1176 y1 = y1 + slope * (clip_x - x1);
1179 if (x2 > clip_x + clip_w)
1181 y2 = y2 + slope * (clip_x + clip_w - x2);
1182 x2 = clip_x + clip_w;
1188 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1192 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1204 slope = (x2 - x1) / d;
1207 x1 = x1 + slope * (clip_y - y1);
1210 if (y2 > clip_y + clip_h)
1212 x2 = x2 + slope * (clip_y + clip_h - y2);
1213 y2 = clip_y + clip_h;
1235 void pixbuf_draw_line(GdkPixbuf *pb,
1236 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1237 gint x1, gint y1, gint x2, gint y2,
1238 guint8 r, guint8 g, guint8 b, guint8 a)
1267 pw = gdk_pixbuf_get_width(pb);
1268 ph = gdk_pixbuf_get_height(pb);
1270 if (!util_clip_region(0, 0, pw, ph,
1271 clip_x, clip_y, clip_w, clip_h,
1272 &rx, &ry, &rw, &rh)) return;
1273 if (!util_clip_line(static_cast<gdouble>(rx), static_cast<gdouble>(ry), static_cast<gdouble>(rw), static_cast<gdouble>(rh),
1274 static_cast<gdouble>(x1), static_cast<gdouble>(y1), static_cast<gdouble>(x2), static_cast<gdouble>(y2),
1275 &rx1, &ry1, &rx2, &ry2)) return;
1282 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1283 prs = gdk_pixbuf_get_rowstride(pb);
1284 p_pix = gdk_pixbuf_get_pixels(pb);
1286 p_step = (has_alpha) ? 4 : 3;
1288 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1292 std::swap(rx1, rx2);
1293 std::swap(ry1, ry2);
1297 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1298 for (x = rx1; x < rx2; x += 1.0)
1300 px = static_cast<gint>(x + 0.5);
1301 py = static_cast<gint>(ry1 + (x - rx1) * slope + 0.5);
1303 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1305 pp = p_pix + py * prs + px * p_step;
1306 *pp = (r * a + *pp * (256-a)) >> 8;
1308 *pp = (g * a + *pp * (256-a)) >> 8;
1310 *pp = (b * a + *pp * (256-a)) >> 8;
1318 std::swap(rx1, rx2);
1319 std::swap(ry1, ry2);
1323 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1324 for (y = ry1; y < ry2; y += 1.0)
1326 px = static_cast<gint>(rx1 + (y - ry1) * slope + 0.5);
1327 py = static_cast<gint>(y + 0.5);
1329 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1331 pp = p_pix + py * prs + px * p_step;
1332 *pp = (r * a + *pp * (256-a)) >> 8;
1334 *pp = (g * a + *pp * (256-a)) >> 8;
1336 *pp = (b * a + *pp * (256-a)) >> 8;
1343 *-----------------------------------------------------------------------------
1344 * pixbuf drawing (fades and shadows)
1345 *-----------------------------------------------------------------------------
1348 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1349 gint s, gboolean vertical, gint border,
1350 gint x1, gint y1, gint x2, gint y2,
1351 guint8 r, guint8 g, guint8 b, guint8 a)
1359 p_step = (has_alpha) ? 4 : 3;
1360 for (j = y1; j < y2; j++)
1362 pp = p_pix + j * prs + x1 * p_step;
1363 if (!vertical) n = a - a * abs(j - s) / border;
1364 for (i = x1; i < x2; i++)
1366 if (vertical) n = a - a * abs(i - s) / border;
1367 pp[0] = (r * n + pp[0] * (256-n)) >> 8;
1368 pp[1] = (g * n + pp[1] * (256-n)) >> 8;
1369 pp[2] = (b * n + pp[2] * (256-n)) >> 8;
1375 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1376 gint sx, gint sy, gint border,
1377 gint x1, gint y1, gint x2, gint y2,
1378 guint8, guint8 g, guint8 b, guint8 a)
1385 p_step = (has_alpha) ? 4 : 3;
1386 for (j = y1; j < y2; j++)
1388 pp = p_pix + j * prs + x1 * p_step;
1389 for (i = x1; i < x2; i++)
1394 r = MIN(border, (gint)hypot(i - sx, j - sy));
1395 n = a - a * r / border;
1396 pp[0] = (r * n + pp[0] * (256-n)) >> 8;
1397 pp[1] = (g * n + pp[1] * (256-n)) >> 8;
1398 pp[2] = (b * n + pp[2] * (256-n)) >> 8;
1404 void pixbuf_draw_shadow(GdkPixbuf *pb,
1405 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1406 gint x, gint y, gint w, gint h, gint border,
1407 guint8 r, guint8 g, guint8 b, guint8 a)
1425 pw = gdk_pixbuf_get_width(pb);
1426 ph = gdk_pixbuf_get_height(pb);
1428 if (!util_clip_region(0, 0, pw, ph,
1429 clip_x, clip_y, clip_w, clip_h,
1430 &rx, &ry, &rw, &rh)) return;
1432 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1433 prs = gdk_pixbuf_get_rowstride(pb);
1434 p_pix = gdk_pixbuf_get_pixels(pb);
1436 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1438 &fx, &fy, &fw, &fh))
1440 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1443 if (border < 1) return;
1445 if (util_clip_region(x, y + border, border, h - border * 2,
1447 &fx, &fy, &fw, &fh))
1449 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1450 x + border, TRUE, border,
1451 fx, fy, fx + fw, fy + fh,
1454 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1456 &fx, &fy, &fw, &fh))
1458 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1459 x + w - border, TRUE, border,
1460 fx, fy, fx + fw, fy + fh,
1463 if (util_clip_region(x + border, y, w - border * 2, border,
1465 &fx, &fy, &fw, &fh))
1467 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1468 y + border, FALSE, border,
1469 fx, fy, fx + fw, fy + fh,
1472 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1474 &fx, &fy, &fw, &fh))
1476 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1477 y + h - border, FALSE, border,
1478 fx, fy, fx + fw, fy + fh,
1481 if (util_clip_region(x, y, border, border,
1483 &fx, &fy, &fw, &fh))
1485 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1486 x + border, y + border, border,
1487 fx, fy, fx + fw, fy + fh,
1490 if (util_clip_region(x + w - border, y, border, border,
1492 &fx, &fy, &fw, &fh))
1494 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1495 x + w - border, y + border, border,
1496 fx, fy, fx + fw, fy + fh,
1499 if (util_clip_region(x, y + h - border, border, border,
1501 &fx, &fy, &fw, &fh))
1503 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1504 x + border, y + h - border, border,
1505 fx, fy, fx + fw, fy + fh,
1508 if (util_clip_region(x + w - border, y + h - border, border, border,
1510 &fx, &fy, &fw, &fh))
1512 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1513 x + w - border, y + h - border, border,
1514 fx, fy, fx + fw, fy + fh,
1521 *-----------------------------------------------------------------------------
1522 * pixbuf color alterations
1523 *-----------------------------------------------------------------------------
1526 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1527 gint x, gint y, gint w, gint h)
1540 pw = gdk_pixbuf_get_width(pb);
1541 ph = gdk_pixbuf_get_height(pb);
1543 if (x < 0 || x + w > pw) return;
1544 if (y < 0 || y + h > ph) return;
1546 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1547 prs = gdk_pixbuf_get_rowstride(pb);
1548 p_pix = gdk_pixbuf_get_pixels(pb);
1550 const gint p_step = has_alpha ? 4 : 3;
1552 for (i = 0; i < h; i++)
1554 pp = p_pix + (y + i) * prs + (x * p_step);
1555 for (j = 0; j < w; j++)
1559 grey = (pp[0] + pp[1] + pp[2]) / 3;
1569 *-----------------------------------------------------------------------------
1570 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1572 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1585 pw = gdk_pixbuf_get_width(pb);
1586 ph = gdk_pixbuf_get_height(pb);
1588 if (x < 0 || x + w > pw) return;
1589 if (y < 0 || y + h > ph) return;
1591 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1592 prs = gdk_pixbuf_get_rowstride(pb);
1593 p_pix = gdk_pixbuf_get_pixels(pb);
1595 const gint p_step = has_alpha ? 4 : 3;
1597 for (i = 0; i < h; i++)
1599 pp = p_pix + (y + i) * prs + (x * p_step);
1600 for (j = 0; j < w; j++)
1602 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1614 *-----------------------------------------------------------------------------
1615 * pixbuf ignore alpha
1616 *-----------------------------------------------------------------------------
1618 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1619 gint x, gint y, gint w, gint h)
1632 pw = gdk_pixbuf_get_width(pb);
1633 ph = gdk_pixbuf_get_height(pb);
1635 if (x < 0 || x + w > pw) return;
1636 if (y < 0 || y + h > ph) return;
1638 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1639 if (!has_alpha) return;
1641 prs = gdk_pixbuf_get_rowstride(pb);
1642 p_pix = gdk_pixbuf_get_pixels(pb);
1644 for (i = 0; i < h; i++)
1646 pp = p_pix + (y + i) * prs + (x * 4 );
1647 for (j = 0; j < w; j++)
1654 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */