Skip to content

Commit 54d2c54

Browse files
committed
optimize: make more funcs as lib
1 parent 56bf830 commit 54d2c54

6 files changed

Lines changed: 65 additions & 80 deletions

File tree

effects.go

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,48 @@
11
package gg
22

3-
// Brightness 调整亮度 范围:±100%
4-
func (dc *Context) Brightness(per int) {
5-
if per == 0 {
3+
import (
4+
"image"
5+
"math"
6+
7+
"github.com/disintegration/imaging"
8+
)
9+
10+
// AdjustBrightness 调整亮度 范围:±100%
11+
func (dc *Context) AdjustBrightness(s float64) {
12+
if math.Abs(s) < 0.001 {
13+
return
14+
}
15+
dc.im = (*image.RGBA)(imaging.AdjustBrightness(dc.im, s))
16+
}
17+
18+
// AdjustContrast 调整对比度 范围:±100%
19+
func (dc *Context) AdjustContrast(s float64) {
20+
if math.Abs(s) < 0.001 {
621
return
722
}
8-
per = clamp(per, -100, 100)
9-
gain := 255 * per / 100
10-
for i, v := range dc.im.Pix {
11-
if i%4 == 3 { // alpha
12-
continue
13-
}
14-
dc.im.Pix[i] = uint8(clamp(int(v)+gain, 0, 255))
23+
dc.im = (*image.RGBA)(imaging.AdjustContrast(dc.im, s))
24+
}
25+
26+
// AdjustContrast 调整饱和度 范围:±100%
27+
func (dc *Context) AdjustSaturation(s float64) {
28+
if math.Abs(s) < 0.001 {
29+
return
1530
}
31+
dc.im = (*image.RGBA)(imaging.AdjustSaturation(dc.im, s))
1632
}
1733

18-
// Contrast 调整对比度 范围:±100%
19-
func (dc *Context) Contrast(per int) {
20-
if per == 0 {
34+
// Sharpen 锐化 范围:±100%
35+
func (dc *Context) Sharpen(s float64) {
36+
if math.Abs(s) < 0.001 {
2137
return
2238
}
23-
per = clamp(per, -100, 100) + 100
24-
switch {
25-
case 0 <= per && per < 100: // 损益
26-
gain := per
27-
for i, v := range dc.im.Pix {
28-
if i%4 == 3 { // alpha
29-
continue
30-
}
31-
dc.im.Pix[i] = uint8(clamp(int(v)*gain/100, 0, 255))
32-
}
33-
case 100 < per && per <= 200: // 增益
34-
gain := 200 - per
35-
if gain == 0 {
36-
gain = 1
37-
}
38-
for i, v := range dc.im.Pix {
39-
if i%4 == 3 { // alpha
40-
continue
41-
}
42-
dc.im.Pix[i] = uint8(clamp(int(v)*100/gain, 0, 255))
43-
}
44-
default:
45-
panic("unreachable")
39+
dc.im = (*image.RGBA)(imaging.Sharpen(dc.im, s))
40+
}
41+
42+
// Blur 模糊图像 正数
43+
func (dc *Context) Blur(s float64) {
44+
if math.Abs(s) < 0.001 {
45+
return
4646
}
47+
dc.im = (*image.RGBA)(imaging.Blur(dc.im, s))
4748
}

effects_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ func TestBrightness(t *testing.T) {
1212
t.Fatal(err)
1313
}
1414
dc := NewContextForImage(im)
15-
dc.Brightness(-50)
15+
dc.AdjustBrightness(-50)
1616
if err := saveImage(dc, "TestBrightness-50"); err != nil {
1717
t.Fatal(err)
1818
}
1919
checkHash(t, dc, "<gg.Context 30e0b039be47de836a9fb4c8b9825ce8>")
20-
dc.Brightness(70) // 70-50=20
20+
dc.AdjustBrightness(70) // 70-50=20
2121
if err := saveImage(dc, "TestBrightness+20"); err != nil {
2222
t.Fatal(err)
2323
}
@@ -30,12 +30,12 @@ func TestContrast(t *testing.T) {
3030
t.Fatal(err)
3131
}
3232
dc := NewContextForImage(im)
33-
dc.Contrast(-50)
33+
dc.AdjustContrast(-50)
3434
if err := saveImage(dc, "TestContrast-50"); err != nil {
3535
t.Fatal(err)
3636
}
3737
checkHash(t, dc, "<gg.Context e6f161e094f0d95603aad891c51c0b6b>")
38-
dc.Contrast(100)
38+
dc.AdjustContrast(100)
3939
if err := saveImage(dc, "TestContrast+100"); err != nil {
4040
t.Fatal(err)
4141
}

image.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,23 @@ func GetImageWxH(path string) (int, int, error) {
6060
sz, _, err := imgsz.DecodeSize(f)
6161
return sz.Width, sz.Height, err
6262
}
63+
64+
// LimitImageBounds returns resized image that newW < w and newH < h, while keeping the W/H ratio.
65+
//
66+
// LimitImageBounds 返回在保持宽高比的条件下,小于 w x h 的新 bound。
67+
func ImageBoundsBelow(b image.Rectangle, w, h int) image.Rectangle {
68+
width := b.Dx()
69+
height := b.Dy()
70+
dstw, dsth := width, height
71+
if dstw > w {
72+
dstw = w
73+
ratio := float64(dstw) / float64(width)
74+
dsth *= int(float64(height) * ratio)
75+
}
76+
if dsth > h {
77+
dsth = h
78+
ratio := float64(dsth) / float64(height)
79+
dstw = int(float64(width) * ratio)
80+
}
81+
return image.Rect(0, 0, dstw, dsth)
82+
}

kmeans.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,8 @@ func newKMeansImage(img image.Image, k uint16) kmeansImage {
6060
ki.gpuInit()
6161
}
6262
if !ki.canUseGPU {
63-
width := img.Bounds().Dx()
64-
height := img.Bounds().Dy()
65-
dstw, dsth := width, height
66-
if dstw > 512 {
67-
dstw = 512
68-
ratio := float64(dstw) / float64(width)
69-
dsth *= int(float64(height) * ratio)
70-
}
71-
if dsth > 512 {
72-
dsth = 512
73-
ratio := float64(dsth) / float64(height)
74-
dstw = int(float64(width) * ratio)
75-
}
76-
ki.bounds = image.Rect(0, 0, dstw, dsth)
63+
ki.bounds = ImageBoundsBelow(img.Bounds(), 512, 512)
64+
dstw, dsth := ki.bounds.Dx(), ki.bounds.Dy()
7765
rgbaimg = (*image.RGBA)(imaging.Resize(img, dstw, dsth, imaging.Lanczos))
7866
pixels = unsafe.Slice(
7967
(*color.RGBA)(unsafe.Pointer(unsafe.SliceData(rgbaimg.Pix))),

kmeans_ocl.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gg
22

33
import (
44
_ "embed"
5-
"image"
65
"image/color"
76
"math"
87
"unsafe"
@@ -40,18 +39,8 @@ func init() {
4039
func (ki *kmeansImage) gpuInit() {
4140
width := ki.bounds.Dx()
4241
height := ki.bounds.Dy()
43-
dstw, dsth := width, height
44-
if dstw > 512 {
45-
dstw = 512
46-
ratio := float64(dstw) / float64(width)
47-
dsth *= int(float64(height) * ratio)
48-
}
49-
if dsth > 512 {
50-
dsth = 512
51-
ratio := float64(dsth) / float64(height)
52-
dstw = int(float64(width) * ratio)
53-
}
54-
ki.bounds = image.Rect(0, 0, dstw, dsth)
42+
ki.bounds = ImageBoundsBelow(ki.bounds, 512, 512)
43+
dstw, dsth := ki.bounds.Dx(), ki.bounds.Dy()
5544

5645
krn1st, err := kmeansModel.KernelCreate("assign_first_iter")
5746
if err != nil {

math.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,3 @@ package gg
66
func sq(n float64) float64 {
77
return n * n
88
}
9-
10-
// clamp controls n in [a, b]
11-
//
12-
// clamp 将 n 的范围限制在 [a, b]
13-
func clamp(n, a, b int) int {
14-
if n > b {
15-
n = b
16-
}
17-
if n < a {
18-
n = a
19-
}
20-
return n
21-
}

0 commit comments

Comments
 (0)