4 * Copyright (C) 2008 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
15 #include "pixbuf_util.h"
17 #include "icons/icons_inline.h"
23 *-----------------------------------------------------------------------------
25 *-----------------------------------------------------------------------------
28 gboolean pixbuf_to_file_as_png (GdkPixbuf *pixbuf, const char *filename)
33 if (!pixbuf || !filename) return FALSE;
35 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
36 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
40 printf("Error saving png file: %s\n", error->message);
48 *-----------------------------------------------------------------------------
50 *-----------------------------------------------------------------------------
53 gboolean pixbuf_to_file_as_jpg(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
59 if (!pixbuf || !filename) return FALSE;
61 if (quality == -1) quality = 75;
62 if (quality < 1 || quality > 100)
64 printf("Jpeg not saved, invalid quality %d\n", quality);
68 qbuf = g_strdup_printf("%d", quality);
69 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
74 printf("Error saving jpeg to %s\n%s\n", filename, error->message);
82 *-----------------------------------------------------------------------------
84 *-----------------------------------------------------------------------------
87 typedef struct _PixbufInline PixbufInline;
94 static PixbufInline inline_pixbuf_data[] = {
95 { PIXBUF_INLINE_FOLDER_CLOSED, folder_closed },
96 { PIXBUF_INLINE_FOLDER_LOCKED, folder_locked },
97 { PIXBUF_INLINE_FOLDER_OPEN, folder_open },
98 { PIXBUF_INLINE_FOLDER_UP, folder_up },
99 { PIXBUF_INLINE_SCROLLER, icon_scroller },
100 { PIXBUF_INLINE_BROKEN, icon_broken },
101 { PIXBUF_INLINE_ICON, gqview_icon },
102 { PIXBUF_INLINE_LOGO, geeqie_logo },
103 { PIXBUF_INLINE_ICON_FLOAT, icon_float },
104 { PIXBUF_INLINE_ICON_THUMB, icon_thumb },
105 { PIXBUF_INLINE_ICON_BOOK, icon_book },
106 { PIXBUF_INLINE_ICON_CONFIG, icon_config },
107 { PIXBUF_INLINE_ICON_TOOLS, icon_tools },
108 { PIXBUF_INLINE_ICON_VIEW, icon_view },
112 GdkPixbuf *pixbuf_inline(const gchar *key)
116 if (!key) return NULL;
119 while (inline_pixbuf_data[i].key)
121 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
123 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
128 printf("warning: inline pixbuf key \"%s\" not found.\n", key);
134 *-----------------------------------------------------------------------------
136 *-----------------------------------------------------------------------------
139 gint util_clip_region(gint x, gint y, gint w, gint h,
140 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
141 gint *rx, gint *ry, gint *rw, gint *rh)
143 if (clip_x + clip_w <= x ||
145 clip_y + clip_h <= y ||
151 *rx = MAX(x, clip_x);
152 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
154 *ry = MAX(y, clip_y);
155 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
161 *-----------------------------------------------------------------------------
163 *-----------------------------------------------------------------------------
166 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
167 guchar *dest, gint dest_row_stride, gint w, gint h,
168 gint bytes_per_pixel, gint counter_clockwise)
174 for (i = 0; i < h; i++)
176 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
177 for (j = 0; j < w; j++)
179 if (counter_clockwise)
181 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
185 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
187 *(dp++) = *(sp++); /* r */
188 *(dp++) = *(sp++); /* g */
189 *(dp++) = *(sp++); /* b */
190 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
196 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
197 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
203 for (i = 0; i < h; i++)
205 sp = src + (i * src_row_stride);
206 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
207 memcpy(dp, sp, w * bytes_per_pixel);
211 #define ROTATE_BUFFER_WIDTH 48
212 #define ROTATE_BUFFER_HEIGHT 48
215 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
218 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gint counter_clockwise)
237 if (!src) return NULL;
239 sw = gdk_pixbuf_get_width(src);
240 sh = gdk_pixbuf_get_height(src);
241 has_alpha = gdk_pixbuf_get_has_alpha(src);
242 srs = gdk_pixbuf_get_rowstride(src);
243 s_pix = gdk_pixbuf_get_pixels(src);
247 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
248 drs = gdk_pixbuf_get_rowstride(dest);
249 d_pix = gdk_pixbuf_get_pixels(dest);
251 a = (has_alpha ? 4 : 3);
253 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
254 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
255 b_pix = gdk_pixbuf_get_pixels(buffer);
256 brs = gdk_pixbuf_get_rowstride(buffer);
258 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
260 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
261 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
265 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
266 pixbuf_copy_block_rotate(s_pix, srs, j, i,
268 a, counter_clockwise);
270 if (counter_clockwise)
280 pixbuf_copy_block(b_pix, brs, w, h,
281 d_pix, drs, x, y, a);
285 gdk_pixbuf_unref(buffer);
288 /* this is the simple version of rotation (roughly 2-4x slower) */
290 for (i = 0; i < sh; i++)
292 sp = s_pix + (i * srs);
293 for (j = 0; j < sw; j++)
295 if (counter_clockwise)
297 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
301 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
304 *(dp++) = *(sp++); /* r */
305 *(dp++) = *(sp++); /* g */
306 *(dp++) = *(sp++); /* b */
307 if (has_alpha) *(dp) = *(sp++); /* a */
316 * Returns a copy of pixbuf mirrored and or flipped.
317 * TO do a 180 degree rotations set both mirror and flipped TRUE
318 * if mirror and flip are FALSE, result is a simple copy.
320 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gint mirror, gint flip)
333 if (!src) return NULL;
335 w = gdk_pixbuf_get_width(src);
336 h = gdk_pixbuf_get_height(src);
337 has_alpha = gdk_pixbuf_get_has_alpha(src);
338 srs = gdk_pixbuf_get_rowstride(src);
339 s_pix = gdk_pixbuf_get_pixels(src);
341 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
342 drs = gdk_pixbuf_get_rowstride(dest);
343 d_pix = gdk_pixbuf_get_pixels(dest);
345 a = has_alpha ? 4 : 3;
347 for (i = 0; i < h; i++)
349 sp = s_pix + (i * srs);
352 dp = d_pix + ((h - i - 1) * drs);
356 dp = d_pix + (i * drs);
361 for (j = 0; j < w; j++)
363 *(dp++) = *(sp++); /* r */
364 *(dp++) = *(sp++); /* g */
365 *(dp++) = *(sp++); /* b */
366 if (has_alpha) *(dp) = *(sp++); /* a */
372 for (j = 0; j < w; j++)
374 *(dp++) = *(sp++); /* r */
375 *(dp++) = *(sp++); /* g */
376 *(dp++) = *(sp++); /* b */
377 if (has_alpha) *(dp++) = *(sp++); /* a */
387 *-----------------------------------------------------------------------------
388 * pixbuf drawing (rectangles)
389 *-----------------------------------------------------------------------------
393 * Fills region of pixbuf at x,y over w,h
394 * with colors red (r), green (g), blue (b)
395 * applying alpha (a), use a=255 for solid.
397 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
398 gint x, gint y, gint w, gint h,
399 gint r, gint g, gint b, gint a)
409 pw = gdk_pixbuf_get_width(pb);
410 ph = gdk_pixbuf_get_height(pb);
412 if (x < 0 || x + w > pw) return;
413 if (y < 0 || y + h > ph) return;
415 p_alpha = gdk_pixbuf_get_has_alpha(pb);
416 prs = gdk_pixbuf_get_rowstride(pb);
417 p_pix = gdk_pixbuf_get_pixels(pb);
419 for (i = 0; i < h; i++)
421 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
422 for (j = 0; j < w; j++)
424 *pp = (r * a + *pp * (256-a)) >> 8;
426 *pp = (g * a + *pp * (256-a)) >> 8;
428 *pp = (b * a + *pp * (256-a)) >> 8;
435 void pixbuf_draw_rect(GdkPixbuf *pb,
436 gint x, gint y, gint w, gint h,
437 gint r, gint g, gint b, gint a,
438 gint left, gint right, gint top, gint bottom)
440 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
442 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
444 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
446 pixbuf_draw_rect_fill(pb, x, y, left, h,
450 void pixbuf_set_rect_fill(GdkPixbuf *pb,
451 gint x, gint y, gint w, gint h,
452 gint r, gint g, gint b, gint a)
462 pw = gdk_pixbuf_get_width(pb);
463 ph = gdk_pixbuf_get_height(pb);
465 if (x < 0 || x + w > pw) return;
466 if (y < 0 || y + h > ph) return;
468 p_alpha = gdk_pixbuf_get_has_alpha(pb);
469 prs = gdk_pixbuf_get_rowstride(pb);
470 p_pix = gdk_pixbuf_get_pixels(pb);
472 for (i = 0; i < h; i++)
474 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
475 for (j = 0; j < w; j++)
480 if (p_alpha) { *pp = a; pp++; }
485 void pixbuf_set_rect(GdkPixbuf *pb,
486 gint x, gint y, gint w, gint h,
487 gint r, gint g, gint b, gint a,
488 gint left, gint right, gint top, gint bottom)
490 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
492 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
494 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
496 pixbuf_set_rect_fill(pb, x, y, left, h,
500 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
507 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
508 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
510 buf = gdk_pixbuf_get_pixels(pb);
511 has_alpha = gdk_pixbuf_get_has_alpha(pb);
512 rowstride = gdk_pixbuf_get_rowstride(pb);
514 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
518 if (has_alpha) *p = a;
523 *-----------------------------------------------------------------------------
524 * pixbuf text rendering
525 *-----------------------------------------------------------------------------
528 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
529 GdkPixbuf *dest, gint dx, gint dy,
531 guint8 r, guint8 g, guint8 b, guint8 a)
546 if (!src || !dest) return;
548 sw = gdk_pixbuf_get_width(src);
549 sh = gdk_pixbuf_get_height(src);
551 if (sx < 0 || sx + w > sw) return;
552 if (sy < 0 || sy + h > sh) return;
554 dw = gdk_pixbuf_get_width(dest);
555 dh = gdk_pixbuf_get_height(dest);
557 if (dx < 0 || dx + w > dw) return;
558 if (dy < 0 || dy + h > dh) return;
560 s_alpha = gdk_pixbuf_get_has_alpha(src);
561 d_alpha = gdk_pixbuf_get_has_alpha(dest);
562 srs = gdk_pixbuf_get_rowstride(src);
563 drs = gdk_pixbuf_get_rowstride(dest);
564 s_pix = gdk_pixbuf_get_pixels(src);
565 d_pix = gdk_pixbuf_get_pixels(dest);
567 s_step = (s_alpha) ? 4 : 3;
568 d_step = (d_alpha) ? 4 : 3;
570 for (i = 0; i < h; i++)
572 sp = s_pix + (sy + i) * srs + sx * s_step;
573 dp = d_pix + (dy + i) * drs + dx * d_step;
574 for (j = 0; j < w; j++)
580 asub = a * sp[0] / 255;
581 *dp = (r * asub + *dp * (256-asub)) >> 8;
583 asub = a * sp[1] / 255;
584 *dp = (g * asub + *dp * (256-asub)) >> 8;
586 asub = a * sp[2] / 255;
587 *dp = (b * asub + *dp * (256-asub)) >> 8;
592 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
606 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
608 guint8 r, guint8 g, guint8 b, guint8 a)
617 if (!widget || !widget->window) return;
619 pango_layout_get_pixel_size(layout, &w, &h);
620 if (w < 1 || h < 1) return;
622 pixmap = gdk_pixmap_new(widget->window, w, h, -1);
624 gc = gdk_gc_new(widget->window);
625 gdk_gc_copy(gc, widget->style->black_gc);
626 gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, w, h);
627 gdk_gc_copy(gc, widget->style->white_gc);
628 gdk_draw_layout(pixmap, gc, 0, 0, layout);
631 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, w, h);
632 gdk_pixbuf_get_from_drawable(buffer, pixmap,
633 gdk_drawable_get_colormap(widget->window),
635 g_object_unref(pixmap);
639 dw = gdk_pixbuf_get_width(pixbuf);
640 dh = gdk_pixbuf_get_height(pixbuf);
656 if (x + w > dw) w = dw - x;
657 if (y + h > dh) h = dh - y;
659 pixbuf_copy_font(buffer, sx, sy,
663 g_object_unref(buffer);
667 *-----------------------------------------------------------------------------
668 * pixbuf drawing (triangle)
669 *-----------------------------------------------------------------------------
672 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
673 gint *rx, gint *ry, gint *rw, gint *rh)
681 tw = MAX(abs(x1 - x2), abs(x2 - x3));
682 tw = MAX(tw, abs(x3 - x1));
683 th = MAX(abs(y1 - y2), abs(y2 - y3));
684 th = MAX(th, abs(y3 - y1));
692 void pixbuf_draw_triangle(GdkPixbuf *pb,
693 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
694 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
695 guint8 r, guint8 g, guint8 b, guint8 a)
707 gdouble slope1, slope2;
708 gint slope1_x, slope1_y;
715 pw = gdk_pixbuf_get_width(pb);
716 ph = gdk_pixbuf_get_height(pb);
718 if (!util_clip_region(0, 0, pw, ph,
719 clip_x, clip_y, clip_w, clip_h,
720 &rx, &ry, &rw, &rh)) return;
722 util_clip_triangle(x1, y1, x2, y2, x3, y3,
725 if (!util_clip_region(rx, ry, rw, rh,
727 &fx1, &fy1, &fw, &fh)) return;
731 p_alpha = gdk_pixbuf_get_has_alpha(pb);
732 prs = gdk_pixbuf_get_rowstride(pb);
733 p_pix = gdk_pixbuf_get_pixels(pb);
735 p_step = (p_alpha) ? 4 : 3;
739 t = x1; x1 = x2; x2 = t;
740 t = y1; y1 = y2; y2 = t;
744 t = x2; x2 = x3; x3 = t;
745 t = y2; y2 = y3; y3 = t;
749 t = x1; x1 = x2; x2 = t;
750 t = y1; y1 = y2; y2 = t;
753 slope1 = (gdouble)(y2 - y1);
754 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
757 slope2 = (gdouble)(y3 - y1);
758 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
760 for (y = fy1; y < fy2; y++)
764 if (!middle && y > y2)
766 slope1 = (gdouble)(y3 - y2);
767 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
774 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
775 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
779 t = xa; xa = xb; xb = t;
782 xa = CLAMP(xa, fx1, fx2);
783 xb = CLAMP(xb, fx1, fx2);
785 pp = p_pix + y * prs + xa * p_step;
789 *pp = (r * a + *pp * (256-a)) >> 8;
791 *pp = (g * a + *pp * (256-a)) >> 8;
793 *pp = (b * a + *pp * (256-a)) >> 8;
803 *-----------------------------------------------------------------------------
804 * pixbuf drawing (line)
805 *-----------------------------------------------------------------------------
808 static gint util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
809 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
810 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
819 t = x1; x1 = x2; x2 = t;
820 t = y1; y1 = y2; y2 = t;
824 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
828 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
832 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
836 if (x1 >= clip_x && x2 <= clip_x + clip_w)
840 if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
844 if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
854 slope = (y2 - y1) / d;
857 y1 = y1 + slope * (clip_x - x1);
860 if (x2 > clip_x + clip_w)
862 y2 = y2 + slope * (clip_x + clip_w - x2);
863 x2 = clip_x + clip_w;
869 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
875 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
877 t = x1; x1 = x2; x2 = t;
878 t = y1; y1 = y2; y2 = t;
887 slope = (x2 - x1) / d;
890 x1 = x1 + slope * (clip_y - y1);
893 if (y2 > clip_y + clip_h)
895 x2 = x2 + slope * (clip_y + clip_h - y2);
896 y2 = clip_y + clip_h;
918 void pixbuf_draw_line(GdkPixbuf *pb,
919 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
920 gint x1, gint y1, gint x2, gint y2,
921 guint8 r, guint8 g, guint8 b, guint8 a)
926 gdouble rx1, ry1, rx2, ry2;
933 gint cx1, cy1, cx2, cy2;
937 pw = gdk_pixbuf_get_width(pb);
938 ph = gdk_pixbuf_get_height(pb);
940 if (!util_clip_region(0, 0, pw, ph,
941 clip_x, clip_y, clip_w, clip_h,
942 &rx, &ry, &rw, &rh)) return;
943 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
944 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
945 &rx1, &ry1, &rx2, &ry2)) return;
952 p_alpha = gdk_pixbuf_get_has_alpha(pb);
953 prs = gdk_pixbuf_get_rowstride(pb);
954 p_pix = gdk_pixbuf_get_pixels(pb);
956 p_step = (p_alpha) ? 4 : 3;
958 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
963 t = rx1; rx1 = rx2; rx2 = t;
964 t = ry1; ry1 = ry2; ry2 = t;
968 if (slope != 0.0) slope = (ry2 - ry1) / slope;
969 for (x = rx1; x < rx2; x += 1.0)
971 px = (gint)(x + 0.5);
972 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
974 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
976 pp = p_pix + py * prs + px * p_step;
977 *pp = (r * a + *pp * (256-a)) >> 8;
979 *pp = (g * a + *pp * (256-a)) >> 8;
981 *pp = (b * a + *pp * (256-a)) >> 8;
990 t = rx1; rx1 = rx2; rx2 = t;
991 t = ry1; ry1 = ry2; ry2 = t;
995 if (slope != 0.0) slope = (rx2 - rx1) / slope;
996 for (y = ry1; y < ry2; y += 1.0)
998 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
999 py = (gint)(y + 0.5);
1001 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1003 pp = p_pix + py * prs + px * p_step;
1004 *pp = (r * a + *pp * (256-a)) >> 8;
1006 *pp = (g * a + *pp * (256-a)) >> 8;
1008 *pp = (b * a + *pp * (256-a)) >> 8;
1015 *-----------------------------------------------------------------------------
1016 * pixbuf drawing (fades and shadows)
1017 *-----------------------------------------------------------------------------
1020 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gint p_alpha,
1021 gint s, gint vertical, gint border,
1022 gint x1, gint y1, gint x2, gint y2,
1023 guint8 r, guint8 g, guint8 b, guint8 a)
1030 p_step = (p_alpha) ? 4 : 3;
1031 for (j = y1; j < y2; j++)
1033 pp = p_pix + j * prs + x1 * p_step;
1034 if (!vertical) n = a - a * abs(j - s) / border;
1035 for (i = x1; i < x2; i++)
1037 if (vertical) n = a - a * abs(i - s) / border;
1038 *pp = (r * n + *pp * (256-n)) >> 8;
1040 *pp = (g * n + *pp * (256-n)) >> 8;
1042 *pp = (b * n + *pp * (256-n)) >> 8;
1049 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gint p_alpha,
1050 gint sx, gint sy, gint border,
1051 gint x1, gint y1, gint x2, gint y2,
1052 guint8 r, guint8 g, guint8 b, guint8 a)
1058 p_step = (p_alpha) ? 4 : 3;
1059 for (j = y1; j < y2; j++)
1061 pp = p_pix + j * prs + x1 * p_step;
1062 for (i = x1; i < x2; i++)
1067 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1068 n = a - a * r / border;
1069 *pp = (r * n + *pp * (256-n)) >> 8;
1071 *pp = (g * n + *pp * (256-n)) >> 8;
1073 *pp = (b * n + *pp * (256-n)) >> 8;
1080 void pixbuf_draw_shadow(GdkPixbuf *pb,
1081 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1082 gint x, gint y, gint w, gint h, gint border,
1083 guint8 r, guint8 g, guint8 b, guint8 a)
1087 gint rx, ry, rw, rh;
1088 gint fx, fy, fw, fh;
1093 pw = gdk_pixbuf_get_width(pb);
1094 ph = gdk_pixbuf_get_height(pb);
1096 if (!util_clip_region(0, 0, pw, ph,
1097 clip_x, clip_y, clip_w, clip_h,
1098 &rx, &ry, &rw, &rh)) return;
1100 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1101 prs = gdk_pixbuf_get_rowstride(pb);
1102 p_pix = gdk_pixbuf_get_pixels(pb);
1104 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1106 &fx, &fy, &fw, &fh))
1108 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1111 if (border < 1) return;
1113 if (util_clip_region(x, y + border, border, h - border * 2,
1115 &fx, &fy, &fw, &fh))
1117 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1118 x + border, TRUE, border,
1119 fx, fy, fx + fw, fy + fh,
1122 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1124 &fx, &fy, &fw, &fh))
1126 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1127 x + w - border, TRUE, border,
1128 fx, fy, fx + fw, fy + fh,
1131 if (util_clip_region(x + border, y, w - border * 2, border,
1133 &fx, &fy, &fw, &fh))
1135 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1136 y + border, FALSE, border,
1137 fx, fy, fx + fw, fy + fh,
1140 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1142 &fx, &fy, &fw, &fh))
1144 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1145 y + h - border, FALSE, border,
1146 fx, fy, fx + fw, fy + fh,
1149 if (util_clip_region(x, y, border, border,
1151 &fx, &fy, &fw, &fh))
1153 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1154 x + border, y + border, border,
1155 fx, fy, fx + fw, fy + fh,
1158 if (util_clip_region(x + w - border, y, border, border,
1160 &fx, &fy, &fw, &fh))
1162 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1163 x + w - border, y + border, border,
1164 fx, fy, fx + fw, fy + fh,
1167 if (util_clip_region(x, y + h - border, border, border,
1169 &fx, &fy, &fw, &fh))
1171 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1172 x + border, y + h - border, border,
1173 fx, fy, fx + fw, fy + fh,
1176 if (util_clip_region(x + w - border, y + h - border, border, border,
1178 &fx, &fy, &fw, &fh))
1180 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1181 x + w - border, y + h - border, border,
1182 fx, fy, fx + fw, fy + fh,
1189 *-----------------------------------------------------------------------------
1190 * pixbuf color alterations
1191 *-----------------------------------------------------------------------------
1194 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1195 gint x, gint y, gint w, gint h)
1205 pw = gdk_pixbuf_get_width(pb);
1206 ph = gdk_pixbuf_get_height(pb);
1208 if (x < 0 || x + w > pw) return;
1209 if (y < 0 || y + h > ph) return;
1211 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1212 prs = gdk_pixbuf_get_rowstride(pb);
1213 p_pix = gdk_pixbuf_get_pixels(pb);
1215 for (i = 0; i < h; i++)
1217 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
1218 for (j = 0; j < w; j++)
1222 grey = (pp[0] + pp[1] + pp[2]) / 3;