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)
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 *-----------------------------------------------------------------------------
95 typedef struct _PixbufInline PixbufInline;
102 static PixbufInline inline_pixbuf_data[] = {
103 { PIXBUF_INLINE_ARCHIVE, "gq-archive-file.png" },
104 { PIXBUF_INLINE_BROKEN, "gq-sheet-broken.png" },
105 { PIXBUF_INLINE_COLLECTION, "gq-collection.png" },
106 { PIXBUF_INLINE_ICON, "gqview-icon.png" },
107 { PIXBUF_INLINE_ICON_180, "gq-icon-rotate-180.png" },
108 { PIXBUF_INLINE_ICON_BOOK, "gq-icon-book.png" },
109 { PIXBUF_INLINE_ICON_CCW, "gq-icon-rotate-counter-clockwise.png" },
110 { PIXBUF_INLINE_ICON_CONFIG, "gq-icon-config.png" },
111 { PIXBUF_INLINE_ICON_CW, "gq-icon-rotate-clockwise.png" },
112 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, "gq-icon-draw-rectangle.png" },
113 { PIXBUF_INLINE_ICON_EXIF, "gq-icon-exif.png" },
114 { PIXBUF_INLINE_ICON_EXPOSURE, "gq-icon-exposure.png" },
115 { PIXBUF_INLINE_ICON_FILE_FILTER, "gq-icon-file-filter.png" },
116 { PIXBUF_INLINE_ICON_FLIP, "gq-icon-flip.png" },
117 { PIXBUF_INLINE_ICON_FLOAT, "gq-icon-float.png" },
118 { PIXBUF_INLINE_ICON_GRAYSCALE, "gq-icon-grayscale.png" },
119 { PIXBUF_INLINE_ICON_HEIF, "gq-icon-heic.png" },
120 { PIXBUF_INLINE_ICON_HIDETOOLS, "gq-icon-hidetools.png" },
121 { PIXBUF_INLINE_ICON_INFO, "gq-icon-info.png" },
122 { PIXBUF_INLINE_ICON_MAINTENANCE, "gq-icon-maintenance.png" },
123 { PIXBUF_INLINE_ICON_MARKS, "gq-icon-marks.png" },
124 { PIXBUF_INLINE_ICON_MIRROR, "gq-icon-mirror.png" },
125 { PIXBUF_INLINE_ICON_MOVE, "gq-icon-move.png" },
126 { PIXBUF_INLINE_ICON_ORIGINAL, "gq-icon-original.png" },
127 { PIXBUF_INLINE_ICON_PANORAMA, "gq-icon-panorama.png" },
128 { PIXBUF_INLINE_ICON_PDF, "gq-icon-pdf.png" },
129 { PIXBUF_INLINE_ICON_RENAME, "gq-icon-rename.png" },
130 { PIXBUF_INLINE_ICON_SELECT_ALL, "gq-icon-select-all.png" },
131 { PIXBUF_INLINE_ICON_SELECT_INVERT, "gq-icon-select-invert.png" },
132 { PIXBUF_INLINE_ICON_SELECT_NONE, "gq-icon-select-none.png" },
133 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, "gq-icon-select-rectangle.png" },
134 { PIXBUF_INLINE_ICON_SORT, "gq-icon-sort.png" },
135 { PIXBUF_INLINE_ICON_THUMB, "gq-icon-thumb.png" },
136 { PIXBUF_INLINE_ICON_TOOLS, "gq-icon-tools.png" },
137 { PIXBUF_INLINE_ICON_TRASH, "gq-icon-trash.png" },
138 { PIXBUF_INLINE_ICON_VIEW, "gq-icon-view.png" },
139 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, "gq-icon-zoomfillhor.png" },
140 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, "gq-icon-zoomfillvert.png" },
141 { PIXBUF_INLINE_LOGO, "geeqie-logo.png" },
142 { PIXBUF_INLINE_METADATA, "gq-sheet-metadata.png" },
143 { PIXBUF_INLINE_SCROLLER, "gq-scroller.png" },
144 { PIXBUF_INLINE_SPLIT_PANE_SYNC, "gq-icon-split-pane-sync.png" },
145 { PIXBUF_INLINE_UNKNOWN, "gq-sheet-unknown.png" },
146 { PIXBUF_INLINE_VIDEO, "gq-sheet-video.png" },
150 GdkPixbuf *pixbuf_inline(const gchar *key)
152 GError *error = NULL;
153 GInputStream *in_stream;
154 GdkPixbuf *icon_pixbuf;
158 if (!key) return NULL;
161 while (inline_pixbuf_data[i].key)
163 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
165 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, inline_pixbuf_data[i].data, NULL);
167 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
172 log_printf("warning: inline pixbuf error: %s", error->message);
174 g_object_unref(in_stream);
178 icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, NULL, &error);
179 g_object_unref(in_stream);
183 log_printf("warning: inline pixbuf error: %s", error->message);
193 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
198 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
200 static GtkIconFactory *icon_factory = NULL;
201 GtkIconSet *icon_set;
205 icon_factory = gtk_icon_factory_new();
206 gtk_icon_factory_add_default(icon_factory);
209 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
210 gtk_icon_factory_add(icon_factory, key, icon_set);
214 void pixbuf_inline_register_stock_icons(void)
219 while (inline_pixbuf_data[i].key)
221 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
226 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
228 GtkIconTheme *icon_theme;
230 GError *error = NULL;
232 icon_theme = gtk_icon_theme_get_default();
234 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
236 pixbuf = gtk_icon_theme_load_icon(icon_theme,
237 icon, /* icon name */
239 static_cast<GtkIconLookupFlags>(0), /* flags */
245 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
250 if (strchr(icon, '.'))
252 /* try again without extension */
253 gchar *icon2 = remove_extension_from_path(icon);
254 pixbuf = gtk_icon_theme_load_icon(icon_theme,
255 icon2, /* icon name */
257 static_cast<GtkIconLookupFlags>(0), /* flags */
261 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
265 /* try as an absolute path */
266 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
269 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
277 if (!pixbuf) return FALSE;
279 register_stock_icon(key, pixbuf);
283 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
284 gint old_w, gint old_h,
285 gint *new_w, gint *new_h)
287 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
290 *new_h = (gdouble)*new_w / old_w * old_h;
291 if (*new_h < 1) *new_h = 1;
296 *new_w = (gdouble)*new_h / old_h * old_w;
297 if (*new_w < 1) *new_w = 1;
300 return (*new_w != old_w || *new_h != old_h);
303 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
307 switch (fd->format_class)
309 case FORMAT_CLASS_UNKNOWN:
310 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
312 case FORMAT_CLASS_META:
313 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
315 case FORMAT_CLASS_VIDEO:
316 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
318 case FORMAT_CLASS_COLLECTION:
319 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
321 case FORMAT_CLASS_DOCUMENT:
322 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
324 case FORMAT_CLASS_ARCHIVE:
325 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
328 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
331 if (requested_width && requested_height)
333 gint w = gdk_pixbuf_get_width(pixbuf);
334 gint h = gdk_pixbuf_get_height(pixbuf);
336 if (w > requested_width || h > requested_height)
340 if (pixbuf_scale_aspect(requested_width, requested_height,
346 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
347 g_object_unref(G_OBJECT(tmp));
356 *-----------------------------------------------------------------------------
358 *-----------------------------------------------------------------------------
361 gboolean util_clip_region(gint x, gint y, gint w, gint h,
362 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
363 gint *rx, gint *ry, gint *rw, gint *rh)
365 if (clip_x + clip_w <= x ||
367 clip_y + clip_h <= y ||
373 *rx = MAX(x, clip_x);
374 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
376 *ry = MAX(y, clip_y);
377 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
383 *-----------------------------------------------------------------------------
385 *-----------------------------------------------------------------------------
388 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
389 guchar *dest, gint dest_row_stride, gint w, gint h,
390 gint bytes_per_pixel, gboolean counter_clockwise)
396 for (i = 0; i < h; i++)
398 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
399 for (j = 0; j < w; j++)
401 if (counter_clockwise)
403 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
407 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
409 *(dp++) = *(sp++); /* r */
410 *(dp++) = *(sp++); /* g */
411 *(dp++) = *(sp++); /* b */
412 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
418 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
419 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
425 for (i = 0; i < h; i++)
427 sp = src + (i * src_row_stride);
428 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
429 memcpy(dp, sp, w * bytes_per_pixel);
433 #define ROTATE_BUFFER_WIDTH 48
434 #define ROTATE_BUFFER_HEIGHT 48
437 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
440 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
455 if (!src) return NULL;
457 sw = gdk_pixbuf_get_width(src);
458 sh = gdk_pixbuf_get_height(src);
459 has_alpha = gdk_pixbuf_get_has_alpha(src);
460 srs = gdk_pixbuf_get_rowstride(src);
461 s_pix = gdk_pixbuf_get_pixels(src);
465 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
466 drs = gdk_pixbuf_get_rowstride(dest);
467 d_pix = gdk_pixbuf_get_pixels(dest);
469 a = (has_alpha ? 4 : 3);
471 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
472 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
473 b_pix = gdk_pixbuf_get_pixels(buffer);
474 brs = gdk_pixbuf_get_rowstride(buffer);
476 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
478 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
479 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
483 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
484 pixbuf_copy_block_rotate(s_pix, srs, j, i,
486 a, counter_clockwise);
488 if (counter_clockwise)
498 pixbuf_copy_block(b_pix, brs, w, h,
499 d_pix, drs, x, y, a);
503 g_object_unref(buffer);
506 /* this is the simple version of rotation (roughly 2-4x slower) */
508 for (i = 0; i < sh; i++)
510 sp = s_pix + (i * srs);
511 for (j = 0; j < sw; j++)
513 if (counter_clockwise)
515 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
519 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
522 *(dp++) = *(sp++); /* r */
523 *(dp++) = *(sp++); /* g */
524 *(dp++) = *(sp++); /* b */
525 if (has_alpha) *(dp) = *(sp++); /* a */
534 * Returns a copy of pixbuf mirrored and or flipped.
535 * TO do a 180 degree rotations set both mirror and flipped TRUE
536 * if mirror and flip are FALSE, result is a simple copy.
538 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
551 if (!src) return NULL;
553 w = gdk_pixbuf_get_width(src);
554 h = gdk_pixbuf_get_height(src);
555 has_alpha = gdk_pixbuf_get_has_alpha(src);
556 srs = gdk_pixbuf_get_rowstride(src);
557 s_pix = gdk_pixbuf_get_pixels(src);
559 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
560 drs = gdk_pixbuf_get_rowstride(dest);
561 d_pix = gdk_pixbuf_get_pixels(dest);
563 a = has_alpha ? 4 : 3;
565 for (i = 0; i < h; i++)
567 sp = s_pix + (i * srs);
570 dp = d_pix + ((h - i - 1) * drs);
574 dp = d_pix + (i * drs);
579 for (j = 0; j < w; j++)
581 *(dp++) = *(sp++); /* r */
582 *(dp++) = *(sp++); /* g */
583 *(dp++) = *(sp++); /* b */
584 if (has_alpha) *(dp) = *(sp++); /* a */
590 for (j = 0; j < w; j++)
592 *(dp++) = *(sp++); /* r */
593 *(dp++) = *(sp++); /* g */
594 *(dp++) = *(sp++); /* b */
595 if (has_alpha) *(dp++) = *(sp++); /* a */
603 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
606 GdkPixbuf *tmp = NULL;
610 case EXIF_ORIENTATION_TOP_LEFT:
611 dest = gdk_pixbuf_copy(pixbuf);
613 case EXIF_ORIENTATION_TOP_RIGHT:
615 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
617 case EXIF_ORIENTATION_BOTTOM_RIGHT:
619 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
621 case EXIF_ORIENTATION_BOTTOM_LEFT:
623 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
625 case EXIF_ORIENTATION_LEFT_TOP:
626 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
627 dest = pixbuf_copy_rotate_90(tmp, FALSE);
629 case EXIF_ORIENTATION_RIGHT_TOP:
630 /* rotated -90 (270) */
631 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
633 case EXIF_ORIENTATION_RIGHT_BOTTOM:
634 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
635 dest = pixbuf_copy_rotate_90(tmp, TRUE);
637 case EXIF_ORIENTATION_LEFT_BOTTOM:
639 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
642 dest = gdk_pixbuf_copy(pixbuf);
645 if (tmp) g_object_unref(tmp);
652 *-----------------------------------------------------------------------------
653 * pixbuf drawing (rectangles)
654 *-----------------------------------------------------------------------------
658 * Fills region of pixbuf at x,y over w,h
659 * with colors red (r), green (g), blue (b)
660 * applying alpha (a), use a=255 for solid.
662 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
663 gint x, gint y, gint w, gint h,
664 gint r, gint g, gint b, gint a)
674 pw = gdk_pixbuf_get_width(pb);
675 ph = gdk_pixbuf_get_height(pb);
677 if (x < 0 || x + w > pw) return;
678 if (y < 0 || y + h > ph) return;
680 has_alpha = gdk_pixbuf_get_has_alpha(pb);
681 prs = gdk_pixbuf_get_rowstride(pb);
682 p_pix = gdk_pixbuf_get_pixels(pb);
684 for (i = 0; i < h; i++)
686 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
687 for (j = 0; j < w; j++)
689 *pp = (r * a + *pp * (256-a)) >> 8;
691 *pp = (g * a + *pp * (256-a)) >> 8;
693 *pp = (b * a + *pp * (256-a)) >> 8;
700 //void pixbuf_draw_rect(GdkPixbuf *pb,
701 //gint x, gint y, gint w, gint h,
702 //gint r, gint g, gint b, gint a,
703 //gint left, gint right, gint top, gint bottom)
705 //pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
707 //pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
709 //pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
711 //pixbuf_draw_rect_fill(pb, x, y, left, h,
715 void pixbuf_set_rect_fill(GdkPixbuf *pb,
716 gint x, gint y, gint w, gint h,
717 gint r, gint g, gint b, gint a)
727 pw = gdk_pixbuf_get_width(pb);
728 ph = gdk_pixbuf_get_height(pb);
730 if (x < 0 || x + w > pw) return;
731 if (y < 0 || y + h > ph) return;
733 has_alpha = gdk_pixbuf_get_has_alpha(pb);
734 prs = gdk_pixbuf_get_rowstride(pb);
735 p_pix = gdk_pixbuf_get_pixels(pb);
737 for (i = 0; i < h; i++)
739 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
740 for (j = 0; j < w; j++)
745 if (has_alpha) { *pp = a; pp++; }
750 void pixbuf_set_rect(GdkPixbuf *pb,
751 gint x, gint y, gint w, gint h,
752 gint r, gint g, gint b, gint a,
753 gint left, gint right, gint top, gint bottom)
755 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
757 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
759 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
761 pixbuf_set_rect_fill(pb, x, y, left, h,
765 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
772 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
773 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
775 buf = gdk_pixbuf_get_pixels(pb);
776 has_alpha = gdk_pixbuf_get_has_alpha(pb);
777 rowstride = gdk_pixbuf_get_rowstride(pb);
779 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
783 if (has_alpha) *p = a;
788 *-----------------------------------------------------------------------------
789 * pixbuf text rendering
790 *-----------------------------------------------------------------------------
793 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
794 GdkPixbuf *dest, gint dx, gint dy,
796 guint8 r, guint8 g, guint8 b, guint8 a)
811 if (!src || !dest) return;
813 sw = gdk_pixbuf_get_width(src);
814 sh = gdk_pixbuf_get_height(src);
816 if (sx < 0 || sx + w > sw) return;
817 if (sy < 0 || sy + h > sh) return;
819 dw = gdk_pixbuf_get_width(dest);
820 dh = gdk_pixbuf_get_height(dest);
822 if (dx < 0 || dx + w > dw) return;
823 if (dy < 0 || dy + h > dh) return;
825 s_alpha = gdk_pixbuf_get_has_alpha(src);
826 d_alpha = gdk_pixbuf_get_has_alpha(dest);
827 srs = gdk_pixbuf_get_rowstride(src);
828 drs = gdk_pixbuf_get_rowstride(dest);
829 s_pix = gdk_pixbuf_get_pixels(src);
830 d_pix = gdk_pixbuf_get_pixels(dest);
832 s_step = (s_alpha) ? 4 : 3;
833 d_step = (d_alpha) ? 4 : 3;
835 for (i = 0; i < h; i++)
837 sp = s_pix + (sy + i) * srs + sx * s_step;
838 dp = d_pix + (dy + i) * drs + dx * d_step;
839 for (j = 0; j < w; j++)
845 asub = a * sp[0] / 255;
846 *dp = (r * asub + *dp * (256-asub)) >> 8;
848 asub = a * sp[1] / 255;
849 *dp = (g * asub + *dp * (256-asub)) >> 8;
851 asub = a * sp[2] / 255;
852 *dp = (b * asub + *dp * (256-asub)) >> 8;
857 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
871 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *UNUSED(widget),
873 guint8 r, guint8 g, guint8 b, guint8 a)
879 cairo_surface_t *source;
882 pango_layout_get_pixel_size(layout, &w, &h);
883 if (w < 1 || h < 1) return;
885 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
887 cr = cairo_create (source);
888 cairo_set_source_rgb(cr, 0, 0, 0);
889 cairo_rectangle (cr, 0, 0, w, h);
891 cairo_set_source_rgb(cr, 1, 1, 1);
892 pango_cairo_show_layout (cr, layout);
895 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
897 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
899 cairo_image_surface_get_width (source),
900 cairo_image_surface_get_height (source),
901 cairo_image_surface_get_stride (source),
907 dw = gdk_pixbuf_get_width(pixbuf);
908 dh = gdk_pixbuf_get_height(pixbuf);
924 if (x + w > dw) w = dw - x;
925 if (y + h > dh) h = dh - y;
927 pixbuf_copy_font(buffer, sx, sy,
931 g_object_unref(buffer);
932 cairo_surface_destroy(source);
936 *-----------------------------------------------------------------------------
937 * pixbuf drawing (triangle)
938 *-----------------------------------------------------------------------------
941 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
942 gint *rx, gint *ry, gint *rw, gint *rh)
950 tw = MAX(abs(x1 - x2), abs(x2 - x3));
951 tw = MAX(tw, abs(x3 - x1));
952 th = MAX(abs(y1 - y2), abs(y2 - y3));
953 th = MAX(th, abs(y3 - y1));
961 void pixbuf_draw_triangle(GdkPixbuf *pb,
962 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
963 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
964 guint8 r, guint8 g, guint8 b, guint8 a)
976 gdouble slope1, slope2;
977 gint slope1_x, slope1_y;
980 gboolean middle = FALSE;
984 pw = gdk_pixbuf_get_width(pb);
985 ph = gdk_pixbuf_get_height(pb);
987 if (!util_clip_region(0, 0, pw, ph,
988 clip_x, clip_y, clip_w, clip_h,
989 &rx, &ry, &rw, &rh)) return;
991 util_clip_triangle(x1, y1, x2, y2, x3, y3,
994 if (!util_clip_region(rx, ry, rw, rh,
996 &fx1, &fy1, &fw, &fh)) return;
1000 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1001 prs = gdk_pixbuf_get_rowstride(pb);
1002 p_pix = gdk_pixbuf_get_pixels(pb);
1004 p_step = (has_alpha) ? 4 : 3;
1008 t = x1; x1 = x2; x2 = t;
1009 t = y1; y1 = y2; y2 = t;
1013 t = x2; x2 = x3; x3 = t;
1014 t = y2; y2 = y3; y3 = t;
1018 t = x1; x1 = x2; x2 = t;
1019 t = y1; y1 = y2; y2 = t;
1022 slope1 = (gdouble)(y2 - y1);
1023 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
1026 slope2 = (gdouble)(y3 - y1);
1027 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
1029 for (y = fy1; y < fy2; y++)
1033 if (!middle && y > y2)
1035 slope1 = (gdouble)(y3 - y2);
1036 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
1043 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
1044 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
1048 t = xa; xa = xb; xb = t;
1051 xa = CLAMP(xa, fx1, fx2);
1052 xb = CLAMP(xb, fx1, fx2);
1054 pp = p_pix + y * prs + xa * p_step;
1058 *pp = (r * a + *pp * (256-a)) >> 8;
1060 *pp = (g * a + *pp * (256-a)) >> 8;
1062 *pp = (b * a + *pp * (256-a)) >> 8;
1064 if (has_alpha) pp++;
1072 *-----------------------------------------------------------------------------
1073 * pixbuf drawing (line)
1074 *-----------------------------------------------------------------------------
1077 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1078 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1079 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1081 gboolean flip = FALSE;
1088 t = x1; x1 = x2; x2 = t;
1089 t = y1; y1 = y2; y2 = t;
1093 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1097 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1101 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1109 slope = (y2 - y1) / d;
1112 y1 = y1 + slope * (clip_x - x1);
1115 if (x2 > clip_x + clip_w)
1117 y2 = y2 + slope * (clip_x + clip_w - x2);
1118 x2 = clip_x + clip_w;
1124 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1130 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1132 t = x1; x1 = x2; x2 = t;
1133 t = y1; y1 = y2; y2 = t;
1142 slope = (x2 - x1) / d;
1145 x1 = x1 + slope * (clip_y - y1);
1148 if (y2 > clip_y + clip_h)
1150 x2 = x2 + slope * (clip_y + clip_h - y2);
1151 y2 = clip_y + clip_h;
1173 void pixbuf_draw_line(GdkPixbuf *pb,
1174 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1175 gint x1, gint y1, gint x2, gint y2,
1176 guint8 r, guint8 g, guint8 b, guint8 a)
1180 gint rx, ry, rw, rh;
1181 gdouble rx1, ry1, rx2, ry2;
1188 gint cx1, cy1, cx2, cy2;
1192 pw = gdk_pixbuf_get_width(pb);
1193 ph = gdk_pixbuf_get_height(pb);
1195 if (!util_clip_region(0, 0, pw, ph,
1196 clip_x, clip_y, clip_w, clip_h,
1197 &rx, &ry, &rw, &rh)) return;
1198 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1199 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1200 &rx1, &ry1, &rx2, &ry2)) return;
1207 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1208 prs = gdk_pixbuf_get_rowstride(pb);
1209 p_pix = gdk_pixbuf_get_pixels(pb);
1211 p_step = (has_alpha) ? 4 : 3;
1213 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1218 t = rx1; rx1 = rx2; rx2 = t;
1219 t = ry1; ry1 = ry2; ry2 = t;
1223 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1224 for (x = rx1; x < rx2; x += 1.0)
1226 px = (gint)(x + 0.5);
1227 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1229 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1231 pp = p_pix + py * prs + px * p_step;
1232 *pp = (r * a + *pp * (256-a)) >> 8;
1234 *pp = (g * a + *pp * (256-a)) >> 8;
1236 *pp = (b * a + *pp * (256-a)) >> 8;
1245 t = rx1; rx1 = rx2; rx2 = t;
1246 t = ry1; ry1 = ry2; ry2 = t;
1250 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1251 for (y = ry1; y < ry2; y += 1.0)
1253 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1254 py = (gint)(y + 0.5);
1256 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1258 pp = p_pix + py * prs + px * p_step;
1259 *pp = (r * a + *pp * (256-a)) >> 8;
1261 *pp = (g * a + *pp * (256-a)) >> 8;
1263 *pp = (b * a + *pp * (256-a)) >> 8;
1270 *-----------------------------------------------------------------------------
1271 * pixbuf drawing (fades and shadows)
1272 *-----------------------------------------------------------------------------
1275 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1276 gint s, gboolean vertical, gint border,
1277 gint x1, gint y1, gint x2, gint y2,
1278 guint8 r, guint8 g, guint8 b, guint8 a)
1285 p_step = (has_alpha) ? 4 : 3;
1286 for (j = y1; j < y2; j++)
1288 pp = p_pix + j * prs + x1 * p_step;
1289 if (!vertical) n = a - a * abs(j - s) / border;
1290 for (i = x1; i < x2; i++)
1292 if (vertical) n = a - a * abs(i - s) / border;
1293 *pp = (r * n + *pp * (256-n)) >> 8;
1295 *pp = (g * n + *pp * (256-n)) >> 8;
1297 *pp = (b * n + *pp * (256-n)) >> 8;
1299 if (has_alpha) pp++;
1304 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1305 gint sx, gint sy, gint border,
1306 gint x1, gint y1, gint x2, gint y2,
1307 guint8 UNUSED(r), guint8 g, guint8 b, guint8 a)
1313 p_step = (has_alpha) ? 4 : 3;
1314 for (j = y1; j < y2; j++)
1316 pp = p_pix + j * prs + x1 * p_step;
1317 for (i = x1; i < x2; i++)
1322 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1323 n = a - a * r / border;
1324 *pp = (r * n + *pp * (256-n)) >> 8;
1326 *pp = (g * n + *pp * (256-n)) >> 8;
1328 *pp = (b * n + *pp * (256-n)) >> 8;
1330 if (has_alpha) pp++;
1335 void pixbuf_draw_shadow(GdkPixbuf *pb,
1336 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1337 gint x, gint y, gint w, gint h, gint border,
1338 guint8 r, guint8 g, guint8 b, guint8 a)
1342 gint rx, ry, rw, rh;
1343 gint fx, fy, fw, fh;
1348 pw = gdk_pixbuf_get_width(pb);
1349 ph = gdk_pixbuf_get_height(pb);
1351 if (!util_clip_region(0, 0, pw, ph,
1352 clip_x, clip_y, clip_w, clip_h,
1353 &rx, &ry, &rw, &rh)) return;
1355 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1356 prs = gdk_pixbuf_get_rowstride(pb);
1357 p_pix = gdk_pixbuf_get_pixels(pb);
1359 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1361 &fx, &fy, &fw, &fh))
1363 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1366 if (border < 1) return;
1368 if (util_clip_region(x, y + border, border, h - border * 2,
1370 &fx, &fy, &fw, &fh))
1372 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1373 x + border, TRUE, border,
1374 fx, fy, fx + fw, fy + fh,
1377 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1379 &fx, &fy, &fw, &fh))
1381 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1382 x + w - border, TRUE, border,
1383 fx, fy, fx + fw, fy + fh,
1386 if (util_clip_region(x + border, y, w - border * 2, border,
1388 &fx, &fy, &fw, &fh))
1390 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1391 y + border, FALSE, border,
1392 fx, fy, fx + fw, fy + fh,
1395 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1397 &fx, &fy, &fw, &fh))
1399 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1400 y + h - border, FALSE, border,
1401 fx, fy, fx + fw, fy + fh,
1404 if (util_clip_region(x, y, border, border,
1406 &fx, &fy, &fw, &fh))
1408 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1409 x + border, y + border, border,
1410 fx, fy, fx + fw, fy + fh,
1413 if (util_clip_region(x + w - border, y, border, border,
1415 &fx, &fy, &fw, &fh))
1417 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1418 x + w - border, y + border, border,
1419 fx, fy, fx + fw, fy + fh,
1422 if (util_clip_region(x, y + h - border, border, border,
1424 &fx, &fy, &fw, &fh))
1426 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1427 x + border, y + h - border, border,
1428 fx, fy, fx + fw, fy + fh,
1431 if (util_clip_region(x + w - border, y + h - border, border, border,
1433 &fx, &fy, &fw, &fh))
1435 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1436 x + w - border, y + h - border, border,
1437 fx, fy, fx + fw, fy + fh,
1444 *-----------------------------------------------------------------------------
1445 * pixbuf color alterations
1446 *-----------------------------------------------------------------------------
1449 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1450 gint x, gint y, gint w, gint h)
1460 pw = gdk_pixbuf_get_width(pb);
1461 ph = gdk_pixbuf_get_height(pb);
1463 if (x < 0 || x + w > pw) return;
1464 if (y < 0 || y + h > ph) return;
1466 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1467 prs = gdk_pixbuf_get_rowstride(pb);
1468 p_pix = gdk_pixbuf_get_pixels(pb);
1470 for (i = 0; i < h; i++)
1472 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1473 for (j = 0; j < w; j++)
1477 grey = (pp[0] + pp[1] + pp[2]) / 3;
1484 if (has_alpha) pp++;
1490 *-----------------------------------------------------------------------------
1491 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1493 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1503 pw = gdk_pixbuf_get_width(pb);
1504 ph = gdk_pixbuf_get_height(pb);
1506 if (x < 0 || x + w > pw) return;
1507 if (y < 0 || y + h > ph) return;
1509 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1510 prs = gdk_pixbuf_get_rowstride(pb);
1511 p_pix = gdk_pixbuf_get_pixels(pb);
1513 for (i = 0; i < h; i++)
1515 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1516 for (j = 0; j < w; j++)
1518 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1526 if (has_alpha) pp++;
1531 if (has_alpha) pp++;
1538 *-----------------------------------------------------------------------------
1539 * pixbuf ignore alpha
1540 *-----------------------------------------------------------------------------
1542 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1543 gint x, gint y, gint w, gint h)
1553 pw = gdk_pixbuf_get_width(pb);
1554 ph = gdk_pixbuf_get_height(pb);
1556 if (x < 0 || x + w > pw) return;
1557 if (y < 0 || y + h > ph) return;
1559 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1560 if (!has_alpha) return;
1562 prs = gdk_pixbuf_get_rowstride(pb);
1563 p_pix = gdk_pixbuf_get_pixels(pb);
1565 for (i = 0; i < h; i++)
1567 pp = p_pix + (y + i) * prs + (x * 4 );
1568 for (j = 0; j < w; j++)
1575 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */