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