Blender  V2.93
node_texture_image.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2006 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "NOD_texture.h"
25 #include "node_texture_util.h"
26 
28  {SOCK_RGBA, N_("Image")},
29  {-1, ""},
30 };
31 
32 static void colorfn(
33  float *out, TexParams *p, bNode *node, bNodeStack **UNUSED(in), short UNUSED(thread))
34 {
35  float x = p->co[0];
36  float y = p->co[1];
37  Image *ima = (Image *)node->id;
38  ImageUser *iuser = (ImageUser *)node->storage;
39 
40  if (ima) {
41  ImBuf *ibuf = BKE_image_acquire_ibuf(ima, iuser, NULL);
42  if (ibuf) {
43  float xsize, ysize;
44  float xoff, yoff;
45  int px, py;
46 
47  const float *result;
48 
49  xsize = ibuf->x / 2;
50  ysize = ibuf->y / 2;
51  xoff = yoff = -1;
52 
53  px = (int)((x - xoff) * xsize);
54  py = (int)((y - yoff) * ysize);
55 
56  if ((!xsize) || (!ysize)) {
57  return;
58  }
59 
60  if (!ibuf->rect_float) {
62  if (!ibuf->rect_float) {
63  IMB_float_from_rect(ibuf);
64  }
66  }
67 
68  while (px < 0) {
69  px += ibuf->x;
70  }
71  while (py < 0) {
72  py += ibuf->y;
73  }
74  while (px >= ibuf->x) {
75  px -= ibuf->x;
76  }
77  while (py >= ibuf->y) {
78  py -= ibuf->y;
79  }
80 
81  result = ibuf->rect_float + py * ibuf->x * 4 + px * 4;
82  copy_v4_v4(out, result);
83 
84  BKE_image_release_ibuf(ima, ibuf, NULL);
85  }
86  }
87 }
88 
89 static void exec(void *data,
90  int UNUSED(thread),
91  bNode *node,
92  bNodeExecData *execdata,
93  bNodeStack **in,
94  bNodeStack **out)
95 {
96  tex_output(node, execdata, in, out[0], &colorfn, data);
97 }
98 
99 static void init(bNodeTree *UNUSED(ntree), bNode *node)
100 {
101  ImageUser *iuser = MEM_callocN(sizeof(ImageUser), "node image user");
102  node->storage = iuser;
103  iuser->sfra = 1;
104  iuser->ok = 1;
105  iuser->flag |= IMA_ANIM_ALWAYS;
106 }
107 
109 {
110  static bNodeType ntype;
111 
114  node_type_init(&ntype, init);
116  node_type_exec(&ntype, NULL, NULL, exec);
118 
119  nodeRegisterType(&ntype);
120 }
void BKE_image_release_ibuf(struct Image *ima, struct ImBuf *ibuf, void *lock)
Definition: image.c:5113
struct ImBuf * BKE_image_acquire_ibuf(struct Image *ima, struct ImageUser *iuser, void **r_lock)
Definition: image.c:5100
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4559
#define TEX_NODE_IMAGE
Definition: BKE_node.h:1333
void node_type_storage(struct bNodeType *ntype, const char *storagename, void(*freefunc)(struct bNode *node), void(*copyfunc)(struct bNodeTree *dest_ntree, struct bNode *dest_node, const struct bNode *src_node))
Definition: node.cc:4599
#define NODE_CLASS_INPUT
Definition: BKE_node.h:334
void node_type_exec(struct bNodeType *ntype, NodeInitExecFunction init_exec_fn, NodeFreeExecFunction free_exec_fn, NodeExecFunction exec_fn)
Definition: node.cc:4635
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
void node_type_label(struct bNodeType *ntype, void(*labelfunc)(struct bNodeTree *ntree, struct bNode *, char *label, int maxlen))
MINLINE void copy_v4_v4(float r[4], const float a[4])
void BLI_thread_unlock(int type)
Definition: threads.cc:389
void BLI_thread_lock(int type)
Definition: threads.cc:384
@ LOCK_IMAGE
Definition: BLI_threads.h:67
#define UNUSED(x)
#define N_(msgid)
#define IMA_ANIM_ALWAYS
#define NODE_PREVIEW
@ SOCK_RGBA
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
void IMB_float_from_rect(struct ImBuf *ibuf)
Definition: divers.c:780
OperationNode * node
bNodeTree * ntree
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static bNodeSocketTemplate outputs[]
void register_node_type_tex_image(void)
static void exec(void *data, int UNUSED(thread), bNode *node, bNodeExecData *execdata, bNodeStack **in, bNodeStack **out)
static void colorfn(float *out, TexParams *p, bNode *node, bNodeStack **UNUSED(in), short UNUSED(thread))
static void init(bNodeTree *UNUSED(ntree), bNode *node)
void tex_node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass, short flag)
void tex_output(bNode *node, bNodeExecData *execdata, bNodeStack **in, bNodeStack *out, TexFn texfn, TexCallData *cdata)
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:67
void node_free_standard_storage(bNode *node)
Definition: node_util.c:55
void node_image_label(bNodeTree *UNUSED(ntree), bNode *node, char *label, int maxlen)
Definition: node_util.c:199
float * rect_float
const float * co
Compact definition of a node socket.
Definition: BKE_node.h:95
Defines a node type.
Definition: BKE_node.h:221