4 * Copyright (C) 2008 - 2010 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)
790 if (!widget || !widget->window) return;
792 pango_layout_get_pixel_size(layout, &w, &h);
793 if (w < 1 || h < 1) return;
795 pixmap = gdk_pixmap_new(widget->window, w, h, -1);
797 gc = gdk_gc_new(widget->window);
798 gdk_gc_copy(gc, widget->style->black_gc);
799 gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, w, h);
800 gdk_gc_copy(gc, widget->style->white_gc);
801 gdk_draw_layout(pixmap, gc, 0, 0, layout);
804 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, w, h);
805 gdk_pixbuf_get_from_drawable(buffer, pixmap,
806 gdk_drawable_get_colormap(widget->window),
808 g_object_unref(pixmap);
812 dw = gdk_pixbuf_get_width(pixbuf);
813 dh = gdk_pixbuf_get_height(pixbuf);
829 if (x + w > dw) w = dw - x;
830 if (y + h > dh) h = dh - y;
832 pixbuf_copy_font(buffer, sx, sy,
836 g_object_unref(buffer);
840 *-----------------------------------------------------------------------------
841 * pixbuf drawing (triangle)
842 *-----------------------------------------------------------------------------
845 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
846 gint *rx, gint *ry, gint *rw, gint *rh)
854 tw = MAX(abs(x1 - x2), abs(x2 - x3));
855 tw = MAX(tw, abs(x3 - x1));
856 th = MAX(abs(y1 - y2), abs(y2 - y3));
857 th = MAX(th, abs(y3 - y1));
865 void pixbuf_draw_triangle(GdkPixbuf *pb,
866 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
867 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
868 guint8 r, guint8 g, guint8 b, guint8 a)
880 gdouble slope1, slope2;
881 gint slope1_x, slope1_y;
884 gboolean middle = FALSE;
888 pw = gdk_pixbuf_get_width(pb);
889 ph = gdk_pixbuf_get_height(pb);
891 if (!util_clip_region(0, 0, pw, ph,
892 clip_x, clip_y, clip_w, clip_h,
893 &rx, &ry, &rw, &rh)) return;
895 util_clip_triangle(x1, y1, x2, y2, x3, y3,
898 if (!util_clip_region(rx, ry, rw, rh,
900 &fx1, &fy1, &fw, &fh)) return;
904 has_alpha = gdk_pixbuf_get_has_alpha(pb);
905 prs = gdk_pixbuf_get_rowstride(pb);
906 p_pix = gdk_pixbuf_get_pixels(pb);
908 p_step = (has_alpha) ? 4 : 3;
912 t = x1; x1 = x2; x2 = t;
913 t = y1; y1 = y2; y2 = t;
917 t = x2; x2 = x3; x3 = t;
918 t = y2; y2 = y3; y3 = t;
922 t = x1; x1 = x2; x2 = t;
923 t = y1; y1 = y2; y2 = t;
926 slope1 = (gdouble)(y2 - y1);
927 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
930 slope2 = (gdouble)(y3 - y1);
931 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
933 for (y = fy1; y < fy2; y++)
937 if (!middle && y > y2)
939 slope1 = (gdouble)(y3 - y2);
940 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
947 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
948 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
952 t = xa; xa = xb; xb = t;
955 xa = CLAMP(xa, fx1, fx2);
956 xb = CLAMP(xb, fx1, fx2);
958 pp = p_pix + y * prs + xa * p_step;
962 *pp = (r * a + *pp * (256-a)) >> 8;
964 *pp = (g * a + *pp * (256-a)) >> 8;
966 *pp = (b * a + *pp * (256-a)) >> 8;
976 *-----------------------------------------------------------------------------
977 * pixbuf drawing (line)
978 *-----------------------------------------------------------------------------
981 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
982 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
983 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
985 gboolean flip = FALSE;
992 t = x1; x1 = x2; x2 = t;
993 t = y1; y1 = y2; y2 = t;
997 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1001 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1005 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1009 if (x1 >= clip_x && x2 <= clip_x + clip_w)
1013 if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
1017 if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
1027 slope = (y2 - y1) / d;
1030 y1 = y1 + slope * (clip_x - x1);
1033 if (x2 > clip_x + clip_w)
1035 y2 = y2 + slope * (clip_x + clip_w - x2);
1036 x2 = clip_x + clip_w;
1042 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1048 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1050 t = x1; x1 = x2; x2 = t;
1051 t = y1; y1 = y2; y2 = t;
1060 slope = (x2 - x1) / d;
1063 x1 = x1 + slope * (clip_y - y1);
1066 if (y2 > clip_y + clip_h)
1068 x2 = x2 + slope * (clip_y + clip_h - y2);
1069 y2 = clip_y + clip_h;
1091 void pixbuf_draw_line(GdkPixbuf *pb,
1092 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1093 gint x1, gint y1, gint x2, gint y2,
1094 guint8 r, guint8 g, guint8 b, guint8 a)
1098 gint rx, ry, rw, rh;
1099 gdouble rx1, ry1, rx2, ry2;
1106 gint cx1, cy1, cx2, cy2;
1110 pw = gdk_pixbuf_get_width(pb);
1111 ph = gdk_pixbuf_get_height(pb);
1113 if (!util_clip_region(0, 0, pw, ph,
1114 clip_x, clip_y, clip_w, clip_h,
1115 &rx, &ry, &rw, &rh)) return;
1116 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1117 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1118 &rx1, &ry1, &rx2, &ry2)) return;
1125 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1126 prs = gdk_pixbuf_get_rowstride(pb);
1127 p_pix = gdk_pixbuf_get_pixels(pb);
1129 p_step = (has_alpha) ? 4 : 3;
1131 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1136 t = rx1; rx1 = rx2; rx2 = t;
1137 t = ry1; ry1 = ry2; ry2 = t;
1141 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1142 for (x = rx1; x < rx2; x += 1.0)
1144 px = (gint)(x + 0.5);
1145 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1147 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1149 pp = p_pix + py * prs + px * p_step;
1150 *pp = (r * a + *pp * (256-a)) >> 8;
1152 *pp = (g * a + *pp * (256-a)) >> 8;
1154 *pp = (b * a + *pp * (256-a)) >> 8;
1163 t = rx1; rx1 = rx2; rx2 = t;
1164 t = ry1; ry1 = ry2; ry2 = t;
1168 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1169 for (y = ry1; y < ry2; y += 1.0)
1171 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1172 py = (gint)(y + 0.5);
1174 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1176 pp = p_pix + py * prs + px * p_step;
1177 *pp = (r * a + *pp * (256-a)) >> 8;
1179 *pp = (g * a + *pp * (256-a)) >> 8;
1181 *pp = (b * a + *pp * (256-a)) >> 8;
1188 *-----------------------------------------------------------------------------
1189 * pixbuf drawing (fades and shadows)
1190 *-----------------------------------------------------------------------------
1193 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1194 gint s, gboolean vertical, gint border,
1195 gint x1, gint y1, gint x2, gint y2,
1196 guint8 r, guint8 g, guint8 b, guint8 a)
1203 p_step = (has_alpha) ? 4 : 3;
1204 for (j = y1; j < y2; j++)
1206 pp = p_pix + j * prs + x1 * p_step;
1207 if (!vertical) n = a - a * abs(j - s) / border;
1208 for (i = x1; i < x2; i++)
1210 if (vertical) n = a - a * abs(i - s) / border;
1211 *pp = (r * n + *pp * (256-n)) >> 8;
1213 *pp = (g * n + *pp * (256-n)) >> 8;
1215 *pp = (b * n + *pp * (256-n)) >> 8;
1217 if (has_alpha) pp++;
1222 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1223 gint sx, gint sy, gint border,
1224 gint x1, gint y1, gint x2, gint y2,
1225 guint8 r, guint8 g, guint8 b, guint8 a)
1231 p_step = (has_alpha) ? 4 : 3;
1232 for (j = y1; j < y2; j++)
1234 pp = p_pix + j * prs + x1 * p_step;
1235 for (i = x1; i < x2; i++)
1240 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1241 n = a - a * r / border;
1242 *pp = (r * n + *pp * (256-n)) >> 8;
1244 *pp = (g * n + *pp * (256-n)) >> 8;
1246 *pp = (b * n + *pp * (256-n)) >> 8;
1248 if (has_alpha) pp++;
1253 void pixbuf_draw_shadow(GdkPixbuf *pb,
1254 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1255 gint x, gint y, gint w, gint h, gint border,
1256 guint8 r, guint8 g, guint8 b, guint8 a)
1260 gint rx, ry, rw, rh;
1261 gint fx, fy, fw, fh;
1266 pw = gdk_pixbuf_get_width(pb);
1267 ph = gdk_pixbuf_get_height(pb);
1269 if (!util_clip_region(0, 0, pw, ph,
1270 clip_x, clip_y, clip_w, clip_h,
1271 &rx, &ry, &rw, &rh)) return;
1273 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1274 prs = gdk_pixbuf_get_rowstride(pb);
1275 p_pix = gdk_pixbuf_get_pixels(pb);
1277 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1279 &fx, &fy, &fw, &fh))
1281 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1284 if (border < 1) return;
1286 if (util_clip_region(x, y + border, border, h - border * 2,
1288 &fx, &fy, &fw, &fh))
1290 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1291 x + border, TRUE, border,
1292 fx, fy, fx + fw, fy + fh,
1295 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1297 &fx, &fy, &fw, &fh))
1299 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1300 x + w - border, TRUE, border,
1301 fx, fy, fx + fw, fy + fh,
1304 if (util_clip_region(x + border, y, w - border * 2, border,
1306 &fx, &fy, &fw, &fh))
1308 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1309 y + border, FALSE, border,
1310 fx, fy, fx + fw, fy + fh,
1313 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1315 &fx, &fy, &fw, &fh))
1317 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1318 y + h - border, FALSE, border,
1319 fx, fy, fx + fw, fy + fh,
1322 if (util_clip_region(x, y, border, border,
1324 &fx, &fy, &fw, &fh))
1326 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1327 x + border, y + border, border,
1328 fx, fy, fx + fw, fy + fh,
1331 if (util_clip_region(x + w - border, y, border, border,
1333 &fx, &fy, &fw, &fh))
1335 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1336 x + w - border, y + border, border,
1337 fx, fy, fx + fw, fy + fh,
1340 if (util_clip_region(x, y + h - border, border, border,
1342 &fx, &fy, &fw, &fh))
1344 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1345 x + border, y + h - border, border,
1346 fx, fy, fx + fw, fy + fh,
1349 if (util_clip_region(x + w - border, y + h - border, border, border,
1351 &fx, &fy, &fw, &fh))
1353 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1354 x + w - border, y + h - border, border,
1355 fx, fy, fx + fw, fy + fh,
1362 *-----------------------------------------------------------------------------
1363 * pixbuf color alterations
1364 *-----------------------------------------------------------------------------
1367 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1368 gint x, gint y, gint w, gint h)
1378 pw = gdk_pixbuf_get_width(pb);
1379 ph = gdk_pixbuf_get_height(pb);
1381 if (x < 0 || x + w > pw) return;
1382 if (y < 0 || y + h > ph) return;
1384 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1385 prs = gdk_pixbuf_get_rowstride(pb);
1386 p_pix = gdk_pixbuf_get_pixels(pb);
1388 for (i = 0; i < h; i++)
1390 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1391 for (j = 0; j < w; j++)
1395 grey = (pp[0] + pp[1] + pp[2]) / 3;
1402 if (has_alpha) pp++;
1406 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */