-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadjust.go
More file actions
285 lines (249 loc) · 5.87 KB
/
Copy pathadjust.go
File metadata and controls
285 lines (249 loc) · 5.87 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package pixo
import (
"image"
"image/color"
"math"
)
// clampUint8 clamps a float64 to [0, 255] and returns uint8.
func clampUint8(v float64) uint8 {
if v < 0 {
return 0
}
if v > 255 {
return 255
}
return uint8(math.Round(v))
}
// AdjustBrightness adjusts the brightness of img by the given percentage [-100, 100].
func AdjustBrightness(img image.Image, percentage float64) *image.NRGBA {
if img == nil {
return &image.NRGBA{}
}
src := toNRGBA(img)
srcBounds := src.Bounds()
w := srcBounds.Dx()
h := srcBounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, w, h))
shift := 255 * percentage / 100
for y := range h {
for x := range w {
c := src.NRGBAAt(srcBounds.Min.X+x, srcBounds.Min.Y+y)
dst.SetNRGBA(x, y, color.NRGBA{
R: clampUint8(float64(c.R) + shift),
G: clampUint8(float64(c.G) + shift),
B: clampUint8(float64(c.B) + shift),
A: c.A,
})
}
}
return dst
}
// AdjustContrast adjusts the contrast of img by the given percentage [-100, 100].
func AdjustContrast(img image.Image, percentage float64) *image.NRGBA {
if img == nil {
return &image.NRGBA{}
}
src := toNRGBA(img)
srcBounds := src.Bounds()
w := srcBounds.Dx()
h := srcBounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, w, h))
factor := (100 + percentage) / 100
factor = factor * factor
for y := range h {
for x := range w {
c := src.NRGBAAt(srcBounds.Min.X+x, srcBounds.Min.Y+y)
dst.SetNRGBA(x, y, color.NRGBA{
R: clampUint8((float64(c.R)/255-0.5)*factor*255 + 128),
G: clampUint8((float64(c.G)/255-0.5)*factor*255 + 128),
B: clampUint8((float64(c.B)/255-0.5)*factor*255 + 128),
A: c.A,
})
}
}
return dst
}
// AdjustGamma adjusts the gamma of img. Gamma must be > 0.
// Gamma < 1 brightens, gamma > 1 darkens.
func AdjustGamma(img image.Image, gamma float64) *image.NRGBA {
if img == nil {
return &image.NRGBA{}
}
if gamma <= 0 {
return toNRGBA(img)
}
src := toNRGBA(img)
srcBounds := src.Bounds()
w := srcBounds.Dx()
h := srcBounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, w, h))
invGamma := 1.0 / gamma
// Precompute lookup table
var lut [256]uint8
for i := range 256 {
lut[i] = clampUint8(math.Pow(float64(i)/255, invGamma) * 255)
}
for y := range h {
for x := range w {
c := src.NRGBAAt(srcBounds.Min.X+x, srcBounds.Min.Y+y)
dst.SetNRGBA(x, y, color.NRGBA{
R: lut[c.R],
G: lut[c.G],
B: lut[c.B],
A: c.A,
})
}
}
return dst
}
// rgbToHSL converts RGB to HSL. h in [0,360), s,l in [0,1].
func rgbToHSL(r, g, b uint8) (h, s, l float64) {
rf := float64(r) / 255
gf := float64(g) / 255
bf := float64(b) / 255
maxC := math.Max(rf, math.Max(gf, bf))
minC := math.Min(rf, math.Min(gf, bf))
l = (maxC + minC) / 2
if maxC == minC {
return 0, 0, l
}
d := maxC - minC
if l > 0.5 {
s = d / (2 - maxC - minC)
} else {
s = d / (maxC + minC)
}
switch maxC {
case rf:
h = (gf - bf) / d
if gf < bf {
h += 6
}
case gf:
h = (bf-rf)/d + 2
case bf:
h = (rf-gf)/d + 4
}
h *= 60
return
}
func hueToRGB(p, q, t float64) float64 {
if t < 0 {
t += 1
}
if t > 1 {
t -= 1
}
if t < 1.0/6 {
return p + (q-p)*6*t
}
if t < 1.0/2 {
return q
}
if t < 2.0/3 {
return p + (q-p)*(2.0/3-t)*6
}
return p
}
// hslToRGB converts HSL to RGB. h in [0,360), s,l in [0,1].
func hslToRGB(h, s, l float64) (r, g, b uint8) {
if s == 0 {
v := clampUint8(l * 255)
return v, v, v
}
var q float64
if l < 0.5 {
q = l * (1 + s)
} else {
q = l + s - l*s
}
p := 2*l - q
hNorm := h / 360
r = clampUint8(hueToRGB(p, q, hNorm+1.0/3) * 255)
g = clampUint8(hueToRGB(p, q, hNorm) * 255)
b = clampUint8(hueToRGB(p, q, hNorm-1.0/3) * 255)
return
}
// AdjustSaturation adjusts the saturation of img by the given percentage [-100, 100].
func AdjustSaturation(img image.Image, percentage float64) *image.NRGBA {
if img == nil {
return &image.NRGBA{}
}
src := toNRGBA(img)
srcBounds := src.Bounds()
w := srcBounds.Dx()
h := srcBounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, w, h))
factor := 1 + percentage/100
for y := range h {
for x := range w {
c := src.NRGBAAt(srcBounds.Min.X+x, srcBounds.Min.Y+y)
hue, sat, lum := rgbToHSL(c.R, c.G, c.B)
sat = math.Max(0, math.Min(1, sat*factor))
nr, ng, nb := hslToRGB(hue, sat, lum)
dst.SetNRGBA(x, y, color.NRGBA{R: nr, G: ng, B: nb, A: c.A})
}
}
return dst
}
// AdjustSigmoid adjusts the contrast of img using a sigmoidal function.
// Midpoint is the center of the sigmoid (typically 0.5), factor controls steepness.
func AdjustSigmoid(img image.Image, midpoint, factor float64) *image.NRGBA {
if img == nil {
return &image.NRGBA{}
}
src := toNRGBA(img)
srcBounds := src.Bounds()
w := srcBounds.Dx()
h := srcBounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, w, h))
sigmoid := func(x float64) float64 {
return 1.0 / (1.0 + math.Exp(-factor*(x-midpoint)))
}
s0 := sigmoid(0)
s1 := sigmoid(1)
// Precompute lookup table
var lut [256]uint8
for i := range 256 {
v := float64(i) / 255
v = (sigmoid(v) - s0) / (s1 - s0)
lut[i] = clampUint8(v * 255)
}
for y := range h {
for x := range w {
c := src.NRGBAAt(srcBounds.Min.X+x, srcBounds.Min.Y+y)
dst.SetNRGBA(x, y, color.NRGBA{
R: lut[c.R],
G: lut[c.G],
B: lut[c.B],
A: c.A,
})
}
}
return dst
}
// AdjustHue shifts the hue of img by the given degrees [-180, 180].
func AdjustHue(img image.Image, shift int) *image.NRGBA {
if img == nil {
return &image.NRGBA{}
}
src := toNRGBA(img)
srcBounds := src.Bounds()
w := srcBounds.Dx()
h := srcBounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, w, h))
shiftF := float64(shift)
for y := range h {
for x := range w {
c := src.NRGBAAt(srcBounds.Min.X+x, srcBounds.Min.Y+y)
hue, sat, lum := rgbToHSL(c.R, c.G, c.B)
hue = math.Mod(hue+shiftF, 360)
if hue < 0 {
hue += 360
}
nr, ng, nb := hslToRGB(hue, sat, lum)
dst.SetNRGBA(x, y, color.NRGBA{R: nr, G: ng, B: nb, A: c.A})
}
}
return dst
}