-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
155 lines (140 loc) · 5.38 KB
/
lib.rs
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
mod utils;
use wasm_bindgen::prelude::*;
use js_sys::Uint8Array;
use image::{ ImageBuffer, Rgba };
// use web_sys::console; // for debugging
#[derive(Clone)]
struct EnergyData {
energy: i64,
}
#[wasm_bindgen]
pub fn process_image(data: JsValue, width: usize, height: usize, iterations: usize) -> Uint8Array {
let data_u8_array: Uint8Array = Uint8Array::from(data);
let data_vec: Vec<u8> = data_u8_array.to_vec();
let img = ImageBuffer::<Rgba<u8>, Vec<u8>>
::from_vec(width as u32, height as u32, data_vec)
.unwrap();
let final_img = seam_carve(img, iterations);
let final_data = final_img.into_raw();
let final_data_u8_array = Uint8Array::from(final_data.as_slice());
return final_data_u8_array;
}
#[wasm_bindgen]
pub fn mark_seam(data: JsValue, width: usize, height: usize) -> Uint8Array {
let data_u8_array: Uint8Array = Uint8Array::from(data);
let data_vec: Vec<u8> = data_u8_array.to_vec();
let img = ImageBuffer::<Rgba<u8>, Vec<u8>>
::from_vec(width as u32, height as u32, data_vec)
.unwrap();
let final_img = handle_mark_seam(img);
let final_data = final_img.into_raw();
let final_data_u8_array = Uint8Array::from(final_data.as_slice());
return final_data_u8_array;
}
fn compute_energies(energies: &mut Vec<Vec<EnergyData>>, img: &ImageBuffer<Rgba<u8>, Vec<u8>>) {
let (width, height) = img.dimensions();
for y in 0..height as usize {
for x in 0..width as usize {
let left_x = if x == 0 { width - 1 } else { (x - 1) as u32 };
let right_x = (if x == (width as usize) - 1 { 0 } else { x + 1 }) as u32;
let top_y = if y == 0 { height - 1 } else { (y - 1) as u32 };
let bottom_y = (if y == (height as usize) - 1 { 0 } else { y + 1 }) as u32;
let left_pixel = img.get_pixel(left_x, y as u32).0;
let right_pixel = img.get_pixel(right_x, y as u32).0;
let top_pixel = img.get_pixel(x as u32, top_y).0;
let bottom_pixel = img.get_pixel(x as u32, bottom_y).0;
let dx = [
((left_pixel[0] as i32) - (right_pixel[0] as i32)).abs(),
((left_pixel[1] as i32) - (right_pixel[1] as i32)).abs(),
((left_pixel[2] as i32) - (right_pixel[2] as i32)).abs(),
];
let dy = [
((top_pixel[0] as i32) - (bottom_pixel[0] as i32)).abs(),
((top_pixel[1] as i32) - (bottom_pixel[1] as i32)).abs(),
((top_pixel[2] as i32) - (bottom_pixel[2] as i32)).abs(),
];
let energy: i64 = dx
.iter()
.chain(dy.iter())
.map(|&d| (d as i64) * (d as i64))
.sum();
energies[y][x].energy = energy;
}
}
}
fn find_seam(energies: &Vec<Vec<EnergyData>>) -> Vec<usize> {
let height = energies.len();
let width = energies[0].len();
let mut seam_energies = vec![vec![0; width]; height];
let mut choices = vec![vec![0; width]; height];
seam_energies[0] = energies[0].iter().map(|e| e.energy).collect();
for y in 1..height {
for x in 0..width {
// support wrap around for left and right edges
let left_x = if x == 0 { width - 1 } else { x - 1 };
let right_x = if x == width - 1 { 0 } else { x + 1 };
let neighbors = [left_x, x, right_x];
let (chosen_idx, min_energy) = neighbors
.iter()
.map(|&x| seam_energies[y - 1][x])
.enumerate()
.min_by_key(|&(_, e)| e)
.unwrap();
seam_energies[y][x] = energies[y][x].energy + min_energy;
choices[y][x] = neighbors[chosen_idx];
}
}
let mut seam = Vec::with_capacity(height);
let mut curr_x = seam_energies[height - 1]
.iter()
.enumerate()
.min_by_key(|&(_, e)| e)
.unwrap().0;
seam.push(curr_x);
for y in (1..height).rev() {
curr_x = choices[y][curr_x];
seam.push(curr_x);
}
seam.reverse();
seam
}
fn remove_seam(
img: &ImageBuffer<Rgba<u8>, Vec<u8>>,
seam: &Vec<usize>
) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
let (width, height) = img.dimensions();
let mut new_img = ImageBuffer::<Rgba<u8>, Vec<u8>>::new(width - 1, height);
for y in 0..height {
let mut curr_x = 0;
for x in 0..width {
if x != (seam[y as usize] as u32) {
new_img.put_pixel(curr_x, y, *img.get_pixel(x, y));
curr_x += 1;
}
}
}
new_img
}
fn seam_carve(
mut img: ImageBuffer<Rgba<u8>, Vec<u8>>,
iterations: usize
) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
for _ in 0..iterations {
let (width, height) = img.dimensions();
let mut energies = vec![vec![EnergyData { energy: 0 }; width as usize]; height as usize];
compute_energies(&mut energies, &img);
let seam = find_seam(&energies);
img = remove_seam(&img, &seam);
}
img
}
fn handle_mark_seam(mut img: ImageBuffer<Rgba<u8>, Vec<u8>>) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
let (width, height) = img.dimensions();
let mut energies = vec![vec![EnergyData { energy: 0 }; width as usize]; height as usize];
compute_energies(&mut energies, &img);
let seam = find_seam(&energies);
for y in 0..height {
img.put_pixel(seam[y as usize] as u32, y, Rgba([255, 0, 0, 255]));
}
img
}