Blender V2.61 - r43446

node_composite_scale.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) 2006 Blender Foundation.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include "node_composite_util.h"
00034 
00035 /* **************** Scale  ******************** */
00036 
00037 static bNodeSocketTemplate cmp_node_scale_in[]= {
00038     {   SOCK_RGBA, 1, "Image",          1.0f, 1.0f, 1.0f, 1.0f},
00039     {   SOCK_FLOAT, 1, "X",             1.0f, 0.0f, 0.0f, 0.0f, 0.0001f, CMP_SCALE_MAX, PROP_FACTOR},
00040     {   SOCK_FLOAT, 1, "Y",             1.0f, 0.0f, 0.0f, 0.0f, 0.0001f, CMP_SCALE_MAX, PROP_FACTOR},
00041     {   -1, 0, ""   }
00042 };
00043 static bNodeSocketTemplate cmp_node_scale_out[]= {
00044     {   SOCK_RGBA, 0, "Image"},
00045     {   -1, 0, ""   }
00046 };
00047 
00048 /* only supports RGBA nodes now */
00049 /* node->custom1 stores if input values are absolute or relative scale */
00050 static void node_composit_exec_scale(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
00051 {
00052     if(out[0]->hasoutput==0)
00053         return;
00054     
00055     if(in[0]->data) {
00056         RenderData *rd= data;
00057         CompBuf *stackbuf, *cbuf= typecheck_compbuf(in[0]->data, CB_RGBA);
00058         ImBuf *ibuf;
00059         int newx, newy;
00060         
00061         if(node->custom1==CMP_SCALE_RELATIVE) {
00062             newx= MAX2((int)(in[1]->vec[0]*cbuf->x), 1);
00063             newy= MAX2((int)(in[2]->vec[0]*cbuf->y), 1);
00064         }
00065         else if(node->custom1==CMP_SCALE_SCENEPERCENT) {
00066             newx = cbuf->x * (rd->size / 100.0f);
00067             newy = cbuf->y * (rd->size / 100.0f);
00068         }
00069         else if (node->custom1==CMP_SCALE_RENDERPERCENT) {
00070             newx= (rd->xsch * rd->size)/100;
00071             newy= (rd->ysch * rd->size)/100;
00072         } else {    /* CMP_SCALE_ABSOLUTE */
00073             newx= MAX2((int)in[1]->vec[0], 1);
00074             newy= MAX2((int)in[2]->vec[0], 1);
00075         }
00076         newx= MIN2(newx, CMP_SCALE_MAX);
00077         newy= MIN2(newy, CMP_SCALE_MAX);
00078 
00079         ibuf= IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
00080         if(ibuf) {
00081             ibuf->rect_float= cbuf->rect;
00082             IMB_scaleImBuf(ibuf, newx, newy);
00083             
00084             if(ibuf->rect_float == cbuf->rect) {
00085                 /* no scaling happened. */
00086                 stackbuf= pass_on_compbuf(in[0]->data);
00087             }
00088             else {
00089                 stackbuf= alloc_compbuf(newx, newy, CB_RGBA, 0);
00090                 stackbuf->rect= ibuf->rect_float;
00091                 stackbuf->malloc= 1;
00092             }
00093 
00094             ibuf->rect_float= NULL;
00095             ibuf->mall &= ~IB_rectfloat;
00096             IMB_freeImBuf(ibuf);
00097             
00098             /* also do the translation vector */
00099             stackbuf->xof = (int)(((float)newx/(float)cbuf->x) * (float)cbuf->xof);
00100             stackbuf->yof = (int)(((float)newy/(float)cbuf->y) * (float)cbuf->yof);
00101         }
00102         else {
00103             stackbuf= dupalloc_compbuf(cbuf);
00104             printf("Scaling to %dx%d failed\n", newx, newy);
00105         }
00106         
00107         out[0]->data= stackbuf;
00108         if(cbuf!=in[0]->data)
00109             free_compbuf(cbuf);
00110     }
00111     else if (node->custom1==CMP_SCALE_ABSOLUTE) {
00112         CompBuf *stackbuf;
00113         int a, x, y;
00114         float *fp;
00115 
00116         x = MAX2((int)in[1]->vec[0], 1);
00117         y = MAX2((int)in[2]->vec[0], 1);
00118 
00119         stackbuf = alloc_compbuf(x, y, CB_RGBA, 1);
00120         fp = stackbuf->rect;
00121 
00122         a = stackbuf->x * stackbuf->y;
00123         while(a--) {
00124             copy_v4_v4(fp, in[0]->vec);
00125             fp += 4;
00126         }
00127 
00128         out[0]->data= stackbuf;
00129     }
00130 }
00131 
00132 void register_node_type_cmp_scale(bNodeTreeType *ttype)
00133 {
00134     static bNodeType ntype;
00135 
00136     node_type_base(ttype, &ntype, CMP_NODE_SCALE, "Scale", NODE_CLASS_DISTORT, NODE_OPTIONS);
00137     node_type_socket_templates(&ntype, cmp_node_scale_in, cmp_node_scale_out);
00138     node_type_size(&ntype, 140, 100, 320);
00139     node_type_exec(&ntype, node_composit_exec_scale);
00140 
00141     nodeRegisterType(ttype, &ntype);
00142 }