close all; clear all; clc;

[filename,pathname]=uigetfile({'*.jpg;*.tif;*.png;*.pgm;*.bmp;','All Image Files';'*.*','All Files' },'Vyberte obraz');
txt=filename(1:length(filename)-4);
txt=strcat(pathname(1:length(pathname)-9),'Positions\',txt,'.txt');                            
                            
vstup=fopen(txt,'r');
x=fscanf(vstup,'%d',1);
y=fscanf(vstup,'%d',1);
w=fscanf(vstup,'%d',1);
h=fscanf(vstup,'%d',1);
point=zeros(8,2);
for i=1:8
    point(i,:)=fscanf(vstup,'%d',2);
end
fclose(vstup);

pathfile=strcat(pathname,filename);
input=imread(pathfile);

konec=0;
output=input;
subplot(1,2,1), subimage(input);
title('Puvodni obraz');
axis off;
subplot(1,2,2), subimage(output);
title('De-identifikovany obraz');
axis off;
kspo=20;

while(konec==0)
    mm = menu('Vyberte metodu','Zatemneni','Prumerovani','Pixelace','Rozmazani','Negative','Ordinal', ...
                'Random Noise','Treshold','Bar mask','T mask','Mouth only','Mouth only cb', ...
                'Negative cb','Random Noise cb','k-Same','Novy obraz','Ukoncit');

    output=input;
    if ((mm==1) || (mm==3) || (mm==4) || (mm==8) || (mm==9) || (mm==10) || (mm==11) || (mm==12) || (mm==15))
        if ((mm==1) || (mm==8) || (mm==9) || (mm==10) || (mm==11) || (mm==12))
            prompt={'Vyberte cislo mezi 0 a 255'};
        elseif ((mm==3) || (mm==4))
            prompt={'Vyberte cislo mezi 1 a 64'};
        elseif mm==15
            prompt=strcat('Vyberte cislo mezi 1 a ',num2str(kspo));
        end
        name='Vstup pro De-Id';
        numlines=1;
        defaultanswer={'0'};
        numb=inputdlg(prompt,name,numlines,defaultanswer);
        numb=cell2mat(numb);
        numb=str2num(numb);
        defaultanswer=cell2mat(defaultanswer);
        defaultanswer=str2num(defaultanswer);
    else
        numb=1;
        defaultanswer=1;
    end

    switch (mm)
        
        %%Zatemneni
        case {1}
            
            if numb>0 
                output(y:y+h,x:x+w)=numb;
            else
                output(y:y+h,x:x+w)=defaultanswer;
            end
        
        %%Prumerovani
        case {2}
            
            b=filename(1:length(filename)-4);
            b=str2num(b);
            randface=randi(20,1,1);

            while (randface == b)
                randface=randi(20,1,1);
            end

            avejpg=strcat(pathname,num2str(randface),'.jpg');
            avetxt=strcat(pathname(1:length(pathname)-9),'Positions\',num2str(randface),'.txt');

            if exist(avejpg, 'file')
                pr = imread(avejpg);
                pr = uint64(pr);
                output = uint64(output);

                vstup=fopen(avetxt,'r');
                xx=fscanf(vstup,'%d',1);
                yy=fscanf(vstup,'%d',1);
                ww=fscanf(vstup,'%d',1);
                hh=fscanf(vstup,'%d',1);
            else
                fprintf('Soubor %s neexistuje.\n', avejpg);
            end
            fclose(vstup);

            for i=y:y+h
                o=round((i*(hh+yy))/(h+y));
                for j=x:x+w
                    s=round((j*(ww+xx))/(w+x));

                    output(i,j)=(output(i,j)+pr(o,s))/2;

                end
            end
            output=uint8(output);
            
        %%Pixelace
        case {3}
            
            p=numb;

            if p==0
                disp('Cislo musi byt vetsi nez 0');
                continue;
            end

            for i=y:p:y+h
                for j=x:p:x+w
                    output(i:(i+p-1),j:(j+p-1))=ones(p).*mean(mean(output(i:(i+p-1),j:(j+p-1))));
                end
            end
        
        %%Rozmazani
        case {4}
            
            b=numb;
            if b==0
                disp('Cislo musi byt vetsi nez 0');
                continue;
            end
            
            R=zeros(h-y,w-x);
            out2=output;
            u=fspecial('gaussian', size(R), b);
            output=imfilter(output, u);

            out2(y:y+h,x:x+w)=output(y:y+h,x:x+w);
            output=out2;

        %%Negative
        case {5}

            for i=y:y+h
                for j=x:x+w
                    output(i,j)=255-output(i,j);
                end
            end

        %%Ordinal
        case {6}

            for i=y:y+h
                for j=x:x+w
                    if output(i,j)<=51
                        output(i,j)=20;
                    elseif (51<output(i,j)) && (output(i,j)<=102)
                        output(i,j)=80;
                    elseif (102<output(i,j)) && (output(i,j)<=153)
                        output(i,j)=130;
                    elseif (153<output(i,j)) && (output(i,j)<=204)
                        output(i,j)=180;
                    elseif (204<output(i,j)) && (output(i,j)<=255)
                        output(i,j)=220;
                    end
                end
            end

        %%Random_Noise
        case {7}
            
            [m,n]=size(output);
            R=round(rand(m,n));

            for i=y:y+h
                for j=x:x+w
                    if R(i,j)==1
                        output(i,j)=randi(255,1,1);
                    end
                end
            end

        %%Treshold
        case {8}
            
            t=numb;

            for i=y:y+h
                for j=x:x+w
                    if output(i,j)<=t
                        output(i,j)=0;
                    elseif output(i,j)>t
                        output(i,j)=255;
                    end
                end
            end

        %%Bar_mask
        case{9}
            
            if (numb>0)
                output(point(6,2)-20:point(7,2)+20,point(6,1)-25:point(7,1)+25)=numb;
            else
                output(point(6,2)-20:point(7,2)+20,point(6,1)-25:point(7,1)+25)=defaultanswer;
            end

        %%T_mask
        case {10}
            
            if (numb>0)
                output(point(6,2)-20:point(7,2)+20,point(6,1)-25:point(7,1)+25)=numb;
                output(point(6,2)-12:point(1,2)+35,point(1,1)-30:point(1,1)+30)=numb;
            else
                output(point(6,2)-20:point(7,2)+20,point(6,1)-25:point(7,1)+25)=defaultanswer;
                output(point(6,2)-12:point(1,2)+35,point(1,1)-30:point(1,1)+30)=defaultanswer;
            end

        %%Mouth_only
        case {11}
            
            if (numb>0)
                output(y:point(1,2)+30,x:x+w)=numb;
            else
                output(y:point(1,2)+30,x:x+w)=defaultanswer;
            end

        %%Mouth_only_cb
        case {12}
            
            t=128;

            for i=y:y+h
                for j=x:x+w
                    if output(i,j)<=t
                        output(i,j)=0;
                    elseif output(i,j)>t
                        output(i,j)=255;
                    end
                end
            end

            if (numb>0)
                output(y:point(1,2)+30,x:x+w)=numb;
            else
                output(y:point(1,2)+30,x:x+w)=defaultanswer;
            end

        %%Negative_cb
        case {13}

            for i=y:y+h
                for j=x:x+w
                    output(i,j)=255-output(i,j);
                    if output(i,j)<128
                        output(i,j)=0;
                    else
                        output(i,j)=255;
                    end
                end
            end

        %%Random_Noise_cb
        case {14}
            
            [m,n]=size(output);
            R=round(rand(m,n));

            for i=y:y+h
                for j=x:x+w
                    if R(i,j)==1
                        output(i,j)=255 - 255*round(rand(1));
                    end
                end
            end

        %%k-Same
        case{15}
            
            k=numb; 

            if k==0
                disp('Cislo musi byt vetsi nez 0');
                continue;
            elseif k>kspo
                fprintf('Cislo musi byt mensi nez %i\n',kspo);
                continue;
            end
            
            if k==1
                continue;
            end

            out = uint64(output);

            os=zeros(kspo,1);
            first=str2num(filename(1:length(filename)-4));

            for f=1:kspo
                
                if ((first==1)&&(k==1))
                    continue;
                end

                kface=strcat(pathname,num2str(f),'.jpg');

                if exist(kface, 'file')
                    kk = imread(kface);
                    kk = uint64(kk);

                    txt2=strcat(pathname(1:length(pathname)-9),'Positions\',num2str(f),'.txt');
                    vstup=fopen(txt2,'r');
                    xx=fscanf(vstup,'%d',1);
                    yy=fscanf(vstup,'%d',1);
                    ww=fscanf(vstup,'%d',1);
                    hh=fscanf(vstup,'%d',1);
                else
                    fprintf('Soubor %s neexistuje.\n', pp);
                end
                fclose(vstup);

                for i=y:y+h
                    o=round((i*(hh+yy))/(h+y));
                    for j=x:x+w
                        s=round((j*(ww+xx))/(w+x));

                        if kk(o,s)>out(i,j)
                            os(f,1) = os(f,1) + (kk(o,s)-out(i,j))^2;
                        elseif kk(o,s)<out(i,j)
                            os(f,1) = os(f,1) + (out(i,j)-kk(o,s))^2;
                        elseif kk(o,s)==out(i,j)
                            os(f,1) = os(f,1);
                        end
                    end
                end
            end

            os=sqrt(os);
            oss=sort(os);
            r=0;
            a=1; k=k+1;

            for f=1:k

                for p=1:kspo
                    if ((oss(f,1)==0) && (os(p,1)==0))
                        break;
                    end
                    if oss(f,1)==os(p,1)
                        pp = strcat(pathname,num2str(p),'.jpg');
                        txt2=strcat(pathname(1:length(pathname)-9),'Positions\',num2str(p),'.txt');
                        r=p;
                        break;
                    end
                end

                if r==0 
                    continue;
                end

                if exist(pp, 'file')    
                    for pic=a:k+1
                        a=a+1;
                        eval(['picture' num2str(pic) '=imread(pp);']);
                        eval(['picture' num2str(pic) '=uint64(picture' num2str(pic) ');']);
                        ksameloc=strcat(pathname(1:length(pathname)-9),'KSAME\');
                        copyfile(txt2,ksameloc);
                        txta=strcat(ksameloc,num2str(p),'.txt');
                        txtb=strcat(ksameloc,num2str(pic+1000),'.txt');
                        movefile(txta,txtb);
                        break;
                    end
                end
            end
            k=k-1;

            for u=1:k
                txtf=strcat(ksameloc,num2str(u+1000),'.txt');
                vstup=fopen(txtf,'r');
                xx=fscanf(vstup,'%d',1);
                yy=fscanf(vstup,'%d',1);
                ww=fscanf(vstup,'%d',1);
                hh=fscanf(vstup,'%d',1);
                fclose(vstup);
                for i=y:y+h
                    o=round((i*(hh+yy))/(h+y));
                    for j=x:x+w
                        s=round((j*(ww+xx))/(w+x));
                        
                        eval(['out(i,j)=(out(i,j)+picture' num2str(u) '(o,s));']);
                    end
                end
            end
            out=out./k;
            delpath=strcat(ksameloc,'*.txt');
            delete(delpath);
            output=uint8(out);

        %%Novy_obraz
        case {16}

            clear all;close all;
            
            [filename,pathname]=uigetfile({'*.jpg;*.tif;*.png;*.pgm;*.bmp;','All Image Files';'*.*','All Files' },'Vyberte obraz');
            txt=filename(1:length(filename)-4);
            txt=strcat(pathname(1:length(pathname)-9),'Positions\',txt,'.txt');
            
            vstup=fopen(txt,'r');
            x=fscanf(vstup,'%d',1);
            y=fscanf(vstup,'%d',1);
            w=fscanf(vstup,'%d',1);
            h=fscanf(vstup,'%d',1);
            point=zeros(8,2);
            for i=1:8
                point(i,:)=fscanf(vstup,'%d',2);
            end
            fclose(vstup);
            
            pathfile=strcat(pathname,filename);
            input=imread(pathfile);
            konec=0;
            output=input;
            kspo=20;

        %%Ukoncit_program
        case {17}
            close all;
            konec=1;

    end

    if konec==0
        subplot(1,2,1), subimage(input);
        title('Puvodni obraz');axis off;
        subplot(1,2,2), subimage(output);
        title('De-identifikovany obraz');axis off;
    end
end

