2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "pixbuf-util.h"
27 #include "ui-fileops.h"
33 *-----------------------------------------------------------------------------
35 *-----------------------------------------------------------------------------
38 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
40 GError *error = nullptr;
43 if (!pixbuf || !filename) return FALSE;
45 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
46 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
50 log_printf("Error saving png file: %s\n", error->message);
58 *-----------------------------------------------------------------------------
60 *-----------------------------------------------------------------------------
63 #pragma GCC diagnostic push
64 #pragma GCC diagnostic ignored "-Wunused-function"
65 gboolean pixbuf_to_file_as_jpg_unused(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
71 if (!pixbuf || !filename) return FALSE;
73 if (quality == -1) quality = 75;
74 if (quality < 1 || quality > 100)
76 log_printf("Jpeg not saved, invalid quality %d\n", quality);
80 qbuf = g_strdup_printf("%d", quality);
81 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
86 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
92 #pragma GCC diagnostic pop
95 *-----------------------------------------------------------------------------
97 *-----------------------------------------------------------------------------
106 static PixbufInline inline_pixbuf_data[] = {
107 { PIXBUF_INLINE_ARCHIVE, "gq-icon-archive-file" },
108 { PIXBUF_INLINE_BROKEN, "gq-icon-broken" },
109 { PIXBUF_INLINE_COLLECTION, "gq-icon-collection" },
110 { PIXBUF_INLINE_ICON_180, "gq-icon-rotate-180" },
111 { PIXBUF_INLINE_ICON_BOOK, "gq-icon-book" },
112 { PIXBUF_INLINE_ICON_CONFIG, "gq-icon-config" },
113 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, "gq-icon-draw-rectangle" },
114 { PIXBUF_INLINE_ICON_EXIF, "gq-icon-exif" },
115 { PIXBUF_INLINE_ICON_EXPOSURE, "gq-icon-exposure" },
116 { PIXBUF_INLINE_ICON_FLOAT, "gq-icon-float" },
117 { PIXBUF_INLINE_ICON, "gqview-icon" },
118 { PIXBUF_INLINE_ICON_GRAYSCALE, "gq-icon-grayscale" },
119 { PIXBUF_INLINE_ICON_HEIF, "gq-icon-heic" },
120 { PIXBUF_INLINE_ICON_HIDETOOLS, "gq-icon-hidetools" },
121 { PIXBUF_INLINE_ICON_MAINTENANCE, "gq-icon-maintenance" },
122 { PIXBUF_INLINE_ICON_MARKS, "gq-icon-marks" },
123 { PIXBUF_INLINE_ICON_MOVE, "gq-icon-move" },
124 { PIXBUF_INLINE_ICON_ORIGINAL, "gq-icon-original" },
125 { PIXBUF_INLINE_ICON_PANORAMA, "gq-icon-panorama" },
126 { PIXBUF_INLINE_ICON_PDF, "gq-icon-pdf" },
127 { PIXBUF_INLINE_ICON_RENAME, "gq-icon-rename" },
128 { PIXBUF_INLINE_ICON_SELECT_ALL, "gq-icon-select-all" },
129 { PIXBUF_INLINE_ICON_SELECT_INVERT, "gq-icon-select-invert" },
130 { PIXBUF_INLINE_ICON_SELECT_NONE, "gq-icon-select-none" },
131 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, "gq-icon-select-rectangle" },
132 { PIXBUF_INLINE_ICON_SORT, "gq-icon-sort" },
133 { PIXBUF_INLINE_ICON_THUMB, "gq-icon-thumb" },
134 { PIXBUF_INLINE_ICON_TOOLS, "gq-icon-tools" },
135 { PIXBUF_INLINE_ICON_VIEW, "gq-icon-view" },
136 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, "gq-icon-zoomfillhor" },
137 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, "gq-icon-zoomfillvert" },
138 { PIXBUF_INLINE_LOGO, "geeqie-logo" },
139 { PIXBUF_INLINE_METADATA, "gq-icon-metadata" },
140 { PIXBUF_INLINE_SCROLLER, "gq-scroller" },
141 { PIXBUF_INLINE_SPLIT_PANE_SYNC, "gq-icon-split-pane-sync" },
142 { PIXBUF_INLINE_UNKNOWN, "gq-icon-unknown" },
143 { PIXBUF_INLINE_VIDEO, "gq-icon-video" },
147 GdkPixbuf *pixbuf_inline(const gchar *key)
149 gboolean dark = FALSE;
150 gchar *file_name = nullptr;
153 GdkPixbuf *icon_pixbuf;
154 GError *error = nullptr;
155 GInputStream *in_stream;
157 GtkSettings *settings;
159 if (!key) return nullptr;
161 settings = gtk_settings_get_default();
162 g_object_get(settings, "gtk-theme-name", &theme_name, nullptr);
163 dark = g_str_has_suffix(theme_name, "dark");
167 while (inline_pixbuf_data[i].key)
169 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
171 file_name = g_strconcat(inline_pixbuf_data[i].data, dark ? "-dark" : "", ".png", nullptr);
172 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
175 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
183 file_name = g_strconcat(inline_pixbuf_data[i].data, ".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);
193 log_printf("warning: inline pixbuf error: %s", error->message);
195 g_object_unref(in_stream);
199 icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, nullptr, &error);
200 g_object_unref(in_stream);
204 log_printf("warning: inline pixbuf error: %s", error->message);
214 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
219 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
221 static GtkIconFactory *icon_factory = nullptr;
222 GtkIconSet *icon_set;
226 icon_factory = gtk_icon_factory_new();
227 gtk_icon_factory_add_default(icon_factory);
230 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
231 gtk_icon_factory_add(icon_factory, key, icon_set);
235 void pixbuf_inline_register_stock_icons()
240 while (inline_pixbuf_data[i].key)
242 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
247 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
249 GtkIconTheme *icon_theme;
251 GError *error = nullptr;
253 icon_theme = gtk_icon_theme_get_default();
255 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
257 pixbuf = gtk_icon_theme_load_icon(icon_theme,
258 icon, /* icon name */
260 static_cast<GtkIconLookupFlags>(0), /* flags */
266 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
271 if (strchr(icon, '.'))
273 /* try again without extension */
274 gchar *icon2 = remove_extension_from_path(icon);
275 pixbuf = gtk_icon_theme_load_icon(icon_theme,
276 icon2, /* icon name */
278 static_cast<GtkIconLookupFlags>(0), /* flags */
282 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
286 /* try as an absolute path */
287 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
290 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
298 if (!pixbuf) return FALSE;
300 register_stock_icon(key, pixbuf);
304 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
305 gint old_w, gint old_h,
306 gint *new_w, gint *new_h)
308 if ((static_cast<gdouble>(req_w) / old_w) < (static_cast<gdouble>(req_h) / old_h))
311 *new_h = static_cast<gdouble>(*new_w) / old_w * old_h;
312 if (*new_h < 1) *new_h = 1;
317 *new_w = static_cast<gdouble>(*new_h) / old_h * old_w;
318 if (*new_w < 1) *new_w = 1;
321 return (*new_w != old_w || *new_h != old_h);
324 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
328 switch (fd->format_class)
330 case FORMAT_CLASS_UNKNOWN:
331 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
333 case FORMAT_CLASS_META:
334 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
336 case FORMAT_CLASS_VIDEO:
337 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
339 case FORMAT_CLASS_COLLECTION:
340 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
342 case FORMAT_CLASS_DOCUMENT:
343 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
345 case FORMAT_CLASS_ARCHIVE:
346 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
349 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
352 if (requested_width && requested_height)
354 gint w = gdk_pixbuf_get_width(pixbuf);
355 gint h = gdk_pixbuf_get_height(pixbuf);
357 if (w > requested_width || h > requested_height)
361 if (pixbuf_scale_aspect(requested_width, requested_height,
367 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
368 g_object_unref(G_OBJECT(tmp));
377 *-----------------------------------------------------------------------------
379 *-----------------------------------------------------------------------------
382 gboolean util_clip_region(gint x, gint y, gint w, gint h,
383 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
384 gint *rx, gint *ry, gint *rw, gint *rh)
386 if (clip_x + clip_w <= x ||
388 clip_y + clip_h <= y ||
394 *rx = MAX(x, clip_x);
395 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
397 *ry = MAX(y, clip_y);
398 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
404 *-----------------------------------------------------------------------------
406 *-----------------------------------------------------------------------------
409 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
410 guchar *dest, gint dest_row_stride, gint w, gint h,
411 gint bytes_per_pixel, gboolean counter_clockwise)
417 for (i = 0; i < h; i++)
419 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
420 for (j = 0; j < w; j++)
422 if (counter_clockwise)
424 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
428 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
430 *(dp++) = *(sp++); /* r */
431 *(dp++) = *(sp++); /* g */
432 *(dp++) = *(sp++); /* b */
433 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
439 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
440 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
446 for (i = 0; i < h; i++)
448 sp = src + (i * src_row_stride);
449 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
450 memcpy(dp, sp, w * bytes_per_pixel);
454 #define ROTATE_BUFFER_WIDTH 48
455 #define ROTATE_BUFFER_HEIGHT 48
458 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
461 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
476 if (!src) return nullptr;
478 sw = gdk_pixbuf_get_width(src);
479 sh = gdk_pixbuf_get_height(src);
480 has_alpha = gdk_pixbuf_get_has_alpha(src);
481 srs = gdk_pixbuf_get_rowstride(src);
482 s_pix = gdk_pixbuf_get_pixels(src);
486 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
487 drs = gdk_pixbuf_get_rowstride(dest);
488 d_pix = gdk_pixbuf_get_pixels(dest);
490 a = (has_alpha ? 4 : 3);
492 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
493 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
494 b_pix = gdk_pixbuf_get_pixels(buffer);
495 brs = gdk_pixbuf_get_rowstride(buffer);
497 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
499 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
500 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
504 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
505 pixbuf_copy_block_rotate(s_pix, srs, j, i,
507 a, counter_clockwise);
509 if (counter_clockwise)
519 pixbuf_copy_block(b_pix, brs, w, h,
520 d_pix, drs, x, y, a);
524 g_object_unref(buffer);
527 /* this is the simple version of rotation (roughly 2-4x slower) */
529 for (i = 0; i < sh; i++)
531 sp = s_pix + (i * srs);
532 for (j = 0; j < sw; j++)
534 if (counter_clockwise)
536 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
540 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
543 *(dp++) = *(sp++); /* r */
544 *(dp++) = *(sp++); /* g */
545 *(dp++) = *(sp++); /* b */
546 if (has_alpha) *(dp) = *(sp++); /* a */
555 * Returns a copy of pixbuf mirrored and or flipped.
556 * TO do a 180 degree rotations set both mirror and flipped TRUE
557 * if mirror and flip are FALSE, result is a simple copy.
559 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
572 if (!src) return nullptr;
574 w = gdk_pixbuf_get_width(src);
575 h = gdk_pixbuf_get_height(src);
576 has_alpha = gdk_pixbuf_get_has_alpha(src);
577 srs = gdk_pixbuf_get_rowstride(src);
578 s_pix = gdk_pixbuf_get_pixels(src);
580 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
581 drs = gdk_pixbuf_get_rowstride(dest);
582 d_pix = gdk_pixbuf_get_pixels(dest);
584 a = has_alpha ? 4 : 3;
586 for (i = 0; i < h; i++)
588 sp = s_pix + (i * srs);
591 dp = d_pix + ((h - i - 1) * drs);
595 dp = d_pix + (i * drs);
600 for (j = 0; j < w; j++)
602 *(dp++) = *(sp++); /* r */
603 *(dp++) = *(sp++); /* g */
604 *(dp++) = *(sp++); /* b */
605 if (has_alpha) *(dp) = *(sp++); /* a */
611 for (j = 0; j < w; j++)
613 *(dp++) = *(sp++); /* r */
614 *(dp++) = *(sp++); /* g */
615 *(dp++) = *(sp++); /* b */
616 if (has_alpha) *(dp++) = *(sp++); /* a */
624 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
627 GdkPixbuf *tmp = nullptr;
631 case EXIF_ORIENTATION_TOP_LEFT:
632 dest = gdk_pixbuf_copy(pixbuf);
634 case EXIF_ORIENTATION_TOP_RIGHT:
636 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
638 case EXIF_ORIENTATION_BOTTOM_RIGHT:
640 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
642 case EXIF_ORIENTATION_BOTTOM_LEFT:
644 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
646 case EXIF_ORIENTATION_LEFT_TOP:
647 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
648 dest = pixbuf_copy_rotate_90(tmp, FALSE);
650 case EXIF_ORIENTATION_RIGHT_TOP:
651 /* rotated -90 (270) */
652 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
654 case EXIF_ORIENTATION_RIGHT_BOTTOM:
655 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
656 dest = pixbuf_copy_rotate_90(tmp, TRUE);
658 case EXIF_ORIENTATION_LEFT_BOTTOM:
660 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
663 dest = gdk_pixbuf_copy(pixbuf);
666 if (tmp) g_object_unref(tmp);
673 *-----------------------------------------------------------------------------
674 * pixbuf drawing (rectangles)
675 *-----------------------------------------------------------------------------
679 * Fills region of pixbuf at x,y over w,h
680 * with colors red (r), green (g), blue (b)
681 * applying alpha (a), use a=255 for solid.
683 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
684 gint x, gint y, gint w, gint h,
685 gint r, gint g, gint b, gint a)
695 pw = gdk_pixbuf_get_width(pb);
696 ph = gdk_pixbuf_get_height(pb);
698 if (x < 0 || x + w > pw) return;
699 if (y < 0 || y + h > ph) return;
701 has_alpha = gdk_pixbuf_get_has_alpha(pb);
702 prs = gdk_pixbuf_get_rowstride(pb);
703 p_pix = gdk_pixbuf_get_pixels(pb);
705 for (i = 0; i < h; i++)
707 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
708 for (j = 0; j < w; j++)
710 *pp = (r * a + *pp * (256-a)) >> 8;
712 *pp = (g * a + *pp * (256-a)) >> 8;
714 *pp = (b * a + *pp * (256-a)) >> 8;
721 #pragma GCC diagnostic push
722 #pragma GCC diagnostic ignored "-Wunused-function"
723 void pixbuf_draw_rect_unused(GdkPixbuf *pb,
724 gint x, gint y, gint w, gint h,
725 gint r, gint g, gint b, gint a,
726 gint left, gint right, gint top, gint bottom)
728 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
730 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
732 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
734 pixbuf_draw_rect_fill(pb, x, y, left, h,
737 #pragma GCC diagnostic pop
739 void pixbuf_set_rect_fill(GdkPixbuf *pb,
740 gint x, gint y, gint w, gint h,
741 gint r, gint g, gint b, gint a)
751 pw = gdk_pixbuf_get_width(pb);
752 ph = gdk_pixbuf_get_height(pb);
754 if (x < 0 || x + w > pw) return;
755 if (y < 0 || y + h > ph) return;
757 has_alpha = gdk_pixbuf_get_has_alpha(pb);
758 prs = gdk_pixbuf_get_rowstride(pb);
759 p_pix = gdk_pixbuf_get_pixels(pb);
761 for (i = 0; i < h; i++)
763 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
764 for (j = 0; j < w; j++)
769 if (has_alpha) { *pp = a; pp++; }
774 void pixbuf_set_rect(GdkPixbuf *pb,
775 gint x, gint y, gint w, gint h,
776 gint r, gint g, gint b, gint a,
777 gint left, gint right, gint top, gint bottom)
779 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
781 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
783 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
785 pixbuf_set_rect_fill(pb, x, y, left, h,
789 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
796 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
797 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
799 buf = gdk_pixbuf_get_pixels(pb);
800 has_alpha = gdk_pixbuf_get_has_alpha(pb);
801 rowstride = gdk_pixbuf_get_rowstride(pb);
803 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
807 if (has_alpha) *p = a;
812 *-----------------------------------------------------------------------------
813 * pixbuf text rendering
814 *-----------------------------------------------------------------------------
817 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
818 GdkPixbuf *dest, gint dx, gint dy,
820 guint8 r, guint8 g, guint8 b, guint8 a)
835 if (!src || !dest) return;
837 sw = gdk_pixbuf_get_width(src);
838 sh = gdk_pixbuf_get_height(src);
840 if (sx < 0 || sx + w > sw) return;
841 if (sy < 0 || sy + h > sh) return;
843 dw = gdk_pixbuf_get_width(dest);
844 dh = gdk_pixbuf_get_height(dest);
846 if (dx < 0 || dx + w > dw) return;
847 if (dy < 0 || dy + h > dh) return;
849 s_alpha = gdk_pixbuf_get_has_alpha(src);
850 d_alpha = gdk_pixbuf_get_has_alpha(dest);
851 srs = gdk_pixbuf_get_rowstride(src);
852 drs = gdk_pixbuf_get_rowstride(dest);
853 s_pix = gdk_pixbuf_get_pixels(src);
854 d_pix = gdk_pixbuf_get_pixels(dest);
856 s_step = (s_alpha) ? 4 : 3;
857 d_step = (d_alpha) ? 4 : 3;
859 for (i = 0; i < h; i++)
861 sp = s_pix + (sy + i) * srs + sx * s_step;
862 dp = d_pix + (dy + i) * drs + dx * d_step;
863 for (j = 0; j < w; j++)
869 asub = a * sp[0] / 255;
870 *dp = (r * asub + *dp * (256-asub)) >> 8;
872 asub = a * sp[1] / 255;
873 *dp = (g * asub + *dp * (256-asub)) >> 8;
875 asub = a * sp[2] / 255;
876 *dp = (b * asub + *dp * (256-asub)) >> 8;
881 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
895 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *,
897 guint8 r, guint8 g, guint8 b, guint8 a)
903 cairo_surface_t *source;
906 pango_layout_get_pixel_size(layout, &w, &h);
907 if (w < 1 || h < 1) return;
909 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
911 cr = cairo_create (source);
912 cairo_set_source_rgb(cr, 0, 0, 0);
913 cairo_rectangle (cr, 0, 0, w, h);
915 cairo_set_source_rgb(cr, 1, 1, 1);
916 pango_cairo_show_layout (cr, layout);
919 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
921 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
923 cairo_image_surface_get_width (source),
924 cairo_image_surface_get_height (source),
925 cairo_image_surface_get_stride (source),
931 dw = gdk_pixbuf_get_width(pixbuf);
932 dh = gdk_pixbuf_get_height(pixbuf);
948 if (x + w > dw) w = dw - x;
949 if (y + h > dh) h = dh - y;
951 pixbuf_copy_font(buffer, sx, sy,
955 g_object_unref(buffer);
956 cairo_surface_destroy(source);
960 *-----------------------------------------------------------------------------
961 * pixbuf drawing (triangle)
962 *-----------------------------------------------------------------------------
965 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
966 gint *rx, gint *ry, gint *rw, gint *rh)
974 tw = MAX(abs(x1 - x2), abs(x2 - x3));
975 tw = MAX(tw, abs(x3 - x1));
976 th = MAX(abs(y1 - y2), abs(y2 - y3));
977 th = MAX(th, abs(y3 - y1));
985 void pixbuf_draw_triangle(GdkPixbuf *pb,
986 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
987 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
988 guint8 r, guint8 g, guint8 b, guint8 a)
1000 gdouble slope1, slope2;
1001 gint slope1_x, slope1_y;
1004 gboolean middle = FALSE;
1008 pw = gdk_pixbuf_get_width(pb);
1009 ph = gdk_pixbuf_get_height(pb);
1011 if (!util_clip_region(0, 0, pw, ph,
1012 clip_x, clip_y, clip_w, clip_h,
1013 &rx, &ry, &rw, &rh)) return;
1015 util_clip_triangle(x1, y1, x2, y2, x3, y3,
1016 &tx, &ty, &tw, &th);
1018 if (!util_clip_region(rx, ry, rw, rh,
1020 &fx1, &fy1, &fw, &fh)) return;
1024 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1025 prs = gdk_pixbuf_get_rowstride(pb);
1026 p_pix = gdk_pixbuf_get_pixels(pb);
1028 p_step = (has_alpha) ? 4 : 3;
1032 t = x1; x1 = x2; x2 = t;
1033 t = y1; y1 = y2; y2 = t;
1037 t = x2; x2 = x3; x3 = t;
1038 t = y2; y2 = y3; y3 = t;
1042 t = x1; x1 = x2; x2 = t;
1043 t = y1; y1 = y2; y2 = t;
1046 slope1 = static_cast<gdouble>(y2 - y1);
1047 if (slope1) slope1 = static_cast<gdouble>(x2 - x1) / slope1;
1050 slope2 = static_cast<gdouble>(y3 - y1);
1051 if (slope2) slope2 = static_cast<gdouble>(x3 - x1) / slope2;
1053 for (y = fy1; y < fy2; y++)
1057 if (!middle && y > y2)
1059 slope1 = static_cast<gdouble>(y3 - y2);
1060 if (slope1) slope1 = static_cast<gdouble>(x3 - x2) / slope1;
1067 xa = slope1_x + (slope1 * (y - slope1_y) + 0.5);
1068 xb = x1 + (slope2 * (y - y1) + 0.5);
1072 t = xa; xa = xb; xb = t;
1075 xa = CLAMP(xa, fx1, fx2);
1076 xb = CLAMP(xb, fx1, fx2);
1078 pp = p_pix + y * prs + xa * p_step;
1082 *pp = (r * a + *pp * (256-a)) >> 8;
1084 *pp = (g * a + *pp * (256-a)) >> 8;
1086 *pp = (b * a + *pp * (256-a)) >> 8;
1088 if (has_alpha) pp++;
1096 *-----------------------------------------------------------------------------
1097 * pixbuf drawing (line)
1098 *-----------------------------------------------------------------------------
1101 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1102 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1103 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1105 gboolean flip = FALSE;
1112 t = x1; x1 = x2; x2 = t;
1113 t = y1; y1 = y2; y2 = t;
1117 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1121 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1125 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1133 slope = (y2 - y1) / d;
1136 y1 = y1 + slope * (clip_x - x1);
1139 if (x2 > clip_x + clip_w)
1141 y2 = y2 + slope * (clip_x + clip_w - x2);
1142 x2 = clip_x + clip_w;
1148 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1154 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1156 t = x1; x1 = x2; x2 = t;
1157 t = y1; y1 = y2; y2 = t;
1166 slope = (x2 - x1) / d;
1169 x1 = x1 + slope * (clip_y - y1);
1172 if (y2 > clip_y + clip_h)
1174 x2 = x2 + slope * (clip_y + clip_h - y2);
1175 y2 = clip_y + clip_h;
1197 void pixbuf_draw_line(GdkPixbuf *pb,
1198 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1199 gint x1, gint y1, gint x2, gint y2,
1200 guint8 r, guint8 g, guint8 b, guint8 a)
1204 gint rx, ry, rw, rh;
1205 gdouble rx1, ry1, rx2, ry2;
1212 gint cx1, cy1, cx2, cy2;
1216 pw = gdk_pixbuf_get_width(pb);
1217 ph = gdk_pixbuf_get_height(pb);
1219 if (!util_clip_region(0, 0, pw, ph,
1220 clip_x, clip_y, clip_w, clip_h,
1221 &rx, &ry, &rw, &rh)) return;
1222 if (!util_clip_line(static_cast<gdouble>(rx), static_cast<gdouble>(ry), static_cast<gdouble>(rw), static_cast<gdouble>(rh),
1223 static_cast<gdouble>(x1), static_cast<gdouble>(y1), static_cast<gdouble>(x2), static_cast<gdouble>(y2),
1224 &rx1, &ry1, &rx2, &ry2)) return;
1231 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1232 prs = gdk_pixbuf_get_rowstride(pb);
1233 p_pix = gdk_pixbuf_get_pixels(pb);
1235 p_step = (has_alpha) ? 4 : 3;
1237 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1242 t = rx1; rx1 = rx2; rx2 = t;
1243 t = ry1; ry1 = ry2; ry2 = t;
1247 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1248 for (x = rx1; x < rx2; x += 1.0)
1250 px = static_cast<gint>(x + 0.5);
1251 py = static_cast<gint>(ry1 + (x - rx1) * slope + 0.5);
1253 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1255 pp = p_pix + py * prs + px * p_step;
1256 *pp = (r * a + *pp * (256-a)) >> 8;
1258 *pp = (g * a + *pp * (256-a)) >> 8;
1260 *pp = (b * a + *pp * (256-a)) >> 8;
1269 t = rx1; rx1 = rx2; rx2 = t;
1270 t = ry1; ry1 = ry2; ry2 = t;
1274 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1275 for (y = ry1; y < ry2; y += 1.0)
1277 px = static_cast<gint>(rx1 + (y - ry1) * slope + 0.5);
1278 py = static_cast<gint>(y + 0.5);
1280 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1282 pp = p_pix + py * prs + px * p_step;
1283 *pp = (r * a + *pp * (256-a)) >> 8;
1285 *pp = (g * a + *pp * (256-a)) >> 8;
1287 *pp = (b * a + *pp * (256-a)) >> 8;
1294 *-----------------------------------------------------------------------------
1295 * pixbuf drawing (fades and shadows)
1296 *-----------------------------------------------------------------------------
1299 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1300 gint s, gboolean vertical, gint border,
1301 gint x1, gint y1, gint x2, gint y2,
1302 guint8 r, guint8 g, guint8 b, guint8 a)
1309 p_step = (has_alpha) ? 4 : 3;
1310 for (j = y1; j < y2; j++)
1312 pp = p_pix + j * prs + x1 * p_step;
1313 if (!vertical) n = a - a * abs(j - s) / border;
1314 for (i = x1; i < x2; i++)
1316 if (vertical) n = a - a * abs(i - s) / border;
1317 *pp = (r * n + *pp * (256-n)) >> 8;
1319 *pp = (g * n + *pp * (256-n)) >> 8;
1321 *pp = (b * n + *pp * (256-n)) >> 8;
1323 if (has_alpha) pp++;
1328 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1329 gint sx, gint sy, gint border,
1330 gint x1, gint y1, gint x2, gint y2,
1331 guint8, guint8 g, guint8 b, guint8 a)
1337 p_step = (has_alpha) ? 4 : 3;
1338 for (j = y1; j < y2; j++)
1340 pp = p_pix + j * prs + x1 * p_step;
1341 for (i = x1; i < x2; i++)
1346 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1347 n = a - a * r / border;
1348 *pp = (r * n + *pp * (256-n)) >> 8;
1350 *pp = (g * n + *pp * (256-n)) >> 8;
1352 *pp = (b * n + *pp * (256-n)) >> 8;
1354 if (has_alpha) pp++;
1359 void pixbuf_draw_shadow(GdkPixbuf *pb,
1360 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1361 gint x, gint y, gint w, gint h, gint border,
1362 guint8 r, guint8 g, guint8 b, guint8 a)
1366 gint rx, ry, rw, rh;
1367 gint fx, fy, fw, fh;
1372 pw = gdk_pixbuf_get_width(pb);
1373 ph = gdk_pixbuf_get_height(pb);
1375 if (!util_clip_region(0, 0, pw, ph,
1376 clip_x, clip_y, clip_w, clip_h,
1377 &rx, &ry, &rw, &rh)) return;
1379 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1380 prs = gdk_pixbuf_get_rowstride(pb);
1381 p_pix = gdk_pixbuf_get_pixels(pb);
1383 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1385 &fx, &fy, &fw, &fh))
1387 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1390 if (border < 1) return;
1392 if (util_clip_region(x, y + border, border, h - border * 2,
1394 &fx, &fy, &fw, &fh))
1396 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1397 x + border, TRUE, border,
1398 fx, fy, fx + fw, fy + fh,
1401 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1403 &fx, &fy, &fw, &fh))
1405 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1406 x + w - border, TRUE, border,
1407 fx, fy, fx + fw, fy + fh,
1410 if (util_clip_region(x + border, y, w - border * 2, border,
1412 &fx, &fy, &fw, &fh))
1414 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1415 y + border, FALSE, border,
1416 fx, fy, fx + fw, fy + fh,
1419 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1421 &fx, &fy, &fw, &fh))
1423 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1424 y + h - border, FALSE, border,
1425 fx, fy, fx + fw, fy + fh,
1428 if (util_clip_region(x, y, border, border,
1430 &fx, &fy, &fw, &fh))
1432 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1433 x + border, y + border, border,
1434 fx, fy, fx + fw, fy + fh,
1437 if (util_clip_region(x + w - border, y, border, border,
1439 &fx, &fy, &fw, &fh))
1441 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1442 x + w - border, y + border, border,
1443 fx, fy, fx + fw, fy + fh,
1446 if (util_clip_region(x, y + h - border, border, border,
1448 &fx, &fy, &fw, &fh))
1450 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1451 x + border, y + h - border, border,
1452 fx, fy, fx + fw, fy + fh,
1455 if (util_clip_region(x + w - border, y + h - border, border, border,
1457 &fx, &fy, &fw, &fh))
1459 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1460 x + w - border, y + h - border, border,
1461 fx, fy, fx + fw, fy + fh,
1468 *-----------------------------------------------------------------------------
1469 * pixbuf color alterations
1470 *-----------------------------------------------------------------------------
1473 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1474 gint x, gint y, gint w, gint h)
1484 pw = gdk_pixbuf_get_width(pb);
1485 ph = gdk_pixbuf_get_height(pb);
1487 if (x < 0 || x + w > pw) return;
1488 if (y < 0 || y + h > ph) return;
1490 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1491 prs = gdk_pixbuf_get_rowstride(pb);
1492 p_pix = gdk_pixbuf_get_pixels(pb);
1494 for (i = 0; i < h; i++)
1496 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1497 for (j = 0; j < w; j++)
1501 grey = (pp[0] + pp[1] + pp[2]) / 3;
1508 if (has_alpha) pp++;
1514 *-----------------------------------------------------------------------------
1515 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1517 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1527 pw = gdk_pixbuf_get_width(pb);
1528 ph = gdk_pixbuf_get_height(pb);
1530 if (x < 0 || x + w > pw) return;
1531 if (y < 0 || y + h > ph) return;
1533 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1534 prs = gdk_pixbuf_get_rowstride(pb);
1535 p_pix = gdk_pixbuf_get_pixels(pb);
1537 for (i = 0; i < h; i++)
1539 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1540 for (j = 0; j < w; j++)
1542 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1550 if (has_alpha) pp++;
1555 if (has_alpha) pp++;
1562 *-----------------------------------------------------------------------------
1563 * pixbuf ignore alpha
1564 *-----------------------------------------------------------------------------
1566 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1567 gint x, gint y, gint w, gint h)
1577 pw = gdk_pixbuf_get_width(pb);
1578 ph = gdk_pixbuf_get_height(pb);
1580 if (x < 0 || x + w > pw) return;
1581 if (y < 0 || y + h > ph) return;
1583 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1584 if (!has_alpha) return;
1586 prs = gdk_pixbuf_get_rowstride(pb);
1587 p_pix = gdk_pixbuf_get_pixels(pb);
1589 for (i = 0; i < h; i++)
1591 pp = p_pix + (y + i) * prs + (x * 4 );
1592 for (j = 0; j < w; j++)
1599 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */