Debian Codegraph @

colors.go

raw symbols SCIP
1 package gift
2
3 import (
4 "image"
5 "image/draw"
6 "math"
7 )
8
9 func prepareLut(lutSize int, fn func(float32) float32) []float32 {
10 lut := make([]float32, lutSize)
11 q := 1 / float32(lutSize-1)
12 for v := range lutSize {
13 u := float32(v) * q
14 lut[v] = fn(u)
15 }
16 return lut
17 }
18
19 func getFromLut(lut []float32, u float32) float32 {
20 v := int(u*float32(len(lut)-1) + 0.5)
21 return lut[v]
22 }
23
24 type colorchanFilter struct {
25 fn func(float32) float32
26 lut bool
27 }
28
29 func (p *colorchanFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) {
30 dstBounds = image.Rect(0, 0, srcBounds.Dx(), srcBounds.Dy())
31 return
32 }
33
34 func (p *colorchanFilter) Draw(dst draw.Image, src image.Image, options *Options) {
35 if options == nil {
36 options = &defaultOptions
37 }
38
39 srcb := src.Bounds()
40 dstb := dst.Bounds()
41 pixGetter := newPixelGetter(src)
42 pixSetter := newPixelSetter(dst)
43
44 var useLut bool
45 var lut []float32
46
47 useLut = false
48 if p.lut {
49 var lutSize int
50
51 it := pixGetter.it
52 if it == itNRGBA || it == itRGBA || it == itGray || it == itYCbCr {
53 lutSize = 0xff + 1
54 } else {
55 lutSize = 0xffff + 1
56 }
57
58 numCalculations := srcb.Dx() * srcb.Dy() * 3
59 if numCalculations > lutSize*2 {
60 useLut = true
61 lut = prepareLut(lutSize, p.fn)
62 }
63 }
64
65 parallelize(options.Workers, srcb.Min.Y, srcb.Max.Y, func(start, stop int) {
66 for y := start; y < stop; y++ {
67 for x := srcb.Min.X; x < srcb.Max.X; x++ {
68 px := pixGetter.getPixel(x, y)
69 if useLut {
70 px.r = getFromLut(lut, px.r)
71 px.g = getFromLut(lut, px.g)
72 px.b = getFromLut(lut, px.b)
73 } else {
74 px.r = p.fn(px.r)
75 px.g = p.fn(px.g)
76 px.b = p.fn(px.b)
77 }
78 pixSetter.setPixel(dstb.Min.X+x-srcb.Min.X, dstb.Min.Y+y-srcb.Min.Y, px)
79 }
80 }
81 })
82 }
83
84 // Invert creates a filter that negates the colors of an image.
85 func Invert() Filter {
86 return &colorchanFilter{
87 fn: func(x float32) float32 {
88 return 1 - x
89 },
90 lut: false,
91 }
92 }
93
94 // ColorspaceSRGBToLinear creates a filter that converts the colors of an image from sRGB to linear RGB.
95 func ColorspaceSRGBToLinear() Filter {
96 return &colorchanFilter{
97 fn: func(x float32) float32 {
98 if x <= 0.04045 {
99 return x / 12.92
100 }
101 return float32(math.Pow(float64((x+0.055)/1.055), 2.4))
102 },
103 lut: true,
104 }
105 }
106
107 // ColorspaceLinearToSRGB creates a filter that converts the colors of an image from linear RGB to sRGB.
108 func ColorspaceLinearToSRGB() Filter {
109 return &colorchanFilter{
110 fn: func(x float32) float32 {
111 if x <= 0.0031308 {
112 return x * 12.92
113 }
114 return float32(1.055*math.Pow(float64(x), 1/2.4) - 0.055)
115 },
116 lut: true,
117 }
118 }
119
120 // Gamma creates a filter that performs a gamma correction on an image.
121 // The gamma parameter must be positive. Gamma = 1 gives the original image.
122 // Gamma less than 1 darkens the image and gamma greater than 1 lightens it.
123 func Gamma(gamma float32) Filter {
124 e := 1 / maxf32(gamma, 1.0e-5)
125 return &colorchanFilter{
126 fn: func(x float32) float32 {
127 return powf32(x, e)
128 },
129 lut: true,
130 }
131 }
132
133 func sigmoid(a, b, x float32) float32 {
134 return 1 / (1 + expf32(b*(a-x)))
135 }
136
137 // Sigmoid creates a filter that changes the contrast of an image using a sigmoidal function and returns the adjusted image.
138 // It's a non-linear contrast change useful for photo adjustments as it preserves highlight and shadow detail.
139 // The midpoint parameter is the midpoint of contrast that must be between 0 and 1, typically 0.5.
140 // The factor parameter indicates how much to increase or decrease the contrast, typically in range (-10, 10).
141 // If the factor parameter is positive the image contrast is increased otherwise the contrast is decreased.
142 //
143 // Example:
144 //
145 // g := gift.New(
146 // gift.Sigmoid(0.5, 5),
147 // )
148 // dst := image.NewRGBA(g.Bounds(src.Bounds()))
149 // g.Draw(dst, src)
150 func Sigmoid(midpoint, factor float32) Filter {
151 a := minf32(maxf32(midpoint, 0), 1)
152 b := absf32(factor)
153 sig0 := sigmoid(a, b, 0)
154 sig1 := sigmoid(a, b, 1)
155 e := float32(1.0e-5)
156
157 return &colorchanFilter{
158 fn: func(x float32) float32 {
159 if factor == 0 {
160 return x
161 } else if factor > 0 {
162 sig := sigmoid(a, b, x)
163 return (sig - sig0) / (sig1 - sig0)
164 } else {
165 arg := minf32(maxf32((sig1-sig0)*x+sig0, e), 1-e)
166 return a - logf32(1/arg-1)/b
167 }
168 },
169 lut: true,
170 }
171 }
172
173 // Contrast creates a filter that changes the contrast of an image.
174 // The percentage parameter must be in range (-100, 100). The percentage = 0 gives the original image.
175 // The percentage = -100 gives solid grey image. The percentage = 100 gives an overcontrasted image.
176 func Contrast(percentage float32) Filter {
177 if percentage == 0 {
178 return &copyimageFilter{}
179 }
180
181 p := 1 + minf32(maxf32(percentage, -100), 100)/100
182
183 return &colorchanFilter{
184 fn: func(x float32) float32 {
185 if 0 <= p && p <= 1 {
186 return 0.5 + (x-0.5)*p
187 } else if 1 < p && p < 2 {
188 return 0.5 + (x-0.5)*(1/(2.0-p))
189 } else {
190 if x < 0.5 {
191 return 0
192 }
193 return 1
194 }
195 },
196 lut: false,
197 }
198 }
199
200 // Brightness creates a filter that changes the brightness of an image.
201 // The percentage parameter must be in range (-100, 100). The percentage = 0 gives the original image.
202 // The percentage = -100 gives solid black image. The percentage = 100 gives solid white image.
203 func Brightness(percentage float32) Filter {
204 if percentage == 0 {
205 return &copyimageFilter{}
206 }
207
208 shift := minf32(maxf32(percentage, -100), 100) / 100
209
210 return &colorchanFilter{
211 fn: func(x float32) float32 {
212 return x + shift
213 },
214 lut: false,
215 }
216 }
217
218 type colorFilter struct {
219 fn func(pixel) pixel
220 }
221
222 func (p *colorFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) {
223 dstBounds = image.Rect(0, 0, srcBounds.Dx(), srcBounds.Dy())
224 return
225 }
226
227 func (p *colorFilter) Draw(dst draw.Image, src image.Image, options *Options) {
228 if options == nil {
229 options = &defaultOptions
230 }
231
232 srcb := src.Bounds()
233 dstb := dst.Bounds()
234 pixGetter := newPixelGetter(src)
235 pixSetter := newPixelSetter(dst)
236
237 parallelize(options.Workers, srcb.Min.Y, srcb.Max.Y, func(start, stop int) {
238 for y := start; y < stop; y++ {
239 for x := srcb.Min.X; x < srcb.Max.X; x++ {
240 px := pixGetter.getPixel(x, y)
241 pixSetter.setPixel(dstb.Min.X+x-srcb.Min.X, dstb.Min.Y+y-srcb.Min.Y, p.fn(px))
242 }
243 }
244 })
245 }
246
247 // Grayscale creates a filter that produces a grayscale version of an image.
248 func Grayscale() Filter {
249 return &colorFilter{
250 fn: func(px pixel) pixel {
251 y := 0.299*px.r + 0.587*px.g + 0.114*px.b
252 return pixel{y, y, y, px.a}
253 },
254 }
255 }
256
257 // Sepia creates a filter that produces a sepia-toned version of an image.
258 // The percentage parameter specifies how much the image should be adjusted. It must be in the range (0, 100)
259 //
260 // Example:
261 //
262 // g := gift.New(
263 // gift.Sepia(100),
264 // )
265 // dst := image.NewRGBA(g.Bounds(src.Bounds()))
266 // g.Draw(dst, src)
267 func Sepia(percentage float32) Filter {
268 adjustAmount := minf32(maxf32(percentage, 0), 100) / 100
269 rr := 1 - 0.607*adjustAmount
270 rg := 0.769 * adjustAmount
271 rb := 0.189 * adjustAmount
272 gr := 0.349 * adjustAmount
273 gg := 1 - 0.314*adjustAmount
274 gb := 0.168 * adjustAmount
275 br := 0.272 * adjustAmount
276 bg := 0.534 * adjustAmount
277 bb := 1 - 0.869*adjustAmount
278 return &colorFilter{
279 fn: func(px pixel) pixel {
280 r := px.r*rr + px.g*rg + px.b*rb
281 g := px.r*gr + px.g*gg + px.b*gb
282 b := px.r*br + px.g*bg + px.b*bb
283 return pixel{r, g, b, px.a}
284 },
285 }
286 }
287
288 func convertHSLToRGB(h, s, l float32) (float32, float32, float32) {
289 if s == 0 {
290 return l, l, l
291 }
292
293 hueToRGB := func(p, q, t float32) float32 {
294 if t < 0 {
295 t++
296 }
297 if t > 1 {
298 t--
299 }
300 if t < 1/6.0 {
301 return p + (q-p)*6*t
302 }
303 if t < 1/2.0 {
304 return q
305 }
306 if t < 2/3.0 {
307 return p + (q-p)*(2/3.0-t)*6
308 }
309 return p
310 }
311
312 var p, q float32
313 if l < 0.5 {
314 q = l * (1 + s)
315 } else {
316 q = l + s - l*s
317 }
318 p = 2*l - q
319
320 r := hueToRGB(p, q, h+1/3.0)
321 g := hueToRGB(p, q, h)
322 b := hueToRGB(p, q, h-1/3.0)
323
324 return r, g, b
325 }
326
327 func convertRGBToHSL(r, g, b float32) (float32, float32, float32) {
328 max := maxf32(r, maxf32(g, b))
329 min := minf32(r, minf32(g, b))
330
331 l := (max + min) / 2
332
333 if max == min {
334 return 0, 0, l
335 }
336
337 var h, s float32
338 d := max - min
339 if l > 0.5 {
340 s = d / (2 - max - min)
341 } else {
342 s = d / (max + min)
343 }
344
345 if r == max {
346 h = (g - b) / d
347 if g < b {
348 h += 6
349 }
350 } else if g == max {
351 h = (b-r)/d + 2
352 } else {
353 h = (r-g)/d + 4
354 }
355 h /= 6
356
357 return h, s, l
358 }
359
360 func normalizeHue(hue float32) float32 {
361 hue = hue - float32(int(hue))
362 if hue < 0 {
363 hue++
364 }
365 return hue
366 }
367
368 // Hue creates a filter that rotates the hue of an image.
369 // The shift parameter is the hue angle shift, typically in range (-180, 180).
370 // The shift = 0 gives the original image.
371 func Hue(shift float32) Filter {
372 p := normalizeHue(shift / 360)
373 if p == 0 {
374 return &copyimageFilter{}
375 }
376
377 return &colorFilter{
378 fn: func(px pixel) pixel {
379 h, s, l := convertRGBToHSL(px.r, px.g, px.b)
380 h = normalizeHue(h + p)
381 r, g, b := convertHSLToRGB(h, s, l)
382 return pixel{r, g, b, px.a}
383 },
384 }
385 }
386
387 // Saturation creates a filter that changes the saturation of an image.
388 // The percentage parameter must be in range (-100, 500). The percentage = 0 gives the original image.
389 func Saturation(percentage float32) Filter {
390 p := 1 + minf32(maxf32(percentage, -100), 500)/100
391 if p == 1 {
392 return &copyimageFilter{}
393 }
394
395 return &colorFilter{
396 fn: func(px pixel) pixel {
397 h, s, l := convertRGBToHSL(px.r, px.g, px.b)
398 s *= p
399 if s > 1 {
400 s = 1
401 }
402 r, g, b := convertHSLToRGB(h, s, l)
403 return pixel{r, g, b, px.a}
404 },
405 }
406 }
407
408 // Colorize creates a filter that produces a colorized version of an image.
409 // The hue parameter is the angle on the color wheel, typically in range (0, 360).
410 // The saturation parameter must be in range (0, 100).
411 // The percentage parameter specifies the strength of the effect, it must be in range (0, 100).
412 //
413 // Example:
414 //
415 // g := gift.New(
416 // gift.Colorize(240, 50, 100), // blue colorization, 50% saturation
417 // )
418 // dst := image.NewRGBA(g.Bounds(src.Bounds()))
419 // g.Draw(dst, src)
420 func Colorize(hue, saturation, percentage float32) Filter {
421 h := normalizeHue(hue / 360)
422 s := minf32(maxf32(saturation, 0), 100) / 100
423 p := minf32(maxf32(percentage, 0), 100) / 100
424 if p == 0 {
425 return &copyimageFilter{}
426 }
427
428 return &colorFilter{
429 fn: func(px pixel) pixel {
430 _, _, l := convertRGBToHSL(px.r, px.g, px.b)
431 r, g, b := convertHSLToRGB(h, s, l)
432 px.r += (r - px.r) * p
433 px.g += (g - px.g) * p
434 px.b += (b - px.b) * p
435 return px
436 },
437 }
438 }
439
440 // ColorBalance creates a filter that changes the color balance of an image.
441 // The percentage parameters for each color channel (red, green, blue) must be in range (-100, 500).
442 //
443 // Example:
444 //
445 // g := gift.New(
446 // gift.ColorBalance(20, -20, 0), // +20% red, -20% green
447 // )
448 // dst := image.NewRGBA(g.Bounds(src.Bounds()))
449 // g.Draw(dst, src)
450 func ColorBalance(percentageRed, percentageGreen, percentageBlue float32) Filter {
451 pr := 1 + minf32(maxf32(percentageRed, -100), 500)/100
452 pg := 1 + minf32(maxf32(percentageGreen, -100), 500)/100
453 pb := 1 + minf32(maxf32(percentageBlue, -100), 500)/100
454
455 return &colorFilter{
456 fn: func(px pixel) pixel {
457 px.r *= pr
458 px.g *= pg
459 px.b *= pb
460 return px
461 },
462 }
463 }
464
465 // Threshold creates a filter that applies black/white thresholding to the image.
466 // The percentage parameter must be in range (0, 100).
467 func Threshold(percentage float32) Filter {
468 p := minf32(maxf32(percentage, 0), 100) / 100
469 return &colorFilter{
470 fn: func(px pixel) pixel {
471 y := 0.299*px.r + 0.587*px.g + 0.114*px.b
472 if y > p {
473 return pixel{1, 1, 1, px.a}
474 }
475 return pixel{0, 0, 0, px.a}
476 },
477 }
478 }
479
480 // ColorFunc creates a filter that changes the colors of an image using custom function.
481 // The fn parameter specifies a function that takes red, green, blue and alpha channels of a pixel
482 // as float32 values in range (0, 1) and returns the modified channel values.
483 //
484 // Example:
485 //
486 // g := gift.New(
487 // gift.ColorFunc(
488 // func(r0, g0, b0, a0 float32) (r, g, b, a float32) {
489 // r = 1 - r0 // invert the red channel
490 // g = g0 + 0.1 // shift the green channel by 0.1
491 // b = 0 // set the blue channel to 0
492 // a = a0 // preserve the alpha channel
493 // return r, g, b, a
494 // },
495 // ),
496 // )
497 // dst := image.NewRGBA(g.Bounds(src.Bounds()))
498 // g.Draw(dst, src)
499 func ColorFunc(fn func(r0, g0, b0, a0 float32) (r, g, b, a float32)) Filter {
500 return &colorFilter{
501 fn: func(px pixel) pixel {
502 r, g, b, a := fn(px.r, px.g, px.b, px.a)
503 return pixel{r, g, b, a}
504 },
505 }
506 }