converted more mixed tab/space indentations to tabs. only whitespace changes.
[blender.git] / source / blender / nodes / intern / TEX_nodes / TEX_image.c
1 /*
2  * $Id$
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): Robin Allen
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/nodes/intern/TEX_nodes/TEX_image.c
31  *  \ingroup texnodes
32  */
33
34
35 #include "../TEX_util.h"
36 #include "TEX_node.h"
37
38 static bNodeSocketType outputs[]= {
39         { SOCK_RGBA, 0, "Image",  0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
40         { -1, 0, "" }
41 };
42
43 static void colorfn(float *out, TexParams *p, bNode *node, bNodeStack **UNUSED(in), short UNUSED(thread))
44 {
45         float x = p->co[0];
46         float y = p->co[1];
47         Image *ima= (Image *)node->id;
48         ImageUser *iuser= (ImageUser *)node->storage;
49         
50         if( ima ) {
51                 ImBuf *ibuf = BKE_image_get_ibuf(ima, iuser);
52                 if( ibuf ) {
53                         float xsize, ysize;
54                         float xoff, yoff;
55                         int px, py;
56                         
57                         float *result;
58
59                         xsize = ibuf->x / 2;
60                         ysize = ibuf->y / 2;
61                         xoff = yoff = -1;
62                                         
63                         px = (int)( (x-xoff) * xsize );
64                         py = (int)( (y-yoff) * ysize );
65                 
66                         if( (!xsize) || (!ysize) ) return;
67                         
68                         if( !ibuf->rect_float ) {
69                                 BLI_lock_thread(LOCK_IMAGE);
70                                 if( !ibuf->rect_float )
71                                         IMB_float_from_rect(ibuf);
72                                 BLI_unlock_thread(LOCK_IMAGE);
73                         }
74                         
75                         while( px < 0 ) px += ibuf->x;
76                         while( py < 0 ) py += ibuf->y;
77                         while( px >= ibuf->x ) px -= ibuf->x;
78                         while( py >= ibuf->y ) py -= ibuf->y;
79                         
80                         result = ibuf->rect_float + py*ibuf->x*4 + px*4;
81                         QUATCOPY( out, result );
82                 }
83         }
84 }
85
86 static void exec(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
87 {
88         tex_output(node, in, out[0], &colorfn, data);
89 }
90
91 static void init(bNode* node)
92 {
93         ImageUser *iuser= MEM_callocN(sizeof(ImageUser), "node image user");
94         node->storage= iuser;
95         iuser->sfra= 1;
96         iuser->fie_ima= 2;
97         iuser->ok= 1;
98 }
99
100 void register_node_type_tex_image(ListBase *lb)
101 {
102         static bNodeType ntype;
103         
104         node_type_base(&ntype, TEX_NODE_IMAGE, "Image", NODE_CLASS_INPUT, NODE_PREVIEW|NODE_OPTIONS,
105                                    NULL, outputs);
106         node_type_size(&ntype, 120, 80, 300);
107         node_type_init(&ntype, init);
108         node_type_storage(&ntype, "ImageUser", node_free_standard_storage, node_copy_standard_storage);
109         node_type_exec(&ntype, exec);
110         
111         nodeRegisterType(lb, &ntype);
112 }