function result = applyfilter2(img, filter)

    img = double(img);
    result = zeros(size(img));

    % sizes
    [i_height, i_width] = size(img);
    [f_height, f_width] = size(filter);

    % calc a, b
    a = (f_width - 1) / 2;
    b = (f_height - 1) / 2;

    % padded img
    padded_img = padarray(img, [b, a], 'replicate');

   % sliding window
   for i = 1:i_height
       for j = 1:i_width

           window = padded_img(i : i+2*b, j : j+2*a);

           result(i,j) = sum(window .* filter, 'all');
       end
   end

   result = uint8(result);

end

