fixed loading of desktop file icons, gtk seems to expect an icon name without extension
[geeqie.git] / src / pixbuf_util.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2009 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         GdkPixmap *pixmap;
784         GdkPixbuf *buffer;
785         gint w, h;
786         GdkGC *gc;
787         gint sx, sy;
788         gint dw, dh;
789
790         if (!widget || !widget->window) return;
791
792         pango_layout_get_pixel_size(layout, &w, &h);
793         if (w < 1 || h < 1) return;
794
795         pixmap = gdk_pixmap_new(widget->window, w, h, -1);
796
797         gc = gdk_gc_new(widget->window);
798         gdk_gc_copy(gc, widget->style->black_gc);
799         gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, w, h);
800         gdk_gc_copy(gc, widget->style->white_gc);
801         gdk_draw_layout(pixmap, gc, 0, 0, layout);
802         g_object_unref(gc);
803
804         buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, w, h);
805         gdk_pixbuf_get_from_drawable(buffer, pixmap,
806                                      gdk_drawable_get_colormap(widget->window),
807                                      0, 0, 0, 0, w, h);
808         g_object_unref(pixmap);
809
810         sx = 0;
811         sy = 0;
812         dw = gdk_pixbuf_get_width(pixbuf);
813         dh = gdk_pixbuf_get_height(pixbuf);
814
815         if (x < 0)
816                 {
817                 w += x;
818                 sx = -x;
819                 x = 0;
820                 }
821
822         if (y < 0)
823                 {
824                 h += y;
825                 sy = -y;
826                 y = 0;
827                 }
828
829         if (x + w > dw) w = dw - x;
830         if (y + h > dh) h = dh - y;
831
832         pixbuf_copy_font(buffer, sx, sy,
833                          pixbuf, x, y, w, h,
834                          r, g, b, a);
835
836         g_object_unref(buffer);
837 }
838
839 /*
840  *-----------------------------------------------------------------------------
841  * pixbuf drawing (triangle)
842  *-----------------------------------------------------------------------------
843  */
844
845 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
846                         gint *rx, gint *ry, gint *rw, gint *rh)
847 {
848         gint tx, ty, tw, th;
849
850         tx = MIN(x1, x2);
851         tx = MIN(tx, x3);
852         ty = MIN(y1, y2);
853         ty = MIN(ty, y3);
854         tw = MAX(abs(x1 - x2), abs(x2 - x3));
855         tw = MAX(tw, abs(x3 - x1));
856         th = MAX(abs(y1 - y2), abs(y2 - y3));
857         th = MAX(th, abs(y3 - y1));
858
859         *rx = tx;
860         *ry = ty;
861         *rw = tw;
862         *rh = th;
863 }
864
865 void pixbuf_draw_triangle(GdkPixbuf *pb,
866                           gint clip_x, gint clip_y, gint clip_w, gint clip_h,
867                           gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
868                           guint8 r, guint8 g, guint8 b, guint8 a)
869 {
870         gboolean has_alpha;
871         gint pw, ph, prs;
872         gint rx, ry, rw, rh;
873         gint tx, ty, tw, th;
874         gint fx1, fy1;
875         gint fx2, fy2;
876         gint fw, fh;
877         guchar *p_pix;
878         guchar *pp;
879         gint p_step;
880         gdouble slope1, slope2;
881         gint slope1_x, slope1_y;
882         gint y;
883         gint t;
884         gboolean middle = FALSE;
885
886         if (!pb) return;
887
888         pw = gdk_pixbuf_get_width(pb);
889         ph = gdk_pixbuf_get_height(pb);
890
891         if (!util_clip_region(0, 0, pw, ph,
892                               clip_x, clip_y, clip_w, clip_h,
893                               &rx, &ry, &rw, &rh)) return;
894
895         util_clip_triangle(x1, y1, x2, y2, x3, y3,
896                            &tx, &ty, &tw, &th);
897
898         if (!util_clip_region(rx, ry, rw, rh,
899                               tx, ty, tw, th,
900                               &fx1, &fy1, &fw, &fh)) return;
901         fx2 = fx1 + fw;
902         fy2 = fy1 + fh;
903
904         has_alpha = gdk_pixbuf_get_has_alpha(pb);
905         prs = gdk_pixbuf_get_rowstride(pb);
906         p_pix = gdk_pixbuf_get_pixels(pb);
907
908         p_step = (has_alpha) ? 4 : 3;
909
910         if (y1 > y2)
911                 {
912                 t = x1; x1 = x2; x2 = t;
913                 t = y1; y1 = y2; y2 = t;
914                 }
915         if (y2 > y3)
916                 {
917                 t = x2; x2 = x3; x3 = t;
918                 t = y2; y2 = y3; y3 = t;
919                 }
920         if (y1 > y2)
921                 {
922                 t = x1; x1 = x2; x2 = t;
923                 t = y1; y1 = y2; y2 = t;
924                 }
925
926         slope1 = (gdouble)(y2 - y1);
927         if (slope1) slope1 = (gdouble)(x2 - x1) / slope1;
928         slope1_x = x1;
929         slope1_y = y1;
930         slope2 = (gdouble)(y3 - y1);
931         if (slope2) slope2 = (gdouble)(x3 - x1) / slope2;
932
933         for (y = fy1; y < fy2; y++)
934                 {
935                 gint xa, xb;
936
937                 if (!middle && y > y2)
938                         {
939                         slope1 = (gdouble)(y3 - y2);
940                         if (slope1) slope1 = (gdouble)(x3 - x2) / slope1;
941                         slope1_x = x2;
942                         slope1_y = y2;
943
944                         middle = TRUE;
945                         }
946
947                 xa = slope1_x + ((gdouble)slope1 * (y - slope1_y) + 0.5);
948                 xb = x1 + ((gdouble)slope2 * (y - y1) + 0.5);
949
950                 if (xa > xb)
951                         {
952                         t = xa; xa = xb; xb = t;
953                         }
954
955                 xa = CLAMP(xa, fx1, fx2);
956                 xb = CLAMP(xb, fx1, fx2);
957
958                 pp = p_pix + y * prs + xa * p_step;
959
960                 while (xa < xb)
961                         {
962                         *pp = (r * a + *pp * (256-a)) >> 8;
963                         pp++;
964                         *pp = (g * a + *pp * (256-a)) >> 8;
965                         pp++;
966                         *pp = (b * a + *pp * (256-a)) >> 8;
967                         pp++;
968                         if (has_alpha) pp++;
969
970                         xa++;
971                         }
972                 }
973 }
974
975 /*
976  *-----------------------------------------------------------------------------
977  * pixbuf drawing (line)
978  *-----------------------------------------------------------------------------
979  */
980
981 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
982                                gdouble x1, gdouble y1, gdouble x2, gdouble y2,
983                                gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
984 {
985         gboolean flip = FALSE;
986         gdouble d;
987
988         if (x1 > x2)
989                 {
990                 gdouble t;
991
992                 t = x1; x1 = x2; x2 = t;
993                 t = y1; y1 = y2; y2 = t;
994                 flip = TRUE;
995                 }
996
997         if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
998
999         if (y1 < y2)
1000                 {
1001                 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1002                 }
1003         else
1004                 {
1005                 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1006                 }
1007
1008 #if 0
1009         if (x1 >= clip_x && x2 <= clip_x + clip_w)
1010                 {
1011                 if (y1 < y2)
1012                         {
1013                         if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
1014                         }
1015                 else
1016                         {
1017                         if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
1018                         }
1019                 }
1020 #endif
1021
1022         d = x2 - x1;
1023         if (d > 0.0)
1024                 {
1025                 gdouble slope;
1026
1027                 slope = (y2 - y1) / d;
1028                 if (x1 < clip_x)
1029                         {
1030                         y1 = y1 + slope * (clip_x - x1);
1031                         x1 = clip_x;
1032                         }
1033                 if (x2 > clip_x + clip_w)
1034                         {
1035                         y2 = y2 + slope * (clip_x + clip_w - x2);
1036                         x2 = clip_x + clip_w;
1037                         }
1038                 }
1039
1040         if (y1 < y2)
1041                 {
1042                 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1043                 }
1044         else
1045                 {
1046                 gdouble t;
1047
1048                 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1049
1050                 t = x1; x1 = x2; x2 = t;
1051                 t = y1; y1 = y2; y2 = t;
1052                 flip = !flip;
1053                 }
1054
1055         d = y2 - y1;
1056         if (d > 0.0)
1057                 {
1058                 gdouble slope;
1059
1060                 slope = (x2 - x1) / d;
1061                 if (y1 < clip_y)
1062                         {
1063                         x1 = x1 + slope * (clip_y - y1);
1064                         y1 = clip_y;
1065                         }
1066                 if (y2 > clip_y + clip_h)
1067                         {
1068                         x2 = x2 + slope * (clip_y + clip_h - y2);
1069                         y2 = clip_y + clip_h;
1070                         }
1071                 }
1072
1073         if (flip)
1074                 {
1075                 *rx1 = x2;
1076                 *ry1 = y2;
1077                 *rx2 = x1;
1078                 *ry2 = y1;
1079                 }
1080         else
1081                 {
1082                 *rx1 = x1;
1083                 *ry1 = y1;
1084                 *rx2 = x2;
1085                 *ry2 = y2;
1086                 }
1087
1088         return TRUE;
1089 }
1090
1091 void pixbuf_draw_line(GdkPixbuf *pb,
1092                       gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1093                       gint x1, gint y1, gint x2, gint y2,
1094                       guint8 r, guint8 g, guint8 b, guint8 a)
1095 {
1096         gboolean has_alpha;
1097         gint pw, ph, prs;
1098         gint rx, ry, rw, rh;
1099         gdouble rx1, ry1, rx2, ry2;
1100         guchar *p_pix;
1101         guchar *pp;
1102         gint p_step;
1103         gdouble slope;
1104         gdouble x, y;
1105         gint px, py;
1106         gint cx1, cy1, cx2, cy2;
1107
1108         if (!pb) return;
1109
1110         pw = gdk_pixbuf_get_width(pb);
1111         ph = gdk_pixbuf_get_height(pb);
1112
1113         if (!util_clip_region(0, 0, pw, ph,
1114                               clip_x, clip_y, clip_w, clip_h,
1115                               &rx, &ry, &rw, &rh)) return;
1116         if (!util_clip_line((gdouble)rx, (gdouble)ry, (gdouble)rw, (gdouble)rh,
1117                             (gdouble)x1, (gdouble)y1, (gdouble)x2, (gdouble)y2,
1118                             &rx1, &ry1, &rx2, &ry2)) return;
1119
1120         cx1 = rx;
1121         cy1 = ry;
1122         cx2 = rx + rw;
1123         cy2 = ry + rh;
1124
1125         has_alpha = gdk_pixbuf_get_has_alpha(pb);
1126         prs = gdk_pixbuf_get_rowstride(pb);
1127         p_pix = gdk_pixbuf_get_pixels(pb);
1128
1129         p_step = (has_alpha) ? 4 : 3;
1130
1131         if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1132                 {
1133                 if (rx1 > rx2)
1134                         {
1135                         gdouble t;
1136                         t = rx1; rx1 = rx2; rx2 = t;
1137                         t = ry1; ry1 = ry2; ry2 = t;
1138                         }
1139
1140                 slope = rx2 - rx1;
1141                 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1142                 for (x = rx1; x < rx2; x += 1.0)
1143                         {
1144                         px = (gint)(x + 0.5);
1145                         py = (gint)(ry1 + (x - rx1) * slope + 0.5);
1146
1147                         if (px >=  cx1 && px < cx2 && py >= cy1 && py < cy2)
1148                                 {
1149                                 pp = p_pix + py * prs + px * p_step;
1150                                 *pp = (r * a + *pp * (256-a)) >> 8;
1151                                 pp++;
1152                                 *pp = (g * a + *pp * (256-a)) >> 8;
1153                                 pp++;
1154                                 *pp = (b * a + *pp * (256-a)) >> 8;
1155                                 }
1156                         }
1157                 }
1158         else
1159                 {
1160                 if (ry1 > ry2)
1161                         {
1162                         gdouble t;
1163                         t = rx1; rx1 = rx2; rx2 = t;
1164                         t = ry1; ry1 = ry2; ry2 = t;
1165                         }
1166
1167                 slope = ry2 - ry1;
1168                 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1169                 for (y = ry1; y < ry2; y += 1.0)
1170                         {
1171                         px = (gint)(rx1 + (y - ry1) * slope + 0.5);
1172                         py = (gint)(y + 0.5);
1173
1174                         if (px >=  cx1 && px < cx2 && py >= cy1 && py < cy2)
1175                                 {
1176                                 pp = p_pix + py * prs + px * p_step;
1177                                 *pp = (r * a + *pp * (256-a)) >> 8;
1178                                 pp++;
1179                                 *pp = (g * a + *pp * (256-a)) >> 8;
1180                                 pp++;
1181                                 *pp = (b * a + *pp * (256-a)) >> 8;
1182                                 }
1183                         }
1184                 }
1185 }
1186
1187 /*
1188  *-----------------------------------------------------------------------------
1189  * pixbuf drawing (fades and shadows)
1190  *-----------------------------------------------------------------------------
1191  */
1192
1193 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1194                                     gint s, gboolean vertical, gint border,
1195                                     gint x1, gint y1, gint x2, gint y2,
1196                                     guint8 r, guint8 g, guint8 b, guint8 a)
1197 {
1198         guchar *pp;
1199         gint p_step;
1200         guint8 n = a;
1201         gint i, j;
1202
1203         p_step = (has_alpha) ? 4 : 3;
1204         for (j = y1; j < y2; j++)
1205                 {
1206                 pp = p_pix + j * prs + x1 * p_step;
1207                 if (!vertical) n = a - a * abs(j - s) / border;
1208                 for (i = x1; i < x2; i++)
1209                         {
1210                         if (vertical) n = a - a * abs(i - s) / border;
1211                         *pp = (r * n + *pp * (256-n)) >> 8;
1212                         pp++;
1213                         *pp = (g * n + *pp * (256-n)) >> 8;
1214                         pp++;
1215                         *pp = (b * n + *pp * (256-n)) >> 8;
1216                         pp++;
1217                         if (has_alpha) pp++;
1218                         }
1219                 }
1220 }
1221
1222 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1223                                     gint sx, gint sy, gint border,
1224                                     gint x1, gint y1, gint x2, gint y2,
1225                                     guint8 r, guint8 g, guint8 b, guint8 a)
1226 {
1227         guchar *pp;
1228         gint p_step;
1229         gint i, j;
1230
1231         p_step = (has_alpha) ? 4 : 3;
1232         for (j = y1; j < y2; j++)
1233                 {
1234                 pp = p_pix + j * prs + x1 * p_step;
1235                 for (i = x1; i < x2; i++)
1236                         {
1237                         guint8 n;
1238                         gint r;
1239
1240                         r = MIN(border, (gint)sqrt((i-sx)*(i-sx) + (j-sy)*(j-sy)));
1241                         n = a - a * r / border;
1242                         *pp = (r * n + *pp * (256-n)) >> 8;
1243                         pp++;
1244                         *pp = (g * n + *pp * (256-n)) >> 8;
1245                         pp++;
1246                         *pp = (b * n + *pp * (256-n)) >> 8;
1247                         pp++;
1248                         if (has_alpha) pp++;
1249                         }
1250                 }
1251 }
1252
1253 void pixbuf_draw_shadow(GdkPixbuf *pb,
1254                         gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1255                         gint x, gint y, gint w, gint h, gint border,
1256                         guint8 r, guint8 g, guint8 b, guint8 a)
1257 {
1258         gint has_alpha;
1259         gint pw, ph, prs;
1260         gint rx, ry, rw, rh;
1261         gint fx, fy, fw, fh;
1262         guchar *p_pix;
1263
1264         if (!pb) return;
1265
1266         pw = gdk_pixbuf_get_width(pb);
1267         ph = gdk_pixbuf_get_height(pb);
1268
1269         if (!util_clip_region(0, 0, pw, ph,
1270                               clip_x, clip_y, clip_w, clip_h,
1271                               &rx, &ry, &rw, &rh)) return;
1272
1273         has_alpha = gdk_pixbuf_get_has_alpha(pb);
1274         prs = gdk_pixbuf_get_rowstride(pb);
1275         p_pix = gdk_pixbuf_get_pixels(pb);
1276
1277         if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1278                              rx, ry, rw, rh,
1279                              &fx, &fy, &fw, &fh))
1280                 {
1281                 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1282                 }
1283
1284         if (border < 1) return;
1285
1286         if (util_clip_region(x, y + border, border, h - border * 2,
1287                              rx, ry, rw, rh,
1288                              &fx, &fy, &fw, &fh))
1289                 {
1290                 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1291                                         x + border, TRUE, border,
1292                                         fx, fy, fx + fw, fy + fh,
1293                                         r, g, b, a);
1294                 }
1295         if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1296                              rx, ry, rw, rh,
1297                              &fx, &fy, &fw, &fh))
1298                 {
1299                 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1300                                         x + w - border, TRUE, border,
1301                                         fx, fy, fx + fw, fy + fh,
1302                                         r, g, b, a);
1303                 }
1304         if (util_clip_region(x + border, y, w - border * 2, border,
1305                              rx, ry, rw, rh,
1306                              &fx, &fy, &fw, &fh))
1307                 {
1308                 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1309                                         y + border, FALSE, border,
1310                                         fx, fy, fx + fw, fy + fh,
1311                                         r, g, b, a);
1312                 }
1313         if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1314                              rx, ry, rw, rh,
1315                              &fx, &fy, &fw, &fh))
1316                 {
1317                 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1318                                         y + h - border, FALSE, border,
1319                                         fx, fy, fx + fw, fy + fh,
1320                                         r, g, b, a);
1321                 }
1322         if (util_clip_region(x, y, border, border,
1323                              rx, ry, rw, rh,
1324                              &fx, &fy, &fw, &fh))
1325                 {
1326                 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1327                                         x + border, y + border, border,
1328                                         fx, fy, fx + fw, fy + fh,
1329                                         r, g, b, a);
1330                 }
1331         if (util_clip_region(x + w - border, y, border, border,
1332                              rx, ry, rw, rh,
1333                              &fx, &fy, &fw, &fh))
1334                 {
1335                 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1336                                         x + w - border, y + border, border,
1337                                         fx, fy, fx + fw, fy + fh,
1338                                         r, g, b, a);
1339                 }
1340         if (util_clip_region(x, y + h - border, border, border,
1341                              rx, ry, rw, rh,
1342                              &fx, &fy, &fw, &fh))
1343                 {
1344                 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1345                                         x + border, y + h - border, border,
1346                                         fx, fy, fx + fw, fy + fh,
1347                                         r, g, b, a);
1348                 }
1349         if (util_clip_region(x + w - border, y + h - border, border, border,
1350                              rx, ry, rw, rh,
1351                              &fx, &fy, &fw, &fh))
1352                 {
1353                 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1354                                         x + w - border, y + h - border, border,
1355                                         fx, fy, fx + fw, fy + fh,
1356                                         r, g, b, a);
1357                 }
1358 }
1359
1360
1361 /*
1362  *-----------------------------------------------------------------------------
1363  * pixbuf color alterations
1364  *-----------------------------------------------------------------------------
1365  */
1366
1367 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1368                             gint x, gint y, gint w, gint h)
1369 {
1370         gboolean has_alpha;
1371         gint pw, ph, prs;
1372         guchar *p_pix;
1373         guchar *pp;
1374         gint i, j;
1375
1376         if (!pb) return;
1377
1378         pw = gdk_pixbuf_get_width(pb);
1379         ph = gdk_pixbuf_get_height(pb);
1380
1381         if (x < 0 || x + w > pw) return;
1382         if (y < 0 || y + h > ph) return;
1383
1384         has_alpha = gdk_pixbuf_get_has_alpha(pb);
1385         prs = gdk_pixbuf_get_rowstride(pb);
1386         p_pix = gdk_pixbuf_get_pixels(pb);
1387
1388         for (i = 0; i < h; i++)
1389                 {
1390                 pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
1391                 for (j = 0; j < w; j++)
1392                         {
1393                         guint8 grey;
1394
1395                         grey = (pp[0] + pp[1] + pp[2]) / 3;
1396                         *pp = grey;
1397                         pp++;
1398                         *pp = grey;
1399                         pp++;
1400                         *pp = grey;
1401                         pp++;
1402                         if (has_alpha) pp++;
1403                         }
1404                 }
1405 }
1406 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */