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
|
/* Libart_LGPL - library of basic graphic primitives
* Copyright (C) 1998, 1999 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "art_misc.h"
#include "art_vpath.h"
#include "art_svp.h"
#include "art_svp_vpath.h"
#include "art_gray_svp.h"
#include "art_rgb_svp.h"
#include "art_svp_vpath_stroke.h"
#include "art_svp_ops.h"
#include "art_affine.h"
#include "art_rgb_affine.h"
#include "art_rgb_bitmap_affine.h"
#include "art_rgb_rgba_affine.h"
#include "art_alphagamma.h"
#include "art_svp_point.h"
#include "art_vpath_dash.h"
#include "art_render.h"
#include "art_render_gradient.h"
#include "art_render_svp.h"
#include "art_svp_intersect.h"
#ifdef DEAD_CODE
static void
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);
}
#endif
static ArtVpath *
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);
#if 0
r = r + 0.9 * (250 - r);
#endif
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;
}
#define TILE_SIZE 512
#define NUM_ITERS 1
#define COLOR
#ifdef COLOR
#define BYTES_PP 3
#else
#define BYTES_PP 1
#endif
#ifndef nDEAD_CODE
static void
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);
}
}
#endif
static void
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");
}
static void
make_testpat (void)
{
ArtVpath *vpath, *vpath2, *vpath3;
ArtSVP *svp, *svp2;
ArtSVP *svp3;
art_u8 buf[512 * 512 * BYTES_PP];
int i, j;
int iter;
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];
ArtAlphaGamma *alphagamma;
double dash_data[] = { 20 };
ArtVpathDash dash;
dash.offset = 0;
dash.n_dash = 1;
dash.dash = dash_data;
#ifdef TEST_AFFINE
test_affine ();
exit (0);
#endif
vpath = randstar (50);
svp = art_svp_from_vpath (vpath);
art_free (vpath);
vpath2 = randstar (50);
#if 1
vpath3 = art_vpath_dash (vpath2, &dash);
art_free (vpath2);
svp2 = art_svp_vpath_stroke (vpath3,
ART_PATH_STROKE_JOIN_MITER,
ART_PATH_STROKE_CAP_BUTT,
15,
4,
0.5);
art_free (vpath3);
#else
svp2 = art_svp_from_vpath (vpath2);
#endif
#if 1
svp3 = art_svp_intersect (svp, svp2);
#else
svp3 = svp2;
#endif
#if 0
print_svp (svp);
#endif
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;
#if 0
alphagamma = art_alphagamma_new (1.8);
#else
alphagamma = NULL;
#endif
#ifdef COLOR
printf ("P6\n512 512\n255\n");
#else
printf ("P5\n512 512\n255\n");
#endif
for (iter = 0; iter < NUM_ITERS; iter++)
for (j = 0; j < 512; j += TILE_SIZE)
for (i = 0; i < 512; i += TILE_SIZE)
{
#ifdef COLOR
art_rgb_svp_aa (svp, i, j, i + TILE_SIZE, j + TILE_SIZE,
0xffe0a0, 0x100040,
buf + (j * 512 + i) * BYTES_PP, 512 * BYTES_PP,
alphagamma);
art_rgb_svp_alpha (svp2, i, j, i + TILE_SIZE, j + TILE_SIZE,
0xff000080,
buf + (j * 512 + i) * BYTES_PP, 512 * BYTES_PP,
alphagamma);
art_rgb_svp_alpha (svp3, i, j, i + TILE_SIZE, j + TILE_SIZE,
0x00ff0080,
buf + (j * 512 + i) * BYTES_PP, 512 * BYTES_PP,
alphagamma);
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,
ART_FILTER_NEAREST, alphagamma);
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,
ART_FILTER_NEAREST, alphagamma);
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,
ART_FILTER_NEAREST, alphagamma);
#else
art_gray_svp_aa (svp, i, j, i + TILE_SIZE, j + TILE_SIZE,
buf + (j * 512 + i) * BYTES_PP, 512 * BYTES_PP);
#endif
}
art_svp_free (svp2);
art_svp_free (svp3);
art_svp_free (svp);
#if 1
fwrite (buf, 1, 512 * 512 * BYTES_PP, stdout);
#endif
}
static void
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);
}
static void
test_dash (void)
{
ArtVpath *vpath, *vpath2;
double dash_data[] = { 10, 4, 1, 4};
ArtVpathDash dash;
dash.offset = 0;
dash.n_dash = 3;
dash.dash = dash_data;
vpath = randstar (50);
vpath2 = art_vpath_dash (vpath, &dash);
printf ("%%!\n");
print_vpath (vpath2);
printf ("showpage\n");
art_free (vpath);
art_free (vpath2);
}
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);
}
#if 0
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);
}
#endif
static void
test_intersect (void)
{
ArtVpath vpath[] = {
#if 0
/* 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
#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
{ 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
#if 0
output_svp_ppm (svp2);
#else
print_svp (svp2);
#endif
art_svp_free (svp);
#ifdef RUN_INTERSECT
art_svp_free (svp2);
#endif
}
static void
usage (void)
{
fprintf (stderr, "usage: testart <test>\n"
" where <test> is one of:\n"
" testpat -- make random star + gradients test pattern\n"
" gradient -- test pattern for rendered gradients\n"
" dash -- dash test (output is valid PostScript)\n"
" dist -- distance test\n"
" intersect -- softball test for intersector\n");
exit (1);
}
int
main (int argc, char **argv)
{
if (argc < 2)
usage ();
if (!strcmp (argv[1], "testpat"))
make_testpat ();
else if (!strcmp (argv[1], "gradient"))
test_gradient ();
else if (!strcmp (argv[1], "dist"))
test_dist ();
else if (!strcmp (argv[1], "dash"))
test_dash ();
else if (!strcmp (argv[1], "intersect"))
test_intersect ();
else
usage ();
return 0;
}
|