Use util_clip_triangle() in pan_item_tri_new()
[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 #include <utility>
29
30 #include <cairo.h>
31 #include <gio/gio.h>
32 #include <glib-object.h>
33 #include <pango/pangocairo.h>
34
35 #include <config.h>
36
37 #include "debug.h"
38 #include "exif.h"
39 #include "filedata.h"
40 #include "main-defines.h"
41 #include "typedefs.h"
42 #include "ui-fileops.h"
43
44
45 /*
46  *-----------------------------------------------------------------------------
47  * png save
48  *-----------------------------------------------------------------------------
49  */
50
51 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
52 {
53         GError *error = nullptr;
54         gboolean ret;
55
56         if (!pixbuf || !filename) return FALSE;
57
58         ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
59                               "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
60
61         if (error)
62                 {
63                 log_printf("Error saving png file: %s\n", error->message);
64                 g_error_free(error);
65                 }
66
67         return ret;
68 }
69
70 /*
71  *-----------------------------------------------------------------------------
72  * jpeg save
73  *-----------------------------------------------------------------------------
74  */
75
76 #pragma GCC diagnostic push
77 #pragma GCC diagnostic ignored "-Wunused-function"
78 gboolean pixbuf_to_file_as_jpg_unused(GdkPixbuf *pixbuf, const gchar *filename, gint quality)
79 {
80         GError *error = nullptr;
81         gchar *qbuf;
82         gboolean ret;
83
84         if (!pixbuf || !filename) return FALSE;
85
86         if (quality == -1) quality = 75;
87         if (quality < 1 || quality > 100)
88                 {
89                 log_printf("Jpeg not saved, invalid quality %d\n", quality);
90                 return FALSE;
91                 }
92
93         qbuf = g_strdup_printf("%d", quality);
94         ret = gdk_pixbuf_save(pixbuf, filename, "jpeg", &error, "quality", qbuf, NULL);
95         g_free(qbuf);
96
97         if (error)
98                 {
99                 log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
100                 g_error_free(error);
101                 }
102
103         return ret;
104 }
105 #pragma GCC diagnostic pop
106
107 /*
108  *-----------------------------------------------------------------------------
109  * pixbuf from inline
110  *-----------------------------------------------------------------------------
111  */
112
113 struct PixbufInline
114 {
115         const gchar *key;
116         const gchar *data;
117 };
118
119 static PixbufInline inline_pixbuf_data[] = {
120         {  PIXBUF_INLINE_ARCHIVE,                "gq-icon-archive-file" },
121         {  PIXBUF_INLINE_BROKEN,                 "gq-icon-broken" },
122         {  PIXBUF_INLINE_COLLECTION,             "gq-icon-collection" },
123         {  PIXBUF_INLINE_ICON_180,               "gq-icon-rotate-180" },
124         {  PIXBUF_INLINE_ICON_BOOK,              "gq-icon-book" },
125         {  PIXBUF_INLINE_ICON_CONFIG,            "gq-icon-config" },
126         {  PIXBUF_INLINE_ICON_DRAW_RECTANGLE,    "gq-icon-draw-rectangle" },
127         {  PIXBUF_INLINE_ICON_EXIF,              "gq-icon-exif" },
128         {  PIXBUF_INLINE_ICON_EXPOSURE,          "gq-icon-exposure" },
129         {  PIXBUF_INLINE_ICON_FLOAT,             "gq-icon-float" },
130         {  PIXBUF_INLINE_ICON,                   "gqview-icon" },
131         {  PIXBUF_INLINE_ICON_GRAYSCALE,         "gq-icon-grayscale" },
132         {  PIXBUF_INLINE_ICON_HEIF,              "gq-icon-heic" },
133         {  PIXBUF_INLINE_ICON_HIDETOOLS,         "gq-icon-hidetools" },
134         {  PIXBUF_INLINE_ICON_MAINTENANCE,       "gq-icon-maintenance" },
135         {  PIXBUF_INLINE_ICON_MARKS,             "gq-icon-marks" },
136         {  PIXBUF_INLINE_ICON_MOVE,              "gq-icon-move" },
137         {  PIXBUF_INLINE_ICON_ORIGINAL,          "gq-icon-original" },
138         {  PIXBUF_INLINE_ICON_PANORAMA,          "gq-icon-panorama" },
139         {  PIXBUF_INLINE_ICON_PDF,               "gq-icon-pdf" },
140         {  PIXBUF_INLINE_ICON_PROPERTIES,        "gq-icon-properties" },
141         {  PIXBUF_INLINE_ICON_RENAME,            "gq-icon-rename" },
142         {  PIXBUF_INLINE_ICON_SELECT_ALL,        "gq-icon-select-all" },
143         {  PIXBUF_INLINE_ICON_SELECT_INVERT,     "gq-icon-select-invert" },
144         {  PIXBUF_INLINE_ICON_SELECT_NONE,       "gq-icon-select-none" },
145         {  PIXBUF_INLINE_ICON_SELECT_RECTANGLE,  "gq-icon-select-rectangle" },
146         {  PIXBUF_INLINE_ICON_SORT,              "gq-icon-sort" },
147         {  PIXBUF_INLINE_ICON_THUMB,             "gq-icon-thumb" },
148         {  PIXBUF_INLINE_ICON_TOOLS,             "gq-icon-tools" },
149         {  PIXBUF_INLINE_ICON_VIEW,              "gq-icon-view" },
150         {  PIXBUF_INLINE_ICON_ZOOMFILLHOR,       "gq-icon-zoomfillhor" },
151         {  PIXBUF_INLINE_ICON_ZOOMFILLVERT,      "gq-icon-zoomfillvert" },
152         {  PIXBUF_INLINE_LOGO,                   "geeqie-logo" },
153         {  PIXBUF_INLINE_METADATA,               "gq-icon-metadata" },
154         {  PIXBUF_INLINE_SCROLLER,               "gq-scroller" },
155         {  PIXBUF_INLINE_SPLIT_PANE_SYNC,        "gq-icon-split-pane-sync" },
156         {  PIXBUF_INLINE_UNKNOWN,                "gq-icon-unknown" },
157         {  PIXBUF_INLINE_VIDEO,                  "gq-icon-video" },
158         {  nullptr,                              nullptr }
159 };
160
161 GdkPixbuf *pixbuf_inline(const gchar *key)
162 {
163         gboolean dark = FALSE;
164         gchar *file_name = nullptr;
165         gchar *path;
166         gchar *theme_name;
167         GdkPixbuf *icon_pixbuf;
168         GError *error = nullptr;
169         GInputStream *in_stream;
170         gint i;
171         GtkSettings *settings;
172
173         if (!key) return nullptr;
174
175         settings = gtk_settings_get_default();
176         g_object_get(settings, "gtk-theme-name", &theme_name, nullptr);
177         dark = g_str_has_suffix(theme_name, "dark");
178         g_free(theme_name);
179
180         i = 0;
181         while (inline_pixbuf_data[i].key)
182                 {
183                 if (strcmp(inline_pixbuf_data[i].key, key) == 0)
184                         {
185                         file_name = g_strconcat(inline_pixbuf_data[i].data, dark ? "-dark" : "", ".png", nullptr);
186                         path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
187                         g_free(file_name);
188
189                         in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
190                         g_free(path);
191
192                         if (error)
193                                 {
194                                 g_error_free(error);
195                                 error = nullptr;
196
197                                 file_name = g_strconcat(inline_pixbuf_data[i].data, ".png", nullptr);
198                                 path = g_build_filename(GQ_RESOURCE_PATH_ICONS, file_name, nullptr);
199                                 g_free(file_name);
200
201                                 in_stream = g_resources_open_stream(path, G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
202                                 g_free(path);
203                                 }
204
205                         if (error)
206                                 {
207                                 log_printf("warning: inline pixbuf error: %s", error->message);
208                                 g_error_free(error);
209                                 g_object_unref(in_stream);
210                                 return nullptr;
211                                 }
212
213                         icon_pixbuf = gdk_pixbuf_new_from_stream(in_stream, nullptr, &error);
214                         g_object_unref(in_stream);
215
216                         if (error)
217                                 {
218                                 log_printf("warning: inline pixbuf error: %s", error->message);
219                                 g_error_free(error);
220                                 return nullptr;
221                                 }
222
223                         return icon_pixbuf;
224                         }
225                 i++;
226                 }
227
228         log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
229
230         return nullptr;
231 }
232
233 static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
234 {
235         static GtkIconFactory *icon_factory = nullptr;
236         GtkIconSet *icon_set;
237
238         if (!icon_factory)
239                 {
240                 icon_factory = gtk_icon_factory_new();
241                 gtk_icon_factory_add_default(icon_factory);
242                 }
243
244         icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
245         gtk_icon_factory_add(icon_factory, key, icon_set);
246 }
247
248
249 void pixbuf_inline_register_stock_icons()
250 {
251         gint i;
252
253         i = 0;
254         while (inline_pixbuf_data[i].key)
255                 {
256                 register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
257                 i++;
258                 }
259 }
260
261 gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
262 {
263         GtkIconTheme *icon_theme;
264         GdkPixbuf *pixbuf;
265         GError *error = nullptr;
266
267         icon_theme = gtk_icon_theme_get_default();
268
269         if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
270
271         pixbuf = gtk_icon_theme_load_icon(icon_theme,
272                            icon, /* icon name */
273                            64, /* size */
274                            static_cast<GtkIconLookupFlags>(0),  /* flags */
275                            &error);
276         if (!pixbuf)
277                 {
278                 if (error)
279                         {
280                         DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
281                         g_error_free(error);
282                         error = nullptr;
283                         }
284
285                 if (strchr(icon, '.'))
286                         {
287                         /* try again without extension */
288                         gchar *icon2 = remove_extension_from_path(icon);
289                         pixbuf = gtk_icon_theme_load_icon(icon_theme,
290                                            icon2, /* icon name */
291                                            64, /* size */
292                                            static_cast<GtkIconLookupFlags>(0),  /* flags */
293                                            &error);
294                         if (error)
295                                 {
296                                 DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
297                                 g_error_free(error);
298                                 error = nullptr;
299
300                                 /* try as an absolute path */
301                                 pixbuf = gdk_pixbuf_new_from_file(icon, &error);
302                                 if (error)
303                                         {
304                                         DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
305                                         g_error_free(error);
306                                         }
307                                 }
308                         g_free(icon2);
309                         }
310                 }
311
312         if (!pixbuf) return FALSE;
313
314         register_stock_icon(key, pixbuf);
315         return TRUE;
316 }
317
318 gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
319                              gint old_w, gint old_h,
320                              gint *new_w, gint *new_h)
321 {
322         if ((static_cast<gdouble>(req_w) / old_w) < (static_cast<gdouble>(req_h) / old_h))
323                 {
324                 *new_w = req_w;
325                 *new_h = static_cast<gdouble>(*new_w) / old_w * old_h;
326                 if (*new_h < 1) *new_h = 1;
327                 }
328         else
329                 {
330                 *new_h = req_h;
331                 *new_w = static_cast<gdouble>(*new_h) / old_h * old_w;
332                 if (*new_w < 1) *new_w = 1;
333                 }
334
335         return (*new_w != old_w || *new_h != old_h);
336 }
337
338 GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
339 {
340         GdkPixbuf *pixbuf;
341
342         switch (fd->format_class)
343                 {
344                 case FORMAT_CLASS_UNKNOWN:
345                         pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
346                         break;
347                 case FORMAT_CLASS_META:
348                         pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
349                         break;
350                 case FORMAT_CLASS_VIDEO:
351                         pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
352                         break;
353                 case FORMAT_CLASS_COLLECTION:
354                         pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
355                         break;
356                 case FORMAT_CLASS_DOCUMENT:
357                         pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
358                         break;
359                 case FORMAT_CLASS_ARCHIVE:
360                         pixbuf = pixbuf_inline(PIXBUF_INLINE_ARCHIVE);
361                         break;
362                 default:
363                         pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
364                 }
365
366         if (requested_width && requested_height)
367                 {
368                 gint w = gdk_pixbuf_get_width(pixbuf);
369                 gint h = gdk_pixbuf_get_height(pixbuf);
370
371                 if (w > requested_width || h > requested_height)
372                         {
373                         gint nw;
374                         gint nh;
375
376                         if (pixbuf_scale_aspect(requested_width, requested_height,
377                                                           w, h, &nw, &nh))
378                                 {
379                                 GdkPixbuf *tmp;
380
381                                 tmp = pixbuf;
382                                 pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
383                                 g_object_unref(G_OBJECT(tmp));
384                                 }
385                         }
386                 }
387         return pixbuf;
388 }
389
390
391 /*
392  *-----------------------------------------------------------------------------
393  * misc utils
394  *-----------------------------------------------------------------------------
395  */
396
397 gboolean util_clip_region(gint x, gint y, gint w, gint h,
398                           gint clip_x, gint clip_y, gint clip_w, gint clip_h,
399                           gint &rx, gint &ry, gint &rw, gint &rh)
400 {
401         GdkRectangle main{x, y, w, h};
402         GdkRectangle clip{clip_x, clip_y, clip_w, clip_h};
403         GdkRectangle r;
404
405         const gboolean rectangles_intersect = gdk_rectangle_intersect(&main, &clip, &r);
406         if (rectangles_intersect)
407                 {
408                 rx = r.x;
409                 ry = r.y;
410                 rw = r.width;
411                 rh = r.height;
412                 }
413
414         return rectangles_intersect;
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                         // TODO(xsdg): Should we do anything about a potential
746                         // existing alpha value here?
747                         pp += p_step;
748                         }
749                 }
750 }
751
752 #pragma GCC diagnostic push
753 #pragma GCC diagnostic ignored "-Wunused-function"
754 void pixbuf_draw_rect_unused(GdkPixbuf *pb,
755                       gint x, gint y, gint w, gint h,
756                       gint r, gint g, gint b, gint a,
757                       gint left, gint right, gint top, gint bottom)
758 {
759         pixbuf_draw_rect_fill(pb, x + left, y, w - left - right, top,
760                               r, g, b ,a);
761         pixbuf_draw_rect_fill(pb, x + w - right, y, right, h,
762                               r, g, b ,a);
763         pixbuf_draw_rect_fill(pb, x + left, y + h - bottom, w - left - right, bottom,
764                               r, g, b ,a);
765         pixbuf_draw_rect_fill(pb, x, y, left, h,
766                               r, g, b ,a);
767 }
768 #pragma GCC diagnostic pop
769
770 void pixbuf_set_rect_fill(GdkPixbuf *pb,
771                           gint x, gint y, gint w, gint h,
772                           gint r, gint g, gint b, gint a)
773 {
774         gboolean has_alpha;
775         gint pw;
776         gint ph;
777         gint prs;
778         guchar *p_pix;
779         guchar *pp;
780         gint i;
781         gint j;
782
783         if (!pb) return;
784
785         pw = gdk_pixbuf_get_width(pb);
786         ph = gdk_pixbuf_get_height(pb);
787
788         if (x < 0 || x + w > pw) return;
789         if (y < 0 || y + h > ph) return;
790
791         has_alpha = gdk_pixbuf_get_has_alpha(pb);
792         prs = gdk_pixbuf_get_rowstride(pb);
793         p_pix = gdk_pixbuf_get_pixels(pb);
794
795         const gint p_step = has_alpha ? 4 : 3;
796
797         for (i = 0; i < h; i++)
798                 {
799                 pp = p_pix + (y + i) * prs + (x * p_step);
800                 for (j = 0; j < w; j++)
801                         {
802                         *pp = r; pp++;
803                         *pp = g; pp++;
804                         *pp = b; pp++;
805                         if (has_alpha) { *pp = a; pp++; }
806                         }
807                 }
808 }
809
810 void pixbuf_set_rect(GdkPixbuf *pb,
811                      gint x, gint y, gint w, gint h,
812                      gint r, gint g, gint b, gint a,
813                      gint left_width, gint right_width, gint top_width, gint bottom_width)
814 {
815         // TODO(xsdg): This function has multiple off-by-one errors.  Would be
816         // much easier to read (and implement correctly) with temporaries to
817         // translate from (x, y, w, h) coordinates to (x1, y1, x2, y2).
818         pixbuf_set_rect_fill(pb,
819                              x + left_width, y, w - left_width - right_width, top_width,
820                              r, g, b ,a);
821         pixbuf_set_rect_fill(pb,
822                              x + w - right_width, y, right_width, h,
823                              r, g, b ,a);
824         pixbuf_set_rect_fill(pb,
825                              x + left_width, y + h - bottom_width, w - left_width - right_width, bottom_width,
826                              r, g, b ,a);
827         pixbuf_set_rect_fill(pb,
828                              x, y, left_width, h,
829                              r, g, b ,a);
830 }
831
832 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
833 {
834         guchar *buf;
835         gboolean has_alpha;
836         gint rowstride;
837         guchar *p;
838
839         if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
840             y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
841
842         buf = gdk_pixbuf_get_pixels(pb);
843         has_alpha = gdk_pixbuf_get_has_alpha(pb);
844         rowstride = gdk_pixbuf_get_rowstride(pb);
845
846         p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
847         *p = r; p++;
848         *p = g; p++;
849         *p = b; p++;
850         if (has_alpha) *p = a;
851 }
852
853
854 /*
855  *-----------------------------------------------------------------------------
856  * pixbuf text rendering
857  *-----------------------------------------------------------------------------
858  */
859
860 static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
861                              GdkPixbuf *dest, gint dx, gint dy,
862                              gint w, gint h,
863                              guint8 r, guint8 g, guint8 b, guint8 a)
864 {
865         gint sw;
866         gint sh;
867         gint srs;
868         gboolean s_alpha;
869         gint s_step;
870         guchar *s_pix;
871         gint dw;
872         gint dh;
873         gint drs;
874         gboolean d_alpha;
875         gint d_step;
876         guchar *d_pix;
877
878         guchar *sp;
879         guchar *dp;
880         gint i;
881         gint j;
882
883         if (!src || !dest) return;
884
885         sw = gdk_pixbuf_get_width(src);
886         sh = gdk_pixbuf_get_height(src);
887
888         if (sx < 0 || sx + w > sw) return;
889         if (sy < 0 || sy + h > sh) return;
890
891         dw = gdk_pixbuf_get_width(dest);
892         dh = gdk_pixbuf_get_height(dest);
893
894         if (dx < 0 || dx + w > dw) return;
895         if (dy < 0 || dy + h > dh) return;
896
897         s_alpha = gdk_pixbuf_get_has_alpha(src);
898         d_alpha = gdk_pixbuf_get_has_alpha(dest);
899         srs = gdk_pixbuf_get_rowstride(src);
900         drs = gdk_pixbuf_get_rowstride(dest);
901         s_pix = gdk_pixbuf_get_pixels(src);
902         d_pix = gdk_pixbuf_get_pixels(dest);
903
904         s_step = (s_alpha) ? 4 : 3;
905         d_step = (d_alpha) ? 4 : 3;
906
907         for (i = 0; i < h; i++)
908                 {
909                 sp = s_pix + (sy + i) * srs + sx * s_step;
910                 dp = d_pix + (dy + i) * drs + dx * d_step;
911                 for (j = 0; j < w; j++)
912                         {
913                         if (*sp)
914                                 {
915                                 guint8 asub;
916
917                                 asub = a * sp[0] / 255;
918                                 *dp = (r * asub + *dp * (256-asub)) >> 8;
919                                 dp++;
920                                 asub = a * sp[1] / 255;
921                                 *dp = (g * asub + *dp * (256-asub)) >> 8;
922                                 dp++;
923                                 asub = a * sp[2] / 255;
924                                 *dp = (b * asub + *dp * (256-asub)) >> 8;
925                                 dp++;
926
927                                 if (d_alpha)
928                                         {
929                                         *dp = MAX(*dp, a * ((sp[0] + sp[1] + sp[2]) / 3) / 255);
930                                         dp++;
931                                         }
932                                 }
933                         else
934                                 {
935                                 dp += d_step;
936                                 }
937
938                         sp += s_step;
939                         }
940                 }
941 }
942
943 void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *,
944                         gint x, gint y,
945                         guint8 r, guint8 g, guint8 b, guint8 a)
946 {
947         GdkPixbuf *buffer;
948         gint w;
949         gint h;
950         gint sx;
951         gint sy;
952         gint dw;
953         gint dh;
954         cairo_surface_t *source;
955         cairo_t *cr;
956
957         pango_layout_get_pixel_size(layout, &w, &h);
958         if (w < 1 || h < 1) return;
959
960         source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
961
962         cr = cairo_create (source);
963         cairo_set_source_rgb(cr, 0, 0, 0);
964         cairo_rectangle (cr, 0, 0, w, h);
965         cairo_fill (cr);
966         cairo_set_source_rgb(cr, 1, 1, 1);
967         pango_cairo_show_layout (cr, layout);
968         cairo_destroy (cr);
969
970         buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
971                                            GDK_COLORSPACE_RGB,
972                                            cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
973                                            8,
974                                            cairo_image_surface_get_width (source),
975                                            cairo_image_surface_get_height (source),
976                                            cairo_image_surface_get_stride (source),
977                                            nullptr,
978                                            nullptr);
979
980         sx = 0;
981         sy = 0;
982         dw = gdk_pixbuf_get_width(pixbuf);
983         dh = gdk_pixbuf_get_height(pixbuf);
984
985         if (x < 0)
986                 {
987                 w += x;
988                 sx = -x;
989                 x = 0;
990                 }
991
992         if (y < 0)
993                 {
994                 h += y;
995                 sy = -y;
996                 y = 0;
997                 }
998
999         if (x + w > dw) w = dw - x;
1000         if (y + h > dh) h = dh - y;
1001
1002         pixbuf_copy_font(buffer, sx, sy,
1003                          pixbuf, x, y, w, h,
1004                          r, g, b, a);
1005
1006         g_object_unref(buffer);
1007         cairo_surface_destroy(source);
1008 }
1009
1010 /*
1011  *-----------------------------------------------------------------------------
1012  * pixbuf drawing (triangle)
1013  *-----------------------------------------------------------------------------
1014  */
1015
1016 void util_clip_triangle(gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
1017                         gint &rx, gint &ry, gint &rw, gint &rh)
1018 {
1019         rx = std::min({x1, x2, x3});
1020         ry = std::min({y1, y2, y3});
1021         rw = std::max({abs(x1 - x2), abs(x2 - x3), abs(x3 - x1)});
1022         rh = std::max({abs(y1 - y2), abs(y2 - y3), abs(y3 - y1)});
1023 }
1024
1025 void pixbuf_draw_triangle(GdkPixbuf *pb,
1026                           gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1027                           gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
1028                           guint8 r, guint8 g, guint8 b, guint8 a)
1029 {
1030         gboolean has_alpha;
1031         gint pw;
1032         gint ph;
1033         gint prs;
1034         gint rx;
1035         gint ry;
1036         gint rw;
1037         gint rh;
1038         gint tx;
1039         gint ty;
1040         gint tw;
1041         gint th;
1042         gint fx1;
1043         gint fy1;
1044         gint fx2;
1045         gint fy2;
1046         gint fw;
1047         gint fh;
1048         guchar *p_pix;
1049         guchar *pp;
1050         gint p_step;
1051         gdouble slope1;
1052         gdouble slope2;
1053         gint slope1_x;
1054         gint slope1_y;
1055         gint y;
1056         gboolean middle = FALSE;
1057
1058         if (!pb) return;
1059
1060         pw = gdk_pixbuf_get_width(pb);
1061         ph = gdk_pixbuf_get_height(pb);
1062
1063         // Intersects the clip region with the pixbuf. r{x,y,w,h} is that
1064         // intersecting region.
1065         if (!util_clip_region(0, 0, pw, ph,
1066                               clip_x, clip_y, clip_w, clip_h,
1067                               rx, ry, rw, rh)) return;
1068
1069         // Determine the bounding box for the triangle.
1070         util_clip_triangle(x1, y1, x2, y2, x3, y3,
1071                            tx, ty, tw, th);
1072
1073         // And now clip the triangle bounding box to the pixbuf clipping region.
1074         if (!util_clip_region(rx, ry, rw, rh,
1075                               tx, ty, tw, th,
1076                               fx1, fy1, fw, fh)) return;
1077         fx2 = fx1 + fw;
1078         fy2 = fy1 + fh;
1079
1080         has_alpha = gdk_pixbuf_get_has_alpha(pb);
1081         prs = gdk_pixbuf_get_rowstride(pb);
1082         p_pix = gdk_pixbuf_get_pixels(pb);
1083
1084         p_step = (has_alpha) ? 4 : 3;
1085
1086         // Ensure that points are ordered by increasing y coordinate.
1087         if (y1 > y2)
1088                 {
1089                 std::swap(x1, x2);
1090                 std::swap(y1, y2);
1091                 }
1092         if (y2 > y3)
1093                 {
1094                 std::swap(x2, x3);
1095                 std::swap(y2, y3);
1096                 }
1097         if (y1 > y2)
1098                 {
1099                 std::swap(x1, x2);
1100                 std::swap(y1, y2);
1101                 }
1102
1103         // TODO(xsdg): Drop these explicit casts.  Int will always promote to
1104         // double without issue.
1105         slope1 = static_cast<gdouble>(y2 - y1);
1106         if (slope1) slope1 = static_cast<gdouble>(x2 - x1) / slope1;
1107         slope1_x = x1;
1108         slope1_y = y1;
1109         slope2 = static_cast<gdouble>(y3 - y1);
1110         if (slope2) slope2 = static_cast<gdouble>(x3 - x1) / slope2;
1111
1112         for (y = fy1; y < fy2; y++)
1113                 {
1114                 gint xa;
1115                 gint xb;
1116
1117                 if (!middle && y > y2)
1118                         {
1119                         slope1 = static_cast<gdouble>(y3 - y2);
1120                         if (slope1) slope1 = static_cast<gdouble>(x3 - x2) / slope1;
1121                         slope1_x = x2;
1122                         slope1_y = y2;
1123
1124                         middle = TRUE;
1125                         }
1126
1127                 xa = slope1_x + (slope1 * (y - slope1_y) + 0.5);
1128                 xb = x1 + (slope2 * (y - y1) + 0.5);
1129
1130                 if (xa > xb)
1131                         {
1132                         std::swap(xa, xb);
1133                         }
1134
1135                 xa = CLAMP(xa, fx1, fx2);
1136                 xb = CLAMP(xb, fx1, fx2);
1137
1138                 pp = p_pix + y * prs + xa * p_step;
1139
1140                 while (xa < xb)
1141                         {
1142                         pp[0] = (r * a + pp[0] * (256-a)) >> 8;
1143                         pp[1] = (g * a + pp[1] * (256-a)) >> 8;
1144                         pp[2] = (b * a + pp[2] * (256-a)) >> 8;
1145                         pp += p_step;
1146
1147                         xa++;
1148                         }
1149                 }
1150 }
1151
1152 /*
1153  *-----------------------------------------------------------------------------
1154  * pixbuf drawing (line)
1155  *-----------------------------------------------------------------------------
1156  */
1157
1158 /**
1159  * @brief Clips the specified line segment to the specified clipping region.
1160  * @param[in] clip_x,clip_y Coordinates of the top-left corner of the clipping region.
1161  * @param[in] clip_w,clip_h Extent of the clipping region.
1162  * @param[in] x1,y1 Coordinates of the first point of the line segment.
1163  * @param[in] x2,y2 Coordinates of the second point of the line segment.
1164  * @param[out] rx1,ry1 Computed coordinates of the first point of the clipped line segment.
1165  * @param[out] rx2,ry2 Computed coordinates of the second point of the clipped line segment.
1166  * @retval FALSE The line segment lies outside of the clipping region.
1167  * @retval TRUE The clip operation was performed, and the output params were set.
1168  */
1169 static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
1170                                gdouble x1, gdouble y1, gdouble x2, gdouble y2,
1171                                gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
1172 {
1173         gboolean flip = FALSE;
1174         gdouble d;
1175
1176         // Normalize: Line endpoint 1 must be farther left.
1177         if (x1 > x2)
1178                 {
1179                 std::swap(x1, x2);
1180                 std::swap(y1, y2);
1181                 flip = TRUE;
1182                 }
1183
1184         // Ensure the line horizontally overlaps with the clip region.
1185         if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
1186
1187         // Ensure the line vertically overlaps with the clip region.
1188         // Note that a line can both horizontally and vertically overlap with
1189         // clipping region, while still being outside of the clipping region.  That
1190         // case is detected further below.
1191         if (y1 < y2)
1192                 {
1193                 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1194                 }
1195         else
1196                 {
1197                 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1198                 }
1199
1200         d = x2 - x1;
1201         // TODO(xsdg): Either use ints here, or define a reasonable epsilon to do the
1202         // right thing if -epsilon < d < 0.  We already guaranteed above that x2 >= x1.
1203         if (d > 0.0)
1204                 {
1205                 gdouble slope;
1206
1207                 slope = (y2 - y1) / d;
1208                 // If needed, project (x1, y1) to be horizontally within the clip
1209                 // region, while maintaining the line's slope and y-offset.
1210                 if (x1 < clip_x)
1211                         {
1212                         y1 = y1 + slope * (clip_x - x1);
1213                         x1 = clip_x;
1214                         }
1215                 // Likewise with (x2, y2).
1216                 if (x2 > clip_x + clip_w)
1217                         {
1218                         y2 = y2 + slope * (clip_x + clip_w - x2);
1219                         x2 = clip_x + clip_w;
1220                         }
1221                 }
1222
1223         // Check that any horizontal projections didn't cause the line segment to
1224         // no longer vertically overlap with the clip region.
1225         if (y1 < y2)
1226                 {
1227                 if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
1228                 }
1229         else
1230                 {
1231                 if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
1232
1233                 // Re-normalize: line endpoint 1 must be farther up.
1234                 std::swap(x1, x2);
1235                 std::swap(y1, y2);
1236                 flip = !flip;
1237                 }
1238
1239         d = y2 - y1;
1240         if (d > 0.0)
1241                 {
1242                 gdouble slope;
1243
1244                 slope = (x2 - x1) / d;
1245                 // If needed, project (x1, y1) to be vertically within the clip
1246                 // region, while maintaining the line's slope and x-offset.
1247                 if (y1 < clip_y)
1248                         {
1249                         x1 = x1 + slope * (clip_y - y1);
1250                         y1 = clip_y;
1251                         }
1252                 // Likewise with (x2, y2).
1253                 if (y2 > clip_y + clip_h)
1254                         {
1255                         x2 = x2 + slope * (clip_y + clip_h - y2);
1256                         y2 = clip_y + clip_h;
1257                         }
1258                 }
1259
1260         // Set the output params, accounting for any flips that might have
1261         // happened during normalization.
1262         if (flip)
1263                 {
1264                 *rx1 = x2;
1265                 *ry1 = y2;
1266                 *rx2 = x1;
1267                 *ry2 = y1;
1268                 }
1269         else
1270                 {
1271                 *rx1 = x1;
1272                 *ry1 = y1;
1273                 *rx2 = x2;
1274                 *ry2 = y2;
1275                 }
1276
1277         return TRUE;
1278 }
1279
1280 void pixbuf_draw_line(GdkPixbuf *pb,
1281                       gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1282                       gint x1, gint y1, gint x2, gint y2,
1283                       guint8 r, guint8 g, guint8 b, guint8 a)
1284 {
1285         gboolean has_alpha;
1286         gint pw;
1287         gint ph;
1288         gint prs;
1289         gint rx;
1290         gint ry;
1291         gint rw;
1292         gint rh;
1293         gdouble rx1;
1294         gdouble ry1;
1295         gdouble rx2;
1296         gdouble ry2;
1297         guchar *p_pix;
1298         guchar *pp;
1299         gint p_step;
1300         gdouble slope;
1301         gdouble x;
1302         gdouble y;
1303         gint px;
1304         gint py;
1305         gint cx1;
1306         gint cy1;
1307         gint cx2;
1308         gint cy2;
1309
1310         if (!pb) return;
1311
1312         pw = gdk_pixbuf_get_width(pb);
1313         ph = gdk_pixbuf_get_height(pb);
1314
1315         // Intersects the clip region with the pixbuf. r{x,y,w,h} is that
1316         // intersecting region.
1317         if (!util_clip_region(0, 0, pw, ph,
1318                               clip_x, clip_y, clip_w, clip_h,
1319                               rx, ry, rw, rh)) return;
1320         // TODO(xsdg): These explicit casts are unnecessary and harm readability.
1321         // Clips the specified line segment to the intersecting region from above.
1322         if (!util_clip_line(static_cast<gdouble>(rx), static_cast<gdouble>(ry), static_cast<gdouble>(rw), static_cast<gdouble>(rh),
1323                             static_cast<gdouble>(x1), static_cast<gdouble>(y1), static_cast<gdouble>(x2), static_cast<gdouble>(y2),
1324                             &rx1, &ry1, &rx2, &ry2)) return;
1325
1326         cx1 = rx;
1327         cy1 = ry;
1328         cx2 = rx + rw;
1329         cy2 = ry + rh;
1330
1331         has_alpha = gdk_pixbuf_get_has_alpha(pb);
1332         prs = gdk_pixbuf_get_rowstride(pb);
1333         p_pix = gdk_pixbuf_get_pixels(pb);
1334
1335         p_step = (has_alpha) ? 4 : 3;
1336
1337         // We draw the clipped line segment along the longer axis first, and
1338         // allow the shorter axis to follow.  This is because our raster line segment
1339         // will contain max(rx2-rx1, ry2-ry1) pixels, and the pixels along the
1340         // shorter axis may not advance for each cycle (the line is not anti-aliased).
1341         if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
1342                 {
1343                 if (rx1 > rx2)
1344                         {
1345                         std::swap(rx1, rx2);
1346                         std::swap(ry1, ry2);
1347                         }
1348
1349                 slope = rx2 - rx1;
1350                 if (slope != 0.0) slope = (ry2 - ry1) / slope;
1351                 for (x = rx1; x < rx2; x += 1.0)
1352                         {
1353                         px = static_cast<gint>(x + 0.5);
1354                         py = static_cast<gint>(ry1 + (x - rx1) * slope + 0.5);
1355
1356                         if (px >=  cx1 && px < cx2 && py >= cy1 && py < cy2)
1357                                 {
1358                                 pp = p_pix + py * prs + px * p_step;
1359                                 *pp = (r * a + *pp * (256-a)) >> 8;
1360                                 pp++;
1361                                 *pp = (g * a + *pp * (256-a)) >> 8;
1362                                 pp++;
1363                                 *pp = (b * a + *pp * (256-a)) >> 8;
1364                                 }
1365                         }
1366                 }
1367         else
1368                 {
1369                 if (ry1 > ry2)
1370                         {
1371                         std::swap(rx1, rx2);
1372                         std::swap(ry1, ry2);
1373                         }
1374
1375                 slope = ry2 - ry1;
1376                 if (slope != 0.0) slope = (rx2 - rx1) / slope;
1377                 for (y = ry1; y < ry2; y += 1.0)
1378                         {
1379                         px = static_cast<gint>(rx1 + (y - ry1) * slope + 0.5);
1380                         py = static_cast<gint>(y + 0.5);
1381
1382                         if (px >=  cx1 && px < cx2 && py >= cy1 && py < cy2)
1383                                 {
1384                                 pp = p_pix + py * prs + px * p_step;
1385                                 *pp = (r * a + *pp * (256-a)) >> 8;
1386                                 pp++;
1387                                 *pp = (g * a + *pp * (256-a)) >> 8;
1388                                 pp++;
1389                                 *pp = (b * a + *pp * (256-a)) >> 8;
1390                                 }
1391                         }
1392                 }
1393 }
1394
1395 /*
1396  *-----------------------------------------------------------------------------
1397  * pixbuf drawing (fades and shadows)
1398  *-----------------------------------------------------------------------------
1399  */
1400
1401 /**
1402  * @brief Composites a horizontal or vertical linear gradient into the rectangular
1403  *        region defined by corners `(x1, y1)` and `(x2, y2)`.  Note that the
1404  *        current implementation breaks if the max distance between `s` and
1405  *        `x1/x2/y1/y2` is greater than `border`.
1406  * @param p_pix The pixel buffer to paint into.
1407  * @param prs The pixel row stride (how many pixels per row of the buffer).
1408  * @param has_alpha TRUE if the p_pix representation is rgba.  FALSE if just rgb.
1409  * @param s The "center" of the gradient, along the axis defined by `vertical`.
1410  *          Note that if the center is not along an edge, the gradient will be
1411  *          symmetric about the center.
1412  * @param vertical When `TRUE`, the gradient color will vary vertically.  When `FALSE`,
1413  *                 horizontally.
1414  * @param border The maximum extent of the gradient, in pixels.
1415  * @param x1,y1 Coordinates of the first corner of the region.
1416  * @param x2,y2 Coordinates of the second corner of the region.
1417  * @param r,g,b Base color of the gradient.
1418  * @param a The peak alpha value when compositing the gradient.  The alpha varies
1419  *          from this value down to 0 (fully transparent).  Note that any alpha
1420  *          value associated with the original pixel is unmodified.
1421  */
1422 static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
1423                                     gint s, gboolean vertical, gint border,
1424                                     gint x1, gint y1, gint x2, gint y2,
1425                                     guint8 r, guint8 g, guint8 b, guint8 a)
1426 {
1427         guchar *pp;
1428         gint p_step;
1429         guint8 n = a;
1430         gint i;
1431         gint j;
1432
1433         p_step = (has_alpha) ? 4 : 3;
1434         for (j = y1; j < y2; j++)
1435                 {
1436                 pp = p_pix + j * prs + x1 * p_step;
1437                 if (!vertical) n = a - a * abs(j - s) / border;
1438                 for (i = x1; i < x2; i++)
1439                         {
1440                         if (vertical) n = a - a * abs(i - s) / border;
1441                         pp[0] = (r * n + pp[0] * (256-n)) >> 8;
1442                         pp[1] = (g * n + pp[1] * (256-n)) >> 8;
1443                         pp[2] = (b * n + pp[2] * (256-n)) >> 8;
1444                         pp += p_step;
1445                         }
1446                 }
1447 }
1448
1449 /**
1450  * @brief Composites a radial gradient into the rectangular region defined by
1451  *        corners `(x1, y1)` and `(x2, y2)`.
1452  * @param p_pix The pixel buffer to paint into.
1453  * @param prs The pixel row stride (how many pixels per row of the buffer).
1454  * @param has_alpha TRUE if the p_pix representation is rgba.  FALSE if just rgb.
1455  * @param sx,sy The coordinates of the center of the gradient.
1456  * @param border The max radius, in pixels, of the gradient.  Pixels farther away
1457  *               from the center than this will be unaffected.
1458  * @param x1,y1 Coordinates of the first corner of the region.
1459  * @param x2,y2 Coordinates of the second corner of the region.
1460  * @param r,g,b Base color of the gradient.
1461  * @param a The peak alpha value when compositing the gradient.  The alpha varies
1462  *          from this value down to 0 (fully transparent).  Note that any alpha
1463  *          value associated with the original pixel is unmodified.
1464  */
1465 static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
1466                                     gint sx, gint sy, gint border,
1467                                     gint x1, gint y1, gint x2, gint y2,
1468                                     guint8, guint8 g, guint8 b, guint8 a)
1469 {
1470         // TODO(xsdg): r (red) was shadowed by r (radius), and was removed from
1471         // the params list by an automated cleanup.  Fix this and distinguish the
1472         // red param from the radius temporary variable.
1473         guchar *pp;
1474         gint p_step;
1475         gint i;
1476         gint j;
1477
1478         p_step = (has_alpha) ? 4 : 3;
1479         for (j = y1; j < y2; j++)
1480                 {
1481                 pp = p_pix + j * prs + x1 * p_step;
1482                 for (i = x1; i < x2; i++)
1483                         {
1484                         guint8 n;
1485                         gint r;
1486
1487                         r = MIN(border, (gint)hypot(i - sx, j - sy));
1488                         n = a - a * r / border;
1489                         pp[0] = (r * n + pp[0] * (256-n)) >> 8;
1490                         pp[1] = (g * n + pp[1] * (256-n)) >> 8;
1491                         pp[2] = (b * n + pp[2] * (256-n)) >> 8;
1492                         pp += p_step;
1493                         }
1494                 }
1495 }
1496
1497 void pixbuf_draw_shadow(GdkPixbuf *pb,
1498                         gint clip_x, gint clip_y, gint clip_w, gint clip_h,
1499                         gint x, gint y, gint w, gint h, gint border,
1500                         guint8 r, guint8 g, guint8 b, guint8 a)
1501 {
1502         gint has_alpha;
1503         gint pw;
1504         gint ph;
1505         gint prs;
1506         gint rx;
1507         gint ry;
1508         gint rw;
1509         gint rh;
1510         gint fx;
1511         gint fy;
1512         gint fw;
1513         gint fh;
1514         guchar *p_pix;
1515
1516         if (!pb) return;
1517
1518         pw = gdk_pixbuf_get_width(pb);
1519         ph = gdk_pixbuf_get_height(pb);
1520
1521         // Intersects the clip region with the pixbuf. r{x,y,w,h} is that
1522         // intersecting region.
1523         if (!util_clip_region(0, 0, pw, ph,
1524                               clip_x, clip_y, clip_w, clip_h,
1525                               rx, ry, rw, rh)) return;
1526
1527         has_alpha = gdk_pixbuf_get_has_alpha(pb);
1528         prs = gdk_pixbuf_get_rowstride(pb);
1529         p_pix = gdk_pixbuf_get_pixels(pb);
1530
1531         // Composites the specified color into the rectangle specified by x, y, w, h,
1532         // as contracted by `border` pixels, with a composition fraction that's defined
1533         // by the supplied `a` parameter.
1534         if (util_clip_region(x + border, y + border, w - border * 2, h - border * 2,
1535                              rx, ry, rw, rh,
1536                              fx, fy, fw, fh))
1537                 {
1538                 pixbuf_draw_rect_fill(pb, fx, fy, fw, fh, r, g, b, a);
1539                 }
1540
1541         if (border < 1) return;
1542
1543         // Draws linear gradients along each of the 4 edges.
1544         if (util_clip_region(x, y + border, border, h - border * 2,
1545                              rx, ry, rw, rh,
1546                              fx, fy, fw, fh))
1547                 {
1548                 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1549                                         x + border, TRUE, border,
1550                                         fx, fy, fx + fw, fy + fh,
1551                                         r, g, b, a);
1552                 }
1553         if (util_clip_region(x + w - border, y + border, border, h - border * 2,
1554                              rx, ry, rw, rh,
1555                              fx, fy, fw, fh))
1556                 {
1557                 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1558                                         x + w - border, TRUE, border,
1559                                         fx, fy, fx + fw, fy + fh,
1560                                         r, g, b, a);
1561                 }
1562         if (util_clip_region(x + border, y, w - border * 2, border,
1563                              rx, ry, rw, rh,
1564                              fx, fy, fw, fh))
1565                 {
1566                 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1567                                         y + border, FALSE, border,
1568                                         fx, fy, fx + fw, fy + fh,
1569                                         r, g, b, a);
1570                 }
1571         if (util_clip_region(x + border, y + h - border, w - border * 2, border,
1572                              rx, ry, rw, rh,
1573                              fx, fy, fw, fh))
1574                 {
1575                 pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
1576                                         y + h - border, FALSE, border,
1577                                         fx, fy, fx + fw, fy + fh,
1578                                         r, g, b, a);
1579                 }
1580         // Draws radial gradients at each of the 4 corners.
1581         if (util_clip_region(x, y, border, border,
1582                              rx, ry, rw, rh,
1583                              fx, fy, fw, fh))
1584                 {
1585                 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1586                                         x + border, y + border, border,
1587                                         fx, fy, fx + fw, fy + fh,
1588                                         r, g, b, a);
1589                 }
1590         if (util_clip_region(x + w - border, y, border, border,
1591                              rx, ry, rw, rh,
1592                              fx, fy, fw, fh))
1593                 {
1594                 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1595                                         x + w - border, y + border, border,
1596                                         fx, fy, fx + fw, fy + fh,
1597                                         r, g, b, a);
1598                 }
1599         if (util_clip_region(x, y + h - border, border, border,
1600                              rx, ry, rw, rh,
1601                              fx, fy, fw, fh))
1602                 {
1603                 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1604                                         x + border, y + h - border, border,
1605                                         fx, fy, fx + fw, fy + fh,
1606                                         r, g, b, a);
1607                 }
1608         if (util_clip_region(x + w - border, y + h - border, border, border,
1609                              rx, ry, rw, rh,
1610                              fx, fy, fw, fh))
1611                 {
1612                 pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
1613                                         x + w - border, y + h - border, border,
1614                                         fx, fy, fx + fw, fy + fh,
1615                                         r, g, b, a);
1616                 }
1617 }
1618
1619
1620 /*
1621  *-----------------------------------------------------------------------------
1622  * pixbuf color alterations
1623  *-----------------------------------------------------------------------------
1624  */
1625
1626 void pixbuf_desaturate_rect(GdkPixbuf *pb,
1627                             gint x, gint y, gint w, gint h)
1628 {
1629         gboolean has_alpha;
1630         gint pw;
1631         gint ph;
1632         gint prs;
1633         guchar *p_pix;
1634         guchar *pp;
1635         gint i;
1636         gint j;
1637
1638         if (!pb) return;
1639
1640         pw = gdk_pixbuf_get_width(pb);
1641         ph = gdk_pixbuf_get_height(pb);
1642
1643         if (x < 0 || x + w > pw) return;
1644         if (y < 0 || y + h > ph) return;
1645
1646         has_alpha = gdk_pixbuf_get_has_alpha(pb);
1647         prs = gdk_pixbuf_get_rowstride(pb);
1648         p_pix = gdk_pixbuf_get_pixels(pb);
1649
1650         const gint p_step = has_alpha ? 4 : 3;
1651
1652         for (i = 0; i < h; i++)
1653                 {
1654                 pp = p_pix + (y + i) * prs + (x * p_step);
1655                 for (j = 0; j < w; j++)
1656                         {
1657                         guint8 grey;
1658
1659                         grey = (pp[0] + pp[1] + pp[2]) / 3;
1660                         pp[0] = grey;
1661                         pp[1] = grey;
1662                         pp[2] = grey;
1663                         pp += p_step;
1664                         }
1665                 }
1666 }
1667
1668 /*
1669  *-----------------------------------------------------------------------------
1670  * pixbuf highlight under/over exposure
1671  *-----------------------------------------------------------------------------
1672  */
1673 void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
1674 {
1675         gboolean has_alpha;
1676         gint pw;
1677         gint ph;
1678         gint prs;
1679         guchar *p_pix;
1680         guchar *pp;
1681         gint i;
1682         gint j;
1683
1684         if (!pb) return;
1685
1686         pw = gdk_pixbuf_get_width(pb);
1687         ph = gdk_pixbuf_get_height(pb);
1688
1689         if (x < 0 || x + w > pw) return;
1690         if (y < 0 || y + h > ph) return;
1691
1692         has_alpha = gdk_pixbuf_get_has_alpha(pb);
1693         prs = gdk_pixbuf_get_rowstride(pb);
1694         p_pix = gdk_pixbuf_get_pixels(pb);
1695
1696         const gint p_step = has_alpha ? 4 : 3;
1697
1698         for (i = 0; i < h; i++)
1699                 {
1700                 pp = p_pix + (y + i) * prs + (x * p_step);
1701                 for (j = 0; j < w; j++)
1702                         {
1703                         if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
1704                                 {
1705                                 pp[0] = 255;
1706                                 pp[1] = 0;
1707                                 pp[2] = 0;
1708                                 }
1709                         pp += p_step;
1710                         }
1711                 }
1712 }
1713
1714 /*
1715  *-----------------------------------------------------------------------------
1716  * pixbuf ignore alpha
1717  *-----------------------------------------------------------------------------
1718 */
1719 void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
1720                               gint x, gint y, gint w, gint h)
1721 {
1722    gboolean has_alpha;
1723    gint pw;
1724    gint ph;
1725    gint prs;
1726    guchar *p_pix;
1727    guchar *pp;
1728    gint i;
1729    gint j;
1730
1731    if (!pb) return;
1732
1733    pw = gdk_pixbuf_get_width(pb);
1734    ph = gdk_pixbuf_get_height(pb);
1735
1736    if (x < 0 || x + w > pw) return;
1737    if (y < 0 || y + h > ph) return;
1738
1739    has_alpha = gdk_pixbuf_get_has_alpha(pb);
1740    if (!has_alpha) return;
1741
1742    prs = gdk_pixbuf_get_rowstride(pb);
1743    p_pix = gdk_pixbuf_get_pixels(pb);
1744
1745    for (i = 0; i < h; i++)
1746        {
1747        pp = p_pix + (y + i) * prs + (x * 4 );
1748        for (j = 0; j < w; j++)
1749            {
1750            pp[3] = 0xff;
1751            pp+=4;
1752            }
1753        }
1754 }
1755 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */