-/*! ========================================================================
-** Sinfg
-** Template File
-** $Id: warp.cpp,v 1.2 2005/01/24 05:00:18 darco Exp $
+/* === S Y N F I G ========================================================= */
+/*! \file warp.cpp
+** \brief Implementation of the "Warp" layer
**
-** Copyright (c) 2002 Robert B. Quattlebaum Jr.
+** $Id$
**
-** This software and associated documentation
-** are CONFIDENTIAL and PROPRIETARY property of
-** the above-mentioned copyright holder.
+** \legal
+** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+** Copyright (c) 2007, 2008 Chris Moore
**
-** You may not copy, print, publish, or in any
-** other way distribute this software without
-** a prior written agreement with
-** the copyright holder.
+** 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
**
** === N O T E S ===========================================================
**
#endif
#include "warp.h"
-#include <sinfg/string.h>
-#include <sinfg/time.h>
-#include <sinfg/context.h>
-#include <sinfg/paramdesc.h>
-#include <sinfg/renddesc.h>
-#include <sinfg/surface.h>
-#include <sinfg/value.h>
-#include <sinfg/valuenode.h>
-#include <sinfg/transform.h>
+#include <synfig/string.h>
+#include <synfig/time.h>
+#include <synfig/context.h>
+#include <synfig/paramdesc.h>
+#include <synfig/renddesc.h>
+#include <synfig/surface.h>
+#include <synfig/value.h>
+#include <synfig/valuenode.h>
+#include <synfig/transform.h>
#include <ETL/misc>
#endif
/* === G L O B A L S ======================================================= */
-SINFG_LAYER_INIT(Warp);
-SINFG_LAYER_SET_NAME(Warp,"warp");
-SINFG_LAYER_SET_LOCAL_NAME(Warp,_("Warp"));
-SINFG_LAYER_SET_CATEGORY(Warp,_("Distortions"));
-SINFG_LAYER_SET_VERSION(Warp,"0.1");
-SINFG_LAYER_SET_CVS_ID(Warp,"$Id: warp.cpp,v 1.2 2005/01/24 05:00:18 darco Exp $");
+SYNFIG_LAYER_INIT(Warp);
+SYNFIG_LAYER_SET_NAME(Warp,"warp");
+SYNFIG_LAYER_SET_LOCAL_NAME(Warp,N_("Warp"));
+SYNFIG_LAYER_SET_CATEGORY(Warp,N_("Distortions"));
+SYNFIG_LAYER_SET_VERSION(Warp,"0.1");
+SYNFIG_LAYER_SET_CVS_ID(Warp,"$Id$");
/* === P R O C E D U R E S ================================================= */
src_br (2,-2),
dest_tl (-1.8,2.1),
dest_tr (1.8,2.1),
- dest_br (2.2,-2),
dest_bl (-2.2,-2),
+ dest_br (2.2,-2),
clip (true)
{
sync();
cache_e=(-dest_tl[1]+dest_tr[1])/(src_br[0]-src_tl[0]);
cache_f=(-dest_tl[1]+dest_bl[1])/(src_br[1]-src_tl[1]);
cache_i=(dest_tl[1]-dest_tr[1]+dest_br[1]-dest_bl[1])/((src_br[1]-src_tl[1])*(src_br[0]-src_tl[0]));
- cache_j=dest_tl[1];
+ cache_j=dest_tl[1];
*/
-
+
/* matrix[2][0]=(dest_tl[0]-dest_tr[0]+dest_br[0]-dest_bl[0])/((src_br[1]-src_tl[1])*(src_br[0]-src_tl[0]));
matrix[2][1]=(dest_tl[1]-dest_tr[1]+dest_br[1]-dest_bl[1])/((src_br[1]-src_tl[1])*(src_br[0]-src_tl[0]));
matrix[2][2]=quad_area(dest_tl,dest_tr,dest_br,dest_bl)/((src_br[1]-src_tl[1])*(src_br[0]-src_tl[0]));
-
+
matrix[0][0]=-(-dest_tl[1]+dest_tr[1])/(src_br[0]-src_tl[0]);
matrix[0][1]=-(-dest_tl[1]+dest_bl[1])/(src_br[1]-src_tl[1]);
const Real& y1(min(src_br[1],src_tl[1]));
const Real& x2(max(src_br[0],src_tl[0]));
const Real& y2(max(src_br[1],src_tl[1]));
-
+
Real tx1(dest_bl[0]);
Real ty1(dest_bl[1]);
Real tx2(dest_br[0]);
matrix[2][2] = 1.0;
}
#undef matrix
-
+
Real scaletrans[3][3]={
{ scalex, 0, -x1*scalex },
{ 0, scaley, -y1*scaley },
IMPORT_PLUS(dest_br,sync());
IMPORT(clip);
IMPORT(horizon);
-
+
return false;
}
EXPORT(dest_br);
EXPORT(clip);
EXPORT(horizon);
-
+
EXPORT_NAME();
EXPORT_VERSION();
-
- return ValueBase();
+
+ return ValueBase();
}
Layer::Vocab
Warp::get_param_vocab()const
{
Layer::Vocab ret;
-
+
ret.push_back(ParamDesc("src_tl")
.set_local_name(_("Source TL"))
.set_box("src_br")
etl::handle<const Warp> layer;
public:
Warp_Trans(const Warp* x):Transform(x->get_guid()),layer(x) { }
-
- sinfg::Vector perform(const sinfg::Vector& x)const
+
+ synfig::Vector perform(const synfig::Vector& x)const
{
return layer->transform_backward(x);
//Point pos(x-layer->origin);
//return Point(layer->cos_val*pos[0]-layer->sin_val*pos[1],layer->sin_val*pos[0]+layer->cos_val*pos[1])+layer->origin;
}
-
- sinfg::Vector unperform(const sinfg::Vector& x)const
+
+ synfig::Vector unperform(const synfig::Vector& x)const
{
return layer->transform_forward(x);
return new Warp_Trans(this);
}
-sinfg::Layer::Handle
-Warp::hit_check(sinfg::Context context, const sinfg::Point &p)const
+synfig::Layer::Handle
+Warp::hit_check(synfig::Context context, const synfig::Point &p)const
{
Point newpos(transform_forward(p));
if(!rect.is_inside(newpos))
return 0;
}
-
+
return context.hit_check(newpos);
}
Real pw=(renddesc.get_w())/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
Real ph=(renddesc.get_h())/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
-
+
if(cb && !cb->amount_complete(0,10000))
return false;
-
+
Point tl(renddesc.get_tl());
Point br(renddesc.get_br());
Rect bounding_rect;
-
+
Rect render_rect(tl,br);
Rect clip_rect(Rect::full_plane());
Rect dest_rect(dest_tl,dest_br); dest_rect.expand(dest_tr).expand(dest_bl);
surface->clear();
return true;
}
-
+
{
Rect other(render_rect);
if(clip)
other&=dest_rect;
-
+
Point min(other.get_min());
Point max(other.get_max());
-
+
bool init_point_set=false;
-
- Point trans_point[4];
+
+ // Point trans_point[4];
Point p;
- Real trans_z[4];
+ // Real trans_z[4];
Real z,minz(10000000000000.0f),maxz(0);
-
-
+
+ //! \todo checking the 4 corners for 0<=z<horizon*2 and using
+ //! only 4 corners which satisfy this condition isn't the
+ //! right thing to do. It's possible that none of the 4
+ //! corners fall within that range, and yet content of the
+ //! tile does.
p=transform_forward(min);
z=transform_backward_z(p);
if(z>0 && z<horizon*2)
bounding_rect=Rect(p);
init_point_set=true;
maxz=std::max(maxz,z);
- minz=std::min(minz,z);
+ minz=std::min(minz,z);
}
-
+
p=transform_forward(max);
z=transform_backward_z(p);
if(z>0 && z<horizon*2)
bounding_rect=Rect(p);
init_point_set=true;
maxz=std::max(maxz,z);
- minz=std::min(minz,z);
+ minz=std::min(minz,z);
}
swap(min[1],max[1]);
bounding_rect=Rect(p);
init_point_set=true;
maxz=std::max(maxz,z);
- minz=std::min(minz,z);
+ minz=std::min(minz,z);
}
-
+
p=transform_forward(max);
z=transform_backward_z(p);
if(z>0 && z<horizon*2)
bounding_rect=Rect(p);
init_point_set=true;
maxz=std::max(maxz,z);
- minz=std::min(minz,z);
+ minz=std::min(minz,z);
}
-
+
if(!init_point_set)
{
surface->set_wh(renddesc.get_w(),renddesc.get_h());
return true;
}
zoom_factor=(1+(maxz-minz));
-
+
}
#ifdef ACCEL_WARP_IS_BROKEN
return Layer::accelerated_render(context,surface,quality,renddesc, cb);
#else
-
+
/*swap(tl[1],br[1]);
bounding_rect
.expand(transform_forward(tl))
.expand(transform_forward(br))
;
swap(tl[1],br[1]);*/
-
- //sinfg::warning("given window: [%f,%f]-[%f,%f] %dx%d",tl[0],tl[1],br[0],br[1],renddesc.get_w(),renddesc.get_h());
- //sinfg::warning("Projected: [%f,%f]-[%f,%f]",bounding_rect.get_min()[0],bounding_rect.get_min()[1],bounding_rect.get_max()[0],bounding_rect.get_max()[1]);
+
+ //synfig::warning("given window: [%f,%f]-[%f,%f] %dx%d",tl[0],tl[1],br[0],br[1],renddesc.get_w(),renddesc.get_h());
+ //synfig::warning("Projected: [%f,%f]-[%f,%f]",bounding_rect.get_min()[0],bounding_rect.get_min()[1],bounding_rect.get_max()[0],bounding_rect.get_max()[1]);
// If we are clipping, then go ahead and clip to the
// source rectangle
if(clip)
clip_rect&=Rect(src_tl,src_br);
-
+
// Bound ourselves to the bounding rectangle of
// what is under us
clip_rect&=context.get_full_bounding_rect();//.expand_x(abs(zoom_factor/pw)).expand_y(abs(zoom_factor/ph));
bounding_rect&=clip_rect;
-
+
Point min_point(bounding_rect.get_min());
Point max_point(bounding_rect.get_max());
-
-
+
+ // we're going to divide by the difference of these pairs soon;
+ // if they're the same, we'll be dividing by zero, and we don't
+ // want to do that!
+ // \todo what should we do in this case?
+ if (min_point[0] == max_point[0]) max_point[0] += 0.001;
+ if (min_point[1] == max_point[1]) max_point[1] += 0.001;
+
if(tl[0]>br[0])
{
tl[0]=max_point[0];
br[1]=max_point[1];
tl[1]=min_point[1];
}
-
-
+
+
const int tmp_d(max(renddesc.get_w(),renddesc.get_h()));
Real src_pw=(tmp_d*zoom_factor)/(br[0]-tl[0]);
Real src_ph=(tmp_d*zoom_factor)/(br[1]-tl[1]);
-
+
RendDesc desc(renddesc);
desc.clear_flags();
//desc.set_flags(RendDesc::PX_ASPECT);
desc.set_br(br);
desc.set_wh(ceil_to_int(src_pw*(br[0]-tl[0])),ceil_to_int(src_ph*(br[1]-tl[1])));
- //sinfg::warning("surface to render: [%f,%f]-[%f,%f] %dx%d",desc.get_tl()[0],desc.get_tl()[1],desc.get_br()[0],desc.get_br()[1],desc.get_w(),desc.get_h());
+ //synfig::warning("surface to render: [%f,%f]-[%f,%f] %dx%d",desc.get_tl()[0],desc.get_tl()[1],desc.get_br()[0],desc.get_br()[1],desc.get_w(),desc.get_h());
if(desc.get_w()==0 && desc.get_h()==0)
{
surface->set_wh(renddesc.get_w(),renddesc.get_h());
src_pw=(desc.get_w())/(desc.get_br()[0]-desc.get_tl()[0]);
src_ph=(desc.get_h())/(desc.get_br()[1]-desc.get_tl()[1]);
-
+
Surface source;
source.set_wh(desc.get_w(),desc.get_h());
if(!context.accelerated_render(&source,quality,desc,&stageone))
return false;
-
+
surface->set_wh(renddesc.get_w(),renddesc.get_h());
surface->clear();
-
+
Surface::pen pen(surface->begin());
if(quality<=4)
(*surface)[y][x]=Color::alpha();
continue;
}
-
+
u=(tmp[0]-tl[0])*src_pw;
v=(tmp[1]-tl[1])*src_ph;
-
+
if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
- {
(*surface)[y][x]=context.get_color(tmp);
- }
- else
+ else
(*surface)[y][x]=source.cubic_sample(u,v);
}
- if(y&31==0 && cb)
+ if((y&31)==0 && cb)
{
if(!stagetwo.amount_complete(y,surface->get_h()))
return false;
(*surface)[y][x]=Color::alpha();
continue;
}
-
+
u=(tmp[0]-tl[0])*src_pw;
v=(tmp[1]-tl[1])*src_ph;
-
+
if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
- {
- if(clip)
- (*surface)[y][x]=Color::alpha();
- else
- (*surface)[y][x]=context.get_color(tmp);
- }
+ (*surface)[y][x]=context.get_color(tmp);
else
(*surface)[y][x]=source.linear_sample(u,v);
}
- if(y&31==0 && cb)
+ if((y&31)==0 && cb)
{
if(!stagetwo.amount_complete(y,surface->get_h()))
return false;
(*surface)[y][x]=Color::alpha();
continue;
}
-
+
u=(tmp[0]-tl[0])*src_pw;
v=(tmp[1]-tl[1])*src_ph;
-
+
if(u<0 || v<0 || u>=source.get_w() || v>=source.get_h() || isnan(u) || isnan(v))
- {
- if(clip)
- (*surface)[y][x]=Color::alpha();
- else
- (*surface)[y][x]=context.get_color(tmp);
- }
+ (*surface)[y][x]=context.get_color(tmp);
else
- //pen.set_value(source[v][u]);
- (*surface)[y][x]=source[floor_to_int(v)][floor_to_int(u)];
+ //pen.set_value(source[v][u]);
+ (*surface)[y][x]=source[floor_to_int(v)][floor_to_int(u)];
}
- if(y&31==0 && cb)
+ if((y&31)==0 && cb)
{
if(!stagetwo.amount_complete(y,surface->get_h()))
return false;
return true;
}
-sinfg::Rect
+synfig::Rect
Warp::get_bounding_rect()const
-{
+{
return Rect::full_plane();
}
-sinfg::Rect
+synfig::Rect
Warp::get_full_bounding_rect(Context context)const
{
// return Rect::full_plane();
-
+
Rect under(context.get_full_bounding_rect());
if(clip)
{
under&=Rect(src_tl,src_br);
}
-
+
return get_transform()->perform(under);
-
+
/*
Rect under(context.get_full_bounding_rect());
Rect ret(Rect::zero());
-
+
if(under.area()==HUGE_VAL)
return Rect::full_plane();
-
+
ret.expand(
transform_backward(
under.get_min()