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
|
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4 (Public License)
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Anti-Grain Geometry - Version 2.4 Release Milano 3 (AggPas 2.4 RM3)
// Pascal Port By: Milan Marusinec alias Milano
// milan@marusinec.sk
// http://www.aggpas.org
// Copyright (c) 2005-2006
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
// mcseemagg@yahoo.com
// http://www.antigrain.com
//
//----------------------------------------------------------------------------
//
// Adaptation for high precision colors has been sponsored by
// Liberty Technology Systems, Inc., visit http://lib-sys.com
//
// Liberty Technology Systems, Inc. is the provider of
// PostScript and PDF technology for software developers.
//
// [Pascal Port History] -----------------------------------------------------
//
// 23.06.2006-Milano: ptrcomp adjustments
// 27.01.2006-Milano: Unit port establishment
//
{ agg_span_gouraud_rgba.pas }
unit
agg_span_gouraud_rgba ;
INTERFACE
{$I agg_mode.inc }
uses
agg_basics ,
agg_color ,
agg_dda_line ,
agg_span_gouraud ,
agg_span_allocator ,
agg_math ;
{ TYPES DEFINITION }
const
subpixel_shift = 4;
subpixel_size = 1 shl subpixel_shift;
type
rgba_calc_ptr = ^rgba_calc;
rgba_calc = object
m_x1 ,m_y1 ,m_dx ,m_1dy : double;
m_r1 ,m_g1 ,m_b1 ,m_a1 ,
m_dr ,m_dg ,m_db ,m_da ,
m_r ,m_g ,m_b ,m_a ,m_x : int;
function round(v : double ) : int;
procedure init (c1 ,c2 : coord_type_ptr );
procedure calc (y : double );
end;
span_gouraud_rgba_ptr = ^span_gouraud_rgba;
span_gouraud_rgba = object(span_gouraud )
m_swap : boolean;
m_y2 : int;
m_rgba1 ,
m_rgba2 ,
m_rgba3 : rgba_calc;
constructor Construct(alloc : span_allocator_ptr ); overload;
constructor Construct(
alloc : span_allocator_ptr;
c1 ,c2 ,c3 : aggclr_ptr;
x1 ,y1 ,x2 ,y2 ,x3 ,y3 ,d : double ); overload;
procedure prepare (max_span_len : unsigned ); virtual;
function generate(x ,y : int; len : unsigned ) : aggclr_ptr; virtual;
// Agg 2.4 impl
constructor Construct_(
c1 ,c2 ,c3 : aggclr_ptr;
x1 ,y1 ,x2 ,y2 ,x3 ,y3 : double;
d : double = 0 );
procedure prepare_;
procedure generate_(span : aggclr_ptr; x ,y : int; len : unsigned );
end;
{ GLOBAL PROCEDURES }
IMPLEMENTATION
{ LOCAL VARIABLES & CONSTANTS }
{ UNIT IMPLEMENTATION }
{ ROUND }
function rgba_calc.round;
begin
if v < 0.0 then
result:=trunc(v - 0.5 )
else
result:=trunc(v + 0.5 );
end;
{ INIT }
procedure rgba_calc.init;
var
dy : double;
begin
m_x1:=c1.x - 0.5;
m_y1:=c1.y - 0.5;
m_dx:=c2.x - c1.x;
dy:=c2.y - c1.y;
if dy < 1e-5 then
m_1dy:=1e5
else
m_1dy:=1.0 / dy;
m_r1:=c1.color.r;
m_g1:=c1.color.g;
m_b1:=c1.color.b;
m_a1:=c1.color.a;
m_dr:=c2.color.r - m_r1;
m_dg:=c2.color.g - m_g1;
m_db:=c2.color.b - m_b1;
m_da:=c2.color.a - m_a1;
end;
{ CALC }
procedure rgba_calc.calc;
var
k : double;
begin
k:=(y - m_y1 ) * m_1dy;
if k < 0.0 then
k:=0.0;
if k > 1.0 then
k:=1.0;
m_r:=m_r1 + self.round(m_dr * k );
m_g:=m_g1 + self.round(m_dg * k );
m_b:=m_b1 + self.round(m_db * k );
m_a:=m_a1 + self.round(m_da * k );
m_x:=self.round((m_x1 + m_dx * k ) * subpixel_size );
end;
{ CONSTRUCT }
constructor span_gouraud_rgba.Construct(alloc : span_allocator_ptr );
begin
inherited Construct(alloc );
end;
{ CONSTRUCT }
constructor span_gouraud_rgba.Construct(
alloc : span_allocator_ptr;
c1 ,c2 ,c3 : aggclr_ptr;
x1 ,y1 ,x2 ,y2 ,x3 ,y3 ,d : double );
begin
inherited Construct(alloc ,c1 ,c2 ,c3 ,x1 ,y1 ,x2 ,y2 ,x3 ,y3 ,d );
end;
{ PREPARE }
procedure span_gouraud_rgba.prepare;
var
coord : array[0..2 ] of coord_type;
begin
inherited prepare(max_span_len );
arrange_vertices(@coord );
m_y2:=trunc(coord[1 ].y );
m_swap:=
calc_point_location(
coord[0 ].x ,coord[0 ].y,
coord[2 ].x ,coord[2 ].y,
coord[1 ].x ,coord[1 ].y ) < 0.0;
m_rgba1.init(@coord[0 ] ,@coord[2 ] );
m_rgba2.init(@coord[0 ] ,@coord[1 ] );
m_rgba3.init(@coord[1 ] ,@coord[2 ] );
end;
{ GENERATE }
function span_gouraud_rgba.generate;
const
lim = agg_color.base_mask;
var
pc1 ,pc2 ,t : rgba_calc_ptr;
nlen ,start ,vr ,vg ,vb ,va : int;
r ,g ,b ,a : dda_line_interpolator;
span : aggclr_ptr;
begin
m_rgba1.calc(y ); //(m_rgba1.m_1dy > 2) ? m_rgba1.m_y1 : y);
pc1:=@m_rgba1;
pc2:=@m_rgba2;
if y <= m_y2 then
// Bottom part of the triangle (first subtriangle)
m_rgba2.calc(y + m_rgba2.m_1dy )
else
begin
// Upper part (second subtriangle)
m_rgba3.calc(y - m_rgba3.m_1dy );
pc2:=@m_rgba3;
end;
if m_swap then
begin
// It means that the triangle is oriented clockwise,
// so that we need to swap the controlling structures
t :=pc2;
pc2:=pc1;
pc1:=t;
end;
// Get the horizontal length with subpixel accuracy
// and protect it from division by zero
nlen:=Abs(pc2.m_x - pc1.m_x );
if nlen <= 0 then
nlen:=1;
r.Construct(pc1.m_r ,pc2.m_r ,nlen ,14 );
g.Construct(pc1.m_g ,pc2.m_g ,nlen ,14 );
b.Construct(pc1.m_b ,pc2.m_b ,nlen ,14 );
a.Construct(pc1.m_a ,pc2.m_a ,nlen ,14 );
// Calculate the starting point of the gradient with subpixel
// accuracy and correct (roll back) the interpolators.
// This operation will also clip the beginning of the span
// if necessary.
start:=pc1.m_x - (x shl subpixel_shift );
r.dec_operator(start );
g.dec_operator(start );
b.dec_operator(start );
a.dec_operator(start );
inc(nlen ,start );
span:=_allocator.span;
// Beginning part of the span. Since we rolled back the
// interpolators, the color values may have overflow.
// So that, we render the beginning part with checking
// for overflow. It lasts until "start" is positive;
// typically it's 1-2 pixels, but may be more in some cases.
while (len <> 0 ) and
(start > 0 ) do
begin
vr:=r._y;
vg:=g._y;
vb:=b._y;
va:=a._y;
if vr < 0 then
vr:=0;
if vr > lim then
vr:=lim;
if vg < 0 then
vg:=0;
if vg > lim then
vg:=lim;
if vb < 0 then
vb:=0;
if vb > lim then
vb:=lim;
if va < 0 then
va:=0;
if va > lim then
va:=lim;
span.r:=int8u(vr );
span.g:=int8u(vg );
span.b:=int8u(vb );
span.a:=int8u(va );
r.inc_operator(subpixel_size );
g.inc_operator(subpixel_size );
b.inc_operator(subpixel_size );
a.inc_operator(subpixel_size );
dec(nlen ,subpixel_size );
dec(start ,subpixel_size );
inc(ptrcomp(span ) ,sizeof(aggclr ) );
dec(len );
end;
// Middle part, no checking for overflow.
// Actual spans can be longer than the calculated length
// because of anti-aliasing, thus, the interpolators can
// overflow. But while "nlen" is positive we are safe.
while (len <> 0 ) and
(nlen > 0 ) do
begin
span.r:=int8u(r._y );
span.g:=int8u(g._y );
span.b:=int8u(b._y );
span.a:=int8u(a._y );
r.inc_operator(subpixel_size );
g.inc_operator(subpixel_size );
b.inc_operator(subpixel_size );
a.inc_operator(subpixel_size );
dec(nlen ,subpixel_size );
inc(ptrcomp(span ) ,sizeof(aggclr ) );
dec(len );
end;
// Ending part; checking for overflow.
// Typically it's 1-2 pixels, but may be more in some cases.
while len <> 0 do
begin
vr:=r._y;
vg:=g._y;
vb:=b._y;
va:=a._y;
if vr < 0 then
vr:=0;
if vr > lim then
vr:=lim;
if vg < 0 then
vg:=0;
if vg > lim then
vg:=lim;
if vb < 0 then
vb:=0;
if vb > lim then
vb:=lim;
if va < 0 then
va:=0;
if va > lim then
va:=lim;
span.r:=int8u(vr );
span.g:=int8u(vg );
span.b:=int8u(vb );
span.a:=int8u(va );
r.inc_operator(subpixel_size );
g.inc_operator(subpixel_size );
b.inc_operator(subpixel_size );
a.inc_operator(subpixel_size );
inc(ptrcomp(span ) ,sizeof(aggclr ) );
dec(len );
end;
result:=_allocator.span;
end;
{ CONSTRUCT_ }
constructor span_gouraud_rgba.Construct_(
c1 ,c2 ,c3 : aggclr_ptr;
x1 ,y1 ,x2 ,y2 ,x3 ,y3 : double;
d : double = 0 );
begin
inherited Construct(NIL ,c1 ,c2 ,c3 ,x1 ,y1 ,x2 ,y2 ,x3 ,y3 ,d );
end;
{ PREPARE_ }
procedure span_gouraud_rgba.prepare_;
var
coord : array[0..2 ] of coord_type;
begin
arrange_vertices(@coord );
m_y2:=int(Trunc(coord[1 ].y ) );
m_swap:=
cross_product(
coord[0 ].x ,coord[0 ].y,
coord[2 ].x ,coord[2 ].y,
coord[1 ].x ,coord[1 ].y ) < 0.0;
m_rgba1.init(@coord[0 ] ,@coord[2 ] );
m_rgba2.init(@coord[0 ] ,@coord[1 ] );
m_rgba3.init(@coord[1 ] ,@coord[2 ] );
end;
{ GENERATE_ }
procedure span_gouraud_rgba.generate_(span : aggclr_ptr; x ,y : int; len : unsigned );
const
lim = agg_color.base_mask;
var
pc1 ,pc2 ,t : rgba_calc_ptr;
nlen ,start ,vr ,vg ,vb ,va : int;
r ,g ,b ,a : dda_line_interpolator;
begin
m_rgba1.calc(y ); //(m_rgba1.m_1dy > 2) ? m_rgba1.m_y1 : y);
pc1:=@m_rgba1;
pc2:=@m_rgba2;
if y <= m_y2 then
// Bottom part of the triangle (first subtriangle)
m_rgba2.calc(y + m_rgba2.m_1dy )
else
begin
// Upper part (second subtriangle)
m_rgba3.calc(y - m_rgba3.m_1dy );
pc2:=@m_rgba3;
end;
if m_swap then
begin
// It means that the triangle is oriented clockwise,
// so that we need to swap the controlling structures
t :=pc2;
pc2:=pc1;
pc1:=t;
end;
// Get the horizontal length with subpixel accuracy
// and protect it from division by zero
nlen:=Abs(pc2.m_x - pc1.m_x );
if nlen <= 0 then
nlen:=1;
r.Construct(pc1.m_r ,pc2.m_r ,nlen ,14 );
g.Construct(pc1.m_g ,pc2.m_g ,nlen ,14 );
b.Construct(pc1.m_b ,pc2.m_b ,nlen ,14 );
a.Construct(pc1.m_a ,pc2.m_a ,nlen ,14 );
// Calculate the starting point of the gradient with subpixel
// accuracy and correct (roll back) the interpolators.
// This operation will also clip the beginning of the span
// if necessary.
start:=pc1.m_x - (x shl subpixel_shift );
r.dec_operator(start );
g.dec_operator(start );
b.dec_operator(start );
a.dec_operator(start );
inc(nlen ,start );
// Beginning part of the span. Since we rolled back the
// interpolators, the color values may have overflow.
// So that, we render the beginning part with checking
// for overflow. It lasts until "start" is positive;
// typically it's 1-2 pixels, but may be more in some cases.
while (len <> 0 ) and
(start > 0 ) do
begin
vr:=r._y;
vg:=g._y;
vb:=b._y;
va:=a._y;
if vr < 0 then
vr:=0;
if vr > lim then
vr:=lim;
if vg < 0 then
vg:=0;
if vg > lim then
vg:=lim;
if vb < 0 then
vb:=0;
if vb > lim then
vb:=lim;
if va < 0 then
va:=0;
if va > lim then
va:=lim;
span.r:=int8u(vr );
span.g:=int8u(vg );
span.b:=int8u(vb );
span.a:=int8u(va );
r.inc_operator(subpixel_size );
g.inc_operator(subpixel_size );
b.inc_operator(subpixel_size );
a.inc_operator(subpixel_size );
dec(nlen ,subpixel_size );
dec(start ,subpixel_size );
inc(ptrcomp(span ) ,sizeof(aggclr ) );
dec(len );
end;
// Middle part, no checking for overflow.
// Actual spans can be longer than the calculated length
// because of anti-aliasing, thus, the interpolators can
// overflow. But while "nlen" is positive we are safe.
while (len <> 0 ) and
(nlen > 0 ) do
begin
span.r:=int8u(r._y );
span.g:=int8u(g._y );
span.b:=int8u(b._y );
span.a:=int8u(a._y );
r.inc_operator(subpixel_size );
g.inc_operator(subpixel_size );
b.inc_operator(subpixel_size );
a.inc_operator(subpixel_size );
dec(nlen ,subpixel_size );
inc(ptrcomp(span ) ,sizeof(aggclr ) );
dec(len );
end;
// Ending part; checking for overflow.
// Typically it's 1-2 pixels, but may be more in some cases.
while len <> 0 do
begin
vr:=r._y;
vg:=g._y;
vb:=b._y;
va:=a._y;
if vr < 0 then
vr:=0;
if vr > lim then
vr:=lim;
if vg < 0 then
vg:=0;
if vg > lim then
vg:=lim;
if vb < 0 then
vb:=0;
if vb > lim then
vb:=lim;
if va < 0 then
va:=0;
if va > lim then
va:=lim;
span.r:=int8u(vr );
span.g:=int8u(vg );
span.b:=int8u(vb );
span.a:=int8u(va );
r.inc_operator(subpixel_size );
g.inc_operator(subpixel_size );
b.inc_operator(subpixel_size );
a.inc_operator(subpixel_size );
inc(ptrcomp(span ) ,sizeof(aggclr ) );
dec(len );
end;
end;
END.
|