-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
356 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[build] | ||
target = "wasm32-unknown-unknown" | ||
# rustflags = "-C target-feature=+simd128" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules/ | ||
dist/ | ||
.parcel-cache/ | ||
.parcel-cache/ | ||
target/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "scanner" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
path = "src-rs/lib.rs" | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
wasm-bindgen = "0.2.78" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"rust-analyzer.cargo.target": "wasm32-unknown-unknown", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use super::Image; | ||
|
||
// TODO: SIMD | ||
pub fn downscale(source: &Image, by: f32) -> Image { | ||
assert!(by > 1.0); | ||
let &Image { data: ref source, width, height } = source; | ||
let over_by = 1.0 / by; | ||
let dw = (width as f32 * over_by) as usize; | ||
let dh = (height as f32 * over_by) as usize; | ||
let mut data = vec![0.0; dh * dw]; | ||
let over_by2 = over_by * over_by; | ||
let mi = dh - 1; | ||
let mj = dw - 1; | ||
for i in 1..mi { | ||
let si = i as f32 * by; | ||
let sie = si + by; | ||
let sif = si as usize; | ||
let sic = sif + 1; | ||
let sief = sie as usize; | ||
let sir = (sic as f32) - si; | ||
let sire = sie - (sief as f32); | ||
let ib = i * dw; | ||
for j in 1..mj { | ||
let sj = j as f32 * by; | ||
let sje = sj + by; | ||
let sjf = sj as usize; | ||
let sjc = sjf + 1; | ||
let sjef = sje as usize; | ||
let sjr = (sjc as f32) - sj; | ||
let sjre = sje - (sjef as f32); | ||
let mut sum = 0.0; | ||
for rsi in sic..sief { | ||
for rsj in sjc..sjef { | ||
sum += source[rsi * width + rsj]; | ||
} | ||
} | ||
for rsj in sjc..sjef { | ||
sum += source[sif * width + rsj] * sir + source[sief * width + rsj] * sire; | ||
} | ||
for rsi in sic..sief { | ||
sum += source[rsi * width + sjf] * sjr + source[rsi * width + sjef] * sjre; | ||
} | ||
sum += source[sif * width + sjf] * sir * sjr; | ||
sum += source[sif * width + sjef] * sir * sjre; | ||
sum += source[sief * width + sjf] * sire * sjr; | ||
sum += source[sief * width + sjef] * sire * sjre; | ||
data[ib + j] = sum * over_by2; | ||
} | ||
} | ||
for i in 1..mi { | ||
let ib = i * dw; | ||
let ibe = ib + mj; | ||
data[ib] = data[ib + 1]; | ||
data[ibe] = data[ibe - 1]; | ||
} | ||
let mibe = mi * dw; | ||
let mib = mibe - dw; | ||
for j in 0..dw { | ||
data[j] = data[j + dw]; | ||
data[mibe + j] = data[mib + j]; | ||
} | ||
Image { data, width: dw, height: dh } | ||
} | ||
|
||
// let over_by = 1.0 / by; | ||
// let width = (source.width as f32 * over_by) as usize; | ||
// let height = (source.height as f32 * over_by) as usize; | ||
// let over_by2 = over_by * over_by; | ||
// let mut data = vec![0.0; width * height]; | ||
// let right = 1; | ||
// let below = width; | ||
// let diag = right + below; | ||
// let sub = (by * 2.0) as usize; | ||
// let mw = source.width - sub; | ||
// let mh = source.height - sub; | ||
// for i in 0..=mh { | ||
// for j in 0..=mw { | ||
// let val = unsafe { *source.data.get_unchecked(i * source.width + j) * over_by2 }; | ||
// let si = i as f32 * over_by; | ||
// let sii = si as usize; | ||
// let sirr = si.fract(); | ||
// let sir = 1.0 - sirr; | ||
// let sj = j as f32 * over_by; | ||
// let sji = sj as usize; | ||
// let sjrr = sj.fract(); | ||
// let sjr = 1.0 - sjrr; | ||
// let di = sii * width + sji; | ||
// unsafe { | ||
// *data.get_unchecked_mut(di) += val * sir * sjr; | ||
// *data.get_unchecked_mut(di + right) += val * sirr * sjr; | ||
// *data.get_unchecked_mut(di + below) += val * sir * sjrr; | ||
// *data.get_unchecked_mut(di + diag) += val * sirr * sjrr; | ||
// } | ||
// } | ||
// } | ||
// for dj in 1..height { | ||
// let ind = dj * width - 3; | ||
// let val = data[ind]; | ||
// data[ind + 1] = val; | ||
// data[ind + 2] = val; | ||
// } | ||
// for di in 0..width { | ||
// let ind = (height - 3) * width + di; | ||
// let val = data[ind]; | ||
// data[ind + width] = val; | ||
// data[ind + width * 2] = val; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use super::{RGBAImage, Image}; | ||
|
||
// TODO: SIMD | ||
pub fn grayscale(source: &RGBAImage) -> Image { | ||
let &RGBAImage { data: ref source, width, height } = source; | ||
Image { | ||
data: source.chunks_exact(4).map(|rgba| unsafe { | ||
(*rgba.get_unchecked(0) as f32) * 0.00116796875 + | ||
(*rgba.get_unchecked(1) as f32) * 0.00229296875 + | ||
(*rgba.get_unchecked(2) as f32) * 0.0004453125 | ||
}).collect(), | ||
width, | ||
height | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use alloc::vec::Vec; | ||
mod grayscale; | ||
mod downscale; | ||
|
||
pub struct Image { | ||
pub data: Vec<f32>, | ||
pub width: usize, | ||
pub height: usize | ||
} | ||
|
||
impl Image { | ||
pub fn downscale(&self, by: f32) -> Image { | ||
downscale::downscale(self, by) | ||
} | ||
} | ||
|
||
pub struct RGBAImage { | ||
pub data: Vec<u8>, | ||
pub width: usize, | ||
pub height: usize | ||
} | ||
|
||
impl RGBAImage { | ||
pub fn to_grayscale(&self) -> Image { | ||
grayscale::grayscale(self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#![no_std] | ||
#[macro_use] | ||
extern crate alloc; | ||
|
||
use wasm_bindgen::prelude::*; | ||
use alloc::vec::Vec; | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
compile_error!("Only compilable to WASM"); | ||
|
||
mod image; | ||
use image::{Image, RGBAImage}; | ||
|
||
struct Point { | ||
x: f32, | ||
y: f32 | ||
} | ||
|
||
struct Rect { | ||
a: Point, | ||
b: Point, | ||
c: Point, | ||
d: Point | ||
} | ||
|
||
impl Into<Vec<f32>> for Rect { | ||
fn into(self) -> Vec<f32> { | ||
vec![self.a.x, self.a.y, self.b.x, self.b.y, self.c.x, self.c.y, self.d.x, self.d.y] | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub fn document(data: Vec<u8>, width: usize, height: usize, by: f32) -> Vec<f32> { | ||
(RGBAImage { data, width, height }).to_grayscale().downscale(by).data | ||
} |
Oops, something went wrong.