2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "pixbuf-util.h"
27 #include "ui-fileops.h"
33 *-----------------------------------------------------------------------------
35 *-----------------------------------------------------------------------------
38 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
40 GError *error = nullptr;
43 if (!pixbuf || !filename) return FALSE;
45 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
46 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
50 log_printf("Error saving png file: %s\n", error->message);
58 *-----------------------------------------------------------------------------
60 *-----------------------------------------------------------------------------
63 #pragma GCC diagnostic push
64 #pragma GCC diagnostic ignored "-Wunused-function"
65 gboolean pixbuf_to_file_as_jpg_unused(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
71 if (!pixbuf || !filename) return FALSE;
73 if (quality == -1) quality = 75;
74 if (quality < 1 || quality > 100)
76 log_printf("Jpeg not saved, invalid quality %d\n", quality);
80 qbuf = g_strdup_printf("%d", quality);
81 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
86 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
92 #pragma GCC diagnostic pop
95 *-----------------------------------------------------------------------------
97 *-----------------------------------------------------------------------------
106 static PixbufInline inline_pixbuf_data[] = {
107 { PIXBUF_INLINE_ARCHIVE, "gq-icon-archive-file" },
108 { PIXBUF_INLINE_BROKEN, "gq-icon-broken" },
109 { PIXBUF_INLINE_COLLECTION, "gq-icon-collection" },
110 { PIXBUF_INLINE_ICON_180, "gq-icon-rotate-180" },
111 { PIXBUF_INLINE_ICON_BOOK, "gq-icon-book" },
112 { PIXBUF_INLINE_ICON_CONFIG, "gq-icon-config" },
113 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, "gq-icon-draw-rectangle" },
114 { PIXBUF_INLINE_ICON_EXIF, "gq-icon-exif" },
115 { PIXBUF_INLINE_ICON_EXPOSURE, "gq-icon-exposure" },
116 { PIXBUF_INLINE_ICON_FLOAT, "gq-icon-float" },
117 { PIXBUF_INLINE_ICON, "gqview-icon" },
118 { PIXBUF_INLINE_ICON_GRAYSCALE, "gq-icon-grayscale" },
119 { PIXBUF_INLINE_ICON_HEIF, "gq-icon-heic" },
120 { PIXBUF_INLINE_ICON_HIDETOOLS, "gq-icon-hidetools" },
121 { PIXBUF_INLINE_ICON_MAINTENANCE, "gq-icon-maintenance" },
122 { PIXBUF_INLINE_ICON_MARKS, "gq-icon-marks" },
123 { PIXBUF_INLINE_ICON_MOVE, "gq-icon-move" },
124 { PIXBUF_INLINE_ICON_ORIGINAL, "gq-icon-original" },
125 { PIXBUF_INLINE_ICON_PANORAMA, "gq-icon-panorama" },
126 { PIXBUF_INLINE_ICON_PDF, "gq-icon-pdf" },
127 { PIXBUF_INLINE_ICON_PROPERTIES, "gq-icon-properties" },
128 { PIXBUF_INLINE_ICON_RENAME, "gq-icon-rename" },
129 { PIXBUF_INLINE_ICON_SELECT_ALL, "gq-icon-select-all" },
130 { PIXBUF_INLINE_ICON_SELECT_INVERT, "gq-icon-select-invert" },
131 { PIXBUF_INLINE_ICON_SELECT_NONE, "gq-icon-select-none" },
132 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, "gq-icon-select-rectangle" },
133 { PIXBUF_INLINE_ICON_SORT, "gq-icon-sort" },
134 { PIXBUF_INLINE_ICON_THUMB, "gq-icon-thumb" },
135 { PIXBUF_INLINE_ICON_TOOLS, "gq-icon-tools" },
136 { PIXBUF_INLINE_ICON_VIEW, "gq-icon-view" },
137 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, "gq-icon-zoomfillhor" },
138 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, "gq-icon-zoomfillvert" },
139 { PIXBUF_INLINE_LOGO, "geeqie-logo" },
140 { PIXBUF_INLINE_METADATA, "gq-icon-metadata" },
141 { PIXBUF_INLINE_SCROLLER, "gq-scroller" },
142 { PIXBUF_INLINE_SPLIT_PANE_SYNC, "gq-icon-split-pane-sync" },
143 { PIXBUF_INLINE_UNKNOWN, "gq-icon-unknown" },
144 { PIXBUF_INLINE_VIDEO, "gq-icon-video" },
148 GdkPixbuf *pixbuf_inline(const gchar *key)
150 gboolean dark = FALSE;
151 gchar *file_name = nullptr;
154 GdkPixbuf *icon_pixbuf;
155 GError *error = nullptr;
156 GInputStream *in_stream;
158 GtkSettings *settings;
160 if (!key) return nullptr;
162 settings = gtk_settings_get_default();
163 g_object_get(settings, "gtk-theme-name", &theme_name, nullptr);
164 dark = g_str_has_suffix(theme_name, "dark");
168 while (inline_pixbuf_data[i].key)
170 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
172 file_name = g_strconcat(inline_pixbuf_data[i].data, dark ? "-dark" : "", ".png", nullptr);
173 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
176 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
184 file_name = g_strconcat(inline_pixbuf_data[i].data, ".png", nullptr);
185 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
188 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
194 log_printf("warning: inline pixbuf error: %s", error->message);
196 g_object_unref(in_stream);
200 icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, nullptr, &error);
201 g_object_unref(in_stream);
205 log_printf("warning: inline pixbuf error: %s", error->message);
215 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
220 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
222 static GtkIconFactory *icon_factory = nullptr;
223 GtkIconSet *icon_set;
227 icon_factory = gtk_icon_factory_new();
228 gtk_icon_factory_add_default(icon_factory);
231 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
232 gtk_icon_factory_add(icon_factory, key, icon_set);
236 void pixbuf_inline_register_stock_icons()
241 while (inline_pixbuf_data[i].key)
243 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
248 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
250 GtkIconTheme *icon_theme;
252 GError *error = nullptr;
254 icon_theme = gtk_icon_theme_get_default();
256 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
258 pixbuf = gtk_icon_theme_load_icon(icon_theme,
259 icon, /* icon name */
261 static_cast<GtkIconLookupFlags>(0), /* flags */
267 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
272 if (strchr(icon, '.'))
274 /* try again without extension */
275 gchar *icon2 = remove_extension_from_path(icon);
276 pixbuf = gtk_icon_theme_load_icon(icon_theme,
277 icon2, /* icon name */
279 static_cast<GtkIconLookupFlags>(0), /* flags */
283 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
287 /* try as an absolute path */
288 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
291 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
299 if (!pixbuf) return FALSE;
301 register_stock_icon(key, pixbuf);
305 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
306 gint old_w, gint old_h,
307 gint *new_w, gint *new_h)
309 if ((static_cast<gdouble>(req_w) / old_w) < (static_cast<gdouble>(req_h) / old_h))
312 *new_h = static_cast<gdouble>(*new_w) / old_w * old_h;
313 if (*new_h < 1) *new_h = 1;
318 *new_w = static_cast<gdouble>(*new_h) / old_h * old_w;
319 if (*new_w < 1) *new_w = 1;
322 return (*new_w != old_w || *new_h != old_h);
325 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
329 switch (fd->format_class)
331 case FORMAT_CLASS_UNKNOWN:
332 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
334 case FORMAT_CLASS_META:
335 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
337 case FORMAT_CLASS_VIDEO:
338 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
340 case FORMAT_CLASS_COLLECTION:
341 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
343 case FORMAT_CLASS_DOCUMENT:
344 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
346 case FORMAT_CLASS_ARCHIVE:
347 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
350 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
353 if (requested_width && requested_height)
355 gint w = gdk_pixbuf_get_width(pixbuf);
356 gint h = gdk_pixbuf_get_height(pixbuf);
358 if (w > requested_width || h > requested_height)
362 if (pixbuf_scale_aspect(requested_width, requested_height,
368 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
369 g_object_unref(G_OBJECT(tmp));
378 *-----------------------------------------------------------------------------
380 *-----------------------------------------------------------------------------
383 gboolean util_clip_region(gint x, gint y, gint w, gint h,
384 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
385 gint *rx, gint *ry, gint *rw, gint *rh)
387 if (clip_x + clip_w <= x ||
389 clip_y + clip_h <= y ||
395 *rx = MAX(x, clip_x);
396 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
398 *ry = MAX(y, clip_y);
399 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
405 *-----------------------------------------------------------------------------
407 *-----------------------------------------------------------------------------
410 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
411 guchar *dest, gint dest_row_stride, gint w, gint h,
412 gint bytes_per_pixel, gboolean counter_clockwise)
418 for (i = 0; i < h; i++)
420 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
421 for (j = 0; j < w; j++)
423 if (counter_clockwise)
425 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
429 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
431 *(dp++) = *(sp++); /* r */
432 *(dp++) = *(sp++); /* g */
433 *(dp++) = *(sp++); /* b */
434 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
440 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
441 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
447 for (i = 0; i < h; i++)
449 sp = src + (i * src_row_stride);
450 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
451 memcpy(dp, sp, w * bytes_per_pixel);
455 #define ROTATE_BUFFER_WIDTH 48
456 #define ROTATE_BUFFER_HEIGHT 48
459 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
462 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
477 if (!src) return nullptr;
479 sw = gdk_pixbuf_get_width(src);
480 sh = gdk_pixbuf_get_height(src);
481 has_alpha = gdk_pixbuf_get_has_alpha(src);
482 srs = gdk_pixbuf_get_rowstride(src);
483 s_pix = gdk_pixbuf_get_pixels(src);
487 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
488 drs = gdk_pixbuf_get_rowstride(dest);
489 d_pix = gdk_pixbuf_get_pixels(dest);
491 a = (has_alpha ? 4 : 3);
493 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
494 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
495 b_pix = gdk_pixbuf_get_pixels(buffer);
496 brs = gdk_pixbuf_get_rowstride(buffer);
498 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
500 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
501 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
505 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
506 pixbuf_copy_block_rotate(s_pix, srs, j, i,
508 a, counter_clockwise);
510 if (counter_clockwise)
520 pixbuf_copy_block(b_pix, brs, w, h,
521 d_pix, drs, x, y, a);
525 g_object_unref(buffer);
528 /* this is the simple version of rotation (roughly 2-4x slower) */
530 for (i = 0; i < sh; i++)
532 sp = s_pix + (i * srs);
533 for (j = 0; j < sw; j++)
535 if (counter_clockwise)
537 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
541 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
544 *(dp++) = *(sp++); /* r */
545 *(dp++) = *(sp++); /* g */
546 *(dp++) = *(sp++); /* b */
547 if (has_alpha) *(dp) = *(sp++); /* a */
556 * Returns a copy of pixbuf mirrored and or flipped.
557 * TO do a 180 degree rotations set both mirror and flipped TRUE
558 * if mirror and flip are FALSE, result is a simple copy.
560 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
573 if (!src) return nullptr;
575 w = gdk_pixbuf_get_width(src);
576 h = gdk_pixbuf_get_height(src);
577 has_alpha = gdk_pixbuf_get_has_alpha(src);
578 srs = gdk_pixbuf_get_rowstride(src);
579 s_pix = gdk_pixbuf_get_pixels(src);
581 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
582 drs = gdk_pixbuf_get_rowstride(dest);
583 d_pix = gdk_pixbuf_get_pixels(dest);
585 a = has_alpha ? 4 : 3;
587 for (i = 0; i < h; i++)
589 sp = s_pix + (i * srs);
592 dp = d_pix + ((h - i - 1) * drs);
596 dp = d_pix + (i * drs);
601 for (j = 0; j < w; j++)
603 *(dp++) = *(sp++); /* r */
604 *(dp++) = *(sp++); /* g */
605 *(dp++) = *(sp++); /* b */
606 if (has_alpha) *(dp) = *(sp++); /* a */
612 for (j = 0; j < w; j++)
614 *(dp++) = *(sp++); /* r */
615 *(dp++) = *(sp++); /* g */
616 *(dp++) = *(sp++); /* b */
617 if (has_alpha) *(dp++) = *(sp++); /* a */
625 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
628 GdkPixbuf *tmp = nullptr;
632 case EXIF_ORIENTATION_TOP_LEFT:
633 dest = gdk_pixbuf_copy(pixbuf);
635 case EXIF_ORIENTATION_TOP_RIGHT:
637 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
639 case EXIF_ORIENTATION_BOTTOM_RIGHT:
641 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
643 case EXIF_ORIENTATION_BOTTOM_LEFT:
645 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
647 case EXIF_ORIENTATION_LEFT_TOP:
648 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
649 dest = pixbuf_copy_rotate_90(tmp, FALSE);
651 case EXIF_ORIENTATION_RIGHT_TOP:
652 /* rotated -90 (270) */
653 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
655 case EXIF_ORIENTATION_RIGHT_BOTTOM:
656 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
657 dest = pixbuf_copy_rotate_90(tmp, TRUE);
659 case EXIF_ORIENTATION_LEFT_BOTTOM:
661 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
664 dest = gdk_pixbuf_copy(pixbuf);
667 if (tmp) g_object_unref(tmp);
674 *-----------------------------------------------------------------------------
675 * pixbuf drawing (rectangles)
676 *-----------------------------------------------------------------------------
680 * Fills region of pixbuf at x,y over w,h
681 * with colors red (r), green (g), blue (b)
682 * applying alpha (a), use a=255 for solid.
684 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
685 gint x, gint y, gint w, gint h,
686 gint r, gint g, gint b, gint a)
696 pw = gdk_pixbuf_get_width(pb);
697 ph = gdk_pixbuf_get_height(pb);
699 if (x < 0 || x + w > pw) return;
700 if (y < 0 || y + h > ph) return;
702 has_alpha = gdk_pixbuf_get_has_alpha(pb);
703 prs = gdk_pixbuf_get_rowstride(pb);
704 p_pix = gdk_pixbuf_get_pixels(pb);
706 for (i = 0; i < h; i++)
708 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
709 for (j = 0; j < w; j++)
711 *pp = (r * a + *pp * (256-a)) >> 8;
713 *pp = (g * a + *pp * (256-a)) >> 8;
715 *pp = (b * a + *pp * (256-a)) >> 8;
722 #pragma GCC diagnostic push
723 #pragma GCC diagnostic ignored "-Wunused-function"
724 void pixbuf_draw_rect_unused(GdkPixbuf *pb,
725 gint x, gint y, gint w, gint h,
726 gint r, gint g, gint b, gint a,
727 gint left, gint right, gint top, gint bottom)
729 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
731 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
733 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
735 pixbuf_draw_rect_fill(pb, x, y, left, h,
738 #pragma GCC diagnostic pop
740 void pixbuf_set_rect_fill(GdkPixbuf *pb,
741 gint x, gint y, gint w, gint h,
742 gint r, gint g, gint b, gint a)
752 pw = gdk_pixbuf_get_width(pb);
753 ph = gdk_pixbuf_get_height(pb);
755 if (x < 0 || x + w > pw) return;
756 if (y < 0 || y + h > ph) return;
758 has_alpha = gdk_pixbuf_get_has_alpha(pb);
759 prs = gdk_pixbuf_get_rowstride(pb);
760 p_pix = gdk_pixbuf_get_pixels(pb);
762 for (i = 0; i < h; i++)
764 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
765 for (j = 0; j < w; j++)
770 if (has_alpha) { *pp = a; pp++; }
775 void pixbuf_set_rect(GdkPixbuf *pb,
776 gint x, gint y, gint w, gint h,
777 gint r, gint g, gint b, gint a,
778 gint left, gint right, gint top, gint bottom)
780 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
782 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
784 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
786 pixbuf_set_rect_fill(pb, x, y, left, h,
790 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
797 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
798 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
800 buf = gdk_pixbuf_get_pixels(pb);
801 has_alpha = gdk_pixbuf_get_has_alpha(pb);
802 rowstride = gdk_pixbuf_get_rowstride(pb);
804 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
808 if (has_alpha) *p = a;
813 *-----------------------------------------------------------------------------
814 * pixbuf text rendering
815 *-----------------------------------------------------------------------------
818 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
819 GdkPixbuf *dest, gint dx, gint dy,
821 guint8 r, guint8 g, guint8 b, guint8 a)
836 if (!src || !dest) return;
838 sw = gdk_pixbuf_get_width(src);
839 sh = gdk_pixbuf_get_height(src);
841 if (sx < 0 || sx + w > sw) return;
842 if (sy < 0 || sy + h > sh) return;
844 dw = gdk_pixbuf_get_width(dest);
845 dh = gdk_pixbuf_get_height(dest);
847 if (dx < 0 || dx + w > dw) return;
848 if (dy < 0 || dy + h > dh) return;
850 s_alpha = gdk_pixbuf_get_has_alpha(src);
851 d_alpha = gdk_pixbuf_get_has_alpha(dest);
852 srs = gdk_pixbuf_get_rowstride(src);
853 drs = gdk_pixbuf_get_rowstride(dest);
854 s_pix = gdk_pixbuf_get_pixels(src);
855 d_pix = gdk_pixbuf_get_pixels(dest);
857 s_step = (s_alpha) ? 4 : 3;
858 d_step = (d_alpha) ? 4 : 3;
860 for (i = 0; i < h; i++)
862 sp = s_pix + (sy + i) * srs + sx * s_step;
863 dp = d_pix + (dy + i) * drs + dx * d_step;
864 for (j = 0; j < w; j++)
870 asub = a * sp[0] / 255;
871 *dp = (r * asub + *dp * (256-asub)) >> 8;
873 asub = a * sp[1] / 255;
874 *dp = (g * asub + *dp * (256-asub)) >> 8;
876 asub = a * sp[2] / 255;
877 *dp = (b * asub + *dp * (256-asub)) >> 8;
882 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
896 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *,
898 guint8 r, guint8 g, guint8 b, guint8 a)
904 cairo_surface_t *source;
907 pango_layout_get_pixel_size(layout, &w, &h);
908 if (w < 1 || h < 1) return;
910 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
912 cr = cairo_create (source);
913 cairo_set_source_rgb(cr, 0, 0, 0);
914 cairo_rectangle (cr, 0, 0, w, h);
916 cairo_set_source_rgb(cr, 1, 1, 1);
917 pango_cairo_show_layout (cr, layout);
920 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
922 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
924 cairo_image_surface_get_width (source),
925 cairo_image_surface_get_height (source),
926 cairo_image_surface_get_stride (source),
932 dw = gdk_pixbuf_get_width(pixbuf);
933 dh = gdk_pixbuf_get_height(pixbuf);
949 if (x + w > dw) w = dw - x;
950 if (y + h > dh) h = dh - y;
952 pixbuf_copy_font(buffer, sx, sy,
956 g_object_unref(buffer);
957 cairo_surface_destroy(source);
961 *-----------------------------------------------------------------------------
962 * pixbuf drawing (triangle)
963 *-----------------------------------------------------------------------------
966 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
967 gint *rx, gint *ry, gint *rw, gint *rh)
975 tw = MAX(abs(x1 - x2), abs(x2 - x3));
976 tw = MAX(tw, abs(x3 - x1));
977 th = MAX(abs(y1 - y2), abs(y2 - y3));
978 th = MAX(th, abs(y3 - y1));
986 void pixbuf_draw_triangle(GdkPixbuf *pb,
987 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
988 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
989 guint8 r, guint8 g, guint8 b, guint8 a)
1001 gdouble slope1, slope2;
1002 gint slope1_x, slope1_y;
1005 gboolean middle = FALSE;
1009 pw = gdk_pixbuf_get_width(pb);
1010 ph = gdk_pixbuf_get_height(pb);
1012 if (!util_clip_region(0, 0, pw, ph,
1013 clip_x, clip_y, clip_w, clip_h,
1014 &rx, &ry, &rw, &rh)) return;
1016 util_clip_triangle(x1, y1, x2, y2, x3, y3,
1017 &tx, &ty, &tw, &th);
1019 if (!util_clip_region(rx, ry, rw, rh,
1021 &fx1, &fy1, &fw, &fh)) return;
1025 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1026 prs = gdk_pixbuf_get_rowstride(pb);
1027 p_pix = gdk_pixbuf_get_pixels(pb);
1029 p_step = (has_alpha) ? 4 : 3;
1033 t = x1; x1 = x2; x2 = t;
1034 t = y1; y1 = y2; y2 = t;
1038 t = x2; x2 = x3; x3 = t;
1039 t = y2; y2 = y3; y3 = t;
1043 t = x1; x1 = x2; x2 = t;
1044 t = y1; y1 = y2; y2 = t;
1047 slope1 = static_cast<gdouble>(y2 - y1);
1048 if (slope1) slope1 = static_cast<gdouble>(x2 - x1) / slope1;
1051 slope2 = static_cast<gdouble>(y3 - y1);
1052 if (slope2) slope2 = static_cast<gdouble>(x3 - x1) / slope2;
1054 for (y = fy1; y < fy2; y++)
1058 if (!middle && y > y2)
1060 slope1 = static_cast<gdouble>(y3 - y2);
1061 if (slope1) slope1 = static_cast<gdouble>(x3 - x2) / slope1;
1068 xa = slope1_x + (slope1 * (y - slope1_y) + 0.5);
1069 xb = x1 + (slope2 * (y - y1) + 0.5);
1073 t = xa; xa = xb; xb = t;
1076 xa = CLAMP(xa, fx1, fx2);
1077 xb = CLAMP(xb, fx1, fx2);
1079 pp = p_pix + y * prs + xa * p_step;
1083 *pp = (r * a + *pp * (256-a)) >> 8;
1085 *pp = (g * a + *pp * (256-a)) >> 8;
1087 *pp = (b * a + *pp * (256-a)) >> 8;
1089 if (has_alpha) pp++;
1097 *-----------------------------------------------------------------------------
1098 * pixbuf drawing (line)
1099 *-----------------------------------------------------------------------------
1102 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1103 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1104 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1106 gboolean flip = FALSE;
1113 t = x1; x1 = x2; x2 = t;
1114 t = y1; y1 = y2; y2 = t;
1118 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1122 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1126 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1134 slope = (y2 - y1) / d;
1137 y1 = y1 + slope * (clip_x - x1);
1140 if (x2 > clip_x + clip_w)
1142 y2 = y2 + slope * (clip_x + clip_w - x2);
1143 x2 = clip_x + clip_w;
1149 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1155 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1157 t = x1; x1 = x2; x2 = t;
1158 t = y1; y1 = y2; y2 = t;
1167 slope = (x2 - x1) / d;
1170 x1 = x1 + slope * (clip_y - y1);
1173 if (y2 > clip_y + clip_h)
1175 x2 = x2 + slope * (clip_y + clip_h - y2);
1176 y2 = clip_y + clip_h;
1198 void pixbuf_draw_line(GdkPixbuf *pb,
1199 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1200 gint x1, gint y1, gint x2, gint y2,
1201 guint8 r, guint8 g, guint8 b, guint8 a)
1205 gint rx, ry, rw, rh;
1206 gdouble rx1, ry1, rx2, ry2;
1213 gint cx1, cy1, cx2, cy2;
1217 pw = gdk_pixbuf_get_width(pb);
1218 ph = gdk_pixbuf_get_height(pb);
1220 if (!util_clip_region(0, 0, pw, ph,
1221 clip_x, clip_y, clip_w, clip_h,
1222 &rx, &ry, &rw, &rh)) return;
1223 if (!util_clip_line(static_cast<gdouble>(rx), static_cast<gdouble>(ry), static_cast<gdouble>(rw), static_cast<gdouble>(rh),
1224 static_cast<gdouble>(x1), static_cast<gdouble>(y1), static_cast<gdouble>(x2), static_cast<gdouble>(y2),
1225 &rx1, &ry1, &rx2, &ry2)) return;
1232 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1233 prs = gdk_pixbuf_get_rowstride(pb);
1234 p_pix = gdk_pixbuf_get_pixels(pb);
1236 p_step = (has_alpha) ? 4 : 3;
1238 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1243 t = rx1; rx1 = rx2; rx2 = t;
1244 t = ry1; ry1 = ry2; ry2 = t;
1248 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1249 for (x = rx1; x < rx2; x += 1.0)
1251 px = static_cast<gint>(x + 0.5);
1252 py = static_cast<gint>(ry1 + (x - rx1) * slope + 0.5);
1254 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1256 pp = p_pix + py * prs + px * p_step;
1257 *pp = (r * a + *pp * (256-a)) >> 8;
1259 *pp = (g * a + *pp * (256-a)) >> 8;
1261 *pp = (b * a + *pp * (256-a)) >> 8;
1270 t = rx1; rx1 = rx2; rx2 = t;
1271 t = ry1; ry1 = ry2; ry2 = t;
1275 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1276 for (y = ry1; y < ry2; y += 1.0)
1278 px = static_cast<gint>(rx1 + (y - ry1) * slope + 0.5);
1279 py = static_cast<gint>(y + 0.5);
1281 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1283 pp = p_pix + py * prs + px * p_step;
1284 *pp = (r * a + *pp * (256-a)) >> 8;
1286 *pp = (g * a + *pp * (256-a)) >> 8;
1288 *pp = (b * a + *pp * (256-a)) >> 8;
1295 *-----------------------------------------------------------------------------
1296 * pixbuf drawing (fades and shadows)
1297 *-----------------------------------------------------------------------------
1300 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1301 gint s, gboolean vertical, gint border,
1302 gint x1, gint y1, gint x2, gint y2,
1303 guint8 r, guint8 g, guint8 b, guint8 a)
1310 p_step = (has_alpha) ? 4 : 3;
1311 for (j = y1; j < y2; j++)
1313 pp = p_pix + j * prs + x1 * p_step;
1314 if (!vertical) n = a - a * abs(j - s) / border;
1315 for (i = x1; i < x2; i++)
1317 if (vertical) n = a - a * abs(i - s) / border;
1318 *pp = (r * n + *pp * (256-n)) >> 8;
1320 *pp = (g * n + *pp * (256-n)) >> 8;
1322 *pp = (b * n + *pp * (256-n)) >> 8;
1324 if (has_alpha) pp++;
1329 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1330 gint sx, gint sy, gint border,
1331 gint x1, gint y1, gint x2, gint y2,
1332 guint8, guint8 g, guint8 b, guint8 a)
1338 p_step = (has_alpha) ? 4 : 3;
1339 for (j = y1; j < y2; j++)
1341 pp = p_pix + j * prs + x1 * p_step;
1342 for (i = x1; i < x2; i++)
1347 r = MIN(border, (gint)hypot(i - sx, j - sy));
1348 n = a - a * r / border;
1349 *pp = (r * n + *pp * (256-n)) >> 8;
1351 *pp = (g * n + *pp * (256-n)) >> 8;
1353 *pp = (b * n + *pp * (256-n)) >> 8;
1355 if (has_alpha) pp++;
1360 void pixbuf_draw_shadow(GdkPixbuf *pb,
1361 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1362 gint x, gint y, gint w, gint h, gint border,
1363 guint8 r, guint8 g, guint8 b, guint8 a)
1367 gint rx, ry, rw, rh;
1368 gint fx, fy, fw, fh;
1373 pw = gdk_pixbuf_get_width(pb);
1374 ph = gdk_pixbuf_get_height(pb);
1376 if (!util_clip_region(0, 0, pw, ph,
1377 clip_x, clip_y, clip_w, clip_h,
1378 &rx, &ry, &rw, &rh)) return;
1380 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1381 prs = gdk_pixbuf_get_rowstride(pb);
1382 p_pix = gdk_pixbuf_get_pixels(pb);
1384 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1386 &fx, &fy, &fw, &fh))
1388 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1391 if (border < 1) return;
1393 if (util_clip_region(x, y + border, border, h - border * 2,
1395 &fx, &fy, &fw, &fh))
1397 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1398 x + border, TRUE, border,
1399 fx, fy, fx + fw, fy + fh,
1402 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1404 &fx, &fy, &fw, &fh))
1406 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1407 x + w - border, TRUE, border,
1408 fx, fy, fx + fw, fy + fh,
1411 if (util_clip_region(x + border, y, w - border * 2, border,
1413 &fx, &fy, &fw, &fh))
1415 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1416 y + border, FALSE, border,
1417 fx, fy, fx + fw, fy + fh,
1420 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1422 &fx, &fy, &fw, &fh))
1424 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1425 y + h - border, FALSE, border,
1426 fx, fy, fx + fw, fy + fh,
1429 if (util_clip_region(x, y, border, border,
1431 &fx, &fy, &fw, &fh))
1433 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1434 x + border, y + border, border,
1435 fx, fy, fx + fw, fy + fh,
1438 if (util_clip_region(x + w - border, y, border, border,
1440 &fx, &fy, &fw, &fh))
1442 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1443 x + w - border, y + border, border,
1444 fx, fy, fx + fw, fy + fh,
1447 if (util_clip_region(x, y + h - border, border, border,
1449 &fx, &fy, &fw, &fh))
1451 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1452 x + border, y + h - border, border,
1453 fx, fy, fx + fw, fy + fh,
1456 if (util_clip_region(x + w - border, y + h - border, border, border,
1458 &fx, &fy, &fw, &fh))
1460 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1461 x + w - border, y + h - border, border,
1462 fx, fy, fx + fw, fy + fh,
1469 *-----------------------------------------------------------------------------
1470 * pixbuf color alterations
1471 *-----------------------------------------------------------------------------
1474 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1475 gint x, gint y, gint w, gint h)
1485 pw = gdk_pixbuf_get_width(pb);
1486 ph = gdk_pixbuf_get_height(pb);
1488 if (x < 0 || x + w > pw) return;
1489 if (y < 0 || y + h > ph) return;
1491 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1492 prs = gdk_pixbuf_get_rowstride(pb);
1493 p_pix = gdk_pixbuf_get_pixels(pb);
1495 for (i = 0; i < h; i++)
1497 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1498 for (j = 0; j < w; j++)
1502 grey = (pp[0] + pp[1] + pp[2]) / 3;
1509 if (has_alpha) pp++;
1515 *-----------------------------------------------------------------------------
1516 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1518 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1528 pw = gdk_pixbuf_get_width(pb);
1529 ph = gdk_pixbuf_get_height(pb);
1531 if (x < 0 || x + w > pw) return;
1532 if (y < 0 || y + h > ph) return;
1534 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1535 prs = gdk_pixbuf_get_rowstride(pb);
1536 p_pix = gdk_pixbuf_get_pixels(pb);
1538 for (i = 0; i < h; i++)
1540 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1541 for (j = 0; j < w; j++)
1543 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1551 if (has_alpha) pp++;
1556 if (has_alpha) pp++;
1563 *-----------------------------------------------------------------------------
1564 * pixbuf ignore alpha
1565 *-----------------------------------------------------------------------------
1567 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1568 gint x, gint y, gint w, gint h)
1578 pw = gdk_pixbuf_get_width(pb);
1579 ph = gdk_pixbuf_get_height(pb);
1581 if (x < 0 || x + w > pw) return;
1582 if (y < 0 || y + h > ph) return;
1584 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1585 if (!has_alpha) return;
1587 prs = gdk_pixbuf_get_rowstride(pb);
1588 p_pix = gdk_pixbuf_get_pixels(pb);
1590 for (i = 0; i < h; i++)
1592 pp = p_pix + (y + i) * prs + (x * 4 );
1593 for (j = 0; j < w; j++)
1600 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */