Blender V2.61 - r43446

node_composite_transform.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version. 
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2011 Blender Foundation.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): Blender Foundation,
00024  *                 Sergey Sharybin
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00033 #include "node_composite_util.h"
00034 
00035 /* **************** Transform  ******************** */
00036 
00037 static bNodeSocketTemplate cmp_node_transform_in[]= {
00038     {   SOCK_RGBA,      1,  "Image",            0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
00039     {   SOCK_FLOAT,     1,  "X",                0.0f, 0.0f, 0.0f, 0.0f, -10000.0f, 10000.0f},
00040     {   SOCK_FLOAT,     1,  "Y",                0.0f, 0.0f, 0.0f, 0.0f, -10000.0f, 10000.0f},
00041     {   SOCK_FLOAT,     1,  "Angle",            0.0f, 0.0f, 0.0f, 0.0f, -10000.0f, 10000.0f, PROP_ANGLE},
00042     {   SOCK_FLOAT,     1,  "Scale",            1.0f, 0.0f, 0.0f, 0.0f, 0.0001f, CMP_SCALE_MAX},
00043     {   -1, 0, ""   }
00044 };
00045 
00046 static bNodeSocketTemplate cmp_node_transform_out[]= {
00047     {   SOCK_RGBA, 0, "Image"},
00048     {   -1, 0, ""   }
00049 };
00050 
00051 CompBuf* node_composit_transform(CompBuf *cbuf, float x, float y, float angle, float scale, int filter_type)
00052 {
00053     CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1);
00054     ImBuf *ibuf, *obuf;
00055     float mat[4][4], lmat[4][4], rmat[4][4], smat[4][4], cmat[4][4], icmat[4][4];
00056     float svec[3]= {scale, scale, scale}, loc[2]= {x, y};
00057 
00058     unit_m4(rmat);
00059     unit_m4(lmat);
00060     unit_m4(smat);
00061     unit_m4(cmat);
00062 
00063     /* image center as rotation center */
00064     cmat[3][0]= (float)cbuf->x/2.0f;
00065     cmat[3][1]= (float)cbuf->y/2.0f;
00066     invert_m4_m4(icmat, cmat);
00067 
00068     size_to_mat4(smat, svec);       /* scale matrix */
00069     add_v2_v2(lmat[3], loc);        /* tranlation matrix */
00070     rotate_m4(rmat, 'Z', angle);    /* rotation matrix */
00071 
00072     /* compose transformation matrix */
00073     mul_serie_m4(mat, lmat, cmat, rmat, smat, icmat, NULL, NULL, NULL);
00074 
00075     invert_m4(mat);
00076 
00077     ibuf= IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
00078     obuf= IMB_allocImBuf(stackbuf->x, stackbuf->y, 32, 0);
00079 
00080     if(ibuf && obuf) {
00081         int i, j;
00082 
00083         ibuf->rect_float= cbuf->rect;
00084         obuf->rect_float= stackbuf->rect;
00085 
00086         for(j=0; j<cbuf->y; j++) {
00087             for(i=0; i<cbuf->x;i++) {
00088                 float vec[3]= {i, j, 0};
00089 
00090                 mul_v3_m4v3(vec, mat, vec);
00091 
00092                 switch(filter_type) {
00093                     case 0:
00094                         neareast_interpolation(ibuf, obuf, vec[0], vec[1], i, j);
00095                         break ;
00096                     case 1:
00097                         bilinear_interpolation(ibuf, obuf, vec[0], vec[1], i, j);
00098                         break;
00099                     case 2:
00100                         bicubic_interpolation(ibuf, obuf, vec[0], vec[1], i, j);
00101                         break;
00102                 }
00103             }
00104         }
00105 
00106         IMB_freeImBuf(ibuf);
00107         IMB_freeImBuf(obuf);
00108     }
00109 
00110     /* pass on output and free */
00111     return stackbuf;
00112 }
00113 
00114 static void node_composit_exec_transform(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
00115 {
00116     if(in[0]->data) {
00117         CompBuf *cbuf= typecheck_compbuf(in[0]->data, CB_RGBA);
00118         CompBuf *stackbuf;
00119 
00120         stackbuf= node_composit_transform(cbuf, in[1]->vec[0], in[2]->vec[0], in[3]->vec[0], in[4]->vec[0], node->custom1);
00121 
00122         /* pass on output and free */
00123         out[0]->data= stackbuf;
00124 
00125         if(cbuf!=in[0]->data)
00126             free_compbuf(cbuf);
00127     }
00128 }
00129 
00130 void register_node_type_cmp_transform(bNodeTreeType *ttype)
00131 {
00132     static bNodeType ntype;
00133 
00134     node_type_base(ttype, &ntype, CMP_NODE_TRANSFORM, "Transform", NODE_CLASS_DISTORT, NODE_OPTIONS);
00135     node_type_socket_templates(&ntype, cmp_node_transform_in, cmp_node_transform_out);
00136     node_type_size(&ntype, 140, 100, 320);
00137     node_type_exec(&ntype, node_composit_exec_transform);
00138 
00139     nodeRegisterType(ttype, &ntype);
00140 }