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 },
150 { PIXBUF_INLINE_ICON_GRAYSCALE, icon_grayscale },
154 GdkPixbuf *pixbuf_inline(const gchar *key)
158 if (!key) return NULL;
161 while (inline_pixbuf_data[i].key)
163 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
165 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
170 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
175 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
177 static GtkIconFactory *icon_factory = NULL;
178 GtkIconSet *icon_set;
182 icon_factory = gtk_icon_factory_new();
183 gtk_icon_factory_add_default(icon_factory);
186 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
187 gtk_icon_factory_add(icon_factory, key, icon_set);
191 void pixbuf_inline_register_stock_icons(void)
196 while (inline_pixbuf_data[i].key)
198 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
203 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
205 GtkIconTheme *icon_theme;
207 GError *error = NULL;
209 icon_theme = gtk_icon_theme_get_default();
211 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
213 pixbuf = gtk_icon_theme_load_icon(icon_theme,
214 icon, /* icon name */
222 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
227 if (strchr(icon, '.'))
229 /* try again without extension */
230 gchar *icon2 = remove_extension_from_path(icon);
231 pixbuf = gtk_icon_theme_load_icon(icon_theme,
232 icon2, /* icon name */
238 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
242 /* try as an absolute path */
243 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
246 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
254 if (!pixbuf) return FALSE;
256 register_stock_icon(key, pixbuf);
260 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
261 gint old_w, gint old_h,
262 gint *new_w, gint *new_h)
264 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
267 *new_h = (gdouble)*new_w / old_w * old_h;
268 if (*new_h < 1) *new_h = 1;
273 *new_w = (gdouble)*new_h / old_h * old_w;
274 if (*new_w < 1) *new_w = 1;
277 return (*new_w != old_w || *new_h != old_h);
280 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
284 switch (fd->format_class)
286 case FORMAT_CLASS_UNKNOWN:
287 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
289 case FORMAT_CLASS_META:
290 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
292 case FORMAT_CLASS_VIDEO:
293 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
295 case FORMAT_CLASS_COLLECTION:
296 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
298 case FORMAT_CLASS_PDF:
299 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
302 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
305 if (requested_width && requested_height)
307 gint w = gdk_pixbuf_get_width(pixbuf);
308 gint h = gdk_pixbuf_get_height(pixbuf);
310 if (w > requested_width || h > requested_height)
314 if (pixbuf_scale_aspect(requested_width, requested_height,
320 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
321 g_object_unref(G_OBJECT(tmp));
330 *-----------------------------------------------------------------------------
332 *-----------------------------------------------------------------------------
335 gboolean util_clip_region(gint x, gint y, gint w, gint h,
336 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
337 gint *rx, gint *ry, gint *rw, gint *rh)
339 if (clip_x + clip_w <= x ||
341 clip_y + clip_h <= y ||
347 *rx = MAX(x, clip_x);
348 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
350 *ry = MAX(y, clip_y);
351 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
357 *-----------------------------------------------------------------------------
359 *-----------------------------------------------------------------------------
362 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
363 guchar *dest, gint dest_row_stride, gint w, gint h,
364 gint bytes_per_pixel, gboolean counter_clockwise)
370 for (i = 0; i < h; i++)
372 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
373 for (j = 0; j < w; j++)
375 if (counter_clockwise)
377 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
381 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
383 *(dp++) = *(sp++); /* r */
384 *(dp++) = *(sp++); /* g */
385 *(dp++) = *(sp++); /* b */
386 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
392 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
393 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
399 for (i = 0; i < h; i++)
401 sp = src + (i * src_row_stride);
402 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
403 memcpy(dp, sp, w * bytes_per_pixel);
407 #define ROTATE_BUFFER_WIDTH 48
408 #define ROTATE_BUFFER_HEIGHT 48
411 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
414 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
429 if (!src) return NULL;
431 sw = gdk_pixbuf_get_width(src);
432 sh = gdk_pixbuf_get_height(src);
433 has_alpha = gdk_pixbuf_get_has_alpha(src);
434 srs = gdk_pixbuf_get_rowstride(src);
435 s_pix = gdk_pixbuf_get_pixels(src);
439 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
440 drs = gdk_pixbuf_get_rowstride(dest);
441 d_pix = gdk_pixbuf_get_pixels(dest);
443 a = (has_alpha ? 4 : 3);
445 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
446 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
447 b_pix = gdk_pixbuf_get_pixels(buffer);
448 brs = gdk_pixbuf_get_rowstride(buffer);
450 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
452 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
453 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
457 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
458 pixbuf_copy_block_rotate(s_pix, srs, j, i,
460 a, counter_clockwise);
462 if (counter_clockwise)
472 pixbuf_copy_block(b_pix, brs, w, h,
473 d_pix, drs, x, y, a);
477 g_object_unref(buffer);
480 /* this is the simple version of rotation (roughly 2-4x slower) */
482 for (i = 0; i < sh; i++)
484 sp = s_pix + (i * srs);
485 for (j = 0; j < sw; j++)
487 if (counter_clockwise)
489 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
493 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
496 *(dp++) = *(sp++); /* r */
497 *(dp++) = *(sp++); /* g */
498 *(dp++) = *(sp++); /* b */
499 if (has_alpha) *(dp) = *(sp++); /* a */
508 * Returns a copy of pixbuf mirrored and or flipped.
509 * TO do a 180 degree rotations set both mirror and flipped TRUE
510 * if mirror and flip are FALSE, result is a simple copy.
512 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
525 if (!src) return NULL;
527 w = gdk_pixbuf_get_width(src);
528 h = gdk_pixbuf_get_height(src);
529 has_alpha = gdk_pixbuf_get_has_alpha(src);
530 srs = gdk_pixbuf_get_rowstride(src);
531 s_pix = gdk_pixbuf_get_pixels(src);
533 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
534 drs = gdk_pixbuf_get_rowstride(dest);
535 d_pix = gdk_pixbuf_get_pixels(dest);
537 a = has_alpha ? 4 : 3;
539 for (i = 0; i < h; i++)
541 sp = s_pix + (i * srs);
544 dp = d_pix + ((h - i - 1) * drs);
548 dp = d_pix + (i * drs);
553 for (j = 0; j < w; j++)
555 *(dp++) = *(sp++); /* r */
556 *(dp++) = *(sp++); /* g */
557 *(dp++) = *(sp++); /* b */
558 if (has_alpha) *(dp) = *(sp++); /* a */
564 for (j = 0; j < w; j++)
566 *(dp++) = *(sp++); /* r */
567 *(dp++) = *(sp++); /* g */
568 *(dp++) = *(sp++); /* b */
569 if (has_alpha) *(dp++) = *(sp++); /* a */
577 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
580 GdkPixbuf *tmp = NULL;
584 case EXIF_ORIENTATION_TOP_LEFT:
585 dest = gdk_pixbuf_copy(pixbuf);
587 case EXIF_ORIENTATION_TOP_RIGHT:
589 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
591 case EXIF_ORIENTATION_BOTTOM_RIGHT:
593 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
595 case EXIF_ORIENTATION_BOTTOM_LEFT:
597 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
599 case EXIF_ORIENTATION_LEFT_TOP:
600 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
601 dest = pixbuf_copy_rotate_90(tmp, FALSE);
603 case EXIF_ORIENTATION_RIGHT_TOP:
604 /* rotated -90 (270) */
605 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
607 case EXIF_ORIENTATION_RIGHT_BOTTOM:
608 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
609 dest = pixbuf_copy_rotate_90(tmp, TRUE);
611 case EXIF_ORIENTATION_LEFT_BOTTOM:
613 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
616 dest = gdk_pixbuf_copy(pixbuf);
619 if (tmp) g_object_unref(tmp);
626 *-----------------------------------------------------------------------------
627 * pixbuf drawing (rectangles)
628 *-----------------------------------------------------------------------------
632 * Fills region of pixbuf at x,y over w,h
633 * with colors red (r), green (g), blue (b)
634 * applying alpha (a), use a=255 for solid.
636 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
637 gint x, gint y, gint w, gint h,
638 gint r, gint g, gint b, gint a)
648 pw = gdk_pixbuf_get_width(pb);
649 ph = gdk_pixbuf_get_height(pb);
651 if (x < 0 || x + w > pw) return;
652 if (y < 0 || y + h > ph) return;
654 has_alpha = gdk_pixbuf_get_has_alpha(pb);
655 prs = gdk_pixbuf_get_rowstride(pb);
656 p_pix = gdk_pixbuf_get_pixels(pb);
658 for (i = 0; i < h; i++)
660 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
661 for (j = 0; j < w; j++)
663 *pp = (r * a + *pp * (256-a)) >> 8;
665 *pp = (g * a + *pp * (256-a)) >> 8;
667 *pp = (b * a + *pp * (256-a)) >> 8;
674 void pixbuf_draw_rect(GdkPixbuf *pb,
675 gint x, gint y, gint w, gint h,
676 gint r, gint g, gint b, gint a,
677 gint left, gint right, gint top, gint bottom)
679 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
681 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
683 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
685 pixbuf_draw_rect_fill(pb, x, y, left, h,
689 void pixbuf_set_rect_fill(GdkPixbuf *pb,
690 gint x, gint y, gint w, gint h,
691 gint r, gint g, gint b, gint a)
701 pw = gdk_pixbuf_get_width(pb);
702 ph = gdk_pixbuf_get_height(pb);
704 if (x < 0 || x + w > pw) return;
705 if (y < 0 || y + h > ph) return;
707 has_alpha = gdk_pixbuf_get_has_alpha(pb);
708 prs = gdk_pixbuf_get_rowstride(pb);
709 p_pix = gdk_pixbuf_get_pixels(pb);
711 for (i = 0; i < h; i++)
713 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
714 for (j = 0; j < w; j++)
719 if (has_alpha) { *pp = a; pp++; }
724 void pixbuf_set_rect(GdkPixbuf *pb,
725 gint x, gint y, gint w, gint h,
726 gint r, gint g, gint b, gint a,
727 gint left, gint right, gint top, gint bottom)
729 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
731 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
733 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
735 pixbuf_set_rect_fill(pb, x, y, left, h,
739 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
746 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
747 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
749 buf = gdk_pixbuf_get_pixels(pb);
750 has_alpha = gdk_pixbuf_get_has_alpha(pb);
751 rowstride = gdk_pixbuf_get_rowstride(pb);
753 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
757 if (has_alpha) *p = a;
762 *-----------------------------------------------------------------------------
763 * pixbuf text rendering
764 *-----------------------------------------------------------------------------
767 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
768 GdkPixbuf *dest, gint dx, gint dy,
770 guint8 r, guint8 g, guint8 b, guint8 a)
785 if (!src || !dest) return;
787 sw = gdk_pixbuf_get_width(src);
788 sh = gdk_pixbuf_get_height(src);
790 if (sx < 0 || sx + w > sw) return;
791 if (sy < 0 || sy + h > sh) return;
793 dw = gdk_pixbuf_get_width(dest);
794 dh = gdk_pixbuf_get_height(dest);
796 if (dx < 0 || dx + w > dw) return;
797 if (dy < 0 || dy + h > dh) return;
799 s_alpha = gdk_pixbuf_get_has_alpha(src);
800 d_alpha = gdk_pixbuf_get_has_alpha(dest);
801 srs = gdk_pixbuf_get_rowstride(src);
802 drs = gdk_pixbuf_get_rowstride(dest);
803 s_pix = gdk_pixbuf_get_pixels(src);
804 d_pix = gdk_pixbuf_get_pixels(dest);
806 s_step = (s_alpha) ? 4 : 3;
807 d_step = (d_alpha) ? 4 : 3;
809 for (i = 0; i < h; i++)
811 sp = s_pix + (sy + i) * srs + sx * s_step;
812 dp = d_pix + (dy + i) * drs + dx * d_step;
813 for (j = 0; j < w; j++)
819 asub = a * sp[0] / 255;
820 *dp = (r * asub + *dp * (256-asub)) >> 8;
822 asub = a * sp[1] / 255;
823 *dp = (g * asub + *dp * (256-asub)) >> 8;
825 asub = a * sp[2] / 255;
826 *dp = (b * asub + *dp * (256-asub)) >> 8;
831 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
845 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
847 guint8 r, guint8 g, guint8 b, guint8 a)
853 cairo_surface_t *source;
856 pango_layout_get_pixel_size(layout, &w, &h);
857 if (w < 1 || h < 1) return;
859 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
861 cr = cairo_create (source);
862 cairo_set_source_rgb(cr, 0, 0, 0);
863 cairo_rectangle (cr, 0, 0, w, h);
865 cairo_set_source_rgb(cr, 1, 1, 1);
866 pango_cairo_show_layout (cr, layout);
869 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
871 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
873 cairo_image_surface_get_width (source),
874 cairo_image_surface_get_height (source),
875 cairo_image_surface_get_stride (source),
881 dw = gdk_pixbuf_get_width(pixbuf);
882 dh = gdk_pixbuf_get_height(pixbuf);
898 if (x + w > dw) w = dw - x;
899 if (y + h > dh) h = dh - y;
901 pixbuf_copy_font(buffer, sx, sy,
905 g_object_unref(buffer);
906 cairo_surface_destroy(source);
910 *-----------------------------------------------------------------------------
911 * pixbuf drawing (triangle)
912 *-----------------------------------------------------------------------------
915 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
916 gint *rx, gint *ry, gint *rw, gint *rh)
924 tw = MAX(abs(x1 - x2), abs(x2 - x3));
925 tw = MAX(tw, abs(x3 - x1));
926 th = MAX(abs(y1 - y2), abs(y2 - y3));
927 th = MAX(th, abs(y3 - y1));
935 void pixbuf_draw_triangle(GdkPixbuf *pb,
936 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
937 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
938 guint8 r, guint8 g, guint8 b, guint8 a)
950 gdouble slope1, slope2;
951 gint slope1_x, slope1_y;
954 gboolean middle = FALSE;
958 pw = gdk_pixbuf_get_width(pb);
959 ph = gdk_pixbuf_get_height(pb);
961 if (!util_clip_region(0, 0, pw, ph,
962 clip_x, clip_y, clip_w, clip_h,
963 &rx, &ry, &rw, &rh)) return;
965 util_clip_triangle(x1, y1, x2, y2, x3, y3,
968 if (!util_clip_region(rx, ry, rw, rh,
970 &fx1, &fy1, &fw, &fh)) return;
974 has_alpha = gdk_pixbuf_get_has_alpha(pb);
975 prs = gdk_pixbuf_get_rowstride(pb);
976 p_pix = gdk_pixbuf_get_pixels(pb);
978 p_step = (has_alpha) ? 4 : 3;
982 t = x1; x1 = x2; x2 = t;
983 t = y1; y1 = y2; y2 = t;
987 t = x2; x2 = x3; x3 = t;
988 t = y2; y2 = y3; y3 = t;
992 t = x1; x1 = x2; x2 = t;
993 t = y1; y1 = y2; y2 = t;
996 slope1 = (gdouble)(y2 - y1);
997 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
1000 slope2 = (gdouble)(y3 - y1);
1001 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
1003 for (y = fy1; y < fy2; y++)
1007 if (!middle && y > y2)
1009 slope1 = (gdouble)(y3 - y2);
1010 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
1017 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
1018 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
1022 t = xa; xa = xb; xb = t;
1025 xa = CLAMP(xa, fx1, fx2);
1026 xb = CLAMP(xb, fx1, fx2);
1028 pp = p_pix + y * prs + xa * p_step;
1032 *pp = (r * a + *pp * (256-a)) >> 8;
1034 *pp = (g * a + *pp * (256-a)) >> 8;
1036 *pp = (b * a + *pp * (256-a)) >> 8;
1038 if (has_alpha) pp++;
1046 *-----------------------------------------------------------------------------
1047 * pixbuf drawing (line)
1048 *-----------------------------------------------------------------------------
1051 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1052 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1053 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1055 gboolean flip = FALSE;
1062 t = x1; x1 = x2; x2 = t;
1063 t = y1; y1 = y2; y2 = t;
1067 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1071 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1075 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1083 slope = (y2 - y1) / d;
1086 y1 = y1 + slope * (clip_x - x1);
1089 if (x2 > clip_x + clip_w)
1091 y2 = y2 + slope * (clip_x + clip_w - x2);
1092 x2 = clip_x + clip_w;
1098 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1104 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1106 t = x1; x1 = x2; x2 = t;
1107 t = y1; y1 = y2; y2 = t;
1116 slope = (x2 - x1) / d;
1119 x1 = x1 + slope * (clip_y - y1);
1122 if (y2 > clip_y + clip_h)
1124 x2 = x2 + slope * (clip_y + clip_h - y2);
1125 y2 = clip_y + clip_h;
1147 void pixbuf_draw_line(GdkPixbuf *pb,
1148 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1149 gint x1, gint y1, gint x2, gint y2,
1150 guint8 r, guint8 g, guint8 b, guint8 a)
1154 gint rx, ry, rw, rh;
1155 gdouble rx1, ry1, rx2, ry2;
1162 gint cx1, cy1, cx2, cy2;
1166 pw = gdk_pixbuf_get_width(pb);
1167 ph = gdk_pixbuf_get_height(pb);
1169 if (!util_clip_region(0, 0, pw, ph,
1170 clip_x, clip_y, clip_w, clip_h,
1171 &rx, &ry, &rw, &rh)) return;
1172 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1173 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1174 &rx1, &ry1, &rx2, &ry2)) return;
1181 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1182 prs = gdk_pixbuf_get_rowstride(pb);
1183 p_pix = gdk_pixbuf_get_pixels(pb);
1185 p_step = (has_alpha) ? 4 : 3;
1187 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1192 t = rx1; rx1 = rx2; rx2 = t;
1193 t = ry1; ry1 = ry2; ry2 = t;
1197 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1198 for (x = rx1; x < rx2; x += 1.0)
1200 px = (gint)(x + 0.5);
1201 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1203 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1205 pp = p_pix + py * prs + px * p_step;
1206 *pp = (r * a + *pp * (256-a)) >> 8;
1208 *pp = (g * a + *pp * (256-a)) >> 8;
1210 *pp = (b * a + *pp * (256-a)) >> 8;
1219 t = rx1; rx1 = rx2; rx2 = t;
1220 t = ry1; ry1 = ry2; ry2 = t;
1224 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1225 for (y = ry1; y < ry2; y += 1.0)
1227 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1228 py = (gint)(y + 0.5);
1230 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1232 pp = p_pix + py * prs + px * p_step;
1233 *pp = (r * a + *pp * (256-a)) >> 8;
1235 *pp = (g * a + *pp * (256-a)) >> 8;
1237 *pp = (b * a + *pp * (256-a)) >> 8;
1244 *-----------------------------------------------------------------------------
1245 * pixbuf drawing (fades and shadows)
1246 *-----------------------------------------------------------------------------
1249 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1250 gint s, gboolean vertical, gint border,
1251 gint x1, gint y1, gint x2, gint y2,
1252 guint8 r, guint8 g, guint8 b, guint8 a)
1259 p_step = (has_alpha) ? 4 : 3;
1260 for (j = y1; j < y2; j++)
1262 pp = p_pix + j * prs + x1 * p_step;
1263 if (!vertical) n = a - a * abs(j - s) / border;
1264 for (i = x1; i < x2; i++)
1266 if (vertical) n = a - a * abs(i - s) / border;
1267 *pp = (r * n + *pp * (256-n)) >> 8;
1269 *pp = (g * n + *pp * (256-n)) >> 8;
1271 *pp = (b * n + *pp * (256-n)) >> 8;
1273 if (has_alpha) pp++;
1278 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1279 gint sx, gint sy, gint border,
1280 gint x1, gint y1, gint x2, gint y2,
1281 guint8 r, guint8 g, guint8 b, guint8 a)
1287 p_step = (has_alpha) ? 4 : 3;
1288 for (j = y1; j < y2; j++)
1290 pp = p_pix + j * prs + x1 * p_step;
1291 for (i = x1; i < x2; i++)
1296 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1297 n = a - a * r / border;
1298 *pp = (r * n + *pp * (256-n)) >> 8;
1300 *pp = (g * n + *pp * (256-n)) >> 8;
1302 *pp = (b * n + *pp * (256-n)) >> 8;
1304 if (has_alpha) pp++;
1309 void pixbuf_draw_shadow(GdkPixbuf *pb,
1310 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1311 gint x, gint y, gint w, gint h, gint border,
1312 guint8 r, guint8 g, guint8 b, guint8 a)
1316 gint rx, ry, rw, rh;
1317 gint fx, fy, fw, fh;
1322 pw = gdk_pixbuf_get_width(pb);
1323 ph = gdk_pixbuf_get_height(pb);
1325 if (!util_clip_region(0, 0, pw, ph,
1326 clip_x, clip_y, clip_w, clip_h,
1327 &rx, &ry, &rw, &rh)) return;
1329 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1330 prs = gdk_pixbuf_get_rowstride(pb);
1331 p_pix = gdk_pixbuf_get_pixels(pb);
1333 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1335 &fx, &fy, &fw, &fh))
1337 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1340 if (border < 1) return;
1342 if (util_clip_region(x, y + border, border, h - border * 2,
1344 &fx, &fy, &fw, &fh))
1346 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1347 x + border, TRUE, border,
1348 fx, fy, fx + fw, fy + fh,
1351 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1353 &fx, &fy, &fw, &fh))
1355 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1356 x + w - border, TRUE, border,
1357 fx, fy, fx + fw, fy + fh,
1360 if (util_clip_region(x + border, y, w - border * 2, border,
1362 &fx, &fy, &fw, &fh))
1364 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1365 y + border, FALSE, border,
1366 fx, fy, fx + fw, fy + fh,
1369 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1371 &fx, &fy, &fw, &fh))
1373 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1374 y + h - border, FALSE, border,
1375 fx, fy, fx + fw, fy + fh,
1378 if (util_clip_region(x, y, border, border,
1380 &fx, &fy, &fw, &fh))
1382 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1383 x + border, y + border, border,
1384 fx, fy, fx + fw, fy + fh,
1387 if (util_clip_region(x + w - border, y, border, border,
1389 &fx, &fy, &fw, &fh))
1391 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1392 x + w - border, y + border, border,
1393 fx, fy, fx + fw, fy + fh,
1396 if (util_clip_region(x, y + h - border, border, border,
1398 &fx, &fy, &fw, &fh))
1400 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1401 x + border, y + h - border, border,
1402 fx, fy, fx + fw, fy + fh,
1405 if (util_clip_region(x + w - border, y + h - border, border, border,
1407 &fx, &fy, &fw, &fh))
1409 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1410 x + w - border, y + h - border, border,
1411 fx, fy, fx + fw, fy + fh,
1418 *-----------------------------------------------------------------------------
1419 * pixbuf color alterations
1420 *-----------------------------------------------------------------------------
1423 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1424 gint x, gint y, gint w, gint h)
1434 pw = gdk_pixbuf_get_width(pb);
1435 ph = gdk_pixbuf_get_height(pb);
1437 if (x < 0 || x + w > pw) return;
1438 if (y < 0 || y + h > ph) return;
1440 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1441 prs = gdk_pixbuf_get_rowstride(pb);
1442 p_pix = gdk_pixbuf_get_pixels(pb);
1444 for (i = 0; i < h; i++)
1446 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1447 for (j = 0; j < w; j++)
1451 grey = (pp[0] + pp[1] + pp[2]) / 3;
1458 if (has_alpha) pp++;
1462 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */