1614c0d4a3ff5c223f922917c6ee68a3ef2738a9
[geeqie.git] / src / pixbuf_util.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2012 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "pixbuf_util.h"
16 #include "exif.h"
17 #include "ui_fileops.h"
18
19 #include "icons/icons_inline.h"
20
21 #include <math.h>
22
23
24 /*
25  *-----------------------------------------------------------------------------
26  * png save
27  *-----------------------------------------------------------------------------
28  */
29
30 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
31 {
32         GError *error = NULL;
33         gboolean ret;
34
35         if (!pixbuf || !filename) return FALSE;
36
37         ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
38                               "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
39
40         if (error)
41                 {
42                 log_printf("Error saving png file: %s\n", error->message);
43                 g_error_free(error);
44                 }
45
46         return ret;
47 }
48
49 /*
50  *-----------------------------------------------------------------------------
51  * jpeg save
52  *-----------------------------------------------------------------------------
53  */
54
55 gboolean pixbuf_to_file_as_jpg(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
56 {
57         GError *error = NULL;
58         gchar *qbuf;
59         gboolean ret;
60
61         if (!pixbuf || !filename) return FALSE;
62
63         if (quality == -1) quality = 75;
64         if (quality < 1 || quality > 100)
65                 {
66                 log_printf("Jpeg not saved, invalid quality %d\n", quality);
67                 return FALSE;
68                 }
69
70         qbuf = g_strdup_printf("%d", quality);
71         ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
72         g_free(qbuf);
73
74         if (error)
75                 {
76                 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
77                 g_error_free(error);
78                 }
79
80         return ret;
81 }
82
83 /*
84  *-----------------------------------------------------------------------------
85  * pixbuf from inline
86  *-----------------------------------------------------------------------------
87  */
88
89 typedef struct _PixbufInline PixbufInline;
90 struct _PixbufInline
91 {
92         const gchar *key;
93         const guint8 *data;
94 };
95
96 static PixbufInline inline_pixbuf_data[] = {
97         { PIXBUF_INLINE_FOLDER_CLOSED,  folder_closed },
98         { PIXBUF_INLINE_FOLDER_LOCKED,  folder_locked },
99         { PIXBUF_INLINE_FOLDER_OPEN,    folder_open },
100         { PIXBUF_INLINE_FOLDER_UP,      folder_up },
101         { PIXBUF_INLINE_SCROLLER,       icon_scroller },
102         { PIXBUF_INLINE_BROKEN,         icon_broken },
103         { PIXBUF_INLINE_ICON,           gqview_icon },
104         { PIXBUF_INLINE_LOGO,           geeqie_logo },
105         { PIXBUF_INLINE_ICON_FLOAT,     icon_float },
106         { PIXBUF_INLINE_ICON_THUMB,     icon_thumb },
107         { PIXBUF_INLINE_ICON_BOOK,      icon_book },
108         { PIXBUF_INLINE_ICON_CONFIG,    icon_config },
109         { PIXBUF_INLINE_ICON_TOOLS,     icon_tools },
110         { PIXBUF_INLINE_ICON_VIEW,      icon_view },
111         { NULL, NULL }
112 };
113
114 GdkPixbuf *pixbuf_inline(const gchar *key)
115 {
116         gint i;
117
118         if (!key) return NULL;
119
120         i = 0;
121         while (inline_pixbuf_data[i].key)
122                 {
123                 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
124                         {
125                         return gdk_pixbuf_new_from_inline(-1, inline_pixbuf_data[i].data, FALSE, NULL);
126                         }
127                 i++;
128                 }
129
130         log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
131
132         return NULL;
133 }
134
135 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
136 {
137         static GtkIconFactory *icon_factory = NULL;
138         GtkIconSet *icon_set;
139         
140         if (!icon_factory)
141                 {
142                 icon_factory = gtk_icon_factory_new();
143                 gtk_icon_factory_add_default(icon_factory);
144                 }
145         
146         icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
147         gtk_icon_factory_add(icon_factory, key, icon_set);
148 }
149
150
151 void pixbuf_inline_register_stock_icons(void)
152 {
153         gint i;
154
155         i = 0;
156         while (inline_pixbuf_data[i].key)
157                 {
158                 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
159                 i++;
160                 }
161 }
162
163 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
164 {
165         GtkIconTheme *icon_theme;
166         GdkPixbuf *pixbuf;
167         GError *error = NULL;
168
169         icon_theme = gtk_icon_theme_get_default();
170         
171         if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
172
173         pixbuf = gtk_icon_theme_load_icon(icon_theme,
174                            icon, /* icon name */
175                            64, /* size */
176                            0,  /* flags */
177                            &error);
178         if (!pixbuf) 
179                 {
180                 if (error)
181                         {
182                         DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
183                         g_error_free(error);
184                         error = NULL;
185                         }
186                         
187                 if (strchr(icon, '.'))
188                         {
189                         /* try again without extension */
190                         gchar *icon2 = remove_extension_from_path(icon);
191                         pixbuf = gtk_icon_theme_load_icon(icon_theme,
192                                            icon2, /* icon name */
193                                            64, /* size */
194                                            0,  /* flags */
195                                            &error);
196                         if (error)
197                                 {
198                                 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
199                                 g_error_free(error);
200                                 }
201                         g_free(icon2);
202                         }
203                 }
204
205         if (!pixbuf) return FALSE;
206         
207         register_stock_icon(key, pixbuf);
208         return TRUE;
209 }
210
211 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
212                              gint old_w, gint old_h,
213                              gint *new_w, gint *new_h)
214 {
215         if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
216                 {
217                 *new_w = req_w;
218                 *new_h = (gdouble)*new_w / old_w * old_h;
219                 if (*new_h < 1) *new_h = 1;
220                 }
221         else
222                 {
223                 *new_h = req_h;
224                 *new_w = (gdouble)*new_h / old_h * old_w;
225                 if (*new_w < 1) *new_w = 1;
226                 }
227
228         return (*new_w != old_w || *new_h != old_h);
229 }
230
231 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
232 {
233         GdkPixbuf *pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN); /* FIXME use different images according to FORMAT_CLASS */
234         
235         if (requested_width && requested_height)
236                 {
237                 gint w = gdk_pixbuf_get_width(pixbuf);
238                 gint h = gdk_pixbuf_get_height(pixbuf);
239
240                 if (w > requested_width || h > requested_height)
241                         {
242                         gint nw, nh;
243
244                         if (pixbuf_scale_aspect(requested_width, requested_height,
245                                                           w, h, &nw, &nh))
246                                 {
247                                 GdkPixbuf *tmp;
248
249                                 tmp = pixbuf;
250                                 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
251                                 g_object_unref(G_OBJECT(tmp));
252                                 }
253                         }
254                 }
255         return pixbuf;
256 }
257
258
259 /*
260  *-----------------------------------------------------------------------------
261  * misc utils
262  *-----------------------------------------------------------------------------
263  */
264
265 gboolean util_clip_region(gint x, gint y, gint w, gint h,
266                           gint clip_x, gint clip_y, gint clip_w, gint clip_h,
267                           gint *rx, gint *ry, gint *rw, gint *rh)
268 {
269         if (clip_x + clip_w <= x ||
270             clip_x >= x + w ||
271             clip_y + clip_h <= y ||
272             clip_y >= y + h)
273                 {
274                 return FALSE;
275                 }
276
277         *rx = MAX(x, clip_x);
278         *rw = MIN((x + w), (clip_x + clip_w)) - *rx;
279
280         *ry = MAX(y, clip_y);
281         *rh = MIN((y + h), (clip_y + clip_h)) - *ry;
282
283         return TRUE;
284 }
285
286 /*
287  *-----------------------------------------------------------------------------
288  * pixbuf rotation
289  *-----------------------------------------------------------------------------
290  */
291
292 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
293                                      guchar *dest, gint dest_row_stride, gint w, gint h,
294                                      gint bytes_per_pixel, gboolean counter_clockwise)
295 {
296         gint i, j;
297         guchar *sp;
298         guchar *dp;
299
300         for (i = 0; i < h; i++)
301                 {
302                 sp = src + ((i + y) * src_row_stride) + (x * bytes_per_pixel);
303                 for (j = 0; j < w; j++)
304                         {
305                         if (counter_clockwise)
306                                 {
307                                 dp = dest + ((w - j - 1) * dest_row_stride) + (i * bytes_per_pixel);
308                                 }
309                         else
310                                 {
311                                 dp = dest + (j * dest_row_stride) + ((h - i - 1) * bytes_per_pixel);
312                                 }
313                         *(dp++) = *(sp++);      /* r */
314                         *(dp++) = *(sp++);      /* g */
315                         *(dp++) = *(sp++);      /* b */
316                         if (bytes_per_pixel == 4) *(dp) = *(sp++);      /* a */
317                         }
318                 }
319
320 }
321
322 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
323                               guchar *dest, gint dest_row_stride, gint x, gint y, gint bytes_per_pixel)
324 {
325         gint i;
326         guchar *sp;
327         guchar *dp;
328
329         for (i = 0; i < h; i++)
330                 {
331                 sp = src + (i * src_row_stride);
332                 dp = dest + ((y + i) * dest_row_stride) + (x * bytes_per_pixel);
333                 memcpy(dp, sp, w * bytes_per_pixel);
334                 }
335 }
336
337 #define ROTATE_BUFFER_WIDTH 48
338 #define ROTATE_BUFFER_HEIGHT 48
339
340 /*
341  * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
342  *
343  */
344 GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
345 {
346         GdkPixbuf *dest;
347         gboolean has_alpha;
348         gint sw, sh, srs;
349         gint dw, dh, drs;
350         guchar *s_pix;
351         guchar *d_pix;
352 #if 0
353         guchar *sp;
354         guchar *dp;
355 #endif
356         gint i, j;
357         gint a;
358         GdkPixbuf *buffer;
359         guchar *b_pix;
360         gint brs;
361         gint w, h;
362
363         if (!src) return NULL;
364
365         sw = gdk_pixbuf_get_width(src);
366         sh = gdk_pixbuf_get_height(src);
367         has_alpha = gdk_pixbuf_get_has_alpha(src);
368         srs = gdk_pixbuf_get_rowstride(src);
369         s_pix = gdk_pixbuf_get_pixels(src);
370
371         dw = sh;
372         dh = sw;
373         dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, dw, dh);
374         drs = gdk_pixbuf_get_rowstride(dest);
375         d_pix = gdk_pixbuf_get_pixels(dest);
376
377         a = (has_alpha ? 4 : 3);
378
379         buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8,
380                                 ROTATE_BUFFER_WIDTH, ROTATE_BUFFER_HEIGHT);
381         b_pix = gdk_pixbuf_get_pixels(buffer);
382         brs = gdk_pixbuf_get_rowstride(buffer);
383
384         for (i = 0; i < sh; i+= ROTATE_BUFFER_WIDTH)
385                 {
386                 w = MIN(ROTATE_BUFFER_WIDTH, (sh - i));
387                 for (j = 0; j < sw; j += ROTATE_BUFFER_HEIGHT)
388                         {
389                         gint x, y;
390
391                         h = MIN(ROTATE_BUFFER_HEIGHT, (sw - j));
392                         pixbuf_copy_block_rotate(s_pix, srs, j, i,
393                                                  b_pix, brs, h, w,
394                                                  a, counter_clockwise);
395
396                         if (counter_clockwise)
397                                 {
398                                 x = i;
399                                 y = sw - h - j;
400                                 }
401                         else
402                                 {
403                                 x = sh - w - i;
404                                 y = j;
405                                 }
406                         pixbuf_copy_block(b_pix, brs, w, h,
407                                           d_pix, drs, x, y, a);
408                         }
409                 }
410
411         g_object_unref(buffer);
412
413 #if 0
414         /* this is the simple version of rotation (roughly 2-4x slower) */
415
416         for (i = 0; i < sh; i++)
417                 {
418                 sp = s_pix + (i * srs);
419                 for (j = 0; j < sw; j++)
420                         {
421                         if (counter_clockwise)
422                                 {
423                                 dp = d_pix + ((dh - j - 1) * drs) + (i * a);
424                                 }
425                         else
426                                 {
427                                 dp = d_pix + (j * drs) + ((dw - i - 1) * a);
428                                 }
429
430                         *(dp++) = *(sp++);      /* r */
431                         *(dp++) = *(sp++);      /* g */
432                         *(dp++) = *(sp++);      /* b */
433                         if (has_alpha) *(dp) = *(sp++); /* a */
434                         }
435                 }
436 #endif
437
438         return dest;
439 }
440
441 /*
442  * Returns a copy of pixbuf mirrored and or flipped.
443  * TO do a 180 degree rotations set both mirror and flipped TRUE
444  * if mirror and flip are FALSE, result is a simple copy.
445  */
446 GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
447 {
448         GdkPixbuf *dest;
449         gboolean has_alpha;
450         gint w, h, srs;
451         gint drs;
452         guchar *s_pix;
453         guchar *d_pix;
454         guchar *sp;
455         guchar *dp;
456         gint i, j;
457         gint a;
458
459         if (!src) return NULL;
460
461         w = gdk_pixbuf_get_width(src);
462         h = 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);
466
467         dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, w, h);
468         drs = gdk_pixbuf_get_rowstride(dest);
469         d_pix = gdk_pixbuf_get_pixels(dest);
470
471         a = has_alpha ? 4 : 3;
472
473         for (i = 0; i < h; i++)
474                 {
475                 sp = s_pix + (i * srs);
476                 if (flip)
477                         {
478                         dp = d_pix + ((h - i - 1) * drs);
479                         }
480                 else
481                         {
482                         dp = d_pix + (i * drs);
483                         }
484                 if (mirror)
485                         {
486                         dp += (w - 1) * a;
487                         for (j = 0; j < w; j++)
488                                 {
489                                 *(dp++) = *(sp++);      /* r */
490                                 *(dp++) = *(sp++);      /* g */
491                                 *(dp++) = *(sp++);      /* b */
492                                 if (has_alpha) *(dp) = *(sp++); /* a */
493                                 dp -= (a + 3);
494                                 }
495                         }
496                 else
497                         {
498                         for (j = 0; j < w; j++)
499                                 {
500                                 *(dp++) = *(sp++);      /* r */
501                                 *(dp++) = *(sp++);      /* g */
502                                 *(dp++) = *(sp++);      /* b */
503                                 if (has_alpha) *(dp++) = *(sp++);       /* a */
504                                 }
505                         }
506                 }
507
508         return dest;
509 }
510
511 GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
512 {
513         GdkPixbuf *dest;
514         GdkPixbuf *tmp = NULL;
515         
516         switch (orientation)
517                 {
518                 case EXIF_ORIENTATION_TOP_LEFT:
519                         dest = gdk_pixbuf_copy(pixbuf);
520                         break;
521                 case EXIF_ORIENTATION_TOP_RIGHT:
522                         /* mirrored */
523                         dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
524                         break;
525                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
526                         /* upside down */
527                         dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
528                         break;
529                 case EXIF_ORIENTATION_BOTTOM_LEFT:
530                         /* flipped */
531                         dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
532                         break;
533                 case EXIF_ORIENTATION_LEFT_TOP:
534                         tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
535                         dest = pixbuf_copy_rotate_90(tmp, FALSE);
536                         break;
537                 case EXIF_ORIENTATION_RIGHT_TOP:
538                         /* rotated -90 (270) */
539                         dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
540                         break;
541                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
542                         tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
543                         dest = pixbuf_copy_rotate_90(tmp, TRUE);
544                         break;
545                 case EXIF_ORIENTATION_LEFT_BOTTOM:
546                         /* rotated 90 */
547                         dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
548                         break;
549                 default:
550                         dest = gdk_pixbuf_copy(pixbuf);
551                         break;
552                 }
553         if (tmp) g_object_unref(tmp);
554         return dest;
555
556 }
557
558
559 /*
560  *-----------------------------------------------------------------------------
561  * pixbuf drawing (rectangles)
562  *-----------------------------------------------------------------------------
563  */
564
565 /*
566  * Fills region of pixbuf at x,y over w,h
567  * with colors red (r), green (g), blue (b)
568  * applying alpha (a), use a=255 for solid.
569  */
570 void pixbuf_draw_rect_fill(GdkPixbuf *pb,
571                            gint x, gint y, gint w, gint h,
572                            gint r, gint g, gint b, gint a)
573 {
574         gboolean has_alpha;
575         gint pw, ph, prs;
576         guchar *p_pix;
577         guchar *pp;
578         gint i, j;
579
580         if (!pb) return;
581
582         pw = gdk_pixbuf_get_width(pb);
583         ph = gdk_pixbuf_get_height(pb);
584
585         if (x < 0 || x + w > pw) return;
586         if (y < 0 || y + h > ph) return;
587
588         has_alpha = gdk_pixbuf_get_has_alpha(pb);
589         prs = gdk_pixbuf_get_rowstride(pb);
590         p_pix = gdk_pixbuf_get_pixels(pb);
591
592         for (i = 0; i < h; i++)
593                 {
594                 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
595                 for (j = 0; j < w; j++)
596                         {
597                         *pp = (r * a + *pp * (256-a)) >> 8;
598                         pp++;
599                         *pp = (g * a + *pp * (256-a)) >> 8;
600                         pp++;
601                         *pp = (b * a + *pp * (256-a)) >> 8;
602                         pp++;
603                         if (has_alpha) pp++;
604                         }
605                 }
606 }
607
608 void pixbuf_draw_rect(GdkPixbuf *pb,
609                       gint x, gint y, gint w, gint h,
610                       gint r, gint g, gint b, gint a,
611                       gint left, gint right, gint top, gint bottom)
612 {
613         pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
614                               r, g, b ,a);
615         pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
616                               r, g, b ,a);
617         pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
618                               r, g, b ,a);
619         pixbuf_draw_rect_fill(pb, x, y, left, h,
620                               r, g, b ,a);
621 }
622
623 void pixbuf_set_rect_fill(GdkPixbuf *pb,
624                           gint x, gint y, gint w, gint h,
625                           gint r, gint g, gint b, gint a)
626 {
627         gboolean has_alpha;
628         gint pw, ph, prs;
629         guchar *p_pix;
630         guchar *pp;
631         gint i, j;
632
633         if (!pb) return;
634
635         pw = gdk_pixbuf_get_width(pb);
636         ph = gdk_pixbuf_get_height(pb);
637
638         if (x < 0 || x + w > pw) return;
639         if (y < 0 || y + h > ph) return;
640
641         has_alpha = gdk_pixbuf_get_has_alpha(pb);
642         prs = gdk_pixbuf_get_rowstride(pb);
643         p_pix = gdk_pixbuf_get_pixels(pb);
644
645         for (i = 0; i < h; i++)
646                 {
647                 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
648                 for (j = 0; j < w; j++)
649                         {
650                         *pp = r; pp++;
651                         *pp = g; pp++;
652                         *pp = b; pp++;
653                         if (has_alpha) { *pp = a; pp++; }
654                         }
655                 }
656 }
657
658 void pixbuf_set_rect(GdkPixbuf *pb,
659                      gint x, gint y, gint w, gint h,
660                      gint r, gint g, gint b, gint a,
661                      gint left, gint right, gint top, gint bottom)
662 {
663         pixbuf_set_rect_fill(pb, x + left, y, w - left - right, top,
664                              r, g, b ,a);
665         pixbuf_set_rect_fill(pb, x + w - right, y, right, h,
666                              r, g, b ,a);
667         pixbuf_set_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
668                              r, g, b ,a);
669         pixbuf_set_rect_fill(pb, x, y, left, h,
670                              r, g, b ,a);
671 }
672
673 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
674 {
675         guchar *buf;
676         gboolean has_alpha;
677         gint rowstride;
678         guchar *p;
679
680         if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
681             y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
682
683         buf = gdk_pixbuf_get_pixels(pb);
684         has_alpha = gdk_pixbuf_get_has_alpha(pb);
685         rowstride = gdk_pixbuf_get_rowstride(pb);
686
687         p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
688         *p = r; p++;
689         *p = g; p++;
690         *p = b; p++;
691         if (has_alpha) *p = a;
692 }
693
694
695 /*
696  *-----------------------------------------------------------------------------
697  * pixbuf text rendering
698  *-----------------------------------------------------------------------------
699  */
700
701 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
702                              GdkPixbuf *dest, gint dx, gint dy,
703                              gint w, gint h,
704                              guint8 r, guint8 g, guint8 b, guint8 a)
705 {
706         gint sw, sh, srs;
707         gboolean s_alpha;
708         gint s_step;
709         guchar *s_pix;
710         gint dw, dh, drs;
711         gboolean d_alpha;
712         gint d_step;
713         guchar *d_pix;
714
715         guchar *sp;
716         guchar *dp;
717         gint i, j;
718
719         if (!src || !dest) return;
720
721         sw = gdk_pixbuf_get_width(src);
722         sh = gdk_pixbuf_get_height(src);
723
724         if (sx < 0 || sx + w > sw) return;
725         if (sy < 0 || sy + h > sh) return;
726
727         dw = gdk_pixbuf_get_width(dest);
728         dh = gdk_pixbuf_get_height(dest);
729
730         if (dx < 0 || dx + w > dw) return;
731         if (dy < 0 || dy + h > dh) return;
732
733         s_alpha = gdk_pixbuf_get_has_alpha(src);
734         d_alpha = gdk_pixbuf_get_has_alpha(dest);
735         srs = gdk_pixbuf_get_rowstride(src);
736         drs = gdk_pixbuf_get_rowstride(dest);
737         s_pix = gdk_pixbuf_get_pixels(src);
738         d_pix = gdk_pixbuf_get_pixels(dest);
739
740         s_step = (s_alpha) ? 4 : 3;
741         d_step = (d_alpha) ? 4 : 3;
742
743         for (i = 0; i < h; i++)
744                 {
745                 sp = s_pix + (sy + i) * srs + sx * s_step;
746                 dp = d_pix + (dy + i) * drs + dx * d_step;
747                 for (j = 0; j < w; j++)
748                         {
749                         if (*sp)
750                                 {
751                                 guint8 asub;
752
753                                 asub = a * sp[0] / 255;
754                                 *dp = (r * asub + *dp * (256-asub)) >> 8;
755                                 dp++;
756                                 asub = a * sp[1] / 255;
757                                 *dp = (g * asub + *dp * (256-asub)) >> 8;
758                                 dp++;
759                                 asub = a * sp[2] / 255;
760                                 *dp = (b * asub + *dp * (256-asub)) >> 8;
761                                 dp++;
762
763                                 if (d_alpha)
764                                         {
765                                         *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
766                                         dp++;
767                                         }
768                                 }
769                         else
770                                 {
771                                 dp += d_step;
772                                 }
773
774                         sp += s_step;
775                         }
776                 }
777 }
778
779 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widget,
780                         gint x, gint y,
781                         guint8 r, guint8 g, guint8 b, guint8 a)
782 {
783         GdkPixbuf *buffer;
784         gint w, h;
785         gint sx, sy;
786         gint dw, dh;
787         cairo_surface_t *source;
788         cairo_t *cr;
789
790         pango_layout_get_pixel_size(layout, &w, &h);
791         if (w < 1 || h < 1) return;
792
793         source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
794
795         cr = cairo_create (source);
796         cairo_set_source_rgb(cr, 0, 0, 0);
797         cairo_rectangle (cr, 0, 0, w, h);
798         cairo_fill (cr);
799         cairo_set_source_rgb(cr, 1, 1, 1);
800         pango_cairo_show_layout (cr, layout);
801         cairo_destroy (cr);
802
803         buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
804                                            GDK_COLORSPACE_RGB,
805                                            cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
806                                            8,
807                                            cairo_image_surface_get_width (source),
808                                            cairo_image_surface_get_height (source),
809                                            cairo_image_surface_get_stride (source),
810                                            NULL,
811                                            NULL);
812
813         sx = 0;
814         sy = 0;
815         dw = gdk_pixbuf_get_width(pixbuf);
816         dh = gdk_pixbuf_get_height(pixbuf);
817
818         if (x < 0)
819                 {
820                 w += x;
821                 sx = -x;
822                 x = 0;
823                 }
824
825         if (y < 0)
826                 {
827                 h += y;
828                 sy = -y;
829                 y = 0;
830                 }
831
832         if (x + w > dw) w = dw - x;
833         if (y + h > dh) h = dh - y;
834
835         pixbuf_copy_font(buffer, sx, sy,
836                          pixbuf, x, y, w, h,
837                          r, g, b, a);
838
839         g_object_unref(buffer);
840         cairo_surface_destroy(source);
841 }
842
843 /*
844  *-----------------------------------------------------------------------------
845  * pixbuf drawing (triangle)
846  *-----------------------------------------------------------------------------
847  */
848
849 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
850                         gint *rx, gint *ry, gint *rw, gint *rh)
851 {
852         gint tx, ty, tw, th;
853
854         tx = MIN(x1, x2);
855         tx = MIN(tx, x3);
856         ty = MIN(y1, y2);
857         ty = MIN(ty, y3);
858         tw = MAX(abs(x1 - x2), abs(x2 - x3));
859         tw = MAX(tw, abs(x3 - x1));
860         th = MAX(abs(y1 - y2), abs(y2 - y3));
861         th = MAX(th, abs(y3 - y1));
862
863         *rx = tx;
864         *ry = ty;
865         *rw = tw;
866         *rh = th;
867 }
868
869 void pixbuf_draw_triangle(GdkPixbuf *pb,
870                           gint clip_x, gint clip_y, gint clip_w, gint clip_h,
871                           gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
872                           guint8 r, guint8 g, guint8 b, guint8 a)
873 {
874         gboolean has_alpha;
875         gint pw, ph, prs;
876         gint rx, ry, rw, rh;
877         gint tx, ty, tw, th;
878         gint fx1, fy1;
879         gint fx2, fy2;
880         gint fw, fh;
881         guchar *p_pix;
882         guchar *pp;
883         gint p_step;
884         gdouble slope1, slope2;
885         gint slope1_x, slope1_y;
886         gint y;
887         gint t;
888         gboolean middle = FALSE;
889
890         if (!pb) return;
891
892         pw = gdk_pixbuf_get_width(pb);
893         ph = gdk_pixbuf_get_height(pb);
894
895         if (!util_clip_region(0, 0, pw, ph,
896                               clip_x, clip_y, clip_w, clip_h,
897                               &rx, &ry, &rw, &rh)) return;
898
899         util_clip_triangle(x1, y1, x2, y2, x3, y3,
900                            &tx, &ty, &tw, &th);
901
902         if (!util_clip_region(rx, ry, rw, rh,
903                               tx, ty, tw, th,
904                               &fx1, &fy1, &fw, &fh)) return;
905         fx2 = fx1 + fw;
906         fy2 = fy1 + fh;
907
908         has_alpha = gdk_pixbuf_get_has_alpha(pb);
909         prs = gdk_pixbuf_get_rowstride(pb);
910         p_pix = gdk_pixbuf_get_pixels(pb);
911
912         p_step = (has_alpha) ? 4 : 3;
913
914         if (y1 > y2)
915                 {
916                 t = x1; x1 = x2; x2 = t;
917                 t = y1; y1 = y2; y2 = t;
918                 }
919         if (y2 > y3)
920                 {
921                 t = x2; x2 = x3; x3 = t;
922                 t = y2; y2 = y3; y3 = t;
923                 }
924         if (y1 > y2)
925                 {
926                 t = x1; x1 = x2; x2 = t;
927                 t = y1; y1 = y2; y2 = t;
928                 }
929
930         slope1 = (gdouble)(y2 - y1);
931         if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
932         slope1_x = x1;
933         slope1_y = y1;
934         slope2 = (gdouble)(y3 - y1);
935         if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
936
937         for (y = fy1; y < fy2; y++)
938                 {
939                 gint xa, xb;
940
941                 if (!middle && y > y2)
942                         {
943                         slope1 = (gdouble)(y3 - y2);
944                         if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
945                         slope1_x = x2;
946                         slope1_y = y2;
947
948                         middle = TRUE;
949                         }
950
951                 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
952                 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
953
954                 if (xa > xb)
955                         {
956                         t = xa; xa = xb; xb = t;
957                         }
958
959                 xa = CLAMP(xa, fx1, fx2);
960                 xb = CLAMP(xb, fx1, fx2);
961
962                 pp = p_pix + y * prs + xa * p_step;
963
964                 while (xa < xb)
965                         {
966                         *pp = (r * a + *pp * (256-a)) >> 8;
967                         pp++;
968                         *pp = (g * a + *pp * (256-a)) >> 8;
969                         pp++;
970                         *pp = (b * a + *pp * (256-a)) >> 8;
971                         pp++;
972                         if (has_alpha) pp++;
973
974                         xa++;
975                         }
976                 }
977 }
978
979 /*
980  *-----------------------------------------------------------------------------
981  * pixbuf drawing (line)
982  *-----------------------------------------------------------------------------
983  */
984
985 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
986                                gdouble x1, gdouble y1, gdouble x2, gdouble y2,
987                                gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
988 {
989         gboolean flip = FALSE;
990         gdouble d;
991
992         if (x1 > x2)
993                 {
994                 gdouble t;
995
996                 t = x1; x1 = x2; x2 = t;
997                 t = y1; y1 = y2; y2 = t;
998                 flip = TRUE;
999                 }
1000
1001         if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1002
1003         if (y1 < y2)
1004                 {
1005                 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1006                 }
1007         else
1008                 {
1009                 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1010                 }
1011
1012 #if 0
1013         if (x1 >= clip_x && x2 <= clip_x + clip_w)
1014                 {
1015                 if (y1 < y2)
1016                         {
1017                         if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
1018                         }
1019                 else
1020                         {
1021                         if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
1022                         }
1023                 }
1024 #endif
1025
1026         d = x2 - x1;
1027         if (d > 0.0)
1028                 {
1029                 gdouble slope;
1030
1031                 slope = (y2 - y1) / d;
1032                 if (x1 < clip_x)
1033                         {
1034                         y1 = y1 + slope * (clip_x - x1);
1035                         x1 = clip_x;
1036                         }
1037                 if (x2 > clip_x + clip_w)
1038                         {
1039                         y2 = y2 + slope * (clip_x + clip_w - x2);
1040                         x2 = clip_x + clip_w;
1041                         }
1042                 }
1043
1044         if (y1 < y2)
1045                 {
1046                 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1047                 }
1048         else
1049                 {
1050                 gdouble t;
1051
1052                 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1053
1054                 t = x1; x1 = x2; x2 = t;
1055                 t = y1; y1 = y2; y2 = t;
1056                 flip = !flip;
1057                 }
1058
1059         d = y2 - y1;
1060         if (d > 0.0)
1061                 {
1062                 gdouble slope;
1063
1064                 slope = (x2 - x1) / d;
1065                 if (y1 < clip_y)
1066                         {
1067                         x1 = x1 + slope * (clip_y - y1);
1068                         y1 = clip_y;
1069                         }
1070                 if (y2 > clip_y + clip_h)
1071                         {
1072                         x2 = x2 + slope * (clip_y + clip_h - y2);
1073                         y2 = clip_y + clip_h;
1074                         }
1075                 }
1076
1077         if (flip)
1078                 {
1079                 *rx1 = x2;
1080                 *ry1 = y2;
1081                 *rx2 = x1;
1082                 *ry2 = y1;
1083                 }
1084         else
1085                 {
1086                 *rx1 = x1;
1087                 *ry1 = y1;
1088                 *rx2 = x2;
1089                 *ry2 = y2;
1090                 }
1091
1092         return TRUE;
1093 }
1094
1095 void pixbuf_draw_line(GdkPixbuf *pb,
1096                       gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1097                       gint x1, gint y1, gint x2, gint y2,
1098                       guint8 r, guint8 g, guint8 b, guint8 a)
1099 {
1100         gboolean has_alpha;
1101         gint pw, ph, prs;
1102         gint rx, ry, rw, rh;
1103         gdouble rx1, ry1, rx2, ry2;
1104         guchar *p_pix;
1105         guchar *pp;
1106         gint p_step;
1107         gdouble slope;
1108         gdouble x, y;
1109         gint px, py;
1110         gint cx1, cy1, cx2, cy2;
1111
1112         if (!pb) return;
1113
1114         pw = gdk_pixbuf_get_width(pb);
1115         ph = gdk_pixbuf_get_height(pb);
1116
1117         if (!util_clip_region(0, 0, pw, ph,
1118                               clip_x, clip_y, clip_w, clip_h,
1119                               &rx, &ry, &rw, &rh)) return;
1120         if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1121                             (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1122                             &rx1, &ry1, &rx2, &ry2)) return;
1123
1124         cx1 = rx;
1125         cy1 = ry;
1126         cx2 = rx + rw;
1127         cy2 = ry + rh;
1128
1129         has_alpha = gdk_pixbuf_get_has_alpha(pb);
1130         prs = gdk_pixbuf_get_rowstride(pb);
1131         p_pix = gdk_pixbuf_get_pixels(pb);
1132
1133         p_step = (has_alpha) ? 4 : 3;
1134
1135         if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1136                 {
1137                 if (rx1 > rx2)
1138                         {
1139                         gdouble t;
1140                         t = rx1; rx1 = rx2; rx2 = t;
1141                         t = ry1; ry1 = ry2; ry2 = t;
1142                         }
1143
1144                 slope = rx2 - rx1;
1145                 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1146                 for (x = rx1; x < rx2; x += 1.0)
1147                         {
1148                         px = (gint)(x + 0.5);
1149                         py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1150
1151                         if (px >=  cx1 && px < cx2 && py >= cy1 && py < cy2)
1152                                 {
1153                                 pp = p_pix + py * prs + px * p_step;
1154                                 *pp = (r * a + *pp * (256-a)) >> 8;
1155                                 pp++;
1156                                 *pp = (g * a + *pp * (256-a)) >> 8;
1157                                 pp++;
1158                                 *pp = (b * a + *pp * (256-a)) >> 8;
1159                                 }
1160                         }
1161                 }
1162         else
1163                 {
1164                 if (ry1 > ry2)
1165                         {
1166                         gdouble t;
1167                         t = rx1; rx1 = rx2; rx2 = t;
1168                         t = ry1; ry1 = ry2; ry2 = t;
1169                         }
1170
1171                 slope = ry2 - ry1;
1172                 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1173                 for (y = ry1; y < ry2; y += 1.0)
1174                         {
1175                         px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1176                         py = (gint)(y + 0.5);
1177
1178                         if (px >=  cx1 && px < cx2 && py >= cy1 && py < cy2)
1179                                 {
1180                                 pp = p_pix + py * prs + px * p_step;
1181                                 *pp = (r * a + *pp * (256-a)) >> 8;
1182                                 pp++;
1183                                 *pp = (g * a + *pp * (256-a)) >> 8;
1184                                 pp++;
1185                                 *pp = (b * a + *pp * (256-a)) >> 8;
1186                                 }
1187                         }
1188                 }
1189 }
1190
1191 /*
1192  *-----------------------------------------------------------------------------
1193  * pixbuf drawing (fades and shadows)
1194  *-----------------------------------------------------------------------------
1195  */
1196
1197 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1198                                     gint s, gboolean vertical, gint border,
1199                                     gint x1, gint y1, gint x2, gint y2,
1200                                     guint8 r, guint8 g, guint8 b, guint8 a)
1201 {
1202         guchar *pp;
1203         gint p_step;
1204         guint8 n = a;
1205         gint i, j;
1206
1207         p_step = (has_alpha) ? 4 : 3;
1208         for (j = y1; j < y2; j++)
1209                 {
1210                 pp = p_pix + j * prs + x1 * p_step;
1211                 if (!vertical) n = a - a * abs(j - s) / border;
1212                 for (i = x1; i < x2; i++)
1213                         {
1214                         if (vertical) n = a - a * abs(i - s) / border;
1215                         *pp = (r * n + *pp * (256-n)) >> 8;
1216                         pp++;
1217                         *pp = (g * n + *pp * (256-n)) >> 8;
1218                         pp++;
1219                         *pp = (b * n + *pp * (256-n)) >> 8;
1220                         pp++;
1221                         if (has_alpha) pp++;
1222                         }
1223                 }
1224 }
1225
1226 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1227                                     gint sx, gint sy, gint border,
1228                                     gint x1, gint y1, gint x2, gint y2,
1229                                     guint8 r, guint8 g, guint8 b, guint8 a)
1230 {
1231         guchar *pp;
1232         gint p_step;
1233         gint i, j;
1234
1235         p_step = (has_alpha) ? 4 : 3;
1236         for (j = y1; j < y2; j++)
1237                 {
1238                 pp = p_pix + j * prs + x1 * p_step;
1239                 for (i = x1; i < x2; i++)
1240                         {
1241                         guint8 n;
1242                         gint r;
1243
1244                         r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1245                         n = a - a * r / border;
1246                         *pp = (r * n + *pp * (256-n)) >> 8;
1247                         pp++;
1248                         *pp = (g * n + *pp * (256-n)) >> 8;
1249                         pp++;
1250                         *pp = (b * n + *pp * (256-n)) >> 8;
1251                         pp++;
1252                         if (has_alpha) pp++;
1253                         }
1254                 }
1255 }
1256
1257 void pixbuf_draw_shadow(GdkPixbuf *pb,
1258                         gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1259                         gint x, gint y, gint w, gint h, gint border,
1260                         guint8 r, guint8 g, guint8 b, guint8 a)
1261 {
1262         gint has_alpha;
1263         gint pw, ph, prs;
1264         gint rx, ry, rw, rh;
1265         gint fx, fy, fw, fh;
1266         guchar *p_pix;
1267
1268         if (!pb) return;
1269
1270         pw = gdk_pixbuf_get_width(pb);
1271         ph = gdk_pixbuf_get_height(pb);
1272
1273         if (!util_clip_region(0, 0, pw, ph,
1274                               clip_x, clip_y, clip_w, clip_h,
1275                               &rx, &ry, &rw, &rh)) return;
1276
1277         has_alpha = gdk_pixbuf_get_has_alpha(pb);
1278         prs = gdk_pixbuf_get_rowstride(pb);
1279         p_pix = gdk_pixbuf_get_pixels(pb);
1280
1281         if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1282                              rx, ry, rw, rh,
1283                              &fx, &fy, &fw, &fh))
1284                 {
1285                 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1286                 }
1287
1288         if (border < 1) return;
1289
1290         if (util_clip_region(x, y + border, border, h - border * 2,
1291                              rx, ry, rw, rh,
1292                              &fx, &fy, &fw, &fh))
1293                 {
1294                 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1295                                         x + border, TRUE, border,
1296                                         fx, fy, fx + fw, fy + fh,
1297                                         r, g, b, a);
1298                 }
1299         if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1300                              rx, ry, rw, rh,
1301                              &fx, &fy, &fw, &fh))
1302                 {
1303                 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1304                                         x + w - border, TRUE, border,
1305                                         fx, fy, fx + fw, fy + fh,
1306                                         r, g, b, a);
1307                 }
1308         if (util_clip_region(x + border, y, w - border * 2, border,
1309                              rx, ry, rw, rh,
1310                              &fx, &fy, &fw, &fh))
1311                 {
1312                 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1313                                         y + border, FALSE, border,
1314                                         fx, fy, fx + fw, fy + fh,
1315                                         r, g, b, a);
1316                 }
1317         if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1318                              rx, ry, rw, rh,
1319                              &fx, &fy, &fw, &fh))
1320                 {
1321                 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1322                                         y + h - border, FALSE, border,
1323                                         fx, fy, fx + fw, fy + fh,
1324                                         r, g, b, a);
1325                 }
1326         if (util_clip_region(x, y, border, border,
1327                              rx, ry, rw, rh,
1328                              &fx, &fy, &fw, &fh))
1329                 {
1330                 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1331                                         x + border, y + border, border,
1332                                         fx, fy, fx + fw, fy + fh,
1333                                         r, g, b, a);
1334                 }
1335         if (util_clip_region(x + w - border, y, border, border,
1336                              rx, ry, rw, rh,
1337                              &fx, &fy, &fw, &fh))
1338                 {
1339                 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1340                                         x + w - border, y + border, border,
1341                                         fx, fy, fx + fw, fy + fh,
1342                                         r, g, b, a);
1343                 }
1344         if (util_clip_region(x, y + h - border, border, border,
1345                              rx, ry, rw, rh,
1346                              &fx, &fy, &fw, &fh))
1347                 {
1348                 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1349                                         x + border, y + h - border, border,
1350                                         fx, fy, fx + fw, fy + fh,
1351                                         r, g, b, a);
1352                 }
1353         if (util_clip_region(x + w - border, y + h - border, border, border,
1354                              rx, ry, rw, rh,
1355                              &fx, &fy, &fw, &fh))
1356                 {
1357                 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1358                                         x + w - border, y + h - border, border,
1359                                         fx, fy, fx + fw, fy + fh,
1360                                         r, g, b, a);
1361                 }
1362 }
1363
1364
1365 /*
1366  *-----------------------------------------------------------------------------
1367  * pixbuf color alterations
1368  *-----------------------------------------------------------------------------
1369  */
1370
1371 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1372                             gint x, gint y, gint w, gint h)
1373 {
1374         gboolean has_alpha;
1375         gint pw, ph, prs;
1376         guchar *p_pix;
1377         guchar *pp;
1378         gint i, j;
1379
1380         if (!pb) return;
1381
1382         pw = gdk_pixbuf_get_width(pb);
1383         ph = gdk_pixbuf_get_height(pb);
1384
1385         if (x < 0 || x + w > pw) return;
1386         if (y < 0 || y + h > ph) return;
1387
1388         has_alpha = gdk_pixbuf_get_has_alpha(pb);
1389         prs = gdk_pixbuf_get_rowstride(pb);
1390         p_pix = gdk_pixbuf_get_pixels(pb);
1391
1392         for (i = 0; i < h; i++)
1393                 {
1394                 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1395                 for (j = 0; j < w; j++)
1396                         {
1397                         guint8 grey;
1398
1399                         grey = (pp[0] + pp[1] + pp[2]) / 3;
1400                         *pp = grey;
1401                         pp++;
1402                         *pp = grey;
1403                         pp++;
1404                         *pp = grey;
1405                         pp++;
1406                         if (has_alpha) pp++;
1407                         }
1408                 }
1409 }
1410 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */