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
extern crate image;
use image::{GenericImageView, ImageBuffer};
extern crate wasm_bindgen;
use crate::helpers;
use crate::{PhotonImage};
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use image::RgbaImage;
use web_sys::{ImageData, HtmlCanvasElement};
use wasm_bindgen::Clamped;
#[cfg(not(target_arch = "wasm32"))]
#[wasm_bindgen]
pub fn crop(photon_image: &mut PhotonImage, x1: u32, y1: u32, x2: u32, y2: u32) -> PhotonImage {
let img = helpers::dyn_image_from_raw(&photon_image);
let mut cropped_img: RgbaImage = ImageBuffer::new(x2 - x1, y2 - y1);
for x in 0..cropped_img.width() {
for y in 0..cropped_img.height() {
let px = img.get_pixel(x, y);
cropped_img.put_pixel(x, y, px);
}
}
let dynimage = image::ImageRgba8(cropped_img);
let raw_pixels = dynimage.raw_pixels();
let cropped_photon_img = PhotonImage { raw_pixels: raw_pixels, width: dynimage.width(), height: dynimage.height()};
return cropped_photon_img
}
#[wasm_bindgen]
pub fn fliph(photon_image: &mut PhotonImage) {
let img = helpers::dyn_image_from_raw(&photon_image);
let width = img.width();
let mut flipped_img: RgbaImage = ImageBuffer::new(width, img.height());
for x in 0..width {
for y in 0..img.height() {
let px = img.get_pixel(x, y);
flipped_img.put_pixel(width - x - 1, y, px);
}
}
let dynimage = image::ImageRgba8(flipped_img);
let raw_pixels = dynimage.raw_pixels();
photon_image.raw_pixels = raw_pixels;
}
#[wasm_bindgen]
pub fn flipv(photon_image: &mut PhotonImage) {
let img = helpers::dyn_image_from_raw(&photon_image);
let width = img.width();
let height = img.height();
let mut flipped_img: RgbaImage = ImageBuffer::new(width, height);
for x in 0..width {
for y in 0..height {
let px = img.get_pixel(x, y);
flipped_img.put_pixel(x, height - y - 1, px);
}
}
let dynimage = image::ImageRgba8(flipped_img);
let raw_pixels = dynimage.raw_pixels();
photon_image.raw_pixels = raw_pixels;
}
#[wasm_bindgen]
pub enum SamplingFilter {
Nearest = 1,
Triangle = 2,
CatmullRom = 3,
Gaussian = 4,
Lanczos3 = 5,
}
fn filter_type_from_sampling_filter(sampling_filter: SamplingFilter) -> image::FilterType {
match sampling_filter {
SamplingFilter::Nearest => image::FilterType::Nearest,
SamplingFilter::Triangle => image::FilterType::Triangle,
SamplingFilter::CatmullRom => image::FilterType::CatmullRom,
SamplingFilter::Gaussian => image::FilterType::Gaussian,
SamplingFilter::Lanczos3 => image::FilterType::Lanczos3
}
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn resize(photon_img: &PhotonImage, width: u32, height: u32, sampling_filter: SamplingFilter) -> HtmlCanvasElement {
let sampling_filter = filter_type_from_sampling_filter(sampling_filter);
let dyn_img = helpers::dyn_image_from_raw(&photon_img);
let resized_img = image::ImageRgba8(image::imageops::resize(&dyn_img, width, height, sampling_filter));
let document = web_sys::window().unwrap().document().unwrap();
let canvas = document
.create_element("canvas").unwrap()
.dyn_into::<web_sys::HtmlCanvasElement>().unwrap();
canvas.set_width(resized_img.width());
canvas.set_height(resized_img.height());
let new_img_data = ImageData::new_with_u8_clamped_array_and_sh(Clamped(&mut resized_img.raw_pixels()), canvas.width(), canvas.height());
let ctx = canvas
.get_context("2d").unwrap()
.unwrap()
.dyn_into::<web_sys::CanvasRenderingContext2d>().unwrap();
ctx.put_image_data(&new_img_data.unwrap(), 0.0, 0.0);
return canvas;
}
#[cfg(not(target_arch = "wasm32"))]
pub fn resize(photon_img: &PhotonImage, width: u32, height: u32, sampling_filter: SamplingFilter) -> PhotonImage {
let sampling_filter = filter_type_from_sampling_filter(sampling_filter);
let dyn_img = helpers::dyn_image_from_raw(&photon_img);
let resized_img = image::ImageRgba8(image::imageops::resize(&dyn_img, width, height, sampling_filter));
return PhotonImage{ raw_pixels: resized_img.raw_pixels(), width: resized_img.width(), height: resized_img.height()}
}