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 gint pixbuf_scale_aspect(gint req_w, gint req_h, gint old_w, gint old_h,
135 gint *new_w, gint *new_h)
137 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
140 *new_h = (gdouble)*new_w / old_w * old_h;
141 if (*new_h < 1) *new_h = 1;
146 *new_w = (gdouble)*new_h / old_h * old_w;
147 if (*new_w < 1) *new_w = 1;
150 return (*new_w != old_w || *new_h != old_h);
153 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
155 GdkPixbuf *pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); /* FIXME use different images according to FORMAT_CLASS */
157 if (requested_width && requested_height)
159 gint w = gdk_pixbuf_get_width(pixbuf);
160 gint h = gdk_pixbuf_get_height(pixbuf);
162 if (w > requested_width || h > requested_height)
166 if (pixbuf_scale_aspect(requested_width, requested_height,
172 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
173 g_object_unref(G_OBJECT(tmp));
182 *-----------------------------------------------------------------------------
184 *-----------------------------------------------------------------------------
187 gint util_clip_region(gint x, gint y, gint w, gint h,
188 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
189 gint *rx, gint *ry, gint *rw, gint *rh)
191 if (clip_x + clip_w <= x ||
193 clip_y + clip_h <= y ||
199 *rx = MAX(x, clip_x);
200 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
202 *ry = MAX(y, clip_y);
203 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
209 *-----------------------------------------------------------------------------
211 *-----------------------------------------------------------------------------
214 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
215 guchar *dest, gint dest_row_stride, gint w, gint h,
216 gint bytes_per_pixel, gint counter_clockwise)
222 for (i = 0; i < h; i++)
224 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
225 for (j = 0; j < w; j++)
227 if (counter_clockwise)
229 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
233 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
235 *(dp++) = *(sp++); /* r */
236 *(dp++) = *(sp++); /* g */
237 *(dp++) = *(sp++); /* b */
238 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
244 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
245 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
251 for (i = 0; i < h; i++)
253 sp = src + (i * src_row_stride);
254 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
255 memcpy(dp, sp, w * bytes_per_pixel);
259 #define ROTATE_BUFFER_WIDTH 48
260 #define ROTATE_BUFFER_HEIGHT 48
263 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
266 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gint counter_clockwise)
285 if (!src) return NULL;
287 sw = gdk_pixbuf_get_width(src);
288 sh = gdk_pixbuf_get_height(src);
289 has_alpha = gdk_pixbuf_get_has_alpha(src);
290 srs = gdk_pixbuf_get_rowstride(src);
291 s_pix = gdk_pixbuf_get_pixels(src);
295 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
296 drs = gdk_pixbuf_get_rowstride(dest);
297 d_pix = gdk_pixbuf_get_pixels(dest);
299 a = (has_alpha ? 4 : 3);
301 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
302 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
303 b_pix = gdk_pixbuf_get_pixels(buffer);
304 brs = gdk_pixbuf_get_rowstride(buffer);
306 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
308 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
309 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
313 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
314 pixbuf_copy_block_rotate(s_pix, srs, j, i,
316 a, counter_clockwise);
318 if (counter_clockwise)
328 pixbuf_copy_block(b_pix, brs, w, h,
329 d_pix, drs, x, y, a);
333 g_object_unref(buffer);
336 /* this is the simple version of rotation (roughly 2-4x slower) */
338 for (i = 0; i < sh; i++)
340 sp = s_pix + (i * srs);
341 for (j = 0; j < sw; j++)
343 if (counter_clockwise)
345 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
349 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
352 *(dp++) = *(sp++); /* r */
353 *(dp++) = *(sp++); /* g */
354 *(dp++) = *(sp++); /* b */
355 if (has_alpha) *(dp) = *(sp++); /* a */
364 * Returns a copy of pixbuf mirrored and or flipped.
365 * TO do a 180 degree rotations set both mirror and flipped TRUE
366 * if mirror and flip are FALSE, result is a simple copy.
368 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gint mirror, gint flip)
381 if (!src) return NULL;
383 w = gdk_pixbuf_get_width(src);
384 h = gdk_pixbuf_get_height(src);
385 has_alpha = gdk_pixbuf_get_has_alpha(src);
386 srs = gdk_pixbuf_get_rowstride(src);
387 s_pix = gdk_pixbuf_get_pixels(src);
389 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
390 drs = gdk_pixbuf_get_rowstride(dest);
391 d_pix = gdk_pixbuf_get_pixels(dest);
393 a = has_alpha ? 4 : 3;
395 for (i = 0; i < h; i++)
397 sp = s_pix + (i * srs);
400 dp = d_pix + ((h - i - 1) * drs);
404 dp = d_pix + (i * drs);
409 for (j = 0; j < w; j++)
411 *(dp++) = *(sp++); /* r */
412 *(dp++) = *(sp++); /* g */
413 *(dp++) = *(sp++); /* b */
414 if (has_alpha) *(dp) = *(sp++); /* a */
420 for (j = 0; j < w; j++)
422 *(dp++) = *(sp++); /* r */
423 *(dp++) = *(sp++); /* g */
424 *(dp++) = *(sp++); /* b */
425 if (has_alpha) *(dp++) = *(sp++); /* a */
433 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
436 GdkPixbuf *tmp = NULL;
440 case EXIF_ORIENTATION_TOP_LEFT:
441 dest = gdk_pixbuf_copy(pixbuf);
443 case EXIF_ORIENTATION_TOP_RIGHT:
445 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
447 case EXIF_ORIENTATION_BOTTOM_RIGHT:
449 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
451 case EXIF_ORIENTATION_BOTTOM_LEFT:
453 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
455 case EXIF_ORIENTATION_LEFT_TOP:
456 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
457 dest = pixbuf_copy_rotate_90(tmp, FALSE);
459 case EXIF_ORIENTATION_RIGHT_TOP:
460 /* rotated -90 (270) */
461 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
463 case EXIF_ORIENTATION_RIGHT_BOTTOM:
464 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
465 dest = pixbuf_copy_rotate_90(tmp, TRUE);
467 case EXIF_ORIENTATION_LEFT_BOTTOM:
469 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
472 dest = gdk_pixbuf_copy(pixbuf);
475 if (tmp) g_object_unref(tmp);
482 *-----------------------------------------------------------------------------
483 * pixbuf drawing (rectangles)
484 *-----------------------------------------------------------------------------
488 * Fills region of pixbuf at x,y over w,h
489 * with colors red (r), green (g), blue (b)
490 * applying alpha (a), use a=255 for solid.
492 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
493 gint x, gint y, gint w, gint h,
494 gint r, gint g, gint b, gint a)
504 pw = gdk_pixbuf_get_width(pb);
505 ph = gdk_pixbuf_get_height(pb);
507 if (x < 0 || x + w > pw) return;
508 if (y < 0 || y + h > ph) return;
510 p_alpha = gdk_pixbuf_get_has_alpha(pb);
511 prs = gdk_pixbuf_get_rowstride(pb);
512 p_pix = gdk_pixbuf_get_pixels(pb);
514 for (i = 0; i < h; i++)
516 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
517 for (j = 0; j < w; j++)
519 *pp = (r * a + *pp * (256-a)) >> 8;
521 *pp = (g * a + *pp * (256-a)) >> 8;
523 *pp = (b * a + *pp * (256-a)) >> 8;
530 void pixbuf_draw_rect(GdkPixbuf *pb,
531 gint x, gint y, gint w, gint h,
532 gint r, gint g, gint b, gint a,
533 gint left, gint right, gint top, gint bottom)
535 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
537 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
539 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
541 pixbuf_draw_rect_fill(pb, x, y, left, h,
545 void pixbuf_set_rect_fill(GdkPixbuf *pb,
546 gint x, gint y, gint w, gint h,
547 gint r, gint g, gint b, gint a)
557 pw = gdk_pixbuf_get_width(pb);
558 ph = gdk_pixbuf_get_height(pb);
560 if (x < 0 || x + w > pw) return;
561 if (y < 0 || y + h > ph) return;
563 p_alpha = gdk_pixbuf_get_has_alpha(pb);
564 prs = gdk_pixbuf_get_rowstride(pb);
565 p_pix = gdk_pixbuf_get_pixels(pb);
567 for (i = 0; i < h; i++)
569 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
570 for (j = 0; j < w; j++)
575 if (p_alpha) { *pp = a; pp++; }
580 void pixbuf_set_rect(GdkPixbuf *pb,
581 gint x, gint y, gint w, gint h,
582 gint r, gint g, gint b, gint a,
583 gint left, gint right, gint top, gint bottom)
585 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
587 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
589 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
591 pixbuf_set_rect_fill(pb, x, y, left, h,
595 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
602 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
603 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
605 buf = gdk_pixbuf_get_pixels(pb);
606 has_alpha = gdk_pixbuf_get_has_alpha(pb);
607 rowstride = gdk_pixbuf_get_rowstride(pb);
609 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
613 if (has_alpha) *p = a;
618 *-----------------------------------------------------------------------------
619 * pixbuf text rendering
620 *-----------------------------------------------------------------------------
623 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
624 GdkPixbuf *dest, gint dx, gint dy,
626 guint8 r, guint8 g, guint8 b, guint8 a)
641 if (!src || !dest) return;
643 sw = gdk_pixbuf_get_width(src);
644 sh = gdk_pixbuf_get_height(src);
646 if (sx < 0 || sx + w > sw) return;
647 if (sy < 0 || sy + h > sh) return;
649 dw = gdk_pixbuf_get_width(dest);
650 dh = gdk_pixbuf_get_height(dest);
652 if (dx < 0 || dx + w > dw) return;
653 if (dy < 0 || dy + h > dh) return;
655 s_alpha = gdk_pixbuf_get_has_alpha(src);
656 d_alpha = gdk_pixbuf_get_has_alpha(dest);
657 srs = gdk_pixbuf_get_rowstride(src);
658 drs = gdk_pixbuf_get_rowstride(dest);
659 s_pix = gdk_pixbuf_get_pixels(src);
660 d_pix = gdk_pixbuf_get_pixels(dest);
662 s_step = (s_alpha) ? 4 : 3;
663 d_step = (d_alpha) ? 4 : 3;
665 for (i = 0; i < h; i++)
667 sp = s_pix + (sy + i) * srs + sx * s_step;
668 dp = d_pix + (dy + i) * drs + dx * d_step;
669 for (j = 0; j < w; j++)
675 asub = a * sp[0] / 255;
676 *dp = (r * asub + *dp * (256-asub)) >> 8;
678 asub = a * sp[1] / 255;
679 *dp = (g * asub + *dp * (256-asub)) >> 8;
681 asub = a * sp[2] / 255;
682 *dp = (b * asub + *dp * (256-asub)) >> 8;
687 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
701 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
703 guint8 r, guint8 g, guint8 b, guint8 a)
712 if (!widget || !widget->window) return;
714 pango_layout_get_pixel_size(layout, &w, &h);
715 if (w < 1 || h < 1) return;
717 pixmap = gdk_pixmap_new(widget->window, w, h, -1);
719 gc = gdk_gc_new(widget->window);
720 gdk_gc_copy(gc, widget->style->black_gc);
721 gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, w, h);
722 gdk_gc_copy(gc, widget->style->white_gc);
723 gdk_draw_layout(pixmap, gc, 0, 0, layout);
726 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, w, h);
727 gdk_pixbuf_get_from_drawable(buffer, pixmap,
728 gdk_drawable_get_colormap(widget->window),
730 g_object_unref(pixmap);
734 dw = gdk_pixbuf_get_width(pixbuf);
735 dh = gdk_pixbuf_get_height(pixbuf);
751 if (x + w > dw) w = dw - x;
752 if (y + h > dh) h = dh - y;
754 pixbuf_copy_font(buffer, sx, sy,
758 g_object_unref(buffer);
762 *-----------------------------------------------------------------------------
763 * pixbuf drawing (triangle)
764 *-----------------------------------------------------------------------------
767 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
768 gint *rx, gint *ry, gint *rw, gint *rh)
776 tw = MAX(abs(x1 - x2), abs(x2 - x3));
777 tw = MAX(tw, abs(x3 - x1));
778 th = MAX(abs(y1 - y2), abs(y2 - y3));
779 th = MAX(th, abs(y3 - y1));
787 void pixbuf_draw_triangle(GdkPixbuf *pb,
788 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
789 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
790 guint8 r, guint8 g, guint8 b, guint8 a)
802 gdouble slope1, slope2;
803 gint slope1_x, slope1_y;
810 pw = gdk_pixbuf_get_width(pb);
811 ph = gdk_pixbuf_get_height(pb);
813 if (!util_clip_region(0, 0, pw, ph,
814 clip_x, clip_y, clip_w, clip_h,
815 &rx, &ry, &rw, &rh)) return;
817 util_clip_triangle(x1, y1, x2, y2, x3, y3,
820 if (!util_clip_region(rx, ry, rw, rh,
822 &fx1, &fy1, &fw, &fh)) return;
826 p_alpha = gdk_pixbuf_get_has_alpha(pb);
827 prs = gdk_pixbuf_get_rowstride(pb);
828 p_pix = gdk_pixbuf_get_pixels(pb);
830 p_step = (p_alpha) ? 4 : 3;
834 t = x1; x1 = x2; x2 = t;
835 t = y1; y1 = y2; y2 = t;
839 t = x2; x2 = x3; x3 = t;
840 t = y2; y2 = y3; y3 = t;
844 t = x1; x1 = x2; x2 = t;
845 t = y1; y1 = y2; y2 = t;
848 slope1 = (gdouble)(y2 - y1);
849 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
852 slope2 = (gdouble)(y3 - y1);
853 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
855 for (y = fy1; y < fy2; y++)
859 if (!middle && y > y2)
861 slope1 = (gdouble)(y3 - y2);
862 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
869 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
870 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
874 t = xa; xa = xb; xb = t;
877 xa = CLAMP(xa, fx1, fx2);
878 xb = CLAMP(xb, fx1, fx2);
880 pp = p_pix + y * prs + xa * p_step;
884 *pp = (r * a + *pp * (256-a)) >> 8;
886 *pp = (g * a + *pp * (256-a)) >> 8;
888 *pp = (b * a + *pp * (256-a)) >> 8;
898 *-----------------------------------------------------------------------------
899 * pixbuf drawing (line)
900 *-----------------------------------------------------------------------------
903 static gint util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
904 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
905 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
914 t = x1; x1 = x2; x2 = t;
915 t = y1; y1 = y2; y2 = t;
919 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
923 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
927 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
931 if (x1 >= clip_x && x2 <= clip_x + clip_w)
935 if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
939 if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
949 slope = (y2 - y1) / d;
952 y1 = y1 + slope * (clip_x - x1);
955 if (x2 > clip_x + clip_w)
957 y2 = y2 + slope * (clip_x + clip_w - x2);
958 x2 = clip_x + clip_w;
964 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
970 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
972 t = x1; x1 = x2; x2 = t;
973 t = y1; y1 = y2; y2 = t;
982 slope = (x2 - x1) / d;
985 x1 = x1 + slope * (clip_y - y1);
988 if (y2 > clip_y + clip_h)
990 x2 = x2 + slope * (clip_y + clip_h - y2);
991 y2 = clip_y + clip_h;
1013 void pixbuf_draw_line(GdkPixbuf *pb,
1014 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1015 gint x1, gint y1, gint x2, gint y2,
1016 guint8 r, guint8 g, guint8 b, guint8 a)
1020 gint rx, ry, rw, rh;
1021 gdouble rx1, ry1, rx2, ry2;
1028 gint cx1, cy1, cx2, cy2;
1032 pw = gdk_pixbuf_get_width(pb);
1033 ph = gdk_pixbuf_get_height(pb);
1035 if (!util_clip_region(0, 0, pw, ph,
1036 clip_x, clip_y, clip_w, clip_h,
1037 &rx, &ry, &rw, &rh)) return;
1038 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1039 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1040 &rx1, &ry1, &rx2, &ry2)) return;
1047 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1048 prs = gdk_pixbuf_get_rowstride(pb);
1049 p_pix = gdk_pixbuf_get_pixels(pb);
1051 p_step = (p_alpha) ? 4 : 3;
1053 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1058 t = rx1; rx1 = rx2; rx2 = t;
1059 t = ry1; ry1 = ry2; ry2 = t;
1063 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1064 for (x = rx1; x < rx2; x += 1.0)
1066 px = (gint)(x + 0.5);
1067 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1069 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1071 pp = p_pix + py * prs + px * p_step;
1072 *pp = (r * a + *pp * (256-a)) >> 8;
1074 *pp = (g * a + *pp * (256-a)) >> 8;
1076 *pp = (b * a + *pp * (256-a)) >> 8;
1085 t = rx1; rx1 = rx2; rx2 = t;
1086 t = ry1; ry1 = ry2; ry2 = t;
1090 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1091 for (y = ry1; y < ry2; y += 1.0)
1093 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1094 py = (gint)(y + 0.5);
1096 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1098 pp = p_pix + py * prs + px * p_step;
1099 *pp = (r * a + *pp * (256-a)) >> 8;
1101 *pp = (g * a + *pp * (256-a)) >> 8;
1103 *pp = (b * a + *pp * (256-a)) >> 8;
1110 *-----------------------------------------------------------------------------
1111 * pixbuf drawing (fades and shadows)
1112 *-----------------------------------------------------------------------------
1115 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gint p_alpha,
1116 gint s, gint vertical, gint border,
1117 gint x1, gint y1, gint x2, gint y2,
1118 guint8 r, guint8 g, guint8 b, guint8 a)
1125 p_step = (p_alpha) ? 4 : 3;
1126 for (j = y1; j < y2; j++)
1128 pp = p_pix + j * prs + x1 * p_step;
1129 if (!vertical) n = a - a * abs(j - s) / border;
1130 for (i = x1; i < x2; i++)
1132 if (vertical) n = a - a * abs(i - s) / border;
1133 *pp = (r * n + *pp * (256-n)) >> 8;
1135 *pp = (g * n + *pp * (256-n)) >> 8;
1137 *pp = (b * n + *pp * (256-n)) >> 8;
1144 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gint p_alpha,
1145 gint sx, gint sy, gint border,
1146 gint x1, gint y1, gint x2, gint y2,
1147 guint8 r, guint8 g, guint8 b, guint8 a)
1153 p_step = (p_alpha) ? 4 : 3;
1154 for (j = y1; j < y2; j++)
1156 pp = p_pix + j * prs + x1 * p_step;
1157 for (i = x1; i < x2; i++)
1162 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1163 n = a - a * r / border;
1164 *pp = (r * n + *pp * (256-n)) >> 8;
1166 *pp = (g * n + *pp * (256-n)) >> 8;
1168 *pp = (b * n + *pp * (256-n)) >> 8;
1175 void pixbuf_draw_shadow(GdkPixbuf *pb,
1176 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1177 gint x, gint y, gint w, gint h, gint border,
1178 guint8 r, guint8 g, guint8 b, guint8 a)
1182 gint rx, ry, rw, rh;
1183 gint fx, fy, fw, fh;
1188 pw = gdk_pixbuf_get_width(pb);
1189 ph = gdk_pixbuf_get_height(pb);
1191 if (!util_clip_region(0, 0, pw, ph,
1192 clip_x, clip_y, clip_w, clip_h,
1193 &rx, &ry, &rw, &rh)) return;
1195 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1196 prs = gdk_pixbuf_get_rowstride(pb);
1197 p_pix = gdk_pixbuf_get_pixels(pb);
1199 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1201 &fx, &fy, &fw, &fh))
1203 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1206 if (border < 1) return;
1208 if (util_clip_region(x, y + border, border, h - border * 2,
1210 &fx, &fy, &fw, &fh))
1212 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1213 x + border, TRUE, border,
1214 fx, fy, fx + fw, fy + fh,
1217 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1219 &fx, &fy, &fw, &fh))
1221 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1222 x + w - border, TRUE, border,
1223 fx, fy, fx + fw, fy + fh,
1226 if (util_clip_region(x + border, y, w - border * 2, border,
1228 &fx, &fy, &fw, &fh))
1230 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1231 y + border, FALSE, border,
1232 fx, fy, fx + fw, fy + fh,
1235 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1237 &fx, &fy, &fw, &fh))
1239 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1240 y + h - border, FALSE, border,
1241 fx, fy, fx + fw, fy + fh,
1244 if (util_clip_region(x, y, border, border,
1246 &fx, &fy, &fw, &fh))
1248 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1249 x + border, y + border, border,
1250 fx, fy, fx + fw, fy + fh,
1253 if (util_clip_region(x + w - border, y, border, border,
1255 &fx, &fy, &fw, &fh))
1257 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1258 x + w - border, y + border, border,
1259 fx, fy, fx + fw, fy + fh,
1262 if (util_clip_region(x, y + h - border, border, border,
1264 &fx, &fy, &fw, &fh))
1266 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1267 x + border, y + h - border, border,
1268 fx, fy, fx + fw, fy + fh,
1271 if (util_clip_region(x + w - border, y + h - border, border, border,
1273 &fx, &fy, &fw, &fh))
1275 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1276 x + w - border, y + h - border, border,
1277 fx, fy, fx + fw, fy + fh,
1284 *-----------------------------------------------------------------------------
1285 * pixbuf color alterations
1286 *-----------------------------------------------------------------------------
1289 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1290 gint x, gint y, gint w, gint h)
1300 pw = gdk_pixbuf_get_width(pb);
1301 ph = gdk_pixbuf_get_height(pb);
1303 if (x < 0 || x + w > pw) return;
1304 if (y < 0 || y + h > ph) return;
1306 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1307 prs = gdk_pixbuf_get_rowstride(pb);
1308 p_pix = gdk_pixbuf_get_pixels(pb);
1310 for (i = 0; i < h; i++)
1312 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
1313 for (j = 0; j < w; j++)
1317 grey = (pp[0] + pp[1] + pp[2]) / 3;
1328 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */