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