/* only supports RGBA nodes now */
static void node_composit_exec_rotate(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
{

	if(out[0]->hasoutput==0)
		return;

	if(in[0]->data) {
		CompBuf *cbuf= typecheck_compbuf(in[0]->data, CB_RGBA);
		CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1);	/* note, this returns zero'd image */
		float rad, u, v, s, c, centx, centy, miny, maxy, minx, maxx;
		int x, y, yo, xo;
		ImBuf *ibuf, *obuf;

		rad= (M_PI*in[1]->vec[0])/180.0f;

		s= sin(rad);
		c= cos(rad);
		centx= cbuf->x/2;
		centy= cbuf->y/2;

		minx= -centx;
		maxx= -centx + (float)cbuf->x;
		miny= -centy;
		maxy= -centy + (float)cbuf->y;


		ibuf=IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
		obuf=IMB_allocImBuf(stackbuf->x, stackbuf->y, 32, 0);

		if(ibuf && obuf){
			ibuf->rect_float=cbuf->rect;
			obuf->rect_float=stackbuf->rect;

			for(y=miny; y<maxy; y++) {
				yo= y+(int)centy;

				for(x=minx; x<maxx;x++) {
					u=c*x + y*s + centx;
					v=-s*x + c*y + centy;
					xo= x+(int)centx;

					switch(node->custom1) {
					case 0:
						neareast_interpolation(ibuf, obuf, u, v, xo, yo);
						break ;
					case 1:
						bilinear_interpolation(ibuf, obuf, u, v, xo, yo);
						break;
					case 2:
						bicubic_interpolation(ibuf, obuf, u, v, xo, yo);
						break;
					}

				}
			}

			/* rotate offset vector too, but why negative rad, ehh?? Has to be replaced with [3][3] matrix once (ton) */
			s= sin(-rad);
			c= cos(-rad);
			centx= (float)cbuf->xof; centy= (float)cbuf->yof;
			stackbuf->xof= (int)( c*centx + s*centy);
			stackbuf->yof= (int)(-s*centx + c*centy);

			IMB_freeImBuf(ibuf);
			IMB_freeImBuf(obuf);
		}

		/* pass on output and free */
		out[0]->data= stackbuf;
		if(cbuf!=in[0]->data) {
			free_compbuf(cbuf);
		}
	}
}
Exemple #2
0
CompBuf* node_composit_transform(CompBuf *cbuf, float x, float y, float angle, float scale, int filter_type)
{
	CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1);
	ImBuf *ibuf, *obuf;
	float mat[4][4], lmat[4][4], rmat[4][4], smat[4][4], cmat[4][4], icmat[4][4];
	float svec[3]= {scale, scale, scale}, loc[2]= {x, y};

	unit_m4(rmat);
	unit_m4(lmat);
	unit_m4(smat);
	unit_m4(cmat);

	/* image center as rotation center */
	cmat[3][0]= (float)cbuf->x/2.0f;
	cmat[3][1]= (float)cbuf->y/2.0f;
	invert_m4_m4(icmat, cmat);

	size_to_mat4(smat, svec);		/* scale matrix */
	add_v2_v2(lmat[3], loc);		/* tranlation matrix */
	rotate_m4(rmat, 'Z', angle);	/* rotation matrix */

	/* compose transformation matrix */
	mul_serie_m4(mat, lmat, cmat, rmat, smat, icmat, NULL, NULL, NULL);

	invert_m4(mat);

	ibuf= IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
	obuf= IMB_allocImBuf(stackbuf->x, stackbuf->y, 32, 0);

	if (ibuf && obuf) {
		int i, j;

		ibuf->rect_float= cbuf->rect;
		obuf->rect_float= stackbuf->rect;

		for (j=0; j<cbuf->y; j++) {
			for (i=0; i<cbuf->x;i++) {
				float vec[3]= {i, j, 0};

				mul_v3_m4v3(vec, mat, vec);

				switch(filter_type) {
					case 0:
						neareast_interpolation(ibuf, obuf, vec[0], vec[1], i, j);
						break;
					case 1:
						bilinear_interpolation(ibuf, obuf, vec[0], vec[1], i, j);
						break;
					case 2:
						bicubic_interpolation(ibuf, obuf, vec[0], vec[1], i, j);
						break;
				}
			}
		}

		IMB_freeImBuf(ibuf);
		IMB_freeImBuf(obuf);
	}

	/* pass on output and free */
	return stackbuf;
}