2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "pixbuf-util.h"
27 #include "ui-fileops.h"
33 *-----------------------------------------------------------------------------
35 *-----------------------------------------------------------------------------
38 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
40 GError *error = nullptr;
43 if (!pixbuf || !filename) return FALSE;
45 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
46 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
50 log_printf("Error saving png file: %s\n", error->message);
58 *-----------------------------------------------------------------------------
60 *-----------------------------------------------------------------------------
63 #pragma GCC diagnostic push
64 #pragma GCC diagnostic ignored "-Wunused-function"
65 gboolean pixbuf_to_file_as_jpg_unused(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
67 GError *error = nullptr;
71 if (!pixbuf || !filename) return FALSE;
73 if (quality == -1) quality = 75;
74 if (quality < 1 || quality > 100)
76 log_printf("Jpeg not saved, invalid quality %d\n", quality);
80 qbuf = g_strdup_printf("%d", quality);
81 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
86 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
92 #pragma GCC diagnostic pop
95 *-----------------------------------------------------------------------------
97 *-----------------------------------------------------------------------------
106 static PixbufInline inline_pixbuf_data[] = {
107 { PIXBUF_INLINE_ARCHIVE, "gq-icon-archive-file" },
108 { PIXBUF_INLINE_BROKEN, "gq-icon-broken" },
109 { PIXBUF_INLINE_COLLECTION, "gq-icon-collection" },
110 { PIXBUF_INLINE_ICON_180, "gq-icon-rotate-180" },
111 { PIXBUF_INLINE_ICON_BOOK, "gq-icon-book" },
112 { PIXBUF_INLINE_ICON_CONFIG, "gq-icon-config" },
113 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, "gq-icon-draw-rectangle" },
114 { PIXBUF_INLINE_ICON_EXIF, "gq-icon-exif" },
115 { PIXBUF_INLINE_ICON_EXPOSURE, "gq-icon-exposure" },
116 { PIXBUF_INLINE_ICON_FLOAT, "gq-icon-float" },
117 { PIXBUF_INLINE_ICON, "gqview-icon" },
118 { PIXBUF_INLINE_ICON_GRAYSCALE, "gq-icon-grayscale" },
119 { PIXBUF_INLINE_ICON_HEIF, "gq-icon-heic" },
120 { PIXBUF_INLINE_ICON_HIDETOOLS, "gq-icon-hidetools" },
121 { PIXBUF_INLINE_ICON_MAINTENANCE, "gq-icon-maintenance" },
122 { PIXBUF_INLINE_ICON_MARKS, "gq-icon-marks" },
123 { PIXBUF_INLINE_ICON_MOVE, "gq-icon-move" },
124 { PIXBUF_INLINE_ICON_ORIGINAL, "gq-icon-original" },
125 { PIXBUF_INLINE_ICON_PANORAMA, "gq-icon-panorama" },
126 { PIXBUF_INLINE_ICON_PDF, "gq-icon-pdf" },
127 { PIXBUF_INLINE_ICON_PROPERTIES, "gq-icon-properties" },
128 { PIXBUF_INLINE_ICON_RENAME, "gq-icon-rename" },
129 { PIXBUF_INLINE_ICON_SELECT_ALL, "gq-icon-select-all" },
130 { PIXBUF_INLINE_ICON_SELECT_INVERT, "gq-icon-select-invert" },
131 { PIXBUF_INLINE_ICON_SELECT_NONE, "gq-icon-select-none" },
132 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, "gq-icon-select-rectangle" },
133 { PIXBUF_INLINE_ICON_SORT, "gq-icon-sort" },
134 { PIXBUF_INLINE_ICON_THUMB, "gq-icon-thumb" },
135 { PIXBUF_INLINE_ICON_TOOLS, "gq-icon-tools" },
136 { PIXBUF_INLINE_ICON_VIEW, "gq-icon-view" },
137 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, "gq-icon-zoomfillhor" },
138 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, "gq-icon-zoomfillvert" },
139 { PIXBUF_INLINE_LOGO, "geeqie-logo" },
140 { PIXBUF_INLINE_METADATA, "gq-icon-metadata" },
141 { PIXBUF_INLINE_SCROLLER, "gq-scroller" },
142 { PIXBUF_INLINE_SPLIT_PANE_SYNC, "gq-icon-split-pane-sync" },
143 { PIXBUF_INLINE_UNKNOWN, "gq-icon-unknown" },
144 { PIXBUF_INLINE_VIDEO, "gq-icon-video" },
148 GdkPixbuf *pixbuf_inline(const gchar *key)
150 gboolean dark = FALSE;
151 gchar *file_name = nullptr;
154 GdkPixbuf *icon_pixbuf;
155 GError *error = nullptr;
156 GInputStream *in_stream;
158 GtkSettings *settings;
160 if (!key) return nullptr;
162 settings = gtk_settings_get_default();
163 g_object_get(settings, "gtk-theme-name", &theme_name, nullptr);
164 dark = g_str_has_suffix(theme_name, "dark");
168 while (inline_pixbuf_data[i].key)
170 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
172 file_name = g_strconcat(inline_pixbuf_data[i].data, dark ? "-dark" : "", ".png", nullptr);
173 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
176 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
184 file_name = g_strconcat(inline_pixbuf_data[i].data, ".png", nullptr);
185 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
188 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
194 log_printf("warning: inline pixbuf error: %s", error->message);
196 g_object_unref(in_stream);
200 icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, nullptr, &error);
201 g_object_unref(in_stream);
205 log_printf("warning: inline pixbuf error: %s", error->message);
215 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
220 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
222 static GtkIconFactory *icon_factory = nullptr;
223 GtkIconSet *icon_set;
227 icon_factory = gtk_icon_factory_new();
228 gtk_icon_factory_add_default(icon_factory);
231 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
232 gtk_icon_factory_add(icon_factory, key, icon_set);
236 void pixbuf_inline_register_stock_icons()
241 while (inline_pixbuf_data[i].key)
243 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
248 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
250 GtkIconTheme *icon_theme;
252 GError *error = nullptr;
254 icon_theme = gtk_icon_theme_get_default();
256 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
258 pixbuf = gtk_icon_theme_load_icon(icon_theme,
259 icon, /* icon name */
261 static_cast<GtkIconLookupFlags>(0), /* flags */
267 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
272 if (strchr(icon, '.'))
274 /* try again without extension */
275 gchar *icon2 = remove_extension_from_path(icon);
276 pixbuf = gtk_icon_theme_load_icon(icon_theme,
277 icon2, /* icon name */
279 static_cast<GtkIconLookupFlags>(0), /* flags */
283 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
287 /* try as an absolute path */
288 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
291 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
299 if (!pixbuf) return FALSE;
301 register_stock_icon(key, pixbuf);
305 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
306 gint old_w, gint old_h,
307 gint *new_w, gint *new_h)
309 if ((static_cast<gdouble>(req_w) / old_w) < (static_cast<gdouble>(req_h) / old_h))
312 *new_h = static_cast<gdouble>(*new_w) / old_w * old_h;
313 if (*new_h < 1) *new_h = 1;
318 *new_w = static_cast<gdouble>(*new_h) / old_h * old_w;
319 if (*new_w < 1) *new_w = 1;
322 return (*new_w != old_w || *new_h != old_h);
325 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
329 switch (fd->format_class)
331 case FORMAT_CLASS_UNKNOWN:
332 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
334 case FORMAT_CLASS_META:
335 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
337 case FORMAT_CLASS_VIDEO:
338 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
340 case FORMAT_CLASS_COLLECTION:
341 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
343 case FORMAT_CLASS_DOCUMENT:
344 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
346 case FORMAT_CLASS_ARCHIVE:
347 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
350 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
353 if (requested_width && requested_height)
355 gint w = gdk_pixbuf_get_width(pixbuf);
356 gint h = gdk_pixbuf_get_height(pixbuf);
358 if (w > requested_width || h > requested_height)
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);
456 ROTATE_BUFFER_WIDTH = 48,
457 ROTATE_BUFFER_HEIGHT = 48
461 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
464 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
479 if (!src) return nullptr;
481 sw = gdk_pixbuf_get_width(src);
482 sh = gdk_pixbuf_get_height(src);
483 has_alpha = gdk_pixbuf_get_has_alpha(src);
484 srs = gdk_pixbuf_get_rowstride(src);
485 s_pix = gdk_pixbuf_get_pixels(src);
489 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
490 drs = gdk_pixbuf_get_rowstride(dest);
491 d_pix = gdk_pixbuf_get_pixels(dest);
493 a = (has_alpha ? 4 : 3);
495 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
496 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
497 b_pix = gdk_pixbuf_get_pixels(buffer);
498 brs = gdk_pixbuf_get_rowstride(buffer);
500 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
502 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
503 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
507 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
508 pixbuf_copy_block_rotate(s_pix, srs, j, i,
510 a, counter_clockwise);
512 if (counter_clockwise)
522 pixbuf_copy_block(b_pix, brs, w, h,
523 d_pix, drs, x, y, a);
527 g_object_unref(buffer);
530 /* this is the simple version of rotation (roughly 2-4x slower) */
532 for (i = 0; i < sh; i++)
534 sp = s_pix + (i * srs);
535 for (j = 0; j < sw; j++)
537 if (counter_clockwise)
539 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
543 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
546 *(dp++) = *(sp++); /* r */
547 *(dp++) = *(sp++); /* g */
548 *(dp++) = *(sp++); /* b */
549 if (has_alpha) *(dp) = *(sp++); /* a */
558 * Returns a copy of pixbuf mirrored and or flipped.
559 * TO do a 180 degree rotations set both mirror and flipped TRUE
560 * if mirror and flip are FALSE, result is a simple copy.
562 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
575 if (!src) return nullptr;
577 w = gdk_pixbuf_get_width(src);
578 h = gdk_pixbuf_get_height(src);
579 has_alpha = gdk_pixbuf_get_has_alpha(src);
580 srs = gdk_pixbuf_get_rowstride(src);
581 s_pix = gdk_pixbuf_get_pixels(src);
583 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
584 drs = gdk_pixbuf_get_rowstride(dest);
585 d_pix = gdk_pixbuf_get_pixels(dest);
587 a = has_alpha ? 4 : 3;
589 for (i = 0; i < h; i++)
591 sp = s_pix + (i * srs);
594 dp = d_pix + ((h - i - 1) * drs);
598 dp = d_pix + (i * drs);
603 for (j = 0; j < w; j++)
605 *(dp++) = *(sp++); /* r */
606 *(dp++) = *(sp++); /* g */
607 *(dp++) = *(sp++); /* b */
608 if (has_alpha) *(dp) = *(sp++); /* a */
614 for (j = 0; j < w; j++)
616 *(dp++) = *(sp++); /* r */
617 *(dp++) = *(sp++); /* g */
618 *(dp++) = *(sp++); /* b */
619 if (has_alpha) *(dp++) = *(sp++); /* a */
627 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
630 GdkPixbuf *tmp = nullptr;
634 case EXIF_ORIENTATION_TOP_LEFT:
635 dest = gdk_pixbuf_copy(pixbuf);
637 case EXIF_ORIENTATION_TOP_RIGHT:
639 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
641 case EXIF_ORIENTATION_BOTTOM_RIGHT:
643 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
645 case EXIF_ORIENTATION_BOTTOM_LEFT:
647 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
649 case EXIF_ORIENTATION_LEFT_TOP:
650 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
651 dest = pixbuf_copy_rotate_90(tmp, FALSE);
653 case EXIF_ORIENTATION_RIGHT_TOP:
654 /* rotated -90 (270) */
655 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
657 case EXIF_ORIENTATION_RIGHT_BOTTOM:
658 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
659 dest = pixbuf_copy_rotate_90(tmp, TRUE);
661 case EXIF_ORIENTATION_LEFT_BOTTOM:
663 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
666 dest = gdk_pixbuf_copy(pixbuf);
669 if (tmp) g_object_unref(tmp);
676 *-----------------------------------------------------------------------------
677 * pixbuf drawing (rectangles)
678 *-----------------------------------------------------------------------------
682 * Fills region of pixbuf at x,y over w,h
683 * with colors red (r), green (g), blue (b)
684 * applying alpha (a), use a=255 for solid.
686 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
687 gint x, gint y, gint w, gint h,
688 gint r, gint g, gint b, gint a)
698 pw = gdk_pixbuf_get_width(pb);
699 ph = gdk_pixbuf_get_height(pb);
701 if (x < 0 || x + w > pw) return;
702 if (y < 0 || y + h > ph) return;
704 has_alpha = gdk_pixbuf_get_has_alpha(pb);
705 prs = gdk_pixbuf_get_rowstride(pb);
706 p_pix = gdk_pixbuf_get_pixels(pb);
708 for (i = 0; i < h; i++)
710 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
711 for (j = 0; j < w; j++)
713 *pp = (r * a + *pp * (256-a)) >> 8;
715 *pp = (g * a + *pp * (256-a)) >> 8;
717 *pp = (b * a + *pp * (256-a)) >> 8;
724 #pragma GCC diagnostic push
725 #pragma GCC diagnostic ignored "-Wunused-function"
726 void pixbuf_draw_rect_unused(GdkPixbuf *pb,
727 gint x, gint y, gint w, gint h,
728 gint r, gint g, gint b, gint a,
729 gint left, gint right, gint top, gint bottom)
731 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
733 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
735 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
737 pixbuf_draw_rect_fill(pb, x, y, left, h,
740 #pragma GCC diagnostic pop
742 void pixbuf_set_rect_fill(GdkPixbuf *pb,
743 gint x, gint y, gint w, gint h,
744 gint r, gint g, gint b, gint a)
754 pw = gdk_pixbuf_get_width(pb);
755 ph = gdk_pixbuf_get_height(pb);
757 if (x < 0 || x + w > pw) return;
758 if (y < 0 || y + h > ph) return;
760 has_alpha = gdk_pixbuf_get_has_alpha(pb);
761 prs = gdk_pixbuf_get_rowstride(pb);
762 p_pix = gdk_pixbuf_get_pixels(pb);
764 for (i = 0; i < h; i++)
766 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
767 for (j = 0; j < w; j++)
772 if (has_alpha) { *pp = a; pp++; }
777 void pixbuf_set_rect(GdkPixbuf *pb,
778 gint x, gint y, gint w, gint h,
779 gint r, gint g, gint b, gint a,
780 gint left, gint right, gint top, gint bottom)
782 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
784 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
786 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
788 pixbuf_set_rect_fill(pb, x, y, left, h,
792 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
799 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
800 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
802 buf = gdk_pixbuf_get_pixels(pb);
803 has_alpha = gdk_pixbuf_get_has_alpha(pb);
804 rowstride = gdk_pixbuf_get_rowstride(pb);
806 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
810 if (has_alpha) *p = a;
815 *-----------------------------------------------------------------------------
816 * pixbuf text rendering
817 *-----------------------------------------------------------------------------
820 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
821 GdkPixbuf *dest, gint dx, gint dy,
823 guint8 r, guint8 g, guint8 b, guint8 a)
838 if (!src || !dest) return;
840 sw = gdk_pixbuf_get_width(src);
841 sh = gdk_pixbuf_get_height(src);
843 if (sx < 0 || sx + w > sw) return;
844 if (sy < 0 || sy + h > sh) return;
846 dw = gdk_pixbuf_get_width(dest);
847 dh = gdk_pixbuf_get_height(dest);
849 if (dx < 0 || dx + w > dw) return;
850 if (dy < 0 || dy + h > dh) return;
852 s_alpha = gdk_pixbuf_get_has_alpha(src);
853 d_alpha = gdk_pixbuf_get_has_alpha(dest);
854 srs = gdk_pixbuf_get_rowstride(src);
855 drs = gdk_pixbuf_get_rowstride(dest);
856 s_pix = gdk_pixbuf_get_pixels(src);
857 d_pix = gdk_pixbuf_get_pixels(dest);
859 s_step = (s_alpha) ? 4 : 3;
860 d_step = (d_alpha) ? 4 : 3;
862 for (i = 0; i < h; i++)
864 sp = s_pix + (sy + i) * srs + sx * s_step;
865 dp = d_pix + (dy + i) * drs + dx * d_step;
866 for (j = 0; j < w; j++)
872 asub = a * sp[0] / 255;
873 *dp = (r * asub + *dp * (256-asub)) >> 8;
875 asub = a * sp[1] / 255;
876 *dp = (g * asub + *dp * (256-asub)) >> 8;
878 asub = a * sp[2] / 255;
879 *dp = (b * asub + *dp * (256-asub)) >> 8;
884 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
898 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *,
900 guint8 r, guint8 g, guint8 b, guint8 a)
906 cairo_surface_t *source;
909 pango_layout_get_pixel_size(layout, &w, &h);
910 if (w < 1 || h < 1) return;
912 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
914 cr = cairo_create (source);
915 cairo_set_source_rgb(cr, 0, 0, 0);
916 cairo_rectangle (cr, 0, 0, w, h);
918 cairo_set_source_rgb(cr, 1, 1, 1);
919 pango_cairo_show_layout (cr, layout);
922 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
924 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
926 cairo_image_surface_get_width (source),
927 cairo_image_surface_get_height (source),
928 cairo_image_surface_get_stride (source),
934 dw = gdk_pixbuf_get_width(pixbuf);
935 dh = gdk_pixbuf_get_height(pixbuf);
951 if (x + w > dw) w = dw - x;
952 if (y + h > dh) h = dh - y;
954 pixbuf_copy_font(buffer, sx, sy,
958 g_object_unref(buffer);
959 cairo_surface_destroy(source);
963 *-----------------------------------------------------------------------------
964 * pixbuf drawing (triangle)
965 *-----------------------------------------------------------------------------
968 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
969 gint *rx, gint *ry, gint *rw, gint *rh)
977 tw = MAX(abs(x1 - x2), abs(x2 - x3));
978 tw = MAX(tw, abs(x3 - x1));
979 th = MAX(abs(y1 - y2), abs(y2 - y3));
980 th = MAX(th, abs(y3 - y1));
988 void pixbuf_draw_triangle(GdkPixbuf *pb,
989 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
990 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
991 guint8 r, guint8 g, guint8 b, guint8 a)
1003 gdouble slope1, slope2;
1004 gint slope1_x, slope1_y;
1007 gboolean middle = FALSE;
1011 pw = gdk_pixbuf_get_width(pb);
1012 ph = gdk_pixbuf_get_height(pb);
1014 if (!util_clip_region(0, 0, pw, ph,
1015 clip_x, clip_y, clip_w, clip_h,
1016 &rx, &ry, &rw, &rh)) return;
1018 util_clip_triangle(x1, y1, x2, y2, x3, y3,
1019 &tx, &ty, &tw, &th);
1021 if (!util_clip_region(rx, ry, rw, rh,
1023 &fx1, &fy1, &fw, &fh)) return;
1027 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1028 prs = gdk_pixbuf_get_rowstride(pb);
1029 p_pix = gdk_pixbuf_get_pixels(pb);
1031 p_step = (has_alpha) ? 4 : 3;
1035 t = x1; x1 = x2; x2 = t;
1036 t = y1; y1 = y2; y2 = t;
1040 t = x2; x2 = x3; x3 = t;
1041 t = y2; y2 = y3; y3 = t;
1045 t = x1; x1 = x2; x2 = t;
1046 t = y1; y1 = y2; y2 = t;
1049 slope1 = static_cast<gdouble>(y2 - y1);
1050 if (slope1) slope1 = static_cast<gdouble>(x2 - x1) / slope1;
1053 slope2 = static_cast<gdouble>(y3 - y1);
1054 if (slope2) slope2 = static_cast<gdouble>(x3 - x1) / slope2;
1056 for (y = fy1; y < fy2; y++)
1060 if (!middle && y > y2)
1062 slope1 = static_cast<gdouble>(y3 - y2);
1063 if (slope1) slope1 = static_cast<gdouble>(x3 - x2) / slope1;
1070 xa = slope1_x + (slope1 * (y - slope1_y) + 0.5);
1071 xb = x1 + (slope2 * (y - y1) + 0.5);
1075 t = xa; xa = xb; xb = t;
1078 xa = CLAMP(xa, fx1, fx2);
1079 xb = CLAMP(xb, fx1, fx2);
1081 pp = p_pix + y * prs + xa * p_step;
1085 *pp = (r * a + *pp * (256-a)) >> 8;
1087 *pp = (g * a + *pp * (256-a)) >> 8;
1089 *pp = (b * a + *pp * (256-a)) >> 8;
1091 if (has_alpha) pp++;
1099 *-----------------------------------------------------------------------------
1100 * pixbuf drawing (line)
1101 *-----------------------------------------------------------------------------
1104 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1105 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1106 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1108 gboolean flip = FALSE;
1115 t = x1; x1 = x2; x2 = t;
1116 t = y1; y1 = y2; y2 = t;
1120 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1124 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1128 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1136 slope = (y2 - y1) / d;
1139 y1 = y1 + slope * (clip_x - x1);
1142 if (x2 > clip_x + clip_w)
1144 y2 = y2 + slope * (clip_x + clip_w - x2);
1145 x2 = clip_x + clip_w;
1151 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1157 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1159 t = x1; x1 = x2; x2 = t;
1160 t = y1; y1 = y2; y2 = t;
1169 slope = (x2 - x1) / d;
1172 x1 = x1 + slope * (clip_y - y1);
1175 if (y2 > clip_y + clip_h)
1177 x2 = x2 + slope * (clip_y + clip_h - y2);
1178 y2 = clip_y + clip_h;
1200 void pixbuf_draw_line(GdkPixbuf *pb,
1201 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1202 gint x1, gint y1, gint x2, gint y2,
1203 guint8 r, guint8 g, guint8 b, guint8 a)
1207 gint rx, ry, rw, rh;
1208 gdouble rx1, ry1, rx2, ry2;
1215 gint cx1, cy1, cx2, cy2;
1219 pw = gdk_pixbuf_get_width(pb);
1220 ph = gdk_pixbuf_get_height(pb);
1222 if (!util_clip_region(0, 0, pw, ph,
1223 clip_x, clip_y, clip_w, clip_h,
1224 &rx, &ry, &rw, &rh)) return;
1225 if (!util_clip_line(static_cast<gdouble>(rx), static_cast<gdouble>(ry), static_cast<gdouble>(rw), static_cast<gdouble>(rh),
1226 static_cast<gdouble>(x1), static_cast<gdouble>(y1), static_cast<gdouble>(x2), static_cast<gdouble>(y2),
1227 &rx1, &ry1, &rx2, &ry2)) return;
1234 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1235 prs = gdk_pixbuf_get_rowstride(pb);
1236 p_pix = gdk_pixbuf_get_pixels(pb);
1238 p_step = (has_alpha) ? 4 : 3;
1240 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1245 t = rx1; rx1 = rx2; rx2 = t;
1246 t = ry1; ry1 = ry2; ry2 = t;
1250 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1251 for (x = rx1; x < rx2; x += 1.0)
1253 px = static_cast<gint>(x + 0.5);
1254 py = static_cast<gint>(ry1 + (x - rx1) * slope + 0.5);
1256 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1258 pp = p_pix + py * prs + px * p_step;
1259 *pp = (r * a + *pp * (256-a)) >> 8;
1261 *pp = (g * a + *pp * (256-a)) >> 8;
1263 *pp = (b * a + *pp * (256-a)) >> 8;
1272 t = rx1; rx1 = rx2; rx2 = t;
1273 t = ry1; ry1 = ry2; ry2 = t;
1277 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1278 for (y = ry1; y < ry2; y += 1.0)
1280 px = static_cast<gint>(rx1 + (y - ry1) * slope + 0.5);
1281 py = static_cast<gint>(y + 0.5);
1283 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1285 pp = p_pix + py * prs + px * p_step;
1286 *pp = (r * a + *pp * (256-a)) >> 8;
1288 *pp = (g * a + *pp * (256-a)) >> 8;
1290 *pp = (b * a + *pp * (256-a)) >> 8;
1297 *-----------------------------------------------------------------------------
1298 * pixbuf drawing (fades and shadows)
1299 *-----------------------------------------------------------------------------
1302 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1303 gint s, gboolean vertical, gint border,
1304 gint x1, gint y1, gint x2, gint y2,
1305 guint8 r, guint8 g, guint8 b, guint8 a)
1312 p_step = (has_alpha) ? 4 : 3;
1313 for (j = y1; j < y2; j++)
1315 pp = p_pix + j * prs + x1 * p_step;
1316 if (!vertical) n = a - a * abs(j - s) / border;
1317 for (i = x1; i < x2; i++)
1319 if (vertical) n = a - a * abs(i - s) / border;
1320 *pp = (r * n + *pp * (256-n)) >> 8;
1322 *pp = (g * n + *pp * (256-n)) >> 8;
1324 *pp = (b * n + *pp * (256-n)) >> 8;
1326 if (has_alpha) pp++;
1331 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1332 gint sx, gint sy, gint border,
1333 gint x1, gint y1, gint x2, gint y2,
1334 guint8, guint8 g, guint8 b, guint8 a)
1340 p_step = (has_alpha) ? 4 : 3;
1341 for (j = y1; j < y2; j++)
1343 pp = p_pix + j * prs + x1 * p_step;
1344 for (i = x1; i < x2; i++)
1349 r = MIN(border, (gint)hypot(i - sx, j - sy));
1350 n = a - a * r / border;
1351 *pp = (r * n + *pp * (256-n)) >> 8;
1353 *pp = (g * n + *pp * (256-n)) >> 8;
1355 *pp = (b * n + *pp * (256-n)) >> 8;
1357 if (has_alpha) pp++;
1362 void pixbuf_draw_shadow(GdkPixbuf *pb,
1363 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1364 gint x, gint y, gint w, gint h, gint border,
1365 guint8 r, guint8 g, guint8 b, guint8 a)
1369 gint rx, ry, rw, rh;
1370 gint fx, fy, fw, fh;
1375 pw = gdk_pixbuf_get_width(pb);
1376 ph = gdk_pixbuf_get_height(pb);
1378 if (!util_clip_region(0, 0, pw, ph,
1379 clip_x, clip_y, clip_w, clip_h,
1380 &rx, &ry, &rw, &rh)) return;
1382 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1383 prs = gdk_pixbuf_get_rowstride(pb);
1384 p_pix = gdk_pixbuf_get_pixels(pb);
1386 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1388 &fx, &fy, &fw, &fh))
1390 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1393 if (border < 1) return;
1395 if (util_clip_region(x, y + border, border, h - border * 2,
1397 &fx, &fy, &fw, &fh))
1399 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1400 x + border, TRUE, border,
1401 fx, fy, fx + fw, fy + fh,
1404 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1406 &fx, &fy, &fw, &fh))
1408 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1409 x + w - border, TRUE, border,
1410 fx, fy, fx + fw, fy + fh,
1413 if (util_clip_region(x + border, y, w - border * 2, border,
1415 &fx, &fy, &fw, &fh))
1417 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1418 y + border, FALSE, border,
1419 fx, fy, fx + fw, fy + fh,
1422 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1424 &fx, &fy, &fw, &fh))
1426 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1427 y + h - border, FALSE, border,
1428 fx, fy, fx + fw, fy + fh,
1431 if (util_clip_region(x, y, border, border,
1433 &fx, &fy, &fw, &fh))
1435 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1436 x + border, y + border, border,
1437 fx, fy, fx + fw, fy + fh,
1440 if (util_clip_region(x + w - border, y, border, border,
1442 &fx, &fy, &fw, &fh))
1444 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1445 x + w - border, y + border, border,
1446 fx, fy, fx + fw, fy + fh,
1449 if (util_clip_region(x, y + h - border, border, border,
1451 &fx, &fy, &fw, &fh))
1453 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1454 x + border, y + h - border, border,
1455 fx, fy, fx + fw, fy + fh,
1458 if (util_clip_region(x + w - border, y + h - border, border, border,
1460 &fx, &fy, &fw, &fh))
1462 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1463 x + w - border, y + h - border, border,
1464 fx, fy, fx + fw, fy + fh,
1471 *-----------------------------------------------------------------------------
1472 * pixbuf color alterations
1473 *-----------------------------------------------------------------------------
1476 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1477 gint x, gint y, gint w, gint h)
1487 pw = gdk_pixbuf_get_width(pb);
1488 ph = gdk_pixbuf_get_height(pb);
1490 if (x < 0 || x + w > pw) return;
1491 if (y < 0 || y + h > ph) return;
1493 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1494 prs = gdk_pixbuf_get_rowstride(pb);
1495 p_pix = gdk_pixbuf_get_pixels(pb);
1497 for (i = 0; i < h; i++)
1499 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1500 for (j = 0; j < w; j++)
1504 grey = (pp[0] + pp[1] + pp[2]) / 3;
1511 if (has_alpha) pp++;
1517 *-----------------------------------------------------------------------------
1518 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1520 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1530 pw = gdk_pixbuf_get_width(pb);
1531 ph = gdk_pixbuf_get_height(pb);
1533 if (x < 0 || x + w > pw) return;
1534 if (y < 0 || y + h > ph) return;
1536 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1537 prs = gdk_pixbuf_get_rowstride(pb);
1538 p_pix = gdk_pixbuf_get_pixels(pb);
1540 for (i = 0; i < h; i++)
1542 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1543 for (j = 0; j < w; j++)
1545 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1553 if (has_alpha) pp++;
1558 if (has_alpha) pp++;
1565 *-----------------------------------------------------------------------------
1566 * pixbuf ignore alpha
1567 *-----------------------------------------------------------------------------
1569 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1570 gint x, gint y, gint w, gint h)
1580 pw = gdk_pixbuf_get_width(pb);
1581 ph = gdk_pixbuf_get_height(pb);
1583 if (x < 0 || x + w > pw) return;
1584 if (y < 0 || y + h > ph) return;
1586 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1587 if (!has_alpha) return;
1589 prs = gdk_pixbuf_get_rowstride(pb);
1590 p_pix = gdk_pixbuf_get_pixels(pb);
1592 for (i = 0; i < h; i++)
1594 pp = p_pix + (y + i) * prs + (x * 4 );
1595 for (j = 0; j < w; j++)
1602 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */