Include a Other Software section in Help file
[geeqie.git] / src / pixbuf_util.c
index a82d587..9d10630 100644 (file)
@@ -1,17 +1,28 @@
 /*
- * GQview
- * (C) 2004 John Ellis
+ * Copyright (C) 2004 John Ellis
+ * Copyright (C) 2008 - 2016 The Geeqie Team
  *
  * Author: John Ellis
  *
- * This software is released under the GNU General Public License (GNU GPL).
- * Please read the included file COPYING for more information.
- * This software comes with no warranty of any kind, use at your own risk!
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-
-#include "gqview.h"
+#include "main.h"
 #include "pixbuf_util.h"
+#include "exif.h"
+#include "ui_fileops.h"
 
 #include "icons/icons_inline.h"
 
  *-----------------------------------------------------------------------------
  */
 
-gboolean pixbuf_to_file_as_png (GdkPixbuf *pixbuf, const char *filename)
+gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
 {
        GError *error = NULL;
-       gint ret;
+       gboolean ret;
 
        if (!pixbuf || !filename) return FALSE;
 
        ret = gdk_pixbuf_save(pixbuf, filename, "png", &error,
-                             "tEXt::Software", "GQview "VERSION, NULL);
+                             "tEXt::Software", GQ_APPNAME " " VERSION, NULL);
 
        if (error)
                {
-               printf("Error saving png file: %s\n", error->message);
+               log_printf("Error saving png file: %s\n", error->message);
                g_error_free(error);
                }
 
@@ -60,7 +71,7 @@ gboolean pixbuf_to_file_as_jpg(GdkPixbuf *pixbuf, const gchar *filename, gint qu
        if (quality == -1) quality = 75;
        if (quality < 1 || quality > 100)
                {
-               printf("Jpeg not saved, invalid quality %d\n", quality);
+               log_printf("Jpeg not saved, invalid quality %d\n", quality);
                return FALSE;
                }
 
@@ -70,7 +81,7 @@ gboolean pixbuf_to_file_as_jpg(GdkPixbuf *pixbuf, const gchar *filename, gint qu
 
        if (error)
                {
-               printf("Error saving jpeg to %s\n%s\n", filename, error->message);
+               log_printf("Error saving jpeg to %s\n%s\n", filename, error->message);
                g_error_free(error);
                }
 
@@ -97,14 +108,48 @@ static PixbufInline inline_pixbuf_data[] = {
        { PIXBUF_INLINE_FOLDER_UP,      folder_up },
        { PIXBUF_INLINE_SCROLLER,       icon_scroller },
        { PIXBUF_INLINE_BROKEN,         icon_broken },
+       { PIXBUF_INLINE_METADATA,       icon_metadata },
+       { PIXBUF_INLINE_UNKNOWN,        icon_unknown },
+       { PIXBUF_INLINE_VIDEO,          icon_video },
+       { PIXBUF_INLINE_COLLECTION,     icon_collection },
        { PIXBUF_INLINE_ICON,           gqview_icon },
-       { PIXBUF_INLINE_LOGO,           gqview_logo },
+       { PIXBUF_INLINE_LOGO,           geeqie_logo },
        { PIXBUF_INLINE_ICON_FLOAT,     icon_float },
        { PIXBUF_INLINE_ICON_THUMB,     icon_thumb },
        { PIXBUF_INLINE_ICON_BOOK,      icon_book },
        { PIXBUF_INLINE_ICON_CONFIG,    icon_config },
        { PIXBUF_INLINE_ICON_TOOLS,     icon_tools },
        { PIXBUF_INLINE_ICON_VIEW,      icon_view },
+       { PIXBUF_INLINE_ICON_GUIDELINES,        icon_guidelines },
+       { PIXBUF_INLINE_ICON_PANORAMA,  icon_panorama },
+       { PIXBUF_INLINE_ICON_MAINTENANCE,       icon_maintenance },
+       { PIXBUF_INLINE_ICON_ZOOMFILLHOR,       icon_zoomfillhor },
+       { PIXBUF_INLINE_ICON_ZOOMFILLVERT,      icon_zoomfillvert },
+       { PIXBUF_INLINE_ICON_HIDETOOLS, icon_hidetools },
+       { PIXBUF_INLINE_ICON_EXIF,      icon_exif },
+       { PIXBUF_INLINE_ICON_MARKS,     icon_marks },
+       { PIXBUF_INLINE_ICON_INFO,      icon_info },
+       { PIXBUF_INLINE_ICON_SORT,      icon_sort },
+       { PIXBUF_INLINE_ICON_PDF,       icon_pdf },
+       { PIXBUF_INLINE_ICON_DRAW_RECTANGLE,    icon_draw_rectangle },
+       { PIXBUF_INLINE_ICON_MOVE,      icon_move },
+       { PIXBUF_INLINE_ICON_RENAME,    icon_rename },
+       { PIXBUF_INLINE_ICON_SELECT_ALL,        icon_select_all },
+       { PIXBUF_INLINE_ICON_SELECT_NONE,       icon_select_none },
+       { PIXBUF_INLINE_ICON_SELECT_INVERT,     icon_select_invert },
+       { PIXBUF_INLINE_ICON_SELECT_RECTANGLE,  icon_select_rectangle },
+       { PIXBUF_INLINE_ICON_FILE_FILTER,       icon_file_filter },
+       { PIXBUF_INLINE_ICON_CW,        icon_rotate_clockwise },
+       { PIXBUF_INLINE_ICON_CCW,       icon_rotate_counter_clockwise },
+       { PIXBUF_INLINE_ICON_180,       icon_rotate_180 },
+       { PIXBUF_INLINE_ICON_MIRROR,    icon_mirror },
+       { PIXBUF_INLINE_ICON_FLIP,      icon_flip },
+       { PIXBUF_INLINE_ICON_ORIGINAL,  icon_original },
+       { PIXBUF_INLINE_ICON_TRASH,     icon_trash },
+       { PIXBUF_INLINE_ICON_HEIF,      icon_heic },
+       { PIXBUF_INLINE_ICON_GRAYSCALE, icon_grayscale },
+       { PIXBUF_INLINE_ICON_EXPOSURE,  icon_exposure },
+       { PIXBUF_INLINE_SPLIT_PANE_SYNC, icon_split_pane_sync },
        { NULL, NULL }
 };
 
@@ -124,20 +169,174 @@ GdkPixbuf *pixbuf_inline(const gchar *key)
                i++;
                }
 
-       printf("warning: inline pixbuf key \"%s\" not found.\n", key);
+       log_printf("warning: inline pixbuf key \"%s\" not found.\n", key);
 
        return NULL;
 }
 
