-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.go
54 lines (45 loc) · 1.29 KB
/
transform.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
package bild
import "image"
// FlipH returns a horizontally flipped version of the image.
func FlipH(img image.Image) *image.RGBA {
bounds := img.Bounds()
src := CloneAsRGBA(img)
dst := image.NewRGBA(bounds)
w, h := dst.Bounds().Max.X, dst.Bounds().Max.Y
parallelize(h, func(start, end int) {
for y := start; y < end; y++ {
for x := 0; x < w; x++ {
iy := y * dst.Stride
pos := iy + (x * 4)
flippedX := w - x - 1
flippedPos := iy + (flippedX * 4)
dst.Pix[pos+0] = src.Pix[flippedPos+0]
dst.Pix[pos+1] = src.Pix[flippedPos+1]
dst.Pix[pos+2] = src.Pix[flippedPos+2]
dst.Pix[pos+3] = src.Pix[flippedPos+3]
}
}
})
return dst
}
// FlipV returns a vertically flipped version of the image.
func FlipV(img image.Image) *image.RGBA {
bounds := img.Bounds()
src := CloneAsRGBA(img)
dst := image.NewRGBA(bounds)
w, h := dst.Bounds().Max.X, dst.Bounds().Max.Y
parallelize(h, func(start, end int) {
for y := start; y < end; y++ {
for x := 0; x < w; x++ {
pos := y*dst.Stride + (x * 4)
flippedY := h - y - 1
flippedPos := flippedY*dst.Stride + (x * 4)
dst.Pix[pos+0] = src.Pix[flippedPos+0]
dst.Pix[pos+1] = src.Pix[flippedPos+1]
dst.Pix[pos+2] = src.Pix[flippedPos+2]
dst.Pix[pos+3] = src.Pix[flippedPos+3]
}
}
})
return dst
}