HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TIL_PixelFilter.h
Go to the documentation of this file.
1 /*
2 * PROPRIETARY INFORMATION. This software is proprietary to
3 * Side Effects Software Inc., and is not to be reproduced,
4 * transmitted, or disclosed in any way without written permission.
5 *
6 * NAME: TIL_PixelFilter.h (TIL Library, C++)
7 *
8 * COMMENTS: Pixel filter classes, for processing TIL_AdaptiveImage samples.
9 */
10 
11 #pragma once
12 
13 #ifndef __TIL_PixelFilter__
14 #define __TIL_PixelFilter__
15 
16 #include "TIL_API.h"
17 #include "TIL_Raster.h"
18 #include <PXL/PXL_Common.h>
19 #include <UT/UT_Assert.h>
20 #include <SYS/SYS_Inline.h>
21 
22 class TIL_AdaptiveImage;
23 
24 /// This is the super-duper-class for all pixel filters in TIL.
26 {
27 public:
28  virtual ~TIL_PixelFilter() {}
29 
30  virtual void filterPlaneVirtual(
31  const TIL_AdaptiveImage &src,
32  TIL_Raster &dest,
33  int planei,
34  int plane_src_components) const = 0;
35 
36  /// When vetsSamples() returns true, this should be called
37  /// so that the pixel filter can decide how to combine samples,
38  /// instead of just summing.
39  /// NOTE: nnew_samples is 1 when adding a new sample,
40  /// but may be more than 1 when combining pixels
41  /// into higher levels of the TIL_AdaptiveImage.
42  virtual void combineSample(
43  int ncomponents,
44  exint nexisting_samples,
45  float *existing,
46  exint nnew_samples,
47  const float *new_sample) const
48  {
49  UT_ASSERT_MSG(0, "Either this should be overridden by the subclass, or it shouldn't be called. Check vetsSamples().");
50  // This is just an example of the default behaviour.
51  for (int i = 0; i < ncomponents; ++i)
52  existing[i] += new_sample[i];
53  }
54 
55  /// Returns true iff this filter requires TIL_AdaptiveImage
56  /// to initialize TIL_AdaptiveImage::Level::mySumsOfSquares for
57  /// the plane that this filter is applied to.
59  bool needsSquares() const
60  {
61  return myNeedsSquares;
62  }
63 
64  /// Returns true iff this filter requires TIL_AdaptiveImage
65  /// to initialize TIL_AdaptiveImage::Level::myWeights from
66  /// the adaptivity plane.
68  bool needsWeights() const
69  {
70  return myNeedsWeights;
71  }
72 
73  /// Returns true iff TIL_AdaptiveImage needs to call
74  /// combineSample when inserting a sample to a plane
75  /// with this filter.
77  bool vetsSamples() const
78  {
79  return myVetsSamples;
80  }
81 
82  /// Returns false iff TIL_AdaptiveImage needs to create/delete it.
83  /// true means that it's owned globally, so will be deleted when the
84  /// process exits.
85  virtual bool ownedBySingleton() const
86  {
87  return false;
88  }
89 
90 protected:
92  : myNeedsSquares(false)
93  , myVetsSamples(false)
94  , myNeedsWeights(false)
95  {}
97  bool needs_squares,
98  bool vets_samples,
99  bool needs_weights)
100  : myNeedsSquares(needs_squares)
101  , myVetsSamples(vets_samples)
102  , myNeedsWeights(needs_weights)
103  {}
104 
105 private:
106  const bool myNeedsSquares:1;
107  const bool myNeedsWeights:1;
108  const bool myVetsSamples:1;
109 };
110 
111 /// Pixel filters should inherit from this using the "curiously-recurring
112 /// template pattern", so that this class can call the templated filterPlane
113 /// function in the subclass, e.g.:
114 /// class TIL_MyPixelFilter : public TIL_PixelFilterT<TIL_MyPixelFilter>
115 /// {
116 /// template<int NSRCCOMPONENTS,typename T,int NDSTCOMPONENTS,bool INTERLEAVED>
117 /// void filterPlane(
118 /// const TIL_AdaptiveImage &src,
119 /// PXL_RasterWrapper<T,NDSTCOMPONENTS,INTERLEAVED> dest,
120 /// int planei,
121 /// int plane_start_src_component) const;
122 /// };
123 /// The primary advantage of this middle class is to unpack how the raster
124 /// is stored, so that it can be accessed more efficiently in the pixel filter.
125 /// Of course, this means that filterPlane will get compiled 45 times, for the 5
126 /// data types and 9 packing formats, (including the two 2-to-3 formats).
127 template<typename SUBCLASS>
129 {
130 public:
131  typedef SUBCLASS SubclassType;
132 
133  virtual ~TIL_PixelFilterT() {}
134 
135  virtual void filterPlaneVirtual(
136  const TIL_AdaptiveImage &src,
137  TIL_Raster &dest,
138  int planei,
139  int plane_src_components) const override
140  {
141  // Switch on the packing type to have compile-time numbers of components
142  // and whether the dest raster is interleaved or not.
143  PXL_Packing packing = dest.getPacking();
144  switch (packing)
145  {
146  case PACK_SINGLE:
147  filterPlanePart<1,1,true>(src, dest, planei); return;
148  case PACK_DUAL:
149  filterPlanePart<2,2,true>(src, dest, planei); return;
150  case PACK_DUAL_NI:
151  filterPlanePart<2,2,false>(src, dest, planei); return;
152  case PACK_RGB:
153  if (plane_src_components == 3)
154  filterPlanePart<3,3,true>(src, dest, planei);
155  else
156  {
157  UT_ASSERT(plane_src_components == 2);
158  filterPlanePart<2,3,true>(src, dest, planei);
159  }
160  return;
161  case PACK_RGBA:
162  filterPlanePart<4,4,true>(src, dest, planei); return;
163  case PACK_RGB_NI:
164  if (plane_src_components == 3)
165  filterPlanePart<3,3,false>(src, dest, planei);
166  else
167  {
168  UT_ASSERT(plane_src_components == 2);
169  filterPlanePart<2,3,false>(src, dest, planei);
170  }
171  return;
172  case PACK_RGBA_NI:
173  filterPlanePart<4,4,false>(src, dest, planei); return;
174  case PACK_UNKNOWN:
175  return;
176  }
177  }
178 
179 protected:
181  bool needs_squares=false,
182  bool vets_samples=false,
183  bool needs_weights=false)
184  : TIL_PixelFilter(needs_squares, vets_samples, needs_weights)
185  {}
186 
187 private:
188  template<int NSRCCOMPONENTS,int NDSTCOMPONENTS,bool INTERLEAVED>
189  void filterPlanePart(
190  const TIL_AdaptiveImage &src,
191  TIL_Raster &dest,
192  int planei) const
193  {
194  // Switch on the dest data type to have it at compile-time.
196  const SUBCLASS *sub = (const SUBCLASS *)this;
197  switch (format)
198  {
199  case PXL_INT8:
200  sub->template filterPlane<NSRCCOMPONENTS>(src,
202  planei);
203  return;
204  case PXL_INT16:
205  sub->template filterPlane<NSRCCOMPONENTS>(src,
207  planei);
208  return;
209  case PXL_INT32:
210  sub->template filterPlane<NSRCCOMPONENTS>(src,
212  planei);
213  return;
214  case PXL_FLOAT16:
215  sub->template filterPlane<NSRCCOMPONENTS>(src,
217  planei);
218  return;
219  case PXL_FLOAT32:
220  sub->template filterPlane<NSRCCOMPONENTS>(src,
222  planei);
223  return;
224  case PXL_MAX_DATA_FORMAT:
225  return;
226  }
227  }
228 };
229 
230 class TIL_API TIL_PixelFilterPixelLevel : public TIL_PixelFilterT<TIL_PixelFilterPixelLevel>
231 {
232 public:
233  template<int NSRCCOMPONENTS,typename T,int NDSTCOMPONENTS,bool INTERLEAVED>
234  void filterPlane(
235  const TIL_AdaptiveImage &src,
237  int planei) const;
238 
239  virtual bool ownedBySingleton() const
240  {
241  return true;
242  }
243 
245 };
246 
247 class TIL_API TIL_PixelFilterSampleCount : public TIL_PixelFilterT<TIL_PixelFilterSampleCount>
248 {
249 public:
250  template<int NSRCCOMPONENTS,typename T,int NDSTCOMPONENTS,bool INTERLEAVED>
251  void filterPlane(
252  const TIL_AdaptiveImage &src,
254  int planei) const;
255 
256  virtual bool ownedBySingleton() const
257  {
258  return true;
259  }
260 
262 };
263 
264 class TIL_API TIL_PixelFilterPower2Block : public TIL_PixelFilterT<TIL_PixelFilterPower2Block>
265 {
266 public:
267  template<int NSRCCOMPONENTS,typename T,int NDSTCOMPONENTS,bool INTERLEAVED>
268  void filterPlane(
269  const TIL_AdaptiveImage &src,
271  int planei) const;
272 
273  virtual bool ownedBySingleton() const
274  {
275  return true;
276  }
277 
279 };
280 
281 template<bool IS_MAX>
282 class TIL_API TIL_PixelFilterMinMax : public TIL_PixelFilterT<TIL_PixelFilterMinMax<IS_MAX> >
283 {
285 public:
287  : Base(false,true) // vets_samples
288  {}
289 
290  template<int NSRCCOMPONENTS,typename T,int NDSTCOMPONENTS,bool INTERLEAVED>
291  void filterPlane(
292  const TIL_AdaptiveImage &src,
294  int planei) const;
295 
296  virtual void combineSample(
297  int ncomponents,
298  exint nexisting_samples,
299  float *existing,
300  exint nnew_samples,
301  const float *new_sample) const
302  {
303  if (nnew_samples == 0)
304  return;
305 
306  if (nexisting_samples == 0)
307  {
308  for (int i = 0; i < ncomponents; ++i)
309  existing[i] = new_sample[i];
310  return;
311  }
312 
313  if (IS_MAX)
314  {
315  for (int i = 0; i < ncomponents; ++i)
316  {
317  float v = new_sample[i];
318  if (v > existing[i])
319  existing[i] = v;
320  }
321  }
322  else
323  {
324  // min
325  for (int i = 0; i < ncomponents; ++i)
326  {
327  float v = new_sample[i];
328  if (v < existing[i])
329  existing[i] = v;
330  }
331  }
332  }
333 
334  virtual bool ownedBySingleton() const
335  {
336  return true;
337  }
338 
340 };
341 
342 template<bool LEVEL_INTERP>
343 class TIL_API TIL_PixelFilterDebug : public TIL_PixelFilterT<TIL_PixelFilterDebug<LEVEL_INTERP> >
344 {
346 public:
347  /// NOTE: This does not need its own plane's squares, just the adaptivity plane's weights.
349  : Base(false,false,true) // needs_squares,vets_samples,needs_weights
350  , myLevel(level)
351  {}
352 
353  template<int NSRCCOMPONENTS,typename T,int NDSTCOMPONENTS,bool INTERLEAVED>
354  void filterPlane(
355  const TIL_AdaptiveImage &src,
357  int planei) const;
358 
359 private:
360  int myLevel;
361 };
362 
363 #if 1
364 class TIL_API TIL_PixelFilterBlurThreshold : public TIL_PixelFilterT<TIL_PixelFilterBlurThreshold>
365 {
367 public:
369  : Base(true) // needs_squares
370  , myThresholdSquared(threshold*threshold)
371  {}
372 
373  template<int NSRCCOMPONENTS,typename T,int NDSTCOMPONENTS,bool INTERLEAVED>
374  void filterPlane(
375  const TIL_AdaptiveImage &src,
377  int planei) const;
378 
379 private:
380  float myThresholdSquared;
381 };
382 #endif
383 
384 #endif
virtual void combineSample(int ncomponents, exint nexisting_samples, float *existing, exint nnew_samples, const float *new_sample) const
virtual void filterPlaneVirtual(const TIL_AdaptiveImage &src, TIL_Raster &dest, int planei, int plane_src_components) const override
virtual bool ownedBySingleton() const
virtual bool ownedBySingleton() const
virtual bool ownedBySingleton() const
SYS_FORCE_INLINE bool needsSquares() const
const GLdouble * v
Definition: glcorearb.h:836
TIL_PixelFilterT(bool needs_squares=false, bool vets_samples=false, bool needs_weights=false)
TIL_PixelFilterDebug(int level)
NOTE: This does not need its own plane's squares, just the adaptivity plane's weights.
GLint level
Definition: glcorearb.h:107
virtual bool ownedBySingleton() const
png_uint_32 i
Definition: png.h:2877
PXL_DataFormat getFormat() const
Definition: PXL_Raster.h:93
This is the super-duper-class for all pixel filters in TIL.
static TIL_PixelFilterPower2Block theInstance
virtual ~TIL_PixelFilterT()
PXL_Packing getPacking() const
Definition: PXL_Raster.h:94
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
int64 exint
Definition: SYS_Types.h:115
TIL_PixelFilter(bool needs_squares, bool vets_samples, bool needs_weights)
virtual ~TIL_PixelFilter()
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
SYS_FORCE_INLINE bool vetsSamples() const
TIL_PixelFilterBlurThreshold(float threshold)
PXL_Packing
Definition: PXL_Common.h:29
static TIL_PixelFilterPixelLevel theInstance
PXL_DataFormat
Definition: PXL_Common.h:19
static TIL_PixelFilterMinMax< IS_MAX > theInstance
virtual bool ownedBySingleton() const
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:107
SYS_FORCE_INLINE bool needsWeights() const
virtual void combineSample(int ncomponents, exint nexisting_samples, float *existing, exint nnew_samples, const float *new_sample) const
static TIL_PixelFilterSampleCount theInstance
#define TIL_API
Definition: TIL_API.h:10
#define UT_ASSERT_MSG(ZZ, MM)
Definition: UT_Assert.h:105
GLenum src
Definition: glcorearb.h:1792