c764c7a22d2994c3d1381248749d410ef01b227b
[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 #include "../TEX_util.h"
31
32 static bNodeSocketType outputs[]= {
33         { SOCK_RGBA, 0, "Image",  0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
34         { -1, 0, "" }
35 };
36
37 static void colorfn(float *out, TexParams *p, bNode *node, bNodeStack **UNUSED(in), short UNUSED(thread))
38 {
39         float x = p->co[0];
40         float y = p->co[1];
41         Image *ima= (Image *)node->id;
42         ImageUser *iuser= (ImageUser *)node->storage;
43         
44         if( ima ) {
45                 ImBuf *ibuf = BKE_image_get_ibuf(ima, iuser);
46                 if( ibuf ) {
47                         float xsize, ysize;
48                         float xoff, yoff;
49                         int px, py;
50                         
51                         float *result;
52
53                         xsize = ibuf->x / 2;
54                         ysize = ibuf->y / 2;
55                         xoff = yoff = -1;
56                                         
57                         px = (int)( (x-xoff) * xsize );
58                         py = (int)( (y-yoff) * ysize );
59                 
60                         if( (!xsize) || (!ysize) ) return;
61                         if( !ibuf->rect_float ) IMB_float_from_rect(ibuf);
62                         
63                         while( px < 0 ) px += ibuf->x;
64                         while( py < 0 ) py += ibuf->y;
65                         while( px >= ibuf->x ) px -= ibuf->x;
66                         while( py >= ibuf->y ) py -= ibuf->y;
67                         
68                         result = ibuf->rect_float + py*ibuf->x*4 + px*4;
69                         QUATCOPY( out, result );
70                 }
71         }
72 }
73
74 static void exec(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
75 {
76         tex_output(node, in, out[0], &colorfn, data);
77 }
78
79 static void init(bNode* node)
80 {
81    ImageUser *iuser= MEM_callocN(sizeof(ImageUser), "node image user");
82    node->storage= iuser;
83    iuser->sfra= 1;
84    iuser->fie_ima= 2;
85    iuser->ok= 1;
86 }
87
88 bNodeType tex_node_image= {
89         /* *next,*prev */       NULL, NULL,
90         /* type code   */       TEX_NODE_IMAGE,
91         /* name        */       "Image",
92         /* width+range */       120, 80, 300,
93         /* class+opts  */       NODE_CLASS_INPUT, NODE_PREVIEW|NODE_OPTIONS,
94         /* input sock  */       NULL,
95         /* output sock */       outputs,
96         /* storage     */       "ImageUser",
97         /* execfunc    */       exec,
98         /* butfunc     */       NULL,
99         /* initfunc    */       init,
100         /* freestoragefunc    */        node_free_standard_storage,
101         /* copystoragefunc    */        node_copy_standard_storage,
102         /* id          */       NULL
103 };
104