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 },
151 { PIXBUF_INLINE_ICON_EXPOSURE, icon_exposure },
152 { PIXBUF_INLINE_SPLIT_PANE_SYNC, icon_split_pane_sync },
156 GdkPixbuf *pixbuf_inline(const gchar *key)
160 if (!key) return NULL;
163 while (inline_pixbuf_data[i].key)
165 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
167 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
172 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
177 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
179 static GtkIconFactory *icon_factory = NULL;
180 GtkIconSet *icon_set;
184 icon_factory = gtk_icon_factory_new();
185 gtk_icon_factory_add_default(icon_factory);
188 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
189 gtk_icon_factory_add(icon_factory, key, icon_set);
193 void pixbuf_inline_register_stock_icons(void)
198 while (inline_pixbuf_data[i].key)
200 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
205 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
207 GtkIconTheme *icon_theme;
209 GError *error = NULL;
211 icon_theme = gtk_icon_theme_get_default();
213 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
215 pixbuf = gtk_icon_theme_load_icon(icon_theme,
216 icon, /* icon name */
224 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
229 if (strchr(icon, '.'))
231 /* try again without extension */
232 gchar *icon2 = remove_extension_from_path(icon);
233 pixbuf = gtk_icon_theme_load_icon(icon_theme,
234 icon2, /* icon name */
240 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
244 /* try as an absolute path */
245 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
248 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
256 if (!pixbuf) return FALSE;
258 register_stock_icon(key, pixbuf);
262 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
263 gint old_w, gint old_h,
264 gint *new_w, gint *new_h)
266 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
269 *new_h = (gdouble)*new_w / old_w * old_h;
270 if (*new_h < 1) *new_h = 1;
275 *new_w = (gdouble)*new_h / old_h * old_w;
276 if (*new_w < 1) *new_w = 1;
279 return (*new_w != old_w || *new_h != old_h);
282 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
286 switch (fd->format_class)
288 case FORMAT_CLASS_UNKNOWN:
289 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
291 case FORMAT_CLASS_META:
292 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
294 case FORMAT_CLASS_VIDEO:
295 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
297 case FORMAT_CLASS_COLLECTION:
298 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
300 case FORMAT_CLASS_DOCUMENT:
301 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
304 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
307 if (requested_width && requested_height)
309 gint w = gdk_pixbuf_get_width(pixbuf);
310 gint h = gdk_pixbuf_get_height(pixbuf);
312 if (w > requested_width || h > requested_height)
316 if (pixbuf_scale_aspect(requested_width, requested_height,
322 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
323 g_object_unref(G_OBJECT(tmp));
332 *-----------------------------------------------------------------------------
334 *-----------------------------------------------------------------------------
337 gboolean util_clip_region(gint x, gint y, gint w, gint h,
338 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
339 gint *rx, gint *ry, gint *rw, gint *rh)
341 if (clip_x + clip_w <= x ||
343 clip_y + clip_h <= y ||
349 *rx = MAX(x, clip_x);
350 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
352 *ry = MAX(y, clip_y);
353 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
359 *-----------------------------------------------------------------------------
361 *-----------------------------------------------------------------------------
364 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
365 guchar *dest, gint dest_row_stride, gint w, gint h,
366 gint bytes_per_pixel, gboolean counter_clockwise)
372 for (i = 0; i < h; i++)
374 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
375 for (j = 0; j < w; j++)
377 if (counter_clockwise)
379 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
383 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
385 *(dp++) = *(sp++); /* r */
386 *(dp++) = *(sp++); /* g */
387 *(dp++) = *(sp++); /* b */
388 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
394 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
395 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
401 for (i = 0; i < h; i++)
403 sp = src + (i * src_row_stride);
404 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
405 memcpy(dp, sp, w * bytes_per_pixel);
409 #define ROTATE_BUFFER_WIDTH 48
410 #define ROTATE_BUFFER_HEIGHT 48
413 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
416 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
431 if (!src) return NULL;
433 sw = gdk_pixbuf_get_width(src);
434 sh = gdk_pixbuf_get_height(src);
435 has_alpha = gdk_pixbuf_get_has_alpha(src);
436 srs = gdk_pixbuf_get_rowstride(src);
437 s_pix = gdk_pixbuf_get_pixels(src);
441 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
442 drs = gdk_pixbuf_get_rowstride(dest);
443 d_pix = gdk_pixbuf_get_pixels(dest);
445 a = (has_alpha ? 4 : 3);
447 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
448 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
449 b_pix = gdk_pixbuf_get_pixels(buffer);
450 brs = gdk_pixbuf_get_rowstride(buffer);
452 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
454 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
455 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
459 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
460 pixbuf_copy_block_rotate(s_pix, srs, j, i,
462 a, counter_clockwise);
464 if (counter_clockwise)
474 pixbuf_copy_block(b_pix, brs, w, h,
475 d_pix, drs, x, y, a);
479 g_object_unref(buffer);
482 /* this is the simple version of rotation (roughly 2-4x slower) */
484 for (i = 0; i < sh; i++)
486 sp = s_pix + (i * srs);
487 for (j = 0; j < sw; j++)
489 if (counter_clockwise)
491 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
495 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
498 *(dp++) = *(sp++); /* r */
499 *(dp++) = *(sp++); /* g */
500 *(dp++) = *(sp++); /* b */
501 if (has_alpha) *(dp) = *(sp++); /* a */
510 * Returns a copy of pixbuf mirrored and or flipped.
511 * TO do a 180 degree rotations set both mirror and flipped TRUE
512 * if mirror and flip are FALSE, result is a simple copy.
514 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
527 if (!src) return NULL;
529 w = gdk_pixbuf_get_width(src);
530 h = gdk_pixbuf_get_height(src);
531 has_alpha = gdk_pixbuf_get_has_alpha(src);
532 srs = gdk_pixbuf_get_rowstride(src);
533 s_pix = gdk_pixbuf_get_pixels(src);
535 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
536 drs = gdk_pixbuf_get_rowstride(dest);
537 d_pix = gdk_pixbuf_get_pixels(dest);
539 a = has_alpha ? 4 : 3;
541 for (i = 0; i < h; i++)
543 sp = s_pix + (i * srs);
546 dp = d_pix + ((h - i - 1) * drs);
550 dp = d_pix + (i * drs);
555 for (j = 0; j < w; j++)
557 *(dp++) = *(sp++); /* r */
558 *(dp++) = *(sp++); /* g */
559 *(dp++) = *(sp++); /* b */
560 if (has_alpha) *(dp) = *(sp++); /* a */
566 for (j = 0; j < w; j++)
568 *(dp++) = *(sp++); /* r */
569 *(dp++) = *(sp++); /* g */
570 *(dp++) = *(sp++); /* b */
571 if (has_alpha) *(dp++) = *(sp++); /* a */
579 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
582 GdkPixbuf *tmp = NULL;
586 case EXIF_ORIENTATION_TOP_LEFT:
587 dest = gdk_pixbuf_copy(pixbuf);
589 case EXIF_ORIENTATION_TOP_RIGHT:
591 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
593 case EXIF_ORIENTATION_BOTTOM_RIGHT:
595 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
597 case EXIF_ORIENTATION_BOTTOM_LEFT:
599 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
601 case EXIF_ORIENTATION_LEFT_TOP:
602 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
603 dest = pixbuf_copy_rotate_90(tmp, FALSE);
605 case EXIF_ORIENTATION_RIGHT_TOP:
606 /* rotated -90 (270) */
607 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
609 case EXIF_ORIENTATION_RIGHT_BOTTOM:
610 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
611 dest = pixbuf_copy_rotate_90(tmp, TRUE);
613 case EXIF_ORIENTATION_LEFT_BOTTOM:
615 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
618 dest = gdk_pixbuf_copy(pixbuf);
621 if (tmp) g_object_unref(tmp);
628 *-----------------------------------------------------------------------------
629 * pixbuf drawing (rectangles)
630 *-----------------------------------------------------------------------------
634 * Fills region of pixbuf at x,y over w,h
635 * with colors red (r), green (g), blue (b)
636 * applying alpha (a), use a=255 for solid.
638 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
639 gint x, gint y, gint w, gint h,
640 gint r, gint g, gint b, gint a)
650 pw = gdk_pixbuf_get_width(pb);
651 ph = gdk_pixbuf_get_height(pb);
653 if (x < 0 || x + w > pw) return;
654 if (y < 0 || y + h > ph) return;
656 has_alpha = gdk_pixbuf_get_has_alpha(pb);
657 prs = gdk_pixbuf_get_rowstride(pb);
658 p_pix = gdk_pixbuf_get_pixels(pb);
660 for (i = 0; i < h; i++)
662 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
663 for (j = 0; j < w; j++)
665 *pp = (r * a + *pp * (256-a)) >> 8;
667 *pp = (g * a + *pp * (256-a)) >> 8;
669 *pp = (b * a + *pp * (256-a)) >> 8;
676 void pixbuf_draw_rect(GdkPixbuf *pb,
677 gint x, gint y, gint w, gint h,
678 gint r, gint g, gint b, gint a,
679 gint left, gint right, gint top, gint bottom)
681 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
683 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
685 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
687 pixbuf_draw_rect_fill(pb, x, y, left, h,
691 void pixbuf_set_rect_fill(GdkPixbuf *pb,
692 gint x, gint y, gint w, gint h,
693 gint r, gint g, gint b, gint a)
703 pw = gdk_pixbuf_get_width(pb);
704 ph = gdk_pixbuf_get_height(pb);
706 if (x < 0 || x + w > pw) return;
707 if (y < 0 || y + h > ph) return;
709 has_alpha = gdk_pixbuf_get_has_alpha(pb);
710 prs = gdk_pixbuf_get_rowstride(pb);
711 p_pix = gdk_pixbuf_get_pixels(pb);
713 for (i = 0; i < h; i++)
715 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
716 for (j = 0; j < w; j++)
721 if (has_alpha) { *pp = a; pp++; }
726 void pixbuf_set_rect(GdkPixbuf *pb,
727 gint x, gint y, gint w, gint h,
728 gint r, gint g, gint b, gint a,
729 gint left, gint right, gint top, gint bottom)
731 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
733 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
735 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
737 pixbuf_set_rect_fill(pb, x, y, left, h,
741 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
748 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
749 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
751 buf = gdk_pixbuf_get_pixels(pb);
752 has_alpha = gdk_pixbuf_get_has_alpha(pb);
753 rowstride = gdk_pixbuf_get_rowstride(pb);
755 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
759 if (has_alpha) *p = a;
764 *-----------------------------------------------------------------------------
765 * pixbuf text rendering
766 *-----------------------------------------------------------------------------
769 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
770 GdkPixbuf *dest, gint dx, gint dy,
772 guint8 r, guint8 g, guint8 b, guint8 a)
787 if (!src || !dest) return;
789 sw = gdk_pixbuf_get_width(src);
790 sh = gdk_pixbuf_get_height(src);
792 if (sx < 0 || sx + w > sw) return;
793 if (sy < 0 || sy + h > sh) return;
795 dw = gdk_pixbuf_get_width(dest);
796 dh = gdk_pixbuf_get_height(dest);
798 if (dx < 0 || dx + w > dw) return;
799 if (dy < 0 || dy + h > dh) return;
801 s_alpha = gdk_pixbuf_get_has_alpha(src);
802 d_alpha = gdk_pixbuf_get_has_alpha(dest);
803 srs = gdk_pixbuf_get_rowstride(src);
804 drs = gdk_pixbuf_get_rowstride(dest);
805 s_pix = gdk_pixbuf_get_pixels(src);
806 d_pix = gdk_pixbuf_get_pixels(dest);
808 s_step = (s_alpha) ? 4 : 3;
809 d_step = (d_alpha) ? 4 : 3;
811 for (i = 0; i < h; i++)
813 sp = s_pix + (sy + i) * srs + sx * s_step;
814 dp = d_pix + (dy + i) * drs + dx * d_step;
815 for (j = 0; j < w; j++)
821 asub = a * sp[0] / 255;
822 *dp = (r * asub + *dp * (256-asub)) >> 8;
824 asub = a * sp[1] / 255;
825 *dp = (g * asub + *dp * (256-asub)) >> 8;
827 asub = a * sp[2] / 255;
828 *dp = (b * asub + *dp * (256-asub)) >> 8;
833 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
847 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
849 guint8 r, guint8 g, guint8 b, guint8 a)
855 cairo_surface_t *source;
858 pango_layout_get_pixel_size(layout, &w, &h);
859 if (w < 1 || h < 1) return;
861 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
863 cr = cairo_create (source);
864 cairo_set_source_rgb(cr, 0, 0, 0);
865 cairo_rectangle (cr, 0, 0, w, h);
867 cairo_set_source_rgb(cr, 1, 1, 1);
868 pango_cairo_show_layout (cr, layout);
871 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
873 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
875 cairo_image_surface_get_width (source),
876 cairo_image_surface_get_height (source),
877 cairo_image_surface_get_stride (source),
883 dw = gdk_pixbuf_get_width(pixbuf);
884 dh = gdk_pixbuf_get_height(pixbuf);
900 if (x + w > dw) w = dw - x;
901 if (y + h > dh) h = dh - y;
903 pixbuf_copy_font(buffer, sx, sy,
907 g_object_unref(buffer);
908 cairo_surface_destroy(source);
912 *-----------------------------------------------------------------------------
913 * pixbuf drawing (triangle)
914 *-----------------------------------------------------------------------------
917 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
918 gint *rx, gint *ry, gint *rw, gint *rh)
926 tw = MAX(abs(x1 - x2), abs(x2 - x3));
927 tw = MAX(tw, abs(x3 - x1));
928 th = MAX(abs(y1 - y2), abs(y2 - y3));
929 th = MAX(th, abs(y3 - y1));
937 void pixbuf_draw_triangle(GdkPixbuf *pb,
938 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
939 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
940 guint8 r, guint8 g, guint8 b, guint8 a)
952 gdouble slope1, slope2;
953 gint slope1_x, slope1_y;
956 gboolean middle = FALSE;
960 pw = gdk_pixbuf_get_width(pb);
961 ph = gdk_pixbuf_get_height(pb);
963 if (!util_clip_region(0, 0, pw, ph,
964 clip_x, clip_y, clip_w, clip_h,
965 &rx, &ry, &rw, &rh)) return;
967 util_clip_triangle(x1, y1, x2, y2, x3, y3,
970 if (!util_clip_region(rx, ry, rw, rh,
972 &fx1, &fy1, &fw, &fh)) return;
976 has_alpha = gdk_pixbuf_get_has_alpha(pb);
977 prs = gdk_pixbuf_get_rowstride(pb);
978 p_pix = gdk_pixbuf_get_pixels(pb);
980 p_step = (has_alpha) ? 4 : 3;
984 t = x1; x1 = x2; x2 = t;
985 t = y1; y1 = y2; y2 = t;
989 t = x2; x2 = x3; x3 = t;
990 t = y2; y2 = y3; y3 = t;
994 t = x1; x1 = x2; x2 = t;
995 t = y1; y1 = y2; y2 = t;
998 slope1 = (gdouble)(y2 - y1);
999 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
1002 slope2 = (gdouble)(y3 - y1);
1003 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
1005 for (y = fy1; y < fy2; y++)
1009 if (!middle && y > y2)
1011 slope1 = (gdouble)(y3 - y2);
1012 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
1019 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
1020 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
1024 t = xa; xa = xb; xb = t;
1027 xa = CLAMP(xa, fx1, fx2);
1028 xb = CLAMP(xb, fx1, fx2);
1030 pp = p_pix + y * prs + xa * p_step;
1034 *pp = (r * a + *pp * (256-a)) >> 8;
1036 *pp = (g * a + *pp * (256-a)) >> 8;
1038 *pp = (b * a + *pp * (256-a)) >> 8;
1040 if (has_alpha) pp++;
1048 *-----------------------------------------------------------------------------
1049 * pixbuf drawing (line)
1050 *-----------------------------------------------------------------------------
1053 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1054 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1055 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1057 gboolean flip = FALSE;
1064 t = x1; x1 = x2; x2 = t;
1065 t = y1; y1 = y2; y2 = t;
1069 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1073 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1077 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1085 slope = (y2 - y1) / d;
1088 y1 = y1 + slope * (clip_x - x1);
1091 if (x2 > clip_x + clip_w)
1093 y2 = y2 + slope * (clip_x + clip_w - x2);
1094 x2 = clip_x + clip_w;
1100 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1106 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1108 t = x1; x1 = x2; x2 = t;
1109 t = y1; y1 = y2; y2 = t;
1118 slope = (x2 - x1) / d;
1121 x1 = x1 + slope * (clip_y - y1);
1124 if (y2 > clip_y + clip_h)
1126 x2 = x2 + slope * (clip_y + clip_h - y2);
1127 y2 = clip_y + clip_h;
1149 void pixbuf_draw_line(GdkPixbuf *pb,
1150 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1151 gint x1, gint y1, gint x2, gint y2,
1152 guint8 r, guint8 g, guint8 b, guint8 a)
1156 gint rx, ry, rw, rh;
1157 gdouble rx1, ry1, rx2, ry2;
1164 gint cx1, cy1, cx2, cy2;
1168 pw = gdk_pixbuf_get_width(pb);
1169 ph = gdk_pixbuf_get_height(pb);
1171 if (!util_clip_region(0, 0, pw, ph,
1172 clip_x, clip_y, clip_w, clip_h,
1173 &rx, &ry, &rw, &rh)) return;
1174 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1175 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1176 &rx1, &ry1, &rx2, &ry2)) return;
1183 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1184 prs = gdk_pixbuf_get_rowstride(pb);
1185 p_pix = gdk_pixbuf_get_pixels(pb);
1187 p_step = (has_alpha) ? 4 : 3;
1189 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1194 t = rx1; rx1 = rx2; rx2 = t;
1195 t = ry1; ry1 = ry2; ry2 = t;
1199 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1200 for (x = rx1; x < rx2; x += 1.0)
1202 px = (gint)(x + 0.5);
1203 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1205 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1207 pp = p_pix + py * prs + px * p_step;
1208 *pp = (r * a + *pp * (256-a)) >> 8;
1210 *pp = (g * a + *pp * (256-a)) >> 8;
1212 *pp = (b * a + *pp * (256-a)) >> 8;
1221 t = rx1; rx1 = rx2; rx2 = t;
1222 t = ry1; ry1 = ry2; ry2 = t;
1226 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1227 for (y = ry1; y < ry2; y += 1.0)
1229 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1230 py = (gint)(y + 0.5);
1232 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1234 pp = p_pix + py * prs + px * p_step;
1235 *pp = (r * a + *pp * (256-a)) >> 8;
1237 *pp = (g * a + *pp * (256-a)) >> 8;
1239 *pp = (b * a + *pp * (256-a)) >> 8;
1246 *-----------------------------------------------------------------------------
1247 * pixbuf drawing (fades and shadows)
1248 *-----------------------------------------------------------------------------
1251 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1252 gint s, gboolean vertical, gint border,
1253 gint x1, gint y1, gint x2, gint y2,
1254 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 if (!vertical) n = a - a * abs(j - s) / border;
1266 for (i = x1; i < x2; i++)
1268 if (vertical) n = a - a * abs(i - s) / border;
1269 *pp = (r * n + *pp * (256-n)) >> 8;
1271 *pp = (g * n + *pp * (256-n)) >> 8;
1273 *pp = (b * n + *pp * (256-n)) >> 8;
1275 if (has_alpha) pp++;
1280 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1281 gint sx, gint sy, gint border,
1282 gint x1, gint y1, gint x2, gint y2,
1283 guint8 r, guint8 g, guint8 b, guint8 a)
1289 p_step = (has_alpha) ? 4 : 3;
1290 for (j = y1; j < y2; j++)
1292 pp = p_pix + j * prs + x1 * p_step;
1293 for (i = x1; i < x2; i++)
1298 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1299 n = a - a * r / border;
1300 *pp = (r * n + *pp * (256-n)) >> 8;
1302 *pp = (g * n + *pp * (256-n)) >> 8;
1304 *pp = (b * n + *pp * (256-n)) >> 8;
1306 if (has_alpha) pp++;
1311 void pixbuf_draw_shadow(GdkPixbuf *pb,
1312 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1313 gint x, gint y, gint w, gint h, gint border,
1314 guint8 r, guint8 g, guint8 b, guint8 a)
1318 gint rx, ry, rw, rh;
1319 gint fx, fy, fw, fh;
1324 pw = gdk_pixbuf_get_width(pb);
1325 ph = gdk_pixbuf_get_height(pb);
1327 if (!util_clip_region(0, 0, pw, ph,
1328 clip_x, clip_y, clip_w, clip_h,
1329 &rx, &ry, &rw, &rh)) return;
1331 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1332 prs = gdk_pixbuf_get_rowstride(pb);
1333 p_pix = gdk_pixbuf_get_pixels(pb);
1335 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1337 &fx, &fy, &fw, &fh))
1339 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1342 if (border < 1) return;
1344 if (util_clip_region(x, y + border, border, h - border * 2,
1346 &fx, &fy, &fw, &fh))
1348 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1349 x + border, TRUE, border,
1350 fx, fy, fx + fw, fy + fh,
1353 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1355 &fx, &fy, &fw, &fh))
1357 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1358 x + w - border, TRUE, border,
1359 fx, fy, fx + fw, fy + fh,
1362 if (util_clip_region(x + border, y, w - border * 2, border,
1364 &fx, &fy, &fw, &fh))
1366 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1367 y + border, FALSE, border,
1368 fx, fy, fx + fw, fy + fh,
1371 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1373 &fx, &fy, &fw, &fh))
1375 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1376 y + h - border, FALSE, border,
1377 fx, fy, fx + fw, fy + fh,
1380 if (util_clip_region(x, y, border, border,
1382 &fx, &fy, &fw, &fh))
1384 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1385 x + border, y + border, border,
1386 fx, fy, fx + fw, fy + fh,
1389 if (util_clip_region(x + w - border, y, border, border,
1391 &fx, &fy, &fw, &fh))
1393 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1394 x + w - border, y + border, border,
1395 fx, fy, fx + fw, fy + fh,
1398 if (util_clip_region(x, y + h - border, border, border,
1400 &fx, &fy, &fw, &fh))
1402 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1403 x + border, y + h - border, border,
1404 fx, fy, fx + fw, fy + fh,
1407 if (util_clip_region(x + w - border, y + h - border, border, border,
1409 &fx, &fy, &fw, &fh))
1411 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1412 x + w - border, y + h - border, border,
1413 fx, fy, fx + fw, fy + fh,
1420 *-----------------------------------------------------------------------------
1421 * pixbuf color alterations
1422 *-----------------------------------------------------------------------------
1425 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1426 gint x, gint y, gint w, gint h)
1436 pw = gdk_pixbuf_get_width(pb);
1437 ph = gdk_pixbuf_get_height(pb);
1439 if (x < 0 || x + w > pw) return;
1440 if (y < 0 || y + h > ph) return;
1442 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1443 prs = gdk_pixbuf_get_rowstride(pb);
1444 p_pix = gdk_pixbuf_get_pixels(pb);
1446 for (i = 0; i < h; i++)
1448 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1449 for (j = 0; j < w; j++)
1453 grey = (pp[0] + pp[1] + pp[2]) / 3;
1460 if (has_alpha) pp++;
1466 *-----------------------------------------------------------------------------
1467 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1469 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1479 pw = gdk_pixbuf_get_width(pb);
1480 ph = gdk_pixbuf_get_height(pb);
1482 if (x < 0 || x + w > pw) return;
1483 if (y < 0 || y + h > ph) return;
1485 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1486 prs = gdk_pixbuf_get_rowstride(pb);
1487 p_pix = gdk_pixbuf_get_pixels(pb);
1489 for (i = 0; i < h; i++)
1491 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1492 for (j = 0; j < w; j++)
1494 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1502 if (has_alpha) pp++;
1507 if (has_alpha) pp++;
1514 *-----------------------------------------------------------------------------
1515 * pixbuf ignore alpha
1516 *-----------------------------------------------------------------------------
1518 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1519 gint x, gint y, gint w, gint h)
1529 pw = gdk_pixbuf_get_width(pb);
1530 ph = gdk_pixbuf_get_height(pb);
1532 if (x < 0 || x + w > pw) return;
1533 if (y < 0 || y + h > ph) return;
1535 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1536 if (!has_alpha) return;
1538 prs = gdk_pixbuf_get_rowstride(pb);
1539 p_pix = gdk_pixbuf_get_pixels(pb);
1541 for (i = 0; i < h; i++)
1543 pp = p_pix + (y + i) * prs + (x * 4 );
1544 for (j = 0; j < w; j++)
1551 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */