2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "pixbuf_util.h"
25 #include "ui_fileops.h"
27 #include "icons/icons_inline.h"
33 *-----------------------------------------------------------------------------
35 *-----------------------------------------------------------------------------
38 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
43 if (!pixbuf || !filename) return FALSE;
45 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
46 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
50 log_printf("Error saving png file: %s\n", error->message);
58 *-----------------------------------------------------------------------------
60 *-----------------------------------------------------------------------------
63 gboolean pixbuf_to_file_as_jpg(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
69 if (!pixbuf || !filename) return FALSE;
71 if (quality == -1) quality = 75;
72 if (quality < 1 || quality > 100)
74 log_printf("Jpeg not saved, invalid quality %d\n", quality);
78 qbuf = g_strdup_printf("%d", quality);
79 ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
84 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
92 *-----------------------------------------------------------------------------
94 *-----------------------------------------------------------------------------
97 typedef struct _PixbufInline PixbufInline;
104 static PixbufInline inline_pixbuf_data[] = {
105 { PIXBUF_INLINE_FOLDER_CLOSED, folder_closed },
106 { PIXBUF_INLINE_FOLDER_LOCKED, folder_locked },
107 { PIXBUF_INLINE_FOLDER_OPEN, folder_open },
108 { PIXBUF_INLINE_FOLDER_UP, folder_up },
109 { PIXBUF_INLINE_SCROLLER, icon_scroller },
110 { PIXBUF_INLINE_BROKEN, icon_broken },
111 { PIXBUF_INLINE_ICON, gqview_icon },
112 { PIXBUF_INLINE_LOGO, geeqie_logo },
113 { PIXBUF_INLINE_ICON_FLOAT, icon_float },
114 { PIXBUF_INLINE_ICON_THUMB, icon_thumb },
115 { PIXBUF_INLINE_ICON_BOOK, icon_book },
116 { PIXBUF_INLINE_ICON_CONFIG, icon_config },
117 { PIXBUF_INLINE_ICON_TOOLS, icon_tools },
118 { PIXBUF_INLINE_ICON_VIEW, icon_view },
122 GdkPixbuf *pixbuf_inline(const gchar *key)
126 if (!key) return NULL;
129 while (inline_pixbuf_data[i].key)
131 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
133 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
138 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
143 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
145 static GtkIconFactory *icon_factory = NULL;
146 GtkIconSet *icon_set;
150 icon_factory = gtk_icon_factory_new();
151 gtk_icon_factory_add_default(icon_factory);
154 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
155 gtk_icon_factory_add(icon_factory, key, icon_set);
159 void pixbuf_inline_register_stock_icons(void)
164 while (inline_pixbuf_data[i].key)
166 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
171 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
173 GtkIconTheme *icon_theme;
175 GError *error = NULL;
177 icon_theme = gtk_icon_theme_get_default();
179 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
181 pixbuf = gtk_icon_theme_load_icon(icon_theme,
182 icon, /* icon name */
190 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
195 if (strchr(icon, '.'))
197 /* try again without extension */
198 gchar *icon2 = remove_extension_from_path(icon);
199 pixbuf = gtk_icon_theme_load_icon(icon_theme,
200 icon2, /* icon name */
206 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
213 if (!pixbuf) return FALSE;
215 register_stock_icon(key, pixbuf);
219 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
220 gint old_w, gint old_h,
221 gint *new_w, gint *new_h)
223 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
226 *new_h = (gdouble)*new_w / old_w * old_h;
227 if (*new_h < 1) *new_h = 1;
232 *new_w = (gdouble)*new_h / old_h * old_w;
233 if (*new_w < 1) *new_w = 1;
236 return (*new_w != old_w || *new_h != old_h);
239 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
241 GdkPixbuf *pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); /* FIXME use different images according to FORMAT_CLASS */
243 if (requested_width && requested_height)
245 gint w = gdk_pixbuf_get_width(pixbuf);
246 gint h = gdk_pixbuf_get_height(pixbuf);
248 if (w > requested_width || h > requested_height)
252 if (pixbuf_scale_aspect(requested_width, requested_height,
258 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
259 g_object_unref(G_OBJECT(tmp));
268 *-----------------------------------------------------------------------------
270 *-----------------------------------------------------------------------------
273 gboolean util_clip_region(gint x, gint y, gint w, gint h,
274 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
275 gint *rx, gint *ry, gint *rw, gint *rh)
277 if (clip_x + clip_w <= x ||
279 clip_y + clip_h <= y ||
285 *rx = MAX(x, clip_x);
286 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
288 *ry = MAX(y, clip_y);
289 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
295 *-----------------------------------------------------------------------------
297 *-----------------------------------------------------------------------------
300 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
301 guchar *dest, gint dest_row_stride, gint w, gint h,
302 gint bytes_per_pixel, gboolean counter_clockwise)
308 for (i = 0; i < h; i++)
310 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
311 for (j = 0; j < w; j++)
313 if (counter_clockwise)
315 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
319 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
321 *(dp++) = *(sp++); /* r */
322 *(dp++) = *(sp++); /* g */
323 *(dp++) = *(sp++); /* b */
324 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
330 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
331 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
337 for (i = 0; i < h; i++)
339 sp = src + (i * src_row_stride);
340 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
341 memcpy(dp, sp, w * bytes_per_pixel);
345 #define ROTATE_BUFFER_WIDTH 48
346 #define ROTATE_BUFFER_HEIGHT 48
349 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
352 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
367 if (!src) return NULL;
369 sw = gdk_pixbuf_get_width(src);
370 sh = gdk_pixbuf_get_height(src);
371 has_alpha = gdk_pixbuf_get_has_alpha(src);
372 srs = gdk_pixbuf_get_rowstride(src);
373 s_pix = gdk_pixbuf_get_pixels(src);
377 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
378 drs = gdk_pixbuf_get_rowstride(dest);
379 d_pix = gdk_pixbuf_get_pixels(dest);
381 a = (has_alpha ? 4 : 3);
383 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
384 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
385 b_pix = gdk_pixbuf_get_pixels(buffer);
386 brs = gdk_pixbuf_get_rowstride(buffer);
388 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
390 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
391 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
395 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
396 pixbuf_copy_block_rotate(s_pix, srs, j, i,
398 a, counter_clockwise);
400 if (counter_clockwise)
410 pixbuf_copy_block(b_pix, brs, w, h,
411 d_pix, drs, x, y, a);
415 g_object_unref(buffer);
418 /* this is the simple version of rotation (roughly 2-4x slower) */
420 for (i = 0; i < sh; i++)
422 sp = s_pix + (i * srs);
423 for (j = 0; j < sw; j++)
425 if (counter_clockwise)
427 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
431 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
434 *(dp++) = *(sp++); /* r */
435 *(dp++) = *(sp++); /* g */
436 *(dp++) = *(sp++); /* b */
437 if (has_alpha) *(dp) = *(sp++); /* a */
446 * Returns a copy of pixbuf mirrored and or flipped.
447 * TO do a 180 degree rotations set both mirror and flipped TRUE
448 * if mirror and flip are FALSE, result is a simple copy.
450 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
463 if (!src) return NULL;
465 w = gdk_pixbuf_get_width(src);
466 h = gdk_pixbuf_get_height(src);
467 has_alpha = gdk_pixbuf_get_has_alpha(src);
468 srs = gdk_pixbuf_get_rowstride(src);
469 s_pix = gdk_pixbuf_get_pixels(src);
471 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
472 drs = gdk_pixbuf_get_rowstride(dest);
473 d_pix = gdk_pixbuf_get_pixels(dest);
475 a = has_alpha ? 4 : 3;
477 for (i = 0; i < h; i++)
479 sp = s_pix + (i * srs);
482 dp = d_pix + ((h - i - 1) * drs);
486 dp = d_pix + (i * drs);
491 for (j = 0; j < w; j++)
493 *(dp++) = *(sp++); /* r */
494 *(dp++) = *(sp++); /* g */
495 *(dp++) = *(sp++); /* b */
496 if (has_alpha) *(dp) = *(sp++); /* a */
502 for (j = 0; j < w; j++)
504 *(dp++) = *(sp++); /* r */
505 *(dp++) = *(sp++); /* g */
506 *(dp++) = *(sp++); /* b */
507 if (has_alpha) *(dp++) = *(sp++); /* a */
515 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
518 GdkPixbuf *tmp = NULL;
522 case EXIF_ORIENTATION_TOP_LEFT:
523 dest = gdk_pixbuf_copy(pixbuf);
525 case EXIF_ORIENTATION_TOP_RIGHT:
527 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
529 case EXIF_ORIENTATION_BOTTOM_RIGHT:
531 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
533 case EXIF_ORIENTATION_BOTTOM_LEFT:
535 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
537 case EXIF_ORIENTATION_LEFT_TOP:
538 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
539 dest = pixbuf_copy_rotate_90(tmp, FALSE);
541 case EXIF_ORIENTATION_RIGHT_TOP:
542 /* rotated -90 (270) */
543 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
545 case EXIF_ORIENTATION_RIGHT_BOTTOM:
546 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
547 dest = pixbuf_copy_rotate_90(tmp, TRUE);
549 case EXIF_ORIENTATION_LEFT_BOTTOM:
551 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
554 dest = gdk_pixbuf_copy(pixbuf);
557 if (tmp) g_object_unref(tmp);
564 *-----------------------------------------------------------------------------
565 * pixbuf drawing (rectangles)
566 *-----------------------------------------------------------------------------
570 * Fills region of pixbuf at x,y over w,h
571 * with colors red (r), green (g), blue (b)
572 * applying alpha (a), use a=255 for solid.
574 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
575 gint x, gint y, gint w, gint h,
576 gint r, gint g, gint b, gint a)
586 pw = gdk_pixbuf_get_width(pb);
587 ph = gdk_pixbuf_get_height(pb);
589 if (x < 0 || x + w > pw) return;
590 if (y < 0 || y + h > ph) return;
592 has_alpha = gdk_pixbuf_get_has_alpha(pb);
593 prs = gdk_pixbuf_get_rowstride(pb);
594 p_pix = gdk_pixbuf_get_pixels(pb);
596 for (i = 0; i < h; i++)
598 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
599 for (j = 0; j < w; j++)
601 *pp = (r * a + *pp * (256-a)) >> 8;
603 *pp = (g * a + *pp * (256-a)) >> 8;
605 *pp = (b * a + *pp * (256-a)) >> 8;
612 void pixbuf_draw_rect(GdkPixbuf *pb,
613 gint x, gint y, gint w, gint h,
614 gint r, gint g, gint b, gint a,
615 gint left, gint right, gint top, gint bottom)
617 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
619 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
621 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
623 pixbuf_draw_rect_fill(pb, x, y, left, h,
627 void pixbuf_set_rect_fill(GdkPixbuf *pb,
628 gint x, gint y, gint w, gint h,
629 gint r, gint g, gint b, gint a)
639 pw = gdk_pixbuf_get_width(pb);
640 ph = gdk_pixbuf_get_height(pb);
642 if (x < 0 || x + w > pw) return;
643 if (y < 0 || y + h > ph) return;
645 has_alpha = gdk_pixbuf_get_has_alpha(pb);
646 prs = gdk_pixbuf_get_rowstride(pb);
647 p_pix = gdk_pixbuf_get_pixels(pb);
649 for (i = 0; i < h; i++)
651 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
652 for (j = 0; j < w; j++)
657 if (has_alpha) { *pp = a; pp++; }
662 void pixbuf_set_rect(GdkPixbuf *pb,
663 gint x, gint y, gint w, gint h,
664 gint r, gint g, gint b, gint a,
665 gint left, gint right, gint top, gint bottom)
667 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
669 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
671 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
673 pixbuf_set_rect_fill(pb, x, y, left, h,
677 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
684 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
685 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
687 buf = gdk_pixbuf_get_pixels(pb);
688 has_alpha = gdk_pixbuf_get_has_alpha(pb);
689 rowstride = gdk_pixbuf_get_rowstride(pb);
691 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
695 if (has_alpha) *p = a;
700 *-----------------------------------------------------------------------------
701 * pixbuf text rendering
702 *-----------------------------------------------------------------------------
705 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
706 GdkPixbuf *dest, gint dx, gint dy,
708 guint8 r, guint8 g, guint8 b, guint8 a)
723 if (!src || !dest) return;
725 sw = gdk_pixbuf_get_width(src);
726 sh = gdk_pixbuf_get_height(src);
728 if (sx < 0 || sx + w > sw) return;
729 if (sy < 0 || sy + h > sh) return;
731 dw = gdk_pixbuf_get_width(dest);
732 dh = gdk_pixbuf_get_height(dest);
734 if (dx < 0 || dx + w > dw) return;
735 if (dy < 0 || dy + h > dh) return;
737 s_alpha = gdk_pixbuf_get_has_alpha(src);
738 d_alpha = gdk_pixbuf_get_has_alpha(dest);
739 srs = gdk_pixbuf_get_rowstride(src);
740 drs = gdk_pixbuf_get_rowstride(dest);
741 s_pix = gdk_pixbuf_get_pixels(src);
742 d_pix = gdk_pixbuf_get_pixels(dest);
744 s_step = (s_alpha) ? 4 : 3;
745 d_step = (d_alpha) ? 4 : 3;
747 for (i = 0; i < h; i++)
749 sp = s_pix + (sy + i) * srs + sx * s_step;
750 dp = d_pix + (dy + i) * drs + dx * d_step;
751 for (j = 0; j < w; j++)
757 asub = a * sp[0] / 255;
758 *dp = (r * asub + *dp * (256-asub)) >> 8;
760 asub = a * sp[1] / 255;
761 *dp = (g * asub + *dp * (256-asub)) >> 8;
763 asub = a * sp[2] / 255;
764 *dp = (b * asub + *dp * (256-asub)) >> 8;
769 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
783 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
785 guint8 r, guint8 g, guint8 b, guint8 a)
791 cairo_surface_t *source;
794 pango_layout_get_pixel_size(layout, &w, &h);
795 if (w < 1 || h < 1) return;
797 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
799 cr = cairo_create (source);
800 cairo_set_source_rgb(cr, 0, 0, 0);
801 cairo_rectangle (cr, 0, 0, w, h);
803 cairo_set_source_rgb(cr, 1, 1, 1);
804 pango_cairo_show_layout (cr, layout);
807 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
809 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
811 cairo_image_surface_get_width (source),
812 cairo_image_surface_get_height (source),
813 cairo_image_surface_get_stride (source),
819 dw = gdk_pixbuf_get_width(pixbuf);
820 dh = gdk_pixbuf_get_height(pixbuf);
836 if (x + w > dw) w = dw - x;
837 if (y + h > dh) h = dh - y;
839 pixbuf_copy_font(buffer, sx, sy,
843 g_object_unref(buffer);
844 cairo_surface_destroy(source);
848 *-----------------------------------------------------------------------------
849 * pixbuf drawing (triangle)
850 *-----------------------------------------------------------------------------
853 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
854 gint *rx, gint *ry, gint *rw, gint *rh)
862 tw = MAX(abs(x1 - x2), abs(x2 - x3));
863 tw = MAX(tw, abs(x3 - x1));
864 th = MAX(abs(y1 - y2), abs(y2 - y3));
865 th = MAX(th, abs(y3 - y1));
873 void pixbuf_draw_triangle(GdkPixbuf *pb,
874 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
875 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
876 guint8 r, guint8 g, guint8 b, guint8 a)
888 gdouble slope1, slope2;
889 gint slope1_x, slope1_y;
892 gboolean middle = FALSE;
896 pw = gdk_pixbuf_get_width(pb);
897 ph = gdk_pixbuf_get_height(pb);
899 if (!util_clip_region(0, 0, pw, ph,
900 clip_x, clip_y, clip_w, clip_h,
901 &rx, &ry, &rw, &rh)) return;
903 util_clip_triangle(x1, y1, x2, y2, x3, y3,
906 if (!util_clip_region(rx, ry, rw, rh,
908 &fx1, &fy1, &fw, &fh)) return;
912 has_alpha = gdk_pixbuf_get_has_alpha(pb);
913 prs = gdk_pixbuf_get_rowstride(pb);
914 p_pix = gdk_pixbuf_get_pixels(pb);
916 p_step = (has_alpha) ? 4 : 3;
920 t = x1; x1 = x2; x2 = t;
921 t = y1; y1 = y2; y2 = t;
925 t = x2; x2 = x3; x3 = t;
926 t = y2; y2 = y3; y3 = t;
930 t = x1; x1 = x2; x2 = t;
931 t = y1; y1 = y2; y2 = t;
934 slope1 = (gdouble)(y2 - y1);
935 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
938 slope2 = (gdouble)(y3 - y1);
939 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
941 for (y = fy1; y < fy2; y++)
945 if (!middle && y > y2)
947 slope1 = (gdouble)(y3 - y2);
948 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
955 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
956 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
960 t = xa; xa = xb; xb = t;
963 xa = CLAMP(xa, fx1, fx2);
964 xb = CLAMP(xb, fx1, fx2);
966 pp = p_pix + y * prs + xa * p_step;
970 *pp = (r * a + *pp * (256-a)) >> 8;
972 *pp = (g * a + *pp * (256-a)) >> 8;
974 *pp = (b * a + *pp * (256-a)) >> 8;
984 *-----------------------------------------------------------------------------
985 * pixbuf drawing (line)
986 *-----------------------------------------------------------------------------
989 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
990 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
991 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
993 gboolean flip = FALSE;
1000 t = x1; x1 = x2; x2 = t;
1001 t = y1; y1 = y2; y2 = t;
1005 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1009 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1013 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1021 slope = (y2 - y1) / d;
1024 y1 = y1 + slope * (clip_x - x1);
1027 if (x2 > clip_x + clip_w)
1029 y2 = y2 + slope * (clip_x + clip_w - x2);
1030 x2 = clip_x + clip_w;
1036 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1042 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1044 t = x1; x1 = x2; x2 = t;
1045 t = y1; y1 = y2; y2 = t;
1054 slope = (x2 - x1) / d;
1057 x1 = x1 + slope * (clip_y - y1);
1060 if (y2 > clip_y + clip_h)
1062 x2 = x2 + slope * (clip_y + clip_h - y2);
1063 y2 = clip_y + clip_h;
1085 void pixbuf_draw_line(GdkPixbuf *pb,
1086 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1087 gint x1, gint y1, gint x2, gint y2,
1088 guint8 r, guint8 g, guint8 b, guint8 a)
1092 gint rx, ry, rw, rh;
1093 gdouble rx1, ry1, rx2, ry2;
1100 gint cx1, cy1, cx2, cy2;
1104 pw = gdk_pixbuf_get_width(pb);
1105 ph = gdk_pixbuf_get_height(pb);
1107 if (!util_clip_region(0, 0, pw, ph,
1108 clip_x, clip_y, clip_w, clip_h,
1109 &rx, &ry, &rw, &rh)) return;
1110 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1111 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1112 &rx1, &ry1, &rx2, &ry2)) return;
1119 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1120 prs = gdk_pixbuf_get_rowstride(pb);
1121 p_pix = gdk_pixbuf_get_pixels(pb);
1123 p_step = (has_alpha) ? 4 : 3;
1125 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1130 t = rx1; rx1 = rx2; rx2 = t;
1131 t = ry1; ry1 = ry2; ry2 = t;
1135 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1136 for (x = rx1; x < rx2; x += 1.0)
1138 px = (gint)(x + 0.5);
1139 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1141 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1143 pp = p_pix + py * prs + px * p_step;
1144 *pp = (r * a + *pp * (256-a)) >> 8;
1146 *pp = (g * a + *pp * (256-a)) >> 8;
1148 *pp = (b * a + *pp * (256-a)) >> 8;
1157 t = rx1; rx1 = rx2; rx2 = t;
1158 t = ry1; ry1 = ry2; ry2 = t;
1162 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1163 for (y = ry1; y < ry2; y += 1.0)
1165 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1166 py = (gint)(y + 0.5);
1168 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1170 pp = p_pix + py * prs + px * p_step;
1171 *pp = (r * a + *pp * (256-a)) >> 8;
1173 *pp = (g * a + *pp * (256-a)) >> 8;
1175 *pp = (b * a + *pp * (256-a)) >> 8;
1182 *-----------------------------------------------------------------------------
1183 * pixbuf drawing (fades and shadows)
1184 *-----------------------------------------------------------------------------
1187 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1188 gint s, gboolean vertical, gint border,
1189 gint x1, gint y1, gint x2, gint y2,
1190 guint8 r, guint8 g, guint8 b, guint8 a)
1197 p_step = (has_alpha) ? 4 : 3;
1198 for (j = y1; j < y2; j++)
1200 pp = p_pix + j * prs + x1 * p_step;
1201 if (!vertical) n = a - a * abs(j - s) / border;
1202 for (i = x1; i < x2; i++)
1204 if (vertical) n = a - a * abs(i - s) / border;
1205 *pp = (r * n + *pp * (256-n)) >> 8;
1207 *pp = (g * n + *pp * (256-n)) >> 8;
1209 *pp = (b * n + *pp * (256-n)) >> 8;
1211 if (has_alpha) pp++;
1216 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1217 gint sx, gint sy, gint border,
1218 gint x1, gint y1, gint x2, gint y2,
1219 guint8 r, guint8 g, guint8 b, guint8 a)
1225 p_step = (has_alpha) ? 4 : 3;
1226 for (j = y1; j < y2; j++)
1228 pp = p_pix + j * prs + x1 * p_step;
1229 for (i = x1; i < x2; i++)
1234 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1235 n = a - a * r / border;
1236 *pp = (r * n + *pp * (256-n)) >> 8;
1238 *pp = (g * n + *pp * (256-n)) >> 8;
1240 *pp = (b * n + *pp * (256-n)) >> 8;
1242 if (has_alpha) pp++;
1247 void pixbuf_draw_shadow(GdkPixbuf *pb,
1248 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1249 gint x, gint y, gint w, gint h, gint border,
1250 guint8 r, guint8 g, guint8 b, guint8 a)
1254 gint rx, ry, rw, rh;
1255 gint fx, fy, fw, fh;
1260 pw = gdk_pixbuf_get_width(pb);
1261 ph = gdk_pixbuf_get_height(pb);
1263 if (!util_clip_region(0, 0, pw, ph,
1264 clip_x, clip_y, clip_w, clip_h,
1265 &rx, &ry, &rw, &rh)) return;
1267 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1268 prs = gdk_pixbuf_get_rowstride(pb);
1269 p_pix = gdk_pixbuf_get_pixels(pb);
1271 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1273 &fx, &fy, &fw, &fh))
1275 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1278 if (border < 1) return;
1280 if (util_clip_region(x, y + border, border, h - border * 2,
1282 &fx, &fy, &fw, &fh))
1284 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1285 x + border, TRUE, border,
1286 fx, fy, fx + fw, fy + fh,
1289 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1291 &fx, &fy, &fw, &fh))
1293 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1294 x + w - border, TRUE, border,
1295 fx, fy, fx + fw, fy + fh,
1298 if (util_clip_region(x + border, y, w - border * 2, border,
1300 &fx, &fy, &fw, &fh))
1302 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1303 y + border, FALSE, border,
1304 fx, fy, fx + fw, fy + fh,
1307 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1309 &fx, &fy, &fw, &fh))
1311 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1312 y + h - border, FALSE, border,
1313 fx, fy, fx + fw, fy + fh,
1316 if (util_clip_region(x, y, border, border,
1318 &fx, &fy, &fw, &fh))
1320 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1321 x + border, y + border, border,
1322 fx, fy, fx + fw, fy + fh,
1325 if (util_clip_region(x + w - border, y, border, border,
1327 &fx, &fy, &fw, &fh))
1329 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1330 x + w - border, y + border, border,
1331 fx, fy, fx + fw, fy + fh,
1334 if (util_clip_region(x, y + h - border, border, border,
1336 &fx, &fy, &fw, &fh))
1338 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1339 x + border, y + h - border, border,
1340 fx, fy, fx + fw, fy + fh,
1343 if (util_clip_region(x + w - border, y + h - border, border, border,
1345 &fx, &fy, &fw, &fh))
1347 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1348 x + w - border, y + h - border, border,
1349 fx, fy, fx + fw, fy + fh,
1356 *-----------------------------------------------------------------------------
1357 * pixbuf color alterations
1358 *-----------------------------------------------------------------------------
1361 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1362 gint x, gint y, gint w, gint h)
1372 pw = gdk_pixbuf_get_width(pb);
1373 ph = gdk_pixbuf_get_height(pb);
1375 if (x < 0 || x + w > pw) return;
1376 if (y < 0 || y + h > ph) return;
1378 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1379 prs = gdk_pixbuf_get_rowstride(pb);
1380 p_pix = gdk_pixbuf_get_pixels(pb);
1382 for (i = 0; i < h; i++)
1384 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1385 for (j = 0; j < w; j++)
1389 grey = (pp[0] + pp[1] + pp[2]) / 3;
1396 if (has_alpha) pp++;
1400 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */