function [out] = patchBasedInpainting(pic,mask,patchsize,searchdist,alpha,beta,gamma)
if gamma > 1 || 0 > gamma
error("Gamma must be between zero and one!");
if (alpha+beta) > 1 || 0 > (alpha+beta)
error("Sum of alpha and beta must be between zero and one!");
if ~exist("patchsize","var")
if isEven(patchsize) || (patchsize < 3)
error("The Patchsize is not allowed to be odd!");
if ~exist("searchdist","var")
searchdist = searchdist + ps;
Phi = pic .* uint8(~mask);
barmax = sum(Omega,"all");
bartext = "Patchbased Inpainting in Iteration: ";
bar = waitbar(0,append(bartext,string(0)));
deltaOmega = zeros(sum(uint64(Omega),'all')*2,2);
if sum(Omega(i-1:i+1,j-1:j+1),"all") < 9
deltaOmega(at,:) = [i,j];
deltaOmega = deltaOmega(1:at-1,:);
patch = Cmap(py-ps:py+ps,px-ps:px+ps);
Cp = sum(patch,"all") / patchsize^2;
RCp = (1-gamma) * Cp + gamma;
Pp = alpha * RCp + beta * Dp;
Phi_p = Phi(pixel_y-ps:pixel_y+ps,pixel_x-ps:pixel_x+ps,:);
Phi_p = Phi(pixel_y-ps:pixel_y+ps,pixel_x-ps:pixel_x+ps);
Omega_p = Omega(pixel_y-ps:pixel_y+ps,pixel_x-ps:pixel_x+ps);
for i = pixel_y - searchdist : pixel_y + searchdist
for j = pixel_x - searchdist : pixel_x + searchdist
if (j+ps > x || i-ps < 1 ||j-ps < 1 || i+ps > y)
Q_mask = Omega(i-ps:i+ps,j-ps:j+ps);
Phi_q = Phi(i-ps:i+ps,j-ps:j+ps,:);
Phi_q = Phi(i-ps:i+ps,i-ps:i+ps);
err = immse(Phi_q,Phi_p);
if Omega(pixel_y-ps+i,pixel_x-ps+j) == 1
Omega(pixel_y-ps+i,pixel_x-ps+j) = 0;
Phi(pixel_y-ps+i,pixel_x-ps+j,:) = Phi(best_y-ps+i,best_x-ps+j,:);
Phi(pixel_y-ps+i,pixel_x-ps+j) = Phi(best_y-ps+i,best_x-ps+j);
Cmap(pixel_y-ps+i,pixel_x-ps+j) = cForMaxP;
iterationcounter = iterationcounter + 1;
remainingPixels = sum(Omega,"all");
waitbar((barmax-remainingPixels)/barmax,bar,append(bartext,string(iterationcounter)));