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"
27 #include "icons/icons_inline.h"
33 *-----------------------------------------------------------------------------
35 *-----------------------------------------------------------------------------
38 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
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 gboolean pixbuf_to_file_as_jpg(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);
92 *-----------------------------------------------------------------------------
94 *-----------------------------------------------------------------------------
97 typedef struct _PixbufInline PixbufInline;
104 static PixbufInline inline_pixbuf_data[] = {
105 { PIXBUF_INLINE_FOLDER_CLOSED, folder_closed },
106 { PIXBUF_INLINE_FOLDER_LOCKED, folder_locked },
107 { PIXBUF_INLINE_FOLDER_OPEN, folder_open },
108 { PIXBUF_INLINE_FOLDER_UP, folder_up },
109 { PIXBUF_INLINE_SCROLLER, icon_scroller },
110 { PIXBUF_INLINE_BROKEN, icon_broken },
111 { PIXBUF_INLINE_METADATA, icon_metadata },
112 { PIXBUF_INLINE_UNKNOWN, icon_unknown },
113 { PIXBUF_INLINE_VIDEO, icon_video },
114 { PIXBUF_INLINE_ICON, gqview_icon },
115 { PIXBUF_INLINE_LOGO, geeqie_logo },
116 { PIXBUF_INLINE_ICON_FLOAT, icon_float },
117 { PIXBUF_INLINE_ICON_THUMB, icon_thumb },
118 { PIXBUF_INLINE_ICON_BOOK, icon_book },
119 { PIXBUF_INLINE_ICON_CONFIG, icon_config },
120 { PIXBUF_INLINE_ICON_TOOLS, icon_tools },
121 { PIXBUF_INLINE_ICON_VIEW, icon_view },
125 GdkPixbuf *pixbuf_inline(const gchar *key)
129 if (!key) return NULL;
132 while (inline_pixbuf_data[i].key)
134 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
136 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
141 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
146 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
148 static GtkIconFactory *icon_factory = NULL;
149 GtkIconSet *icon_set;
153 icon_factory = gtk_icon_factory_new();
154 gtk_icon_factory_add_default(icon_factory);
157 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
158 gtk_icon_factory_add(icon_factory, key, icon_set);
162 void pixbuf_inline_register_stock_icons(void)
167 while (inline_pixbuf_data[i].key)
169 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
174 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
176 GtkIconTheme *icon_theme;
178 GError *error = NULL;
180 icon_theme = gtk_icon_theme_get_default();
182 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
184 pixbuf = gtk_icon_theme_load_icon(icon_theme,
185 icon, /* icon name */
193 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
198 if (strchr(icon, '.'))
200 /* try again without extension */
201 gchar *icon2 = remove_extension_from_path(icon);
202 pixbuf = gtk_icon_theme_load_icon(icon_theme,
203 icon2, /* icon name */
209 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
216 if (!pixbuf) return FALSE;
218 register_stock_icon(key, pixbuf);
222 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
223 gint old_w, gint old_h,
224 gint *new_w, gint *new_h)
226 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
229 *new_h = (gdouble)*new_w / old_w * old_h;
230 if (*new_h < 1) *new_h = 1;
235 *new_w = (gdouble)*new_h / old_h * old_w;
236 if (*new_w < 1) *new_w = 1;
239 return (*new_w != old_w || *new_h != old_h);
242 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
246 switch (fd->format_class)
248 case FORMAT_CLASS_UNKNOWN:
249 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
251 case FORMAT_CLASS_META:
252 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
254 case FORMAT_CLASS_VIDEO:
255 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
258 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
261 if (requested_width && requested_height)
263 gint w = gdk_pixbuf_get_width(pixbuf);
264 gint h = gdk_pixbuf_get_height(pixbuf);
266 if (w > requested_width || h > requested_height)
270 if (pixbuf_scale_aspect(requested_width, requested_height,
276 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
277 g_object_unref(G_OBJECT(tmp));
286 *-----------------------------------------------------------------------------
288 *-----------------------------------------------------------------------------
291 gboolean util_clip_region(gint x, gint y, gint w, gint h,
292 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
293 gint *rx, gint *ry, gint *rw, gint *rh)
295 if (clip_x + clip_w <= x ||
297 clip_y + clip_h <= y ||
303 *rx = MAX(x, clip_x);
304 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
306 *ry = MAX(y, clip_y);
307 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
313 *-----------------------------------------------------------------------------
315 *-----------------------------------------------------------------------------
318 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
319 guchar *dest, gint dest_row_stride, gint w, gint h,
320 gint bytes_per_pixel, gboolean counter_clockwise)
326 for (i = 0; i < h; i++)
328 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
329 for (j = 0; j < w; j++)
331 if (counter_clockwise)
333 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
337 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
339 *(dp++) = *(sp++); /* r */
340 *(dp++) = *(sp++); /* g */
341 *(dp++) = *(sp++); /* b */
342 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
348 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
349 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
355 for (i = 0; i < h; i++)
357 sp = src + (i * src_row_stride);
358 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
359 memcpy(dp, sp, w * bytes_per_pixel);
363 #define ROTATE_BUFFER_WIDTH 48
364 #define ROTATE_BUFFER_HEIGHT 48
367 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
370 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
385 if (!src) return NULL;
387 sw = gdk_pixbuf_get_width(src);
388 sh = gdk_pixbuf_get_height(src);
389 has_alpha = gdk_pixbuf_get_has_alpha(src);
390 srs = gdk_pixbuf_get_rowstride(src);
391 s_pix = gdk_pixbuf_get_pixels(src);
395 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
396 drs = gdk_pixbuf_get_rowstride(dest);
397 d_pix = gdk_pixbuf_get_pixels(dest);
399 a = (has_alpha ? 4 : 3);
401 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
402 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
403 b_pix = gdk_pixbuf_get_pixels(buffer);
404 brs = gdk_pixbuf_get_rowstride(buffer);
406 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
408 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
409 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
413 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
414 pixbuf_copy_block_rotate(s_pix, srs, j, i,
416 a, counter_clockwise);
418 if (counter_clockwise)
428 pixbuf_copy_block(b_pix, brs, w, h,
429 d_pix, drs, x, y, a);
433 g_object_unref(buffer);
436 /* this is the simple version of rotation (roughly 2-4x slower) */
438 for (i = 0; i < sh; i++)
440 sp = s_pix + (i * srs);
441 for (j = 0; j < sw; j++)
443 if (counter_clockwise)
445 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
449 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
452 *(dp++) = *(sp++); /* r */
453 *(dp++) = *(sp++); /* g */
454 *(dp++) = *(sp++); /* b */
455 if (has_alpha) *(dp) = *(sp++); /* a */
464 * Returns a copy of pixbuf mirrored and or flipped.
465 * TO do a 180 degree rotations set both mirror and flipped TRUE
466 * if mirror and flip are FALSE, result is a simple copy.
468 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
481 if (!src) return NULL;
483 w = gdk_pixbuf_get_width(src);
484 h = gdk_pixbuf_get_height(src);
485 has_alpha = gdk_pixbuf_get_has_alpha(src);
486 srs = gdk_pixbuf_get_rowstride(src);
487 s_pix = gdk_pixbuf_get_pixels(src);
489 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
490 drs = gdk_pixbuf_get_rowstride(dest);
491 d_pix = gdk_pixbuf_get_pixels(dest);
493 a = has_alpha ? 4 : 3;
495 for (i = 0; i < h; i++)
497 sp = s_pix + (i * srs);
500 dp = d_pix + ((h - i - 1) * drs);
504 dp = d_pix + (i * drs);
509 for (j = 0; j < w; j++)
511 *(dp++) = *(sp++); /* r */
512 *(dp++) = *(sp++); /* g */
513 *(dp++) = *(sp++); /* b */
514 if (has_alpha) *(dp) = *(sp++); /* a */
520 for (j = 0; j < w; j++)
522 *(dp++) = *(sp++); /* r */
523 *(dp++) = *(sp++); /* g */
524 *(dp++) = *(sp++); /* b */
525 if (has_alpha) *(dp++) = *(sp++); /* a */
533 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
536 GdkPixbuf *tmp = NULL;
540 case EXIF_ORIENTATION_TOP_LEFT:
541 dest = gdk_pixbuf_copy(pixbuf);
543 case EXIF_ORIENTATION_TOP_RIGHT:
545 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
547 case EXIF_ORIENTATION_BOTTOM_RIGHT:
549 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
551 case EXIF_ORIENTATION_BOTTOM_LEFT:
553 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
555 case EXIF_ORIENTATION_LEFT_TOP:
556 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
557 dest = pixbuf_copy_rotate_90(tmp, FALSE);
559 case EXIF_ORIENTATION_RIGHT_TOP:
560 /* rotated -90 (270) */
561 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
563 case EXIF_ORIENTATION_RIGHT_BOTTOM:
564 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
565 dest = pixbuf_copy_rotate_90(tmp, TRUE);
567 case EXIF_ORIENTATION_LEFT_BOTTOM:
569 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
572 dest = gdk_pixbuf_copy(pixbuf);
575 if (tmp) g_object_unref(tmp);
582 *-----------------------------------------------------------------------------
583 * pixbuf drawing (rectangles)
584 *-----------------------------------------------------------------------------
588 * Fills region of pixbuf at x,y over w,h
589 * with colors red (r), green (g), blue (b)
590 * applying alpha (a), use a=255 for solid.
592 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
593 gint x, gint y, gint w, gint h,
594 gint r, gint g, gint b, gint a)
604 pw = gdk_pixbuf_get_width(pb);
605 ph = gdk_pixbuf_get_height(pb);
607 if (x < 0 || x + w > pw) return;
608 if (y < 0 || y + h > ph) return;
610 has_alpha = gdk_pixbuf_get_has_alpha(pb);
611 prs = gdk_pixbuf_get_rowstride(pb);
612 p_pix = gdk_pixbuf_get_pixels(pb);
614 for (i = 0; i < h; i++)
616 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
617 for (j = 0; j < w; j++)
619 *pp = (r * a + *pp * (256-a)) >> 8;
621 *pp = (g * a + *pp * (256-a)) >> 8;
623 *pp = (b * a + *pp * (256-a)) >> 8;
630 void pixbuf_draw_rect(GdkPixbuf *pb,
631 gint x, gint y, gint w, gint h,
632 gint r, gint g, gint b, gint a,
633 gint left, gint right, gint top, gint bottom)
635 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
637 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
639 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
641 pixbuf_draw_rect_fill(pb, x, y, left, h,
645 void pixbuf_set_rect_fill(GdkPixbuf *pb,
646 gint x, gint y, gint w, gint h,
647 gint r, gint g, gint b, gint a)
657 pw = gdk_pixbuf_get_width(pb);
658 ph = gdk_pixbuf_get_height(pb);
660 if (x < 0 || x + w > pw) return;
661 if (y < 0 || y + h > ph) return;
663 has_alpha = gdk_pixbuf_get_has_alpha(pb);
664 prs = gdk_pixbuf_get_rowstride(pb);
665 p_pix = gdk_pixbuf_get_pixels(pb);
667 for (i = 0; i < h; i++)
669 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
670 for (j = 0; j < w; j++)
675 if (has_alpha) { *pp = a; pp++; }
680 void pixbuf_set_rect(GdkPixbuf *pb,
681 gint x, gint y, gint w, gint h,
682 gint r, gint g, gint b, gint a,
683 gint left, gint right, gint top, gint bottom)
685 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
687 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
689 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
691 pixbuf_set_rect_fill(pb, x, y, left, h,
695 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
702 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
703 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
705 buf = gdk_pixbuf_get_pixels(pb);
706 has_alpha = gdk_pixbuf_get_has_alpha(pb);
707 rowstride = gdk_pixbuf_get_rowstride(pb);
709 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
713 if (has_alpha) *p = a;
718 *-----------------------------------------------------------------------------
719 * pixbuf text rendering
720 *-----------------------------------------------------------------------------
723 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
724 GdkPixbuf *dest, gint dx, gint dy,
726 guint8 r, guint8 g, guint8 b, guint8 a)
741 if (!src || !dest) return;
743 sw = gdk_pixbuf_get_width(src);
744 sh = gdk_pixbuf_get_height(src);
746 if (sx < 0 || sx + w > sw) return;
747 if (sy < 0 || sy + h > sh) return;
749 dw = gdk_pixbuf_get_width(dest);
750 dh = gdk_pixbuf_get_height(dest);
752 if (dx < 0 || dx + w > dw) return;
753 if (dy < 0 || dy + h > dh) return;
755 s_alpha = gdk_pixbuf_get_has_alpha(src);
756 d_alpha = gdk_pixbuf_get_has_alpha(dest);
757 srs = gdk_pixbuf_get_rowstride(src);
758 drs = gdk_pixbuf_get_rowstride(dest);
759 s_pix = gdk_pixbuf_get_pixels(src);
760 d_pix = gdk_pixbuf_get_pixels(dest);
762 s_step = (s_alpha) ? 4 : 3;
763 d_step = (d_alpha) ? 4 : 3;
765 for (i = 0; i < h; i++)
767 sp = s_pix + (sy + i) * srs + sx * s_step;
768 dp = d_pix + (dy + i) * drs + dx * d_step;
769 for (j = 0; j < w; j++)
775 asub = a * sp[0] / 255;
776 *dp = (r * asub + *dp * (256-asub)) >> 8;
778 asub = a * sp[1] / 255;
779 *dp = (g * asub + *dp * (256-asub)) >> 8;
781 asub = a * sp[2] / 255;
782 *dp = (b * asub + *dp * (256-asub)) >> 8;
787 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
801 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
803 guint8 r, guint8 g, guint8 b, guint8 a)
809 cairo_surface_t *source;
812 pango_layout_get_pixel_size(layout, &w, &h);
813 if (w < 1 || h < 1) return;
815 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
817 cr = cairo_create (source);
818 cairo_set_source_rgb(cr, 0, 0, 0);
819 cairo_rectangle (cr, 0, 0, w, h);
821 cairo_set_source_rgb(cr, 1, 1, 1);
822 pango_cairo_show_layout (cr, layout);
825 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
827 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
829 cairo_image_surface_get_width (source),
830 cairo_image_surface_get_height (source),
831 cairo_image_surface_get_stride (source),
837 dw = gdk_pixbuf_get_width(pixbuf);
838 dh = gdk_pixbuf_get_height(pixbuf);
854 if (x + w > dw) w = dw - x;
855 if (y + h > dh) h = dh - y;
857 pixbuf_copy_font(buffer, sx, sy,
861 g_object_unref(buffer);
862 cairo_surface_destroy(source);
866 *-----------------------------------------------------------------------------
867 * pixbuf drawing (triangle)
868 *-----------------------------------------------------------------------------
871 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
872 gint *rx, gint *ry, gint *rw, gint *rh)
880 tw = MAX(abs(x1 - x2), abs(x2 - x3));
881 tw = MAX(tw, abs(x3 - x1));
882 th = MAX(abs(y1 - y2), abs(y2 - y3));
883 th = MAX(th, abs(y3 - y1));
891 void pixbuf_draw_triangle(GdkPixbuf *pb,
892 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
893 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
894 guint8 r, guint8 g, guint8 b, guint8 a)
906 gdouble slope1, slope2;
907 gint slope1_x, slope1_y;
910 gboolean middle = FALSE;
914 pw = gdk_pixbuf_get_width(pb);
915 ph = gdk_pixbuf_get_height(pb);
917 if (!util_clip_region(0, 0, pw, ph,
918 clip_x, clip_y, clip_w, clip_h,
919 &rx, &ry, &rw, &rh)) return;
921 util_clip_triangle(x1, y1, x2, y2, x3, y3,
924 if (!util_clip_region(rx, ry, rw, rh,
926 &fx1, &fy1, &fw, &fh)) return;
930 has_alpha = gdk_pixbuf_get_has_alpha(pb);
931 prs = gdk_pixbuf_get_rowstride(pb);
932 p_pix = gdk_pixbuf_get_pixels(pb);
934 p_step = (has_alpha) ? 4 : 3;
938 t = x1; x1 = x2; x2 = t;
939 t = y1; y1 = y2; y2 = t;
943 t = x2; x2 = x3; x3 = t;
944 t = y2; y2 = y3; y3 = t;
948 t = x1; x1 = x2; x2 = t;
949 t = y1; y1 = y2; y2 = t;
952 slope1 = (gdouble)(y2 - y1);
953 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
956 slope2 = (gdouble)(y3 - y1);
957 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
959 for (y = fy1; y < fy2; y++)
963 if (!middle && y > y2)
965 slope1 = (gdouble)(y3 - y2);
966 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
973 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
974 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
978 t = xa; xa = xb; xb = t;
981 xa = CLAMP(xa, fx1, fx2);
982 xb = CLAMP(xb, fx1, fx2);
984 pp = p_pix + y * prs + xa * p_step;
988 *pp = (r * a + *pp * (256-a)) >> 8;
990 *pp = (g * a + *pp * (256-a)) >> 8;
992 *pp = (b * a + *pp * (256-a)) >> 8;
1002 *-----------------------------------------------------------------------------
1003 * pixbuf drawing (line)
1004 *-----------------------------------------------------------------------------
1007 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1008 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1009 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1011 gboolean flip = FALSE;
1018 t = x1; x1 = x2; x2 = t;
1019 t = y1; y1 = y2; y2 = t;
1023 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1027 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1031 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1039 slope = (y2 - y1) / d;
1042 y1 = y1 + slope * (clip_x - x1);
1045 if (x2 > clip_x + clip_w)
1047 y2 = y2 + slope * (clip_x + clip_w - x2);
1048 x2 = clip_x + clip_w;
1054 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1060 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1062 t = x1; x1 = x2; x2 = t;
1063 t = y1; y1 = y2; y2 = t;
1072 slope = (x2 - x1) / d;
1075 x1 = x1 + slope * (clip_y - y1);
1078 if (y2 > clip_y + clip_h)
1080 x2 = x2 + slope * (clip_y + clip_h - y2);
1081 y2 = clip_y + clip_h;
1103 void pixbuf_draw_line(GdkPixbuf *pb,
1104 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1105 gint x1, gint y1, gint x2, gint y2,
1106 guint8 r, guint8 g, guint8 b, guint8 a)
1110 gint rx, ry, rw, rh;
1111 gdouble rx1, ry1, rx2, ry2;
1118 gint cx1, cy1, cx2, cy2;
1122 pw = gdk_pixbuf_get_width(pb);
1123 ph = gdk_pixbuf_get_height(pb);
1125 if (!util_clip_region(0, 0, pw, ph,
1126 clip_x, clip_y, clip_w, clip_h,
1127 &rx, &ry, &rw, &rh)) return;
1128 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1129 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1130 &rx1, &ry1, &rx2, &ry2)) return;
1137 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1138 prs = gdk_pixbuf_get_rowstride(pb);
1139 p_pix = gdk_pixbuf_get_pixels(pb);
1141 p_step = (has_alpha) ? 4 : 3;
1143 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1148 t = rx1; rx1 = rx2; rx2 = t;
1149 t = ry1; ry1 = ry2; ry2 = t;
1153 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1154 for (x = rx1; x < rx2; x += 1.0)
1156 px = (gint)(x + 0.5);
1157 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1159 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1161 pp = p_pix + py * prs + px * p_step;
1162 *pp = (r * a + *pp * (256-a)) >> 8;
1164 *pp = (g * a + *pp * (256-a)) >> 8;
1166 *pp = (b * a + *pp * (256-a)) >> 8;
1175 t = rx1; rx1 = rx2; rx2 = t;
1176 t = ry1; ry1 = ry2; ry2 = t;
1180 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1181 for (y = ry1; y < ry2; y += 1.0)
1183 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1184 py = (gint)(y + 0.5);
1186 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1188 pp = p_pix + py * prs + px * p_step;
1189 *pp = (r * a + *pp * (256-a)) >> 8;
1191 *pp = (g * a + *pp * (256-a)) >> 8;
1193 *pp = (b * a + *pp * (256-a)) >> 8;
1200 *-----------------------------------------------------------------------------
1201 * pixbuf drawing (fades and shadows)
1202 *-----------------------------------------------------------------------------
1205 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1206 gint s, gboolean vertical, gint border,
1207 gint x1, gint y1, gint x2, gint y2,
1208 guint8 r, guint8 g, guint8 b, guint8 a)
1215 p_step = (has_alpha) ? 4 : 3;
1216 for (j = y1; j < y2; j++)
1218 pp = p_pix + j * prs + x1 * p_step;
1219 if (!vertical) n = a - a * abs(j - s) / border;
1220 for (i = x1; i < x2; i++)
1222 if (vertical) n = a - a * abs(i - s) / border;
1223 *pp = (r * n + *pp * (256-n)) >> 8;
1225 *pp = (g * n + *pp * (256-n)) >> 8;
1227 *pp = (b * n + *pp * (256-n)) >> 8;
1229 if (has_alpha) pp++;
1234 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1235 gint sx, gint sy, gint border,
1236 gint x1, gint y1, gint x2, gint y2,
1237 guint8 r, guint8 g, guint8 b, guint8 a)
1243 p_step = (has_alpha) ? 4 : 3;
1244 for (j = y1; j < y2; j++)
1246 pp = p_pix + j * prs + x1 * p_step;
1247 for (i = x1; i < x2; i++)
1252 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1253 n = a - a * r / border;
1254 *pp = (r * n + *pp * (256-n)) >> 8;
1256 *pp = (g * n + *pp * (256-n)) >> 8;
1258 *pp = (b * n + *pp * (256-n)) >> 8;
1260 if (has_alpha) pp++;
1265 void pixbuf_draw_shadow(GdkPixbuf *pb,
1266 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1267 gint x, gint y, gint w, gint h, gint border,
1268 guint8 r, guint8 g, guint8 b, guint8 a)
1272 gint rx, ry, rw, rh;
1273 gint fx, fy, fw, fh;
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;
1285 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1286 prs = gdk_pixbuf_get_rowstride(pb);
1287 p_pix = gdk_pixbuf_get_pixels(pb);
1289 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1291 &fx, &fy, &fw, &fh))
1293 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1296 if (border < 1) return;
1298 if (util_clip_region(x, y + border, border, h - border * 2,
1300 &fx, &fy, &fw, &fh))
1302 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1303 x + border, TRUE, border,
1304 fx, fy, fx + fw, fy + fh,
1307 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1309 &fx, &fy, &fw, &fh))
1311 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1312 x + w - border, TRUE, border,
1313 fx, fy, fx + fw, fy + fh,
1316 if (util_clip_region(x + border, y, w - border * 2, border,
1318 &fx, &fy, &fw, &fh))
1320 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1321 y + border, FALSE, border,
1322 fx, fy, fx + fw, fy + fh,
1325 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1327 &fx, &fy, &fw, &fh))
1329 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1330 y + h - border, FALSE, border,
1331 fx, fy, fx + fw, fy + fh,
1334 if (util_clip_region(x, y, border, border,
1336 &fx, &fy, &fw, &fh))
1338 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1339 x + border, y + border, border,
1340 fx, fy, fx + fw, fy + fh,
1343 if (util_clip_region(x + w - border, y, border, border,
1345 &fx, &fy, &fw, &fh))
1347 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1348 x + w - border, y + border, border,
1349 fx, fy, fx + fw, fy + fh,
1352 if (util_clip_region(x, y + h - border, border, border,
1354 &fx, &fy, &fw, &fh))
1356 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1357 x + border, y + h - border, border,
1358 fx, fy, fx + fw, fy + fh,
1361 if (util_clip_region(x + w - border, y + h - border, border, border,
1363 &fx, &fy, &fw, &fh))
1365 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1366 x + w - border, y + h - border, border,
1367 fx, fy, fx + fw, fy + fh,
1374 *-----------------------------------------------------------------------------
1375 * pixbuf color alterations
1376 *-----------------------------------------------------------------------------
1379 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1380 gint x, gint y, gint w, gint h)
1390 pw = gdk_pixbuf_get_width(pb);
1391 ph = gdk_pixbuf_get_height(pb);
1393 if (x < 0 || x + w > pw) return;
1394 if (y < 0 || y + h > ph) return;
1396 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1397 prs = gdk_pixbuf_get_rowstride(pb);
1398 p_pix = gdk_pixbuf_get_pixels(pb);
1400 for (i = 0; i < h; i++)
1402 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1403 for (j = 0; j < w; j++)
1407 grey = (pp[0] + pp[1] + pp[2]) / 3;
1414 if (has_alpha) pp++;
1418 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */