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
extern crate image;
extern crate rand;
use image::{DynamicImage, GenericImageView, RgbaImage};
use palette::{Srgba, LinSrgba, Lab, Blend, Lch, Pixel, Gradient, Srgb};
use crate::channels::color_sim;
use crate::{PhotonImage, Rgb, helpers, GenericImage};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn watermark(mut img: &mut PhotonImage, watermark: PhotonImage, x: u32, y: u32) {
let dyn_watermark: DynamicImage = crate::helpers::dyn_image_from_raw(&watermark);
let mut dyn_img: DynamicImage = crate::helpers::dyn_image_from_raw(&img);
image::imageops::overlay(&mut dyn_img, &dyn_watermark, x, y);
img.raw_pixels = dyn_img.raw_pixels();
}
#[wasm_bindgen]
pub fn blend(mut photon_image: &mut PhotonImage, photon_image2: &PhotonImage, blend_mode: &str) {
let img = crate::helpers::dyn_image_from_raw(&photon_image);
let img2 = crate::helpers::dyn_image_from_raw(&photon_image2);
let (width, height) = img.dimensions();
let (width2, height2) = img2.dimensions();
if width > width2 || height > height2 {
panic!("img must be smaller than img2! First image parameter must be smaller than second image parameter.To fix, swap img and img2 params.");
}
let mut img = img.to_rgba();
for x in 0..width {
for y in 0..height {
let px_data = img.get_pixel(x, y).data;
let color: LinSrgba = LinSrgba::from_raw(&px_data).into_format();
let px_data2 = img2.get_pixel(x, y).data;
let color2: LinSrgba = LinSrgba::from_raw(&px_data2).into_format();
let blended = match blend_mode {
"overlay" => color2.overlay(color),
"over" => color2.over(color),
"atop" => color2.atop(color),
"xor" => color2.xor(color),
"plus" => color2.plus(color),
"multiply" => color2.multiply(color),
"burn" => color2.burn(color),
"difference" => color2.difference(color),
"soft_light" => color2.soft_light(color),
"hard_light" => color2.hard_light(color),
"dodge" => color2.dodge(color),
"exclusion" => color2.dodge(color),
"lighten" => color2.lighten(color),
"darken" => color2.darken(color),
_ => color2.overlay(color),
};
img.put_pixel(x, y, image::Rgba {
data: Srgba::from_linear(blended.into()).into_format().into_raw()
});
}
}
let dynimage = image::ImageRgba8(img);
photon_image.raw_pixels = dynimage.raw_pixels();
}
pub fn replace_background(mut photon_image: &mut PhotonImage, img2: &PhotonImage, background_color: Rgb) {
let mut img = helpers::dyn_image_from_raw(&photon_image);
let img2 = helpers::dyn_image_from_raw(&img2);
let (width, height) = img.dimensions();
for x in 0..width {
for y in 0..height {
let px = img.get_pixel(x, y);
let lab: Lab = Srgb::new(background_color.r as f32 / 255.0, background_color.g as f32 / 255.0, background_color.b as f32 / 255.0).into();
let r_val: f32 = px.data[0] as f32 / 255.0;
let g_val: f32 = px.data[1] as f32 / 255.0;
let b_val: f32 = px.data[2] as f32 / 255.0;
let px_lab: Lab = Srgb::new(r_val, g_val, b_val).into();
let sim = color_sim(lab, px_lab);
if sim < 20 {
img.put_pixel(x, y, img2.get_pixel(x, y));
}
else {
img.put_pixel(x, y, px);
}
}
}
let raw_pixels = img.raw_pixels();
photon_image.raw_pixels = raw_pixels;
}
#[wasm_bindgen]
pub fn create_gradient(width: u32, height: u32) -> PhotonImage {
let mut image = RgbaImage::new(width, height);
let grad1 = Gradient::new(vec![
LinSrgba::new(1.0, 0.1, 0.1, 1.0),
LinSrgba::new(0.1, 0.1, 1.0, 1.0),
LinSrgba::new(0.1, 1.0, 0.1, 1.0),
]);
let _grad3 = Gradient::new(vec![
Lch::from(LinSrgba::new(1.0, 0.1, 0.1, 1.0)),
Lch::from(LinSrgba::new(0.1, 0.1, 1.0, 1.0)),
Lch::from(LinSrgba::new(0.1, 1.0, 0.1, 1.0)),
]);
for (i, c1) in grad1
.take(width as usize)
.enumerate()
{
let c1 = Srgba::from_linear(c1).into_format().into_raw();
{
let mut sub_image = image.sub_image(i as u32, 0, 1, height);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgba {
data: c1
});
}
}
}
}
let rgba_img = image::ImageRgba8(image);
let raw_pixels = rgba_img.raw_pixels();
return PhotonImage { raw_pixels: raw_pixels, width: width, height: height};
}
#[wasm_bindgen]
pub fn apply_gradient(mut image: &mut PhotonImage) {
let gradient = create_gradient(image.width, image.height);
blend(&mut image, &gradient, "overlay");
}