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"
31 #include <glib-object.h>
32 #include <pango/pangocairo.h>
39 #include "main-defines.h"
41 #include "ui-fileops.h"
45 *-----------------------------------------------------------------------------
47 *-----------------------------------------------------------------------------
50 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
52 GError *error = nullptr;
55 if (!pixbuf || !filename) return FALSE;
57 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
58 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
62 log_printf("Error saving png file: %s\n", error->message);
70 *-----------------------------------------------------------------------------
72 *-----------------------------------------------------------------------------
75 #pragma GCC diagnostic push
76 #pragma GCC diagnostic ignored "-Wunused-function"
77 gboolean pixbuf_to_file_as_jpg_unused(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
79 GError *error = nullptr;
83 if (!pixbuf || !filename) return FALSE;
85 if (quality == -1) quality = 75;
86 if (quality < 1 || quality > 100)
88 log_printf("Jpeg not saved, invalid quality %d\n", quality);
92 qbuf = g_strdup_printf("%d", quality);
93 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
98 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
104 #pragma GCC diagnostic pop
107 *-----------------------------------------------------------------------------
109 *-----------------------------------------------------------------------------
118 static PixbufInline inline_pixbuf_data[] = {
119 { PIXBUF_INLINE_ARCHIVE, "gq-icon-archive-file" },
120 { PIXBUF_INLINE_BROKEN, "gq-icon-broken" },
121 { PIXBUF_INLINE_COLLECTION, "gq-icon-collection" },
122 { PIXBUF_INLINE_ICON_180, "gq-icon-rotate-180" },
123 { PIXBUF_INLINE_ICON_BOOK, "gq-icon-book" },
124 { PIXBUF_INLINE_ICON_CONFIG, "gq-icon-config" },
125 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, "gq-icon-draw-rectangle" },
126 { PIXBUF_INLINE_ICON_EXIF, "gq-icon-exif" },
127 { PIXBUF_INLINE_ICON_EXPOSURE, "gq-icon-exposure" },
128 { PIXBUF_INLINE_ICON_FLOAT, "gq-icon-float" },
129 { PIXBUF_INLINE_ICON, "gqview-icon" },
130 { PIXBUF_INLINE_ICON_GRAYSCALE, "gq-icon-grayscale" },
131 { PIXBUF_INLINE_ICON_HEIF, "gq-icon-heic" },
132 { PIXBUF_INLINE_ICON_HIDETOOLS, "gq-icon-hidetools" },
133 { PIXBUF_INLINE_ICON_MAINTENANCE, "gq-icon-maintenance" },
134 { PIXBUF_INLINE_ICON_MARKS, "gq-icon-marks" },
135 { PIXBUF_INLINE_ICON_MOVE, "gq-icon-move" },
136 { PIXBUF_INLINE_ICON_ORIGINAL, "gq-icon-original" },
137 { PIXBUF_INLINE_ICON_PANORAMA, "gq-icon-panorama" },
138 { PIXBUF_INLINE_ICON_PDF, "gq-icon-pdf" },
139 { PIXBUF_INLINE_ICON_PROPERTIES, "gq-icon-properties" },
140 { PIXBUF_INLINE_ICON_RENAME, "gq-icon-rename" },
141 { PIXBUF_INLINE_ICON_SELECT_ALL, "gq-icon-select-all" },
142 { PIXBUF_INLINE_ICON_SELECT_INVERT, "gq-icon-select-invert" },
143 { PIXBUF_INLINE_ICON_SELECT_NONE, "gq-icon-select-none" },
144 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, "gq-icon-select-rectangle" },
145 { PIXBUF_INLINE_ICON_SORT, "gq-icon-sort" },
146 { PIXBUF_INLINE_ICON_THUMB, "gq-icon-thumb" },
147 { PIXBUF_INLINE_ICON_TOOLS, "gq-icon-tools" },
148 { PIXBUF_INLINE_ICON_VIEW, "gq-icon-view" },
149 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, "gq-icon-zoomfillhor" },
150 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, "gq-icon-zoomfillvert" },
151 { PIXBUF_INLINE_LOGO, "geeqie-logo" },
152 { PIXBUF_INLINE_METADATA, "gq-icon-metadata" },
153 { PIXBUF_INLINE_SCROLLER, "gq-scroller" },
154 { PIXBUF_INLINE_SPLIT_PANE_SYNC, "gq-icon-split-pane-sync" },
155 { PIXBUF_INLINE_UNKNOWN, "gq-icon-unknown" },
156 { PIXBUF_INLINE_VIDEO, "gq-icon-video" },
160 GdkPixbuf *pixbuf_inline(const gchar *key)
162 gboolean dark = FALSE;
163 gchar *file_name = nullptr;
166 GdkPixbuf *icon_pixbuf;
167 GError *error = nullptr;
168 GInputStream *in_stream;
170 GtkSettings *settings;
172 if (!key) return nullptr;
174 settings = gtk_settings_get_default();
175 g_object_get(settings, "gtk-theme-name", &theme_name, nullptr);
176 dark = g_str_has_suffix(theme_name, "dark");
180 while (inline_pixbuf_data[i].key)
182 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
184 file_name = g_strconcat(inline_pixbuf_data[i].data, dark ? "-dark" : "", ".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);
196 file_name = g_strconcat(inline_pixbuf_data[i].data, ".png", nullptr);
197 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
200 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
206 log_printf("warning: inline pixbuf error: %s", error->message);
208 g_object_unref(in_stream);
212 icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, nullptr, &error);
213 g_object_unref(in_stream);
217 log_printf("warning: inline pixbuf error: %s", error->message);
227 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
232 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
234 static GtkIconFactory *icon_factory = nullptr;
235 GtkIconSet *icon_set;
239 icon_factory = gtk_icon_factory_new();
240 gtk_icon_factory_add_default(icon_factory);
243 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
244 gtk_icon_factory_add(icon_factory, key, icon_set);
248 void pixbuf_inline_register_stock_icons()
253 while (inline_pixbuf_data[i].key)
255 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
260 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
262 GtkIconTheme *icon_theme;
264 GError *error = nullptr;
266 icon_theme = gtk_icon_theme_get_default();
268 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
270 pixbuf = gtk_icon_theme_load_icon(icon_theme,
271 icon, /* icon name */
273 static_cast<GtkIconLookupFlags>(0), /* flags */
279 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
284 if (strchr(icon, '.'))
286 /* try again without extension */
287 gchar *icon2 = remove_extension_from_path(icon);
288 pixbuf = gtk_icon_theme_load_icon(icon_theme,
289 icon2, /* icon name */
291 static_cast<GtkIconLookupFlags>(0), /* flags */
295 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
299 /* try as an absolute path */
300 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
303 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
311 if (!pixbuf) return FALSE;
313 register_stock_icon(key, pixbuf);
317 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
318 gint old_w, gint old_h,
319 gint *new_w, gint *new_h)
321 if ((static_cast<gdouble>(req_w) / old_w) < (static_cast<gdouble>(req_h) / old_h))
324 *new_h = static_cast<gdouble>(*new_w) / old_w * old_h;
325 if (*new_h < 1) *new_h = 1;
330 *new_w = static_cast<gdouble>(*new_h) / old_h * old_w;
331 if (*new_w < 1) *new_w = 1;
334 return (*new_w != old_w || *new_h != old_h);
337 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
341 switch (fd->format_class)
343 case FORMAT_CLASS_UNKNOWN:
344 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
346 case FORMAT_CLASS_META:
347 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
349 case FORMAT_CLASS_VIDEO:
350 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
352 case FORMAT_CLASS_COLLECTION:
353 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
355 case FORMAT_CLASS_DOCUMENT:
356 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
358 case FORMAT_CLASS_ARCHIVE:
359 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
362 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
365 if (requested_width && requested_height)
367 gint w = gdk_pixbuf_get_width(pixbuf);
368 gint h = gdk_pixbuf_get_height(pixbuf);
370 if (w > requested_width || h > requested_height)
375 if (pixbuf_scale_aspect(requested_width, requested_height,
381 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
382 g_object_unref(G_OBJECT(tmp));
391 *-----------------------------------------------------------------------------
393 *-----------------------------------------------------------------------------
396 gboolean util_clip_region(gint x, gint y, gint w, gint h,
397 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
398 gint *rx, gint *ry, gint *rw, gint *rh)
400 if (clip_x + clip_w <= x ||
402 clip_y + clip_h <= y ||
408 *rx = MAX(x, clip_x);
409 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
411 *ry = MAX(y, clip_y);
412 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
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 for (i = 0; i < h; i++)
737 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
738 for (j = 0; j < w; j++)
740 *pp = (r * a + *pp * (256-a)) >> 8;
742 *pp = (g * a + *pp * (256-a)) >> 8;
744 *pp = (b * a + *pp * (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 for (i = 0; i < h; i++)
796 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
797 for (j = 0; j < w; j++)
802 if (has_alpha) { *pp = a; pp++; }
807 void pixbuf_set_rect(GdkPixbuf *pb,
808 gint x, gint y, gint w, gint h,
809 gint r, gint g, gint b, gint a,
810 gint left, gint right, gint top, gint bottom)
812 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
814 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
816 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
818 pixbuf_set_rect_fill(pb, x, y, left, h,
822 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
829 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
830 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
832 buf = gdk_pixbuf_get_pixels(pb);
833 has_alpha = gdk_pixbuf_get_has_alpha(pb);
834 rowstride = gdk_pixbuf_get_rowstride(pb);
836 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
840 if (has_alpha) *p = a;
845 *-----------------------------------------------------------------------------
846 * pixbuf text rendering
847 *-----------------------------------------------------------------------------
850 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
851 GdkPixbuf *dest, gint dx, gint dy,
853 guint8 r, guint8 g, guint8 b, guint8 a)
873 if (!src || !dest) return;
875 sw = gdk_pixbuf_get_width(src);
876 sh = gdk_pixbuf_get_height(src);
878 if (sx < 0 || sx + w > sw) return;
879 if (sy < 0 || sy + h > sh) return;
881 dw = gdk_pixbuf_get_width(dest);
882 dh = gdk_pixbuf_get_height(dest);
884 if (dx < 0 || dx + w > dw) return;
885 if (dy < 0 || dy + h > dh) return;
887 s_alpha = gdk_pixbuf_get_has_alpha(src);
888 d_alpha = gdk_pixbuf_get_has_alpha(dest);
889 srs = gdk_pixbuf_get_rowstride(src);
890 drs = gdk_pixbuf_get_rowstride(dest);
891 s_pix = gdk_pixbuf_get_pixels(src);
892 d_pix = gdk_pixbuf_get_pixels(dest);
894 s_step = (s_alpha) ? 4 : 3;
895 d_step = (d_alpha) ? 4 : 3;
897 for (i = 0; i < h; i++)
899 sp = s_pix + (sy + i) * srs + sx * s_step;
900 dp = d_pix + (dy + i) * drs + dx * d_step;
901 for (j = 0; j < w; j++)
907 asub = a * sp[0] / 255;
908 *dp = (r * asub + *dp * (256-asub)) >> 8;
910 asub = a * sp[1] / 255;
911 *dp = (g * asub + *dp * (256-asub)) >> 8;
913 asub = a * sp[2] / 255;
914 *dp = (b * asub + *dp * (256-asub)) >> 8;
919 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
933 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *,
935 guint8 r, guint8 g, guint8 b, guint8 a)
944 cairo_surface_t *source;
947 pango_layout_get_pixel_size(layout, &w, &h);
948 if (w < 1 || h < 1) return;
950 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
952 cr = cairo_create (source);
953 cairo_set_source_rgb(cr, 0, 0, 0);
954 cairo_rectangle (cr, 0, 0, w, h);
956 cairo_set_source_rgb(cr, 1, 1, 1);
957 pango_cairo_show_layout (cr, layout);
960 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
962 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
964 cairo_image_surface_get_width (source),
965 cairo_image_surface_get_height (source),
966 cairo_image_surface_get_stride (source),
972 dw = gdk_pixbuf_get_width(pixbuf);
973 dh = gdk_pixbuf_get_height(pixbuf);
989 if (x + w > dw) w = dw - x;
990 if (y + h > dh) h = dh - y;
992 pixbuf_copy_font(buffer, sx, sy,
996 g_object_unref(buffer);
997 cairo_surface_destroy(source);
1001 *-----------------------------------------------------------------------------
1002 * pixbuf drawing (triangle)
1003 *-----------------------------------------------------------------------------
1006 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
1007 gint &rx, gint &ry, gint &rw, gint &rh)
1009 rx = std::min({x1, x2, x3});
1010 ry = std::min({y1, y2, y3});
1011 rw = std::max({abs(x1 - x2), abs(x2 - x3), abs(x3 - x1)});
1012 rh = std::max({abs(y1 - y2), abs(y2 - y3), abs(y3 - y1)});
1015 void pixbuf_draw_triangle(GdkPixbuf *pb,
1016 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1017 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
1018 guint8 r, guint8 g, guint8 b, guint8 a)
1047 gboolean middle = FALSE;
1051 pw = gdk_pixbuf_get_width(pb);
1052 ph = gdk_pixbuf_get_height(pb);
1054 if (!util_clip_region(0, 0, pw, ph,
1055 clip_x, clip_y, clip_w, clip_h,
1056 &rx, &ry, &rw, &rh)) return;
1058 util_clip_triangle(x1, y1, x2, y2, x3, y3,
1061 if (!util_clip_region(rx, ry, rw, rh,
1063 &fx1, &fy1, &fw, &fh)) return;
1067 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1068 prs = gdk_pixbuf_get_rowstride(pb);
1069 p_pix = gdk_pixbuf_get_pixels(pb);
1071 p_step = (has_alpha) ? 4 : 3;
1075 t = x1; x1 = x2; x2 = t;
1076 t = y1; y1 = y2; y2 = t;
1080 t = x2; x2 = x3; x3 = t;
1081 t = y2; y2 = y3; y3 = t;
1085 t = x1; x1 = x2; x2 = t;
1086 t = y1; y1 = y2; y2 = t;
1089 slope1 = static_cast<gdouble>(y2 - y1);
1090 if (slope1) slope1 = static_cast<gdouble>(x2 - x1) / slope1;
1093 slope2 = static_cast<gdouble>(y3 - y1);
1094 if (slope2) slope2 = static_cast<gdouble>(x3 - x1) / slope2;
1096 for (y = fy1; y < fy2; y++)
1101 if (!middle && y > y2)
1103 slope1 = static_cast<gdouble>(y3 - y2);
1104 if (slope1) slope1 = static_cast<gdouble>(x3 - x2) / slope1;
1111 xa = slope1_x + (slope1 * (y - slope1_y) + 0.5);
1112 xb = x1 + (slope2 * (y - y1) + 0.5);
1116 t = xa; xa = xb; xb = t;
1119 xa = CLAMP(xa, fx1, fx2);
1120 xb = CLAMP(xb, fx1, fx2);
1122 pp = p_pix + y * prs + xa * p_step;
1126 *pp = (r * a + *pp * (256-a)) >> 8;
1128 *pp = (g * a + *pp * (256-a)) >> 8;
1130 *pp = (b * a + *pp * (256-a)) >> 8;
1132 if (has_alpha) pp++;
1140 *-----------------------------------------------------------------------------
1141 * pixbuf drawing (line)
1142 *-----------------------------------------------------------------------------
1145 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1146 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1147 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1149 gboolean flip = FALSE;
1156 t = x1; x1 = x2; x2 = t;
1157 t = y1; y1 = y2; y2 = t;
1161 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1165 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1169 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1177 slope = (y2 - y1) / d;
1180 y1 = y1 + slope * (clip_x - x1);
1183 if (x2 > clip_x + clip_w)
1185 y2 = y2 + slope * (clip_x + clip_w - x2);
1186 x2 = clip_x + clip_w;
1192 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1198 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1200 t = x1; x1 = x2; x2 = t;
1201 t = y1; y1 = y2; y2 = t;
1210 slope = (x2 - x1) / d;
1213 x1 = x1 + slope * (clip_y - y1);
1216 if (y2 > clip_y + clip_h)
1218 x2 = x2 + slope * (clip_y + clip_h - y2);
1219 y2 = clip_y + clip_h;
1241 void pixbuf_draw_line(GdkPixbuf *pb,
1242 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1243 gint x1, gint y1, gint x2, gint y2,
1244 guint8 r, guint8 g, guint8 b, guint8 a)
1273 pw = gdk_pixbuf_get_width(pb);
1274 ph = gdk_pixbuf_get_height(pb);
1276 if (!util_clip_region(0, 0, pw, ph,
1277 clip_x, clip_y, clip_w, clip_h,
1278 &rx, &ry, &rw, &rh)) return;
1279 if (!util_clip_line(static_cast<gdouble>(rx), static_cast<gdouble>(ry), static_cast<gdouble>(rw), static_cast<gdouble>(rh),
1280 static_cast<gdouble>(x1), static_cast<gdouble>(y1), static_cast<gdouble>(x2), static_cast<gdouble>(y2),
1281 &rx1, &ry1, &rx2, &ry2)) return;
1288 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1289 prs = gdk_pixbuf_get_rowstride(pb);
1290 p_pix = gdk_pixbuf_get_pixels(pb);
1292 p_step = (has_alpha) ? 4 : 3;
1294 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1299 t = rx1; rx1 = rx2; rx2 = t;
1300 t = ry1; ry1 = ry2; ry2 = t;
1304 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1305 for (x = rx1; x < rx2; x += 1.0)
1307 px = static_cast<gint>(x + 0.5);
1308 py = static_cast<gint>(ry1 + (x - rx1) * slope + 0.5);
1310 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1312 pp = p_pix + py * prs + px * p_step;
1313 *pp = (r * a + *pp * (256-a)) >> 8;
1315 *pp = (g * a + *pp * (256-a)) >> 8;
1317 *pp = (b * a + *pp * (256-a)) >> 8;
1326 t = rx1; rx1 = rx2; rx2 = t;
1327 t = ry1; ry1 = ry2; ry2 = t;
1331 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1332 for (y = ry1; y < ry2; y += 1.0)
1334 px = static_cast<gint>(rx1 + (y - ry1) * slope + 0.5);
1335 py = static_cast<gint>(y + 0.5);
1337 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1339 pp = p_pix + py * prs + px * p_step;
1340 *pp = (r * a + *pp * (256-a)) >> 8;
1342 *pp = (g * a + *pp * (256-a)) >> 8;
1344 *pp = (b * a + *pp * (256-a)) >> 8;
1351 *-----------------------------------------------------------------------------
1352 * pixbuf drawing (fades and shadows)
1353 *-----------------------------------------------------------------------------
1356 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1357 gint s, gboolean vertical, gint border,
1358 gint x1, gint y1, gint x2, gint y2,
1359 guint8 r, guint8 g, guint8 b, guint8 a)
1367 p_step = (has_alpha) ? 4 : 3;
1368 for (j = y1; j < y2; j++)
1370 pp = p_pix + j * prs + x1 * p_step;
1371 if (!vertical) n = a - a * abs(j - s) / border;
1372 for (i = x1; i < x2; i++)
1374 if (vertical) n = a - a * abs(i - s) / border;
1375 *pp = (r * n + *pp * (256-n)) >> 8;
1377 *pp = (g * n + *pp * (256-n)) >> 8;
1379 *pp = (b * n + *pp * (256-n)) >> 8;
1381 if (has_alpha) pp++;
1386 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1387 gint sx, gint sy, gint border,
1388 gint x1, gint y1, gint x2, gint y2,
1389 guint8, guint8 g, guint8 b, guint8 a)
1396 p_step = (has_alpha) ? 4 : 3;
1397 for (j = y1; j < y2; j++)
1399 pp = p_pix + j * prs + x1 * p_step;
1400 for (i = x1; i < x2; i++)
1405 r = MIN(border, (gint)hypot(i - sx, j - sy));
1406 n = a - a * r / border;
1407 *pp = (r * n + *pp * (256-n)) >> 8;
1409 *pp = (g * n + *pp * (256-n)) >> 8;
1411 *pp = (b * n + *pp * (256-n)) >> 8;
1413 if (has_alpha) pp++;
1418 void pixbuf_draw_shadow(GdkPixbuf *pb,
1419 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1420 gint x, gint y, gint w, gint h, gint border,
1421 guint8 r, guint8 g, guint8 b, guint8 a)
1439 pw = gdk_pixbuf_get_width(pb);
1440 ph = gdk_pixbuf_get_height(pb);
1442 if (!util_clip_region(0, 0, pw, ph,
1443 clip_x, clip_y, clip_w, clip_h,
1444 &rx, &ry, &rw, &rh)) return;
1446 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1447 prs = gdk_pixbuf_get_rowstride(pb);
1448 p_pix = gdk_pixbuf_get_pixels(pb);
1450 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1452 &fx, &fy, &fw, &fh))
1454 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1457 if (border < 1) return;
1459 if (util_clip_region(x, y + border, border, h - border * 2,
1461 &fx, &fy, &fw, &fh))
1463 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1464 x + border, TRUE, border,
1465 fx, fy, fx + fw, fy + fh,
1468 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1470 &fx, &fy, &fw, &fh))
1472 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1473 x + w - border, TRUE, border,
1474 fx, fy, fx + fw, fy + fh,
1477 if (util_clip_region(x + border, y, w - border * 2, border,
1479 &fx, &fy, &fw, &fh))
1481 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1482 y + border, FALSE, border,
1483 fx, fy, fx + fw, fy + fh,
1486 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1488 &fx, &fy, &fw, &fh))
1490 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1491 y + h - border, FALSE, border,
1492 fx, fy, fx + fw, fy + fh,
1495 if (util_clip_region(x, y, border, border,
1497 &fx, &fy, &fw, &fh))
1499 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1500 x + border, y + border, border,
1501 fx, fy, fx + fw, fy + fh,
1504 if (util_clip_region(x + w - border, y, border, border,
1506 &fx, &fy, &fw, &fh))
1508 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1509 x + w - border, y + border, border,
1510 fx, fy, fx + fw, fy + fh,
1513 if (util_clip_region(x, y + h - border, border, border,
1515 &fx, &fy, &fw, &fh))
1517 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1518 x + border, y + h - border, border,
1519 fx, fy, fx + fw, fy + fh,
1522 if (util_clip_region(x + w - border, y + h - border, border, border,
1524 &fx, &fy, &fw, &fh))
1526 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1527 x + w - border, y + h - border, border,
1528 fx, fy, fx + fw, fy + fh,
1535 *-----------------------------------------------------------------------------
1536 * pixbuf color alterations
1537 *-----------------------------------------------------------------------------
1540 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1541 gint x, gint y, gint w, gint h)
1554 pw = gdk_pixbuf_get_width(pb);
1555 ph = gdk_pixbuf_get_height(pb);
1557 if (x < 0 || x + w > pw) return;
1558 if (y < 0 || y + h > ph) return;
1560 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1561 prs = gdk_pixbuf_get_rowstride(pb);
1562 p_pix = gdk_pixbuf_get_pixels(pb);
1564 for (i = 0; i < h; i++)
1566 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1567 for (j = 0; j < w; j++)
1571 grey = (pp[0] + pp[1] + pp[2]) / 3;
1578 if (has_alpha) pp++;
1584 *-----------------------------------------------------------------------------
1585 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1587 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1600 pw = gdk_pixbuf_get_width(pb);
1601 ph = gdk_pixbuf_get_height(pb);
1603 if (x < 0 || x + w > pw) return;
1604 if (y < 0 || y + h > ph) return;
1606 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1607 prs = gdk_pixbuf_get_rowstride(pb);
1608 p_pix = gdk_pixbuf_get_pixels(pb);
1610 for (i = 0; i < h; i++)
1612 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1613 for (j = 0; j < w; j++)
1615 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1623 if (has_alpha) pp++;
1628 if (has_alpha) pp++;
1635 *-----------------------------------------------------------------------------
1636 * pixbuf ignore alpha
1637 *-----------------------------------------------------------------------------
1639 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1640 gint x, gint y, gint w, gint h)
1653 pw = gdk_pixbuf_get_width(pb);
1654 ph = gdk_pixbuf_get_height(pb);
1656 if (x < 0 || x + w > pw) return;
1657 if (y < 0 || y + h > ph) return;
1659 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1660 if (!has_alpha) return;
1662 prs = gdk_pixbuf_get_rowstride(pb);
1663 p_pix = gdk_pixbuf_get_pixels(pb);
1665 for (i = 0; i < h; i++)
1667 pp = p_pix + (y + i) * prs + (x * 4 );
1668 for (j = 0; j < w; j++)
1675 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */