1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24
25 #include "pxr/pxr.h"
26 #include "pxr/base/gf/gamma.h"
27
28 #include "pxr/base/gf/math.h"
29 #include "pxr/base/gf/vec3d.h"
30 #include "pxr/base/gf/vec3f.h"
31 #include "pxr/base/gf/vec4d.h"
32 #include "pxr/base/gf/vec4f.h"
33
34 PXR_NAMESPACE_OPEN_SCOPE
35
36 // Display colors (such as colors for UI elements) are always gamma 2.2
37 // and aspects of interactive rendering such as OpenGL's sRGB texture
38 // format assume that space as well. So, gamma 2.2 is hard coded here
39 // as the display gamma. In the future if those assumptions change we may
40 // need to move this to a higher level and get the gamma from somewhere else.
41 static const double _DisplayGamma = 2.2;
42
GfGetDisplayGamma()43 double GfGetDisplayGamma() {
44 return _DisplayGamma;
45 }
46
GfApplyGamma(const GfVec3f & v,double g)47 GfVec3f GfApplyGamma(const GfVec3f &v, double g) {
48 return GfVec3f(pow(v[0],g),pow(v[1],g),pow(v[2],g));
49 }
50
GfApplyGamma(const GfVec3d & v,double g)51 GfVec3d GfApplyGamma(const GfVec3d &v, double g) {
52 return GfVec3d(pow(v[0],g),pow(v[1],g),pow(v[2],g));
53 }
54
GfApplyGamma(const GfVec3h & v,double g)55 GfVec3h GfApplyGamma(const GfVec3h &v, double g) {
56 // Explicitly cast half to float to avoid ambiguous call to pow(...)
57 return GfVec3h(pow(static_cast<float>(v[0]),g),
58 pow(static_cast<float>(v[1]),g),
59 pow(static_cast<float>(v[2]),g));
60 }
61
GfApplyGamma(const GfVec4f & v,double g)62 GfVec4f GfApplyGamma(const GfVec4f &v, double g) {
63 return GfVec4f(pow(v[0],g),pow(v[1],g),pow(v[2],g),v[3]);
64 }
65
GfApplyGamma(const GfVec4d & v,double g)66 GfVec4d GfApplyGamma(const GfVec4d &v, double g) {
67 return GfVec4d(pow(v[0],g),pow(v[1],g),pow(v[2],g),v[3]);
68 }
69
GfApplyGamma(const GfVec4h & v,double g)70 GfVec4h GfApplyGamma(const GfVec4h &v, double g) {
71 // Explicitly cast half to float to avoid ambiguous call to pow(...)
72 return GfVec4h(pow(static_cast<float>(v[0]),g),
73 pow(static_cast<float>(v[1]),g),
74 pow(static_cast<float>(v[2]),g),
75 v[3]);
76 }
77
GfApplyGamma(const float & v,double g)78 float GfApplyGamma(const float &v, double g)
79 {
80 return pow(v, g);
81 }
82
GfApplyGamma(const unsigned char & v,double g)83 unsigned char GfApplyGamma(const unsigned char &v, double g)
84 {
85 return (unsigned char) (pow((v / 255.0), g) * 255);
86 }
87
88
89 template <class T>
Gf_ConvertLinearToDisplay(const T & v)90 static T Gf_ConvertLinearToDisplay(const T& v) {
91 return GfApplyGamma(v,1.0/_DisplayGamma);
92 }
93
94 template <class T>
Gf_ConvertDisplayToLinear(const T & v)95 static T Gf_ConvertDisplayToLinear(const T& v) {
96 return GfApplyGamma(v,_DisplayGamma);
97 }
98
GfConvertLinearToDisplay(const GfVec3f & v)99 GfVec3f GfConvertLinearToDisplay(const GfVec3f &v) { return Gf_ConvertLinearToDisplay(v); }
GfConvertLinearToDisplay(const GfVec3d & v)100 GfVec3d GfConvertLinearToDisplay(const GfVec3d &v) { return Gf_ConvertLinearToDisplay(v); }
GfConvertLinearToDisplay(const GfVec3h & v)101 GfVec3h GfConvertLinearToDisplay(const GfVec3h &v) { return Gf_ConvertLinearToDisplay(v); }
GfConvertLinearToDisplay(const GfVec4f & v)102 GfVec4f GfConvertLinearToDisplay(const GfVec4f &v) { return Gf_ConvertLinearToDisplay(v); }
GfConvertLinearToDisplay(const GfVec4d & v)103 GfVec4d GfConvertLinearToDisplay(const GfVec4d &v) { return Gf_ConvertLinearToDisplay(v); }
GfConvertLinearToDisplay(const GfVec4h & v)104 GfVec4h GfConvertLinearToDisplay(const GfVec4h &v) { return Gf_ConvertLinearToDisplay(v); }
GfConvertLinearToDisplay(const float & v)105 float GfConvertLinearToDisplay(const float &v) { return Gf_ConvertLinearToDisplay(v); }
GfConvertLinearToDisplay(const unsigned char & v)106 unsigned char GfConvertLinearToDisplay(const unsigned char &v) { return Gf_ConvertLinearToDisplay(v); }
107
GfConvertDisplayToLinear(const GfVec3f & v)108 GfVec3f GfConvertDisplayToLinear(const GfVec3f &v) { return Gf_ConvertDisplayToLinear(v); }
GfConvertDisplayToLinear(const GfVec3d & v)109 GfVec3d GfConvertDisplayToLinear(const GfVec3d &v) { return Gf_ConvertDisplayToLinear(v); }
GfConvertDisplayToLinear(const GfVec3h & v)110 GfVec3h GfConvertDisplayToLinear(const GfVec3h &v) { return Gf_ConvertDisplayToLinear(v); }
GfConvertDisplayToLinear(const GfVec4f & v)111 GfVec4f GfConvertDisplayToLinear(const GfVec4f &v) { return Gf_ConvertDisplayToLinear(v); }
GfConvertDisplayToLinear(const GfVec4d & v)112 GfVec4d GfConvertDisplayToLinear(const GfVec4d &v) { return Gf_ConvertDisplayToLinear(v); }
GfConvertDisplayToLinear(const GfVec4h & v)113 GfVec4h GfConvertDisplayToLinear(const GfVec4h &v) { return Gf_ConvertDisplayToLinear(v); }
GfConvertDisplayToLinear(const float & v)114 float GfConvertDisplayToLinear(const float &v) { return Gf_ConvertDisplayToLinear(v); }
GfConvertDisplayToLinear(const unsigned char & v)115 unsigned char GfConvertDisplayToLinear(const unsigned char &v) { return Gf_ConvertDisplayToLinear(v); }
116
117 PXR_NAMESPACE_CLOSE_SCOPE
118