Skip to content

Commit 2eca594

Browse files
Merge pull request #151 from axetroy/new_from_blob
feat: create image from Blob and Image
2 parents f465cea + d5a648d commit 2eca594

File tree

4 files changed

+61
-18
lines changed

4 files changed

+61
-18
lines changed

crate/Cargo.toml

+6-5
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@ rand="0.7.2"
2424
imageproc = { version = "0.22.0", default-features = false }
2525
rusttype="0.9.2"
2626
base64="0.13.0"
27-
time="0.2.23"
28-
wasm-bindgen = { version = "=0.2.78", optional = true }
27+
time="0.3.21"
28+
wasm-bindgen = { version = "=0.2.85", optional = true }
2929
serde = { version = "1.0", features = ["derive"] }
3030
thiserror = "1.0"
31-
js-sys = { version = "0.3.45", optional = true }
31+
js-sys = { version = "0.3.62", optional = true }
3232
node-sys = { version = "0.4.2", optional = true }
3333
perlin2d = "0.2.6"
3434

3535
# The `console_error_panic_hook` crate provides better debugging of panics by
3636
# logging them with `console.error`. This is great for development, but requires
3737
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
3838
# code size when deploying.
39-
console_error_panic_hook = { version = "0.1.5", optional = true }
39+
console_error_panic_hook = { version = "0.1.7", optional = true }
4040

4141
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
4242
# compared to the default allocator's ~10K. It is slower than the default
4343
# allocator, however.
4444
wee_alloc = { version = "0.4.2", optional = true }
4545

4646
[dev-dependencies]
47-
time="0.2.1"
47+
time="0.3.21"
4848
criterion = "0.3"
4949

5050
[[bench]]
@@ -69,6 +69,7 @@ features = [
6969
"console",
7070
'CssStyleDeclaration',
7171
'EventTarget',
72+
"Blob"
7273
]
7374
optional = true
7475

crate/benches/photon_benchmark.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use photon_rs::transform::{resize, SamplingFilter};
44
use std::time::Duration;
55

66
fn criterion_benchmark(c: &mut Criterion) {
7-
c.bench_function("invert_image", |b| b.iter(|| invert_image()));
7+
c.bench_function("invert_image", |b| b.iter(invert_image));
88

9-
c.bench_function("resize_png", |b| b.iter(|| resize_png()));
9+
c.bench_function("resize_png", |b| b.iter(resize_png));
1010

11-
c.bench_function("resize_jpg", |b| b.iter(|| resize_jpg()));
11+
c.bench_function("resize_jpg", |b| b.iter(resize_jpg));
1212
}
1313

1414
fn invert_image() {
@@ -22,30 +22,30 @@ fn invert_image() {
2222
let output_img_path = "output.jpg";
2323

2424
// Write to filesystem
25-
save_image(img, output_img_path);
25+
save_image(img, output_img_path).unwrap();
2626
}
2727

2828
fn resize_png() {
29-
let mut img =
29+
let img =
3030
open_image("examples/input_images/underground.png").expect("File should open");
3131

32-
let resized_img = resize(&mut img, 800, 600, SamplingFilter::Lanczos3);
32+
let resized_img = resize(&img, 800, 600, SamplingFilter::Lanczos3);
3333

3434
let output_img_path = "output.png";
3535

36-
save_image(resized_img, output_img_path);
36+
save_image(resized_img, output_img_path).unwrap();
3737
}
3838

3939
fn resize_jpg() {
4040
// Open the image (a PhotonImage is returned)
41-
let mut img =
41+
let img =
4242
open_image("examples/input_images/underground.jpg").expect("File should open");
4343

44-
let resized_img = resize(&mut img, 800, 600, SamplingFilter::Lanczos3);
44+
let resized_img = resize(&img, 800, 600, SamplingFilter::Lanczos3);
4545

4646
let output_img_path = "output.jpg";
4747

48-
save_image(resized_img, output_img_path);
48+
save_image(resized_img, output_img_path).unwrap();
4949
}
5050

5151
fn alter_sample_size() -> Criterion {

crate/examples/seam_carver.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
77
println!("file name = {}", file_name);
88

99
// // Open the image
10-
let mut img = photon_rs::native::open_image(file_name)?;
10+
let img = photon_rs::native::open_image(file_name)?;
1111
let start = time::Instant::now();
1212
// Seam Carver
1313
let (w, h) = (img.get_width(), img.get_height());
1414
println!("original = w: {}, h: {}", w, h);
1515
let w = w - 60;
1616
let h = h - 10;
17-
let res = photon_rs::transform::seam_carve(&mut img, w, h);
17+
let res = photon_rs::transform::seam_carve(&img, w, h);
1818
println!("after = w: {}, h: {}", w, h);
1919

2020
// Write the contents of this image in JPEG format.

crate/src/lib.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ use wasm_bindgen::prelude::*;
5151
use wasm_bindgen::Clamped;
5252

5353
#[cfg(feature = "web-sys")]
54-
use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement, ImageData};
54+
use web_sys::{
55+
Blob, CanvasRenderingContext2d, HtmlCanvasElement, HtmlImageElement, ImageData,
56+
};
5557

5658
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
5759
// allocator.
@@ -101,6 +103,46 @@ impl PhotonImage {
101103
}
102104
}
103105

106+
/// Create a new PhotonImage from a Blob/File.
107+
#[cfg(feature = "web-sys")]
108+
pub fn new_from_blob(blob: Blob) -> PhotonImage {
109+
let bytes: js_sys::Uint8Array = js_sys::Uint8Array::new(&blob);
110+
111+
let vec = bytes.to_vec();
112+
113+
PhotonImage::new_from_byteslice(vec)
114+
}
115+
116+
/// Create a new PhotonImage from a HTMLImageElement
117+
#[cfg(feature = "web-sys")]
118+
pub fn new_from_image(image: HtmlImageElement) -> PhotonImage {
119+
set_panic_hook();
120+
121+
let document = web_sys::window().unwrap().document().unwrap();
122+
123+
let canvas = document
124+
.create_element("canvas")
125+
.unwrap()
126+
.dyn_into::<web_sys::HtmlCanvasElement>()
127+
.unwrap();
128+
129+
canvas.set_width(image.width());
130+
canvas.set_height(image.height());
131+
132+
let context = canvas
133+
.get_context("2d")
134+
.unwrap()
135+
.unwrap()
136+
.dyn_into::<CanvasRenderingContext2d>()
137+
.unwrap();
138+
139+
context
140+
.draw_image_with_html_image_element(&image, 0.0, 0.0)
141+
.unwrap();
142+
143+
open_image(canvas, context)
144+
}
145+
104146
// pub fn new_from_buffer(buffer: &Buffer, width: u32, height: u32) -> PhotonImage {
105147
// // Convert a Node.js Buffer into a Vec<u8>
106148
// let raw_pixels: Vec<u8> = Uint8Array::new_with_byte_offset_and_length(

0 commit comments

Comments
 (0)