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 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
|
/* === S Y N F I G ========================================================= */
/*! \file synfig/renddesc.cpp
** \brief Class that defines the parameters needed by the Renderer to
* render a context to a surface.
**
** $Id$
**
** \legal
** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
** Copyright (c) 2008 Chris Moore
**
** This package is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License as
** published by the Free Software Foundation; either version 2 of
** the License, or (at your option) any later version.
**
** This package 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
** General Public License for more details.
** \endlegal
*/
/* ========================================================================= */
/* === H E A D E R S ======================================================= */
#ifdef USING_PCH
# include "pch.h"
#else
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "renddesc.h"
#include <ETL/misc>
#endif
/* === U S I N G =========================================================== */
using namespace std;
using namespace etl;
using namespace synfig;
/* === M A C R O S ========================================================= */
#undef FLAGS
#define FLAGS(x,y) (((x)&(y))==(y))
/* === G L O B A L S ======================================================= */
/* === M E T H O D S ======================================================= */
const Color &
RendDesc::get_bg_color()const
{
return background;
}
RendDesc &
RendDesc::set_bg_color(const Color &bg)
{
background=bg; return *this;
}
Real
RendDesc::get_physical_w()const
{
return (Real)get_w()/get_x_res();
}
Real
RendDesc::get_physical_h()const
{
return (Real)get_h()/get_y_res();
}
RendDesc&
RendDesc::set_physical_w(Real w)
{
set_w(round_to_int(w*get_x_res()));
return *this;
}
RendDesc&
RendDesc::set_physical_h(Real h)
{
set_h(round_to_int(h*get_y_res()));
return *this;
}
int
RendDesc::get_w()const
{
return w_;
}
RendDesc &
RendDesc::set_w(int x)
{
if(FLAGS(flags,LINK_IM_ASPECT)) // "Width and Height ratio"
{
int new_h = h_ratio_*x/w_ratio_;
if(FLAGS(flags,PX_ASPECT))
{
br_[1]-=focus[1];
br_[1]=h_?br_[1]/h_*new_h:0.0;
br_[1]+=focus[1];
tl_[1]-=focus[1];
tl_[1]=h_?tl_[1]/h_*new_h:0.0;
tl_[1]+=focus[1];
br_[0]-=focus[0];
br_[0]=w_?br_[0]/w_*x:0.0;
br_[0]+=focus[0];
tl_[0]-=focus[0];
tl_[0]=w_?tl_[0]/w_*x:0.0;
tl_[0]+=focus[0];
}
h_=new_h;
w_=x;
return *this;
}
if(FLAGS(flags,LINK_PX_ASPECT)) // never set
{
h_=w_?h_*x/w_:0.0;
w_=x;
}
else if(FLAGS(flags,LINK_PX_AREA)) // never set
{
//! \writeme
w_=x;
}
else if(FLAGS(flags,PX_ASPECT)) // "Pixel Aspect"
{
Vector d=br_-tl_;
float old_span=get_span();
// If we should preserve image width
if( FLAGS(flags,IM_W) // "Image Width"
|| (FLAGS(flags,IM_ZOOMIN) && x && d[1]>d[1]/x*w_) // never set
|| (FLAGS(flags,IM_ZOOMOUT) && x && d[1]<d[1]/x*w_)) // never set
{
br_[1]-=focus[1];
br_[1]=x?br_[1]/x*w_:0.0;
br_[1]+=focus[1];
tl_[1]-=focus[1];
tl_[1]=x?tl_[1]/x*w_:0.0;
tl_[1]+=focus[1];
} else
{
br_[0]-=focus[0];
br_[0]=w_?br_[0]/w_*x:0.0;
br_[0]+=focus[0];
tl_[0]-=focus[0];
tl_[0]=w_?tl_[0]/w_*x:0.0;
tl_[0]+=focus[0];
}
w_=x;
if(FLAGS(flags,IM_SPAN)) // "Image Span"
set_span(old_span);
}
else if(FLAGS(flags,PX_AREA)) // never set
{
//! \writeme
w_=x;
}
else
w_=x;
return *this;
}
int
RendDesc::get_h()const
{
return h_;
}
RendDesc &
RendDesc::set_h(int y)
{
if(FLAGS(flags,LINK_IM_ASPECT)) // "Width and Height ratio"
{
int new_w = w_ratio_*y/h_ratio_;
if(FLAGS(flags,PX_ASPECT))
{
br_[0]-=focus[0];
br_[0]=w_?br_[0]/w_*new_w:0.0;
br_[0]+=focus[0];
tl_[0]-=focus[0];
tl_[0]=w_?tl_[0]/w_*new_w:0.0;
tl_[0]+=focus[0];
br_[1]-=focus[1];
br_[1]=h_?br_[1]/h_*y:0.0;
br_[1]+=focus[1];
tl_[1]-=focus[1];
tl_[1]=h_?tl_[1]/h_*y:0.0;
tl_[1]+=focus[1];
}
w_=new_w;
h_=y;
return *this;
}
if(FLAGS(flags,LINK_PX_ASPECT)) // never set
{
w_=h_?w_*y/h_:0.0;
h_=y;
}
else if(FLAGS(flags,LINK_PX_AREA)) // never set
{
//! \writeme
h_=y;
}
else if(FLAGS(flags,PX_ASPECT)) // "Pixel Aspect"
{
Vector d=br_-tl_;
float old_span=get_span();
// If we should preserve image width
if( FLAGS(flags,IM_W) // "Image Width"
|| (FLAGS(flags,IM_ZOOMIN) && y && d[0]>d[0]/y*h_) // never set
|| (FLAGS(flags,IM_ZOOMOUT) && y && d[0]<d[0]/y*h_)) // never set
{
br_[0]-=focus[0];
br_[0]=y?br_[0]/y*h_:0.0;
br_[0]+=focus[0];
tl_[0]-=focus[0];
tl_[0]=y?tl_[0]/y*h_:0.0;
tl_[0]+=focus[0];
} else
{
br_[1]-=focus[1];
br_[1]=h_?br_[1]/h_*y:0.0;
br_[1]+=focus[1];
tl_[1]-=focus[1];
tl_[1]=h_?tl_[1]/h_*y:0.0;
tl_[1]+=focus[1];
}
h_=y;
if(FLAGS(flags,IM_SPAN)) // "Image Span"
set_span(old_span);
}
else if(FLAGS(flags,PX_AREA)) // never set
{
//! \writeme
h_=y;
}
else
h_=y;
return *this;
}
RendDesc &
RendDesc::set_wh(int x, int y)
{
// FIXME: This is a working hack...
set_w(x);
set_h(y);
return *this;
}
Real
RendDesc::get_x_res()const
{
return x_res;
}
RendDesc &
RendDesc::set_x_res(Real x)
{
if(FLAGS(flags,LINK_RES)) // "Resolution ratio"
{
y_res = y_res_ratio_*x/x_res_ratio_;
}
x_res=x; return *this;
}
Real
RendDesc::get_y_res()const
{
return y_res;
}
RendDesc &
RendDesc::set_y_res(Real y)
{
if(FLAGS(flags,LINK_RES)) // "Resolution ratio"
{
x_res = x_res_ratio_*y/y_res_ratio_;
}
y_res=y; return *this;
}
int
RendDesc::get_frame_start()const
{
return round_to_int(time_begin*frame_rate);
}
RendDesc &
RendDesc::set_frame_start(int x)
{
return set_time_start(Time(x)/frame_rate);
}
int
RendDesc::get_frame_end()const
{
return round_to_int(time_end*frame_rate);
}
RendDesc &
RendDesc::set_frame_end(int x)
{
return set_time_end(Time(x)/frame_rate);
}
const Time
RendDesc::get_time_start()const
{
return time_begin;
}
RendDesc &
RendDesc::set_time_start(Time x)
{
if(x>time_end)
time_begin=time_end=x;
else
time_begin=x;
return *this;
}
const Time
RendDesc::get_time_end()const
{
return time_end;
}
RendDesc &
RendDesc::set_time_end(Time x)
{
if(x<time_begin)
time_end=time_begin=x;
else
time_end=x;
return *this;
}
RendDesc &
RendDesc::set_time(Time x)
{
time_end=time_begin=x;
return *this;
}
RendDesc &
RendDesc::set_frame(int x)
{
return set_time(Time(x)/frame_rate);
}
const float &
RendDesc::get_frame_rate()const
{
return frame_rate;
}
RendDesc &
RendDesc::set_frame_rate(float x)
{
frame_rate=x;
return *this;
}
const bool &
RendDesc::get_interlaced()const
{
return interlaced;
}
RendDesc &
RendDesc::set_interlaced(bool x)
{ interlaced=x; return *this; }
//! Return the status of the clamp flag
const bool &
RendDesc::get_clamp()const
{ return clamp; }
//! Set the clamp flag
RendDesc &
RendDesc::set_clamp(bool x)
{ clamp=x; return *this; }
//! Return the status of the render_excluded_contexts flag
const bool &
RendDesc::get_render_excluded_contexts()const
{ return render_excluded_contexts; }
//! Set the render_excluded_contexts flag
RendDesc &
RendDesc::set_render_excluded_contexts(bool x)
{ render_excluded_contexts=x; return *this; }
//! Set constraint flags
RendDesc &
RendDesc::set_flags(const int &x)
{ flags=x; return *this; }
//! Clear constraint flags
RendDesc &
RendDesc::clear_flags()
{ flags=0; return *this; }
int
RendDesc::get_flags()const
{ return flags; }
//! Return the aspect ratio of a single pixel
Real
RendDesc::get_pixel_aspect()const
{
if (!w_ || !h_) return 1.0;
Vector tmp=br_-tl_;
tmp[0]/=tmp[0];
tmp[1]/=tmp[1];
tmp[0]/=tmp[1];
if(tmp[0]<0.0)
return -tmp[0];
return tmp[0];
}
//! Return the aspect ratio of the entire image
Real
RendDesc::get_image_aspect()const
{
Point tmp=br_-tl_;
tmp[0]/=tmp[1];
if(tmp[0]<0.0)
return -tmp[0];
return tmp[0];
}
//! Affect the pixel ratio for LINK_IM_ASPECT flag
void
RendDesc::set_pixel_ratio(const int &x, const int &y)
{
w_ratio_ = x;
h_ratio_ = y;
}
//! Get the reduced pixel ratio (based on euclide reduction)
void
RendDesc::get_pixel_ratio_reduced(int &w_ratio_reduced, int &h_ratio_reduced)
{
int w = w_;
int h = h_;
int last_rem = h_;
int bigger_commun_div;
div_t dv;
if(!w_ || !h_)
{
w_ratio_reduced = h_ratio_reduced = 0;
return;
}
if(w_ == h_)
{
w_ratio_reduced = h_ratio_reduced = 1;
return;
}
while (last_rem != 0)
{
dv = div(w, h);
w = h;
bigger_commun_div = last_rem;
last_rem = h = dv.rem;
}
w_ratio_reduced = w_ / bigger_commun_div;
h_ratio_reduced = h_ / bigger_commun_div;
}
//! Affect the resolution ratio for LINK_RES flag
void
RendDesc::set_res_ratio(const Real &x, const Real &y)
{
x_res_ratio_ = x;
y_res_ratio_ = y;
}
//! Return the antialias amount
const int &
RendDesc::get_antialias()const
{ return a; }
//! Set the antialias amount
RendDesc &
RendDesc::set_antialias(const int &x)
{ a=x; return *this; }
//! Return the distance from the bottom-right to the top-left
Real
RendDesc::get_span()const
{
return (br_-tl_).mag();
}
//! Set the span distance
RendDesc &
RendDesc::set_span(const Real &x)
{
Vector::value_type ratio=x/get_span();
//! \todo this looks wrong. I suspect the intention was to check
// "(not IM_W) AND (not IM_H)", ie "not(IM_W OR IM_H)" but
// this check does "not(IM_W AND IM_H)"
if(!FLAGS(flags,IM_W|IM_H) || FLAGS(flags,IM_ASPECT)) // (not "Image Width") or (not "Image Height") or "Image Aspect"
{
br_-=focus;
br_=br_*ratio;
br_+=focus;
tl_-=focus;
tl_=tl_*ratio;
tl_+=focus;
}
else if(FLAGS(flags,IM_W)) // "Image Width"
{
//! \writeme or fix me
br_-=focus;
br_=br_*ratio;
br_+=focus;
tl_-=focus;
tl_=tl_*ratio;
tl_+=focus;
}else // IM_H // "Image Height"
{
//! \writeme or fix me
br_-=focus;
br_=br_*ratio;
br_+=focus;
tl_-=focus;
tl_=tl_*ratio;
tl_+=focus;
}
return *this;
}
const Point &
RendDesc::get_focus()const
{ return focus; }
RendDesc &
RendDesc::set_focus(const Point &x)
{ focus=x; return *this; }
const Point &
RendDesc::get_tl()const
{ return tl_; }
const Point &
RendDesc::get_br()const
{ return br_; }
RendDesc &
RendDesc::set_tl(const Point &x)
{
if(FLAGS(flags,PX_ASPECT)) // "Pixel Aspect"
{
Vector new_size(x-br_);
new_size[0]=abs(new_size[0]);
new_size[1]=abs(new_size[1]);
Vector old_size(tl_-br_);
old_size[0]=abs(old_size[0]);
old_size[1]=abs(old_size[1]);
if(new_size[0]!=old_size[0])
w_=round_to_int(new_size[0]*w_/old_size[0]);
if(new_size[1]!=old_size[1])
h_=round_to_int(new_size[1]*h_/old_size[1]);
}
tl_=x; return *this;
}
RendDesc &
RendDesc::set_br(const Point &x)
{
if(FLAGS(flags,PX_ASPECT)) // "Pixel Aspect"
{
Vector new_size(x-tl_);
new_size[0]=abs(new_size[0]);
new_size[1]=abs(new_size[1]);
Vector old_size(tl_-br_);
old_size[0]=abs(old_size[0]);
old_size[1]=abs(old_size[1]);
if(new_size[0]!=old_size[0])
w_=round_to_int(new_size[0]*w_/old_size[0]);
if(new_size[1]!=old_size[1])
h_=round_to_int(new_size[1]*h_/old_size[1]);
}
br_=x; return *this;
}
RendDesc &
RendDesc::set_tl_br(const Point &x, const Point &y)
{
if(FLAGS(flags, PX_ASPECT))
{
Vector new_size(y-x);
new_size[0]=abs(new_size[0]);
new_size[1]=abs(new_size[1]);
Vector old_size(tl_-br_);
old_size[0]=abs(old_size[0]);
old_size[1]=abs(old_size[1]);
if(new_size[0]!=old_size[0])
w_=round_to_int(new_size[0]*w_/old_size[0]);
if(new_size[1]!=old_size[1])
h_=round_to_int(new_size[1]*h_/old_size[1]);
}
tl_=x;
br_=y;
return *this;
}
RendDesc &
RendDesc::set_viewport(const Point &__tl, const Point &__br)
{ tl_=__tl; br_=__br; return *this; }
RendDesc &
RendDesc::set_viewport(Vector::value_type a, Vector::value_type b, Vector::value_type c, Vector::value_type d)
{ tl_=Point(a,b); br_=Point(c,d); return *this; }
Real
RendDesc::get_pw()const
{
return w_ ? (br_[0] - tl_[0]) / w_ : 0;
}
Real
RendDesc::get_ph()const
{
return h_ ? (br_[1] - tl_[1]) / h_ : 0;
}
RendDesc &
RendDesc::set_subwindow(int x, int y, int w, int h)
{
const Real pw(get_pw());
const Real ph(get_ph());
tl_[0]+=pw*x;
tl_[1]+=ph*y;
br_[0]-=pw*(w_-(x+w));
br_[1]-=ph*(h_-(y+h));
w_=w;
h_=h;
return *this;
}
RendDesc &
RendDesc::set_duration(Time duration)
{
if(get_frame_rate())
{
if(duration > Time(0.0))
set_time_end(get_time_start() + duration - Time(1/get_frame_rate()));
else
set_time_end(get_time_start());
}
return *this;
}
const Time
RendDesc::get_duration()
{
if(get_frame_rate())
return (get_time_end() - get_time_start() + Time(1/get_frame_rate()));
return Time(0.0);
}
|