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 */
178 DEBUG_1("Couldn't load icon: %s", error->message);
184 register_stock_icon(key, pixbuf);
188 gint pixbuf_scale_aspect(gint req_w, gint req_h, gint old_w, gint old_h,
189 gint *new_w, gint *new_h)
191 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
194 *new_h = (gdouble)*new_w / old_w * old_h;
195 if (*new_h < 1) *new_h = 1;
200 *new_w = (gdouble)*new_h / old_h * old_w;
201 if (*new_w < 1) *new_w = 1;
204 return (*new_w != old_w || *new_h != old_h);
207 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
209 GdkPixbuf *pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); /* FIXME use different images according to FORMAT_CLASS */
211 if (requested_width && requested_height)
213 gint w = gdk_pixbuf_get_width(pixbuf);
214 gint h = gdk_pixbuf_get_height(pixbuf);
216 if (w > requested_width || h > requested_height)
220 if (pixbuf_scale_aspect(requested_width, requested_height,
226 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
227 g_object_unref(G_OBJECT(tmp));
236 *-----------------------------------------------------------------------------
238 *-----------------------------------------------------------------------------
241 gint util_clip_region(gint x, gint y, gint w, gint h,
242 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
243 gint *rx, gint *ry, gint *rw, gint *rh)
245 if (clip_x + clip_w <= x ||
247 clip_y + clip_h <= y ||
253 *rx = MAX(x, clip_x);
254 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
256 *ry = MAX(y, clip_y);
257 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
263 *-----------------------------------------------------------------------------
265 *-----------------------------------------------------------------------------
268 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
269 guchar *dest, gint dest_row_stride, gint w, gint h,
270 gint bytes_per_pixel, gint counter_clockwise)
276 for (i = 0; i < h; i++)
278 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
279 for (j = 0; j < w; j++)
281 if (counter_clockwise)
283 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
287 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
289 *(dp++) = *(sp++); /* r */
290 *(dp++) = *(sp++); /* g */
291 *(dp++) = *(sp++); /* b */
292 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
298 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
299 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
305 for (i = 0; i < h; i++)
307 sp = src + (i * src_row_stride);
308 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
309 memcpy(dp, sp, w * bytes_per_pixel);
313 #define ROTATE_BUFFER_WIDTH 48
314 #define ROTATE_BUFFER_HEIGHT 48
317 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
320 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gint counter_clockwise)
339 if (!src) return NULL;
341 sw = gdk_pixbuf_get_width(src);
342 sh = gdk_pixbuf_get_height(src);
343 has_alpha = gdk_pixbuf_get_has_alpha(src);
344 srs = gdk_pixbuf_get_rowstride(src);
345 s_pix = gdk_pixbuf_get_pixels(src);
349 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
350 drs = gdk_pixbuf_get_rowstride(dest);
351 d_pix = gdk_pixbuf_get_pixels(dest);
353 a = (has_alpha ? 4 : 3);
355 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
356 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
357 b_pix = gdk_pixbuf_get_pixels(buffer);
358 brs = gdk_pixbuf_get_rowstride(buffer);
360 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
362 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
363 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
367 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
368 pixbuf_copy_block_rotate(s_pix, srs, j, i,
370 a, counter_clockwise);
372 if (counter_clockwise)
382 pixbuf_copy_block(b_pix, brs, w, h,
383 d_pix, drs, x, y, a);
387 g_object_unref(buffer);
390 /* this is the simple version of rotation (roughly 2-4x slower) */
392 for (i = 0; i < sh; i++)
394 sp = s_pix + (i * srs);
395 for (j = 0; j < sw; j++)
397 if (counter_clockwise)
399 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
403 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
406 *(dp++) = *(sp++); /* r */
407 *(dp++) = *(sp++); /* g */
408 *(dp++) = *(sp++); /* b */
409 if (has_alpha) *(dp) = *(sp++); /* a */
418 * Returns a copy of pixbuf mirrored and or flipped.
419 * TO do a 180 degree rotations set both mirror and flipped TRUE
420 * if mirror and flip are FALSE, result is a simple copy.
422 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gint mirror, gint flip)
435 if (!src) return NULL;
437 w = gdk_pixbuf_get_width(src);
438 h = gdk_pixbuf_get_height(src);
439 has_alpha = gdk_pixbuf_get_has_alpha(src);
440 srs = gdk_pixbuf_get_rowstride(src);
441 s_pix = gdk_pixbuf_get_pixels(src);
443 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
444 drs = gdk_pixbuf_get_rowstride(dest);
445 d_pix = gdk_pixbuf_get_pixels(dest);
447 a = has_alpha ? 4 : 3;
449 for (i = 0; i < h; i++)
451 sp = s_pix + (i * srs);
454 dp = d_pix + ((h - i - 1) * drs);
458 dp = d_pix + (i * drs);
463 for (j = 0; j < w; j++)
465 *(dp++) = *(sp++); /* r */
466 *(dp++) = *(sp++); /* g */
467 *(dp++) = *(sp++); /* b */
468 if (has_alpha) *(dp) = *(sp++); /* a */
474 for (j = 0; j < w; j++)
476 *(dp++) = *(sp++); /* r */
477 *(dp++) = *(sp++); /* g */
478 *(dp++) = *(sp++); /* b */
479 if (has_alpha) *(dp++) = *(sp++); /* a */
487 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
490 GdkPixbuf *tmp = NULL;
494 case EXIF_ORIENTATION_TOP_LEFT:
495 dest = gdk_pixbuf_copy(pixbuf);
497 case EXIF_ORIENTATION_TOP_RIGHT:
499 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
501 case EXIF_ORIENTATION_BOTTOM_RIGHT:
503 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
505 case EXIF_ORIENTATION_BOTTOM_LEFT:
507 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
509 case EXIF_ORIENTATION_LEFT_TOP:
510 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
511 dest = pixbuf_copy_rotate_90(tmp, FALSE);
513 case EXIF_ORIENTATION_RIGHT_TOP:
514 /* rotated -90 (270) */
515 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
517 case EXIF_ORIENTATION_RIGHT_BOTTOM:
518 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
519 dest = pixbuf_copy_rotate_90(tmp, TRUE);
521 case EXIF_ORIENTATION_LEFT_BOTTOM:
523 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
526 dest = gdk_pixbuf_copy(pixbuf);
529 if (tmp) g_object_unref(tmp);
536 *-----------------------------------------------------------------------------
537 * pixbuf drawing (rectangles)
538 *-----------------------------------------------------------------------------
542 * Fills region of pixbuf at x,y over w,h
543 * with colors red (r), green (g), blue (b)
544 * applying alpha (a), use a=255 for solid.
546 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
547 gint x, gint y, gint w, gint h,
548 gint r, gint g, gint b, gint a)
558 pw = gdk_pixbuf_get_width(pb);
559 ph = gdk_pixbuf_get_height(pb);
561 if (x < 0 || x + w > pw) return;
562 if (y < 0 || y + h > ph) return;
564 p_alpha = gdk_pixbuf_get_has_alpha(pb);
565 prs = gdk_pixbuf_get_rowstride(pb);
566 p_pix = gdk_pixbuf_get_pixels(pb);
568 for (i = 0; i < h; i++)
570 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
571 for (j = 0; j < w; j++)
573 *pp = (r * a + *pp * (256-a)) >> 8;
575 *pp = (g * a + *pp * (256-a)) >> 8;
577 *pp = (b * a + *pp * (256-a)) >> 8;
584 void pixbuf_draw_rect(GdkPixbuf *pb,
585 gint x, gint y, gint w, gint h,
586 gint r, gint g, gint b, gint a,
587 gint left, gint right, gint top, gint bottom)
589 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
591 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
593 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
595 pixbuf_draw_rect_fill(pb, x, y, left, h,
599 void pixbuf_set_rect_fill(GdkPixbuf *pb,
600 gint x, gint y, gint w, gint h,
601 gint r, gint g, gint b, gint a)
611 pw = gdk_pixbuf_get_width(pb);
612 ph = gdk_pixbuf_get_height(pb);
614 if (x < 0 || x + w > pw) return;
615 if (y < 0 || y + h > ph) return;
617 p_alpha = gdk_pixbuf_get_has_alpha(pb);
618 prs = gdk_pixbuf_get_rowstride(pb);
619 p_pix = gdk_pixbuf_get_pixels(pb);
621 for (i = 0; i < h; i++)
623 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
624 for (j = 0; j < w; j++)
629 if (p_alpha) { *pp = a; pp++; }
634 void pixbuf_set_rect(GdkPixbuf *pb,
635 gint x, gint y, gint w, gint h,
636 gint r, gint g, gint b, gint a,
637 gint left, gint right, gint top, gint bottom)
639 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
641 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
643 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
645 pixbuf_set_rect_fill(pb, x, y, left, h,
649 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
656 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
657 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
659 buf = gdk_pixbuf_get_pixels(pb);
660 has_alpha = gdk_pixbuf_get_has_alpha(pb);
661 rowstride = gdk_pixbuf_get_rowstride(pb);
663 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
667 if (has_alpha) *p = a;
672 *-----------------------------------------------------------------------------
673 * pixbuf text rendering
674 *-----------------------------------------------------------------------------
677 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
678 GdkPixbuf *dest, gint dx, gint dy,
680 guint8 r, guint8 g, guint8 b, guint8 a)
695 if (!src || !dest) return;
697 sw = gdk_pixbuf_get_width(src);
698 sh = gdk_pixbuf_get_height(src);
700 if (sx < 0 || sx + w > sw) return;
701 if (sy < 0 || sy + h > sh) return;
703 dw = gdk_pixbuf_get_width(dest);
704 dh = gdk_pixbuf_get_height(dest);
706 if (dx < 0 || dx + w > dw) return;
707 if (dy < 0 || dy + h > dh) return;
709 s_alpha = gdk_pixbuf_get_has_alpha(src);
710 d_alpha = gdk_pixbuf_get_has_alpha(dest);
711 srs = gdk_pixbuf_get_rowstride(src);
712 drs = gdk_pixbuf_get_rowstride(dest);
713 s_pix = gdk_pixbuf_get_pixels(src);
714 d_pix = gdk_pixbuf_get_pixels(dest);
716 s_step = (s_alpha) ? 4 : 3;
717 d_step = (d_alpha) ? 4 : 3;
719 for (i = 0; i < h; i++)
721 sp = s_pix + (sy + i) * srs + sx * s_step;
722 dp = d_pix + (dy + i) * drs + dx * d_step;
723 for (j = 0; j < w; j++)
729 asub = a * sp[0] / 255;
730 *dp = (r * asub + *dp * (256-asub)) >> 8;
732 asub = a * sp[1] / 255;
733 *dp = (g * asub + *dp * (256-asub)) >> 8;
735 asub = a * sp[2] / 255;
736 *dp = (b * asub + *dp * (256-asub)) >> 8;
741 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
755 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
757 guint8 r, guint8 g, guint8 b, guint8 a)
766 if (!widget || !widget->window) return;
768 pango_layout_get_pixel_size(layout, &w, &h);
769 if (w < 1 || h < 1) return;
771 pixmap = gdk_pixmap_new(widget->window, w, h, -1);
773 gc = gdk_gc_new(widget->window);
774 gdk_gc_copy(gc, widget->style->black_gc);
775 gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, w, h);
776 gdk_gc_copy(gc, widget->style->white_gc);
777 gdk_draw_layout(pixmap, gc, 0, 0, layout);
780 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, w, h);
781 gdk_pixbuf_get_from_drawable(buffer, pixmap,
782 gdk_drawable_get_colormap(widget->window),
784 g_object_unref(pixmap);
788 dw = gdk_pixbuf_get_width(pixbuf);
789 dh = gdk_pixbuf_get_height(pixbuf);
805 if (x + w > dw) w = dw - x;
806 if (y + h > dh) h = dh - y;
808 pixbuf_copy_font(buffer, sx, sy,
812 g_object_unref(buffer);
816 *-----------------------------------------------------------------------------
817 * pixbuf drawing (triangle)
818 *-----------------------------------------------------------------------------
821 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
822 gint *rx, gint *ry, gint *rw, gint *rh)
830 tw = MAX(abs(x1 - x2), abs(x2 - x3));
831 tw = MAX(tw, abs(x3 - x1));
832 th = MAX(abs(y1 - y2), abs(y2 - y3));
833 th = MAX(th, abs(y3 - y1));
841 void pixbuf_draw_triangle(GdkPixbuf *pb,
842 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
843 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
844 guint8 r, guint8 g, guint8 b, guint8 a)
856 gdouble slope1, slope2;
857 gint slope1_x, slope1_y;
864 pw = gdk_pixbuf_get_width(pb);
865 ph = gdk_pixbuf_get_height(pb);
867 if (!util_clip_region(0, 0, pw, ph,
868 clip_x, clip_y, clip_w, clip_h,
869 &rx, &ry, &rw, &rh)) return;
871 util_clip_triangle(x1, y1, x2, y2, x3, y3,
874 if (!util_clip_region(rx, ry, rw, rh,
876 &fx1, &fy1, &fw, &fh)) return;
880 p_alpha = gdk_pixbuf_get_has_alpha(pb);
881 prs = gdk_pixbuf_get_rowstride(pb);
882 p_pix = gdk_pixbuf_get_pixels(pb);
884 p_step = (p_alpha) ? 4 : 3;
888 t = x1; x1 = x2; x2 = t;
889 t = y1; y1 = y2; y2 = t;
893 t = x2; x2 = x3; x3 = t;
894 t = y2; y2 = y3; y3 = t;
898 t = x1; x1 = x2; x2 = t;
899 t = y1; y1 = y2; y2 = t;
902 slope1 = (gdouble)(y2 - y1);
903 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
906 slope2 = (gdouble)(y3 - y1);
907 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
909 for (y = fy1; y < fy2; y++)
913 if (!middle && y > y2)
915 slope1 = (gdouble)(y3 - y2);
916 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
923 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
924 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
928 t = xa; xa = xb; xb = t;
931 xa = CLAMP(xa, fx1, fx2);
932 xb = CLAMP(xb, fx1, fx2);
934 pp = p_pix + y * prs + xa * p_step;
938 *pp = (r * a + *pp * (256-a)) >> 8;
940 *pp = (g * a + *pp * (256-a)) >> 8;
942 *pp = (b * a + *pp * (256-a)) >> 8;
952 *-----------------------------------------------------------------------------
953 * pixbuf drawing (line)
954 *-----------------------------------------------------------------------------
957 static gint util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
958 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
959 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
968 t = x1; x1 = x2; x2 = t;
969 t = y1; y1 = y2; y2 = t;
973 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
977 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
981 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
985 if (x1 >= clip_x && x2 <= clip_x + clip_w)
989 if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
993 if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
1003 slope = (y2 - y1) / d;
1006 y1 = y1 + slope * (clip_x - x1);
1009 if (x2 > clip_x + clip_w)
1011 y2 = y2 + slope * (clip_x + clip_w - x2);
1012 x2 = clip_x + clip_w;
1018 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1024 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1026 t = x1; x1 = x2; x2 = t;
1027 t = y1; y1 = y2; y2 = t;
1036 slope = (x2 - x1) / d;
1039 x1 = x1 + slope * (clip_y - y1);
1042 if (y2 > clip_y + clip_h)
1044 x2 = x2 + slope * (clip_y + clip_h - y2);
1045 y2 = clip_y + clip_h;
1067 void pixbuf_draw_line(GdkPixbuf *pb,
1068 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1069 gint x1, gint y1, gint x2, gint y2,
1070 guint8 r, guint8 g, guint8 b, guint8 a)
1074 gint rx, ry, rw, rh;
1075 gdouble rx1, ry1, rx2, ry2;
1082 gint cx1, cy1, cx2, cy2;
1086 pw = gdk_pixbuf_get_width(pb);
1087 ph = gdk_pixbuf_get_height(pb);
1089 if (!util_clip_region(0, 0, pw, ph,
1090 clip_x, clip_y, clip_w, clip_h,
1091 &rx, &ry, &rw, &rh)) return;
1092 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1093 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1094 &rx1, &ry1, &rx2, &ry2)) return;
1101 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1102 prs = gdk_pixbuf_get_rowstride(pb);
1103 p_pix = gdk_pixbuf_get_pixels(pb);
1105 p_step = (p_alpha) ? 4 : 3;
1107 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1112 t = rx1; rx1 = rx2; rx2 = t;
1113 t = ry1; ry1 = ry2; ry2 = t;
1117 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1118 for (x = rx1; x < rx2; x += 1.0)
1120 px = (gint)(x + 0.5);
1121 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1123 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1125 pp = p_pix + py * prs + px * p_step;
1126 *pp = (r * a + *pp * (256-a)) >> 8;
1128 *pp = (g * a + *pp * (256-a)) >> 8;
1130 *pp = (b * a + *pp * (256-a)) >> 8;
1139 t = rx1; rx1 = rx2; rx2 = t;
1140 t = ry1; ry1 = ry2; ry2 = t;
1144 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1145 for (y = ry1; y < ry2; y += 1.0)
1147 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1148 py = (gint)(y + 0.5);
1150 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1152 pp = p_pix + py * prs + px * p_step;
1153 *pp = (r * a + *pp * (256-a)) >> 8;
1155 *pp = (g * a + *pp * (256-a)) >> 8;
1157 *pp = (b * a + *pp * (256-a)) >> 8;
1164 *-----------------------------------------------------------------------------
1165 * pixbuf drawing (fades and shadows)
1166 *-----------------------------------------------------------------------------
1169 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gint p_alpha,
1170 gint s, gint vertical, gint border,
1171 gint x1, gint y1, gint x2, gint y2,
1172 guint8 r, guint8 g, guint8 b, guint8 a)
1179 p_step = (p_alpha) ? 4 : 3;
1180 for (j = y1; j < y2; j++)
1182 pp = p_pix + j * prs + x1 * p_step;
1183 if (!vertical) n = a - a * abs(j - s) / border;
1184 for (i = x1; i < x2; i++)
1186 if (vertical) n = a - a * abs(i - s) / border;
1187 *pp = (r * n + *pp * (256-n)) >> 8;
1189 *pp = (g * n + *pp * (256-n)) >> 8;
1191 *pp = (b * n + *pp * (256-n)) >> 8;
1198 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gint p_alpha,
1199 gint sx, gint sy, gint border,
1200 gint x1, gint y1, gint x2, gint y2,
1201 guint8 r, guint8 g, guint8 b, guint8 a)
1207 p_step = (p_alpha) ? 4 : 3;
1208 for (j = y1; j < y2; j++)
1210 pp = p_pix + j * prs + x1 * p_step;
1211 for (i = x1; i < x2; i++)
1216 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1217 n = a - a * r / border;
1218 *pp = (r * n + *pp * (256-n)) >> 8;
1220 *pp = (g * n + *pp * (256-n)) >> 8;
1222 *pp = (b * n + *pp * (256-n)) >> 8;
1229 void pixbuf_draw_shadow(GdkPixbuf *pb,
1230 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1231 gint x, gint y, gint w, gint h, gint border,
1232 guint8 r, guint8 g, guint8 b, guint8 a)
1236 gint rx, ry, rw, rh;
1237 gint fx, fy, fw, fh;
1242 pw = gdk_pixbuf_get_width(pb);
1243 ph = gdk_pixbuf_get_height(pb);
1245 if (!util_clip_region(0, 0, pw, ph,
1246 clip_x, clip_y, clip_w, clip_h,
1247 &rx, &ry, &rw, &rh)) return;
1249 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1250 prs = gdk_pixbuf_get_rowstride(pb);
1251 p_pix = gdk_pixbuf_get_pixels(pb);
1253 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1255 &fx, &fy, &fw, &fh))
1257 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1260 if (border < 1) return;
1262 if (util_clip_region(x, y + border, border, h - border * 2,
1264 &fx, &fy, &fw, &fh))
1266 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1267 x + border, TRUE, border,
1268 fx, fy, fx + fw, fy + fh,
1271 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1273 &fx, &fy, &fw, &fh))
1275 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1276 x + w - border, TRUE, border,
1277 fx, fy, fx + fw, fy + fh,
1280 if (util_clip_region(x + border, y, w - border * 2, border,
1282 &fx, &fy, &fw, &fh))
1284 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1285 y + border, FALSE, border,
1286 fx, fy, fx + fw, fy + fh,
1289 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1291 &fx, &fy, &fw, &fh))
1293 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1294 y + h - border, FALSE, border,
1295 fx, fy, fx + fw, fy + fh,
1298 if (util_clip_region(x, y, border, border,
1300 &fx, &fy, &fw, &fh))
1302 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1303 x + border, y + border, border,
1304 fx, fy, fx + fw, fy + fh,
1307 if (util_clip_region(x + w - border, y, border, border,
1309 &fx, &fy, &fw, &fh))
1311 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1312 x + w - border, y + border, border,
1313 fx, fy, fx + fw, fy + fh,
1316 if (util_clip_region(x, y + h - border, border, border,
1318 &fx, &fy, &fw, &fh))
1320 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1321 x + border, y + h - border, border,
1322 fx, fy, fx + fw, fy + fh,
1325 if (util_clip_region(x + w - border, y + h - border, border, border,
1327 &fx, &fy, &fw, &fh))
1329 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1330 x + w - border, y + h - border, border,
1331 fx, fy, fx + fw, fy + fh,
1338 *-----------------------------------------------------------------------------
1339 * pixbuf color alterations
1340 *-----------------------------------------------------------------------------
1343 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1344 gint x, gint y, gint w, gint h)
1354 pw = gdk_pixbuf_get_width(pb);
1355 ph = gdk_pixbuf_get_height(pb);
1357 if (x < 0 || x + w > pw) return;
1358 if (y < 0 || y + h > ph) return;
1360 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1361 prs = gdk_pixbuf_get_rowstride(pb);
1362 p_pix = gdk_pixbuf_get_pixels(pb);
1364 for (i = 0; i < h; i++)
1366 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
1367 for (j = 0; j < w; j++)
1371 grey = (pp[0] + pp[1] + pp[2]) / 3;
1382 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */