testart.c 14 KB
Newer Older
Raph Levien's avatar
Raph Levien committed
1
/* Libart_LGPL - library of basic graphic primitives
2
 * Copyright (C) 1998, 1999 Raph Levien
Raph Levien's avatar
Raph Levien committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20 21 22
#include <stdio.h>
#include <string.h>
#include <math.h>
Raph Levien's avatar
Raph Levien committed
23
#include "art_misc.h"
24 25 26
#include "art_vpath.h"
#include "art_svp.h"
#include "art_svp_vpath.h"
27 28
#include "art_gray_svp.h"
#include "art_rgb_svp.h"
29 30
#include "art_svp_vpath_stroke.h"
#include "art_svp_ops.h"
31 32 33 34
#include "art_affine.h"
#include "art_rgb_affine.h"
#include "art_rgb_bitmap_affine.h"
#include "art_rgb_rgba_affine.h"
35
#include "art_alphagamma.h"
36
#include "art_svp_point.h"
37
#include "art_vpath_dash.h"
38 39 40
#include "art_render.h"
#include "art_render_gradient.h"
#include "art_render_svp.h"
41
#include "art_svp_intersect.h"
42

43 44
#ifdef DEAD_CODE
static void
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
test_affine (void) {
  double src[6];
  double dst[6];
  double src2[6];
  char str[128];
  int i;
  ArtPoint ps, pd, ptmp;

  for (i = 0; i < 6; i++)
    {
      src[i] = (rand () * 2.0 / RAND_MAX) - 1.0;
      src2[i] = (rand () * 2.0 / RAND_MAX) - 1.0;
    }
#if 0
  src[0] = 0.9999999;
  src[1] = -0.000001;
  src[2] = 0.000001;
  src[3] = 0.9999999;
  src[4] = 0;
  src[5] = 0;
#if 1
  src[0] = 0.98480775;
  src[1] = -0.17364818;
  src[2] = 0.17364818;
  src[3] = 0.98480775;
#endif

  src2[0] = 0.98480775;
  src2[1] = -0.17364818;
  src2[2] = 0.17364818;
  src2[3] = 0.98480775;
#endif


  ps.x = rand() * 100.0 / RAND_MAX;
  ps.y = rand() * 100.0 / RAND_MAX;

  art_affine_point (&pd, &ps, src);
  art_affine_invert (dst, src);
  art_affine_point (&ptmp, &pd, dst);
  art_affine_to_string (str, src);
  printf ("src = %s\n", str);
  art_affine_to_string (str, dst);
  printf ("dst = %s\n", str);
  printf ("point (%g, %g) -> (%g, %g) -> (%g, %g)\n",
	  ps.x, ps.y, pd.x, pd.y, ptmp.x, ptmp.y);

  art_affine_point (&ptmp, &ps, src);
  art_affine_point (&pd, &ptmp, src2);
  art_affine_to_string (str, src2);
  printf ("src2 = %s\n", str);
  printf ("point (%g, %g) -> (%g, %g) -> (%g, %g)\n",
	  ps.x, ps.y, ptmp.x, ptmp.y, pd.x, pd.y);
  art_affine_multiply (dst, src, src2);
  art_affine_to_string (str, dst);
  printf ("dst = %s\n", str);
  art_affine_point (&pd, &ps, dst);
  printf ("point (%g, %g) -> (%g, %g)\n",
	  ps.x, ps.y, pd.x, pd.y);

}
106
#endif
107

108
static ArtVpath *
109 110 111 112 113 114 115 116 117 118 119
randstar (int n)
{
  ArtVpath *vec;
  int i;
  double r, th;

  vec = art_new (ArtVpath, n + 2);
  for (i = 0; i < n; i++)
    {
      vec[i].code = i ? ART_LINETO : ART_MOVETO;
      r = rand () * (250.0 / RAND_MAX);
120 121 122
#if 0
      r = r + 0.9 * (250 - r);
#endif
123 124 125 126 127 128 129 130 131 132 133 134 135
      th = i * 2 * M_PI / n;
      vec[i].x = 250 + r * cos (th);
      vec[i].y = 250 - r * sin (th);
    }
  vec[i].code = ART_LINETO;
  vec[i].x = vec[0].x;
  vec[i].y = vec[0].y;
  i++;
  vec[i].code = ART_END;
  vec[i].x = 0;
  vec[i].y = 0;
  return vec;
}
Raph Levien's avatar
Raph Levien committed
136

137
#define TILE_SIZE 512
138
#define NUM_ITERS 1
139 140 141 142 143 144 145 146
#define COLOR

#ifdef COLOR
#define BYTES_PP 3
#else
#define BYTES_PP 1
#endif

147
#ifndef nDEAD_CODE
148
static void
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
print_svp (ArtSVP *vp)
{
  int i, j;

  for (i = 0; i < vp->n_segs; i++)
    {
      printf ("segment %d, dir = %s (%f, %f) - (%f, %f)\n",
	      i, vp->segs[i].dir ? "down" : "up",
	      vp->segs[i].bbox.x0,
	      vp->segs[i].bbox.y0,
	      vp->segs[i].bbox.x1,
	      vp->segs[i].bbox.y1);
      for (j = 0; j < vp->segs[i].n_points; j++)
        printf ("  (%g, %g)\n",
                vp->segs[i].points[j].x,
                vp->segs[i].points[j].y);
    }
}
167
#endif
168

169
static void
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
print_vpath (ArtVpath *vpath)
{
  int i;

  for (i = 0; vpath[i].code != ART_END; i++)
    printf ("%g %g %s\n",
	    vpath[i].x, vpath[i].y,
	    vpath[i].code == ART_MOVETO_OPEN ? "moveto %open" :
	    vpath[i].code == ART_MOVETO ? "moveto" :
	    vpath[i].code == ART_LINETO ? "lineto" :
	    "?");

  printf ("stroke\n");
}

185
static void
186
make_testpat (void)
Raph Levien's avatar
Raph Levien committed
187
{
188
  ArtVpath *vpath, *vpath2, *vpath3;
189
  ArtSVP *svp, *svp2;
190
  ArtSVP *svp3;
191 192 193
  art_u8 buf[512 * 512 * BYTES_PP];
  int i, j;
  int iter;
194 195 196 197 198 199 200
  art_u8 colorimg[256][256][3];
  art_u8 rgbaimg[256][256][4];
  art_u8 bitimg[16][2];
  int x, y;
  double affine[6];
  double affine2[6];
  double affine3[6];
201
  ArtAlphaGamma *alphagamma;
202
  double dash_data[] = { 20 };
203 204 205 206 207
  ArtVpathDash dash;

  dash.offset = 0;
  dash.n_dash = 1;
  dash.dash = dash_data;
208

209 210 211 212
#ifdef TEST_AFFINE
  test_affine ();
  exit (0);
#endif
213

214 215
  vpath = randstar (50);
  svp = art_svp_from_vpath (vpath);
216
  art_free (vpath);
217 218

  vpath2 = randstar (50);
219
#if 1
220 221 222
  vpath3 = art_vpath_dash (vpath2, &dash);
  art_free (vpath2);
  svp2 = art_svp_vpath_stroke (vpath3,
223 224 225 226 227
			       ART_PATH_STROKE_JOIN_MITER,
			       ART_PATH_STROKE_CAP_BUTT,
			       15,
			       4,
			       0.5);
228
  art_free (vpath3);
229 230 231
#else
  svp2 = art_svp_from_vpath (vpath2);
#endif
232

233
#if 1
234
  svp3 = art_svp_intersect (svp, svp2);
235 236 237 238
#else
  svp3 = svp2;
#endif

239 240 241
#if 0
  print_svp (svp);
#endif
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
  for (y = 0; y < 256; y++)
    for (x = 0; x < 256; x++)
      {
	colorimg[y][x][0] = (x + y) >> 1;
	colorimg[y][x][1] = (x + (255 - y)) >> 1;
	colorimg[y][x][2] = ((255 - x) + y) >> 1;

	rgbaimg[y][x][0] = (x + y) >> 1;
	rgbaimg[y][x][1] = (x + (255 - y)) >> 1;
	rgbaimg[y][x][2] = ((255 - x) + y) >> 1;
	rgbaimg[y][x][3] = y;
      }

  for (y = 0; y < 16; y++)
    for (x = 0; x < 2; x++)
      bitimg[y][x] = (x << 4) | y;

  affine[0] = 0.5;
  affine[1] = .2;
  affine[2] = -.2;
  affine[3] = 0.5;
  affine[4] = 64;
  affine[5] = 64;
  
  affine2[0] = 1;
  affine2[1] = -.2;
  affine2[2] = .2;
  affine2[3] = 1;
  affine2[4] = 128;
  affine2[5] = 128;

  affine3[0] = 5;
  affine3[1] = -.2;
  affine3[2] = .2;
  affine3[3] = 5;
  affine3[4] = 384;
  affine3[5] = 32;
280

281
#if 0
282
  alphagamma = art_alphagamma_new (1.8);
283
#else
284
  alphagamma = NULL;
285
#endif
286

287 288 289
#ifdef COLOR
  printf ("P6\n512 512\n255\n");
#else
290
  printf ("P5\n512 512\n255\n");
291 292 293 294 295
#endif
  for (iter = 0; iter < NUM_ITERS; iter++)
    for (j = 0; j < 512; j += TILE_SIZE)
      for (i = 0; i < 512; i += TILE_SIZE)
	{
296
#ifdef COLOR
297 298
	  art_rgb_svp_aa (svp, i, j, i + TILE_SIZE, j + TILE_SIZE,
			  0xffe0a0, 0x100040,
299 300
			  buf + (j * 512 + i) * BYTES_PP, 512 * BYTES_PP,
			  alphagamma);
301 302
	  art_rgb_svp_alpha (svp2, i, j, i + TILE_SIZE, j + TILE_SIZE,
			     0xff000080,
303 304
			     buf + (j * 512 + i) * BYTES_PP, 512 * BYTES_PP,
			     alphagamma);
305 306
	  art_rgb_svp_alpha (svp3, i, j, i + TILE_SIZE, j + TILE_SIZE,
			     0x00ff0080,
307 308
			     buf + (j * 512 + i) * BYTES_PP, 512 * BYTES_PP,
			     alphagamma);
309 310 311 312
	  art_rgb_affine (buf + (j * 512 + i) * BYTES_PP,
			  i, j, i + TILE_SIZE, j + TILE_SIZE, 512 * BYTES_PP,
			  (art_u8 *)colorimg, 256, 256, 256 * 3,
			  affine,
313
			  ART_FILTER_NEAREST, alphagamma);
314 315 316 317 318
	  art_rgb_rgba_affine (buf + (j * 512 + i) * BYTES_PP,
			       i, j, i + TILE_SIZE, j + TILE_SIZE,
			       512 * BYTES_PP,
			       (art_u8 *)rgbaimg, 256, 256, 256 * 4,
			       affine2,
319
			       ART_FILTER_NEAREST, alphagamma);
320 321 322 323 324 325
	  art_rgb_bitmap_affine (buf + (j * 512 + i) * BYTES_PP,
				 i, j, i + TILE_SIZE, j + TILE_SIZE,
				 512 * BYTES_PP,
				 (art_u8 *)bitimg, 16, 16, 2,
				 0xffff00ff,
				 affine3,
326
				 ART_FILTER_NEAREST, alphagamma);
327
#else
328 329
	  art_gray_svp_aa (svp, i, j, i + TILE_SIZE, j + TILE_SIZE,
			   buf + (j * 512 + i) * BYTES_PP, 512 * BYTES_PP);
330
#endif
331 332 333 334 335
	}

  art_svp_free (svp2);
  art_svp_free (svp3);
  art_svp_free (svp);
336

337
#if 1
338
  fwrite (buf, 1, 512 * 512 * BYTES_PP, stdout);
339
#endif
340 341
}

342
static void
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
test_dist (void)
{
  ArtVpath *vpath;
  ArtSVP *svp;
  art_u8 buf[512 * 512 * BYTES_PP];
  int x, y;
  int ix;
  double dist;
  int wind;

  vpath = randstar (20);
#ifdef NO_STROKE
  svp = art_svp_from_vpath (vpath);
#else
  svp = art_svp_vpath_stroke (vpath,
			       ART_PATH_STROKE_JOIN_MITER,
			       ART_PATH_STROKE_CAP_BUTT,
			       15,
			       4,
			       0.5);
#endif

  art_rgb_svp_aa (svp, 0, 0, 512, 512,
		  0xffe0a0, 0x100040,
		  buf, 512 * BYTES_PP,
		  NULL);

  ix = 0;
  for (y = 0; y < 512; y++)
    {
      for (x = 0; x < 512; x++)
	{
	  wind = art_svp_point_wind (svp, x, y);
	  buf[ix] = 204 - wind * 51;
	  dist = art_svp_point_dist (svp, x, y);
	  if (((x | y) & 0x3f) == 0)
	    {
	      fprintf (stderr, "%d,%d: %f\n", x, y, dist);
	    }
	  buf[ix + 1] = 255 - dist;
	  ix += 3;
	}
    }

  printf ("P6\n512 512\n255\n");
  fwrite (buf, 1, 512 * 512 * BYTES_PP, stdout);

}
391

392
static void
393 394 395 396
test_dash (void)
{
  ArtVpath *vpath, *vpath2;
  double dash_data[] = { 10, 4, 1, 4};
397 398 399 400 401 402
  ArtVpathDash dash;
	  
  dash.offset = 0;
  dash.n_dash = 3;
  dash.dash = dash_data;
  
403 404 405 406 407 408 409 410 411
  vpath = randstar (50);
  vpath2 = art_vpath_dash (vpath, &dash);
  printf ("%%!\n");
  print_vpath (vpath2);
  printf ("showpage\n");
  art_free (vpath);
  art_free (vpath2);
}

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
static void
test_render_gradient (art_u8 *buf)
{
  ArtGradientLinear gradient;
  ArtGradientStop stops[3] = {
    { 0.0, { 0x7fff, 0x0000, 0x0000, 0x7fff }},
    { 0.5, { 0x0000, 0x0000, 0x0000, 0x1000 }},
    { 1.0, { 0x0000, 0x7fff, 0x0000, 0x7fff }}
  };
  ArtVpath *vpath;
  ArtSVP *svp;
  ArtRender *render;

  gradient.a = 0.003;
  gradient.b = -0.0015;
  gradient.c = 0.1;
  gradient.spread = ART_GRADIENT_PAD;
  gradient.n_stops = sizeof(stops) / sizeof(stops[0]);
  gradient.stops = stops;

  vpath = randstar (50);
  svp = art_svp_from_vpath (vpath);

  render = art_render_new (0, 0, 512, 512, buf, 512 * 3, 3, 8, ART_ALPHA_NONE,
			   NULL);
  art_render_svp (render, svp);
  art_render_gradient_linear (render, &gradient, ART_FILTER_NEAREST);
  art_render_invoke (render);

}

static void
test_render_rad_gradient (art_u8 *buf)
{
  ArtGradientRadial gradient;
  ArtGradientStop stops[3] = {
    { 0.0, { 0xffff, 0x0000, 0x0000, 0xffff }},
    { 0.5, { 0xe000, 0xe000, 0x0000, 0xe000 }},
    { 1.0, { 0x0000, 0x0000, 0x0000, 0x0000 }}
  };
  ArtVpath *vpath;
  ArtSVP *svp;
  ArtRender *render;

  gradient.affine[0] = 3.0 / 512;
  gradient.affine[1] = 0;
  gradient.affine[2] = 0;
  gradient.affine[3] = 3.0 / 512;
  gradient.affine[4] = -1.5;
  gradient.affine[5] = -1.5;
  gradient.fx = 0.9;
  gradient.fy = 0.1;
  
  gradient.n_stops = sizeof(stops) / sizeof(stops[0]);
  gradient.stops = stops;

  vpath = randstar (50);
  svp = art_svp_from_vpath (vpath);

  render = art_render_new (0, 0, 512, 512, buf, 512 * 3, 3, 8, ART_ALPHA_NONE,
			   NULL);
  art_render_svp (render, svp);
  art_render_gradient_radial (render, &gradient, ART_FILTER_NEAREST);
  art_render_invoke (render);

}

static void
test_gradient (void)
{
  ArtVpath *vpath;
  ArtSVP *svp;
  art_u8 buf[512 * 512 * 3];
  ArtRender *render;
  ArtPixMaxDepth color[3] = {0x0000, 0x0000, 0x8000 };
  int i;
  const int n_iter = 1;

  vpath = randstar (50);
  svp = art_svp_from_vpath (vpath);

  for (i = 0; i < n_iter; i++)
    {
#define USE_RENDER
#ifdef USE_RENDER
      render = art_render_new (0, 0, 512, 512, buf, 512 * 3, 3, 8, ART_ALPHA_NONE,
			       NULL);
      art_render_clear_rgb (render, 0xfff0c0);
      art_render_svp (render, svp);
      art_render_image_solid (render, color);
      art_render_invoke (render);
#else
      art_rgb_svp_aa (svp, 0, 0, 512, 512, 0xfff0c0, 0x000080,
		      buf, 512 * 3, NULL);
#endif
    }

#if 1
  test_render_gradient (buf);
#endif
  test_render_rad_gradient (buf);

  printf ("P6\n512 512\n255\n");
  fwrite (buf, 1, 512 * 512 * 3, stdout);
}

518
#if 0
519 520 521 522 523 524 525 526 527
static void
output_svp_ppm (const ArtSVP *svp)
{
  art_u8 buf[512 * 512 * 3];
  art_rgb_svp_aa (svp, 0, 0, 512, 512, 0xfff0c0, 0x000080,
		  buf, 512 * 3, NULL);
  printf ("P6\n512 512\n255\n");
  fwrite (buf, 1, 512 * 512 * 3, stdout);
}
528
#endif
529 530 531 532 533 534

static void
test_intersect (void)
{
  ArtVpath vpath[] = {

535
#if 0
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
    /* two triangles */
    { ART_MOVETO, 100, 100 },
    { ART_LINETO, 300, 400 },
    { ART_LINETO, 400, 200 },
    { ART_LINETO, 100, 100 },
    { ART_MOVETO, 110, 110 },
    { ART_LINETO, 310, 410 },
    { ART_LINETO, 410, 210 },
    { ART_LINETO, 110, 110 },
#endif

#if 0
    /* a bowtie */
    { ART_MOVETO, 100, 100 },
    { ART_LINETO, 400, 400 },
    { ART_LINETO, 400, 100 },
    { ART_LINETO, 100, 400 },
    { ART_LINETO, 100, 100 },
#endif

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
#if 1
    /* a square */
    { ART_MOVETO, 100, 100 },
    { ART_LINETO, 100, 400 },
    { ART_LINETO, 400, 400 },
    { ART_LINETO, 400, 100 },
    { ART_LINETO, 100, 100 },
#endif

#if 1
    /* another square */
#define XOFF 10
#define YOFF 10
    { ART_MOVETO, 100 + XOFF, 100 + YOFF },
    { ART_LINETO, 100 + XOFF, 400 + YOFF },
    { ART_LINETO, 400 + XOFF, 400 + YOFF },
    { ART_LINETO, 400 + XOFF, 100 + YOFF },
    { ART_LINETO, 100 + XOFF, 100 + YOFF },
#endif

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
    { ART_END, 0, 0}
  };
  ArtSVP *svp, *svp2;
  ArtSvpWriter *swr;

  svp = art_svp_from_vpath (vpath);

#define RUN_INTERSECT
#ifdef RUN_INTERSECT
  swr = art_svp_writer_rewind_new (ART_WIND_RULE_ODDEVEN);
  art_svp_intersector (svp, swr);

  svp2 = art_svp_writer_rewind_reap (swr);
#endif

591
#if 0
592
  output_svp_ppm (svp2);
593 594
#else
  print_svp (svp2);
595 596 597 598 599 600 601 602 603
#endif

  art_svp_free (svp);

#ifdef RUN_INTERSECT
  art_svp_free (svp2);
#endif
}

604
static void
605 606 607 608 609
usage (void)
{
  fprintf (stderr, "usage: testart <test>\n"
"  where <test> is one of:\n"
"  testpat    -- make random star + gradients test pattern\n"
610
"  gradient   -- test pattern for rendered gradients\n"
611
"  dash       -- dash test (output is valid PostScript)\n"
612 613
"  dist       -- distance test\n"
"  intersect  -- softball test for intersector\n");
614 615 616 617 618 619 620 621 622 623 624
  exit (1);
}

int
main (int argc, char **argv)
{
  if (argc < 2)
    usage ();

  if (!strcmp (argv[1], "testpat"))
    make_testpat ();
625 626
  else if (!strcmp (argv[1], "gradient"))
    test_gradient ();
627 628
  else if (!strcmp (argv[1], "dist"))
    test_dist ();
629 630
  else if (!strcmp (argv[1], "dash"))
    test_dash ();
631 632
  else if (!strcmp (argv[1], "intersect"))
    test_intersect ();
633 634
  else
    usage ();
Raph Levien's avatar
Raph Levien committed
635 636
  return 0;
}