Skip to content

Commit 124ed0c

Browse files
committed
use f64
1 parent c7a64de commit 124ed0c

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

app/index.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@
4242
</div>
4343

4444
<div id="footer">
45-
<p style="text-align: center; font-size: 0.8em; color: #666">
46-
The code runs entirely in your browser using WebAssembly. <br>
47-
The source code is available at <a href="https://github.com/MenxLi/chaotic-enc">menxli/chaotic-enc</a>.
45+
<p style="text-align: center; font-size: 0.75em; color: #666">
46+
The code runs entirely in your browser using WebAssembly.
47+
<br>
48+
The source code is available at
49+
<a href="https://github.com/MenxLi/chaotic-enc">menxli/chaotic-enc</a>.
4850
</p>
4951
</div>
5052

src/lib.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ pub struct ImageOptions {
2525
pub height: u32,
2626
}
2727

28-
fn str2f(s: &str) -> f32 {
28+
fn str2f(s: &str) -> f64 {
2929
let mut hasher = DefaultHasher::new();
3030
s.hash(&mut hasher);
3131
let hash = hasher.finish();
32-
let x = (hash as f32) / (u64::MAX as f32);
32+
let x = (hash as f64) / (u64::MAX as f64);
3333
x
3434
}
3535

@@ -40,9 +40,9 @@ fn img2vec(im: &[u8], limit_max_side: Option<u32>) -> (Vec<[u8; 3]>, ImageOption
4040
if let Some(max_side) = limit_max_side {
4141
let (mut width, mut height) = rgb.dimensions();
4242
if width > max_side || height > max_side {
43-
let scale = max_side as f32 / width.max(height) as f32;
44-
width = (width as f32 * scale).round() as u32;
45-
height = (height as f32 * scale).round() as u32;
43+
let scale = max_side as f64 / width.max(height) as f64;
44+
width = (width as f64 * scale).round() as u32;
45+
height = (height as f64 * scale).round() as u32;
4646
rgb = image::imageops::resize(
4747
&rgb, width, height,
4848
image::imageops::FilterType::Lanczos3
@@ -73,9 +73,9 @@ fn vec2pngblob(pixels: &Vec<[u8; 3]>, im_opt: ImageOptions, limit_max_side: Opti
7373

7474
if let Some(max_side) = limit_max_side {
7575
if width > max_side || height > max_side {
76-
let scale = max_side as f32 / width.max(height) as f32;
77-
width = (width as f32 * scale).round() as u32;
78-
height = (height as f32 * scale).round() as u32;
76+
let scale = max_side as f64 / width.max(height) as f64;
77+
width = (width as f64 * scale).round() as u32;
78+
height = (height as f64 * scale).round() as u32;
7979
img = image::imageops::resize(
8080
&img, width, height,
8181
image::imageops::FilterType::Lanczos3
@@ -102,12 +102,11 @@ pub fn encode(im: &[u8], secret: &str, max_side: i32) -> Box<[u8]> {
102102
console_log!("Encoding image with secret: {}, max_side: {}", secret, max_side);
103103
let max_side = if max_side < 1 { None } else { Some(max_side as u32) };
104104

105-
let (mut im_v, im_opt) = img2vec(im, max_side);
105+
let (im_v, im_opt) = img2vec(im, max_side);
106+
let seed = str2f(&secret);
107+
console_log!("Seed: {}", seed);
106108

107-
let pixels = logistic_map::encode(
108-
&mut im_v,
109-
str2f(&secret)
110-
);
109+
let pixels = logistic_map::encode(&im_v, seed);
111110

112111
vec2pngblob(&pixels, im_opt, None)
113112
}
@@ -118,11 +117,10 @@ pub fn decode(im: &[u8], secret: &str, max_side: i32) -> Box<[u8]> {
118117
let max_side = if max_side < 1 { None } else { Some(max_side as u32) };
119118

120119
let (im_v, im_opt) = img2vec(im, None);
120+
let seed = str2f(&secret);
121+
console_log!("Seed: {}", seed);
121122

122-
let pixels = logistic_map::decode(
123-
&im_v,
124-
str2f(&secret)
125-
);
123+
let pixels = logistic_map::decode(&im_v, seed);
126124

127125
vec2pngblob(&pixels, im_opt, max_side)
128126
}

src/logistic_map.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

22

3-
const R: f32 = 4.0;
3+
const R: f64 = 4.0;
44

55
struct LogisticMapOptions {
6-
x: f32,
7-
r: f32,
6+
x: f64,
7+
r: f64,
88
size: usize,
99
}
1010

11-
fn generate_map(opt: LogisticMapOptions) -> Vec<f32> {
11+
fn generate_map(opt: LogisticMapOptions) -> Vec<f64> {
1212
let mut pixels = Vec::with_capacity(opt.size);
1313
let mut x = opt.x;
1414

@@ -27,7 +27,7 @@ fn argsort<T: Ord>(vec: &Vec<T>) -> Vec<usize> {
2727
indices
2828
}
2929

30-
fn confuse_xor(im: &Vec<[u8; 3]>, enc_map: &Vec<f32>) -> Vec<[u8; 3]> {
30+
fn confuse_xor(im: &Vec<[u8; 3]>, enc_map: &Vec<f64>) -> Vec<[u8; 3]> {
3131
let mut enc_pixels = Vec::with_capacity(im.len());
3232

3333
for (i, pixel) in enc_map.iter().map(
@@ -47,7 +47,7 @@ enum DiffuseDirection {
4747
Forward,
4848
Backward,
4949
}
50-
fn diffuse(im: &Vec<[u8; 3]>, enc_map: &Vec<f32>, direction: DiffuseDirection) -> Vec<[u8; 3]> {
50+
fn diffuse(im: &Vec<[u8; 3]>, enc_map: &Vec<f64>, direction: DiffuseDirection) -> Vec<[u8; 3]> {
5151
let enc_map = enc_map
5252
.iter()
5353
.map(|&x| x.to_bits())
@@ -78,8 +78,7 @@ fn diffuse(im: &Vec<[u8; 3]>, enc_map: &Vec<f32>, direction: DiffuseDirection) -
7878
diffuse_pixels
7979
}
8080

81-
#[allow(dead_code)]
82-
pub fn encode(im: &Vec<[u8; 3]>, x0: f32) -> Vec<[u8; 3]> {
81+
pub fn encode(im: &Vec<[u8; 3]>, x0: f64) -> Vec<[u8; 3]> {
8382
let enc_map = generate_map(LogisticMapOptions {
8483
x: x0,
8584
r: R,
@@ -90,8 +89,7 @@ pub fn encode(im: &Vec<[u8; 3]>, x0: f32) -> Vec<[u8; 3]> {
9089
confuse_xor(&im, &enc_map)
9190
}
9291

93-
#[allow(dead_code)]
94-
pub fn decode(im: &Vec<[u8; 3]>, x0: f32) -> Vec<[u8; 3]> {
92+
pub fn decode(im: &Vec<[u8; 3]>, x0: f64) -> Vec<[u8; 3]> {
9593
let enc_map = generate_map(LogisticMapOptions {
9694
x: x0,
9795
r: R,

0 commit comments

Comments
 (0)