I am trying to replace a part of an image with a rotated version of another one. The image source should appear in such a way that origin_source ends up at orign_dest in image *this. Also source should be rotated around origin_source before replacing pixels.
The code below works when R is a mirror matrix, but if I actually makes it to a rotation matrix, the resulting image becomes sheared. What is wrong?
void Image::imageApply(const Image& source,const Point& origin_dest,const Point& origin_source,const Point& direction)
{
Matrix22<double> R=transformRotationCreate(direction);
Point source_new_size=sqrt(2)*((Point){source.widthGet(),source.heightGet()});
Point blit_start=origin_dest;
for(unsigned int k=0;k<source_new_size.y;k++)
{
for(unsigned int l=0;l<source_new_size.x;l++)
{
Point point_source=(Point){l,k};
Point point_dest=point_source-origin_source;
point_dest*=R;
point_dest+=blit_start;
if(point_source.rectangleInIs((Point){0,0},(Point){source.widthGet(),source.heightGet()})
&&point_dest.rectangleInIs((Point){0,0},(Point){widthGet(),heightGet()}))
{
(*this)(point_dest)=source(point_source);
}
}
}
}
Here are some other functions used:
T=double
template<class T>
struct Matrix22
{
T xx;
T xy;
T yx;
T yy;
};
direction is a normalized vector
inline Matrix22<double> transformRotationCreate(const Vector2d<double>& direction)
{
return (Matrix22<double>){direction.x, -direction.y, direction.y, direction.x};
}
Also
Vector2d<T>& operator*=(const Matrix22<T>& M)
{
x=x*M.xx + y*M.xy;
y=x*M.yx + y*M.yy;
return *this;
}
I solved it
First of all, the matrix-vector multiplication operator was wrong:
The final “rotate-and-paste” routine looks like this:
Finally, the rotation direction was wrong, but that is just a swap of the xy factors in the transformation.