]> Pileus Git - ~andy/gtk/blobdiff - modules/engines/pixbuf/pixbuf-render.c
pixbuf-engine: Get rid of unused mask argument
[~andy/gtk] / modules / engines / pixbuf / pixbuf-render.c
index 8d4f41fc5b02ad6f82e76db66b263d349f20631d..ce25cfbb78cc8a7de31c953fc3154582093ad610 100644 (file)
 #include "pixbuf.h"
 #include <gdk-pixbuf/gdk-pixbuf.h>
 
-GCache *pixbuf_cache = NULL;
+static GCache *pixbuf_cache = NULL;
+
+static GdkPixbuf *
+bilinear_gradient (GdkPixbuf    *src,
+                  gint          src_x,
+                  gint          src_y,
+                  gint          width,
+                  gint          height)
+{
+  guint n_channels = gdk_pixbuf_get_n_channels (src);
+  guint src_rowstride = gdk_pixbuf_get_rowstride (src);
+  guchar *src_pixels = gdk_pixbuf_get_pixels (src);
+  guchar *p1, *p2, *p3, *p4;
+  guint dest_rowstride;
+  guchar *dest_pixels;
+  GdkPixbuf *result;
+  int i, j, k;
+
+  if (src_x == 0 || src_y == 0)
+    {
+      g_warning ("invalid source position for bilinear gradient");
+      return NULL;
+    }
+
+  p1 = src_pixels + (src_y - 1) * src_rowstride + (src_x - 1) * n_channels;
+  p2 = p1 + n_channels;
+  p3 = src_pixels + src_y * src_rowstride + (src_x - 1) * n_channels;
+  p4 = p3 + n_channels;
+
+  result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8,
+                          width, height);
+
+  if (result == NULL)
+    {
+      g_warning ("failed to create a %dx%d pixbuf", width, height);
+      return NULL;
+    }
+
+  dest_rowstride = gdk_pixbuf_get_rowstride (result);
+  dest_pixels = gdk_pixbuf_get_pixels (result);
+
+  for (i = 0; i < height; i++)
+    {
+      guchar *p = dest_pixels + dest_rowstride *i;
+      guint v[4];
+      gint dv[4];
+
+      for (k = 0; k < n_channels; k++)
+       {
+         guint start = ((height - i) * p1[k] + (1 + i) * p3[k]) / (height + 1);
+         guint end = ((height -  i) * p2[k] + (1 + i) * p4[k]) / (height + 1);
+
+         dv[k] = (((gint)end - (gint)start) << 16) / (width + 1);
+         v[k] = (start << 16) + dv[k] + 0x8000;
+       }
+
+      for (j = width; j; j--)
+       {
+         for (k = 0; k < n_channels; k++)
+           {
+             *(p++) = v[k] >> 16;
+             v[k] += dv[k];
+           }
+       }
+    }
+
+  return result;
+}
+
+static GdkPixbuf *
+horizontal_gradient (GdkPixbuf    *src,
+                    gint          src_x,
+                    gint          src_y,
+                    gint          width,
+                    gint          height)
+{
+  guint n_channels = gdk_pixbuf_get_n_channels (src);
+  guint src_rowstride = gdk_pixbuf_get_rowstride (src);
+  guchar *src_pixels = gdk_pixbuf_get_pixels (src);
+  guint dest_rowstride;
+  guchar *dest_pixels;
+  GdkPixbuf *result;
+  int i, j, k;
+
+  if (src_x == 0)
+    {
+      g_warning ("invalid source position for horizontal gradient");
+      return NULL;
+    }
+
+  result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8,
+                          width, height);
+
+  if (result == NULL)
+    {
+      g_warning ("failed to create a %dx%d pixbuf", width, height);
+      return NULL;
+    }
+
+  dest_rowstride = gdk_pixbuf_get_rowstride (result);
+  dest_pixels = gdk_pixbuf_get_pixels (result);
+
+  for (i = 0; i < height; i++)
+    {
+      guchar *p = dest_pixels + dest_rowstride *i;
+      guchar *p1 = src_pixels + (src_y + i) * src_rowstride + (src_x - 1) * n_channels;
+      guchar *p2 = p1 + n_channels;
+
+      guint v[4];
+      gint dv[4];
+
+      for (k = 0; k < n_channels; k++)
+       {
+         dv[k] = (((gint)p2[k] - (gint)p1[k]) << 16) / (width + 1);
+         v[k] = (p1[k] << 16) + dv[k] + 0x8000;
+       }
+      
+      for (j = width; j; j--)
+       {
+         for (k = 0; k < n_channels; k++)
+           {
+             *(p++) = v[k] >> 16;
+             v[k] += dv[k];
+           }
+       }
+    }
+
+  return result;
+}
+
+static GdkPixbuf *
+vertical_gradient (GdkPixbuf    *src,
+                  gint          src_x,
+                  gint          src_y,
+                  gint          width,
+                  gint          height)
+{
+  guint n_channels = gdk_pixbuf_get_n_channels (src);
+  guint src_rowstride = gdk_pixbuf_get_rowstride (src);
+  guchar *src_pixels = gdk_pixbuf_get_pixels (src);
+  guchar *top_pixels, *bottom_pixels;
+  guint dest_rowstride;
+  guchar *dest_pixels;
+  GdkPixbuf *result;
+  int i, j;
+
+  if (src_y == 0)
+    {
+      g_warning ("invalid source position for vertical gradient");
+      return NULL;
+    }
+
+  top_pixels = src_pixels + (src_y - 1) * src_rowstride + (src_x) * n_channels;
+  bottom_pixels = top_pixels + src_rowstride;
+
+  result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8,
+                          width, height);
+
+  if (result == NULL)
+    {
+      g_warning ("failed to create a %dx%d pixbuf", width, height);
+      return NULL;
+    }
+
+  dest_rowstride = gdk_pixbuf_get_rowstride (result);
+  dest_pixels = gdk_pixbuf_get_pixels (result);
+
+  for (i = 0; i < height; i++)
+    {
+      guchar *p = dest_pixels + dest_rowstride *i;
+      guchar *p1 = top_pixels;
+      guchar *p2 = bottom_pixels;
+
+      for (j = width * n_channels; j; j--)
+       *(p++) = ((height - i) * *(p1++) + (1 + i) * *(p2++)) / (height + 1);
+    }
+
+  return result;
+}
 
 static GdkPixbuf *
 replicate_single (GdkPixbuf    *src,
@@ -52,6 +230,13 @@ replicate_single (GdkPixbuf    *src,
 
   result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8,
                           width, height);
+
+  if (result == NULL)
+    {
+      g_warning ("failed to create a %dx%d pixbuf", width, height);
+      return NULL;
+    }
+
   dest_rowstride = gdk_pixbuf_get_rowstride (result);
   dest_pixels = gdk_pixbuf_get_pixels (result);
   
@@ -90,6 +275,13 @@ replicate_rows (GdkPixbuf    *src,
 
   result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8,
                           width, height);
+
+  if (result == NULL)
+    {
+      g_warning ("failed to create a %dx%d pixbuf", width, height);
+      return NULL;
+    }
+
   dest_rowstride = gdk_pixbuf_get_rowstride (result);
   dest_pixels = gdk_pixbuf_get_pixels (result);
 
@@ -116,6 +308,13 @@ replicate_cols (GdkPixbuf    *src,
 
   result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, n_channels == 4, 8,
                           width, height);
+
+  if (result == NULL)
+    {
+      g_warning ("failed to create a %dx%d pixbuf", width, height);
+      return NULL;
+    }
+
   dest_rowstride = gdk_pixbuf_get_rowstride (result);
   dest_pixels = gdk_pixbuf_get_pixels (result);
 
@@ -154,7 +353,6 @@ static void
 pixbuf_render (GdkPixbuf    *src,
               guint         hints,
               GdkWindow    *window,
-              GdkBitmap    *mask,
               GdkRectangle *clip_rect,
               gint          src_x,
               gint          src_y,
@@ -165,7 +363,7 @@ pixbuf_render (GdkPixbuf    *src,
               gint          dest_width,
               gint          dest_height)
 {
-  GdkPixbuf *tmp_pixbuf;
+  GdkPixbuf *tmp_pixbuf = NULL;
   GdkRectangle rect;
   int x_offset, y_offset;
   gboolean has_alpha = gdk_pixbuf_get_has_alpha (src);
@@ -183,12 +381,7 @@ pixbuf_render (GdkPixbuf    *src,
   if (hints & THEME_MISSING)
     return;
 
-  /* FIXME: Because we use the mask to shape windows, we don't use
-   * clip_rect to clip what we draw to the mask, only to clip
-   * what we actually draw. But this leads to the horrible ineffiency
-   * of scale the whole image to get a little bit of it.
-   */
-  if (!mask && clip_rect)
+  if (clip_rect)
     {
       if (!gdk_rectangle_intersect (clip_rect, &rect, &rect))
        return;
@@ -201,6 +394,27 @@ pixbuf_render (GdkPixbuf    *src,
       x_offset = src_x + rect.x - dest_x;
       y_offset = src_y + rect.y - dest_y;
     }
+  else if (src_width == 0 && src_height == 0)
+    {
+      tmp_pixbuf = bilinear_gradient (src, src_x, src_y, dest_width, dest_height);      
+      
+      x_offset = rect.x - dest_x;
+      y_offset = rect.y - dest_y;
+    }
+  else if (src_width == 0 && dest_height == src_height)
+    {
+      tmp_pixbuf = horizontal_gradient (src, src_x, src_y, dest_width, dest_height);      
+      
+      x_offset = rect.x - dest_x;
+      y_offset = rect.y - dest_y;
+    }
+  else if (src_height == 0 && dest_width == src_width)
+    {
+      tmp_pixbuf = vertical_gradient (src, src_x, src_y, dest_width, dest_height);
+      
+      x_offset = rect.x - dest_x;
+      y_offset = rect.y - dest_y;
+    }
   else if ((hints & THEME_CONSTANT_COLS) && (hints & THEME_CONSTANT_ROWS))
     {
       tmp_pixbuf = replicate_single (src, src_x, src_y, dest_width, dest_height);
@@ -222,7 +436,7 @@ pixbuf_render (GdkPixbuf    *src,
       x_offset = rect.x - dest_x;
       y_offset = rect.y - dest_y;
     }
-  else 
+  else if (src_width > 0 && src_height > 0)
     {
       double x_scale = (double)dest_width / src_width;
       double y_scale = (double)dest_height / src_height;
@@ -249,29 +463,27 @@ pixbuf_render (GdkPixbuf    *src,
                        x_scale, y_scale,
                        GDK_INTERP_BILINEAR);
 
-      gdk_pixbuf_unref (partial_src);
+      g_object_unref (partial_src);
 
       x_offset = 0;
       y_offset = 0;
     }
 
-  if (mask)
+  if (tmp_pixbuf)
     {
-      gdk_pixbuf_render_threshold_alpha (tmp_pixbuf, mask,
-                                        x_offset, y_offset,
-                                        rect.x, rect.y,
-                                        rect.width, rect.height,
-                                        128);
+      cairo_t *cr;
+      
+      cr = gdk_cairo_create (window);
+      gdk_cairo_set_source_pixbuf (cr, 
+                                   tmp_pixbuf,
+                                   -x_offset + rect.x, 
+                                   -y_offset + rect.y);
+      gdk_cairo_rectangle (cr, &rect);
+      cairo_fill (cr);
+
+      cairo_destroy (cr);
+      g_object_unref (tmp_pixbuf);
     }
-
-  gdk_pixbuf_render_to_drawable_alpha (tmp_pixbuf, window,
-                                      x_offset, y_offset,
-                                      rect.x, rect.y,
-                                      rect.width, rect.height,
-                                      GDK_PIXBUF_ALPHA_FULL, 128,
-                                      GDK_RGB_DITHER_NORMAL,
-                                      0, 0);
-  gdk_pixbuf_unref (tmp_pixbuf);
 }
 
 ThemePixbuf *
@@ -307,8 +519,7 @@ theme_pixbuf_set_filename (ThemePixbuf *theme_pb,
       theme_pb->pixbuf = NULL;
     }
 
-  if (theme_pb->filename)
-    g_free (theme_pb->filename);
+  g_free (theme_pb->filename);
 
   if (filename)
     theme_pb->filename = g_strdup (filename);
@@ -336,13 +547,13 @@ compute_hint (GdkPixbuf *pixbuf,
   for (i = y0; i < y1; i++)
     {
       guchar *p = data + i * rowstride + x0 * n_channels;
-      guchar r = *(p++);
-      guchar g = *(p++);
-      guchar b = *(p++);
+      guchar r = p[0];
+      guchar g = p[1];
+      guchar b = p[2];
       guchar a = 0;
       
       if (n_channels == 4)
-       a = *(p++);
+       a = p[3];
 
       for (j = x0; j < x1 ; j++)
        {
@@ -388,17 +599,22 @@ theme_pixbuf_compute_hints (ThemePixbuf *theme_pb)
   gint width = gdk_pixbuf_get_width (theme_pb->pixbuf);
   gint height = gdk_pixbuf_get_height (theme_pb->pixbuf);
 
-  if (theme_pb->border_left + theme_pb->border_right >= width ||
-      theme_pb->border_top + theme_pb->border_bottom >= height)
+  if (theme_pb->border_left + theme_pb->border_right > width ||
+      theme_pb->border_top + theme_pb->border_bottom > height)
     {
       g_warning ("Invalid borders specified for theme pixmap:\n"
                 "        %s,\n"
-                "there must be at least one pixel not in the border both horizontally\n"
-                "and vertically", theme_pb->filename);
-      if (theme_pb->border_left + theme_pb->border_right >= width)
-       theme_pb->border_left = theme_pb->border_right = width / 2 + 1 - (width % 2);
-      if (theme_pb->border_bottom + theme_pb->border_top >= height)
-       theme_pb->border_bottom = theme_pb->border_top = height / 2 + 1 - (height % 2);
+                "borders don't fit within the image", theme_pb->filename);
+      if (theme_pb->border_left + theme_pb->border_right > width)
+       {
+         theme_pb->border_left = width / 2;
+         theme_pb->border_right = (width + 1) / 2;
+       }
+      if (theme_pb->border_bottom + theme_pb->border_top > height)
+       {
+         theme_pb->border_top = height / 2;
+         theme_pb->border_bottom = (height + 1) / 2;
+       }
     }
   
   for (i = 0; i < 3; i++)
@@ -473,7 +689,7 @@ theme_pixbuf_set_stretch (ThemePixbuf *theme_pb,
     theme_pixbuf_compute_hints (theme_pb);
 }
 
-GdkPixbuf *
+static GdkPixbuf *
 pixbuf_cache_value_new (gchar *filename)
 {
   GError *err = NULL;
@@ -496,7 +712,7 @@ theme_pixbuf_get_pixbuf (ThemePixbuf *theme_pb)
     {
       if (!pixbuf_cache)
        pixbuf_cache = g_cache_new ((GCacheNewFunc)pixbuf_cache_value_new,
-                                   (GCacheDestroyFunc)gdk_pixbuf_unref,
+                                   (GCacheDestroyFunc)g_object_unref,
                                    (GCacheDupFunc)g_strdup,
                                    (GCacheDestroyFunc)g_free,
                                    g_str_hash, g_direct_hash, g_str_equal);
@@ -513,7 +729,6 @@ theme_pixbuf_get_pixbuf (ThemePixbuf *theme_pb)
 void
 theme_pixbuf_render (ThemePixbuf  *theme_pb,
                     GdkWindow    *window,
-                    GdkBitmap    *mask,
                     GdkRectangle *clip_rect,
                     guint         component_mask,
                     gboolean      center,
@@ -532,6 +747,9 @@ theme_pixbuf_render (ThemePixbuf  *theme_pb,
 
   if (theme_pb->stretch)
     {
+      if (component_mask & COMPONENT_ALL)
+       component_mask = (COMPONENT_ALL - 1) & ~component_mask;
+
       src_x[0] = 0;
       src_x[1] = theme_pb->border_left;
       src_x[2] = pixbuf_width - theme_pb->border_right;
@@ -547,19 +765,30 @@ theme_pixbuf_render (ThemePixbuf  *theme_pb,
       dest_x[2] = x + width - theme_pb->border_right;
       dest_x[3] = x + width;
 
+      if (dest_x[1] > dest_x[2])
+       {
+         component_mask &= ~(COMPONENT_NORTH | COMPONENT_SOUTH | COMPONENT_CENTER);
+         dest_x[1] = dest_x[2] = (dest_x[1] + dest_x[2]) / 2;
+       }
+
       dest_y[0] = y;
       dest_y[1] = y + theme_pb->border_top;
       dest_y[2] = y + height - theme_pb->border_bottom;
       dest_y[3] = y + height;
 
-      if (component_mask & COMPONENT_ALL)
-       component_mask = (COMPONENT_ALL - 1) & ~component_mask;
+      if (dest_y[1] > dest_y[2])
+       {
+         component_mask &= ~(COMPONENT_EAST | COMPONENT_WEST | COMPONENT_CENTER);
+         dest_y[1] = dest_y[2] = (dest_y[1] + dest_y[2]) / 2;
+       }
+
 
-#define RENDER_COMPONENT(X1,X2,Y1,Y2)                                           \
-        pixbuf_render (pixbuf, theme_pb->hints[Y1][X1], window, mask, clip_rect, \
-                      src_x[X1], src_y[Y1],                                     \
-                      src_x[X2] - src_x[X1], src_y[Y2] - src_y[Y1],             \
-                      dest_x[X1], dest_y[Y1],                                   \
+
+#define RENDER_COMPONENT(X1,X2,Y1,Y2)                                     \
+        pixbuf_render (pixbuf, theme_pb->hints[Y1][X1], window, clip_rect, \
+                      src_x[X1], src_y[Y1],                               \
+                      src_x[X2] - src_x[X1], src_y[Y2] - src_y[Y1],       \
+                      dest_x[X1], dest_y[Y1],                             \
                       dest_x[X2] - dest_x[X1], dest_y[Y2] - dest_y[Y1]);
       
       if (component_mask & COMPONENT_NORTH_WEST)
@@ -596,7 +825,7 @@ theme_pixbuf_render (ThemePixbuf  *theme_pb,
          x += (width - pixbuf_width) / 2;
          y += (height - pixbuf_height) / 2;
          
-         pixbuf_render (pixbuf, 0, window, NULL, clip_rect,
+         pixbuf_render (pixbuf, 0, window, clip_rect,
                         0, 0,
                         pixbuf_width, pixbuf_height,
                         x, y,
@@ -604,35 +833,19 @@ theme_pixbuf_render (ThemePixbuf  *theme_pb,
        }
       else
        {
-         GdkPixmap *tmp_pixmap;
-         GdkGC *tmp_gc;
-         GdkGCValues gc_values;
-
-         tmp_pixmap = gdk_pixmap_new (window,
-                                      pixbuf_width,
-                                      pixbuf_height,
-                                      -1);
-         tmp_gc = gdk_gc_new (tmp_pixmap);
-         gdk_pixbuf_render_to_drawable (pixbuf, tmp_pixmap, tmp_gc,
-                                        0, 0, 
-                                        0, 0,
-                                        pixbuf_width, pixbuf_height,
-                                        GDK_RGB_DITHER_NORMAL,
-                                        0, 0);
-         gdk_gc_unref (tmp_gc);
-
-         gc_values.fill = GDK_TILED;
-         gc_values.tile = tmp_pixmap;
-         tmp_gc = gdk_gc_new_with_values (window,
-                                          &gc_values, GDK_GC_FILL | GDK_GC_TILE);
+          cairo_t *cr = gdk_cairo_create (window);
+
+          gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
+          cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
+
          if (clip_rect)
-           gdk_draw_rectangle (window, tmp_gc, TRUE,
-                               clip_rect->x, clip_rect->y, clip_rect->width, clip_rect->height);
+           gdk_cairo_rectangle (cr, clip_rect);
          else
-           gdk_draw_rectangle (window, tmp_gc, TRUE, x, y, width, height);
+           cairo_rectangle (cr, x, y, width, height);
          
-         gdk_gc_unref (tmp_gc);
-         gdk_pixmap_unref (tmp_pixmap);
+          cairo_fill (cr);
+
+          cairo_destroy (cr);
        }
     }
 }