-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.go
More file actions
295 lines (254 loc) · 7.6 KB
/
Copy pathtransform.go
File metadata and controls
295 lines (254 loc) · 7.6 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
286
287
288
289
290
291
292
293
294
295
package prism
import (
"image"
"image/color"
"math"
)
// FlipH returns a horizontally flipped copy of the image.
func FlipH(img image.Image) *image.NRGBA {
bounds := img.Bounds()
w := bounds.Dx()
h := bounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, w, h))
s := newScanner(img)
parallel(0, h, func(y int) {
row := make([]byte, w*4)
s.scan(bounds.Min.X, bounds.Min.Y+y, bounds.Max.X, bounds.Min.Y+y+1, row)
dstOff := y * dst.Stride
for x := 0; x < w; x++ {
srcX := (w - 1 - x) * 4
dst.Pix[dstOff+x*4+0] = row[srcX+0]
dst.Pix[dstOff+x*4+1] = row[srcX+1]
dst.Pix[dstOff+x*4+2] = row[srcX+2]
dst.Pix[dstOff+x*4+3] = row[srcX+3]
}
})
return dst
}
// FlipV returns a vertically flipped copy of the image.
func FlipV(img image.Image) *image.NRGBA {
bounds := img.Bounds()
w := bounds.Dx()
h := bounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, w, h))
s := newScanner(img)
parallel(0, h, func(y int) {
srcY := h - 1 - y
dstOff := y * dst.Stride
s.scan(bounds.Min.X, bounds.Min.Y+srcY, bounds.Max.X, bounds.Min.Y+srcY+1, dst.Pix[dstOff:dstOff+w*4])
})
return dst
}
// Rotate90 rotates the image 90 degrees clockwise.
func Rotate90(img image.Image) *image.NRGBA {
bounds := img.Bounds()
w := bounds.Dx()
h := bounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, h, w))
s := newScanner(img)
parallel(0, h, func(y int) {
row := make([]byte, w*4)
s.scan(bounds.Min.X, bounds.Min.Y+y, bounds.Max.X, bounds.Min.Y+y+1, row)
// src(x, y) -> dst(h-1-y, x) => dst col = h-1-y, dst row = x
dstX := h - 1 - y
for x := 0; x < w; x++ {
si := x * 4
di := x*dst.Stride + dstX*4
dst.Pix[di+0] = row[si+0]
dst.Pix[di+1] = row[si+1]
dst.Pix[di+2] = row[si+2]
dst.Pix[di+3] = row[si+3]
}
})
return dst
}
// Rotate180 rotates the image 180 degrees.
func Rotate180(img image.Image) *image.NRGBA {
bounds := img.Bounds()
w := bounds.Dx()
h := bounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, w, h))
s := newScanner(img)
parallel(0, h, func(y int) {
row := make([]byte, w*4)
s.scan(bounds.Min.X, bounds.Min.Y+y, bounds.Max.X, bounds.Min.Y+y+1, row)
dstY := h - 1 - y
dstOff := dstY * dst.Stride
for x := 0; x < w; x++ {
srcX := (w - 1 - x) * 4
dst.Pix[dstOff+x*4+0] = row[srcX+0]
dst.Pix[dstOff+x*4+1] = row[srcX+1]
dst.Pix[dstOff+x*4+2] = row[srcX+2]
dst.Pix[dstOff+x*4+3] = row[srcX+3]
}
})
return dst
}
// Rotate270 rotates the image 270 degrees clockwise (90 degrees counter-clockwise).
func Rotate270(img image.Image) *image.NRGBA {
bounds := img.Bounds()
w := bounds.Dx()
h := bounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, h, w))
s := newScanner(img)
parallel(0, h, func(y int) {
row := make([]byte, w*4)
s.scan(bounds.Min.X, bounds.Min.Y+y, bounds.Max.X, bounds.Min.Y+y+1, row)
// src(x, y) -> dst(y, w-1-x) => dst col = y, dst row = w-1-x
dstX := y
for x := 0; x < w; x++ {
si := x * 4
dstY := w - 1 - x
di := dstY*dst.Stride + dstX*4
dst.Pix[di+0] = row[si+0]
dst.Pix[di+1] = row[si+1]
dst.Pix[di+2] = row[si+2]
dst.Pix[di+3] = row[si+3]
}
})
return dst
}
// Transpose flips the image along the top-left to bottom-right diagonal.
// Equivalent to Rotate90 then FlipH.
func Transpose(img image.Image) *image.NRGBA {
bounds := img.Bounds()
w := bounds.Dx()
h := bounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, h, w))
s := newScanner(img)
parallel(0, h, func(y int) {
row := make([]byte, w*4)
s.scan(bounds.Min.X, bounds.Min.Y+y, bounds.Max.X, bounds.Min.Y+y+1, row)
// src(x, y) -> dst(y, x)
dstX := y
for x := 0; x < w; x++ {
si := x * 4
di := x*dst.Stride + dstX*4
dst.Pix[di+0] = row[si+0]
dst.Pix[di+1] = row[si+1]
dst.Pix[di+2] = row[si+2]
dst.Pix[di+3] = row[si+3]
}
})
return dst
}
// Transverse flips the image along the top-right to bottom-left diagonal.
// Equivalent to Rotate90 then FlipV.
func Transverse(img image.Image) *image.NRGBA {
bounds := img.Bounds()
w := bounds.Dx()
h := bounds.Dy()
dst := image.NewNRGBA(image.Rect(0, 0, h, w))
s := newScanner(img)
parallel(0, h, func(y int) {
row := make([]byte, w*4)
s.scan(bounds.Min.X, bounds.Min.Y+y, bounds.Max.X, bounds.Min.Y+y+1, row)
// src(x, y) -> dst(h-1-y, w-1-x)
dstX := h - 1 - y
for x := 0; x < w; x++ {
si := x * 4
dstY := w - 1 - x
di := dstY*dst.Stride + dstX*4
dst.Pix[di+0] = row[si+0]
dst.Pix[di+1] = row[si+1]
dst.Pix[di+2] = row[si+2]
dst.Pix[di+3] = row[si+3]
}
})
return dst
}
// Rotate rotates the image by the given angle (in degrees, counter-clockwise)
// around its center. Empty areas are filled with bgColor.
func Rotate(img image.Image, angle float64, bgColor color.Color) *image.NRGBA {
// Normalize angle to [0, 360).
angle = math.Mod(angle, 360)
if angle < 0 {
angle += 360
}
// Handle exact 90-degree rotations without interpolation.
switch angle {
case 0:
return Clone(img)
case 90:
return Rotate90(img)
case 180:
return Rotate180(img)
case 270:
return Rotate270(img)
}
bounds := img.Bounds()
srcW := bounds.Dx()
srcH := bounds.Dy()
// Calculate new bounds after rotation.
rad := angle * math.Pi / 180
sinA := math.Abs(math.Sin(rad))
cosA := math.Abs(math.Cos(rad))
dstW := int(math.Ceil(float64(srcW)*cosA + float64(srcH)*sinA))
dstH := int(math.Ceil(float64(srcW)*sinA + float64(srcH)*cosA))
if dstW <= 0 {
dstW = 1
}
if dstH <= 0 {
dstH = 1
}
bg := color.NRGBAModel.Convert(bgColor).(color.NRGBA)
dst := New(dstW, dstH, bg)
s := newScanner(img)
// Center of source and destination.
srcCX := float64(srcW) / 2
srcCY := float64(srcH) / 2
dstCX := float64(dstW) / 2
dstCY := float64(dstH) / 2
sinR := math.Sin(-rad)
cosR := math.Cos(-rad)
parallel(0, dstH, func(y int) {
for x := 0; x < dstW; x++ {
// Map destination pixel to source pixel (inverse rotation).
dx := float64(x) - dstCX + 0.5
dy := float64(y) - dstCY + 0.5
srcX := dx*cosR - dy*sinR + srcCX
srcY := dx*sinR + dy*cosR + srcCY
// Bilinear interpolation.
sx := int(math.Floor(srcX))
sy := int(math.Floor(srcY))
if sx < 0 || sy < 0 || sx >= srcW-1 || sy >= srcH-1 {
// Out of bounds — use background (already filled).
if sx >= 0 && sy >= 0 && sx < srcW && sy < srcH {
// Edge pixel — use nearest.
pix := make([]byte, 4)
s.scan(bounds.Min.X+sx, bounds.Min.Y+sy, bounds.Min.X+sx+1, bounds.Min.Y+sy+1, pix)
di := y*dst.Stride + x*4
dst.Pix[di+0] = pix[0]
dst.Pix[di+1] = pix[1]
dst.Pix[di+2] = pix[2]
dst.Pix[di+3] = pix[3]
}
continue
}
fx := srcX - float64(sx)
fy := srcY - float64(sy)
// Read 2x2 block.
block := make([]byte, 2*4)
row1 := make([]byte, 2*4)
s.scan(bounds.Min.X+sx, bounds.Min.Y+sy, bounds.Min.X+sx+2, bounds.Min.Y+sy+1, block)
s.scan(bounds.Min.X+sx, bounds.Min.Y+sy+1, bounds.Min.X+sx+2, bounds.Min.Y+sy+2, row1)
r00, g00, b00, a00 := float64(block[0]), float64(block[1]), float64(block[2]), float64(block[3])
r10, g10, b10, a10 := float64(block[4]), float64(block[5]), float64(block[6]), float64(block[7])
r01, g01, b01, a01 := float64(row1[0]), float64(row1[1]), float64(row1[2]), float64(row1[3])
r11, g11, b11, a11 := float64(row1[4]), float64(row1[5]), float64(row1[6]), float64(row1[7])
r := bilinear(r00, r10, r01, r11, fx, fy)
g := bilinear(g00, g10, g01, g11, fx, fy)
b := bilinear(b00, b10, b01, b11, fx, fy)
a := bilinear(a00, a10, a01, a11, fx, fy)
di := y*dst.Stride + x*4
dst.Pix[di+0] = clamp(r)
dst.Pix[di+1] = clamp(g)
dst.Pix[di+2] = clamp(b)
dst.Pix[di+3] = clamp(a)
}
})
return dst
}
func bilinear(v00, v10, v01, v11, fx, fy float64) float64 {
return v00*(1-fx)*(1-fy) + v10*fx*(1-fy) + v01*(1-fx)*fy + v11*fx*fy
}