LightWrap++
AC++wrapperfortheLightWave3DSDK
imagefilter_handler.h
Go to the documentation of this file.
1 #ifndef LWPP_IMAGEFILTER_HANDLER_H
2 #define LWPP_IMAGEFILTER_HANDLER_H
3 
4 #include "lwpp/plugin_handler.h"
5 #include <set>
6 
7 #include <lwfilter.h>
8 
9 namespace LWBUF
10 {
11  enum
12  {
45  RED,
77  };
78 }
79 
80 namespace lwpp
81 {
82  namespace lw10
83  {
85  {
86  LWInstanceFuncs *inst;
87  LWItemFuncs *item;
88  void (*process) (LWInstance, const LWFilterAccess *);
89  int * (*flags) (LWInstance);
90  };
91  }
92 
93  typedef std::set<int> BufferSet;
94 
96 
101 {
102 protected:
105  public:
106  ImageFilterHandler(void *g, void *context, LWError *err)
107  : InstanceHandler(g, context, err, LWIMAGEFILTER_HCLASS),
108  m_bufferReturn(0)
109  {
110  ;
111  }
112  virtual ~ImageFilterHandler() {;}
113  virtual void Process(const LWFilterAccess *fa)
114  {
115  // default image copy
116  for ( int y = 0; y < fa->height; y++ )
117  {
118  /* get each scanline */
119  float *r = fa->getLine( LWBUF::RED, y );
120  float *g = fa->getLine( LWBUF::GREEN, y );
121  float *b = fa->getLine( LWBUF::BLUE, y );
122  float *a = fa->getLine( LWBUF::ALPHA, y );
123 
124  for ( int x = 0; x < fa->width; x++ )
125  {
126  float out[] = {r[x], g[x], b[x]};
127  fa->setRGB( x, y, out );
128  fa->setAlpha( x, y, a[ x ] );
129  }
130  }
131  }
132  virtual unsigned int Flags() {return 0;}
133  virtual void UpdateFlags() {;} // Used by LW 10 and higher, update m_bufferSet
134  int *Flags10()
135  {
136  m_bufferSet.clear(); // clear the set of used buffers
137  UpdateFlags(); // update the set of used buffers
138  if (m_bufferReturn) delete[] m_bufferReturn;
139  m_bufferReturn = new int[m_bufferSet.size()+1];
140  int *idx = m_bufferReturn;
141  *idx++ = static_cast<int>(m_bufferSet.size()); // store the number of buffers
142  for (BufferSet::iterator iter = m_bufferSet.begin(); iter != m_bufferSet.end(); ++iter)
143  {
144  *idx++ = *iter;
145  }
146  return m_bufferReturn;
147  }
148 };
149 
151 
154  template <class T, int maxVersion, int minVersion>
155  class ImageFilterAdaptor : public InstanceAdaptor <T>, public ItemAdaptor <T>
156  {
157  public:
158  ImageFilterAdaptor(const char *name, ServerTagInfo *tags)
159  {
160  LWServer::AddPlugin(LWIMAGEFILTER_HCLASS, name, Activate, tags);
161  }
163  static int Activate (int version, GlobalFunc *global, LWInstance inst, void *serverData)
164  {
165  if ( version > maxVersion ) return AFUNC_BADVERSION;
166  if ( version < minVersion ) return AFUNC_BADVERSION;
167  try
168  {
169  lwpp::SetSuperGlobal(global);
170 
171  if (version < 5)
172  {
173  LWImageFilterHandler *plugin = static_cast<LWImageFilterHandler *>(inst);
174 
175  InstanceAdaptor<T>::Activate(plugin->inst);
176  ItemAdaptor<T>::Activate(plugin->item);
177  plugin->process = ImageFilterAdaptor::Process;
178  plugin->flags = ImageFilterAdaptor::Flags;
179  return AFUNC_OK;
180  }
181  else
182  {
183  lw10::LWImageFilterHandler *plugin = static_cast<lw10::LWImageFilterHandler *>(inst);
184 
187  plugin->process = ImageFilterAdaptor::Process;
188  plugin->flags = ImageFilterAdaptor::Flags10;
189  return AFUNC_OK;
190  }
191  }
192  catch (std::exception &e)
193  {
194  lwpp::LWMessage::Error("An exception occured in ImageFilter::Activate():", e.what());
195  return AFUNC_BADAPP;
196  }
197  }
198  private:
199  static void Process (LWInstance instance, const LWFilterAccess *fa)
200  {
201  try
202  {
203  T *plugin = (T *) instance;
204  plugin->Process(fa);
205  }
206  catch (std::exception &e)
207  {
208  lwpp::LWMessage::Error("An exception occured in ImageFilter::Process():", e.what());
209  }
210  }
211 
212  static unsigned int Flags (LWInstance instance)
213  {
214  try
215  {
216  T *plugin = (T *) instance;
217  return plugin->Flags();
218  }
219  catch (std::exception &e)
220  {
221  lwpp::LWMessage::Error("An exception occured in ImageFilter::Flags():", e.what());
222  return 0;
223  }
224  }
225  static int *Flags10 (LWInstance instance)
226  {
227  try
228  {
229  T *plugin = (T *) instance;
230  return plugin->Flags10();
231  }
232  catch (std::exception &e)
233  {
234  lwpp::LWMessage::Error("An exception occured in ImageFilter::Flags10():", e.what());
235  return 0;
236  }
237  }
238 
239  };
240 
250 }
251 
252 #endif // LWPP_IMAGEFILTER_HANDLER_H