1## Copyright (C) 2007 Søren Hauberg <soren@hauberg.org>
2## Copyright (C) 2012-2014 Carnë Draug <carandraug+dev@gmail.com>
3##
4## This program is free software; you can redistribute it and/or modify it under
5## the terms of the GNU General Public License as published by the Free Software
6## Foundation; either version 3 of the License, or (at your option) any later
7## version.
8##
9## This program is distributed in the hope that it will be useful, but WITHOUT
10## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12## details.
13##
14## You should have received a copy of the GNU General Public License along with
15## this program; if not, see <http://www.gnu.org/licenses/>.
16
17## -*- texinfo -*-
18## @deftypefn  {Function File} {} im2uint16 (@var{img})
19## @deftypefnx {Function File} {} im2uint16 (@var{img}, "indexed")
20## Convert image to uint16.
21##
22## The conversion of @var{img} to a 16-bit unsigned integer, is dependent
23## on the type of input image.  The following input classes are supported
24## for non-indexed images:
25##
26## @table @samp
27## @item int16 or uint8
28## Values are rescaled to the range of the uint16 class [0 65535].
29##
30## @item logical
31## True and false values are assigned a value of 0 and 255 respectively.
32##
33## @item double or single
34## Values are truncated to the interval [0 1] and then rescaled to the range
35## of values of the int16 class [0 255].
36##
37## @item uint16
38## Returns the same image.
39##
40## @end table
41##
42## If the second argument is the string @qcode{"indexed"}, then values are
43## cast to uint16, and a -1 offset is applied if input is
44## a floating point class.  Input checking is performed and an error will
45## be throw is the range of values in uint16 is not enough for all the
46## image indices.
47##
48## @seealso{im2bw, imcast, im2uint8, im2double, im2int16, im2single}
49## @end deftypefn
50
51function imout = im2uint16 (im, varargin)
52  if (nargin < 1 || nargin > 2)
53    print_usage ();
54  elseif (nargin == 2 && ! strcmpi (varargin{1}, "indexed"))
55    error ("im2uint16: second input argument must be the string \"indexed\"");
56  endif
57  imout = imcast (im, "uint16", varargin{:});
58endfunction
59
60%!assert (im2uint16 (uint16 ([1 2 3])), uint16 ([1 2 3]));
61%!assert (im2uint16 (uint8 ([0 127 128 255])), uint16 ([0 32639 32896 65535]));
62%!assert (im2uint16 ([0 0.5 1]), uint16 ([0 32768 65535]));
63%!assert (im2uint16 ([0 1/65535 1.4/65535 1.5/65535 1]), uint16 ([0 1 1 2 65535]));
64%!assert (im2uint16 ([1 2]), uint16 ([65535 65535]));
65%!assert (im2uint16 ([-1 0 0.5 1]), uint16 ([0 0 32768 65535]));
66%!assert (im2uint16 (int16 ([-32768 -1 0 32768])), uint16 ([0 32767 32768 65535]));
67%!assert (im2uint16 ([false true]), uint16 ([0 65535]));
68%!assert (im2uint16 ([true false]), uint16 ([65535 0]));
69
70%!assert (im2uint16 (uint8 ([3 25]), "indexed"), uint16 ([3 25]));
71%!assert (im2uint16 ([1 3 25], "indexed"), uint16 ([0 2 24]));
72
73%!error <indexed> im2uint16 ([0 1 2], "indexed");
74%!error <indexed> im2uint16 (int16 ([17 8]), "indexed");
75%!error <indexed> im2uint16 (int16 ([-7 8]), "indexed");
76%!error <indexed> im2uint16 ([false true], "indexed");
77%!error <range of values> im2uint16 (65537, "indexed");
78
79