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();
169 pixbuf = gtk_icon_theme_load_icon (icon_theme,
170 icon, /* icon name */
176 DEBUG_1("Couldn't load icon: %s", error->message);
181 register_stock_icon(key, pixbuf);
185 gint pixbuf_scale_aspect(gint req_w, gint req_h, gint old_w, gint old_h,
186 gint *new_w, gint *new_h)
188 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
191 *new_h = (gdouble)*new_w / old_w * old_h;
192 if (*new_h < 1) *new_h = 1;
197 *new_w = (gdouble)*new_h / old_h * old_w;
198 if (*new_w < 1) *new_w = 1;
201 return (*new_w != old_w || *new_h != old_h);
204 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
206 GdkPixbuf *pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); /* FIXME use different images according to FORMAT_CLASS */
208 if (requested_width && requested_height)
210 gint w = gdk_pixbuf_get_width(pixbuf);
211 gint h = gdk_pixbuf_get_height(pixbuf);
213 if (w > requested_width || h > requested_height)
217 if (pixbuf_scale_aspect(requested_width, requested_height,
223 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
224 g_object_unref(G_OBJECT(tmp));
233 *-----------------------------------------------------------------------------
235 *-----------------------------------------------------------------------------
238 gint util_clip_region(gint x, gint y, gint w, gint h,
239 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
240 gint *rx, gint *ry, gint *rw, gint *rh)
242 if (clip_x + clip_w <= x ||
244 clip_y + clip_h <= y ||
250 *rx = MAX(x, clip_x);
251 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
253 *ry = MAX(y, clip_y);
254 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
260 *-----------------------------------------------------------------------------
262 *-----------------------------------------------------------------------------
265 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
266 guchar *dest, gint dest_row_stride, gint w, gint h,
267 gint bytes_per_pixel, gint counter_clockwise)
273 for (i = 0; i < h; i++)
275 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
276 for (j = 0; j < w; j++)
278 if (counter_clockwise)
280 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
284 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
286 *(dp++) = *(sp++); /* r */
287 *(dp++) = *(sp++); /* g */
288 *(dp++) = *(sp++); /* b */
289 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
295 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
296 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
302 for (i = 0; i < h; i++)
304 sp = src + (i * src_row_stride);
305 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
306 memcpy(dp, sp, w * bytes_per_pixel);
310 #define ROTATE_BUFFER_WIDTH 48
311 #define ROTATE_BUFFER_HEIGHT 48
314 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
317 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gint counter_clockwise)
336 if (!src) return NULL;
338 sw = gdk_pixbuf_get_width(src);
339 sh = gdk_pixbuf_get_height(src);
340 has_alpha = gdk_pixbuf_get_has_alpha(src);
341 srs = gdk_pixbuf_get_rowstride(src);
342 s_pix = gdk_pixbuf_get_pixels(src);
346 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
347 drs = gdk_pixbuf_get_rowstride(dest);
348 d_pix = gdk_pixbuf_get_pixels(dest);
350 a = (has_alpha ? 4 : 3);
352 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
353 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
354 b_pix = gdk_pixbuf_get_pixels(buffer);
355 brs = gdk_pixbuf_get_rowstride(buffer);
357 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
359 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
360 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
364 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
365 pixbuf_copy_block_rotate(s_pix, srs, j, i,
367 a, counter_clockwise);
369 if (counter_clockwise)
379 pixbuf_copy_block(b_pix, brs, w, h,
380 d_pix, drs, x, y, a);
384 g_object_unref(buffer);
387 /* this is the simple version of rotation (roughly 2-4x slower) */
389 for (i = 0; i < sh; i++)
391 sp = s_pix + (i * srs);
392 for (j = 0; j < sw; j++)
394 if (counter_clockwise)
396 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
400 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
403 *(dp++) = *(sp++); /* r */
404 *(dp++) = *(sp++); /* g */
405 *(dp++) = *(sp++); /* b */
406 if (has_alpha) *(dp) = *(sp++); /* a */
415 * Returns a copy of pixbuf mirrored and or flipped.
416 * TO do a 180 degree rotations set both mirror and flipped TRUE
417 * if mirror and flip are FALSE, result is a simple copy.
419 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gint mirror, gint flip)
432 if (!src) return NULL;
434 w = gdk_pixbuf_get_width(src);
435 h = gdk_pixbuf_get_height(src);
436 has_alpha = gdk_pixbuf_get_has_alpha(src);
437 srs = gdk_pixbuf_get_rowstride(src);
438 s_pix = gdk_pixbuf_get_pixels(src);
440 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
441 drs = gdk_pixbuf_get_rowstride(dest);
442 d_pix = gdk_pixbuf_get_pixels(dest);
444 a = has_alpha ? 4 : 3;
446 for (i = 0; i < h; i++)
448 sp = s_pix + (i * srs);
451 dp = d_pix + ((h - i - 1) * drs);
455 dp = d_pix + (i * drs);
460 for (j = 0; j < w; j++)
462 *(dp++) = *(sp++); /* r */
463 *(dp++) = *(sp++); /* g */
464 *(dp++) = *(sp++); /* b */
465 if (has_alpha) *(dp) = *(sp++); /* a */
471 for (j = 0; j < w; j++)
473 *(dp++) = *(sp++); /* r */
474 *(dp++) = *(sp++); /* g */
475 *(dp++) = *(sp++); /* b */
476 if (has_alpha) *(dp++) = *(sp++); /* a */
484 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
487 GdkPixbuf *tmp = NULL;
491 case EXIF_ORIENTATION_TOP_LEFT:
492 dest = gdk_pixbuf_copy(pixbuf);
494 case EXIF_ORIENTATION_TOP_RIGHT:
496 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
498 case EXIF_ORIENTATION_BOTTOM_RIGHT:
500 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
502 case EXIF_ORIENTATION_BOTTOM_LEFT:
504 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
506 case EXIF_ORIENTATION_LEFT_TOP:
507 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
508 dest = pixbuf_copy_rotate_90(tmp, FALSE);
510 case EXIF_ORIENTATION_RIGHT_TOP:
511 /* rotated -90 (270) */
512 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
514 case EXIF_ORIENTATION_RIGHT_BOTTOM:
515 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
516 dest = pixbuf_copy_rotate_90(tmp, TRUE);
518 case EXIF_ORIENTATION_LEFT_BOTTOM:
520 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
523 dest = gdk_pixbuf_copy(pixbuf);
526 if (tmp) g_object_unref(tmp);
533 *-----------------------------------------------------------------------------
534 * pixbuf drawing (rectangles)
535 *-----------------------------------------------------------------------------
539 * Fills region of pixbuf at x,y over w,h
540 * with colors red (r), green (g), blue (b)
541 * applying alpha (a), use a=255 for solid.
543 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
544 gint x, gint y, gint w, gint h,
545 gint r, gint g, gint b, gint a)
555 pw = gdk_pixbuf_get_width(pb);
556 ph = gdk_pixbuf_get_height(pb);
558 if (x < 0 || x + w > pw) return;
559 if (y < 0 || y + h > ph) return;
561 p_alpha = gdk_pixbuf_get_has_alpha(pb);
562 prs = gdk_pixbuf_get_rowstride(pb);
563 p_pix = gdk_pixbuf_get_pixels(pb);
565 for (i = 0; i < h; i++)
567 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
568 for (j = 0; j < w; j++)
570 *pp = (r * a + *pp * (256-a)) >> 8;
572 *pp = (g * a + *pp * (256-a)) >> 8;
574 *pp = (b * a + *pp * (256-a)) >> 8;
581 void pixbuf_draw_rect(GdkPixbuf *pb,
582 gint x, gint y, gint w, gint h,
583 gint r, gint g, gint b, gint a,
584 gint left, gint right, gint top, gint bottom)
586 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
588 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
590 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
592 pixbuf_draw_rect_fill(pb, x, y, left, h,
596 void pixbuf_set_rect_fill(GdkPixbuf *pb,
597 gint x, gint y, gint w, gint h,
598 gint r, gint g, gint b, gint a)
608 pw = gdk_pixbuf_get_width(pb);
609 ph = gdk_pixbuf_get_height(pb);
611 if (x < 0 || x + w > pw) return;
612 if (y < 0 || y + h > ph) return;
614 p_alpha = gdk_pixbuf_get_has_alpha(pb);
615 prs = gdk_pixbuf_get_rowstride(pb);
616 p_pix = gdk_pixbuf_get_pixels(pb);
618 for (i = 0; i < h; i++)
620 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
621 for (j = 0; j < w; j++)
626 if (p_alpha) { *pp = a; pp++; }
631 void pixbuf_set_rect(GdkPixbuf *pb,
632 gint x, gint y, gint w, gint h,
633 gint r, gint g, gint b, gint a,
634 gint left, gint right, gint top, gint bottom)
636 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
638 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
640 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
642 pixbuf_set_rect_fill(pb, x, y, left, h,
646 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
653 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
654 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
656 buf = gdk_pixbuf_get_pixels(pb);
657 has_alpha = gdk_pixbuf_get_has_alpha(pb);
658 rowstride = gdk_pixbuf_get_rowstride(pb);
660 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
664 if (has_alpha) *p = a;
669 *-----------------------------------------------------------------------------
670 * pixbuf text rendering
671 *-----------------------------------------------------------------------------
674 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
675 GdkPixbuf *dest, gint dx, gint dy,
677 guint8 r, guint8 g, guint8 b, guint8 a)
692 if (!src || !dest) return;
694 sw = gdk_pixbuf_get_width(src);
695 sh = gdk_pixbuf_get_height(src);
697 if (sx < 0 || sx + w > sw) return;
698 if (sy < 0 || sy + h > sh) return;
700 dw = gdk_pixbuf_get_width(dest);
701 dh = gdk_pixbuf_get_height(dest);
703 if (dx < 0 || dx + w > dw) return;
704 if (dy < 0 || dy + h > dh) return;
706 s_alpha = gdk_pixbuf_get_has_alpha(src);
707 d_alpha = gdk_pixbuf_get_has_alpha(dest);
708 srs = gdk_pixbuf_get_rowstride(src);
709 drs = gdk_pixbuf_get_rowstride(dest);
710 s_pix = gdk_pixbuf_get_pixels(src);
711 d_pix = gdk_pixbuf_get_pixels(dest);
713 s_step = (s_alpha) ? 4 : 3;
714 d_step = (d_alpha) ? 4 : 3;
716 for (i = 0; i < h; i++)
718 sp = s_pix + (sy + i) * srs + sx * s_step;
719 dp = d_pix + (dy + i) * drs + dx * d_step;
720 for (j = 0; j < w; j++)
726 asub = a * sp[0] / 255;
727 *dp = (r * asub + *dp * (256-asub)) >> 8;
729 asub = a * sp[1] / 255;
730 *dp = (g * asub + *dp * (256-asub)) >> 8;
732 asub = a * sp[2] / 255;
733 *dp = (b * asub + *dp * (256-asub)) >> 8;
738 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
752 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
754 guint8 r, guint8 g, guint8 b, guint8 a)
763 if (!widget || !widget->window) return;
765 pango_layout_get_pixel_size(layout, &w, &h);
766 if (w < 1 || h < 1) return;
768 pixmap = gdk_pixmap_new(widget->window, w, h, -1);
770 gc = gdk_gc_new(widget->window);
771 gdk_gc_copy(gc, widget->style->black_gc);
772 gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, w, h);
773 gdk_gc_copy(gc, widget->style->white_gc);
774 gdk_draw_layout(pixmap, gc, 0, 0, layout);
777 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, w, h);
778 gdk_pixbuf_get_from_drawable(buffer, pixmap,
779 gdk_drawable_get_colormap(widget->window),
781 g_object_unref(pixmap);
785 dw = gdk_pixbuf_get_width(pixbuf);
786 dh = gdk_pixbuf_get_height(pixbuf);
802 if (x + w > dw) w = dw - x;
803 if (y + h > dh) h = dh - y;
805 pixbuf_copy_font(buffer, sx, sy,
809 g_object_unref(buffer);
813 *-----------------------------------------------------------------------------
814 * pixbuf drawing (triangle)
815 *-----------------------------------------------------------------------------
818 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
819 gint *rx, gint *ry, gint *rw, gint *rh)
827 tw = MAX(abs(x1 - x2), abs(x2 - x3));
828 tw = MAX(tw, abs(x3 - x1));
829 th = MAX(abs(y1 - y2), abs(y2 - y3));
830 th = MAX(th, abs(y3 - y1));
838 void pixbuf_draw_triangle(GdkPixbuf *pb,
839 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
840 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
841 guint8 r, guint8 g, guint8 b, guint8 a)
853 gdouble slope1, slope2;
854 gint slope1_x, slope1_y;
861 pw = gdk_pixbuf_get_width(pb);
862 ph = gdk_pixbuf_get_height(pb);
864 if (!util_clip_region(0, 0, pw, ph,
865 clip_x, clip_y, clip_w, clip_h,
866 &rx, &ry, &rw, &rh)) return;
868 util_clip_triangle(x1, y1, x2, y2, x3, y3,
871 if (!util_clip_region(rx, ry, rw, rh,
873 &fx1, &fy1, &fw, &fh)) return;
877 p_alpha = gdk_pixbuf_get_has_alpha(pb);
878 prs = gdk_pixbuf_get_rowstride(pb);
879 p_pix = gdk_pixbuf_get_pixels(pb);
881 p_step = (p_alpha) ? 4 : 3;
885 t = x1; x1 = x2; x2 = t;
886 t = y1; y1 = y2; y2 = t;
890 t = x2; x2 = x3; x3 = t;
891 t = y2; y2 = y3; y3 = t;
895 t = x1; x1 = x2; x2 = t;
896 t = y1; y1 = y2; y2 = t;
899 slope1 = (gdouble)(y2 - y1);
900 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
903 slope2 = (gdouble)(y3 - y1);
904 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
906 for (y = fy1; y < fy2; y++)
910 if (!middle && y > y2)
912 slope1 = (gdouble)(y3 - y2);
913 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
920 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
921 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
925 t = xa; xa = xb; xb = t;
928 xa = CLAMP(xa, fx1, fx2);
929 xb = CLAMP(xb, fx1, fx2);
931 pp = p_pix + y * prs + xa * p_step;
935 *pp = (r * a + *pp * (256-a)) >> 8;
937 *pp = (g * a + *pp * (256-a)) >> 8;
939 *pp = (b * a + *pp * (256-a)) >> 8;
949 *-----------------------------------------------------------------------------
950 * pixbuf drawing (line)
951 *-----------------------------------------------------------------------------
954 static gint util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
955 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
956 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
965 t = x1; x1 = x2; x2 = t;
966 t = y1; y1 = y2; y2 = t;
970 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
974 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
978 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
982 if (x1 >= clip_x && x2 <= clip_x + clip_w)
986 if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
990 if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
1000 slope = (y2 - y1) / d;
1003 y1 = y1 + slope * (clip_x - x1);
1006 if (x2 > clip_x + clip_w)
1008 y2 = y2 + slope * (clip_x + clip_w - x2);
1009 x2 = clip_x + clip_w;
1015 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1021 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1023 t = x1; x1 = x2; x2 = t;
1024 t = y1; y1 = y2; y2 = t;
1033 slope = (x2 - x1) / d;
1036 x1 = x1 + slope * (clip_y - y1);
1039 if (y2 > clip_y + clip_h)
1041 x2 = x2 + slope * (clip_y + clip_h - y2);
1042 y2 = clip_y + clip_h;
1064 void pixbuf_draw_line(GdkPixbuf *pb,
1065 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1066 gint x1, gint y1, gint x2, gint y2,
1067 guint8 r, guint8 g, guint8 b, guint8 a)
1071 gint rx, ry, rw, rh;
1072 gdouble rx1, ry1, rx2, ry2;
1079 gint cx1, cy1, cx2, cy2;
1083 pw = gdk_pixbuf_get_width(pb);
1084 ph = gdk_pixbuf_get_height(pb);
1086 if (!util_clip_region(0, 0, pw, ph,
1087 clip_x, clip_y, clip_w, clip_h,
1088 &rx, &ry, &rw, &rh)) return;
1089 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1090 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1091 &rx1, &ry1, &rx2, &ry2)) return;
1098 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1099 prs = gdk_pixbuf_get_rowstride(pb);
1100 p_pix = gdk_pixbuf_get_pixels(pb);
1102 p_step = (p_alpha) ? 4 : 3;
1104 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1109 t = rx1; rx1 = rx2; rx2 = t;
1110 t = ry1; ry1 = ry2; ry2 = t;
1114 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1115 for (x = rx1; x < rx2; x += 1.0)
1117 px = (gint)(x + 0.5);
1118 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1120 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1122 pp = p_pix + py * prs + px * p_step;
1123 *pp = (r * a + *pp * (256-a)) >> 8;
1125 *pp = (g * a + *pp * (256-a)) >> 8;
1127 *pp = (b * a + *pp * (256-a)) >> 8;
1136 t = rx1; rx1 = rx2; rx2 = t;
1137 t = ry1; ry1 = ry2; ry2 = t;
1141 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1142 for (y = ry1; y < ry2; y += 1.0)
1144 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1145 py = (gint)(y + 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;
1161 *-----------------------------------------------------------------------------
1162 * pixbuf drawing (fades and shadows)
1163 *-----------------------------------------------------------------------------
1166 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gint p_alpha,
1167 gint s, gint vertical, gint border,
1168 gint x1, gint y1, gint x2, gint y2,
1169 guint8 r, guint8 g, guint8 b, guint8 a)
1176 p_step = (p_alpha) ? 4 : 3;
1177 for (j = y1; j < y2; j++)
1179 pp = p_pix + j * prs + x1 * p_step;
1180 if (!vertical) n = a - a * abs(j - s) / border;
1181 for (i = x1; i < x2; i++)
1183 if (vertical) n = a - a * abs(i - s) / border;
1184 *pp = (r * n + *pp * (256-n)) >> 8;
1186 *pp = (g * n + *pp * (256-n)) >> 8;
1188 *pp = (b * n + *pp * (256-n)) >> 8;
1195 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gint p_alpha,
1196 gint sx, gint sy, gint border,
1197 gint x1, gint y1, gint x2, gint y2,
1198 guint8 r, guint8 g, guint8 b, guint8 a)
1204 p_step = (p_alpha) ? 4 : 3;
1205 for (j = y1; j < y2; j++)
1207 pp = p_pix + j * prs + x1 * p_step;
1208 for (i = x1; i < x2; i++)
1213 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1214 n = a - a * r / border;
1215 *pp = (r * n + *pp * (256-n)) >> 8;
1217 *pp = (g * n + *pp * (256-n)) >> 8;
1219 *pp = (b * n + *pp * (256-n)) >> 8;
1226 void pixbuf_draw_shadow(GdkPixbuf *pb,
1227 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1228 gint x, gint y, gint w, gint h, gint border,
1229 guint8 r, guint8 g, guint8 b, guint8 a)
1233 gint rx, ry, rw, rh;
1234 gint fx, fy, fw, fh;
1239 pw = gdk_pixbuf_get_width(pb);
1240 ph = gdk_pixbuf_get_height(pb);
1242 if (!util_clip_region(0, 0, pw, ph,
1243 clip_x, clip_y, clip_w, clip_h,
1244 &rx, &ry, &rw, &rh)) return;
1246 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1247 prs = gdk_pixbuf_get_rowstride(pb);
1248 p_pix = gdk_pixbuf_get_pixels(pb);
1250 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1252 &fx, &fy, &fw, &fh))
1254 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1257 if (border < 1) return;
1259 if (util_clip_region(x, y + border, border, h - border * 2,
1261 &fx, &fy, &fw, &fh))
1263 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1264 x + border, TRUE, border,
1265 fx, fy, fx + fw, fy + fh,
1268 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1270 &fx, &fy, &fw, &fh))
1272 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1273 x + w - border, TRUE, border,
1274 fx, fy, fx + fw, fy + fh,
1277 if (util_clip_region(x + border, y, w - border * 2, border,
1279 &fx, &fy, &fw, &fh))
1281 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1282 y + border, FALSE, border,
1283 fx, fy, fx + fw, fy + fh,
1286 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1288 &fx, &fy, &fw, &fh))
1290 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1291 y + h - border, FALSE, border,
1292 fx, fy, fx + fw, fy + fh,
1295 if (util_clip_region(x, y, border, border,
1297 &fx, &fy, &fw, &fh))
1299 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1300 x + border, y + border, border,
1301 fx, fy, fx + fw, fy + fh,
1304 if (util_clip_region(x + w - border, y, border, border,
1306 &fx, &fy, &fw, &fh))
1308 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1309 x + w - border, y + border, border,
1310 fx, fy, fx + fw, fy + fh,
1313 if (util_clip_region(x, y + h - border, border, border,
1315 &fx, &fy, &fw, &fh))
1317 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1318 x + border, y + h - border, border,
1319 fx, fy, fx + fw, fy + fh,
1322 if (util_clip_region(x + w - border, y + h - border, border, border,
1324 &fx, &fy, &fw, &fh))
1326 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1327 x + w - border, y + h - border, border,
1328 fx, fy, fx + fw, fy + fh,
1335 *-----------------------------------------------------------------------------
1336 * pixbuf color alterations
1337 *-----------------------------------------------------------------------------
1340 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1341 gint x, gint y, gint w, gint h)
1351 pw = gdk_pixbuf_get_width(pb);
1352 ph = gdk_pixbuf_get_height(pb);
1354 if (x < 0 || x + w > pw) return;
1355 if (y < 0 || y + h > ph) return;
1357 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1358 prs = gdk_pixbuf_get_rowstride(pb);
1359 p_pix = gdk_pixbuf_get_pixels(pb);
1361 for (i = 0; i < h; i++)
1363 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
1364 for (j = 0; j < w; j++)
1368 grey = (pp[0] + pp[1] + pp[2]) / 3;
1379 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */