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_ICON_NEXT_PAGE, icon_next_page },
153 { PIXBUF_INLINE_ICON_PREVIOUS_PAGE, icon_previous_page },
157 GdkPixbuf *pixbuf_inline(const gchar *key)
161 if (!key) return NULL;
164 while (inline_pixbuf_data[i].key)
166 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
168 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
173 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
178 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
180 static GtkIconFactory *icon_factory = NULL;
181 GtkIconSet *icon_set;
185 icon_factory = gtk_icon_factory_new();
186 gtk_icon_factory_add_default(icon_factory);
189 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
190 gtk_icon_factory_add(icon_factory, key, icon_set);
194 void pixbuf_inline_register_stock_icons(void)
199 while (inline_pixbuf_data[i].key)
201 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
206 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
208 GtkIconTheme *icon_theme;
210 GError *error = NULL;
212 icon_theme = gtk_icon_theme_get_default();
214 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
216 pixbuf = gtk_icon_theme_load_icon(icon_theme,
217 icon, /* icon name */
225 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
230 if (strchr(icon, '.'))
232 /* try again without extension */
233 gchar *icon2 = remove_extension_from_path(icon);
234 pixbuf = gtk_icon_theme_load_icon(icon_theme,
235 icon2, /* icon name */
241 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
245 /* try as an absolute path */
246 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
249 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
257 if (!pixbuf) return FALSE;
259 register_stock_icon(key, pixbuf);
263 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
264 gint old_w, gint old_h,
265 gint *new_w, gint *new_h)
267 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
270 *new_h = (gdouble)*new_w / old_w * old_h;
271 if (*new_h < 1) *new_h = 1;
276 *new_w = (gdouble)*new_h / old_h * old_w;
277 if (*new_w < 1) *new_w = 1;
280 return (*new_w != old_w || *new_h != old_h);
283 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
287 switch (fd->format_class)
289 case FORMAT_CLASS_UNKNOWN:
290 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
292 case FORMAT_CLASS_META:
293 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
295 case FORMAT_CLASS_VIDEO:
296 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
298 case FORMAT_CLASS_COLLECTION:
299 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
301 case FORMAT_CLASS_PDF:
302 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
305 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
308 if (requested_width && requested_height)
310 gint w = gdk_pixbuf_get_width(pixbuf);
311 gint h = gdk_pixbuf_get_height(pixbuf);
313 if (w > requested_width || h > requested_height)
317 if (pixbuf_scale_aspect(requested_width, requested_height,
323 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
324 g_object_unref(G_OBJECT(tmp));
333 *-----------------------------------------------------------------------------
335 *-----------------------------------------------------------------------------
338 gboolean util_clip_region(gint x, gint y, gint w, gint h,
339 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
340 gint *rx, gint *ry, gint *rw, gint *rh)
342 if (clip_x + clip_w <= x ||
344 clip_y + clip_h <= y ||
350 *rx = MAX(x, clip_x);
351 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
353 *ry = MAX(y, clip_y);
354 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
360 *-----------------------------------------------------------------------------
362 *-----------------------------------------------------------------------------
365 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
366 guchar *dest, gint dest_row_stride, gint w, gint h,
367 gint bytes_per_pixel, gboolean counter_clockwise)
373 for (i = 0; i < h; i++)
375 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
376 for (j = 0; j < w; j++)
378 if (counter_clockwise)
380 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
384 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
386 *(dp++) = *(sp++); /* r */
387 *(dp++) = *(sp++); /* g */
388 *(dp++) = *(sp++); /* b */
389 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
395 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
396 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
402 for (i = 0; i < h; i++)
404 sp = src + (i * src_row_stride);
405 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
406 memcpy(dp, sp, w * bytes_per_pixel);
410 #define ROTATE_BUFFER_WIDTH 48
411 #define ROTATE_BUFFER_HEIGHT 48
414 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
417 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
432 if (!src) return NULL;
434 sw = gdk_pixbuf_get_width(src);
435 sh = gdk_pixbuf_get_height(src);
436 has_alpha = gdk_pixbuf_get_has_alpha(src);
437 srs = gdk_pixbuf_get_rowstride(src);
438 s_pix = gdk_pixbuf_get_pixels(src);
442 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
443 drs = gdk_pixbuf_get_rowstride(dest);
444 d_pix = gdk_pixbuf_get_pixels(dest);
446 a = (has_alpha ? 4 : 3);
448 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
449 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
450 b_pix = gdk_pixbuf_get_pixels(buffer);
451 brs = gdk_pixbuf_get_rowstride(buffer);
453 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
455 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
456 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
460 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
461 pixbuf_copy_block_rotate(s_pix, srs, j, i,
463 a, counter_clockwise);
465 if (counter_clockwise)
475 pixbuf_copy_block(b_pix, brs, w, h,
476 d_pix, drs, x, y, a);
480 g_object_unref(buffer);
483 /* this is the simple version of rotation (roughly 2-4x slower) */
485 for (i = 0; i < sh; i++)
487 sp = s_pix + (i * srs);
488 for (j = 0; j < sw; j++)
490 if (counter_clockwise)
492 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
496 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
499 *(dp++) = *(sp++); /* r */
500 *(dp++) = *(sp++); /* g */
501 *(dp++) = *(sp++); /* b */
502 if (has_alpha) *(dp) = *(sp++); /* a */
511 * Returns a copy of pixbuf mirrored and or flipped.
512 * TO do a 180 degree rotations set both mirror and flipped TRUE
513 * if mirror and flip are FALSE, result is a simple copy.
515 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
528 if (!src) return NULL;
530 w = gdk_pixbuf_get_width(src);
531 h = gdk_pixbuf_get_height(src);
532 has_alpha = gdk_pixbuf_get_has_alpha(src);
533 srs = gdk_pixbuf_get_rowstride(src);
534 s_pix = gdk_pixbuf_get_pixels(src);
536 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
537 drs = gdk_pixbuf_get_rowstride(dest);
538 d_pix = gdk_pixbuf_get_pixels(dest);
540 a = has_alpha ? 4 : 3;
542 for (i = 0; i < h; i++)
544 sp = s_pix + (i * srs);
547 dp = d_pix + ((h - i - 1) * drs);
551 dp = d_pix + (i * drs);
556 for (j = 0; j < w; j++)
558 *(dp++) = *(sp++); /* r */
559 *(dp++) = *(sp++); /* g */
560 *(dp++) = *(sp++); /* b */
561 if (has_alpha) *(dp) = *(sp++); /* a */
567 for (j = 0; j < w; j++)
569 *(dp++) = *(sp++); /* r */
570 *(dp++) = *(sp++); /* g */
571 *(dp++) = *(sp++); /* b */
572 if (has_alpha) *(dp++) = *(sp++); /* a */
580 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
583 GdkPixbuf *tmp = NULL;
587 case EXIF_ORIENTATION_TOP_LEFT:
588 dest = gdk_pixbuf_copy(pixbuf);
590 case EXIF_ORIENTATION_TOP_RIGHT:
592 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
594 case EXIF_ORIENTATION_BOTTOM_RIGHT:
596 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
598 case EXIF_ORIENTATION_BOTTOM_LEFT:
600 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
602 case EXIF_ORIENTATION_LEFT_TOP:
603 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
604 dest = pixbuf_copy_rotate_90(tmp, FALSE);
606 case EXIF_ORIENTATION_RIGHT_TOP:
607 /* rotated -90 (270) */
608 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
610 case EXIF_ORIENTATION_RIGHT_BOTTOM:
611 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
612 dest = pixbuf_copy_rotate_90(tmp, TRUE);
614 case EXIF_ORIENTATION_LEFT_BOTTOM:
616 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
619 dest = gdk_pixbuf_copy(pixbuf);
622 if (tmp) g_object_unref(tmp);
629 *-----------------------------------------------------------------------------
630 * pixbuf drawing (rectangles)
631 *-----------------------------------------------------------------------------
635 * Fills region of pixbuf at x,y over w,h
636 * with colors red (r), green (g), blue (b)
637 * applying alpha (a), use a=255 for solid.
639 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
640 gint x, gint y, gint w, gint h,
641 gint r, gint g, gint b, gint a)
651 pw = gdk_pixbuf_get_width(pb);
652 ph = gdk_pixbuf_get_height(pb);
654 if (x < 0 || x + w > pw) return;
655 if (y < 0 || y + h > ph) return;
657 has_alpha = gdk_pixbuf_get_has_alpha(pb);
658 prs = gdk_pixbuf_get_rowstride(pb);
659 p_pix = gdk_pixbuf_get_pixels(pb);
661 for (i = 0; i < h; i++)
663 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
664 for (j = 0; j < w; j++)
666 *pp = (r * a + *pp * (256-a)) >> 8;
668 *pp = (g * a + *pp * (256-a)) >> 8;
670 *pp = (b * a + *pp * (256-a)) >> 8;
677 void pixbuf_draw_rect(GdkPixbuf *pb,
678 gint x, gint y, gint w, gint h,
679 gint r, gint g, gint b, gint a,
680 gint left, gint right, gint top, gint bottom)
682 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
684 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
686 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
688 pixbuf_draw_rect_fill(pb, x, y, left, h,
692 void pixbuf_set_rect_fill(GdkPixbuf *pb,
693 gint x, gint y, gint w, gint h,
694 gint r, gint g, gint b, gint a)
704 pw = gdk_pixbuf_get_width(pb);
705 ph = gdk_pixbuf_get_height(pb);
707 if (x < 0 || x + w > pw) return;
708 if (y < 0 || y + h > ph) return;
710 has_alpha = gdk_pixbuf_get_has_alpha(pb);
711 prs = gdk_pixbuf_get_rowstride(pb);
712 p_pix = gdk_pixbuf_get_pixels(pb);
714 for (i = 0; i < h; i++)
716 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
717 for (j = 0; j < w; j++)
722 if (has_alpha) { *pp = a; pp++; }
727 void pixbuf_set_rect(GdkPixbuf *pb,
728 gint x, gint y, gint w, gint h,
729 gint r, gint g, gint b, gint a,
730 gint left, gint right, gint top, gint bottom)
732 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
734 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
736 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
738 pixbuf_set_rect_fill(pb, x, y, left, h,
742 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
749 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
750 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
752 buf = gdk_pixbuf_get_pixels(pb);
753 has_alpha = gdk_pixbuf_get_has_alpha(pb);
754 rowstride = gdk_pixbuf_get_rowstride(pb);
756 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
760 if (has_alpha) *p = a;
765 *-----------------------------------------------------------------------------
766 * pixbuf text rendering
767 *-----------------------------------------------------------------------------
770 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
771 GdkPixbuf *dest, gint dx, gint dy,
773 guint8 r, guint8 g, guint8 b, guint8 a)
788 if (!src || !dest) return;
790 sw = gdk_pixbuf_get_width(src);
791 sh = gdk_pixbuf_get_height(src);
793 if (sx < 0 || sx + w > sw) return;
794 if (sy < 0 || sy + h > sh) return;
796 dw = gdk_pixbuf_get_width(dest);
797 dh = gdk_pixbuf_get_height(dest);
799 if (dx < 0 || dx + w > dw) return;
800 if (dy < 0 || dy + h > dh) return;
802 s_alpha = gdk_pixbuf_get_has_alpha(src);
803 d_alpha = gdk_pixbuf_get_has_alpha(dest);
804 srs = gdk_pixbuf_get_rowstride(src);
805 drs = gdk_pixbuf_get_rowstride(dest);
806 s_pix = gdk_pixbuf_get_pixels(src);
807 d_pix = gdk_pixbuf_get_pixels(dest);
809 s_step = (s_alpha) ? 4 : 3;
810 d_step = (d_alpha) ? 4 : 3;
812 for (i = 0; i < h; i++)
814 sp = s_pix + (sy + i) * srs + sx * s_step;
815 dp = d_pix + (dy + i) * drs + dx * d_step;
816 for (j = 0; j < w; j++)
822 asub = a * sp[0] / 255;
823 *dp = (r * asub + *dp * (256-asub)) >> 8;
825 asub = a * sp[1] / 255;
826 *dp = (g * asub + *dp * (256-asub)) >> 8;
828 asub = a * sp[2] / 255;
829 *dp = (b * asub + *dp * (256-asub)) >> 8;
834 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
848 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
850 guint8 r, guint8 g, guint8 b, guint8 a)
856 cairo_surface_t *source;
859 pango_layout_get_pixel_size(layout, &w, &h);
860 if (w < 1 || h < 1) return;
862 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
864 cr = cairo_create (source);
865 cairo_set_source_rgb(cr, 0, 0, 0);
866 cairo_rectangle (cr, 0, 0, w, h);
868 cairo_set_source_rgb(cr, 1, 1, 1);
869 pango_cairo_show_layout (cr, layout);
872 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
874 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
876 cairo_image_surface_get_width (source),
877 cairo_image_surface_get_height (source),
878 cairo_image_surface_get_stride (source),
884 dw = gdk_pixbuf_get_width(pixbuf);
885 dh = gdk_pixbuf_get_height(pixbuf);
901 if (x + w > dw) w = dw - x;
902 if (y + h > dh) h = dh - y;
904 pixbuf_copy_font(buffer, sx, sy,
908 g_object_unref(buffer);
909 cairo_surface_destroy(source);
913 *-----------------------------------------------------------------------------
914 * pixbuf drawing (triangle)
915 *-----------------------------------------------------------------------------
918 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
919 gint *rx, gint *ry, gint *rw, gint *rh)
927 tw = MAX(abs(x1 - x2), abs(x2 - x3));
928 tw = MAX(tw, abs(x3 - x1));
929 th = MAX(abs(y1 - y2), abs(y2 - y3));
930 th = MAX(th, abs(y3 - y1));
938 void pixbuf_draw_triangle(GdkPixbuf *pb,
939 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
940 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
941 guint8 r, guint8 g, guint8 b, guint8 a)
953 gdouble slope1, slope2;
954 gint slope1_x, slope1_y;
957 gboolean middle = FALSE;
961 pw = gdk_pixbuf_get_width(pb);
962 ph = gdk_pixbuf_get_height(pb);
964 if (!util_clip_region(0, 0, pw, ph,
965 clip_x, clip_y, clip_w, clip_h,
966 &rx, &ry, &rw, &rh)) return;
968 util_clip_triangle(x1, y1, x2, y2, x3, y3,
971 if (!util_clip_region(rx, ry, rw, rh,
973 &fx1, &fy1, &fw, &fh)) return;
977 has_alpha = gdk_pixbuf_get_has_alpha(pb);
978 prs = gdk_pixbuf_get_rowstride(pb);
979 p_pix = gdk_pixbuf_get_pixels(pb);
981 p_step = (has_alpha) ? 4 : 3;
985 t = x1; x1 = x2; x2 = t;
986 t = y1; y1 = y2; y2 = t;
990 t = x2; x2 = x3; x3 = t;
991 t = y2; y2 = y3; y3 = t;
995 t = x1; x1 = x2; x2 = t;
996 t = y1; y1 = y2; y2 = t;
999 slope1 = (gdouble)(y2 - y1);
1000 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
1003 slope2 = (gdouble)(y3 - y1);
1004 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
1006 for (y = fy1; y < fy2; y++)
1010 if (!middle && y > y2)
1012 slope1 = (gdouble)(y3 - y2);
1013 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
1020 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
1021 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
1025 t = xa; xa = xb; xb = t;
1028 xa = CLAMP(xa, fx1, fx2);
1029 xb = CLAMP(xb, fx1, fx2);
1031 pp = p_pix + y * prs + xa * p_step;
1035 *pp = (r * a + *pp * (256-a)) >> 8;
1037 *pp = (g * a + *pp * (256-a)) >> 8;
1039 *pp = (b * a + *pp * (256-a)) >> 8;
1041 if (has_alpha) pp++;
1049 *-----------------------------------------------------------------------------
1050 * pixbuf drawing (line)
1051 *-----------------------------------------------------------------------------
1054 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1055 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1056 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1058 gboolean flip = FALSE;
1065 t = x1; x1 = x2; x2 = t;
1066 t = y1; y1 = y2; y2 = t;
1070 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1074 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1078 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1086 slope = (y2 - y1) / d;
1089 y1 = y1 + slope * (clip_x - x1);
1092 if (x2 > clip_x + clip_w)
1094 y2 = y2 + slope * (clip_x + clip_w - x2);
1095 x2 = clip_x + clip_w;
1101 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1107 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1109 t = x1; x1 = x2; x2 = t;
1110 t = y1; y1 = y2; y2 = t;
1119 slope = (x2 - x1) / d;
1122 x1 = x1 + slope * (clip_y - y1);
1125 if (y2 > clip_y + clip_h)
1127 x2 = x2 + slope * (clip_y + clip_h - y2);
1128 y2 = clip_y + clip_h;
1150 void pixbuf_draw_line(GdkPixbuf *pb,
1151 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1152 gint x1, gint y1, gint x2, gint y2,
1153 guint8 r, guint8 g, guint8 b, guint8 a)
1157 gint rx, ry, rw, rh;
1158 gdouble rx1, ry1, rx2, ry2;
1165 gint cx1, cy1, cx2, cy2;
1169 pw = gdk_pixbuf_get_width(pb);
1170 ph = gdk_pixbuf_get_height(pb);
1172 if (!util_clip_region(0, 0, pw, ph,
1173 clip_x, clip_y, clip_w, clip_h,
1174 &rx, &ry, &rw, &rh)) return;
1175 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1176 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1177 &rx1, &ry1, &rx2, &ry2)) return;
1184 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1185 prs = gdk_pixbuf_get_rowstride(pb);
1186 p_pix = gdk_pixbuf_get_pixels(pb);
1188 p_step = (has_alpha) ? 4 : 3;
1190 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1195 t = rx1; rx1 = rx2; rx2 = t;
1196 t = ry1; ry1 = ry2; ry2 = t;
1200 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1201 for (x = rx1; x < rx2; x += 1.0)
1203 px = (gint)(x + 0.5);
1204 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1206 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1208 pp = p_pix + py * prs + px * p_step;
1209 *pp = (r * a + *pp * (256-a)) >> 8;
1211 *pp = (g * a + *pp * (256-a)) >> 8;
1213 *pp = (b * a + *pp * (256-a)) >> 8;
1222 t = rx1; rx1 = rx2; rx2 = t;
1223 t = ry1; ry1 = ry2; ry2 = t;
1227 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1228 for (y = ry1; y < ry2; y += 1.0)
1230 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1231 py = (gint)(y + 0.5);
1233 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1235 pp = p_pix + py * prs + px * p_step;
1236 *pp = (r * a + *pp * (256-a)) >> 8;
1238 *pp = (g * a + *pp * (256-a)) >> 8;
1240 *pp = (b * a + *pp * (256-a)) >> 8;
1247 *-----------------------------------------------------------------------------
1248 * pixbuf drawing (fades and shadows)
1249 *-----------------------------------------------------------------------------
1252 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1253 gint s, gboolean vertical, gint border,
1254 gint x1, gint y1, gint x2, gint y2,
1255 guint8 r, guint8 g, guint8 b, guint8 a)
1262 p_step = (has_alpha) ? 4 : 3;
1263 for (j = y1; j < y2; j++)
1265 pp = p_pix + j * prs + x1 * p_step;
1266 if (!vertical) n = a - a * abs(j - s) / border;
1267 for (i = x1; i < x2; i++)
1269 if (vertical) n = a - a * abs(i - s) / border;
1270 *pp = (r * n + *pp * (256-n)) >> 8;
1272 *pp = (g * n + *pp * (256-n)) >> 8;
1274 *pp = (b * n + *pp * (256-n)) >> 8;
1276 if (has_alpha) pp++;
1281 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1282 gint sx, gint sy, gint border,
1283 gint x1, gint y1, gint x2, gint y2,
1284 guint8 r, guint8 g, guint8 b, guint8 a)
1290 p_step = (has_alpha) ? 4 : 3;
1291 for (j = y1; j < y2; j++)
1293 pp = p_pix + j * prs + x1 * p_step;
1294 for (i = x1; i < x2; i++)
1299 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1300 n = a - a * r / border;
1301 *pp = (r * n + *pp * (256-n)) >> 8;
1303 *pp = (g * n + *pp * (256-n)) >> 8;
1305 *pp = (b * n + *pp * (256-n)) >> 8;
1307 if (has_alpha) pp++;
1312 void pixbuf_draw_shadow(GdkPixbuf *pb,
1313 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1314 gint x, gint y, gint w, gint h, gint border,
1315 guint8 r, guint8 g, guint8 b, guint8 a)
1319 gint rx, ry, rw, rh;
1320 gint fx, fy, fw, fh;
1325 pw = gdk_pixbuf_get_width(pb);
1326 ph = gdk_pixbuf_get_height(pb);
1328 if (!util_clip_region(0, 0, pw, ph,
1329 clip_x, clip_y, clip_w, clip_h,
1330 &rx, &ry, &rw, &rh)) return;
1332 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1333 prs = gdk_pixbuf_get_rowstride(pb);
1334 p_pix = gdk_pixbuf_get_pixels(pb);
1336 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1338 &fx, &fy, &fw, &fh))
1340 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1343 if (border < 1) return;
1345 if (util_clip_region(x, y + border, border, h - border * 2,
1347 &fx, &fy, &fw, &fh))
1349 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1350 x + border, TRUE, border,
1351 fx, fy, fx + fw, fy + fh,
1354 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1356 &fx, &fy, &fw, &fh))
1358 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1359 x + w - border, TRUE, border,
1360 fx, fy, fx + fw, fy + fh,
1363 if (util_clip_region(x + border, y, w - border * 2, border,
1365 &fx, &fy, &fw, &fh))
1367 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1368 y + border, FALSE, border,
1369 fx, fy, fx + fw, fy + fh,
1372 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1374 &fx, &fy, &fw, &fh))
1376 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1377 y + h - border, FALSE, border,
1378 fx, fy, fx + fw, fy + fh,
1381 if (util_clip_region(x, y, border, border,
1383 &fx, &fy, &fw, &fh))
1385 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1386 x + border, y + border, border,
1387 fx, fy, fx + fw, fy + fh,
1390 if (util_clip_region(x + w - border, y, border, border,
1392 &fx, &fy, &fw, &fh))
1394 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1395 x + w - border, y + border, border,
1396 fx, fy, fx + fw, fy + fh,
1399 if (util_clip_region(x, y + h - border, border, border,
1401 &fx, &fy, &fw, &fh))
1403 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1404 x + border, y + h - border, border,
1405 fx, fy, fx + fw, fy + fh,
1408 if (util_clip_region(x + w - border, y + h - border, border, border,
1410 &fx, &fy, &fw, &fh))
1412 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1413 x + w - border, y + h - border, border,
1414 fx, fy, fx + fw, fy + fh,
1421 *-----------------------------------------------------------------------------
1422 * pixbuf color alterations
1423 *-----------------------------------------------------------------------------
1426 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1427 gint x, gint y, gint w, gint h)
1437 pw = gdk_pixbuf_get_width(pb);
1438 ph = gdk_pixbuf_get_height(pb);
1440 if (x < 0 || x + w > pw) return;
1441 if (y < 0 || y + h > ph) return;
1443 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1444 prs = gdk_pixbuf_get_rowstride(pb);
1445 p_pix = gdk_pixbuf_get_pixels(pb);
1447 for (i = 0; i < h; i++)
1449 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1450 for (j = 0; j < w; j++)
1454 grey = (pp[0] + pp[1] + pp[2]) / 3;
1461 if (has_alpha) pp++;
1467 *-----------------------------------------------------------------------------
1468 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1470 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1480 pw = gdk_pixbuf_get_width(pb);
1481 ph = gdk_pixbuf_get_height(pb);
1483 if (x < 0 || x + w > pw) return;
1484 if (y < 0 || y + h > ph) return;
1486 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1487 prs = gdk_pixbuf_get_rowstride(pb);
1488 p_pix = gdk_pixbuf_get_pixels(pb);
1490 for (i = 0; i < h; i++)
1492 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1493 for (j = 0; j < w; j++)
1495 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1503 if (has_alpha) pp++;
1508 if (has_alpha) pp++;
1513 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */