summaryrefslogtreecommitdiffstats
path: root/ksvg/impl/libs/art_support/art_rgba_svp.c
blob: ea2c14766982b5841e48e6163491ed7afc8bb0fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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
344
345
346
347
348
349
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/* Libart_LGPL - library of basic graphic primitives
 * Copyright (C) 1998 Raph Levien
 *
 * This library 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 library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/* Render a sorted vector path into an RGB buffer. */

#include <X11/Xos.h>

#include "art_rgba_svp.h"

#include <libart_lgpl/art_svp.h>
#include <libart_lgpl/art_svp_render_aa.h>
#include <libart_lgpl/art_rgba.h>
#include <libart_lgpl/art_rgb.h>

/* RGBA renderers */

typedef struct _ArtKSVGRgbaSVPAlphaData ArtKSVGRgbaSVPAlphaData;

struct _ArtKSVGRgbaSVPAlphaData {
  int alphatab[256];
  art_u8 r, g, b, alpha;
  art_u32 rgba;
  art_u8 *buf;
	art_u8 *tqmask;
  int rowstride;
  int x0, x1;
	int y0;
};

/**
 * art_rgba_fill_run: fill an RGBA buffer a solid RGB color.
 * @buf: Buffer to fill.
 * @r: Red, range 0..255.
 * @g: Green, range 0..255.
 * @b: Blue, range 0..255.
 * @n: Number of RGB triples to fill.
 *
 * Fills a buffer with @n copies of the (@r, @g, @b) triple, solid
 * alpha. Thus, locations @buf (inclusive) through @buf + 4 * @n
 * (exclusive) are written.
 **/
static void
art_ksvg_rgba_fill_run (art_u8 *buf, art_u8 r, art_u8 g, art_u8 b, int n)
{
	int i;
#if X_BYTE_ORDER == X_BIG_ENDIAN
	art_u32 src_rgba;
#else
	art_u32 src_abgr;
#endif

#if X_BYTE_ORDER == X_BIG_ENDIAN
	src_rgba = (r << 24) | (g << 16) | (b << 8) | 255;
#else
	src_abgr = (255 << 24) | (b << 16) | (g << 8) | r;
#endif
	for(i = 0; i < n; i++)
	{
#if X_BYTE_ORDER == X_BIG_ENDIAN
		((art_u32 *)buf)[i] = src_rgba;
#else
		((art_u32 *)buf)[i] = src_abgr;
#endif
	}    
}

/**
 * art_rgba_run_alpha: Render semitransparent color over RGBA buffer.
 * @buf: Buffer for rendering.
 * @r: Red, range 0..255.
 * @g: Green, range 0..255.
 * @b: Blue, range 0..255.
 * @alpha: Alpha, range 0..255.
 * @n: Number of RGB triples to render.
 *
 * Renders a sequential run of solid (@r, @g, @b) color over @buf with
 * opacity @alpha. Note that the range of @alpha is 0..255, in contrast
 * to art_rgb_run_alpha, which has a range of 0..256.
 **/
static void
art_ksvg_rgba_run_alpha (art_u8 *buf, art_u8 r, art_u8 g, art_u8 b, int alpha, int n)
{
	int i;
	int v;
	int tmp;

	if(alpha > 255)
		alpha = 255;

	for(i = 0; i < n; i++)
	{
		v = *buf;
		tmp = (r - v) * alpha + 0x80;
		*buf++ = v + ((tmp + (tmp >> 8)) >> 8);

		v = *buf;
		tmp = (g - v) * alpha + 0x80;
		*buf++ = v + ((tmp + (tmp >> 8)) >> 8);

		v = *buf;
		tmp = (b - v) * alpha + 0x80;
		*buf++ = v + ((tmp + (tmp >> 8)) >> 8);

		v = *buf;
		tmp = (255 - alpha) * v + 0x80;
		*buf++ = alpha + ((tmp + (tmp >> 8)) >> 8);
	}
}

static void
art_ksvg_rgba_tqmask_run_alpha (art_u8 *buf, art_u8 *tqmask, art_u8 r, art_u8 g, art_u8 b, int alpha, int n)
{
	int i;
	int v;
	int am;
	int tmp;

	if(alpha > 255)
		alpha = 255;

	for(i = 0; i < n; i++)
	{
		am = (alpha * *tqmask++) + 0x80;
		am = (am + (am >> 8)) >> 8;

		v = *buf;
		tmp = (r - v) * am + 0x80;
		*buf++ = v + ((tmp + (tmp >> 8)) >> 8);

		v = *buf;
		tmp = (g - v) * am + 0x80;
		*buf++ = v + ((tmp + (tmp >> 8)) >> 8);

		v = *buf;
		tmp = (b - v) * am + 0x80;
		*buf++ = v + ((tmp + (tmp >> 8)) >> 8);

		v = *buf;
		tmp = (255 - am) * v + 0x80;
		*buf++ = am + ((tmp + (tmp >> 8)) >> 8);
	}
}

static void
art_ksvg_rgba_svp_alpha_callback(void *callback_data, int y,
			    int start, ArtSVPRenderAAStep *steps, int n_steps)
{
  ArtKSVGRgbaSVPAlphaData *data = (ArtKSVGRgbaSVPAlphaData *)callback_data;
  art_u8 *linebuf;
  int run_x0, run_x1;
  art_u32 running_sum = start;
  int x0, x1;
  int k;
  art_u8 r, g, b;
  int *alphatab;
  int alpha;

  linebuf = data->buf;
  x0 = data->x0;
  x1 = data->x1;

  r = data->r;
  g = data->g;
  b = data->b;
  alphatab = data->alphatab;

  if (n_steps > 0)
    {
      run_x1 = steps[0].x;
      if (run_x1 > x0)
	{
	  alpha = (running_sum >> 16) & 0xff;
	  if (alpha)
	    art_ksvg_rgba_run_alpha (linebuf,
			       r, g, b, alphatab[alpha],
			       run_x1 - x0);
	}

      for (k = 0; k < n_steps - 1; k++)
	{
	  running_sum += steps[k].delta;
	  run_x0 = run_x1;
	  run_x1 = steps[k + 1].x;
	  if (run_x1 > run_x0)
	    {
	      alpha = (running_sum >> 16) & 0xff;
	      if (alpha)
		art_ksvg_rgba_run_alpha (linebuf + (run_x0 - x0) * 4,
				   r, g, b, alphatab[alpha],
				   run_x1 - run_x0);
	    }
	}
      running_sum += steps[k].delta;
      if (x1 > run_x1)
	{
	  alpha = (running_sum >> 16) & 0xff;
	  if (alpha)
	    art_ksvg_rgba_run_alpha (linebuf + (run_x1 - x0) * 4,
			       r, g, b, alphatab[alpha],
			       x1 - run_x1);
	}
    }
  else
    {
      alpha = (running_sum >> 16) & 0xff;
      if (alpha)
	art_ksvg_rgba_run_alpha (linebuf,
			   r, g, b, alphatab[alpha],
			   x1 - x0);
    }

  data->buf += data->rowstride;
}

static void
art_ksvg_rgba_svp_alpha_opaque_callback(void *callback_data, int y,
				   int start,
				   ArtSVPRenderAAStep *steps, int n_steps)
{
  ArtKSVGRgbaSVPAlphaData *data = (ArtKSVGRgbaSVPAlphaData *)callback_data;
  art_u8 *linebuf;
  int run_x0, run_x1;
  art_u32 running_sum = start;
  int x0, x1;
  int k;
  art_u8 r, g, b;
  art_u32 rgba;
  int *alphatab;
  int alpha;

  linebuf = data->buf;
  x0 = data->x0;
  x1 = data->x1;

  r = data->r;
  g = data->g;
  b = data->b;
  rgba = data->rgba;
  alphatab = data->alphatab;

  if (n_steps > 0)
    {
      run_x1 = steps[0].x;
      if (run_x1 > x0)
	{
	  alpha = running_sum >> 16;
	  if (alpha)
	    {
	      if (alpha >= 255)
		art_ksvg_rgba_fill_run (linebuf,
				  r, g, b,
				  run_x1 - x0);
	      else
		art_ksvg_rgba_run_alpha (linebuf,
				   r, g, b, alphatab[alpha],
				   run_x1 - x0);
	    }
	}

      for (k = 0; k < n_steps - 1; k++)
	{
	  running_sum += steps[k].delta;
	  run_x0 = run_x1;
	  run_x1 = steps[k + 1].x;
	  if (run_x1 > run_x0)
	    {
	      alpha = running_sum >> 16;
	      if (alpha)
		{
		  if (alpha >= 255)
		    art_ksvg_rgba_fill_run (linebuf + (run_x0 - x0) * 4,
				      r, g, b,
				      run_x1 - run_x0);
		  else
		    art_ksvg_rgba_run_alpha (linebuf + (run_x0 - x0) * 4,
				       r, g, b, alphatab[alpha],
				       run_x1 - run_x0);
		}
	    }
	}
      running_sum += steps[k].delta;
      if (x1 > run_x1)
	{
	  alpha = running_sum >> 16;
	  if (alpha)
	    {
	      if (alpha >= 255)
		art_ksvg_rgba_fill_run (linebuf + (run_x1 - x0) * 4,
				  r, g, b,
				  x1 - run_x1);
	      else
		art_ksvg_rgba_run_alpha (linebuf + (run_x1 - x0) * 4,
				   r, g, b, alphatab[alpha],
				   x1 - run_x1);
	    }
	}
    }
  else
    {
      alpha = running_sum >> 16;
      if (alpha)
	{
	  if (alpha >= 255)
	    art_ksvg_rgba_fill_run (linebuf,
			      r, g, b,
			      x1 - x0);
	  else
	    art_ksvg_rgba_run_alpha (linebuf,
			       r, g, b, alphatab[alpha],
			       x1 - x0);
	}
    }
  data->buf += data->rowstride;
}

static void
art_ksvg_rgba_svp_alpha_tqmask_callback(void *callback_data, int y,
																 int start, ArtSVPRenderAAStep *steps, int n_steps)
{
	ArtKSVGRgbaSVPAlphaData *data = (ArtKSVGRgbaSVPAlphaData *)callback_data;
	art_u8 *linebuf;
	int run_x0, run_x1;
	art_u32 running_sum = start;
	int x0, x1;
	int k;
	art_u8 r, g, b;
	int *alphatab;
	int alpha;
	art_u8 *tqmaskbuf;

	linebuf = data->buf;
	x0 = data->x0;
	x1 = data->x1;

	r = data->r;
	g = data->g;
	b = data->b;
	alphatab = data->alphatab;

	tqmaskbuf = data->tqmask + (y - data->y0) * (data->x1 - data->x0);

	if(n_steps > 0)
	{
		run_x1 = steps[0].x;
		if(run_x1 > x0)
		{
			alpha = (running_sum >> 16) & 0xff;
			if(alpha)
				art_ksvg_rgba_tqmask_run_alpha (linebuf, tqmaskbuf,
													 r, g, b, alphatab[alpha],
													 run_x1 - x0);
		}

		for(k = 0; k < n_steps - 1; k++)
		{
			running_sum += steps[k].delta;
			run_x0 = run_x1;
			run_x1 = steps[k + 1].x;
			if(run_x1 > run_x0)
			{
				alpha = (running_sum >> 16) & 0xff;
				if(alpha)
					art_ksvg_rgba_tqmask_run_alpha (linebuf + (run_x0 - x0) * 4, tqmaskbuf + (run_x0 - x0),
														 r, g, b, alphatab[alpha],
														 run_x1 - run_x0);
			}
		}
		running_sum += steps[k].delta;
		if(x1 > run_x1)
		{
			alpha = (running_sum >> 16) & 0xff;
			if(alpha)
				art_ksvg_rgba_tqmask_run_alpha (linebuf + (run_x1 - x0) * 4, tqmaskbuf + (run_x1 - x0) ,
													 r, g, b, alphatab[alpha],
													 x1 - run_x1);
		}
	}
	else
	{
		alpha = (running_sum >> 16) & 0xff;
		if(alpha)
			art_ksvg_rgba_tqmask_run_alpha (linebuf,	tqmaskbuf,
												 r, g, b, alphatab[alpha],
												 x1 - x0);
	}

	data->buf += data->rowstride;
}

/**
 * art_rgb_svp_alpha: Alpha-composite sorted vector path over RGB buffer.
 * @svp: The source sorted vector path.
 * @x0: Left coordinate of destination rectangle.
 * @y0: Top coordinate of destination rectangle.
 * @x1: Right coordinate of destination rectangle.
 * @y1: Bottom coordinate of destination rectangle.
 * @rgba: Color in 0xRRGGBBAA format.
 * @buf: Destination RGB buffer.
 * @rowstride: Rowstride of @buf buffer.
 * @alphagamma: #ArtAlphaGamma for gamma-correcting the compositing.
 *
 * Renders the tqshape specified with @svp over the @buf RGB buffer.
 * @x1 - @x0 specifies the width, and @y1 - @y0 specifies the height,
 * of the rectangle rendered. The new pixels are stored starting at
 * the first byte of @buf. Thus, the @x0 and @y0 parameters specify
 * an offset within @svp, and may be tweaked as a way of doing
 * integer-pixel translations without fiddling with @svp itself.
 *
 * The @rgba argument specifies the color for the rendering. Pixels of
 * entirely 0 winding number are left untouched. Pixels of entirely
 * 1 winding number have the color @rgba composited over them (ie,
 * are replaced by the red, green, blue components of @rgba if the alpha
 * component is 0xff). Pixels of intermediate coverage are interpolated
 * according to the rule in @alphagamma, or default to linear if
 * @alphagamma is NULL.
 **/
void
art_ksvg_rgba_svp_alpha(const ArtSVP *svp,
		   int x0, int y0, int x1, int y1,
		   art_u32 rgba,
		   art_u8 *buf, int rowstride,
		   ArtAlphaGamma *alphagamma,
			 art_u8 *tqmask)
{
  ArtKSVGRgbaSVPAlphaData data;
  int r, g, b;
  int i;
  int a, da;
	int alpha;

  r = (rgba >> 24) & 0xff;
  g = (rgba >> 16) & 0xff;
  b = (rgba >> 8) & 0xff;
	alpha = rgba & 0xff;

  data.r = r;
  data.g = g;
  data.b = b;
  data.alpha = alpha;
  data.rgba = rgba;
	data.tqmask = tqmask;

  a = 0x8000;
  da = (alpha * 66051 + 0x80) >> 8; /* 66051 equals 2 ^ 32 / (255 * 255) */

  for (i = 0; i < 256; i++)
    {
      data.alphatab[i] = a >> 16;
      a += da;
    }

  data.buf = buf;
  data.rowstride = rowstride;
  data.x0 = x0;
  data.x1 = x1;
	data.y0 = y0;

	if(tqmask)
		art_svp_render_aa (svp, x0, y0, x1, y1, art_ksvg_rgba_svp_alpha_tqmask_callback, &data);
	else
	{
		if (alpha == 255)
			art_svp_render_aa (svp, x0, y0, x1, y1, art_ksvg_rgba_svp_alpha_opaque_callback, &data);
		else
			art_svp_render_aa (svp, x0, y0, x1, y1, art_ksvg_rgba_svp_alpha_callback, &data);
	}
}

/* RGB renderers */

static void
art_ksvg_rgb_tqmask_run_alpha(art_u8 *buf, art_u8 *tqmask, art_u8 r, art_u8 g, art_u8 b, int alpha, int n)
{
	int i;
	int v;
	int am;
	int tmp;

	if(alpha > 255)
		alpha = 255;

	for(i = 0; i < n; i++)
	{
		am = (alpha * *tqmask++) + 0x80;
		am = (am + (am >> 8)) >> 8;

		v = *buf;
		tmp = (r - v) * am + 0x80;
		*buf++ = v + ((tmp + (tmp >> 8)) >> 8);

		v = *buf;
		tmp = (g - v) * am + 0x80;
		*buf++ = v + ((tmp + (tmp >> 8)) >> 8);

		v = *buf;
		tmp = (b - v) * am + 0x80;
		*buf++ = v + ((tmp + (tmp >> 8)) >> 8);
	}
}

static void
art_ksvg_rgb_svp_alpha_tqmask_callback(void *callback_data, int y,
																 int start, ArtSVPRenderAAStep *steps, int n_steps)
{
	ArtKSVGRgbaSVPAlphaData *data = (ArtKSVGRgbaSVPAlphaData *)callback_data;
	art_u8 *linebuf;
	int run_x0, run_x1;
	art_u32 running_sum = start;
	int x0, x1;
	int k;
	art_u8 r, g, b;
	int *alphatab;
	int alpha;
	art_u8 *tqmaskbuf;

	linebuf = data->buf;
	x0 = data->x0;
	x1 = data->x1;

	r = data->r;
	g = data->g;
	b = data->b;
	alphatab = data->alphatab;

	tqmaskbuf = data->tqmask + (y - data->y0) * (data->x1 - data->x0);

	if(n_steps > 0)
	{
		run_x1 = steps[0].x;
		if(run_x1 > x0)
		{
			alpha = (running_sum >> 16) & 0xff;
			if(alpha)
				art_ksvg_rgb_tqmask_run_alpha (linebuf, tqmaskbuf,
													 r, g, b, alphatab[alpha],
													 run_x1 - x0);
		}

		for(k = 0; k < n_steps - 1; k++)
		{
			running_sum += steps[k].delta;
			run_x0 = run_x1;
			run_x1 = steps[k + 1].x;
			if(run_x1 > run_x0)
			{
				alpha = (running_sum >> 16) & 0xff;
				if(alpha)
					art_ksvg_rgb_tqmask_run_alpha (linebuf + (run_x0 - x0) * 3, tqmaskbuf + (run_x0 - x0),
														 r, g, b, alphatab[alpha],
														 run_x1 - run_x0);
			}
		}
		running_sum += steps[k].delta;
		if(x1 > run_x1)
		{
			alpha = (running_sum >> 16) & 0xff;
			if(alpha)
				art_ksvg_rgb_tqmask_run_alpha (linebuf + (run_x1 - x0) * 3, tqmaskbuf + (run_x1 - x0) ,
													 r, g, b, alphatab[alpha],
													 x1 - run_x1);
		}
	}
	else
	{
		alpha = (running_sum >> 16) & 0xff;
		if(alpha)
			art_ksvg_rgb_tqmask_run_alpha (linebuf,	tqmaskbuf,
												 r, g, b, alphatab[alpha],
												 x1 - x0);
	}

	data->buf += data->rowstride;
}

/**
 * art_rgb_svp_alpha: Alpha-composite sorted vector path over RGB buffer.
 * @svp: The source sorted vector path.
 * @x0: Left coordinate of destination rectangle.
 * @y0: Top coordinate of destination rectangle.
 * @x1: Right coordinate of destination rectangle.
 * @y1: Bottom coordinate of destination rectangle.
 * @rgba: Color in 0xRRGGBBAA format.
 * @buf: Destination RGB buffer.
 * @rowstride: Rowstride of @buf buffer.
 * @alphagamma: #ArtAlphaGamma for gamma-correcting the compositing.
 *
 * Renders the tqshape specified with @svp over the @buf RGB buffer.
 * @x1 - @x0 specifies the width, and @y1 - @y0 specifies the height,
 * of the rectangle rendered. The new pixels are stored starting at
 * the first byte of @buf. Thus, the @x0 and @y0 parameters specify
 * an offset within @svp, and may be tweaked as a way of doing
 * integer-pixel translations without fiddling with @svp itself.
 *
 * The @rgba argument specifies the color for the rendering. Pixels of
 * entirely 0 winding number are left untouched. Pixels of entirely
 * 1 winding number have the color @rgba composited over them (ie,
 * are replaced by the red, green, blue components of @rgba if the alpha
 * component is 0xff). Pixels of intermediate coverage are interpolated
 * according to the rule in @alphagamma, or default to linear if
 * @alphagamma is NULL.
 **/
void
art_ksvg_rgb_svp_alpha_tqmask(const ArtSVP *svp,
												int x0, int y0, int x1, int y1,
												art_u32 rgba,
												art_u8 *buf, int rowstride,
												ArtAlphaGamma *alphagamma,
												art_u8 *tqmask)
{
	ArtKSVGRgbaSVPAlphaData data;
	int r, g, b, alpha;
	int i;
	int a, da;

	r = rgba >> 24;
	g = (rgba >> 16) & 0xff;
	b = (rgba >> 8) & 0xff;
	alpha = rgba & 0xff;

	data.r = r;
	data.g = g;
	data.b = b;
	data.alpha = alpha;
	data.tqmask = tqmask;

	a = 0x8000;
	da = (alpha * 66051 + 0x80) >> 8;	/* 66051 equals 2 ^ 32 / (255 * 255) */

	for(i = 0; i < 256; i++)
	{
		data.alphatab[i] = a >> 16;
		a += da;
	}

	data.buf = buf;
	data.rowstride = rowstride;
	data.x0 = x0;
	data.x1 = x1;
	data.y0 = y0;

	art_svp_render_aa(svp, x0, y0, x1, y1, art_ksvg_rgb_svp_alpha_tqmask_callback, &data);
}