-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.mbt
More file actions
55 lines (53 loc) · 1.75 KB
/
Copy pathtransform.mbt
File metadata and controls
55 lines (53 loc) · 1.75 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
///|
/// Horizontal mirror: `dst(x, y) = src(width-1-x, y)`. Each pixel's four
/// channels travel together, so alpha is preserved per pixel.
pub fn Image::flip_horizontal(self : Image) -> Image {
let out = Image::new(self.width, self.height)
for y in 0..<self.height {
for x in 0..<self.width {
let src = (y * self.width + (self.width - 1 - x)) * 4
let dst = (y * self.width + x) * 4
out.data[dst] = self.data[src]
out.data[dst + 1] = self.data[src + 1]
out.data[dst + 2] = self.data[src + 2]
out.data[dst + 3] = self.data[src + 3]
}
}
out
}
///|
/// Vertical mirror: `dst(x, y) = src(x, height-1-y)`.
pub fn Image::flip_vertical(self : Image) -> Image {
let out = Image::new(self.width, self.height)
for y in 0..<self.height {
for x in 0..<self.width {
let src = ((self.height - 1 - y) * self.width + x) * 4
let dst = (y * self.width + x) * 4
out.data[dst] = self.data[src]
out.data[dst + 1] = self.data[src + 1]
out.data[dst + 2] = self.data[src + 2]
out.data[dst + 3] = self.data[src + 3]
}
}
out
}
///|
/// Rotates the image 90° clockwise. The result is `height x width`, so this
/// is a library-level transform and is intentionally not exposed through the
/// fixed-size `apply_filter_id` dispatch.
pub fn Image::rotate90(self : Image) -> Image {
let out = Image::new(self.height, self.width)
for y in 0..<self.height {
for x in 0..<self.width {
let nx = self.height - 1 - y
let ny = x
let src = (y * self.width + x) * 4
let dst = (ny * out.width + nx) * 4
out.data[dst] = self.data[src]
out.data[dst + 1] = self.data[src + 1]
out.data[dst + 2] = self.data[src + 2]
out.data[dst + 3] = self.data[src + 3]
}
}
out
}