%pkg install "image-2.4.1.tar.gz";
%pkg install "control-2.8.5.tar.gz";
%pkg install "signal-1.3.2.tar.gz";
pkg load all;

function bitstr = toBits(str)
  bytestr = toascii(str);
  [q, anz] = size(bytestr);
  bitstr = [];
  for I=1:16
    bitstr = [bitstr, 0];
  endfor
  for I=1:16
    bitstr = [bitstr, 1];
  endfor
  for I=1:anz
    val = bytestr(I);
    for J=1:8
      if val >= 2^(8-J)
        val = val-2^(8-J);
        bitstr = [bitstr, 1];
      else
        bitstr = [bitstr, 0];
      endif
    endfor
  endfor
  for I=1:8
    bitstr = [bitstr, 0];
  endfor
endfunction

function str = toString(bitstr)
  [q, maxanz] = size(bitstr);
  str='';
  subbytsstr = [];
  start = 1;
  startok = 0;
  while startok<32 && start < maxanz
    while startok<16 && start < maxanz
      if bitstr(start)==0
        startok++;
      else
        startok=0;
      endif
      start++;
    endwhile
    while startok>=16 && startok<32 && start < maxanz
      if bitstr(start)==1
        startok++;
      else
        startok=0;
      endif
      start++;
    endwhile
  endwhile
  aktpos = start;
  while aktpos <= maxanz
    nextA = 0;
    anzN = 0;
    subA = [];
    while nextA<8 && aktpos <= maxanz
      subA = [subA, bitstr(aktpos)];
      if bitstr(aktpos)==0
        anzN++;
      else
        anzN=0;
      endif
      nextA++;
      aktpos++;
    endwhile
    if anzN==8 
      aktpos=maxanz+1;
    elseif nextA != 8 
      aktpos=maxanz+1;
    else
      byte = 0;
      for I=1:8
        byte=2*byte + subA(I);
      endfor
      subbytsstr = [subbytsstr, byte];
    endif
  endwhile
  [q, anzchar] = size(subbytsstr);
  if anzchar>0
    str = char(subbytsstr);
  endif
endfunction

function WMWork = imbedBits(im, bitSeq, pos)
  [q, bitNum] = size(bitSeq);
  [maxX, maxY, maxZ] = size(im);
  addMat = zeros(maxX,maxY,1);
  z=1;
  for x=1:maxX
    for y=1:maxY
      if bitSeq(z) == 1
        addMat(x,y,1)=1;
      endif
      z++;
      if z>bitNum
        z=1;
      endif
    endfor
  endfor
  WMWork(:,:,:) = im(:,:,:);
  WMWork(:,:,pos) = (im(:,:,pos).-mod(im(:,:,pos),2)).+addMat(:,:,1);
endfunction

function bitSeq = getBits(im, pos)
  bitSeq = [];
  [maxX, maxY, maxZ] = size(im);
  for x=1:maxX
    for y=1:maxY
      bitSeq = [bitSeq, mod(im(x,y,pos),2)];
    endfor
  endfor
endfunction

function diffInPng = testPng(im, pos)
  [orghight, orgwidth, zmax] = size(im);
  randMat = unidrnd(2,orghight, orgwidth,1).-1;
  imTest_org=im;
  imTest_org(:,:,pos) = (imTest_org(:,:,pos).-mod(imTest_org(:,:,pos),2)).+randMat(:,:,1);
  imwrite(imTest_org,'test_image.png');%,'Quality',100);
  imTest_new=imread('test_image.png');
  diff_test = (imTest_org(:,:,:)-imTest_new(:,:,:)) != 0;
  diffInPng = 0;
  for J=1:orghight
    for K=1:orgwidth
      for L=1:zmax
        if diff_test(J,K,L) != 0
           diffanz++;
        endif
      endfor
    endfor
  endfor
endfunction

clear;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% The following part can be used to embedd some text in 
%% an png-image. The source image has to be specified by
%% "imgInPath", the destination by "imgOutPath" and the message
%% has to be specified by "message". 
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% imgInPath   = argv(){1};
% imgOutPath  = argv(){2};
% message     = argv(){3};
% pos         = 1;
%
% disp(["message is : " message]);
%
%% read the image
% disp("read the image ...");
% img = imread(imgInPath);
%
%% covert the message to a bit string
%disp("convert the message ...");
%bitStr = toBits(message);
%
%% embed the bitstring
%disp("embed the bit-string ...");
%WMWork = imbedBits(img, bitStr, pos);
%
%% write image to file
%disp("write image to file ...");
%imwrite(WMWork, imgOutPath);
%
%disp("finished.");
%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% The following part can be used to read the embedded text 
%% from an png-image if the text has been embedded by the 
%% embedding procedure of this octave program. The source 
%% image has to be specified by "imgPath". 
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

imgPath     = 'test01.png';
pos         = 1;

% reading image from file
disp("reading image from file ...");
WMWork=imread(imgPath);

% read text from image
disp('extract bit-string of the embedded text');
bitseq = getBits(WMWork, pos);
disp('convert bit-string to text');
str = toString(bitseq);
disp('the embedded text is: ');
disp(str);

break;