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_ARCHIVE, icon_archive },
118 { PIXBUF_INLINE_ICON_FLOAT, icon_float },
119 { PIXBUF_INLINE_ICON_THUMB, icon_thumb },
120 { PIXBUF_INLINE_ICON_BOOK, icon_book },
121 { PIXBUF_INLINE_ICON_CONFIG, icon_config },
122 { PIXBUF_INLINE_ICON_TOOLS, icon_tools },
123 { PIXBUF_INLINE_ICON_VIEW, icon_view },
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);
303 case FORMAT_CLASS_ARCHIVE:
304 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
307 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
310 if (requested_width && requested_height)
312 gint w = gdk_pixbuf_get_width(pixbuf);
313 gint h = gdk_pixbuf_get_height(pixbuf);
315 if (w > requested_width || h > requested_height)
319 if (pixbuf_scale_aspect(requested_width, requested_height,
325 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
326 g_object_unref(G_OBJECT(tmp));
335 *-----------------------------------------------------------------------------
337 *-----------------------------------------------------------------------------
340 gboolean util_clip_region(gint x, gint y, gint w, gint h,
341 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
342 gint *rx, gint *ry, gint *rw, gint *rh)
344 if (clip_x + clip_w <= x ||
346 clip_y + clip_h <= y ||
352 *rx = MAX(x, clip_x);
353 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
355 *ry = MAX(y, clip_y);
356 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
362 *-----------------------------------------------------------------------------
364 *-----------------------------------------------------------------------------
367 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
368 guchar *dest, gint dest_row_stride, gint w, gint h,
369 gint bytes_per_pixel, gboolean counter_clockwise)
375 for (i = 0; i < h; i++)
377 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
378 for (j = 0; j < w; j++)
380 if (counter_clockwise)
382 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
386 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
388 *(dp++) = *(sp++); /* r */
389 *(dp++) = *(sp++); /* g */
390 *(dp++) = *(sp++); /* b */
391 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
397 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
398 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
404 for (i = 0; i < h; i++)
406 sp = src + (i * src_row_stride);
407 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
408 memcpy(dp, sp, w * bytes_per_pixel);
412 #define ROTATE_BUFFER_WIDTH 48
413 #define ROTATE_BUFFER_HEIGHT 48
416 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
419 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
434 if (!src) return NULL;
436 sw = gdk_pixbuf_get_width(src);
437 sh = gdk_pixbuf_get_height(src);
438 has_alpha = gdk_pixbuf_get_has_alpha(src);
439 srs = gdk_pixbuf_get_rowstride(src);
440 s_pix = gdk_pixbuf_get_pixels(src);
444 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
445 drs = gdk_pixbuf_get_rowstride(dest);
446 d_pix = gdk_pixbuf_get_pixels(dest);
448 a = (has_alpha ? 4 : 3);
450 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
451 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
452 b_pix = gdk_pixbuf_get_pixels(buffer);
453 brs = gdk_pixbuf_get_rowstride(buffer);
455 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
457 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
458 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
462 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
463 pixbuf_copy_block_rotate(s_pix, srs, j, i,
465 a, counter_clockwise);
467 if (counter_clockwise)
477 pixbuf_copy_block(b_pix, brs, w, h,
478 d_pix, drs, x, y, a);
482 g_object_unref(buffer);
485 /* this is the simple version of rotation (roughly 2-4x slower) */
487 for (i = 0; i < sh; i++)
489 sp = s_pix + (i * srs);
490 for (j = 0; j < sw; j++)
492 if (counter_clockwise)
494 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
498 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
501 *(dp++) = *(sp++); /* r */
502 *(dp++) = *(sp++); /* g */
503 *(dp++) = *(sp++); /* b */
504 if (has_alpha) *(dp) = *(sp++); /* a */
513 * Returns a copy of pixbuf mirrored and or flipped.
514 * TO do a 180 degree rotations set both mirror and flipped TRUE
515 * if mirror and flip are FALSE, result is a simple copy.
517 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
530 if (!src) return NULL;
532 w = gdk_pixbuf_get_width(src);
533 h = gdk_pixbuf_get_height(src);
534 has_alpha = gdk_pixbuf_get_has_alpha(src);
535 srs = gdk_pixbuf_get_rowstride(src);
536 s_pix = gdk_pixbuf_get_pixels(src);
538 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
539 drs = gdk_pixbuf_get_rowstride(dest);
540 d_pix = gdk_pixbuf_get_pixels(dest);
542 a = has_alpha ? 4 : 3;
544 for (i = 0; i < h; i++)
546 sp = s_pix + (i * srs);
549 dp = d_pix + ((h - i - 1) * drs);
553 dp = d_pix + (i * drs);
558 for (j = 0; j < w; j++)
560 *(dp++) = *(sp++); /* r */
561 *(dp++) = *(sp++); /* g */
562 *(dp++) = *(sp++); /* b */
563 if (has_alpha) *(dp) = *(sp++); /* a */
569 for (j = 0; j < w; j++)
571 *(dp++) = *(sp++); /* r */
572 *(dp++) = *(sp++); /* g */
573 *(dp++) = *(sp++); /* b */
574 if (has_alpha) *(dp++) = *(sp++); /* a */
582 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
585 GdkPixbuf *tmp = NULL;
589 case EXIF_ORIENTATION_TOP_LEFT:
590 dest = gdk_pixbuf_copy(pixbuf);
592 case EXIF_ORIENTATION_TOP_RIGHT:
594 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
596 case EXIF_ORIENTATION_BOTTOM_RIGHT:
598 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
600 case EXIF_ORIENTATION_BOTTOM_LEFT:
602 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
604 case EXIF_ORIENTATION_LEFT_TOP:
605 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
606 dest = pixbuf_copy_rotate_90(tmp, FALSE);
608 case EXIF_ORIENTATION_RIGHT_TOP:
609 /* rotated -90 (270) */
610 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
612 case EXIF_ORIENTATION_RIGHT_BOTTOM:
613 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
614 dest = pixbuf_copy_rotate_90(tmp, TRUE);
616 case EXIF_ORIENTATION_LEFT_BOTTOM:
618 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
621 dest = gdk_pixbuf_copy(pixbuf);
624 if (tmp) g_object_unref(tmp);
631 *-----------------------------------------------------------------------------
632 * pixbuf drawing (rectangles)
633 *-----------------------------------------------------------------------------
637 * Fills region of pixbuf at x,y over w,h
638 * with colors red (r), green (g), blue (b)
639 * applying alpha (a), use a=255 for solid.
641 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
642 gint x, gint y, gint w, gint h,
643 gint r, gint g, gint b, gint a)
653 pw = gdk_pixbuf_get_width(pb);
654 ph = gdk_pixbuf_get_height(pb);
656 if (x < 0 || x + w > pw) return;
657 if (y < 0 || y + h > ph) return;
659 has_alpha = gdk_pixbuf_get_has_alpha(pb);
660 prs = gdk_pixbuf_get_rowstride(pb);
661 p_pix = gdk_pixbuf_get_pixels(pb);
663 for (i = 0; i < h; i++)
665 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
666 for (j = 0; j < w; j++)
668 *pp = (r * a + *pp * (256-a)) >> 8;
670 *pp = (g * a + *pp * (256-a)) >> 8;
672 *pp = (b * a + *pp * (256-a)) >> 8;
679 void pixbuf_draw_rect(GdkPixbuf *pb,
680 gint x, gint y, gint w, gint h,
681 gint r, gint g, gint b, gint a,
682 gint left, gint right, gint top, gint bottom)
684 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
686 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
688 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
690 pixbuf_draw_rect_fill(pb, x, y, left, h,
694 void pixbuf_set_rect_fill(GdkPixbuf *pb,
695 gint x, gint y, gint w, gint h,
696 gint r, gint g, gint b, gint a)
706 pw = gdk_pixbuf_get_width(pb);
707 ph = gdk_pixbuf_get_height(pb);
709 if (x < 0 || x + w > pw) return;
710 if (y < 0 || y + h > ph) return;
712 has_alpha = gdk_pixbuf_get_has_alpha(pb);
713 prs = gdk_pixbuf_get_rowstride(pb);
714 p_pix = gdk_pixbuf_get_pixels(pb);
716 for (i = 0; i < h; i++)
718 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
719 for (j = 0; j < w; j++)
724 if (has_alpha) { *pp = a; pp++; }
729 void pixbuf_set_rect(GdkPixbuf *pb,
730 gint x, gint y, gint w, gint h,
731 gint r, gint g, gint b, gint a,
732 gint left, gint right, gint top, gint bottom)
734 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
736 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
738 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
740 pixbuf_set_rect_fill(pb, x, y, left, h,
744 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
751 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
752 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
754 buf = gdk_pixbuf_get_pixels(pb);
755 has_alpha = gdk_pixbuf_get_has_alpha(pb);
756 rowstride = gdk_pixbuf_get_rowstride(pb);
758 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
762 if (has_alpha) *p = a;
767 *-----------------------------------------------------------------------------
768 * pixbuf text rendering
769 *-----------------------------------------------------------------------------
772 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
773 GdkPixbuf *dest, gint dx, gint dy,
775 guint8 r, guint8 g, guint8 b, guint8 a)
790 if (!src || !dest) return;
792 sw = gdk_pixbuf_get_width(src);
793 sh = gdk_pixbuf_get_height(src);
795 if (sx < 0 || sx + w > sw) return;
796 if (sy < 0 || sy + h > sh) return;
798 dw = gdk_pixbuf_get_width(dest);
799 dh = gdk_pixbuf_get_height(dest);
801 if (dx < 0 || dx + w > dw) return;
802 if (dy < 0 || dy + h > dh) return;
804 s_alpha = gdk_pixbuf_get_has_alpha(src);
805 d_alpha = gdk_pixbuf_get_has_alpha(dest);
806 srs = gdk_pixbuf_get_rowstride(src);
807 drs = gdk_pixbuf_get_rowstride(dest);
808 s_pix = gdk_pixbuf_get_pixels(src);
809 d_pix = gdk_pixbuf_get_pixels(dest);
811 s_step = (s_alpha) ? 4 : 3;
812 d_step = (d_alpha) ? 4 : 3;
814 for (i = 0; i < h; i++)
816 sp = s_pix + (sy + i) * srs + sx * s_step;
817 dp = d_pix + (dy + i) * drs + dx * d_step;
818 for (j = 0; j < w; j++)
824 asub = a * sp[0] / 255;
825 *dp = (r * asub + *dp * (256-asub)) >> 8;
827 asub = a * sp[1] / 255;
828 *dp = (g * asub + *dp * (256-asub)) >> 8;
830 asub = a * sp[2] / 255;
831 *dp = (b * asub + *dp * (256-asub)) >> 8;
836 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
850 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
852 guint8 r, guint8 g, guint8 b, guint8 a)
858 cairo_surface_t *source;
861 pango_layout_get_pixel_size(layout, &w, &h);
862 if (w < 1 || h < 1) return;
864 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
866 cr = cairo_create (source);
867 cairo_set_source_rgb(cr, 0, 0, 0);
868 cairo_rectangle (cr, 0, 0, w, h);
870 cairo_set_source_rgb(cr, 1, 1, 1);
871 pango_cairo_show_layout (cr, layout);
874 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
876 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
878 cairo_image_surface_get_width (source),
879 cairo_image_surface_get_height (source),
880 cairo_image_surface_get_stride (source),
886 dw = gdk_pixbuf_get_width(pixbuf);
887 dh = gdk_pixbuf_get_height(pixbuf);
903 if (x + w > dw) w = dw - x;
904 if (y + h > dh) h = dh - y;
906 pixbuf_copy_font(buffer, sx, sy,
910 g_object_unref(buffer);
911 cairo_surface_destroy(source);
915 *-----------------------------------------------------------------------------
916 * pixbuf drawing (triangle)
917 *-----------------------------------------------------------------------------
920 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
921 gint *rx, gint *ry, gint *rw, gint *rh)
929 tw = MAX(abs(x1 - x2), abs(x2 - x3));
930 tw = MAX(tw, abs(x3 - x1));
931 th = MAX(abs(y1 - y2), abs(y2 - y3));
932 th = MAX(th, abs(y3 - y1));
940 void pixbuf_draw_triangle(GdkPixbuf *pb,
941 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
942 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
943 guint8 r, guint8 g, guint8 b, guint8 a)
955 gdouble slope1, slope2;
956 gint slope1_x, slope1_y;
959 gboolean middle = FALSE;
963 pw = gdk_pixbuf_get_width(pb);
964 ph = gdk_pixbuf_get_height(pb);
966 if (!util_clip_region(0, 0, pw, ph,
967 clip_x, clip_y, clip_w, clip_h,
968 &rx, &ry, &rw, &rh)) return;
970 util_clip_triangle(x1, y1, x2, y2, x3, y3,
973 if (!util_clip_region(rx, ry, rw, rh,
975 &fx1, &fy1, &fw, &fh)) return;
979 has_alpha = gdk_pixbuf_get_has_alpha(pb);
980 prs = gdk_pixbuf_get_rowstride(pb);
981 p_pix = gdk_pixbuf_get_pixels(pb);
983 p_step = (has_alpha) ? 4 : 3;
987 t = x1; x1 = x2; x2 = t;
988 t = y1; y1 = y2; y2 = t;
992 t = x2; x2 = x3; x3 = t;
993 t = y2; y2 = y3; y3 = t;
997 t = x1; x1 = x2; x2 = t;
998 t = y1; y1 = y2; y2 = t;
1001 slope1 = (gdouble)(y2 - y1);
1002 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
1005 slope2 = (gdouble)(y3 - y1);
1006 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
1008 for (y = fy1; y < fy2; y++)
1012 if (!middle && y > y2)
1014 slope1 = (gdouble)(y3 - y2);
1015 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
1022 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
1023 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
1027 t = xa; xa = xb; xb = t;
1030 xa = CLAMP(xa, fx1, fx2);
1031 xb = CLAMP(xb, fx1, fx2);
1033 pp = p_pix + y * prs + xa * p_step;
1037 *pp = (r * a + *pp * (256-a)) >> 8;
1039 *pp = (g * a + *pp * (256-a)) >> 8;
1041 *pp = (b * a + *pp * (256-a)) >> 8;
1043 if (has_alpha) pp++;
1051 *-----------------------------------------------------------------------------
1052 * pixbuf drawing (line)
1053 *-----------------------------------------------------------------------------
1056 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1057 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1058 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1060 gboolean flip = FALSE;
1067 t = x1; x1 = x2; x2 = t;
1068 t = y1; y1 = y2; y2 = t;
1072 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1076 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1080 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1088 slope = (y2 - y1) / d;
1091 y1 = y1 + slope * (clip_x - x1);
1094 if (x2 > clip_x + clip_w)
1096 y2 = y2 + slope * (clip_x + clip_w - x2);
1097 x2 = clip_x + clip_w;
1103 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1109 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1111 t = x1; x1 = x2; x2 = t;
1112 t = y1; y1 = y2; y2 = t;
1121 slope = (x2 - x1) / d;
1124 x1 = x1 + slope * (clip_y - y1);
1127 if (y2 > clip_y + clip_h)
1129 x2 = x2 + slope * (clip_y + clip_h - y2);
1130 y2 = clip_y + clip_h;
1152 void pixbuf_draw_line(GdkPixbuf *pb,
1153 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1154 gint x1, gint y1, gint x2, gint y2,
1155 guint8 r, guint8 g, guint8 b, guint8 a)
1159 gint rx, ry, rw, rh;
1160 gdouble rx1, ry1, rx2, ry2;
1167 gint cx1, cy1, cx2, cy2;
1171 pw = gdk_pixbuf_get_width(pb);
1172 ph = gdk_pixbuf_get_height(pb);
1174 if (!util_clip_region(0, 0, pw, ph,
1175 clip_x, clip_y, clip_w, clip_h,
1176 &rx, &ry, &rw, &rh)) return;
1177 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1178 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1179 &rx1, &ry1, &rx2, &ry2)) return;
1186 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1187 prs = gdk_pixbuf_get_rowstride(pb);
1188 p_pix = gdk_pixbuf_get_pixels(pb);
1190 p_step = (has_alpha) ? 4 : 3;
1192 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1197 t = rx1; rx1 = rx2; rx2 = t;
1198 t = ry1; ry1 = ry2; ry2 = t;
1202 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1203 for (x = rx1; x < rx2; x += 1.0)
1205 px = (gint)(x + 0.5);
1206 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1208 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1210 pp = p_pix + py * prs + px * p_step;
1211 *pp = (r * a + *pp * (256-a)) >> 8;
1213 *pp = (g * a + *pp * (256-a)) >> 8;
1215 *pp = (b * a + *pp * (256-a)) >> 8;
1224 t = rx1; rx1 = rx2; rx2 = t;
1225 t = ry1; ry1 = ry2; ry2 = t;
1229 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1230 for (y = ry1; y < ry2; y += 1.0)
1232 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1233 py = (gint)(y + 0.5);
1235 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1237 pp = p_pix + py * prs + px * p_step;
1238 *pp = (r * a + *pp * (256-a)) >> 8;
1240 *pp = (g * a + *pp * (256-a)) >> 8;
1242 *pp = (b * a + *pp * (256-a)) >> 8;
1249 *-----------------------------------------------------------------------------
1250 * pixbuf drawing (fades and shadows)
1251 *-----------------------------------------------------------------------------
1254 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1255 gint s, gboolean vertical, gint border,
1256 gint x1, gint y1, gint x2, gint y2,
1257 guint8 r, guint8 g, guint8 b, guint8 a)
1264 p_step = (has_alpha) ? 4 : 3;
1265 for (j = y1; j < y2; j++)
1267 pp = p_pix + j * prs + x1 * p_step;
1268 if (!vertical) n = a - a * abs(j - s) / border;
1269 for (i = x1; i < x2; i++)
1271 if (vertical) n = a - a * abs(i - s) / border;
1272 *pp = (r * n + *pp * (256-n)) >> 8;
1274 *pp = (g * n + *pp * (256-n)) >> 8;
1276 *pp = (b * n + *pp * (256-n)) >> 8;
1278 if (has_alpha) pp++;
1283 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1284 gint sx, gint sy, gint border,
1285 gint x1, gint y1, gint x2, gint y2,
1286 guint8 r, guint8 g, guint8 b, guint8 a)
1292 p_step = (has_alpha) ? 4 : 3;
1293 for (j = y1; j < y2; j++)
1295 pp = p_pix + j * prs + x1 * p_step;
1296 for (i = x1; i < x2; i++)
1301 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1302 n = a - a * r / border;
1303 *pp = (r * n + *pp * (256-n)) >> 8;
1305 *pp = (g * n + *pp * (256-n)) >> 8;
1307 *pp = (b * n + *pp * (256-n)) >> 8;
1309 if (has_alpha) pp++;
1314 void pixbuf_draw_shadow(GdkPixbuf *pb,
1315 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1316 gint x, gint y, gint w, gint h, gint border,
1317 guint8 r, guint8 g, guint8 b, guint8 a)
1321 gint rx, ry, rw, rh;
1322 gint fx, fy, fw, fh;
1327 pw = gdk_pixbuf_get_width(pb);
1328 ph = gdk_pixbuf_get_height(pb);
1330 if (!util_clip_region(0, 0, pw, ph,
1331 clip_x, clip_y, clip_w, clip_h,
1332 &rx, &ry, &rw, &rh)) return;
1334 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1335 prs = gdk_pixbuf_get_rowstride(pb);
1336 p_pix = gdk_pixbuf_get_pixels(pb);
1338 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1340 &fx, &fy, &fw, &fh))
1342 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1345 if (border < 1) return;
1347 if (util_clip_region(x, y + border, border, h - border * 2,
1349 &fx, &fy, &fw, &fh))
1351 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1352 x + border, TRUE, border,
1353 fx, fy, fx + fw, fy + fh,
1356 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1358 &fx, &fy, &fw, &fh))
1360 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1361 x + w - border, TRUE, border,
1362 fx, fy, fx + fw, fy + fh,
1365 if (util_clip_region(x + border, y, w - border * 2, border,
1367 &fx, &fy, &fw, &fh))
1369 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1370 y + border, FALSE, border,
1371 fx, fy, fx + fw, fy + fh,
1374 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1376 &fx, &fy, &fw, &fh))
1378 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1379 y + h - border, FALSE, border,
1380 fx, fy, fx + fw, fy + fh,
1383 if (util_clip_region(x, y, border, border,
1385 &fx, &fy, &fw, &fh))
1387 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1388 x + border, y + border, border,
1389 fx, fy, fx + fw, fy + fh,
1392 if (util_clip_region(x + w - border, y, border, border,
1394 &fx, &fy, &fw, &fh))
1396 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1397 x + w - border, y + border, border,
1398 fx, fy, fx + fw, fy + fh,
1401 if (util_clip_region(x, y + h - border, border, border,
1403 &fx, &fy, &fw, &fh))
1405 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1406 x + border, y + h - border, border,
1407 fx, fy, fx + fw, fy + fh,
1410 if (util_clip_region(x + w - border, y + h - border, border, border,
1412 &fx, &fy, &fw, &fh))
1414 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1415 x + w - border, y + h - border, border,
1416 fx, fy, fx + fw, fy + fh,
1423 *-----------------------------------------------------------------------------
1424 * pixbuf color alterations
1425 *-----------------------------------------------------------------------------
1428 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1429 gint x, gint y, gint w, gint h)
1439 pw = gdk_pixbuf_get_width(pb);
1440 ph = gdk_pixbuf_get_height(pb);
1442 if (x < 0 || x + w > pw) return;
1443 if (y < 0 || y + h > ph) return;
1445 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1446 prs = gdk_pixbuf_get_rowstride(pb);
1447 p_pix = gdk_pixbuf_get_pixels(pb);
1449 for (i = 0; i < h; i++)
1451 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1452 for (j = 0; j < w; j++)
1456 grey = (pp[0] + pp[1] + pp[2]) / 3;
1463 if (has_alpha) pp++;
1469 *-----------------------------------------------------------------------------
1470 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1472 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1482 pw = gdk_pixbuf_get_width(pb);
1483 ph = gdk_pixbuf_get_height(pb);
1485 if (x < 0 || x + w > pw) return;
1486 if (y < 0 || y + h > ph) return;
1488 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1489 prs = gdk_pixbuf_get_rowstride(pb);
1490 p_pix = gdk_pixbuf_get_pixels(pb);
1492 for (i = 0; i < h; i++)
1494 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1495 for (j = 0; j < w; j++)
1497 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1505 if (has_alpha) pp++;
1510 if (has_alpha) pp++;
1517 *-----------------------------------------------------------------------------
1518 * pixbuf ignore alpha
1519 *-----------------------------------------------------------------------------
1521 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1522 gint x, gint y, gint w, gint h)
1532 pw = gdk_pixbuf_get_width(pb);
1533 ph = gdk_pixbuf_get_height(pb);
1535 if (x < 0 || x + w > pw) return;
1536 if (y < 0 || y + h > ph) return;
1538 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1539 if (!has_alpha) return;
1541 prs = gdk_pixbuf_get_rowstride(pb);
1542 p_pix = gdk_pixbuf_get_pixels(pb);
1544 for (i = 0; i < h; i++)
1546 pp = p_pix + (y + i) * prs + (x * 4 );
1547 for (j = 0; j < w; j++)
1554 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */