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 pixbuf_inline_register_icon(const gchar *key)
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_inline(key));
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 pixbuf_inline_register_icon(inline_pixbuf_data[i].key);
162 gint pixbuf_scale_aspect(gint req_w, gint req_h, gint old_w, gint old_h,
163 gint *new_w, gint *new_h)
165 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
168 *new_h = (gdouble)*new_w / old_w * old_h;
169 if (*new_h < 1) *new_h = 1;
174 *new_w = (gdouble)*new_h / old_h * old_w;
175 if (*new_w < 1) *new_w = 1;
178 return (*new_w != old_w || *new_h != old_h);
181 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
183 GdkPixbuf *pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); /* FIXME use different images according to FORMAT_CLASS */
185 if (requested_width && requested_height)
187 gint w = gdk_pixbuf_get_width(pixbuf);
188 gint h = gdk_pixbuf_get_height(pixbuf);
190 if (w > requested_width || h > requested_height)
194 if (pixbuf_scale_aspect(requested_width, requested_height,
200 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
201 g_object_unref(G_OBJECT(tmp));
210 *-----------------------------------------------------------------------------
212 *-----------------------------------------------------------------------------
215 gint util_clip_region(gint x, gint y, gint w, gint h,
216 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
217 gint *rx, gint *ry, gint *rw, gint *rh)
219 if (clip_x + clip_w <= x ||
221 clip_y + clip_h <= y ||
227 *rx = MAX(x, clip_x);
228 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
230 *ry = MAX(y, clip_y);
231 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
237 *-----------------------------------------------------------------------------
239 *-----------------------------------------------------------------------------
242 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
243 guchar *dest, gint dest_row_stride, gint w, gint h,
244 gint bytes_per_pixel, gint counter_clockwise)
250 for (i = 0; i < h; i++)
252 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
253 for (j = 0; j < w; j++)
255 if (counter_clockwise)
257 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
261 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
263 *(dp++) = *(sp++); /* r */
264 *(dp++) = *(sp++); /* g */
265 *(dp++) = *(sp++); /* b */
266 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
272 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
273 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
279 for (i = 0; i < h; i++)
281 sp = src + (i * src_row_stride);
282 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
283 memcpy(dp, sp, w * bytes_per_pixel);
287 #define ROTATE_BUFFER_WIDTH 48
288 #define ROTATE_BUFFER_HEIGHT 48
291 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
294 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gint counter_clockwise)
313 if (!src) return NULL;
315 sw = gdk_pixbuf_get_width(src);
316 sh = gdk_pixbuf_get_height(src);
317 has_alpha = gdk_pixbuf_get_has_alpha(src);
318 srs = gdk_pixbuf_get_rowstride(src);
319 s_pix = gdk_pixbuf_get_pixels(src);
323 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
324 drs = gdk_pixbuf_get_rowstride(dest);
325 d_pix = gdk_pixbuf_get_pixels(dest);
327 a = (has_alpha ? 4 : 3);
329 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
330 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
331 b_pix = gdk_pixbuf_get_pixels(buffer);
332 brs = gdk_pixbuf_get_rowstride(buffer);
334 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
336 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
337 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
341 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
342 pixbuf_copy_block_rotate(s_pix, srs, j, i,
344 a, counter_clockwise);
346 if (counter_clockwise)
356 pixbuf_copy_block(b_pix, brs, w, h,
357 d_pix, drs, x, y, a);
361 g_object_unref(buffer);
364 /* this is the simple version of rotation (roughly 2-4x slower) */
366 for (i = 0; i < sh; i++)
368 sp = s_pix + (i * srs);
369 for (j = 0; j < sw; j++)
371 if (counter_clockwise)
373 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
377 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
380 *(dp++) = *(sp++); /* r */
381 *(dp++) = *(sp++); /* g */
382 *(dp++) = *(sp++); /* b */
383 if (has_alpha) *(dp) = *(sp++); /* a */
392 * Returns a copy of pixbuf mirrored and or flipped.
393 * TO do a 180 degree rotations set both mirror and flipped TRUE
394 * if mirror and flip are FALSE, result is a simple copy.
396 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gint mirror, gint flip)
409 if (!src) return NULL;
411 w = gdk_pixbuf_get_width(src);
412 h = gdk_pixbuf_get_height(src);
413 has_alpha = gdk_pixbuf_get_has_alpha(src);
414 srs = gdk_pixbuf_get_rowstride(src);
415 s_pix = gdk_pixbuf_get_pixels(src);
417 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
418 drs = gdk_pixbuf_get_rowstride(dest);
419 d_pix = gdk_pixbuf_get_pixels(dest);
421 a = has_alpha ? 4 : 3;
423 for (i = 0; i < h; i++)
425 sp = s_pix + (i * srs);
428 dp = d_pix + ((h - i - 1) * drs);
432 dp = d_pix + (i * drs);
437 for (j = 0; j < w; j++)
439 *(dp++) = *(sp++); /* r */
440 *(dp++) = *(sp++); /* g */
441 *(dp++) = *(sp++); /* b */
442 if (has_alpha) *(dp) = *(sp++); /* a */
448 for (j = 0; j < w; j++)
450 *(dp++) = *(sp++); /* r */
451 *(dp++) = *(sp++); /* g */
452 *(dp++) = *(sp++); /* b */
453 if (has_alpha) *(dp++) = *(sp++); /* a */
461 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
464 GdkPixbuf *tmp = NULL;
468 case EXIF_ORIENTATION_TOP_LEFT:
469 dest = gdk_pixbuf_copy(pixbuf);
471 case EXIF_ORIENTATION_TOP_RIGHT:
473 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
475 case EXIF_ORIENTATION_BOTTOM_RIGHT:
477 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
479 case EXIF_ORIENTATION_BOTTOM_LEFT:
481 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
483 case EXIF_ORIENTATION_LEFT_TOP:
484 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
485 dest = pixbuf_copy_rotate_90(tmp, FALSE);
487 case EXIF_ORIENTATION_RIGHT_TOP:
488 /* rotated -90 (270) */
489 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
491 case EXIF_ORIENTATION_RIGHT_BOTTOM:
492 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
493 dest = pixbuf_copy_rotate_90(tmp, TRUE);
495 case EXIF_ORIENTATION_LEFT_BOTTOM:
497 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
500 dest = gdk_pixbuf_copy(pixbuf);
503 if (tmp) g_object_unref(tmp);
510 *-----------------------------------------------------------------------------
511 * pixbuf drawing (rectangles)
512 *-----------------------------------------------------------------------------
516 * Fills region of pixbuf at x,y over w,h
517 * with colors red (r), green (g), blue (b)
518 * applying alpha (a), use a=255 for solid.
520 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
521 gint x, gint y, gint w, gint h,
522 gint r, gint g, gint b, gint a)
532 pw = gdk_pixbuf_get_width(pb);
533 ph = gdk_pixbuf_get_height(pb);
535 if (x < 0 || x + w > pw) return;
536 if (y < 0 || y + h > ph) return;
538 p_alpha = gdk_pixbuf_get_has_alpha(pb);
539 prs = gdk_pixbuf_get_rowstride(pb);
540 p_pix = gdk_pixbuf_get_pixels(pb);
542 for (i = 0; i < h; i++)
544 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
545 for (j = 0; j < w; j++)
547 *pp = (r * a + *pp * (256-a)) >> 8;
549 *pp = (g * a + *pp * (256-a)) >> 8;
551 *pp = (b * a + *pp * (256-a)) >> 8;
558 void pixbuf_draw_rect(GdkPixbuf *pb,
559 gint x, gint y, gint w, gint h,
560 gint r, gint g, gint b, gint a,
561 gint left, gint right, gint top, gint bottom)
563 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
565 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
567 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
569 pixbuf_draw_rect_fill(pb, x, y, left, h,
573 void pixbuf_set_rect_fill(GdkPixbuf *pb,
574 gint x, gint y, gint w, gint h,
575 gint r, gint g, gint b, gint a)
585 pw = gdk_pixbuf_get_width(pb);
586 ph = gdk_pixbuf_get_height(pb);
588 if (x < 0 || x + w > pw) return;
589 if (y < 0 || y + h > ph) return;
591 p_alpha = gdk_pixbuf_get_has_alpha(pb);
592 prs = gdk_pixbuf_get_rowstride(pb);
593 p_pix = gdk_pixbuf_get_pixels(pb);
595 for (i = 0; i < h; i++)
597 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
598 for (j = 0; j < w; j++)
603 if (p_alpha) { *pp = a; pp++; }
608 void pixbuf_set_rect(GdkPixbuf *pb,
609 gint x, gint y, gint w, gint h,
610 gint r, gint g, gint b, gint a,
611 gint left, gint right, gint top, gint bottom)
613 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
615 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
617 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
619 pixbuf_set_rect_fill(pb, x, y, left, h,
623 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
630 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
631 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
633 buf = gdk_pixbuf_get_pixels(pb);
634 has_alpha = gdk_pixbuf_get_has_alpha(pb);
635 rowstride = gdk_pixbuf_get_rowstride(pb);
637 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
641 if (has_alpha) *p = a;
646 *-----------------------------------------------------------------------------
647 * pixbuf text rendering
648 *-----------------------------------------------------------------------------
651 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
652 GdkPixbuf *dest, gint dx, gint dy,
654 guint8 r, guint8 g, guint8 b, guint8 a)
669 if (!src || !dest) return;
671 sw = gdk_pixbuf_get_width(src);
672 sh = gdk_pixbuf_get_height(src);
674 if (sx < 0 || sx + w > sw) return;
675 if (sy < 0 || sy + h > sh) return;
677 dw = gdk_pixbuf_get_width(dest);
678 dh = gdk_pixbuf_get_height(dest);
680 if (dx < 0 || dx + w > dw) return;
681 if (dy < 0 || dy + h > dh) return;
683 s_alpha = gdk_pixbuf_get_has_alpha(src);
684 d_alpha = gdk_pixbuf_get_has_alpha(dest);
685 srs = gdk_pixbuf_get_rowstride(src);
686 drs = gdk_pixbuf_get_rowstride(dest);
687 s_pix = gdk_pixbuf_get_pixels(src);
688 d_pix = gdk_pixbuf_get_pixels(dest);
690 s_step = (s_alpha) ? 4 : 3;
691 d_step = (d_alpha) ? 4 : 3;
693 for (i = 0; i < h; i++)
695 sp = s_pix + (sy + i) * srs + sx * s_step;
696 dp = d_pix + (dy + i) * drs + dx * d_step;
697 for (j = 0; j < w; j++)
703 asub = a * sp[0] / 255;
704 *dp = (r * asub + *dp * (256-asub)) >> 8;
706 asub = a * sp[1] / 255;
707 *dp = (g * asub + *dp * (256-asub)) >> 8;
709 asub = a * sp[2] / 255;
710 *dp = (b * asub + *dp * (256-asub)) >> 8;
715 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
729 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
731 guint8 r, guint8 g, guint8 b, guint8 a)
740 if (!widget || !widget->window) return;
742 pango_layout_get_pixel_size(layout, &w, &h);
743 if (w < 1 || h < 1) return;
745 pixmap = gdk_pixmap_new(widget->window, w, h, -1);
747 gc = gdk_gc_new(widget->window);
748 gdk_gc_copy(gc, widget->style->black_gc);
749 gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, w, h);
750 gdk_gc_copy(gc, widget->style->white_gc);
751 gdk_draw_layout(pixmap, gc, 0, 0, layout);
754 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, w, h);
755 gdk_pixbuf_get_from_drawable(buffer, pixmap,
756 gdk_drawable_get_colormap(widget->window),
758 g_object_unref(pixmap);
762 dw = gdk_pixbuf_get_width(pixbuf);
763 dh = gdk_pixbuf_get_height(pixbuf);
779 if (x + w > dw) w = dw - x;
780 if (y + h > dh) h = dh - y;
782 pixbuf_copy_font(buffer, sx, sy,
786 g_object_unref(buffer);
790 *-----------------------------------------------------------------------------
791 * pixbuf drawing (triangle)
792 *-----------------------------------------------------------------------------
795 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
796 gint *rx, gint *ry, gint *rw, gint *rh)
804 tw = MAX(abs(x1 - x2), abs(x2 - x3));
805 tw = MAX(tw, abs(x3 - x1));
806 th = MAX(abs(y1 - y2), abs(y2 - y3));
807 th = MAX(th, abs(y3 - y1));
815 void pixbuf_draw_triangle(GdkPixbuf *pb,
816 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
817 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
818 guint8 r, guint8 g, guint8 b, guint8 a)
830 gdouble slope1, slope2;
831 gint slope1_x, slope1_y;
838 pw = gdk_pixbuf_get_width(pb);
839 ph = gdk_pixbuf_get_height(pb);
841 if (!util_clip_region(0, 0, pw, ph,
842 clip_x, clip_y, clip_w, clip_h,
843 &rx, &ry, &rw, &rh)) return;
845 util_clip_triangle(x1, y1, x2, y2, x3, y3,
848 if (!util_clip_region(rx, ry, rw, rh,
850 &fx1, &fy1, &fw, &fh)) return;
854 p_alpha = gdk_pixbuf_get_has_alpha(pb);
855 prs = gdk_pixbuf_get_rowstride(pb);
856 p_pix = gdk_pixbuf_get_pixels(pb);
858 p_step = (p_alpha) ? 4 : 3;
862 t = x1; x1 = x2; x2 = t;
863 t = y1; y1 = y2; y2 = t;
867 t = x2; x2 = x3; x3 = t;
868 t = y2; y2 = y3; y3 = t;
872 t = x1; x1 = x2; x2 = t;
873 t = y1; y1 = y2; y2 = t;
876 slope1 = (gdouble)(y2 - y1);
877 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
880 slope2 = (gdouble)(y3 - y1);
881 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
883 for (y = fy1; y < fy2; y++)
887 if (!middle && y > y2)
889 slope1 = (gdouble)(y3 - y2);
890 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
897 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
898 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
902 t = xa; xa = xb; xb = t;
905 xa = CLAMP(xa, fx1, fx2);
906 xb = CLAMP(xb, fx1, fx2);
908 pp = p_pix + y * prs + xa * p_step;
912 *pp = (r * a + *pp * (256-a)) >> 8;
914 *pp = (g * a + *pp * (256-a)) >> 8;
916 *pp = (b * a + *pp * (256-a)) >> 8;
926 *-----------------------------------------------------------------------------
927 * pixbuf drawing (line)
928 *-----------------------------------------------------------------------------
931 static gint util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
932 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
933 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
942 t = x1; x1 = x2; x2 = t;
943 t = y1; y1 = y2; y2 = t;
947 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
951 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
955 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
959 if (x1 >= clip_x && x2 <= clip_x + clip_w)
963 if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
967 if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
977 slope = (y2 - y1) / d;
980 y1 = y1 + slope * (clip_x - x1);
983 if (x2 > clip_x + clip_w)
985 y2 = y2 + slope * (clip_x + clip_w - x2);
986 x2 = clip_x + clip_w;
992 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
998 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1000 t = x1; x1 = x2; x2 = t;
1001 t = y1; y1 = y2; y2 = t;
1010 slope = (x2 - x1) / d;
1013 x1 = x1 + slope * (clip_y - y1);
1016 if (y2 > clip_y + clip_h)
1018 x2 = x2 + slope * (clip_y + clip_h - y2);
1019 y2 = clip_y + clip_h;
1041 void pixbuf_draw_line(GdkPixbuf *pb,
1042 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1043 gint x1, gint y1, gint x2, gint y2,
1044 guint8 r, guint8 g, guint8 b, guint8 a)
1048 gint rx, ry, rw, rh;
1049 gdouble rx1, ry1, rx2, ry2;
1056 gint cx1, cy1, cx2, cy2;
1060 pw = gdk_pixbuf_get_width(pb);
1061 ph = gdk_pixbuf_get_height(pb);
1063 if (!util_clip_region(0, 0, pw, ph,
1064 clip_x, clip_y, clip_w, clip_h,
1065 &rx, &ry, &rw, &rh)) return;
1066 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1067 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1068 &rx1, &ry1, &rx2, &ry2)) return;
1075 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1076 prs = gdk_pixbuf_get_rowstride(pb);
1077 p_pix = gdk_pixbuf_get_pixels(pb);
1079 p_step = (p_alpha) ? 4 : 3;
1081 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1086 t = rx1; rx1 = rx2; rx2 = t;
1087 t = ry1; ry1 = ry2; ry2 = t;
1091 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1092 for (x = rx1; x < rx2; x += 1.0)
1094 px = (gint)(x + 0.5);
1095 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1097 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1099 pp = p_pix + py * prs + px * p_step;
1100 *pp = (r * a + *pp * (256-a)) >> 8;
1102 *pp = (g * a + *pp * (256-a)) >> 8;
1104 *pp = (b * a + *pp * (256-a)) >> 8;
1113 t = rx1; rx1 = rx2; rx2 = t;
1114 t = ry1; ry1 = ry2; ry2 = t;
1118 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1119 for (y = ry1; y < ry2; y += 1.0)
1121 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1122 py = (gint)(y + 0.5);
1124 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1126 pp = p_pix + py * prs + px * p_step;
1127 *pp = (r * a + *pp * (256-a)) >> 8;
1129 *pp = (g * a + *pp * (256-a)) >> 8;
1131 *pp = (b * a + *pp * (256-a)) >> 8;
1138 *-----------------------------------------------------------------------------
1139 * pixbuf drawing (fades and shadows)
1140 *-----------------------------------------------------------------------------
1143 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gint p_alpha,
1144 gint s, gint vertical, gint border,
1145 gint x1, gint y1, gint x2, gint y2,
1146 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 if (!vertical) n = a - a * abs(j - s) / border;
1158 for (i = x1; i < x2; i++)
1160 if (vertical) n = a - a * abs(i - s) / border;
1161 *pp = (r * n + *pp * (256-n)) >> 8;
1163 *pp = (g * n + *pp * (256-n)) >> 8;
1165 *pp = (b * n + *pp * (256-n)) >> 8;
1172 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gint p_alpha,
1173 gint sx, gint sy, gint border,
1174 gint x1, gint y1, gint x2, gint y2,
1175 guint8 r, guint8 g, guint8 b, guint8 a)
1181 p_step = (p_alpha) ? 4 : 3;
1182 for (j = y1; j < y2; j++)
1184 pp = p_pix + j * prs + x1 * p_step;
1185 for (i = x1; i < x2; i++)
1190 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1191 n = a - a * r / border;
1192 *pp = (r * n + *pp * (256-n)) >> 8;
1194 *pp = (g * n + *pp * (256-n)) >> 8;
1196 *pp = (b * n + *pp * (256-n)) >> 8;
1203 void pixbuf_draw_shadow(GdkPixbuf *pb,
1204 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1205 gint x, gint y, gint w, gint h, gint border,
1206 guint8 r, guint8 g, guint8 b, guint8 a)
1210 gint rx, ry, rw, rh;
1211 gint fx, fy, fw, fh;
1216 pw = gdk_pixbuf_get_width(pb);
1217 ph = gdk_pixbuf_get_height(pb);
1219 if (!util_clip_region(0, 0, pw, ph,
1220 clip_x, clip_y, clip_w, clip_h,
1221 &rx, &ry, &rw, &rh)) return;
1223 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1224 prs = gdk_pixbuf_get_rowstride(pb);
1225 p_pix = gdk_pixbuf_get_pixels(pb);
1227 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1229 &fx, &fy, &fw, &fh))
1231 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1234 if (border < 1) return;
1236 if (util_clip_region(x, y + border, border, h - border * 2,
1238 &fx, &fy, &fw, &fh))
1240 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1241 x + border, TRUE, border,
1242 fx, fy, fx + fw, fy + fh,
1245 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1247 &fx, &fy, &fw, &fh))
1249 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1250 x + w - border, TRUE, border,
1251 fx, fy, fx + fw, fy + fh,
1254 if (util_clip_region(x + border, y, w - border * 2, border,
1256 &fx, &fy, &fw, &fh))
1258 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1259 y + border, FALSE, border,
1260 fx, fy, fx + fw, fy + fh,
1263 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1265 &fx, &fy, &fw, &fh))
1267 pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
1268 y + h - border, FALSE, border,
1269 fx, fy, fx + fw, fy + fh,
1272 if (util_clip_region(x, y, border, border,
1274 &fx, &fy, &fw, &fh))
1276 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1277 x + border, y + border, border,
1278 fx, fy, fx + fw, fy + fh,
1281 if (util_clip_region(x + w - border, y, border, border,
1283 &fx, &fy, &fw, &fh))
1285 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1286 x + w - border, y + border, border,
1287 fx, fy, fx + fw, fy + fh,
1290 if (util_clip_region(x, y + h - border, border, border,
1292 &fx, &fy, &fw, &fh))
1294 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1295 x + border, y + h - border, border,
1296 fx, fy, fx + fw, fy + fh,
1299 if (util_clip_region(x + w - border, y + h - border, border, border,
1301 &fx, &fy, &fw, &fh))
1303 pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
1304 x + w - border, y + h - border, border,
1305 fx, fy, fx + fw, fy + fh,
1312 *-----------------------------------------------------------------------------
1313 * pixbuf color alterations
1314 *-----------------------------------------------------------------------------
1317 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1318 gint x, gint y, gint w, gint h)
1328 pw = gdk_pixbuf_get_width(pb);
1329 ph = gdk_pixbuf_get_height(pb);
1331 if (x < 0 || x + w > pw) return;
1332 if (y < 0 || y + h > ph) return;
1334 p_alpha = gdk_pixbuf_get_has_alpha(pb);
1335 prs = gdk_pixbuf_get_rowstride(pb);
1336 p_pix = gdk_pixbuf_get_pixels(pb);
1338 for (i = 0; i < h; i++)
1340 pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
1341 for (j = 0; j < w; j++)
1345 grey = (pp[0] + pp[1] + pp[2]) / 3;
1356 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */