OpenImageIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
texture.h
Go to the documentation of this file.
1 /*
2  Copyright 2008 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 
35 
36 
37 #ifndef OPENIMAGEIO_TEXTURE_H
38 #define OPENIMAGEIO_TEXTURE_H
39 
40 #include "varyingref.h"
41 #include "ustring.h"
42 #include "imageio.h"
43 
44 #include <OpenEXR/ImathVec.h> /* because we need V3f */
45 
46 OIIO_NAMESPACE_ENTER
47 {
48 
49 // Forward declaration
50 namespace pvt {
51 
52 class TextureSystemImpl;
53 
54 // Used internally by TextureSystem. Unfortunately, this is the only
55 // clean place to store it. Sorry, users, this isn't really for you.
56 enum TexFormat {
57  TexFormatUnknown, TexFormatTexture, TexFormatTexture3d,
58  TexFormatShadow, TexFormatCubeFaceShadow, TexFormatVolumeShadow,
59  TexFormatLatLongEnv, TexFormatCubeFaceEnv,
60  TexFormatLast
61 };
62 
63 enum EnvLayout {
64  LayoutTexture=0 /* ordinary texture - no special env wrap */,
65  LayoutLatLong, LayoutCubeThreeByTwo, LayoutCubeOneBySix, EnvLayoutLast
66 };
67 
68 } // pvt namespace
69 
70 
71 
72 
75 typedef unsigned char Runflag;
76 
79 enum RunFlagVal { RunFlagOff = 0, RunFlagOn = 255 };
80 
81 class TextureOptions; // forward declaration
82 
83 
84 
91 class OIIO_API TextureOpt {
92 public:
95  enum Wrap {
101  WrapLast
102  };
103 
106  enum MipMode {
111  MipModeAniso
112  };
113 
116  enum InterpMode {
120  InterpSmartBicubic
121  };
122 
123 
127  : nchannels(1), firstchannel(0), subimage(0),
128  swrap(WrapDefault), twrap(WrapDefault),
129  mipmode(MipModeDefault), interpmode(InterpSmartBicubic),
130  anisotropic(32), conservative_filter(true),
131  sblur(0.0f), tblur(0.0f), swidth(1.0f), twidth(1.0f),
132  fill(0.0f), missingcolor(NULL),
133  dresultds(NULL), dresultdt(NULL),
134  time(0.0f), bias(0.0f), samples(1),
135  rwrap(WrapDefault), rblur(0.0f), rwidth(1.0f), dresultdr(NULL),
136  actualchannels(0),
137  swrap_func(NULL), twrap_func(NULL), rwrap_func(NULL),
138  envlayout(0)
139  { }
140 
143  TextureOpt (const TextureOptions &opt, int index);
144 
145  int nchannels;
147  int subimage;
155  float sblur, tblur;
156  float swidth, twidth;
157  float fill;
158  const float *missingcolor;
159  float *dresultds;
160  float *dresultdt;
161  float time;
162  float bias;
163  int samples;
164 
165  // For 3D volume texture lookups only:
167  float rblur;
168  float rwidth;
169  float *dresultdr;
170 
173  static Wrap decode_wrapmode (const char *name);
174  static Wrap decode_wrapmode (ustring name);
175 
179  static void parse_wrapmodes (const char *wrapmodes,
180  TextureOpt::Wrap &swrapcode,
181  TextureOpt::Wrap &twrapcode);
182 
183 private:
184  // Options set INTERNALLY by libtexture after the options are passed
185  // by the user. Users should not attempt to alter these!
186  int actualchannels; // True number of channels read
187  typedef bool (*wrap_impl) (int &coord, int origin, int width);
188  wrap_impl swrap_func, twrap_func, rwrap_func;
189  int envlayout; // Layout for environment wrap
190  friend class pvt::TextureSystemImpl;
191 };
192 
193 
194 
201 class OIIO_API TextureOptions {
202 public:
205  enum Wrap {
211  WrapLast
212  };
213 
216  enum MipMode {
221  MipModeAniso
222  };
223 
226  enum InterpMode {
230  InterpSmartBicubic
231  };
232 
235  TextureOptions ();
236 
239  TextureOptions (const TextureOpt &opt);
240 
241  // Options that must be the same for all points we're texturing at once
243  int nchannels;
244  int subimage;
252 
253  // Options that may be different for each point we're texturing
254  VaryingRef<float> sblur, tblur;
255  VaryingRef<float> swidth, twidth;
256  VaryingRef<float> time;
257  VaryingRef<float> bias;
258  VaryingRef<float> fill;
259  VaryingRef<float> missingcolor;
260  VaryingRef<int> samples;
261  float *dresultds;
262  float *dresultdt;
263 
264  // For 3D volume texture lookups only:
266  VaryingRef<float> rblur;
267  VaryingRef<float> rwidth;
268  float *dresultdr;
269 
272  static Wrap decode_wrapmode (const char *name) {
273  return (Wrap)TextureOpt::decode_wrapmode (name);
274  }
275  static Wrap decode_wrapmode (ustring name) {
276  return (Wrap)TextureOpt::decode_wrapmode (name);
277  }
278 
282  static void parse_wrapmodes (const char *wrapmodes,
283  TextureOptions::Wrap &swrapcode,
284  TextureOptions::Wrap &twrapcode) {
285  TextureOpt::parse_wrapmodes (wrapmodes,
286  *(TextureOpt::Wrap *)&swrapcode,
287  *(TextureOpt::Wrap *)&twrapcode);
288  }
289 
290 private:
291  // Options set INTERNALLY by libtexture after the options are passed
292  // by the user. Users should not attempt to alter these!
293  int actualchannels; // True number of channels read
294  typedef bool (*wrap_impl) (int &coord, int origin, int width);
295  wrap_impl swrap_func, twrap_func, rwrap_func;
296  friend class pvt::TextureSystemImpl;
297  friend class TextureOpt;
298 };
299 
300 
301 
307 class OIIO_API TextureSystem {
308 public:
315  static TextureSystem *create (bool shared=true);
316 
319  static void destroy (TextureSystem *x);
320 
321  TextureSystem (void) { }
322  virtual ~TextureSystem () { }
323 
326  virtual void clear () = 0;
327 
347  virtual bool attribute (const std::string &name, TypeDesc type, const void *val) = 0;
348  // Shortcuts for common types
349  virtual bool attribute (const std::string &name, int val) = 0;
350  virtual bool attribute (const std::string &name, float val) = 0;
351  virtual bool attribute (const std::string &name, double val) = 0;
352  virtual bool attribute (const std::string &name, const char *val) = 0;
353  virtual bool attribute (const std::string &name, const std::string &val) = 0;
354 
356  virtual bool getattribute (const std::string &name, TypeDesc type, void *val) = 0;
357  // Shortcuts for common types
358  virtual bool getattribute (const std::string &name, int &val) = 0;
359  virtual bool getattribute (const std::string &name, float &val) = 0;
360  virtual bool getattribute (const std::string &name, double &val) = 0;
361  virtual bool getattribute (const std::string &name, char **val) = 0;
362  virtual bool getattribute (const std::string &name, std::string &val) = 0;
363 
366  class Perthread;
367 
371  virtual Perthread * get_perthread_info () = 0;
372 
376  class TextureHandle;
377 
382  virtual TextureHandle * get_texture_handle (ustring filename,
383  Perthread *thread_info=NULL) = 0;
384 
396  virtual bool texture (ustring filename, TextureOpt &options,
397  float s, float t, float dsdx, float dtdx,
398  float dsdy, float dtdy, float *result) = 0;
399 
402  virtual bool texture (TextureHandle *texture_handle,
403  Perthread *thread_info, TextureOpt &options,
404  float s, float t, float dsdx, float dtdx,
405  float dsdy, float dtdy, float *result) = 0;
406 
409  virtual bool texture (ustring filename, TextureOptions &options,
410  float s, float t, float dsdx, float dtdx,
411  float dsdy, float dtdy, float *result) = 0;
412 
424  virtual bool texture (ustring filename, TextureOptions &options,
425  Runflag *runflags, int beginactive, int endactive,
426  VaryingRef<float> s, VaryingRef<float> t,
427  VaryingRef<float> dsdx, VaryingRef<float> dtdx,
428  VaryingRef<float> dsdy, VaryingRef<float> dtdy,
429  float *result) = 0;
430 
435  virtual bool texture3d (ustring filename, TextureOpt &options,
436  const Imath::V3f &P, const Imath::V3f &dPdx,
437  const Imath::V3f &dPdy, const Imath::V3f &dPdz,
438  float *result) = 0;
439 
442  virtual bool texture3d (TextureHandle *texture_handle,
443  Perthread *thread_info, TextureOpt &options,
444  const Imath::V3f &P, const Imath::V3f &dPdx,
445  const Imath::V3f &dPdy, const Imath::V3f &dPdz,
446  float *result) = 0;
447 
450  virtual bool texture3d (ustring filename, TextureOptions &options,
451  const Imath::V3f &P, const Imath::V3f &dPdx,
452  const Imath::V3f &dPdy, const Imath::V3f &dPdz,
453  float *result) {
454  TextureOpt opt (options, 0);
455  return texture3d (filename, opt, P, dPdx, dPdy, dPdz, result);
456  }
457 
462  virtual bool texture3d (ustring filename, TextureOptions &options,
463  Runflag *runflags, int beginactive, int endactive,
464  VaryingRef<Imath::V3f> P,
465  VaryingRef<Imath::V3f> dPdx,
466  VaryingRef<Imath::V3f> dPdy,
467  VaryingRef<Imath::V3f> dPdz,
468  float *result) = 0;
469 
474  virtual bool shadow (ustring filename, TextureOpt &options,
475  const Imath::V3f &P, const Imath::V3f &dPdx,
476  const Imath::V3f &dPdy, float *result) = 0;
477 
480  virtual bool shadow (TextureHandle *texture_handle, Perthread *thread_info,
481  TextureOpt &options,
482  const Imath::V3f &P, const Imath::V3f &dPdx,
483  const Imath::V3f &dPdy, float *result) = 0;
484 
489  virtual bool shadow (ustring filename, TextureOptions &options,
490  Runflag *runflags, int beginactive, int endactive,
491  VaryingRef<Imath::V3f> P,
492  VaryingRef<Imath::V3f> dPdx,
493  VaryingRef<Imath::V3f> dPdy,
494  float *result) = 0;
495 
500  virtual bool environment (ustring filename, TextureOpt &options,
501  const Imath::V3f &R, const Imath::V3f &dRdx,
502  const Imath::V3f &dRdy, float *result) = 0;
503 
506  virtual bool environment (TextureHandle *texture_handle,
507  Perthread *thread_info, TextureOpt &options,
508  const Imath::V3f &R, const Imath::V3f &dRdx,
509  const Imath::V3f &dRdy, float *result) = 0;
510 
516  virtual bool environment (ustring filename, TextureOptions &options,
517  Runflag *runflags, int beginactive, int endactive,
518  VaryingRef<Imath::V3f> R,
519  VaryingRef<Imath::V3f> dRdx,
520  VaryingRef<Imath::V3f> dRdy,
521  float *result) = 0;
522 
525  virtual std::string resolve_filename (const std::string &filename) const=0;
526 
531  virtual bool get_texture_info (ustring filename, int subimage,
532  ustring dataname, TypeDesc datatype, void *data) = 0;
533 
540  virtual bool get_imagespec (ustring filename, int subimage,
541  ImageSpec &spec) = 0;
542 
555  virtual const ImageSpec *imagespec (ustring filename, int subimage=0) = 0;
556 
571  virtual bool get_texels (ustring filename, TextureOpt &options,
572  int miplevel, int xbegin, int xend,
573  int ybegin, int yend, int zbegin, int zend,
574  TypeDesc format, void *result) = 0;
575 
578  virtual bool get_texels (ustring filename, TextureOptions &options,
579  int miplevel, int xbegin, int xend,
580  int ybegin, int yend, int zbegin, int zend,
581  TypeDesc format, void *result) {
582  TextureOpt opt (options, 0);
583  return get_texels (filename, opt, miplevel, xbegin, xend,
584  ybegin, yend, zbegin, zend, format, result);
585  }
586 
591  virtual std::string geterror () const = 0;
592 
595  virtual std::string getstats (int level=1, bool icstats=true) const = 0;
596 
600  virtual void invalidate (ustring filename) = 0;
601 
607  virtual void invalidate_all (bool force=false) = 0;
608 
614  virtual void reset_stats () = 0;
615 
616 private:
617  // Make delete private and unimplemented in order to prevent apps
618  // from calling it. Instead, they should call TextureSystem::destroy().
619  void operator delete (void * /*todel*/) { }
620 
621 };
622 
623 
624 }
625 OIIO_NAMESPACE_EXIT
626 
627 #endif // OPENIMAGEIO_TEXTURE_H