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

function scaled_im = scale_uint8(im, min_val, max_val)

  % First cast to float to avoid integer division if im is uint8
  im = double(im);

  min_im = min(im(:));
  max_im = max(im(:));

  min_part = min_val*(max_im - im) / (max_im - min_im);
  max_part = max_val*(im - min_im) / (max_im - min_im);

  scaled_im = uint8(round(max_part + min_part));

end

