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 },
144 GdkPixbuf *pixbuf_inline(const gchar *key)
148 if (!key) return NULL;
151 while (inline_pixbuf_data[i].key)
153 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
155 return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
160 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
165 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
167 static GtkIconFactory *icon_factory = NULL;
168 GtkIconSet *icon_set;
172 icon_factory = gtk_icon_factory_new();
173 gtk_icon_factory_add_default(icon_factory);
176 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
177 gtk_icon_factory_add(icon_factory, key, icon_set);
181 void pixbuf_inline_register_stock_icons(void)
186 while (inline_pixbuf_data[i].key)
188 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
193 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
195 GtkIconTheme *icon_theme;
197 GError *error = NULL;
199 icon_theme = gtk_icon_theme_get_default();
201 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
203 pixbuf = gtk_icon_theme_load_icon(icon_theme,
204 icon, /* icon name */
212 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
217 if (strchr(icon, '.'))
219 /* try again without extension */
220 gchar *icon2 = remove_extension_from_path(icon);
221 pixbuf = gtk_icon_theme_load_icon(icon_theme,
222 icon2, /* icon name */
228 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
235 if (!pixbuf) return FALSE;
237 register_stock_icon(key, pixbuf);
241 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
242 gint old_w, gint old_h,
243 gint *new_w, gint *new_h)
245 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
248 *new_h = (gdouble)*new_w / old_w * old_h;
249 if (*new_h < 1) *new_h = 1;
254 *new_w = (gdouble)*new_h / old_h * old_w;
255 if (*new_w < 1) *new_w = 1;
258 return (*new_w != old_w || *new_h != old_h);
261 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
265 switch (fd->format_class)
267 case FORMAT_CLASS_UNKNOWN:
268 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
270 case FORMAT_CLASS_META:
271 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
273 case FORMAT_CLASS_VIDEO:
274 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
276 case FORMAT_CLASS_COLLECTION:
277 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
279 case FORMAT_CLASS_PDF:
280 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
283 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
286 if (requested_width && requested_height)
288 gint w = gdk_pixbuf_get_width(pixbuf);
289 gint h = gdk_pixbuf_get_height(pixbuf);
291 if (w > requested_width || h > requested_height)
295 if (pixbuf_scale_aspect(requested_width, requested_height,
301 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
302 g_object_unref(G_OBJECT(tmp));
311 *-----------------------------------------------------------------------------
313 *-----------------------------------------------------------------------------
316 gboolean util_clip_region(gint x, gint y, gint w, gint h,
317 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
318 gint *rx, gint *ry, gint *rw, gint *rh)
320 if (clip_x + clip_w <= x ||
322 clip_y + clip_h <= y ||
328 *rx = MAX(x, clip_x);
329 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
331 *ry = MAX(y, clip_y);
332 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
338 *-----------------------------------------------------------------------------
340 *-----------------------------------------------------------------------------
343 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
344 guchar *dest, gint dest_row_stride, gint w, gint h,
345 gint bytes_per_pixel, gboolean counter_clockwise)
351 for (i = 0; i < h; i++)
353 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
354 for (j = 0; j < w; j++)
356 if (counter_clockwise)
358 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
362 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
364 *(dp++) = *(sp++); /* r */
365 *(dp++) = *(sp++); /* g */
366 *(dp++) = *(sp++); /* b */
367 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
373 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
374 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
380 for (i = 0; i < h; i++)
382 sp = src + (i * src_row_stride);
383 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
384 memcpy(dp, sp, w * bytes_per_pixel);
388 #define ROTATE_BUFFER_WIDTH 48
389 #define ROTATE_BUFFER_HEIGHT 48
392 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
395 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
410 if (!src) return NULL;
412 sw = gdk_pixbuf_get_width(src);
413 sh = gdk_pixbuf_get_height(src);
414 has_alpha = gdk_pixbuf_get_has_alpha(src);
415 srs = gdk_pixbuf_get_rowstride(src);
416 s_pix = gdk_pixbuf_get_pixels(src);
420 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
421 drs = gdk_pixbuf_get_rowstride(dest);
422 d_pix = gdk_pixbuf_get_pixels(dest);
424 a = (has_alpha ? 4 : 3);
426 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
427 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
428 b_pix = gdk_pixbuf_get_pixels(buffer);
429 brs = gdk_pixbuf_get_rowstride(buffer);
431 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
433 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
434 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
438 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
439 pixbuf_copy_block_rotate(s_pix, srs, j, i,
441 a, counter_clockwise);
443 if (counter_clockwise)
453 pixbuf_copy_block(b_pix, brs, w, h,
454 d_pix, drs, x, y, a);
458 g_object_unref(buffer);
461 /* this is the simple version of rotation (roughly 2-4x slower) */
463 for (i = 0; i < sh; i++)
465 sp = s_pix + (i * srs);
466 for (j = 0; j < sw; j++)
468 if (counter_clockwise)
470 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
474 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
477 *(dp++) = *(sp++); /* r */
478 *(dp++) = *(sp++); /* g */
479 *(dp++) = *(sp++); /* b */
480 if (has_alpha) *(dp) = *(sp++); /* a */
489 * Returns a copy of pixbuf mirrored and or flipped.
490 * TO do a 180 degree rotations set both mirror and flipped TRUE
491 * if mirror and flip are FALSE, result is a simple copy.
493 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
506 if (!src) return NULL;
508 w = gdk_pixbuf_get_width(src);
509 h = gdk_pixbuf_get_height(src);
510 has_alpha = gdk_pixbuf_get_has_alpha(src);
511 srs = gdk_pixbuf_get_rowstride(src);
512 s_pix = gdk_pixbuf_get_pixels(src);
514 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
515 drs = gdk_pixbuf_get_rowstride(dest);
516 d_pix = gdk_pixbuf_get_pixels(dest);
518 a = has_alpha ? 4 : 3;
520 for (i = 0; i < h; i++)
522 sp = s_pix + (i * srs);
525 dp = d_pix + ((h - i - 1) * drs);
529 dp = d_pix + (i * drs);
534 for (j = 0; j < w; j++)
536 *(dp++) = *(sp++); /* r */
537 *(dp++) = *(sp++); /* g */
538 *(dp++) = *(sp++); /* b */
539 if (has_alpha) *(dp) = *(sp++); /* a */
545 for (j = 0; j < w; j++)
547 *(dp++) = *(sp++); /* r */
548 *(dp++) = *(sp++); /* g */
549 *(dp++) = *(sp++); /* b */
550 if (has_alpha) *(dp++) = *(sp++); /* a */
558 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
561 GdkPixbuf *tmp = NULL;
565 case EXIF_ORIENTATION_TOP_LEFT:
566 dest = gdk_pixbuf_copy(pixbuf);
568 case EXIF_ORIENTATION_TOP_RIGHT:
570 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
572 case EXIF_ORIENTATION_BOTTOM_RIGHT:
574 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
576 case EXIF_ORIENTATION_BOTTOM_LEFT:
578 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
580 case EXIF_ORIENTATION_LEFT_TOP:
581 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
582 dest = pixbuf_copy_rotate_90(tmp, FALSE);
584 case EXIF_ORIENTATION_RIGHT_TOP:
585 /* rotated -90 (270) */
586 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
588 case EXIF_ORIENTATION_RIGHT_BOTTOM:
589 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
590 dest = pixbuf_copy_rotate_90(tmp, TRUE);
592 case EXIF_ORIENTATION_LEFT_BOTTOM:
594 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
597 dest = gdk_pixbuf_copy(pixbuf);
600 if (tmp) g_object_unref(tmp);
607 *-----------------------------------------------------------------------------
608 * pixbuf drawing (rectangles)
609 *-----------------------------------------------------------------------------
613 * Fills region of pixbuf at x,y over w,h
614 * with colors red (r), green (g), blue (b)
615 * applying alpha (a), use a=255 for solid.
617 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
618 gint x, gint y, gint w, gint h,
619 gint r, gint g, gint b, gint a)
629 pw = gdk_pixbuf_get_width(pb);
630 ph = gdk_pixbuf_get_height(pb);
632 if (x < 0 || x + w > pw) return;
633 if (y < 0 || y + h > ph) return;
635 has_alpha = gdk_pixbuf_get_has_alpha(pb);
636 prs = gdk_pixbuf_get_rowstride(pb);
637 p_pix = gdk_pixbuf_get_pixels(pb);
639 for (i = 0; i < h; i++)
641 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
642 for (j = 0; j < w; j++)
644 *pp = (r * a + *pp * (256-a)) >> 8;
646 *pp = (g * a + *pp * (256-a)) >> 8;
648 *pp = (b * a + *pp * (256-a)) >> 8;
655 void pixbuf_draw_rect(GdkPixbuf *pb,
656 gint x, gint y, gint w, gint h,
657 gint r, gint g, gint b, gint a,
658 gint left, gint right, gint top, gint bottom)
660 pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
662 pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
664 pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
666 pixbuf_draw_rect_fill(pb, x, y, left, h,
670 void pixbuf_set_rect_fill(GdkPixbuf *pb,
671 gint x, gint y, gint w, gint h,
672 gint r, gint g, gint b, gint a)
682 pw = gdk_pixbuf_get_width(pb);
683 ph = gdk_pixbuf_get_height(pb);
685 if (x < 0 || x + w > pw) return;
686 if (y < 0 || y + h > ph) return;
688 has_alpha = gdk_pixbuf_get_has_alpha(pb);
689 prs = gdk_pixbuf_get_rowstride(pb);
690 p_pix = gdk_pixbuf_get_pixels(pb);
692 for (i = 0; i < h; i++)
694 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
695 for (j = 0; j < w; j++)
700 if (has_alpha) { *pp = a; pp++; }
705 void pixbuf_set_rect(GdkPixbuf *pb,
706 gint x, gint y, gint w, gint h,
707 gint r, gint g, gint b, gint a,
708 gint left, gint right, gint top, gint bottom)
710 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
712 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
714 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
716 pixbuf_set_rect_fill(pb, x, y, left, h,
720 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
727 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
728 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
730 buf = gdk_pixbuf_get_pixels(pb);
731 has_alpha = gdk_pixbuf_get_has_alpha(pb);
732 rowstride = gdk_pixbuf_get_rowstride(pb);
734 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
738 if (has_alpha) *p = a;
743 *-----------------------------------------------------------------------------
744 * pixbuf text rendering
745 *-----------------------------------------------------------------------------
748 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
749 GdkPixbuf *dest, gint dx, gint dy,
751 guint8 r, guint8 g, guint8 b, guint8 a)
766 if (!src || !dest) return;
768 sw = gdk_pixbuf_get_width(src);
769 sh = gdk_pixbuf_get_height(src);
771 if (sx < 0 || sx + w > sw) return;
772 if (sy < 0 || sy + h > sh) return;
774 dw = gdk_pixbuf_get_width(dest);
775 dh = gdk_pixbuf_get_height(dest);
777 if (dx < 0 || dx + w > dw) return;
778 if (dy < 0 || dy + h > dh) return;
780 s_alpha = gdk_pixbuf_get_has_alpha(src);
781 d_alpha = gdk_pixbuf_get_has_alpha(dest);
782 srs = gdk_pixbuf_get_rowstride(src);
783 drs = gdk_pixbuf_get_rowstride(dest);
784 s_pix = gdk_pixbuf_get_pixels(src);
785 d_pix = gdk_pixbuf_get_pixels(dest);
787 s_step = (s_alpha) ? 4 : 3;
788 d_step = (d_alpha) ? 4 : 3;
790 for (i = 0; i < h; i++)
792 sp = s_pix + (sy + i) * srs + sx * s_step;
793 dp = d_pix + (dy + i) * drs + dx * d_step;
794 for (j = 0; j < w; j++)
800 asub = a * sp[0] / 255;
801 *dp = (r * asub + *dp * (256-asub)) >> 8;
803 asub = a * sp[1] / 255;
804 *dp = (g * asub + *dp * (256-asub)) >> 8;
806 asub = a * sp[2] / 255;
807 *dp = (b * asub + *dp * (256-asub)) >> 8;
812 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
826 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
828 guint8 r, guint8 g, guint8 b, guint8 a)
834 cairo_surface_t *source;
837 pango_layout_get_pixel_size(layout, &w, &h);
838 if (w < 1 || h < 1) return;
840 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
842 cr = cairo_create (source);
843 cairo_set_source_rgb(cr, 0, 0, 0);
844 cairo_rectangle (cr, 0, 0, w, h);
846 cairo_set_source_rgb(cr, 1, 1, 1);
847 pango_cairo_show_layout (cr, layout);
850 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
852 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
854 cairo_image_surface_get_width (source),
855 cairo_image_surface_get_height (source),
856 cairo_image_surface_get_stride (source),
862 dw = gdk_pixbuf_get_width(pixbuf);
863 dh = gdk_pixbuf_get_height(pixbuf);
879 if (x + w > dw) w = dw - x;
880 if (y + h > dh) h = dh - y;
882 pixbuf_copy_font(buffer, sx, sy,
886 g_object_unref(buffer);
887 cairo_surface_destroy(source);
891 *-----------------------------------------------------------------------------
892 * pixbuf drawing (triangle)
893 *-----------------------------------------------------------------------------
896 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
897 gint *rx, gint *ry, gint *rw, gint *rh)
905 tw = MAX(abs(x1 - x2), abs(x2 - x3));
906 tw = MAX(tw, abs(x3 - x1));
907 th = MAX(abs(y1 - y2), abs(y2 - y3));
908 th = MAX(th, abs(y3 - y1));
916 void pixbuf_draw_triangle(GdkPixbuf *pb,
917 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
918 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
919 guint8 r, guint8 g, guint8 b, guint8 a)
931 gdouble slope1, slope2;
932 gint slope1_x, slope1_y;
935 gboolean middle = FALSE;
939 pw = gdk_pixbuf_get_width(pb);
940 ph = gdk_pixbuf_get_height(pb);
942 if (!util_clip_region(0, 0, pw, ph,
943 clip_x, clip_y, clip_w, clip_h,
944 &rx, &ry, &rw, &rh)) return;
946 util_clip_triangle(x1, y1, x2, y2, x3, y3,
949 if (!util_clip_region(rx, ry, rw, rh,
951 &fx1, &fy1, &fw, &fh)) return;
955 has_alpha = gdk_pixbuf_get_has_alpha(pb);
956 prs = gdk_pixbuf_get_rowstride(pb);
957 p_pix = gdk_pixbuf_get_pixels(pb);
959 p_step = (has_alpha) ? 4 : 3;
963 t = x1; x1 = x2; x2 = t;
964 t = y1; y1 = y2; y2 = t;
968 t = x2; x2 = x3; x3 = t;
969 t = y2; y2 = y3; y3 = t;
973 t = x1; x1 = x2; x2 = t;
974 t = y1; y1 = y2; y2 = t;
977 slope1 = (gdouble)(y2 - y1);
978 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
981 slope2 = (gdouble)(y3 - y1);
982 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
984 for (y = fy1; y < fy2; y++)
988 if (!middle && y > y2)
990 slope1 = (gdouble)(y3 - y2);
991 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
998 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
999 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
1003 t = xa; xa = xb; xb = t;
1006 xa = CLAMP(xa, fx1, fx2);
1007 xb = CLAMP(xb, fx1, fx2);
1009 pp = p_pix + y * prs + xa * p_step;
1013 *pp = (r * a + *pp * (256-a)) >> 8;
1015 *pp = (g * a + *pp * (256-a)) >> 8;
1017 *pp = (b * a + *pp * (256-a)) >> 8;
1019 if (has_alpha) pp++;
1027 *-----------------------------------------------------------------------------
1028 * pixbuf drawing (line)
1029 *-----------------------------------------------------------------------------
1032 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1033 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1034 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1036 gboolean flip = FALSE;
1043 t = x1; x1 = x2; x2 = t;
1044 t = y1; y1 = y2; y2 = t;
1048 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1052 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1056 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1064 slope = (y2 - y1) / d;
1067 y1 = y1 + slope * (clip_x - x1);
1070 if (x2 > clip_x + clip_w)
1072 y2 = y2 + slope * (clip_x + clip_w - x2);
1073 x2 = clip_x + clip_w;
1079 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1085 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1087 t = x1; x1 = x2; x2 = t;
1088 t = y1; y1 = y2; y2 = t;
1097 slope = (x2 - x1) / d;
1100 x1 = x1 + slope * (clip_y - y1);
1103 if (y2 > clip_y + clip_h)
1105 x2 = x2 + slope * (clip_y + clip_h - y2);
1106 y2 = clip_y + clip_h;
1128 void pixbuf_draw_line(GdkPixbuf *pb,
1129 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1130 gint x1, gint y1, gint x2, gint y2,
1131 guint8 r, guint8 g, guint8 b, guint8 a)
1135 gint rx, ry, rw, rh;
1136 gdouble rx1, ry1, rx2, ry2;
1143 gint cx1, cy1, cx2, cy2;
1147 pw = gdk_pixbuf_get_width(pb);
1148 ph = gdk_pixbuf_get_height(pb);
1150 if (!util_clip_region(0, 0, pw, ph,
1151 clip_x, clip_y, clip_w, clip_h,
1152 &rx, &ry, &rw, &rh)) return;
1153 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1154 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1155 &rx1, &ry1, &rx2, &ry2)) return;
1162 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1163 prs = gdk_pixbuf_get_rowstride(pb);
1164 p_pix = gdk_pixbuf_get_pixels(pb);
1166 p_step = (has_alpha) ? 4 : 3;
1168 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1173 t = rx1; rx1 = rx2; rx2 = t;
1174 t = ry1; ry1 = ry2; ry2 = t;
1178 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1179 for (x = rx1; x < rx2; x += 1.0)
1181 px = (gint)(x + 0.5);
1182 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1184 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1186 pp = p_pix + py * prs + px * p_step;
1187 *pp = (r * a + *pp * (256-a)) >> 8;
1189 *pp = (g * a + *pp * (256-a)) >> 8;
1191 *pp = (b * a + *pp * (256-a)) >> 8;
1200 t = rx1; rx1 = rx2; rx2 = t;
1201 t = ry1; ry1 = ry2; ry2 = t;
1205 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1206 for (y = ry1; y < ry2; y += 1.0)
1208 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1209 py = (gint)(y + 0.5);
1211 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1213 pp = p_pix + py * prs + px * p_step;
1214 *pp = (r * a + *pp * (256-a)) >> 8;
1216 *pp = (g * a + *pp * (256-a)) >> 8;
1218 *pp = (b * a + *pp * (256-a)) >> 8;
1225 *-----------------------------------------------------------------------------
1226 * pixbuf drawing (fades and shadows)
1227 *-----------------------------------------------------------------------------
1230 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1231 gint s, gboolean vertical, gint border,
1232 gint x1, gint y1, gint x2, gint y2,
1233 guint8 r, guint8 g, guint8 b, guint8 a)
1240 p_step = (has_alpha) ? 4 : 3;
1241 for (j = y1; j < y2; j++)
1243 pp = p_pix + j * prs + x1 * p_step;
1244 if (!vertical) n = a - a * abs(j - s) / border;
1245 for (i = x1; i < x2; i++)
1247 if (vertical) n = a - a * abs(i - s) / border;
1248 *pp = (r * n + *pp * (256-n)) >> 8;
1250 *pp = (g * n + *pp * (256-n)) >> 8;
1252 *pp = (b * n + *pp * (256-n)) >> 8;
1254 if (has_alpha) pp++;
1259 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1260 gint sx, gint sy, gint border,
1261 gint x1, gint y1, gint x2, gint y2,
1262 guint8 r, guint8 g, guint8 b, guint8 a)
1268 p_step = (has_alpha) ? 4 : 3;
1269 for (j = y1; j < y2; j++)
1271 pp = p_pix + j * prs + x1 * p_step;
1272 for (i = x1; i < x2; i++)
1277 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1278 n = a - a * r / border;
1279 *pp = (r * n + *pp * (256-n)) >> 8;
1281 *pp = (g * n + *pp * (256-n)) >> 8;
1283 *pp = (b * n + *pp * (256-n)) >> 8;
1285 if (has_alpha) pp++;
1290 void pixbuf_draw_shadow(GdkPixbuf *pb,
1291 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1292 gint x, gint y, gint w, gint h, gint border,
1293 guint8 r, guint8 g, guint8 b, guint8 a)
1297 gint rx, ry, rw, rh;
1298 gint fx, fy, fw, fh;
1303 pw = gdk_pixbuf_get_width(pb);
1304 ph = gdk_pixbuf_get_height(pb);
1306 if (!util_clip_region(0, 0, pw, ph,
1307 clip_x, clip_y, clip_w, clip_h,
1308 &rx, &ry, &rw, &rh)) return;
1310 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1311 prs = gdk_pixbuf_get_rowstride(pb);
1312 p_pix = gdk_pixbuf_get_pixels(pb);
1314 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1316 &fx, &fy, &fw, &fh))
1318 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1321 if (border < 1) return;
1323 if (util_clip_region(x, y + border, border, h - border * 2,
1325 &fx, &fy, &fw, &fh))
1327 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1328 x + border, TRUE, border,
1329 fx, fy, fx + fw, fy + fh,
1332 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1334 &fx, &fy, &fw, &fh))
1336 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1337 x + w - border, TRUE, border,
1338 fx, fy, fx + fw, fy + fh,
1341 if (util_clip_region(x + border, y, w - border * 2, border,
1343 &fx, &fy, &fw, &fh))
1345 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1346 y + border, FALSE, border,
1347 fx, fy, fx + fw, fy + fh,
1350 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1352 &fx, &fy, &fw, &fh))
1354 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1355 y + h - border, FALSE, border,
1356 fx, fy, fx + fw, fy + fh,
1359 if (util_clip_region(x, y, border, border,
1361 &fx, &fy, &fw, &fh))
1363 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1364 x + border, y + border, border,
1365 fx, fy, fx + fw, fy + fh,
1368 if (util_clip_region(x + w - border, y, border, border,
1370 &fx, &fy, &fw, &fh))
1372 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1373 x + w - border, y + border, border,
1374 fx, fy, fx + fw, fy + fh,
1377 if (util_clip_region(x, y + h - border, border, border,
1379 &fx, &fy, &fw, &fh))
1381 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1382 x + border, y + h - border, border,
1383 fx, fy, fx + fw, fy + fh,
1386 if (util_clip_region(x + w - border, y + h - border, border, border,
1388 &fx, &fy, &fw, &fh))
1390 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1391 x + w - border, y + h - border, border,
1392 fx, fy, fx + fw, fy + fh,
1399 *-----------------------------------------------------------------------------
1400 * pixbuf color alterations
1401 *-----------------------------------------------------------------------------
1404 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1405 gint x, gint y, gint w, gint h)
1415 pw = gdk_pixbuf_get_width(pb);
1416 ph = gdk_pixbuf_get_height(pb);
1418 if (x < 0 || x + w > pw) return;
1419 if (y < 0 || y + h > ph) return;
1421 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1422 prs = gdk_pixbuf_get_rowstride(pb);
1423 p_pix = gdk_pixbuf_get_pixels(pb);
1425 for (i = 0; i < h; i++)
1427 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1428 for (j = 0; j < w; j++)
1432 grey = (pp[0] + pp[1] + pp[2]) / 3;
1439 if (has_alpha) pp++;
1443 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */