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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! Stego library
//! A steganography swiss army knife

use image::{GenericImageView, RgbaImage, DynamicImage, Rgba, Pixel};

const MASK_ONE_VALUES: &[u8] = &[1,2,4,8,16,32,64,128];
const MASK_ZERO_VALUES: &[u8] = &[254,253,251,247,239,223,191,127];

pub struct LSBStego  {
    /// Image loaded into Stego
    image: RgbaImage,
    /// Hieght of loaded image
    height: u32,
    /// Width of loaded image
    width: u32,

    /// Number of channels in loaded image
    channels: usize,

    /// Current width position
    current_width: u32,
    /// Current height position
    current_height: u32,
    /// Current channel position
    current_channel: usize,

    /// Current index in the MASK_ONE_VALUES
    mask_one: usize,
    /// Current index in the MASK_ZERO_VALUES
    mask_zero: usize,

}

impl LSBStego {

    /// Create a new LSBStego instance by taking in a DynamicImage
    pub fn new(im: DynamicImage) -> Self {
        let (width, height) = im.dimensions();


        LSBStego {
            image: im.to_rgba(),
            width,
            height,
            channels: <Rgba<u8> as Pixel>::channel_count() as usize,
            current_height: 0,
            current_width: 0,
            current_channel: 0,
            mask_one: 0,
            mask_zero: 0
        }
    }

    /// Create a new LSBStego instance by taking in a DynamicImage
    pub fn from_rgba(im: RgbaImage) -> Self {
        let (width, height) = im.dimensions();


        LSBStego {
            image: im,
            width,
            height,
            channels: <Rgba<u8> as Pixel>::channel_count() as usize,
            current_height: 0,
            current_width: 0,
            current_channel: 0,
            mask_one: 0,
            mask_zero: 0
        }
    }

    // /// Returns the size of the loaded image
    // fn get_size(&self) -> u32 {
    //     self.height * self.width
    // }

    /// Returns the mask value of the current maskONE index
    pub fn get_mask_one(&self) -> usize {
        MASK_ONE_VALUES[self.mask_one as usize] as usize
    }

    /// Returns the mask value of the current maskZERO index
    pub fn get_mask_zero(&self) -> usize {
        MASK_ZERO_VALUES[self.mask_zero as usize] as usize
    }

    /// Put a string of binary_values into `self.image`
    pub fn put_binary_value(&mut self, bits: String) {
        for c in bits.chars() {
            // Get pixel value
            let val = self.image.get_pixel_mut(self.current_width, self.current_height);

            if c == '1' {
                val[self.current_channel] = val[self.current_channel] | MASK_ONE_VALUES[self.mask_one as usize]; // Or with maskONE
            }
            else {
                val[self.current_channel] = val[self.current_channel] & MASK_ZERO_VALUES[self.mask_zero as usize]; // And with maskZERO
            }

            *val = *val;

            self.next_slot();
        }
            

    }

    /// move to the next slot where information can me mutated
    pub fn next_slot(&mut self) {
        if self.current_channel == self.channels - 1 {
            self.current_channel = 0;

            if self.current_width == self.width - 1 {
                self.current_width = 0;

                if self.current_height == self.height - 1 {
                    self.current_height = 0;

                    if MASK_ONE_VALUES[self.mask_one as usize] == 128 {
                        panic!("No available slots remaining (image filled)");
                    }
                    else {
                        self.mask_one += 1;
                        self.mask_zero += 1;
                    }
                }
                else {
                    self.current_height += 1;
                }
            }
            else {
                self.current_width += 1;
            }
        }
        else {
            self.current_channel += 1;
        }
    }

    /// Read a single bit from the image
    fn read_bit(&mut self) -> char {
        let val = self.image.get_pixel(self.current_width, self.current_height)[self.current_channel];
        let val = val & MASK_ONE_VALUES[self.mask_one];
        self.next_slot();

        if val > 0 { '1' } else { '0' }
        
    }

    /// Read a byte of the image
    fn read_byte(&mut self) -> String {
        self.read_bits(8)
    }

    /// Read n bits from an image
    fn read_bits(&mut self, n: u32) -> String {
        let mut bits = String::with_capacity(n as usize);

        for _ in 0..n {
            bits.push(self.read_bit())
        }

        bits
    }

    /// Returns a binary string in byte size of a given integer
    fn byte_value(&self, val: usize) -> String {
        self.binary_value(val, 8)
    }
    
    /// Returns the binary of a given integer in the length of `bitsize`
    fn binary_value(&self, val: usize, bitsize: usize) -> String {
        let mut binval = String::with_capacity(bitsize);
        binval.push_str(&format!("{:b}", val));

        if binval.len() > bitsize {
            panic!("binary value larger than the expected size");
        }
        
        while binval.len() < bitsize {
            binval.insert(0, '0');
        }
        binval

    }
    
    /// Encodes a text message into an image
    pub fn encode_text(&mut self, txt: String) -> RgbaImage {
        // Length coded on 2 bytes
        let binl = self.binary_value(txt.len(), 16);
        self.put_binary_value(binl);
        for c in txt.chars() {
            let byte_value = self.byte_value(c as usize);
            self.put_binary_value(byte_value)
        }

        // Return the new image
        self.image.clone()
    }

    /// Decodes a hidden message from an image
    pub fn decode_text(&mut self) -> String {
        let size = self.read_bits(16);
        let l = u32::from_str_radix(&size, 2).unwrap();

        let mut txt = String::new();

        for _ in 0..l {
            let tmp = self.read_byte();
            txt.push(u32::from_str_radix(&tmp,2).unwrap() as u8 as char);
        }

        txt
    }

    /// Encodes an image into another image
    pub fn encode_image(&mut self, im: DynamicImage) -> RgbaImage {
        let im = im.to_bgra();
        let (width, height) = im.dimensions();

        let channels = <Rgba<u8> as Pixel>::channel_count() as u32;

        if self.width * self.height * (self.channels as u32) < width * height * channels {
            panic!("Carrier image not big enough to hold hidden image");
        }

        let binw = self.binary_value(width as usize, 16);
        let binh = self.binary_value(height as usize, 16);

        self.put_binary_value(binw);
        self.put_binary_value(binh);

        for h in 0..height{
            for w in 0..width {
                for chan in 0..channels {
                    let val = im.get_pixel(w, h);
                    self.put_binary_value(self.byte_value(val[chan as usize] as usize));
                    println!("Chan: {}/{}, Val: {}", chan, channels, val[chan as usize]);
                }

            }
        }

        self.image.clone()
    }

    /// Decodes a hidden image from another image
    pub fn decode_image(&mut self) -> RgbaImage {
        let channels = <Rgba<u8> as Pixel>::channel_count() as u32;

        let width = u32::from_str_radix(&self.read_bits(16), 2).unwrap();
        let height = u32::from_str_radix(&self.read_bits(16), 2).unwrap();

        let mut unhideimg = image::RgbaImage::new(width, height);

        for h in 0..height {
            for w in 0..width {
                for chan in 0..channels {
                    let val = unhideimg.get_pixel_mut(w,h);
                    // let color = match chan {
                    //     0 => 0, // Red
                    //     1 => 1, // Green
                    //     2 => 2, // Blue
                    //     3 => 3, // Alpha
                    //     _ => continue
                            
                    // };

                    val[chan as usize] = u8::from_str_radix(&self.read_byte(), 2).unwrap();
                    println!("Chan: {}/{}, Val: {}", chan, channels, val[chan as usize]);
                }
            }
        }

        unhideimg
    }

    /// Encodes a binary file into the image
    pub fn encode_binary(&mut self, data: Vec<u8>) -> RgbaImage {
        let length = data.len();

        if self.width*self.height*(self.channels as u32) < length as u32 + 64 {
            panic!("Carrier image not big enough to hold hidden file");
        }

        self.put_binary_value(self.binary_value(length, 64));

        for byte in data {
            self.put_binary_value(self.byte_value(byte as usize));
        }

        self.image.clone()
    }

    /// Encodes a binary file into the image
    pub fn decode_binary(&mut self) -> Vec<u8> {
        let length = usize::from_str_radix(&self.read_bits(64), 2).unwrap();
        let mut output: Vec<u8> = Vec::with_capacity(length);

        if self.width*self.height*(self.channels as u32) < length as u32 + 64 {
            panic!("Carrier image not big enough to hold hidden file");
        }

        for _ in 0..length{
            output.push(u8::from_str_radix(&self.read_byte(),2).unwrap());
        }
         
        output

    }
}