2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "pixbuf-util.h"
25 #include "ui-fileops.h"
31 *-----------------------------------------------------------------------------
33 *-----------------------------------------------------------------------------
36 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
38 GError *error = nullptr;
41 if (!pixbuf || !filename) return FALSE;
43 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
44 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
48 log_printf("Error saving png file: %s\n", error->message);
56 *-----------------------------------------------------------------------------
58 *-----------------------------------------------------------------------------
61 #pragma GCC diagnostic push
62 #pragma GCC diagnostic ignored "-Wunused-function"
63 gboolean pixbuf_to_file_as_jpg_unused(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
69 if (!pixbuf || !filename) return FALSE;
71 if (quality == -1) quality = 75;
72 if (quality < 1 || quality > 100)
74 log_printf("Jpeg not saved, invalid quality %d\n", quality);
78 qbuf = g_strdup_printf("%d", quality);
79 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
84 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
90 #pragma GCC diagnostic pop
93 *-----------------------------------------------------------------------------
95 *-----------------------------------------------------------------------------
104 static PixbufInline inline_pixbuf_data[] = {
105 { PIXBUF_INLINE_ARCHIVE, "gq-archive-file.png" },
106 { PIXBUF_INLINE_BROKEN, "gq-sheet-broken.png" },
107 { PIXBUF_INLINE_COLLECTION, "gq-collection.png" },
108 { PIXBUF_INLINE_ICON, "gqview-icon.png" },
109 { PIXBUF_INLINE_ICON_180, "gq-icon-rotate-180.png" },
110 { PIXBUF_INLINE_ICON_BOOK, "gq-icon-book.png" },
111 { PIXBUF_INLINE_ICON_CCW, "gq-icon-rotate-counter-clockwise.png" },
112 { PIXBUF_INLINE_ICON_CONFIG, "gq-icon-config.png" },
113 { PIXBUF_INLINE_ICON_CW, "gq-icon-rotate-clockwise.png" },
114 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, "gq-icon-draw-rectangle.png" },
115 { PIXBUF_INLINE_ICON_EXIF, "gq-icon-exif.png" },
116 { PIXBUF_INLINE_ICON_EXPOSURE, "gq-icon-exposure.png" },
117 { PIXBUF_INLINE_ICON_FILE_FILTER, "gq-icon-file-filter.png" },
118 { PIXBUF_INLINE_ICON_FLIP, "gq-icon-flip.png" },
119 { PIXBUF_INLINE_ICON_FLOAT, "gq-icon-float.png" },
120 { PIXBUF_INLINE_ICON_GRAYSCALE, "gq-icon-grayscale.png" },
121 { PIXBUF_INLINE_ICON_HEIF, "gq-icon-heic.png" },
122 { PIXBUF_INLINE_ICON_HIDETOOLS, "gq-icon-hidetools.png" },
123 { PIXBUF_INLINE_ICON_INFO, "gq-icon-info.png" },
124 { PIXBUF_INLINE_ICON_MAINTENANCE, "gq-icon-maintenance.png" },
125 { PIXBUF_INLINE_ICON_MARKS, "gq-icon-marks.png" },
126 { PIXBUF_INLINE_ICON_MIRROR, "gq-icon-mirror.png" },
127 { PIXBUF_INLINE_ICON_MOVE, "gq-icon-move.png" },
128 { PIXBUF_INLINE_ICON_ORIGINAL, "gq-icon-original.png" },
129 { PIXBUF_INLINE_ICON_PANORAMA, "gq-icon-panorama.png" },
130 { PIXBUF_INLINE_ICON_PDF, "gq-icon-pdf.png" },
131 { PIXBUF_INLINE_ICON_RENAME, "gq-icon-rename.png" },
132 { PIXBUF_INLINE_ICON_SELECT_ALL, "gq-icon-select-all.png" },
133 { PIXBUF_INLINE_ICON_SELECT_INVERT, "gq-icon-select-invert.png" },
134 { PIXBUF_INLINE_ICON_SELECT_NONE, "gq-icon-select-none.png" },
135 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, "gq-icon-select-rectangle.png" },
136 { PIXBUF_INLINE_ICON_SORT, "gq-icon-sort.png" },
137 { PIXBUF_INLINE_ICON_THUMB, "gq-icon-thumb.png" },
138 { PIXBUF_INLINE_ICON_TOOLS, "gq-icon-tools.png" },
139 { PIXBUF_INLINE_ICON_TRASH, "gq-icon-trash.png" },
140 { PIXBUF_INLINE_ICON_VIEW, "gq-icon-view.png" },
141 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, "gq-icon-zoomfillhor.png" },
142 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, "gq-icon-zoomfillvert.png" },
143 { PIXBUF_INLINE_LOGO, "geeqie-logo.png" },
144 { PIXBUF_INLINE_METADATA, "gq-sheet-metadata.png" },
145 { PIXBUF_INLINE_SCROLLER, "gq-scroller.png" },
146 { PIXBUF_INLINE_SPLIT_PANE_SYNC, "gq-icon-split-pane-sync.png" },
147 { PIXBUF_INLINE_UNKNOWN, "gq-sheet-unknown.png" },
148 { PIXBUF_INLINE_VIDEO, "gq-sheet-video.png" },
152 GdkPixbuf *pixbuf_inline(const gchar *key)
154 GError *error = nullptr;
155 GInputStream *in_stream;
156 GdkPixbuf *icon_pixbuf;
160 if (!key) return nullptr;
163 while (inline_pixbuf_data[i].key)
165 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
167 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, inline_pixbuf_data[i].data, NULL);
169 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
174 log_printf("warning: inline pixbuf error: %s", error->message);
176 g_object_unref(in_stream);
180 icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, nullptr, &error);
181 g_object_unref(in_stream);
185 log_printf("warning: inline pixbuf error: %s", error->message);
195 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
200 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
202 static GtkIconFactory *icon_factory = nullptr;
203 GtkIconSet *icon_set;
207 icon_factory = gtk_icon_factory_new();
208 gtk_icon_factory_add_default(icon_factory);
211 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
212 gtk_icon_factory_add(icon_factory, key, icon_set);
216 void pixbuf_inline_register_stock_icons()
221 while (inline_pixbuf_data[i].key)
223 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
228 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
230 GtkIconTheme *icon_theme;
232 GError *error = nullptr;
234 icon_theme = gtk_icon_theme_get_default();
236 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
238 pixbuf = gtk_icon_theme_load_icon(icon_theme,
239 icon, /* icon name */
241 static_cast<GtkIconLookupFlags>(0), /* flags */
247 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
252 if (strchr(icon, '.'))
254 /* try again without extension */
255 gchar *icon2 = remove_extension_from_path(icon);
256 pixbuf = gtk_icon_theme_load_icon(icon_theme,
257 icon2, /* icon name */
259 static_cast<GtkIconLookupFlags>(0), /* flags */
263 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
267 /* try as an absolute path */
268 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
271 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
279 if (!pixbuf) return FALSE;
281 register_stock_icon(key, pixbuf);
285 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
286 gint old_w, gint old_h,
287 gint *new_w, gint *new_h)
289 if ((static_cast<gdouble>(req_w) / old_w) < (static_cast<gdouble>(req_h) / old_h))
292 *new_h = static_cast<gdouble>(*new_w) / old_w * old_h;
293 if (*new_h < 1) *new_h = 1;
298 *new_w = static_cast<gdouble>(*new_h) / old_h * old_w;
299 if (*new_w < 1) *new_w = 1;
302 return (*new_w != old_w || *new_h != old_h);
305 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
309 switch (fd->format_class)
311 case FORMAT_CLASS_UNKNOWN:
312 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
314 case FORMAT_CLASS_META:
315 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
317 case FORMAT_CLASS_VIDEO:
318 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
320 case FORMAT_CLASS_COLLECTION:
321 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
323 case FORMAT_CLASS_DOCUMENT:
324 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
326 case FORMAT_CLASS_ARCHIVE:
327 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
330 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
333 if (requested_width && requested_height)
335 gint w = gdk_pixbuf_get_width(pixbuf);
336 gint h = gdk_pixbuf_get_height(pixbuf);
338 if (w > requested_width || h > requested_height)
342 if (pixbuf_scale_aspect(requested_width, requested_height,
348 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
349 g_object_unref(G_OBJECT(tmp));
358 *-----------------------------------------------------------------------------
360 *-----------------------------------------------------------------------------
363 gboolean util_clip_region(gint x, gint y, gint w, gint h,
364 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
365 gint *rx, gint *ry, gint *rw, gint *rh)
367 if (clip_x + clip_w <= x ||
369 clip_y + clip_h <= y ||
375 *rx = MAX(x, clip_x);
376 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
378 *ry = MAX(y, clip_y);
379 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
385 *-----------------------------------------------------------------------------
387 *-----------------------------------------------------------------------------
390 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
391 guchar *dest, gint dest_row_stride, gint w, gint h,
392 gint bytes_per_pixel, gboolean counter_clockwise)
398 for (i = 0; i < h; i++)
400 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
401 for (j = 0; j < w; j++)
403 if (counter_clockwise)
405 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
409 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
411 *(dp++) = *(sp++); /* r */
412 *(dp++) = *(sp++); /* g */
413 *(dp++) = *(sp++); /* b */
414 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
420 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
421 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
427 for (i = 0; i < h; i++)
429 sp = src + (i * src_row_stride);
430 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
431 memcpy(dp, sp, w * bytes_per_pixel);
435 #define ROTATE_BUFFER_WIDTH 48
436 #define ROTATE_BUFFER_HEIGHT 48
439 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
442 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
457 if (!src) return nullptr;
459 sw = gdk_pixbuf_get_width(src);
460 sh = gdk_pixbuf_get_height(src);
461 has_alpha = gdk_pixbuf_get_has_alpha(src);
462 srs = gdk_pixbuf_get_rowstride(src);
463 s_pix = gdk_pixbuf_get_pixels(src);
467 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
468 drs = gdk_pixbuf_get_rowstride(dest);
469 d_pix = gdk_pixbuf_get_pixels(dest);
471 a = (has_alpha ? 4 : 3);
473 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
474 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
475 b_pix = gdk_pixbuf_get_pixels(buffer);
476 brs = gdk_pixbuf_get_rowstride(buffer);
478 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
480 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
481 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
485 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
486 pixbuf_copy_block_rotate(s_pix, srs, j, i,
488 a, counter_clockwise);
490 if (counter_clockwise)
500 pixbuf_copy_block(b_pix, brs, w, h,
501 d_pix, drs, x, y, a);
505 g_object_unref(buffer);
508 /* this is the simple version of rotation (roughly 2-4x slower) */
510 for (i = 0; i < sh; i++)
512 sp = s_pix + (i * srs);
513 for (j = 0; j < sw; j++)
515 if (counter_clockwise)
517 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
521 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
524 *(dp++) = *(sp++); /* r */
525 *(dp++) = *(sp++); /* g */
526 *(dp++) = *(sp++); /* b */
527 if (has_alpha) *(dp) = *(sp++); /* a */
536 * Returns a copy of pixbuf mirrored and or flipped.
537 * TO do a 180 degree rotations set both mirror and flipped TRUE
538 * if mirror and flip are FALSE, result is a simple copy.
540 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
553 if (!src) return nullptr;
555 w = gdk_pixbuf_get_width(src);
556 h = gdk_pixbuf_get_height(src);
557 has_alpha = gdk_pixbuf_get_has_alpha(src);
558 srs = gdk_pixbuf_get_rowstride(src);
559 s_pix = gdk_pixbuf_get_pixels(src);
561 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
562 drs = gdk_pixbuf_get_rowstride(dest);
563 d_pix = gdk_pixbuf_get_pixels(dest);
565 a = has_alpha ? 4 : 3;
567 for (i = 0; i < h; i++)
569 sp = s_pix + (i * srs);
572 dp = d_pix + ((h - i - 1) * drs);
576 dp = d_pix + (i * drs);
581 for (j = 0; j < w; j++)
583 *(dp++) = *(sp++); /* r */
584 *(dp++) = *(sp++); /* g */
585 *(dp++) = *(sp++); /* b */
586 if (has_alpha) *(dp) = *(sp++); /* a */
592 for (j = 0; j < w; j++)
594 *(dp++) = *(sp++); /* r */
595 *(dp++) = *(sp++); /* g */
596 *(dp++) = *(sp++); /* b */
597 if (has_alpha) *(dp++) = *(sp++); /* a */
605 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
608 GdkPixbuf *tmp = nullptr;
612 case EXIF_ORIENTATION_TOP_LEFT:
613 dest = gdk_pixbuf_copy(pixbuf);
615 case EXIF_ORIENTATION_TOP_RIGHT:
617 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
619 case EXIF_ORIENTATION_BOTTOM_RIGHT:
621 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
623 case EXIF_ORIENTATION_BOTTOM_LEFT:
625 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
627 case EXIF_ORIENTATION_LEFT_TOP:
628 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
629 dest = pixbuf_copy_rotate_90(tmp, FALSE);
631 case EXIF_ORIENTATION_RIGHT_TOP:
632 /* rotated -90 (270) */
633 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
635 case EXIF_ORIENTATION_RIGHT_BOTTOM:
636 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
637 dest = pixbuf_copy_rotate_90(tmp, TRUE);
639 case EXIF_ORIENTATION_LEFT_BOTTOM:
641 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
644 dest = gdk_pixbuf_copy(pixbuf);
647 if (tmp) g_object_unref(tmp);
654 *-----------------------------------------------------------------------------
655 * pixbuf drawing (rectangles)
656 *-----------------------------------------------------------------------------
660 * Fills region of pixbuf at x,y over w,h
661 * with colors red (r), green (g), blue (b)
662 * applying alpha (a), use a=255 for solid.
664 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
665 gint x, gint y, gint w, gint h,
666 gint r, gint g, gint b, gint a)
676 pw = gdk_pixbuf_get_width(pb);
677 ph = gdk_pixbuf_get_height(pb);
679 if (x < 0 || x + w > pw) return;
680 if (y < 0 || y + h > ph) return;
682 has_alpha = gdk_pixbuf_get_has_alpha(pb);
683 prs = gdk_pixbuf_get_rowstride(pb);
684 p_pix = gdk_pixbuf_get_pixels(pb);
686 for (i = 0; i < h; i++)
688 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
689 for (j = 0; j < w; j++)
691 *pp = (r * a + *pp * (256-a)) >> 8;
693 *pp = (g * a + *pp * (256-a)) >> 8;
695 *pp = (b * a + *pp * (256-a)) >> 8;
702 #pragma GCC diagnostic push
703 #pragma GCC diagnostic ignored "-Wunused-function"
704 void pixbuf_draw_rect_unused(GdkPixbuf *pb,
705 gint x, gint y, gint w, gint h,
706 gint r, gint g, gint b, gint a,
707 gint left, gint right, gint top, gint bottom)
709 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
711 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
713 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
715 pixbuf_draw_rect_fill(pb, x, y, left, h,
718 #pragma GCC diagnostic pop
720 void pixbuf_set_rect_fill(GdkPixbuf *pb,
721 gint x, gint y, gint w, gint h,
722 gint r, gint g, gint b, gint a)
732 pw = gdk_pixbuf_get_width(pb);
733 ph = gdk_pixbuf_get_height(pb);
735 if (x < 0 || x + w > pw) return;
736 if (y < 0 || y + h > ph) return;
738 has_alpha = gdk_pixbuf_get_has_alpha(pb);
739 prs = gdk_pixbuf_get_rowstride(pb);
740 p_pix = gdk_pixbuf_get_pixels(pb);
742 for (i = 0; i < h; i++)
744 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
745 for (j = 0; j < w; j++)
750 if (has_alpha) { *pp = a; pp++; }
755 void pixbuf_set_rect(GdkPixbuf *pb,
756 gint x, gint y, gint w, gint h,
757 gint r, gint g, gint b, gint a,
758 gint left, gint right, gint top, gint bottom)
760 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
762 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
764 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
766 pixbuf_set_rect_fill(pb, x, y, left, h,
770 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
777 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
778 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
780 buf = gdk_pixbuf_get_pixels(pb);
781 has_alpha = gdk_pixbuf_get_has_alpha(pb);
782 rowstride = gdk_pixbuf_get_rowstride(pb);
784 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
788 if (has_alpha) *p = a;
793 *-----------------------------------------------------------------------------
794 * pixbuf text rendering
795 *-----------------------------------------------------------------------------
798 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
799 GdkPixbuf *dest, gint dx, gint dy,
801 guint8 r, guint8 g, guint8 b, guint8 a)
816 if (!src || !dest) return;
818 sw = gdk_pixbuf_get_width(src);
819 sh = gdk_pixbuf_get_height(src);
821 if (sx < 0 || sx + w > sw) return;
822 if (sy < 0 || sy + h > sh) return;
824 dw = gdk_pixbuf_get_width(dest);
825 dh = gdk_pixbuf_get_height(dest);
827 if (dx < 0 || dx + w > dw) return;
828 if (dy < 0 || dy + h > dh) return;
830 s_alpha = gdk_pixbuf_get_has_alpha(src);
831 d_alpha = gdk_pixbuf_get_has_alpha(dest);
832 srs = gdk_pixbuf_get_rowstride(src);
833 drs = gdk_pixbuf_get_rowstride(dest);
834 s_pix = gdk_pixbuf_get_pixels(src);
835 d_pix = gdk_pixbuf_get_pixels(dest);
837 s_step = (s_alpha) ? 4 : 3;
838 d_step = (d_alpha) ? 4 : 3;
840 for (i = 0; i < h; i++)
842 sp = s_pix + (sy + i) * srs + sx * s_step;
843 dp = d_pix + (dy + i) * drs + dx * d_step;
844 for (j = 0; j < w; j++)
850 asub = a * sp[0] / 255;
851 *dp = (r * asub + *dp * (256-asub)) >> 8;
853 asub = a * sp[1] / 255;
854 *dp = (g * asub + *dp * (256-asub)) >> 8;
856 asub = a * sp[2] / 255;
857 *dp = (b * asub + *dp * (256-asub)) >> 8;
862 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
876 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *UNUSED(widget),
878 guint8 r, guint8 g, guint8 b, guint8 a)
884 cairo_surface_t *source;
887 pango_layout_get_pixel_size(layout, &w, &h);
888 if (w < 1 || h < 1) return;
890 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
892 cr = cairo_create (source);
893 cairo_set_source_rgb(cr, 0, 0, 0);
894 cairo_rectangle (cr, 0, 0, w, h);
896 cairo_set_source_rgb(cr, 1, 1, 1);
897 pango_cairo_show_layout (cr, layout);
900 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
902 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
904 cairo_image_surface_get_width (source),
905 cairo_image_surface_get_height (source),
906 cairo_image_surface_get_stride (source),
912 dw = gdk_pixbuf_get_width(pixbuf);
913 dh = gdk_pixbuf_get_height(pixbuf);
929 if (x + w > dw) w = dw - x;
930 if (y + h > dh) h = dh - y;
932 pixbuf_copy_font(buffer, sx, sy,
936 g_object_unref(buffer);
937 cairo_surface_destroy(source);
941 *-----------------------------------------------------------------------------
942 * pixbuf drawing (triangle)
943 *-----------------------------------------------------------------------------
946 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
947 gint *rx, gint *ry, gint *rw, gint *rh)
955 tw = MAX(abs(x1 - x2), abs(x2 - x3));
956 tw = MAX(tw, abs(x3 - x1));
957 th = MAX(abs(y1 - y2), abs(y2 - y3));
958 th = MAX(th, abs(y3 - y1));
966 void pixbuf_draw_triangle(GdkPixbuf *pb,
967 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
968 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
969 guint8 r, guint8 g, guint8 b, guint8 a)
981 gdouble slope1, slope2;
982 gint slope1_x, slope1_y;
985 gboolean middle = FALSE;
989 pw = gdk_pixbuf_get_width(pb);
990 ph = gdk_pixbuf_get_height(pb);
992 if (!util_clip_region(0, 0, pw, ph,
993 clip_x, clip_y, clip_w, clip_h,
994 &rx, &ry, &rw, &rh)) return;
996 util_clip_triangle(x1, y1, x2, y2, x3, y3,
999 if (!util_clip_region(rx, ry, rw, rh,
1001 &fx1, &fy1, &fw, &fh)) return;
1005 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1006 prs = gdk_pixbuf_get_rowstride(pb);
1007 p_pix = gdk_pixbuf_get_pixels(pb);
1009 p_step = (has_alpha) ? 4 : 3;
1013 t = x1; x1 = x2; x2 = t;
1014 t = y1; y1 = y2; y2 = t;
1018 t = x2; x2 = x3; x3 = t;
1019 t = y2; y2 = y3; y3 = t;
1023 t = x1; x1 = x2; x2 = t;
1024 t = y1; y1 = y2; y2 = t;
1027 slope1 = static_cast<gdouble>(y2 - y1);
1028 if (slope1) slope1 = static_cast<gdouble>(x2 - x1) / slope1;
1031 slope2 = static_cast<gdouble>(y3 - y1);
1032 if (slope2) slope2 = static_cast<gdouble>(x3 - x1) / slope2;
1034 for (y = fy1; y < fy2; y++)
1038 if (!middle && y > y2)
1040 slope1 = static_cast<gdouble>(y3 - y2);
1041 if (slope1) slope1 = static_cast<gdouble>(x3 - x2) / slope1;
1048 xa = slope1_x + (slope1 * (y - slope1_y) + 0.5);
1049 xb = x1 + (slope2 * (y - y1) + 0.5);
1053 t = xa; xa = xb; xb = t;
1056 xa = CLAMP(xa, fx1, fx2);
1057 xb = CLAMP(xb, fx1, fx2);
1059 pp = p_pix + y * prs + xa * p_step;
1063 *pp = (r * a + *pp * (256-a)) >> 8;
1065 *pp = (g * a + *pp * (256-a)) >> 8;
1067 *pp = (b * a + *pp * (256-a)) >> 8;
1069 if (has_alpha) pp++;
1077 *-----------------------------------------------------------------------------
1078 * pixbuf drawing (line)
1079 *-----------------------------------------------------------------------------
1082 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1083 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1084 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1086 gboolean flip = FALSE;
1093 t = x1; x1 = x2; x2 = t;
1094 t = y1; y1 = y2; y2 = t;
1098 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1102 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1106 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1114 slope = (y2 - y1) / d;
1117 y1 = y1 + slope * (clip_x - x1);
1120 if (x2 > clip_x + clip_w)
1122 y2 = y2 + slope * (clip_x + clip_w - x2);
1123 x2 = clip_x + clip_w;
1129 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1135 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1137 t = x1; x1 = x2; x2 = t;
1138 t = y1; y1 = y2; y2 = t;
1147 slope = (x2 - x1) / d;
1150 x1 = x1 + slope * (clip_y - y1);
1153 if (y2 > clip_y + clip_h)
1155 x2 = x2 + slope * (clip_y + clip_h - y2);
1156 y2 = clip_y + clip_h;
1178 void pixbuf_draw_line(GdkPixbuf *pb,
1179 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1180 gint x1, gint y1, gint x2, gint y2,
1181 guint8 r, guint8 g, guint8 b, guint8 a)
1185 gint rx, ry, rw, rh;
1186 gdouble rx1, ry1, rx2, ry2;
1193 gint cx1, cy1, cx2, cy2;
1197 pw = gdk_pixbuf_get_width(pb);
1198 ph = gdk_pixbuf_get_height(pb);
1200 if (!util_clip_region(0, 0, pw, ph,
1201 clip_x, clip_y, clip_w, clip_h,
1202 &rx, &ry, &rw, &rh)) return;
1203 if (!util_clip_line(static_cast<gdouble>(rx), static_cast<gdouble>(ry), static_cast<gdouble>(rw), static_cast<gdouble>(rh),
1204 static_cast<gdouble>(x1), static_cast<gdouble>(y1), static_cast<gdouble>(x2), static_cast<gdouble>(y2),
1205 &rx1, &ry1, &rx2, &ry2)) return;
1212 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1213 prs = gdk_pixbuf_get_rowstride(pb);
1214 p_pix = gdk_pixbuf_get_pixels(pb);
1216 p_step = (has_alpha) ? 4 : 3;
1218 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1223 t = rx1; rx1 = rx2; rx2 = t;
1224 t = ry1; ry1 = ry2; ry2 = t;
1228 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1229 for (x = rx1; x < rx2; x += 1.0)
1231 px = static_cast<gint>(x + 0.5);
1232 py = static_cast<gint>(ry1 + (x - rx1) * slope + 0.5);
1234 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1236 pp = p_pix + py * prs + px * p_step;
1237 *pp = (r * a + *pp * (256-a)) >> 8;
1239 *pp = (g * a + *pp * (256-a)) >> 8;
1241 *pp = (b * a + *pp * (256-a)) >> 8;
1250 t = rx1; rx1 = rx2; rx2 = t;
1251 t = ry1; ry1 = ry2; ry2 = t;
1255 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1256 for (y = ry1; y < ry2; y += 1.0)
1258 px = static_cast<gint>(rx1 + (y - ry1) * slope + 0.5);
1259 py = static_cast<gint>(y + 0.5);
1261 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1263 pp = p_pix + py * prs + px * p_step;
1264 *pp = (r * a + *pp * (256-a)) >> 8;
1266 *pp = (g * a + *pp * (256-a)) >> 8;
1268 *pp = (b * a + *pp * (256-a)) >> 8;
1275 *-----------------------------------------------------------------------------
1276 * pixbuf drawing (fades and shadows)
1277 *-----------------------------------------------------------------------------
1280 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1281 gint s, gboolean vertical, gint border,
1282 gint x1, gint y1, gint x2, gint y2,
1283 guint8 r, guint8 g, guint8 b, guint8 a)
1290 p_step = (has_alpha) ? 4 : 3;
1291 for (j = y1; j < y2; j++)
1293 pp = p_pix + j * prs + x1 * p_step;
1294 if (!vertical) n = a - a * abs(j - s) / border;
1295 for (i = x1; i < x2; i++)
1297 if (vertical) n = a - a * abs(i - s) / border;
1298 *pp = (r * n + *pp * (256-n)) >> 8;
1300 *pp = (g * n + *pp * (256-n)) >> 8;
1302 *pp = (b * n + *pp * (256-n)) >> 8;
1304 if (has_alpha) pp++;
1309 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1310 gint sx, gint sy, gint border,
1311 gint x1, gint y1, gint x2, gint y2,
1312 guint8 UNUSED(r), guint8 g, guint8 b, guint8 a)
1318 p_step = (has_alpha) ? 4 : 3;
1319 for (j = y1; j < y2; j++)
1321 pp = p_pix + j * prs + x1 * p_step;
1322 for (i = x1; i < x2; i++)
1327 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1328 n = a - a * r / border;
1329 *pp = (r * n + *pp * (256-n)) >> 8;
1331 *pp = (g * n + *pp * (256-n)) >> 8;
1333 *pp = (b * n + *pp * (256-n)) >> 8;
1335 if (has_alpha) pp++;
1340 void pixbuf_draw_shadow(GdkPixbuf *pb,
1341 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1342 gint x, gint y, gint w, gint h, gint border,
1343 guint8 r, guint8 g, guint8 b, guint8 a)
1347 gint rx, ry, rw, rh;
1348 gint fx, fy, fw, fh;
1353 pw = gdk_pixbuf_get_width(pb);
1354 ph = gdk_pixbuf_get_height(pb);
1356 if (!util_clip_region(0, 0, pw, ph,
1357 clip_x, clip_y, clip_w, clip_h,
1358 &rx, &ry, &rw, &rh)) return;
1360 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1361 prs = gdk_pixbuf_get_rowstride(pb);
1362 p_pix = gdk_pixbuf_get_pixels(pb);
1364 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1366 &fx, &fy, &fw, &fh))
1368 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1371 if (border < 1) return;
1373 if (util_clip_region(x, y + border, border, h - border * 2,
1375 &fx, &fy, &fw, &fh))
1377 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1378 x + border, TRUE, border,
1379 fx, fy, fx + fw, fy + fh,
1382 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1384 &fx, &fy, &fw, &fh))
1386 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1387 x + w - border, TRUE, border,
1388 fx, fy, fx + fw, fy + fh,
1391 if (util_clip_region(x + border, y, w - border * 2, border,
1393 &fx, &fy, &fw, &fh))
1395 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1396 y + border, FALSE, border,
1397 fx, fy, fx + fw, fy + fh,
1400 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1402 &fx, &fy, &fw, &fh))
1404 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1405 y + h - border, FALSE, border,
1406 fx, fy, fx + fw, fy + fh,
1409 if (util_clip_region(x, y, border, border,
1411 &fx, &fy, &fw, &fh))
1413 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1414 x + border, y + border, border,
1415 fx, fy, fx + fw, fy + fh,
1418 if (util_clip_region(x + w - border, y, border, border,
1420 &fx, &fy, &fw, &fh))
1422 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1423 x + w - border, y + border, border,
1424 fx, fy, fx + fw, fy + fh,
1427 if (util_clip_region(x, y + h - border, border, border,
1429 &fx, &fy, &fw, &fh))
1431 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1432 x + border, y + h - border, border,
1433 fx, fy, fx + fw, fy + fh,
1436 if (util_clip_region(x + w - border, y + h - border, border, border,
1438 &fx, &fy, &fw, &fh))
1440 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1441 x + w - border, y + h - border, border,
1442 fx, fy, fx + fw, fy + fh,
1449 *-----------------------------------------------------------------------------
1450 * pixbuf color alterations
1451 *-----------------------------------------------------------------------------
1454 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1455 gint x, gint y, gint w, gint h)
1465 pw = gdk_pixbuf_get_width(pb);
1466 ph = gdk_pixbuf_get_height(pb);
1468 if (x < 0 || x + w > pw) return;
1469 if (y < 0 || y + h > ph) return;
1471 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1472 prs = gdk_pixbuf_get_rowstride(pb);
1473 p_pix = gdk_pixbuf_get_pixels(pb);
1475 for (i = 0; i < h; i++)
1477 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1478 for (j = 0; j < w; j++)
1482 grey = (pp[0] + pp[1] + pp[2]) / 3;
1489 if (has_alpha) pp++;
1495 *-----------------------------------------------------------------------------
1496 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1498 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1508 pw = gdk_pixbuf_get_width(pb);
1509 ph = gdk_pixbuf_get_height(pb);
1511 if (x < 0 || x + w > pw) return;
1512 if (y < 0 || y + h > ph) return;
1514 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1515 prs = gdk_pixbuf_get_rowstride(pb);
1516 p_pix = gdk_pixbuf_get_pixels(pb);
1518 for (i = 0; i < h; i++)
1520 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1521 for (j = 0; j < w; j++)
1523 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1531 if (has_alpha) pp++;
1536 if (has_alpha) pp++;
1543 *-----------------------------------------------------------------------------
1544 * pixbuf ignore alpha
1545 *-----------------------------------------------------------------------------
1547 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1548 gint x, gint y, gint w, gint h)
1558 pw = gdk_pixbuf_get_width(pb);
1559 ph = gdk_pixbuf_get_height(pb);
1561 if (x < 0 || x + w > pw) return;
1562 if (y < 0 || y + h > ph) return;
1564 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1565 if (!has_alpha) return;
1567 prs = gdk_pixbuf_get_rowstride(pb);
1568 p_pix = gdk_pixbuf_get_pixels(pb);
1570 for (i = 0; i < h; i++)
1572 pp = p_pix + (y + i) * prs + (x * 4 );
1573 for (j = 0; j < w; j++)
1580 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */