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