converted more mixed tab/space indentations to tabs. only whitespace changes.
[blender.git] / source / blender / nodes / intern / CMP_nodes / CMP_crop.c
1 /*
2  *
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2006 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Juho Vepsäläinen
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/nodes/intern/CMP_nodes/CMP_crop.c
31  *  \ingroup cmpnodes
32  */
33
34
35 #include "../CMP_util.h"
36
37 /* **************** Crop  ******************** */
38
39 static bNodeSocketType cmp_node_crop_in[]= {
40         {       SOCK_RGBA, 1, "Image",                  0.8f, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f},
41         {       -1, 0, ""       }
42 };
43 static bNodeSocketType cmp_node_crop_out[]= {
44         {       SOCK_RGBA, 0, "Image",                  0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
45         {       -1, 0, ""       }
46 };
47
48 static void node_composit_exec_crop(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
49 {
50         if(in[0]->data) {
51                 NodeTwoXYs *ntxy= node->storage;
52                 CompBuf *cbuf= in[0]->data;
53                 CompBuf *stackbuf;
54                 int x, y;
55                 float *srcfp, *outfp;
56                 rcti outputrect;
57
58                 if(node->custom2) {
59                         ntxy->x1= cbuf->x* ntxy->fac_x1;
60                         ntxy->x2= cbuf->x* ntxy->fac_x2;
61                         ntxy->y1= cbuf->y* ntxy->fac_y1;
62                         ntxy->y2= cbuf->y* ntxy->fac_y2;
63                 }
64
65                 /* check input image size */
66                 if(cbuf->x <= ntxy->x1 + 1)
67                         ntxy->x1= cbuf->x - 1;
68
69                 if(cbuf->y <= ntxy->y1 + 1)
70                         ntxy->y1= cbuf->y - 1;
71
72                 if(cbuf->x <= ntxy->x2 + 1)
73                         ntxy->x2= cbuf->x - 1;
74
75                 if(cbuf->y <= ntxy->y2 + 1)
76                         ntxy->y2= cbuf->y - 1;
77
78                 /* figure out the minimums and maximums */
79                 outputrect.xmax=MAX2(ntxy->x1, ntxy->x2) + 1;
80                 outputrect.xmin=MIN2(ntxy->x1, ntxy->x2);
81                 outputrect.ymax=MAX2(ntxy->y1, ntxy->y2) + 1;
82                 outputrect.ymin=MIN2(ntxy->y1, ntxy->y2);
83
84                 if(node->custom1) {
85                         /* this option crops the image size too  */     
86                         stackbuf= get_cropped_compbuf(&outputrect, cbuf->rect, cbuf->x, cbuf->y, cbuf->type);
87                 }
88                 else {
89                         /* this option won't crop the size of the image as well  */
90                         /* allocate memory for the output image            */
91                         stackbuf = alloc_compbuf(cbuf->x, cbuf->y, cbuf->type, 1);
92
93                         /* select the cropped part of the image and set it to the output */
94                         for(y=outputrect.ymin; y<outputrect.ymax; y++){
95                                 srcfp= cbuf->rect     + (y * cbuf->x     + outputrect.xmin) * cbuf->type;
96                                 outfp= stackbuf->rect + (y * stackbuf->x + outputrect.xmin) * stackbuf->type;
97                                 for(x=outputrect.xmin; x<outputrect.xmax; x++, outfp+= stackbuf->type, srcfp+= cbuf->type)
98                                                         memcpy(outfp, srcfp, sizeof(float)*stackbuf->type);
99                         }
100                 }
101
102                 out[0]->data= stackbuf;
103         }
104 }
105
106 static void node_composit_init_crop(bNode* node)
107 {
108         NodeTwoXYs *nxy= MEM_callocN(sizeof(NodeTwoXYs), "node xy data");
109         node->storage= nxy;
110         nxy->x1= 0;
111         nxy->x2= 0;
112         nxy->y1= 0;
113         nxy->y2= 0;
114 }
115
116 void register_node_type_cmp_crop(ListBase *lb)
117 {
118         static bNodeType ntype;
119
120         node_type_base(&ntype, CMP_NODE_CROP, "Crop", NODE_CLASS_DISTORT, NODE_OPTIONS,
121                 cmp_node_crop_in, cmp_node_crop_out);
122         node_type_size(&ntype, 140, 100, 320);
123         node_type_init(&ntype, node_composit_init_crop);
124         node_type_storage(&ntype, "NodeTwoXYs", node_free_standard_storage, node_copy_standard_storage);
125         node_type_exec(&ntype, node_composit_exec_crop);
126
127         nodeRegisterType(lb, &ntype);
128 }
129