1 // Copyright 2008-present Contributors to the OpenImageIO project.
2 // SPDX-License-Identifier: BSD-3-Clause
3 // https://github.com/OpenImageIO/oiio/blob/master/LICENSE.md
4 
5 #include <string>
6 
7 #include <OpenImageIO/dassert.h>
8 #include <OpenImageIO/imageio.h>
9 #include <OpenImageIO/texture.h>
10 #include <OpenImageIO/typedesc.h>
11 #include <OpenImageIO/ustring.h>
12 #include <OpenImageIO/varyingref.h>
13 
14 
15 OIIO_NAMESPACE_BEGIN
16 
17 
18 namespace {  // anonymous
19 
20 static float default_blur  = 0;
21 static float default_width = 1;
22 static float default_time  = 0;
23 static float default_bias  = 0;
24 static float default_fill  = 0;
25 static int default_samples = 1;
26 
27 static const ustring wrap_type_name[] = {
28     // MUST match the order of TextureOptions::Wrap
29     ustring("default"),
30     ustring("black"),
31     ustring("clamp"),
32     ustring("periodic"),
33     ustring("mirror"),
34     ustring("periodic_pow2"),
35     ustring("periodic_sharedborder"),
36     ustring()
37 };
38 
39 }  // end anonymous namespace
40 
41 
42 
43 /// Special private ctr that makes a canonical default TextureOptions.
44 /// For use internal to libtexture.  Users, don't call this!
TextureOptions()45 TextureOptions::TextureOptions()
46     : firstchannel(0)
47     , subimage(0)
48     , swrap(TextureOptions::WrapDefault)
49     , twrap(TextureOptions::WrapDefault)
50     , mipmode(TextureOptions::MipModeDefault)
51     , interpmode(TextureOptions::InterpSmartBicubic)
52     , anisotropic(32)
53     , conservative_filter(true)
54     , sblur(default_blur)
55     , tblur(default_blur)
56     , swidth(default_width)
57     , twidth(default_width)
58     , time(default_time)
59     , bias(default_bias)
60     , fill(default_fill)
61     , missingcolor(NULL)
62     , samples(default_samples)
63     , rwrap(TextureOptions::WrapDefault)
64     , rblur(default_blur)
65     , rwidth(default_width)
66 {
67 }
68 
69 
70 
TextureOptions(const TextureOpt & opt)71 TextureOptions::TextureOptions(const TextureOpt& opt)
72     : firstchannel(opt.firstchannel)
73     , subimage(opt.subimage)
74     , subimagename(opt.subimagename)
75     , swrap((Wrap)opt.swrap)
76     , twrap((Wrap)opt.twrap)
77     , mipmode((MipMode)opt.mipmode)
78     , interpmode((InterpMode)opt.interpmode)
79     , anisotropic(opt.anisotropic)
80     , conservative_filter(opt.conservative_filter)
81     , sblur((float*)&opt.sblur)
82     , tblur((float*)&opt.tblur)
83     , swidth((float*)&opt.swidth)
84     , twidth((float*)&opt.twidth)
85     , time((float*)&opt.time)
86     , bias((float*)&opt.bias)
87     , fill((float*)&opt.fill)
88     , missingcolor((void*)opt.missingcolor)
89     , samples((int*)&opt.samples)
90     , rwrap((Wrap)opt.rwrap)
91     , rblur((float*)&opt.rblur)
92     , rwidth((float*)&opt.rwidth)
93 {
94 }
95 
96 
97 
TextureOpt(const TextureOptions & opt,int index)98 TextureOpt::TextureOpt(const TextureOptions& opt, int index)
99     : firstchannel(opt.firstchannel)
100     , subimage(opt.subimage)
101     , subimagename(opt.subimagename)
102     , swrap((Wrap)opt.swrap)
103     , twrap((Wrap)opt.twrap)
104     , mipmode((MipMode)opt.mipmode)
105     , interpmode((InterpMode)opt.interpmode)
106     , anisotropic(opt.anisotropic)
107     , conservative_filter(opt.conservative_filter)
108     , sblur(opt.sblur[index])
109     , tblur(opt.tblur[index])
110     , swidth(opt.swidth[index])
111     , twidth(opt.twidth[index])
112     , fill(opt.fill[index])
113     , missingcolor(opt.missingcolor.ptr() ? &opt.missingcolor[index] : NULL)
114     , time(opt.time[index])
115     , bias(opt.bias[index])
116     , samples(opt.samples[index])
117     , rwrap((Wrap)opt.rwrap)
118     , rblur(opt.rblur[index])
119     , rwidth(opt.rwidth[index])
120     , envlayout(0)
121 {
122 }
123 
124 
125 
126 Tex::Wrap
decode_wrapmode(const char * name)127 Tex::decode_wrapmode(const char* name)
128 {
129     for (int i = 0; i < (int)Tex::Wrap::Last; ++i)
130         if (!strcmp(name, wrap_type_name[i].c_str()))
131             return (Wrap)i;
132     return Tex::Wrap::Default;
133 }
134 
135 
136 
137 Tex::Wrap
decode_wrapmode(ustring name)138 Tex::decode_wrapmode(ustring name)
139 {
140     for (int i = 0; i < (int)Tex::Wrap::Last; ++i)
141         if (name == wrap_type_name[i])
142             return (Wrap)i;
143     return Tex::Wrap::Default;
144 }
145 
146 
147 
148 void
parse_wrapmodes(const char * wrapmodes,Tex::Wrap & swrapcode,Tex::Wrap & twrapcode)149 Tex::parse_wrapmodes(const char* wrapmodes, Tex::Wrap& swrapcode,
150                      Tex::Wrap& twrapcode)
151 {
152     char* swrap = OIIO_ALLOCA(char, strlen(wrapmodes) + 1);
153     const char* twrap;
154     int i;
155     for (i = 0; wrapmodes[i] && wrapmodes[i] != ','; ++i)
156         swrap[i] = wrapmodes[i];
157     swrap[i] = 0;
158     if (wrapmodes[i] == ',')
159         twrap = wrapmodes + i + 1;
160     else
161         twrap = swrap;
162     swrapcode = decode_wrapmode(swrap);
163     twrapcode = decode_wrapmode(twrap);
164 }
165 
166 
167 OIIO_NAMESPACE_END
168