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