-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
math.go
141 lines (122 loc) · 3.7 KB
/
math.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package gltf
import "math"
// NormalizeByte normalize a float32 into a int8
func NormalizeByte(v float32) int8 {
return int8(math.Round(float64(v) * 127))
}
// NormalizeByte denormalize a int8 into a float32
func DenormalizeByte(v int8) float32 {
return float32(math.Max(float64(v)/127, -1))
}
// NormalizeUbyte normalize a float32 into a uint8
func NormalizeUbyte(v float32) uint8 {
return uint8(math.Round(float64(v) * 255))
}
// DenormalizeUbyte denormalize a uint8 into a float32
func DenormalizeUbyte(v uint8) float32 {
return float32(v) / 255
}
// NormalizeShort normalize a float32 into a int16
func NormalizeShort(v float32) int16 {
return int16(math.Round(float64(v) * 32767))
}
// DenormalizeShort denormalize a int16 into a float32
func DenormalizeShort(v int16) float32 {
return float32(math.Max(float64(v)/32767, -1))
}
// NormalizeUshort normalize a float32 into a uint16
func NormalizeUshort(v float32) uint16 {
return uint16(math.Round(float64(v) * 65535))
}
// DenormalizeuShort denormalize a uint16 into a float32
func DenormalizeUshort(v uint16) float32 {
return float32(v) / 65535
}
// NormalizeRGB transform a RGB float32 color (from 0 to 1)
// to its uint8 represtation (from 0 to 255).
func NormalizeRGB(v [3]float32) [3]uint8 {
return [3]uint8{
uint8(deliniarize(v[0]) * 255),
uint8(deliniarize(v[1]) * 255),
uint8(deliniarize(v[2]) * 255),
}
}
// DenormalizeRGB transform a RGB uint8 color (from 0 to 255)
// to its float represtation (from 0 to 1).
func DenormalizeRGB(v [3]uint8) [3]float32 {
return [3]float32{
linearize(float32(v[0]) / 255),
linearize(float32(v[1]) / 255),
linearize(float32(v[2]) / 255),
}
}
// NormalizeRGB transform a RGBA float32 color (from 0 to 1)
// to its uint8 represtation (from 0 to 255).
func NormalizeRGBA(v [4]float32) [4]uint8 {
return [4]uint8{
uint8(deliniarize(v[0]) * 255),
uint8(deliniarize(v[1]) * 255),
uint8(deliniarize(v[2]) * 255),
uint8(v[3] * 255),
}
}
// DenormalizeRGBA transform a RGBA uint8 color (from 0 to 255)
// to its float represtation (from 0 to 1).
func DenormalizeRGBA(v [4]uint8) [4]float32 {
return [4]float32{
linearize(float32(v[0]) / 255),
linearize(float32(v[1]) / 255),
linearize(float32(v[2]) / 255),
float32(v[3]) / 255,
}
}
// NormalizeRGB64 transform a RGB float32 color (from 0 to 1)
// to its uint16 represtation (from 0 to 65535).
func NormalizeRGB64(v [3]float32) [3]uint16 {
return [3]uint16{
uint16(deliniarize(v[0]) * 65535),
uint16(deliniarize(v[1]) * 65535),
uint16(deliniarize(v[2]) * 65535),
}
}
// DenormalizeRGB64 transform a RGB uint16 color (from 0 to 65535)
// to its float represtation (from 0 to 1).
func DenormalizeRGB64(v [3]uint16) [3]float32 {
return [3]float32{
linearize(float32(v[0]) / 65535),
linearize(float32(v[1]) / 65535),
linearize(float32(v[2]) / 65535),
}
}
// NormalizeRGBA64 transform a RGBA float32 color (from 0 to 1)
// to its uint16 represtation (from 0 to 65535).
func NormalizeRGBA64(v [4]float32) [4]uint16 {
return [4]uint16{
uint16(deliniarize(v[0]) * 65535),
uint16(deliniarize(v[1]) * 65535),
uint16(deliniarize(v[2]) * 65535),
uint16(v[3] * 65535),
}
}
// DenormalizeRGBA64 transform a RGBA uint16 color (from 0 to 65535)
// to its float represtation (from 0 to 1).
func DenormalizeRGBA64(v [4]uint16) [4]float32 {
return [4]float32{
linearize(float32(v[0]) / 65535),
linearize(float32(v[1]) / 65535),
linearize(float32(v[2]) / 65535),
float32(v[3]) / 65535,
}
}
func linearize(v float32) float32 {
if v <= 0.04045 {
return float32(v / 12.92)
}
return float32(math.Pow(float64(v+0.055)/1.055, 2.4))
}
func deliniarize(v float32) float32 {
if v < 0.0031308 {
return v * 12.92
}
return float32(1.055*math.Pow(float64(v), 1.0/2.4) - 0.055)
}