Blame gdk/gdkpixbuf-render.c

Packit 98cdb6
/* GdkPixbuf library - Rendering functions
Packit 98cdb6
 *
Packit 98cdb6
 * Copyright (C) 1999 The Free Software Foundation
Packit 98cdb6
 *
Packit 98cdb6
 * Author: Federico Mena-Quintero <federico@gimp.org>
Packit 98cdb6
 *
Packit 98cdb6
 * This library is free software; you can redistribute it and/or
Packit 98cdb6
 * modify it under the terms of the GNU Lesser General Public
Packit 98cdb6
 * License as published by the Free Software Foundation; either
Packit 98cdb6
 * version 2 of the License, or (at your option) any later version.
Packit 98cdb6
 *
Packit 98cdb6
 * This library is distributed in the hope that it will be useful,
Packit 98cdb6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 98cdb6
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 98cdb6
 * Lesser General Public License for more details.
Packit 98cdb6
 *
Packit 98cdb6
 * You should have received a copy of the GNU Lesser General Public
Packit 98cdb6
 * License along with this library; if not, write to the
Packit 98cdb6
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 98cdb6
 * Boston, MA 02111-1307, USA.
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
#include "config.h"
Packit 98cdb6
#include <gdk/gdk.h>
Packit 98cdb6
#include <gdk-pixbuf/gdk-pixbuf.h>
Packit 98cdb6
#include "gdkpixbuf.h"
Packit 98cdb6
#include "gdkscreen.h"
Packit 98cdb6
#include "gdkinternals.h"
Packit 98cdb6
#include "gdkalias.h"
Packit 98cdb6
Packit 98cdb6

Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_pixbuf_render_threshold_alpha:
Packit 98cdb6
 * @pixbuf: A pixbuf.
Packit 98cdb6
 * @bitmap: Bitmap where the bilevel mask will be painted to.
Packit 98cdb6
 * @src_x: Source X coordinate.
Packit 98cdb6
 * @src_y: source Y coordinate.
Packit 98cdb6
 * @dest_x: Destination X coordinate.
Packit 98cdb6
 * @dest_y: Destination Y coordinate.
Packit 98cdb6
 * @width: Width of region to threshold, or -1 to use pixbuf width
Packit 98cdb6
 * @height: Height of region to threshold, or -1 to use pixbuf height
Packit 98cdb6
 * @alpha_threshold: Opacity values below this will be painted as zero; all
Packit 98cdb6
 * other values will be painted as one.
Packit 98cdb6
 *
Packit 98cdb6
 * Takes the opacity values in a rectangular portion of a pixbuf and thresholds
Packit 98cdb6
 * them to produce a bi-level alpha mask that can be used as a clipping mask for
Packit 98cdb6
 * a drawable.
Packit 98cdb6
 *
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_pixbuf_render_threshold_alpha (GdkPixbuf *pixbuf,
Packit 98cdb6
                                   GdkBitmap *bitmap,
Packit 98cdb6
                                   int        src_x,
Packit 98cdb6
                                   int        src_y,
Packit 98cdb6
                                   int        dest_x,
Packit 98cdb6
                                   int        dest_y,
Packit 98cdb6
                                   int        width,
Packit 98cdb6
                                   int        height,
Packit 98cdb6
                                   int        alpha_threshold)
Packit 98cdb6
{
Packit 98cdb6
  GdkGC *gc;
Packit 98cdb6
  GdkColor color;
Packit 98cdb6
  int x, y;
Packit 98cdb6
  guchar *p;
Packit 98cdb6
  int start, start_status;
Packit 98cdb6
  int status;
Packit 98cdb6
Packit 98cdb6
  g_return_if_fail (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
Packit 98cdb6
  g_return_if_fail (gdk_pixbuf_get_n_channels (pixbuf) == 3 ||
Packit 98cdb6
                        gdk_pixbuf_get_n_channels (pixbuf) == 4);
Packit 98cdb6
  g_return_if_fail (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
Packit 98cdb6
Packit 98cdb6
  if (width == -1) 
Packit 98cdb6
    width = gdk_pixbuf_get_width (pixbuf);
Packit 98cdb6
  if (height == -1)
Packit 98cdb6
    height = gdk_pixbuf_get_height (pixbuf);
Packit 98cdb6
Packit 98cdb6
  g_return_if_fail (bitmap != NULL);
Packit 98cdb6
  g_return_if_fail (width >= 0 && height >= 0);
Packit 98cdb6
  g_return_if_fail (src_x >= 0 && src_x + width <= gdk_pixbuf_get_width (pixbuf));
Packit 98cdb6
  g_return_if_fail (src_y >= 0 && src_y + height <= gdk_pixbuf_get_height (pixbuf));
Packit 98cdb6
Packit 98cdb6
  g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);
Packit 98cdb6
Packit 98cdb6
  if (width == 0 || height == 0)
Packit 98cdb6
    return;
Packit 98cdb6
Packit 98cdb6
  gc = _gdk_drawable_get_scratch_gc (bitmap, FALSE);
Packit 98cdb6
Packit 98cdb6
  if (!gdk_pixbuf_get_has_alpha (pixbuf))
Packit 98cdb6
    {
Packit 98cdb6
      color.pixel = (alpha_threshold == 255) ? 0 : 1;
Packit 98cdb6
      gdk_gc_set_foreground (gc, &color;;
Packit 98cdb6
      gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
Packit 98cdb6
      return;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  color.pixel = 0;
Packit 98cdb6
  gdk_gc_set_foreground (gc, &color;;
Packit 98cdb6
  gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);
Packit 98cdb6
Packit 98cdb6
  color.pixel = 1;
Packit 98cdb6
  gdk_gc_set_foreground (gc, &color;;
Packit 98cdb6
Packit 98cdb6
  for (y = 0; y < height; y++)
Packit 98cdb6
    {
Packit 98cdb6
      p = (gdk_pixbuf_get_pixels (pixbuf) + (y + src_y) * gdk_pixbuf_get_rowstride (pixbuf) + src_x * gdk_pixbuf_get_n_channels (pixbuf)
Packit 98cdb6
	   + gdk_pixbuf_get_n_channels (pixbuf) - 1);
Packit 98cdb6
	    
Packit 98cdb6
      start = 0;
Packit 98cdb6
      start_status = *p < alpha_threshold;
Packit 98cdb6
	    
Packit 98cdb6
      for (x = 0; x < width; x++)
Packit 98cdb6
	{
Packit 98cdb6
	  status = *p < alpha_threshold;
Packit 98cdb6
	  
Packit 98cdb6
	  if (status != start_status)
Packit 98cdb6
	    {
Packit 98cdb6
	      if (!start_status)
Packit 98cdb6
		gdk_draw_line (bitmap, gc,
Packit 98cdb6
			       start + dest_x, y + dest_y,
Packit 98cdb6
			       x - 1 + dest_x, y + dest_y);
Packit 98cdb6
	      
Packit 98cdb6
	      start = x;
Packit 98cdb6
	      start_status = status;
Packit 98cdb6
	    }
Packit 98cdb6
	  
Packit 98cdb6
	  p += gdk_pixbuf_get_n_channels (pixbuf);
Packit 98cdb6
	}
Packit 98cdb6
      
Packit 98cdb6
      if (!start_status)
Packit 98cdb6
	gdk_draw_line (bitmap, gc,
Packit 98cdb6
		       start + dest_x, y + dest_y,
Packit 98cdb6
		       x - 1 + dest_x, y + dest_y);
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6

Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_pixbuf_render_to_drawable:
Packit 98cdb6
 * @pixbuf: A pixbuf.
Packit 98cdb6
 * @drawable: Destination drawable.
Packit 98cdb6
 * @gc: GC used for rendering.
Packit 98cdb6
 * @src_x: Source X coordinate within pixbuf.
Packit 98cdb6
 * @src_y: Source Y coordinate within pixbuf.
Packit 98cdb6
 * @dest_x: Destination X coordinate within drawable.
Packit 98cdb6
 * @dest_y: Destination Y coordinate within drawable.
Packit 98cdb6
 * @width: Width of region to render, in pixels, or -1 to use pixbuf width
Packit 98cdb6
 * @height: Height of region to render, in pixels, or -1 to use pixbuf height
Packit 98cdb6
 * @dither: Dithering mode for GdkRGB.
Packit 98cdb6
 * @x_dither: X offset for dither.
Packit 98cdb6
 * @y_dither: Y offset for dither.
Packit 98cdb6
 *
Packit 98cdb6
 * Renders a rectangular portion of a pixbuf to a drawable while using the
Packit 98cdb6
 * specified GC.  This is done using GdkRGB, so the specified drawable must have
Packit 98cdb6
 * the GdkRGB visual and colormap.  Note that this function will ignore the
Packit 98cdb6
 * opacity information for images with an alpha channel; the GC must already
Packit 98cdb6
 * have the clipping mask set if you want transparent regions to show through.
Packit 98cdb6
 *
Packit 98cdb6
 * For an explanation of dither offsets, see the GdkRGB documentation.  In
Packit 98cdb6
 * brief, the dither offset is important when re-rendering partial regions of an
Packit 98cdb6
 * image to a rendered version of the full image, or for when the offsets to a
Packit 98cdb6
 * base position change, as in scrolling.  The dither matrix has to be shifted
Packit 98cdb6
 * for consistent visual results.  If you do not have any of these cases, the
Packit 98cdb6
 * dither offsets can be both zero.
Packit 98cdb6
 *
Packit 98cdb6
 * Deprecated: 2.4: This function is obsolete. Use gdk_draw_pixbuf() instead.
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_pixbuf_render_to_drawable (GdkPixbuf   *pixbuf,
Packit 98cdb6
			       GdkDrawable *drawable,
Packit 98cdb6
			       GdkGC       *gc,
Packit 98cdb6
			       int src_x,    int src_y,
Packit 98cdb6
			       int dest_x,   int dest_y,
Packit 98cdb6
			       int width,    int height,
Packit 98cdb6
			       GdkRgbDither dither,
Packit 98cdb6
			       int x_dither, int y_dither)
Packit 98cdb6
{
Packit 98cdb6
  gdk_draw_pixbuf (drawable, gc, pixbuf,
Packit 98cdb6
		   src_x, src_y, dest_x, dest_y, width, height,
Packit 98cdb6
		   dither, x_dither, y_dither);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6

Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_pixbuf_render_to_drawable_alpha:
Packit 98cdb6
 * @pixbuf: A pixbuf.
Packit 98cdb6
 * @drawable: Destination drawable.
Packit 98cdb6
 * @src_x: Source X coordinate within pixbuf.
Packit 98cdb6
 * @src_y: Source Y coordinates within pixbuf.
Packit 98cdb6
 * @dest_x: Destination X coordinate within drawable.
Packit 98cdb6
 * @dest_y: Destination Y coordinate within drawable.
Packit 98cdb6
 * @width: Width of region to render, in pixels, or -1 to use pixbuf width.
Packit 98cdb6
 * @height: Height of region to render, in pixels, or -1 to use pixbuf height.
Packit 98cdb6
 * @alpha_mode: Ignored. Present for backwards compatibility.
Packit 98cdb6
 * @alpha_threshold: Ignored. Present for backwards compatibility
Packit 98cdb6
 * @dither: Dithering mode for GdkRGB.
Packit 98cdb6
 * @x_dither: X offset for dither.
Packit 98cdb6
 * @y_dither: Y offset for dither.
Packit 98cdb6
 *
Packit 98cdb6
 * Renders a rectangular portion of a pixbuf to a drawable.  The destination
Packit 98cdb6
 * drawable must have a colormap. All windows have a colormap, however, pixmaps
Packit 98cdb6
 * only have colormap by default if they were created with a non-%NULL window argument.
Packit 98cdb6
 * Otherwise a colormap must be set on them with gdk_drawable_set_colormap.
Packit 98cdb6
 *
Packit 98cdb6
 * On older X servers, rendering pixbufs with an alpha channel involves round trips
Packit 98cdb6
 * to the X server, and may be somewhat slow.
Packit 98cdb6
 *
Packit 98cdb6
 * Deprecated: 2.4: This function is obsolete. Use gdk_draw_pixbuf() instead.
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_pixbuf_render_to_drawable_alpha (GdkPixbuf   *pixbuf,
Packit 98cdb6
				     GdkDrawable *drawable,
Packit 98cdb6
				     int src_x,    int src_y,
Packit 98cdb6
				     int dest_x,   int dest_y,
Packit 98cdb6
				     int width,    int height,
Packit 98cdb6
				     GdkPixbufAlphaMode alpha_mode,
Packit 98cdb6
				     int                alpha_threshold,
Packit 98cdb6
				     GdkRgbDither       dither,
Packit 98cdb6
				     int x_dither, int y_dither)
Packit 98cdb6
{
Packit 98cdb6
  gdk_draw_pixbuf (drawable, NULL, pixbuf,
Packit 98cdb6
		   src_x, src_y, dest_x, dest_y, width, height,
Packit 98cdb6
		   dither, x_dither, y_dither);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_pixbuf_render_pixmap_and_mask:
Packit 98cdb6
 * @pixbuf: A pixbuf.
Packit 98cdb6
 * @pixmap_return: Location to store a pointer to the created pixmap,
Packit 98cdb6
 *   or %NULL if the pixmap is not needed.
Packit 98cdb6
 * @mask_return: Location to store a pointer to the created mask,
Packit 98cdb6
 *   or %NULL if the mask is not needed.
Packit 98cdb6
 * @alpha_threshold: Threshold value for opacity values.
Packit 98cdb6
 *
Packit 98cdb6
 * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
Packit 98cdb6
 * and @mask_return arguments, respectively, and renders a pixbuf and its
Packit 98cdb6
 * corresponding thresholded alpha mask to them.  This is merely a convenience
Packit 98cdb6
 * function; applications that need to render pixbufs with dither offsets or to
Packit 98cdb6
 * given drawables should use gdk_draw_pixbuf() and gdk_pixbuf_render_threshold_alpha().
Packit 98cdb6
 *
Packit 98cdb6
 * The pixmap that is created is created for the colormap returned
Packit 98cdb6
 * by gdk_rgb_get_colormap(). You normally will want to instead use
Packit 98cdb6
 * the actual colormap for a widget, and use
Packit 98cdb6
 * gdk_pixbuf_render_pixmap_and_mask_for_colormap().
Packit 98cdb6
 *
Packit 98cdb6
 * If the pixbuf does not have an alpha channel, then *@mask_return will be set
Packit 98cdb6
 * to %NULL.
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_pixbuf_render_pixmap_and_mask (GdkPixbuf  *pixbuf,
Packit 98cdb6
				   GdkPixmap **pixmap_return,
Packit 98cdb6
				   GdkBitmap **mask_return,
Packit 98cdb6
				   int         alpha_threshold)
Packit 98cdb6
{
Packit 98cdb6
  gdk_pixbuf_render_pixmap_and_mask_for_colormap (pixbuf,
Packit 98cdb6
						  gdk_rgb_get_colormap (),
Packit 98cdb6
						  pixmap_return, mask_return,
Packit 98cdb6
						  alpha_threshold);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_pixbuf_render_pixmap_and_mask_for_colormap:
Packit 98cdb6
 * @pixbuf: A pixbuf.
Packit 98cdb6
 * @colormap: A #GdkColormap
Packit 98cdb6
 * @pixmap_return: Location to store a pointer to the created pixmap,
Packit 98cdb6
 *   or %NULL if the pixmap is not needed.
Packit 98cdb6
 * @mask_return: Location to store a pointer to the created mask,
Packit 98cdb6
 *   or %NULL if the mask is not needed.
Packit 98cdb6
 * @alpha_threshold: Threshold value for opacity values.
Packit 98cdb6
 *
Packit 98cdb6
 * Creates a pixmap and a mask bitmap which are returned in the @pixmap_return
Packit 98cdb6
 * and @mask_return arguments, respectively, and renders a pixbuf and its
Packit 98cdb6
 * corresponding tresholded alpha mask to them.  This is merely a convenience
Packit 98cdb6
 * function; applications that need to render pixbufs with dither offsets or to
Packit 98cdb6
 * given drawables should use gdk_draw_pixbuf(), and gdk_pixbuf_render_threshold_alpha().
Packit 98cdb6
 *
Packit 98cdb6
 * The pixmap that is created uses the #GdkColormap specified by @colormap.
Packit 98cdb6
 * This colormap must match the colormap of the window where the pixmap
Packit 98cdb6
 * will eventually be used or an error will result.
Packit 98cdb6
 *
Packit 98cdb6
 * If the pixbuf does not have an alpha channel, then *@mask_return will be set
Packit 98cdb6
 * to %NULL.
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_pixbuf_render_pixmap_and_mask_for_colormap (GdkPixbuf   *pixbuf,
Packit 98cdb6
						GdkColormap *colormap,
Packit 98cdb6
						GdkPixmap  **pixmap_return,
Packit 98cdb6
						GdkBitmap  **mask_return,
Packit 98cdb6
						int          alpha_threshold)
Packit 98cdb6
{
Packit 98cdb6
  GdkScreen *screen;
Packit 98cdb6
Packit 98cdb6
  g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
Packit 98cdb6
  g_return_if_fail (GDK_IS_COLORMAP (colormap));
Packit 98cdb6
Packit 98cdb6
  screen = gdk_colormap_get_screen (colormap);
Packit 98cdb6
  
Packit 98cdb6
  if (pixmap_return)
Packit 98cdb6
    {
Packit 98cdb6
      GdkGC *gc;
Packit 98cdb6
      *pixmap_return = gdk_pixmap_new (gdk_screen_get_root_window (screen),
Packit 98cdb6
				       gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
Packit 98cdb6
				       gdk_colormap_get_visual (colormap)->depth);
Packit 98cdb6
Packit 98cdb6
      gdk_drawable_set_colormap (GDK_DRAWABLE (*pixmap_return), colormap);
Packit 98cdb6
      gc = _gdk_drawable_get_scratch_gc (*pixmap_return, FALSE);
Packit 98cdb6
Packit 98cdb6
      /* If the pixbuf has an alpha channel, using gdk_pixbuf_draw would give
Packit 98cdb6
       * random pixel values in the area that are within the mask, but semi-
Packit 98cdb6
       * transparent. So we treat the pixbuf like a pixbuf without alpha channel;
Packit 98cdb6
       * see bug #487865.
Packit 98cdb6
       */
Packit 98cdb6
      if (gdk_pixbuf_get_has_alpha (pixbuf))
Packit 98cdb6
        gdk_draw_rgb_32_image (*pixmap_return, gc,
Packit 98cdb6
                               0, 0,
Packit 98cdb6
                               gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
Packit 98cdb6
                               GDK_RGB_DITHER_NORMAL,
Packit 98cdb6
                               gdk_pixbuf_get_pixels (pixbuf), gdk_pixbuf_get_rowstride (pixbuf));
Packit 98cdb6
      else
Packit 98cdb6
        gdk_draw_pixbuf (*pixmap_return, gc, pixbuf, 
Packit 98cdb6
                         0, 0, 0, 0,
Packit 98cdb6
                         gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
Packit 98cdb6
                         GDK_RGB_DITHER_NORMAL,
Packit 98cdb6
                         0, 0);
Packit 98cdb6
    }
Packit 98cdb6
  
Packit 98cdb6
  if (mask_return)
Packit 98cdb6
    {
Packit 98cdb6
      if (gdk_pixbuf_get_has_alpha (pixbuf))
Packit 98cdb6
	{
Packit 98cdb6
	  *mask_return = gdk_pixmap_new (gdk_screen_get_root_window (screen),
Packit 98cdb6
					 gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf), 1);
Packit 98cdb6
Packit 98cdb6
	  gdk_pixbuf_render_threshold_alpha (pixbuf, *mask_return,
Packit 98cdb6
					     0, 0, 0, 0,
Packit 98cdb6
					     gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
Packit 98cdb6
					     alpha_threshold);
Packit 98cdb6
	}
Packit 98cdb6
      else
Packit 98cdb6
	*mask_return = NULL;
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
#define __GDK_PIXBUF_RENDER_C__
Packit 98cdb6
#include "gdkaliasdef.c"
Packit 98cdb6