% ::::::::::::::::::::::::::::::::::::::::::::::::::::::: %
% Solution proposal to programming exercises week 7       %
%                                                         %
% INF2310, spring 2017                                    %
%                                                         %
% Ole-Johan Skrede, 18.02.2017                            %
% ::::::::::::::::::::::::::::::::::::::::::::::::::::::: %

function conv_im = convolution1dH(in_im, filter_kernel)
  %  filter_kernel has to be a vertical vector
  nbh_size = (length(filter_kernel) - 1)/2; % Assumes odd filter kernel length
  conv_im = zeros(size(in_im));
  [m,n] = size(in_im);
  new_val = 0;
  filter_val = 0;
  orig_im_val = 0;
  im_i = 0;
  im_j = 0;

  % Mirrored boundary treatment
  for conv_i = 1:m
    for conv_j = 1:n
      orig_vals = [in_im(conv_i,max(nbh_size - conv_j + 1, 0):-1:1), ... % left mirrored boundary
                   in_im(conv_i,max(conv_j - nbh_size, 1):min(conv_j + nbh_size, n)), ...
                   in_im(conv_i,n:-1:min(n - (conv_j + nbh_size - n) + 1, n + 1))]; % right mirrored boundary
      conv_im(conv_i, conv_j) = double(orig_vals) * double(fliplr(filter_kernel))';
    end
  end
end
