HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
COP2_PixelAdd.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024
3  * Side Effects Software Inc. All rights reserved.
4  *
5  * Redistribution and use of Houdini Development Kit samples in source and
6  * binary forms, with or without modification, are permitted provided that the
7  * following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. The name of Side Effects Software may not be used to endorse or
11  * promote products derived from this software without specific prior
12  * written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17  * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  *----------------------------------------------------------------------------
26  * Addness Op.
27  */
28 #include <UT/UT_DSOVersion.h>
29 #include <UT/UT_WorkBuffer.h>
30 #include <CH/CH_Manager.h>
31 #include <OP/OP_Context.h>
32 #include <OP/OP_OperatorTable.h>
33 
34 #include <PRM/PRM_Include.h>
35 #include <PRM/PRM_Parm.h>
36 
37 #include <RU/RU_PixelFunctions.h>
38 
39 #include "COP2_PixelAdd.h"
40 
41 using namespace HDK_Sample;
42 
43 COP2_PIXEL_OP_SWITCHER(1, "Sample Pixel Add");
44 
45 static PRM_Name names[] =
46 {
47  PRM_Name("addend", "Add Value"),
48 };
49 
50 
51 static PRM_Range addRange(PRM_RANGE_UI, -1, PRM_RANGE_UI, 1);
54 {
56 
58  &addRange),
59 
60  PRM_Template(),
61 };
62 
65 
67 
68 const char * COP2_PixelAdd::myInputLabels[] =
69 {
70  "Image to Add to",
71  "Mask Input",
72  0
73 };
74 
75 OP_Node *
77  const char *name,
78  OP_Operator *op)
79 {
80  return new COP2_PixelAdd(net, name, op);
81 }
82 
83 COP2_PixelAdd::COP2_PixelAdd(OP_Network *parent,
84  const char *name,
85  OP_Operator *entry)
86  : COP2_PixelOp(parent, name, entry)
87 {
88 }
89 
90 COP2_PixelAdd::~COP2_PixelAdd() { }
91 
92 
93 
94 // this is the pixel function, which does the work.
95 namespace HDK_Sample {
97 {
98 public:
99  cop2_AddFunc(float r,float g, float b, float a)
100  { myAddend[0] = r; myAddend[1] = g; myAddend[2] = b; myAddend[3] = a; }
101 protected:
102 
103  // the operation differs per component.
104  bool eachComponentDifferent() const override
105  { return true; }
106 
107  // we don't need to process all the compoents together, as a vector.
108  bool needAllComponents() const override { return false; }
109 
110 
111  // Here's the heart of the pixel function - it simply adds our addend to
112  // the passed in val for the given component. This is the scalar version.
113  static float add(RU_PixelFunction *pf, float val, int comp)
114  { return val +((cop2_AddFunc*)pf)->myAddend[comp]; }
115 
116  // we return the static function above as our scalar function.
117  RUPixelFunc getPixelFunction() const override { return add; }
118 
119  // --or--
120 
121  // These are the methods you would use for a vector function. They are
122  // not used in this case, since needAllComponents is false.
123  // You can define a function with both a scalar and a vector function,
124  // and switch between the two based on parameters.
125 
126  static void addvec(RU_PixelFunction *f, float **vals,
127  const bool *scope)
128  {
129  cop2_AddFunc *pf = (cop2_AddFunc *) f;
130 
131  if(vals[0] && scope[0]) *vals[0] = *vals[0] + pf->myAddend[0];
132  if(vals[1] && scope[1]) *vals[1] = *vals[1] + pf->myAddend[1];
133  if(vals[2] && scope[2]) *vals[2] = *vals[2] + pf->myAddend[2];
134  if(vals[3] && scope[3]) *vals[3] = *vals[3] + pf->myAddend[3];
135  }
136 
137  // we return the static function above as our vector function.
138  RUVectorFunc getVectorFunction() const override { return addvec; }
139 
140 private:
141  float myAddend[4];
142 };
143 }
144 
145 
146 
147 // This is where we create and return our a new pixel function, defined above.
148 // Parms can be evaluated in this method.
149 
151 COP2_PixelAdd::addPixelFunction(const TIL_Plane*plane,int,float t, int,int,int)
152 {
153  // The frame scope effect is only used if parms on the frame scope page
154  // are altered.
155  int index = mySequence.getImageIndex(t);
156  float effect = getFrameScopeEffect(index);
157  float r,g,b,a;
158 
159  // Note that we treat the alpha plane differently than other planes.
160  if(plane->isAlphaPlane())
161  {
162  // use the alpha value for the alpha plane.
163  r = g = b = a = ADD(3, t) * effect;
164  }
165  else
166  {
167  // all other planes, use comps 0-3.
168  r = ADD(0,t) * effect;
169  g = ADD(1,t) * effect;
170  b = ADD(2,t) * effect;
171  a = ADD(3,t) * effect;
172  }
173 
174  return new cop2_AddFunc(r,g,b,a);
175 }
176 
177 const char *
178 COP2_PixelAdd::getOperationInfo()
179 {
180  // return a small string describing what this function does in the info
181  // popup.
182  fpreal t = CHgetEvalTime();
183  int index = mySequence.getImageIndex(t);
184  float effect = getFrameScopeEffect(index);
185 
186  static UT_WorkBuffer info;
187 
188  info.sprintf("Add (%g, %g, %g, %g)",
189  ADD(0,t)*effect, ADD(1,t)*effect,
190  ADD(2,t)*effect, ADD(3,t)*effect);
191 
192  return info.buffer();
193 }
194 
195 void
197 {
198  table->addOperator(new OP_Operator("hdk_samplepixadd",
199  "HDK Sample Pixel Add",
202  1,
203  2, // optional mask input
205 }
206 
static OP_Node * myConstructor(OP_Network *, const char *, OP_Operator *)
Definition: COP2_PixelAdd.C:76
void newCop2Operator(OP_OperatorTable *table)
TIL_Sequence mySequence
Definition: COP2_Node.h:1299
static PRM_Template myTemplateList[]
Definition: COP2_PixelAdd.h:51
COP2_PIXEL_OP_SWITCHER(1,"Sample Pixel Add")
static void addvec(RU_PixelFunction *f, float **vals, const bool *scope)
GLboolean GLboolean g
Definition: glcorearb.h:1222
SYS_FORCE_INLINE const char * buffer() const
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
cop2_AddFunc(float r, float g, float b, float a)
Definition: COP2_PixelAdd.C:99
bool addOperator(OP_Operator *op, std::ostream *err=nullptr)
static OP_VariablePair myVariablePair
Definition: COP2_Node.h:619
PRM_API const PRM_Type PRM_RGB_J
RU_PixelFunction * addPixelFunction(const TIL_Plane *, int, float t, int, int, int thread) override
GLfloat f
Definition: glcorearb.h:1926
static const char * myInputLabels[]
Definition: COP2_PixelAdd.h:53
static float add(RU_PixelFunction *pf, float val, int comp)
RUVectorFunc getVectorFunction() const override
fpreal CHgetEvalTime()
Definition: CH_Manager.h:2129
GLuint const GLchar * name
Definition: glcorearb.h:786
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLenum GLenum GLsizei void * table
Definition: glad.h:5129
PRM_API const PRM_Type PRM_SWITCHER
GLdouble t
Definition: glad.h:2397
int sprintf(const char *fmt,...) SYS_PRINTF_CHECK_ATTRIBUTE(2
static OP_TemplatePair myTemplatePair
Definition: COP2_PixelOp.h:35
bool needAllComponents() const override
#define TOOL_PARM
Definition: COP2_Common.h:26
PRM_API PRM_Default PRMoneDefaults[]
PRM_API PRM_Name PRMswitcherName
float getFrameScopeEffect(int image_index) override
static OP_TemplatePair myTemplatePair
Definition: COP2_PixelAdd.h:49
fpreal64 fpreal
Definition: SYS_Types.h:277
GLuint index
Definition: glcorearb.h:786
GLuint GLfloat * val
Definition: glcorearb.h:1608
static OP_VariablePair myVariablePair
Definition: COP2_PixelAdd.h:50
RUPixelFunc getPixelFunction() const override
bool eachComponentDifferent() const override
GLboolean r
Definition: glcorearb.h:1222
void(* RUVectorFunc)(RU_PixelFunction *, float **, const bool *)
exint getImageIndex(double t, int clamp_range=1, int round_off=SEQUENCE_NEAREST) const
float(* RUPixelFunc)(RU_PixelFunction *, float, int)
bool isAlphaPlane() const