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_PANORAMA, icon_panorama },
124 { PIXBUF_INLINE_ICON_MAINTENANCE, icon_maintenance },
125 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, icon_zoomfillhor },
126 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, icon_zoomfillvert },
127 { PIXBUF_INLINE_ICON_HIDETOOLS, icon_hidetools },
128 { PIXBUF_INLINE_ICON_EXIF, icon_exif },
129 { PIXBUF_INLINE_ICON_MARKS, icon_marks },
130 { PIXBUF_INLINE_ICON_INFO, icon_info },
131 { PIXBUF_INLINE_ICON_SORT, icon_sort },
132 { PIXBUF_INLINE_ICON_PDF, icon_pdf },
133 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, icon_draw_rectangle },
134 { PIXBUF_INLINE_ICON_MOVE, icon_move },
135 { PIXBUF_INLINE_ICON_RENAME, icon_rename },
136 { PIXBUF_INLINE_ICON_SELECT_ALL, icon_select_all },
137 { PIXBUF_INLINE_ICON_SELECT_NONE, icon_select_none },
138 { PIXBUF_INLINE_ICON_SELECT_INVERT, icon_select_invert },
139 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, icon_select_rectangle },
140 { PIXBUF_INLINE_ICON_FILE_FILTER, icon_file_filter },
141 { PIXBUF_INLINE_ICON_CW, icon_rotate_clockwise },
142 { PIXBUF_INLINE_ICON_CCW, icon_rotate_counter_clockwise },
143 { PIXBUF_INLINE_ICON_180, icon_rotate_180 },
144 { PIXBUF_INLINE_ICON_MIRROR, icon_mirror },
145 { PIXBUF_INLINE_ICON_FLIP, icon_flip },
146 { PIXBUF_INLINE_ICON_ORIGINAL, icon_original },
147 { PIXBUF_INLINE_ICON_TRASH, icon_trash },
148 { PIXBUF_INLINE_ICON_HEIF, icon_heic },
149 { PIXBUF_INLINE_ICON_GRAYSCALE, icon_grayscale },
150 { PIXBUF_INLINE_ICON_EXPOSURE, icon_exposure },
151 { PIXBUF_INLINE_SPLIT_PANE_SYNC, icon_split_pane_sync },
155 GdkPixbuf *pixbuf_inline(const gchar *key)
159 if (!key) return NULL;
162 while (inline_pixbuf_data[i].key)
164 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
166 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
171 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
176 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
178 static GtkIconFactory *icon_factory = NULL;
179 GtkIconSet *icon_set;
183 icon_factory = gtk_icon_factory_new();
184 gtk_icon_factory_add_default(icon_factory);
187 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
188 gtk_icon_factory_add(icon_factory, key, icon_set);
192 void pixbuf_inline_register_stock_icons(void)
197 while (inline_pixbuf_data[i].key)
199 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
204 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
206 GtkIconTheme *icon_theme;
208 GError *error = NULL;
210 icon_theme = gtk_icon_theme_get_default();
212 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
214 pixbuf = gtk_icon_theme_load_icon(icon_theme,
215 icon, /* icon name */
223 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
228 if (strchr(icon, '.'))
230 /* try again without extension */
231 gchar *icon2 = remove_extension_from_path(icon);
232 pixbuf = gtk_icon_theme_load_icon(icon_theme,
233 icon2, /* icon name */
239 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
243 /* try as an absolute path */
244 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
247 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
255 if (!pixbuf) return FALSE;
257 register_stock_icon(key, pixbuf);
261 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
262 gint old_w, gint old_h,
263 gint *new_w, gint *new_h)
265 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
268 *new_h = (gdouble)*new_w / old_w * old_h;
269 if (*new_h < 1) *new_h = 1;
274 *new_w = (gdouble)*new_h / old_h * old_w;
275 if (*new_w < 1) *new_w = 1;
278 return (*new_w != old_w || *new_h != old_h);
281 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
285 switch (fd->format_class)
287 case FORMAT_CLASS_UNKNOWN:
288 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
290 case FORMAT_CLASS_META:
291 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
293 case FORMAT_CLASS_VIDEO:
294 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
296 case FORMAT_CLASS_COLLECTION:
297 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
299 case FORMAT_CLASS_DOCUMENT:
300 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
303 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
306 if (requested_width && requested_height)
308 gint w = gdk_pixbuf_get_width(pixbuf);
309 gint h = gdk_pixbuf_get_height(pixbuf);
311 if (w > requested_width || h > requested_height)
315 if (pixbuf_scale_aspect(requested_width, requested_height,
321 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
322 g_object_unref(G_OBJECT(tmp));
331 *-----------------------------------------------------------------------------
333 *-----------------------------------------------------------------------------
336 gboolean util_clip_region(gint x, gint y, gint w, gint h,
337 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
338 gint *rx, gint *ry, gint *rw, gint *rh)
340 if (clip_x + clip_w <= x ||
342 clip_y + clip_h <= y ||
348 *rx = MAX(x, clip_x);
349 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
351 *ry = MAX(y, clip_y);
352 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
358 *-----------------------------------------------------------------------------
360 *-----------------------------------------------------------------------------
363 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
364 guchar *dest, gint dest_row_stride, gint w, gint h,
365 gint bytes_per_pixel, gboolean counter_clockwise)
371 for (i = 0; i < h; i++)
373 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
374 for (j = 0; j < w; j++)
376 if (counter_clockwise)
378 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
382 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
384 *(dp++) = *(sp++); /* r */
385 *(dp++) = *(sp++); /* g */
386 *(dp++) = *(sp++); /* b */
387 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
393 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
394 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
400 for (i = 0; i < h; i++)
402 sp = src + (i * src_row_stride);
403 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
404 memcpy(dp, sp, w * bytes_per_pixel);
408 #define ROTATE_BUFFER_WIDTH 48
409 #define ROTATE_BUFFER_HEIGHT 48
412 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
415 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
430 if (!src) return NULL;
432 sw = gdk_pixbuf_get_width(src);
433 sh = gdk_pixbuf_get_height(src);
434 has_alpha = gdk_pixbuf_get_has_alpha(src);
435 srs = gdk_pixbuf_get_rowstride(src);
436 s_pix = gdk_pixbuf_get_pixels(src);
440 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
441 drs = gdk_pixbuf_get_rowstride(dest);
442 d_pix = gdk_pixbuf_get_pixels(dest);
444 a = (has_alpha ? 4 : 3);
446 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
447 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
448 b_pix = gdk_pixbuf_get_pixels(buffer);
449 brs = gdk_pixbuf_get_rowstride(buffer);
451 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
453 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
454 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
458 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
459 pixbuf_copy_block_rotate(s_pix, srs, j, i,
461 a, counter_clockwise);
463 if (counter_clockwise)
473 pixbuf_copy_block(b_pix, brs, w, h,
474 d_pix, drs, x, y, a);
478 g_object_unref(buffer);
481 /* this is the simple version of rotation (roughly 2-4x slower) */
483 for (i = 0; i < sh; i++)
485 sp = s_pix + (i * srs);
486 for (j = 0; j < sw; j++)
488 if (counter_clockwise)
490 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
494 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
497 *(dp++) = *(sp++); /* r */
498 *(dp++) = *(sp++); /* g */
499 *(dp++) = *(sp++); /* b */
500 if (has_alpha) *(dp) = *(sp++); /* a */
509 * Returns a copy of pixbuf mirrored and or flipped.
510 * TO do a 180 degree rotations set both mirror and flipped TRUE
511 * if mirror and flip are FALSE, result is a simple copy.
513 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
526 if (!src) return NULL;
528 w = gdk_pixbuf_get_width(src);
529 h = gdk_pixbuf_get_height(src);
530 has_alpha = gdk_pixbuf_get_has_alpha(src);
531 srs = gdk_pixbuf_get_rowstride(src);
532 s_pix = gdk_pixbuf_get_pixels(src);
534 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
535 drs = gdk_pixbuf_get_rowstride(dest);
536 d_pix = gdk_pixbuf_get_pixels(dest);
538 a = has_alpha ? 4 : 3;
540 for (i = 0; i < h; i++)
542 sp = s_pix + (i * srs);
545 dp = d_pix + ((h - i - 1) * drs);
549 dp = d_pix + (i * drs);
554 for (j = 0; j < w; j++)
556 *(dp++) = *(sp++); /* r */
557 *(dp++) = *(sp++); /* g */
558 *(dp++) = *(sp++); /* b */
559 if (has_alpha) *(dp) = *(sp++); /* a */
565 for (j = 0; j < w; j++)
567 *(dp++) = *(sp++); /* r */
568 *(dp++) = *(sp++); /* g */
569 *(dp++) = *(sp++); /* b */
570 if (has_alpha) *(dp++) = *(sp++); /* a */
578 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
581 GdkPixbuf *tmp = NULL;
585 case EXIF_ORIENTATION_TOP_LEFT:
586 dest = gdk_pixbuf_copy(pixbuf);
588 case EXIF_ORIENTATION_TOP_RIGHT:
590 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
592 case EXIF_ORIENTATION_BOTTOM_RIGHT:
594 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
596 case EXIF_ORIENTATION_BOTTOM_LEFT:
598 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
600 case EXIF_ORIENTATION_LEFT_TOP:
601 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
602 dest = pixbuf_copy_rotate_90(tmp, FALSE);
604 case EXIF_ORIENTATION_RIGHT_TOP:
605 /* rotated -90 (270) */
606 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
608 case EXIF_ORIENTATION_RIGHT_BOTTOM:
609 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
610 dest = pixbuf_copy_rotate_90(tmp, TRUE);
612 case EXIF_ORIENTATION_LEFT_BOTTOM:
614 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
617 dest = gdk_pixbuf_copy(pixbuf);
620 if (tmp) g_object_unref(tmp);
627 *-----------------------------------------------------------------------------
628 * pixbuf drawing (rectangles)
629 *-----------------------------------------------------------------------------
633 * Fills region of pixbuf at x,y over w,h
634 * with colors red (r), green (g), blue (b)
635 * applying alpha (a), use a=255 for solid.
637 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
638 gint x, gint y, gint w, gint h,
639 gint r, gint g, gint b, gint a)
649 pw = gdk_pixbuf_get_width(pb);
650 ph = gdk_pixbuf_get_height(pb);
652 if (x < 0 || x + w > pw) return;
653 if (y < 0 || y + h > ph) return;
655 has_alpha = gdk_pixbuf_get_has_alpha(pb);
656 prs = gdk_pixbuf_get_rowstride(pb);
657 p_pix = gdk_pixbuf_get_pixels(pb);
659 for (i = 0; i < h; i++)
661 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
662 for (j = 0; j < w; j++)
664 *pp = (r * a + *pp * (256-a)) >> 8;
666 *pp = (g * a + *pp * (256-a)) >> 8;
668 *pp = (b * a + *pp * (256-a)) >> 8;
675 void pixbuf_draw_rect(GdkPixbuf *pb,
676 gint x, gint y, gint w, gint h,
677 gint r, gint g, gint b, gint a,
678 gint left, gint right, gint top, gint bottom)
680 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
682 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
684 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
686 pixbuf_draw_rect_fill(pb, x, y, left, h,
690 void pixbuf_set_rect_fill(GdkPixbuf *pb,
691 gint x, gint y, gint w, gint h,
692 gint r, gint g, gint b, gint a)
702 pw = gdk_pixbuf_get_width(pb);
703 ph = gdk_pixbuf_get_height(pb);
705 if (x < 0 || x + w > pw) return;
706 if (y < 0 || y + h > ph) return;
708 has_alpha = gdk_pixbuf_get_has_alpha(pb);
709 prs = gdk_pixbuf_get_rowstride(pb);
710 p_pix = gdk_pixbuf_get_pixels(pb);
712 for (i = 0; i < h; i++)
714 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
715 for (j = 0; j < w; j++)
720 if (has_alpha) { *pp = a; pp++; }
725 void pixbuf_set_rect(GdkPixbuf *pb,
726 gint x, gint y, gint w, gint h,
727 gint r, gint g, gint b, gint a,
728 gint left, gint right, gint top, gint bottom)
730 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
732 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
734 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
736 pixbuf_set_rect_fill(pb, x, y, left, h,
740 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
747 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
748 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
750 buf = gdk_pixbuf_get_pixels(pb);
751 has_alpha = gdk_pixbuf_get_has_alpha(pb);
752 rowstride = gdk_pixbuf_get_rowstride(pb);
754 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
758 if (has_alpha) *p = a;
763 *-----------------------------------------------------------------------------
764 * pixbuf text rendering
765 *-----------------------------------------------------------------------------
768 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
769 GdkPixbuf *dest, gint dx, gint dy,
771 guint8 r, guint8 g, guint8 b, guint8 a)
786 if (!src || !dest) return;
788 sw = gdk_pixbuf_get_width(src);
789 sh = gdk_pixbuf_get_height(src);
791 if (sx < 0 || sx + w > sw) return;
792 if (sy < 0 || sy + h > sh) return;
794 dw = gdk_pixbuf_get_width(dest);
795 dh = gdk_pixbuf_get_height(dest);
797 if (dx < 0 || dx + w > dw) return;
798 if (dy < 0 || dy + h > dh) return;
800 s_alpha = gdk_pixbuf_get_has_alpha(src);
801 d_alpha = gdk_pixbuf_get_has_alpha(dest);
802 srs = gdk_pixbuf_get_rowstride(src);
803 drs = gdk_pixbuf_get_rowstride(dest);
804 s_pix = gdk_pixbuf_get_pixels(src);
805 d_pix = gdk_pixbuf_get_pixels(dest);
807 s_step = (s_alpha) ? 4 : 3;
808 d_step = (d_alpha) ? 4 : 3;
810 for (i = 0; i < h; i++)
812 sp = s_pix + (sy + i) * srs + sx * s_step;
813 dp = d_pix + (dy + i) * drs + dx * d_step;
814 for (j = 0; j < w; j++)
820 asub = a * sp[0] / 255;
821 *dp = (r * asub + *dp * (256-asub)) >> 8;
823 asub = a * sp[1] / 255;
824 *dp = (g * asub + *dp * (256-asub)) >> 8;
826 asub = a * sp[2] / 255;
827 *dp = (b * asub + *dp * (256-asub)) >> 8;
832 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
846 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
848 guint8 r, guint8 g, guint8 b, guint8 a)
854 cairo_surface_t *source;
857 pango_layout_get_pixel_size(layout, &w, &h);
858 if (w < 1 || h < 1) return;
860 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
862 cr = cairo_create (source);
863 cairo_set_source_rgb(cr, 0, 0, 0);
864 cairo_rectangle (cr, 0, 0, w, h);
866 cairo_set_source_rgb(cr, 1, 1, 1);
867 pango_cairo_show_layout (cr, layout);
870 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
872 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
874 cairo_image_surface_get_width (source),
875 cairo_image_surface_get_height (source),
876 cairo_image_surface_get_stride (source),
882 dw = gdk_pixbuf_get_width(pixbuf);
883 dh = gdk_pixbuf_get_height(pixbuf);
899 if (x + w > dw) w = dw - x;
900 if (y + h > dh) h = dh - y;
902 pixbuf_copy_font(buffer, sx, sy,
906 g_object_unref(buffer);
907 cairo_surface_destroy(source);
911 *-----------------------------------------------------------------------------
912 * pixbuf drawing (triangle)
913 *-----------------------------------------------------------------------------
916 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
917 gint *rx, gint *ry, gint *rw, gint *rh)
925 tw = MAX(abs(x1 - x2), abs(x2 - x3));
926 tw = MAX(tw, abs(x3 - x1));
927 th = MAX(abs(y1 - y2), abs(y2 - y3));
928 th = MAX(th, abs(y3 - y1));
936 void pixbuf_draw_triangle(GdkPixbuf *pb,
937 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
938 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
939 guint8 r, guint8 g, guint8 b, guint8 a)
951 gdouble slope1, slope2;
952 gint slope1_x, slope1_y;
955 gboolean middle = FALSE;
959 pw = gdk_pixbuf_get_width(pb);
960 ph = gdk_pixbuf_get_height(pb);
962 if (!util_clip_region(0, 0, pw, ph,
963 clip_x, clip_y, clip_w, clip_h,
964 &rx, &ry, &rw, &rh)) return;
966 util_clip_triangle(x1, y1, x2, y2, x3, y3,
969 if (!util_clip_region(rx, ry, rw, rh,
971 &fx1, &fy1, &fw, &fh)) return;
975 has_alpha = gdk_pixbuf_get_has_alpha(pb);
976 prs = gdk_pixbuf_get_rowstride(pb);
977 p_pix = gdk_pixbuf_get_pixels(pb);
979 p_step = (has_alpha) ? 4 : 3;
983 t = x1; x1 = x2; x2 = t;
984 t = y1; y1 = y2; y2 = t;
988 t = x2; x2 = x3; x3 = t;
989 t = y2; y2 = y3; y3 = t;
993 t = x1; x1 = x2; x2 = t;
994 t = y1; y1 = y2; y2 = t;
997 slope1 = (gdouble)(y2 - y1);
998 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
1001 slope2 = (gdouble)(y3 - y1);
1002 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
1004 for (y = fy1; y < fy2; y++)
1008 if (!middle && y > y2)
1010 slope1 = (gdouble)(y3 - y2);
1011 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
1018 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
1019 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
1023 t = xa; xa = xb; xb = t;
1026 xa = CLAMP(xa, fx1, fx2);
1027 xb = CLAMP(xb, fx1, fx2);
1029 pp = p_pix + y * prs + xa * p_step;
1033 *pp = (r * a + *pp * (256-a)) >> 8;
1035 *pp = (g * a + *pp * (256-a)) >> 8;
1037 *pp = (b * a + *pp * (256-a)) >> 8;
1039 if (has_alpha) pp++;
1047 *-----------------------------------------------------------------------------
1048 * pixbuf drawing (line)
1049 *-----------------------------------------------------------------------------
1052 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1053 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1054 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1056 gboolean flip = FALSE;
1063 t = x1; x1 = x2; x2 = t;
1064 t = y1; y1 = y2; y2 = t;
1068 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1072 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1076 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1084 slope = (y2 - y1) / d;
1087 y1 = y1 + slope * (clip_x - x1);
1090 if (x2 > clip_x + clip_w)
1092 y2 = y2 + slope * (clip_x + clip_w - x2);
1093 x2 = clip_x + clip_w;
1099 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1105 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1107 t = x1; x1 = x2; x2 = t;
1108 t = y1; y1 = y2; y2 = t;
1117 slope = (x2 - x1) / d;
1120 x1 = x1 + slope * (clip_y - y1);
1123 if (y2 > clip_y + clip_h)
1125 x2 = x2 + slope * (clip_y + clip_h - y2);
1126 y2 = clip_y + clip_h;
1148 void pixbuf_draw_line(GdkPixbuf *pb,
1149 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1150 gint x1, gint y1, gint x2, gint y2,
1151 guint8 r, guint8 g, guint8 b, guint8 a)
1155 gint rx, ry, rw, rh;
1156 gdouble rx1, ry1, rx2, ry2;
1163 gint cx1, cy1, cx2, cy2;
1167 pw = gdk_pixbuf_get_width(pb);
1168 ph = gdk_pixbuf_get_height(pb);
1170 if (!util_clip_region(0, 0, pw, ph,
1171 clip_x, clip_y, clip_w, clip_h,
1172 &rx, &ry, &rw, &rh)) return;
1173 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1174 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1175 &rx1, &ry1, &rx2, &ry2)) return;
1182 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1183 prs = gdk_pixbuf_get_rowstride(pb);
1184 p_pix = gdk_pixbuf_get_pixels(pb);
1186 p_step = (has_alpha) ? 4 : 3;
1188 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1193 t = rx1; rx1 = rx2; rx2 = t;
1194 t = ry1; ry1 = ry2; ry2 = t;
1198 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1199 for (x = rx1; x < rx2; x += 1.0)
1201 px = (gint)(x + 0.5);
1202 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1204 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1206 pp = p_pix + py * prs + px * p_step;
1207 *pp = (r * a + *pp * (256-a)) >> 8;
1209 *pp = (g * a + *pp * (256-a)) >> 8;
1211 *pp = (b * a + *pp * (256-a)) >> 8;
1220 t = rx1; rx1 = rx2; rx2 = t;
1221 t = ry1; ry1 = ry2; ry2 = t;
1225 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1226 for (y = ry1; y < ry2; y += 1.0)
1228 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1229 py = (gint)(y + 0.5);
1231 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1233 pp = p_pix + py * prs + px * p_step;
1234 *pp = (r * a + *pp * (256-a)) >> 8;
1236 *pp = (g * a + *pp * (256-a)) >> 8;
1238 *pp = (b * a + *pp * (256-a)) >> 8;
1245 *-----------------------------------------------------------------------------
1246 * pixbuf drawing (fades and shadows)
1247 *-----------------------------------------------------------------------------
1250 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1251 gint s, gboolean vertical, gint border,
1252 gint x1, gint y1, gint x2, gint y2,
1253 guint8 r, guint8 g, guint8 b, guint8 a)
1260 p_step = (has_alpha) ? 4 : 3;
1261 for (j = y1; j < y2; j++)
1263 pp = p_pix + j * prs + x1 * p_step;
1264 if (!vertical) n = a - a * abs(j - s) / border;
1265 for (i = x1; i < x2; i++)
1267 if (vertical) n = a - a * abs(i - s) / border;
1268 *pp = (r * n + *pp * (256-n)) >> 8;
1270 *pp = (g * n + *pp * (256-n)) >> 8;
1272 *pp = (b * n + *pp * (256-n)) >> 8;
1274 if (has_alpha) pp++;
1279 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1280 gint sx, gint sy, gint border,
1281 gint x1, gint y1, gint x2, gint y2,
1282 guint8 r, guint8 g, guint8 b, guint8 a)
1288 p_step = (has_alpha) ? 4 : 3;
1289 for (j = y1; j < y2; j++)
1291 pp = p_pix + j * prs + x1 * p_step;
1292 for (i = x1; i < x2; i++)
1297 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1298 n = a - a * r / border;
1299 *pp = (r * n + *pp * (256-n)) >> 8;
1301 *pp = (g * n + *pp * (256-n)) >> 8;
1303 *pp = (b * n + *pp * (256-n)) >> 8;
1305 if (has_alpha) pp++;
1310 void pixbuf_draw_shadow(GdkPixbuf *pb,
1311 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1312 gint x, gint y, gint w, gint h, gint border,
1313 guint8 r, guint8 g, guint8 b, guint8 a)
1317 gint rx, ry, rw, rh;
1318 gint fx, fy, fw, fh;
1323 pw = gdk_pixbuf_get_width(pb);
1324 ph = gdk_pixbuf_get_height(pb);
1326 if (!util_clip_region(0, 0, pw, ph,
1327 clip_x, clip_y, clip_w, clip_h,
1328 &rx, &ry, &rw, &rh)) return;
1330 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1331 prs = gdk_pixbuf_get_rowstride(pb);
1332 p_pix = gdk_pixbuf_get_pixels(pb);
1334 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1336 &fx, &fy, &fw, &fh))
1338 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1341 if (border < 1) return;
1343 if (util_clip_region(x, y + border, border, h - border * 2,
1345 &fx, &fy, &fw, &fh))
1347 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1348 x + border, TRUE, border,
1349 fx, fy, fx + fw, fy + fh,
1352 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1354 &fx, &fy, &fw, &fh))
1356 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1357 x + w - border, TRUE, border,
1358 fx, fy, fx + fw, fy + fh,
1361 if (util_clip_region(x + border, y, w - border * 2, border,
1363 &fx, &fy, &fw, &fh))
1365 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1366 y + border, FALSE, border,
1367 fx, fy, fx + fw, fy + fh,
1370 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1372 &fx, &fy, &fw, &fh))
1374 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1375 y + h - border, FALSE, border,
1376 fx, fy, fx + fw, fy + fh,
1379 if (util_clip_region(x, y, border, border,
1381 &fx, &fy, &fw, &fh))
1383 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1384 x + border, y + border, border,
1385 fx, fy, fx + fw, fy + fh,
1388 if (util_clip_region(x + w - border, y, border, border,
1390 &fx, &fy, &fw, &fh))
1392 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1393 x + w - border, y + border, border,
1394 fx, fy, fx + fw, fy + fh,
1397 if (util_clip_region(x, y + h - border, border, border,
1399 &fx, &fy, &fw, &fh))
1401 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1402 x + border, y + h - border, border,
1403 fx, fy, fx + fw, fy + fh,
1406 if (util_clip_region(x + w - border, y + h - border, border, border,
1408 &fx, &fy, &fw, &fh))
1410 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1411 x + w - border, y + h - border, border,
1412 fx, fy, fx + fw, fy + fh,
1419 *-----------------------------------------------------------------------------
1420 * pixbuf color alterations
1421 *-----------------------------------------------------------------------------
1424 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1425 gint x, gint y, gint w, gint h)
1435 pw = gdk_pixbuf_get_width(pb);
1436 ph = gdk_pixbuf_get_height(pb);
1438 if (x < 0 || x + w > pw) return;
1439 if (y < 0 || y + h > ph) return;
1441 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1442 prs = gdk_pixbuf_get_rowstride(pb);
1443 p_pix = gdk_pixbuf_get_pixels(pb);
1445 for (i = 0; i < h; i++)
1447 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1448 for (j = 0; j < w; j++)
1452 grey = (pp[0] + pp[1] + pp[2]) / 3;
1459 if (has_alpha) pp++;
1465 *-----------------------------------------------------------------------------
1466 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1468 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1478 pw = gdk_pixbuf_get_width(pb);
1479 ph = gdk_pixbuf_get_height(pb);
1481 if (x < 0 || x + w > pw) return;
1482 if (y < 0 || y + h > ph) return;
1484 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1485 prs = gdk_pixbuf_get_rowstride(pb);
1486 p_pix = gdk_pixbuf_get_pixels(pb);
1488 for (i = 0; i < h; i++)
1490 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1491 for (j = 0; j < w; j++)
1493 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1501 if (has_alpha) pp++;
1506 if (has_alpha) pp++;
1513 *-----------------------------------------------------------------------------
1514 * pixbuf ignore alpha
1515 *-----------------------------------------------------------------------------
1517 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1518 gint x, gint y, gint w, gint h)
1528 pw = gdk_pixbuf_get_width(pb);
1529 ph = gdk_pixbuf_get_height(pb);
1531 if (x < 0 || x + w > pw) return;
1532 if (y < 0 || y + h > ph) return;
1534 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1535 if (!has_alpha) return;
1537 prs = gdk_pixbuf_get_rowstride(pb);
1538 p_pix = gdk_pixbuf_get_pixels(pb);
1540 for (i = 0; i < h; i++)
1542 pp = p_pix + (y + i) * prs + (x * 4 );
1543 for (j = 0; j < w; j++)
1550 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */