1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
extern crate image;

use std::path::Path;

use std::io::prelude::*;
use std::fs::File;

use image::{
	GenericImage,
	DynamicImage,
	ImageBuffer,
	Rgba
};


#[test]
fn test_buffer_read_write() {
	let buffer = [0x00_u8];

	let out = write_to_file(&buffer, "test.jpg".to_string());

    let ref mut pout = &Path::new("test.png");
    // Write the contents of this image to the Writer in PNG format.
    let _ = out.save(pout).unwrap();

	let out_buf = read_from_file("test.png".to_string());

	assert_eq!(0 as u8, *out_buf.get(0).unwrap());
}

#[test]
fn test_string_read_write() {
	let message = "test".to_string();
	
	let out = message_to_file(message, "test.jpg".to_string());
		
    let ref mut pout = &Path::new("test.png");
    // Write the contents of this image to the Writer in PNG format.
    let _ = out.save(pout).unwrap();

	let out_buf = message_from_file("test.png".to_string(), 4 as usize);

	assert_eq!("test".to_string(), out_buf);
}

#[test]
fn test_file_read_write() {
	let out = file_to_file("test.txt".to_string(), "test.jpg".to_string());
	
    let ref mut pout = &Path::new("test.png");
    // Write the contents of this image to the Writer in PNG format.
    let _ = out.save(pout).unwrap();
	
	let out_buf = message_from_file("test.png".to_string(), 14 as usize);
	assert_eq!("this is a test".to_string(), out_buf);
}

pub fn write_to_file(input: &[u8], filename: String) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
	let img = image::open(&Path::new(&filename)).unwrap();
	return write_to_image(input, img);
}

pub fn read_from_file(filename: String) -> Vec<u8> {
	let img = image::open(&Path::new(&filename)).unwrap().to_rgba();
	return read_from_image(img);	
}

pub fn write_to_image(input: &[u8], img: DynamicImage) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
	let (width, height) = img.dimensions();
	let bytes = width * height;

	if input.len() > bytes as usize{
		panic!("Input is too large for image size");
	}

	let mut out = ImageBuffer::new(width, height);
	
	for (x, y, pixel) in img.pixels() {
		let mut tmp_pixel = pixel;
		
		let inputIndex = x + (y * width);
		
		if inputIndex < input.len() as u32{
			tmp_pixel.data[3] = input[inputIndex as usize];
		}

		out.put_pixel(x, y, tmp_pixel);
	}

	return out;
}

pub fn read_from_image(img: ImageBuffer<Rgba<u8>, Vec<u8>>) -> Vec<u8> {
	let mut out: Vec<u8> = Vec::new();

	for (x, y, pixel) in img.enumerate_pixels() {
		out.push(pixel.data[3]);
	}

	return out;
}

pub fn message_to_image(src: String, img: DynamicImage) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
	return write_to_image(src.as_bytes(), img);
}

pub fn message_to_file(src: String, filename: String) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
	let img = image::open(&Path::new(&filename)).unwrap();
	return message_to_image(src, img);
}

pub fn message_from_image(img: ImageBuffer<Rgba<u8>, Vec<u8>>, len: usize) -> String {
	let mut out = read_from_image(img);
	out.truncate(len);
	return String::from_utf8_lossy(&out).into_owned();	
}

pub fn message_from_file(filename: String, len: usize) -> String {
	let img = image::open(&Path::new(&filename)).unwrap().to_rgba();
	return message_from_image(img, len);
}

pub fn file_to_image(src: String, img: DynamicImage) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
	let mut f = File::open(src).expect("Could not open file");
	let mut buffer = Vec::new();
	f.read_to_end(&mut buffer).expect("Could not read file");
	return write_to_image(buffer.as_slice(), img);
}

pub fn file_to_file(src: String, filename: String) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
	let mut f = File::open(src).expect("Could not open file");
	let mut buffer = Vec::new();
	f.read_to_end(&mut buffer).expect("Could not read file");
	return write_to_file(buffer.as_slice(), filename);
}

/*
pub fn file_from_image() {

}

pub fn file_from_file() {

}*/