Skip to content

Commit ac4b964

Browse files
committed
channel wise enc
1 parent f60f33d commit ac4b964

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ macro_rules! console_log {
2323
pub struct ImageOptions {
2424
pub width: u32,
2525
pub height: u32,
26+
pub channels: u32,
2627
}
2728

2829
fn str2f(s: &str) -> f64 {
@@ -33,7 +34,7 @@ fn str2f(s: &str) -> f64 {
3334
x
3435
}
3536

36-
fn img2vec(im: &[u8], limit_max_side: Option<u32>) -> (Vec<[u8; 3]>, ImageOptions) {
37+
fn img2vec(im: &[u8], limit_max_side: Option<u32>) -> (Vec<u8>, ImageOptions) {
3738
let img = image::load_from_memory(im).expect("Failed to load image");
3839
let mut rgb = img.to_rgb8();
3940

@@ -50,25 +51,32 @@ fn img2vec(im: &[u8], limit_max_side: Option<u32>) -> (Vec<[u8; 3]>, ImageOption
5051
}
5152
}
5253

53-
let mut pixels = Vec::with_capacity(rgb.width() as usize * rgb.height() as usize);
54+
let mut pixels = Vec::with_capacity(rgb.width() as usize * rgb.height() as usize * 3);
5455
for pixel in rgb.pixels() {
55-
pixels.push([pixel[0], pixel[1], pixel[2]]);
56+
pixels.push(pixel[0]);
57+
pixels.push(pixel[1]);
58+
pixels.push(pixel[2]);
5659
};
5760

5861
(pixels, ImageOptions {
5962
width: rgb.width(),
6063
height: rgb.height(),
64+
channels: 3,
6165
})
6266
}
6367

64-
fn vec2pngblob(pixels: &Vec<[u8; 3]>, im_opt: ImageOptions, limit_max_side: Option<u32>) -> Box<[u8]> {
65-
let ImageOptions { mut width, mut height } = im_opt;
68+
fn vec2pngblob(pixels: &Vec<u8>, im_opt: ImageOptions, limit_max_side: Option<u32>) -> Box<[u8]> {
69+
let ImageOptions { mut width, mut height , channels} = im_opt;
6670

6771
let mut img = image::RgbImage::new(width, height);
68-
for (i, pixel) in pixels.iter().enumerate() {
72+
for (i, pixel) in pixels.chunks(channels as usize).enumerate() {
6973
let x = (i % width as usize) as u32;
7074
let y = (i / width as usize) as u32;
71-
img.put_pixel(x, y, image::Rgb(*pixel));
75+
img.put_pixel(x, y, image::Rgb([
76+
pixel[0],
77+
pixel[1],
78+
pixel[2],
79+
]));
7280
};
7381

7482
if let Some(max_side) = limit_max_side {

src/logistic_map.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,22 @@ fn argsort<T: Ord>(vec: &Vec<T>) -> Vec<usize> {
2727
indices
2828
}
2929

30-
fn confuse_xor(im: &Vec<[u8; 3]>, enc_map: &Vec<f64>) -> Vec<[u8; 3]> {
30+
fn confuse_xor(im: &Vec<u8>, enc_map: &Vec<f64>) -> Vec<u8> {
3131
let mut enc_pixels = Vec::with_capacity(im.len());
32-
3332
for (i, pixel) in enc_map.iter().map(
3433
|&x| (x * 255.0) as u8
3534
).enumerate() {
36-
// use xor to encode the pixel
37-
let r = im[i][0] ^ pixel;
38-
let g = im[i][1] ^ pixel;
39-
let b = im[i][2] ^ pixel;
40-
enc_pixels.push([r, g, b]);
35+
enc_pixels.push(im[i] ^ pixel);
4136
}
42-
4337
enc_pixels
4438
}
4539

4640
enum DiffuseDirection {
4741
Forward,
4842
Backward,
4943
}
50-
fn diffuse(im: &Vec<[u8; 3]>, enc_map: &Vec<f64>, direction: DiffuseDirection) -> Vec<[u8; 3]> {
44+
fn diffuse<T: Copy>(im: &Vec<T>, enc_map: &Vec<f64>, direction: DiffuseDirection) -> Vec<T>
45+
{
5146
let enc_map = enc_map
5247
.iter()
5348
.map(|&x| x.to_bits())
@@ -78,7 +73,7 @@ fn diffuse(im: &Vec<[u8; 3]>, enc_map: &Vec<f64>, direction: DiffuseDirection) -
7873
diffuse_pixels
7974
}
8075

81-
pub fn encode(im: &Vec<[u8; 3]>, x0: f64) -> Vec<[u8; 3]> {
76+
pub fn encode(im: &Vec<u8>, x0: f64) -> Vec<u8> {
8277
let enc_map = generate_map(LogisticMapOptions {
8378
x: x0,
8479
r: R,
@@ -89,7 +84,7 @@ pub fn encode(im: &Vec<[u8; 3]>, x0: f64) -> Vec<[u8; 3]> {
8984
confuse_xor(&im, &enc_map)
9085
}
9186

92-
pub fn decode(im: &Vec<[u8; 3]>, x0: f64) -> Vec<[u8; 3]> {
87+
pub fn decode(im: &Vec<u8>, x0: f64) -> Vec<u8> {
9388
let enc_map = generate_map(LogisticMapOptions {
9489
x: x0,
9590
r: R,

0 commit comments

Comments
 (0)