4 * Copyright (C) 2008 - 2009 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 gchar *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);
134 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
136 static GtkIconFactory *icon_factory = NULL;
137 GtkIconSet *icon_set;
141 icon_factory = gtk_icon_factory_new();
142 gtk_icon_factory_add_default(icon_factory);
145 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
146 gtk_icon_factory_add(icon_factory, key, icon_set);
150 void pixbuf_inline_register_stock_icons(void)
155 while (inline_pixbuf_data[i].key)
157 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
162 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
164 GtkIconTheme *icon_theme;
166 GError *error = NULL;
168 icon_theme = gtk_icon_theme_get_default();
170 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
172 pixbuf = gtk_icon_theme_load_icon(icon_theme,
173 icon, /* icon name */
181 DEBUG_1("Couldn't load icon: %s", error->message);
187 register_stock_icon(key, pixbuf);
191 gint pixbuf_scale_aspect(gint req_w, gint req_h, gint old_w, gint old_h,
192 gint *new_w, gint *new_h)
194 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
197 *new_h = (gdouble)*new_w / old_w * old_h;
198 if (*new_h < 1) *new_h = 1;
203 *new_w = (gdouble)*new_h / old_h * old_w;
204 if (*new_w < 1) *new_w = 1;
207 return (*new_w != old_w || *new_h != old_h);
210 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
212 GdkPixbuf *pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); /* FIXME use different images according to FORMAT_CLASS */
214 if (requested_width && requested_height)
216 gint w = gdk_pixbuf_get_width(pixbuf);
217 gint h = gdk_pixbuf_get_height(pixbuf);
219 if (w > requested_width || h > requested_height)
223 if (pixbuf_scale_aspect(requested_width, requested_height,
229 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
230 g_object_unref(G_OBJECT(tmp));
239 *-----------------------------------------------------------------------------
241 *-----------------------------------------------------------------------------
244 gint util_clip_region(gint x, gint y, gint w, gint h,
245 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
246 gint *rx, gint *ry, gint *rw, gint *rh)
248 if (clip_x + clip_w <= x ||
250 clip_y + clip_h <= y ||
256 *rx = MAX(x, clip_x);
257 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
259 *ry = MAX(y, clip_y);
260 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
266 *-----------------------------------------------------------------------------
268 *-----------------------------------------------------------------------------
271 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
272 guchar *dest, gint dest_row_stride, gint w, gint h,
273 gint bytes_per_pixel, gint counter_clockwise)
279 for (i = 0; i < h; i++)
281 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
282 for (j = 0; j < w; j++)
284 if (counter_clockwise)
286 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
290 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
292 *(dp++) = *(sp++); /* r */
293 *(dp++) = *(sp++); /* g */
294 *(dp++) = *(sp++); /* b */
295 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
301 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
302 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
308 for (i = 0; i < h; i++)
310 sp = src + (i * src_row_stride);
311 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
312 memcpy(dp, sp, w * bytes_per_pixel);
316 #define ROTATE_BUFFER_WIDTH 48
317 #define ROTATE_BUFFER_HEIGHT 48
320 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
323 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gint counter_clockwise)
342 if (!src) return NULL;
344 sw = gdk_pixbuf_get_width(src);
345 sh = gdk_pixbuf_get_height(src);
346 has_alpha = gdk_pixbuf_get_has_alpha(src);
347 srs = gdk_pixbuf_get_rowstride(src);
348 s_pix = gdk_pixbuf_get_pixels(src);
352 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
353 drs = gdk_pixbuf_get_rowstride(dest);
354 d_pix = gdk_pixbuf_get_pixels(dest);
356 a = (has_alpha ? 4 : 3);
358 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
359 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
360 b_pix = gdk_pixbuf_get_pixels(buffer);
361 brs = gdk_pixbuf_get_rowstride(buffer);
363 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
365 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
366 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
370 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
371 pixbuf_copy_block_rotate(s_pix, srs, j, i,
373 a, counter_clockwise);
375 if (counter_clockwise)
385 pixbuf_copy_block(b_pix, brs, w, h,
386 d_pix, drs, x, y, a);
390 g_object_unref(buffer);
393 /* this is the simple version of rotation (roughly 2-4x slower) */
395 for (i = 0; i < sh; i++)
397 sp = s_pix + (i * srs);
398 for (j = 0; j < sw; j++)
400 if (counter_clockwise)
402 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
406 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
409 *(dp++) = *(sp++); /* r */
410 *(dp++) = *(sp++); /* g */
411 *(dp++) = *(sp++); /* b */
412 if (has_alpha) *(dp) = *(sp++); /* a */
421 * Returns a copy of pixbuf mirrored and or flipped.
422 * TO do a 180 degree rotations set both mirror and flipped TRUE
423 * if mirror and flip are FALSE, result is a simple copy.
425 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gint mirror, gint flip)
438 if (!src) return NULL;
440 w = gdk_pixbuf_get_width(src);
441 h = gdk_pixbuf_get_height(src);
442 has_alpha = gdk_pixbuf_get_has_alpha(src);
443 srs = gdk_pixbuf_get_rowstride(src);
444 s_pix = gdk_pixbuf_get_pixels(src);
446 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
447 drs = gdk_pixbuf_get_rowstride(dest);
448 d_pix = gdk_pixbuf_get_pixels(dest);
450 a = has_alpha ? 4 : 3;
452 for (i = 0; i < h; i++)
454 sp = s_pix + (i * srs);
457 dp = d_pix + ((h - i - 1) * drs);
461 dp = d_pix + (i * drs);
466 for (j = 0; j < w; j++)
468 *(dp++) = *(sp++); /* r */
469 *(dp++) = *(sp++); /* g */
470 *(dp++) = *(sp++); /* b */
471 if (has_alpha) *(dp) = *(sp++); /* a */
477 for (j = 0; j < w; j++)
479 *(dp++) = *(sp++); /* r */
480 *(dp++) = *(sp++); /* g */
481 *(dp++) = *(sp++); /* b */
482 if (has_alpha) *(dp++) = *(sp++); /* a */
490 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
493 GdkPixbuf *tmp = NULL;
497 case EXIF_ORIENTATION_TOP_LEFT:
498 dest = gdk_pixbuf_copy(pixbuf);
500 case EXIF_ORIENTATION_TOP_RIGHT:
502 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
504 case EXIF_ORIENTATION_BOTTOM_RIGHT:
506 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
508 case EXIF_ORIENTATION_BOTTOM_LEFT:
510 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
512 case EXIF_ORIENTATION_LEFT_TOP:
513 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
514 dest = pixbuf_copy_rotate_90(tmp, FALSE);
516 case EXIF_ORIENTATION_RIGHT_TOP:
517 /* rotated -90 (270) */
518 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
520 case EXIF_ORIENTATION_RIGHT_BOTTOM:
521 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
522 dest = pixbuf_copy_rotate_90(tmp, TRUE);
524 case EXIF_ORIENTATION_LEFT_BOTTOM:
526 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
529 dest = gdk_pixbuf_copy(pixbuf);
532 if (tmp) g_object_unref(tmp);
539 *-----------------------------------------------------------------------------
540 * pixbuf drawing (rectangles)
541 *-----------------------------------------------------------------------------
545 * Fills region of pixbuf at x,y over w,h
546 * with colors red (r), green (g), blue (b)
547 * applying alpha (a), use a=255 for solid.
549 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
550 gint x, gint y, gint w, gint h,
551 gint r, gint g, gint b, gint a)
561 pw = gdk_pixbuf_get_width(pb);
562 ph = gdk_pixbuf_get_height(pb);
564 if (x < 0 || x + w > pw) return;
565 if (y < 0 || y + h > ph) return;
567 p_alpha = gdk_pixbuf_get_has_alpha(pb);
568 prs = gdk_pixbuf_get_rowstride(pb);
569 p_pix = gdk_pixbuf_get_pixels(pb);
571 for (i = 0; i < h; i++)
573 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
574 for (j = 0; j < w; j++)
576 *pp = (r * a + *pp * (256-a)) >> 8;
578 *pp = (g * a + *pp * (256-a)) >> 8;
580 *pp = (b * a + *pp * (256-a)) >> 8;
587 void pixbuf_draw_rect(GdkPixbuf *pb,
588 gint x, gint y, gint w, gint h,
589 gint r, gint g, gint b, gint a,
590 gint left, gint right, gint top, gint bottom)
592 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
594 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
596 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
598 pixbuf_draw_rect_fill(pb, x, y, left, h,
602 void pixbuf_set_rect_fill(GdkPixbuf *pb,
603 gint x, gint y, gint w, gint h,
604 gint r, gint g, gint b, gint a)
614 pw = gdk_pixbuf_get_width(pb);
615 ph = gdk_pixbuf_get_height(pb);
617 if (x < 0 || x + w > pw) return;
618 if (y < 0 || y + h > ph) return;
620 p_alpha = gdk_pixbuf_get_has_alpha(pb);
621 prs = gdk_pixbuf_get_rowstride(pb);
622 p_pix = gdk_pixbuf_get_pixels(pb);
624 for (i = 0; i < h; i++)
626 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
627 for (j = 0; j < w; j++)
632 if (p_alpha) { *pp = a; pp++; }
637 void pixbuf_set_rect(GdkPixbuf *pb,
638 gint x, gint y, gint w, gint h,
639 gint r, gint g, gint b, gint a,
640 gint left, gint right, gint top, gint bottom)
642 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
644 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
646 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
648 pixbuf_set_rect_fill(pb, x, y, left, h,
652 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
659 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
660 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
662 buf = gdk_pixbuf_get_pixels(pb);
663 has_alpha = gdk_pixbuf_get_has_alpha(pb);
664 rowstride = gdk_pixbuf_get_rowstride(pb);
666 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
670 if (has_alpha) *p = a;
675 *-----------------------------------------------------------------------------
676 * pixbuf text rendering
677 *-----------------------------------------------------------------------------
680 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
681 GdkPixbuf *dest, gint dx, gint dy,
683 guint8 r, guint8 g, guint8 b, guint8 a)
698 if (!src || !dest) return;
700 sw = gdk_pixbuf_get_width(src);
701 sh = gdk_pixbuf_get_height(src);
703 if (sx < 0 || sx + w > sw) return;
704 if (sy < 0 || sy + h > sh) return;
706 dw = gdk_pixbuf_get_width(dest);
707 dh = gdk_pixbuf_get_height(dest);
709 if (dx < 0 || dx + w > dw) return;
710 if (dy < 0 || dy + h > dh) return;
712 s_alpha = gdk_pixbuf_get_has_alpha(src);
713 d_alpha = gdk_pixbuf_get_has_alpha(dest);
714 srs = gdk_pixbuf_get_rowstride(src);
715 drs = gdk_pixbuf_get_rowstride(dest);
716 s_pix = gdk_pixbuf_get_pixels(src);
717 d_pix = gdk_pixbuf_get_pixels(dest);
719 s_step = (s_alpha) ? 4 : 3;
720 d_step = (d_alpha) ? 4 : 3;
722 for (i = 0; i < h; i++)
724 sp = s_pix + (sy + i) * srs + sx * s_step;
725 dp = d_pix + (dy + i) * drs + dx * d_step;
726 for (j = 0; j < w; j++)
732 asub = a * sp[0] / 255;
733 *dp = (r * asub + *dp * (256-asub)) >> 8;
735 asub = a * sp[1] / 255;
736 *dp = (g * asub + *dp * (256-asub)) >> 8;
738 asub = a * sp[2] / 255;
739 *dp = (b * asub + *dp * (256-asub)) >> 8;
744 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
758 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
760 guint8 r, guint8 g, guint8 b, guint8 a)
769 if (!widget || !widget->window) return;
771 pango_layout_get_pixel_size(layout, &w, &h);
772 if (w < 1 || h < 1) return;
774 pixmap = gdk_pixmap_new(widget->window, w, h, -1);
776 gc = gdk_gc_new(widget->window);
777 gdk_gc_copy(gc, widget->style->black_gc);
778 gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, w, h);
779 gdk_gc_copy(gc, widget->style->white_gc);
780 gdk_draw_layout(pixmap, gc, 0, 0, layout);
783 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, w, h);
784 gdk_pixbuf_get_from_drawable(buffer, pixmap,
785 gdk_drawable_get_colormap(widget->window),
787 g_object_unref(pixmap);
791 dw = gdk_pixbuf_get_width(pixbuf);
792 dh = gdk_pixbuf_get_height(pixbuf);
808 if (x + w > dw) w = dw - x;
809 if (y + h > dh) h = dh - y;
811 pixbuf_copy_font(buffer, sx, sy,
815 g_object_unref(buffer);
819 *-----------------------------------------------------------------------------
820 * pixbuf drawing (triangle)
821 *-----------------------------------------------------------------------------
824 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
825 gint *rx, gint *ry, gint *rw, gint *rh)
833 tw = MAX(abs(x1 - x2), abs(x2 - x3));
834 tw = MAX(tw, abs(x3 - x1));
835 th = MAX(abs(y1 - y2), abs(y2 - y3));
836 th = MAX(th, abs(y3 - y1));
844 void pixbuf_draw_triangle(GdkPixbuf *pb,
845 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
846 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
847 guint8 r, guint8 g, guint8 b, guint8 a)
859 gdouble slope1, slope2;
860 gint slope1_x, slope1_y;
863 gboolean middle = FALSE;
867 pw = gdk_pixbuf_get_width(pb);
868 ph = gdk_pixbuf_get_height(pb);
870 if (!util_clip_region(0, 0, pw, ph,
871 clip_x, clip_y, clip_w, clip_h,
872 &rx, &ry, &rw, &rh)) return;
874 util_clip_triangle(x1, y1, x2, y2, x3, y3,
877 if (!util_clip_region(rx, ry, rw, rh,
879 &fx1, &fy1, &fw, &fh)) return;
883 p_alpha = gdk_pixbuf_get_has_alpha(pb);
884 prs = gdk_pixbuf_get_rowstride(pb);
885 p_pix = gdk_pixbuf_get_pixels(pb);
887 p_step = (p_alpha) ? 4 : 3;
891 t = x1; x1 = x2; x2 = t;
892 t = y1; y1 = y2; y2 = t;
896 t = x2; x2 = x3; x3 = t;
897 t = y2; y2 = y3; y3 = t;
901 t = x1; x1 = x2; x2 = t;
902 t = y1; y1 = y2; y2 = t;
905 slope1 = (gdouble)(y2 - y1);
906 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
909 slope2 = (gdouble)(y3 - y1);
910 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
912 for (y = fy1; y < fy2; y++)
916 if (!middle && y > y2)
918 slope1 = (gdouble)(y3 - y2);
919 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
926 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
927 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
931 t = xa; xa = xb; xb = t;
934 xa = CLAMP(xa, fx1, fx2);
935 xb = CLAMP(xb, fx1, fx2);
937 pp = p_pix + y * prs + xa * p_step;
941 *pp = (r * a + *pp * (256-a)) >> 8;
943 *pp = (g * a + *pp * (256-a)) >> 8;
945 *pp = (b * a + *pp * (256-a)) >> 8;
955 *-----------------------------------------------------------------------------
956 * pixbuf drawing (line)
957 *-----------------------------------------------------------------------------
960 static gint util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
961 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
962 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
964 gboolean flip = FALSE;
971 t = x1; x1 = x2; x2 = t;
972 t = y1; y1 = y2; y2 = t;
976 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
980 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
984 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
988 if (x1 >= clip_x && x2 <= clip_x + clip_w)
992 if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
996 if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
1006 slope = (y2 - y1) / d;
1009 y1 = y1 + slope * (clip_x - x1);
1012 if (x2 > clip_x + clip_w)
1014 y2 = y2 + slope * (clip_x + clip_w - x2);
1015 x2 = clip_x + clip_w;
1021 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1027 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1029 t = x1; x1 = x2; x2 = t;
1030 t = y1; y1 = y2; y2 = t;
1039 slope = (x2 - x1) / d;
1042 x1 = x1 + slope * (clip_y - y1);
1045 if (y2 > clip_y + clip_h)
1047 x2 = x2 + slope * (clip_y + clip_h - y2);
1048 y2 = clip_y + clip_h;
1070 void pixbuf_draw_line(GdkPixbuf *pb,
1071 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1072 gint x1, gint y1, gint x2, gint y2,
1073 guint8 r, guint8 g, guint8 b, guint8 a)
1077 gint rx, ry, rw, rh;
1078 gdouble rx1, ry1, rx2, ry2;
1085 gint cx1, cy1, cx2, cy2;
1089 pw = gdk_pixbuf_get_width(pb);
1090 ph = gdk_pixbuf_get_height(pb);
1092 if (!util_clip_region(0, 0, pw, ph,
1093 clip_x, clip_y, clip_w, clip_h,
1094 &rx, &ry, &rw, &rh)) return;
1095 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1096 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1097 &rx1, &ry1, &rx2, &ry2)) return;
1104 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1105 prs = gdk_pixbuf_get_rowstride(pb);
1106 p_pix = gdk_pixbuf_get_pixels(pb);
1108 p_step = (p_alpha) ? 4 : 3;
1110 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1115 t = rx1; rx1 = rx2; rx2 = t;
1116 t = ry1; ry1 = ry2; ry2 = t;
1120 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1121 for (x = rx1; x < rx2; x += 1.0)
1123 px = (gint)(x + 0.5);
1124 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1126 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1128 pp = p_pix + py * prs + px * p_step;
1129 *pp = (r * a + *pp * (256-a)) >> 8;
1131 *pp = (g * a + *pp * (256-a)) >> 8;
1133 *pp = (b * a + *pp * (256-a)) >> 8;
1142 t = rx1; rx1 = rx2; rx2 = t;
1143 t = ry1; ry1 = ry2; ry2 = t;
1147 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1148 for (y = ry1; y < ry2; y += 1.0)
1150 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1151 py = (gint)(y + 0.5);
1153 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1155 pp = p_pix + py * prs + px * p_step;
1156 *pp = (r * a + *pp * (256-a)) >> 8;
1158 *pp = (g * a + *pp * (256-a)) >> 8;
1160 *pp = (b * a + *pp * (256-a)) >> 8;
1167 *-----------------------------------------------------------------------------
1168 * pixbuf drawing (fades and shadows)
1169 *-----------------------------------------------------------------------------
1172 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gint p_alpha,
1173 gint s, gint vertical, gint border,
1174 gint x1, gint y1, gint x2, gint y2,
1175 guint8 r, guint8 g, guint8 b, guint8 a)
1182 p_step = (p_alpha) ? 4 : 3;
1183 for (j = y1; j < y2; j++)
1185 pp = p_pix + j * prs + x1 * p_step;
1186 if (!vertical) n = a - a * abs(j - s) / border;
1187 for (i = x1; i < x2; i++)
1189 if (vertical) n = a - a * abs(i - s) / border;
1190 *pp = (r * n + *pp * (256-n)) >> 8;
1192 *pp = (g * n + *pp * (256-n)) >> 8;
1194 *pp = (b * n + *pp * (256-n)) >> 8;
1201 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gint p_alpha,
1202 gint sx, gint sy, gint border,
1203 gint x1, gint y1, gint x2, gint y2,
1204 guint8 r, guint8 g, guint8 b, guint8 a)
1210 p_step = (p_alpha) ? 4 : 3;
1211 for (j = y1; j < y2; j++)
1213 pp = p_pix + j * prs + x1 * p_step;
1214 for (i = x1; i < x2; i++)
1219 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1220 n = a - a * r / border;
1221 *pp = (r * n + *pp * (256-n)) >> 8;
1223 *pp = (g * n + *pp * (256-n)) >> 8;
1225 *pp = (b * n + *pp * (256-n)) >> 8;
1232 void pixbuf_draw_shadow(GdkPixbuf *pb,
1233 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1234 gint x, gint y, gint w, gint h, gint border,
1235 guint8 r, guint8 g, guint8 b, guint8 a)
1239 gint rx, ry, rw, rh;
1240 gint fx, fy, fw, fh;
1245 pw = gdk_pixbuf_get_width(pb);
1246 ph = gdk_pixbuf_get_height(pb);
1248 if (!util_clip_region(0, 0, pw, ph,
1249 clip_x, clip_y, clip_w, clip_h,
1250 &rx, &ry, &rw, &rh)) return;
1252 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1253 prs = gdk_pixbuf_get_rowstride(pb);
1254 p_pix = gdk_pixbuf_get_pixels(pb);
1256 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1258 &fx, &fy, &fw, &fh))
1260 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1263 if (border < 1) return;
1265 if (util_clip_region(x, y + border, border, h - border * 2,
1267 &fx, &fy, &fw, &fh))
1269 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1270 x + border, TRUE, border,
1271 fx, fy, fx + fw, fy + fh,
1274 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1276 &fx, &fy, &fw, &fh))
1278 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1279 x + w - border, TRUE, border,
1280 fx, fy, fx + fw, fy + fh,
1283 if (util_clip_region(x + border, y, w - border * 2, border,
1285 &fx, &fy, &fw, &fh))
1287 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1288 y + border, FALSE, border,
1289 fx, fy, fx + fw, fy + fh,
1292 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1294 &fx, &fy, &fw, &fh))
1296 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1297 y + h - border, FALSE, border,
1298 fx, fy, fx + fw, fy + fh,
1301 if (util_clip_region(x, y, border, border,
1303 &fx, &fy, &fw, &fh))
1305 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1306 x + border, y + border, border,
1307 fx, fy, fx + fw, fy + fh,
1310 if (util_clip_region(x + w - border, y, border, border,
1312 &fx, &fy, &fw, &fh))
1314 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1315 x + w - border, y + border, border,
1316 fx, fy, fx + fw, fy + fh,
1319 if (util_clip_region(x, y + h - border, border, border,
1321 &fx, &fy, &fw, &fh))
1323 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1324 x + border, y + h - border, border,
1325 fx, fy, fx + fw, fy + fh,
1328 if (util_clip_region(x + w - border, y + h - border, border, border,
1330 &fx, &fy, &fw, &fh))
1332 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1333 x + w - border, y + h - border, border,
1334 fx, fy, fx + fw, fy + fh,
1341 *-----------------------------------------------------------------------------
1342 * pixbuf color alterations
1343 *-----------------------------------------------------------------------------
1346 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1347 gint x, gint y, gint w, gint h)
1357 pw = gdk_pixbuf_get_width(pb);
1358 ph = gdk_pixbuf_get_height(pb);
1360 if (x < 0 || x + w > pw) return;
1361 if (y < 0 || y + h > ph) return;
1363 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1364 prs = gdk_pixbuf_get_rowstride(pb);
1365 p_pix = gdk_pixbuf_get_pixels(pb);
1367 for (i = 0; i < h; i++)
1369 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
1370 for (j = 0; j < w; j++)
1374 grey = (pp[0] + pp[1] + pp[2]) / 3;
1385 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */