2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "pixbuf-util.h"
30 #include <glib-object.h>
31 #include <pango/pangocairo.h>
38 #include "main-defines.h"
40 #include "ui-fileops.h"
44 *-----------------------------------------------------------------------------
46 *-----------------------------------------------------------------------------
49 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
51 GError *error = nullptr;
54 if (!pixbuf || !filename) return FALSE;
56 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
57 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
61 log_printf("Error saving png file: %s\n", error->message);
69 *-----------------------------------------------------------------------------
71 *-----------------------------------------------------------------------------
74 #pragma GCC diagnostic push
75 #pragma GCC diagnostic ignored "-Wunused-function"
76 gboolean pixbuf_to_file_as_jpg_unused(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
78 GError *error = nullptr;
82 if (!pixbuf || !filename) return FALSE;
84 if (quality == -1) quality = 75;
85 if (quality < 1 || quality > 100)
87 log_printf("Jpeg not saved, invalid quality %d\n", quality);
91 qbuf = g_strdup_printf("%d", quality);
92 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
97 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
103 #pragma GCC diagnostic pop
106 *-----------------------------------------------------------------------------
108 *-----------------------------------------------------------------------------
117 static PixbufInline inline_pixbuf_data[] = {
118 { PIXBUF_INLINE_ARCHIVE, "gq-icon-archive-file" },
119 { PIXBUF_INLINE_BROKEN, "gq-icon-broken" },
120 { PIXBUF_INLINE_COLLECTION, "gq-icon-collection" },
121 { PIXBUF_INLINE_ICON_180, "gq-icon-rotate-180" },
122 { PIXBUF_INLINE_ICON_BOOK, "gq-icon-book" },
123 { PIXBUF_INLINE_ICON_CONFIG, "gq-icon-config" },
124 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, "gq-icon-draw-rectangle" },
125 { PIXBUF_INLINE_ICON_EXIF, "gq-icon-exif" },
126 { PIXBUF_INLINE_ICON_EXPOSURE, "gq-icon-exposure" },
127 { PIXBUF_INLINE_ICON_FLOAT, "gq-icon-float" },
128 { PIXBUF_INLINE_ICON, "gqview-icon" },
129 { PIXBUF_INLINE_ICON_GRAYSCALE, "gq-icon-grayscale" },
130 { PIXBUF_INLINE_ICON_HEIF, "gq-icon-heic" },
131 { PIXBUF_INLINE_ICON_HIDETOOLS, "gq-icon-hidetools" },
132 { PIXBUF_INLINE_ICON_MAINTENANCE, "gq-icon-maintenance" },
133 { PIXBUF_INLINE_ICON_MARKS, "gq-icon-marks" },
134 { PIXBUF_INLINE_ICON_MOVE, "gq-icon-move" },
135 { PIXBUF_INLINE_ICON_ORIGINAL, "gq-icon-original" },
136 { PIXBUF_INLINE_ICON_PANORAMA, "gq-icon-panorama" },
137 { PIXBUF_INLINE_ICON_PDF, "gq-icon-pdf" },
138 { PIXBUF_INLINE_ICON_PROPERTIES, "gq-icon-properties" },
139 { PIXBUF_INLINE_ICON_RENAME, "gq-icon-rename" },
140 { PIXBUF_INLINE_ICON_SELECT_ALL, "gq-icon-select-all" },
141 { PIXBUF_INLINE_ICON_SELECT_INVERT, "gq-icon-select-invert" },
142 { PIXBUF_INLINE_ICON_SELECT_NONE, "gq-icon-select-none" },
143 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, "gq-icon-select-rectangle" },
144 { PIXBUF_INLINE_ICON_SORT, "gq-icon-sort" },
145 { PIXBUF_INLINE_ICON_THUMB, "gq-icon-thumb" },
146 { PIXBUF_INLINE_ICON_TOOLS, "gq-icon-tools" },
147 { PIXBUF_INLINE_ICON_VIEW, "gq-icon-view" },
148 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, "gq-icon-zoomfillhor" },
149 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, "gq-icon-zoomfillvert" },
150 { PIXBUF_INLINE_LOGO, "geeqie-logo" },
151 { PIXBUF_INLINE_METADATA, "gq-icon-metadata" },
152 { PIXBUF_INLINE_SCROLLER, "gq-scroller" },
153 { PIXBUF_INLINE_SPLIT_PANE_SYNC, "gq-icon-split-pane-sync" },
154 { PIXBUF_INLINE_UNKNOWN, "gq-icon-unknown" },
155 { PIXBUF_INLINE_VIDEO, "gq-icon-video" },
159 GdkPixbuf *pixbuf_inline(const gchar *key)
161 gboolean dark = FALSE;
162 gchar *file_name = nullptr;
165 GdkPixbuf *icon_pixbuf;
166 GError *error = nullptr;
167 GInputStream *in_stream;
169 GtkSettings *settings;
171 if (!key) return nullptr;
173 settings = gtk_settings_get_default();
174 g_object_get(settings, "gtk-theme-name", &theme_name, nullptr);
175 dark = g_str_has_suffix(theme_name, "dark");
179 while (inline_pixbuf_data[i].key)
181 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
183 file_name = g_strconcat(inline_pixbuf_data[i].data, dark ? "-dark" : "", ".png", nullptr);
184 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
187 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
195 file_name = g_strconcat(inline_pixbuf_data[i].data, ".png", nullptr);
196 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
199 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
205 log_printf("warning: inline pixbuf error: %s", error->message);
207 g_object_unref(in_stream);
211 icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, nullptr, &error);
212 g_object_unref(in_stream);
216 log_printf("warning: inline pixbuf error: %s", error->message);
226 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
231 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
233 static GtkIconFactory *icon_factory = nullptr;
234 GtkIconSet *icon_set;
238 icon_factory = gtk_icon_factory_new();
239 gtk_icon_factory_add_default(icon_factory);
242 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
243 gtk_icon_factory_add(icon_factory, key, icon_set);
247 void pixbuf_inline_register_stock_icons()
252 while (inline_pixbuf_data[i].key)
254 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
259 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
261 GtkIconTheme *icon_theme;
263 GError *error = nullptr;
265 icon_theme = gtk_icon_theme_get_default();
267 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
269 pixbuf = gtk_icon_theme_load_icon(icon_theme,
270 icon, /* icon name */
272 static_cast<GtkIconLookupFlags>(0), /* flags */
278 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
283 if (strchr(icon, '.'))
285 /* try again without extension */
286 gchar *icon2 = remove_extension_from_path(icon);
287 pixbuf = gtk_icon_theme_load_icon(icon_theme,
288 icon2, /* icon name */
290 static_cast<GtkIconLookupFlags>(0), /* flags */
294 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
298 /* try as an absolute path */
299 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
302 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
310 if (!pixbuf) return FALSE;
312 register_stock_icon(key, pixbuf);
316 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
317 gint old_w, gint old_h,
318 gint *new_w, gint *new_h)
320 if ((static_cast<gdouble>(req_w) / old_w) < (static_cast<gdouble>(req_h) / old_h))
323 *new_h = static_cast<gdouble>(*new_w) / old_w * old_h;
324 if (*new_h < 1) *new_h = 1;
329 *new_w = static_cast<gdouble>(*new_h) / old_h * old_w;
330 if (*new_w < 1) *new_w = 1;
333 return (*new_w != old_w || *new_h != old_h);
336 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
340 switch (fd->format_class)
342 case FORMAT_CLASS_UNKNOWN:
343 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
345 case FORMAT_CLASS_META:
346 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
348 case FORMAT_CLASS_VIDEO:
349 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
351 case FORMAT_CLASS_COLLECTION:
352 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
354 case FORMAT_CLASS_DOCUMENT:
355 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
357 case FORMAT_CLASS_ARCHIVE:
358 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
361 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
364 if (requested_width && requested_height)
366 gint w = gdk_pixbuf_get_width(pixbuf);
367 gint h = gdk_pixbuf_get_height(pixbuf);
369 if (w > requested_width || h > requested_height)
374 if (pixbuf_scale_aspect(requested_width, requested_height,
380 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
381 g_object_unref(G_OBJECT(tmp));
390 *-----------------------------------------------------------------------------
392 *-----------------------------------------------------------------------------
395 gboolean util_clip_region(gint x, gint y, gint w, gint h,
396 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
397 gint *rx, gint *ry, gint *rw, gint *rh)
399 if (clip_x + clip_w <= x ||
401 clip_y + clip_h <= y ||
407 *rx = MAX(x, clip_x);
408 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
410 *ry = MAX(y, clip_y);
411 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
417 *-----------------------------------------------------------------------------
419 *-----------------------------------------------------------------------------
422 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
423 guchar *dest, gint dest_row_stride, gint w, gint h,
424 gint bytes_per_pixel, gboolean counter_clockwise)
431 for (i = 0; i < h; i++)
433 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
434 for (j = 0; j < w; j++)
436 if (counter_clockwise)
438 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
442 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
444 *(dp++) = *(sp++); /* r */
445 *(dp++) = *(sp++); /* g */
446 *(dp++) = *(sp++); /* b */
447 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
453 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
454 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
460 for (i = 0; i < h; i++)
462 sp = src + (i * src_row_stride);
463 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
464 memcpy(dp, sp, w * bytes_per_pixel);
469 ROTATE_BUFFER_WIDTH = 48,
470 ROTATE_BUFFER_HEIGHT = 48
474 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
477 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
498 if (!src) return nullptr;
500 sw = gdk_pixbuf_get_width(src);
501 sh = gdk_pixbuf_get_height(src);
502 has_alpha = gdk_pixbuf_get_has_alpha(src);
503 srs = gdk_pixbuf_get_rowstride(src);
504 s_pix = gdk_pixbuf_get_pixels(src);
508 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
509 drs = gdk_pixbuf_get_rowstride(dest);
510 d_pix = gdk_pixbuf_get_pixels(dest);
512 a = (has_alpha ? 4 : 3);
514 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
515 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
516 b_pix = gdk_pixbuf_get_pixels(buffer);
517 brs = gdk_pixbuf_get_rowstride(buffer);
519 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
521 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
522 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
527 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
528 pixbuf_copy_block_rotate(s_pix, srs, j, i,
530 a, counter_clockwise);
532 if (counter_clockwise)
542 pixbuf_copy_block(b_pix, brs, w, h,
543 d_pix, drs, x, y, a);
547 g_object_unref(buffer);
550 /* this is the simple version of rotation (roughly 2-4x slower) */
552 for (i = 0; i < sh; i++)
554 sp = s_pix + (i * srs);
555 for (j = 0; j < sw; j++)
557 if (counter_clockwise)
559 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
563 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
566 *(dp++) = *(sp++); /* r */
567 *(dp++) = *(sp++); /* g */
568 *(dp++) = *(sp++); /* b */
569 if (has_alpha) *(dp) = *(sp++); /* a */
578 * Returns a copy of pixbuf mirrored and or flipped.
579 * TO do a 180 degree rotations set both mirror and flipped TRUE
580 * if mirror and flip are FALSE, result is a simple copy.
582 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
598 if (!src) return nullptr;
600 w = gdk_pixbuf_get_width(src);
601 h = gdk_pixbuf_get_height(src);
602 has_alpha = gdk_pixbuf_get_has_alpha(src);
603 srs = gdk_pixbuf_get_rowstride(src);
604 s_pix = gdk_pixbuf_get_pixels(src);
606 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
607 drs = gdk_pixbuf_get_rowstride(dest);
608 d_pix = gdk_pixbuf_get_pixels(dest);
610 a = has_alpha ? 4 : 3;
612 for (i = 0; i < h; i++)
614 sp = s_pix + (i * srs);
617 dp = d_pix + ((h - i - 1) * drs);
621 dp = d_pix + (i * drs);
626 for (j = 0; j < w; j++)
628 *(dp++) = *(sp++); /* r */
629 *(dp++) = *(sp++); /* g */
630 *(dp++) = *(sp++); /* b */
631 if (has_alpha) *(dp) = *(sp++); /* a */
637 for (j = 0; j < w; j++)
639 *(dp++) = *(sp++); /* r */
640 *(dp++) = *(sp++); /* g */
641 *(dp++) = *(sp++); /* b */
642 if (has_alpha) *(dp++) = *(sp++); /* a */
650 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
653 GdkPixbuf *tmp = nullptr;
657 case EXIF_ORIENTATION_TOP_LEFT:
658 dest = gdk_pixbuf_copy(pixbuf);
660 case EXIF_ORIENTATION_TOP_RIGHT:
662 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
664 case EXIF_ORIENTATION_BOTTOM_RIGHT:
666 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
668 case EXIF_ORIENTATION_BOTTOM_LEFT:
670 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
672 case EXIF_ORIENTATION_LEFT_TOP:
673 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
674 dest = pixbuf_copy_rotate_90(tmp, FALSE);
676 case EXIF_ORIENTATION_RIGHT_TOP:
677 /* rotated -90 (270) */
678 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
680 case EXIF_ORIENTATION_RIGHT_BOTTOM:
681 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
682 dest = pixbuf_copy_rotate_90(tmp, TRUE);
684 case EXIF_ORIENTATION_LEFT_BOTTOM:
686 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
689 dest = gdk_pixbuf_copy(pixbuf);
692 if (tmp) g_object_unref(tmp);
699 *-----------------------------------------------------------------------------
700 * pixbuf drawing (rectangles)
701 *-----------------------------------------------------------------------------
705 * Fills region of pixbuf at x,y over w,h
706 * with colors red (r), green (g), blue (b)
707 * applying alpha (a), use a=255 for solid.
709 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
710 gint x, gint y, gint w, gint h,
711 gint r, gint g, gint b, gint a)
724 pw = gdk_pixbuf_get_width(pb);
725 ph = gdk_pixbuf_get_height(pb);
727 if (x < 0 || x + w > pw) return;
728 if (y < 0 || y + h > ph) return;
730 has_alpha = gdk_pixbuf_get_has_alpha(pb);
731 prs = gdk_pixbuf_get_rowstride(pb);
732 p_pix = gdk_pixbuf_get_pixels(pb);
734 for (i = 0; i < h; i++)
736 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
737 for (j = 0; j < w; j++)
739 *pp = (r * a + *pp * (256-a)) >> 8;
741 *pp = (g * a + *pp * (256-a)) >> 8;
743 *pp = (b * a + *pp * (256-a)) >> 8;
750 #pragma GCC diagnostic push
751 #pragma GCC diagnostic ignored "-Wunused-function"
752 void pixbuf_draw_rect_unused(GdkPixbuf *pb,
753 gint x, gint y, gint w, gint h,
754 gint r, gint g, gint b, gint a,
755 gint left, gint right, gint top, gint bottom)
757 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
759 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
761 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
763 pixbuf_draw_rect_fill(pb, x, y, left, h,
766 #pragma GCC diagnostic pop
768 void pixbuf_set_rect_fill(GdkPixbuf *pb,
769 gint x, gint y, gint w, gint h,
770 gint r, gint g, gint b, gint a)
783 pw = gdk_pixbuf_get_width(pb);
784 ph = gdk_pixbuf_get_height(pb);
786 if (x < 0 || x + w > pw) return;
787 if (y < 0 || y + h > ph) return;
789 has_alpha = gdk_pixbuf_get_has_alpha(pb);
790 prs = gdk_pixbuf_get_rowstride(pb);
791 p_pix = gdk_pixbuf_get_pixels(pb);
793 for (i = 0; i < h; i++)
795 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
796 for (j = 0; j < w; j++)
801 if (has_alpha) { *pp = a; pp++; }
806 void pixbuf_set_rect(GdkPixbuf *pb,
807 gint x, gint y, gint w, gint h,
808 gint r, gint g, gint b, gint a,
809 gint left, gint right, gint top, gint bottom)
811 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
813 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
815 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
817 pixbuf_set_rect_fill(pb, x, y, left, h,
821 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
828 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
829 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
831 buf = gdk_pixbuf_get_pixels(pb);
832 has_alpha = gdk_pixbuf_get_has_alpha(pb);
833 rowstride = gdk_pixbuf_get_rowstride(pb);
835 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
839 if (has_alpha) *p = a;
844 *-----------------------------------------------------------------------------
845 * pixbuf text rendering
846 *-----------------------------------------------------------------------------
849 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
850 GdkPixbuf *dest, gint dx, gint dy,
852 guint8 r, guint8 g, guint8 b, guint8 a)
872 if (!src || !dest) return;
874 sw = gdk_pixbuf_get_width(src);
875 sh = gdk_pixbuf_get_height(src);
877 if (sx < 0 || sx + w > sw) return;
878 if (sy < 0 || sy + h > sh) return;
880 dw = gdk_pixbuf_get_width(dest);
881 dh = gdk_pixbuf_get_height(dest);
883 if (dx < 0 || dx + w > dw) return;
884 if (dy < 0 || dy + h > dh) return;
886 s_alpha = gdk_pixbuf_get_has_alpha(src);
887 d_alpha = gdk_pixbuf_get_has_alpha(dest);
888 srs = gdk_pixbuf_get_rowstride(src);
889 drs = gdk_pixbuf_get_rowstride(dest);
890 s_pix = gdk_pixbuf_get_pixels(src);
891 d_pix = gdk_pixbuf_get_pixels(dest);
893 s_step = (s_alpha) ? 4 : 3;
894 d_step = (d_alpha) ? 4 : 3;
896 for (i = 0; i < h; i++)
898 sp = s_pix + (sy + i) * srs + sx * s_step;
899 dp = d_pix + (dy + i) * drs + dx * d_step;
900 for (j = 0; j < w; j++)
906 asub = a * sp[0] / 255;
907 *dp = (r * asub + *dp * (256-asub)) >> 8;
909 asub = a * sp[1] / 255;
910 *dp = (g * asub + *dp * (256-asub)) >> 8;
912 asub = a * sp[2] / 255;
913 *dp = (b * asub + *dp * (256-asub)) >> 8;
918 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
932 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *,
934 guint8 r, guint8 g, guint8 b, guint8 a)
943 cairo_surface_t *source;
946 pango_layout_get_pixel_size(layout, &w, &h);
947 if (w < 1 || h < 1) return;
949 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
951 cr = cairo_create (source);
952 cairo_set_source_rgb(cr, 0, 0, 0);
953 cairo_rectangle (cr, 0, 0, w, h);
955 cairo_set_source_rgb(cr, 1, 1, 1);
956 pango_cairo_show_layout (cr, layout);
959 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
961 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
963 cairo_image_surface_get_width (source),
964 cairo_image_surface_get_height (source),
965 cairo_image_surface_get_stride (source),
971 dw = gdk_pixbuf_get_width(pixbuf);
972 dh = gdk_pixbuf_get_height(pixbuf);
988 if (x + w > dw) w = dw - x;
989 if (y + h > dh) h = dh - y;
991 pixbuf_copy_font(buffer, sx, sy,
995 g_object_unref(buffer);
996 cairo_surface_destroy(source);
1000 *-----------------------------------------------------------------------------
1001 * pixbuf drawing (triangle)
1002 *-----------------------------------------------------------------------------
1005 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
1006 gint *rx, gint *ry, gint *rw, gint *rh)
1017 tw = MAX(abs(x1 - x2), abs(x2 - x3));
1018 tw = MAX(tw, abs(x3 - x1));
1019 th = MAX(abs(y1 - y2), abs(y2 - y3));
1020 th = MAX(th, abs(y3 - y1));
1028 void pixbuf_draw_triangle(GdkPixbuf *pb,
1029 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1030 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
1031 guint8 r, guint8 g, guint8 b, guint8 a)
1060 gboolean middle = FALSE;
1064 pw = gdk_pixbuf_get_width(pb);
1065 ph = gdk_pixbuf_get_height(pb);
1067 if (!util_clip_region(0, 0, pw, ph,
1068 clip_x, clip_y, clip_w, clip_h,
1069 &rx, &ry, &rw, &rh)) return;
1071 util_clip_triangle(x1, y1, x2, y2, x3, y3,
1072 &tx, &ty, &tw, &th);
1074 if (!util_clip_region(rx, ry, rw, rh,
1076 &fx1, &fy1, &fw, &fh)) return;
1080 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1081 prs = gdk_pixbuf_get_rowstride(pb);
1082 p_pix = gdk_pixbuf_get_pixels(pb);
1084 p_step = (has_alpha) ? 4 : 3;
1088 t = x1; x1 = x2; x2 = t;
1089 t = y1; y1 = y2; y2 = t;
1093 t = x2; x2 = x3; x3 = t;
1094 t = y2; y2 = y3; y3 = t;
1098 t = x1; x1 = x2; x2 = t;
1099 t = y1; y1 = y2; y2 = t;
1102 slope1 = static_cast<gdouble>(y2 - y1);
1103 if (slope1) slope1 = static_cast<gdouble>(x2 - x1) / slope1;
1106 slope2 = static_cast<gdouble>(y3 - y1);
1107 if (slope2) slope2 = static_cast<gdouble>(x3 - x1) / slope2;
1109 for (y = fy1; y < fy2; y++)
1114 if (!middle && y > y2)
1116 slope1 = static_cast<gdouble>(y3 - y2);
1117 if (slope1) slope1 = static_cast<gdouble>(x3 - x2) / slope1;
1124 xa = slope1_x + (slope1 * (y - slope1_y) + 0.5);
1125 xb = x1 + (slope2 * (y - y1) + 0.5);
1129 t = xa; xa = xb; xb = t;
1132 xa = CLAMP(xa, fx1, fx2);
1133 xb = CLAMP(xb, fx1, fx2);
1135 pp = p_pix + y * prs + xa * p_step;
1139 *pp = (r * a + *pp * (256-a)) >> 8;
1141 *pp = (g * a + *pp * (256-a)) >> 8;
1143 *pp = (b * a + *pp * (256-a)) >> 8;
1145 if (has_alpha) pp++;
1153 *-----------------------------------------------------------------------------
1154 * pixbuf drawing (line)
1155 *-----------------------------------------------------------------------------
1158 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1159 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1160 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1162 gboolean flip = FALSE;
1169 t = x1; x1 = x2; x2 = t;
1170 t = y1; y1 = y2; y2 = t;
1174 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1178 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1182 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1190 slope = (y2 - y1) / d;
1193 y1 = y1 + slope * (clip_x - x1);
1196 if (x2 > clip_x + clip_w)
1198 y2 = y2 + slope * (clip_x + clip_w - x2);
1199 x2 = clip_x + clip_w;
1205 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1211 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1213 t = x1; x1 = x2; x2 = t;
1214 t = y1; y1 = y2; y2 = t;
1223 slope = (x2 - x1) / d;
1226 x1 = x1 + slope * (clip_y - y1);
1229 if (y2 > clip_y + clip_h)
1231 x2 = x2 + slope * (clip_y + clip_h - y2);
1232 y2 = clip_y + clip_h;
1254 void pixbuf_draw_line(GdkPixbuf *pb,
1255 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1256 gint x1, gint y1, gint x2, gint y2,
1257 guint8 r, guint8 g, guint8 b, guint8 a)
1286 pw = gdk_pixbuf_get_width(pb);
1287 ph = gdk_pixbuf_get_height(pb);
1289 if (!util_clip_region(0, 0, pw, ph,
1290 clip_x, clip_y, clip_w, clip_h,
1291 &rx, &ry, &rw, &rh)) return;
1292 if (!util_clip_line(static_cast<gdouble>(rx), static_cast<gdouble>(ry), static_cast<gdouble>(rw), static_cast<gdouble>(rh),
1293 static_cast<gdouble>(x1), static_cast<gdouble>(y1), static_cast<gdouble>(x2), static_cast<gdouble>(y2),
1294 &rx1, &ry1, &rx2, &ry2)) return;
1301 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1302 prs = gdk_pixbuf_get_rowstride(pb);
1303 p_pix = gdk_pixbuf_get_pixels(pb);
1305 p_step = (has_alpha) ? 4 : 3;
1307 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1312 t = rx1; rx1 = rx2; rx2 = t;
1313 t = ry1; ry1 = ry2; ry2 = t;
1317 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1318 for (x = rx1; x < rx2; x += 1.0)
1320 px = static_cast<gint>(x + 0.5);
1321 py = static_cast<gint>(ry1 + (x - rx1) * slope + 0.5);
1323 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1325 pp = p_pix + py * prs + px * p_step;
1326 *pp = (r * a + *pp * (256-a)) >> 8;
1328 *pp = (g * a + *pp * (256-a)) >> 8;
1330 *pp = (b * a + *pp * (256-a)) >> 8;
1339 t = rx1; rx1 = rx2; rx2 = t;
1340 t = ry1; ry1 = ry2; ry2 = t;
1344 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1345 for (y = ry1; y < ry2; y += 1.0)
1347 px = static_cast<gint>(rx1 + (y - ry1) * slope + 0.5);
1348 py = static_cast<gint>(y + 0.5);
1350 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1352 pp = p_pix + py * prs + px * p_step;
1353 *pp = (r * a + *pp * (256-a)) >> 8;
1355 *pp = (g * a + *pp * (256-a)) >> 8;
1357 *pp = (b * a + *pp * (256-a)) >> 8;
1364 *-----------------------------------------------------------------------------
1365 * pixbuf drawing (fades and shadows)
1366 *-----------------------------------------------------------------------------
1369 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1370 gint s, gboolean vertical, gint border,
1371 gint x1, gint y1, gint x2, gint y2,
1372 guint8 r, guint8 g, guint8 b, guint8 a)
1380 p_step = (has_alpha) ? 4 : 3;
1381 for (j = y1; j < y2; j++)
1383 pp = p_pix + j * prs + x1 * p_step;
1384 if (!vertical) n = a - a * abs(j - s) / border;
1385 for (i = x1; i < x2; i++)
1387 if (vertical) n = a - a * abs(i - s) / border;
1388 *pp = (r * n + *pp * (256-n)) >> 8;
1390 *pp = (g * n + *pp * (256-n)) >> 8;
1392 *pp = (b * n + *pp * (256-n)) >> 8;
1394 if (has_alpha) pp++;
1399 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1400 gint sx, gint sy, gint border,
1401 gint x1, gint y1, gint x2, gint y2,
1402 guint8, guint8 g, guint8 b, guint8 a)
1409 p_step = (has_alpha) ? 4 : 3;
1410 for (j = y1; j < y2; j++)
1412 pp = p_pix + j * prs + x1 * p_step;
1413 for (i = x1; i < x2; i++)
1418 r = MIN(border, (gint)hypot(i - sx, j - sy));
1419 n = a - a * r / border;
1420 *pp = (r * n + *pp * (256-n)) >> 8;
1422 *pp = (g * n + *pp * (256-n)) >> 8;
1424 *pp = (b * n + *pp * (256-n)) >> 8;
1426 if (has_alpha) pp++;
1431 void pixbuf_draw_shadow(GdkPixbuf *pb,
1432 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1433 gint x, gint y, gint w, gint h, gint border,
1434 guint8 r, guint8 g, guint8 b, guint8 a)
1452 pw = gdk_pixbuf_get_width(pb);
1453 ph = gdk_pixbuf_get_height(pb);
1455 if (!util_clip_region(0, 0, pw, ph,
1456 clip_x, clip_y, clip_w, clip_h,
1457 &rx, &ry, &rw, &rh)) return;
1459 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1460 prs = gdk_pixbuf_get_rowstride(pb);
1461 p_pix = gdk_pixbuf_get_pixels(pb);
1463 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1465 &fx, &fy, &fw, &fh))
1467 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1470 if (border < 1) return;
1472 if (util_clip_region(x, y + border, border, h - border * 2,
1474 &fx, &fy, &fw, &fh))
1476 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1477 x + border, TRUE, border,
1478 fx, fy, fx + fw, fy + fh,
1481 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1483 &fx, &fy, &fw, &fh))
1485 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1486 x + w - border, TRUE, border,
1487 fx, fy, fx + fw, fy + fh,
1490 if (util_clip_region(x + border, y, w - border * 2, border,
1492 &fx, &fy, &fw, &fh))
1494 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1495 y + border, FALSE, border,
1496 fx, fy, fx + fw, fy + fh,
1499 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1501 &fx, &fy, &fw, &fh))
1503 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1504 y + h - border, FALSE, border,
1505 fx, fy, fx + fw, fy + fh,
1508 if (util_clip_region(x, y, border, border,
1510 &fx, &fy, &fw, &fh))
1512 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1513 x + border, y + border, border,
1514 fx, fy, fx + fw, fy + fh,
1517 if (util_clip_region(x + w - border, y, border, border,
1519 &fx, &fy, &fw, &fh))
1521 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1522 x + w - border, y + border, border,
1523 fx, fy, fx + fw, fy + fh,
1526 if (util_clip_region(x, y + h - border, border, border,
1528 &fx, &fy, &fw, &fh))
1530 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1531 x + border, y + h - border, border,
1532 fx, fy, fx + fw, fy + fh,
1535 if (util_clip_region(x + w - border, y + h - border, border, border,
1537 &fx, &fy, &fw, &fh))
1539 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1540 x + w - border, y + h - border, border,
1541 fx, fy, fx + fw, fy + fh,
1548 *-----------------------------------------------------------------------------
1549 * pixbuf color alterations
1550 *-----------------------------------------------------------------------------
1553 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1554 gint x, gint y, gint w, gint h)
1567 pw = gdk_pixbuf_get_width(pb);
1568 ph = gdk_pixbuf_get_height(pb);
1570 if (x < 0 || x + w > pw) return;
1571 if (y < 0 || y + h > ph) return;
1573 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1574 prs = gdk_pixbuf_get_rowstride(pb);
1575 p_pix = gdk_pixbuf_get_pixels(pb);
1577 for (i = 0; i < h; i++)
1579 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1580 for (j = 0; j < w; j++)
1584 grey = (pp[0] + pp[1] + pp[2]) / 3;
1591 if (has_alpha) pp++;
1597 *-----------------------------------------------------------------------------
1598 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1600 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1613 pw = gdk_pixbuf_get_width(pb);
1614 ph = gdk_pixbuf_get_height(pb);
1616 if (x < 0 || x + w > pw) return;
1617 if (y < 0 || y + h > ph) return;
1619 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1620 prs = gdk_pixbuf_get_rowstride(pb);
1621 p_pix = gdk_pixbuf_get_pixels(pb);
1623 for (i = 0; i < h; i++)
1625 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1626 for (j = 0; j < w; j++)
1628 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1636 if (has_alpha) pp++;
1641 if (has_alpha) pp++;
1648 *-----------------------------------------------------------------------------
1649 * pixbuf ignore alpha
1650 *-----------------------------------------------------------------------------
1652 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1653 gint x, gint y, gint w, gint h)
1666 pw = gdk_pixbuf_get_width(pb);
1667 ph = gdk_pixbuf_get_height(pb);
1669 if (x < 0 || x + w > pw) return;
1670 if (y < 0 || y + h > ph) return;
1672 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1673 if (!has_alpha) return;
1675 prs = gdk_pixbuf_get_rowstride(pb);
1676 p_pix = gdk_pixbuf_get_pixels(pb);
1678 for (i = 0; i < h; i++)
1680 pp = p_pix + (y + i) * prs + (x * 4 );
1681 for (j = 0; j < w; j++)
1688 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */