gdkpixbuf-render.c 11.3 KB
Newer Older
1 2 3 4 5 6 7
/* GdkPixbuf library - Rendering functions
 *
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Author: Federico Mena-Quintero <federico@gimp.org>
 *
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9 10 11 12 13 14
 * 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
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 19 20 21 22 23 24
 * 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 <gdk/gdk.h>
25
#include "gdk-pixbuf-private.h"
Owen Taylor's avatar
Owen Taylor committed
26
#include "gdkpixbuf.h"
27
#include "gdkscreen.h"
28 29 30 31 32 33 34 35 36 37 38



/**
 * gdk_pixbuf_render_threshold_alpha:
 * @pixbuf: A pixbuf.
 * @bitmap: Bitmap where the bilevel mask will be painted to.
 * @src_x: Source X coordinate.
 * @src_y: source Y coordinate.
 * @dest_x: Destination X coordinate.
 * @dest_y: Destination Y coordinate.
39 40
 * @width: Width of region to threshold, or -1 to use pixbuf width
 * @height: Height of region to threshold, or -1 to use pixbuf height
41 42
 * @alpha_threshold: Opacity values below this will be painted as zero; all
 * other values will be painted as one.
43 44 45 46
 *
 * Takes the opacity values in a rectangular portion of a pixbuf and thresholds
 * them to produce a bi-level alpha mask that can be used as a clipping mask for
 * a drawable.
47
 *
48 49
 **/
void
50 51 52
gdk_pixbuf_render_threshold_alpha (GdkPixbuf *pixbuf,
				   GdkBitmap *bitmap,
				   int src_x,  int src_y,
53
				   int dest_x, int dest_y,
54
				   int width,  int height,
55 56
				   int alpha_threshold)
{
57 58 59 60 61 62 63
  GdkGC *gc;
  GdkColor color;
  int x, y;
  guchar *p;
  int start, start_status;
  int status;

64
  g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
65 66 67 68
  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);

69 70 71 72 73
  if (width == -1) 
    width = pixbuf->width;
  if (height == -1)
    height = pixbuf->height;

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  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);
  g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);

  g_return_if_fail (alpha_threshold >= 0 && alpha_threshold <= 255);

  if (width == 0 || height == 0)
    return;

  gc = gdk_gc_new (bitmap);

  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);
91
      g_object_unref (gc);
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
      return;
    }

  color.pixel = 0;
  gdk_gc_set_foreground (gc, &color);
  gdk_draw_rectangle (bitmap, gc, TRUE, dest_x, dest_y, width, height);

  color.pixel = 1;
  gdk_gc_set_foreground (gc, &color);

  for (y = 0; y < height; y++)
    {
      p = (pixbuf->pixels + (y + src_y) * pixbuf->rowstride + src_x * pixbuf->n_channels
	   + pixbuf->n_channels - 1);
	    
      start = 0;
      start_status = *p < alpha_threshold;
	    
      for (x = 0; x < width; x++)
	{
	  status = *p < alpha_threshold;
	  
	  if (status != start_status)
	    {
	      if (!start_status)
		gdk_draw_line (bitmap, gc,
			       start + dest_x, y + dest_y,
			       x - 1 + dest_x, y + dest_y);
	      
	      start = x;
	      start_status = status;
	    }
	  
	  p += pixbuf->n_channels;
126
	}
127 128 129 130 131 132 133
      
      if (!start_status)
	gdk_draw_line (bitmap, gc,
		       start + dest_x, y + dest_y,
		       x - 1 + dest_x, y + dest_y);
    }
	
134
  g_object_unref (gc);
135 136
}

137 138 139 140 141 142 143 144 145 146 147


/**
 * gdk_pixbuf_render_to_drawable:
 * @pixbuf: A pixbuf.
 * @drawable: Destination drawable.
 * @gc: GC used for rendering.
 * @src_x: Source X coordinate within pixbuf.
 * @src_y: Source Y coordinate within pixbuf.
 * @dest_x: Destination X coordinate within drawable.
 * @dest_y: Destination Y coordinate within drawable.
148 149
 * @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
150 151 152
 * @dither: Dithering mode for GdkRGB.
 * @x_dither: X offset for dither.
 * @y_dither: Y offset for dither.
153
 *
154 155 156 157 158
 * Renders a rectangular portion of a pixbuf to a drawable while using the
 * specified GC.  This is done using GdkRGB, so the specified drawable must have
 * the GdkRGB visual and colormap.  Note that this function will ignore the
 * opacity information for images with an alpha channel; the GC must already
 * have the clipping mask set if you want transparent regions to show through.
159
 *
Arturo Espinosa's avatar
Arturo Espinosa committed
160 161 162 163 164 165
 * For an explanation of dither offsets, see the GdkRGB documentation.  In
 * brief, the dither offset is important when re-rendering partial regions of an
 * image to a rendered version of the full image, or for when the offsets to a
 * 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.
166 167
 *
 * This function is obsolete. Use gdk_draw_pixbuf() instead.
168
 **/
169
void
170 171 172 173 174 175
gdk_pixbuf_render_to_drawable (GdkPixbuf   *pixbuf,
			       GdkDrawable *drawable,
			       GdkGC       *gc,
			       int src_x,    int src_y,
			       int dest_x,   int dest_y,
			       int width,    int height,
176 177 178
			       GdkRgbDither dither,
			       int x_dither, int y_dither)
{
179 180 181
  gdk_draw_pixbuf (drawable, gc, pixbuf,
		   src_x, src_y, dest_x, dest_y, width, height,
		   dither, x_dither, y_dither);
182 183 184 185 186 187 188 189 190 191 192 193
}



/**
 * gdk_pixbuf_render_to_drawable_alpha:
 * @pixbuf: A pixbuf.
 * @drawable: Destination drawable.
 * @src_x: Source X coordinate within pixbuf.
 * @src_y: Source Y coordinates within pixbuf.
 * @dest_x: Destination X coordinate within drawable.
 * @dest_y: Destination Y coordinate within drawable.
194 195
 * @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.
196 197
 * @alpha_mode: Ignored. Present for backwards compatibility.
 * @alpha_threshold: Ignored. Present for backwards compatibility
198 199 200 201
 * @dither: Dithering mode for GdkRGB.
 * @x_dither: X offset for dither.
 * @y_dither: Y offset for dither.
 *
202 203 204 205
 * 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.
206
 *
207 208
 * On older X servers, rendering pixbufs with an alpha channel involves round trips
 * to the X server, and may be somewhat slow.
209 210
 *
 * This function is obsolete. Use gdk_draw_pixbuf() instead.
211 212
 **/
void
213 214 215 216 217
gdk_pixbuf_render_to_drawable_alpha (GdkPixbuf   *pixbuf,
				     GdkDrawable *drawable,
				     int src_x,    int src_y,
				     int dest_x,   int dest_y,
				     int width,    int height,
218
				     GdkPixbufAlphaMode alpha_mode,
219 220
				     int                alpha_threshold,
				     GdkRgbDither       dither,
221 222
				     int x_dither, int y_dither)
{
223 224 225
  gdk_draw_pixbuf (drawable, NULL, pixbuf,
		   src_x, src_y, dest_x, dest_y, width, height,
		   dither, x_dither, y_dither);
226
}
227

228
/**
229
 * gdk_pixbuf_render_pixmap_and_mask:
230
 * @pixbuf: A pixbuf.
231 232 233 234
 * @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.
235
 * @alpha_threshold: Threshold value for opacity values.
236
 *
237 238
 * 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
239
 * corresponding thresholded alpha mask to them.  This is merely a convenience
240 241 242
 * 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().
243
 *
244 245 246 247 248
 * 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.
 *
249 250
 * If the pixbuf does not have an alpha channel, then *@mask_return will be set
 * to NULL.
251 252
 **/
void
253 254 255 256
gdk_pixbuf_render_pixmap_and_mask (GdkPixbuf  *pixbuf,
				   GdkPixmap **pixmap_return,
				   GdkBitmap **mask_return,
				   int         alpha_threshold)
257 258 259 260 261 262 263 264 265 266 267
{
  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
268 269 270 271
 * @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.
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
 * @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_pixbuf_render_to_drawable_alpha() or
 * gdk_pixbuf_render_to_drawable(), 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)
294
{
295 296 297 298 299 300
  GdkScreen *screen;

  g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
  g_return_if_fail (GDK_IS_COLORMAP (colormap));

  screen = gdk_colormap_get_screen (colormap);
301 302 303 304
  
  if (pixmap_return)
    {
      GdkGC *gc;
305
      *pixmap_return = gdk_pixmap_new (gdk_screen_get_root_window (screen),
306
				       gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
307
				       gdk_colormap_get_visual (colormap)->depth);
308 309

      gdk_drawable_set_colormap (GDK_DRAWABLE (*pixmap_return), colormap);
310 311 312
      gc = gdk_gc_new (*pixmap_return);
      gdk_pixbuf_render_to_drawable (pixbuf, *pixmap_return, gc,
				     0, 0, 0, 0,
313
				     gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
314 315
				     GDK_RGB_DITHER_NORMAL,
				     0, 0);
316
      g_object_unref (gc);
317 318 319 320
    }
  
  if (mask_return)
    {
321
      if (gdk_pixbuf_get_has_alpha (pixbuf))
322
	{
323
	  *mask_return = gdk_pixmap_new (gdk_screen_get_root_window (screen),
324 325
					 gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf), 1);

326 327
	  gdk_pixbuf_render_threshold_alpha (pixbuf, *mask_return,
					     0, 0, 0, 0,
328
					     gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf),
329
					     alpha_threshold);
330
	}
331 332 333
      else
	*mask_return = NULL;
    }
334
}