+static void register_stock_icon(const gchar *key, GdkPixbuf *pixbuf)
+{
+       static GtkIconFactory *icon_factory = NULL;
+       GtkIconSet *icon_set;
+
+       if (!icon_factory)
+               {
+               icon_factory = gtk_icon_factory_new();
+               gtk_icon_factory_add_default(icon_factory);
+               }
+
+       icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
+       gtk_icon_factory_add(icon_factory, key, icon_set);
+}
+
+
+void pixbuf_inline_register_stock_icons(void)
+{
+       gint i;
+
+       i = 0;
+       while (inline_pixbuf_data[i].key)
+               {
+               register_stock_icon(inline_pixbuf_data[i].key, pixbuf_inline(inline_pixbuf_data[i].key));
+               i++;
+               }
+}
+
+gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
+{
+       GtkIconTheme *icon_theme;
+       GdkPixbuf *pixbuf;
+       GError *error = NULL;
+
+       icon_theme = gtk_icon_theme_get_default();
+
+       if (gtk_icon_theme_has_icon(icon_theme, key)) return FALSE;
+
+       pixbuf = gtk_icon_theme_load_icon(icon_theme,
+                           icon, /* icon name */
+                           64, /* size */
+                           0,  /* flags */
+                           &error);
+       if (!pixbuf)
+               {
+               if (error)
+                       {
+                       DEBUG_1("Couldn't load icon %s: %s", icon, error->message);
+                       g_error_free(error);
+                       error = NULL;
+                       }
+
+               if (strchr(icon, '.'))
+                       {
+                       /* try again without extension */
+                       gchar *icon2 = remove_extension_from_path(icon);
+                       pixbuf = gtk_icon_theme_load_icon(icon_theme,
+                                          icon2, /* icon name */
+                                          64, /* size */
+                                          0,  /* flags */
+                                          &error);
+                       if (error)
+                               {
+                               DEBUG_1("Couldn't load icon %s: %s", icon2, error->message);
+                               g_error_free(error);
+                               error = NULL;
+
+                               /* try as an absolute path */
+                               pixbuf = gdk_pixbuf_new_from_file(icon, &error);
+                               if (error)
+                                       {
+                                       DEBUG_1("Couldn't load icon as absolute path %s: %s", icon, error->message);
+                                       g_error_free(error);
+                                       }
+                               }
+                       g_free(icon2);
+                       }
+               }
+
+       if (!pixbuf) return FALSE;
+
+       register_stock_icon(key, pixbuf);
+       return TRUE;
+}
+
+gboolean pixbuf_scale_aspect(gint req_w, gint req_h,
+                            gint old_w, gint old_h,
+                            gint *new_w, gint *new_h)
+{
+       if (((gdouble)req_w / old_w) < ((gdouble)req_h / old_h))
+               {
+               *new_w = req_w;
+               *new_h = (gdouble)*new_w / old_w * old_h;
+               if (*new_h < 1) *new_h = 1;
+               }
+       else
+               {
+               *new_h = req_h;
+               *new_w = (gdouble)*new_h / old_h * old_w;
+               if (*new_w < 1) *new_w = 1;
+               }
+
+       return (*new_w != old_w || *new_h != old_h);
+}
+
+GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_height)
+{
+       GdkPixbuf *pixbuf;
+
+       switch (fd->format_class)
+               {
+               case FORMAT_CLASS_UNKNOWN:
+                       pixbuf = pixbuf_inline(PIXBUF_INLINE_UNKNOWN);
+                       break;
+               case FORMAT_CLASS_META:
+                       pixbuf = pixbuf_inline(PIXBUF_INLINE_METADATA);
+                       break;
+               case FORMAT_CLASS_VIDEO:
+                       pixbuf = pixbuf_inline(PIXBUF_INLINE_VIDEO);
+                       break;
+               case FORMAT_CLASS_COLLECTION:
+                       pixbuf = pixbuf_inline(PIXBUF_INLINE_COLLECTION);
+                       break;
+               case FORMAT_CLASS_DOCUMENT:
+                       pixbuf = pixbuf_inline(PIXBUF_INLINE_ICON_PDF);
+                       break;
+               default:
+                       pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
+               }
+
+       if (requested_width && requested_height)
+               {
+               gint w = gdk_pixbuf_get_width(pixbuf);
+               gint h = gdk_pixbuf_get_height(pixbuf);
+
+               if (w > requested_width || h > requested_height)
+                       {
+                       gint nw, nh;
+
+                       if (pixbuf_scale_aspect(requested_width, requested_height,
+                                                         w, h, &nw, &nh))
+                               {
+                               GdkPixbuf *tmp;
+
+                               tmp = pixbuf;
+                               pixbuf = gdk_pixbuf_scale_simple(tmp, nw, nh, GDK_INTERP_TILES);
+                               g_object_unref(G_OBJECT(tmp));
+                               }
+                       }
+               }
+       return pixbuf;
+}
+
+
 /*
  *-----------------------------------------------------------------------------
  * misc utils
  *-----------------------------------------------------------------------------
  */
 
