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"
29 #include "main-defines.h"
30 #include "ui-fileops.h"
36 *-----------------------------------------------------------------------------
38 *-----------------------------------------------------------------------------
41 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
43 GError *error = nullptr;
46 if (!pixbuf || !filename) return FALSE;
48 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
49 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
53 log_printf("Error saving png file: %s\n", error->message);
61 *-----------------------------------------------------------------------------
63 *-----------------------------------------------------------------------------
66 #pragma GCC diagnostic push
67 #pragma GCC diagnostic ignored "-Wunused-function"
68 gboolean pixbuf_to_file_as_jpg_unused(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
70 GError *error = nullptr;
74 if (!pixbuf || !filename) return FALSE;
76 if (quality == -1) quality = 75;
77 if (quality < 1 || quality > 100)
79 log_printf("Jpeg not saved, invalid quality %d\n", quality);
83 qbuf = g_strdup_printf("%d", quality);
84 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
89 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
95 #pragma GCC diagnostic pop
98 *-----------------------------------------------------------------------------
100 *-----------------------------------------------------------------------------
109 static PixbufInline inline_pixbuf_data[] = {
110 { PIXBUF_INLINE_ARCHIVE, "gq-icon-archive-file" },
111 { PIXBUF_INLINE_BROKEN, "gq-icon-broken" },
112 { PIXBUF_INLINE_COLLECTION, "gq-icon-collection" },
113 { PIXBUF_INLINE_ICON_180, "gq-icon-rotate-180" },
114 { PIXBUF_INLINE_ICON_BOOK, "gq-icon-book" },
115 { PIXBUF_INLINE_ICON_CONFIG, "gq-icon-config" },
116 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, "gq-icon-draw-rectangle" },
117 { PIXBUF_INLINE_ICON_EXIF, "gq-icon-exif" },
118 { PIXBUF_INLINE_ICON_EXPOSURE, "gq-icon-exposure" },
119 { PIXBUF_INLINE_ICON_FLOAT, "gq-icon-float" },
120 { PIXBUF_INLINE_ICON, "gqview-icon" },
121 { PIXBUF_INLINE_ICON_GRAYSCALE, "gq-icon-grayscale" },
122 { PIXBUF_INLINE_ICON_HEIF, "gq-icon-heic" },
123 { PIXBUF_INLINE_ICON_HIDETOOLS, "gq-icon-hidetools" },
124 { PIXBUF_INLINE_ICON_MAINTENANCE, "gq-icon-maintenance" },
125 { PIXBUF_INLINE_ICON_MARKS, "gq-icon-marks" },
126 { PIXBUF_INLINE_ICON_MOVE, "gq-icon-move" },
127 { PIXBUF_INLINE_ICON_ORIGINAL, "gq-icon-original" },
128 { PIXBUF_INLINE_ICON_PANORAMA, "gq-icon-panorama" },
129 { PIXBUF_INLINE_ICON_PDF, "gq-icon-pdf" },
130 { PIXBUF_INLINE_ICON_PROPERTIES, "gq-icon-properties" },
131 { PIXBUF_INLINE_ICON_RENAME, "gq-icon-rename" },
132 { PIXBUF_INLINE_ICON_SELECT_ALL, "gq-icon-select-all" },
133 { PIXBUF_INLINE_ICON_SELECT_INVERT, "gq-icon-select-invert" },
134 { PIXBUF_INLINE_ICON_SELECT_NONE, "gq-icon-select-none" },
135 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, "gq-icon-select-rectangle" },
136 { PIXBUF_INLINE_ICON_SORT, "gq-icon-sort" },
137 { PIXBUF_INLINE_ICON_THUMB, "gq-icon-thumb" },
138 { PIXBUF_INLINE_ICON_TOOLS, "gq-icon-tools" },
139 { PIXBUF_INLINE_ICON_VIEW, "gq-icon-view" },
140 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, "gq-icon-zoomfillhor" },
141 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, "gq-icon-zoomfillvert" },
142 { PIXBUF_INLINE_LOGO, "geeqie-logo" },
143 { PIXBUF_INLINE_METADATA, "gq-icon-metadata" },
144 { PIXBUF_INLINE_SCROLLER, "gq-scroller" },
145 { PIXBUF_INLINE_SPLIT_PANE_SYNC, "gq-icon-split-pane-sync" },
146 { PIXBUF_INLINE_UNKNOWN, "gq-icon-unknown" },
147 { PIXBUF_INLINE_VIDEO, "gq-icon-video" },
151 GdkPixbuf *pixbuf_inline(const gchar *key)
153 gboolean dark = FALSE;
154 gchar *file_name = nullptr;
157 GdkPixbuf *icon_pixbuf;
158 GError *error = nullptr;
159 GInputStream *in_stream;
161 GtkSettings *settings;
163 if (!key) return nullptr;
165 settings = gtk_settings_get_default();
166 g_object_get(settings, "gtk-theme-name", &theme_name, nullptr);
167 dark = g_str_has_suffix(theme_name, "dark");
171 while (inline_pixbuf_data[i].key)
173 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
175 file_name = g_strconcat(inline_pixbuf_data[i].data, dark ? "-dark" : "", ".png", nullptr);
176 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
179 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
187 file_name = g_strconcat(inline_pixbuf_data[i].data, ".png", nullptr);
188 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
191 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
197 log_printf("warning: inline pixbuf error: %s", error->message);
199 g_object_unref(in_stream);
203 icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, nullptr, &error);
204 g_object_unref(in_stream);
208 log_printf("warning: inline pixbuf error: %s", error->message);
218 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
223 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
225 static GtkIconFactory *icon_factory = nullptr;
226 GtkIconSet *icon_set;
230 icon_factory = gtk_icon_factory_new();
231 gtk_icon_factory_add_default(icon_factory);
234 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
235 gtk_icon_factory_add(icon_factory, key, icon_set);
239 void pixbuf_inline_register_stock_icons()
244 while (inline_pixbuf_data[i].key)
246 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
251 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
253 GtkIconTheme *icon_theme;
255 GError *error = nullptr;
257 icon_theme = gtk_icon_theme_get_default();
259 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
261 pixbuf = gtk_icon_theme_load_icon(icon_theme,
262 icon, /* icon name */
264 static_cast<GtkIconLookupFlags>(0), /* flags */
270 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
275 if (strchr(icon, '.'))
277 /* try again without extension */
278 gchar *icon2 = remove_extension_from_path(icon);
279 pixbuf = gtk_icon_theme_load_icon(icon_theme,
280 icon2, /* icon name */
282 static_cast<GtkIconLookupFlags>(0), /* flags */
286 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
290 /* try as an absolute path */
291 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
294 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
302 if (!pixbuf) return FALSE;
304 register_stock_icon(key, pixbuf);
308 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
309 gint old_w, gint old_h,
310 gint *new_w, gint *new_h)
312 if ((static_cast<gdouble>(req_w) / old_w) < (static_cast<gdouble>(req_h) / old_h))
315 *new_h = static_cast<gdouble>(*new_w) / old_w * old_h;
316 if (*new_h < 1) *new_h = 1;
321 *new_w = static_cast<gdouble>(*new_h) / old_h * old_w;
322 if (*new_w < 1) *new_w = 1;
325 return (*new_w != old_w || *new_h != old_h);
328 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
332 switch (fd->format_class)
334 case FORMAT_CLASS_UNKNOWN:
335 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
337 case FORMAT_CLASS_META:
338 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
340 case FORMAT_CLASS_VIDEO:
341 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
343 case FORMAT_CLASS_COLLECTION:
344 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
346 case FORMAT_CLASS_DOCUMENT:
347 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
349 case FORMAT_CLASS_ARCHIVE:
350 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
353 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
356 if (requested_width && requested_height)
358 gint w = gdk_pixbuf_get_width(pixbuf);
359 gint h = gdk_pixbuf_get_height(pixbuf);
361 if (w > requested_width || h > requested_height)
366 if (pixbuf_scale_aspect(requested_width, requested_height,
372 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
373 g_object_unref(G_OBJECT(tmp));
382 *-----------------------------------------------------------------------------
384 *-----------------------------------------------------------------------------
387 gboolean util_clip_region(gint x, gint y, gint w, gint h,
388 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
389 gint *rx, gint *ry, gint *rw, gint *rh)
391 if (clip_x + clip_w <= x ||
393 clip_y + clip_h <= y ||
399 *rx = MAX(x, clip_x);
400 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
402 *ry = MAX(y, clip_y);
403 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
409 *-----------------------------------------------------------------------------
411 *-----------------------------------------------------------------------------
414 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
415 guchar *dest, gint dest_row_stride, gint w, gint h,
416 gint bytes_per_pixel, gboolean counter_clockwise)
423 for (i = 0; i < h; i++)
425 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
426 for (j = 0; j < w; j++)
428 if (counter_clockwise)
430 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
434 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
436 *(dp++) = *(sp++); /* r */
437 *(dp++) = *(sp++); /* g */
438 *(dp++) = *(sp++); /* b */
439 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
445 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
446 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
452 for (i = 0; i < h; i++)
454 sp = src + (i * src_row_stride);
455 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
456 memcpy(dp, sp, w * bytes_per_pixel);
461 ROTATE_BUFFER_WIDTH = 48,
462 ROTATE_BUFFER_HEIGHT = 48
466 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
469 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
490 if (!src) return nullptr;
492 sw = gdk_pixbuf_get_width(src);
493 sh = gdk_pixbuf_get_height(src);
494 has_alpha = gdk_pixbuf_get_has_alpha(src);
495 srs = gdk_pixbuf_get_rowstride(src);
496 s_pix = gdk_pixbuf_get_pixels(src);
500 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
501 drs = gdk_pixbuf_get_rowstride(dest);
502 d_pix = gdk_pixbuf_get_pixels(dest);
504 a = (has_alpha ? 4 : 3);
506 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
507 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
508 b_pix = gdk_pixbuf_get_pixels(buffer);
509 brs = gdk_pixbuf_get_rowstride(buffer);
511 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
513 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
514 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
519 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
520 pixbuf_copy_block_rotate(s_pix, srs, j, i,
522 a, counter_clockwise);
524 if (counter_clockwise)
534 pixbuf_copy_block(b_pix, brs, w, h,
535 d_pix, drs, x, y, a);
539 g_object_unref(buffer);
542 /* this is the simple version of rotation (roughly 2-4x slower) */
544 for (i = 0; i < sh; i++)
546 sp = s_pix + (i * srs);
547 for (j = 0; j < sw; j++)
549 if (counter_clockwise)
551 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
555 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
558 *(dp++) = *(sp++); /* r */
559 *(dp++) = *(sp++); /* g */
560 *(dp++) = *(sp++); /* b */
561 if (has_alpha) *(dp) = *(sp++); /* a */
570 * Returns a copy of pixbuf mirrored and or flipped.
571 * TO do a 180 degree rotations set both mirror and flipped TRUE
572 * if mirror and flip are FALSE, result is a simple copy.
574 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
590 if (!src) return nullptr;
592 w = gdk_pixbuf_get_width(src);
593 h = gdk_pixbuf_get_height(src);
594 has_alpha = gdk_pixbuf_get_has_alpha(src);
595 srs = gdk_pixbuf_get_rowstride(src);
596 s_pix = gdk_pixbuf_get_pixels(src);
598 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
599 drs = gdk_pixbuf_get_rowstride(dest);
600 d_pix = gdk_pixbuf_get_pixels(dest);
602 a = has_alpha ? 4 : 3;
604 for (i = 0; i < h; i++)
606 sp = s_pix + (i * srs);
609 dp = d_pix + ((h - i - 1) * drs);
613 dp = d_pix + (i * drs);
618 for (j = 0; j < w; j++)
620 *(dp++) = *(sp++); /* r */
621 *(dp++) = *(sp++); /* g */
622 *(dp++) = *(sp++); /* b */
623 if (has_alpha) *(dp) = *(sp++); /* a */
629 for (j = 0; j < w; j++)
631 *(dp++) = *(sp++); /* r */
632 *(dp++) = *(sp++); /* g */
633 *(dp++) = *(sp++); /* b */
634 if (has_alpha) *(dp++) = *(sp++); /* a */
642 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
645 GdkPixbuf *tmp = nullptr;
649 case EXIF_ORIENTATION_TOP_LEFT:
650 dest = gdk_pixbuf_copy(pixbuf);
652 case EXIF_ORIENTATION_TOP_RIGHT:
654 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
656 case EXIF_ORIENTATION_BOTTOM_RIGHT:
658 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
660 case EXIF_ORIENTATION_BOTTOM_LEFT:
662 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
664 case EXIF_ORIENTATION_LEFT_TOP:
665 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
666 dest = pixbuf_copy_rotate_90(tmp, FALSE);
668 case EXIF_ORIENTATION_RIGHT_TOP:
669 /* rotated -90 (270) */
670 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
672 case EXIF_ORIENTATION_RIGHT_BOTTOM:
673 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
674 dest = pixbuf_copy_rotate_90(tmp, TRUE);
676 case EXIF_ORIENTATION_LEFT_BOTTOM:
678 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
681 dest = gdk_pixbuf_copy(pixbuf);
684 if (tmp) g_object_unref(tmp);
691 *-----------------------------------------------------------------------------
692 * pixbuf drawing (rectangles)
693 *-----------------------------------------------------------------------------
697 * Fills region of pixbuf at x,y over w,h
698 * with colors red (r), green (g), blue (b)
699 * applying alpha (a), use a=255 for solid.
701 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
702 gint x, gint y, gint w, gint h,
703 gint r, gint g, gint b, gint a)
716 pw = gdk_pixbuf_get_width(pb);
717 ph = gdk_pixbuf_get_height(pb);
719 if (x < 0 || x + w > pw) return;
720 if (y < 0 || y + h > ph) return;
722 has_alpha = gdk_pixbuf_get_has_alpha(pb);
723 prs = gdk_pixbuf_get_rowstride(pb);
724 p_pix = gdk_pixbuf_get_pixels(pb);
726 for (i = 0; i < h; i++)
728 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
729 for (j = 0; j < w; j++)
731 *pp = (r * a + *pp * (256-a)) >> 8;
733 *pp = (g * a + *pp * (256-a)) >> 8;
735 *pp = (b * a + *pp * (256-a)) >> 8;
742 #pragma GCC diagnostic push
743 #pragma GCC diagnostic ignored "-Wunused-function"
744 void pixbuf_draw_rect_unused(GdkPixbuf *pb,
745 gint x, gint y, gint w, gint h,
746 gint r, gint g, gint b, gint a,
747 gint left, gint right, gint top, gint bottom)
749 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
751 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
753 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
755 pixbuf_draw_rect_fill(pb, x, y, left, h,
758 #pragma GCC diagnostic pop
760 void pixbuf_set_rect_fill(GdkPixbuf *pb,
761 gint x, gint y, gint w, gint h,
762 gint r, gint g, gint b, gint a)
775 pw = gdk_pixbuf_get_width(pb);
776 ph = gdk_pixbuf_get_height(pb);
778 if (x < 0 || x + w > pw) return;
779 if (y < 0 || y + h > ph) return;
781 has_alpha = gdk_pixbuf_get_has_alpha(pb);
782 prs = gdk_pixbuf_get_rowstride(pb);
783 p_pix = gdk_pixbuf_get_pixels(pb);
785 for (i = 0; i < h; i++)
787 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
788 for (j = 0; j < w; j++)
793 if (has_alpha) { *pp = a; pp++; }
798 void pixbuf_set_rect(GdkPixbuf *pb,
799 gint x, gint y, gint w, gint h,
800 gint r, gint g, gint b, gint a,
801 gint left, gint right, gint top, gint bottom)
803 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
805 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
807 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
809 pixbuf_set_rect_fill(pb, x, y, left, h,
813 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
820 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
821 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
823 buf = gdk_pixbuf_get_pixels(pb);
824 has_alpha = gdk_pixbuf_get_has_alpha(pb);
825 rowstride = gdk_pixbuf_get_rowstride(pb);
827 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
831 if (has_alpha) *p = a;
836 *-----------------------------------------------------------------------------
837 * pixbuf text rendering
838 *-----------------------------------------------------------------------------
841 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
842 GdkPixbuf *dest, gint dx, gint dy,
844 guint8 r, guint8 g, guint8 b, guint8 a)
864 if (!src || !dest) return;
866 sw = gdk_pixbuf_get_width(src);
867 sh = gdk_pixbuf_get_height(src);
869 if (sx < 0 || sx + w > sw) return;
870 if (sy < 0 || sy + h > sh) return;
872 dw = gdk_pixbuf_get_width(dest);
873 dh = gdk_pixbuf_get_height(dest);
875 if (dx < 0 || dx + w > dw) return;
876 if (dy < 0 || dy + h > dh) return;
878 s_alpha = gdk_pixbuf_get_has_alpha(src);
879 d_alpha = gdk_pixbuf_get_has_alpha(dest);
880 srs = gdk_pixbuf_get_rowstride(src);
881 drs = gdk_pixbuf_get_rowstride(dest);
882 s_pix = gdk_pixbuf_get_pixels(src);
883 d_pix = gdk_pixbuf_get_pixels(dest);
885 s_step = (s_alpha) ? 4 : 3;
886 d_step = (d_alpha) ? 4 : 3;
888 for (i = 0; i < h; i++)
890 sp = s_pix + (sy + i) * srs + sx * s_step;
891 dp = d_pix + (dy + i) * drs + dx * d_step;
892 for (j = 0; j < w; j++)
898 asub = a * sp[0] / 255;
899 *dp = (r * asub + *dp * (256-asub)) >> 8;
901 asub = a * sp[1] / 255;
902 *dp = (g * asub + *dp * (256-asub)) >> 8;
904 asub = a * sp[2] / 255;
905 *dp = (b * asub + *dp * (256-asub)) >> 8;
910 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
924 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *,
926 guint8 r, guint8 g, guint8 b, guint8 a)
935 cairo_surface_t *source;
938 pango_layout_get_pixel_size(layout, &w, &h);
939 if (w < 1 || h < 1) return;
941 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
943 cr = cairo_create (source);
944 cairo_set_source_rgb(cr, 0, 0, 0);
945 cairo_rectangle (cr, 0, 0, w, h);
947 cairo_set_source_rgb(cr, 1, 1, 1);
948 pango_cairo_show_layout (cr, layout);
951 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
953 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
955 cairo_image_surface_get_width (source),
956 cairo_image_surface_get_height (source),
957 cairo_image_surface_get_stride (source),
963 dw = gdk_pixbuf_get_width(pixbuf);
964 dh = gdk_pixbuf_get_height(pixbuf);
980 if (x + w > dw) w = dw - x;
981 if (y + h > dh) h = dh - y;
983 pixbuf_copy_font(buffer, sx, sy,
987 g_object_unref(buffer);
988 cairo_surface_destroy(source);
992 *-----------------------------------------------------------------------------
993 * pixbuf drawing (triangle)
994 *-----------------------------------------------------------------------------
997 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
998 gint *rx, gint *ry, gint *rw, gint *rh)
1009 tw = MAX(abs(x1 - x2), abs(x2 - x3));
1010 tw = MAX(tw, abs(x3 - x1));
1011 th = MAX(abs(y1 - y2), abs(y2 - y3));
1012 th = MAX(th, abs(y3 - y1));
1020 void pixbuf_draw_triangle(GdkPixbuf *pb,
1021 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1022 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
1023 guint8 r, guint8 g, guint8 b, guint8 a)
1052 gboolean middle = FALSE;
1056 pw = gdk_pixbuf_get_width(pb);
1057 ph = gdk_pixbuf_get_height(pb);
1059 if (!util_clip_region(0, 0, pw, ph,
1060 clip_x, clip_y, clip_w, clip_h,
1061 &rx, &ry, &rw, &rh)) return;
1063 util_clip_triangle(x1, y1, x2, y2, x3, y3,
1064 &tx, &ty, &tw, &th);
1066 if (!util_clip_region(rx, ry, rw, rh,
1068 &fx1, &fy1, &fw, &fh)) return;
1072 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1073 prs = gdk_pixbuf_get_rowstride(pb);
1074 p_pix = gdk_pixbuf_get_pixels(pb);
1076 p_step = (has_alpha) ? 4 : 3;
1080 t = x1; x1 = x2; x2 = t;
1081 t = y1; y1 = y2; y2 = t;
1085 t = x2; x2 = x3; x3 = t;
1086 t = y2; y2 = y3; y3 = t;
1090 t = x1; x1 = x2; x2 = t;
1091 t = y1; y1 = y2; y2 = t;
1094 slope1 = static_cast<gdouble>(y2 - y1);
1095 if (slope1) slope1 = static_cast<gdouble>(x2 - x1) / slope1;
1098 slope2 = static_cast<gdouble>(y3 - y1);
1099 if (slope2) slope2 = static_cast<gdouble>(x3 - x1) / slope2;
1101 for (y = fy1; y < fy2; y++)
1106 if (!middle && y > y2)
1108 slope1 = static_cast<gdouble>(y3 - y2);
1109 if (slope1) slope1 = static_cast<gdouble>(x3 - x2) / slope1;
1116 xa = slope1_x + (slope1 * (y - slope1_y) + 0.5);
1117 xb = x1 + (slope2 * (y - y1) + 0.5);
1121 t = xa; xa = xb; xb = t;
1124 xa = CLAMP(xa, fx1, fx2);
1125 xb = CLAMP(xb, fx1, fx2);
1127 pp = p_pix + y * prs + xa * p_step;
1131 *pp = (r * a + *pp * (256-a)) >> 8;
1133 *pp = (g * a + *pp * (256-a)) >> 8;
1135 *pp = (b * a + *pp * (256-a)) >> 8;
1137 if (has_alpha) pp++;
1145 *-----------------------------------------------------------------------------
1146 * pixbuf drawing (line)
1147 *-----------------------------------------------------------------------------
1150 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1151 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1152 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1154 gboolean flip = FALSE;
1161 t = x1; x1 = x2; x2 = t;
1162 t = y1; y1 = y2; y2 = t;
1166 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1170 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1174 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1182 slope = (y2 - y1) / d;
1185 y1 = y1 + slope * (clip_x - x1);
1188 if (x2 > clip_x + clip_w)
1190 y2 = y2 + slope * (clip_x + clip_w - x2);
1191 x2 = clip_x + clip_w;
1197 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1203 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1205 t = x1; x1 = x2; x2 = t;
1206 t = y1; y1 = y2; y2 = t;
1215 slope = (x2 - x1) / d;
1218 x1 = x1 + slope * (clip_y - y1);
1221 if (y2 > clip_y + clip_h)
1223 x2 = x2 + slope * (clip_y + clip_h - y2);
1224 y2 = clip_y + clip_h;
1246 void pixbuf_draw_line(GdkPixbuf *pb,
1247 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1248 gint x1, gint y1, gint x2, gint y2,
1249 guint8 r, guint8 g, guint8 b, guint8 a)
1278 pw = gdk_pixbuf_get_width(pb);
1279 ph = gdk_pixbuf_get_height(pb);
1281 if (!util_clip_region(0, 0, pw, ph,
1282 clip_x, clip_y, clip_w, clip_h,
1283 &rx, &ry, &rw, &rh)) return;
1284 if (!util_clip_line(static_cast<gdouble>(rx), static_cast<gdouble>(ry), static_cast<gdouble>(rw), static_cast<gdouble>(rh),
1285 static_cast<gdouble>(x1), static_cast<gdouble>(y1), static_cast<gdouble>(x2), static_cast<gdouble>(y2),
1286 &rx1, &ry1, &rx2, &ry2)) return;
1293 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1294 prs = gdk_pixbuf_get_rowstride(pb);
1295 p_pix = gdk_pixbuf_get_pixels(pb);
1297 p_step = (has_alpha) ? 4 : 3;
1299 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1304 t = rx1; rx1 = rx2; rx2 = t;
1305 t = ry1; ry1 = ry2; ry2 = t;
1309 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1310 for (x = rx1; x < rx2; x += 1.0)
1312 px = static_cast<gint>(x + 0.5);
1313 py = static_cast<gint>(ry1 + (x - rx1) * slope + 0.5);
1315 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1317 pp = p_pix + py * prs + px * p_step;
1318 *pp = (r * a + *pp * (256-a)) >> 8;
1320 *pp = (g * a + *pp * (256-a)) >> 8;
1322 *pp = (b * a + *pp * (256-a)) >> 8;
1331 t = rx1; rx1 = rx2; rx2 = t;
1332 t = ry1; ry1 = ry2; ry2 = t;
1336 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1337 for (y = ry1; y < ry2; y += 1.0)
1339 px = static_cast<gint>(rx1 + (y - ry1) * slope + 0.5);
1340 py = static_cast<gint>(y + 0.5);
1342 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1344 pp = p_pix + py * prs + px * p_step;
1345 *pp = (r * a + *pp * (256-a)) >> 8;
1347 *pp = (g * a + *pp * (256-a)) >> 8;
1349 *pp = (b * a + *pp * (256-a)) >> 8;
1356 *-----------------------------------------------------------------------------
1357 * pixbuf drawing (fades and shadows)
1358 *-----------------------------------------------------------------------------
1361 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1362 gint s, gboolean vertical, gint border,
1363 gint x1, gint y1, gint x2, gint y2,
1364 guint8 r, guint8 g, guint8 b, guint8 a)
1372 p_step = (has_alpha) ? 4 : 3;
1373 for (j = y1; j < y2; j++)
1375 pp = p_pix + j * prs + x1 * p_step;
1376 if (!vertical) n = a - a * abs(j - s) / border;
1377 for (i = x1; i < x2; i++)
1379 if (vertical) n = a - a * abs(i - s) / border;
1380 *pp = (r * n + *pp * (256-n)) >> 8;
1382 *pp = (g * n + *pp * (256-n)) >> 8;
1384 *pp = (b * n + *pp * (256-n)) >> 8;
1386 if (has_alpha) pp++;
1391 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1392 gint sx, gint sy, gint border,
1393 gint x1, gint y1, gint x2, gint y2,
1394 guint8, guint8 g, guint8 b, guint8 a)
1401 p_step = (has_alpha) ? 4 : 3;
1402 for (j = y1; j < y2; j++)
1404 pp = p_pix + j * prs + x1 * p_step;
1405 for (i = x1; i < x2; i++)
1410 r = MIN(border, (gint)hypot(i - sx, j - sy));
1411 n = a - a * r / border;
1412 *pp = (r * n + *pp * (256-n)) >> 8;
1414 *pp = (g * n + *pp * (256-n)) >> 8;
1416 *pp = (b * n + *pp * (256-n)) >> 8;
1418 if (has_alpha) pp++;
1423 void pixbuf_draw_shadow(GdkPixbuf *pb,
1424 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1425 gint x, gint y, gint w, gint h, gint border,
1426 guint8 r, guint8 g, guint8 b, guint8 a)
1444 pw = gdk_pixbuf_get_width(pb);
1445 ph = gdk_pixbuf_get_height(pb);
1447 if (!util_clip_region(0, 0, pw, ph,
1448 clip_x, clip_y, clip_w, clip_h,
1449 &rx, &ry, &rw, &rh)) return;
1451 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1452 prs = gdk_pixbuf_get_rowstride(pb);
1453 p_pix = gdk_pixbuf_get_pixels(pb);
1455 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1457 &fx, &fy, &fw, &fh))
1459 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1462 if (border < 1) return;
1464 if (util_clip_region(x, y + border, border, h - border * 2,
1466 &fx, &fy, &fw, &fh))
1468 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1469 x + border, TRUE, border,
1470 fx, fy, fx + fw, fy + fh,
1473 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1475 &fx, &fy, &fw, &fh))
1477 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1478 x + w - border, TRUE, border,
1479 fx, fy, fx + fw, fy + fh,
1482 if (util_clip_region(x + border, y, w - border * 2, border,
1484 &fx, &fy, &fw, &fh))
1486 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1487 y + border, FALSE, border,
1488 fx, fy, fx + fw, fy + fh,
1491 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1493 &fx, &fy, &fw, &fh))
1495 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1496 y + h - border, FALSE, border,
1497 fx, fy, fx + fw, fy + fh,
1500 if (util_clip_region(x, y, border, border,
1502 &fx, &fy, &fw, &fh))
1504 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1505 x + border, y + border, border,
1506 fx, fy, fx + fw, fy + fh,
1509 if (util_clip_region(x + w - border, y, border, border,
1511 &fx, &fy, &fw, &fh))
1513 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1514 x + w - border, y + border, border,
1515 fx, fy, fx + fw, fy + fh,
1518 if (util_clip_region(x, y + h - border, border, border,
1520 &fx, &fy, &fw, &fh))
1522 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1523 x + border, y + h - border, border,
1524 fx, fy, fx + fw, fy + fh,
1527 if (util_clip_region(x + w - border, y + h - border, border, border,
1529 &fx, &fy, &fw, &fh))
1531 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1532 x + w - border, y + h - border, border,
1533 fx, fy, fx + fw, fy + fh,
1540 *-----------------------------------------------------------------------------
1541 * pixbuf color alterations
1542 *-----------------------------------------------------------------------------
1545 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1546 gint x, gint y, gint w, gint h)
1559 pw = gdk_pixbuf_get_width(pb);
1560 ph = gdk_pixbuf_get_height(pb);
1562 if (x < 0 || x + w > pw) return;
1563 if (y < 0 || y + h > ph) return;
1565 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1566 prs = gdk_pixbuf_get_rowstride(pb);
1567 p_pix = gdk_pixbuf_get_pixels(pb);
1569 for (i = 0; i < h; i++)
1571 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1572 for (j = 0; j < w; j++)
1576 grey = (pp[0] + pp[1] + pp[2]) / 3;
1583 if (has_alpha) pp++;
1589 *-----------------------------------------------------------------------------
1590 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1592 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1605 pw = gdk_pixbuf_get_width(pb);
1606 ph = gdk_pixbuf_get_height(pb);
1608 if (x < 0 || x + w > pw) return;
1609 if (y < 0 || y + h > ph) return;
1611 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1612 prs = gdk_pixbuf_get_rowstride(pb);
1613 p_pix = gdk_pixbuf_get_pixels(pb);
1615 for (i = 0; i < h; i++)
1617 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1618 for (j = 0; j < w; j++)
1620 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1628 if (has_alpha) pp++;
1633 if (has_alpha) pp++;
1640 *-----------------------------------------------------------------------------
1641 * pixbuf ignore alpha
1642 *-----------------------------------------------------------------------------
1644 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1645 gint x, gint y, gint w, gint h)
1658 pw = gdk_pixbuf_get_width(pb);
1659 ph = gdk_pixbuf_get_height(pb);
1661 if (x < 0 || x + w > pw) return;
1662 if (y < 0 || y + h > ph) return;
1664 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1665 if (!has_alpha) return;
1667 prs = gdk_pixbuf_get_rowstride(pb);
1668 p_pix = gdk_pixbuf_get_pixels(pb);
1670 for (i = 0; i < h; i++)
1672 pp = p_pix + (y + i) * prs + (x * 4 );
1673 for (j = 0; j < w; j++)
1680 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */