]> Pileus Git - ~andy/gtk/blobdiff - gdk/gdkpixbuf-render.c
add missing #ifndef GDK_DISABLE_DEPRECATED.
[~andy/gtk] / gdk / gdkpixbuf-render.c
index 545f91123b593a922eafbc4ec779d5f76f286284..a702e2e7ca8d889f10f418a016738017be258b04 100644 (file)
@@ -5,25 +5,28 @@
  * Author: Federico Mena-Quintero <federico@gimp.org>
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library 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
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
 
-#include <config.h>
+#include "config.h"
 #include <gdk/gdk.h>
 #include "gdk-pixbuf-private.h"
 #include "gdkpixbuf.h"
+#include "gdkscreen.h"
+#include "gdkinternals.h"
+#include "gdkalias.h"
 
 \f
 
@@ -35,8 +38,8 @@
  * @src_y: source Y coordinate.
  * @dest_x: Destination X coordinate.
  * @dest_y: Destination Y coordinate.
- * @width: Width of region to threshold.
- * @height: Height of region to threshold.
+ * @width: Width of region to threshold, or -1 to use pixbuf width
+ * @height: Height of region to threshold, or -1 to use pixbuf height
  * @alpha_threshold: Opacity values below this will be painted as zero; all
  * other values will be painted as one.
  *
@@ -60,11 +63,16 @@ gdk_pixbuf_render_threshold_alpha (GdkPixbuf *pixbuf,
   int start, start_status;
   int status;
 
-  g_return_if_fail (pixbuf != NULL);
+  g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
   g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
   g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
   g_return_if_fail (pixbuf->bits_per_sample == 8);
 
+  if (width == -1) 
+    width = pixbuf->width;
+  if (height == -1)
+    height = pixbuf->height;
+
   g_return_if_fail (bitmap != NULL);
   g_return_if_fail (width >= 0 && height >= 0);
   g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
@@ -75,14 +83,13 @@ gdk_pixbuf_render_threshold_alpha (GdkPixbuf *pixbuf,
   if (width == 0 || height == 0)
     return;
 
-  gc = gdk_gc_new (bitmap);
+  gc = _gdk_drawable_get_scratch_gc (bitmap, FALSE);
 
   if (!pixbuf->has_alpha)
     {
       color.pixel = (alpha_threshold == 255) ? 0 : 1;
       gdk_gc_set_foreground (gc, &color);
       gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
-      gdk_gc_unref (gc);
       return;
     }
 
@@ -124,50 +131,10 @@ gdk_pixbuf_render_threshold_alpha (GdkPixbuf *pixbuf,
                       start + dest_x, y + dest_y,
                       x - 1 + dest_x, y + dest_y);
     }
-       
-  gdk_gc_unref (gc);
 }
 
 \f
 
-/* Creates a buffer by stripping the alpha channel of a pixbuf */
-static guchar *
-remove_alpha (GdkPixbuf *pixbuf,
-             int x,     int y,
-             int width, int height,
-             int *rowstride)
-{
-  guchar *buf;
-  int xx, yy;
-  guchar *src, *dest;
-
-  g_assert (pixbuf->n_channels == 4);
-  g_assert (pixbuf->has_alpha);
-  g_assert (width > 0 && height > 0);
-  g_assert (x >= 0 && x + width <= pixbuf->width);
-  g_assert (y >= 0 && y + height <= pixbuf->height);
-
-  *rowstride = 4 * ((width * 3 + 3) / 4);
-
-  buf = g_new (guchar, *rowstride * height);
-
-  for (yy = 0; yy < height; yy++)
-    {
-      src = pixbuf->pixels + pixbuf->rowstride * (yy + y) + x * pixbuf->n_channels;
-      dest = buf + *rowstride * yy;
-      
-      for (xx = 0; xx < width; xx++)
-       {
-         *dest++ = *src++;
-         *dest++ = *src++;
-         *dest++ = *src++;
-         src++;
-       }
-    }
-  
-  return buf;
-}
-
 /**
  * gdk_pixbuf_render_to_drawable:
  * @pixbuf: A pixbuf.
@@ -177,8 +144,8 @@ remove_alpha (GdkPixbuf *pixbuf,
  * @src_y: Source Y coordinate within pixbuf.
  * @dest_x: Destination X coordinate within drawable.
  * @dest_y: Destination Y coordinate within drawable.
- * @width: Width of region to render, in pixels.
- * @height: Height of region to render, in pixels.
+ * @width: Width of region to render, in pixels, or -1 to use pixbuf width
+ * @height: Height of region to render, in pixels, or -1 to use pixbuf height
  * @dither: Dithering mode for GdkRGB.
  * @x_dither: X offset for dither.
  * @y_dither: Y offset for dither.
@@ -195,6 +162,8 @@ remove_alpha (GdkPixbuf *pixbuf,
  * base position change, as in scrolling.  The dither matrix has to be shifted
  * for consistent visual results.  If you do not have any of these cases, the
  * dither offsets can be both zero.
+ *
+ * Deprecated: 2.4: This function is obsolete. Use gdk_draw_pixbuf() instead.
  **/
 void
 gdk_pixbuf_render_to_drawable (GdkPixbuf   *pixbuf,
@@ -206,46 +175,9 @@ gdk_pixbuf_render_to_drawable (GdkPixbuf   *pixbuf,
                               GdkRgbDither dither,
                               int x_dither, int y_dither)
 {
-  guchar *buf;
-  int rowstride;
-
-  g_return_if_fail (pixbuf != NULL);
-  g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
-  g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
-  g_return_if_fail (pixbuf->bits_per_sample == 8);
-
-  g_return_if_fail (drawable != NULL);
-  g_return_if_fail (gc != NULL);
-
-  g_return_if_fail (width >= 0 && height >= 0);
-  g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
-  g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
-
-  if (width == 0 || height == 0)
-    return;
-
-  /* This will have to be modified once we support other image types.
-   * Also, GdkRGB does not have gdk_draw_rgb_32_image_dithalign(), so we
-   * have to pack the buffer first.  Sigh.
-   */
-
-  if (pixbuf->has_alpha)
-    buf = remove_alpha (pixbuf, src_x, src_y, width, height, &rowstride);
-  else
-    {
-      buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
-      rowstride = pixbuf->rowstride;
-    }
-
-  gdk_draw_rgb_image_dithalign (drawable, gc,
-                               dest_x, dest_y,
-                               width, height,
-                               dither,
-                               buf, rowstride,
-                               x_dither, y_dither);
-
-  if (pixbuf->has_alpha)
-    g_free (buf);
+  gdk_draw_pixbuf (drawable, gc, pixbuf,
+                  src_x, src_y, dest_x, dest_y, width, height,
+                  dither, x_dither, y_dither);
 }
 
 \f
@@ -258,27 +190,23 @@ gdk_pixbuf_render_to_drawable (GdkPixbuf   *pixbuf,
  * @src_y: Source Y coordinates within pixbuf.
  * @dest_x: Destination X coordinate within drawable.
  * @dest_y: Destination Y coordinate within drawable.
- * @width: Width of region to render, in pixels.
- * @height: Height of region to render, in pixels.
- * @alpha_mode: If the image does not have opacity information, this is ignored.
- * Otherwise, specifies how to handle transparency when rendering.
- * @alpha_threshold: If the image does have opacity information and @alpha_mode
- * is GDK_PIXBUF_ALPHA_BILEVEL, specifies the threshold value for opacity
- * values.
+ * @width: Width of region to render, in pixels, or -1 to use pixbuf width.
+ * @height: Height of region to render, in pixels, or -1 to use pixbuf height.
+ * @alpha_mode: Ignored. Present for backwards compatibility.
+ * @alpha_threshold: Ignored. Present for backwards compatibility
  * @dither: Dithering mode for GdkRGB.
  * @x_dither: X offset for dither.
  * @y_dither: Y offset for dither.
  *
- * Renders a rectangular portion of a pixbuf to a drawable.  This is done using
- * GdkRGB, so the specified drawable must have the GdkRGB visual and colormap.
+ * Renders a rectangular portion of a pixbuf to a drawable.  The destination
+ * drawable must have a colormap. All windows have a colormap, however, pixmaps
+ * only have colormap by default if they were created with a non-%NULL window argument.
+ * Otherwise a colormap must be set on them with gdk_drawable_set_colormap.
+ *
+ * On older X servers, rendering pixbufs with an alpha channel involves round trips
+ * to the X server, and may be somewhat slow.
  *
- * When used with #GDK_PIXBUF_ALPHA_BILEVEL, this function has to create a bitmap
- * out of the thresholded alpha channel of the image and, it has to set this
- * bitmap as the clipping mask for the GC used for drawing.  This can be a
- * significant performance penalty depending on the size and the complexity of
- * the alpha channel of the image.  If performance is crucial, consider handling
- * the alpha channel yourself (possibly by caching it in your application) and
- * using gdk_pixbuf_render_to_drawable() or GdkRGB directly instead.
+ * Deprecated: 2.4: This function is obsolete. Use gdk_draw_pixbuf() instead.
  **/
 void
 gdk_pixbuf_render_to_drawable_alpha (GdkPixbuf   *pixbuf,
@@ -291,70 +219,33 @@ gdk_pixbuf_render_to_drawable_alpha (GdkPixbuf   *pixbuf,
                                     GdkRgbDither       dither,
                                     int x_dither, int y_dither)
 {
-  GdkBitmap *bitmap = NULL;
-  GdkGC *gc;
-
-  g_return_if_fail (pixbuf != NULL);
-  g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
-  g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
-  g_return_if_fail (pixbuf->bits_per_sample == 8);
-
-  g_return_if_fail (drawable != NULL);
-  g_return_if_fail (width >= 0 && height >= 0);
-  g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
-  g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
-
-  if (width == 0 || height == 0)
-    return;
-
-  gc = gdk_gc_new (drawable);
-
-  if (pixbuf->has_alpha)
-    {
-      /* Right now we only support GDK_PIXBUF_ALPHA_BILEVEL, so we
-       * unconditionally create the clipping mask.
-       */
-
-      bitmap = gdk_pixmap_new (NULL, width, height, 1);
-      gdk_pixbuf_render_threshold_alpha (pixbuf, bitmap,
-                                        src_x, src_y,
-                                        0, 0,
-                                        width, height,
-                                        alpha_threshold);
-
-      gdk_gc_set_clip_mask (gc, bitmap);
-      gdk_gc_set_clip_origin (gc, dest_x, dest_y);
-    }
-
-  gdk_pixbuf_render_to_drawable (pixbuf, drawable, gc,
-                                src_x, src_y,
-                                dest_x, dest_y,
-                                width, height,
-                                dither,
-                                x_dither, y_dither);
-
-  if (bitmap)
-    gdk_bitmap_unref (bitmap);
-
-  gdk_gc_unref (gc);
+  gdk_draw_pixbuf (drawable, NULL, pixbuf,
+                  src_x, src_y, dest_x, dest_y, width, height,
+                  dither, x_dither, y_dither);
 }
 
 /**
  * gdk_pixbuf_render_pixmap_and_mask:
  * @pixbuf: A pixbuf.
- * @pixmap_return: Return value for the created pixmap.
- * @mask_return: Return value for the created mask.
+ * @pixmap_return: Location to store a pointer to the created pixmap,
+ *   or %NULL if the pixmap is not needed.
+ * @mask_return: Location to store a pointer to the created mask,
+ *   or %NULL if the mask is not needed.
  * @alpha_threshold: Threshold value for opacity values.
  *
  * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
  * and @mask_return arguments, respectively, and renders a pixbuf and its
- * corresponding tresholded alpha mask to them.  This is merely a convenience
+ * corresponding thresholded alpha mask to them.  This is merely a convenience
  * function; applications that need to render pixbufs with dither offsets or to
- * given drawables should use gdk_pixbuf_render_to_drawable_alpha() or
- * gdk_pixbuf_render_to_drawable(), and gdk_pixbuf_render_threshold_alpha().
+ * given drawables should use gdk_draw_pixbuf() and gdk_pixbuf_render_threshold_alpha().
+ *
+ * The pixmap that is created is created for the colormap returned
+ * by gdk_rgb_get_colormap(). You normally will want to instead use
+ * the actual colormap for a widget, and use
+ * gdk_pixbuf_render_pixmap_and_mask_for_colormap().
  *
  * If the pixbuf does not have an alpha channel, then *@mask_return will be set
- * to NULL.
+ * to %NULL.
  **/
 void
 gdk_pixbuf_render_pixmap_and_mask (GdkPixbuf  *pixbuf,
@@ -362,34 +253,95 @@ gdk_pixbuf_render_pixmap_and_mask (GdkPixbuf  *pixbuf,
                                   GdkBitmap **mask_return,
                                   int         alpha_threshold)
 {
-  g_return_if_fail (pixbuf != NULL);
+  gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf,
+                                                 gdk_rgb_get_colormap (),
+                                                 pixmap_return, mask_return,
+                                                 alpha_threshold);
+}
+
+/**
+ * gdk_pixbuf_render_pixmap_and_mask_for_colormap:
+ * @pixbuf: A pixbuf.
+ * @colormap: A #GdkColormap
+ * @pixmap_return: Location to store a pointer to the created pixmap,
+ *   or %NULL if the pixmap is not needed.
+ * @mask_return: Location to store a pointer to the created mask,
+ *   or %NULL if the mask is not needed.
+ * @alpha_threshold: Threshold value for opacity values.
+ *
+ * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
+ * and @mask_return arguments, respectively, and renders a pixbuf and its
+ * corresponding tresholded alpha mask to them.  This is merely a convenience
+ * function; applications that need to render pixbufs with dither offsets or to
+ * given drawables should use gdk_draw_pixbuf(), and gdk_pixbuf_render_threshold_alpha().
+ *
+ * The pixmap that is created uses the #GdkColormap specified by @colormap.
+ * This colormap must match the colormap of the window where the pixmap
+ * will eventually be used or an error will result.
+ *
+ * If the pixbuf does not have an alpha channel, then *@mask_return will be set
+ * to %NULL.
+ **/
+void
+gdk_pixbuf_render_pixmap_and_mask_for_colormap (GdkPixbuf   *pixbuf,
+                                               GdkColormap *colormap,
+                                               GdkPixmap  **pixmap_return,
+                                               GdkBitmap  **mask_return,
+                                               int          alpha_threshold)
+{
+  GdkScreen *screen;
+
+  g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
+  g_return_if_fail (GDK_IS_COLORMAP (colormap));
+
+  screen = gdk_colormap_get_screen (colormap);
   
   if (pixmap_return)
     {
       GdkGC *gc;
-      
-      *pixmap_return = gdk_pixmap_new (NULL, pixbuf->width, pixbuf->height,
-                                      gdk_rgb_get_visual ()->depth);
-      gc = gdk_gc_new (*pixmap_return);
-      gdk_pixbuf_render_to_drawable (pixbuf, *pixmap_return, gc,
-                                    0, 0, 0, 0,
-                                    pixbuf->width, pixbuf->height,
-                                    GDK_RGB_DITHER_NORMAL,
-                                    0, 0);
-      gdk_gc_unref (gc);
+      *pixmap_return = gdk_pixmap_new (gdk_screen_get_root_window (screen),
+                                      gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
+                                      gdk_colormap_get_visual (colormap)->depth);
+
+      gdk_drawable_set_colormap (GDK_DRAWABLE (*pixmap_return), colormap);
+      gc = _gdk_drawable_get_scratch_gc (*pixmap_return, FALSE);
+
+      /* If the pixbuf has an alpha channel, using gdk_pixbuf_draw would give
+       * random pixel values in the area that are within the mask, but semi-
+       * transparent. So we treat the pixbuf like a pixbuf without alpha channel;
+       * see bug #487865.
+       */
+      if (gdk_pixbuf_get_has_alpha (pixbuf))
+        gdk_draw_rgb_32_image (*pixmap_return, gc,
+                               0, 0,
+                               gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
+                               GDK_RGB_DITHER_NORMAL,
+                               gdk_pixbuf_get_pixels (pixbuf), gdk_pixbuf_get_rowstride (pixbuf));
+      else
+        gdk_draw_pixbuf (*pixmap_return, gc, pixbuf, 
+                         0, 0, 0, 0,
+                         gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
+                         GDK_RGB_DITHER_NORMAL,
+                         0, 0);
     }
   
   if (mask_return)
     {
-      if (pixbuf->has_alpha)
+      if (gdk_pixbuf_get_has_alpha (pixbuf))
        {
-         *mask_return = gdk_pixmap_new (NULL, pixbuf->width, pixbuf->height, 1);
+         *mask_return = gdk_pixmap_new (gdk_screen_get_root_window (screen),
+                                        gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf), 1);
+
          gdk_pixbuf_render_threshold_alpha (pixbuf, *mask_return,
                                             0, 0, 0, 0,
-                                            pixbuf->width, pixbuf->height,
+                                            gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
                                             alpha_threshold);
        }
       else
        *mask_return = NULL;
     }
 }
+
+#define __GDK_PIXBUF_RENDER_C__
+#include "gdkaliasdef.c"
+