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 },
151 GdkPixbuf *pixbuf_inline(const gchar *key)
155 if (!key) return NULL;
158 while (inline_pixbuf_data[i].key)
160 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
162 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
167 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
172 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
174 static GtkIconFactory *icon_factory = NULL;
175 GtkIconSet *icon_set;
179 icon_factory = gtk_icon_factory_new();
180 gtk_icon_factory_add_default(icon_factory);
183 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
184 gtk_icon_factory_add(icon_factory, key, icon_set);
188 void pixbuf_inline_register_stock_icons(void)
193 while (inline_pixbuf_data[i].key)
195 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
200 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
202 GtkIconTheme *icon_theme;
204 GError *error = NULL;
206 icon_theme = gtk_icon_theme_get_default();
208 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
210 pixbuf = gtk_icon_theme_load_icon(icon_theme,
211 icon, /* icon name */
219 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
224 if (strchr(icon, '.'))
226 /* try again without extension */
227 gchar *icon2 = remove_extension_from_path(icon);
228 pixbuf = gtk_icon_theme_load_icon(icon_theme,
229 icon2, /* icon name */
235 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
242 if (!pixbuf) return FALSE;
244 register_stock_icon(key, pixbuf);
248 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
249 gint old_w, gint old_h,
250 gint *new_w, gint *new_h)
252 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
255 *new_h = (gdouble)*new_w / old_w * old_h;
256 if (*new_h < 1) *new_h = 1;
261 *new_w = (gdouble)*new_h / old_h * old_w;
262 if (*new_w < 1) *new_w = 1;
265 return (*new_w != old_w || *new_h != old_h);
268 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
272 switch (fd->format_class)
274 case FORMAT_CLASS_UNKNOWN:
275 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
277 case FORMAT_CLASS_META:
278 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
280 case FORMAT_CLASS_VIDEO:
281 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
283 case FORMAT_CLASS_COLLECTION:
284 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
286 case FORMAT_CLASS_PDF:
287 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
290 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
293 if (requested_width && requested_height)
295 gint w = gdk_pixbuf_get_width(pixbuf);
296 gint h = gdk_pixbuf_get_height(pixbuf);
298 if (w > requested_width || h > requested_height)
302 if (pixbuf_scale_aspect(requested_width, requested_height,
308 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
309 g_object_unref(G_OBJECT(tmp));
318 *-----------------------------------------------------------------------------
320 *-----------------------------------------------------------------------------
323 gboolean util_clip_region(gint x, gint y, gint w, gint h,
324 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
325 gint *rx, gint *ry, gint *rw, gint *rh)
327 if (clip_x + clip_w <= x ||
329 clip_y + clip_h <= y ||
335 *rx = MAX(x, clip_x);
336 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
338 *ry = MAX(y, clip_y);
339 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
345 *-----------------------------------------------------------------------------
347 *-----------------------------------------------------------------------------
350 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
351 guchar *dest, gint dest_row_stride, gint w, gint h,
352 gint bytes_per_pixel, gboolean counter_clockwise)
358 for (i = 0; i < h; i++)
360 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
361 for (j = 0; j < w; j++)
363 if (counter_clockwise)
365 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
369 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
371 *(dp++) = *(sp++); /* r */
372 *(dp++) = *(sp++); /* g */
373 *(dp++) = *(sp++); /* b */
374 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
380 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
381 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
387 for (i = 0; i < h; i++)
389 sp = src + (i * src_row_stride);
390 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
391 memcpy(dp, sp, w * bytes_per_pixel);
395 #define ROTATE_BUFFER_WIDTH 48
396 #define ROTATE_BUFFER_HEIGHT 48
399 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
402 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
417 if (!src) return NULL;
419 sw = gdk_pixbuf_get_width(src);
420 sh = gdk_pixbuf_get_height(src);
421 has_alpha = gdk_pixbuf_get_has_alpha(src);
422 srs = gdk_pixbuf_get_rowstride(src);
423 s_pix = gdk_pixbuf_get_pixels(src);
427 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
428 drs = gdk_pixbuf_get_rowstride(dest);
429 d_pix = gdk_pixbuf_get_pixels(dest);
431 a = (has_alpha ? 4 : 3);
433 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
434 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
435 b_pix = gdk_pixbuf_get_pixels(buffer);
436 brs = gdk_pixbuf_get_rowstride(buffer);
438 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
440 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
441 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
445 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
446 pixbuf_copy_block_rotate(s_pix, srs, j, i,
448 a, counter_clockwise);
450 if (counter_clockwise)
460 pixbuf_copy_block(b_pix, brs, w, h,
461 d_pix, drs, x, y, a);
465 g_object_unref(buffer);
468 /* this is the simple version of rotation (roughly 2-4x slower) */
470 for (i = 0; i < sh; i++)
472 sp = s_pix + (i * srs);
473 for (j = 0; j < sw; j++)
475 if (counter_clockwise)
477 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
481 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
484 *(dp++) = *(sp++); /* r */
485 *(dp++) = *(sp++); /* g */
486 *(dp++) = *(sp++); /* b */
487 if (has_alpha) *(dp) = *(sp++); /* a */
496 * Returns a copy of pixbuf mirrored and or flipped.
497 * TO do a 180 degree rotations set both mirror and flipped TRUE
498 * if mirror and flip are FALSE, result is a simple copy.
500 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
513 if (!src) return NULL;
515 w = gdk_pixbuf_get_width(src);
516 h = gdk_pixbuf_get_height(src);
517 has_alpha = gdk_pixbuf_get_has_alpha(src);
518 srs = gdk_pixbuf_get_rowstride(src);
519 s_pix = gdk_pixbuf_get_pixels(src);
521 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
522 drs = gdk_pixbuf_get_rowstride(dest);
523 d_pix = gdk_pixbuf_get_pixels(dest);
525 a = has_alpha ? 4 : 3;
527 for (i = 0; i < h; i++)
529 sp = s_pix + (i * srs);
532 dp = d_pix + ((h - i - 1) * drs);
536 dp = d_pix + (i * drs);
541 for (j = 0; j < w; j++)
543 *(dp++) = *(sp++); /* r */
544 *(dp++) = *(sp++); /* g */
545 *(dp++) = *(sp++); /* b */
546 if (has_alpha) *(dp) = *(sp++); /* a */
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 */
565 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
568 GdkPixbuf *tmp = NULL;
572 case EXIF_ORIENTATION_TOP_LEFT:
573 dest = gdk_pixbuf_copy(pixbuf);
575 case EXIF_ORIENTATION_TOP_RIGHT:
577 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
579 case EXIF_ORIENTATION_BOTTOM_RIGHT:
581 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
583 case EXIF_ORIENTATION_BOTTOM_LEFT:
585 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
587 case EXIF_ORIENTATION_LEFT_TOP:
588 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
589 dest = pixbuf_copy_rotate_90(tmp, FALSE);
591 case EXIF_ORIENTATION_RIGHT_TOP:
592 /* rotated -90 (270) */
593 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
595 case EXIF_ORIENTATION_RIGHT_BOTTOM:
596 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
597 dest = pixbuf_copy_rotate_90(tmp, TRUE);
599 case EXIF_ORIENTATION_LEFT_BOTTOM:
601 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
604 dest = gdk_pixbuf_copy(pixbuf);
607 if (tmp) g_object_unref(tmp);
614 *-----------------------------------------------------------------------------
615 * pixbuf drawing (rectangles)
616 *-----------------------------------------------------------------------------
620 * Fills region of pixbuf at x,y over w,h
621 * with colors red (r), green (g), blue (b)
622 * applying alpha (a), use a=255 for solid.
624 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
625 gint x, gint y, gint w, gint h,
626 gint r, gint g, gint b, gint a)
636 pw = gdk_pixbuf_get_width(pb);
637 ph = gdk_pixbuf_get_height(pb);
639 if (x < 0 || x + w > pw) return;
640 if (y < 0 || y + h > ph) return;
642 has_alpha = gdk_pixbuf_get_has_alpha(pb);
643 prs = gdk_pixbuf_get_rowstride(pb);
644 p_pix = gdk_pixbuf_get_pixels(pb);
646 for (i = 0; i < h; i++)
648 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
649 for (j = 0; j < w; j++)
651 *pp = (r * a + *pp * (256-a)) >> 8;
653 *pp = (g * a + *pp * (256-a)) >> 8;
655 *pp = (b * a + *pp * (256-a)) >> 8;
662 void pixbuf_draw_rect(GdkPixbuf *pb,
663 gint x, gint y, gint w, gint h,
664 gint r, gint g, gint b, gint a,
665 gint left, gint right, gint top, gint bottom)
667 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
669 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
671 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
673 pixbuf_draw_rect_fill(pb, x, y, left, h,
677 void pixbuf_set_rect_fill(GdkPixbuf *pb,
678 gint x, gint y, gint w, gint h,
679 gint r, gint g, gint b, gint a)
689 pw = gdk_pixbuf_get_width(pb);
690 ph = gdk_pixbuf_get_height(pb);
692 if (x < 0 || x + w > pw) return;
693 if (y < 0 || y + h > ph) return;
695 has_alpha = gdk_pixbuf_get_has_alpha(pb);
696 prs = gdk_pixbuf_get_rowstride(pb);
697 p_pix = gdk_pixbuf_get_pixels(pb);
699 for (i = 0; i < h; i++)
701 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
702 for (j = 0; j < w; j++)
707 if (has_alpha) { *pp = a; pp++; }
712 void pixbuf_set_rect(GdkPixbuf *pb,
713 gint x, gint y, gint w, gint h,
714 gint r, gint g, gint b, gint a,
715 gint left, gint right, gint top, gint bottom)
717 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
719 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
721 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
723 pixbuf_set_rect_fill(pb, x, y, left, h,
727 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
734 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
735 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
737 buf = gdk_pixbuf_get_pixels(pb);
738 has_alpha = gdk_pixbuf_get_has_alpha(pb);
739 rowstride = gdk_pixbuf_get_rowstride(pb);
741 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
745 if (has_alpha) *p = a;
750 *-----------------------------------------------------------------------------
751 * pixbuf text rendering
752 *-----------------------------------------------------------------------------
755 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
756 GdkPixbuf *dest, gint dx, gint dy,
758 guint8 r, guint8 g, guint8 b, guint8 a)
773 if (!src || !dest) return;
775 sw = gdk_pixbuf_get_width(src);
776 sh = gdk_pixbuf_get_height(src);
778 if (sx < 0 || sx + w > sw) return;
779 if (sy < 0 || sy + h > sh) return;
781 dw = gdk_pixbuf_get_width(dest);
782 dh = gdk_pixbuf_get_height(dest);
784 if (dx < 0 || dx + w > dw) return;
785 if (dy < 0 || dy + h > dh) return;
787 s_alpha = gdk_pixbuf_get_has_alpha(src);
788 d_alpha = gdk_pixbuf_get_has_alpha(dest);
789 srs = gdk_pixbuf_get_rowstride(src);
790 drs = gdk_pixbuf_get_rowstride(dest);
791 s_pix = gdk_pixbuf_get_pixels(src);
792 d_pix = gdk_pixbuf_get_pixels(dest);
794 s_step = (s_alpha) ? 4 : 3;
795 d_step = (d_alpha) ? 4 : 3;
797 for (i = 0; i < h; i++)
799 sp = s_pix + (sy + i) * srs + sx * s_step;
800 dp = d_pix + (dy + i) * drs + dx * d_step;
801 for (j = 0; j < w; j++)
807 asub = a * sp[0] / 255;
808 *dp = (r * asub + *dp * (256-asub)) >> 8;
810 asub = a * sp[1] / 255;
811 *dp = (g * asub + *dp * (256-asub)) >> 8;
813 asub = a * sp[2] / 255;
814 *dp = (b * asub + *dp * (256-asub)) >> 8;
819 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
833 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
835 guint8 r, guint8 g, guint8 b, guint8 a)
841 cairo_surface_t *source;
844 pango_layout_get_pixel_size(layout, &w, &h);
845 if (w < 1 || h < 1) return;
847 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
849 cr = cairo_create (source);
850 cairo_set_source_rgb(cr, 0, 0, 0);
851 cairo_rectangle (cr, 0, 0, w, h);
853 cairo_set_source_rgb(cr, 1, 1, 1);
854 pango_cairo_show_layout (cr, layout);
857 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
859 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
861 cairo_image_surface_get_width (source),
862 cairo_image_surface_get_height (source),
863 cairo_image_surface_get_stride (source),
869 dw = gdk_pixbuf_get_width(pixbuf);
870 dh = gdk_pixbuf_get_height(pixbuf);
886 if (x + w > dw) w = dw - x;
887 if (y + h > dh) h = dh - y;
889 pixbuf_copy_font(buffer, sx, sy,
893 g_object_unref(buffer);
894 cairo_surface_destroy(source);
898 *-----------------------------------------------------------------------------
899 * pixbuf drawing (triangle)
900 *-----------------------------------------------------------------------------
903 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
904 gint *rx, gint *ry, gint *rw, gint *rh)
912 tw = MAX(abs(x1 - x2), abs(x2 - x3));
913 tw = MAX(tw, abs(x3 - x1));
914 th = MAX(abs(y1 - y2), abs(y2 - y3));
915 th = MAX(th, abs(y3 - y1));
923 void pixbuf_draw_triangle(GdkPixbuf *pb,
924 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
925 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
926 guint8 r, guint8 g, guint8 b, guint8 a)
938 gdouble slope1, slope2;
939 gint slope1_x, slope1_y;
942 gboolean middle = FALSE;
946 pw = gdk_pixbuf_get_width(pb);
947 ph = gdk_pixbuf_get_height(pb);
949 if (!util_clip_region(0, 0, pw, ph,
950 clip_x, clip_y, clip_w, clip_h,
951 &rx, &ry, &rw, &rh)) return;
953 util_clip_triangle(x1, y1, x2, y2, x3, y3,
956 if (!util_clip_region(rx, ry, rw, rh,
958 &fx1, &fy1, &fw, &fh)) return;
962 has_alpha = gdk_pixbuf_get_has_alpha(pb);
963 prs = gdk_pixbuf_get_rowstride(pb);
964 p_pix = gdk_pixbuf_get_pixels(pb);
966 p_step = (has_alpha) ? 4 : 3;
970 t = x1; x1 = x2; x2 = t;
971 t = y1; y1 = y2; y2 = t;
975 t = x2; x2 = x3; x3 = t;
976 t = y2; y2 = y3; y3 = t;
980 t = x1; x1 = x2; x2 = t;
981 t = y1; y1 = y2; y2 = t;
984 slope1 = (gdouble)(y2 - y1);
985 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
988 slope2 = (gdouble)(y3 - y1);
989 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
991 for (y = fy1; y < fy2; y++)
995 if (!middle && y > y2)
997 slope1 = (gdouble)(y3 - y2);
998 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
1005 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
1006 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
1010 t = xa; xa = xb; xb = t;
1013 xa = CLAMP(xa, fx1, fx2);
1014 xb = CLAMP(xb, fx1, fx2);
1016 pp = p_pix + y * prs + xa * p_step;
1020 *pp = (r * a + *pp * (256-a)) >> 8;
1022 *pp = (g * a + *pp * (256-a)) >> 8;
1024 *pp = (b * a + *pp * (256-a)) >> 8;
1026 if (has_alpha) pp++;
1034 *-----------------------------------------------------------------------------
1035 * pixbuf drawing (line)
1036 *-----------------------------------------------------------------------------
1039 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1040 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1041 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1043 gboolean flip = FALSE;
1050 t = x1; x1 = x2; x2 = t;
1051 t = y1; y1 = y2; y2 = t;
1055 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1059 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1063 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1071 slope = (y2 - y1) / d;
1074 y1 = y1 + slope * (clip_x - x1);
1077 if (x2 > clip_x + clip_w)
1079 y2 = y2 + slope * (clip_x + clip_w - x2);
1080 x2 = clip_x + clip_w;
1086 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1092 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1094 t = x1; x1 = x2; x2 = t;
1095 t = y1; y1 = y2; y2 = t;
1104 slope = (x2 - x1) / d;
1107 x1 = x1 + slope * (clip_y - y1);
1110 if (y2 > clip_y + clip_h)
1112 x2 = x2 + slope * (clip_y + clip_h - y2);
1113 y2 = clip_y + clip_h;
1135 void pixbuf_draw_line(GdkPixbuf *pb,
1136 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1137 gint x1, gint y1, gint x2, gint y2,
1138 guint8 r, guint8 g, guint8 b, guint8 a)
1142 gint rx, ry, rw, rh;
1143 gdouble rx1, ry1, rx2, ry2;
1150 gint cx1, cy1, cx2, cy2;
1154 pw = gdk_pixbuf_get_width(pb);
1155 ph = gdk_pixbuf_get_height(pb);
1157 if (!util_clip_region(0, 0, pw, ph,
1158 clip_x, clip_y, clip_w, clip_h,
1159 &rx, &ry, &rw, &rh)) return;
1160 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1161 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1162 &rx1, &ry1, &rx2, &ry2)) return;
1169 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1170 prs = gdk_pixbuf_get_rowstride(pb);
1171 p_pix = gdk_pixbuf_get_pixels(pb);
1173 p_step = (has_alpha) ? 4 : 3;
1175 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1180 t = rx1; rx1 = rx2; rx2 = t;
1181 t = ry1; ry1 = ry2; ry2 = t;
1185 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1186 for (x = rx1; x < rx2; x += 1.0)
1188 px = (gint)(x + 0.5);
1189 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1191 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1193 pp = p_pix + py * prs + px * p_step;
1194 *pp = (r * a + *pp * (256-a)) >> 8;
1196 *pp = (g * a + *pp * (256-a)) >> 8;
1198 *pp = (b * a + *pp * (256-a)) >> 8;
1207 t = rx1; rx1 = rx2; rx2 = t;
1208 t = ry1; ry1 = ry2; ry2 = t;
1212 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1213 for (y = ry1; y < ry2; y += 1.0)
1215 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1216 py = (gint)(y + 0.5);
1218 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1220 pp = p_pix + py * prs + px * p_step;
1221 *pp = (r * a + *pp * (256-a)) >> 8;
1223 *pp = (g * a + *pp * (256-a)) >> 8;
1225 *pp = (b * a + *pp * (256-a)) >> 8;
1232 *-----------------------------------------------------------------------------
1233 * pixbuf drawing (fades and shadows)
1234 *-----------------------------------------------------------------------------
1237 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1238 gint s, gboolean vertical, gint border,
1239 gint x1, gint y1, gint x2, gint y2,
1240 guint8 r, guint8 g, guint8 b, guint8 a)
1247 p_step = (has_alpha) ? 4 : 3;
1248 for (j = y1; j < y2; j++)
1250 pp = p_pix + j * prs + x1 * p_step;
1251 if (!vertical) n = a - a * abs(j - s) / border;
1252 for (i = x1; i < x2; i++)
1254 if (vertical) n = a - a * abs(i - s) / border;
1255 *pp = (r * n + *pp * (256-n)) >> 8;
1257 *pp = (g * n + *pp * (256-n)) >> 8;
1259 *pp = (b * n + *pp * (256-n)) >> 8;
1261 if (has_alpha) pp++;
1266 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1267 gint sx, gint sy, gint border,
1268 gint x1, gint y1, gint x2, gint y2,
1269 guint8 r, guint8 g, guint8 b, guint8 a)
1275 p_step = (has_alpha) ? 4 : 3;
1276 for (j = y1; j < y2; j++)
1278 pp = p_pix + j * prs + x1 * p_step;
1279 for (i = x1; i < x2; i++)
1284 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1285 n = a - a * r / border;
1286 *pp = (r * n + *pp * (256-n)) >> 8;
1288 *pp = (g * n + *pp * (256-n)) >> 8;
1290 *pp = (b * n + *pp * (256-n)) >> 8;
1292 if (has_alpha) pp++;
1297 void pixbuf_draw_shadow(GdkPixbuf *pb,
1298 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1299 gint x, gint y, gint w, gint h, gint border,
1300 guint8 r, guint8 g, guint8 b, guint8 a)
1304 gint rx, ry, rw, rh;
1305 gint fx, fy, fw, fh;
1310 pw = gdk_pixbuf_get_width(pb);
1311 ph = gdk_pixbuf_get_height(pb);
1313 if (!util_clip_region(0, 0, pw, ph,
1314 clip_x, clip_y, clip_w, clip_h,
1315 &rx, &ry, &rw, &rh)) return;
1317 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1318 prs = gdk_pixbuf_get_rowstride(pb);
1319 p_pix = gdk_pixbuf_get_pixels(pb);
1321 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1323 &fx, &fy, &fw, &fh))
1325 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1328 if (border < 1) return;
1330 if (util_clip_region(x, y + border, border, h - border * 2,
1332 &fx, &fy, &fw, &fh))
1334 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1335 x + border, TRUE, border,
1336 fx, fy, fx + fw, fy + fh,
1339 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1341 &fx, &fy, &fw, &fh))
1343 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1344 x + w - border, TRUE, border,
1345 fx, fy, fx + fw, fy + fh,
1348 if (util_clip_region(x + border, y, w - border * 2, border,
1350 &fx, &fy, &fw, &fh))
1352 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1353 y + border, FALSE, border,
1354 fx, fy, fx + fw, fy + fh,
1357 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1359 &fx, &fy, &fw, &fh))
1361 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1362 y + h - border, FALSE, border,
1363 fx, fy, fx + fw, fy + fh,
1366 if (util_clip_region(x, y, border, border,
1368 &fx, &fy, &fw, &fh))
1370 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1371 x + border, y + border, border,
1372 fx, fy, fx + fw, fy + fh,
1375 if (util_clip_region(x + w - border, y, border, border,
1377 &fx, &fy, &fw, &fh))
1379 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1380 x + w - border, y + border, border,
1381 fx, fy, fx + fw, fy + fh,
1384 if (util_clip_region(x, y + h - border, border, border,
1386 &fx, &fy, &fw, &fh))
1388 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1389 x + border, y + h - border, border,
1390 fx, fy, fx + fw, fy + fh,
1393 if (util_clip_region(x + w - border, y + h - border, border, border,
1395 &fx, &fy, &fw, &fh))
1397 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1398 x + w - border, y + h - border, border,
1399 fx, fy, fx + fw, fy + fh,
1406 *-----------------------------------------------------------------------------
1407 * pixbuf color alterations
1408 *-----------------------------------------------------------------------------
1411 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1412 gint x, gint y, gint w, gint h)
1422 pw = gdk_pixbuf_get_width(pb);
1423 ph = gdk_pixbuf_get_height(pb);
1425 if (x < 0 || x + w > pw) return;
1426 if (y < 0 || y + h > ph) return;
1428 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1429 prs = gdk_pixbuf_get_rowstride(pb);
1430 p_pix = gdk_pixbuf_get_pixels(pb);
1432 for (i = 0; i < h; i++)
1434 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1435 for (j = 0; j < w; j++)
1439 grey = (pp[0] + pp[1] + pp[2]) / 3;
1446 if (has_alpha) pp++;
1450 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */