% This function is the encoding algorithm.  The user has three inputs:
%  image - a string that tells which image to use (ie 'example.tif')
%  code - a binary number vector less than 96 bits in length with message
% 	  to be encoded
%  output - a string that tells which image to output the encoded picture 
%  	    to (ie 'output.tif')
%  In the end, the function will encode the binary vector into the image!

function [] = encode(image,code,output);

sample = imread(image);

samplesize = size(sample);
rows = samplesize(1);
cols = samplesize(2);

sample0= sample(1:round(rows/2), 1:round(cols/2));
sample1= sample(1:round(rows/2), round(cols/2)+1:cols);
sample2= sample(round(rows/2)+1:rows, 1:round(cols/2));
sample3= sample(round(rows/2)+1:rows, round(cols/2)+1:cols);


sampdct0 = dct2(sample0);
sampdct1 = dct2(sample1);
sampdct2 = dct2(sample2);
sampdct3 = dct2(sample3);

blkrows = 1; %# of rows in each block (where one block represents one bit) 
blkcols = 16; %# or columns in each block
blkcount = 12; %# of blocks to encode on each axis
blkspace = 2; %double spaced

horzshift = 50; %column at which the set of blocks on the horizontal axis starts
vertshift = 50; %row at which the set of blocks on the vertical axis starts

for i = 1:blkcount,
   row = i*blkspace;
   col = horzshift;
   if(code(i) == 1),
      sampdct0(row,col:col+blkcols-1) = sampdct0(row,col:col+blkcols-1)./abs(sampdct0(row,col:col+blkcols-1));   
   end
   if(code(i+2*blkcount)==1),
      sampdct1(row,col:col+blkcols-1) = sampdct1(row,col:col+blkcols-1)./abs(sampdct1(row,col:col+blkcols-1));
   end
   if(code(i+4*blkcount)==1),
      sampdct2(row,col:col+blkcols-1) = sampdct2(row,col:col+blkcols-1)./abs(sampdct2(row,col:col+blkcols-1));
   end
   if(code(i+6*blkcount)==1),
      sampdct3(row,col:col+blkcols-1) = sampdct3(row,col:col+blkcols-1)./abs(sampdct3(row,col:col+blkcols-1));
   end

   
   row = vertshift;
   col = i*blkspace+1;
   
   if(code(i+blkcount) == 1),
      sampdct0(row:row+blkcols-1,col) = sampdct0(row:row+blkcols-1,col)./abs(sampdct0(row:row+blkcols-1,col));
   end
   if(code(i+3*blkcount) == 1),
      sampdct1(row:row+blkcols-1,col) = sampdct1(row:row+blkcols-1,col)./abs(sampdct1(row:row+blkcols-1,col));
   end
   if(code(i+5*blkcount) == 1),
      sampdct2(row:row+blkcols-1,col) = sampdct2(row:row+blkcols-1,col)./abs(sampdct2(row:row+blkcols-1,col));
   end
   if(code(i+7*blkcount) == 1),
      sampdct3(row:row+blkcols-1,col) = sampdct3(row:row+blkcols-1,col)./abs(sampdct3(row:row+blkcols-1,col));
   end
   
end
   	
sampidct0 = idct2(sampdct0);
sampidct1 = idct2(sampdct1);
sampidct2 = idct2(sampdct2);
sampidct3 = idct2(sampdct3);


marksam(1:round(rows/2), 1:round(cols/2))= sampidct0;
marksam(1:round(rows/2), round(cols/2)+1:cols)= sampidct1;
marksam(round(rows/2)+1:rows, 1:round(cols/2))= sampidct2;
marksam(round(rows/2)+1:rows, round(cols/2)+1:cols)= sampidct3;

newimage = uint8(marksam);
imshow(newimage);
imwrite(newimage,output,'tif','compression','none');

