Trim trailing white spaces.
[geeqie.git] / src / pixbuf_util.c
index 2f99d06..6d4ad5b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Geeqie
  * (C) 2004 John Ellis
- * Copyright (C) 2008 - 2009 The Geeqie Team
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Author: John Ellis
  *
@@ -14,6 +14,7 @@
 #include "main.h"
 #include "pixbuf_util.h"
 #include "exif.h"
+#include "ui_fileops.h"
 
 #include "icons/icons_inline.h"
 
@@ -29,7 +30,7 @@
 gboolean pixbuf_to_file_as_png(GdkPixbuf *pixbuf, const gchar *filename)
 {
        GError *error = NULL;
-       gint ret;
+       gboolean ret;
 
        if (!pixbuf || !filename) return FALSE;
 
@@ -166,24 +167,50 @@ gboolean register_theme_icon_as_stock(const gchar *key, const gchar *icon)
        GError *error = NULL;
 
        icon_theme = gtk_icon_theme_get_default();
-       pixbuf = gtk_icon_theme_load_icon (icon_theme,
+       
+       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 (!pixbuf)
                {
-               DEBUG_1("Couldn't load icon: %s", error->message);
-               g_error_free(error);
-               return FALSE;
+               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);
+                               }
+                       g_free(icon2);
+                       }
                }
+
+       if (!pixbuf) return FALSE;
        
        register_stock_icon(key, pixbuf);
        return TRUE;
 }
 
-gint pixbuf_scale_aspect(gint req_w, gint req_h, gint old_w, gint old_h,
-                                         gint *new_w, gint *new_h)
+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))
                {
@@ -235,9 +262,9 @@ GdkPixbuf *pixbuf_fallback(FileData *fd, gint requested_width, gint requested_he
  *-----------------------------------------------------------------------------
  */
 
-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 ||
@@ -264,7 +291,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;
@@ -314,18 +341,14 @@ 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
        gint i, j;
        gint a;
        GdkPixbuf *buffer;
@@ -416,10 +439,10 @@ 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;
@@ -544,7 +567,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;
@@ -558,13 +581,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++)
                {
-               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;
@@ -573,7 +596,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++;
                        }
                }
 }
@@ -597,7 +620,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;
@@ -611,19 +634,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++)
                {
-               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++; }
                        }
                }
 }
@@ -646,7 +669,7 @@ 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;
 
@@ -677,11 +700,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;
 
@@ -753,32 +776,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);
        if (w < 1 || h < 1) return;
 
-       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);
+       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;
@@ -807,6 +833,7 @@ void pixbuf_draw_layout(GdkPixbuf *pixbuf, PangoLayout *layout, GtkWidget *widge
                         r, g, b, a);
 
        g_object_unref(buffer);
+       cairo_surface_destroy(source);
 }
 
 /*
@@ -840,7 +867,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;
@@ -854,7 +881,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;
 
@@ -874,11 +901,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)
                {
@@ -938,7 +965,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++;
                        }
@@ -951,11 +978,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)
@@ -978,20 +1005,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)
                {
@@ -1066,7 +1079,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;
@@ -1095,11 +1108,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))
                {
@@ -1163,8 +1176,8 @@ 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,
+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)
 {
@@ -1173,7 +1186,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;
@@ -1187,12 +1200,12 @@ 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,
                                    guint8 r, guint8 g, guint8 b, guint8 a)
@@ -1201,7 +1214,7 @@ static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gint p_alpha,
        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;
@@ -1218,7 +1231,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++;
                        }
                }
 }
@@ -1228,7 +1241,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;
@@ -1243,7 +1256,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);
 
@@ -1260,7 +1273,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);
@@ -1269,7 +1282,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);
@@ -1278,7 +1291,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);
@@ -1287,7 +1300,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);
@@ -1296,7 +1309,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);
@@ -1305,7 +1318,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);
@@ -1314,7 +1327,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);
@@ -1323,7 +1336,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);
@@ -1340,7 +1353,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;
@@ -1354,13 +1367,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++)
                {
-               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;
@@ -1372,7 +1385,7 @@ void pixbuf_desaturate_rect(GdkPixbuf *pb,
                        pp++;
                        *pp = grey;
                        pp++;
-                       if (p_alpha) pp++;
+                       if (has_alpha) pp++;
                        }
                }
 }