/* -*- mia-c++ -*-
*
* Copyright (c) 2007 Gert Wollny <gert dot wollny at acm dot org>
*
* This program 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 program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <mia/2d/transform.hh>
#include <mia/2d/deformer.hh>
NS_MIA_BEGIN
C2DGridTransformation::C2DGridTransformation(const C2DBounds& size, P2DInterpolatorFactory ipf):
_M_field(size),
_M_ipf(ipf)
{
}
C2DFVectorfield& C2DGridTransformation::field()
{
return _M_field;
}
P2DInterpolatorFactory C2DGridTransformation::get_ipf()
{
return _M_ipf;
}
C2DGridTransformation& C2DGridTransformation::operator += (const C2DGridTransformation& other)
{
_M_field = (*this + other).field();
return *this;
}
C2DGridTransformation C2DGridTransformation::operator + (const C2DGridTransformation& other)
{
assert(_M_field.get_size() == other._M_field.get_size());
C2DGridTransformation tmp(_M_field.get_size(), _M_ipf);
C2DFVectorfield::iterator itmp = tmp._M_field.begin();
C2DFVectorfield::const_iterator ib = other._M_field.begin();
for (size_t y = 0; y < _M_field.get_size().y; ++y) {
for (size_t x = 0; x < _M_field.get_size().x; ++x, ++itmp, ++ib) {
C2DFVector xi = C2DFVector(x, y) - *ib;
*itmp = _M_field.get_interpol_val_at(xi) + *ib;
}
}
return tmp;
}
void C2DGridTransformation::apply(const C2DImage& input, C2DImage& output) const
{
FDeformer2D deformer(_M_field, *_M_ipf);
output = *mia::filter(deformer, input);
}
C2DGridTransformation C2DGridTransformation::upscale(const C2DBounds& size)
{
C2DGridTransformation result(size, _M_ipf);
// initial upscale
if (_M_field.get_size().x == 0 || _M_field.get_size().y == 0)
return result;
float x_mult = float(size.x) / (float)_M_field.get_size().x;
float y_mult = float(size.y) / (float)_M_field.get_size().y;
float ix_mult = 1.0f / x_mult;
float iy_mult = 1.0f / y_mult;
C2DFVectorfield::iterator i = result._M_field.begin();
for (unsigned int y = 0; y < size.y; y++){
for (unsigned int x = 0; x < size.x; x++,++i){
C2DFVector help(ix_mult * x, iy_mult * y);
C2DFVector val = _M_field.get_interpol_val_at(help);
*i = C2DFVector(val.x * x_mult,val.y * y_mult);
}
}
return result;
}
NS_MIA_END