-gint util_clip_region(gint x, gint y, gint w, gint h,
-                     gint clip_x, gint clip_y, gint clip_w, gint clip_h,
-                     gint *rx, gint *ry, gint *rw, gint *rh)
+gboolean util_clip_region(gint x, gint y, gint w, gint h,
+                         gint clip_x, gint clip_y, gint clip_w, gint clip_h,
+                         gint *rx, gint *ry, gint *rw, gint *rh)
 {
        if (clip_x + clip_w <= x ||
            clip_x >= x + w ||
@@ -164,7 +363,7 @@ gint util_clip_region(gint x, gint y, gint w, gint h,
 
 static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, gint y,
                                     guchar *dest, gint dest_row_stride, gint w, gint h,
-                                    gint bytes_per_pixel, gint counter_clockwise)
+                                    gint bytes_per_pixel, gboolean counter_clockwise)
 {
        gint i, j;
        guchar *sp;
@@ -189,7 +388,7 @@ static void pixbuf_copy_block_rotate(guchar *src, gint src_row_stride, gint x, g
                        if (bytes_per_pixel == 4) *(dp) = *(sp++);      /* a */
                        }
                }
-       
+
 }
 
 static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
@@ -214,22 +413,18 @@ static void pixbuf_copy_block(guchar *src, gint src_row_stride, gint w, gint h,
  * Returns a copy of pixbuf src rotated 90 degrees clockwise or 90 counterclockwise
  *
  */
-GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gint counter_clockwise)
+GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gboolean counter_clockwise)
 {
        GdkPixbuf *dest;
-       gint has_alpha;
+       gboolean has_alpha;
        gint sw, sh, srs;
        gint dw, dh, drs;
        guchar *s_pix;
-        guchar *d_pix;
-#if 0
-       guchar *sp;
-        guchar *dp;
-#endif
+       guchar *d_pix;
        gint i, j;
        gint a;
        GdkPixbuf *buffer;
-        guchar *b_pix;
+       guchar *b_pix;
        gint brs;
        gint w, h;
 
@@ -281,7 +476,7 @@ GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gint counter_clockwise)
                        }
                }
 
-       gdk_pixbuf_unref(buffer);
+       g_object_unref(buffer);
 
 #if 0
        /* this is the simple version of rotation (roughly 2-4x slower) */
@@ -316,16 +511,16 @@ GdkPixbuf *pixbuf_copy_rotate_90(GdkPixbuf *src, gint counter_clockwise)
  * TO do a 180 degree rotations set both mirror and flipped TRUE
  * if mirror and flip are FALSE, result is a simple copy.
  */
-GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gint mirror, gint flip)
+GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gboolean mirror, gboolean flip)
 {
        GdkPixbuf *dest;
-       gint has_alpha;
+       gboolean has_alpha;
        gint w, h, srs;
        gint drs;
        guchar *s_pix;
-        guchar *d_pix;
+       guchar *d_pix;
        guchar *sp;
-        guchar *dp;
+       guchar *dp;
        gint i, j;
        gint a;
 
@@ -381,6 +576,53 @@ GdkPixbuf *pixbuf_copy_mirror(GdkPixbuf *src, gint mirror, gint flip)
        return dest;
 }
 
+GdkPixbuf *pixbuf_apply_orientation(GdkPixbuf *pixbuf, gint orientation)
+{
+       GdkPixbuf *dest;
+       GdkPixbuf *tmp = NULL;
+
+       switch (orientation)
+               {
+               case EXIF_ORIENTATION_TOP_LEFT:
+                       dest = gdk_pixbuf_copy(pixbuf);
+                       break;
+               case EXIF_ORIENTATION_TOP_RIGHT:
+                       /* mirrored */
+                       dest = pixbuf_copy_mirror(pixbuf, TRUE, FALSE);
+                       break;
+               case EXIF_ORIENTATION_BOTTOM_RIGHT:
+                       /* upside down */
+                       dest = pixbuf_copy_mirror(pixbuf, TRUE, TRUE);
+                       break;
+               case EXIF_ORIENTATION_BOTTOM_LEFT:
+                       /* flipped */
+                       dest = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
+                       break;
+               case EXIF_ORIENTATION_LEFT_TOP:
+                       tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
+                       dest = pixbuf_copy_rotate_90(tmp, FALSE);
+                       break;
+               case EXIF_ORIENTATION_RIGHT_TOP:
+                       /* rotated -90 (270) */
+                       dest = pixbuf_copy_rotate_90(pixbuf, FALSE);
+                       break;
+               case EXIF_ORIENTATION_RIGHT_BOTTOM:
+                       tmp = pixbuf_copy_mirror(pixbuf, FALSE, TRUE);
+                       dest = pixbuf_copy_rotate_90(tmp, TRUE);
+                       break;
+               case EXIF_ORIENTATION_LEFT_BOTTOM:
+                       /* rotated 90 */
+                       dest = pixbuf_copy_rotate_90(pixbuf, TRUE);
+                       break;
+               default:
+                       dest = gdk_pixbuf_copy(pixbuf);
+                       break;
+               }
+       if (tmp) g_object_unref(tmp);
+       return dest;
+
+}
+
 
 /*
  *-----------------------------------------------------------------------------
@@ -397,7 +639,7 @@ void pixbuf_draw_rect_fill(GdkPixbuf *pb,
                           gint x, gint y, gint w, gint h,
                           gint r, gint g, gint b, gint a)
 {
-       gint p_alpha;
+       gboolean has_alpha;
        gint pw, ph, prs;
        guchar *p_pix;
        guchar *pp;
@@ -411,13 +653,13 @@ void pixbuf_draw_rect_fill(GdkPixbuf *pb,
        if (x < 0 || x + w > pw) return;
        if (y < 0 || y + h > ph) return;
 
-       p_alpha = gdk_pixbuf_get_has_alpha(pb);
+       has_alpha = gdk_pixbuf_get_has_alpha(pb);
        prs = gdk_pixbuf_get_rowstride(pb);
        p_pix = gdk_pixbuf_get_pixels(pb);
 
-        for (i = 0; i < h; i++)
+       for (i = 0; i < h; i++)
                {
-               pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
+               pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
                for (j = 0; j < w; j++)
                        {
                        *pp = (r * a + *pp * (256-a)) >> 8;
@@ -426,7 +668,7 @@ void pixbuf_draw_rect_fill(GdkPixbuf *pb,
                        pp++;
                        *pp = (b * a + *pp * (256-a)) >> 8;
                        pp++;
-                       if (p_alpha) pp++;
+                       if (has_alpha) pp++;
                        }
                }
 }
@@ -450,7 +692,7 @@ void pixbuf_set_rect_fill(GdkPixbuf *pb,
                          gint x, gint y, gint w, gint h,
                          gint r, gint g, gint b, gint a)
 {
-       gint p_alpha;
+       gboolean has_alpha;
        gint pw, ph, prs;
        guchar *p_pix;
        guchar *pp;
@@ -464,19 +706,19 @@ void pixbuf_set_rect_fill(GdkPixbuf *pb,
        if (x < 0 || x + w > pw) return;
        if (y < 0 || y + h > ph) return;
 
-       p_alpha = gdk_pixbuf_get_has_alpha(pb);
+       has_alpha = gdk_pixbuf_get_has_alpha(pb);
        prs = gdk_pixbuf_get_rowstride(pb);
        p_pix = gdk_pixbuf_get_pixels(pb);
 
-        for (i = 0; i < h; i++)
+       for (i = 0; i < h; i++)
                {
-               pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
+               pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
                for (j = 0; j < w; j++)
                        {
                        *pp = r; pp++;
                        *pp = g; pp++;
                        *pp = b; pp++;
-                       if (p_alpha) { *pp = a; pp++; }
+                       if (has_alpha) { *pp = a; pp++; }
                        }
                }
 }
@@ -499,18 +741,18 @@ void pixbuf_set_rect(GdkPixbuf *pb,
 void pixbuf_pixel_set(GdkPixbuf *pb, gint x, gint y, gint r, gint g, gint b, gint a)
 {
        guchar *buf;
-       gint has_alpha;
+       gboolean has_alpha;
        gint rowstride;
        guchar *p;
 
        if (x < 0 || x >= gdk_pixbuf_get_width(pb) ||
-            y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
+           y < 0 || y >= gdk_pixbuf_get_height(pb)) return;
 
        buf = gdk_pixbuf_get_pixels(pb);
        has_alpha = gdk_pixbuf_get_has_alpha(pb);
        rowstride = gdk_pixbuf_get_rowstride(pb);
 
-        p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
+       p = buf + (y * rowstride) + (x * (has_alpha ? 4 : 3));
        *p = r; p++;
        *p = g; p++;
        *p = b; p++;
@@ -530,11 +772,11 @@ static void pixbuf_copy_font(GdkPixbuf *src, gint sx, gint sy,
                             guint8 r, guint8 g, guint8 b, guint8 a)
 {
        gint sw, sh, srs;
-       gint s_alpha;
+       gboolean s_alpha;
        gint s_step;
        guchar *s_pix;
        gint dw, dh, drs;
-       gint d_alpha;
+       gboolean d_alpha;
        gint d_step;
        guchar *d_pix;
 
@@ -606,30 +848,35 @@ void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widge
                        gint x, gint y,
                        guint8 r, guint8 g, guint8 b, guint8 a)
 {
-       GdkPixmap *pixmap;
        GdkPixbuf *buffer;
        gint w, h;
-       GdkGC *gc;
        gint sx, sy;
        gint dw, dh;
-
-       if (!widget || !widget->window) return;
+       cairo_surface_t *source;
+       cairo_t *cr;
 
        pango_layout_get_pixel_size(layout, &w, &h);
-       pixmap = gdk_pixmap_new(widget->window, w, h, -1);
-
-       gc = gdk_gc_new(widget->window);
-       gdk_gc_copy(gc, widget->style->black_gc);
-       gdk_draw_rectangle(pixmap, gc, TRUE, 0, 0, w, h);
-       gdk_gc_copy(gc, widget->style->white_gc);
-       gdk_draw_layout(pixmap, gc, 0, 0, layout);
-       g_object_unref(gc);
-
-       buffer = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, w, h);
-       gdk_pixbuf_get_from_drawable(buffer, pixmap,
-                                    gdk_drawable_get_colormap(widget->window),
-                                    0, 0, 0, 0, w, h);
-       g_object_unref(pixmap);
+       if (w < 1 || h < 1) return;
+
+       source = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
+
+       cr = cairo_create (source);
+       cairo_set_source_rgb(cr, 0, 0, 0);
+       cairo_rectangle (cr, 0, 0, w, h);
+       cairo_fill (cr);
+       cairo_set_source_rgb(cr, 1, 1, 1);
+       pango_cairo_show_layout (cr, layout);
+       cairo_destroy (cr);
+
+       buffer = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (source),
+                                          GDK_COLORSPACE_RGB,
+                                          cairo_image_surface_get_format (source) == CAIRO_FORMAT_ARGB32,
+                                          8,
+                                          cairo_image_surface_get_width (source),
+                                          cairo_image_surface_get_height (source),
+                                          cairo_image_surface_get_stride (source),
+                                          NULL,
+                                          NULL);
 
        sx = 0;
        sy = 0;
@@ -658,6 +905,7 @@ void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widge
                         r, g, b, a);
 
        g_object_unref(buffer);
+       cairo_surface_destroy(source);
 }
 
 /*
@@ -691,7 +939,7 @@ void pixbuf_draw_triangle(GdkPixbuf *pb,
                          gint x1, gint y1, gint x2, gint y2, gint x3, gint y3,
                          guint8 r, guint8 g, guint8 b, guint8 a)
 {
-       gint p_alpha;
+       gboolean has_alpha;
        gint pw, ph, prs;
        gint rx, ry, rw, rh;
        gint tx, ty, tw, th;
@@ -705,7 +953,7 @@ void pixbuf_draw_triangle(GdkPixbuf *pb,
        gint slope1_x, slope1_y;
        gint y;
        gint t;
-       gint middle = FALSE;
+       gboolean middle = FALSE;
 
        if (!pb) return;
 
@@ -725,11 +973,11 @@ void pixbuf_draw_triangle(GdkPixbuf *pb,
        fx2 = fx1 + fw;
        fy2 = fy1 + fh;
 
-       p_alpha = gdk_pixbuf_get_has_alpha(pb);
+       has_alpha = gdk_pixbuf_get_has_alpha(pb);
        prs = gdk_pixbuf_get_rowstride(pb);
        p_pix = gdk_pixbuf_get_pixels(pb);
 
-       p_step = (p_alpha) ? 4 : 3;
+       p_step = (has_alpha) ? 4 : 3;
 
        if (y1 > y2)
                {
@@ -789,7 +1037,7 @@ void pixbuf_draw_triangle(GdkPixbuf *pb,
                        pp++;
                        *pp = (b * a + *pp * (256-a)) >> 8;
                        pp++;
-                       if (p_alpha) pp++;
+                       if (has_alpha) pp++;
 
                        xa++;
                        }
@@ -802,11 +1050,11 @@ void pixbuf_draw_triangle(GdkPixbuf *pb,
  *-----------------------------------------------------------------------------
  */
 
-static gint util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
-                          gdouble x1, gdouble y1, gdouble x2, gdouble y2,
-                          gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
+static gboolean util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
+                              gdouble x1, gdouble y1, gdouble x2, gdouble y2,
+                              gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
 {
-       gint flip = FALSE;
+       gboolean flip = FALSE;
        gdouble d;
 
        if (x1 > x2)
@@ -829,20 +1077,6 @@ static gint util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdoub
                if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
                }
 
-#if 0
-       if (x1 >= clip_x && x2 <= clip_x + clip_w)
-               {
-               if (y1 < y2)
-                       {
-                       if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
-                       }
-               else
-                       {
-                       if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
-                       }
-               }
-#endif
-
        d = x2 - x1;
        if (d > 0.0)
                {
@@ -917,7 +1151,7 @@ void pixbuf_draw_line(GdkPixbuf *pb,
                      gint x1, gint y1, gint x2, gint y2,
                      guint8 r, guint8 g, guint8 b, guint8 a)
 {
-       gint p_alpha;
+       gboolean has_alpha;
        gint pw, ph, prs;
        gint rx, ry, rw, rh;
        gdouble rx1, ry1, rx2, ry2;
@@ -946,11 +1180,11 @@ void pixbuf_draw_line(GdkPixbuf *pb,
        cx2 = rx + rw;
        cy2 = ry + rh;
 
-       p_alpha = gdk_pixbuf_get_has_alpha(pb);
+       has_alpha = gdk_pixbuf_get_has_alpha(pb);
        prs = gdk_pixbuf_get_rowstride(pb);
        p_pix = gdk_pixbuf_get_pixels(pb);
 
-       p_step = (p_alpha) ? 4 : 3;
+       p_step = (has_alpha) ? 4 : 3;
 
        if (fabs(rx2 - rx1) > fabs(ry2 - ry1))
                {
@@ -1014,9 +1248,9 @@ void pixbuf_draw_line(GdkPixbuf *pb,
  *-----------------------------------------------------------------------------
  */
 
-static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gint p_alpha,
-                                   gint s, gint vertical, gint border,
-                                   gint x1, gint y1, gint x2, gint y2,
+static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
+                                   gint s, gboolean vertical, gint border,
+                                   gint x1, gint y1, gint x2, gint y2,
                                    guint8 r, guint8 g, guint8 b, guint8 a)
 {
        guchar *pp;
@@ -1024,7 +1258,7 @@ static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gint p_alpha,
        guint8 n = a;
        gint i, j;
 
-       p_step = (p_alpha) ? 4 : 3;
+       p_step = (has_alpha) ? 4 : 3;
        for (j = y1; j < y2; j++)
                {
                pp = p_pix + j * prs + x1 * p_step;
@@ -1038,21 +1272,21 @@ static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gint p_alpha,
                        pp++;
                        *pp = (b * n + *pp * (256-n)) >> 8;
                        pp++;
-                       if (p_alpha) pp++;
+                       if (has_alpha) pp++;
                        }
                }
 }
 
-static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gint p_alpha,
+static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
                                    gint sx, gint sy, gint border,
-                                   gint x1, gint y1, gint x2, gint y2,
+                                   gint x1, gint y1, gint x2, gint y2,
                                    guint8 r, guint8 g, guint8 b, guint8 a)
 {
        guchar *pp;
        gint p_step;
        gint i, j;
 
-       p_step = (p_alpha) ? 4 : 3;
+       p_step = (has_alpha) ? 4 : 3;
        for (j = y1; j < y2; j++)
                {
                pp = p_pix + j * prs + x1 * p_step;
@@ -1069,7 +1303,7 @@ static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gint p_alpha,
                        pp++;
                        *pp = (b * n + *pp * (256-n)) >> 8;
                        pp++;
-                       if (p_alpha) pp++;
+                       if (has_alpha) pp++;
                        }
                }
 }
@@ -1079,7 +1313,7 @@ void pixbuf_draw_shadow(GdkPixbuf *pb,
                        gint x, gint y, gint w, gint h, gint border,
                        guint8 r, guint8 g, guint8 b, guint8 a)
 {
-       gint p_alpha;
+       gint has_alpha;
        gint pw, ph, prs;
        gint rx, ry, rw, rh;
        gint fx, fy, fw, fh;
@@ -1094,7 +1328,7 @@ void pixbuf_draw_shadow(GdkPixbuf *pb,
                              clip_x, clip_y, clip_w, clip_h,
                              &rx, &ry, &rw, &rh)) return;
 
-       p_alpha = gdk_pixbuf_get_has_alpha(pb);
+       has_alpha = gdk_pixbuf_get_has_alpha(pb);
        prs = gdk_pixbuf_get_rowstride(pb);
        p_pix = gdk_pixbuf_get_pixels(pb);
 
@@ -1111,7 +1345,7 @@ void pixbuf_draw_shadow(GdkPixbuf *pb,
                             rx, ry, rw, rh,
                             &fx, &fy, &fw, &fh))
                {
-               pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
+               pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
                                        x + border, TRUE, border,
                                        fx, fy, fx + fw, fy + fh,
                                        r, g, b, a);
@@ -1120,7 +1354,7 @@ void pixbuf_draw_shadow(GdkPixbuf *pb,
                             rx, ry, rw, rh,
                             &fx, &fy, &fw, &fh))
                {
-               pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
+               pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
                                        x + w - border, TRUE, border,
                                        fx, fy, fx + fw, fy + fh,
                                        r, g, b, a);
@@ -1129,7 +1363,7 @@ void pixbuf_draw_shadow(GdkPixbuf *pb,
                             rx, ry, rw, rh,
                             &fx, &fy, &fw, &fh))
                {
-               pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
+               pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
                                        y + border, FALSE, border,
                                        fx, fy, fx + fw, fy + fh,
                                        r, g, b, a);
@@ -1138,7 +1372,7 @@ void pixbuf_draw_shadow(GdkPixbuf *pb,
                             rx, ry, rw, rh,
                             &fx, &fy, &fw, &fh))
                {
-               pixbuf_draw_fade_linear(p_pix, prs, p_alpha,
+               pixbuf_draw_fade_linear(p_pix, prs, has_alpha,
                                        y + h - border, FALSE, border,
                                        fx, fy, fx + fw, fy + fh,
                                        r, g, b, a);
@@ -1147,7 +1381,7 @@ void pixbuf_draw_shadow(GdkPixbuf *pb,
                             rx, ry, rw, rh,
                             &fx, &fy, &fw, &fh))
                {
-               pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
+               pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
                                        x + border, y + border, border,
                                        fx, fy, fx + fw, fy + fh,
                                        r, g, b, a);
@@ -1156,7 +1390,7 @@ void pixbuf_draw_shadow(GdkPixbuf *pb,
                             rx, ry, rw, rh,
                             &fx, &fy, &fw, &fh))
                {
-               pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
+               pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
                                        x + w - border, y + border, border,
                                        fx, fy, fx + fw, fy + fh,
                                        r, g, b, a);
@@ -1165,7 +1399,7 @@ void pixbuf_draw_shadow(GdkPixbuf *pb,
                             rx, ry, rw, rh,
                             &fx, &fy, &fw, &fh))
                {
-               pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
+               pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
                                        x + border, y + h - border, border,
                                        fx, fy, fx + fw, fy + fh,
                                        r, g, b, a);
@@ -1174,7 +1408,7 @@ void pixbuf_draw_shadow(GdkPixbuf *pb,
                             rx, ry, rw, rh,
                             &fx, &fy, &fw, &fh))
                {
-               pixbuf_draw_fade_radius(p_pix, prs, p_alpha,
+               pixbuf_draw_fade_radius(p_pix, prs, has_alpha,
                                        x + w - border, y + h - border, border,
                                        fx, fy, fx + fw, fy + fh,
                                        r, g, b, a);
@@ -1191,7 +1425,7 @@ void pixbuf_draw_shadow(GdkPixbuf *pb,
 void pixbuf_desaturate_rect(GdkPixbuf *pb,
                            gint x, gint y, gint w, gint h)
 {
-       gint p_alpha;
+       gboolean has_alpha;
        gint pw, ph, prs;
        guchar *p_pix;
        guchar *pp;
@@ -1205,13 +1439,13 @@ void pixbuf_desaturate_rect(GdkPixbuf *pb,
        if (x < 0 || x + w > pw) return;
        if (y < 0 || y + h > ph) return;
 
-       p_alpha = gdk_pixbuf_get_has_alpha(pb);
+       has_alpha = gdk_pixbuf_get_has_alpha(pb);
        prs = gdk_pixbuf_get_rowstride(pb);
        p_pix = gdk_pixbuf_get_pixels(pb);
 
-        for (i = 0; i < h; i++)
+       for (i = 0; i < h; i++)
                {
-               pp = p_pix + (y + i) * prs + (x * (p_alpha ? 4 : 3));
+               pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
                for (j = 0; j < w; j++)
                        {
                        guint8 grey;
@@ -1223,9 +1457,95 @@ void pixbuf_desaturate_rect(GdkPixbuf *pb,
                        pp++;
                        *pp = grey;
                        pp++;
-                       if (p_alpha) pp++;
+                       if (has_alpha) pp++;
                        }
                }
 }
 
+/*
+ *-----------------------------------------------------------------------------
+ * pixbuf highlight under/over exposure *-----------------------------------------------------------------------------
+ */
+void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gint h)
+{
+       gboolean has_alpha;
+       gint pw, ph, prs;
+       guchar *p_pix;
+       guchar *pp;
+       gint i, j;
+
+       if (!pb) return;
+
+       pw = gdk_pixbuf_get_width(pb);
+       ph = gdk_pixbuf_get_height(pb);
+
+       if (x < 0 || x + w > pw) return;
+       if (y < 0 || y + h > ph) return;
+
+       has_alpha = gdk_pixbuf_get_has_alpha(pb);
+       prs = gdk_pixbuf_get_rowstride(pb);
+       p_pix = gdk_pixbuf_get_pixels(pb);
 
+       for (i = 0; i < h; i++)
+               {
+               pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
+               for (j = 0; j < w; j++)
+                       {
+                       if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
+                               {
+                               *pp = 255;
+                               pp++;
+                               *pp = 0;
+                               pp++;
+                               *pp = 0;
+                               pp++;
+                               if (has_alpha) pp++;
+                               }
+                       else
+                               {
+                               pp = pp + 3;
+                               if (has_alpha) pp++;
+                               }
+                       }
+               }
+}
+
+/*
+ *-----------------------------------------------------------------------------
+ * pixbuf ignore alpha
+ *-----------------------------------------------------------------------------
+*/
+void pixbuf_ignore_alpha_rect(GdkPixbuf *pb,
+                 gint x, gint y, gint w, gint h)
+{
+   gboolean has_alpha;
+   gint pw, ph, prs;
+   guchar *p_pix;
+   guchar *pp;
+   gint i, j;
+
+   if (!pb) return;
+
+   pw = gdk_pixbuf_get_width(pb);
+   ph = gdk_pixbuf_get_height(pb);
+
+   if (x < 0 || x + w > pw) return;
+   if (y < 0 || y + h > ph) return;
+
+   has_alpha = gdk_pixbuf_get_has_alpha(pb);
+   if (!has_alpha) return;
+
+   prs = gdk_pixbuf_get_rowstride(pb);
+   p_pix = gdk_pixbuf_get_pixels(pb);
+
+   for (i = 0; i < h; i++)
+       {
+       pp = p_pix + (y + i) * prs + (x * 4 );
+       for (j = 0; j < w; j++)
+           {
+           pp[3] = 0xff;
+           pp+=4;
+           }
+       }
+}
+/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */