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_METADATA, icon_metadata },
112 { PIXBUF_INLINE_UNKNOWN, icon_unknown },
113 { PIXBUF_INLINE_VIDEO, icon_video },
114 { PIXBUF_INLINE_COLLECTION, icon_collection },
115 { PIXBUF_INLINE_ICON, gqview_icon },
116 { PIXBUF_INLINE_LOGO, geeqie_logo },
117 { PIXBUF_INLINE_ICON_FLOAT, icon_float },
118 { PIXBUF_INLINE_ICON_THUMB, icon_thumb },
119 { PIXBUF_INLINE_ICON_BOOK, icon_book },
120 { PIXBUF_INLINE_ICON_CONFIG, icon_config },
121 { PIXBUF_INLINE_ICON_TOOLS, icon_tools },
122 { PIXBUF_INLINE_ICON_VIEW, icon_view },
123 { PIXBUF_INLINE_ICON_GUIDELINES, icon_guidelines },
124 { PIXBUF_INLINE_ICON_PANORAMA, icon_panorama },
125 { PIXBUF_INLINE_ICON_MAINTENANCE, icon_maintenance },
126 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, icon_zoomfillhor },
127 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, icon_zoomfillvert },
128 { PIXBUF_INLINE_ICON_HIDETOOLS, icon_hidetools },
129 { PIXBUF_INLINE_ICON_EXIF, icon_exif },
130 { PIXBUF_INLINE_ICON_MARKS, icon_marks },
131 { PIXBUF_INLINE_ICON_INFO, icon_info },
132 { PIXBUF_INLINE_ICON_SORT, icon_sort },
136 GdkPixbuf *pixbuf_inline(const gchar *key)
140 if (!key) return NULL;
143 while (inline_pixbuf_data[i].key)
145 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
147 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
152 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
157 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
159 static GtkIconFactory *icon_factory = NULL;
160 GtkIconSet *icon_set;
164 icon_factory = gtk_icon_factory_new();
165 gtk_icon_factory_add_default(icon_factory);
168 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
169 gtk_icon_factory_add(icon_factory, key, icon_set);
173 void pixbuf_inline_register_stock_icons(void)
178 while (inline_pixbuf_data[i].key)
180 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
185 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
187 GtkIconTheme *icon_theme;
189 GError *error = NULL;
191 icon_theme = gtk_icon_theme_get_default();
193 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
195 pixbuf = gtk_icon_theme_load_icon(icon_theme,
196 icon, /* icon name */
204 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
209 if (strchr(icon, '.'))
211 /* try again without extension */
212 gchar *icon2 = remove_extension_from_path(icon);
213 pixbuf = gtk_icon_theme_load_icon(icon_theme,
214 icon2, /* icon name */
220 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
227 if (!pixbuf) return FALSE;
229 register_stock_icon(key, pixbuf);
233 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
234 gint old_w, gint old_h,
235 gint *new_w, gint *new_h)
237 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
240 *new_h = (gdouble)*new_w / old_w * old_h;
241 if (*new_h < 1) *new_h = 1;
246 *new_w = (gdouble)*new_h / old_h * old_w;
247 if (*new_w < 1) *new_w = 1;
250 return (*new_w != old_w || *new_h != old_h);
253 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
257 switch (fd->format_class)
259 case FORMAT_CLASS_UNKNOWN:
260 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
262 case FORMAT_CLASS_META:
263 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
265 case FORMAT_CLASS_VIDEO:
266 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
268 case FORMAT_CLASS_COLLECTION:
269 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
272 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
275 if (requested_width && requested_height)
277 gint w = gdk_pixbuf_get_width(pixbuf);
278 gint h = gdk_pixbuf_get_height(pixbuf);
280 if (w > requested_width || h > requested_height)
284 if (pixbuf_scale_aspect(requested_width, requested_height,
290 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
291 g_object_unref(G_OBJECT(tmp));
300 *-----------------------------------------------------------------------------
302 *-----------------------------------------------------------------------------
305 gboolean util_clip_region(gint x, gint y, gint w, gint h,
306 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
307 gint *rx, gint *ry, gint *rw, gint *rh)
309 if (clip_x + clip_w <= x ||
311 clip_y + clip_h <= y ||
317 *rx = MAX(x, clip_x);
318 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
320 *ry = MAX(y, clip_y);
321 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
327 *-----------------------------------------------------------------------------
329 *-----------------------------------------------------------------------------
332 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
333 guchar *dest, gint dest_row_stride, gint w, gint h,
334 gint bytes_per_pixel, gboolean counter_clockwise)
340 for (i = 0; i < h; i++)
342 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
343 for (j = 0; j < w; j++)
345 if (counter_clockwise)
347 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
351 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
353 *(dp++) = *(sp++); /* r */
354 *(dp++) = *(sp++); /* g */
355 *(dp++) = *(sp++); /* b */
356 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
362 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
363 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
369 for (i = 0; i < h; i++)
371 sp = src + (i * src_row_stride);
372 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
373 memcpy(dp, sp, w * bytes_per_pixel);
377 #define ROTATE_BUFFER_WIDTH 48
378 #define ROTATE_BUFFER_HEIGHT 48
381 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
384 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
399 if (!src) return NULL;
401 sw = gdk_pixbuf_get_width(src);
402 sh = gdk_pixbuf_get_height(src);
403 has_alpha = gdk_pixbuf_get_has_alpha(src);
404 srs = gdk_pixbuf_get_rowstride(src);
405 s_pix = gdk_pixbuf_get_pixels(src);
409 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
410 drs = gdk_pixbuf_get_rowstride(dest);
411 d_pix = gdk_pixbuf_get_pixels(dest);
413 a = (has_alpha ? 4 : 3);
415 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
416 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
417 b_pix = gdk_pixbuf_get_pixels(buffer);
418 brs = gdk_pixbuf_get_rowstride(buffer);
420 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
422 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
423 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
427 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
428 pixbuf_copy_block_rotate(s_pix, srs, j, i,
430 a, counter_clockwise);
432 if (counter_clockwise)
442 pixbuf_copy_block(b_pix, brs, w, h,
443 d_pix, drs, x, y, a);
447 g_object_unref(buffer);
450 /* this is the simple version of rotation (roughly 2-4x slower) */
452 for (i = 0; i < sh; i++)
454 sp = s_pix + (i * srs);
455 for (j = 0; j < sw; j++)
457 if (counter_clockwise)
459 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
463 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
466 *(dp++) = *(sp++); /* r */
467 *(dp++) = *(sp++); /* g */
468 *(dp++) = *(sp++); /* b */
469 if (has_alpha) *(dp) = *(sp++); /* a */
478 * Returns a copy of pixbuf mirrored and or flipped.
479 * TO do a 180 degree rotations set both mirror and flipped TRUE
480 * if mirror and flip are FALSE, result is a simple copy.
482 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
495 if (!src) return NULL;
497 w = gdk_pixbuf_get_width(src);
498 h = gdk_pixbuf_get_height(src);
499 has_alpha = gdk_pixbuf_get_has_alpha(src);
500 srs = gdk_pixbuf_get_rowstride(src);
501 s_pix = gdk_pixbuf_get_pixels(src);
503 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
504 drs = gdk_pixbuf_get_rowstride(dest);
505 d_pix = gdk_pixbuf_get_pixels(dest);
507 a = has_alpha ? 4 : 3;
509 for (i = 0; i < h; i++)
511 sp = s_pix + (i * srs);
514 dp = d_pix + ((h - i - 1) * drs);
518 dp = d_pix + (i * drs);
523 for (j = 0; j < w; j++)
525 *(dp++) = *(sp++); /* r */
526 *(dp++) = *(sp++); /* g */
527 *(dp++) = *(sp++); /* b */
528 if (has_alpha) *(dp) = *(sp++); /* a */
534 for (j = 0; j < w; j++)
536 *(dp++) = *(sp++); /* r */
537 *(dp++) = *(sp++); /* g */
538 *(dp++) = *(sp++); /* b */
539 if (has_alpha) *(dp++) = *(sp++); /* a */
547 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
550 GdkPixbuf *tmp = NULL;
554 case EXIF_ORIENTATION_TOP_LEFT:
555 dest = gdk_pixbuf_copy(pixbuf);
557 case EXIF_ORIENTATION_TOP_RIGHT:
559 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
561 case EXIF_ORIENTATION_BOTTOM_RIGHT:
563 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
565 case EXIF_ORIENTATION_BOTTOM_LEFT:
567 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
569 case EXIF_ORIENTATION_LEFT_TOP:
570 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
571 dest = pixbuf_copy_rotate_90(tmp, FALSE);
573 case EXIF_ORIENTATION_RIGHT_TOP:
574 /* rotated -90 (270) */
575 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
577 case EXIF_ORIENTATION_RIGHT_BOTTOM:
578 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
579 dest = pixbuf_copy_rotate_90(tmp, TRUE);
581 case EXIF_ORIENTATION_LEFT_BOTTOM:
583 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
586 dest = gdk_pixbuf_copy(pixbuf);
589 if (tmp) g_object_unref(tmp);
596 *-----------------------------------------------------------------------------
597 * pixbuf drawing (rectangles)
598 *-----------------------------------------------------------------------------
602 * Fills region of pixbuf at x,y over w,h
603 * with colors red (r), green (g), blue (b)
604 * applying alpha (a), use a=255 for solid.
606 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
607 gint x, gint y, gint w, gint h,
608 gint r, gint g, gint b, gint a)
618 pw = gdk_pixbuf_get_width(pb);
619 ph = gdk_pixbuf_get_height(pb);
621 if (x < 0 || x + w > pw) return;
622 if (y < 0 || y + h > ph) return;
624 has_alpha = gdk_pixbuf_get_has_alpha(pb);
625 prs = gdk_pixbuf_get_rowstride(pb);
626 p_pix = gdk_pixbuf_get_pixels(pb);
628 for (i = 0; i < h; i++)
630 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
631 for (j = 0; j < w; j++)
633 *pp = (r * a + *pp * (256-a)) >> 8;
635 *pp = (g * a + *pp * (256-a)) >> 8;
637 *pp = (b * a + *pp * (256-a)) >> 8;
644 void pixbuf_draw_rect(GdkPixbuf *pb,
645 gint x, gint y, gint w, gint h,
646 gint r, gint g, gint b, gint a,
647 gint left, gint right, gint top, gint bottom)
649 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
651 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
653 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
655 pixbuf_draw_rect_fill(pb, x, y, left, h,
659 void pixbuf_set_rect_fill(GdkPixbuf *pb,
660 gint x, gint y, gint w, gint h,
661 gint r, gint g, gint b, gint a)
671 pw = gdk_pixbuf_get_width(pb);
672 ph = gdk_pixbuf_get_height(pb);
674 if (x < 0 || x + w > pw) return;
675 if (y < 0 || y + h > ph) return;
677 has_alpha = gdk_pixbuf_get_has_alpha(pb);
678 prs = gdk_pixbuf_get_rowstride(pb);
679 p_pix = gdk_pixbuf_get_pixels(pb);
681 for (i = 0; i < h; i++)
683 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
684 for (j = 0; j < w; j++)
689 if (has_alpha) { *pp = a; pp++; }
694 void pixbuf_set_rect(GdkPixbuf *pb,
695 gint x, gint y, gint w, gint h,
696 gint r, gint g, gint b, gint a,
697 gint left, gint right, gint top, gint bottom)
699 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
701 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
703 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
705 pixbuf_set_rect_fill(pb, x, y, left, h,
709 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
716 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
717 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
719 buf = gdk_pixbuf_get_pixels(pb);
720 has_alpha = gdk_pixbuf_get_has_alpha(pb);
721 rowstride = gdk_pixbuf_get_rowstride(pb);
723 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
727 if (has_alpha) *p = a;
732 *-----------------------------------------------------------------------------
733 * pixbuf text rendering
734 *-----------------------------------------------------------------------------
737 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
738 GdkPixbuf *dest, gint dx, gint dy,
740 guint8 r, guint8 g, guint8 b, guint8 a)
755 if (!src || !dest) return;
757 sw = gdk_pixbuf_get_width(src);
758 sh = gdk_pixbuf_get_height(src);
760 if (sx < 0 || sx + w > sw) return;
761 if (sy < 0 || sy + h > sh) return;
763 dw = gdk_pixbuf_get_width(dest);
764 dh = gdk_pixbuf_get_height(dest);
766 if (dx < 0 || dx + w > dw) return;
767 if (dy < 0 || dy + h > dh) return;
769 s_alpha = gdk_pixbuf_get_has_alpha(src);
770 d_alpha = gdk_pixbuf_get_has_alpha(dest);
771 srs = gdk_pixbuf_get_rowstride(src);
772 drs = gdk_pixbuf_get_rowstride(dest);
773 s_pix = gdk_pixbuf_get_pixels(src);
774 d_pix = gdk_pixbuf_get_pixels(dest);
776 s_step = (s_alpha) ? 4 : 3;
777 d_step = (d_alpha) ? 4 : 3;
779 for (i = 0; i < h; i++)
781 sp = s_pix + (sy + i) * srs + sx * s_step;
782 dp = d_pix + (dy + i) * drs + dx * d_step;
783 for (j = 0; j < w; j++)
789 asub = a * sp[0] / 255;
790 *dp = (r * asub + *dp * (256-asub)) >> 8;
792 asub = a * sp[1] / 255;
793 *dp = (g * asub + *dp * (256-asub)) >> 8;
795 asub = a * sp[2] / 255;
796 *dp = (b * asub + *dp * (256-asub)) >> 8;
801 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
815 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
817 guint8 r, guint8 g, guint8 b, guint8 a)
823 cairo_surface_t *source;
826 pango_layout_get_pixel_size(layout, &w, &h);
827 if (w < 1 || h < 1) return;
829 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
831 cr = cairo_create (source);
832 cairo_set_source_rgb(cr, 0, 0, 0);
833 cairo_rectangle (cr, 0, 0, w, h);
835 cairo_set_source_rgb(cr, 1, 1, 1);
836 pango_cairo_show_layout (cr, layout);
839 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
841 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
843 cairo_image_surface_get_width (source),
844 cairo_image_surface_get_height (source),
845 cairo_image_surface_get_stride (source),
851 dw = gdk_pixbuf_get_width(pixbuf);
852 dh = gdk_pixbuf_get_height(pixbuf);
868 if (x + w > dw) w = dw - x;
869 if (y + h > dh) h = dh - y;
871 pixbuf_copy_font(buffer, sx, sy,
875 g_object_unref(buffer);
876 cairo_surface_destroy(source);
880 *-----------------------------------------------------------------------------
881 * pixbuf drawing (triangle)
882 *-----------------------------------------------------------------------------
885 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
886 gint *rx, gint *ry, gint *rw, gint *rh)
894 tw = MAX(abs(x1 - x2), abs(x2 - x3));
895 tw = MAX(tw, abs(x3 - x1));
896 th = MAX(abs(y1 - y2), abs(y2 - y3));
897 th = MAX(th, abs(y3 - y1));
905 void pixbuf_draw_triangle(GdkPixbuf *pb,
906 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
907 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
908 guint8 r, guint8 g, guint8 b, guint8 a)
920 gdouble slope1, slope2;
921 gint slope1_x, slope1_y;
924 gboolean middle = FALSE;
928 pw = gdk_pixbuf_get_width(pb);
929 ph = gdk_pixbuf_get_height(pb);
931 if (!util_clip_region(0, 0, pw, ph,
932 clip_x, clip_y, clip_w, clip_h,
933 &rx, &ry, &rw, &rh)) return;
935 util_clip_triangle(x1, y1, x2, y2, x3, y3,
938 if (!util_clip_region(rx, ry, rw, rh,
940 &fx1, &fy1, &fw, &fh)) return;
944 has_alpha = gdk_pixbuf_get_has_alpha(pb);
945 prs = gdk_pixbuf_get_rowstride(pb);
946 p_pix = gdk_pixbuf_get_pixels(pb);
948 p_step = (has_alpha) ? 4 : 3;
952 t = x1; x1 = x2; x2 = t;
953 t = y1; y1 = y2; y2 = t;
957 t = x2; x2 = x3; x3 = t;
958 t = y2; y2 = y3; y3 = t;
962 t = x1; x1 = x2; x2 = t;
963 t = y1; y1 = y2; y2 = t;
966 slope1 = (gdouble)(y2 - y1);
967 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
970 slope2 = (gdouble)(y3 - y1);
971 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
973 for (y = fy1; y < fy2; y++)
977 if (!middle && y > y2)
979 slope1 = (gdouble)(y3 - y2);
980 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
987 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
988 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
992 t = xa; xa = xb; xb = t;
995 xa = CLAMP(xa, fx1, fx2);
996 xb = CLAMP(xb, fx1, fx2);
998 pp = p_pix + y * prs + xa * p_step;
1002 *pp = (r * a + *pp * (256-a)) >> 8;
1004 *pp = (g * a + *pp * (256-a)) >> 8;
1006 *pp = (b * a + *pp * (256-a)) >> 8;
1008 if (has_alpha) pp++;
1016 *-----------------------------------------------------------------------------
1017 * pixbuf drawing (line)
1018 *-----------------------------------------------------------------------------
1021 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1022 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1023 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1025 gboolean flip = FALSE;
1032 t = x1; x1 = x2; x2 = t;
1033 t = y1; y1 = y2; y2 = t;
1037 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1041 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1045 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1053 slope = (y2 - y1) / d;
1056 y1 = y1 + slope * (clip_x - x1);
1059 if (x2 > clip_x + clip_w)
1061 y2 = y2 + slope * (clip_x + clip_w - x2);
1062 x2 = clip_x + clip_w;
1068 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1074 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1076 t = x1; x1 = x2; x2 = t;
1077 t = y1; y1 = y2; y2 = t;
1086 slope = (x2 - x1) / d;
1089 x1 = x1 + slope * (clip_y - y1);
1092 if (y2 > clip_y + clip_h)
1094 x2 = x2 + slope * (clip_y + clip_h - y2);
1095 y2 = clip_y + clip_h;
1117 void pixbuf_draw_line(GdkPixbuf *pb,
1118 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1119 gint x1, gint y1, gint x2, gint y2,
1120 guint8 r, guint8 g, guint8 b, guint8 a)
1124 gint rx, ry, rw, rh;
1125 gdouble rx1, ry1, rx2, ry2;
1132 gint cx1, cy1, cx2, cy2;
1136 pw = gdk_pixbuf_get_width(pb);
1137 ph = gdk_pixbuf_get_height(pb);
1139 if (!util_clip_region(0, 0, pw, ph,
1140 clip_x, clip_y, clip_w, clip_h,
1141 &rx, &ry, &rw, &rh)) return;
1142 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1143 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1144 &rx1, &ry1, &rx2, &ry2)) return;
1151 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1152 prs = gdk_pixbuf_get_rowstride(pb);
1153 p_pix = gdk_pixbuf_get_pixels(pb);
1155 p_step = (has_alpha) ? 4 : 3;
1157 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1162 t = rx1; rx1 = rx2; rx2 = t;
1163 t = ry1; ry1 = ry2; ry2 = t;
1167 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1168 for (x = rx1; x < rx2; x += 1.0)
1170 px = (gint)(x + 0.5);
1171 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1173 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1175 pp = p_pix + py * prs + px * p_step;
1176 *pp = (r * a + *pp * (256-a)) >> 8;
1178 *pp = (g * a + *pp * (256-a)) >> 8;
1180 *pp = (b * a + *pp * (256-a)) >> 8;
1189 t = rx1; rx1 = rx2; rx2 = t;
1190 t = ry1; ry1 = ry2; ry2 = t;
1194 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1195 for (y = ry1; y < ry2; y += 1.0)
1197 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1198 py = (gint)(y + 0.5);
1200 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1202 pp = p_pix + py * prs + px * p_step;
1203 *pp = (r * a + *pp * (256-a)) >> 8;
1205 *pp = (g * a + *pp * (256-a)) >> 8;
1207 *pp = (b * a + *pp * (256-a)) >> 8;
1214 *-----------------------------------------------------------------------------
1215 * pixbuf drawing (fades and shadows)
1216 *-----------------------------------------------------------------------------
1219 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1220 gint s, gboolean vertical, gint border,
1221 gint x1, gint y1, gint x2, gint y2,
1222 guint8 r, guint8 g, guint8 b, guint8 a)
1229 p_step = (has_alpha) ? 4 : 3;
1230 for (j = y1; j < y2; j++)
1232 pp = p_pix + j * prs + x1 * p_step;
1233 if (!vertical) n = a - a * abs(j - s) / border;
1234 for (i = x1; i < x2; i++)
1236 if (vertical) n = a - a * abs(i - s) / border;
1237 *pp = (r * n + *pp * (256-n)) >> 8;
1239 *pp = (g * n + *pp * (256-n)) >> 8;
1241 *pp = (b * n + *pp * (256-n)) >> 8;
1243 if (has_alpha) pp++;
1248 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1249 gint sx, gint sy, gint border,
1250 gint x1, gint y1, gint x2, gint y2,
1251 guint8 r, guint8 g, guint8 b, guint8 a)
1257 p_step = (has_alpha) ? 4 : 3;
1258 for (j = y1; j < y2; j++)
1260 pp = p_pix + j * prs + x1 * p_step;
1261 for (i = x1; i < x2; i++)
1266 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1267 n = a - a * r / border;
1268 *pp = (r * n + *pp * (256-n)) >> 8;
1270 *pp = (g * n + *pp * (256-n)) >> 8;
1272 *pp = (b * n + *pp * (256-n)) >> 8;
1274 if (has_alpha) pp++;
1279 void pixbuf_draw_shadow(GdkPixbuf *pb,
1280 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1281 gint x, gint y, gint w, gint h, gint border,
1282 guint8 r, guint8 g, guint8 b, guint8 a)
1286 gint rx, ry, rw, rh;
1287 gint fx, fy, fw, fh;
1292 pw = gdk_pixbuf_get_width(pb);
1293 ph = gdk_pixbuf_get_height(pb);
1295 if (!util_clip_region(0, 0, pw, ph,
1296 clip_x, clip_y, clip_w, clip_h,
1297 &rx, &ry, &rw, &rh)) return;
1299 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1300 prs = gdk_pixbuf_get_rowstride(pb);
1301 p_pix = gdk_pixbuf_get_pixels(pb);
1303 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1305 &fx, &fy, &fw, &fh))
1307 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1310 if (border < 1) return;
1312 if (util_clip_region(x, y + border, border, h - border * 2,
1314 &fx, &fy, &fw, &fh))
1316 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1317 x + border, TRUE, border,
1318 fx, fy, fx + fw, fy + fh,
1321 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1323 &fx, &fy, &fw, &fh))
1325 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1326 x + w - border, TRUE, border,
1327 fx, fy, fx + fw, fy + fh,
1330 if (util_clip_region(x + border, y, w - border * 2, border,
1332 &fx, &fy, &fw, &fh))
1334 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1335 y + border, FALSE, border,
1336 fx, fy, fx + fw, fy + fh,
1339 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1341 &fx, &fy, &fw, &fh))
1343 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1344 y + h - border, FALSE, border,
1345 fx, fy, fx + fw, fy + fh,
1348 if (util_clip_region(x, y, border, border,
1350 &fx, &fy, &fw, &fh))
1352 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1353 x + border, y + border, border,
1354 fx, fy, fx + fw, fy + fh,
1357 if (util_clip_region(x + w - border, y, border, border,
1359 &fx, &fy, &fw, &fh))
1361 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1362 x + w - border, y + border, border,
1363 fx, fy, fx + fw, fy + fh,
1366 if (util_clip_region(x, y + h - border, border, border,
1368 &fx, &fy, &fw, &fh))
1370 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1371 x + border, y + h - border, border,
1372 fx, fy, fx + fw, fy + fh,
1375 if (util_clip_region(x + w - border, y + h - border, border, border,
1377 &fx, &fy, &fw, &fh))
1379 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1380 x + w - border, y + h - border, border,
1381 fx, fy, fx + fw, fy + fh,
1388 *-----------------------------------------------------------------------------
1389 * pixbuf color alterations
1390 *-----------------------------------------------------------------------------
1393 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1394 gint x, gint y, gint w, gint h)
1404 pw = gdk_pixbuf_get_width(pb);
1405 ph = gdk_pixbuf_get_height(pb);
1407 if (x < 0 || x + w > pw) return;
1408 if (y < 0 || y + h > ph) return;
1410 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1411 prs = gdk_pixbuf_get_rowstride(pb);
1412 p_pix = gdk_pixbuf_get_pixels(pb);
1414 for (i = 0; i < h; i++)
1416 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1417 for (j = 0; j < w; j++)
1421 grey = (pp[0] + pp[1] + pp[2]) / 3;
1428 if (has_alpha) pp++;
1432 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */