1## Copyright (C) 2000 Kai Habel <kai.habel@gmx.de>
2## Copyright (C) 2011, 2015 Carnë Draug <carandraug+dev@gmail.com>
3##
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 3 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, see <http://www.gnu.org/licenses/>.
16
17## -*- texinfo -*-
18## @deftypefn {Function File} {} isgray (@var{img})
19## Return true if @var{img} is a grayscale image.
20##
21## A variable can be considered a grayscale image if it is a non-sparse,
22## real array of size @nospell{MxNx1xK}, and:
23##
24## @itemize @bullet
25## @item of floating point class with values in the [0 1] range or NaN;
26## @item of class uint8, uint16, or int16.
27## @end itemize
28##
29## @emph{Note}: despite their suggestive names, the functions isbw,
30## isgray, isind, and isrgb, are ambiguous since it is not always possible
31## to distinguish between those image types.  For example: an uint8 matrix
32## can be both a grayscale and indexed image; a grayscale image may have
33## values outside the range [0 1].  They are good to dismiss input as an
34## invalid image type, but not for identification.
35##
36## @seealso{gray2ind, isbw, isind, isrgb}
37## @end deftypefn
38
39function bool = isgray (img)
40  if (nargin () != 1)
41    print_usage ();
42  endif
43
44  bool = false;
45  if (isimage (img) && ndims (img) < 5 && size (img, 3) == 1)
46    if (isfloat (img))
47      bool = ispart (@is_float_image, img);
48    elseif (any (isa (img, {"uint8", "uint16", "int16"})))
49      bool = true;
50    endif
51  endif
52endfunction
53
54%!assert (isgray ([0 0 1; 1 0 1]), true)
55%!assert (isgray (zeros (3)), true)
56%!assert (isgray (ones (3)), true)
57
58%!test
59%! a = rand (10);
60%! assert (isgray (a), true);
61%! a(5, 5) = 2;
62%! assert (isgray (a), false);
63
64%!test
65%! a = uint8 (randi (255, 10));
66%! assert (isgray (a), true);
67%! a = int8 (a);
68%! assert (isgray (a), false);
69
70%!test
71%! a = rand (10);
72%! a(50) = NaN;
73%! assert (isgray (a), true);
74
75%!assert (isgray (rand (5, 5, 1, 4)), true);
76%!assert (isgray (rand (5, 5, 3, 4)), false);
77%!assert (isgray (rand (5, 5, 3)), false);
78%!assert (isgray (rand (5, 5, 1, 3, 4)), false);
79
80%!assert (isgray (rand (5, "single")), true)
81
82## While having some NaN is ok, having all NaN is not
83%!assert (isgray ([.1 .2 .3; .4 NaN .6; .7 .8 .9]), true)
84%!assert (isgray ([.1 .2 .3; NA NaN .6; .7 .8 .9]), true)
85%!assert (isgray ([.1 .2 .3; NA  .5 .6; .7 .8 .9]), true)
86%!assert (isgray (NaN (5)), false)
87%!assert (isgray (NA (5)), false)
88