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"
31 *-----------------------------------------------------------------------------
33 *-----------------------------------------------------------------------------
36 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
38 GError *error = nullptr;
41 if (!pixbuf || !filename) return FALSE;
43 ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
44 "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
48 log_printf("Error saving png file: %s\n", error->message);
56 *-----------------------------------------------------------------------------
58 *-----------------------------------------------------------------------------
61 //gboolean pixbuf_to_file_as_jpg(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
63 //GError *error = NULL;
67 //if (!pixbuf || !filename) return FALSE;
69 //if (quality == -1) quality = 75;
70 //if (quality < 1 || quality > 100)
72 //log_printf("Jpeg not saved, invalid quality %d\n", quality);
76 //qbuf = g_strdup_printf("%d", quality);
77 //ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
82 //log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
83 //g_error_free(error);
90 *-----------------------------------------------------------------------------
92 *-----------------------------------------------------------------------------
101 static PixbufInline inline_pixbuf_data[] = {
102 { PIXBUF_INLINE_ARCHIVE, "gq-archive-file.png" },
103 { PIXBUF_INLINE_BROKEN, "gq-sheet-broken.png" },
104 { PIXBUF_INLINE_COLLECTION, "gq-collection.png" },
105 { PIXBUF_INLINE_ICON, "gqview-icon.png" },
106 { PIXBUF_INLINE_ICON_180, "gq-icon-rotate-180.png" },
107 { PIXBUF_INLINE_ICON_BOOK, "gq-icon-book.png" },
108 { PIXBUF_INLINE_ICON_CCW, "gq-icon-rotate-counter-clockwise.png" },
109 { PIXBUF_INLINE_ICON_CONFIG, "gq-icon-config.png" },
110 { PIXBUF_INLINE_ICON_CW, "gq-icon-rotate-clockwise.png" },
111 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, "gq-icon-draw-rectangle.png" },
112 { PIXBUF_INLINE_ICON_EXIF, "gq-icon-exif.png" },
113 { PIXBUF_INLINE_ICON_EXPOSURE, "gq-icon-exposure.png" },
114 { PIXBUF_INLINE_ICON_FILE_FILTER, "gq-icon-file-filter.png" },
115 { PIXBUF_INLINE_ICON_FLIP, "gq-icon-flip.png" },
116 { PIXBUF_INLINE_ICON_FLOAT, "gq-icon-float.png" },
117 { PIXBUF_INLINE_ICON_GRAYSCALE, "gq-icon-grayscale.png" },
118 { PIXBUF_INLINE_ICON_HEIF, "gq-icon-heic.png" },
119 { PIXBUF_INLINE_ICON_HIDETOOLS, "gq-icon-hidetools.png" },
120 { PIXBUF_INLINE_ICON_INFO, "gq-icon-info.png" },
121 { PIXBUF_INLINE_ICON_MAINTENANCE, "gq-icon-maintenance.png" },
122 { PIXBUF_INLINE_ICON_MARKS, "gq-icon-marks.png" },
123 { PIXBUF_INLINE_ICON_MIRROR, "gq-icon-mirror.png" },
124 { PIXBUF_INLINE_ICON_MOVE, "gq-icon-move.png" },
125 { PIXBUF_INLINE_ICON_ORIGINAL, "gq-icon-original.png" },
126 { PIXBUF_INLINE_ICON_PANORAMA, "gq-icon-panorama.png" },
127 { PIXBUF_INLINE_ICON_PDF, "gq-icon-pdf.png" },
128 { PIXBUF_INLINE_ICON_RENAME, "gq-icon-rename.png" },
129 { PIXBUF_INLINE_ICON_SELECT_ALL, "gq-icon-select-all.png" },
130 { PIXBUF_INLINE_ICON_SELECT_INVERT, "gq-icon-select-invert.png" },
131 { PIXBUF_INLINE_ICON_SELECT_NONE, "gq-icon-select-none.png" },
132 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, "gq-icon-select-rectangle.png" },
133 { PIXBUF_INLINE_ICON_SORT, "gq-icon-sort.png" },
134 { PIXBUF_INLINE_ICON_THUMB, "gq-icon-thumb.png" },
135 { PIXBUF_INLINE_ICON_TOOLS, "gq-icon-tools.png" },
136 { PIXBUF_INLINE_ICON_TRASH, "gq-icon-trash.png" },
137 { PIXBUF_INLINE_ICON_VIEW, "gq-icon-view.png" },
138 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, "gq-icon-zoomfillhor.png" },
139 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, "gq-icon-zoomfillvert.png" },
140 { PIXBUF_INLINE_LOGO, "geeqie-logo.png" },
141 { PIXBUF_INLINE_METADATA, "gq-sheet-metadata.png" },
142 { PIXBUF_INLINE_SCROLLER, "gq-scroller.png" },
143 { PIXBUF_INLINE_SPLIT_PANE_SYNC, "gq-icon-split-pane-sync.png" },
144 { PIXBUF_INLINE_UNKNOWN, "gq-sheet-unknown.png" },
145 { PIXBUF_INLINE_VIDEO, "gq-sheet-video.png" },
149 GdkPixbuf *pixbuf_inline(const gchar *key)
151 GError *error = nullptr;
152 GInputStream *in_stream;
153 GdkPixbuf *icon_pixbuf;
157 if (!key) return nullptr;
160 while (inline_pixbuf_data[i].key)
162 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
164 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, inline_pixbuf_data[i].data, NULL);
166 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
171 log_printf("warning: inline pixbuf error: %s", error->message);
173 g_object_unref(in_stream);
177 icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, nullptr, &error);
178 g_object_unref(in_stream);
182 log_printf("warning: inline pixbuf error: %s", error->message);
192 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
197 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
199 static GtkIconFactory *icon_factory = nullptr;
200 GtkIconSet *icon_set;
204 icon_factory = gtk_icon_factory_new();
205 gtk_icon_factory_add_default(icon_factory);
208 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
209 gtk_icon_factory_add(icon_factory, key, icon_set);
213 void pixbuf_inline_register_stock_icons(void)
218 while (inline_pixbuf_data[i].key)
220 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
225 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
227 GtkIconTheme *icon_theme;
229 GError *error = nullptr;
231 icon_theme = gtk_icon_theme_get_default();
233 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
235 pixbuf = gtk_icon_theme_load_icon(icon_theme,
236 icon, /* icon name */
238 static_cast<GtkIconLookupFlags>(0), /* flags */
244 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
249 if (strchr(icon, '.'))
251 /* try again without extension */
252 gchar *icon2 = remove_extension_from_path(icon);
253 pixbuf = gtk_icon_theme_load_icon(icon_theme,
254 icon2, /* icon name */
256 static_cast<GtkIconLookupFlags>(0), /* flags */
260 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
264 /* try as an absolute path */
265 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
268 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
276 if (!pixbuf) return FALSE;
278 register_stock_icon(key, pixbuf);
282 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
283 gint old_w, gint old_h,
284 gint *new_w, gint *new_h)
286 if ((static_cast<gdouble>(req_w) / old_w) < (static_cast<gdouble>(req_h) / old_h))
289 *new_h = static_cast<gdouble>(*new_w) / old_w * old_h;
290 if (*new_h < 1) *new_h = 1;
295 *new_w = static_cast<gdouble>(*new_h) / old_h * old_w;
296 if (*new_w < 1) *new_w = 1;
299 return (*new_w != old_w || *new_h != old_h);
302 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
306 switch (fd->format_class)
308 case FORMAT_CLASS_UNKNOWN:
309 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
311 case FORMAT_CLASS_META:
312 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
314 case FORMAT_CLASS_VIDEO:
315 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
317 case FORMAT_CLASS_COLLECTION:
318 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
320 case FORMAT_CLASS_DOCUMENT:
321 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
323 case FORMAT_CLASS_ARCHIVE:
324 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
327 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
330 if (requested_width && requested_height)
332 gint w = gdk_pixbuf_get_width(pixbuf);
333 gint h = gdk_pixbuf_get_height(pixbuf);
335 if (w > requested_width || h > requested_height)
339 if (pixbuf_scale_aspect(requested_width, requested_height,
345 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
346 g_object_unref(G_OBJECT(tmp));
355 *-----------------------------------------------------------------------------
357 *-----------------------------------------------------------------------------
360 gboolean util_clip_region(gint x, gint y, gint w, gint h,
361 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
362 gint *rx, gint *ry, gint *rw, gint *rh)
364 if (clip_x + clip_w <= x ||
366 clip_y + clip_h <= y ||
372 *rx = MAX(x, clip_x);
373 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
375 *ry = MAX(y, clip_y);
376 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
382 *-----------------------------------------------------------------------------
384 *-----------------------------------------------------------------------------
387 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
388 guchar *dest, gint dest_row_stride, gint w, gint h,
389 gint bytes_per_pixel, gboolean counter_clockwise)
395 for (i = 0; i < h; i++)
397 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
398 for (j = 0; j < w; j++)
400 if (counter_clockwise)
402 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
406 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
408 *(dp++) = *(sp++); /* r */
409 *(dp++) = *(sp++); /* g */
410 *(dp++) = *(sp++); /* b */
411 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
417 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
418 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
424 for (i = 0; i < h; i++)
426 sp = src + (i * src_row_stride);
427 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
428 memcpy(dp, sp, w * bytes_per_pixel);
432 #define ROTATE_BUFFER_WIDTH 48
433 #define ROTATE_BUFFER_HEIGHT 48
436 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
439 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
454 if (!src) return nullptr;
456 sw = gdk_pixbuf_get_width(src);
457 sh = gdk_pixbuf_get_height(src);
458 has_alpha = gdk_pixbuf_get_has_alpha(src);
459 srs = gdk_pixbuf_get_rowstride(src);
460 s_pix = gdk_pixbuf_get_pixels(src);
464 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
465 drs = gdk_pixbuf_get_rowstride(dest);
466 d_pix = gdk_pixbuf_get_pixels(dest);
468 a = (has_alpha ? 4 : 3);
470 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
471 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
472 b_pix = gdk_pixbuf_get_pixels(buffer);
473 brs = gdk_pixbuf_get_rowstride(buffer);
475 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
477 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
478 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
482 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
483 pixbuf_copy_block_rotate(s_pix, srs, j, i,
485 a, counter_clockwise);
487 if (counter_clockwise)
497 pixbuf_copy_block(b_pix, brs, w, h,
498 d_pix, drs, x, y, a);
502 g_object_unref(buffer);
505 /* this is the simple version of rotation (roughly 2-4x slower) */
507 for (i = 0; i < sh; i++)
509 sp = s_pix + (i * srs);
510 for (j = 0; j < sw; j++)
512 if (counter_clockwise)
514 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
518 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
521 *(dp++) = *(sp++); /* r */
522 *(dp++) = *(sp++); /* g */
523 *(dp++) = *(sp++); /* b */
524 if (has_alpha) *(dp) = *(sp++); /* a */
533 * Returns a copy of pixbuf mirrored and or flipped.
534 * TO do a 180 degree rotations set both mirror and flipped TRUE
535 * if mirror and flip are FALSE, result is a simple copy.
537 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
550 if (!src) return nullptr;
552 w = gdk_pixbuf_get_width(src);
553 h = gdk_pixbuf_get_height(src);
554 has_alpha = gdk_pixbuf_get_has_alpha(src);
555 srs = gdk_pixbuf_get_rowstride(src);
556 s_pix = gdk_pixbuf_get_pixels(src);
558 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
559 drs = gdk_pixbuf_get_rowstride(dest);
560 d_pix = gdk_pixbuf_get_pixels(dest);
562 a = has_alpha ? 4 : 3;
564 for (i = 0; i < h; i++)
566 sp = s_pix + (i * srs);
569 dp = d_pix + ((h - i - 1) * drs);
573 dp = d_pix + (i * drs);
578 for (j = 0; j < w; j++)
580 *(dp++) = *(sp++); /* r */
581 *(dp++) = *(sp++); /* g */
582 *(dp++) = *(sp++); /* b */
583 if (has_alpha) *(dp) = *(sp++); /* a */
589 for (j = 0; j < w; j++)
591 *(dp++) = *(sp++); /* r */
592 *(dp++) = *(sp++); /* g */
593 *(dp++) = *(sp++); /* b */
594 if (has_alpha) *(dp++) = *(sp++); /* a */
602 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
605 GdkPixbuf *tmp = nullptr;
609 case EXIF_ORIENTATION_TOP_LEFT:
610 dest = gdk_pixbuf_copy(pixbuf);
612 case EXIF_ORIENTATION_TOP_RIGHT:
614 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
616 case EXIF_ORIENTATION_BOTTOM_RIGHT:
618 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
620 case EXIF_ORIENTATION_BOTTOM_LEFT:
622 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
624 case EXIF_ORIENTATION_LEFT_TOP:
625 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
626 dest = pixbuf_copy_rotate_90(tmp, FALSE);
628 case EXIF_ORIENTATION_RIGHT_TOP:
629 /* rotated -90 (270) */
630 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
632 case EXIF_ORIENTATION_RIGHT_BOTTOM:
633 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
634 dest = pixbuf_copy_rotate_90(tmp, TRUE);
636 case EXIF_ORIENTATION_LEFT_BOTTOM:
638 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
641 dest = gdk_pixbuf_copy(pixbuf);
644 if (tmp) g_object_unref(tmp);
651 *-----------------------------------------------------------------------------
652 * pixbuf drawing (rectangles)
653 *-----------------------------------------------------------------------------
657 * Fills region of pixbuf at x,y over w,h
658 * with colors red (r), green (g), blue (b)
659 * applying alpha (a), use a=255 for solid.
661 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
662 gint x, gint y, gint w, gint h,
663 gint r, gint g, gint b, gint a)
673 pw = gdk_pixbuf_get_width(pb);
674 ph = gdk_pixbuf_get_height(pb);
676 if (x < 0 || x + w > pw) return;
677 if (y < 0 || y + h > ph) return;
679 has_alpha = gdk_pixbuf_get_has_alpha(pb);
680 prs = gdk_pixbuf_get_rowstride(pb);
681 p_pix = gdk_pixbuf_get_pixels(pb);
683 for (i = 0; i < h; i++)
685 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
686 for (j = 0; j < w; j++)
688 *pp = (r * a + *pp * (256-a)) >> 8;
690 *pp = (g * a + *pp * (256-a)) >> 8;
692 *pp = (b * a + *pp * (256-a)) >> 8;
699 //void pixbuf_draw_rect(GdkPixbuf *pb,
700 //gint x, gint y, gint w, gint h,
701 //gint r, gint g, gint b, gint a,
702 //gint left, gint right, gint top, gint bottom)
704 //pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
706 //pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
708 //pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
710 //pixbuf_draw_rect_fill(pb, x, y, left, h,
714 void pixbuf_set_rect_fill(GdkPixbuf *pb,
715 gint x, gint y, gint w, gint h,
716 gint r, gint g, gint b, gint a)
726 pw = gdk_pixbuf_get_width(pb);
727 ph = gdk_pixbuf_get_height(pb);
729 if (x < 0 || x + w > pw) return;
730 if (y < 0 || y + h > ph) return;
732 has_alpha = gdk_pixbuf_get_has_alpha(pb);
733 prs = gdk_pixbuf_get_rowstride(pb);
734 p_pix = gdk_pixbuf_get_pixels(pb);
736 for (i = 0; i < h; i++)
738 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
739 for (j = 0; j < w; j++)
744 if (has_alpha) { *pp = a; pp++; }
749 void pixbuf_set_rect(GdkPixbuf *pb,
750 gint x, gint y, gint w, gint h,
751 gint r, gint g, gint b, gint a,
752 gint left, gint right, gint top, gint bottom)
754 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
756 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
758 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
760 pixbuf_set_rect_fill(pb, x, y, left, h,
764 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
771 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
772 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
774 buf = gdk_pixbuf_get_pixels(pb);
775 has_alpha = gdk_pixbuf_get_has_alpha(pb);
776 rowstride = gdk_pixbuf_get_rowstride(pb);
778 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
782 if (has_alpha) *p = a;
787 *-----------------------------------------------------------------------------
788 * pixbuf text rendering
789 *-----------------------------------------------------------------------------
792 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
793 GdkPixbuf *dest, gint dx, gint dy,
795 guint8 r, guint8 g, guint8 b, guint8 a)
810 if (!src || !dest) return;
812 sw = gdk_pixbuf_get_width(src);
813 sh = gdk_pixbuf_get_height(src);
815 if (sx < 0 || sx + w > sw) return;
816 if (sy < 0 || sy + h > sh) return;
818 dw = gdk_pixbuf_get_width(dest);
819 dh = gdk_pixbuf_get_height(dest);
821 if (dx < 0 || dx + w > dw) return;
822 if (dy < 0 || dy + h > dh) return;
824 s_alpha = gdk_pixbuf_get_has_alpha(src);
825 d_alpha = gdk_pixbuf_get_has_alpha(dest);
826 srs = gdk_pixbuf_get_rowstride(src);
827 drs = gdk_pixbuf_get_rowstride(dest);
828 s_pix = gdk_pixbuf_get_pixels(src);
829 d_pix = gdk_pixbuf_get_pixels(dest);
831 s_step = (s_alpha) ? 4 : 3;
832 d_step = (d_alpha) ? 4 : 3;
834 for (i = 0; i < h; i++)
836 sp = s_pix + (sy + i) * srs + sx * s_step;
837 dp = d_pix + (dy + i) * drs + dx * d_step;
838 for (j = 0; j < w; j++)
844 asub = a * sp[0] / 255;
845 *dp = (r * asub + *dp * (256-asub)) >> 8;
847 asub = a * sp[1] / 255;
848 *dp = (g * asub + *dp * (256-asub)) >> 8;
850 asub = a * sp[2] / 255;
851 *dp = (b * asub + *dp * (256-asub)) >> 8;
856 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
870 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *UNUSED(widget),
872 guint8 r, guint8 g, guint8 b, guint8 a)
878 cairo_surface_t *source;
881 pango_layout_get_pixel_size(layout, &w, &h);
882 if (w < 1 || h < 1) return;
884 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
886 cr = cairo_create (source);
887 cairo_set_source_rgb(cr, 0, 0, 0);
888 cairo_rectangle (cr, 0, 0, w, h);
890 cairo_set_source_rgb(cr, 1, 1, 1);
891 pango_cairo_show_layout (cr, layout);
894 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
896 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
898 cairo_image_surface_get_width (source),
899 cairo_image_surface_get_height (source),
900 cairo_image_surface_get_stride (source),
906 dw = gdk_pixbuf_get_width(pixbuf);
907 dh = gdk_pixbuf_get_height(pixbuf);
923 if (x + w > dw) w = dw - x;
924 if (y + h > dh) h = dh - y;
926 pixbuf_copy_font(buffer, sx, sy,
930 g_object_unref(buffer);
931 cairo_surface_destroy(source);
935 *-----------------------------------------------------------------------------
936 * pixbuf drawing (triangle)
937 *-----------------------------------------------------------------------------
940 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
941 gint *rx, gint *ry, gint *rw, gint *rh)
949 tw = MAX(abs(x1 - x2), abs(x2 - x3));
950 tw = MAX(tw, abs(x3 - x1));
951 th = MAX(abs(y1 - y2), abs(y2 - y3));
952 th = MAX(th, abs(y3 - y1));
960 void pixbuf_draw_triangle(GdkPixbuf *pb,
961 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
962 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
963 guint8 r, guint8 g, guint8 b, guint8 a)
975 gdouble slope1, slope2;
976 gint slope1_x, slope1_y;
979 gboolean middle = FALSE;
983 pw = gdk_pixbuf_get_width(pb);
984 ph = gdk_pixbuf_get_height(pb);
986 if (!util_clip_region(0, 0, pw, ph,
987 clip_x, clip_y, clip_w, clip_h,
988 &rx, &ry, &rw, &rh)) return;
990 util_clip_triangle(x1, y1, x2, y2, x3, y3,
993 if (!util_clip_region(rx, ry, rw, rh,
995 &fx1, &fy1, &fw, &fh)) return;
999 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1000 prs = gdk_pixbuf_get_rowstride(pb);
1001 p_pix = gdk_pixbuf_get_pixels(pb);
1003 p_step = (has_alpha) ? 4 : 3;
1007 t = x1; x1 = x2; x2 = t;
1008 t = y1; y1 = y2; y2 = t;
1012 t = x2; x2 = x3; x3 = t;
1013 t = y2; y2 = y3; y3 = t;
1017 t = x1; x1 = x2; x2 = t;
1018 t = y1; y1 = y2; y2 = t;
1021 slope1 = static_cast<gdouble>(y2 - y1);
1022 if (slope1) slope1 = static_cast<gdouble>(x2 - x1) / slope1;
1025 slope2 = static_cast<gdouble>(y3 - y1);
1026 if (slope2) slope2 = static_cast<gdouble>(x3 - x1) / slope2;
1028 for (y = fy1; y < fy2; y++)
1032 if (!middle && y > y2)
1034 slope1 = static_cast<gdouble>(y3 - y2);
1035 if (slope1) slope1 = static_cast<gdouble>(x3 - x2) / slope1;
1042 xa = slope1_x + (slope1 * (y - slope1_y) + 0.5);
1043 xb = x1 + (slope2 * (y - y1) + 0.5);
1047 t = xa; xa = xb; xb = t;
1050 xa = CLAMP(xa, fx1, fx2);
1051 xb = CLAMP(xb, fx1, fx2);
1053 pp = p_pix + y * prs + xa * p_step;
1057 *pp = (r * a + *pp * (256-a)) >> 8;
1059 *pp = (g * a + *pp * (256-a)) >> 8;
1061 *pp = (b * a + *pp * (256-a)) >> 8;
1063 if (has_alpha) pp++;
1071 *-----------------------------------------------------------------------------
1072 * pixbuf drawing (line)
1073 *-----------------------------------------------------------------------------
1076 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1077 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1078 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1080 gboolean flip = FALSE;
1087 t = x1; x1 = x2; x2 = t;
1088 t = y1; y1 = y2; y2 = t;
1092 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1096 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1100 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1108 slope = (y2 - y1) / d;
1111 y1 = y1 + slope * (clip_x - x1);
1114 if (x2 > clip_x + clip_w)
1116 y2 = y2 + slope * (clip_x + clip_w - x2);
1117 x2 = clip_x + clip_w;
1123 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1129 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1131 t = x1; x1 = x2; x2 = t;
1132 t = y1; y1 = y2; y2 = t;
1141 slope = (x2 - x1) / d;
1144 x1 = x1 + slope * (clip_y - y1);
1147 if (y2 > clip_y + clip_h)
1149 x2 = x2 + slope * (clip_y + clip_h - y2);
1150 y2 = clip_y + clip_h;
1172 void pixbuf_draw_line(GdkPixbuf *pb,
1173 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1174 gint x1, gint y1, gint x2, gint y2,
1175 guint8 r, guint8 g, guint8 b, guint8 a)
1179 gint rx, ry, rw, rh;
1180 gdouble rx1, ry1, rx2, ry2;
1187 gint cx1, cy1, cx2, cy2;
1191 pw = gdk_pixbuf_get_width(pb);
1192 ph = gdk_pixbuf_get_height(pb);
1194 if (!util_clip_region(0, 0, pw, ph,
1195 clip_x, clip_y, clip_w, clip_h,
1196 &rx, &ry, &rw, &rh)) return;
1197 if (!util_clip_line(static_cast<gdouble>(rx), static_cast<gdouble>(ry), static_cast<gdouble>(rw), static_cast<gdouble>(rh),
1198 static_cast<gdouble>(x1), static_cast<gdouble>(y1), static_cast<gdouble>(x2), static_cast<gdouble>(y2),
1199 &rx1, &ry1, &rx2, &ry2)) return;
1206 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1207 prs = gdk_pixbuf_get_rowstride(pb);
1208 p_pix = gdk_pixbuf_get_pixels(pb);
1210 p_step = (has_alpha) ? 4 : 3;
1212 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1217 t = rx1; rx1 = rx2; rx2 = t;
1218 t = ry1; ry1 = ry2; ry2 = t;
1222 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1223 for (x = rx1; x < rx2; x += 1.0)
1225 px = static_cast<gint>(x + 0.5);
1226 py = static_cast<gint>(ry1 + (x - rx1) * slope + 0.5);
1228 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1230 pp = p_pix + py * prs + px * p_step;
1231 *pp = (r * a + *pp * (256-a)) >> 8;
1233 *pp = (g * a + *pp * (256-a)) >> 8;
1235 *pp = (b * a + *pp * (256-a)) >> 8;
1244 t = rx1; rx1 = rx2; rx2 = t;
1245 t = ry1; ry1 = ry2; ry2 = t;
1249 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1250 for (y = ry1; y < ry2; y += 1.0)
1252 px = static_cast<gint>(rx1 + (y - ry1) * slope + 0.5);
1253 py = static_cast<gint>(y + 0.5);
1255 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1257 pp = p_pix + py * prs + px * p_step;
1258 *pp = (r * a + *pp * (256-a)) >> 8;
1260 *pp = (g * a + *pp * (256-a)) >> 8;
1262 *pp = (b * a + *pp * (256-a)) >> 8;
1269 *-----------------------------------------------------------------------------
1270 * pixbuf drawing (fades and shadows)
1271 *-----------------------------------------------------------------------------
1274 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1275 gint s, gboolean vertical, gint border,
1276 gint x1, gint y1, gint x2, gint y2,
1277 guint8 r, guint8 g, guint8 b, guint8 a)
1284 p_step = (has_alpha) ? 4 : 3;
1285 for (j = y1; j < y2; j++)
1287 pp = p_pix + j * prs + x1 * p_step;
1288 if (!vertical) n = a - a * abs(j - s) / border;
1289 for (i = x1; i < x2; i++)
1291 if (vertical) n = a - a * abs(i - s) / border;
1292 *pp = (r * n + *pp * (256-n)) >> 8;
1294 *pp = (g * n + *pp * (256-n)) >> 8;
1296 *pp = (b * n + *pp * (256-n)) >> 8;
1298 if (has_alpha) pp++;
1303 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1304 gint sx, gint sy, gint border,
1305 gint x1, gint y1, gint x2, gint y2,
1306 guint8 UNUSED(r), guint8 g, guint8 b, guint8 a)
1312 p_step = (has_alpha) ? 4 : 3;
1313 for (j = y1; j < y2; j++)
1315 pp = p_pix + j * prs + x1 * p_step;
1316 for (i = x1; i < x2; i++)
1321 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1322 n = a - a * r / border;
1323 *pp = (r * n + *pp * (256-n)) >> 8;
1325 *pp = (g * n + *pp * (256-n)) >> 8;
1327 *pp = (b * n + *pp * (256-n)) >> 8;
1329 if (has_alpha) pp++;
1334 void pixbuf_draw_shadow(GdkPixbuf *pb,
1335 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1336 gint x, gint y, gint w, gint h, gint border,
1337 guint8 r, guint8 g, guint8 b, guint8 a)
1341 gint rx, ry, rw, rh;
1342 gint fx, fy, fw, fh;
1347 pw = gdk_pixbuf_get_width(pb);
1348 ph = gdk_pixbuf_get_height(pb);
1350 if (!util_clip_region(0, 0, pw, ph,
1351 clip_x, clip_y, clip_w, clip_h,
1352 &rx, &ry, &rw, &rh)) return;
1354 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1355 prs = gdk_pixbuf_get_rowstride(pb);
1356 p_pix = gdk_pixbuf_get_pixels(pb);
1358 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1360 &fx, &fy, &fw, &fh))
1362 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1365 if (border < 1) return;
1367 if (util_clip_region(x, y + border, border, h - border * 2,
1369 &fx, &fy, &fw, &fh))
1371 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1372 x + border, TRUE, border,
1373 fx, fy, fx + fw, fy + fh,
1376 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1378 &fx, &fy, &fw, &fh))
1380 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1381 x + w - border, TRUE, border,
1382 fx, fy, fx + fw, fy + fh,
1385 if (util_clip_region(x + border, y, w - border * 2, border,
1387 &fx, &fy, &fw, &fh))
1389 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1390 y + border, FALSE, border,
1391 fx, fy, fx + fw, fy + fh,
1394 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1396 &fx, &fy, &fw, &fh))
1398 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1399 y + h - border, FALSE, border,
1400 fx, fy, fx + fw, fy + fh,
1403 if (util_clip_region(x, y, border, border,
1405 &fx, &fy, &fw, &fh))
1407 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1408 x + border, y + border, border,
1409 fx, fy, fx + fw, fy + fh,
1412 if (util_clip_region(x + w - border, y, border, border,
1414 &fx, &fy, &fw, &fh))
1416 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1417 x + w - border, y + border, border,
1418 fx, fy, fx + fw, fy + fh,
1421 if (util_clip_region(x, y + h - border, border, border,
1423 &fx, &fy, &fw, &fh))
1425 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1426 x + border, y + h - border, border,
1427 fx, fy, fx + fw, fy + fh,
1430 if (util_clip_region(x + w - border, y + h - border, border, border,
1432 &fx, &fy, &fw, &fh))
1434 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1435 x + w - border, y + h - border, border,
1436 fx, fy, fx + fw, fy + fh,
1443 *-----------------------------------------------------------------------------
1444 * pixbuf color alterations
1445 *-----------------------------------------------------------------------------
1448 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1449 gint x, gint y, gint w, gint h)
1459 pw = gdk_pixbuf_get_width(pb);
1460 ph = gdk_pixbuf_get_height(pb);
1462 if (x < 0 || x + w > pw) return;
1463 if (y < 0 || y + h > ph) return;
1465 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1466 prs = gdk_pixbuf_get_rowstride(pb);
1467 p_pix = gdk_pixbuf_get_pixels(pb);
1469 for (i = 0; i < h; i++)
1471 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1472 for (j = 0; j < w; j++)
1476 grey = (pp[0] + pp[1] + pp[2]) / 3;
1483 if (has_alpha) pp++;
1489 *-----------------------------------------------------------------------------
1490 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1492 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1502 pw = gdk_pixbuf_get_width(pb);
1503 ph = gdk_pixbuf_get_height(pb);
1505 if (x < 0 || x + w > pw) return;
1506 if (y < 0 || y + h > ph) return;
1508 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1509 prs = gdk_pixbuf_get_rowstride(pb);
1510 p_pix = gdk_pixbuf_get_pixels(pb);
1512 for (i = 0; i < h; i++)
1514 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1515 for (j = 0; j < w; j++)
1517 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1525 if (has_alpha) pp++;
1530 if (has_alpha) pp++;
1537 *-----------------------------------------------------------------------------
1538 * pixbuf ignore alpha
1539 *-----------------------------------------------------------------------------
1541 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1542 gint x, gint y, gint w, gint h)
1552 pw = gdk_pixbuf_get_width(pb);
1553 ph = gdk_pixbuf_get_height(pb);
1555 if (x < 0 || x + w > pw) return;
1556 if (y < 0 || y + h > ph) return;
1558 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1559 if (!has_alpha) return;
1561 prs = gdk_pixbuf_get_rowstride(pb);
1562 p_pix = gdk_pixbuf_get_pixels(pb);
1564 for (i = 0; i < h; i++)
1566 pp = p_pix + (y + i) * prs + (x * 4 );
1567 for (j = 0; j < w; j++)
1574 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */