gdkdraw.c 51 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GDK - The GIMP Drawing Kit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

27
#include "config.h"
28
#include <math.h>
29
#include <pango/pangocairo.h>
30
#include <gdk-pixbuf/gdk-pixbuf.h>
31
#include "gdkcairo.h"
Owen Taylor's avatar
Started  
Owen Taylor committed
32
#include "gdkdrawable.h"
33
#include "gdkinternals.h"
34
#include "gdkwindow.h"
35
#include "gdkscreen.h"
36
#include "gdkpixbuf.h"
37
#include "gdkalias.h"
38 39 40 41 42 43 44 45 46 47 48 49 50

static GdkImage*    gdk_drawable_real_get_image (GdkDrawable     *drawable,
						 gint             x,
						 gint             y,
						 gint             width,
						 gint             height);
static GdkDrawable* gdk_drawable_real_get_composite_drawable (GdkDrawable  *drawable,
							      gint          x,
							      gint          y,
							      gint          width,
							      gint          height,
							      gint         *composite_x_offset,
							      gint         *composite_y_offset);
51
static cairo_region_t *  gdk_drawable_real_get_visible_region     (GdkDrawable  *drawable);
52 53 54 55 56 57 58 59 60 61 62 63
static void         gdk_drawable_real_draw_pixbuf            (GdkDrawable  *drawable,
							      GdkGC        *gc,
							      GdkPixbuf    *pixbuf,
							      gint          src_x,
							      gint          src_y,
							      gint          dest_x,
							      gint          dest_y,
							      gint          width,
							      gint          height,
							      GdkRgbDither  dither,
							      gint          x_dither,
							      gint          y_dither);
64 65 66 67 68 69 70 71 72
static void         gdk_drawable_real_draw_drawable          (GdkDrawable  *drawable,
							      GdkGC	   *gc,
							      GdkDrawable  *src,
							      gint          xsrc,
							      gint	    ysrc,
							      gint	    xdest,
							      gint	    ydest,
							      gint	    width,
							      gint	    height);
73
     
Havoc Pennington's avatar
Havoc Pennington committed
74

Matthias Clasen's avatar
Matthias Clasen committed
75
G_DEFINE_ABSTRACT_TYPE (GdkDrawable, gdk_drawable, G_TYPE_OBJECT)
76

Havoc Pennington's avatar
Havoc Pennington committed
77 78 79
static void
gdk_drawable_class_init (GdkDrawableClass *klass)
{
80
  klass->get_image = gdk_drawable_real_get_image;
Havoc Pennington's avatar
Havoc Pennington committed
81
  klass->get_composite_drawable = gdk_drawable_real_get_composite_drawable;
82 83 84
  /* Default implementation for clip and visible region is the same */
  klass->get_clip_region = gdk_drawable_real_get_visible_region;
  klass->get_visible_region = gdk_drawable_real_get_visible_region;
85
  klass->draw_pixbuf = gdk_drawable_real_draw_pixbuf;
86
  klass->draw_drawable = gdk_drawable_real_draw_drawable;
Havoc Pennington's avatar
Havoc Pennington committed
87 88
}

Matthias Clasen's avatar
Matthias Clasen committed
89 90 91 92 93
static void
gdk_drawable_init (GdkDrawable *drawable)
{
}

94 95 96
/* Manipulation of drawables
 */

Havoc Pennington's avatar
Havoc Pennington committed
97 98 99
/**
 * gdk_drawable_get_size:
 * @drawable: a #GdkDrawable
100 101
 * @width: (out) (allow-none): location to store drawable's width, or %NULL
 * @height: (out) (allow-none): location to store drawable's height, or %NULL
Havoc Pennington's avatar
Havoc Pennington committed
102 103 104
 *
 * Fills *@width and *@height with the size of @drawable.
 * @width or @height can be %NULL if you only want the other one.
105
 *
Havoc Pennington's avatar
Havoc Pennington committed
106 107 108 109
 * On the X11 platform, if @drawable is a #GdkWindow, the returned
 * size is the size reported in the most-recently-processed configure
 * event, rather than the current size on the X server.
 * 
Havoc Pennington's avatar
Havoc Pennington committed
110
 **/
111 112 113 114 115
void
gdk_drawable_get_size (GdkDrawable *drawable,
		       gint        *width,
		       gint        *height)
{
116 117
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));

Cody Russell's avatar
Updates  
Cody Russell committed
118 119
  if (GDK_DRAWABLE_GET_CLASS (drawable)->get_size != NULL)
    GDK_DRAWABLE_GET_CLASS (drawable)->get_size (drawable, width, height);  
120 121
}

Havoc Pennington's avatar
Havoc Pennington committed
122 123 124 125 126 127 128 129
/**
 * gdk_drawable_get_visual:
 * @drawable: a #GdkDrawable
 * 
 * Gets the #GdkVisual describing the pixel format of @drawable.
 * 
 * Return value: a #GdkVisual
 **/
130 131 132
GdkVisual*
gdk_drawable_get_visual (GdkDrawable *drawable)
{
133 134 135
  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
  
  return GDK_DRAWABLE_GET_CLASS (drawable)->get_visual (drawable);
136 137
}

Havoc Pennington's avatar
Havoc Pennington committed
138 139 140 141 142 143 144 145 146 147
/**
 * gdk_drawable_get_depth:
 * @drawable: a #GdkDrawable
 * 
 * Obtains the bit depth of the drawable, that is, the number of bits
 * that make up a pixel in the drawable's visual. Examples are 8 bits
 * per pixel, 24 bits per pixel, etc.
 * 
 * Return value: number of bits per pixel
 **/
148 149 150
gint
gdk_drawable_get_depth (GdkDrawable *drawable)
{
151
  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), 0);
152

153 154
  return GDK_DRAWABLE_GET_CLASS (drawable)->get_depth (drawable);
}
155 156 157 158 159 160 161
/**
 * gdk_drawable_get_screen:
 * @drawable: a #GdkDrawable
 * 
 * Gets the #GdkScreen associated with a #GdkDrawable.
 * 
 * Return value: the #GdkScreen associated with @drawable
Matthias Clasen's avatar
Matthias Clasen committed
162 163
 *
 * Since: 2.2
164 165 166 167 168 169 170 171
 **/
GdkScreen*
gdk_drawable_get_screen(GdkDrawable *drawable)
{
  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);

  return GDK_DRAWABLE_GET_CLASS (drawable)->get_screen (drawable);
}
172

173 174 175 176 177 178 179
/**
 * gdk_drawable_get_display:
 * @drawable: a #GdkDrawable
 * 
 * Gets the #GdkDisplay associated with a #GdkDrawable.
 * 
 * Return value: the #GdkDisplay associated with @drawable
Matthias Clasen's avatar
Matthias Clasen committed
180 181
 *
 * Since: 2.2
182 183 184 185 186 187 188 189 190
 **/
GdkDisplay*
gdk_drawable_get_display (GdkDrawable *drawable)
{
  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
  
  return gdk_screen_get_display (gdk_drawable_get_screen (drawable));
}
	
Havoc Pennington's avatar
Havoc Pennington committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
/**
 * gdk_drawable_set_colormap:
 * @drawable: a #GdkDrawable
 * @colormap: a #GdkColormap
 *
 * Sets the colormap associated with @drawable. Normally this will
 * happen automatically when the drawable is created; you only need to
 * use this function if the drawable-creating function did not have a
 * way to determine the colormap, and you then use drawable operations
 * that require a colormap. The colormap for all drawables and
 * graphics contexts you intend to use together should match. i.e.
 * when using a #GdkGC to draw to a drawable, or copying one drawable
 * to another, the colormaps should match.
 * 
 **/
206 207 208 209 210
void
gdk_drawable_set_colormap (GdkDrawable *drawable,
                           GdkColormap *cmap)
{
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
211 212
  g_return_if_fail (cmap == NULL || gdk_drawable_get_depth (drawable)
                    == cmap->visual->depth);
213 214 215 216

  GDK_DRAWABLE_GET_CLASS (drawable)->set_colormap (drawable, cmap);
}

Havoc Pennington's avatar
Havoc Pennington committed
217 218 219 220 221 222 223 224 225
/**
 * gdk_drawable_get_colormap:
 * @drawable: a #GdkDrawable
 * 
 * Gets the colormap for @drawable, if one is set; returns
 * %NULL otherwise.
 * 
 * Return value: the colormap, or %NULL
 **/
226 227 228 229 230 231
GdkColormap*
gdk_drawable_get_colormap (GdkDrawable *drawable)
{
  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);

  return GDK_DRAWABLE_GET_CLASS (drawable)->get_colormap (drawable);
232 233
}

234 235
/* Drawing
 */
236 237 238 239 240 241 242 243 244 245 246

/**
 * gdk_draw_point:
 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
 * @gc: a #GdkGC.
 * @x: the x coordinate of the point.
 * @y: the y coordinate of the point.
 * 
 * Draws a point, using the foreground color and other attributes of 
 * the #GdkGC.
 **/
Elliot Lee's avatar
Elliot Lee committed
247 248 249 250 251 252
void
gdk_draw_point (GdkDrawable *drawable,
                GdkGC       *gc,
                gint         x,
                gint         y)
{
253
  GdkPoint point;
Elliot Lee's avatar
Elliot Lee committed
254

255 256
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
257

258 259 260
  point.x = x;
  point.y = y;
  
261
  GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc, &point, 1);
Elliot Lee's avatar
Elliot Lee committed
262 263
}

264 265 266 267
/**
 * gdk_draw_line:
 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap). 
 * @gc: a #GdkGC.
268 269 270 271
 * @x1_: the x coordinate of the start point.
 * @y1_: the y coordinate of the start point.
 * @x2_: the x coordinate of the end point.
 * @y2_: the y coordinate of the end point.
272 273 274 275
 * 
 * Draws a line, using the foreground color and other attributes of 
 * the #GdkGC.
 **/
Elliot Lee's avatar
Elliot Lee committed
276 277 278 279 280 281 282 283
void
gdk_draw_line (GdkDrawable *drawable,
	       GdkGC       *gc,
	       gint         x1,
	       gint         y1,
	       gint         x2,
	       gint         y2)
{
284
  GdkSegment segment;
Elliot Lee's avatar
Elliot Lee committed
285

286 287
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
288

289 290 291 292
  segment.x1 = x1;
  segment.y1 = y1;
  segment.x2 = x2;
  segment.y2 = y2;
293
  GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc, &segment, 1);
Elliot Lee's avatar
Elliot Lee committed
294 295
}

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
/**
 * gdk_draw_rectangle:
 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
 * @gc: a #GdkGC.
 * @filled: %TRUE if the rectangle should be filled.
 * @x: the x coordinate of the left edge of the rectangle.
 * @y: the y coordinate of the top edge of the rectangle.
 * @width: the width of the rectangle.
 * @height: the height of the rectangle.
 * 
 * Draws a rectangular outline or filled rectangle, using the foreground color
 * and other attributes of the #GdkGC.
 *
 * A rectangle drawn filled is 1 pixel smaller in both dimensions than a 
 * rectangle outlined. Calling 
 * <literal>gdk_draw_rectangle (window, gc, TRUE, 0, 0, 20, 20)</literal> 
 * results in a filled rectangle 20 pixels wide and 20 pixels high. Calling
 * <literal>gdk_draw_rectangle (window, gc, FALSE, 0, 0, 20, 20)</literal> 
 * results in an outlined rectangle with corners at (0, 0), (0, 20), (20, 20),
 * and (20, 0), which makes it 21 pixels wide and 21 pixels high.
 **/
Elliot Lee's avatar
Elliot Lee committed
317 318 319
void
gdk_draw_rectangle (GdkDrawable *drawable,
		    GdkGC       *gc,
320
		    gboolean     filled,
Elliot Lee's avatar
Elliot Lee committed
321 322 323 324
		    gint         x,
		    gint         y,
		    gint         width,
		    gint         height)
325 326 327
{  
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
328

329 330 331 332 333 334 335 336 337 338 339 340
  if (width < 0 || height < 0)
    {
      gint real_width;
      gint real_height;
      
      gdk_drawable_get_size (drawable, &real_width, &real_height);

      if (width < 0)
        width = real_width;
      if (height < 0)
        height = real_height;
    }
Elliot Lee's avatar
Elliot Lee committed
341

342 343
  GDK_DRAWABLE_GET_CLASS (drawable)->draw_rectangle (drawable, gc, filled, x, y,
                                                     width, height);
Elliot Lee's avatar
Elliot Lee committed
344 345
}

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
/**
 * gdk_draw_arc:
 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
 * @gc: a #GdkGC.
 * @filled: %TRUE if the arc should be filled, producing a 'pie slice'.
 * @x: the x coordinate of the left edge of the bounding rectangle.
 * @y: the y coordinate of the top edge of the bounding rectangle.
 * @width: the width of the bounding rectangle.
 * @height: the height of the bounding rectangle.
 * @angle1: the start angle of the arc, relative to the 3 o'clock position,
 *     counter-clockwise, in 1/64ths of a degree.
 * @angle2: the end angle of the arc, relative to @angle1, in 1/64ths 
 *     of a degree.
 * 
 * Draws an arc or a filled 'pie slice'. The arc is defined by the bounding
 * rectangle of the entire ellipse, and the start and end angles of the part 
 * of the ellipse to be drawn.
 **/
Elliot Lee's avatar
Elliot Lee committed
364 365 366
void
gdk_draw_arc (GdkDrawable *drawable,
	      GdkGC       *gc,
367
	      gboolean     filled,
Elliot Lee's avatar
Elliot Lee committed
368 369 370 371 372 373
	      gint         x,
	      gint         y,
	      gint         width,
	      gint         height,
	      gint         angle1,
	      gint         angle2)
374 375 376
{  
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
377

378 379 380 381 382 383 384 385 386 387 388 389
  if (width < 0 || height < 0)
    {
      gint real_width;
      gint real_height;
      
      gdk_drawable_get_size (drawable, &real_width, &real_height);

      if (width < 0)
        width = real_width;
      if (height < 0)
        height = real_height;
    }
Elliot Lee's avatar
Elliot Lee committed
390

391 392
  GDK_DRAWABLE_GET_CLASS (drawable)->draw_arc (drawable, gc, filled,
                                               x, y, width, height, angle1, angle2);
Elliot Lee's avatar
Elliot Lee committed
393 394
}

395 396 397 398 399 400 401 402 403
/**
 * gdk_draw_polygon:
 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
 * @gc: a #GdkGC.
 * @filled: %TRUE if the polygon should be filled. The polygon is closed
 *     automatically, connecting the last point to the first point if 
 *     necessary.
 * @points: an array of #GdkPoint structures specifying the points making 
 *     up the polygon.
404
 * @n_points: the number of points.
405 406 407
 * 
 * Draws an outlined or filled polygon.
 **/
Elliot Lee's avatar
Elliot Lee committed
408
void
409 410 411 412 413
gdk_draw_polygon (GdkDrawable    *drawable,
		  GdkGC          *gc,
		  gboolean        filled,
		  const GdkPoint *points,
		  gint            n_points)
Elliot Lee's avatar
Elliot Lee committed
414
{
415 416
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
417

418
  GDK_DRAWABLE_GET_CLASS (drawable)->draw_polygon (drawable, gc, filled,
419 420
                                                   (GdkPoint *) points,
                                                   n_points);
Elliot Lee's avatar
Elliot Lee committed
421 422
}

Havoc Pennington's avatar
Havoc Pennington committed
423 424 425 426
/**
 * gdk_draw_drawable:
 * @drawable: a #GdkDrawable
 * @gc: a #GdkGC sharing the drawable's visual and colormap
Matthias Clasen's avatar
Matthias Clasen committed
427
 * @src: the source #GdkDrawable, which may be the same as @drawable
Havoc Pennington's avatar
Havoc Pennington committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
 * @xsrc: X position in @src of rectangle to draw
 * @ysrc: Y position in @src of rectangle to draw
 * @xdest: X position in @drawable where the rectangle should be drawn
 * @ydest: Y position in @drawable where the rectangle should be drawn
 * @width: width of rectangle to draw, or -1 for entire @src width
 * @height: height of rectangle to draw, or -1 for entire @src height
 *
 * Copies the @width x @height region of @src at coordinates (@xsrc,
 * @ysrc) to coordinates (@xdest, @ydest) in @drawable.
 * @width and/or @height may be given as -1, in which case the entire
 * @src drawable will be copied.
 *
 * Most fields in @gc are not used for this operation, but notably the
 * clip mask or clip region will be honored.
 *
 * The source and destination drawables must have the same visual and
 * colormap, or errors will result. (On X11, failure to match
 * visual/colormap results in a BadMatch error from the X server.)
 * A common cause of this problem is an attempt to draw a bitmap to
Matthias Clasen's avatar
2.7.3  
Matthias Clasen committed
447 448 449
 * a color drawable. The way to draw a bitmap is to set the bitmap as 
 * the stipple on the #GdkGC, set the fill mode to %GDK_STIPPLED, and 
 * then draw the rectangle.
Havoc Pennington's avatar
Havoc Pennington committed
450
 **/
Elliot Lee's avatar
Elliot Lee committed
451
void
452 453 454 455 456 457 458 459 460
gdk_draw_drawable (GdkDrawable *drawable,
		   GdkGC       *gc,
		   GdkDrawable *src,
		   gint         xsrc,
		   gint         ysrc,
		   gint         xdest,
		   gint         ydest,
		   gint         width,
		   gint         height)
Elliot Lee's avatar
Elliot Lee committed
461
{
462
  GdkDrawable *composite;
Havoc Pennington's avatar
Havoc Pennington committed
463 464 465
  gint composite_x_offset = 0;
  gint composite_y_offset = 0;

466
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
467
  g_return_if_fail (GDK_IS_DRAWABLE (src));
468
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
469

470 471 472 473 474 475 476 477 478 479 480 481
  if (width < 0 || height < 0)
    {
      gint real_width;
      gint real_height;
      
      gdk_drawable_get_size (src, &real_width, &real_height);

      if (width < 0)
        width = real_width;
      if (height < 0)
        height = real_height;
    }
Elliot Lee's avatar
Elliot Lee committed
482

Havoc Pennington's avatar
Havoc Pennington committed
483 484 485 486 487 488 489 490

  composite =
    GDK_DRAWABLE_GET_CLASS (src)->get_composite_drawable (src,
                                                          xsrc, ysrc,
                                                          width, height,
                                                          &composite_x_offset,
                                                          &composite_y_offset);

491
  /* TODO: For non-native windows this may copy stuff from other overlapping
492 493 494
     windows. We should clip that and (for windows with bg != None) clear that
     area in the destination instead. */

495 496
  if (GDK_DRAWABLE_GET_CLASS (drawable)->draw_drawable_with_src)
    GDK_DRAWABLE_GET_CLASS (drawable)->draw_drawable_with_src (drawable, gc,
497
							       composite,
498 499 500 501 502 503 504
							       xsrc - composite_x_offset,
							       ysrc - composite_y_offset,
							       xdest, ydest,
							       width, height,
							       src);
  else /* backwards compat for old out-of-tree implementations of GdkDrawable (are there any?) */
    GDK_DRAWABLE_GET_CLASS (drawable)->draw_drawable (drawable, gc,
505
						      composite,
506 507 508 509
						      xsrc - composite_x_offset,
						      ysrc - composite_y_offset,
						      xdest, ydest,
						      width, height);
510

511
  g_object_unref (composite);
Elliot Lee's avatar
Elliot Lee committed
512 513
}

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
/**
 * gdk_draw_image:
 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
 * @gc: a #GdkGC.
 * @image: the #GdkImage to draw.
 * @xsrc: the left edge of the source rectangle within @image.
 * @ysrc: the top of the source rectangle within @image.
 * @xdest: the x coordinate of the destination within @drawable.
 * @ydest: the y coordinate of the destination within @drawable.
 * @width: the width of the area to be copied, or -1 to make the area 
 *     extend to the right edge of @image.
 * @height: the height of the area to be copied, or -1 to make the area 
 *     extend to the bottom edge of @image.
 * 
 * Draws a #GdkImage onto a drawable.
 * The depth of the #GdkImage must match the depth of the #GdkDrawable.
 **/
Elliot Lee's avatar
Elliot Lee committed
531 532 533 534 535 536 537 538 539 540 541
void
gdk_draw_image (GdkDrawable *drawable,
		GdkGC       *gc,
		GdkImage    *image,
		gint         xsrc,
		gint         ysrc,
		gint         xdest,
		gint         ydest,
		gint         width,
		gint         height)
{
542
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
543
  g_return_if_fail (GDK_IS_IMAGE (image));
544
  g_return_if_fail (GDK_IS_GC (gc));
Elliot Lee's avatar
Elliot Lee committed
545 546 547 548 549 550

  if (width == -1)
    width = image->width;
  if (height == -1)
    height = image->height;

551 552
  GDK_DRAWABLE_GET_CLASS (drawable)->draw_image (drawable, gc, image, xsrc, ysrc,
                                                 xdest, ydest, width, height);
Elliot Lee's avatar
Elliot Lee committed
553 554
}

555
/**
556
 * gdk_draw_pixbuf:
557
 * @drawable: Destination drawable.
Johan Dahlin's avatar
Johan Dahlin committed
558
 * @gc: (allow-none): a #GdkGC, used for clipping, or %NULL
559 560 561 562 563 564 565
 * @pixbuf: a #GdkPixbuf
 * @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.
 * @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.
Matthias Clasen's avatar
Matthias Clasen committed
566
 * @dither: Dithering mode for #GdkRGB.
567 568 569 570 571
 * @x_dither: X offset for dither.
 * @y_dither: Y offset for dither.
 * 
 * Renders a rectangular portion of a pixbuf to a drawable.  The destination
 * drawable must have a colormap. All windows have a colormap, however, pixmaps
Matthias Clasen's avatar
Matthias Clasen committed
572 573 574 575 576 577
 * 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.
578
 *
579 580 581 582 583 584
 * If GDK is built with the Sun mediaLib library, the gdk_draw_pixbuf
 * function is accelerated using mediaLib, which provides hardware
 * acceleration on Intel, AMD, and Sparc chipsets.  If desired, mediaLib
 * support can be turned off by setting the GDK_DISABLE_MEDIALIB environment
 * variable.
 *
Matthias Clasen's avatar
Matthias Clasen committed
585
 * Since: 2.2
586 587
 **/
void
588
gdk_draw_pixbuf (GdkDrawable     *drawable,
589 590 591 592 593 594 595 596 597 598 599
                 GdkGC           *gc,
                 const GdkPixbuf *pixbuf,
                 gint             src_x,
                 gint             src_y,
                 gint             dest_x,
                 gint             dest_y,
                 gint             width,
                 gint             height,
                 GdkRgbDither     dither,
                 gint             x_dither,
                 gint             y_dither)
600 601 602 603 604
{
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
  g_return_if_fail (gc == NULL || GDK_IS_GC (gc));
  g_return_if_fail (GDK_IS_PIXBUF (pixbuf));

605 606 607
  if (width == 0 || height == 0)
    return;

608 609 610 611 612
  if (width == -1)
    width = gdk_pixbuf_get_width (pixbuf);
  if (height == -1)
    height = gdk_pixbuf_get_height (pixbuf);

613 614
  GDK_DRAWABLE_GET_CLASS (drawable)->draw_pixbuf (drawable, gc,
                                                  (GdkPixbuf *) pixbuf,
615 616
						  src_x, src_y, dest_x, dest_y,
                                                  width, height,
617
						  dither, x_dither, y_dither);
618 619
}

620 621 622 623 624
/**
 * gdk_draw_points:
 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
 * @gc: a #GdkGC.
 * @points: an array of #GdkPoint structures.
625
 * @n_points: the number of points to be drawn.
626 627 628 629
 * 
 * Draws a number of points, using the foreground color and other 
 * attributes of the #GdkGC.
 **/
Elliot Lee's avatar
Elliot Lee committed
630
void
631 632 633 634
gdk_draw_points (GdkDrawable    *drawable,
		 GdkGC          *gc,
		 const GdkPoint *points,
		 gint            n_points)
Elliot Lee's avatar
Elliot Lee committed
635
{
636
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
637
  g_return_if_fail ((points != NULL) && (n_points > 0));
638
  g_return_if_fail (GDK_IS_GC (gc));
639
  g_return_if_fail (n_points >= 0);
640

641
  if (n_points == 0)
642
    return;
Elliot Lee's avatar
Elliot Lee committed
643

644 645
  GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc,
                                                  (GdkPoint *) points, n_points);
Elliot Lee's avatar
Elliot Lee committed
646 647
}

648 649 650 651 652 653
/**
 * gdk_draw_segments:
 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
 * @gc: a #GdkGC.
 * @segs: an array of #GdkSegment structures specifying the start and 
 *   end points of the lines to be drawn.
654
 * @n_segs: the number of line segments to draw, i.e. the size of the 
655 656 657 658
 *   @segs array.
 * 
 * Draws a number of unconnected lines.
 **/
Elliot Lee's avatar
Elliot Lee committed
659
void
660 661 662 663
gdk_draw_segments (GdkDrawable      *drawable,
		   GdkGC            *gc,
		   const GdkSegment *segs,
		   gint              n_segs)
Elliot Lee's avatar
Elliot Lee committed
664
{
665
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
666

667
  if (n_segs == 0)
668
    return;
Elliot Lee's avatar
Elliot Lee committed
669

670
  g_return_if_fail (segs != NULL);
671
  g_return_if_fail (GDK_IS_GC (gc));
672
  g_return_if_fail (n_segs >= 0);
673

674 675
  GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc,
                                                    (GdkSegment *) segs, n_segs);
Elliot Lee's avatar
Elliot Lee committed
676
}
677

678 679 680 681 682
/**
 * gdk_draw_lines:
 * @drawable: a #GdkDrawable (a #GdkWindow or a #GdkPixmap).
 * @gc: a #GdkGC.
 * @points: an array of #GdkPoint structures specifying the endpoints of the
683
 * @n_points: the size of the @points array.
684 685 686 687 688 689
 * 
 * Draws a series of lines connecting the given points.
 * The way in which joins between lines are draw is determined by the
 * #GdkCapStyle value in the #GdkGC. This can be set with
 * gdk_gc_set_line_attributes().
 **/
690
void
691 692 693 694
gdk_draw_lines (GdkDrawable    *drawable,
		GdkGC          *gc,
		const GdkPoint *points,
		gint            n_points)
695
{
696
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
697
  g_return_if_fail (points != NULL);
698
  g_return_if_fail (GDK_IS_GC (gc));
699
  g_return_if_fail (n_points >= 0);
700

701
  if (n_points == 0)
702
    return;
703

704 705
  GDK_DRAWABLE_GET_CLASS (drawable)->draw_lines (drawable, gc,
                                                 (GdkPoint *) points, n_points);
706
}
707

708
static void
709 710 711 712 713 714 715
real_draw_glyphs (GdkDrawable       *drawable,
		  GdkGC	            *gc,
		  const PangoMatrix *matrix,
		  PangoFont         *font,
		  gdouble            x,
		  gdouble            y,
		  PangoGlyphString  *glyphs)
716 717 718
{
  cairo_t *cr;

719
  cr = gdk_cairo_create (drawable);
720
  _gdk_gc_update_context (gc, cr, NULL, NULL, TRUE, drawable);
721 722 723

  if (matrix)
    {
724 725 726 727 728 729 730 731
      cairo_matrix_t cairo_matrix;

      cairo_matrix.xx = matrix->xx;
      cairo_matrix.yx = matrix->yx;
      cairo_matrix.xy = matrix->xy;
      cairo_matrix.yy = matrix->yy;
      cairo_matrix.x0 = matrix->x0;
      cairo_matrix.y0 = matrix->y0;
732
      
733
      cairo_set_matrix (cr, &cairo_matrix);
734 735 736 737 738 739 740 741
    }

  cairo_move_to (cr, x, y);
  pango_cairo_show_glyph_string (cr, font, glyphs);

  cairo_destroy (cr);
}

Havoc Pennington's avatar
Havoc Pennington committed
742 743 744 745 746 747 748
/**
 * gdk_draw_glyphs:
 * @drawable: a #GdkDrawable
 * @gc: a #GdkGC
 * @font: font to be used
 * @x: X coordinate of baseline origin
 * @y: Y coordinate of baseline origin
749
 * @glyphs: the glyph string to draw
Havoc Pennington's avatar
Havoc Pennington committed
750 751 752 753
 *
 * This is a low-level function; 99% of text rendering should be done
 * using gdk_draw_layout() instead.
 *
754
 * A glyph is a single image in a font. This function draws a sequence of
Havoc Pennington's avatar
Havoc Pennington committed
755 756 757 758 759 760
 * glyphs.  To obtain a sequence of glyphs you have to understand a
 * lot about internationalized text handling, which you don't want to
 * understand; thus, use gdk_draw_layout() instead of this function,
 * gdk_draw_layout() handles the details.
 * 
 **/
761 762 763 764 765 766 767 768
void
gdk_draw_glyphs (GdkDrawable      *drawable,
		 GdkGC            *gc,
		 PangoFont        *font,
		 gint              x,
		 gint              y,
		 PangoGlyphString *glyphs)
{
769 770
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
  g_return_if_fail (GDK_IS_GC (gc));
771 772 773
  
  real_draw_glyphs (drawable, gc, NULL, font,
		    x, y, glyphs);
774
}
775

776 777 778 779
/**
 * gdk_draw_glyphs_transformed:
 * @drawable: a #GdkDrawable
 * @gc: a #GdkGC
Johan Dahlin's avatar
Johan Dahlin committed
780
 * @matrix: (allow-none): a #PangoMatrix, or %NULL to use an identity transformation
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
 * @font: the font in which to draw the string
 * @x:       the x position of the start of the string (in Pango
 *           units in user space coordinates)
 * @y:       the y position of the baseline (in Pango units
 *           in user space coordinates)
 * @glyphs:  the glyph string to draw
 * 
 * Renders a #PangoGlyphString onto a drawable, possibly
 * transforming the layed-out coordinates through a transformation
 * matrix. Note that the transformation matrix for @font is not
 * changed, so to produce correct rendering results, the @font
 * must have been loaded using a #PangoContext with an identical
 * transformation matrix to that passed in to this function.
 *
 * See also gdk_draw_glyphs(), gdk_draw_layout().
 *
 * Since: 2.6
 **/
void
800 801 802 803 804 805 806
gdk_draw_glyphs_transformed (GdkDrawable       *drawable,
			     GdkGC	       *gc,
			     const PangoMatrix *matrix,
			     PangoFont         *font,
			     gint               x,
			     gint               y,
			     PangoGlyphString  *glyphs)
807 808 809 810
{
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
  g_return_if_fail (GDK_IS_GC (gc));

811 812
  real_draw_glyphs (drawable, gc, matrix, font,
		    x / PANGO_SCALE, y / PANGO_SCALE, glyphs);
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
}

/**
 * gdk_draw_trapezoids:
 * @drawable: a #GdkDrawable
 * @gc: a #GdkGC
 * @trapezoids: an array of #GdkTrapezoid structures
 * @n_trapezoids: the number of trapezoids to draw
 * 
 * Draws a set of anti-aliased trapezoids. The trapezoids are
 * combined using saturation addition, then drawn over the background
 * as a set. This is low level functionality used internally to implement
 * rotated underlines and backgrouds when rendering a PangoLayout and is
 * likely not useful for applications.
 *
 * Since: 2.6
 **/
void
831 832 833 834
gdk_draw_trapezoids (GdkDrawable        *drawable,
		     GdkGC	        *gc,
		     const GdkTrapezoid *trapezoids,
		     gint                n_trapezoids)
835
{
836 837 838
  cairo_t *cr;
  int i;

839 840 841 842
  g_return_if_fail (GDK_IS_DRAWABLE (drawable));
  g_return_if_fail (GDK_IS_GC (gc));
  g_return_if_fail (n_trapezoids == 0 || trapezoids != NULL);

843
  cr = gdk_cairo_create (drawable);
844
  _gdk_gc_update_context (gc, cr, NULL, NULL, TRUE, drawable);
845 846 847 848 849 850
  
  for (i = 0; i < n_trapezoids; i++)
    {
      cairo_move_to (cr, trapezoids[i].x11, trapezoids[i].y1);
      cairo_line_to (cr, trapezoids[i].x21, trapezoids[i].y1);
      cairo_line_to (cr, trapezoids[i].x22, trapezoids[i].y2);
851
      cairo_line_to (cr, trapezoids[i].x12, trapezoids[i].y2);
852 853 854 855 856 857
      cairo_close_path (cr);
    }

  cairo_fill (cr);

  cairo_destroy (cr);
858
}
859

860
/**
861
 * gdk_drawable_copy_to_image:
862
 * @drawable: a #GdkDrawable
Johan Dahlin's avatar
Johan Dahlin committed
863
 * @image: (allow-none): a #GdkDrawable, or %NULL if a new @image should be created.
864 865 866 867 868 869 870 871 872 873 874 875
 * @src_x: x coordinate on @drawable
 * @src_y: y coordinate on @drawable
 * @dest_x: x coordinate within @image. Must be 0 if @image is %NULL
 * @dest_y: y coordinate within @image. Must be 0 if @image is %NULL
 * @width: width of region to get
 * @height: height or region to get
 *
 * Copies a portion of @drawable into the client side image structure
 * @image. If @image is %NULL, creates a new image of size @width x @height
 * and copies into that. See gdk_drawable_get_image() for further details.
 * 
 * Return value: @image, or a new a #GdkImage containing the contents
876 877 878
 *               of @drawable
 * 
 * Since: 2.4
879 880
 **/
GdkImage*
881 882 883 884 885 886 887 888
gdk_drawable_copy_to_image (GdkDrawable *drawable,
			    GdkImage    *image,
			    gint         src_x,
			    gint         src_y,
			    gint         dest_x,
			    gint         dest_y,
			    gint         width,
			    gint         height)
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
{
  GdkDrawable *composite;
  gint composite_x_offset = 0;
  gint composite_y_offset = 0;
  GdkImage *retval;
  GdkColormap *cmap;
  
  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (src_x >= 0, NULL);
  g_return_val_if_fail (src_y >= 0, NULL);

  /* FIXME? Note race condition since we get the size then
   * get the image, and the size may have changed.
   */
  
  if (width < 0 || height < 0)
    gdk_drawable_get_size (drawable,
                           width < 0 ? &width : NULL,
                           height < 0 ? &height : NULL);
  
  composite =
    GDK_DRAWABLE_GET_CLASS (drawable)->get_composite_drawable (drawable,
                                                               src_x, src_y,
                                                               width, height,
                                                               &composite_x_offset,
                                                               &composite_y_offset); 
  
  retval = GDK_DRAWABLE_GET_CLASS (composite)->_copy_to_image (composite,
							       image,
							       src_x - composite_x_offset,
							       src_y - composite_y_offset,
							       dest_x, dest_y,
							       width, height);

923
  g_object_unref (composite);
924 925 926 927 928 929 930 931 932 933 934 935

  if (!image && retval)
    {
      cmap = gdk_drawable_get_colormap (drawable);
      
      if (cmap)
	gdk_image_set_colormap (retval, cmap);
    }
  
  return retval;
}

Havoc Pennington's avatar
Havoc Pennington committed
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
/**
 * gdk_drawable_get_image:
 * @drawable: a #GdkDrawable
 * @x: x coordinate on @drawable
 * @y: y coordinate on @drawable
 * @width: width of region to get
 * @height: height or region to get
 * 
 * A #GdkImage stores client-side image data (pixels). In contrast,
 * #GdkPixmap and #GdkWindow are server-side
 * objects. gdk_drawable_get_image() obtains the pixels from a
 * server-side drawable as a client-side #GdkImage.  The format of a
 * #GdkImage depends on the #GdkVisual of the current display, which
 * makes manipulating #GdkImage extremely difficult; therefore, in
 * most cases you should use gdk_pixbuf_get_from_drawable() instead of
 * this lower-level function. A #GdkPixbuf contains image data in a
 * canonicalized RGB format, rather than a display-dependent format.
 * Of course, there's a convenience vs. speed tradeoff here, so you'll
 * want to think about what makes sense for your application.
 *
 * @x, @y, @width, and @height define the region of @drawable to
 * obtain as an image.
 *
 * You would usually copy image data to the client side if you intend
 * to examine the values of individual pixels, for example to darken
 * an image or add a red tint. It would be prohibitively slow to
 * make a round-trip request to the windowing system for each pixel,
 * so instead you get all of them at once, modify them, then copy
 * them all back at once.
 *
 * If the X server or other windowing system backend is on the local
 * machine, this function may use shared memory to avoid copying
 * the image data.
969 970 971 972
 *
 * If the source drawable is a #GdkWindow and partially offscreen
 * or obscured, then the obscured portions of the returned image
 * will contain undefined data.
Havoc Pennington's avatar
Havoc Pennington committed
973 974 975
 * 
 * Return value: a #GdkImage containing the contents of @drawable
 **/
Havoc Pennington's avatar
Havoc Pennington committed
976 977 978 979 980 981 982 983 984 985 986
GdkImage*
gdk_drawable_get_image (GdkDrawable *drawable,
                        gint         x,
                        gint         y,
                        gint         width,
                        gint         height)
{
  GdkDrawable *composite;
  gint composite_x_offset = 0;
  gint composite_y_offset = 0;
  GdkImage *retval;
987
  GdkColormap *cmap;
Havoc Pennington's avatar
Havoc Pennington committed
988 989 990 991 992
  
  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (x >= 0, NULL);
  g_return_val_if_fail (y >= 0, NULL);

993 994 995 996
  /* FIXME? Note race condition since we get the size then
   * get the image, and the size may have changed.
   */
  
997 998 999 1000 1001
  if (width < 0 || height < 0)
    gdk_drawable_get_size (drawable,
                           width < 0 ? &width : NULL,
                           height < 0 ? &height : NULL);
  
Havoc Pennington's avatar
Havoc Pennington committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
  composite =
    GDK_DRAWABLE_GET_CLASS (drawable)->get_composite_drawable (drawable,
                                                               x, y,
                                                               width, height,
                                                               &composite_x_offset,
                                                               &composite_y_offset); 
  
  retval = GDK_DRAWABLE_GET_CLASS (composite)->get_image (composite,
                                                          x - composite_x_offset,
                                                          y - composite_y_offset,
                                                          width, height);

1014
  g_object_unref (composite);
Havoc Pennington's avatar
Havoc Pennington committed
1015

1016 1017 1018 1019 1020
  cmap = gdk_drawable_get_colormap (drawable);
  
  if (retval && cmap)
    gdk_image_set_colormap (retval, cmap);
  
Havoc Pennington's avatar
Havoc Pennington committed
1021 1022 1023
  return retval;
}

1024 1025 1026 1027 1028 1029 1030
static GdkImage*
gdk_drawable_real_get_image (GdkDrawable     *drawable,
			     gint             x,
			     gint             y,
			     gint             width,
			     gint             height)
{
1031
  return gdk_drawable_copy_to_image (drawable, NULL, x, y, 0, 0, width, height);
1032 1033
}

1034
static GdkDrawable *
Havoc Pennington's avatar
Havoc Pennington committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
gdk_drawable_real_get_composite_drawable (GdkDrawable *drawable,
                                          gint         x,
                                          gint         y,
                                          gint         width,
                                          gint         height,
                                          gint        *composite_x_offset,
                                          gint        *composite_y_offset)
{
  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);

  *composite_x_offset = 0;
  *composite_y_offset = 0;
  
1048
  return g_object_ref (drawable);
Havoc Pennington's avatar
Havoc Pennington committed
1049
}
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

/**
 * gdk_drawable_get_clip_region:
 * @drawable: a #GdkDrawable
 * 
 * Computes the region of a drawable that potentially can be written
 * to by drawing primitives. This region will not take into account
 * the clip region for the GC, and may also not take into account
 * other factors such as if the window is obscured by other windows,
 * but no area outside of this region will be affected by drawing
 * primitives.
 * 
1062
 * Returns: a #cairo_region_t. This must be freed with cairo_region_destroy()
1063
 *          when you are done.
1064
 **/
1065
cairo_region_t *
1066 1067 1068 1069 1070 1071 1072 1073 1074
gdk_drawable_get_clip_region (GdkDrawable *drawable)
{
  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);

  return GDK_DRAWABLE_GET_CLASS (drawable)->get_clip_region (drawable);
}

/**
 * gdk_drawable_get_visible_region:
1075
 * @drawable: a #GdkDrawable
1076 1077 1078 1079 1080 1081
 * 
 * Computes the region of a drawable that is potentially visible.
 * This does not necessarily take into account if the window is
 * obscured by other windows, but no area outside of this region
 * is visible.
 * 
1082
 * Returns: a #cairo_region_t. This must be freed with cairo_region_destroy()
1083
 *          when you are done.
1084
 **/
1085
cairo_region_t *
1086 1087 1088 1089 1090 1091 1092
gdk_drawable_get_visible_region (GdkDrawable *drawable)
{
  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);

  return GDK_DRAWABLE_GET_CLASS (drawable)->get_visible_region (drawable);
}

1093
static cairo_region_t *
1094 1095 1096 1097 1098 1099 1100 1101 1102
gdk_drawable_real_get_visible_region (GdkDrawable *drawable)
{
  GdkRectangle rect;

  rect.x = 0;
  rect.y = 0;

  gdk_drawable_get_size (drawable, &rect.width, &rect.height);

Benjamin Otte's avatar
Benjamin Otte committed
1103
  return cairo_region_create_rectangle (&rect);
1104
}
1105

1106
/**
1107
 * _gdk_drawable_ref_cairo_surface:
1108 1109
 * @drawable: a #GdkDrawable
 * 
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
 * Obtains a #cairo_surface_t for the given drawable. If a
 * #cairo_surface_t for the drawable already exists, it will be
 * referenced, otherwise a new surface will be created.
 * 
 * Return value: a newly referenced #cairo_surface_t that points
 *  to @drawable. Unref with cairo_surface_destroy()
 **/
cairo_surface_t *
_gdk_drawable_ref_cairo_surface (GdkDrawable *drawable)
{
  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);

  return GDK_DRAWABLE_GET_CLASS (drawable)->ref_cairo_surface (drawable);
}

1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
static void
composite (guchar *src_buf,
	   gint    src_rowstride,
	   guchar *dest_buf,
	   gint    dest_rowstride,
	   gint    width,
	   gint    height)
{
  guchar *src = src_buf;
  guchar *dest = dest_buf;

  while (height--)
    {
      gint twidth = width;
      guchar *p = src;
      guchar *q = dest;

      while (twidth--)
	{
	  guchar a = p[3];
	  guint t;

	  t = a * p[0] + (255 - a) * q[0] + 0x80;
	  q[0] = (t + (t >> 8)) >> 8;
	  t = a * p[1] + (255 - a) * q[1] + 0x80;
	  q[1] = (t + (t >> 8)) >> 8;
	  t = a * p[2] + (255 - a) * q[2] + 0x80;
	  q[2] = (t + (t >> 8)) >> 8;

	  p += 4;
	  q += 3;
	}
      
      src += src_rowstride;
      dest += dest_rowstride;
    }
}