Ptex
PtexUtils.h
Go to the documentation of this file.
1 #ifndef PtexUtils_h
2 #define PtexUtils_h
3 
4 /*
5 PTEX SOFTWARE
6 Copyright 2009 Disney Enterprises, Inc. All rights reserved
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are
10 met:
11 
12  * Redistributions of source code must retain the above copyright
13  notice, this list of conditions and the following disclaimer.
14 
15  * Redistributions in binary form must reproduce the above copyright
16  notice, this list of conditions and the following disclaimer in
17  the documentation and/or other materials provided with the
18  distribution.
19 
20  * The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
21  Studios" or the names of its contributors may NOT be used to
22  endorse or promote products derived from this software without
23  specific prior written permission from Walt Disney Pictures.
24 
25 Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
26 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
27 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
28 FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
29 IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
30 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37 */
38 
39 #include "Ptexture.h"
40 
41 struct PtexUtils : public Ptex {
42 
43  static bool isPowerOfTwo(int x)
44  {
45  return !(x&(x-1));
46  }
47 
48  static uint32_t ones(uint32_t x)
49  {
50  // count number of ones
51  x = (x & 0x55555555) + ((x >> 1) & 0x55555555); // add pairs of bits
52  x = (x & 0x33333333) + ((x >> 2) & 0x33333333); // add bit pairs
53  x = (x & 0x0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f); // add nybbles
54  x += (x >> 8); // add bytes
55  x += (x >> 16); // add words
56  return(x & 0xff);
57  }
58 
59  static uint32_t floor_log2(uint32_t x)
60  {
61  // floor(log2(n))
62  x |= (x >> 1);
63  x |= (x >> 2);
64  x |= (x >> 4);
65  x |= (x >> 8);
66  x |= (x >> 16);
67  return ones(x>>1);
68  }
69 
70  static uint32_t ceil_log2(uint32_t x)
71  {
72  // ceil(log2(n))
73  bool isPow2 = isPowerOfTwo(x);
74  x |= (x >> 1);
75  x |= (x >> 2);
76  x |= (x >> 4);
77  x |= (x >> 8);
78  x |= (x >> 16);
79  return ones(x>>1) + !isPow2;
80  }
81 
82  static int calcResFromWidth(float w)
83  {
84  // read exponent directly from float32 representation
85  // equiv to ceil(log2(1.0/w)) but much faster and no error
86  union {
87  float wf;
88  int32_t wi;
89  };
90  wf = w;
91  int result = 127 - ((wi >> 23) & 0xff);
92  return result;
93  }
94 
95  static float smoothstep(float x, float a, float b)
96  {
97  if ( x < a ) return 0;
98  if ( x >= b ) return 1;
99  x = (x - a)/(b - a);
100  return x*x * (3 - 2*x);
101  }
102 
103  static float qsmoothstep(float x, float a, float b)
104  {
105  // quintic smoothstep (cubic is only C1)
106  if ( x < a ) return 0;
107  if ( x >= b ) return 1;
108  x = (x - a)/(b - a);
109  return x*x*x * (10 + x * (-15 + x*6));
110  }
111 
112  template<typename T>
113  static T cond(bool c, T a, T b) { return (T)((T)c * a + (T)!c * b); }
114 
115  template<typename T>
116  static T min(T a, T b) { return cond(a < b, a, b); }
117 
118  template<typename T>
119  static T max(T a, T b) { return cond(a >= b, a, b); }
120 
121  template<typename T>
122  static T clamp(T x, T lo, T hi) { return cond(x < lo, lo, cond(x > hi, hi, x)); }
123 
124  static bool isConstant(const void* data, int stride, int ures, int vres,
125  int pixelSize);
126  static void interleave(const void* src, int sstride, int ures, int vres,
127  void* dst, int dstride, DataType dt, int nchannels);
128  static void deinterleave(const void* src, int sstride, int ures, int vres,
129  void* dst, int dstride, DataType dt, int nchannels);
130  static void encodeDifference(void* data, int size, DataType dt);
131  static void decodeDifference(void* data, int size, DataType dt);
132  typedef void ReduceFn(const void* src, int sstride, int ures, int vres,
133  void* dst, int dstride, DataType dt, int nchannels);
134  static void reduce(const void* src, int sstride, int ures, int vres,
135  void* dst, int dstride, DataType dt, int nchannels);
136  static void reduceu(const void* src, int sstride, int ures, int vres,
137  void* dst, int dstride, DataType dt, int nchannels);
138  static void reducev(const void* src, int sstride, int ures, int vres,
139  void* dst, int dstride, DataType dt, int nchannels);
140  static void reduceTri(const void* src, int sstride, int ures, int vres,
141  void* dst, int dstride, DataType dt, int nchannels);
142  static void average(const void* src, int sstride, int ures, int vres,
143  void* dst, DataType dt, int nchannels);
144  static void fill(const void* src, void* dst, int dstride,
145  int ures, int vres, int pixelsize);
146  static void copy(const void* src, int sstride, void* dst, int dstride,
147  int nrows, int rowlen);
148  static void blend(const void* src, float weight, void* dst, bool flip,
149  int rowlen, DataType dt, int nchannels);
150  static void multalpha(void* data, int npixels, DataType dt, int nchannels, int alphachan);
151  static void divalpha(void* data, int npixels, DataType dt, int nchannels, int alphachan);
152 
153  static void genRfaceids(const FaceInfo* faces, int nfaces,
154  uint32_t* rfaceids, uint32_t* faceids);
155 
156  // fixed length vector accumulator: dst[i] += val[i] * weight
157  template<typename T, int n>
158  struct VecAccum {
159  VecAccum() {}
160  void operator()(float* dst, const T* val, float weight)
161  {
162  *dst += (float)*val * weight;
163  // use template to unroll loop
164  VecAccum<T,n-1>()(dst+1, val+1, weight);
165  }
166  };
167  template<typename T>
168  struct VecAccum<T,0> { void operator()(float*, const T*, float) {} };
169 
170  // variable length vector accumulator: dst[i] += val[i] * weight
171  template<typename T>
172  struct VecAccumN {
173  void operator()(float* dst, const T* val, int nchan, float weight)
174  {
175  for (int i = 0; i < nchan; i++) dst[i] += (float)val[i] * weight;
176  }
177  };
178 
179  // fixed length vector multiplier: dst[i] += val[i] * weight
180  template<typename T, int n>
181  struct VecMult {
182  VecMult() {}
183  void operator()(float* dst, const T* val, float weight)
184  {
185  *dst = (float)*val * weight;
186  // use template to unroll loop
187  VecMult<T,n-1>()(dst+1, val+1, weight);
188  }
189  };
190  template<typename T>
191  struct VecMult<T,0> { void operator()(float*, const T*, float) {} };
192 
193  // variable length vector multiplier: dst[i] = val[i] * weight
194  template<typename T>
195  struct VecMultN {
196  void operator()(float* dst, const T* val, int nchan, float weight)
197  {
198  for (int i = 0; i < nchan; i++) dst[i] = (float)val[i] * weight;
199  }
200  };
201 
202  typedef void (*ApplyConstFn)(float weight, float* dst, void* data, int nChan);
204  static void applyConst(float weight, float* dst, void* data, Ptex::DataType dt, int nChan)
205  {
206  // dispatch specialized apply function
207  ApplyConstFn fn = applyConstFunctions[((unsigned)nChan<=4)*nChan*4 + dt];
208  fn(weight, dst, data, nChan);
209  }
210 };
211 
212 #endif
void operator()(float *dst, const T *val, float weight)
Definition: PtexUtils.h:160
static void multalpha(void *data, int npixels, DataType dt, int nchannels, int alphachan)
Definition: PtexUtils.cpp:562
Common data structures and enums used throughout the API.
Definition: Ptexture.h:73
static uint32_t ceil_log2(uint32_t x)
Definition: PtexUtils.h:70
static bool isPowerOfTwo(int x)
Definition: PtexUtils.h:43
static void reduceTri(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition: PtexUtils.cpp:389
DataType
Type of data stored in texture file.
Definition: Ptexture.h:83
static void blend(const void *src, float weight, void *dst, bool flip, int rowlen, DataType dt, int nchannels)
Definition: PtexUtils.cpp:463
void(* ApplyConstFn)(float weight, float *dst, void *data, int nChan)
Definition: PtexUtils.h:202
static void average(const void *src, int sstride, int ures, int vres, void *dst, DataType dt, int nchannels)
Definition: PtexUtils.cpp:505
static T min(T a, T b)
Definition: PtexUtils.h:116
void operator()(float *dst, const T *val, int nchan, float weight)
Definition: PtexUtils.h:173
static void copy(const void *src, int sstride, void *dst, int dstride, int nrows, int rowlen)
Definition: PtexUtils.cpp:421
void operator()(float *dst, const T *val, int nchan, float weight)
Definition: PtexUtils.h:196
void operator()(float *dst, const T *val, float weight)
Definition: PtexUtils.h:183
static ApplyConstFn applyConstFunctions[20]
Definition: PtexUtils.h:203
void operator()(float *, const T *, float)
Definition: PtexUtils.h:168
static T clamp(T x, T lo, T hi)
Definition: PtexUtils.h:122
static void genRfaceids(const FaceInfo *faces, int nfaces, uint32_t *rfaceids, uint32_t *faceids)
Definition: PtexUtils.cpp:613
DataType
Type of data stored in texture file.
Definition: Ptexture.h:196
static int calcResFromWidth(float w)
Definition: PtexUtils.h:82
void ReduceFn(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition: PtexUtils.h:132
static void fill(const void *src, void *dst, int dstride, int ures, int vres, int pixelsize)
Definition: PtexUtils.cpp:405
static uint32_t floor_log2(uint32_t x)
Definition: PtexUtils.h:59
static float smoothstep(float x, float a, float b)
Definition: PtexUtils.h:95
void operator()(float *, const T *, float)
Definition: PtexUtils.h:191
static void encodeDifference(void *data, int size, DataType dt)
Definition: PtexUtils.cpp:233
static void divalpha(void *data, int npixels, DataType dt, int nchannels, int alphachan)
Definition: PtexUtils.cpp:601
static void deinterleave(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition: PtexUtils.cpp:208
static void reduceu(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition: PtexUtils.cpp:316
static void interleave(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition: PtexUtils.cpp:171
static uint32_t ones(uint32_t x)
Definition: PtexUtils.h:48
static void reducev(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition: PtexUtils.cpp:349
Information about a face, as stored in the Ptex file header.
Definition: Ptexture.h:237
static void decodeDifference(void *data, int size, DataType dt)
Definition: PtexUtils.cpp:253
static void reduce(const void *src, int sstride, int ures, int vres, void *dst, int dstride, DataType dt, int nchannels)
Definition: PtexUtils.cpp:282
static bool isConstant(const void *data, int stride, int ures, int vres, int pixelSize)
Definition: PtexUtils.cpp:129
Public API classes for reading, writing, caching, and filtering Ptex files.
static T cond(bool c, T a, T b)
Definition: PtexUtils.h:113
static void applyConst(float weight, float *dst, void *data, Ptex::DataType dt, int nChan)
Definition: PtexUtils.h:204
static float qsmoothstep(float x, float a, float b)
Definition: PtexUtils.h:103
static T max(T a, T b)
Definition: PtexUtils.h:119