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_FOLDER_CLOSED, "gq-folder-closed.png" },
107 { PIXBUF_INLINE_FOLDER_LOCKED, "gq-folder-locked.png" },
108 { PIXBUF_INLINE_FOLDER_OPEN, "gq-folder-open.png" },
109 { PIXBUF_INLINE_FOLDER_UP, "gq-folder-up.png" },
110 { PIXBUF_INLINE_ICON, "gqview-icon.png" },
111 { PIXBUF_INLINE_ICON_180, "gq-icon-rotate-180.png" },
112 { PIXBUF_INLINE_ICON_BOOK, "gq-icon-book.png" },
113 { PIXBUF_INLINE_ICON_CCW, "gq-icon-rotate-counter-clockwise.png" },
114 { PIXBUF_INLINE_ICON_CONFIG, "gq-icon-config.png" },
115 { PIXBUF_INLINE_ICON_CW, "gq-icon-rotate-clockwise.png" },
116 { PIXBUF_INLINE_ICON_DRAW_RECTANGLE, "gq-icon-draw-rectangle.png" },
117 { PIXBUF_INLINE_ICON_EXIF, "gq-icon-exif.png" },
118 { PIXBUF_INLINE_ICON_EXPOSURE, "gq-icon-exposure.png" },
119 { PIXBUF_INLINE_ICON_FILE_FILTER, "gq-icon-file-filter.png" },
120 { PIXBUF_INLINE_ICON_FLIP, "gq-icon-flip.png" },
121 { PIXBUF_INLINE_ICON_FLOAT, "gq-icon-float.png" },
122 { PIXBUF_INLINE_ICON_GRAYSCALE, "gq-icon-grayscale.png" },
123 { PIXBUF_INLINE_ICON_HEIF, "gq-icon-heic.png" },
124 { PIXBUF_INLINE_ICON_HIDETOOLS, "gq-icon-hidetools.png" },
125 { PIXBUF_INLINE_ICON_INFO, "gq-icon-info.png" },
126 { PIXBUF_INLINE_ICON_MAINTENANCE, "gq-icon-maintenance.png" },
127 { PIXBUF_INLINE_ICON_MARKS, "gq-icon-marks.png" },
128 { PIXBUF_INLINE_ICON_MIRROR, "gq-icon-mirror.png" },
129 { PIXBUF_INLINE_ICON_MOVE, "gq-icon-move.png" },
130 { PIXBUF_INLINE_ICON_ORIGINAL, "gq-icon-original.png" },
131 { PIXBUF_INLINE_ICON_PANORAMA, "gq-icon-panorama.png" },
132 { PIXBUF_INLINE_ICON_PDF, "gq-icon-pdf.png" },
133 { PIXBUF_INLINE_ICON_RENAME, "gq-icon-rename.png" },
134 { PIXBUF_INLINE_ICON_SELECT_ALL, "gq-icon-select-all.png" },
135 { PIXBUF_INLINE_ICON_SELECT_INVERT, "gq-icon-select-invert.png" },
136 { PIXBUF_INLINE_ICON_SELECT_NONE, "gq-icon-select-none.png" },
137 { PIXBUF_INLINE_ICON_SELECT_RECTANGLE, "gq-icon-select-rectangle.png" },
138 { PIXBUF_INLINE_ICON_SORT, "gq-icon-sort.png" },
139 { PIXBUF_INLINE_ICON_THUMB, "gq-icon-thumb.png" },
140 { PIXBUF_INLINE_ICON_TOOLS, "gq-icon-tools.png" },
141 { PIXBUF_INLINE_ICON_TRASH, "gq-icon-trash.png" },
142 { PIXBUF_INLINE_ICON_VIEW, "gq-icon-view.png" },
143 { PIXBUF_INLINE_ICON_ZOOMFILLHOR, "gq-icon-zoomfillhor.png" },
144 { PIXBUF_INLINE_ICON_ZOOMFILLVERT, "gq-icon-zoomfillvert.png" },
145 { PIXBUF_INLINE_LOGO, "geeqie-logo.png" },
146 { PIXBUF_INLINE_METADATA, "gq-sheet-metadata.png" },
147 { PIXBUF_INLINE_SCROLLER, "gq-scroller.png" },
148 { PIXBUF_INLINE_SPLIT_PANE_SYNC, "gq-icon-split-pane-sync.png" },
149 { PIXBUF_INLINE_UNKNOWN, "gq-sheet-unknown.png" },
150 { PIXBUF_INLINE_VIDEO, "gq-sheet-video.png" },
154 GdkPixbuf *pixbuf_inline(const gchar *key)
156 GError *error = NULL;
157 GInputStream *in_stream;
158 GdkPixbuf *icon_pixbuf;
162 if (!key) return NULL;
165 while (inline_pixbuf_data[i].key)
167 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
169 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, inline_pixbuf_data[i].data, NULL);
171 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
176 log_printf("warning: inline pixbuf error: %s", error->message);
178 g_object_unref(in_stream);
182 icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, NULL, &error);
183 g_object_unref(in_stream);
187 log_printf("warning: inline pixbuf error: %s", error->message);
197 log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
202 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
204 static GtkIconFactory *icon_factory = NULL;
205 GtkIconSet *icon_set;
209 icon_factory = gtk_icon_factory_new();
210 gtk_icon_factory_add_default(icon_factory);
213 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
214 gtk_icon_factory_add(icon_factory, key, icon_set);
218 void pixbuf_inline_register_stock_icons(void)
223 while (inline_pixbuf_data[i].key)
225 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
230 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
232 GtkIconTheme *icon_theme;
234 GError *error = NULL;
236 icon_theme = gtk_icon_theme_get_default();
238 if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
240 pixbuf = gtk_icon_theme_load_icon(icon_theme,
241 icon, /* icon name */
243 static_cast<GtkIconLookupFlags>(0), /* flags */
249 DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
254 if (strchr(icon, '.'))
256 /* try again without extension */
257 gchar *icon2 = remove_extension_from_path(icon);
258 pixbuf = gtk_icon_theme_load_icon(icon_theme,
259 icon2, /* icon name */
261 static_cast<GtkIconLookupFlags>(0), /* flags */
265 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
269 /* try as an absolute path */
270 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
273 DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
281 if (!pixbuf) return FALSE;
283 register_stock_icon(key, pixbuf);
287 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
288 gint old_w, gint old_h,
289 gint *new_w, gint *new_h)
291 if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
294 *new_h = (gdouble)*new_w / old_w * old_h;
295 if (*new_h < 1) *new_h = 1;
300 *new_w = (gdouble)*new_h / old_h * old_w;
301 if (*new_w < 1) *new_w = 1;
304 return (*new_w != old_w || *new_h != old_h);
307 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
311 switch (fd->format_class)
313 case FORMAT_CLASS_UNKNOWN:
314 pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
316 case FORMAT_CLASS_META:
317 pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
319 case FORMAT_CLASS_VIDEO:
320 pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
322 case FORMAT_CLASS_COLLECTION:
323 pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
325 case FORMAT_CLASS_DOCUMENT:
326 pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
328 case FORMAT_CLASS_ARCHIVE:
329 pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
332 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
335 if (requested_width && requested_height)
337 gint w = gdk_pixbuf_get_width(pixbuf);
338 gint h = gdk_pixbuf_get_height(pixbuf);
340 if (w > requested_width || h > requested_height)
344 if (pixbuf_scale_aspect(requested_width, requested_height,
350 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
351 g_object_unref(G_OBJECT(tmp));
360 *-----------------------------------------------------------------------------
362 *-----------------------------------------------------------------------------
365 gboolean util_clip_region(gint x, gint y, gint w, gint h,
366 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
367 gint *rx, gint *ry, gint *rw, gint *rh)
369 if (clip_x + clip_w <= x ||
371 clip_y + clip_h <= y ||
377 *rx = MAX(x, clip_x);
378 *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
380 *ry = MAX(y, clip_y);
381 *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
387 *-----------------------------------------------------------------------------
389 *-----------------------------------------------------------------------------
392 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
393 guchar *dest, gint dest_row_stride, gint w, gint h,
394 gint bytes_per_pixel, gboolean counter_clockwise)
400 for (i = 0; i < h; i++)
402 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
403 for (j = 0; j < w; j++)
405 if (counter_clockwise)
407 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
411 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
413 *(dp++) = *(sp++); /* r */
414 *(dp++) = *(sp++); /* g */
415 *(dp++) = *(sp++); /* b */
416 if (bytes_per_pixel == 4) *(dp) = *(sp++); /* a */
422 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
423 guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
429 for (i = 0; i < h; i++)
431 sp = src + (i * src_row_stride);
432 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
433 memcpy(dp, sp, w * bytes_per_pixel);
437 #define ROTATE_BUFFER_WIDTH 48
438 #define ROTATE_BUFFER_HEIGHT 48
441 * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
444 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
459 if (!src) return NULL;
461 sw = gdk_pixbuf_get_width(src);
462 sh = gdk_pixbuf_get_height(src);
463 has_alpha = gdk_pixbuf_get_has_alpha(src);
464 srs = gdk_pixbuf_get_rowstride(src);
465 s_pix = gdk_pixbuf_get_pixels(src);
469 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
470 drs = gdk_pixbuf_get_rowstride(dest);
471 d_pix = gdk_pixbuf_get_pixels(dest);
473 a = (has_alpha ? 4 : 3);
475 buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
476 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
477 b_pix = gdk_pixbuf_get_pixels(buffer);
478 brs = gdk_pixbuf_get_rowstride(buffer);
480 for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
482 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
483 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
487 h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
488 pixbuf_copy_block_rotate(s_pix, srs, j, i,
490 a, counter_clockwise);
492 if (counter_clockwise)
502 pixbuf_copy_block(b_pix, brs, w, h,
503 d_pix, drs, x, y, a);
507 g_object_unref(buffer);
510 /* this is the simple version of rotation (roughly 2-4x slower) */
512 for (i = 0; i < sh; i++)
514 sp = s_pix + (i * srs);
515 for (j = 0; j < sw; j++)
517 if (counter_clockwise)
519 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
523 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
526 *(dp++) = *(sp++); /* r */
527 *(dp++) = *(sp++); /* g */
528 *(dp++) = *(sp++); /* b */
529 if (has_alpha) *(dp) = *(sp++); /* a */
538 * Returns a copy of pixbuf mirrored and or flipped.
539 * TO do a 180 degree rotations set both mirror and flipped TRUE
540 * if mirror and flip are FALSE, result is a simple copy.
542 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
555 if (!src) return NULL;
557 w = gdk_pixbuf_get_width(src);
558 h = gdk_pixbuf_get_height(src);
559 has_alpha = gdk_pixbuf_get_has_alpha(src);
560 srs = gdk_pixbuf_get_rowstride(src);
561 s_pix = gdk_pixbuf_get_pixels(src);
563 dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
564 drs = gdk_pixbuf_get_rowstride(dest);
565 d_pix = gdk_pixbuf_get_pixels(dest);
567 a = has_alpha ? 4 : 3;
569 for (i = 0; i < h; i++)
571 sp = s_pix + (i * srs);
574 dp = d_pix + ((h - i - 1) * drs);
578 dp = d_pix + (i * drs);
583 for (j = 0; j < w; j++)
585 *(dp++) = *(sp++); /* r */
586 *(dp++) = *(sp++); /* g */
587 *(dp++) = *(sp++); /* b */
588 if (has_alpha) *(dp) = *(sp++); /* a */
594 for (j = 0; j < w; j++)
596 *(dp++) = *(sp++); /* r */
597 *(dp++) = *(sp++); /* g */
598 *(dp++) = *(sp++); /* b */
599 if (has_alpha) *(dp++) = *(sp++); /* a */
607 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
610 GdkPixbuf *tmp = NULL;
614 case EXIF_ORIENTATION_TOP_LEFT:
615 dest = gdk_pixbuf_copy(pixbuf);
617 case EXIF_ORIENTATION_TOP_RIGHT:
619 dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
621 case EXIF_ORIENTATION_BOTTOM_RIGHT:
623 dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
625 case EXIF_ORIENTATION_BOTTOM_LEFT:
627 dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
629 case EXIF_ORIENTATION_LEFT_TOP:
630 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
631 dest = pixbuf_copy_rotate_90(tmp, FALSE);
633 case EXIF_ORIENTATION_RIGHT_TOP:
634 /* rotated -90 (270) */
635 dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
637 case EXIF_ORIENTATION_RIGHT_BOTTOM:
638 tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
639 dest = pixbuf_copy_rotate_90(tmp, TRUE);
641 case EXIF_ORIENTATION_LEFT_BOTTOM:
643 dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
646 dest = gdk_pixbuf_copy(pixbuf);
649 if (tmp) g_object_unref(tmp);
656 *-----------------------------------------------------------------------------
657 * pixbuf drawing (rectangles)
658 *-----------------------------------------------------------------------------
662 * Fills region of pixbuf at x,y over w,h
663 * with colors red (r), green (g), blue (b)
664 * applying alpha (a), use a=255 for solid.
666 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
667 gint x, gint y, gint w, gint h,
668 gint r, gint g, gint b, gint a)
678 pw = gdk_pixbuf_get_width(pb);
679 ph = gdk_pixbuf_get_height(pb);
681 if (x < 0 || x + w > pw) return;
682 if (y < 0 || y + h > ph) return;
684 has_alpha = gdk_pixbuf_get_has_alpha(pb);
685 prs = gdk_pixbuf_get_rowstride(pb);
686 p_pix = gdk_pixbuf_get_pixels(pb);
688 for (i = 0; i < h; i++)
690 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
691 for (j = 0; j < w; j++)
693 *pp = (r * a + *pp * (256-a)) >> 8;
695 *pp = (g * a + *pp * (256-a)) >> 8;
697 *pp = (b * a + *pp * (256-a)) >> 8;
704 //void pixbuf_draw_rect(GdkPixbuf *pb,
705 //gint x, gint y, gint w, gint h,
706 //gint r, gint g, gint b, gint a,
707 //gint left, gint right, gint top, gint bottom)
709 //pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
711 //pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
713 //pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
715 //pixbuf_draw_rect_fill(pb, x, y, left, h,
719 void pixbuf_set_rect_fill(GdkPixbuf *pb,
720 gint x, gint y, gint w, gint h,
721 gint r, gint g, gint b, gint a)
731 pw = gdk_pixbuf_get_width(pb);
732 ph = gdk_pixbuf_get_height(pb);
734 if (x < 0 || x + w > pw) return;
735 if (y < 0 || y + h > ph) return;
737 has_alpha = gdk_pixbuf_get_has_alpha(pb);
738 prs = gdk_pixbuf_get_rowstride(pb);
739 p_pix = gdk_pixbuf_get_pixels(pb);
741 for (i = 0; i < h; i++)
743 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
744 for (j = 0; j < w; j++)
749 if (has_alpha) { *pp = a; pp++; }
754 void pixbuf_set_rect(GdkPixbuf *pb,
755 gint x, gint y, gint w, gint h,
756 gint r, gint g, gint b, gint a,
757 gint left, gint right, gint top, gint bottom)
759 pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
761 pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
763 pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
765 pixbuf_set_rect_fill(pb, x, y, left, h,
769 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
776 if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
777 y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
779 buf = gdk_pixbuf_get_pixels(pb);
780 has_alpha = gdk_pixbuf_get_has_alpha(pb);
781 rowstride = gdk_pixbuf_get_rowstride(pb);
783 p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
787 if (has_alpha) *p = a;
792 *-----------------------------------------------------------------------------
793 * pixbuf text rendering
794 *-----------------------------------------------------------------------------
797 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
798 GdkPixbuf *dest, gint dx, gint dy,
800 guint8 r, guint8 g, guint8 b, guint8 a)
815 if (!src || !dest) return;
817 sw = gdk_pixbuf_get_width(src);
818 sh = gdk_pixbuf_get_height(src);
820 if (sx < 0 || sx + w > sw) return;
821 if (sy < 0 || sy + h > sh) return;
823 dw = gdk_pixbuf_get_width(dest);
824 dh = gdk_pixbuf_get_height(dest);
826 if (dx < 0 || dx + w > dw) return;
827 if (dy < 0 || dy + h > dh) return;
829 s_alpha = gdk_pixbuf_get_has_alpha(src);
830 d_alpha = gdk_pixbuf_get_has_alpha(dest);
831 srs = gdk_pixbuf_get_rowstride(src);
832 drs = gdk_pixbuf_get_rowstride(dest);
833 s_pix = gdk_pixbuf_get_pixels(src);
834 d_pix = gdk_pixbuf_get_pixels(dest);
836 s_step = (s_alpha) ? 4 : 3;
837 d_step = (d_alpha) ? 4 : 3;
839 for (i = 0; i < h; i++)
841 sp = s_pix + (sy + i) * srs + sx * s_step;
842 dp = d_pix + (dy + i) * drs + dx * d_step;
843 for (j = 0; j < w; j++)
849 asub = a * sp[0] / 255;
850 *dp = (r * asub + *dp * (256-asub)) >> 8;
852 asub = a * sp[1] / 255;
853 *dp = (g * asub + *dp * (256-asub)) >> 8;
855 asub = a * sp[2] / 255;
856 *dp = (b * asub + *dp * (256-asub)) >> 8;
861 *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
875 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *UNUSED(widget),
877 guint8 r, guint8 g, guint8 b, guint8 a)
883 cairo_surface_t *source;
886 pango_layout_get_pixel_size(layout, &w, &h);
887 if (w < 1 || h < 1) return;
889 source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
891 cr = cairo_create (source);
892 cairo_set_source_rgb(cr, 0, 0, 0);
893 cairo_rectangle (cr, 0, 0, w, h);
895 cairo_set_source_rgb(cr, 1, 1, 1);
896 pango_cairo_show_layout (cr, layout);
899 buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
901 cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
903 cairo_image_surface_get_width (source),
904 cairo_image_surface_get_height (source),
905 cairo_image_surface_get_stride (source),
911 dw = gdk_pixbuf_get_width(pixbuf);
912 dh = gdk_pixbuf_get_height(pixbuf);
928 if (x + w > dw) w = dw - x;
929 if (y + h > dh) h = dh - y;
931 pixbuf_copy_font(buffer, sx, sy,
935 g_object_unref(buffer);
936 cairo_surface_destroy(source);
940 *-----------------------------------------------------------------------------
941 * pixbuf drawing (triangle)
942 *-----------------------------------------------------------------------------
945 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
946 gint *rx, gint *ry, gint *rw, gint *rh)
954 tw = MAX(abs(x1 - x2), abs(x2 - x3));
955 tw = MAX(tw, abs(x3 - x1));
956 th = MAX(abs(y1 - y2), abs(y2 - y3));
957 th = MAX(th, abs(y3 - y1));
965 void pixbuf_draw_triangle(GdkPixbuf *pb,
966 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
967 gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
968 guint8 r, guint8 g, guint8 b, guint8 a)
980 gdouble slope1, slope2;
981 gint slope1_x, slope1_y;
984 gboolean middle = FALSE;
988 pw = gdk_pixbuf_get_width(pb);
989 ph = gdk_pixbuf_get_height(pb);
991 if (!util_clip_region(0, 0, pw, ph,
992 clip_x, clip_y, clip_w, clip_h,
993 &rx, &ry, &rw, &rh)) return;
995 util_clip_triangle(x1, y1, x2, y2, x3, y3,
998 if (!util_clip_region(rx, ry, rw, rh,
1000 &fx1, &fy1, &fw, &fh)) return;
1004 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1005 prs = gdk_pixbuf_get_rowstride(pb);
1006 p_pix = gdk_pixbuf_get_pixels(pb);
1008 p_step = (has_alpha) ? 4 : 3;
1012 t = x1; x1 = x2; x2 = t;
1013 t = y1; y1 = y2; y2 = t;
1017 t = x2; x2 = x3; x3 = t;
1018 t = y2; y2 = y3; y3 = t;
1022 t = x1; x1 = x2; x2 = t;
1023 t = y1; y1 = y2; y2 = t;
1026 slope1 = (gdouble)(y2 - y1);
1027 if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
1030 slope2 = (gdouble)(y3 - y1);
1031 if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
1033 for (y = fy1; y < fy2; y++)
1037 if (!middle && y > y2)
1039 slope1 = (gdouble)(y3 - y2);
1040 if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
1047 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
1048 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
1052 t = xa; xa = xb; xb = t;
1055 xa = CLAMP(xa, fx1, fx2);
1056 xb = CLAMP(xb, fx1, fx2);
1058 pp = p_pix + y * prs + xa * p_step;
1062 *pp = (r * a + *pp * (256-a)) >> 8;
1064 *pp = (g * a + *pp * (256-a)) >> 8;
1066 *pp = (b * a + *pp * (256-a)) >> 8;
1068 if (has_alpha) pp++;
1076 *-----------------------------------------------------------------------------
1077 * pixbuf drawing (line)
1078 *-----------------------------------------------------------------------------
1081 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1082 gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1083 gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1085 gboolean flip = FALSE;
1092 t = x1; x1 = x2; x2 = t;
1093 t = y1; y1 = y2; y2 = t;
1097 if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1101 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1105 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1113 slope = (y2 - y1) / d;
1116 y1 = y1 + slope * (clip_x - x1);
1119 if (x2 > clip_x + clip_w)
1121 y2 = y2 + slope * (clip_x + clip_w - x2);
1122 x2 = clip_x + clip_w;
1128 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1134 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1136 t = x1; x1 = x2; x2 = t;
1137 t = y1; y1 = y2; y2 = t;
1146 slope = (x2 - x1) / d;
1149 x1 = x1 + slope * (clip_y - y1);
1152 if (y2 > clip_y + clip_h)
1154 x2 = x2 + slope * (clip_y + clip_h - y2);
1155 y2 = clip_y + clip_h;
1177 void pixbuf_draw_line(GdkPixbuf *pb,
1178 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1179 gint x1, gint y1, gint x2, gint y2,
1180 guint8 r, guint8 g, guint8 b, guint8 a)
1184 gint rx, ry, rw, rh;
1185 gdouble rx1, ry1, rx2, ry2;
1192 gint cx1, cy1, cx2, cy2;
1196 pw = gdk_pixbuf_get_width(pb);
1197 ph = gdk_pixbuf_get_height(pb);
1199 if (!util_clip_region(0, 0, pw, ph,
1200 clip_x, clip_y, clip_w, clip_h,
1201 &rx, &ry, &rw, &rh)) return;
1202 if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1203 (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1204 &rx1, &ry1, &rx2, &ry2)) return;
1211 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1212 prs = gdk_pixbuf_get_rowstride(pb);
1213 p_pix = gdk_pixbuf_get_pixels(pb);
1215 p_step = (has_alpha) ? 4 : 3;
1217 if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1222 t = rx1; rx1 = rx2; rx2 = t;
1223 t = ry1; ry1 = ry2; ry2 = t;
1227 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1228 for (x = rx1; x < rx2; x += 1.0)
1230 px = (gint)(x + 0.5);
1231 py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1233 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1235 pp = p_pix + py * prs + px * p_step;
1236 *pp = (r * a + *pp * (256-a)) >> 8;
1238 *pp = (g * a + *pp * (256-a)) >> 8;
1240 *pp = (b * a + *pp * (256-a)) >> 8;
1249 t = rx1; rx1 = rx2; rx2 = t;
1250 t = ry1; ry1 = ry2; ry2 = t;
1254 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1255 for (y = ry1; y < ry2; y += 1.0)
1257 px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1258 py = (gint)(y + 0.5);
1260 if (px >= cx1 && px < cx2 && py >= cy1 && py < cy2)
1262 pp = p_pix + py * prs + px * p_step;
1263 *pp = (r * a + *pp * (256-a)) >> 8;
1265 *pp = (g * a + *pp * (256-a)) >> 8;
1267 *pp = (b * a + *pp * (256-a)) >> 8;
1274 *-----------------------------------------------------------------------------
1275 * pixbuf drawing (fades and shadows)
1276 *-----------------------------------------------------------------------------
1279 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1280 gint s, gboolean vertical, gint border,
1281 gint x1, gint y1, gint x2, gint y2,
1282 guint8 r, guint8 g, guint8 b, guint8 a)
1289 p_step = (has_alpha) ? 4 : 3;
1290 for (j = y1; j < y2; j++)
1292 pp = p_pix + j * prs + x1 * p_step;
1293 if (!vertical) n = a - a * abs(j - s) / border;
1294 for (i = x1; i < x2; i++)
1296 if (vertical) n = a - a * abs(i - s) / border;
1297 *pp = (r * n + *pp * (256-n)) >> 8;
1299 *pp = (g * n + *pp * (256-n)) >> 8;
1301 *pp = (b * n + *pp * (256-n)) >> 8;
1303 if (has_alpha) pp++;
1308 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1309 gint sx, gint sy, gint border,
1310 gint x1, gint y1, gint x2, gint y2,
1311 guint8 UNUSED(r), guint8 g, guint8 b, guint8 a)
1317 p_step = (has_alpha) ? 4 : 3;
1318 for (j = y1; j < y2; j++)
1320 pp = p_pix + j * prs + x1 * p_step;
1321 for (i = x1; i < x2; i++)
1326 r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1327 n = a - a * r / border;
1328 *pp = (r * n + *pp * (256-n)) >> 8;
1330 *pp = (g * n + *pp * (256-n)) >> 8;
1332 *pp = (b * n + *pp * (256-n)) >> 8;
1334 if (has_alpha) pp++;
1339 void pixbuf_draw_shadow(GdkPixbuf *pb,
1340 gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1341 gint x, gint y, gint w, gint h, gint border,
1342 guint8 r, guint8 g, guint8 b, guint8 a)
1346 gint rx, ry, rw, rh;
1347 gint fx, fy, fw, fh;
1352 pw = gdk_pixbuf_get_width(pb);
1353 ph = gdk_pixbuf_get_height(pb);
1355 if (!util_clip_region(0, 0, pw, ph,
1356 clip_x, clip_y, clip_w, clip_h,
1357 &rx, &ry, &rw, &rh)) return;
1359 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1360 prs = gdk_pixbuf_get_rowstride(pb);
1361 p_pix = gdk_pixbuf_get_pixels(pb);
1363 if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1365 &fx, &fy, &fw, &fh))
1367 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1370 if (border < 1) return;
1372 if (util_clip_region(x, y + border, border, h - border * 2,
1374 &fx, &fy, &fw, &fh))
1376 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1377 x + border, TRUE, border,
1378 fx, fy, fx + fw, fy + fh,
1381 if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1383 &fx, &fy, &fw, &fh))
1385 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1386 x + w - border, TRUE, border,
1387 fx, fy, fx + fw, fy + fh,
1390 if (util_clip_region(x + border, y, w - border * 2, border,
1392 &fx, &fy, &fw, &fh))
1394 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1395 y + border, FALSE, border,
1396 fx, fy, fx + fw, fy + fh,
1399 if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1401 &fx, &fy, &fw, &fh))
1403 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1404 y + h - border, FALSE, border,
1405 fx, fy, fx + fw, fy + fh,
1408 if (util_clip_region(x, y, border, border,
1410 &fx, &fy, &fw, &fh))
1412 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1413 x + border, y + border, border,
1414 fx, fy, fx + fw, fy + fh,
1417 if (util_clip_region(x + w - border, y, border, border,
1419 &fx, &fy, &fw, &fh))
1421 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1422 x + w - border, y + border, border,
1423 fx, fy, fx + fw, fy + fh,
1426 if (util_clip_region(x, y + h - border, border, border,
1428 &fx, &fy, &fw, &fh))
1430 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1431 x + border, y + h - border, border,
1432 fx, fy, fx + fw, fy + fh,
1435 if (util_clip_region(x + w - border, y + h - border, border, border,
1437 &fx, &fy, &fw, &fh))
1439 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1440 x + w - border, y + h - border, border,
1441 fx, fy, fx + fw, fy + fh,
1448 *-----------------------------------------------------------------------------
1449 * pixbuf color alterations
1450 *-----------------------------------------------------------------------------
1453 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1454 gint x, gint y, gint w, gint h)
1464 pw = gdk_pixbuf_get_width(pb);
1465 ph = gdk_pixbuf_get_height(pb);
1467 if (x < 0 || x + w > pw) return;
1468 if (y < 0 || y + h > ph) return;
1470 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1471 prs = gdk_pixbuf_get_rowstride(pb);
1472 p_pix = gdk_pixbuf_get_pixels(pb);
1474 for (i = 0; i < h; i++)
1476 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1477 for (j = 0; j < w; j++)
1481 grey = (pp[0] + pp[1] + pp[2]) / 3;
1488 if (has_alpha) pp++;
1494 *-----------------------------------------------------------------------------
1495 * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
1497 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1507 pw = gdk_pixbuf_get_width(pb);
1508 ph = gdk_pixbuf_get_height(pb);
1510 if (x < 0 || x + w > pw) return;
1511 if (y < 0 || y + h > ph) return;
1513 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1514 prs = gdk_pixbuf_get_rowstride(pb);
1515 p_pix = gdk_pixbuf_get_pixels(pb);
1517 for (i = 0; i < h; i++)
1519 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1520 for (j = 0; j < w; j++)
1522 if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1530 if (has_alpha) pp++;
1535 if (has_alpha) pp++;
1542 *-----------------------------------------------------------------------------
1543 * pixbuf ignore alpha
1544 *-----------------------------------------------------------------------------
1546 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1547 gint x, gint y, gint w, gint h)
1557 pw = gdk_pixbuf_get_width(pb);
1558 ph = gdk_pixbuf_get_height(pb);
1560 if (x < 0 || x + w > pw) return;
1561 if (y < 0 || y + h > ph) return;
1563 has_alpha = gdk_pixbuf_get_has_alpha(pb);
1564 if (!has_alpha) return;
1566 prs = gdk_pixbuf_get_rowstride(pb);
1567 p_pix = gdk_pixbuf_get_pixels(pb);
1569 for (i = 0; i < h; i++)
1571 pp = p_pix + (y + i) * prs + (x * 4 );
1572 for (j = 0; j < w; j++)
1579 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */