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 },
134 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, icon_draw_rectangle },
135 { PIXBUF_INLINE_ICON_MOVE, icon_move },
136 { PIXBUF_INLINE_ICON_RENAME, icon_rename },
137 { PIXBUF_INLINE_ICON_SELECT_ALL, icon_select_all },
138 { PIXBUF_INLINE_ICON_SELECT_NONE, icon_select_none },
139 { PIXBUF_INLINE_ICON_SELECT_INVERT, icon_select_invert },
140 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, icon_select_rectangle },
141 { PIXBUF_INLINE_ICON_FILE_FILTER, icon_file_filter },
142 { PIXBUF_INLINE_ICON_CW, icon_rotate_clockwise },
143 { PIXBUF_INLINE_ICON_CCW, icon_rotate_counter_clockwise },
144 { PIXBUF_INLINE_ICON_180, icon_rotate_180 },
145 { PIXBUF_INLINE_ICON_MIRROR, icon_mirror },
146 { PIXBUF_INLINE_ICON_FLIP, icon_flip },
147 { PIXBUF_INLINE_ICON_ORIGINAL, icon_original },
148 { PIXBUF_INLINE_ICON_TRASH, icon_trash },
149 { PIXBUF_INLINE_ICON_HEIF, icon_heic },
153 GdkPixbuf *pixbuf_inline(const gchar *key)
157 if (!key) return NULL;
160 while (inline_pixbuf_data[i].key)
162 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
164 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
169 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
174 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
176 static GtkIconFactory *icon_factory = NULL;
177 GtkIconSet *icon_set;
181 icon_factory = gtk_icon_factory_new();
182 gtk_icon_factory_add_default(icon_factory);
185 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
186 gtk_icon_factory_add(icon_factory, key, icon_set);
190 void pixbuf_inline_register_stock_icons(void)
195 while (inline_pixbuf_data[i].key)
197 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
202 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
204 GtkIconTheme *icon_theme;
206 GError *error = NULL;
208 icon_theme = gtk_icon_theme_get_default();
210 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
212 pixbuf = gtk_icon_theme_load_icon(icon_theme,
213 icon, /* icon name */
221 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
226 if (strchr(icon, '.'))
228 /* try again without extension */
229 gchar *icon2 = remove_extension_from_path(icon);
230 pixbuf = gtk_icon_theme_load_icon(icon_theme,
231 icon2, /* icon name */
237 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
241 /* try as an absolute path */
242 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
245 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
253 if (!pixbuf) return FALSE;
255 register_stock_icon(key, pixbuf);
259 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
260 gint old_w, gint old_h,
261 gint *new_w, gint *new_h)
263 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
266 *new_h = (gdouble)*new_w / old_w * old_h;
267 if (*new_h < 1) *new_h = 1;
272 *new_w = (gdouble)*new_h / old_h * old_w;
273 if (*new_w < 1) *new_w = 1;
276 return (*new_w != old_w || *new_h != old_h);
279 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
283 switch (fd->format_class)
285 case FORMAT_CLASS_UNKNOWN:
286 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
288 case FORMAT_CLASS_META:
289 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
291 case FORMAT_CLASS_VIDEO:
292 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
294 case FORMAT_CLASS_COLLECTION:
295 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
297 case FORMAT_CLASS_PDF:
298 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
301 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
304 if (requested_width && requested_height)
306 gint w = gdk_pixbuf_get_width(pixbuf);
307 gint h = gdk_pixbuf_get_height(pixbuf);
309 if (w > requested_width || h > requested_height)
313 if (pixbuf_scale_aspect(requested_width, requested_height,
319 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
320 g_object_unref(G_OBJECT(tmp));
329 *-----------------------------------------------------------------------------
331 *-----------------------------------------------------------------------------
334 gboolean util_clip_region(gint x, gint y, gint w, gint h,
335 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
336 gint *rx, gint *ry, gint *rw, gint *rh)
338 if (clip_x + clip_w <= x ||
340 clip_y + clip_h <= y ||
346 *rx = MAX(x, clip_x);
347 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
349 *ry = MAX(y, clip_y);
350 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
356 *-----------------------------------------------------------------------------
358 *-----------------------------------------------------------------------------
361 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
362 guchar *dest, gint dest_row_stride, gint w, gint h,
363 gint bytes_per_pixel, gboolean counter_clockwise)
369 for (i = 0; i < h; i++)
371 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
372 for (j = 0; j < w; j++)
374 if (counter_clockwise)
376 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
380 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
382 *(dp++) = *(sp++); /* r */
383 *(dp++) = *(sp++); /* g */
384 *(dp++) = *(sp++); /* b */
385 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
391 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
392 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
398 for (i = 0; i < h; i++)
400 sp = src + (i * src_row_stride);
401 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
402 memcpy(dp, sp, w * bytes_per_pixel);
406 #define ROTATE_BUFFER_WIDTH 48
407 #define ROTATE_BUFFER_HEIGHT 48
410 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
413 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
428 if (!src) return NULL;
430 sw = gdk_pixbuf_get_width(src);
431 sh = gdk_pixbuf_get_height(src);
432 has_alpha = gdk_pixbuf_get_has_alpha(src);
433 srs = gdk_pixbuf_get_rowstride(src);
434 s_pix = gdk_pixbuf_get_pixels(src);
438 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
439 drs = gdk_pixbuf_get_rowstride(dest);
440 d_pix = gdk_pixbuf_get_pixels(dest);
442 a = (has_alpha ? 4 : 3);
444 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
445 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
446 b_pix = gdk_pixbuf_get_pixels(buffer);
447 brs = gdk_pixbuf_get_rowstride(buffer);
449 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
451 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
452 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
456 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
457 pixbuf_copy_block_rotate(s_pix, srs, j, i,
459 a, counter_clockwise);
461 if (counter_clockwise)
471 pixbuf_copy_block(b_pix, brs, w, h,
472 d_pix, drs, x, y, a);
476 g_object_unref(buffer);
479 /* this is the simple version of rotation (roughly 2-4x slower) */
481 for (i = 0; i < sh; i++)
483 sp = s_pix + (i * srs);
484 for (j = 0; j < sw; j++)
486 if (counter_clockwise)
488 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
492 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
495 *(dp++) = *(sp++); /* r */
496 *(dp++) = *(sp++); /* g */
497 *(dp++) = *(sp++); /* b */
498 if (has_alpha) *(dp) = *(sp++); /* a */
507 * Returns a copy of pixbuf mirrored and or flipped.
508 * TO do a 180 degree rotations set both mirror and flipped TRUE
509 * if mirror and flip are FALSE, result is a simple copy.
511 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
524 if (!src) return NULL;
526 w = gdk_pixbuf_get_width(src);
527 h = gdk_pixbuf_get_height(src);
528 has_alpha = gdk_pixbuf_get_has_alpha(src);
529 srs = gdk_pixbuf_get_rowstride(src);
530 s_pix = gdk_pixbuf_get_pixels(src);
532 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
533 drs = gdk_pixbuf_get_rowstride(dest);
534 d_pix = gdk_pixbuf_get_pixels(dest);
536 a = has_alpha ? 4 : 3;
538 for (i = 0; i < h; i++)
540 sp = s_pix + (i * srs);
543 dp = d_pix + ((h - i - 1) * drs);
547 dp = d_pix + (i * drs);
552 for (j = 0; j < w; j++)
554 *(dp++) = *(sp++); /* r */
555 *(dp++) = *(sp++); /* g */
556 *(dp++) = *(sp++); /* b */
557 if (has_alpha) *(dp) = *(sp++); /* a */
563 for (j = 0; j < w; j++)
565 *(dp++) = *(sp++); /* r */
566 *(dp++) = *(sp++); /* g */
567 *(dp++) = *(sp++); /* b */
568 if (has_alpha) *(dp++) = *(sp++); /* a */
576 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
579 GdkPixbuf *tmp = NULL;
583 case EXIF_ORIENTATION_TOP_LEFT:
584 dest = gdk_pixbuf_copy(pixbuf);
586 case EXIF_ORIENTATION_TOP_RIGHT:
588 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
590 case EXIF_ORIENTATION_BOTTOM_RIGHT:
592 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
594 case EXIF_ORIENTATION_BOTTOM_LEFT:
596 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
598 case EXIF_ORIENTATION_LEFT_TOP:
599 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
600 dest = pixbuf_copy_rotate_90(tmp, FALSE);
602 case EXIF_ORIENTATION_RIGHT_TOP:
603 /* rotated -90 (270) */
604 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
606 case EXIF_ORIENTATION_RIGHT_BOTTOM:
607 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
608 dest = pixbuf_copy_rotate_90(tmp, TRUE);
610 case EXIF_ORIENTATION_LEFT_BOTTOM:
612 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
615 dest = gdk_pixbuf_copy(pixbuf);
618 if (tmp) g_object_unref(tmp);
625 *-----------------------------------------------------------------------------
626 * pixbuf drawing (rectangles)
627 *-----------------------------------------------------------------------------
631 * Fills region of pixbuf at x,y over w,h
632 * with colors red (r), green (g), blue (b)
633 * applying alpha (a), use a=255 for solid.
635 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
636 gint x, gint y, gint w, gint h,
637 gint r, gint g, gint b, gint a)
647 pw = gdk_pixbuf_get_width(pb);
648 ph = gdk_pixbuf_get_height(pb);
650 if (x < 0 || x + w > pw) return;
651 if (y < 0 || y + h > ph) return;
653 has_alpha = gdk_pixbuf_get_has_alpha(pb);
654 prs = gdk_pixbuf_get_rowstride(pb);
655 p_pix = gdk_pixbuf_get_pixels(pb);
657 for (i = 0; i < h; i++)
659 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
660 for (j = 0; j < w; j++)
662 *pp = (r * a + *pp * (256-a)) >> 8;
664 *pp = (g * a + *pp * (256-a)) >> 8;
666 *pp = (b * a + *pp * (256-a)) >> 8;
673 void pixbuf_draw_rect(GdkPixbuf *pb,
674 gint x, gint y, gint w, gint h,
675 gint r, gint g, gint b, gint a,
676 gint left, gint right, gint top, gint bottom)
678 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
680 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
682 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
684 pixbuf_draw_rect_fill(pb, x, y, left, h,
688 void pixbuf_set_rect_fill(GdkPixbuf *pb,
689 gint x, gint y, gint w, gint h,
690 gint r, gint g, gint b, gint a)
700 pw = gdk_pixbuf_get_width(pb);
701 ph = gdk_pixbuf_get_height(pb);
703 if (x < 0 || x + w > pw) return;
704 if (y < 0 || y + h > ph) return;
706 has_alpha = gdk_pixbuf_get_has_alpha(pb);
707 prs = gdk_pixbuf_get_rowstride(pb);
708 p_pix = gdk_pixbuf_get_pixels(pb);
710 for (i = 0; i < h; i++)
712 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
713 for (j = 0; j < w; j++)
718 if (has_alpha) { *pp = a; pp++; }
723 void pixbuf_set_rect(GdkPixbuf *pb,
724 gint x, gint y, gint w, gint h,
725 gint r, gint g, gint b, gint a,
726 gint left, gint right, gint top, gint bottom)
728 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
730 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
732 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
734 pixbuf_set_rect_fill(pb, x, y, left, h,
738 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
745 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
746 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
748 buf = gdk_pixbuf_get_pixels(pb);
749 has_alpha = gdk_pixbuf_get_has_alpha(pb);
750 rowstride = gdk_pixbuf_get_rowstride(pb);
752 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
756 if (has_alpha) *p = a;
761 *-----------------------------------------------------------------------------
762 * pixbuf text rendering
763 *-----------------------------------------------------------------------------
766 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
767 GdkPixbuf *dest, gint dx, gint dy,
769 guint8 r, guint8 g, guint8 b, guint8 a)
784 if (!src || !dest) return;
786 sw = gdk_pixbuf_get_width(src);
787 sh = gdk_pixbuf_get_height(src);
789 if (sx < 0 || sx + w > sw) return;
790 if (sy < 0 || sy + h > sh) return;
792 dw = gdk_pixbuf_get_width(dest);
793 dh = gdk_pixbuf_get_height(dest);
795 if (dx < 0 || dx + w > dw) return;
796 if (dy < 0 || dy + h > dh) return;
798 s_alpha = gdk_pixbuf_get_has_alpha(src);
799 d_alpha = gdk_pixbuf_get_has_alpha(dest);
800 srs = gdk_pixbuf_get_rowstride(src);
801 drs = gdk_pixbuf_get_rowstride(dest);
802 s_pix = gdk_pixbuf_get_pixels(src);
803 d_pix = gdk_pixbuf_get_pixels(dest);
805 s_step = (s_alpha) ? 4 : 3;
806 d_step = (d_alpha) ? 4 : 3;
808 for (i = 0; i < h; i++)
810 sp = s_pix + (sy + i) * srs + sx * s_step;
811 dp = d_pix + (dy + i) * drs + dx * d_step;
812 for (j = 0; j < w; j++)
818 asub = a * sp[0] / 255;
819 *dp = (r * asub + *dp * (256-asub)) >> 8;
821 asub = a * sp[1] / 255;
822 *dp = (g * asub + *dp * (256-asub)) >> 8;
824 asub = a * sp[2] / 255;
825 *dp = (b * asub + *dp * (256-asub)) >> 8;
830 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
844 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
846 guint8 r, guint8 g, guint8 b, guint8 a)
852 cairo_surface_t *source;
855 pango_layout_get_pixel_size(layout, &w, &h);
856 if (w < 1 || h < 1) return;
858 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
860 cr = cairo_create (source);
861 cairo_set_source_rgb(cr, 0, 0, 0);
862 cairo_rectangle (cr, 0, 0, w, h);
864 cairo_set_source_rgb(cr, 1, 1, 1);
865 pango_cairo_show_layout (cr, layout);
868 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
870 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
872 cairo_image_surface_get_width (source),
873 cairo_image_surface_get_height (source),
874 cairo_image_surface_get_stride (source),
880 dw = gdk_pixbuf_get_width(pixbuf);
881 dh = gdk_pixbuf_get_height(pixbuf);
897 if (x + w > dw) w = dw - x;
898 if (y + h > dh) h = dh - y;
900 pixbuf_copy_font(buffer, sx, sy,
904 g_object_unref(buffer);
905 cairo_surface_destroy(source);
909 *-----------------------------------------------------------------------------
910 * pixbuf drawing (triangle)
911 *-----------------------------------------------------------------------------
914 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
915 gint *rx, gint *ry, gint *rw, gint *rh)
923 tw = MAX(abs(x1 - x2), abs(x2 - x3));
924 tw = MAX(tw, abs(x3 - x1));
925 th = MAX(abs(y1 - y2), abs(y2 - y3));
926 th = MAX(th, abs(y3 - y1));
934 void pixbuf_draw_triangle(GdkPixbuf *pb,
935 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
936 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
937 guint8 r, guint8 g, guint8 b, guint8 a)
949 gdouble slope1, slope2;
950 gint slope1_x, slope1_y;
953 gboolean middle = FALSE;
957 pw = gdk_pixbuf_get_width(pb);
958 ph = gdk_pixbuf_get_height(pb);
960 if (!util_clip_region(0, 0, pw, ph,
961 clip_x, clip_y, clip_w, clip_h,
962 &rx, &ry, &rw, &rh)) return;
964 util_clip_triangle(x1, y1, x2, y2, x3, y3,
967 if (!util_clip_region(rx, ry, rw, rh,
969 &fx1, &fy1, &fw, &fh)) return;
973 has_alpha = gdk_pixbuf_get_has_alpha(pb);
974 prs = gdk_pixbuf_get_rowstride(pb);
975 p_pix = gdk_pixbuf_get_pixels(pb);
977 p_step = (has_alpha) ? 4 : 3;
981 t = x1; x1 = x2; x2 = t;
982 t = y1; y1 = y2; y2 = t;
986 t = x2; x2 = x3; x3 = t;
987 t = y2; y2 = y3; y3 = t;
991 t = x1; x1 = x2; x2 = t;
992 t = y1; y1 = y2; y2 = t;
995 slope1 = (gdouble)(y2 - y1);
996 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
999 slope2 = (gdouble)(y3 - y1);
1000 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
1002 for (y = fy1; y < fy2; y++)
1006 if (!middle && y > y2)
1008 slope1 = (gdouble)(y3 - y2);
1009 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
1016 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
1017 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
1021 t = xa; xa = xb; xb = t;
1024 xa = CLAMP(xa, fx1, fx2);
1025 xb = CLAMP(xb, fx1, fx2);
1027 pp = p_pix + y * prs + xa * p_step;
1031 *pp = (r * a + *pp * (256-a)) >> 8;
1033 *pp = (g * a + *pp * (256-a)) >> 8;
1035 *pp = (b * a + *pp * (256-a)) >> 8;
1037 if (has_alpha) pp++;
1045 *-----------------------------------------------------------------------------
1046 * pixbuf drawing (line)
1047 *-----------------------------------------------------------------------------
1050 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1051 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1052 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1054 gboolean flip = FALSE;
1061 t = x1; x1 = x2; x2 = t;
1062 t = y1; y1 = y2; y2 = t;
1066 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1070 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1074 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1082 slope = (y2 - y1) / d;
1085 y1 = y1 + slope * (clip_x - x1);
1088 if (x2 > clip_x + clip_w)
1090 y2 = y2 + slope * (clip_x + clip_w - x2);
1091 x2 = clip_x + clip_w;
1097 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1103 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1105 t = x1; x1 = x2; x2 = t;
1106 t = y1; y1 = y2; y2 = t;
1115 slope = (x2 - x1) / d;
1118 x1 = x1 + slope * (clip_y - y1);
1121 if (y2 > clip_y + clip_h)
1123 x2 = x2 + slope * (clip_y + clip_h - y2);
1124 y2 = clip_y + clip_h;
1146 void pixbuf_draw_line(GdkPixbuf *pb,
1147 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1148 gint x1, gint y1, gint x2, gint y2,
1149 guint8 r, guint8 g, guint8 b, guint8 a)
1153 gint rx, ry, rw, rh;
1154 gdouble rx1, ry1, rx2, ry2;
1161 gint cx1, cy1, cx2, cy2;
1165 pw = gdk_pixbuf_get_width(pb);
1166 ph = gdk_pixbuf_get_height(pb);
1168 if (!util_clip_region(0, 0, pw, ph,
1169 clip_x, clip_y, clip_w, clip_h,
1170 &rx, &ry, &rw, &rh)) return;
1171 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1172 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1173 &rx1, &ry1, &rx2, &ry2)) return;
1180 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1181 prs = gdk_pixbuf_get_rowstride(pb);
1182 p_pix = gdk_pixbuf_get_pixels(pb);
1184 p_step = (has_alpha) ? 4 : 3;
1186 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1191 t = rx1; rx1 = rx2; rx2 = t;
1192 t = ry1; ry1 = ry2; ry2 = t;
1196 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1197 for (x = rx1; x < rx2; x += 1.0)
1199 px = (gint)(x + 0.5);
1200 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1202 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1204 pp = p_pix + py * prs + px * p_step;
1205 *pp = (r * a + *pp * (256-a)) >> 8;
1207 *pp = (g * a + *pp * (256-a)) >> 8;
1209 *pp = (b * a + *pp * (256-a)) >> 8;
1218 t = rx1; rx1 = rx2; rx2 = t;
1219 t = ry1; ry1 = ry2; ry2 = t;
1223 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1224 for (y = ry1; y < ry2; y += 1.0)
1226 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1227 py = (gint)(y + 0.5);
1229 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1231 pp = p_pix + py * prs + px * p_step;
1232 *pp = (r * a + *pp * (256-a)) >> 8;
1234 *pp = (g * a + *pp * (256-a)) >> 8;
1236 *pp = (b * a + *pp * (256-a)) >> 8;
1243 *-----------------------------------------------------------------------------
1244 * pixbuf drawing (fades and shadows)
1245 *-----------------------------------------------------------------------------
1248 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1249 gint s, gboolean vertical, gint border,
1250 gint x1, gint y1, gint x2, gint y2,
1251 guint8 r, guint8 g, guint8 b, guint8 a)
1258 p_step = (has_alpha) ? 4 : 3;
1259 for (j = y1; j < y2; j++)
1261 pp = p_pix + j * prs + x1 * p_step;
1262 if (!vertical) n = a - a * abs(j - s) / border;
1263 for (i = x1; i < x2; i++)
1265 if (vertical) n = a - a * abs(i - s) / border;
1266 *pp = (r * n + *pp * (256-n)) >> 8;
1268 *pp = (g * n + *pp * (256-n)) >> 8;
1270 *pp = (b * n + *pp * (256-n)) >> 8;
1272 if (has_alpha) pp++;
1277 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1278 gint sx, gint sy, gint border,
1279 gint x1, gint y1, gint x2, gint y2,
1280 guint8 r, guint8 g, guint8 b, guint8 a)
1286 p_step = (has_alpha) ? 4 : 3;
1287 for (j = y1; j < y2; j++)
1289 pp = p_pix + j * prs + x1 * p_step;
1290 for (i = x1; i < x2; i++)
1295 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1296 n = a - a * r / border;
1297 *pp = (r * n + *pp * (256-n)) >> 8;
1299 *pp = (g * n + *pp * (256-n)) >> 8;
1301 *pp = (b * n + *pp * (256-n)) >> 8;
1303 if (has_alpha) pp++;
1308 void pixbuf_draw_shadow(GdkPixbuf *pb,
1309 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1310 gint x, gint y, gint w, gint h, gint border,
1311 guint8 r, guint8 g, guint8 b, guint8 a)
1315 gint rx, ry, rw, rh;
1316 gint fx, fy, fw, fh;
1321 pw = gdk_pixbuf_get_width(pb);
1322 ph = gdk_pixbuf_get_height(pb);
1324 if (!util_clip_region(0, 0, pw, ph,
1325 clip_x, clip_y, clip_w, clip_h,
1326 &rx, &ry, &rw, &rh)) return;
1328 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1329 prs = gdk_pixbuf_get_rowstride(pb);
1330 p_pix = gdk_pixbuf_get_pixels(pb);
1332 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1334 &fx, &fy, &fw, &fh))
1336 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1339 if (border < 1) return;
1341 if (util_clip_region(x, y + border, border, h - border * 2,
1343 &fx, &fy, &fw, &fh))
1345 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1346 x + border, TRUE, border,
1347 fx, fy, fx + fw, fy + fh,
1350 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1352 &fx, &fy, &fw, &fh))
1354 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1355 x + w - border, TRUE, border,
1356 fx, fy, fx + fw, fy + fh,
1359 if (util_clip_region(x + border, y, w - border * 2, border,
1361 &fx, &fy, &fw, &fh))
1363 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1364 y + border, FALSE, border,
1365 fx, fy, fx + fw, fy + fh,
1368 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1370 &fx, &fy, &fw, &fh))
1372 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1373 y + h - border, FALSE, border,
1374 fx, fy, fx + fw, fy + fh,
1377 if (util_clip_region(x, y, border, border,
1379 &fx, &fy, &fw, &fh))
1381 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1382 x + border, y + border, border,
1383 fx, fy, fx + fw, fy + fh,
1386 if (util_clip_region(x + w - border, y, border, border,
1388 &fx, &fy, &fw, &fh))
1390 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1391 x + w - border, y + border, border,
1392 fx, fy, fx + fw, fy + fh,
1395 if (util_clip_region(x, y + h - border, border, border,
1397 &fx, &fy, &fw, &fh))
1399 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1400 x + border, y + h - border, border,
1401 fx, fy, fx + fw, fy + fh,
1404 if (util_clip_region(x + w - border, y + h - border, border, border,
1406 &fx, &fy, &fw, &fh))
1408 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1409 x + w - border, y + h - border, border,
1410 fx, fy, fx + fw, fy + fh,
1417 *-----------------------------------------------------------------------------
1418 * pixbuf color alterations
1419 *-----------------------------------------------------------------------------
1422 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1423 gint x, gint y, gint w, gint h)
1433 pw = gdk_pixbuf_get_width(pb);
1434 ph = gdk_pixbuf_get_height(pb);
1436 if (x < 0 || x + w > pw) return;
1437 if (y < 0 || y + h > ph) return;
1439 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1440 prs = gdk_pixbuf_get_rowstride(pb);
1441 p_pix = gdk_pixbuf_get_pixels(pb);
1443 for (i = 0; i < h; i++)
1445 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1446 for (j = 0; j < w; j++)
1450 grey = (pp[0] + pp[1] + pp[2]) / 3;
1457 if (has_alpha) pp++;
1461 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */