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

function conv_im = convolution1dV(in_im, filter_kernel)
  % filter kernel has to be a vertical vector;

  nbh_size = (length(filter_kernel) - 1)/2;
  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(max(nbh_size - conv_i + 1, 0):-1:1,conv_j)', ... % Top mirrored boundary
                   in_im(max(conv_i - nbh_size,1):min(conv_i + nbh_size,m), conv_j)', ...
                   in_im(m:-1:min(m - (conv_i + nbh_size - m) + 1, m + 1),conv_j)']; % Bottom mirrored boundary
      conv_im(conv_i, conv_j) = double(fliplr(orig_vals))*double(filter_kernel);
    end
  end
end
