rsvg-filter.c 129 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* vim: set sw=4: -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* 
   rsvg-filter.c: Provides filters
 
   Copyright (C) 2004 Caleb Moore
  
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.
  
   This program 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.
  
   You should have received a copy of the GNU Library General Public
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
  
22
   Author: Caleb Moore <c.moore@student.unsw.edu.au>
23 24
*/

Dom Lachowicz's avatar
Dom Lachowicz committed
25 26
#include "config.h"

27 28 29
#include "rsvg-private.h"
#include "rsvg-filter.h"
#include "rsvg-styles.h"
30
#include "rsvg-image.h"
31
#include "rsvg-css.h"
Dom Lachowicz's avatar
Dom Lachowicz committed
32
#include <string.h>
33 34

#include <math.h>
Dom Lachowicz's avatar
Dom Lachowicz committed
35

36 37 38 39

/*************************************************************/
/*************************************************************/

Caleb Michael Moore's avatar
Caleb Michael Moore committed
40 41 42 43 44
typedef struct _RsvgFilterPrimitiveOutput RsvgFilterPrimitiveOutput;

struct _RsvgFilterPrimitiveOutput
{
	GdkPixbuf *result;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
45
	RsvgIRect bounds;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
46 47 48 49 50 51
	gboolean Rused;
	gboolean Gused;
	gboolean Bused;
	gboolean Aused;
};

52 53 54 55 56 57 58 59 60
typedef struct _RsvgFilterContext RsvgFilterContext;

struct _RsvgFilterContext
{
	gint width, height;
	RsvgFilter *filter;
	GHashTable *results;
	GdkPixbuf *source;
	GdkPixbuf *bg;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
61
	RsvgFilterPrimitiveOutput lastresult;
62 63
	double affine[6];
	double paffine[6];
64
	int channelmap[4];
65
	RsvgDrawingCtx * ctx;
66 67 68 69 70 71
};

typedef struct _RsvgFilterPrimitive RsvgFilterPrimitive;

struct _RsvgFilterPrimitive
{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
72
	RsvgNode super;
73
	RsvgLength x, y, width, height;
74 75
	GString *in;
	GString *result;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
76
   
77 78 79 80 81 82 83 84 85 86 87 88 89
	void (*render) (RsvgFilterPrimitive * self, RsvgFilterContext * ctx);
};

/*************************************************************/
/*************************************************************/

static void
rsvg_filter_primitive_render (RsvgFilterPrimitive * self,
			      RsvgFilterContext * ctx)
{
	self->render (self, ctx);
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
90
static RsvgIRect
91
rsvg_filter_primitive_get_bounds (RsvgFilterPrimitive * self,
92
								  RsvgFilterContext * ctx)
93
{
94 95
	RsvgBbox box, otherbox;
	double affine[6];
96

97 98 99 100
	_rsvg_affine_identity(affine);
	rsvg_bbox_init(&box, affine);
	rsvg_bbox_init(&otherbox, ctx->affine);
	otherbox.virgin = 0;
101 102
	if (ctx->filter->filterunits == objectBoundingBox)
		_rsvg_push_view_box(ctx->ctx, 1., 1.);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
103
	otherbox.x = _rsvg_css_normalize_length(&ctx->filter->x, 
104
												   ctx->ctx, 'h');
Caleb Michael Moore's avatar
Caleb Michael Moore committed
105
	otherbox.y = _rsvg_css_normalize_length(&ctx->filter->y, 
106
												   ctx->ctx, 'v');
Caleb Michael Moore's avatar
Caleb Michael Moore committed
107
	otherbox.w = _rsvg_css_normalize_length(&ctx->filter->width, 
108
												   ctx->ctx, 'h');
Caleb Michael Moore's avatar
Caleb Michael Moore committed
109
	otherbox.h = _rsvg_css_normalize_length(&ctx->filter->height, 
110 111 112
												   ctx->ctx, 'v');
	if (ctx->filter->filterunits == objectBoundingBox)
		_rsvg_pop_view_box(ctx->ctx);
113

114
	rsvg_bbox_insert(&box, &otherbox);
115

116 117 118 119 120 121 122 123 124 125
	if (self != NULL)
		if (self->x.factor != 'n' || self->y.factor != 'n' || 
			self->width.factor != 'n' || self->height.factor != 'n')
			{
				
				rsvg_bbox_init(&otherbox, ctx->paffine);
				otherbox.virgin = 0;
				if (ctx->filter->primitiveunits == objectBoundingBox)
					_rsvg_push_view_box(ctx->ctx, 1., 1.);
				if (self->x.factor != 'n')
Caleb Michael Moore's avatar
Caleb Michael Moore committed
126
					otherbox.x = _rsvg_css_normalize_length(&self->x, 
127 128 129 130
																   ctx->ctx, 'h');
				else
					otherbox.x = 0;
				if (self->y.factor != 'n')
Caleb Michael Moore's avatar
Caleb Michael Moore committed
131
					otherbox.y = _rsvg_css_normalize_length(&self->y, 
132 133 134 135
																   ctx->ctx, 'v');
				else
					otherbox.y = 0;
				if (self->width.factor != 'n')
Caleb Michael Moore's avatar
Caleb Michael Moore committed
136
					otherbox.w = _rsvg_css_normalize_length(&self->width, 
137 138 139 140
																   ctx->ctx, 'h');
				else
					otherbox.w = ctx->ctx->vb.w;
				if (self->height.factor != 'n')
Caleb Michael Moore's avatar
Caleb Michael Moore committed
141
					otherbox.h = _rsvg_css_normalize_length(&self->height, 
142 143 144 145 146 147 148 149
																   ctx->ctx, 'v');
				else
					otherbox.h = ctx->ctx->vb.h;
				if (ctx->filter->primitiveunits == objectBoundingBox)
					_rsvg_pop_view_box(ctx->ctx);
				rsvg_bbox_clip(&box, &otherbox);
			}
	
150 151 152 153 154 155 156 157 158 159 160 161 162
	rsvg_bbox_init(&otherbox, affine);
	otherbox.virgin = 0;
	otherbox.x = 0;
	otherbox.y = 0;
	otherbox.w = ctx->width;
	otherbox.h = ctx->height;
	rsvg_bbox_clip(&box, &otherbox);
	{
		RsvgIRect output = {box.x, box.y, 
							box.x + box.w, 
							box.y + box.h};
		return output;
	}
163 164
}

165 166 167
GdkPixbuf *
_rsvg_pixbuf_new_cleared (GdkColorspace colorspace, gboolean has_alpha, int bits_per_sample,
						  int width, int height)
168 169 170 171 172 173 174 175 176 177 178 179
{
	GdkPixbuf *pb;
	guchar *data;

	pb = gdk_pixbuf_new (colorspace, has_alpha, bits_per_sample, width, height);
	data = gdk_pixbuf_get_pixels (pb);
	memset(data, 0, width * height * 4);

	return pb;
}

static guchar
Caleb Michael Moore's avatar
Caleb Michael Moore committed
180
gdk_pixbuf_get_interp_pixel(guchar * src, gdouble ox, gdouble oy, guchar ch, RsvgIRect boundarys, guint rowstride)
181 182 183 184 185 186 187 188 189 190 191 192 193
{
	double xmod, ymod;
	double dist1, dist2, dist3, dist4;
	double c, c1, c2, c3, c4;

	xmod = fmod(ox, 1.0);
	ymod = fmod(oy, 1.0);

	dist1 = (1 - xmod) * (1 - ymod);
	dist2 = (xmod) * (1 - ymod);
	dist3 = (xmod) * (ymod);
	dist4 = (1 - xmod) * (ymod);

Caleb Michael Moore's avatar
Caleb Michael Moore committed
194 195
	if (floor(ox) <= boundarys.x0 || floor(ox) >= boundarys.x1 || 
		floor(oy) <= boundarys.y0 || floor(oy) >= boundarys.y1)
196 197 198 199
		c1 = 0;
	else
		c1 = src[(guint)floor(oy) * rowstride + (guint)floor(ox) * 4 + ch];

Caleb Michael Moore's avatar
Caleb Michael Moore committed
200 201
	if (ceil(ox) <= boundarys.x0 || ceil(ox) >= boundarys.x1 || 
		floor(oy) <= boundarys.y0 || floor(oy) >= boundarys.y1)
202 203 204 205
		c2 = 0;
	else
		c2 = src[(guint)floor(oy) * rowstride + (guint)ceil(ox) * 4 + ch];

Caleb Michael Moore's avatar
Caleb Michael Moore committed
206 207
	if (ceil(ox) <= boundarys.x0 || ceil(ox) >= boundarys.x1 || 
		ceil(oy) <= boundarys.y0 || ceil(oy) >= boundarys.y1)
208 209 210 211
		c3 = 0;
	else
		c3 = src[(guint)ceil(oy) * rowstride + (guint)ceil(ox) * 4 + ch];
	
Caleb Michael Moore's avatar
Caleb Michael Moore committed
212 213
	if (floor(ox) <= boundarys.x0 || floor(ox) >= boundarys.x1 || 
		ceil(oy) <= boundarys.y0 || ceil(oy) >= boundarys.y1)
214 215 216 217 218 219 220 221 222 223
		c4 = 0;
	else
		c4 = src[(guint)ceil(oy) * rowstride + (guint)floor(ox) * 4 + ch];

	c = (c1 * dist1 + c2 * dist2 + c3 * dist3 + c4 * dist4) / (dist1 + dist2 + dist3 + dist4);

	return (guchar)c;
}

static void
Caleb Michael Moore's avatar
Caleb Michael Moore committed
224
rsvg_filter_fix_coordinate_system (RsvgFilterContext * ctx, RsvgState * state,
225
								   RsvgBbox bbox)
226
{
227 228
	int x, y, height, width;
	int i;
229

230 231 232 233
	x = bbox.x;
	y = bbox.y;
	width = bbox.w;
	height = bbox.h;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
234

235 236 237 238 239 240
	ctx->width = gdk_pixbuf_get_width(ctx->source);
	ctx->height = gdk_pixbuf_get_height(ctx->source);

	for (i = 0; i < 6; i++)
		ctx->affine[i] = state->affine[i];
	if (ctx->filter->filterunits == objectBoundingBox)
241
		{
242 243
			double affine[6] = {width, 0, 0, height, x, y};
			_rsvg_affine_multiply(ctx->affine, affine, ctx->affine);
244
		}
245 246 247
	for (i = 0; i < 6; i++)
		ctx->paffine[i] = state->affine[i];	
	if (ctx->filter->primitiveunits == objectBoundingBox)
248
		{
249 250
			double affine[6] = {width, 0, 0, height, x, y};
			_rsvg_affine_multiply(ctx->paffine, affine, ctx->paffine);
251 252 253
		}
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
void
rsvg_alpha_blt (GdkPixbuf * src, gint srcx, gint srcy, gint srcwidth,
				gint srcheight, GdkPixbuf * dst, gint dstx, gint dsty)
{
	gint rightx;
	gint bottomy;
	gint dstwidth;
	gint dstheight;
	
	gint srcoffsetx;
	gint srcoffsety;
	gint dstoffsetx;
	gint dstoffsety;
	
	gint x, y, srcrowstride, dstrowstride, sx, sy, dx, dy;
	guchar *src_pixels, *dst_pixels;
	
	dstheight = srcheight;
	dstwidth = srcwidth;
	
	rightx = srcx + srcwidth;
	bottomy = srcy + srcheight;
	
	if (rightx > gdk_pixbuf_get_width (src))
		rightx = gdk_pixbuf_get_width (src);
	if (bottomy > gdk_pixbuf_get_height (src))
		bottomy = gdk_pixbuf_get_height (src);
	srcwidth = rightx - srcx;
	srcheight = bottomy - srcy;
	
	rightx = dstx + dstwidth;
	bottomy = dsty + dstheight;
	if (rightx > gdk_pixbuf_get_width (dst))
		rightx = gdk_pixbuf_get_width (dst);
	if (bottomy > gdk_pixbuf_get_height (dst))
		bottomy = gdk_pixbuf_get_height (dst);
	dstwidth = rightx - dstx;
	dstheight = bottomy - dsty;
	
	if (dstwidth < srcwidth)
		srcwidth = dstwidth;
	if (dstheight < srcheight)
		srcheight = dstheight;
	
	if (srcx < 0)
		srcoffsetx = 0 - srcx;
	else
		srcoffsetx = 0;

	if (srcy < 0)
		srcoffsety = 0 - srcy;
	else
		srcoffsety = 0;

	if (dstx < 0)
		dstoffsetx = 0 - dstx;
	else
		dstoffsetx = 0;

	if (dsty < 0)
		dstoffsety = 0 - dsty;
	else
		dstoffsety = 0;
	
	if (dstoffsetx > srcoffsetx)
		srcoffsetx = dstoffsetx;
	if (dstoffsety > srcoffsety)
		srcoffsety = dstoffsety;
	
	srcrowstride = gdk_pixbuf_get_rowstride (src);
	dstrowstride = gdk_pixbuf_get_rowstride (dst);
	
	src_pixels = gdk_pixbuf_get_pixels (src);
	dst_pixels = gdk_pixbuf_get_pixels (dst);
	
	for (y = srcoffsety; y < srcheight; y++)
		for (x = srcoffsetx; x < srcwidth; x++)
			{
				guint a, c, ad, cd, ar, cr, i;

				sx = x + srcx;
				sy = y + srcy;
				dx = x + dstx;
				dy = y + dsty;
				a = src_pixels[4 * sx + sy * srcrowstride + 3];

				if (a)
					{
						ad = dst_pixels[4 * dx + dy * dstrowstride + 3];
						ar = a + ad * (255 - a) / 255;
344
						dst_pixels[4 * dx + dy * dstrowstride + 3] = ar;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
345 346
						for (i = 0; i < 3; i++)
							{
347 348 349
								c = src_pixels[4 * sx + sy * srcrowstride + i];
								cd = dst_pixels[4 * dx + dy * dstrowstride + i];
								cr = c + cd * (255 - a) / 255;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
								dst_pixels[4 * dx + dy * dstrowstride + i] = cr;
							}
					}
			}
}

void
rsvg_art_affine_image(const GdkPixbuf *img, GdkPixbuf *intermediate, 
					  double * affine, double w, double h)
{
	gdouble tmp_affine[6];
	gdouble inv_affine[6];
	gdouble raw_inv_affine[6];
	gint intstride;
	gint basestride;	
	gint basex, basey;
	gdouble fbasex, fbasey;
	gdouble rawx, rawy;
	guchar * intpix;
	guchar * basepix;
	gint i, j, k, basebpp, ii, jj;
	gboolean has_alpha;
	gdouble pixsum[4];
	gboolean xrunnoff, yrunnoff;
	gint iwidth, iheight;
	gint width, height;

	width = gdk_pixbuf_get_width (img);
	height = gdk_pixbuf_get_height (img);
	iwidth = gdk_pixbuf_get_width (intermediate);
	iheight = gdk_pixbuf_get_height (intermediate);

	has_alpha = gdk_pixbuf_get_has_alpha (img);

	basestride = gdk_pixbuf_get_rowstride (img);
	intstride = gdk_pixbuf_get_rowstride (intermediate);
	basepix = gdk_pixbuf_get_pixels (img);
	intpix = gdk_pixbuf_get_pixels (intermediate);
	basebpp = has_alpha ? 4 : 3;

	_rsvg_affine_invert(raw_inv_affine, affine);

	/*scale to w and h*/
	tmp_affine[0] = (double)w;
	tmp_affine[3] = (double)h;
	tmp_affine[1] = tmp_affine[2] = tmp_affine[4] = tmp_affine[5] = 0;
	_rsvg_affine_multiply(tmp_affine, tmp_affine, affine);

	_rsvg_affine_invert(inv_affine, tmp_affine);


	/*apply the transformation*/
	for (i = 0; i < iwidth; i++)
		for (j = 0; j < iheight; j++)		
			{
				fbasex = (inv_affine[0] * (double)i + inv_affine[2] * (double)j + 
						  inv_affine[4]) * (double)width;
				fbasey = (inv_affine[1] * (double)i + inv_affine[3] * (double)j + 
						  inv_affine[5]) * (double)height;
				basex = floor(fbasex);
				basey = floor(fbasey);
				rawx = raw_inv_affine[0] * i + raw_inv_affine[2] * j + 
					raw_inv_affine[4];
				rawy = raw_inv_affine[1] * i + raw_inv_affine[3] * j + 
					raw_inv_affine[5];
				if (rawx < 0 || rawy < 0 || rawx >= w || 
					rawy >= h || basex < 0 || basey < 0 
					|| basex >= width || basey >= height)
					{					
						for (k = 0; k < 4; k++)
							intpix[i * 4 + j * intstride + k] = 0;
					}
				else
					{
						if (basex < 0 || basex + 1 >= width)
							xrunnoff = TRUE;
						else
							xrunnoff = FALSE;
						if (basey < 0 || basey + 1 >= height)
							yrunnoff = TRUE;
						else
							yrunnoff = FALSE;
						for (k = 0; k < basebpp; k++)
							pixsum[k] = 0;
						for (ii = 0; ii < 2; ii++)
							for (jj = 0; jj < 2; jj++)
								{
									if (basex + ii < 0 || basey + jj< 0 
										|| basex + ii >= width || basey + jj >= height)
										;
									else
										{
											for (k = 0; k < basebpp; k++)
												{
													pixsum[k] += 
														(double)basepix[basebpp * (basex + ii) + (basey + jj) * basestride + k] 
														* (xrunnoff ? 1 : fabs(fbasex - (double)(basex + (1 - ii))))
														* (yrunnoff ? 1 : fabs(fbasey - (double)(basey + (1 - jj))));
												}
										}
								}
						for (k = 0; k < basebpp; k++)
							intpix[i * 4 + j * intstride + k] = pixsum[k];
						if (!has_alpha)
							intpix[i * 4 + j * intstride + 3] = 255;
					}	

			}
}

460
static void
461
rsvg_filter_free_pair (gpointer value)
462
{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
463
	RsvgFilterPrimitiveOutput * output;
464 465

	output = (RsvgFilterPrimitiveOutput *)value;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
466 467
	g_object_unref (G_OBJECT (output->result));
	g_free (output);
468 469 470 471 472 473 474 475 476 477 478 479
}

/**
 * rsvg_filter_render: Copy the source to the bg using a filter.
 * @self: a pointer to the filter to use
 * @source: a pointer to the source pixbuf
 * @bg: the background pixbuf
 * @context: the context
 *
 * This function will create a context for itself, set up the coordinate systems
 * execute all its little primatives and then clean up its own mess
 **/
Caleb Michael Moore's avatar
Caleb Michael Moore committed
480 481 482
GdkPixbuf *
rsvg_filter_render (RsvgFilter * self, GdkPixbuf * source,
					GdkPixbuf * bg, RsvgDrawingCtx * context, 
483
					RsvgBbox * bounds, char * channelmap)
484 485 486 487
{
	RsvgFilterContext *ctx;
	RsvgFilterPrimitive *current;
	guint i;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
488 489
	GdkPixbuf * out;	

490

491 492 493 494
	ctx = g_new (RsvgFilterContext, 1);
	ctx->filter = self;
	ctx->source = source;
	ctx->bg = bg;
495
	ctx->results = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, rsvg_filter_free_pair);
496 497 498
	ctx->ctx = context;	

	g_object_ref (G_OBJECT (source));
Caleb Michael Moore's avatar
Caleb Michael Moore committed
499

Caleb Michael Moore's avatar
Caleb Michael Moore committed
500 501
	rsvg_filter_fix_coordinate_system (ctx, rsvg_state_current (context), 
									   *bounds);
502

Caleb Michael Moore's avatar
Caleb Michael Moore committed
503 504 505 506 507 508 509
	ctx->lastresult.result = source;
	ctx->lastresult.Rused = 1;
	ctx->lastresult.Gused = 1;
	ctx->lastresult.Bused = 1;
	ctx->lastresult.Aused = 1;
	ctx->lastresult.bounds = rsvg_filter_primitive_get_bounds (NULL, ctx);	

510 511 512
	for (i = 0; i < 4; i++)
		ctx->channelmap[i] = channelmap[i] - '0';

513
	for (i = 0; i < self->super.children->len; i++)
514
		{
515
			current = g_ptr_array_index (self->super.children, i);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
516
			if (!strncmp(current->super.type->str, "fe", 2))
517
				rsvg_filter_primitive_render (current, ctx);
518 519
		}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
520
	out = ctx->lastresult.result;
521

Caleb Michael Moore's avatar
Caleb Michael Moore committed
522
	g_hash_table_destroy (ctx->results);
523

524
	g_free(ctx);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
525 526

	return out;
527 528 529 530 531 532 533 534 535 536 537 538
}

/**
 * rsvg_filter_store_result: Files a result into a context.
 * @name: The name of the result
 * @result: The pointer to the result
 * @ctx: the context that this was called in
 *
 * Puts the new result into the hash for easy finding later, also
 * Stores it as the last result
 **/
static void
Caleb Michael Moore's avatar
Caleb Michael Moore committed
539
rsvg_filter_store_output(GString * name, RsvgFilterPrimitiveOutput result,
540 541
						  RsvgFilterContext * ctx)
{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
542 543
	RsvgFilterPrimitiveOutput * store;

Dom Lachowicz's avatar
Dom Lachowicz committed
544 545
	g_object_unref (G_OBJECT (ctx->lastresult.result));
	
Caleb Michael Moore's avatar
Caleb Michael Moore committed
546 547 548
	store = g_new(RsvgFilterPrimitiveOutput, 1);
	*store = result;

549 550
	if (strcmp (name->str, ""))
		{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
551 552
			g_object_ref (G_OBJECT (result.result));	/* increments the references for the table */
			g_hash_table_insert (ctx->results, g_strdup (name->str), store);
553 554
		}
	
Caleb Michael Moore's avatar
Caleb Michael Moore committed
555
	g_object_ref (G_OBJECT (result.result));	/* increments the references for the last result */
556 557 558
	ctx->lastresult = result;
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
559 560 561 562 563 564 565 566 567
static void
rsvg_filter_store_result(GString * name, GdkPixbuf * result,
						  RsvgFilterContext * ctx)
{
	RsvgFilterPrimitiveOutput output;
	output.Rused = 1;
	output.Gused = 1;
	output.Bused = 1;
	output.Aused = 1;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
568 569 570 571
	output.bounds.x0 = 0;
	output.bounds.y0 = 0;
	output.bounds.x1 = ctx->width;
	output.bounds.y1 = ctx->height;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
572 573 574 575 576
	output.result = result;

	rsvg_filter_store_output(name, output, ctx);
}

577
static GdkPixbuf *
578
pixbuf_get_alpha (GdkPixbuf * pb, RsvgFilterContext * ctx)
579 580 581 582 583 584 585 586 587
{
	guchar *data;
	guchar *pbdata;
	GdkPixbuf *output;
	
	gsize i, pbsize;

	pbsize = gdk_pixbuf_get_width (pb) * gdk_pixbuf_get_height (pb);

588 589 590
	output = _rsvg_pixbuf_new_cleared (GDK_COLORSPACE_RGB, 1, 8,
									   gdk_pixbuf_get_width (pb),
									   gdk_pixbuf_get_height (pb));
591 592 593 594 595
	
	data = gdk_pixbuf_get_pixels (output);
	pbdata = gdk_pixbuf_get_pixels (pb);
	
	for (i = 0; i < pbsize; i++)
596
		data[i * 4 + ctx->channelmap[3]] = pbdata[i * 4 + ctx->channelmap[3]];
597 598 599 600 601 602 603 604 605 606 607 608
	
	return output;
}

/**
 * rsvg_filter_get_in: Gets a pixbuf for a primative.
 * @name: The name of the pixbuf
 * @ctx: the context that this was called in
 *
 * Returns a pointer to the result that the name refers to, a special
 * Pixbuf if the name is a special keyword or NULL if nothing was found
 **/
Caleb Michael Moore's avatar
Caleb Michael Moore committed
609 610
static RsvgFilterPrimitiveOutput
rsvg_filter_get_result (GString * name, RsvgFilterContext * ctx)
611
{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
612 613
	RsvgFilterPrimitiveOutput output;
	RsvgFilterPrimitiveOutput * outputpointer;
614 615
	output.bounds.x0 = output.bounds.x1 = 
		output.bounds.y0 = output.bounds.y1 = 0;
616 617 618 619

	if (!strcmp (name->str, "SourceGraphic"))
		{
			g_object_ref (G_OBJECT (ctx->source));
Caleb Michael Moore's avatar
Caleb Michael Moore committed
620 621 622
			output.result = ctx->source;
			output.Rused = output.Gused = output.Bused = output.Aused = 1;
			return output;
623 624 625 626
		}
	else if (!strcmp (name->str, "BackgroundImage"))
		{
			g_object_ref (G_OBJECT (ctx->bg));
Caleb Michael Moore's avatar
Caleb Michael Moore committed
627 628 629
			output.result = ctx->bg;
			output.Rused = output.Gused = output.Bused = output.Aused = 1;
			return output;
630
		}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
631
	else if (!strcmp (name->str, "") || !strcmp (name->str, "none") || !name)
632
		{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
633 634 635
			g_object_ref (G_OBJECT (ctx->lastresult.result));
			output = ctx->lastresult;
			return output;
636 637
		}
	else if (!strcmp (name->str, "SourceAlpha"))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
638 639 640
		{
			output.Rused = output.Gused = output.Bused = 0;
			output.Aused = 1;
641
		    output.result = pixbuf_get_alpha (ctx->source, ctx);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
642 643
			return output;
		}
644
	else if (!strcmp (name->str, "BackgroundAlpha"))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
645 646 647
		{
			output.Rused = output.Gused = output.Bused = 0;
			output.Aused = 1;
648
			output.result = pixbuf_get_alpha (ctx->bg, ctx);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
649 650 651 652 653 654 655 656 657
			return output;
		}	

	outputpointer = (RsvgFilterPrimitiveOutput*)(g_hash_table_lookup (ctx->results, name->str));

	if (outputpointer != NULL)
		{
			output = *outputpointer;		
			g_object_ref (G_OBJECT (output.result));
658
			return output;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
659 660
		}

Dom Lachowicz's avatar
Dom Lachowicz committed
661
	g_warning(_("%s not found\n"),name->str);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
662 663 664 665 666
	
	output = ctx->lastresult;
	g_object_ref (G_OBJECT (ctx->lastresult.result));
	return output;
}
667

Caleb Michael Moore's avatar
Caleb Michael Moore committed
668 669 670 671 672

static GdkPixbuf *
rsvg_filter_get_in (GString * name, RsvgFilterContext * ctx)
{
	return rsvg_filter_get_result (name, ctx).result;
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
}

/**
 * rsvg_filter_parse: Looks up an allready created filter.
 * @defs: a pointer to the hash of definitions
 * @str: a string with the name of the filter to be looked up
 *
 * Returns a pointer to the filter that the name refers to, or NULL
 * if none was found
 **/
RsvgFilter *
rsvg_filter_parse (const RsvgDefs * defs, const char *str)
{
	if (!strncmp (str, "url(", 4))
		{
			const char *p = str + 4;
			int ix;
			char *name;
691
			RsvgNode *val;
692 693 694 695
			
			while (g_ascii_isspace (*p))
				p++;

696 697 698 699 700
			for (ix = 0; p[ix]; ix++)
				if (p[ix] == ')')
					break;
			
			if (p[ix] == ')')
701
				{
702 703 704 705
					name = g_strndup (p, ix);
					val = rsvg_defs_lookup (defs, name);
					g_free (name);
					
Caleb Michael Moore's avatar
Caleb Michael Moore committed
706 707
					if (val)
						if (!strcmp(val->type->str, "filter"))
708
						return (RsvgFilter *) val;
709 710 711 712 713 714
				}
		}
	
	return NULL;
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
715 716
static void
rsvg_filter_set_args (RsvgNode * self, RsvgHandle * ctx, RsvgPropertyBag * atts)
717
{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
718
	const char *value;
719
	RsvgFilter *filter;
720

Caleb Michael Moore's avatar
Caleb Michael Moore committed
721
	filter = (RsvgFilter *)self;
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
	
	if (rsvg_property_bag_size (atts))
		{
			if ((value = rsvg_property_bag_lookup (atts, "filterUnits")))
				{
					if (!strcmp (value, "userSpaceOnUse"))
						filter->filterunits = userSpaceOnUse;
					else
						filter->filterunits = objectBoundingBox;
				}
			if ((value = rsvg_property_bag_lookup (atts, "primitiveUnits")))
				{
					if (!strcmp (value, "objectBoundingBox"))
						filter->primitiveunits = objectBoundingBox;
					else
						filter->primitiveunits = userSpaceOnUse;
				}
			if ((value = rsvg_property_bag_lookup (atts, "x")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
740
				filter->x =	_rsvg_css_parse_length(value);
741
			if ((value = rsvg_property_bag_lookup (atts, "y")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
742
				filter->y =	_rsvg_css_parse_length(value);
743
			if ((value = rsvg_property_bag_lookup (atts, "width")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
744
				filter->width =	_rsvg_css_parse_length(value);
745
			if ((value = rsvg_property_bag_lookup (atts, "height")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
746
				filter->height = _rsvg_css_parse_length(value);
747
			if ((value = rsvg_property_bag_lookup (atts, "id")))
748
				rsvg_defs_register_name (ctx->priv->defs, value, &filter->super);
749
		}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
750 751 752 753 754 755 756 757 758 759 760 761 762
}

/**
 * rsvg_new_filter: Creates a black filter
 *
 * Creates a blank filter and assigns default values to everything
 **/
RsvgNode *
rsvg_new_filter (void)
{
	RsvgFilter *filter;

	filter = g_new (RsvgFilter, 1);
763
	_rsvg_node_init(&filter->super);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
764 765
	filter->filterunits = objectBoundingBox;
	filter->primitiveunits = userSpaceOnUse;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
766 767 768 769
	filter->x = _rsvg_css_parse_length("-10%");
	filter->y = _rsvg_css_parse_length("-10%");
	filter->width = _rsvg_css_parse_length("120%");
	filter->height = _rsvg_css_parse_length("120%");
770
	filter->super.children = g_ptr_array_new ();
Caleb Michael Moore's avatar
Caleb Michael Moore committed
771
	filter->super.set_atts = rsvg_filter_set_args;
772
	return (RsvgNode *)filter;
773 774 775 776 777 778 779
}

/*************************************************************/
/*************************************************************/

typedef enum
{
780 781 782
	normal, multiply, screen, darken, lighten, softlight, 
	hardlight, colordodge, colorburn, overlay, exclusion,
	difference
783 784 785 786 787 788 789 790 791 792 793
}
RsvgFilterPrimitiveBlendMode;

typedef struct _RsvgFilterPrimitiveBlend RsvgFilterPrimitiveBlend;
struct _RsvgFilterPrimitiveBlend
{
	RsvgFilterPrimitive super;
	RsvgFilterPrimitiveBlendMode mode;
	GString *in2;
};

794
static void rsvg_filter_blend(RsvgFilterPrimitiveBlendMode mode, GdkPixbuf *in, GdkPixbuf *in2, GdkPixbuf *output, RsvgIRect boundarys, int * channelmap)
795 796 797
{
	guchar i;
	gint x, y;
798
	gint rowstride, rowstride2, rowstrideo, height, width;
799 800 801 802 803 804
	guchar *in_pixels;
	guchar *in2_pixels;
	guchar *output_pixels;
	height = gdk_pixbuf_get_height (in);
	width = gdk_pixbuf_get_width (in);
	rowstride = gdk_pixbuf_get_rowstride (in);
805 806
	rowstride2 = gdk_pixbuf_get_rowstride (in2);
	rowstrideo = gdk_pixbuf_get_rowstride (output);
807

808
	output_pixels = gdk_pixbuf_get_pixels (output);
809 810 811
	in_pixels = gdk_pixbuf_get_pixels (in);
	in2_pixels = gdk_pixbuf_get_pixels (in2);

Caleb Michael Moore's avatar
Caleb Michael Moore committed
812 813 814 815 816 817 818 819
	if (boundarys.x0 < 0)
		boundarys.x0 = 0;
	if (boundarys.y0 < 0)
		boundarys.y1 = 0;
	if (boundarys.x1 >= width)
		boundarys.x1 = width;
	if (boundarys.y1 >= height)
		boundarys.y1 = height;
820
		
Caleb Michael Moore's avatar
Caleb Michael Moore committed
821 822
	for (y = boundarys.y0; y < boundarys.y1; y++)
		for (x = boundarys.x0; x < boundarys.x1; x++)
823
			{
824
				double qr, cr, qa, qb, ca, cb, bca, bcb;
825 826 827 828
				int ch;				

				qa = (double) in_pixels[4 * x + y * rowstride + channelmap[3]] / 255.0;
				qb = (double) in2_pixels[4 * x + y * rowstride2 + channelmap[3]] / 255.0;
829 830
				qr = 1 - (1 - qa) * (1 - qb);
				cr = 0;
831
				for (ch = 0; ch < 3; ch++)
832
					{
833
						i = channelmap[ch];
834 835
						ca = (double) in_pixels[4 * x + y * rowstride + i] / 255.0;
						cb = (double) in2_pixels[4 * x + y * rowstride2 + i] / 255.0;
836 837 838
						/*these are the ca and cb that are used in the non-standard blend functions*/
						bcb = (1 - qa) * cb + ca;
						bca = (1 - qb) * ca + cb;
839
						switch (mode)
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
							{
							case normal:
								cr = (1 - qa) * cb + ca;
								break;
							case multiply:
								cr = (1 - qa) * cb + (1 - qb) * ca + ca * cb;
								break;
							case screen:
								cr = cb + ca - ca * cb;
								break;
							case darken:
								cr = MIN ((1 - qa) * cb + ca, (1 - qb) * ca + cb);
								break;
							case lighten:
								cr = MAX ((1 - qa) * cb + ca, (1 - qb) * ca + cb);
								break;
856
							case softlight:
857 858
								if (bcb < 0.5)
									cr = 2 * bca * bcb + bca * bca * (1 - 2 * bcb);
859
								else
860
									cr = sqrt(bca)*(2*bcb-1)+(2*bca)*(1-bcb);
861 862 863
								break;
							case hardlight:
								if (cb < 0.5)
864
									cr = 2 * bca * bcb;
865
								else
866
									cr = 1 - 2 * (1 - bca) * (1 - bcb);
867 868
								break;
							case colordodge:
869
								if (bcb == 1)
870 871
									cr = 1;
								else
872
									cr = MIN(bca / (1 - bcb), 1);
873 874
								break;
							case colorburn:
875
								if (bcb == 0)
876 877
									cr = 0;
								else
878
									cr = MAX(1 - (1 - bca) / bcb, 0);
879 880
								break;
							case overlay:
881 882
								if (bca < 0.5)
									cr = 2 * bca * bcb;
883
								else
884
									cr = 1 - 2 * (1 - bca) * (1 - bcb);
885 886
								break;
							case exclusion:
887
								cr = bca + bcb - 2 * bca * bcb;
888 889
								break;
							case difference:
890
								cr = abs(bca - bcb);
891
								break;
892
							}
893
						cr *= 255.0;
894 895 896 897
						if (cr > 255)
							cr = 255;
						if (cr < 0)
							cr = 0;
898
						output_pixels[4 * x + y * rowstrideo + i] = (guchar) cr;
899 900
						
					}
901
				output_pixels[4 * x + y * rowstrideo + channelmap[3]] = qr * 255.0;
902
			}
903 904 905 906 907 908 909
}


static void
rsvg_filter_primitive_blend_render (RsvgFilterPrimitive * self,
									RsvgFilterContext * ctx)
{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
910
	RsvgIRect boundarys;
911
	
912
	RsvgFilterPrimitiveBlend *upself;
913 914 915 916 917
	
	GdkPixbuf *output;
	GdkPixbuf *in;
	GdkPixbuf *in2;
	
918
	upself = (RsvgFilterPrimitiveBlend *) self;
919 920 921
	boundarys = rsvg_filter_primitive_get_bounds (self, ctx);
	
	in = rsvg_filter_get_in (self->in, ctx);
922
	in2 = rsvg_filter_get_in (upself->in2, ctx);
923
	
924
	output = _rsvg_pixbuf_new_cleared (GDK_COLORSPACE_RGB, 1, 8, gdk_pixbuf_get_width (in), gdk_pixbuf_get_height (in));
925
	
926
	rsvg_filter_blend(upself->mode, in, in2, output, boundarys, ctx->channelmap);
927

928 929 930 931 932 933 934
	rsvg_filter_store_result (self->result, output, ctx);
	
	g_object_unref (G_OBJECT (in));
	g_object_unref (G_OBJECT (in2));
	g_object_unref (G_OBJECT (output));
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
935
void rsvg_filter_adobe_blend(gint modenum, GdkPixbuf *in, GdkPixbuf *bg, GdkPixbuf *output, RsvgIRect boundarys, RsvgDrawingCtx * ctx)
936
{
937
	int standardmap[4] = {0, 1, 2, 3};
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 969 970 971 972 973 974 975 976 977 978 979 980
	RsvgFilterPrimitiveBlendMode mode;
	mode = normal;

	switch(modenum)
		{
		case 0:
			mode = normal;
			break;
		case 1:
			mode = multiply; 
			break;
		case 2:
			mode = screen;
			break;
		case 3:
			mode = darken; 
			break;
		case 4:
			mode = lighten;
			break;
		case 5:
			mode = softlight; 
			break;
		case 6:
			mode = hardlight;
			break;
		case 7:
			mode = colordodge; 
			break;
		case 8:
			mode = colorburn;
			break;
		case 9:
			mode = overlay; 
			break;
		case 10:
			mode = exclusion;
			break;
		case 11:
			mode = difference; 
			break;
		}

981
	rsvg_filter_blend(mode, in, bg, output, boundarys, standardmap);
982 983
}

984
static void
Caleb Michael Moore's avatar
Caleb Michael Moore committed
985
rsvg_filter_primitive_blend_free (RsvgNode * self)
986
{
987 988 989 990 991 992
	RsvgFilterPrimitiveBlend *upself;	
	upself = (RsvgFilterPrimitiveBlend *) self;
	g_string_free (upself->super.result, TRUE);
	g_string_free (upself->super.in, TRUE);
	g_string_free (upself->in2, TRUE);
	_rsvg_node_free(self);
993 994
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
995 996
static void
rsvg_filter_primitive_blend_set_atts (RsvgNode * node, RsvgHandle * ctx, RsvgPropertyBag * atts)
997
{
Dom Lachowicz's avatar
Dom Lachowicz committed
998
	const char *value;
999 1000
	RsvgFilterPrimitiveBlend *filter;

Caleb Michael Moore's avatar
Caleb Michael Moore committed
1001
	filter = (RsvgFilterPrimitiveBlend *)node;
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	
	if (rsvg_property_bag_size (atts))
		{
			if ((value = rsvg_property_bag_lookup (atts, "mode")))
			{
				if (!strcmp (value, "multiply"))
					filter->mode = multiply;
				else if (!strcmp (value, "screen"))
					filter->mode = screen;
				else if (!strcmp (value, "darken"))
					filter->mode = darken;
				else if (!strcmp (value, "lighten"))
					filter->mode = lighten;
				else
					filter->mode = normal;
			}
			if ((value = rsvg_property_bag_lookup (atts, "in")))
				g_string_assign (filter->super.in, value);					
			if ((value = rsvg_property_bag_lookup (atts, "in2")))
				g_string_assign (filter->in2, value);					
			if ((value = rsvg_property_bag_lookup (atts, "result")))
				g_string_assign (filter->super.result, value);					
			if ((value = rsvg_property_bag_lookup (atts, "x")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1025
				filter->super.x = _rsvg_css_parse_length(value);
1026
			if ((value = rsvg_property_bag_lookup (atts, "y")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1027
				filter->super.y = _rsvg_css_parse_length(value);
1028
			if ((value = rsvg_property_bag_lookup (atts, "width")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1029
				filter->super.width = _rsvg_css_parse_length(value);
1030
			if ((value = rsvg_property_bag_lookup (atts, "height")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1031
				filter->super.height = _rsvg_css_parse_length(value);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1032
			if ((value = rsvg_property_bag_lookup (atts, "id")))
1033
				rsvg_defs_register_name (ctx->priv->defs, value, &filter->super.super);
1034
		}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1035 1036 1037 1038 1039 1040 1041
}

RsvgNode *
rsvg_new_filter_primitive_blend (void)
{
	RsvgFilterPrimitiveBlend *filter;
	filter = g_new (RsvgFilterPrimitiveBlend, 1);
1042
	_rsvg_node_init(&filter->super.super);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1043 1044 1045 1046
	filter->mode = normal;
	filter->super.in = g_string_new ("none");
	filter->in2 = g_string_new ("none");
	filter->super.result = g_string_new ("none");
1047
	filter->super.x.factor = filter->super.y.factor = filter->super.width.factor = filter->super.height.factor = 'n';
1048
	filter->super.render = &rsvg_filter_primitive_blend_render;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1049
	filter->super.super.free = &rsvg_filter_primitive_blend_free;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1050
	filter->super.super.set_atts = rsvg_filter_primitive_blend_set_atts;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1051
	return (RsvgNode *)filter;
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
}

/*************************************************************/
/*************************************************************/

typedef struct _RsvgFilterPrimitiveConvolveMatrix RsvgFilterPrimitiveConvolveMatrix;

struct _RsvgFilterPrimitiveConvolveMatrix
{
	RsvgFilterPrimitive super;
	double *KernelMatrix;
	double divisor;
	gint orderx, ordery;
	double dx, dy;
	double bias;
	gint targetx, targety;
	gboolean preservealpha;
	gint edgemode;
};

static void
rsvg_filter_primitive_convolve_matrix_render (RsvgFilterPrimitive * self,
											  RsvgFilterContext * ctx)
{
	guchar ch;
	gint x, y;
	gint i, j;
	gint rowstride, height, width;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1080
	RsvgIRect boundarys;
1081 1082 1083 1084
	
	guchar *in_pixels;
	guchar *output_pixels;
	
1085
	RsvgFilterPrimitiveConvolveMatrix *upself;
1086 1087 1088 1089 1090 1091 1092
	
	GdkPixbuf *output;
	GdkPixbuf *in;
	
	gint sx, sy, kx, ky;
	guchar sval;
	double kval, sum, dx, dy, targetx, targety;
1093
	int umch;
1094 1095 1096
	
	gint tempresult;
	
1097
	upself = (RsvgFilterPrimitiveConvolveMatrix *) self;
1098 1099 1100 1101 1102 1103 1104 1105
	boundarys = rsvg_filter_primitive_get_bounds (self, ctx);
	
	in = rsvg_filter_get_in (self->in, ctx);
	in_pixels = gdk_pixbuf_get_pixels (in);
	
	height = gdk_pixbuf_get_height (in);
	width = gdk_pixbuf_get_width (in);
	
1106 1107
	targetx = upself->targetx * ctx->paffine[0];
	targety = upself->targety * ctx->paffine[3];
1108

1109
	if (upself->dx != 0 || upself->dy != 0)
1110
		{
1111 1112
			dx = upself->dx * ctx->paffine[0];
			dy = upself->dy * ctx->paffine[3];
1113 1114 1115 1116 1117 1118
		}
	else
		dx = dy = 1;

	rowstride = gdk_pixbuf_get_rowstride (in);
	
1119
	output = _rsvg_pixbuf_new_cleared (GDK_COLORSPACE_RGB, 1, 8, width, height);
1120 1121
	output_pixels = gdk_pixbuf_get_pixels (output);
	
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1122 1123
	for (y = boundarys.y0; y < boundarys.y1; y++)
		for (x = boundarys.x0; x < boundarys.x1; x++)
1124
			{
1125
				for (umch = 0; umch < 3 + !upself->preservealpha; umch++)
1126
					{
1127
						ch = ctx->channelmap[umch];
1128
						sum = 0;
1129 1130
						for (i = 0; i < upself->ordery; i++)
							for (j = 0; j < upself->orderx; j++)
1131
								{
1132
									int alpha;
1133 1134
									sx = x - targetx + j * dx;
									sy = y - targety + i * dy;
1135
									if (upself->edgemode == 0)
1136
										{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1137 1138 1139 1140 1141 1142 1143 1144
											if (sx < boundarys.x0)
												sx = boundarys.x0;
											if (sx >= boundarys.x1)
												sx = boundarys.x1 - 1;
											if (sy < boundarys.y0)
												sy = boundarys.y0;
											if (sy >= boundarys.y1)
												sy = boundarys.y1 - 1;
1145
										}
1146
									else if (upself->edgemode == 1)
1147
										{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1148 1149 1150 1151 1152 1153
											if (sx < boundarys.x0 || (sx >= boundarys.x1))
												sx = boundarys.x0 + (sx - boundarys.x0) %
													(boundarys.x1 - boundarys.x0);
											if (sy < boundarys.y0 || (sy >= boundarys.y1))
												sy = boundarys.y0 + (sy - boundarys.y0) %
													(boundarys.y1 - boundarys.y0);
1154
										}
1155
									else if (upself->edgemode == 2)
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1156 1157
										if (sx < boundarys.x0 || (sx >= boundarys.x1) || 
											sy < boundarys.y0 || (sy >= boundarys.y1))
1158 1159
										continue;

1160 1161
									kx = upself->orderx - j - 1;
									ky = upself->ordery - i - 1;
1162 1163 1164 1165 1166 1167 1168 1169
									alpha = in_pixels[4 * sx + sy * rowstride + 3];
									if (ch == 3)
										sval = alpha;
									else if (alpha)
										sval = in_pixels[4 * sx + sy * rowstride + ch] * 255 / 
											alpha;
									else
										sval = 0;
1170
									kval = upself->KernelMatrix[kx + ky * upself->orderx];
1171 1172
									sum += (double) sval *kval;
								}
1173
						tempresult = sum / upself->divisor + upself->bias;
1174 1175 1176 1177 1178 1179 1180 1181

						if (tempresult > 255)
							tempresult = 255;
						if (tempresult < 0)
							tempresult = 0;
						
						output_pixels[4 * x + y * rowstride + ch] = tempresult;
					}
1182
				if (upself->preservealpha)
1183 1184 1185 1186 1187 1188 1189 1190 1191
					output_pixels[4 * x + y * rowstride + ctx->channelmap[3]] =
						in_pixels[4 * x + y * rowstride + ctx->channelmap[3]];
				for (umch = 0; umch < 3; umch++)
					{
						ch = ctx->channelmap[umch];
						output_pixels[4 * x + y * rowstride + ch] =
							output_pixels[4 * x + y * rowstride + ch] *
							output_pixels[4 * x + y * rowstride + ctx->channelmap[3]] / 255;
					}
1192 1193 1194 1195 1196 1197 1198 1199
			}
	rsvg_filter_store_result (self->result, output, ctx);
	
	g_object_unref (G_OBJECT (in));
	g_object_unref (G_OBJECT (output));
}

static void
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1200
rsvg_filter_primitive_convolve_matrix_free (RsvgNode * self)
1201
{
1202
	RsvgFilterPrimitiveConvolveMatrix *upself;
1203

1204 1205 1206 1207 1208
	upself = (RsvgFilterPrimitiveConvolveMatrix *) self;
	g_string_free (upself->super.result, TRUE);
	g_string_free (upself->super.in, TRUE);
	g_free (upself->KernelMatrix);
	_rsvg_node_free(self);
1209 1210
}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
1211 1212 1213 1214
static void
rsvg_filter_primitive_convolve_matrix_set_atts (RsvgNode * self,
												RsvgHandle * ctx,
												RsvgPropertyBag * atts)
1215
{
1216 1217
	gint i, j;
	guint listlen;
Dom Lachowicz's avatar
Dom Lachowicz committed
1218
	const char *value;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1219
	gboolean has_target_x, has_target_y;
1220 1221
	RsvgFilterPrimitiveConvolveMatrix *filter;
	
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1222
	filter = (RsvgFilterPrimitiveConvolveMatrix*)self;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1223 1224
	has_target_x = 0;
	has_target_y = 0;
1225 1226 1227 1228 1229 1230 1231 1232

	if (rsvg_property_bag_size (atts))
		{
			if ((value = rsvg_property_bag_lookup (atts, "in")))
				g_string_assign (filter->super.in, value);
			if ((value = rsvg_property_bag_lookup (atts, "result")))
				g_string_assign (filter->super.result, value);
			if ((value = rsvg_property_bag_lookup (atts, "x")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1233
				filter->super.x = _rsvg_css_parse_length(value);
1234
			if ((value = rsvg_property_bag_lookup (atts, "y")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1235
				filter->super.y = _rsvg_css_parse_length(value);
1236
			if ((value = rsvg_property_bag_lookup (atts, "width")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1237
				filter->super.width = _rsvg_css_parse_length(value);
1238
			if ((value = rsvg_property_bag_lookup (atts, "height")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1239
				filter->super.height = _rsvg_css_parse_length(value);
1240
			if ((value = rsvg_property_bag_lookup (atts, "targetX")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1241 1242 1243 1244
				{
					has_target_x = 1;
					filter->targetx = atoi (value);
				}
1245
			if ((value = rsvg_property_bag_lookup (atts, "targetY")))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1246 1247 1248 1249
				{
					has_target_y = 1;
					filter->targety = atoi (value);
				}
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
			if ((value = rsvg_property_bag_lookup (atts, "bias")))
				filter->bias = atof (value);
			if ((value = rsvg_property_bag_lookup (atts, "preserveAlpha")))
				{
					if (!strcmp (value, "true"))
						filter->preservealpha = TRUE;
					else
						filter->preservealpha = FALSE;
				}
			if ((value = rsvg_property_bag_lookup (atts, "divisor")))
				filter->divisor = atof (value);					
			if ((value = rsvg_property_bag_lookup (atts, "order")))
				{
					double tempx, tempy;
					rsvg_css_parse_number_optional_number (value,
														   &tempx, &tempy);
					filter->orderx = tempx;
					filter->ordery = tempy;
					
				}
			if ((value = rsvg_property_bag_lookup (atts, "kernelUnitLength")))
				rsvg_css_parse_number_optional_number (value,
													   &filter->dx, &filter->dy);
							
			if ((value = rsvg_property_bag_lookup (atts, "kernelMatrix")))
				filter->KernelMatrix =
					rsvg_css_parse_number_list (value, &listlen);
			
			if ((value = rsvg_property_bag_lookup (atts, "edgeMode"))) 
				{
					if (!strcmp (value, "wrap"))
						filter->edgemode = 1;
					else if (!strcmp (value, "none"))
						filter->edgemode = 2;
					else
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1285
						filter->edgemode = 0;
1286
				}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1287
			if ((value = rsvg_property_bag_lookup (atts, "id")))
1288
				rsvg_defs_register_name (ctx->priv->defs, value, &filter->super.super);
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
		}

	if (filter->divisor == 0)
		{
			for (j = 0; j < filter->orderx; j++)
				for (i = 0; i < filter->ordery; i++)
					filter->divisor += filter->KernelMatrix[j + i * filter->orderx];
		}

	if (filter->divisor == 0)
		filter->divisor = 1;
		
1301
	if ((gint)listlen < filter->orderx * filter->ordery)
1302 1303
		filter->orderx = filter->ordery = 0;

Caleb Michael Moore's avatar
Caleb Michael Moore committed
1304 1305 1306 1307 1308 1309 1310 1311
	if (!has_target_x)
		{
			filter->targetx = floor(filter->orderx / 2);
		}
	if (!has_target_y)
		{
			filter->targety = floor(filter->ordery / 2);
		}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1312
}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1313

Caleb Michael Moore's avatar
Caleb Michael Moore committed
1314 1315 1316 1317 1318
RsvgNode *
rsvg_new_filter_primitive_convolve_matrix(void)
{
	RsvgFilterPrimitiveConvolveMatrix *filter;
	filter = g_new (RsvgFilterPrimitiveConvolveMatrix, 1);	
1319
	_rsvg_node_init(&filter->super.super);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1320 1321
	filter->super.in = g_string_new ("none");
	filter->super.result = g_string_new ("none");
1322
	filter->super.x.factor = filter->super.y.factor = filter->super.width.factor = filter->super.height.factor = 'n';		
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1323 1324 1325 1326 1327 1328
	filter->divisor = 0;
	filter->bias = 0;
	filter->dx = 0;
	filter->dy = 0;
	filter->preservealpha = FALSE;	
	filter->edgemode = 0;
1329
	filter->super.render = &rsvg_filter_primitive_convolve_matrix_render;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1330
	filter->super.super.free = &rsvg_filter_primitive_convolve_matrix_free;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1331
	filter->super.super.set_atts = rsvg_filter_primitive_convolve_matrix_set_atts;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1332
	return (RsvgNode *)filter;
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
}

/*************************************************************/
/*************************************************************/

typedef struct _RsvgFilterPrimitiveGaussianBlur
RsvgFilterPrimitiveGaussianBlur;

struct _RsvgFilterPrimitiveGaussianBlur
{
	RsvgFilterPrimitive super;
	double sdx, sdy;
};

static void
1348
box_blur (GdkPixbuf *in, GdkPixbuf *output, guchar *intermediate, gint kw, 
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1349
		  gint kh, RsvgIRect boundarys, RsvgFilterPrimitiveOutput op)
1350
{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1351
	gint ch;
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
	gint x, y;
	gint rowstride, height, width;
	
	guchar *in_pixels;
	guchar *output_pixels;

	gint sum;	

	
	height = gdk_pixbuf_get_height (in);
	width = gdk_pixbuf_get_width (in);

	in_pixels = gdk_pixbuf_get_pixels (in);
1365
	output_pixels = gdk_pixbuf_get_pixels (output);
1366 1367 1368
	
	rowstride = gdk_pixbuf_get_rowstride (in);

Caleb Michael Moore's avatar
Caleb Michael Moore committed
1369 1370
	if (kw > boundarys.x1 - boundarys.x0)
		kw = boundarys.x1 - boundarys.x0;
1371

Caleb Michael Moore's avatar
Caleb Michael Moore committed
1372 1373
	if (kh > boundarys.y1 - boundarys.y0)
		kh = boundarys.y1 - boundarys.y0;
1374

1375 1376 1377 1378 1379

	if (kw >= 1)	
		{
			for (ch = 0; ch < 4; ch++)
				{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
					switch (ch)
						{	
						case 0:
							if (!op.Rused)
								continue;
						case 1:
							if (!op.Gused)
								continue;						
						case 2:
							if (!op.Bused)
								continue;
						case 3:
							if (!op.Aused)
								continue;
						}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1395
					for (y = boundarys.y0; y < boundarys.y1; y++)
1396 1397
						{
							sum = 0;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1398
							for (x = boundarys.x0; x < boundarys.x0 + kw; x++)
1399
								{
1400
									sum += (intermediate[x % kw] = in_pixels[4 * x + y * rowstride + ch]);
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1401

Caleb Michael Moore's avatar
Caleb Michael Moore committed
1402
									if (x - kw / 2 >= 0 && x - kw / 2 < boundarys.x1)
1403
										output_pixels[4 * (x - kw / 2) + y * rowstride + ch] = sum / kw;
1404
								}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1405
							for (x = boundarys.x0 + kw; x < boundarys.x1; x++)
1406
								{
1407 1408
									sum -= intermediate[x % kw];
									sum += (intermediate[x % kw] = in_pixels[4 * x + y * rowstride + ch]);
1409
									output_pixels[4 * (x - kw / 2) + y * rowstride + ch] = sum / kw;
1410
								}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1411
							for (x = boundarys.x1; x < boundarys.x1 + kw; x++)
1412
								{
1413
									sum -= intermediate[x % kw];
1414

Caleb Michael Moore's avatar
Caleb Michael Moore committed
1415
									if (x - kw / 2 >= 0 && x - kw / 2 < boundarys.x1)
1416
										output_pixels[4 * (x - kw / 2) + y * rowstride + ch] = sum / kw;
1417 1418 1419
								}
						}
				}
1420
			in_pixels = output_pixels;
1421 1422 1423 1424 1425 1426
		}
	
	if (kh >= 1)
		{
			for (ch = 0; ch < 4; ch++)
				{
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
					switch (ch)
						{	
						case 0:
							if (!op.Rused)
								continue;
						case 1:
							if (!op.Gused)
								continue;						
						case 2:
							if (!op.Bused)
								continue;
						case 3:
							if (!op.Aused)
								continue;
						}
				

Caleb Michael Moore's avatar
Caleb Michael Moore committed
1444
					for (x = boundarys.x0; x < boundarys.x1; x++)
1445 1446 1447
						{
							sum = 0;
							
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1448
							for (y = boundarys.y0; y < boundarys.y0 + kh; y++)
1449
								{
1450
									sum += (intermediate[y % kh] = in_pixels[4 * x + y * rowstride + ch]);
1451
   
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1452
									if (y - kh / 2 >= 0 && y - kh / 2 < boundarys.y1)
1453
										output_pixels[4 * x + (y - kh / 2) * rowstride + ch] = sum / kh;
1454
								}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1455
							for (; y < boundarys.y1; y++)
1456
								{
1457 1458
									sum -= intermediate[y % kh];
									sum += (intermediate[y % kh] = in_pixels[4 * x + y * rowstride + ch]);