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