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
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;
#[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;
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn resize(photon_img: &PhotonImage, width: u32, height: u32) -> HtmlCanvasElement {
let sampling_filter = image::FilterType::Nearest;
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) -> PhotonImage {
let sampling_filter = image::FilterType::Nearest;
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()}
}