Blender  V2.59
CMP_directionalblur.c
Go to the documentation of this file.
00001 /*
00002  *
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version. 
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2006 Blender Foundation.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): Alfredo de Greef  (eeshlo)
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #include "../CMP_util.h"
00035 
00036 static bNodeSocketType cmp_node_dblur_in[]= {
00037         {       SOCK_RGBA, 1, "Image", 0.8f, 0.8f, 0.8f, 1.f, 0.f, 1.f},
00038         {       -1, 0, ""       }
00039 };
00040 
00041 static bNodeSocketType cmp_node_dblur_out[]= {
00042         {       SOCK_RGBA, 0, "Image", 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
00043         {       -1, 0, ""       }
00044 };
00045 
00046 static CompBuf *dblur(bNode *node, CompBuf *img, int iterations, int wrap,
00047                   float center_x, float center_y, float dist, float angle, float spin, float zoom)
00048 {
00049         if ((dist != 0.f) || (spin != 0.f) || (zoom != 0.f)) {
00050                 void (*getpix)(CompBuf*, float, float, float*) = wrap ? qd_getPixelLerpWrap : qd_getPixelLerp;
00051                 const float a= angle * (float)M_PI / 180.f;
00052                 const float itsc= 1.f / pow(2.f, (float)iterations);
00053                 float D;
00054                 float center_x_pix, center_y_pix;
00055                 float tx, ty;
00056                 float sc, rot;
00057                 CompBuf *tmp;
00058                 int i, j;
00059                 
00060                 tmp= dupalloc_compbuf(img);
00061                 
00062                 D= dist * sqrtf(img->x * img->x + img->y * img->y);
00063                 center_x_pix= center_x * img->x;
00064                 center_y_pix= center_y * img->y;
00065 
00066                 tx=  itsc * D * cos(a);
00067                 ty= -itsc * D * sin(a);
00068                 sc=  itsc * zoom;
00069                 rot= itsc * spin * (float)M_PI / 180.f;
00070 
00071                 /* blur the image */
00072                 for(i= 0; i < iterations; ++i) {
00073                         const float cs= cos(rot), ss= sin(rot);
00074                         const float isc= 1.f / (1.f + sc);
00075                         unsigned int x, y;
00076                         float col[4]= {0,0,0,0};
00077 
00078                         for(y= 0; y < img->y; ++y) {
00079                                 const float v= isc * (y - center_y_pix) + ty;
00080 
00081                                 for(x= 0; x < img->x; ++x) {
00082                                         const float  u= isc * (x - center_x_pix) + tx;
00083                                         unsigned int p= (x + y * img->x) * img->type;
00084 
00085                                         getpix(tmp, cs * u + ss * v + center_x_pix, cs * v - ss * u + center_y_pix, col);
00086 
00087                                         /* mix img and transformed tmp */
00088                                         for(j= 0; j < 4; ++j)
00089                                                 img->rect[p + j]= AVG2(img->rect[p + j], col[j]);
00090                                 }
00091                         }
00092 
00093                         /* copy img to tmp */
00094                         if(i != (iterations - 1)) 
00095                                 memcpy(tmp->rect, img->rect, sizeof(float) * img->x * img->y * img->type);
00096 
00097                         /* double transformations */
00098                         tx *= 2.f, ty  *= 2.f;
00099                         sc *= 2.f, rot *= 2.f;
00100 
00101                         if(node->exec & NODE_BREAK) break;
00102                 }
00103 
00104                 free_compbuf(tmp);
00105         }
00106 
00107         return img;
00108 }
00109 
00110 static void node_composit_exec_dblur(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
00111 {
00112         NodeDBlurData *ndbd= node->storage;
00113         CompBuf *new, *img= in[0]->data;
00114         
00115         if((img == NULL) || (out[0]->hasoutput == 0)) return;
00116 
00117         if (img->type != CB_RGBA)
00118                 new = typecheck_compbuf(img, CB_RGBA);
00119         else
00120                 new = dupalloc_compbuf(img);
00121         
00122         out[0]->data= dblur(node, new, ndbd->iter, ndbd->wrap, ndbd->center_x, ndbd->center_y, ndbd->distance, ndbd->angle, ndbd->spin, ndbd->zoom);
00123 }
00124 
00125 static void node_composit_init_dblur(bNode* node)
00126 {
00127         NodeDBlurData *ndbd= MEM_callocN(sizeof(NodeDBlurData), "node dblur data");
00128         node->storage= ndbd;
00129         ndbd->center_x= 0.5;
00130         ndbd->center_y= 0.5;
00131 }
00132 
00133 void register_node_type_cmp_dblur(ListBase *lb)
00134 {
00135         static bNodeType ntype;
00136 
00137         node_type_base(&ntype, CMP_NODE_DBLUR, "Directional Blur", NODE_CLASS_OP_FILTER, NODE_OPTIONS,
00138                 cmp_node_dblur_in, cmp_node_dblur_out);
00139         node_type_size(&ntype, 150, 120, 200);
00140         node_type_init(&ntype, node_composit_init_dblur);
00141         node_type_storage(&ntype, "NodeDBlurData", node_free_standard_storage, node_copy_standard_storage);
00142         node_type_exec(&ntype, node_composit_exec_dblur);
00143 
00144         nodeRegisterType(lb, &ntype);
00145 }
00146