Skip to content

Commit

Permalink
Add basic image bit plane analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
lhvy committed Nov 3, 2023
1 parent 224bfe6 commit 42523c1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,41 @@ pub fn lsb_raw_decode(carrier: &[u8], n_bits: usize) -> Vec<u8> {
payload
}

pub fn extract_bit_planes(image: image::RgbImage) {
let mut planes = Vec::new();
for i in 0..8 {
for j in 0..3 {
let mut plane = image.clone();
for pixel in plane.pixels_mut() {
pixel[j] = (pixel[j] & (1 << i)) * 255;
for k in 0..3 {
if k != j {
pixel[k] = 0;
}
}
}
planes.push(plane);
}
}

// Save into output folder, labelled R, G, B 0-7
// Folder first
std::fs::create_dir_all("output").unwrap();
// Then save the images
for (i, plane) in planes.into_iter().enumerate() {
let color = match i % 3 {
0 => "R",
1 => "G",
2 => "B",
_ => unreachable!(),
};
plane
.save(format!("output/{}{}.png", color, i / 3))
.unwrap();
}
println!("Saved images to ./output");
}

pub fn bytes_to_wav(bytes: &[u8]) {
let spec = hound::WavSpec {
channels: 1,
Expand Down
21 changes: 19 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
mod cipher;

use inquire::validator::Validation;
use sneaky::{bytes_to_wav, lsb_decode, lsb_encode, wav_to_bytes};
use sneaky::{bytes_to_wav, extract_bit_planes, lsb_decode, lsb_encode, wav_to_bytes};
use std::ops::BitXor;
use std::path::{Path, PathBuf};

fn main() {
let options = vec!["Encode a message", "Decode a message"];
let options = vec!["Encode a message", "Decode a message", "Analyse a file"];
let choice = inquire::Select::new("Select an option", options)
.prompt()
.unwrap();

match choice {
"Encode a message" => encode(),
"Decode a message" => decode(),
"Analyse a file" => analyse(),
_ => panic!("Invalid option"),
}

Expand Down Expand Up @@ -103,6 +104,22 @@ fn decode() {
}
}

fn analyse() {
let options = vec!["Analyse an image"];
let choice = inquire::Select::new("Select an option", options)
.prompt()
.unwrap();

match choice {
"Analyse an image's bit planes" => {
let image_path = get_path();
let image = image::open(image_path).unwrap().to_rgb8();
extract_bit_planes(image);
}
_ => panic!("Invalid option"),
}
}

fn get_bits() -> u8 {
// How many bits to use for the message, 1-8
let n_bits = inquire::Text::new("How many bits to use for LSB?")
Expand Down

0 comments on commit 42523c1

Please sign in to comment.