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)
359 if (!src) return NULL;
361 sw = gdk_pixbuf_get_width(src);
362 sh = gdk_pixbuf_get_height(src);
363 has_alpha = gdk_pixbuf_get_has_alpha(src);
364 srs = gdk_pixbuf_get_rowstride(src);
365 s_pix = gdk_pixbuf_get_pixels(src);
369 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
370 drs = gdk_pixbuf_get_rowstride(dest);
371 d_pix = gdk_pixbuf_get_pixels(dest);
373 a = (has_alpha ? 4 : 3);
375 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
376 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
377 b_pix = gdk_pixbuf_get_pixels(buffer);
378 brs = gdk_pixbuf_get_rowstride(buffer);
380 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
382 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
383 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
387 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
388 pixbuf_copy_block_rotate(s_pix, srs, j, i,
390 a, counter_clockwise);
392 if (counter_clockwise)
402 pixbuf_copy_block(b_pix, brs, w, h,
403 d_pix, drs, x, y, a);
407 g_object_unref(buffer);
410 /* this is the simple version of rotation (roughly 2-4x slower) */
412 for (i = 0; i < sh; i++)
414 sp = s_pix + (i * srs);
415 for (j = 0; j < sw; j++)
417 if (counter_clockwise)
419 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
423 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
426 *(dp++) = *(sp++); /* r */
427 *(dp++) = *(sp++); /* g */
428 *(dp++) = *(sp++); /* b */
429 if (has_alpha) *(dp) = *(sp++); /* a */
438 * Returns a copy of pixbuf mirrored and or flipped.
439 * TO do a 180 degree rotations set both mirror and flipped TRUE
440 * if mirror and flip are FALSE, result is a simple copy.
442 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
455 if (!src) return NULL;
457 w = gdk_pixbuf_get_width(src);
458 h = gdk_pixbuf_get_height(src);
459 has_alpha = gdk_pixbuf_get_has_alpha(src);
460 srs = gdk_pixbuf_get_rowstride(src);
461 s_pix = gdk_pixbuf_get_pixels(src);
463 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
464 drs = gdk_pixbuf_get_rowstride(dest);
465 d_pix = gdk_pixbuf_get_pixels(dest);
467 a = has_alpha ? 4 : 3;
469 for (i = 0; i < h; i++)
471 sp = s_pix + (i * srs);
474 dp = d_pix + ((h - i - 1) * drs);
478 dp = d_pix + (i * drs);
483 for (j = 0; j < w; j++)
485 *(dp++) = *(sp++); /* r */
486 *(dp++) = *(sp++); /* g */
487 *(dp++) = *(sp++); /* b */
488 if (has_alpha) *(dp) = *(sp++); /* a */
494 for (j = 0; j < w; j++)
496 *(dp++) = *(sp++); /* r */
497 *(dp++) = *(sp++); /* g */
498 *(dp++) = *(sp++); /* b */
499 if (has_alpha) *(dp++) = *(sp++); /* a */
507 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
510 GdkPixbuf *tmp = NULL;
514 case EXIF_ORIENTATION_TOP_LEFT:
515 dest = gdk_pixbuf_copy(pixbuf);
517 case EXIF_ORIENTATION_TOP_RIGHT:
519 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
521 case EXIF_ORIENTATION_BOTTOM_RIGHT:
523 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
525 case EXIF_ORIENTATION_BOTTOM_LEFT:
527 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
529 case EXIF_ORIENTATION_LEFT_TOP:
530 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
531 dest = pixbuf_copy_rotate_90(tmp, FALSE);
533 case EXIF_ORIENTATION_RIGHT_TOP:
534 /* rotated -90 (270) */
535 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
537 case EXIF_ORIENTATION_RIGHT_BOTTOM:
538 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
539 dest = pixbuf_copy_rotate_90(tmp, TRUE);
541 case EXIF_ORIENTATION_LEFT_BOTTOM:
543 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
546 dest = gdk_pixbuf_copy(pixbuf);
549 if (tmp) g_object_unref(tmp);
556 *-----------------------------------------------------------------------------
557 * pixbuf drawing (rectangles)
558 *-----------------------------------------------------------------------------
562 * Fills region of pixbuf at x,y over w,h
563 * with colors red (r), green (g), blue (b)
564 * applying alpha (a), use a=255 for solid.
566 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
567 gint x, gint y, gint w, gint h,
568 gint r, gint g, gint b, gint a)
578 pw = gdk_pixbuf_get_width(pb);
579 ph = gdk_pixbuf_get_height(pb);
581 if (x < 0 || x + w > pw) return;
582 if (y < 0 || y + h > ph) return;
584 has_alpha = gdk_pixbuf_get_has_alpha(pb);
585 prs = gdk_pixbuf_get_rowstride(pb);
586 p_pix = gdk_pixbuf_get_pixels(pb);
588 for (i = 0; i < h; i++)
590 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
591 for (j = 0; j < w; j++)
593 *pp = (r * a + *pp * (256-a)) >> 8;
595 *pp = (g * a + *pp * (256-a)) >> 8;
597 *pp = (b * a + *pp * (256-a)) >> 8;
604 void pixbuf_draw_rect(GdkPixbuf *pb,
605 gint x, gint y, gint w, gint h,
606 gint r, gint g, gint b, gint a,
607 gint left, gint right, gint top, gint bottom)
609 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
611 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
613 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
615 pixbuf_draw_rect_fill(pb, x, y, left, h,
619 void pixbuf_set_rect_fill(GdkPixbuf *pb,
620 gint x, gint y, gint w, gint h,
621 gint r, gint g, gint b, gint a)
631 pw = gdk_pixbuf_get_width(pb);
632 ph = gdk_pixbuf_get_height(pb);
634 if (x < 0 || x + w > pw) return;
635 if (y < 0 || y + h > ph) return;
637 has_alpha = gdk_pixbuf_get_has_alpha(pb);
638 prs = gdk_pixbuf_get_rowstride(pb);
639 p_pix = gdk_pixbuf_get_pixels(pb);
641 for (i = 0; i < h; i++)
643 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
644 for (j = 0; j < w; j++)
649 if (has_alpha) { *pp = a; pp++; }
654 void pixbuf_set_rect(GdkPixbuf *pb,
655 gint x, gint y, gint w, gint h,
656 gint r, gint g, gint b, gint a,
657 gint left, gint right, gint top, gint bottom)
659 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
661 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
663 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
665 pixbuf_set_rect_fill(pb, x, y, left, h,
669 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
676 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
677 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
679 buf = gdk_pixbuf_get_pixels(pb);
680 has_alpha = gdk_pixbuf_get_has_alpha(pb);
681 rowstride = gdk_pixbuf_get_rowstride(pb);
683 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
687 if (has_alpha) *p = a;
692 *-----------------------------------------------------------------------------
693 * pixbuf text rendering
694 *-----------------------------------------------------------------------------
697 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
698 GdkPixbuf *dest, gint dx, gint dy,
700 guint8 r, guint8 g, guint8 b, guint8 a)
715 if (!src || !dest) return;
717 sw = gdk_pixbuf_get_width(src);
718 sh = gdk_pixbuf_get_height(src);
720 if (sx < 0 || sx + w > sw) return;
721 if (sy < 0 || sy + h > sh) return;
723 dw = gdk_pixbuf_get_width(dest);
724 dh = gdk_pixbuf_get_height(dest);
726 if (dx < 0 || dx + w > dw) return;
727 if (dy < 0 || dy + h > dh) return;
729 s_alpha = gdk_pixbuf_get_has_alpha(src);
730 d_alpha = gdk_pixbuf_get_has_alpha(dest);
731 srs = gdk_pixbuf_get_rowstride(src);
732 drs = gdk_pixbuf_get_rowstride(dest);
733 s_pix = gdk_pixbuf_get_pixels(src);
734 d_pix = gdk_pixbuf_get_pixels(dest);
736 s_step = (s_alpha) ? 4 : 3;
737 d_step = (d_alpha) ? 4 : 3;
739 for (i = 0; i < h; i++)
741 sp = s_pix + (sy + i) * srs + sx * s_step;
742 dp = d_pix + (dy + i) * drs + dx * d_step;
743 for (j = 0; j < w; j++)
749 asub = a * sp[0] / 255;
750 *dp = (r * asub + *dp * (256-asub)) >> 8;
752 asub = a * sp[1] / 255;
753 *dp = (g * asub + *dp * (256-asub)) >> 8;
755 asub = a * sp[2] / 255;
756 *dp = (b * asub + *dp * (256-asub)) >> 8;
761 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
775 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
777 guint8 r, guint8 g, guint8 b, guint8 a)
783 cairo_surface_t *source;
786 pango_layout_get_pixel_size(layout, &w, &h);
787 if (w < 1 || h < 1) return;
789 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
791 cr = cairo_create (source);
792 cairo_set_source_rgb(cr, 0, 0, 0);
793 cairo_rectangle (cr, 0, 0, w, h);
795 cairo_set_source_rgb(cr, 1, 1, 1);
796 pango_cairo_show_layout (cr, layout);
799 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
801 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
803 cairo_image_surface_get_width (source),
804 cairo_image_surface_get_height (source),
805 cairo_image_surface_get_stride (source),
811 dw = gdk_pixbuf_get_width(pixbuf);
812 dh = gdk_pixbuf_get_height(pixbuf);
828 if (x + w > dw) w = dw - x;
829 if (y + h > dh) h = dh - y;
831 pixbuf_copy_font(buffer, sx, sy,
835 g_object_unref(buffer);
836 cairo_surface_destroy(source);
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;
1013 slope = (y2 - y1) / d;
1016 y1 = y1 + slope * (clip_x - x1);
1019 if (x2 > clip_x + clip_w)
1021 y2 = y2 + slope * (clip_x + clip_w - x2);
1022 x2 = clip_x + clip_w;
1028 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1034 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1036 t = x1; x1 = x2; x2 = t;
1037 t = y1; y1 = y2; y2 = t;
1046 slope = (x2 - x1) / d;
1049 x1 = x1 + slope * (clip_y - y1);
1052 if (y2 > clip_y + clip_h)
1054 x2 = x2 + slope * (clip_y + clip_h - y2);
1055 y2 = clip_y + clip_h;
1077 void pixbuf_draw_line(GdkPixbuf *pb,
1078 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1079 gint x1, gint y1, gint x2, gint y2,
1080 guint8 r, guint8 g, guint8 b, guint8 a)
1084 gint rx, ry, rw, rh;
1085 gdouble rx1, ry1, rx2, ry2;
1092 gint cx1, cy1, cx2, cy2;
1096 pw = gdk_pixbuf_get_width(pb);
1097 ph = gdk_pixbuf_get_height(pb);
1099 if (!util_clip_region(0, 0, pw, ph,
1100 clip_x, clip_y, clip_w, clip_h,
1101 &rx, &ry, &rw, &rh)) return;
1102 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1103 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1104 &rx1, &ry1, &rx2, &ry2)) return;
1111 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1112 prs = gdk_pixbuf_get_rowstride(pb);
1113 p_pix = gdk_pixbuf_get_pixels(pb);
1115 p_step = (has_alpha) ? 4 : 3;
1117 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1122 t = rx1; rx1 = rx2; rx2 = t;
1123 t = ry1; ry1 = ry2; ry2 = t;
1127 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1128 for (x = rx1; x < rx2; x += 1.0)
1130 px = (gint)(x + 0.5);
1131 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1133 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1135 pp = p_pix + py * prs + px * p_step;
1136 *pp = (r * a + *pp * (256-a)) >> 8;
1138 *pp = (g * a + *pp * (256-a)) >> 8;
1140 *pp = (b * a + *pp * (256-a)) >> 8;
1149 t = rx1; rx1 = rx2; rx2 = t;
1150 t = ry1; ry1 = ry2; ry2 = t;
1154 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1155 for (y = ry1; y < ry2; y += 1.0)
1157 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1158 py = (gint)(y + 0.5);
1160 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1162 pp = p_pix + py * prs + px * p_step;
1163 *pp = (r * a + *pp * (256-a)) >> 8;
1165 *pp = (g * a + *pp * (256-a)) >> 8;
1167 *pp = (b * a + *pp * (256-a)) >> 8;
1174 *-----------------------------------------------------------------------------
1175 * pixbuf drawing (fades and shadows)
1176 *-----------------------------------------------------------------------------
1179 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1180 gint s, gboolean vertical, gint border,
1181 gint x1, gint y1, gint x2, gint y2,
1182 guint8 r, guint8 g, guint8 b, guint8 a)
1189 p_step = (has_alpha) ? 4 : 3;
1190 for (j = y1; j < y2; j++)
1192 pp = p_pix + j * prs + x1 * p_step;
1193 if (!vertical) n = a - a * abs(j - s) / border;
1194 for (i = x1; i < x2; i++)
1196 if (vertical) n = a - a * abs(i - s) / border;
1197 *pp = (r * n + *pp * (256-n)) >> 8;
1199 *pp = (g * n + *pp * (256-n)) >> 8;
1201 *pp = (b * n + *pp * (256-n)) >> 8;
1203 if (has_alpha) pp++;
1208 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1209 gint sx, gint sy, gint border,
1210 gint x1, gint y1, gint x2, gint y2,
1211 guint8 r, guint8 g, guint8 b, guint8 a)
1217 p_step = (has_alpha) ? 4 : 3;
1218 for (j = y1; j < y2; j++)
1220 pp = p_pix + j * prs + x1 * p_step;
1221 for (i = x1; i < x2; i++)
1226 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1227 n = a - a * r / border;
1228 *pp = (r * n + *pp * (256-n)) >> 8;
1230 *pp = (g * n + *pp * (256-n)) >> 8;
1232 *pp = (b * n + *pp * (256-n)) >> 8;
1234 if (has_alpha) pp++;
1239 void pixbuf_draw_shadow(GdkPixbuf *pb,
1240 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1241 gint x, gint y, gint w, gint h, gint border,
1242 guint8 r, guint8 g, guint8 b, guint8 a)
1246 gint rx, ry, rw, rh;
1247 gint fx, fy, fw, fh;
1252 pw = gdk_pixbuf_get_width(pb);
1253 ph = gdk_pixbuf_get_height(pb);
1255 if (!util_clip_region(0, 0, pw, ph,
1256 clip_x, clip_y, clip_w, clip_h,
1257 &rx, &ry, &rw, &rh)) return;
1259 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1260 prs = gdk_pixbuf_get_rowstride(pb);
1261 p_pix = gdk_pixbuf_get_pixels(pb);
1263 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1265 &fx, &fy, &fw, &fh))
1267 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1270 if (border < 1) return;
1272 if (util_clip_region(x, y + border, border, h - border * 2,
1274 &fx, &fy, &fw, &fh))
1276 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1277 x + border, TRUE, border,
1278 fx, fy, fx + fw, fy + fh,
1281 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1283 &fx, &fy, &fw, &fh))
1285 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1286 x + w - border, TRUE, border,
1287 fx, fy, fx + fw, fy + fh,
1290 if (util_clip_region(x + border, y, w - border * 2, border,
1292 &fx, &fy, &fw, &fh))
1294 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1295 y + border, FALSE, border,
1296 fx, fy, fx + fw, fy + fh,
1299 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1301 &fx, &fy, &fw, &fh))
1303 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1304 y + h - border, FALSE, border,
1305 fx, fy, fx + fw, fy + fh,
1308 if (util_clip_region(x, y, border, border,
1310 &fx, &fy, &fw, &fh))
1312 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1313 x + border, y + border, border,
1314 fx, fy, fx + fw, fy + fh,
1317 if (util_clip_region(x + w - border, y, border, border,
1319 &fx, &fy, &fw, &fh))
1321 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1322 x + w - border, y + border, border,
1323 fx, fy, fx + fw, fy + fh,
1326 if (util_clip_region(x, y + h - border, border, border,
1328 &fx, &fy, &fw, &fh))
1330 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1331 x + border, y + h - border, border,
1332 fx, fy, fx + fw, fy + fh,
1335 if (util_clip_region(x + w - border, y + h - border, border, border,
1337 &fx, &fy, &fw, &fh))
1339 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1340 x + w - border, y + h - border, border,
1341 fx, fy, fx + fw, fy + fh,
1348 *-----------------------------------------------------------------------------
1349 * pixbuf color alterations
1350 *-----------------------------------------------------------------------------
1353 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1354 gint x, gint y, gint w, gint h)
1364 pw = gdk_pixbuf_get_width(pb);
1365 ph = gdk_pixbuf_get_height(pb);
1367 if (x < 0 || x + w > pw) return;
1368 if (y < 0 || y + h > ph) return;
1370 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1371 prs = gdk_pixbuf_get_rowstride(pb);
1372 p_pix = gdk_pixbuf_get_pixels(pb);
1374 for (i = 0; i < h; i++)
1376 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1377 for (j = 0; j < w; j++)
1381 grey = (pp[0] + pp[1] + pp[2]) / 3;
1388 if (has_alpha) pp++;
1392 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */