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"
18 #include "icons/icons_inline.h"
24 *-----------------------------------------------------------------------------
26 *-----------------------------------------------------------------------------
29 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const char *filename)
34 if (!pixbuf || !filename) return FALSE;
36 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
37 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
41 log_printf("Error saving png file: %s\n", error->message);
49 *-----------------------------------------------------------------------------
51 *-----------------------------------------------------------------------------
54 gboolean pixbuf_to_file_as_jpg(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
60 if (!pixbuf || !filename) return FALSE;
62 if (quality == -1) quality = 75;
63 if (quality < 1 || quality > 100)
65 log_printf("Jpeg not saved, invalid quality %d\n", quality);
69 qbuf = g_strdup_printf("%d", quality);
70 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
75 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
83 *-----------------------------------------------------------------------------
85 *-----------------------------------------------------------------------------
88 typedef struct _PixbufInline PixbufInline;
95 static PixbufInline inline_pixbuf_data[] = {
96 { PIXBUF_INLINE_FOLDER_CLOSED, folder_closed },
97 { PIXBUF_INLINE_FOLDER_LOCKED, folder_locked },
98 { PIXBUF_INLINE_FOLDER_OPEN, folder_open },
99 { PIXBUF_INLINE_FOLDER_UP, folder_up },
100 { PIXBUF_INLINE_SCROLLER, icon_scroller },
101 { PIXBUF_INLINE_BROKEN, icon_broken },
102 { PIXBUF_INLINE_ICON, gqview_icon },
103 { PIXBUF_INLINE_LOGO, geeqie_logo },
104 { PIXBUF_INLINE_ICON_FLOAT, icon_float },
105 { PIXBUF_INLINE_ICON_THUMB, icon_thumb },
106 { PIXBUF_INLINE_ICON_BOOK, icon_book },
107 { PIXBUF_INLINE_ICON_CONFIG, icon_config },
108 { PIXBUF_INLINE_ICON_TOOLS, icon_tools },
109 { PIXBUF_INLINE_ICON_VIEW, icon_view },
113 GdkPixbuf *pixbuf_inline(const gchar *key)
117 if (!key) return NULL;
120 while (inline_pixbuf_data[i].key)
122 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
124 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
129 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
135 *-----------------------------------------------------------------------------
137 *-----------------------------------------------------------------------------
140 gint util_clip_region(gint x, gint y, gint w, gint h,
141 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
142 gint *rx, gint *ry, gint *rw, gint *rh)
144 if (clip_x + clip_w <= x ||
146 clip_y + clip_h <= y ||
152 *rx = MAX(x, clip_x);
153 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
155 *ry = MAX(y, clip_y);
156 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
162 *-----------------------------------------------------------------------------
164 *-----------------------------------------------------------------------------
167 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
168 guchar *dest, gint dest_row_stride, gint w, gint h,
169 gint bytes_per_pixel, gint counter_clockwise)
175 for (i = 0; i < h; i++)
177 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
178 for (j = 0; j < w; j++)
180 if (counter_clockwise)
182 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
186 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
188 *(dp++) = *(sp++); /* r */
189 *(dp++) = *(sp++); /* g */
190 *(dp++) = *(sp++); /* b */
191 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
197 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
198 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
204 for (i = 0; i < h; i++)
206 sp = src + (i * src_row_stride);
207 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
208 memcpy(dp, sp, w * bytes_per_pixel);
212 #define ROTATE_BUFFER_WIDTH 48
213 #define ROTATE_BUFFER_HEIGHT 48
216 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
219 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gint counter_clockwise)
238 if (!src) return NULL;
240 sw = gdk_pixbuf_get_width(src);
241 sh = gdk_pixbuf_get_height(src);
242 has_alpha = gdk_pixbuf_get_has_alpha(src);
243 srs = gdk_pixbuf_get_rowstride(src);
244 s_pix = gdk_pixbuf_get_pixels(src);
248 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
249 drs = gdk_pixbuf_get_rowstride(dest);
250 d_pix = gdk_pixbuf_get_pixels(dest);
252 a = (has_alpha ? 4 : 3);
254 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
255 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
256 b_pix = gdk_pixbuf_get_pixels(buffer);
257 brs = gdk_pixbuf_get_rowstride(buffer);
259 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
261 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
262 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
266 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
267 pixbuf_copy_block_rotate(s_pix, srs, j, i,
269 a, counter_clockwise);
271 if (counter_clockwise)
281 pixbuf_copy_block(b_pix, brs, w, h,
282 d_pix, drs, x, y, a);
286 gdk_pixbuf_unref(buffer);
289 /* this is the simple version of rotation (roughly 2-4x slower) */
291 for (i = 0; i < sh; i++)
293 sp = s_pix + (i * srs);
294 for (j = 0; j < sw; j++)
296 if (counter_clockwise)
298 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
302 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
305 *(dp++) = *(sp++); /* r */
306 *(dp++) = *(sp++); /* g */
307 *(dp++) = *(sp++); /* b */
308 if (has_alpha) *(dp) = *(sp++); /* a */
317 * Returns a copy of pixbuf mirrored and or flipped.
318 * TO do a 180 degree rotations set both mirror and flipped TRUE
319 * if mirror and flip are FALSE, result is a simple copy.
321 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gint mirror, gint flip)
334 if (!src) return NULL;
336 w = gdk_pixbuf_get_width(src);
337 h = gdk_pixbuf_get_height(src);
338 has_alpha = gdk_pixbuf_get_has_alpha(src);
339 srs = gdk_pixbuf_get_rowstride(src);
340 s_pix = gdk_pixbuf_get_pixels(src);
342 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
343 drs = gdk_pixbuf_get_rowstride(dest);
344 d_pix = gdk_pixbuf_get_pixels(dest);
346 a = has_alpha ? 4 : 3;
348 for (i = 0; i < h; i++)
350 sp = s_pix + (i * srs);
353 dp = d_pix + ((h - i - 1) * drs);
357 dp = d_pix + (i * drs);
362 for (j = 0; j < w; j++)
364 *(dp++) = *(sp++); /* r */
365 *(dp++) = *(sp++); /* g */
366 *(dp++) = *(sp++); /* b */
367 if (has_alpha) *(dp) = *(sp++); /* a */
373 for (j = 0; j < w; j++)
375 *(dp++) = *(sp++); /* r */
376 *(dp++) = *(sp++); /* g */
377 *(dp++) = *(sp++); /* b */
378 if (has_alpha) *(dp++) = *(sp++); /* a */
386 GdkPixbuf* pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
389 GdkPixbuf *tmp = NULL;
393 case EXIF_ORIENTATION_TOP_LEFT:
394 dest = gdk_pixbuf_copy(pixbuf);
396 case EXIF_ORIENTATION_TOP_RIGHT:
398 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
400 case EXIF_ORIENTATION_BOTTOM_RIGHT:
402 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
404 case EXIF_ORIENTATION_BOTTOM_LEFT:
406 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
408 case EXIF_ORIENTATION_LEFT_TOP:
409 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
410 dest = pixbuf_copy_rotate_90(tmp, FALSE);
412 case EXIF_ORIENTATION_RIGHT_TOP:
413 /* rotated -90 (270) */
414 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
416 case EXIF_ORIENTATION_RIGHT_BOTTOM:
417 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
418 dest = pixbuf_copy_rotate_90(tmp, TRUE);
420 case EXIF_ORIENTATION_LEFT_BOTTOM:
422 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
425 dest = gdk_pixbuf_copy(pixbuf);
428 if (tmp) gdk_pixbuf_unref(tmp);
435 *-----------------------------------------------------------------------------
436 * pixbuf drawing (rectangles)
437 *-----------------------------------------------------------------------------
441 * Fills region of pixbuf at x,y over w,h
442 * with colors red (r), green (g), blue (b)
443 * applying alpha (a), use a=255 for solid.
445 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
446 gint x, gint y, gint w, gint h,
447 gint r, gint g, gint b, gint a)
457 pw = gdk_pixbuf_get_width(pb);
458 ph = gdk_pixbuf_get_height(pb);
460 if (x < 0 || x + w > pw) return;
461 if (y < 0 || y + h > ph) return;
463 p_alpha = gdk_pixbuf_get_has_alpha(pb);
464 prs = gdk_pixbuf_get_rowstride(pb);
465 p_pix = gdk_pixbuf_get_pixels(pb);
467 for (i = 0; i < h; i++)
469 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
470 for (j = 0; j < w; j++)
472 *pp = (r * a + *pp * (256-a)) >> 8;
474 *pp = (g * a + *pp * (256-a)) >> 8;
476 *pp = (b * a + *pp * (256-a)) >> 8;
483 void pixbuf_draw_rect(GdkPixbuf *pb,
484 gint x, gint y, gint w, gint h,
485 gint r, gint g, gint b, gint a,
486 gint left, gint right, gint top, gint bottom)
488 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
490 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
492 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
494 pixbuf_draw_rect_fill(pb, x, y, left, h,
498 void pixbuf_set_rect_fill(GdkPixbuf *pb,
499 gint x, gint y, gint w, gint h,
500 gint r, gint g, gint b, gint a)
510 pw = gdk_pixbuf_get_width(pb);
511 ph = gdk_pixbuf_get_height(pb);
513 if (x < 0 || x + w > pw) return;
514 if (y < 0 || y + h > ph) return;
516 p_alpha = gdk_pixbuf_get_has_alpha(pb);
517 prs = gdk_pixbuf_get_rowstride(pb);
518 p_pix = gdk_pixbuf_get_pixels(pb);
520 for (i = 0; i < h; i++)
522 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
523 for (j = 0; j < w; j++)
528 if (p_alpha) { *pp = a; pp++; }
533 void pixbuf_set_rect(GdkPixbuf *pb,
534 gint x, gint y, gint w, gint h,
535 gint r, gint g, gint b, gint a,
536 gint left, gint right, gint top, gint bottom)
538 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
540 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
542 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
544 pixbuf_set_rect_fill(pb, x, y, left, h,
548 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
555 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
556 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
558 buf = gdk_pixbuf_get_pixels(pb);
559 has_alpha = gdk_pixbuf_get_has_alpha(pb);
560 rowstride = gdk_pixbuf_get_rowstride(pb);
562 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
566 if (has_alpha) *p = a;
571 *-----------------------------------------------------------------------------
572 * pixbuf text rendering
573 *-----------------------------------------------------------------------------
576 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
577 GdkPixbuf *dest, gint dx, gint dy,
579 guint8 r, guint8 g, guint8 b, guint8 a)
594 if (!src || !dest) return;
596 sw = gdk_pixbuf_get_width(src);
597 sh = gdk_pixbuf_get_height(src);
599 if (sx < 0 || sx + w > sw) return;
600 if (sy < 0 || sy + h > sh) return;
602 dw = gdk_pixbuf_get_width(dest);
603 dh = gdk_pixbuf_get_height(dest);
605 if (dx < 0 || dx + w > dw) return;
606 if (dy < 0 || dy + h > dh) return;
608 s_alpha = gdk_pixbuf_get_has_alpha(src);
609 d_alpha = gdk_pixbuf_get_has_alpha(dest);
610 srs = gdk_pixbuf_get_rowstride(src);
611 drs = gdk_pixbuf_get_rowstride(dest);
612 s_pix = gdk_pixbuf_get_pixels(src);
613 d_pix = gdk_pixbuf_get_pixels(dest);
615 s_step = (s_alpha) ? 4 : 3;
616 d_step = (d_alpha) ? 4 : 3;
618 for (i = 0; i < h; i++)
620 sp = s_pix + (sy + i) * srs + sx * s_step;
621 dp = d_pix + (dy + i) * drs + dx * d_step;
622 for (j = 0; j < w; j++)
628 asub = a * sp[0] / 255;
629 *dp = (r * asub + *dp * (256-asub)) >> 8;
631 asub = a * sp[1] / 255;
632 *dp = (g * asub + *dp * (256-asub)) >> 8;
634 asub = a * sp[2] / 255;
635 *dp = (b * asub + *dp * (256-asub)) >> 8;
640 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
654 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
656 guint8 r, guint8 g, guint8 b, guint8 a)
665 if (!widget || !widget->window) return;
667 pango_layout_get_pixel_size(layout, &w, &h);
668 if (w < 1 || h < 1) return;
670 pixmap = gdk_pixmap_new(widget->window, w, h, -1);
672 gc = gdk_gc_new(widget->window);
673 gdk_gc_copy(gc, widget->style->black_gc);
674 gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, w, h);
675 gdk_gc_copy(gc, widget->style->white_gc);
676 gdk_draw_layout(pixmap, gc, 0, 0, layout);
679 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, w, h);
680 gdk_pixbuf_get_from_drawable(buffer, pixmap,
681 gdk_drawable_get_colormap(widget->window),
683 g_object_unref(pixmap);
687 dw = gdk_pixbuf_get_width(pixbuf);
688 dh = gdk_pixbuf_get_height(pixbuf);
704 if (x + w > dw) w = dw - x;
705 if (y + h > dh) h = dh - y;
707 pixbuf_copy_font(buffer, sx, sy,
711 g_object_unref(buffer);
715 *-----------------------------------------------------------------------------
716 * pixbuf drawing (triangle)
717 *-----------------------------------------------------------------------------
720 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
721 gint *rx, gint *ry, gint *rw, gint *rh)
729 tw = MAX(abs(x1 - x2), abs(x2 - x3));
730 tw = MAX(tw, abs(x3 - x1));
731 th = MAX(abs(y1 - y2), abs(y2 - y3));
732 th = MAX(th, abs(y3 - y1));
740 void pixbuf_draw_triangle(GdkPixbuf *pb,
741 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
742 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
743 guint8 r, guint8 g, guint8 b, guint8 a)
755 gdouble slope1, slope2;
756 gint slope1_x, slope1_y;
763 pw = gdk_pixbuf_get_width(pb);
764 ph = gdk_pixbuf_get_height(pb);
766 if (!util_clip_region(0, 0, pw, ph,
767 clip_x, clip_y, clip_w, clip_h,
768 &rx, &ry, &rw, &rh)) return;
770 util_clip_triangle(x1, y1, x2, y2, x3, y3,
773 if (!util_clip_region(rx, ry, rw, rh,
775 &fx1, &fy1, &fw, &fh)) return;
779 p_alpha = gdk_pixbuf_get_has_alpha(pb);
780 prs = gdk_pixbuf_get_rowstride(pb);
781 p_pix = gdk_pixbuf_get_pixels(pb);
783 p_step = (p_alpha) ? 4 : 3;
787 t = x1; x1 = x2; x2 = t;
788 t = y1; y1 = y2; y2 = t;
792 t = x2; x2 = x3; x3 = t;
793 t = y2; y2 = y3; y3 = t;
797 t = x1; x1 = x2; x2 = t;
798 t = y1; y1 = y2; y2 = t;
801 slope1 = (gdouble)(y2 - y1);
802 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
805 slope2 = (gdouble)(y3 - y1);
806 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
808 for (y = fy1; y < fy2; y++)
812 if (!middle && y > y2)
814 slope1 = (gdouble)(y3 - y2);
815 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
822 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
823 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
827 t = xa; xa = xb; xb = t;
830 xa = CLAMP(xa, fx1, fx2);
831 xb = CLAMP(xb, fx1, fx2);
833 pp = p_pix + y * prs + xa * p_step;
837 *pp = (r * a + *pp * (256-a)) >> 8;
839 *pp = (g * a + *pp * (256-a)) >> 8;
841 *pp = (b * a + *pp * (256-a)) >> 8;
851 *-----------------------------------------------------------------------------
852 * pixbuf drawing (line)
853 *-----------------------------------------------------------------------------
856 static gint util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
857 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
858 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
867 t = x1; x1 = x2; x2 = t;
868 t = y1; y1 = y2; y2 = t;
872 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
876 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
880 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
884 if (x1 >= clip_x && x2 <= clip_x + clip_w)
888 if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
892 if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
902 slope = (y2 - y1) / d;
905 y1 = y1 + slope * (clip_x - x1);
908 if (x2 > clip_x + clip_w)
910 y2 = y2 + slope * (clip_x + clip_w - x2);
911 x2 = clip_x + clip_w;
917 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
923 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
925 t = x1; x1 = x2; x2 = t;
926 t = y1; y1 = y2; y2 = t;
935 slope = (x2 - x1) / d;
938 x1 = x1 + slope * (clip_y - y1);
941 if (y2 > clip_y + clip_h)
943 x2 = x2 + slope * (clip_y + clip_h - y2);
944 y2 = clip_y + clip_h;
966 void pixbuf_draw_line(GdkPixbuf *pb,
967 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
968 gint x1, gint y1, gint x2, gint y2,
969 guint8 r, guint8 g, guint8 b, guint8 a)
974 gdouble rx1, ry1, rx2, ry2;
981 gint cx1, cy1, cx2, cy2;
985 pw = gdk_pixbuf_get_width(pb);
986 ph = gdk_pixbuf_get_height(pb);
988 if (!util_clip_region(0, 0, pw, ph,
989 clip_x, clip_y, clip_w, clip_h,
990 &rx, &ry, &rw, &rh)) return;
991 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
992 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
993 &rx1, &ry1, &rx2, &ry2)) return;
1000 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1001 prs = gdk_pixbuf_get_rowstride(pb);
1002 p_pix = gdk_pixbuf_get_pixels(pb);
1004 p_step = (p_alpha) ? 4 : 3;
1006 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1011 t = rx1; rx1 = rx2; rx2 = t;
1012 t = ry1; ry1 = ry2; ry2 = t;
1016 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1017 for (x = rx1; x < rx2; x += 1.0)
1019 px = (gint)(x + 0.5);
1020 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1022 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1024 pp = p_pix + py * prs + px * p_step;
1025 *pp = (r * a + *pp * (256-a)) >> 8;
1027 *pp = (g * a + *pp * (256-a)) >> 8;
1029 *pp = (b * a + *pp * (256-a)) >> 8;
1038 t = rx1; rx1 = rx2; rx2 = t;
1039 t = ry1; ry1 = ry2; ry2 = t;
1043 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1044 for (y = ry1; y < ry2; y += 1.0)
1046 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1047 py = (gint)(y + 0.5);
1049 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1051 pp = p_pix + py * prs + px * p_step;
1052 *pp = (r * a + *pp * (256-a)) >> 8;
1054 *pp = (g * a + *pp * (256-a)) >> 8;
1056 *pp = (b * a + *pp * (256-a)) >> 8;
1063 *-----------------------------------------------------------------------------
1064 * pixbuf drawing (fades and shadows)
1065 *-----------------------------------------------------------------------------
1068 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gint p_alpha,
1069 gint s, gint vertical, gint border,
1070 gint x1, gint y1, gint x2, gint y2,
1071 guint8 r, guint8 g, guint8 b, guint8 a)
1078 p_step = (p_alpha) ? 4 : 3;
1079 for (j = y1; j < y2; j++)
1081 pp = p_pix + j * prs + x1 * p_step;
1082 if (!vertical) n = a - a * abs(j - s) / border;
1083 for (i = x1; i < x2; i++)
1085 if (vertical) n = a - a * abs(i - s) / border;
1086 *pp = (r * n + *pp * (256-n)) >> 8;
1088 *pp = (g * n + *pp * (256-n)) >> 8;
1090 *pp = (b * n + *pp * (256-n)) >> 8;
1097 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gint p_alpha,
1098 gint sx, gint sy, gint border,
1099 gint x1, gint y1, gint x2, gint y2,
1100 guint8 r, guint8 g, guint8 b, guint8 a)
1106 p_step = (p_alpha) ? 4 : 3;
1107 for (j = y1; j < y2; j++)
1109 pp = p_pix + j * prs + x1 * p_step;
1110 for (i = x1; i < x2; i++)
1115 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1116 n = a - a * r / border;
1117 *pp = (r * n + *pp * (256-n)) >> 8;
1119 *pp = (g * n + *pp * (256-n)) >> 8;
1121 *pp = (b * n + *pp * (256-n)) >> 8;
1128 void pixbuf_draw_shadow(GdkPixbuf *pb,
1129 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1130 gint x, gint y, gint w, gint h, gint border,
1131 guint8 r, guint8 g, guint8 b, guint8 a)
1135 gint rx, ry, rw, rh;
1136 gint fx, fy, fw, fh;
1141 pw = gdk_pixbuf_get_width(pb);
1142 ph = gdk_pixbuf_get_height(pb);
1144 if (!util_clip_region(0, 0, pw, ph,
1145 clip_x, clip_y, clip_w, clip_h,
1146 &rx, &ry, &rw, &rh)) return;
1148 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1149 prs = gdk_pixbuf_get_rowstride(pb);
1150 p_pix = gdk_pixbuf_get_pixels(pb);
1152 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1154 &fx, &fy, &fw, &fh))
1156 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1159 if (border < 1) return;
1161 if (util_clip_region(x, y + border, border, h - border * 2,
1163 &fx, &fy, &fw, &fh))
1165 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1166 x + border, TRUE, border,
1167 fx, fy, fx + fw, fy + fh,
1170 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1172 &fx, &fy, &fw, &fh))
1174 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1175 x + w - border, TRUE, border,
1176 fx, fy, fx + fw, fy + fh,
1179 if (util_clip_region(x + border, y, w - border * 2, border,
1181 &fx, &fy, &fw, &fh))
1183 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1184 y + border, FALSE, border,
1185 fx, fy, fx + fw, fy + fh,
1188 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1190 &fx, &fy, &fw, &fh))
1192 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1193 y + h - border, FALSE, border,
1194 fx, fy, fx + fw, fy + fh,
1197 if (util_clip_region(x, y, border, border,
1199 &fx, &fy, &fw, &fh))
1201 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1202 x + border, y + border, border,
1203 fx, fy, fx + fw, fy + fh,
1206 if (util_clip_region(x + w - border, y, border, border,
1208 &fx, &fy, &fw, &fh))
1210 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1211 x + w - border, y + border, border,
1212 fx, fy, fx + fw, fy + fh,
1215 if (util_clip_region(x, y + h - border, border, border,
1217 &fx, &fy, &fw, &fh))
1219 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1220 x + border, y + h - border, border,
1221 fx, fy, fx + fw, fy + fh,
1224 if (util_clip_region(x + w - border, y + h - border, border, border,
1226 &fx, &fy, &fw, &fh))
1228 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1229 x + w - border, y + h - border, border,
1230 fx, fy, fx + fw, fy + fh,
1237 *-----------------------------------------------------------------------------
1238 * pixbuf color alterations
1239 *-----------------------------------------------------------------------------
1242 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1243 gint x, gint y, gint w, gint h)
1253 pw = gdk_pixbuf_get_width(pb);
1254 ph = gdk_pixbuf_get_height(pb);
1256 if (x < 0 || x + w > pw) return;
1257 if (y < 0 || y + h > ph) return;
1259 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1260 prs = gdk_pixbuf_get_rowstride(pb);
1261 p_pix = gdk_pixbuf_get_pixels(pb);
1263 for (i = 0; i < h; i++)
1265 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
1266 for (j = 0; j < w; j++)
1270 grey = (pp[0] + pp[1] + pp[2]) / 3;