function [final_message] = jpeg_stego_decode(mat, message_size)

binmessage = '';
imsize = size(mat);
xdim= imsize(1,1);
ydim = imsize(1,2);
count = 1;
finished=0;
for i=1:8:xdim,
    for j=1:8:ydim,        
        temp = double(mat(i:1:min(i+7, xdim), j:1:min(j+7,ydim)));        
        tempsize = size(temp);
        width = tempsize(1,1);
        if width < 8,
            temp(width:8,:) = 0;
        end
        height=tempsize(1,2);
        if height < 8,
            temp(:,height:8) = 0;
        end
        
        B = bdct(temp);

        if count > message_size*8,
            display('Done')
            finished = 1;
            break;
        end
        
        if B(2,3) <= B(4,1),
            binmessage = strcat(binmessage,'0');
        else,
            binmessage = strcat(binmessage,'1');
        end
        
        count = count + 1;
            
    end
    if finished == 1,
        break;
    end;        
end


% get the final message in char's
final_message = '';
for i=1:8:message_size*8,
    num = bin2dec(binmessage(i:i+7));
    final_message = [final_message char(num)];     
end    
