4 * Copyright (C) 2008 - 2012 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 "ui_fileops.h"
19 #include "icons/icons_inline.h"
25 *-----------------------------------------------------------------------------
27 *-----------------------------------------------------------------------------
30 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
35 if (!pixbuf || !filename) return FALSE;
37 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
38 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
42 log_printf("Error saving png file: %s\n", error->message);
50 *-----------------------------------------------------------------------------
52 *-----------------------------------------------------------------------------
55 gboolean pixbuf_to_file_as_jpg(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
61 if (!pixbuf || !filename) return FALSE;
63 if (quality == -1) quality = 75;
64 if (quality < 1 || quality > 100)
66 log_printf("Jpeg not saved, invalid quality %d\n", quality);
70 qbuf = g_strdup_printf("%d", quality);
71 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
76 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
84 *-----------------------------------------------------------------------------
86 *-----------------------------------------------------------------------------
89 typedef struct _PixbufInline PixbufInline;
96 static PixbufInline inline_pixbuf_data[] = {
97 { PIXBUF_INLINE_FOLDER_CLOSED, folder_closed },
98 { PIXBUF_INLINE_FOLDER_LOCKED, folder_locked },
99 { PIXBUF_INLINE_FOLDER_OPEN, folder_open },
100 { PIXBUF_INLINE_FOLDER_UP, folder_up },
101 { PIXBUF_INLINE_SCROLLER, icon_scroller },
102 { PIXBUF_INLINE_BROKEN, icon_broken },
103 { PIXBUF_INLINE_ICON, gqview_icon },
104 { PIXBUF_INLINE_LOGO, geeqie_logo },
105 { PIXBUF_INLINE_ICON_FLOAT, icon_float },
106 { PIXBUF_INLINE_ICON_THUMB, icon_thumb },
107 { PIXBUF_INLINE_ICON_BOOK, icon_book },
108 { PIXBUF_INLINE_ICON_CONFIG, icon_config },
109 { PIXBUF_INLINE_ICON_TOOLS, icon_tools },
110 { PIXBUF_INLINE_ICON_VIEW, icon_view },
114 GdkPixbuf *pixbuf_inline(const gchar *key)
118 if (!key) return NULL;
121 while (inline_pixbuf_data[i].key)
123 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
125 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
130 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
135 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
137 static GtkIconFactory *icon_factory = NULL;
138 GtkIconSet *icon_set;
142 icon_factory = gtk_icon_factory_new();
143 gtk_icon_factory_add_default(icon_factory);
146 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
147 gtk_icon_factory_add(icon_factory, key, icon_set);
151 void pixbuf_inline_register_stock_icons(void)
156 while (inline_pixbuf_data[i].key)
158 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
163 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
165 GtkIconTheme *icon_theme;
167 GError *error = NULL;
169 icon_theme = gtk_icon_theme_get_default();
171 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
173 pixbuf = gtk_icon_theme_load_icon(icon_theme,
174 icon, /* icon name */
182 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
187 if (strchr(icon, '.'))
189 /* try again without extension */
190 gchar *icon2 = remove_extension_from_path(icon);
191 pixbuf = gtk_icon_theme_load_icon(icon_theme,
192 icon2, /* icon name */
198 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
205 if (!pixbuf) return FALSE;
207 register_stock_icon(key, pixbuf);
211 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
212 gint old_w, gint old_h,
213 gint *new_w, gint *new_h)
215 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
218 *new_h = (gdouble)*new_w / old_w * old_h;
219 if (*new_h < 1) *new_h = 1;
224 *new_w = (gdouble)*new_h / old_h * old_w;
225 if (*new_w < 1) *new_w = 1;
228 return (*new_w != old_w || *new_h != old_h);
231 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
233 GdkPixbuf *pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); /* FIXME use different images according to FORMAT_CLASS */
235 if (requested_width && requested_height)
237 gint w = gdk_pixbuf_get_width(pixbuf);
238 gint h = gdk_pixbuf_get_height(pixbuf);
240 if (w > requested_width || h > requested_height)
244 if (pixbuf_scale_aspect(requested_width, requested_height,
250 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
251 g_object_unref(G_OBJECT(tmp));
260 *-----------------------------------------------------------------------------
262 *-----------------------------------------------------------------------------
265 gboolean util_clip_region(gint x, gint y, gint w, gint h,
266 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
267 gint *rx, gint *ry, gint *rw, gint *rh)
269 if (clip_x + clip_w <= x ||
271 clip_y + clip_h <= y ||
277 *rx = MAX(x, clip_x);
278 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
280 *ry = MAX(y, clip_y);
281 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
287 *-----------------------------------------------------------------------------
289 *-----------------------------------------------------------------------------
292 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
293 guchar *dest, gint dest_row_stride, gint w, gint h,
294 gint bytes_per_pixel, gboolean counter_clockwise)
300 for (i = 0; i < h; i++)
302 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
303 for (j = 0; j < w; j++)
305 if (counter_clockwise)
307 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
311 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
313 *(dp++) = *(sp++); /* r */
314 *(dp++) = *(sp++); /* g */
315 *(dp++) = *(sp++); /* b */
316 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
322 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
323 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
329 for (i = 0; i < h; i++)
331 sp = src + (i * src_row_stride);
332 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
333 memcpy(dp, sp, w * bytes_per_pixel);
337 #define ROTATE_BUFFER_WIDTH 48
338 #define ROTATE_BUFFER_HEIGHT 48
341 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
344 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
363 if (!src) return NULL;
365 sw = gdk_pixbuf_get_width(src);
366 sh = gdk_pixbuf_get_height(src);
367 has_alpha = gdk_pixbuf_get_has_alpha(src);
368 srs = gdk_pixbuf_get_rowstride(src);
369 s_pix = gdk_pixbuf_get_pixels(src);
373 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
374 drs = gdk_pixbuf_get_rowstride(dest);
375 d_pix = gdk_pixbuf_get_pixels(dest);
377 a = (has_alpha ? 4 : 3);
379 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
380 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
381 b_pix = gdk_pixbuf_get_pixels(buffer);
382 brs = gdk_pixbuf_get_rowstride(buffer);
384 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
386 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
387 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
391 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
392 pixbuf_copy_block_rotate(s_pix, srs, j, i,
394 a, counter_clockwise);
396 if (counter_clockwise)
406 pixbuf_copy_block(b_pix, brs, w, h,
407 d_pix, drs, x, y, a);
411 g_object_unref(buffer);
414 /* this is the simple version of rotation (roughly 2-4x slower) */
416 for (i = 0; i < sh; i++)
418 sp = s_pix + (i * srs);
419 for (j = 0; j < sw; j++)
421 if (counter_clockwise)
423 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
427 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
430 *(dp++) = *(sp++); /* r */
431 *(dp++) = *(sp++); /* g */
432 *(dp++) = *(sp++); /* b */
433 if (has_alpha) *(dp) = *(sp++); /* a */
442 * Returns a copy of pixbuf mirrored and or flipped.
443 * TO do a 180 degree rotations set both mirror and flipped TRUE
444 * if mirror and flip are FALSE, result is a simple copy.
446 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
459 if (!src) return NULL;
461 w = gdk_pixbuf_get_width(src);
462 h = gdk_pixbuf_get_height(src);
463 has_alpha = gdk_pixbuf_get_has_alpha(src);
464 srs = gdk_pixbuf_get_rowstride(src);
465 s_pix = gdk_pixbuf_get_pixels(src);
467 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
468 drs = gdk_pixbuf_get_rowstride(dest);
469 d_pix = gdk_pixbuf_get_pixels(dest);
471 a = has_alpha ? 4 : 3;
473 for (i = 0; i < h; i++)
475 sp = s_pix + (i * srs);
478 dp = d_pix + ((h - i - 1) * drs);
482 dp = d_pix + (i * drs);
487 for (j = 0; j < w; j++)
489 *(dp++) = *(sp++); /* r */
490 *(dp++) = *(sp++); /* g */
491 *(dp++) = *(sp++); /* b */
492 if (has_alpha) *(dp) = *(sp++); /* a */
498 for (j = 0; j < w; j++)
500 *(dp++) = *(sp++); /* r */
501 *(dp++) = *(sp++); /* g */
502 *(dp++) = *(sp++); /* b */
503 if (has_alpha) *(dp++) = *(sp++); /* a */
511 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
514 GdkPixbuf *tmp = NULL;
518 case EXIF_ORIENTATION_TOP_LEFT:
519 dest = gdk_pixbuf_copy(pixbuf);
521 case EXIF_ORIENTATION_TOP_RIGHT:
523 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
525 case EXIF_ORIENTATION_BOTTOM_RIGHT:
527 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
529 case EXIF_ORIENTATION_BOTTOM_LEFT:
531 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
533 case EXIF_ORIENTATION_LEFT_TOP:
534 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
535 dest = pixbuf_copy_rotate_90(tmp, FALSE);
537 case EXIF_ORIENTATION_RIGHT_TOP:
538 /* rotated -90 (270) */
539 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
541 case EXIF_ORIENTATION_RIGHT_BOTTOM:
542 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
543 dest = pixbuf_copy_rotate_90(tmp, TRUE);
545 case EXIF_ORIENTATION_LEFT_BOTTOM:
547 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
550 dest = gdk_pixbuf_copy(pixbuf);
553 if (tmp) g_object_unref(tmp);
560 *-----------------------------------------------------------------------------
561 * pixbuf drawing (rectangles)
562 *-----------------------------------------------------------------------------
566 * Fills region of pixbuf at x,y over w,h
567 * with colors red (r), green (g), blue (b)
568 * applying alpha (a), use a=255 for solid.
570 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
571 gint x, gint y, gint w, gint h,
572 gint r, gint g, gint b, gint a)
582 pw = gdk_pixbuf_get_width(pb);
583 ph = gdk_pixbuf_get_height(pb);
585 if (x < 0 || x + w > pw) return;
586 if (y < 0 || y + h > ph) return;
588 has_alpha = gdk_pixbuf_get_has_alpha(pb);
589 prs = gdk_pixbuf_get_rowstride(pb);
590 p_pix = gdk_pixbuf_get_pixels(pb);
592 for (i = 0; i < h; i++)
594 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
595 for (j = 0; j < w; j++)
597 *pp = (r * a + *pp * (256-a)) >> 8;
599 *pp = (g * a + *pp * (256-a)) >> 8;
601 *pp = (b * a + *pp * (256-a)) >> 8;
608 void pixbuf_draw_rect(GdkPixbuf *pb,
609 gint x, gint y, gint w, gint h,
610 gint r, gint g, gint b, gint a,
611 gint left, gint right, gint top, gint bottom)
613 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
615 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
617 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
619 pixbuf_draw_rect_fill(pb, x, y, left, h,
623 void pixbuf_set_rect_fill(GdkPixbuf *pb,
624 gint x, gint y, gint w, gint h,
625 gint r, gint g, gint b, gint a)
635 pw = gdk_pixbuf_get_width(pb);
636 ph = gdk_pixbuf_get_height(pb);
638 if (x < 0 || x + w > pw) return;
639 if (y < 0 || y + h > ph) return;
641 has_alpha = gdk_pixbuf_get_has_alpha(pb);
642 prs = gdk_pixbuf_get_rowstride(pb);
643 p_pix = gdk_pixbuf_get_pixels(pb);
645 for (i = 0; i < h; i++)
647 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
648 for (j = 0; j < w; j++)
653 if (has_alpha) { *pp = a; pp++; }
658 void pixbuf_set_rect(GdkPixbuf *pb,
659 gint x, gint y, gint w, gint h,
660 gint r, gint g, gint b, gint a,
661 gint left, gint right, gint top, gint bottom)
663 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
665 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
667 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
669 pixbuf_set_rect_fill(pb, x, y, left, h,
673 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
680 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
681 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
683 buf = gdk_pixbuf_get_pixels(pb);
684 has_alpha = gdk_pixbuf_get_has_alpha(pb);
685 rowstride = gdk_pixbuf_get_rowstride(pb);
687 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
691 if (has_alpha) *p = a;
696 *-----------------------------------------------------------------------------
697 * pixbuf text rendering
698 *-----------------------------------------------------------------------------
701 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
702 GdkPixbuf *dest, gint dx, gint dy,
704 guint8 r, guint8 g, guint8 b, guint8 a)
719 if (!src || !dest) return;
721 sw = gdk_pixbuf_get_width(src);
722 sh = gdk_pixbuf_get_height(src);
724 if (sx < 0 || sx + w > sw) return;
725 if (sy < 0 || sy + h > sh) return;
727 dw = gdk_pixbuf_get_width(dest);
728 dh = gdk_pixbuf_get_height(dest);
730 if (dx < 0 || dx + w > dw) return;
731 if (dy < 0 || dy + h > dh) return;
733 s_alpha = gdk_pixbuf_get_has_alpha(src);
734 d_alpha = gdk_pixbuf_get_has_alpha(dest);
735 srs = gdk_pixbuf_get_rowstride(src);
736 drs = gdk_pixbuf_get_rowstride(dest);
737 s_pix = gdk_pixbuf_get_pixels(src);
738 d_pix = gdk_pixbuf_get_pixels(dest);
740 s_step = (s_alpha) ? 4 : 3;
741 d_step = (d_alpha) ? 4 : 3;
743 for (i = 0; i < h; i++)
745 sp = s_pix + (sy + i) * srs + sx * s_step;
746 dp = d_pix + (dy + i) * drs + dx * d_step;
747 for (j = 0; j < w; j++)
753 asub = a * sp[0] / 255;
754 *dp = (r * asub + *dp * (256-asub)) >> 8;
756 asub = a * sp[1] / 255;
757 *dp = (g * asub + *dp * (256-asub)) >> 8;
759 asub = a * sp[2] / 255;
760 *dp = (b * asub + *dp * (256-asub)) >> 8;
765 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
779 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
781 guint8 r, guint8 g, guint8 b, guint8 a)
787 cairo_surface_t *source;
790 pango_layout_get_pixel_size(layout, &w, &h);
791 if (w < 1 || h < 1) return;
793 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
795 cr = cairo_create (source);
796 cairo_set_source_rgb(cr, 0, 0, 0);
797 cairo_rectangle (cr, 0, 0, w, h);
799 cairo_set_source_rgb(cr, 1, 1, 1);
800 pango_cairo_show_layout (cr, layout);
803 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
805 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
807 cairo_image_surface_get_width (source),
808 cairo_image_surface_get_height (source),
809 cairo_image_surface_get_stride (source),
815 dw = gdk_pixbuf_get_width(pixbuf);
816 dh = gdk_pixbuf_get_height(pixbuf);
832 if (x + w > dw) w = dw - x;
833 if (y + h > dh) h = dh - y;
835 pixbuf_copy_font(buffer, sx, sy,
839 g_object_unref(buffer);
840 cairo_surface_destroy(source);
844 *-----------------------------------------------------------------------------
845 * pixbuf drawing (triangle)
846 *-----------------------------------------------------------------------------
849 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
850 gint *rx, gint *ry, gint *rw, gint *rh)
858 tw = MAX(abs(x1 - x2), abs(x2 - x3));
859 tw = MAX(tw, abs(x3 - x1));
860 th = MAX(abs(y1 - y2), abs(y2 - y3));
861 th = MAX(th, abs(y3 - y1));
869 void pixbuf_draw_triangle(GdkPixbuf *pb,
870 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
871 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
872 guint8 r, guint8 g, guint8 b, guint8 a)
884 gdouble slope1, slope2;
885 gint slope1_x, slope1_y;
888 gboolean middle = FALSE;
892 pw = gdk_pixbuf_get_width(pb);
893 ph = gdk_pixbuf_get_height(pb);
895 if (!util_clip_region(0, 0, pw, ph,
896 clip_x, clip_y, clip_w, clip_h,
897 &rx, &ry, &rw, &rh)) return;
899 util_clip_triangle(x1, y1, x2, y2, x3, y3,
902 if (!util_clip_region(rx, ry, rw, rh,
904 &fx1, &fy1, &fw, &fh)) return;
908 has_alpha = gdk_pixbuf_get_has_alpha(pb);
909 prs = gdk_pixbuf_get_rowstride(pb);
910 p_pix = gdk_pixbuf_get_pixels(pb);
912 p_step = (has_alpha) ? 4 : 3;
916 t = x1; x1 = x2; x2 = t;
917 t = y1; y1 = y2; y2 = t;
921 t = x2; x2 = x3; x3 = t;
922 t = y2; y2 = y3; y3 = t;
926 t = x1; x1 = x2; x2 = t;
927 t = y1; y1 = y2; y2 = t;
930 slope1 = (gdouble)(y2 - y1);
931 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
934 slope2 = (gdouble)(y3 - y1);
935 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
937 for (y = fy1; y < fy2; y++)
941 if (!middle && y > y2)
943 slope1 = (gdouble)(y3 - y2);
944 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
951 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
952 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
956 t = xa; xa = xb; xb = t;
959 xa = CLAMP(xa, fx1, fx2);
960 xb = CLAMP(xb, fx1, fx2);
962 pp = p_pix + y * prs + xa * p_step;
966 *pp = (r * a + *pp * (256-a)) >> 8;
968 *pp = (g * a + *pp * (256-a)) >> 8;
970 *pp = (b * a + *pp * (256-a)) >> 8;
980 *-----------------------------------------------------------------------------
981 * pixbuf drawing (line)
982 *-----------------------------------------------------------------------------
985 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
986 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
987 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
989 gboolean flip = FALSE;
996 t = x1; x1 = x2; x2 = t;
997 t = y1; y1 = y2; y2 = t;
1001 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1005 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1009 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1013 if (x1 >= clip_x && x2 <= clip_x + clip_w)
1017 if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
1021 if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
1031 slope = (y2 - y1) / d;
1034 y1 = y1 + slope * (clip_x - x1);
1037 if (x2 > clip_x + clip_w)
1039 y2 = y2 + slope * (clip_x + clip_w - x2);
1040 x2 = clip_x + clip_w;
1046 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1052 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1054 t = x1; x1 = x2; x2 = t;
1055 t = y1; y1 = y2; y2 = t;
1064 slope = (x2 - x1) / d;
1067 x1 = x1 + slope * (clip_y - y1);
1070 if (y2 > clip_y + clip_h)
1072 x2 = x2 + slope * (clip_y + clip_h - y2);
1073 y2 = clip_y + clip_h;
1095 void pixbuf_draw_line(GdkPixbuf *pb,
1096 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1097 gint x1, gint y1, gint x2, gint y2,
1098 guint8 r, guint8 g, guint8 b, guint8 a)
1102 gint rx, ry, rw, rh;
1103 gdouble rx1, ry1, rx2, ry2;
1110 gint cx1, cy1, cx2, cy2;
1114 pw = gdk_pixbuf_get_width(pb);
1115 ph = gdk_pixbuf_get_height(pb);
1117 if (!util_clip_region(0, 0, pw, ph,
1118 clip_x, clip_y, clip_w, clip_h,
1119 &rx, &ry, &rw, &rh)) return;
1120 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1121 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1122 &rx1, &ry1, &rx2, &ry2)) return;
1129 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1130 prs = gdk_pixbuf_get_rowstride(pb);
1131 p_pix = gdk_pixbuf_get_pixels(pb);
1133 p_step = (has_alpha) ? 4 : 3;
1135 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1140 t = rx1; rx1 = rx2; rx2 = t;
1141 t = ry1; ry1 = ry2; ry2 = t;
1145 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1146 for (x = rx1; x < rx2; x += 1.0)
1148 px = (gint)(x + 0.5);
1149 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1151 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1153 pp = p_pix + py * prs + px * p_step;
1154 *pp = (r * a + *pp * (256-a)) >> 8;
1156 *pp = (g * a + *pp * (256-a)) >> 8;
1158 *pp = (b * a + *pp * (256-a)) >> 8;
1167 t = rx1; rx1 = rx2; rx2 = t;
1168 t = ry1; ry1 = ry2; ry2 = t;
1172 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1173 for (y = ry1; y < ry2; y += 1.0)
1175 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1176 py = (gint)(y + 0.5);
1178 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1180 pp = p_pix + py * prs + px * p_step;
1181 *pp = (r * a + *pp * (256-a)) >> 8;
1183 *pp = (g * a + *pp * (256-a)) >> 8;
1185 *pp = (b * a + *pp * (256-a)) >> 8;
1192 *-----------------------------------------------------------------------------
1193 * pixbuf drawing (fades and shadows)
1194 *-----------------------------------------------------------------------------
1197 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1198 gint s, gboolean vertical, gint border,
1199 gint x1, gint y1, gint x2, gint y2,
1200 guint8 r, guint8 g, guint8 b, guint8 a)
1207 p_step = (has_alpha) ? 4 : 3;
1208 for (j = y1; j < y2; j++)
1210 pp = p_pix + j * prs + x1 * p_step;
1211 if (!vertical) n = a - a * abs(j - s) / border;
1212 for (i = x1; i < x2; i++)
1214 if (vertical) n = a - a * abs(i - s) / border;
1215 *pp = (r * n + *pp * (256-n)) >> 8;
1217 *pp = (g * n + *pp * (256-n)) >> 8;
1219 *pp = (b * n + *pp * (256-n)) >> 8;
1221 if (has_alpha) pp++;
1226 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1227 gint sx, gint sy, gint border,
1228 gint x1, gint y1, gint x2, gint y2,
1229 guint8 r, guint8 g, guint8 b, guint8 a)
1235 p_step = (has_alpha) ? 4 : 3;
1236 for (j = y1; j < y2; j++)
1238 pp = p_pix + j * prs + x1 * p_step;
1239 for (i = x1; i < x2; i++)
1244 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1245 n = a - a * r / border;
1246 *pp = (r * n + *pp * (256-n)) >> 8;
1248 *pp = (g * n + *pp * (256-n)) >> 8;
1250 *pp = (b * n + *pp * (256-n)) >> 8;
1252 if (has_alpha) pp++;
1257 void pixbuf_draw_shadow(GdkPixbuf *pb,
1258 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1259 gint x, gint y, gint w, gint h, gint border,
1260 guint8 r, guint8 g, guint8 b, guint8 a)
1264 gint rx, ry, rw, rh;
1265 gint fx, fy, fw, fh;
1270 pw = gdk_pixbuf_get_width(pb);
1271 ph = gdk_pixbuf_get_height(pb);
1273 if (!util_clip_region(0, 0, pw, ph,
1274 clip_x, clip_y, clip_w, clip_h,
1275 &rx, &ry, &rw, &rh)) return;
1277 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1278 prs = gdk_pixbuf_get_rowstride(pb);
1279 p_pix = gdk_pixbuf_get_pixels(pb);
1281 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1283 &fx, &fy, &fw, &fh))
1285 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1288 if (border < 1) return;
1290 if (util_clip_region(x, y + border, border, h - border * 2,
1292 &fx, &fy, &fw, &fh))
1294 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1295 x + border, TRUE, border,
1296 fx, fy, fx + fw, fy + fh,
1299 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1301 &fx, &fy, &fw, &fh))
1303 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1304 x + w - border, TRUE, border,
1305 fx, fy, fx + fw, fy + fh,
1308 if (util_clip_region(x + border, y, w - border * 2, border,
1310 &fx, &fy, &fw, &fh))
1312 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1313 y + border, FALSE, border,
1314 fx, fy, fx + fw, fy + fh,
1317 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1319 &fx, &fy, &fw, &fh))
1321 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1322 y + h - border, FALSE, border,
1323 fx, fy, fx + fw, fy + fh,
1326 if (util_clip_region(x, y, border, border,
1328 &fx, &fy, &fw, &fh))
1330 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1331 x + border, y + border, border,
1332 fx, fy, fx + fw, fy + fh,
1335 if (util_clip_region(x + w - border, y, border, border,
1337 &fx, &fy, &fw, &fh))
1339 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1340 x + w - border, y + border, border,
1341 fx, fy, fx + fw, fy + fh,
1344 if (util_clip_region(x, y + h - border, border, border,
1346 &fx, &fy, &fw, &fh))
1348 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1349 x + border, y + h - border, border,
1350 fx, fy, fx + fw, fy + fh,
1353 if (util_clip_region(x + w - border, y + h - border, border, border,
1355 &fx, &fy, &fw, &fh))
1357 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1358 x + w - border, y + h - border, border,
1359 fx, fy, fx + fw, fy + fh,
1366 *-----------------------------------------------------------------------------
1367 * pixbuf color alterations
1368 *-----------------------------------------------------------------------------
1371 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1372 gint x, gint y, gint w, gint h)
1382 pw = gdk_pixbuf_get_width(pb);
1383 ph = gdk_pixbuf_get_height(pb);
1385 if (x < 0 || x + w > pw) return;
1386 if (y < 0 || y + h > ph) return;
1388 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1389 prs = gdk_pixbuf_get_rowstride(pb);
1390 p_pix = gdk_pixbuf_get_pixels(pb);
1392 for (i = 0; i < h; i++)
1394 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1395 for (j = 0; j < w; j++)
1399 grey = (pp[0] + pp[1] + pp[2]) / 3;
1406 if (has_alpha) pp++;
1410 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */