%
% LSB text steganography encoder
% 
% Function to hide the text message inside the image matrix
% supplied. Returns the stego matrix and also the keys
% representing the random locations of the bits
% 
% We really dont need the keys, just the seed for a PRNG
% and we can write the output to an image file 
% but I have done it this way as its easier
%
function [mat, keys]= lsb_send(mat, message)


% read message and convert to binary string of bits
message = dec2bin(message, 8);
marraysize = size(message);
binmessage ='';
for i = 1:marraysize(1,1),
    binmessage = strcat(binmessage,message(i,:));
end

% get size of matrix
s = size(mat); count = s(1,1)*s(1,2);
message_size = size(message);
msize = message_size(1,1)*message_size(1,2);

% get list of random keys
keys = randperm(msize);
k = keys(1);

for i = 1:msize,    
    
    % convert 1d k into 2d x,y location
    y= mod(k, s(1,2));
    if y == 0, 
        y = s(1,2);
    end
    if floor(k/s(1,2)) < k/s(1,2),
        x = floor(k/s(1,2)) + 1;
    else,
        x = k/s(1,2);           
    end    
    
    % edge check
    if x > s(1,1),
        x = s(1,1);
    end        
    
 
    % embed one bit in the message
    binstr = dec2bin(double(mat(x,y,3)),8);
    binstr(1,8) = char(binmessage(i));
    mat(x,y,3) = bin2dec(binstr)  ;
    
    % goto next random location
    k = k + keys(i);
    
    % wrap around
    if k > count,
        k = k-count;
    end
    
end
