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"
25 #include "ui-fileops.h"
31 *-----------------------------------------------------------------------------
33 *-----------------------------------------------------------------------------
36 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
38 GError *error = nullptr;
41 if (!pixbuf || !filename) return FALSE;
43 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
44 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
48 log_printf("Error saving png file: %s\n", error->message);
56 *-----------------------------------------------------------------------------
58 *-----------------------------------------------------------------------------
61 #pragma GCC diagnostic push
62 #pragma GCC diagnostic ignored "-Wunused-function"
63 gboolean pixbuf_to_file_as_jpg_unused(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
69 if (!pixbuf || !filename) return FALSE;
71 if (quality == -1) quality = 75;
72 if (quality < 1 || quality > 100)
74 log_printf("Jpeg not saved, invalid quality %d\n", quality);
78 qbuf = g_strdup_printf("%d", quality);
79 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
84 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
90 #pragma GCC diagnostic pop
93 *-----------------------------------------------------------------------------
95 *-----------------------------------------------------------------------------
104 static PixbufInline inline_pixbuf_data[] = {
105 { PIXBUF_INLINE_ARCHIVE, "gq-archive-file.png" },
106 { PIXBUF_INLINE_BROKEN, "gq-sheet-broken.png" },
107 { PIXBUF_INLINE_COLLECTION, "gq-collection.png" },
108 { PIXBUF_INLINE_ICON, "gqview-icon.png" },
109 { PIXBUF_INLINE_ICON_180, "gq-icon-rotate-180.png" },
110 { PIXBUF_INLINE_ICON_BOOK, "gq-icon-book.png" },
111 { PIXBUF_INLINE_ICON_CONFIG, "gq-icon-config.png" },
112 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, "gq-icon-draw-rectangle.png" },
113 { PIXBUF_INLINE_ICON_EXIF, "gq-icon-exif.png" },
114 { PIXBUF_INLINE_ICON_EXPOSURE, "gq-icon-exposure.png" },
115 { PIXBUF_INLINE_ICON_FILE_FILTER, "gq-icon-file-filter.png" },
116 { PIXBUF_INLINE_ICON_FLOAT, "gq-icon-float.png" },
117 { PIXBUF_INLINE_ICON_GRAYSCALE, "gq-icon-grayscale.png" },
118 { PIXBUF_INLINE_ICON_HEIF, "gq-icon-heic.png" },
119 { PIXBUF_INLINE_ICON_HIDETOOLS, "gq-icon-hidetools.png" },
120 { PIXBUF_INLINE_ICON_MAINTENANCE, "gq-icon-maintenance.png" },
121 { PIXBUF_INLINE_ICON_MARKS, "gq-icon-marks.png" },
122 { PIXBUF_INLINE_ICON_MOVE, "gq-icon-move.png" },
123 { PIXBUF_INLINE_ICON_ORIGINAL, "gq-icon-original.png" },
124 { PIXBUF_INLINE_ICON_PANORAMA, "gq-icon-panorama.png" },
125 { PIXBUF_INLINE_ICON_PDF, "gq-icon-pdf.png" },
126 { PIXBUF_INLINE_ICON_RENAME, "gq-icon-rename.png" },
127 { PIXBUF_INLINE_ICON_SELECT_ALL, "gq-icon-select-all.png" },
128 { PIXBUF_INLINE_ICON_SELECT_INVERT, "gq-icon-select-invert.png" },
129 { PIXBUF_INLINE_ICON_SELECT_NONE, "gq-icon-select-none.png" },
130 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, "gq-icon-select-rectangle.png" },
131 { PIXBUF_INLINE_ICON_SORT, "gq-icon-sort.png" },
132 { PIXBUF_INLINE_ICON_THUMB, "gq-icon-thumb.png" },
133 { PIXBUF_INLINE_ICON_TOOLS, "gq-icon-tools.png" },
134 { PIXBUF_INLINE_ICON_TRASH, "gq-icon-trash.png" },
135 { PIXBUF_INLINE_ICON_VIEW, "gq-icon-view.png" },
136 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, "gq-icon-zoomfillhor.png" },
137 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, "gq-icon-zoomfillvert.png" },
138 { PIXBUF_INLINE_LOGO, "geeqie-logo.png" },
139 { PIXBUF_INLINE_METADATA, "gq-sheet-metadata.png" },
140 { PIXBUF_INLINE_SCROLLER, "gq-scroller.png" },
141 { PIXBUF_INLINE_SPLIT_PANE_SYNC, "gq-icon-split-pane-sync.png" },
142 { PIXBUF_INLINE_UNKNOWN, "gq-sheet-unknown.png" },
143 { PIXBUF_INLINE_VIDEO, "gq-sheet-video.png" },
147 GdkPixbuf *pixbuf_inline(const gchar *key)
149 GError *error = nullptr;
150 GInputStream *in_stream;
151 GdkPixbuf *icon_pixbuf;
155 if (!key) return nullptr;
158 while (inline_pixbuf_data[i].key)
160 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
162 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, inline_pixbuf_data[i].data, NULL);
164 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
169 log_printf("warning: inline pixbuf error: %s", error->message);
171 g_object_unref(in_stream);
175 icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, nullptr, &error);
176 g_object_unref(in_stream);
180 log_printf("warning: inline pixbuf error: %s", error->message);
190 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
195 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
197 static GtkIconFactory *icon_factory = nullptr;
198 GtkIconSet *icon_set;
202 icon_factory = gtk_icon_factory_new();
203 gtk_icon_factory_add_default(icon_factory);
206 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
207 gtk_icon_factory_add(icon_factory, key, icon_set);
211 void pixbuf_inline_register_stock_icons()
216 while (inline_pixbuf_data[i].key)
218 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
223 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
225 GtkIconTheme *icon_theme;
227 GError *error = nullptr;
229 icon_theme = gtk_icon_theme_get_default();
231 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
233 pixbuf = gtk_icon_theme_load_icon(icon_theme,
234 icon, /* icon name */
236 static_cast<GtkIconLookupFlags>(0), /* flags */
242 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
247 if (strchr(icon, '.'))
249 /* try again without extension */
250 gchar *icon2 = remove_extension_from_path(icon);
251 pixbuf = gtk_icon_theme_load_icon(icon_theme,
252 icon2, /* icon name */
254 static_cast<GtkIconLookupFlags>(0), /* flags */
258 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
262 /* try as an absolute path */
263 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
266 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
274 if (!pixbuf) return FALSE;
276 register_stock_icon(key, pixbuf);
280 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
281 gint old_w, gint old_h,
282 gint *new_w, gint *new_h)
284 if ((static_cast<gdouble>(req_w) / old_w) < (static_cast<gdouble>(req_h) / old_h))
287 *new_h = static_cast<gdouble>(*new_w) / old_w * old_h;
288 if (*new_h < 1) *new_h = 1;
293 *new_w = static_cast<gdouble>(*new_h) / old_h * old_w;
294 if (*new_w < 1) *new_w = 1;
297 return (*new_w != old_w || *new_h != old_h);
300 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
304 switch (fd->format_class)
306 case FORMAT_CLASS_UNKNOWN:
307 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
309 case FORMAT_CLASS_META:
310 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
312 case FORMAT_CLASS_VIDEO:
313 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
315 case FORMAT_CLASS_COLLECTION:
316 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
318 case FORMAT_CLASS_DOCUMENT:
319 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
321 case FORMAT_CLASS_ARCHIVE:
322 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
325 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
328 if (requested_width && requested_height)
330 gint w = gdk_pixbuf_get_width(pixbuf);
331 gint h = gdk_pixbuf_get_height(pixbuf);
333 if (w > requested_width || h > requested_height)
337 if (pixbuf_scale_aspect(requested_width, requested_height,
343 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
344 g_object_unref(G_OBJECT(tmp));
353 *-----------------------------------------------------------------------------
355 *-----------------------------------------------------------------------------
358 gboolean util_clip_region(gint x, gint y, gint w, gint h,
359 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
360 gint *rx, gint *ry, gint *rw, gint *rh)
362 if (clip_x + clip_w <= x ||
364 clip_y + clip_h <= y ||
370 *rx = MAX(x, clip_x);
371 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
373 *ry = MAX(y, clip_y);
374 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
380 *-----------------------------------------------------------------------------
382 *-----------------------------------------------------------------------------
385 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
386 guchar *dest, gint dest_row_stride, gint w, gint h,
387 gint bytes_per_pixel, gboolean counter_clockwise)
393 for (i = 0; i < h; i++)
395 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
396 for (j = 0; j < w; j++)
398 if (counter_clockwise)
400 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
404 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
406 *(dp++) = *(sp++); /* r */
407 *(dp++) = *(sp++); /* g */
408 *(dp++) = *(sp++); /* b */
409 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
415 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
416 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
422 for (i = 0; i < h; i++)
424 sp = src + (i * src_row_stride);
425 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
426 memcpy(dp, sp, w * bytes_per_pixel);
430 #define ROTATE_BUFFER_WIDTH 48
431 #define ROTATE_BUFFER_HEIGHT 48
434 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
437 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
452 if (!src) return nullptr;
454 sw = gdk_pixbuf_get_width(src);
455 sh = gdk_pixbuf_get_height(src);
456 has_alpha = gdk_pixbuf_get_has_alpha(src);
457 srs = gdk_pixbuf_get_rowstride(src);
458 s_pix = gdk_pixbuf_get_pixels(src);
462 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
463 drs = gdk_pixbuf_get_rowstride(dest);
464 d_pix = gdk_pixbuf_get_pixels(dest);
466 a = (has_alpha ? 4 : 3);
468 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
469 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
470 b_pix = gdk_pixbuf_get_pixels(buffer);
471 brs = gdk_pixbuf_get_rowstride(buffer);
473 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
475 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
476 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
480 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
481 pixbuf_copy_block_rotate(s_pix, srs, j, i,
483 a, counter_clockwise);
485 if (counter_clockwise)
495 pixbuf_copy_block(b_pix, brs, w, h,
496 d_pix, drs, x, y, a);
500 g_object_unref(buffer);
503 /* this is the simple version of rotation (roughly 2-4x slower) */
505 for (i = 0; i < sh; i++)
507 sp = s_pix + (i * srs);
508 for (j = 0; j < sw; j++)
510 if (counter_clockwise)
512 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
516 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
519 *(dp++) = *(sp++); /* r */
520 *(dp++) = *(sp++); /* g */
521 *(dp++) = *(sp++); /* b */
522 if (has_alpha) *(dp) = *(sp++); /* a */
531 * Returns a copy of pixbuf mirrored and or flipped.
532 * TO do a 180 degree rotations set both mirror and flipped TRUE
533 * if mirror and flip are FALSE, result is a simple copy.
535 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
548 if (!src) return nullptr;
550 w = gdk_pixbuf_get_width(src);
551 h = gdk_pixbuf_get_height(src);
552 has_alpha = gdk_pixbuf_get_has_alpha(src);
553 srs = gdk_pixbuf_get_rowstride(src);
554 s_pix = gdk_pixbuf_get_pixels(src);
556 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
557 drs = gdk_pixbuf_get_rowstride(dest);
558 d_pix = gdk_pixbuf_get_pixels(dest);
560 a = has_alpha ? 4 : 3;
562 for (i = 0; i < h; i++)
564 sp = s_pix + (i * srs);
567 dp = d_pix + ((h - i - 1) * drs);
571 dp = d_pix + (i * drs);
576 for (j = 0; j < w; j++)
578 *(dp++) = *(sp++); /* r */
579 *(dp++) = *(sp++); /* g */
580 *(dp++) = *(sp++); /* b */
581 if (has_alpha) *(dp) = *(sp++); /* a */
587 for (j = 0; j < w; j++)
589 *(dp++) = *(sp++); /* r */
590 *(dp++) = *(sp++); /* g */
591 *(dp++) = *(sp++); /* b */
592 if (has_alpha) *(dp++) = *(sp++); /* a */
600 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
603 GdkPixbuf *tmp = nullptr;
607 case EXIF_ORIENTATION_TOP_LEFT:
608 dest = gdk_pixbuf_copy(pixbuf);
610 case EXIF_ORIENTATION_TOP_RIGHT:
612 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
614 case EXIF_ORIENTATION_BOTTOM_RIGHT:
616 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
618 case EXIF_ORIENTATION_BOTTOM_LEFT:
620 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
622 case EXIF_ORIENTATION_LEFT_TOP:
623 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
624 dest = pixbuf_copy_rotate_90(tmp, FALSE);
626 case EXIF_ORIENTATION_RIGHT_TOP:
627 /* rotated -90 (270) */
628 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
630 case EXIF_ORIENTATION_RIGHT_BOTTOM:
631 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
632 dest = pixbuf_copy_rotate_90(tmp, TRUE);
634 case EXIF_ORIENTATION_LEFT_BOTTOM:
636 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
639 dest = gdk_pixbuf_copy(pixbuf);
642 if (tmp) g_object_unref(tmp);
649 *-----------------------------------------------------------------------------
650 * pixbuf drawing (rectangles)
651 *-----------------------------------------------------------------------------
655 * Fills region of pixbuf at x,y over w,h
656 * with colors red (r), green (g), blue (b)
657 * applying alpha (a), use a=255 for solid.
659 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
660 gint x, gint y, gint w, gint h,
661 gint r, gint g, gint b, gint a)
671 pw = gdk_pixbuf_get_width(pb);
672 ph = gdk_pixbuf_get_height(pb);
674 if (x < 0 || x + w > pw) return;
675 if (y < 0 || y + h > ph) return;
677 has_alpha = gdk_pixbuf_get_has_alpha(pb);
678 prs = gdk_pixbuf_get_rowstride(pb);
679 p_pix = gdk_pixbuf_get_pixels(pb);
681 for (i = 0; i < h; i++)
683 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
684 for (j = 0; j < w; j++)
686 *pp = (r * a + *pp * (256-a)) >> 8;
688 *pp = (g * a + *pp * (256-a)) >> 8;
690 *pp = (b * a + *pp * (256-a)) >> 8;
697 #pragma GCC diagnostic push
698 #pragma GCC diagnostic ignored "-Wunused-function"
699 void pixbuf_draw_rect_unused(GdkPixbuf *pb,
700 gint x, gint y, gint w, gint h,
701 gint r, gint g, gint b, gint a,
702 gint left, gint right, gint top, gint bottom)
704 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
706 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
708 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
710 pixbuf_draw_rect_fill(pb, x, y, left, h,
713 #pragma GCC diagnostic pop
715 void pixbuf_set_rect_fill(GdkPixbuf *pb,
716 gint x, gint y, gint w, gint h,
717 gint r, gint g, gint b, gint a)
727 pw = gdk_pixbuf_get_width(pb);
728 ph = gdk_pixbuf_get_height(pb);
730 if (x < 0 || x + w > pw) return;
731 if (y < 0 || y + h > ph) return;
733 has_alpha = gdk_pixbuf_get_has_alpha(pb);
734 prs = gdk_pixbuf_get_rowstride(pb);
735 p_pix = gdk_pixbuf_get_pixels(pb);
737 for (i = 0; i < h; i++)
739 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
740 for (j = 0; j < w; j++)
745 if (has_alpha) { *pp = a; pp++; }
750 void pixbuf_set_rect(GdkPixbuf *pb,
751 gint x, gint y, gint w, gint h,
752 gint r, gint g, gint b, gint a,
753 gint left, gint right, gint top, gint bottom)
755 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
757 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
759 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
761 pixbuf_set_rect_fill(pb, x, y, left, h,
765 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
772 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
773 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
775 buf = gdk_pixbuf_get_pixels(pb);
776 has_alpha = gdk_pixbuf_get_has_alpha(pb);
777 rowstride = gdk_pixbuf_get_rowstride(pb);
779 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
783 if (has_alpha) *p = a;
788 *-----------------------------------------------------------------------------
789 * pixbuf text rendering
790 *-----------------------------------------------------------------------------
793 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
794 GdkPixbuf *dest, gint dx, gint dy,
796 guint8 r, guint8 g, guint8 b, guint8 a)
811 if (!src || !dest) return;
813 sw = gdk_pixbuf_get_width(src);
814 sh = gdk_pixbuf_get_height(src);
816 if (sx < 0 || sx + w > sw) return;
817 if (sy < 0 || sy + h > sh) return;
819 dw = gdk_pixbuf_get_width(dest);
820 dh = gdk_pixbuf_get_height(dest);
822 if (dx < 0 || dx + w > dw) return;
823 if (dy < 0 || dy + h > dh) return;
825 s_alpha = gdk_pixbuf_get_has_alpha(src);
826 d_alpha = gdk_pixbuf_get_has_alpha(dest);
827 srs = gdk_pixbuf_get_rowstride(src);
828 drs = gdk_pixbuf_get_rowstride(dest);
829 s_pix = gdk_pixbuf_get_pixels(src);
830 d_pix = gdk_pixbuf_get_pixels(dest);
832 s_step = (s_alpha) ? 4 : 3;
833 d_step = (d_alpha) ? 4 : 3;
835 for (i = 0; i < h; i++)
837 sp = s_pix + (sy + i) * srs + sx * s_step;
838 dp = d_pix + (dy + i) * drs + dx * d_step;
839 for (j = 0; j < w; j++)
845 asub = a * sp[0] / 255;
846 *dp = (r * asub + *dp * (256-asub)) >> 8;
848 asub = a * sp[1] / 255;
849 *dp = (g * asub + *dp * (256-asub)) >> 8;
851 asub = a * sp[2] / 255;
852 *dp = (b * asub + *dp * (256-asub)) >> 8;
857 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
871 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *,
873 guint8 r, guint8 g, guint8 b, guint8 a)
879 cairo_surface_t *source;
882 pango_layout_get_pixel_size(layout, &w, &h);
883 if (w < 1 || h < 1) return;
885 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
887 cr = cairo_create (source);
888 cairo_set_source_rgb(cr, 0, 0, 0);
889 cairo_rectangle (cr, 0, 0, w, h);
891 cairo_set_source_rgb(cr, 1, 1, 1);
892 pango_cairo_show_layout (cr, layout);
895 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
897 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
899 cairo_image_surface_get_width (source),
900 cairo_image_surface_get_height (source),
901 cairo_image_surface_get_stride (source),
907 dw = gdk_pixbuf_get_width(pixbuf);
908 dh = gdk_pixbuf_get_height(pixbuf);
924 if (x + w > dw) w = dw - x;
925 if (y + h > dh) h = dh - y;
927 pixbuf_copy_font(buffer, sx, sy,
931 g_object_unref(buffer);
932 cairo_surface_destroy(source);
936 *-----------------------------------------------------------------------------
937 * pixbuf drawing (triangle)
938 *-----------------------------------------------------------------------------
941 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
942 gint *rx, gint *ry, gint *rw, gint *rh)
950 tw = MAX(abs(x1 - x2), abs(x2 - x3));
951 tw = MAX(tw, abs(x3 - x1));
952 th = MAX(abs(y1 - y2), abs(y2 - y3));
953 th = MAX(th, abs(y3 - y1));
961 void pixbuf_draw_triangle(GdkPixbuf *pb,
962 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
963 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
964 guint8 r, guint8 g, guint8 b, guint8 a)
976 gdouble slope1, slope2;
977 gint slope1_x, slope1_y;
980 gboolean middle = FALSE;
984 pw = gdk_pixbuf_get_width(pb);
985 ph = gdk_pixbuf_get_height(pb);
987 if (!util_clip_region(0, 0, pw, ph,
988 clip_x, clip_y, clip_w, clip_h,
989 &rx, &ry, &rw, &rh)) return;
991 util_clip_triangle(x1, y1, x2, y2, x3, y3,
994 if (!util_clip_region(rx, ry, rw, rh,
996 &fx1, &fy1, &fw, &fh)) return;
1000 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1001 prs = gdk_pixbuf_get_rowstride(pb);
1002 p_pix = gdk_pixbuf_get_pixels(pb);
1004 p_step = (has_alpha) ? 4 : 3;
1008 t = x1; x1 = x2; x2 = t;
1009 t = y1; y1 = y2; y2 = t;
1013 t = x2; x2 = x3; x3 = t;
1014 t = y2; y2 = y3; y3 = t;
1018 t = x1; x1 = x2; x2 = t;
1019 t = y1; y1 = y2; y2 = t;
1022 slope1 = static_cast<gdouble>(y2 - y1);
1023 if (slope1) slope1 = static_cast<gdouble>(x2 - x1) / slope1;
1026 slope2 = static_cast<gdouble>(y3 - y1);
1027 if (slope2) slope2 = static_cast<gdouble>(x3 - x1) / slope2;
1029 for (y = fy1; y < fy2; y++)
1033 if (!middle && y > y2)
1035 slope1 = static_cast<gdouble>(y3 - y2);
1036 if (slope1) slope1 = static_cast<gdouble>(x3 - x2) / slope1;
1043 xa = slope1_x + (slope1 * (y - slope1_y) + 0.5);
1044 xb = x1 + (slope2 * (y - y1) + 0.5);
1048 t = xa; xa = xb; xb = t;
1051 xa = CLAMP(xa, fx1, fx2);
1052 xb = CLAMP(xb, fx1, fx2);
1054 pp = p_pix + y * prs + xa * p_step;
1058 *pp = (r * a + *pp * (256-a)) >> 8;
1060 *pp = (g * a + *pp * (256-a)) >> 8;
1062 *pp = (b * a + *pp * (256-a)) >> 8;
1064 if (has_alpha) pp++;
1072 *-----------------------------------------------------------------------------
1073 * pixbuf drawing (line)
1074 *-----------------------------------------------------------------------------
1077 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1078 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1079 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1081 gboolean flip = FALSE;
1088 t = x1; x1 = x2; x2 = t;
1089 t = y1; y1 = y2; y2 = t;
1093 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1097 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1101 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1109 slope = (y2 - y1) / d;
1112 y1 = y1 + slope * (clip_x - x1);
1115 if (x2 > clip_x + clip_w)
1117 y2 = y2 + slope * (clip_x + clip_w - x2);
1118 x2 = clip_x + clip_w;
1124 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1130 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1132 t = x1; x1 = x2; x2 = t;
1133 t = y1; y1 = y2; y2 = t;
1142 slope = (x2 - x1) / d;
1145 x1 = x1 + slope * (clip_y - y1);
1148 if (y2 > clip_y + clip_h)
1150 x2 = x2 + slope * (clip_y + clip_h - y2);
1151 y2 = clip_y + clip_h;
1173 void pixbuf_draw_line(GdkPixbuf *pb,
1174 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1175 gint x1, gint y1, gint x2, gint y2,
1176 guint8 r, guint8 g, guint8 b, guint8 a)
1180 gint rx, ry, rw, rh;
1181 gdouble rx1, ry1, rx2, ry2;
1188 gint cx1, cy1, cx2, cy2;
1192 pw = gdk_pixbuf_get_width(pb);
1193 ph = gdk_pixbuf_get_height(pb);
1195 if (!util_clip_region(0, 0, pw, ph,
1196 clip_x, clip_y, clip_w, clip_h,
1197 &rx, &ry, &rw, &rh)) return;
1198 if (!util_clip_line(static_cast<gdouble>(rx), static_cast<gdouble>(ry), static_cast<gdouble>(rw), static_cast<gdouble>(rh),
1199 static_cast<gdouble>(x1), static_cast<gdouble>(y1), static_cast<gdouble>(x2), static_cast<gdouble>(y2),
1200 &rx1, &ry1, &rx2, &ry2)) return;
1207 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1208 prs = gdk_pixbuf_get_rowstride(pb);
1209 p_pix = gdk_pixbuf_get_pixels(pb);
1211 p_step = (has_alpha) ? 4 : 3;
1213 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1218 t = rx1; rx1 = rx2; rx2 = t;
1219 t = ry1; ry1 = ry2; ry2 = t;
1223 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1224 for (x = rx1; x < rx2; x += 1.0)
1226 px = static_cast<gint>(x + 0.5);
1227 py = static_cast<gint>(ry1 + (x - rx1) * slope + 0.5);
1229 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1231 pp = p_pix + py * prs + px * p_step;
1232 *pp = (r * a + *pp * (256-a)) >> 8;
1234 *pp = (g * a + *pp * (256-a)) >> 8;
1236 *pp = (b * a + *pp * (256-a)) >> 8;
1245 t = rx1; rx1 = rx2; rx2 = t;
1246 t = ry1; ry1 = ry2; ry2 = t;
1250 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1251 for (y = ry1; y < ry2; y += 1.0)
1253 px = static_cast<gint>(rx1 + (y - ry1) * slope + 0.5);
1254 py = static_cast<gint>(y + 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;
1270 *-----------------------------------------------------------------------------
1271 * pixbuf drawing (fades and shadows)
1272 *-----------------------------------------------------------------------------
1275 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1276 gint s, gboolean vertical, gint border,
1277 gint x1, gint y1, gint x2, gint y2,
1278 guint8 r, guint8 g, guint8 b, guint8 a)
1285 p_step = (has_alpha) ? 4 : 3;
1286 for (j = y1; j < y2; j++)
1288 pp = p_pix + j * prs + x1 * p_step;
1289 if (!vertical) n = a - a * abs(j - s) / border;
1290 for (i = x1; i < x2; i++)
1292 if (vertical) n = a - a * abs(i - s) / border;
1293 *pp = (r * n + *pp * (256-n)) >> 8;
1295 *pp = (g * n + *pp * (256-n)) >> 8;
1297 *pp = (b * n + *pp * (256-n)) >> 8;
1299 if (has_alpha) pp++;
1304 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1305 gint sx, gint sy, gint border,
1306 gint x1, gint y1, gint x2, gint y2,
1307 guint8, guint8 g, guint8 b, guint8 a)
1313 p_step = (has_alpha) ? 4 : 3;
1314 for (j = y1; j < y2; j++)
1316 pp = p_pix + j * prs + x1 * p_step;
1317 for (i = x1; i < x2; i++)
1322 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1323 n = a - a * r / border;
1324 *pp = (r * n + *pp * (256-n)) >> 8;
1326 *pp = (g * n + *pp * (256-n)) >> 8;
1328 *pp = (b * n + *pp * (256-n)) >> 8;
1330 if (has_alpha) pp++;
1335 void pixbuf_draw_shadow(GdkPixbuf *pb,
1336 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1337 gint x, gint y, gint w, gint h, gint border,
1338 guint8 r, guint8 g, guint8 b, guint8 a)
1342 gint rx, ry, rw, rh;
1343 gint fx, fy, fw, fh;
1348 pw = gdk_pixbuf_get_width(pb);
1349 ph = gdk_pixbuf_get_height(pb);
1351 if (!util_clip_region(0, 0, pw, ph,
1352 clip_x, clip_y, clip_w, clip_h,
1353 &rx, &ry, &rw, &rh)) return;
1355 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1356 prs = gdk_pixbuf_get_rowstride(pb);
1357 p_pix = gdk_pixbuf_get_pixels(pb);
1359 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1361 &fx, &fy, &fw, &fh))
1363 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1366 if (border < 1) return;
1368 if (util_clip_region(x, y + border, border, h - border * 2,
1370 &fx, &fy, &fw, &fh))
1372 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1373 x + border, TRUE, border,
1374 fx, fy, fx + fw, fy + fh,
1377 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1379 &fx, &fy, &fw, &fh))
1381 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1382 x + w - border, TRUE, border,
1383 fx, fy, fx + fw, fy + fh,
1386 if (util_clip_region(x + border, y, w - border * 2, border,
1388 &fx, &fy, &fw, &fh))
1390 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1391 y + border, FALSE, border,
1392 fx, fy, fx + fw, fy + fh,
1395 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1397 &fx, &fy, &fw, &fh))
1399 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1400 y + h - border, FALSE, border,
1401 fx, fy, fx + fw, fy + fh,
1404 if (util_clip_region(x, y, border, border,
1406 &fx, &fy, &fw, &fh))
1408 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1409 x + border, y + border, border,
1410 fx, fy, fx + fw, fy + fh,
1413 if (util_clip_region(x + w - border, y, border, border,
1415 &fx, &fy, &fw, &fh))
1417 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1418 x + w - border, y + border, border,
1419 fx, fy, fx + fw, fy + fh,
1422 if (util_clip_region(x, y + h - border, border, border,
1424 &fx, &fy, &fw, &fh))
1426 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1427 x + border, y + h - border, border,
1428 fx, fy, fx + fw, fy + fh,
1431 if (util_clip_region(x + w - border, y + h - border, border, border,
1433 &fx, &fy, &fw, &fh))
1435 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1436 x + w - border, y + h - border, border,
1437 fx, fy, fx + fw, fy + fh,
1444 *-----------------------------------------------------------------------------
1445 * pixbuf color alterations
1446 *-----------------------------------------------------------------------------
1449 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1450 gint x, gint y, gint w, gint h)
1460 pw = gdk_pixbuf_get_width(pb);
1461 ph = gdk_pixbuf_get_height(pb);
1463 if (x < 0 || x + w > pw) return;
1464 if (y < 0 || y + h > ph) return;
1466 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1467 prs = gdk_pixbuf_get_rowstride(pb);
1468 p_pix = gdk_pixbuf_get_pixels(pb);
1470 for (i = 0; i < h; i++)
1472 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1473 for (j = 0; j < w; j++)
1477 grey = (pp[0] + pp[1] + pp[2]) / 3;
1484 if (has_alpha) pp++;
1490 *-----------------------------------------------------------------------------
1491 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1493 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1503 pw = gdk_pixbuf_get_width(pb);
1504 ph = gdk_pixbuf_get_height(pb);
1506 if (x < 0 || x + w > pw) return;
1507 if (y < 0 || y + h > ph) return;
1509 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1510 prs = gdk_pixbuf_get_rowstride(pb);
1511 p_pix = gdk_pixbuf_get_pixels(pb);
1513 for (i = 0; i < h; i++)
1515 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1516 for (j = 0; j < w; j++)
1518 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1526 if (has_alpha) pp++;
1531 if (has_alpha) pp++;
1538 *-----------------------------------------------------------------------------
1539 * pixbuf ignore alpha
1540 *-----------------------------------------------------------------------------
1542 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1543 gint x, gint y, gint w, gint h)
1553 pw = gdk_pixbuf_get_width(pb);
1554 ph = gdk_pixbuf_get_height(pb);
1556 if (x < 0 || x + w > pw) return;
1557 if (y < 0 || y + h > ph) return;
1559 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1560 if (!has_alpha) return;
1562 prs = gdk_pixbuf_get_rowstride(pb);
1563 p_pix = gdk_pixbuf_get_pixels(pb);
1565 for (i = 0; i < h; i++)
1567 pp = p_pix + (y + i) * prs + (x * 4 );
1568 for (j = 0; j < w; j++)
1575 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */