1## Copyright (C) 2011 Carnë Draug <carandraug+dev@gmail.com>
2##
3## This program is free software; you can redistribute it and/or modify it under
4## the terms of the GNU General Public License as published by the Free Software
5## Foundation; either version 3 of the License, or (at your option) any later
6## version.
7##
8## This program is distributed in the hope that it will be useful, but WITHOUT
9## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11## details.
12##
13## You should have received a copy of the GNU General Public License along with
14## this program; if not, see <http://www.gnu.org/licenses/>.
15
16## -*- texinfo -*-
17## @deftypefn {Function File} {@var{out} =} immultiply (@var{a}, @var{b})
18## @deftypefnx {Function File} {@var{out} =} immultiply (@var{a}, @var{b}, @var{class})
19## Multiply image by another image or constant.
20##
21## If @var{a} and @var{b} are two images of same size and class, the images are
22## multiplied. Alternatively, if @var{b} is a floating-point scalar, @var{a} is
23## multiplied by it.
24##
25## The class of @var{out} will be the same as @var{a} unless @var{a} is logical
26## in which case @var{out} will be double. Alternatively, it can be
27## specified with @var{class}.
28##
29## @emph{Note}: the values are truncated to the mininum value of the output
30## class.
31## @seealso{imabsdiff, imadd, imcomplement, imdivide, imlincomb, imsubtract}
32## @end deftypefn
33
34function img = immultiply (img, val, out_class = class (img))
35
36  if (nargin < 2 || nargin > 3)
37    print_usage;
38  endif
39  [img, val] = imarithmetics ("immultiply", img, val, out_class, nargin);
40
41  ## have to check how matlab behaves in this situation. Their documentation
42  ## does not say anything but add and subtract silently ignore it and return
43  ## double anyway. This may be done in the call to imarithmetics
44  if (nargin > 2 && strcmpi (out_class, "logical"))
45    warning ("Ignoring request to return logical as output of multiplication.");
46  endif
47
48  img = img .* val;
49
50endfunction
51
52%!assert (immultiply (uint8   ([255 50]), uint16  ([300 50])),           uint8  ([255 255]));  # default to first class and truncate
53%!assert (immultiply (uint8   ([250 50]), uint16  ([  3  4]), "uint32"), uint32 ([750 200]));  # defining output class works (not in matlab?)
54%!assert (immultiply (uint8   ([255 50]),                  4),           uint8  ([255 200]));  # works multiplying by a scalar
55%!assert (immultiply (logical ([  1  0]), uint16  ([300 50])),           uint16 ([300   0]));  # output class defaults to whatever input is not logical
56%!assert (immultiply (logical ([  1  0]), logical ([  1  1])),           double ([  1   0]));  # tested on matlab for compatibility
57