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 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
192 gint old_w, gint old_h,
193 gint *new_w, gint *new_h)
195 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
198 *new_h = (gdouble)*new_w / old_w * old_h;
199 if (*new_h < 1) *new_h = 1;
204 *new_w = (gdouble)*new_h / old_h * old_w;
205 if (*new_w < 1) *new_w = 1;
208 return (*new_w != old_w || *new_h != old_h);
211 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
213 GdkPixbuf *pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); /* FIXME use different images according to FORMAT_CLASS */
215 if (requested_width && requested_height)
217 gint w = gdk_pixbuf_get_width(pixbuf);
218 gint h = gdk_pixbuf_get_height(pixbuf);
220 if (w > requested_width || h > requested_height)
224 if (pixbuf_scale_aspect(requested_width, requested_height,
230 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
231 g_object_unref(G_OBJECT(tmp));
240 *-----------------------------------------------------------------------------
242 *-----------------------------------------------------------------------------
245 gboolean util_clip_region(gint x, gint y, gint w, gint h,
246 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
247 gint *rx, gint *ry, gint *rw, gint *rh)
249 if (clip_x + clip_w <= x ||
251 clip_y + clip_h <= y ||
257 *rx = MAX(x, clip_x);
258 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
260 *ry = MAX(y, clip_y);
261 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
267 *-----------------------------------------------------------------------------
269 *-----------------------------------------------------------------------------
272 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
273 guchar *dest, gint dest_row_stride, gint w, gint h,
274 gint bytes_per_pixel, gboolean counter_clockwise)
280 for (i = 0; i < h; i++)
282 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
283 for (j = 0; j < w; j++)
285 if (counter_clockwise)
287 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
291 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
293 *(dp++) = *(sp++); /* r */
294 *(dp++) = *(sp++); /* g */
295 *(dp++) = *(sp++); /* b */
296 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
302 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
303 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
309 for (i = 0; i < h; i++)
311 sp = src + (i * src_row_stride);
312 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
313 memcpy(dp, sp, w * bytes_per_pixel);
317 #define ROTATE_BUFFER_WIDTH 48
318 #define ROTATE_BUFFER_HEIGHT 48
321 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
324 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
343 if (!src) return NULL;
345 sw = gdk_pixbuf_get_width(src);
346 sh = gdk_pixbuf_get_height(src);
347 has_alpha = gdk_pixbuf_get_has_alpha(src);
348 srs = gdk_pixbuf_get_rowstride(src);
349 s_pix = gdk_pixbuf_get_pixels(src);
353 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
354 drs = gdk_pixbuf_get_rowstride(dest);
355 d_pix = gdk_pixbuf_get_pixels(dest);
357 a = (has_alpha ? 4 : 3);
359 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
360 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
361 b_pix = gdk_pixbuf_get_pixels(buffer);
362 brs = gdk_pixbuf_get_rowstride(buffer);
364 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
366 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
367 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
371 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
372 pixbuf_copy_block_rotate(s_pix, srs, j, i,
374 a, counter_clockwise);
376 if (counter_clockwise)
386 pixbuf_copy_block(b_pix, brs, w, h,
387 d_pix, drs, x, y, a);
391 g_object_unref(buffer);
394 /* this is the simple version of rotation (roughly 2-4x slower) */
396 for (i = 0; i < sh; i++)
398 sp = s_pix + (i * srs);
399 for (j = 0; j < sw; j++)
401 if (counter_clockwise)
403 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
407 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
410 *(dp++) = *(sp++); /* r */
411 *(dp++) = *(sp++); /* g */
412 *(dp++) = *(sp++); /* b */
413 if (has_alpha) *(dp) = *(sp++); /* a */
422 * Returns a copy of pixbuf mirrored and or flipped.
423 * TO do a 180 degree rotations set both mirror and flipped TRUE
424 * if mirror and flip are FALSE, result is a simple copy.
426 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
439 if (!src) return NULL;
441 w = gdk_pixbuf_get_width(src);
442 h = gdk_pixbuf_get_height(src);
443 has_alpha = gdk_pixbuf_get_has_alpha(src);
444 srs = gdk_pixbuf_get_rowstride(src);
445 s_pix = gdk_pixbuf_get_pixels(src);
447 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
448 drs = gdk_pixbuf_get_rowstride(dest);
449 d_pix = gdk_pixbuf_get_pixels(dest);
451 a = has_alpha ? 4 : 3;
453 for (i = 0; i < h; i++)
455 sp = s_pix + (i * srs);
458 dp = d_pix + ((h - i - 1) * drs);
462 dp = d_pix + (i * drs);
467 for (j = 0; j < w; j++)
469 *(dp++) = *(sp++); /* r */
470 *(dp++) = *(sp++); /* g */
471 *(dp++) = *(sp++); /* b */
472 if (has_alpha) *(dp) = *(sp++); /* a */
478 for (j = 0; j < w; j++)
480 *(dp++) = *(sp++); /* r */
481 *(dp++) = *(sp++); /* g */
482 *(dp++) = *(sp++); /* b */
483 if (has_alpha) *(dp++) = *(sp++); /* a */
491 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
494 GdkPixbuf *tmp = NULL;
498 case EXIF_ORIENTATION_TOP_LEFT:
499 dest = gdk_pixbuf_copy(pixbuf);
501 case EXIF_ORIENTATION_TOP_RIGHT:
503 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
505 case EXIF_ORIENTATION_BOTTOM_RIGHT:
507 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
509 case EXIF_ORIENTATION_BOTTOM_LEFT:
511 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
513 case EXIF_ORIENTATION_LEFT_TOP:
514 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
515 dest = pixbuf_copy_rotate_90(tmp, FALSE);
517 case EXIF_ORIENTATION_RIGHT_TOP:
518 /* rotated -90 (270) */
519 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
521 case EXIF_ORIENTATION_RIGHT_BOTTOM:
522 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
523 dest = pixbuf_copy_rotate_90(tmp, TRUE);
525 case EXIF_ORIENTATION_LEFT_BOTTOM:
527 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
530 dest = gdk_pixbuf_copy(pixbuf);
533 if (tmp) g_object_unref(tmp);
540 *-----------------------------------------------------------------------------
541 * pixbuf drawing (rectangles)
542 *-----------------------------------------------------------------------------
546 * Fills region of pixbuf at x,y over w,h
547 * with colors red (r), green (g), blue (b)
548 * applying alpha (a), use a=255 for solid.
550 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
551 gint x, gint y, gint w, gint h,
552 gint r, gint g, gint b, gint a)
562 pw = gdk_pixbuf_get_width(pb);
563 ph = gdk_pixbuf_get_height(pb);
565 if (x < 0 || x + w > pw) return;
566 if (y < 0 || y + h > ph) return;
568 has_alpha = gdk_pixbuf_get_has_alpha(pb);
569 prs = gdk_pixbuf_get_rowstride(pb);
570 p_pix = gdk_pixbuf_get_pixels(pb);
572 for (i = 0; i < h; i++)
574 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
575 for (j = 0; j < w; j++)
577 *pp = (r * a + *pp * (256-a)) >> 8;
579 *pp = (g * a + *pp * (256-a)) >> 8;
581 *pp = (b * a + *pp * (256-a)) >> 8;
588 void pixbuf_draw_rect(GdkPixbuf *pb,
589 gint x, gint y, gint w, gint h,
590 gint r, gint g, gint b, gint a,
591 gint left, gint right, gint top, gint bottom)
593 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
595 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
597 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
599 pixbuf_draw_rect_fill(pb, x, y, left, h,
603 void pixbuf_set_rect_fill(GdkPixbuf *pb,
604 gint x, gint y, gint w, gint h,
605 gint r, gint g, gint b, gint a)
615 pw = gdk_pixbuf_get_width(pb);
616 ph = gdk_pixbuf_get_height(pb);
618 if (x < 0 || x + w > pw) return;
619 if (y < 0 || y + h > ph) return;
621 has_alpha = gdk_pixbuf_get_has_alpha(pb);
622 prs = gdk_pixbuf_get_rowstride(pb);
623 p_pix = gdk_pixbuf_get_pixels(pb);
625 for (i = 0; i < h; i++)
627 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
628 for (j = 0; j < w; j++)
633 if (has_alpha) { *pp = a; pp++; }
638 void pixbuf_set_rect(GdkPixbuf *pb,
639 gint x, gint y, gint w, gint h,
640 gint r, gint g, gint b, gint a,
641 gint left, gint right, gint top, gint bottom)
643 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
645 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
647 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
649 pixbuf_set_rect_fill(pb, x, y, left, h,
653 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
660 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
661 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
663 buf = gdk_pixbuf_get_pixels(pb);
664 has_alpha = gdk_pixbuf_get_has_alpha(pb);
665 rowstride = gdk_pixbuf_get_rowstride(pb);
667 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
671 if (has_alpha) *p = a;
676 *-----------------------------------------------------------------------------
677 * pixbuf text rendering
678 *-----------------------------------------------------------------------------
681 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
682 GdkPixbuf *dest, gint dx, gint dy,
684 guint8 r, guint8 g, guint8 b, guint8 a)
699 if (!src || !dest) return;
701 sw = gdk_pixbuf_get_width(src);
702 sh = gdk_pixbuf_get_height(src);
704 if (sx < 0 || sx + w > sw) return;
705 if (sy < 0 || sy + h > sh) return;
707 dw = gdk_pixbuf_get_width(dest);
708 dh = gdk_pixbuf_get_height(dest);
710 if (dx < 0 || dx + w > dw) return;
711 if (dy < 0 || dy + h > dh) return;
713 s_alpha = gdk_pixbuf_get_has_alpha(src);
714 d_alpha = gdk_pixbuf_get_has_alpha(dest);
715 srs = gdk_pixbuf_get_rowstride(src);
716 drs = gdk_pixbuf_get_rowstride(dest);
717 s_pix = gdk_pixbuf_get_pixels(src);
718 d_pix = gdk_pixbuf_get_pixels(dest);
720 s_step = (s_alpha) ? 4 : 3;
721 d_step = (d_alpha) ? 4 : 3;
723 for (i = 0; i < h; i++)
725 sp = s_pix + (sy + i) * srs + sx * s_step;
726 dp = d_pix + (dy + i) * drs + dx * d_step;
727 for (j = 0; j < w; j++)
733 asub = a * sp[0] / 255;
734 *dp = (r * asub + *dp * (256-asub)) >> 8;
736 asub = a * sp[1] / 255;
737 *dp = (g * asub + *dp * (256-asub)) >> 8;
739 asub = a * sp[2] / 255;
740 *dp = (b * asub + *dp * (256-asub)) >> 8;
745 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
759 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
761 guint8 r, guint8 g, guint8 b, guint8 a)
770 if (!widget || !widget->window) return;
772 pango_layout_get_pixel_size(layout, &w, &h);
773 if (w < 1 || h < 1) return;
775 pixmap = gdk_pixmap_new(widget->window, w, h, -1);
777 gc = gdk_gc_new(widget->window);
778 gdk_gc_copy(gc, widget->style->black_gc);
779 gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, w, h);
780 gdk_gc_copy(gc, widget->style->white_gc);
781 gdk_draw_layout(pixmap, gc, 0, 0, layout);
784 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, w, h);
785 gdk_pixbuf_get_from_drawable(buffer, pixmap,
786 gdk_drawable_get_colormap(widget->window),
788 g_object_unref(pixmap);
792 dw = gdk_pixbuf_get_width(pixbuf);
793 dh = gdk_pixbuf_get_height(pixbuf);
809 if (x + w > dw) w = dw - x;
810 if (y + h > dh) h = dh - y;
812 pixbuf_copy_font(buffer, sx, sy,
816 g_object_unref(buffer);
820 *-----------------------------------------------------------------------------
821 * pixbuf drawing (triangle)
822 *-----------------------------------------------------------------------------
825 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
826 gint *rx, gint *ry, gint *rw, gint *rh)
834 tw = MAX(abs(x1 - x2), abs(x2 - x3));
835 tw = MAX(tw, abs(x3 - x1));
836 th = MAX(abs(y1 - y2), abs(y2 - y3));
837 th = MAX(th, abs(y3 - y1));
845 void pixbuf_draw_triangle(GdkPixbuf *pb,
846 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
847 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
848 guint8 r, guint8 g, guint8 b, guint8 a)
860 gdouble slope1, slope2;
861 gint slope1_x, slope1_y;
864 gboolean middle = FALSE;
868 pw = gdk_pixbuf_get_width(pb);
869 ph = gdk_pixbuf_get_height(pb);
871 if (!util_clip_region(0, 0, pw, ph,
872 clip_x, clip_y, clip_w, clip_h,
873 &rx, &ry, &rw, &rh)) return;
875 util_clip_triangle(x1, y1, x2, y2, x3, y3,
878 if (!util_clip_region(rx, ry, rw, rh,
880 &fx1, &fy1, &fw, &fh)) return;
884 has_alpha = gdk_pixbuf_get_has_alpha(pb);
885 prs = gdk_pixbuf_get_rowstride(pb);
886 p_pix = gdk_pixbuf_get_pixels(pb);
888 p_step = (has_alpha) ? 4 : 3;
892 t = x1; x1 = x2; x2 = t;
893 t = y1; y1 = y2; y2 = t;
897 t = x2; x2 = x3; x3 = t;
898 t = y2; y2 = y3; y3 = t;
902 t = x1; x1 = x2; x2 = t;
903 t = y1; y1 = y2; y2 = t;
906 slope1 = (gdouble)(y2 - y1);
907 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
910 slope2 = (gdouble)(y3 - y1);
911 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
913 for (y = fy1; y < fy2; y++)
917 if (!middle && y > y2)
919 slope1 = (gdouble)(y3 - y2);
920 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
927 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
928 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
932 t = xa; xa = xb; xb = t;
935 xa = CLAMP(xa, fx1, fx2);
936 xb = CLAMP(xb, fx1, fx2);
938 pp = p_pix + y * prs + xa * p_step;
942 *pp = (r * a + *pp * (256-a)) >> 8;
944 *pp = (g * a + *pp * (256-a)) >> 8;
946 *pp = (b * a + *pp * (256-a)) >> 8;
956 *-----------------------------------------------------------------------------
957 * pixbuf drawing (line)
958 *-----------------------------------------------------------------------------
961 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
962 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
963 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
965 gboolean flip = FALSE;
972 t = x1; x1 = x2; x2 = t;
973 t = y1; y1 = y2; y2 = t;
977 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
981 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
985 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
989 if (x1 >= clip_x && x2 <= clip_x + clip_w)
993 if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
997 if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
1007 slope = (y2 - y1) / d;
1010 y1 = y1 + slope * (clip_x - x1);
1013 if (x2 > clip_x + clip_w)
1015 y2 = y2 + slope * (clip_x + clip_w - x2);
1016 x2 = clip_x + clip_w;
1022 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1028 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1030 t = x1; x1 = x2; x2 = t;
1031 t = y1; y1 = y2; y2 = t;
1040 slope = (x2 - x1) / d;
1043 x1 = x1 + slope * (clip_y - y1);
1046 if (y2 > clip_y + clip_h)
1048 x2 = x2 + slope * (clip_y + clip_h - y2);
1049 y2 = clip_y + clip_h;
1071 void pixbuf_draw_line(GdkPixbuf *pb,
1072 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1073 gint x1, gint y1, gint x2, gint y2,
1074 guint8 r, guint8 g, guint8 b, guint8 a)
1078 gint rx, ry, rw, rh;
1079 gdouble rx1, ry1, rx2, ry2;
1086 gint cx1, cy1, cx2, cy2;
1090 pw = gdk_pixbuf_get_width(pb);
1091 ph = gdk_pixbuf_get_height(pb);
1093 if (!util_clip_region(0, 0, pw, ph,
1094 clip_x, clip_y, clip_w, clip_h,
1095 &rx, &ry, &rw, &rh)) return;
1096 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1097 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1098 &rx1, &ry1, &rx2, &ry2)) return;
1105 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1106 prs = gdk_pixbuf_get_rowstride(pb);
1107 p_pix = gdk_pixbuf_get_pixels(pb);
1109 p_step = (has_alpha) ? 4 : 3;
1111 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1116 t = rx1; rx1 = rx2; rx2 = t;
1117 t = ry1; ry1 = ry2; ry2 = t;
1121 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1122 for (x = rx1; x < rx2; x += 1.0)
1124 px = (gint)(x + 0.5);
1125 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1127 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1129 pp = p_pix + py * prs + px * p_step;
1130 *pp = (r * a + *pp * (256-a)) >> 8;
1132 *pp = (g * a + *pp * (256-a)) >> 8;
1134 *pp = (b * a + *pp * (256-a)) >> 8;
1143 t = rx1; rx1 = rx2; rx2 = t;
1144 t = ry1; ry1 = ry2; ry2 = t;
1148 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1149 for (y = ry1; y < ry2; y += 1.0)
1151 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1152 py = (gint)(y + 0.5);
1154 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1156 pp = p_pix + py * prs + px * p_step;
1157 *pp = (r * a + *pp * (256-a)) >> 8;
1159 *pp = (g * a + *pp * (256-a)) >> 8;
1161 *pp = (b * a + *pp * (256-a)) >> 8;
1168 *-----------------------------------------------------------------------------
1169 * pixbuf drawing (fades and shadows)
1170 *-----------------------------------------------------------------------------
1173 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1174 gint s, gboolean vertical, gint border,
1175 gint x1, gint y1, gint x2, gint y2,
1176 guint8 r, guint8 g, guint8 b, guint8 a)
1183 p_step = (has_alpha) ? 4 : 3;
1184 for (j = y1; j < y2; j++)
1186 pp = p_pix + j * prs + x1 * p_step;
1187 if (!vertical) n = a - a * abs(j - s) / border;
1188 for (i = x1; i < x2; i++)
1190 if (vertical) n = a - a * abs(i - s) / border;
1191 *pp = (r * n + *pp * (256-n)) >> 8;
1193 *pp = (g * n + *pp * (256-n)) >> 8;
1195 *pp = (b * n + *pp * (256-n)) >> 8;
1197 if (has_alpha) pp++;
1202 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1203 gint sx, gint sy, gint border,
1204 gint x1, gint y1, gint x2, gint y2,
1205 guint8 r, guint8 g, guint8 b, guint8 a)
1211 p_step = (has_alpha) ? 4 : 3;
1212 for (j = y1; j < y2; j++)
1214 pp = p_pix + j * prs + x1 * p_step;
1215 for (i = x1; i < x2; i++)
1220 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1221 n = a - a * r / border;
1222 *pp = (r * n + *pp * (256-n)) >> 8;
1224 *pp = (g * n + *pp * (256-n)) >> 8;
1226 *pp = (b * n + *pp * (256-n)) >> 8;
1228 if (has_alpha) pp++;
1233 void pixbuf_draw_shadow(GdkPixbuf *pb,
1234 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1235 gint x, gint y, gint w, gint h, gint border,
1236 guint8 r, guint8 g, guint8 b, guint8 a)
1240 gint rx, ry, rw, rh;
1241 gint fx, fy, fw, fh;
1246 pw = gdk_pixbuf_get_width(pb);
1247 ph = gdk_pixbuf_get_height(pb);
1249 if (!util_clip_region(0, 0, pw, ph,
1250 clip_x, clip_y, clip_w, clip_h,
1251 &rx, &ry, &rw, &rh)) return;
1253 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1254 prs = gdk_pixbuf_get_rowstride(pb);
1255 p_pix = gdk_pixbuf_get_pixels(pb);
1257 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1259 &fx, &fy, &fw, &fh))
1261 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1264 if (border < 1) return;
1266 if (util_clip_region(x, y + border, border, h - border * 2,
1268 &fx, &fy, &fw, &fh))
1270 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1271 x + border, TRUE, border,
1272 fx, fy, fx + fw, fy + fh,
1275 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1277 &fx, &fy, &fw, &fh))
1279 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1280 x + w - border, TRUE, border,
1281 fx, fy, fx + fw, fy + fh,
1284 if (util_clip_region(x + border, y, w - border * 2, border,
1286 &fx, &fy, &fw, &fh))
1288 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1289 y + border, FALSE, border,
1290 fx, fy, fx + fw, fy + fh,
1293 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1295 &fx, &fy, &fw, &fh))
1297 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1298 y + h - border, FALSE, border,
1299 fx, fy, fx + fw, fy + fh,
1302 if (util_clip_region(x, y, border, border,
1304 &fx, &fy, &fw, &fh))
1306 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1307 x + border, y + border, border,
1308 fx, fy, fx + fw, fy + fh,
1311 if (util_clip_region(x + w - border, y, border, border,
1313 &fx, &fy, &fw, &fh))
1315 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1316 x + w - border, y + border, border,
1317 fx, fy, fx + fw, fy + fh,
1320 if (util_clip_region(x, y + h - border, border, border,
1322 &fx, &fy, &fw, &fh))
1324 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1325 x + border, y + h - border, border,
1326 fx, fy, fx + fw, fy + fh,
1329 if (util_clip_region(x + w - border, y + h - border, border, border,
1331 &fx, &fy, &fw, &fh))
1333 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1334 x + w - border, y + h - border, border,
1335 fx, fy, fx + fw, fy + fh,
1342 *-----------------------------------------------------------------------------
1343 * pixbuf color alterations
1344 *-----------------------------------------------------------------------------
1347 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1348 gint x, gint y, gint w, gint h)
1358 pw = gdk_pixbuf_get_width(pb);
1359 ph = gdk_pixbuf_get_height(pb);
1361 if (x < 0 || x + w > pw) return;
1362 if (y < 0 || y + h > ph) return;
1364 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1365 prs = gdk_pixbuf_get_rowstride(pb);
1366 p_pix = gdk_pixbuf_get_pixels(pb);
1368 for (i = 0; i < h; i++)
1370 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1371 for (j = 0; j < w; j++)
1375 grey = (pp[0] + pp[1] + pp[2]) / 3;
1382 if (has_alpha) pp++;
1386 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */