-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
56 lines (46 loc) · 1.56 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
mod utils;
use image::flat::{FlatSamples, SampleLayout};
use image::{GenericImage, Rgb, RgbImage};
use wasm_bindgen::prelude::*;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
pub fn setup() {
utils::set_panic_hook();
}
#[wasm_bindgen]
pub fn process_images(image_array1: Vec<u8>, image_array2: Vec<u8>) -> Vec<u8> {
let image1 = array_to_image(image_array1);
let image2 = array_to_image(image_array2);
let mut target = RgbImage::new(
image1.width() + image2.width(),
image1.height().max(image2.height()),
);
// Fill target image with white color.
let white_buffer = FlatSamples {
samples: &[0xff],
layout: SampleLayout {
channels: 3,
channel_stride: 0,
width: target.width(),
width_stride: 0,
height: target.height(),
height_stride: 0,
},
color_hint: None,
};
let white_bg = white_buffer.as_view::<Rgb<u8>>().unwrap();
target.copy_from(&white_bg, 0, 0).unwrap();
target.copy_from(&image1, 0, 0).unwrap();
target.copy_from(&image2, image1.width(), 0).unwrap();
let mut jpg_buffer: Vec<u8> = vec![];
let mut jpg_encoder = image::jpeg::JpegEncoder::new(&mut jpg_buffer);
jpg_encoder.encode_image(&target).unwrap();
jpg_buffer
}
fn array_to_image(array: Vec<u8>) -> RgbImage {
image::load_from_memory(&array).unwrap().into_rgb8()
}