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} =} imdivide (@var{a}, @var{b})
18## @deftypefnx {Function File} {@var{out} =} imdivide (@var{a}, @var{b}, @var{class})
19## Divide image by another image or constant.
20##
21## If @var{a} and @var{b} are two images of same size and class, @var{a} is divided
22## by @var{b}. Alternatively, if @var{b} is a floating-point scalar, @var{a} is divided
23## 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, immultiply, imlincomb, imsubtract}
32## @end deftypefn
33
34function img = imdivide (img, val, out_class = class (img))
35
36  if (nargin < 2 || nargin > 3)
37    print_usage;
38  endif
39  [img, val] = imarithmetics ("imdivide", img, val, out_class);
40
41  ## matlab doesn't even gives a warning in this situation, it simply returns
42  ## a double precision float
43  if (nargin > 2 && strcmpi (out_class, "logical"))
44    warning ("Ignoring request to return logical as output of division.");
45  endif
46
47  warning ("off", "Octave:divide-by-zero", "local");
48  img = img ./ val;
49
50endfunction
51
52%!assert (imdivide (uint8   ([23 250]), uint8   ([ 2  50])),            uint8   ([ 12   5])); # default to first class
53%!assert (imdivide (uint8   ([56 255]), uint8   ([ 0   0])),            uint8   ([255 255])); # dividing by zero works (tested in matlab)
54%!assert (imdivide (uint8   ([23 250]), 2),                             uint8   ([ 12 125])); # works subtracting a scalar
55%!assert (imdivide (uint8   ([23 250]), uint8   ([ 2  50]), "uint16"),  uint16  ([ 12   5])); # defining output class works (not in matlab)
56%!assert (imdivide (logical ([1 1 0 0]), logical ([1 0 1 0])),          double  ([1 Inf 0 NaN])); # dividing logical matrix  (tested in matlab)
57%!fail  ("imdivide (uint8   ([23 250]), uint16  ([23 250]))");                                # input needs to have same class
58