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 },
133 { PIXBUF_INLINE_ICON_PDF, icon_pdf },
137 GdkPixbuf *pixbuf_inline(const gchar *key)
141 if (!key) return NULL;
144 while (inline_pixbuf_data[i].key)
146 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
148 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
153 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
158 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
160 static GtkIconFactory *icon_factory = NULL;
161 GtkIconSet *icon_set;
165 icon_factory = gtk_icon_factory_new();
166 gtk_icon_factory_add_default(icon_factory);
169 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
170 gtk_icon_factory_add(icon_factory, key, icon_set);
174 void pixbuf_inline_register_stock_icons(void)
179 while (inline_pixbuf_data[i].key)
181 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
186 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
188 GtkIconTheme *icon_theme;
190 GError *error = NULL;
192 icon_theme = gtk_icon_theme_get_default();
194 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
196 pixbuf = gtk_icon_theme_load_icon(icon_theme,
197 icon, /* icon name */
205 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
210 if (strchr(icon, '.'))
212 /* try again without extension */
213 gchar *icon2 = remove_extension_from_path(icon);
214 pixbuf = gtk_icon_theme_load_icon(icon_theme,
215 icon2, /* icon name */
221 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
228 if (!pixbuf) return FALSE;
230 register_stock_icon(key, pixbuf);
234 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
235 gint old_w, gint old_h,
236 gint *new_w, gint *new_h)
238 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
241 *new_h = (gdouble)*new_w / old_w * old_h;
242 if (*new_h < 1) *new_h = 1;
247 *new_w = (gdouble)*new_h / old_h * old_w;
248 if (*new_w < 1) *new_w = 1;
251 return (*new_w != old_w || *new_h != old_h);
254 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
258 switch (fd->format_class)
260 case FORMAT_CLASS_UNKNOWN:
261 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
263 case FORMAT_CLASS_META:
264 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
266 case FORMAT_CLASS_VIDEO:
267 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
269 case FORMAT_CLASS_COLLECTION:
270 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
272 case FORMAT_CLASS_PDF:
273 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
276 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
279 if (requested_width && requested_height)
281 gint w = gdk_pixbuf_get_width(pixbuf);
282 gint h = gdk_pixbuf_get_height(pixbuf);
284 if (w > requested_width || h > requested_height)
288 if (pixbuf_scale_aspect(requested_width, requested_height,
294 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
295 g_object_unref(G_OBJECT(tmp));
304 *-----------------------------------------------------------------------------
306 *-----------------------------------------------------------------------------
309 gboolean util_clip_region(gint x, gint y, gint w, gint h,
310 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
311 gint *rx, gint *ry, gint *rw, gint *rh)
313 if (clip_x + clip_w <= x ||
315 clip_y + clip_h <= y ||
321 *rx = MAX(x, clip_x);
322 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
324 *ry = MAX(y, clip_y);
325 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
331 *-----------------------------------------------------------------------------
333 *-----------------------------------------------------------------------------
336 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
337 guchar *dest, gint dest_row_stride, gint w, gint h,
338 gint bytes_per_pixel, gboolean counter_clockwise)
344 for (i = 0; i < h; i++)
346 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
347 for (j = 0; j < w; j++)
349 if (counter_clockwise)
351 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
355 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
357 *(dp++) = *(sp++); /* r */
358 *(dp++) = *(sp++); /* g */
359 *(dp++) = *(sp++); /* b */
360 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
366 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
367 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
373 for (i = 0; i < h; i++)
375 sp = src + (i * src_row_stride);
376 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
377 memcpy(dp, sp, w * bytes_per_pixel);
381 #define ROTATE_BUFFER_WIDTH 48
382 #define ROTATE_BUFFER_HEIGHT 48
385 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
388 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
403 if (!src) return NULL;
405 sw = gdk_pixbuf_get_width(src);
406 sh = gdk_pixbuf_get_height(src);
407 has_alpha = gdk_pixbuf_get_has_alpha(src);
408 srs = gdk_pixbuf_get_rowstride(src);
409 s_pix = gdk_pixbuf_get_pixels(src);
413 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
414 drs = gdk_pixbuf_get_rowstride(dest);
415 d_pix = gdk_pixbuf_get_pixels(dest);
417 a = (has_alpha ? 4 : 3);
419 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
420 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
421 b_pix = gdk_pixbuf_get_pixels(buffer);
422 brs = gdk_pixbuf_get_rowstride(buffer);
424 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
426 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
427 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
431 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
432 pixbuf_copy_block_rotate(s_pix, srs, j, i,
434 a, counter_clockwise);
436 if (counter_clockwise)
446 pixbuf_copy_block(b_pix, brs, w, h,
447 d_pix, drs, x, y, a);
451 g_object_unref(buffer);
454 /* this is the simple version of rotation (roughly 2-4x slower) */
456 for (i = 0; i < sh; i++)
458 sp = s_pix + (i * srs);
459 for (j = 0; j < sw; j++)
461 if (counter_clockwise)
463 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
467 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
470 *(dp++) = *(sp++); /* r */
471 *(dp++) = *(sp++); /* g */
472 *(dp++) = *(sp++); /* b */
473 if (has_alpha) *(dp) = *(sp++); /* a */
482 * Returns a copy of pixbuf mirrored and or flipped.
483 * TO do a 180 degree rotations set both mirror and flipped TRUE
484 * if mirror and flip are FALSE, result is a simple copy.
486 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
499 if (!src) return NULL;
501 w = gdk_pixbuf_get_width(src);
502 h = gdk_pixbuf_get_height(src);
503 has_alpha = gdk_pixbuf_get_has_alpha(src);
504 srs = gdk_pixbuf_get_rowstride(src);
505 s_pix = gdk_pixbuf_get_pixels(src);
507 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
508 drs = gdk_pixbuf_get_rowstride(dest);
509 d_pix = gdk_pixbuf_get_pixels(dest);
511 a = has_alpha ? 4 : 3;
513 for (i = 0; i < h; i++)
515 sp = s_pix + (i * srs);
518 dp = d_pix + ((h - i - 1) * drs);
522 dp = d_pix + (i * drs);
527 for (j = 0; j < w; j++)
529 *(dp++) = *(sp++); /* r */
530 *(dp++) = *(sp++); /* g */
531 *(dp++) = *(sp++); /* b */
532 if (has_alpha) *(dp) = *(sp++); /* a */
538 for (j = 0; j < w; j++)
540 *(dp++) = *(sp++); /* r */
541 *(dp++) = *(sp++); /* g */
542 *(dp++) = *(sp++); /* b */
543 if (has_alpha) *(dp++) = *(sp++); /* a */
551 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
554 GdkPixbuf *tmp = NULL;
558 case EXIF_ORIENTATION_TOP_LEFT:
559 dest = gdk_pixbuf_copy(pixbuf);
561 case EXIF_ORIENTATION_TOP_RIGHT:
563 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
565 case EXIF_ORIENTATION_BOTTOM_RIGHT:
567 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
569 case EXIF_ORIENTATION_BOTTOM_LEFT:
571 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
573 case EXIF_ORIENTATION_LEFT_TOP:
574 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
575 dest = pixbuf_copy_rotate_90(tmp, FALSE);
577 case EXIF_ORIENTATION_RIGHT_TOP:
578 /* rotated -90 (270) */
579 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
581 case EXIF_ORIENTATION_RIGHT_BOTTOM:
582 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
583 dest = pixbuf_copy_rotate_90(tmp, TRUE);
585 case EXIF_ORIENTATION_LEFT_BOTTOM:
587 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
590 dest = gdk_pixbuf_copy(pixbuf);
593 if (tmp) g_object_unref(tmp);
600 *-----------------------------------------------------------------------------
601 * pixbuf drawing (rectangles)
602 *-----------------------------------------------------------------------------
606 * Fills region of pixbuf at x,y over w,h
607 * with colors red (r), green (g), blue (b)
608 * applying alpha (a), use a=255 for solid.
610 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
611 gint x, gint y, gint w, gint h,
612 gint r, gint g, gint b, gint a)
622 pw = gdk_pixbuf_get_width(pb);
623 ph = gdk_pixbuf_get_height(pb);
625 if (x < 0 || x + w > pw) return;
626 if (y < 0 || y + h > ph) return;
628 has_alpha = gdk_pixbuf_get_has_alpha(pb);
629 prs = gdk_pixbuf_get_rowstride(pb);
630 p_pix = gdk_pixbuf_get_pixels(pb);
632 for (i = 0; i < h; i++)
634 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
635 for (j = 0; j < w; j++)
637 *pp = (r * a + *pp * (256-a)) >> 8;
639 *pp = (g * a + *pp * (256-a)) >> 8;
641 *pp = (b * a + *pp * (256-a)) >> 8;
648 void pixbuf_draw_rect(GdkPixbuf *pb,
649 gint x, gint y, gint w, gint h,
650 gint r, gint g, gint b, gint a,
651 gint left, gint right, gint top, gint bottom)
653 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
655 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
657 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
659 pixbuf_draw_rect_fill(pb, x, y, left, h,
663 void pixbuf_set_rect_fill(GdkPixbuf *pb,
664 gint x, gint y, gint w, gint h,
665 gint r, gint g, gint b, gint a)
675 pw = gdk_pixbuf_get_width(pb);
676 ph = gdk_pixbuf_get_height(pb);
678 if (x < 0 || x + w > pw) return;
679 if (y < 0 || y + h > ph) return;
681 has_alpha = gdk_pixbuf_get_has_alpha(pb);
682 prs = gdk_pixbuf_get_rowstride(pb);
683 p_pix = gdk_pixbuf_get_pixels(pb);
685 for (i = 0; i < h; i++)
687 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
688 for (j = 0; j < w; j++)
693 if (has_alpha) { *pp = a; pp++; }
698 void pixbuf_set_rect(GdkPixbuf *pb,
699 gint x, gint y, gint w, gint h,
700 gint r, gint g, gint b, gint a,
701 gint left, gint right, gint top, gint bottom)
703 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
705 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
707 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
709 pixbuf_set_rect_fill(pb, x, y, left, h,
713 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
720 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
721 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
723 buf = gdk_pixbuf_get_pixels(pb);
724 has_alpha = gdk_pixbuf_get_has_alpha(pb);
725 rowstride = gdk_pixbuf_get_rowstride(pb);
727 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
731 if (has_alpha) *p = a;
736 *-----------------------------------------------------------------------------
737 * pixbuf text rendering
738 *-----------------------------------------------------------------------------
741 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
742 GdkPixbuf *dest, gint dx, gint dy,
744 guint8 r, guint8 g, guint8 b, guint8 a)
759 if (!src || !dest) return;
761 sw = gdk_pixbuf_get_width(src);
762 sh = gdk_pixbuf_get_height(src);
764 if (sx < 0 || sx + w > sw) return;
765 if (sy < 0 || sy + h > sh) return;
767 dw = gdk_pixbuf_get_width(dest);
768 dh = gdk_pixbuf_get_height(dest);
770 if (dx < 0 || dx + w > dw) return;
771 if (dy < 0 || dy + h > dh) return;
773 s_alpha = gdk_pixbuf_get_has_alpha(src);
774 d_alpha = gdk_pixbuf_get_has_alpha(dest);
775 srs = gdk_pixbuf_get_rowstride(src);
776 drs = gdk_pixbuf_get_rowstride(dest);
777 s_pix = gdk_pixbuf_get_pixels(src);
778 d_pix = gdk_pixbuf_get_pixels(dest);
780 s_step = (s_alpha) ? 4 : 3;
781 d_step = (d_alpha) ? 4 : 3;
783 for (i = 0; i < h; i++)
785 sp = s_pix + (sy + i) * srs + sx * s_step;
786 dp = d_pix + (dy + i) * drs + dx * d_step;
787 for (j = 0; j < w; j++)
793 asub = a * sp[0] / 255;
794 *dp = (r * asub + *dp * (256-asub)) >> 8;
796 asub = a * sp[1] / 255;
797 *dp = (g * asub + *dp * (256-asub)) >> 8;
799 asub = a * sp[2] / 255;
800 *dp = (b * asub + *dp * (256-asub)) >> 8;
805 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
819 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
821 guint8 r, guint8 g, guint8 b, guint8 a)
827 cairo_surface_t *source;
830 pango_layout_get_pixel_size(layout, &w, &h);
831 if (w < 1 || h < 1) return;
833 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
835 cr = cairo_create (source);
836 cairo_set_source_rgb(cr, 0, 0, 0);
837 cairo_rectangle (cr, 0, 0, w, h);
839 cairo_set_source_rgb(cr, 1, 1, 1);
840 pango_cairo_show_layout (cr, layout);
843 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
845 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
847 cairo_image_surface_get_width (source),
848 cairo_image_surface_get_height (source),
849 cairo_image_surface_get_stride (source),
855 dw = gdk_pixbuf_get_width(pixbuf);
856 dh = gdk_pixbuf_get_height(pixbuf);
872 if (x + w > dw) w = dw - x;
873 if (y + h > dh) h = dh - y;
875 pixbuf_copy_font(buffer, sx, sy,
879 g_object_unref(buffer);
880 cairo_surface_destroy(source);
884 *-----------------------------------------------------------------------------
885 * pixbuf drawing (triangle)
886 *-----------------------------------------------------------------------------
889 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
890 gint *rx, gint *ry, gint *rw, gint *rh)
898 tw = MAX(abs(x1 - x2), abs(x2 - x3));
899 tw = MAX(tw, abs(x3 - x1));
900 th = MAX(abs(y1 - y2), abs(y2 - y3));
901 th = MAX(th, abs(y3 - y1));
909 void pixbuf_draw_triangle(GdkPixbuf *pb,
910 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
911 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
912 guint8 r, guint8 g, guint8 b, guint8 a)
924 gdouble slope1, slope2;
925 gint slope1_x, slope1_y;
928 gboolean middle = FALSE;
932 pw = gdk_pixbuf_get_width(pb);
933 ph = gdk_pixbuf_get_height(pb);
935 if (!util_clip_region(0, 0, pw, ph,
936 clip_x, clip_y, clip_w, clip_h,
937 &rx, &ry, &rw, &rh)) return;
939 util_clip_triangle(x1, y1, x2, y2, x3, y3,
942 if (!util_clip_region(rx, ry, rw, rh,
944 &fx1, &fy1, &fw, &fh)) return;
948 has_alpha = gdk_pixbuf_get_has_alpha(pb);
949 prs = gdk_pixbuf_get_rowstride(pb);
950 p_pix = gdk_pixbuf_get_pixels(pb);
952 p_step = (has_alpha) ? 4 : 3;
956 t = x1; x1 = x2; x2 = t;
957 t = y1; y1 = y2; y2 = t;
961 t = x2; x2 = x3; x3 = t;
962 t = y2; y2 = y3; y3 = t;
966 t = x1; x1 = x2; x2 = t;
967 t = y1; y1 = y2; y2 = t;
970 slope1 = (gdouble)(y2 - y1);
971 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
974 slope2 = (gdouble)(y3 - y1);
975 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
977 for (y = fy1; y < fy2; y++)
981 if (!middle && y > y2)
983 slope1 = (gdouble)(y3 - y2);
984 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
991 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
992 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
996 t = xa; xa = xb; xb = t;
999 xa = CLAMP(xa, fx1, fx2);
1000 xb = CLAMP(xb, fx1, fx2);
1002 pp = p_pix + y * prs + xa * p_step;
1006 *pp = (r * a + *pp * (256-a)) >> 8;
1008 *pp = (g * a + *pp * (256-a)) >> 8;
1010 *pp = (b * a + *pp * (256-a)) >> 8;
1012 if (has_alpha) pp++;
1020 *-----------------------------------------------------------------------------
1021 * pixbuf drawing (line)
1022 *-----------------------------------------------------------------------------
1025 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1026 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1027 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1029 gboolean flip = FALSE;
1036 t = x1; x1 = x2; x2 = t;
1037 t = y1; y1 = y2; y2 = t;
1041 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1045 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1049 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1057 slope = (y2 - y1) / d;
1060 y1 = y1 + slope * (clip_x - x1);
1063 if (x2 > clip_x + clip_w)
1065 y2 = y2 + slope * (clip_x + clip_w - x2);
1066 x2 = clip_x + clip_w;
1072 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1078 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1080 t = x1; x1 = x2; x2 = t;
1081 t = y1; y1 = y2; y2 = t;
1090 slope = (x2 - x1) / d;
1093 x1 = x1 + slope * (clip_y - y1);
1096 if (y2 > clip_y + clip_h)
1098 x2 = x2 + slope * (clip_y + clip_h - y2);
1099 y2 = clip_y + clip_h;
1121 void pixbuf_draw_line(GdkPixbuf *pb,
1122 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1123 gint x1, gint y1, gint x2, gint y2,
1124 guint8 r, guint8 g, guint8 b, guint8 a)
1128 gint rx, ry, rw, rh;
1129 gdouble rx1, ry1, rx2, ry2;
1136 gint cx1, cy1, cx2, cy2;
1140 pw = gdk_pixbuf_get_width(pb);
1141 ph = gdk_pixbuf_get_height(pb);
1143 if (!util_clip_region(0, 0, pw, ph,
1144 clip_x, clip_y, clip_w, clip_h,
1145 &rx, &ry, &rw, &rh)) return;
1146 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1147 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1148 &rx1, &ry1, &rx2, &ry2)) return;
1155 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1156 prs = gdk_pixbuf_get_rowstride(pb);
1157 p_pix = gdk_pixbuf_get_pixels(pb);
1159 p_step = (has_alpha) ? 4 : 3;
1161 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1166 t = rx1; rx1 = rx2; rx2 = t;
1167 t = ry1; ry1 = ry2; ry2 = t;
1171 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1172 for (x = rx1; x < rx2; x += 1.0)
1174 px = (gint)(x + 0.5);
1175 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1177 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1179 pp = p_pix + py * prs + px * p_step;
1180 *pp = (r * a + *pp * (256-a)) >> 8;
1182 *pp = (g * a + *pp * (256-a)) >> 8;
1184 *pp = (b * a + *pp * (256-a)) >> 8;
1193 t = rx1; rx1 = rx2; rx2 = t;
1194 t = ry1; ry1 = ry2; ry2 = t;
1198 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1199 for (y = ry1; y < ry2; y += 1.0)
1201 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1202 py = (gint)(y + 0.5);
1204 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1206 pp = p_pix + py * prs + px * p_step;
1207 *pp = (r * a + *pp * (256-a)) >> 8;
1209 *pp = (g * a + *pp * (256-a)) >> 8;
1211 *pp = (b * a + *pp * (256-a)) >> 8;
1218 *-----------------------------------------------------------------------------
1219 * pixbuf drawing (fades and shadows)
1220 *-----------------------------------------------------------------------------
1223 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1224 gint s, gboolean vertical, gint border,
1225 gint x1, gint y1, gint x2, gint y2,
1226 guint8 r, guint8 g, guint8 b, guint8 a)
1233 p_step = (has_alpha) ? 4 : 3;
1234 for (j = y1; j < y2; j++)
1236 pp = p_pix + j * prs + x1 * p_step;
1237 if (!vertical) n = a - a * abs(j - s) / border;
1238 for (i = x1; i < x2; i++)
1240 if (vertical) n = a - a * abs(i - s) / border;
1241 *pp = (r * n + *pp * (256-n)) >> 8;
1243 *pp = (g * n + *pp * (256-n)) >> 8;
1245 *pp = (b * n + *pp * (256-n)) >> 8;
1247 if (has_alpha) pp++;
1252 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1253 gint sx, gint sy, gint border,
1254 gint x1, gint y1, gint x2, gint y2,
1255 guint8 r, guint8 g, guint8 b, guint8 a)
1261 p_step = (has_alpha) ? 4 : 3;
1262 for (j = y1; j < y2; j++)
1264 pp = p_pix + j * prs + x1 * p_step;
1265 for (i = x1; i < x2; i++)
1270 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1271 n = a - a * r / border;
1272 *pp = (r * n + *pp * (256-n)) >> 8;
1274 *pp = (g * n + *pp * (256-n)) >> 8;
1276 *pp = (b * n + *pp * (256-n)) >> 8;
1278 if (has_alpha) pp++;
1283 void pixbuf_draw_shadow(GdkPixbuf *pb,
1284 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1285 gint x, gint y, gint w, gint h, gint border,
1286 guint8 r, guint8 g, guint8 b, guint8 a)
1290 gint rx, ry, rw, rh;
1291 gint fx, fy, fw, fh;
1296 pw = gdk_pixbuf_get_width(pb);
1297 ph = gdk_pixbuf_get_height(pb);
1299 if (!util_clip_region(0, 0, pw, ph,
1300 clip_x, clip_y, clip_w, clip_h,
1301 &rx, &ry, &rw, &rh)) return;
1303 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1304 prs = gdk_pixbuf_get_rowstride(pb);
1305 p_pix = gdk_pixbuf_get_pixels(pb);
1307 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1309 &fx, &fy, &fw, &fh))
1311 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1314 if (border < 1) return;
1316 if (util_clip_region(x, y + border, border, h - border * 2,
1318 &fx, &fy, &fw, &fh))
1320 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1321 x + border, TRUE, border,
1322 fx, fy, fx + fw, fy + fh,
1325 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1327 &fx, &fy, &fw, &fh))
1329 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1330 x + w - border, TRUE, border,
1331 fx, fy, fx + fw, fy + fh,
1334 if (util_clip_region(x + border, y, w - border * 2, border,
1336 &fx, &fy, &fw, &fh))
1338 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1339 y + border, FALSE, border,
1340 fx, fy, fx + fw, fy + fh,
1343 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1345 &fx, &fy, &fw, &fh))
1347 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1348 y + h - border, FALSE, border,
1349 fx, fy, fx + fw, fy + fh,
1352 if (util_clip_region(x, y, border, border,
1354 &fx, &fy, &fw, &fh))
1356 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1357 x + border, y + border, border,
1358 fx, fy, fx + fw, fy + fh,
1361 if (util_clip_region(x + w - border, y, border, border,
1363 &fx, &fy, &fw, &fh))
1365 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1366 x + w - border, y + border, border,
1367 fx, fy, fx + fw, fy + fh,
1370 if (util_clip_region(x, y + h - border, border, border,
1372 &fx, &fy, &fw, &fh))
1374 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1375 x + border, y + h - border, border,
1376 fx, fy, fx + fw, fy + fh,
1379 if (util_clip_region(x + w - border, y + h - border, border, border,
1381 &fx, &fy, &fw, &fh))
1383 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1384 x + w - border, y + h - border, border,
1385 fx, fy, fx + fw, fy + fh,
1392 *-----------------------------------------------------------------------------
1393 * pixbuf color alterations
1394 *-----------------------------------------------------------------------------
1397 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1398 gint x, gint y, gint w, gint h)
1408 pw = gdk_pixbuf_get_width(pb);
1409 ph = gdk_pixbuf_get_height(pb);
1411 if (x < 0 || x + w > pw) return;
1412 if (y < 0 || y + h > ph) return;
1414 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1415 prs = gdk_pixbuf_get_rowstride(pb);
1416 p_pix = gdk_pixbuf_get_pixels(pb);
1418 for (i = 0; i < h; i++)
1420 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1421 for (j = 0; j < w; j++)
1425 grey = (pp[0] + pp[1] + pp[2]) / 3;
1432 if (has_alpha) pp++;
1436 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */